Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compress-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ ${HOW_TO_COMPRESS_RULES}

ACP TAGS

Each message in the conversation is annotated with a <acp tokens="2.1K" type="tool:bash">m00175</acp> 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 <acp tokens="2.1K" type="tool:bash">m00175</acp> 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

Expand Down
34 changes: 25 additions & 9 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,18 @@
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(
Expand All @@ -208,9 +212,9 @@
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" });

Check failure on line 215 in src/server.ts

View workflow job for this annotation

GitHub Actions / test (22)

Object literal may only specify known properties, and 'renderTags' does not exist in type 'ProcessTurnInput'.

Check failure on line 215 in src/server.ts

View workflow job for this annotation

GitHub Actions / test (24)

Object literal may only specify known properties, and 'renderTags' does not exist in type 'ProcessTurnInput'.
session.state = turn.state;
stripToolTags(turn.messages);
log("info", diagTagSummary(turn.messages, sessionId, "text-only"));
processedMessages = applyCondense(turn.messages, opts, session);
rebuiltMessages = coreToAnthropic(processedMessages);

Expand Down Expand Up @@ -263,9 +267,9 @@
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" });

Check failure on line 270 in src/server.ts

View workflow job for this annotation

GitHub Actions / test (22)

Object literal may only specify known properties, and 'renderTags' does not exist in type 'ProcessTurnInput'.

Check failure on line 270 in src/server.ts

View workflow job for this annotation

GitHub Actions / test (24)

Object literal may only specify known properties, and 'renderTags' does not exist in type 'ProcessTurnInput'.
session.state = turn.state;
stripToolTags(turn.messages);
log("info", diagTagSummary(turn.messages, sessionId, "text-only"));
processedMessages = applyCondense(turn.messages, opts, session);
rebuiltMessages = coreToOpenai(processedMessages);

Expand Down Expand Up @@ -426,11 +430,23 @@
{ 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 <acp tag`);
}
}
if (!res.write(chunk)) await new Promise<void>((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 <acp tag`);
}
}
if (!res.write(chunk)) await new Promise<void>((r) => res.once("drain", () => r()));
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/stream-openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <acp tag: ${existingText.slice(0, 120).replace(/\n/g, " ")}`);
}
if (!converted) return body;
const note = noteParts.join("\n");
msg.content = existingText ? `${existingText}\n${note}` : note;
Expand Down
10 changes: 10 additions & 0 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ function routeEvent(
if (typeof partial === "string") st.json += partial;
return NOOP;
}
const dt = (d as { delta?: { text?: string } }).delta;
if (dt?.text && (dt.text.includes("\x3cacp ") || dt.text.includes("\x3c/acp"))) {
ctx.log(`[warn: tag echo] model emitted <acp tag in text delta: ${dt.text.slice(0, 120).replace(/\n/g, " ")}`);
}
return emitEvent(ev);
}
if (t === "content_block_stop") {
Expand Down Expand Up @@ -230,6 +234,12 @@ export function rewriteJsonResponse(body: unknown, ctx: RewriteCtx): unknown {
}
b.content = newContent;
if (converted && !sawRealToolUse) b.stop_reason = "end_turn";
for (const blk of newContent) {
const t = (blk as { type?: string; text?: string }).text;
if (typeof t === "string" && (t.includes("\x3cacp ") || t.includes("\x3c/acp"))) {
ctx.log(`[warn: tag echo] non-stream model output contains <acp tag: ${t.slice(0, 120).replace(/\n/g, " ")}`);
}
}
return body;
}

Expand Down
Loading