[GH-ISSUE #2984] [Issue]: Batched text_rewrite agents lose markup preservation on retry, so prose-guardian/continuity rewrites are silently dropped (custom rewrites destroy markup) #1558

Closed
opened 2026-10-21 05:39:03 +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/2984

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong
When a text_rewrite agent (prose-guardian, continuity, or a custom rewrite agent) is run in a batch on the retry path, it is fed a tag-stripped version of the assistant response instead of the raw markup. As a result its rewrite comes back without the original HTML/XML tags. For prose-guardian/continuity the markup-drop safety guard then trips and silently skips the rewrite (warn log only). For a custom text_rewrite agent the tag-free edit is written anyway, destroying all original HTML/markup in the message. The same agent works correctly when it runs alone or during fresh generation — only retry batching is broken.

How to reproduce (try it yourself)

  1. Configure two post-processing agents on the SAME connection+model: prose-guardian plus any other batchable agent (e.g. character-tracker or world-state).
  2. Have an assistant message that contains HTML/XML markup (e.g. <div>, <i>, <speaker> — common in roleplay).
  3. Trigger an agent retry/regenerate for that message (POST /retry-agents).

You'll see: prose-guardian's rewrite is silently skipped (only a "dropped protected markup" warn log). With a custom text_rewrite agent instead, its tag-free edit overwrites the message and wipes the markup.
Expected: the rewrite applies and preserves markup, exactly as it does when prose-guardian runs alone or during fresh generation.

Where it is
packages/server/src/services/agents/agent-executor.ts:762-768 (batch call to buildAgentMessages with no options object, so preserveAssistantResponseMarkup defaults to false), :1769-1773 (the <assistant_response> markup branch uses stripHtmlTags when the flag is false), :1173-1182 (shouldRunAgentIndividually omits text_rewrite). Single-agent path sets the flag correctly at :1265-1268. Retry reachability: packages/server/src/routes/generate/retry-agents-route.ts:3159-3166 (no text_rewrite carve-out), :2011-2016 (non-tool agents batched), :2253-2268 (markup-drop guard / overwrite). Guard: packages/server/src/services/generation/text-rewrite-safety.ts:1-17.

What's happening
executeAgentBatch builds one shared message array via buildAgentMessages(...) and never forwards the per-agent options that the single-agent path computes — so preserveAssistantResponseMarkup is false and the shared <assistant_response> block is stripHtmlTags(mainResponse). The main generate route carves text_rewrite agents out of the pipeline so they always run individually, but the retry route does not, and shouldRunAgentIndividually only isolates expression/illustrator/lorebook-keeper/JSON-music. So a batched rewrite agent only ever sees stripped content and returns tag-free editedText, which the markup-drop guard (which compares against the still-tagged original) rejects.

Suggested fix
Run text_rewrite agents individually at the single batching chokepoint. Extend shouldRunAgentIndividually (agent-executor.ts:1173-1182) to also isolate any agent whose RESOLVED result type is text_rewrite (so it covers built-in prose-guardian/continuity, the merged prose-guardian+continuity agent, AND custom agents):

  return (
    config.type === "expression" ||
    config.type === "illustrator" ||
    config.type === "lorebook-keeper" ||
    resolveAgentResultType(config) === "text_rewrite" ||
    musicDjUsesJsonOnlyProvider(config)
  );

This routes every batched text_rewrite agent through executeAgent → buildStandardAgentMessages, which already sets preserveAssistantResponseMarkup: resultType === "text_rewrite". Regression avoided: do NOT instead thread preserveAssistantResponseMarkup: true into the batch path — the batch builds ONE shared <assistant_response>, so co-batched JSON tracker agents (world-state, director, etc.) would then receive raw markup instead of the deliberately tag-stripped version, wasting tokens and confusing their JSON extraction. The fix MUST key off resolveAgentResultType, not config.type — there is no agent literally typed text_rewrite; it is a RESULT type. First-generation already carves these out before the pipeline, so this is a no-op there.


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/2984 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** When a text_rewrite agent (prose-guardian, continuity, or a custom rewrite agent) is run in a batch on the retry path, it is fed a tag-stripped version of the assistant response instead of the raw markup. As a result its rewrite comes back without the original HTML/XML tags. For prose-guardian/continuity the markup-drop safety guard then trips and silently skips the rewrite (warn log only). For a custom text_rewrite agent the tag-free edit is written anyway, destroying all original HTML/markup in the message. The same agent works correctly when it runs alone or during fresh generation — only retry batching is broken. **How to reproduce (try it yourself)** 1. Configure two post-processing agents on the SAME connection+model: prose-guardian plus any other batchable agent (e.g. character-tracker or world-state). 2. Have an assistant message that contains HTML/XML markup (e.g. `<div>`, `<i>`, `<speaker>` — common in roleplay). 3. Trigger an agent retry/regenerate for that message (POST /retry-agents). You'll see: prose-guardian's rewrite is silently skipped (only a "dropped protected markup" warn log). With a custom text_rewrite agent instead, its tag-free edit overwrites the message and wipes the markup. Expected: the rewrite applies and preserves markup, exactly as it does when prose-guardian runs alone or during fresh generation. **Where it is** `packages/server/src/services/agents/agent-executor.ts:762-768` (batch call to `buildAgentMessages` with no options object, so `preserveAssistantResponseMarkup` defaults to false), `:1769-1773` (the `<assistant_response>` markup branch uses `stripHtmlTags` when the flag is false), `:1173-1182` (`shouldRunAgentIndividually` omits text_rewrite). Single-agent path sets the flag correctly at `:1265-1268`. Retry reachability: `packages/server/src/routes/generate/retry-agents-route.ts:3159-3166` (no text_rewrite carve-out), `:2011-2016` (non-tool agents batched), `:2253-2268` (markup-drop guard / overwrite). Guard: `packages/server/src/services/generation/text-rewrite-safety.ts:1-17`. **What's happening** `executeAgentBatch` builds one shared message array via `buildAgentMessages(...)` and never forwards the per-agent options that the single-agent path computes — so `preserveAssistantResponseMarkup` is false and the shared `<assistant_response>` block is `stripHtmlTags(mainResponse)`. The main generate route carves text_rewrite agents out of the pipeline so they always run individually, but the retry route does not, and `shouldRunAgentIndividually` only isolates expression/illustrator/lorebook-keeper/JSON-music. So a batched rewrite agent only ever sees stripped content and returns tag-free editedText, which the markup-drop guard (which compares against the still-tagged original) rejects. **Suggested fix** Run text_rewrite agents individually at the single batching chokepoint. Extend `shouldRunAgentIndividually` (agent-executor.ts:1173-1182) to also isolate any agent whose RESOLVED result type is `text_rewrite` (so it covers built-in prose-guardian/continuity, the merged prose-guardian+continuity agent, AND custom agents): ```ts return ( config.type === "expression" || config.type === "illustrator" || config.type === "lorebook-keeper" || resolveAgentResultType(config) === "text_rewrite" || musicDjUsesJsonOnlyProvider(config) ); ``` This routes every batched text_rewrite agent through `executeAgent` → `buildStandardAgentMessages`, which already sets `preserveAssistantResponseMarkup: resultType === "text_rewrite"`. Regression avoided: do NOT instead thread `preserveAssistantResponseMarkup: true` into the batch path — the batch builds ONE shared `<assistant_response>`, so co-batched JSON tracker agents (world-state, director, etc.) would then receive raw markup instead of the deliberately tag-stripped version, wasting tokens and confusing their JSON extraction. The fix MUST key off `resolveAgentResultType`, not `config.type` — there is no agent literally typed `text_rewrite`; it is a RESULT type. First-generation already carves these out before the pipeline, so this is a no-op there. --- *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:4832278260 --> @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#1558
No description provided.