mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[PR #533] [MERGED] fix(agents): prevent text agent output leaking into main prompt #4659
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#4659
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 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)
c24459cfix(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
prepareProviderMessagesconverts 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: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 ascontext_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.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.isTextOutputAgentType,sanitizeTextAgentResponse) keyed off the existingAGENT_RESULT_TYPE_MAP/TEXT_RESULT_TYPESclassification 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 checkpasses locallyCONTRIBUTING.mdManual verification notes
User → Assistant roleplay → <committed_tracker_state>{...}pattern).[Director's note: ...]instructions that influence the next generation correctly.BANNED ELEMENTS / RHETORICAL DEVICES / ...format.Docs and release impact
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.