[PR #533] [MERGED] fix(agents): prevent text agent output leaking into main prompt #4659

Closed
opened 2026-10-22 04:09:33 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/533
Author: @Romuromylus
Created: 5/7/2026
Status: ✅ Merged
Merged: 5/7/2026
Merged by: @Romuromylus

Base: main ← Head: fix/agent-text-output-leakage


📝 Commits (1)

  • c24459c fix(agents): prevent text agent output leaking into main prompt

📊 Changes

1 file changed (+60 additions, -4 deletions)

View changed files

📝 packages/server/src/services/agents/agent-executor.ts (+60 -4)

📄 Description

Linked issue

Closes #526

Why this change

Text-output pre-generation agents (Narrative Director, Prose Guardian) could mimic the chat-history pattern they were shown and emit roleplay text plus <committed_tracker_state> JSON in their response.

That leaked content was injected as a system block before main generation. Because prepareProviderMessages converts trailing system messages to user role for provider API safety, the main AI received the leak as if the user had typed it — producing jarring double-responses or hallucinated continuations of the previous turn.

The chain reproduces especially reliably on small or fine-tuned models, where the LLM is more prone to continue a salient input pattern ([user] msg → [assistant] roleplay + <committed_tracker_state>{...}) instead of obeying the agent's instructions.

What changed

Two-layer defense in packages/server/src/services/agents/agent-executor.ts:

  • Cause fix in buildAgentMessages: skip the <committed_tracker_state> append on assistant messages when the agent's primary output is text (director, prose-guardian, or any built-in agent typed as context_injection / director_event). They evaluate pacing and writing quality and don't need raw tracker JSON, so removing it removes the pattern. JSON agents (world-state, character-tracker, etc.) and __batch__ calls keep getting it as before — unknown agent types fall through the safe default.
  • Symptom fix in parseAgentResponse: sanitize text-agent output before it becomes an injection. Strips leaked <committed_tracker_state> and <assistant_response> blocks. For the Narrative Director specifically, extracts only the canonical [Director's note: ...] bracket its prompt mandates (taking the last match, which is most likely the model's "final" intent); if no bracketed note is present at all the response is fully off-format and is dropped, so the pipeline injects nothing rather than hallucinated roleplay.
  • Added two small helpers (isTextOutputAgentType, sanitizeTextAgentResponse) keyed off the existing AGENT_RESULT_TYPE_MAP / TEXT_RESULT_TYPES classification so no new sources of truth are introduced.

The defense is layered on purpose: layer 1 closes the cause for known text agents, layer 2 catches anything that still leaks (custom user-defined text agents, future regressions, batch-mode calls where multiple agents share a prompt).

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

  • Built and deployed the change to a self-hosted Marinara instance via the maintainer's fork autoDeploy.
  • Reproduced the original symptom by running a Game-Mode chat with Narrative Director enabled on a small / fine-tuned model that previously produced the leak (recent history contained the User → Assistant roleplay → <committed_tracker_state>{...} pattern).
  • Confirmed after the fix:
    • The main AI no longer responds to leaked roleplay or tracker JSON as if it were a user turn.
    • Director notes that DO appear are well-formed [Director's note: ...] instructions that influence the next generation correctly.
    • Prose Guardian directives still arrive in their usual BANNED ELEMENTS / RHETORICAL DEVICES / ... format.
    • JSON agents (world-state, character-tracker) continue to receive committed tracker context — their post-processing tracker updates still apply.

Docs and release impact

  • No docs changes needed

UI evidence (if applicable)

Not applicable — server-side fix in the agent execution pipeline; no UI surface changes.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/533 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 5/7/2026 **Status:** ✅ Merged **Merged:** 5/7/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `main` ← **Head:** `fix/agent-text-output-leakage` --- ### 📝 Commits (1) - [`c24459c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/c24459c38a008f68d2617a0ec05219235696ca3c) fix(agents): prevent text agent output leaking into main prompt ### 📊 Changes **1 file changed** (+60 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/services/agents/agent-executor.ts` (+60 -4) </details> ### 📄 Description ## Linked issue Closes #526 ## Why this change Text-output pre-generation agents (Narrative Director, Prose Guardian) could mimic the chat-history pattern they were shown and emit roleplay text plus `<committed_tracker_state>` JSON in their response. That leaked content was injected as a system block before main generation. Because `prepareProviderMessages` converts trailing system messages to user role for provider API safety, the main AI received the leak as if the user had typed it — producing jarring double-responses or hallucinated continuations of the previous turn. The chain reproduces especially reliably on small or fine-tuned models, where the LLM is more prone to continue a salient input pattern (`[user] msg → [assistant] roleplay + <committed_tracker_state>{...}`) instead of obeying the agent's instructions. ## What changed Two-layer defense in `packages/server/src/services/agents/agent-executor.ts`: - **Cause fix in `buildAgentMessages`**: skip the `<committed_tracker_state>` append on assistant messages when the agent's primary output is text (director, prose-guardian, or any built-in agent typed as `context_injection` / `director_event`). They evaluate pacing and writing quality and don't need raw tracker JSON, so removing it removes the pattern. JSON agents (world-state, character-tracker, etc.) and `__batch__` calls keep getting it as before — unknown agent types fall through the safe default. - **Symptom fix in `parseAgentResponse`**: sanitize text-agent output before it becomes an injection. Strips leaked `<committed_tracker_state>` and `<assistant_response>` blocks. For the Narrative Director specifically, extracts only the canonical `[Director's note: ...]` bracket its prompt mandates (taking the last match, which is most likely the model's "final" intent); if no bracketed note is present at all the response is fully off-format and is dropped, so the pipeline injects nothing rather than hallucinated roleplay. - Added two small helpers (`isTextOutputAgentType`, `sanitizeTextAgentResponse`) keyed off the existing `AGENT_RESULT_TYPE_MAP` / `TEXT_RESULT_TYPES` classification so no new sources of truth are introduced. The defense is layered on purpose: layer 1 closes the cause for known text agents, layer 2 catches anything that still leaks (custom user-defined text agents, future regressions, batch-mode calls where multiple agents share a prompt). ## Validation - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes - Built and deployed the change to a self-hosted Marinara instance via the maintainer's fork autoDeploy. - Reproduced the original symptom by running a Game-Mode chat with Narrative Director enabled on a small / fine-tuned model that previously produced the leak (recent history contained the `User → Assistant roleplay → <committed_tracker_state>{...}` pattern). - Confirmed after the fix: - The main AI no longer responds to leaked roleplay or tracker JSON as if it were a user turn. - Director notes that DO appear are well-formed `[Director's note: ...]` instructions that influence the next generation correctly. - Prose Guardian directives still arrive in their usual `BANNED ELEMENTS / RHETORICAL DEVICES / ...` format. - JSON agents (world-state, character-tracker) continue to receive committed tracker context — their post-processing tracker updates still apply. ## Docs and release impact - [x] No docs changes needed ## UI evidence (if applicable) Not applicable — server-side fix in the agent execution pipeline; no UI surface changes. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:09:33 +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#4659
No description provided.