[GH-ISSUE #2992] [Issue]: With no visible last-assistant message, tracker retries write directly onto a prior committed game-state snapshot (corrupts historical state) #1567

Closed
opened 2026-10-21 05:39:19 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 29, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2992

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong
When the retry path cannot resolve a last-assistant message (no assistant message at all, or in conversation/roleplay/VN mode every assistant message in the retry window is hidden-from-AI), retryMessageId becomes the empty string. The persona/quest/custom-tracker effect handlers then resolve the target snapshot to a PRIOR turn's committed snapshot and write tracker data directly onto that snapshot's row, corrupting previously committed game state. The world-state and character-tracker handlers similarly write to a cross-chat messageId="" row.

How to reproduce (try it yourself)

  1. Have a game/RPG chat (persona-stats) or roleplay/VN chat (world/character trackers) with an existing committed game-state snapshot from a prior turn.
  2. Reach a state where lastAssistant resolves to undefined for the retry window — e.g. POST /retry-agents on a chat whose retry window has no visible assistant message (no assistant message, or all assistant messages flagged hiddenFromAI).
  3. Include a tracker agent (persona-stats / quest / custom-tracker / world-state / character-tracker) in agentTypes.

You'll see: the tracker update is written onto the prior committed snapshot's id (persona/quest/custom), or a cross-chat messageId="" snapshot is created (world/character) — corrupting historical/other-chat state.
Expected: the tracker effect no-ops when there is no resolvable target message.

Where it is
packages/server/src/routes/generate/retry-agents-route.ts:3299 (retryMessageId = lastAssistant?.id ?? ""), :2235-2242 (loadRetryTargetGameStateSnapshot returns the base snapshot when empty), :2469-2475 (persona direct write to latest.id), :2552-2556 (quest), :2622-2626 (custom_tracker), :2328/:2427 (world-state/character-tracker updateByMessage("")). Storage: packages/server/src/services/storage/game-state.storage.ts:138-146 (getLatestCommitted), :244-251 (getByMessage is unscoped, with an explicit cross-chat-collision warning).

What's happening
retryMessageId silently degrades to "". The persona/quest/custom blocks lack the if (retryMessageId && ...) guard that the text_rewrite, context_injection, and CYOA blocks have, and they bypass updateByMessage to write raw to whatever snapshot loadRetryTargetGameStateSnapshot returns — which for an empty messageId is getLatestCommitted(chatId), a prior committed row.

Suggested fix
Do NOT use a blanket early return (it would drop the chat-scoped secret_plot and lorebook_update effects, which don't depend on retryMessageId). Instead guard exactly the six message/game-state-scoped blocks with retryMessageId &&, matching the existing guarded blocks: world-state game_state_update (~2306-2313), character_tracker_update (~2392-2398), persona_stats_update (~2443-2449), quest_update (~2522-2528), custom_tracker_update (~2603-2609), and sprite_change (~2888). Additionally, as defense-in-depth, change loadRetryTargetGameStateSnapshot at line 2236 from if (!retryMessageId) return loadRetryBaseGameStateSnapshot(); to if (!retryMessageId) return null; so even a missed guard cannot land a direct .where(eq(id, snap.id)) write on a prior committed row. All changes are gated on the empty-retryMessageId case, so the happy path is byte-for-byte unchanged; leave the chat-scoped secret_plot (2485) and lorebook_update (2499) blocks unguarded so they keep running.


AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2992 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** When the retry path cannot resolve a last-assistant message (no assistant message at all, or in conversation/roleplay/VN mode every assistant message in the retry window is hidden-from-AI), `retryMessageId` becomes the empty string. The persona/quest/custom-tracker effect handlers then resolve the target snapshot to a PRIOR turn's committed snapshot and write tracker data directly onto that snapshot's row, corrupting previously committed game state. The world-state and character-tracker handlers similarly write to a cross-chat `messageId=""` row. **How to reproduce (try it yourself)** 1. Have a game/RPG chat (persona-stats) or roleplay/VN chat (world/character trackers) with an existing committed game-state snapshot from a prior turn. 2. Reach a state where lastAssistant resolves to undefined for the retry window — e.g. POST /retry-agents on a chat whose retry window has no visible assistant message (no assistant message, or all assistant messages flagged hiddenFromAI). 3. Include a tracker agent (persona-stats / quest / custom-tracker / world-state / character-tracker) in agentTypes. You'll see: the tracker update is written onto the prior committed snapshot's id (persona/quest/custom), or a cross-chat `messageId=""` snapshot is created (world/character) — corrupting historical/other-chat state. Expected: the tracker effect no-ops when there is no resolvable target message. **Where it is** `packages/server/src/routes/generate/retry-agents-route.ts:3299` (`retryMessageId = lastAssistant?.id ?? ""`), `:2235-2242` (`loadRetryTargetGameStateSnapshot` returns the base snapshot when empty), `:2469-2475` (persona direct write to `latest.id`), `:2552-2556` (quest), `:2622-2626` (custom_tracker), `:2328`/`:2427` (world-state/character-tracker `updateByMessage("")`). Storage: `packages/server/src/services/storage/game-state.storage.ts:138-146` (`getLatestCommitted`), `:244-251` (`getByMessage` is unscoped, with an explicit cross-chat-collision warning). **What's happening** `retryMessageId` silently degrades to `""`. The persona/quest/custom blocks lack the `if (retryMessageId && ...)` guard that the text_rewrite, context_injection, and CYOA blocks have, and they bypass `updateByMessage` to write raw to whatever snapshot `loadRetryTargetGameStateSnapshot` returns — which for an empty messageId is `getLatestCommitted(chatId)`, a prior committed row. **Suggested fix** Do NOT use a blanket early return (it would drop the chat-scoped secret_plot and lorebook_update effects, which don't depend on retryMessageId). Instead guard exactly the six message/game-state-scoped blocks with `retryMessageId &&`, matching the existing guarded blocks: world-state game_state_update (~2306-2313), character_tracker_update (~2392-2398), persona_stats_update (~2443-2449), quest_update (~2522-2528), custom_tracker_update (~2603-2609), and sprite_change (~2888). Additionally, as defense-in-depth, change `loadRetryTargetGameStateSnapshot` at line 2236 from `if (!retryMessageId) return loadRetryBaseGameStateSnapshot();` to `if (!retryMessageId) return null;` so even a missed guard cannot land a direct `.where(eq(id, snap.id))` write on a prior committed row. All changes are gated on the empty-retryMessageId case, so the happy path is byte-for-byte unchanged; leave the chat-scoped secret_plot (2485) and lorebook_update (2499) blocks unguarded so they keep running. --- *AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.*
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Fixed in merged staging PR #3004.

<!-- gh-comment-id:4832280981 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in merged staging PR #3004.
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#1567
No description provided.