[GH-ISSUE #821] [Issue]: Game Mode Start Game race re-fires GM intro generation (regression of #321 with shifted symptom) #397

Closed
opened 2026-10-20 17:28:24 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @Romuromylus on GitHub (May 14, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/821

Originally assigned to: @Romuromylus on GitHub.

Summary

Game Mode regression: clicking Start Game sometimes snaps back to the idle Start Game screen mid-stream and a second click fires another GM intro generation. This is the same underlying race as #321 (which was fixed by #320), but the user-visible shape has shifted — the chat ends up with two role: "assistant" GM intro messages back-to-back with no user message between them, instead of two [Start the game] user messages. The chat still effectively starts at "scene 2".

Expected behavior

After clicking Start Game once on a freshly-set-up Game Mode chat, the intro typewriter plays through, the GM streams the opening scene, the Continue button replaces the Start screen, and the chat contains exactly one assistant GM turn.

Actual behavior

After clicking Start Game:

  1. Intro typewriter / generation indicator appears (correct).
  2. Mid-stream the screen falls back to the idle Start Game screen with the big primary Start Game button visible again.
  3. User clicks Start Game a second time.
  4. Chat persists two assistant GM messages back-to-back with no user message between them — the second one continues the scene as if the first never invited player input.
  5. gameNarrationIndex stays at 0 so the user only sees the first message in the live narration UI, but the duplicate is visible in Logs.

Steps to reproduce

  1. Create a new Game Mode chat and run through /game/setup (any genre / setting / tone).
  2. Wait for the Start Game button to appear.
  3. Click Start Game.
  4. Observe: typewriter starts, then mid-stream the surface drops back to the idle Start Game button.
  5. Click Start Game a second time.
  6. Open Logs — you'll see two GM assistant turns, with no user message between.

Reproduced twice in a row on 1.5.9 against a real game setup. Network-trace evidence (next section) confirms two POST /api/game/start calls both returned 200, meaning the server saw gameSessionStatus === "ready" at both call times — even though the first call had just written "active".

Environment

  • Marinara Engine version: 1.5.9
  • Install type: source (Docker, deployed to EasyPanel)
  • OS + version: Linux (Docker container, host: Linode VPS)
  • Browser or app shell: Chromium-based

Logs, screenshots, or video

Client-side fetch interceptor captured this sequence around the bug (timestamps UTC, dur is the resolve time for fetch — for /api/generate that's just SSE headers, not the full stream):

10:39:23.625 POST 200   126ms  /api/game/start         ← 1st click, server writes status="active"
10:39:23.762 POST 200   174ms  /api/generate           ← 1st intro generation begins streaming
10:40:01.638 POST 200 37904ms  /api/game/scene-wrap    ← scene analysis after 1st intro saves
10:40:01.637 POST 200   129ms  /api/game/weather/update
10:40:02.629 PUT  200   136ms  /api/game/<id>/widgets
10:40:05.739 PUT  200   131ms  /api/game/<id>/widgets
10:40:05.871 GET  200   134ms  /api/chats/<id>         ← client refetch returns status="ready" !!
10:40:05.877 POST 200   187ms  /api/game/start         ← 2nd click ACCEPTED (server saw "ready")
10:40:06.073 POST 200   225ms  /api/generate           ← 2nd intro fires, second assistant msg gets saved

Server-side independent verification: probing POST /api/game/start against the same chat right now (after the bug, chat still in gameSessionStatus: "active") returns 500 — confirming the meta.gameSessionStatus !== "ready" guard at packages/server/src/routes/game.routes.ts:3464 works correctly. The only way the 2nd call during the bug returned 200 is that some other handler wrote "ready" back to the DB between the two /game/start calls.

Additional context

PR #320 audited packages/server/src/routes/generate.routes.ts and converted 7 stale-meta writes (preset entry-state-overrides, lorebook entry-state-overrides x3, lastMapPosition, game-state-patch gameMap sync, chat_summary, mariContext) to the freshChat → freshMeta → field-only update pattern. All 6 remaining chats.updateMetadata(input.chatId, …) sites in generate.routes.ts use re-fetched meta and look safe.

The remaining race must be in another handler that runs concurrently with the stream and still uses {...meta, …} from an early read. Likely candidates in packages/server/src/routes/game.routes.ts that wrote during the trace's race window (10:39:23 → 10:40:05):

  • app.post("/weather/update") at game.routes.ts:5440 — synchronous read+write, but worth confirming.
  • app.put("/:chatId/widgets") at game.routes.ts:5629 — same pattern, fires twice in the trace.
  • Other writes inside app.post("/scene-wrap") (game.routes.ts:6065) and app.post("/generate-assets") (game.routes.ts:6830) appear to use latestMeta / freshMeta correctly.

A separate audit pass over every chats.updateMetadata(…, { ...meta, … }) in packages/server/src/routes/game.routes.ts (and ideally migration of partial updates to the existing chats.patchMetadata(…) helper at packages/server/src/services/storage/chats.storage.ts:303, which uses a per-chat queue and is race-safe) is the proper root-cause fix.

Proposed defensive fix in this issue: add a second guard to /api/game/start that rejects the call when the chat already contains a non-empty role: "assistant" message — making the duplicate-intro symptom impossible regardless of which exact route is clobbering the status. Even a fully successful /game/start race becomes a no-op error toast on the second click, rather than spending a paid generation on a second intro. The deeper updateMetadata audit can be a separate PR.

Reference to original issue: #321. Reference to original fix attempt: #320.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (May 14, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/821 Originally assigned to: @Romuromylus on GitHub. ## Summary Game Mode regression: clicking **Start Game** sometimes snaps back to the idle Start Game screen mid-stream and a second click fires another GM intro generation. This is the same underlying race as #321 (which was fixed by #320), but the user-visible shape has shifted — the chat ends up with **two `role: "assistant"` GM intro messages back-to-back with no user message between them**, instead of two `[Start the game]` user messages. The chat still effectively starts at "scene 2". ## Expected behavior After clicking **Start Game** once on a freshly-set-up Game Mode chat, the intro typewriter plays through, the GM streams the opening scene, the **Continue** button replaces the Start screen, and the chat contains exactly one assistant GM turn. ## Actual behavior After clicking **Start Game**: 1. Intro typewriter / generation indicator appears (correct). 2. Mid-stream the screen falls back to the idle Start Game screen with the big primary **Start Game** button visible again. 3. User clicks **Start Game** a second time. 4. Chat persists **two `assistant` GM messages back-to-back with no `user` message between them** — the second one continues the scene as if the first never invited player input. 5. `gameNarrationIndex` stays at 0 so the user only sees the first message in the live narration UI, but the duplicate is visible in **Logs**. ## Steps to reproduce 1. Create a new Game Mode chat and run through `/game/setup` (any genre / setting / tone). 2. Wait for the **Start Game** button to appear. 3. Click **Start Game**. 4. Observe: typewriter starts, then mid-stream the surface drops back to the idle Start Game button. 5. Click **Start Game** a second time. 6. Open **Logs** — you'll see two GM assistant turns, with no user message between. Reproduced twice in a row on 1.5.9 against a real game setup. Network-trace evidence (next section) confirms two `POST /api/game/start` calls **both returned 200**, meaning the server saw `gameSessionStatus === "ready"` at both call times — even though the first call had just written `"active"`. ## Environment - Marinara Engine version: 1.5.9 - Install type: source (Docker, deployed to EasyPanel) - OS + version: Linux (Docker container, host: Linode VPS) - Browser or app shell: Chromium-based ## Logs, screenshots, or video Client-side fetch interceptor captured this sequence around the bug (timestamps UTC, `dur` is the resolve time for fetch — for `/api/generate` that's just SSE headers, not the full stream): ``` 10:39:23.625 POST 200 126ms /api/game/start ← 1st click, server writes status="active" 10:39:23.762 POST 200 174ms /api/generate ← 1st intro generation begins streaming 10:40:01.638 POST 200 37904ms /api/game/scene-wrap ← scene analysis after 1st intro saves 10:40:01.637 POST 200 129ms /api/game/weather/update 10:40:02.629 PUT 200 136ms /api/game/<id>/widgets 10:40:05.739 PUT 200 131ms /api/game/<id>/widgets 10:40:05.871 GET 200 134ms /api/chats/<id> ← client refetch returns status="ready" !! 10:40:05.877 POST 200 187ms /api/game/start ← 2nd click ACCEPTED (server saw "ready") 10:40:06.073 POST 200 225ms /api/generate ← 2nd intro fires, second assistant msg gets saved ``` Server-side independent verification: probing `POST /api/game/start` against the same chat **right now** (after the bug, chat still in `gameSessionStatus: "active"`) returns **500** — confirming the `meta.gameSessionStatus !== "ready"` guard at `packages/server/src/routes/game.routes.ts:3464` works correctly. The only way the 2nd call during the bug returned 200 is that some other handler wrote `"ready"` back to the DB between the two `/game/start` calls. ## Additional context PR #320 audited `packages/server/src/routes/generate.routes.ts` and converted 7 stale-meta writes (preset entry-state-overrides, lorebook entry-state-overrides x3, `lastMapPosition`, game-state-patch `gameMap` sync, `chat_summary`, `mariContext`) to the `freshChat` → `freshMeta` → field-only update pattern. All 6 remaining `chats.updateMetadata(input.chatId, …)` sites in `generate.routes.ts` use re-fetched meta and look safe. The remaining race must be in another handler that runs concurrently with the stream and still uses `{...meta, …}` from an early read. Likely candidates in `packages/server/src/routes/game.routes.ts` that wrote during the trace's race window (10:39:23 → 10:40:05): - `app.post("/weather/update")` at `game.routes.ts:5440` — synchronous read+write, but worth confirming. - `app.put("/:chatId/widgets")` at `game.routes.ts:5629` — same pattern, fires twice in the trace. - Other writes inside `app.post("/scene-wrap")` (`game.routes.ts:6065`) and `app.post("/generate-assets")` (`game.routes.ts:6830`) appear to use `latestMeta` / `freshMeta` correctly. A separate audit pass over every `chats.updateMetadata(…, { ...meta, … })` in `packages/server/src/routes/game.routes.ts` (and ideally migration of partial updates to the existing `chats.patchMetadata(…)` helper at `packages/server/src/services/storage/chats.storage.ts:303`, which uses a per-chat queue and is race-safe) is the proper root-cause fix. **Proposed defensive fix in this issue**: add a second guard to `/api/game/start` that rejects the call when the chat already contains a non-empty `role: "assistant"` message — making the duplicate-intro symptom impossible regardless of which exact route is clobbering the status. Even a fully successful `/game/start` race becomes a no-op error toast on the second click, rather than spending a paid generation on a second intro. The deeper `updateMetadata` audit can be a separate PR. Reference to original issue: #321. Reference to original fix attempt: #320. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#397
No description provided.