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..757d149647 100644 --- a/src/server.ts +++ b/src/server.ts @@ -176,14 +176,18 @@ async function handle( await forward(req, res, opts, outBody, prepared, core, reqConfig, log); } -const ACP_TAG_RE = /^\x3cacp [^>]*\x3e[^\x3c]*\x3c\/acp\x3e\n?/; +const ACP_TAG_MARK = "\x3cacp "; -function stripToolTags(messages: CoreMessage[]): void { +function diagTagSummary(messages: CoreMessage[], sessionId: string, strategy: string): string { + let textTagged = 0; + let toolTagged = 0; for (const m of messages) { - if (m.contentType === "tool-call" || m.contentType === "tool-result") { - m.text = (m.text ?? "").replace(ACP_TAG_RE, ""); - } + const hasTag = (m.text ?? "").includes(ACP_TAG_MARK); + if (!hasTag) continue; + if (m.contentType === "tool-call" || m.contentType === "tool-result") toolTagged++; + else textTagged++; } + return `[${sessionId}] processTurn: ${messages.length} msgs, renderTags=${strategy}, ${textTagged} text tagged, ${toolTagged} tool tagged (should be 0 with text-only)`; } function prepareAnthropic( @@ -208,9 +212,9 @@ 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); + log("info", diagTagSummary(turn.messages, sessionId, "text-only")); processedMessages = applyCondense(turn.messages, opts, session); rebuiltMessages = coreToAnthropic(processedMessages); @@ -263,9 +267,9 @@ 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); + log("info", diagTagSummary(turn.messages, sessionId, "text-only")); processedMessages = applyCondense(turn.messages, opts, session); rebuiltMessages = coreToOpenai(processedMessages); @@ -426,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