Skip to content

Seed AI RNG deterministically to prevent replay desync - #1956

Open
FlexApex wants to merge 1 commit into
Return-To-The-Roots:masterfrom
FlexApex:fix-ai-rng-determinism
Open

Seed AI RNG deterministically to prevent replay desync#1956
FlexApex wants to merge 1 commit into
Return-To-The-Roots:masterfrom
FlexApex:fix-ai-rng-determinism

Conversation

@FlexApex

Copy link
Copy Markdown

Problem

The AI uses a non-deterministic random source, which breaks replay determinism. Two distinct issues:

  1. AI::getRandomGenerator() is never seeded from the game seed. It is lazily seeded from std::random_device on first use, so in replay mode it starts from a different state than during the original match. Any AI decision that draws from it diverges, desyncing the replay.
  2. AIPlayerJH::TrySeaAttack() allocates its own generatorstd::mt19937(std::random_device()()) — on every call, which is non-deterministic by construction.

The net effect: any replay in which an AI evaluates a sea attack desyncs ("The played replay is not in sync with the original match"), even though the simulation is otherwise correct.

Fix

  • Seed AI::getRandomGenerator() from random_init in GameClient::StartGame(), right where RANDOM.Init(random_init) is already called — so replay and live runs share the same AI RNG sequence.
  • Replace the local std::mt19937 in TrySeaAttack() with the shared AI::getRandomGenerator().

3 lines across 2 files; no behavioural change to a live game beyond making the AI RNG reproducible.

How it was found

Surfaced while running headless replay verification on AI-battle recordings (related to the headless replay player in #1950) — a desync verifier flagged AI sea-attack frames as the first point of divergence, with the RNG checksum being the differing field.

The AI used two separate non-deterministic random sources:
- AI::getRandomGenerator() in random.cpp was seeded with std::random_device
  on first use, so it started at a different state in replay mode than during
  the original game run, causing AI decisions to diverge
- CheckSeaAttack in AIPlayerJH.cpp allocated its own std::mt19937 seeded
  with std::random_device()() every call, doubly breaking determinism

Fix: seed AI::getRandomGenerator() from the game's random_init at StartGame
(same point where RANDOM.Init is called), so replay and live game use the same
AI RNG sequence. Replace the local mt19937 with the shared AI generator.
@Flamefire

Copy link
Copy Markdown
Member

any replay in which an AI evaluates a sea attack desyncs

There is no AI in replays. So how exactly have you seen errors?

}
}
auto prng = std::mt19937(std::random_device()());
auto& prng = AI::getRandomGenerator();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one looks correct though

@FlexApex

Copy link
Copy Markdown
Author

any replay in which an AI evaluates a sea attack desyncs

There is no AI in replays. So how exactly have you seen errors?

In my own fork I use ai-battle with a small eval framework to improve the AI:
See recent commits here: https://github.com/FlexApex/s25client/commits/master
There it lead to an .rpl desync.

@Flamefire

Copy link
Copy Markdown
Member

Can you explain a bit what you are doing there so I don't have to search your code? Because replays replay recorded actions. So there is no AI running there doing actions and hence no possibility for asyncs in replays.

Note that this gives a false impression of safety: You seed the AI at the start. Then you save the game. The AI actions from the continued game at that point and from the loaded game won't match.
It does look like they will match for replays if the AI would do actions there, which it doesn't (currently).

@FlexApex

Copy link
Copy Markdown
Author

I use extras/ai-battle for self-play benchmarking (challenger vs baseline over maps and seeds), record those games with --replay, and play the recordings back headless through the GameClient path, using the headless replay player from #1950 to compare the stored checksums.

Those replays do desync, but not because of the AI, you are right. HeadlessGame's ctor does MapLoader::Load then InitAfterLoad, while GameClient::StartGame does MakeStartPacts, Load, SetupResources, InitAfterLoad. So playback happens on a world with different resources (and different pacts when teams are used) and the rand checksum diverges early. So ai-battle recordings aren't replayable in the client at all right now.

So this PR isn't a desync fix. What's left is reproducibility. TrySeaAttack() built a std::mt19937(std::random_device()()) per call and never touched AI::getRandomGenerator(), which a36241c seems to have missed, so not even ai-battle's --random_ai_init (147d082) reaches it.

About the savegame: agreed. It's the same guarantee RANDOM has though. random_init is generated fresh at every start, no RNG state goes into the savegame, and AI players are rebuilt from the world on load, so continuing vs save+load can't match anyway. Seeding the AI generator from random_init just puts it under the same rule. Doing it properly would mean serialising the AI RNG and the AI state into the save, which I'd leave out of scope.

Happy to drop the StartGame part and keep only the TrySeaAttack fix, rewrite the title and description for both, or close this. Whatever you prefer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants