[GH-ISSUE #2639] [Issue]: text_rewrite stale-edit guard breaks the whole retry result loop, dropping every later agent's persistence #1299

Closed
opened 2026-10-21 05:31:07 +07:00 by bsakaguchi · 0 comments
Owner

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

Originally assigned to: @Romuromylus on GitHub.

Summary

Severity: Medium — data-loss. In applyRetryResultEffects, the stale-edit guard for a text_rewrite result runs break, which exits the entire for (const result of sortedResults) loop instead of skipping only the rewrite. Every result that sorts after the text_rewrite entry (game/tracker/persona/quest/custom-tracker/lorebook/CYOA/illustrator/sprite) is then never applied, even though those agents already ran and burned tokens.

Expected behavior

A stale text_rewrite result should be skipped on its own. All other agent results in the same retry batch should still be persisted (DB writes) and rendered (illustrator image generation).

Actual behavior

packages/server/src/routes/generate/retry-agents-route.ts:2016 — inside the single result loop:

if (retryMessageId && changedMessage) {
  const currentMessage = await chats.getMessage(retryMessageId);
  if ((currentMessage?.content ?? "") !== currentResponseForRewrite) {
    logger.info(
      "[retry-agents] Skipping rewrite for message %s because the message was edited during agent retry",
      retryMessageId,
    );
    break; // <-- exits the WHOLE loop, not just the text_rewrite branch
  }
  ...
}

The loop body sequentially handles ~11 result types, many of which persist after text_rewrite: game_state_update, context_injection, character_tracker_update, persona_stats_update (app.db.update), secret_plot, lorebook_update, quest_update (app.db.update), cyoa_choices, custom_tracker_update (app.db.update), image_prompt/illustrator (image generation). The sort only hoists game_state_update to the front, so a text_rewrite result from a post-processing prose-guardian/continuity agent commonly sits ahead of those — and break drops all of them silently.

There is also a false-positive trigger: currentResponseForRewrite is seeded from the macro-resolved agentContext.mainResponse, while currentMessage.content from chats.getMessage is the raw stored content. A message that legitimately contains literal {{...}} makes resolved ≠ raw, so the guard wrongly reports "edited during retry" and breaks even when nothing was edited.

Steps to reproduce

  1. Open a chat with several result-producing agents enabled (e.g. a tracker / quest / CYOA agent plus a prose-guardian or continuity rewrite agent).
  2. Retry the batch on an assistant message that either was edited during the retry, or contains literal {{...}} text.
  3. Observe: the rewrite is skipped (expected) but the tracker / quest / CYOA / illustrator results are also never persisted or rendered.

Environment

  • Marinara Engine version: staging @ 3dc062e2
  • Install type: source
  • OS + version: n/a (static source audit)
  • Browser or app shell: n/a

Logs, screenshots, or video

Static source audit, cited to file:line above.

Additional context

Fix sketch: replace the break at retry-agents-route.ts:2016 with continue so only the stale rewrite is skipped and the unrelated results that follow it still apply. Separately, compare against the raw stored content (or store/compare a resolved baseline on both sides) so a message containing literal {{...}} does not trip the bogus "edited during retry" path.

Regression notes: not a regression. The break-exits-whole-loop defect is byte-identical on main (retry-agents-route.ts:1407, loop opened at 1394), so it is long-standing. The only staging-side change is that staging seeds the rewrite baseline from the macro-resolved mainResponse (line 576) where main uses the raw value (line 298) — this widens the {{...}} false-positive window but does not introduce the underlying break defect. Found via static audit.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (Jun 19, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2639 Originally assigned to: @Romuromylus on GitHub. ## Summary **Severity: Medium — data-loss.** In `applyRetryResultEffects`, the stale-edit guard for a `text_rewrite` result runs `break`, which exits the entire `for (const result of sortedResults)` loop instead of skipping only the rewrite. Every result that sorts *after* the `text_rewrite` entry (game/tracker/persona/quest/custom-tracker/lorebook/CYOA/illustrator/sprite) is then never applied, even though those agents already ran and burned tokens. ## Expected behavior A stale `text_rewrite` result should be skipped on its own. All other agent results in the same retry batch should still be persisted (DB writes) and rendered (illustrator image generation). ## Actual behavior `packages/server/src/routes/generate/retry-agents-route.ts:2016` — inside the single result loop: ```ts if (retryMessageId && changedMessage) { const currentMessage = await chats.getMessage(retryMessageId); if ((currentMessage?.content ?? "") !== currentResponseForRewrite) { logger.info( "[retry-agents] Skipping rewrite for message %s because the message was edited during agent retry", retryMessageId, ); break; // <-- exits the WHOLE loop, not just the text_rewrite branch } ... } ``` The loop body sequentially handles ~11 result types, many of which persist *after* `text_rewrite`: `game_state_update`, `context_injection`, `character_tracker_update`, `persona_stats_update` (`app.db.update`), `secret_plot`, `lorebook_update`, `quest_update` (`app.db.update`), `cyoa_choices`, `custom_tracker_update` (`app.db.update`), `image_prompt`/illustrator (image generation). The sort only hoists `game_state_update` to the front, so a `text_rewrite` result from a post-processing prose-guardian/continuity agent commonly sits ahead of those — and `break` drops all of them silently. There is also a false-positive trigger: `currentResponseForRewrite` is seeded from the macro-**resolved** `agentContext.mainResponse`, while `currentMessage.content` from `chats.getMessage` is the **raw** stored content. A message that legitimately contains literal `{{...}}` makes resolved ≠ raw, so the guard wrongly reports "edited during retry" and breaks even when nothing was edited. ## Steps to reproduce 1. Open a chat with several result-producing agents enabled (e.g. a tracker / quest / CYOA agent **plus** a prose-guardian or continuity rewrite agent). 2. Retry the batch on an assistant message that either was edited during the retry, **or** contains literal `{{...}}` text. 3. Observe: the rewrite is skipped (expected) but the tracker / quest / CYOA / illustrator results are also never persisted or rendered. ## Environment - Marinara Engine version: `staging @ 3dc062e2` - Install type: source - OS + version: n/a (static source audit) - Browser or app shell: n/a ## Logs, screenshots, or video Static source audit, cited to file:line above. ## Additional context Fix sketch: replace the `break` at `retry-agents-route.ts:2016` with `continue` so only the stale rewrite is skipped and the unrelated results that follow it still apply. Separately, compare against the **raw** stored content (or store/compare a resolved baseline on both sides) so a message containing literal `{{...}}` does not trip the bogus "edited during retry" path. Regression notes: not a regression. The break-exits-whole-loop defect is byte-identical on `main` (`retry-agents-route.ts:1407`, loop opened at `1394`), so it is long-standing. The only staging-side change is that staging seeds the rewrite baseline from the macro-resolved `mainResponse` (line 576) where `main` uses the raw value (line 298) — this widens the `{{...}}` false-positive window but does not introduce the underlying `break` defect. Found via static audit. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:31:07 +07:00
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#1299
No description provided.