Seed AI RNG deterministically to prevent replay desync - #1956
Conversation
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.
There is no AI in replays. So how exactly have you seen errors? |
| } | ||
| } | ||
| auto prng = std::mt19937(std::random_device()()); | ||
| auto& prng = AI::getRandomGenerator(); |
There was a problem hiding this comment.
This one looks correct though
In my own fork I use ai-battle with a small eval framework to improve the AI: |
|
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. |
|
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. |
Problem
The AI uses a non-deterministic random source, which breaks replay determinism. Two distinct issues:
AI::getRandomGenerator()is never seeded from the game seed. It is lazily seeded fromstd::random_deviceon 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.AIPlayerJH::TrySeaAttack()allocates its own generator —std::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
AI::getRandomGenerator()fromrandom_initinGameClient::StartGame(), right whereRANDOM.Init(random_init)is already called — so replay and live runs share the same AI RNG sequence.std::mt19937inTrySeaAttack()with the sharedAI::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.