From 991a8fa04efa8e90c28e11dec00dd8646e83f1b8 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Fri, 7 Aug 2026 08:46:46 +0800 Subject: [PATCH 1/2] feat: use kernel renderTags:"text-only" + forbid tag echoing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two related changes that keep tags out of model output and client history: 1. server.ts: pass renderTags:"text-only" to processTurn (both Anthropic and OpenAI paths). The acp-kernel render-refs node now skips tool messages (tool-call args / tool-result content) so structured JSON is never polluted by an tag. This replaces the stripToolTags hack (proxy-side post-processing) — the responsibility moves into the kernel where ref assignment and tag rendering are properly separated. - delete ACP_TAG_RE constant and stripToolTags function - delete the two stripToolTags(turn.messages) call sites 2. compress-tool.ts: ACP TAGS prompt section now states tags are system metadata injected by the proxy and must NEVER be echoed in responses; use only the ref ID inside compress calls. The old wording encouraged the model to 'use these annotations', which led it to reproduce the tag syntax in its output (observed as typos in replies). Tags never reach the client regardless — they exist only between proxy and provider on the request path. This change ensures they also don't leak into the model's own generated text. --- src/compress-tool.ts | 2 +- src/server.ts | 16 ++-------------- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/src/compress-tool.ts b/src/compress-tool.ts index cd1fab4950..1dd043fb86 100644 --- a/src/compress-tool.ts +++ b/src/compress-tool.ts @@ -100,7 +100,7 @@ ${HOW_TO_COMPRESS_RULES} ACP TAGS -Each message in the conversation is annotated with a m00175 tag showing its reference ID, approximate token size, and content type. Use these annotations to assess which messages are consuming the most context and prioritize compression accordingly. The token size is approximate — treat it as a relative guide, not an exact count. +Each message in the conversation is annotated with a m00175 tag showing its reference ID, approximate token size, and content type. These tags are system metadata injected by the proxy. NEVER echo, repeat, or reference these XML tags in your responses — the tags must not appear in your output. Use only the ref ID (e.g. m00005) inside compress calls, never the XML wrapper. The token size is approximate — treat it as a relative guide, not an exact count. TOOLS diff --git a/src/server.ts b/src/server.ts index 56e4debf44..76a36c57e9 100644 --- a/src/server.ts +++ b/src/server.ts @@ -176,16 +176,6 @@ async function handle( await forward(req, res, opts, outBody, prepared, core, reqConfig, log); } -const ACP_TAG_RE = /^\x3cacp [^>]*\x3e[^\x3c]*\x3c\/acp\x3e\n?/; - -function stripToolTags(messages: CoreMessage[]): void { - for (const m of messages) { - if (m.contentType === "tool-call" || m.contentType === "tool-result") { - m.text = (m.text ?? "").replace(ACP_TAG_RE, ""); - } - } -} - function prepareAnthropic( bodyBuffer: Buffer, req: http.IncomingMessage, @@ -208,9 +198,8 @@ function prepareAnthropic( try { const { msgs } = anthropicToCore(parsed); const tokenCount = estimateTokensFast(msgs.map((m) => m.text ?? "").join("\n")); - const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount }); + const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount, renderTags: "text-only" }); session.state = turn.state; - stripToolTags(turn.messages); processedMessages = applyCondense(turn.messages, opts, session); rebuiltMessages = coreToAnthropic(processedMessages); @@ -263,9 +252,8 @@ function prepareOpenai( try { const { msgs } = openaiToCore(parsed); const tokenCount = estimateTokensFast(msgs.map((m) => m.text ?? "").join("\n")); - const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount }); + const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount, renderTags: "text-only" }); session.state = turn.state; - stripToolTags(turn.messages); processedMessages = applyCondense(turn.messages, opts, session); rebuiltMessages = coreToOpenai(processedMessages); From 28a0339ef8b027b981c9ac1951c1dd4bb6d23e98 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Fri, 7 Aug 2026 09:03:41 +0800 Subject: [PATCH 2/2] feat: add tag-echo diagnostics across request and response paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Request-side (server.ts): after processTurn, log renderTags strategy and tag-injection counts — confirms text-only keeps tool messages pristine while text messages stay tagged for the LLM. Response-side (esbuild-safe): detect m.text ?? "").join("\n")); const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount, renderTags: "text-only" }); session.state = turn.state; + log("info", diagTagSummary(turn.messages, sessionId, "text-only")); processedMessages = applyCondense(turn.messages, opts, session); rebuiltMessages = coreToAnthropic(processedMessages); @@ -254,6 +269,7 @@ function prepareOpenai( const tokenCount = estimateTokensFast(msgs.map((m) => m.text ?? "").join("\n")); const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount, renderTags: "text-only" }); session.state = turn.state; + log("info", diagTagSummary(turn.messages, sessionId, "text-only")); processedMessages = applyCondense(turn.messages, opts, session); rebuiltMessages = coreToOpenai(processedMessages); @@ -414,11 +430,23 @@ async function forward( { url: upstreamUrl, headers: reqHeaders }, ); for await (const chunk of loop) { + { + const s = chunk.toString("utf8"); + if (s.includes("\x3cacp ") || s.includes("\x3c/acp")) { + log("warn", `[${prepared.session.id}] tag echo: openai response stream contains ((r) => res.once("drain", () => r())); } } else { const rewriter = rewriteSseStream(streamToRead, ctx); for await (const chunk of rewriter) { + { + const s = chunk.toString("utf8"); + if (s.includes("\x3cacp ") || s.includes("\x3c/acp")) { + log("warn", `[${prepared.session.id}] tag echo: anthropic response stream contains ((r) => res.once("drain", () => r())); } } diff --git a/src/stream-openai.ts b/src/stream-openai.ts index 39e1ad8a02..e00327af6e 100644 --- a/src/stream-openai.ts +++ b/src/stream-openai.ts @@ -197,6 +197,9 @@ export function rewriteOpenaiJsonResponse(body: unknown, ctx: RewriteCtx): unkno } } } + if (existingText && (existingText.includes("\x3cacp ") || existingText.includes("\x3c/acp"))) { + ctx.log(`[warn: tag echo] non-stream openai output contains