[GH-ISSUE #2686] [Issue]: Agent batch prompt & response-parsing XML integrity (unescaped name/type/macros + parseBatchResponse truncation/bare-tag hijack) #1329

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

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

Originally assigned to: @SpicyMarinara on GitHub.

Summary

The batched-agent prompt and its response parser have several XML-integrity weaknesses that corrupt the agent→output half of the pipeline: agent name/type (and {{macro}} settings values) are interpolated unescaped into the batch prompt's XML, and parseBatchResponse uses regexes that can truncate the last agent block or hijack an unrelated nested tag — mis-assigning, dropping, or corrupting agent outputs.

Expected behavior

A custom agent's name/type/settings should never be able to break the batch prompt structure, and the parser should assign each <result> block to exactly the right agent without grabbing nested/unrelated tags.

Actual behavior

  • buildBatchSystemPrompt inserts config.type and config.name into XML attributes unescaped (<agent_task id="${config.type}" name="${config.name}">, <result agent="${config.type}">). A name/type containing ", <, >, or a literal </agents>/</result> breaks the block structure and desyncs parseBatchResponse's regexes.
  • renderAgentSettingsMacros substitutes settings {{key}} values unescaped into the <agents> block, so a settings value containing </agents>/<result agent="..."> can close the block early or forge a result block.
  • parseBatchResponse: the lazy [\s\S]*? match can truncate the last agent's block on a </result> inside JSON when trailing text follows; the bare-tag fallback <type>...</type> (pattern 5) matches the first same-named tag anywhere — including a tag nested inside another agent's body — and reports it as a successful result instead of failing/retrying.
  • nameToXmlTag (feedback injection) lowercases + strips non-[a-z0-9_-], so two distinct agent names can slug to the same tag, producing duplicate/ambiguous <tag> blocks in the injected feedback.

How it breaks the pipeline

Code trace (staging @ b826f8f4) — packages/server/src/services/agents/agent-executor.ts:

  • Unescaped attrs: buildBatchSystemPrompt ~:821 (id/name), ~:843 (<result agent>). escapeXmlAttribute exists (:1329) but is used only for message_id.
  • Unescaped macros: renderAgentSettingsMacros :109-113 → injected into <agents> at ~:822/:1064.
  • Parser: parseBatchResponse :860-930; lazy patterns :882-891; bare-tag pattern 5 at :894; assignment :902-926. Custom type is free-form (agent.schema.ts:44 z.string().min(1)).
  • nameToXmlTag collision: runtime-agent-sections.ts:33-37 / packages/shared/src/utils/xml-wrapper.ts:9-16.

Steps to reproduce

  • Unescaped name: create a custom agent named e.g. Combat "Pro" (contains a quote), enable it in a batch → the name="Combat "Pro"" attribute breaks the <agent_task> element and can desync parsing.
  • Bare-tag hijack: create a custom agent with type p (via the REST API/import — the editor auto-slugs type, so this needs the raw path) batched with a narrator/expression agent that emits <p>...</p>; when the model omits the p agent's <result> block, pattern 5 assigns the narrator's <p>Once upon a time.</p> body to the p agent as success (no retry).
  • Tag collision: two agents named Mood Tracker and Mood-Tracker both slug to mood-tracker → duplicate feedback blocks.

Suggested fix

  • Escape config.type/config.name with escapeXmlAttribute in buildBatchSystemPrompt, and escapeXml the rendered macro values in renderAgentSettingsMacros.
  • Harden parseBatchResponse: first extract all <result agent=...> blocks and remove their spans before running the bare-tag/underscore fallbacks against the residual; drop or denylist-gate pattern 5 (reserved HTML tags + length/whitespace sanity), and/or anchor the bare tag to top level. Reject/normalize custom type values that collide with HTML tags or other agents' types at creation.
  • Disambiguate nameToXmlTag collisions (append an index / include agentType as an attribute).

Environment

  • Marinara Engine version: 2.0.0 (staging @ b826f8f4)
  • Install type: source
  • OS + version: All (code-level defect, platform-independent)
  • Browser or app shell: All

Additional context

Found via a systematic multi-agent audit of the agent subsystem. The unescaped-name path affects any custom agent with special characters in its name; the bare-tag hijack mainly affects raw-API/imported short type values. Part of a set of agent-pipeline issues filed together.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @kolacheee on GitHub (Jun 20, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2686 Originally assigned to: @SpicyMarinara on GitHub. ## Summary The batched-agent prompt and its response parser have several XML-integrity weaknesses that corrupt the agent→output half of the pipeline: agent `name`/`type` (and `{{macro}}` settings values) are interpolated **unescaped** into the batch prompt's XML, and `parseBatchResponse` uses regexes that can truncate the last agent block or hijack an unrelated nested tag — mis-assigning, dropping, or corrupting agent outputs. ## Expected behavior A custom agent's name/type/settings should never be able to break the batch prompt structure, and the parser should assign each `<result>` block to exactly the right agent without grabbing nested/unrelated tags. ## Actual behavior - `buildBatchSystemPrompt` inserts `config.type` and `config.name` into XML attributes **unescaped** (`<agent_task id="${config.type}" name="${config.name}">`, `<result agent="${config.type}">`). A name/type containing `"`, `<`, `>`, or a literal `</agents>`/`</result>` breaks the block structure and desyncs `parseBatchResponse`'s regexes. - `renderAgentSettingsMacros` substitutes settings `{{key}}` values **unescaped** into the `<agents>` block, so a settings value containing `</agents>`/`<result agent="...">` can close the block early or forge a result block. - `parseBatchResponse`: the lazy `[\s\S]*?` match can truncate the **last** agent's block on a `</result>` inside JSON when trailing text follows; the bare-tag fallback `<type>...</type>` (pattern 5) matches the **first** same-named tag anywhere — including a tag nested inside another agent's body — and reports it as a successful result instead of failing/retrying. - `nameToXmlTag` (feedback injection) lowercases + strips non-`[a-z0-9_-]`, so two distinct agent names can slug to the same tag, producing duplicate/ambiguous `<tag>` blocks in the injected feedback. ## How it breaks the pipeline Code trace (staging @ `b826f8f4`) — `packages/server/src/services/agents/agent-executor.ts`: - Unescaped attrs: `buildBatchSystemPrompt` `~:821` (`id`/`name`), `~:843` (`<result agent>`). `escapeXmlAttribute` exists (`:1329`) but is used only for `message_id`. - Unescaped macros: `renderAgentSettingsMacros` `:109-113` → injected into `<agents>` at `~:822`/`:1064`. - Parser: `parseBatchResponse` `:860-930`; lazy patterns `:882-891`; bare-tag pattern 5 at `:894`; assignment `:902-926`. Custom `type` is free-form (`agent.schema.ts:44` `z.string().min(1)`). - `nameToXmlTag` collision: `runtime-agent-sections.ts:33-37` / `packages/shared/src/utils/xml-wrapper.ts:9-16`. ## Steps to reproduce - **Unescaped name:** create a custom agent named e.g. `Combat "Pro"` (contains a quote), enable it in a batch → the `name="Combat "Pro""` attribute breaks the `<agent_task>` element and can desync parsing. - **Bare-tag hijack:** create a custom agent with `type` `p` (via the REST API/import — the editor auto-slugs type, so this needs the raw path) batched with a narrator/expression agent that emits `<p>...</p>`; when the model omits the `p` agent's `<result>` block, pattern 5 assigns the narrator's `<p>Once upon a time.</p>` body to the `p` agent as success (no retry). - **Tag collision:** two agents named `Mood Tracker` and `Mood-Tracker` both slug to `mood-tracker` → duplicate feedback blocks. ## Suggested fix - Escape `config.type`/`config.name` with `escapeXmlAttribute` in `buildBatchSystemPrompt`, and `escapeXml` the rendered macro values in `renderAgentSettingsMacros`. - Harden `parseBatchResponse`: first extract all `<result agent=...>` blocks and remove their spans before running the bare-tag/underscore fallbacks against the residual; drop or denylist-gate pattern 5 (reserved HTML tags + length/whitespace sanity), and/or anchor the bare tag to top level. Reject/normalize custom `type` values that collide with HTML tags or other agents' types at creation. - Disambiguate `nameToXmlTag` collisions (append an index / include `agentType` as an attribute). ## Environment - Marinara Engine version: 2.0.0 (staging @ `b826f8f4`) - Install type: source - OS + version: All (code-level defect, platform-independent) - Browser or app shell: All ## Additional context Found via a systematic multi-agent audit of the agent subsystem. The unescaped-name path affects any custom agent with special characters in its name; the bare-tag hijack mainly affects raw-API/imported short `type` values. Part of a set of agent-pipeline issues filed together. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
Author
Owner

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

Addressed on the issue-sweep branch. Batched agent prompts now escape agent id/name attributes and XML-escape settings macro values inside the batched XML prompt. Batch response parsing now extracts explicit spans first, decodes escaped agent attrs, removes those spans before legacy fallback parsing, and drops the unsafe bare fallback so nested/unrelated tags cannot satisfy another agent. Runtime agent XML injection tags are now unique when names slug to the same tag. Final repo checks will run after the sweep.

<!-- gh-comment-id:4761489650 --> @SpicyMarinara commented on GitHub (Jun 21, 2026): Addressed on the issue-sweep branch. Batched agent prompts now escape agent id/name attributes and XML-escape settings macro values inside the batched XML prompt. Batch response parsing now extracts explicit <result agent=...> spans first, decodes escaped agent attrs, removes those spans before legacy fallback parsing, and drops the unsafe bare <type> fallback so nested/unrelated tags cannot satisfy another agent. Runtime agent XML injection tags are now unique when names slug to the same tag. Final repo checks will run after the sweep.
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#1329
No description provided.