Skip to content

Fix MMAI crash with simultaneous neutral battles - #2

Draft
starkdmi wants to merge 6 commits into
developfrom
ai/mmai-concurrent-battles-1.7.5-d935
Draft

starkdmi wants to merge 6 commits into
developfrom
ai/mmai-concurrent-battles-1.7.5-d935

Conversation

@starkdmi

@starkdmi starkdmi commented Sep 3, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes a host crash when multiple human players fight separate neutral armies concurrently during simultaneous turns with MMAI as the neutral combat AI.

This branch includes origin/develop with the Router fix forward-ported.

Root cause: MMAI::BAI::Router stored a single bai pointer. Starting battle B overwrote battle A's delegated BAI, so later callbacks for battle A were delivered to BAI(B).

Fix

  • Per-BattleID map of delegated BAI instances.
  • All BattleID-scoped callbacks dispatch through withBattle().
  • battleEnd(bid) delegates, then erases only that battle.
  • Duplicate battleStart(bid) is an invariant failure.
  • Unknown BattleIDs in callback/battleEnd paths log the requested and active IDs and return.
  • Per-battle log tags are applied locally.
  • Router class layout is identical in every TU (test factory hook is always present). ENABLE_MMAI_TEST is no longer a compile definition on the MMAI library, so AIFactory::createBattleAI("MMAI") cannot allocate the wrong size.

Tests

MMAI_router_test (8 cases): sequential traces, concurrent isolation, both end orders, unknown ID isolation, post-end isolation, duplicate battleStart, and factory construction via AIFactory::createBattleAI("MMAI").

cmake --build build --target MMAI_router_test
./build/MMAI_router_test
# 8/8 passed

Compatibility

Host-only MMAI Router change. No network/save/protocol changes.

Open in Web Open in Cursor 

cursoragent and others added 4 commits September 3, 2026 16:20
Replace the single delegated BAI pointer with a per-BattleID map so
simultaneous neutral battles each keep their own BAI instance.

- Add getBattleAI() with explicit errors for unknown BattleIDs
- Reject duplicate battleStart for the same BattleID
- Erase only the ended battle in battleEnd()
- Extract createDelegatedBAI() to preserve fallback and NN paths

Co-authored-by: Dmitry Starkov <starkdmi@users.noreply.github.com>
Introduce MMAI_router_test with mock BAI instances verifying that
callbacks are routed by BattleID, battleEnd cleanup is isolated, and
invalid/duplicate BattleIDs fail with clear errors.

Co-authored-by: Dmitry Starkov <starkdmi@users.noreply.github.com>
Resolve Router conflicts by keeping per-BattleID routing while adopting
develop's logging (logtag/basetag), siege model keys, and BAI factory.
Store logtag per battle so concurrent model versions do not overwrite
each other. Adjust MMAI_router_test for the OBJECT-library layout.

Co-authored-by: Dmitry Starkov <starkdmi@users.noreply.github.com>
MMAI is an OBJECT library on develop, so the router test cannot link
MMAI/vcmiMain objects directly. Defer creating MMAI_router_test until
the facade exists and link libvcmi instead.

Co-authored-by: Dmitry Starkov <starkdmi@users.noreply.github.com>
@cursor cursor Bot changed the title Fix MMAI crash with simultaneous neutral battles (1.7.5) Fix MMAI crash with simultaneous neutral battles Sep 3, 2026
cursoragent and others added 2 commits September 3, 2026 18:15
Stop throwing on unknown BattleIDs in callback paths so a post-battleEnd
event cannot abort the host; log the missing ID and leave other battles
alone. Keep duplicate battleStart as an invariant failure.

Use per-battle log tags locally instead of mutating Router-wide logtag.
Strengthen routing tests to assert full event traces, no cross-talk after
end, and empty Router state when both battles finish.

Co-authored-by: Dmitry Starkov <starkdmi@users.noreply.github.com>
The test factory member was gated on ENABLE_MMAI_TEST only in MMAI,
so AIFactory's make_shared<Router>() allocated a smaller object than the
MMAI constructor wrote. Always declare the factory hook on Router and
stop defining ENABLE_MMAI_TEST on the MMAI target.

Add a factory construction regression test via createBattleAI("MMAI").

Co-authored-by: Dmitry Starkov <starkdmi@users.noreply.github.com>
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