From 38f1bbde6bbce9407b561c42efd0196f387cd659 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sun, 11 Oct 2026 21:58:05 +0800 Subject: [PATCH 1/4] fix: strip ACP panels and status markers on the google lane before projection (#2798) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit prepare-google.ts imported stripAcpPanelMessages/stripAcpStatusMarkers but never called them, so ACP panel user messages (#359) and ephemeral [ACP] status marker lines (#717/#1029, incl. multi-line acp_status reports #2789/#2797) rode unstripped into the compression state on every /v1beta turn and were billed/replayed forever while the openai/anthropic/responses lanes stripped them. The existing strippers only understand {role, content} arrays — google contents are {role, parts} — so passing parsed.contents would have been a silent no-op. Add parts-aware variants to acp-panel.ts: stripAcpPanelGoogleContents (whole panel user contents removed; any non-text/signatured part makes a content ineligible) and stripAcpStatusMarkersGoogleContents (per-part marker-line stripping that skips thought/thoughtSignature parts per KDD #10). Call both in prepareGoogle and prepareGoogleCountTokens before the chain-carrier strip, with the same log lines as the siblings. Mirror the same two passes in fork-adoption's incomingCoreMessages google branch so adoption id derivation stays in parity with prepareGoogle (maybeAdoptForkBlocks runs pre-prepare on the raw body). Behavior change (old->new): /v1beta generateContent/streamGenerateContent/countTokens previously forwarded and measured ACP panel messages + [ACP] marker lines verbatim; they are now stripped before projection/measurement, matching the other three lanes. No config surface change, no kernel change. --- src/acp-panel.ts | 73 ++++++ src/fork-adoption.ts | 7 +- src/server/prepare-google.ts | 24 +- tests/issue2798-google-acp-strip.test.ts | 268 +++++++++++++++++++++++ 4 files changed, 369 insertions(+), 3 deletions(-) create mode 100644 tests/issue2798-google-acp-strip.test.ts diff --git a/src/acp-panel.ts b/src/acp-panel.ts index d6db3c0deb..c18cbae3f3 100644 --- a/src/acp-panel.ts +++ b/src/acp-panel.ts @@ -174,3 +174,76 @@ export function stripAcpPanelResponsesInput(input: unknown): number { } return stripped; } + +// Gemini-native wire variants (#2798): contents are {role, parts: [{text}]}, +// so the messages-array strippers above are silent no-ops on /v1beta traffic +// (no `content` field; model role is "model", not "assistant"). Same +// whole-message panel discipline as stripAcpPanelMessages; the marker pass +// additionally skips signature-verified thinking-family parts — Gemini +// validates thoughtSignature byte-for-byte on replay (KDD #10), so those +// parts are never rewritten even though they carry a text field. + +function googleContentText(parts: unknown): string | undefined { + if (!Array.isArray(parts)) return undefined; + let joined = ""; + for (const part of parts) { + const p = part as Record | null; + if (p === null || typeof p !== "object") return undefined; + if (typeof p.text !== "string") return undefined; + if (Object.keys(p).some((k) => k !== "text")) return undefined; + joined += p.text; + } + return joined; +} + +// Strip ACP panel user contents from a Gemini contents array (in place); +// returns the count removed. A content is panel-eligible only when every part +// is a plain {text} object — any image/function/thinking/signature part makes +// it real conversation content that must survive. +export function stripAcpPanelGoogleContents(contents: unknown): number { + if (!Array.isArray(contents)) return 0; + let stripped = 0; + for (let i = contents.length - 1; i >= 0; i--) { + const rec = contents[i] as Record | null; + if (rec === null || typeof rec !== "object") continue; + if (rec.role !== "user") continue; + const text = googleContentText(rec.parts); + if (text !== undefined && isAcpPanelText(text)) { + contents.splice(i, 1); + stripped++; + } + } + return stripped; +} + +// Strip ACP status marker lines from user/model text parts of a Gemini +// contents array (in place); returns the line count removed. Role-less +// contents count as user side (the wire default). Parts are never deleted — +// a marker-only text degrades to a single space, mirroring +// stripAcpStatusMarkers above. +export function stripAcpStatusMarkersGoogleContents(contents: unknown): number { + if (!Array.isArray(contents)) return 0; + let stripped = 0; + for (const rec of contents) { + if (rec === null || typeof rec !== "object") continue; + const c = rec as Record; + const role = c.role; + if (role !== "user" && role !== "model" && role !== undefined) continue; + const parts = c.parts; + if (!Array.isArray(parts)) continue; + for (const part of parts) { + if (part === null || typeof part !== "object") continue; + const p = part as Record; + if (p.thought === true) continue; + if (p.thoughtSignature !== undefined && p.thoughtSignature !== null) continue; + const text = p.text; + if (typeof text !== "string") continue; + const out = stripMarkerLines(text); + if (out !== text) { + p.text = out.trim().length > 0 ? out : " "; + stripped += countMarkerLines(text); + } + } + } + return stripped; +} diff --git a/src/fork-adoption.ts b/src/fork-adoption.ts index 090e31a933..830ad6b5f5 100644 --- a/src/fork-adoption.ts +++ b/src/fork-adoption.ts @@ -1,6 +1,6 @@ import { anthropicToCore, googleToCore, openaiToCore } from "acp-kernel/wire"; import { BLOCKED_REF, STORED_PLACEHOLDER_MARKER, highestUsedIndex, type CompressionBlock, type CoreMessage } from "acp-kernel"; -import { stripAcpPanelMessages, stripAcpPanelResponsesInput, stripAcpStatusMarkers } from "./acp-panel.js"; +import { stripAcpPanelGoogleContents, stripAcpPanelMessages, stripAcpPanelResponsesInput, stripAcpStatusMarkers, stripAcpStatusMarkersGoogleContents } from "./acp-panel.js"; import { normalizeResponsesMessageItems, sanitizeResponsesInputIds, dropWhitespaceResponsesMessages } from "./loop/adapter-responses.js"; import { responsesToCoreWithToolImages } from "./responses-tool-output.js"; import { replaceBiliCompactionItems } from "./codex-compact.js"; @@ -101,7 +101,8 @@ interface ForkAdoptionPlan { * mutations IN ORDER (compaction-echo replacement → type stamping → id * sanitize → whitespace drop → panel/marker strip → chain-carrier strip) * before converting with the same tool-images wrapper prepare uses; google - * mirrors prepareGoogle (chain-carrier strip → googleToCore). Id parity + * mirrors prepareGoogle (panel/marker strip → chain-carrier strip → + * googleToCore, #2798). Id parity * with the kernel's processTurn input is the whole point: an id this pass * misses drops a parent ref from the inheritance; an id it invents matches * nothing (harmless superset). The plugin-mode mid-history sys/developer @@ -125,6 +126,8 @@ export function incomingCoreMessages(protocol: WireProtocol, parsed: unknown): C return responsesToCoreWithToolImages(clone as Parameters[0]).msgs; } if (protocol === "google") { + stripAcpPanelGoogleContents(clone.contents); + stripAcpStatusMarkersGoogleContents(clone.contents); stripEmbeddedChainCarriers(clone, "google"); return googleToCore(clone as Parameters[0]).msgs; } diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts index 414b900311..0527f16cfd 100644 --- a/src/server/prepare-google.ts +++ b/src/server/prepare-google.ts @@ -20,7 +20,7 @@ import { rulesEnabled, storeEffectiveRules } from "../rules-feature.js"; import { autoFoldEngaged, externalSummaryEnabled, growthFoldingArmed } from "../external-summary-surface.js"; import { attachSubagentSessions } from "../subagent-sessions.js"; import { reconcileSystemAnchor } from "../system-anchor.js"; -import { stripAcpPanelMessages, stripAcpStatusMarkers } from "../acp-panel.js"; +import { stripAcpPanelGoogleContents, stripAcpStatusMarkersGoogleContents } from "../acp-panel.js"; import { stripEmbeddedChainCarriers } from "../chain-checkpoint.js"; import { renderNone as knobRenderNone } from "../knobs.js"; import { clampOutgoingOutput, countSystemAndToolsTokens, dshLedgerFloorTokens, emergencyNudge } from "./budget.js"; @@ -92,6 +92,18 @@ export async function prepareGoogle( // in processTurn, Prepared and countTokens) — the knob was dead config on /v1beta. const renderStrategy = knobRenderNone() ? "none" : "text-only"; + // Same incoming-history strip pass as the openai/anthropic/responses lanes: + // panels (#359) and ephemeral [ACP] status markers (#717/#1029) are proxy- + // injected artifacts, not conversation content — without this pass they + // ride into the compression state on every /v1beta turn (#2798). + const strippedPanels = stripAcpPanelGoogleContents(parsed.contents); + if (strippedPanels > 0) { + log("info", `[${sessionId}] stripped ${strippedPanels} ACP panel message(s) before projection (UI-only, issue #359)`); + } + const strippedMarkerLines = stripAcpStatusMarkersGoogleContents(parsed.contents); + if (strippedMarkerLines > 0) { + log("info", `[${sessionId}] stripped ${strippedMarkerLines} ACP status marker line(s) from incoming history (ephemeral proxy status, issue #1029)`); + } const strippedCarriers = stripEmbeddedChainCarriers(parsed, "google"); if (strippedCarriers > 0) { log("info", `[${sessionId}] stripped ${strippedCarriers} embedded chain checkpoint(s) from incoming history (leaked egress control data, issue #1542)`); @@ -312,6 +324,16 @@ export function prepareGoogleCountTokens( ): Prepared { const sessionId = session.id; try { + // The measurement must match what prepareGoogle forwards (#2798): + // upstream counts the payload the proxy would actually send. + const strippedPanels = stripAcpPanelGoogleContents(parsed.contents); + if (strippedPanels > 0) { + log("info", `[${sessionId}] stripped ${strippedPanels} ACP panel message(s) before projection (UI-only, issue #359)`); + } + const strippedMarkerLines = stripAcpStatusMarkersGoogleContents(parsed.contents); + if (strippedMarkerLines > 0) { + log("info", `[${sessionId}] stripped ${strippedMarkerLines} ACP status marker line(s) from incoming history (ephemeral proxy status, issue #1029)`); + } const { msgs } = googleToCore(parsed); // Read-only preview: the store rides in so placeholder substitution is // counted, but nothing is adopted (state is discarded here too). diff --git a/tests/issue2798-google-acp-strip.test.ts b/tests/issue2798-google-acp-strip.test.ts new file mode 100644 index 0000000000..95a377b5b8 --- /dev/null +++ b/tests/issue2798-google-acp-strip.test.ts @@ -0,0 +1,268 @@ +// #2798 regressions: the Google (Gemini native) lane never ran the incoming +// ACP panel/marker strip passes — prepare-google imported both strippers but +// never called them, so panels (#359) and ephemeral [ACP] status markers +// (#717/#1029, incl. multi-line acp_status reports #2789/#2797) rode straight +// into the compression state on every /v1beta turn and were billed/replayed +// forever while the three sibling lanes stripped them. Covers: the new +// parts-aware strippers (incl. KDD #10 thinking-part protection), the +// fork-adoption id-parity mirror, the google countTokens twin (the anthropic +// twin's regression test lives in tests/count-tokens.test.ts), and a +// full-server proof that upstream /v1beta traffic arrives stripped. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { createCore, createInitialState, defaultConfig } from "acp-kernel"; +import { buildStatusPanel } from "acp-kernel/panel"; +import type { GoogleRequestBody } from "acp-kernel/wire"; +import type { Session } from "../src/session.ts"; +import { startServer } from "../src/server.ts"; +import { prepareGoogleCountTokens } from "../src/server/prepare-google.ts"; +import { stripAcpPanelGoogleContents, stripAcpStatusMarkersGoogleContents, wrapCacheReport } from "../src/acp-panel.ts"; +import { incomingCoreMessages } from "../src/fork-adoption.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import type { ProxyOptions } from "../src/config.ts"; + +function realPanel(): string { + return buildStatusPanel({ + version: "billion-context@0.1.64", + tokenCount: 100000, + systemPromptTokens: 0, + state: createInitialState(), + nudge: undefined, + modelContextLimit: 200000, + }); +} + +const FALLBACK = "📊 ACP status\n context: 12.3K / 200.0K (6.2%)\n requests: 7"; +const MARKER_FAIL = "❌ [ACP] Compression FAILED: no valid ranges parsed."; +const MARKER_OK = "📦 [ACP] Compressed m00001–m00010."; + +function makeSession(): Session { + return { + id: `i2798-${Math.random().toString(36).slice(2)}`, + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0, compressCreditTokens: 0, retrieveCalls: 0, retrieveHits: 0, retrieveMisses: 0, storedBytes: 0, storeBytesSaved: 0, rangeRestores: 0 }, + metadata: {}, + state: createInitialState(), + pendingRetrievals: [], + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + }; +} + +test("#2798 stripAcpPanelGoogleContents removes whole panel user contents (box/fallback/reports)", () => { + const contents: Array> = [ + { role: "user", parts: [{ text: "hi" }] }, + { role: "user", parts: [{ text: realPanel() }] }, + { role: "user", parts: [{ text: FALLBACK }] }, + { role: "user", parts: [{ text: wrapCacheReport("cache report body") }] }, + { role: "model", parts: [{ text: realPanel() }] }, + { role: "user", parts: [{ text: `${realPanel()}\nmy follow-up question` }] }, + ]; + assert.equal(stripAcpPanelGoogleContents(contents), 3, "box + fallback + cache report removed"); + assert.deepEqual(contents.map((c) => c.role), ["user", "model", "user"]); +}); + +test("#2798 panel split across text parts is still removed (whole-content join)", () => { + const half = Math.floor(realPanel().length / 2); + const contents: Array> = [ + { role: "user", parts: [{ text: realPanel().slice(0, half) }, { text: realPanel().slice(half) }] }, + { role: "user", parts: [{ text: "keep me" }] }, + ]; + assert.equal(stripAcpPanelGoogleContents(contents), 1); + assert.equal(contents.length, 1); + assert.equal((contents[0]!.parts as Array<{ text: string }>)[0]!.text, "keep me"); +}); + +test("#2798 any non-text/signatured part makes a content panel-ineligible (preserved)", () => { + const panel = realPanel(); + const cases: Array<{ name: string; parts: unknown[] }> = [ + { name: "inlineData image part", parts: [{ text: panel }, { inlineData: { mimeType: "image/png", data: "x" } }] }, + { name: "functionCall part", parts: [{ text: panel }, { functionCall: { id: "c1", name: "f", args: {} } }] }, + { name: "functionResponse part", parts: [{ text: panel }, { functionResponse: { id: "c1", name: "f", response: {} } }] }, + { name: "thought part", parts: [{ text: panel.slice(0, 5), thought: true }, { text: panel.slice(5) }] }, + { name: "thoughtSignature-carrying part", parts: [{ text: panel, thoughtSignature: "sig" }] }, + { name: "model role", parts: [{ text: panel }] }, + ]; + for (const c of cases) { + const arr: Array> = c.name === "model role" + ? [{ role: "model", parts: c.parts }] + : [{ role: "user", parts: c.parts }]; + assert.equal(stripAcpPanelGoogleContents(arr), 0, `${c.name} must be preserved`); + assert.equal(arr.length, 1, `${c.name}: content never deleted`); + } +}); + +test("#2798 stripAcpPanelGoogleContents is a no-op on non-array input", () => { + assert.equal(stripAcpPanelGoogleContents({}), 0); + assert.equal(stripAcpPanelGoogleContents(undefined), 0); +}); + +test("#2798 stripAcpStatusMarkersGoogleContents strips marker lines from user/model/role-less text parts", () => { + const contents: Array> = [ + { role: "user", parts: [{ text: `继续\n${MARKER_FAIL}\n后面还有内容` }] }, + { role: "model", parts: [{ text: `answer\n${MARKER_OK}` }] }, + { parts: [{ text: `role-less defaults to user side\n${MARKER_OK}` }] }, + { role: "model", parts: [{ text: "clean model text" }] }, + { role: "model", parts: [{ functionCall: { id: "c1", name: "compress", args: {} } }] }, + ]; + assert.equal(stripAcpStatusMarkersGoogleContents(contents), 3, "three marker lines removed"); + assert.equal((contents[0]!.parts as Array<{ text: string }>)[0]!.text, "继续\n后面还有内容"); + assert.equal((contents[1]!.parts as Array<{ text: string }>)[0]!.text, "answer\n"); + assert.equal((contents[2]!.parts as Array<{ text: string }>)[0]!.text, "role-less defaults to user side\n"); + assert.equal((contents[3]!.parts as Array<{ text: string }>)[0]!.text, "clean model text"); +}); + +test("#2798 multi-line acp_status report loses every marker line (#2789/#2797 shape)", () => { + const report = `${MARKER_OK}\n📊 [ACP] acp_status:\ntotal tokens 12345\n`; + const contents: Array> = [{ role: "user", parts: [{ text: report }] }]; + assert.equal(stripAcpStatusMarkersGoogleContents(contents), 2, "only marker-prefixed lines match"); + assert.equal((contents[0]!.parts as Array<{ text: string }>)[0]!.text, "total tokens 12345\n"); +}); + +test("#2798 thinking-family parts are byte-exact untouched (KDD #10)", () => { + const contents: Array> = [ + { role: "model", parts: [{ text: MARKER_FAIL, thought: true }] }, + { role: "model", parts: [{ text: `${MARKER_FAIL}\nreal`, thoughtSignature: "abc" }] }, + { role: "model", parts: [{ text: `${MARKER_FAIL}\nreal` }] }, + ]; + assert.equal(stripAcpStatusMarkersGoogleContents(contents), 1, "only the unsigned plain part is touched"); + assert.equal((contents[0]!.parts as Array<{ text: string }>)[0]!.text, MARKER_FAIL, "thought part byte-exact"); + assert.equal((contents[1]!.parts as Array<{ text: string }>)[0]!.text, `${MARKER_FAIL}\nreal`, "signed part byte-exact"); + assert.equal((contents[2]!.parts as Array<{ text: string }>)[0]!.text, "real", "unsigned part stripped (marker line incl. newline removed)"); +}); + +test("#2798 marker-only parts degrade to a space and are never deleted", () => { + const contents: Array> = [{ role: "model", parts: [{ text: MARKER_FAIL }] }]; + assert.equal(stripAcpStatusMarkersGoogleContents(contents), 1); + assert.equal(contents.length, 1, "part kept"); + assert.equal((contents[0]!.parts as Array<{ text: string }>)[0]!.text, " ", "degraded to a space"); + assert.equal(stripAcpStatusMarkersGoogleContents(null), 0, "non-array input is a no-op"); +}); + +test("#2798 fork-adoption google id-pass mirrors prepareGoogle (panels + markers stripped)", () => { + const body = { + contents: [ + { role: "user", parts: [{ text: realPanel() }] }, + { role: "model", parts: [{ text: `real answer\n${MARKER_FAIL}` }] }, + { role: "user", parts: [{ text: "next question" }] }, + ], + }; + const msgs = incomingCoreMessages("google", body); + assert.ok(msgs, "google adoption supported"); + assert.equal(msgs!.length, 2, "panel content gone from the projection"); + const texts = msgs!.map((m) => m.text ?? ""); + assert.ok(!texts.some((t) => t.includes("ACP Context Analysis")), "panel text absent"); + assert.ok(!texts.some((t) => t.includes("[ACP]")), "marker lines absent"); + assert.ok(texts.some((t) => t.includes("next question")), "real conversation text intact"); + assert.ok(texts.some((t) => t.includes("real answer")), "stripped model text keeps its payload"); +}); + +test("#2798 prepareGoogleCountTokens strips panels + markers before measuring", () => { + const session = makeSession(); + const core = createCore(); + const config = defaultConfig(200000); + const logs: string[] = []; + const parsed: GoogleRequestBody = { + contents: [ + { role: "user", parts: [{ text: realPanel() }] }, + { role: "model", parts: [{ text: `answer\n${MARKER_FAIL}` }] }, + { role: "user", parts: [{ text: "how much is this?" }] }, + ], + }; + const prepared = prepareGoogleCountTokens(parsed, core, config, (_l, m) => logs.push(m), session); + const out = JSON.parse(prepared.body.toString()) as { contents: unknown[] }; + assert.equal(out.contents.length, 2, "panel content not measured"); + assert.ok(!JSON.stringify(out.contents).includes("ACP Context Analysis"), "panel absent from measured payload"); + assert.ok(!JSON.stringify(out.contents).includes("[ACP]"), "markers absent from measured payload"); + assert.ok(logs.some((l) => /stripped 1 ACP panel message\(s\) before projection/.test(l)), "panel strip logged"); + assert.ok(logs.some((l) => /stripped 1 ACP status marker line\(s\) from incoming history/.test(l)), "marker strip logged"); + assert.equal(session.state.blocks.length, 0, "countTokens stays read-only"); +}); + +test("#2798 full-server: upstream /v1beta traffic arrives stripped of panels + markers", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const upstreamReqs: string[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + upstreamReqs.push(Buffer.concat(chunks).toString("utf8")); + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + const frame = (parts: Array>, finishReason?: string): void => { + const candidate: Record = { content: { role: "model", parts }, index: 0 }; + if (finishReason) candidate.finishReason = finishReason; + res.write(`data: ${JSON.stringify({ candidates: [candidate], modelVersion: "gemini-test" })}\n\n`); + }; + frame([{ text: "ok" }]); + frame([], "STOP"); + res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gemini-test": { context: 1_000_000 } } } } as ProxyOptions["routes"], + modelContextLimit: 1_000_000, + kernelConfig: defaultConfig(1_000_000), + compress: { injectTool: false, injectNudge: false }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + mitm: { enabled: false, domains: [] }, + }); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + try { + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1beta/models/gemini-test:streamGenerateContent?alt=sse`; + const res = await fetch(url, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ + contents: [ + { role: "user", parts: [{ text: realPanel() }] }, + { role: "model", parts: [{ text: `real answer\n${MARKER_FAIL}` }] }, + { role: "user", parts: [{ text: "how much is this?" }] }, + ], + generationConfig: { maxOutputTokens: 4096 }, + }), + }); + assert.equal(res.status, 200, `google turn failed: ${res.status}`); + const reply = await res.text(); + assert.ok(reply.includes("ok"), "lane still answers through the proxy"); + + assert.equal(upstreamReqs.length, 1, "exactly one upstream request"); + const sent = JSON.parse(upstreamReqs[0]!) as { contents: Array<{ role?: string; parts: Array<{ text?: string }> }> }; + assert.equal(sent.contents.length, 2, "panel content must not reach upstream"); + const allText = sent.contents.map((c) => (c.parts ?? []).map((p) => p.text ?? "").join("")).join("\n"); + assert.ok(!allText.includes("ACP Context Analysis"), "panel text must not reach upstream"); + assert.ok(!allText.includes("[ACP]"), "marker lines must not reach upstream"); + assert.ok(allText.includes("real answer"), "conversation payload preserved"); + assert.ok(allText.includes("how much is this?"), "trailing question preserved"); + } finally { + proxy.close(); + await once(proxy, "close"); + upstream.close(); + await once(upstream, "close"); + } +}); From 3be5fd47b4e0e1fcd85ee95d64cc68c3f1c13819 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sun, 11 Oct 2026 21:58:14 +0800 Subject: [PATCH 2/4] fix: strip ACP panels and status markers in the anthropic count_tokens preview too (in-passing, #2798) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Found while working #2798: prepareCountTokens (/v1/messages/count_tokens) forwarded the raw body to upstream for measurement while prepareAnthropic strips panels (#359) and [ACP] marker lines (#717/#1029) before projection — so the preview measured a payload that is never what the proxy sends. Same mechanism family as the google gap, trivially fixable in passing: run the same two strip passes at the top of the preview, same log lines, still read-only. OpenAI/Responses have no count_tokens endpoint, so this twin completes the set. Also drops the unused stripAcpPanelResponsesInput import from server.ts (vestigial; tsc does not flag it — noUnusedLocals is off). Behavior change (old->new): /v1/messages/count_tokens previously measured panel messages + marker lines verbatim; they are now excluded from the measured payload, matching what /v1/messages forwards. --- src/server.ts | 12 +++++++++++- tests/count-tokens.test.ts | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/server.ts b/src/server.ts index 9c56cd5a24..4c891c9b1e 100644 --- a/src/server.ts +++ b/src/server.ts @@ -104,7 +104,7 @@ import { isStrictReasoningEcho, modelIdOf, normalizeStrictEchoReasoning, normali export { isStrictReasoningEcho, normalizeStrictEchoReasoning, normalizeStrictEchoResponsesInput }; import { sanitizeResponsesInputIds, dropWhitespaceResponsesMessages, normalizeResponsesMessageItems } from "./loop/adapter-responses.js"; import { CODEX_COMPACT_HEALTH_RATIO, codexCompactMode, isCodexClient, hasCompactionTrigger, stripBiliCompactionItems, replaceBiliCompactionItems, codexCompactGate, codexCompactGatePre, buildTriggerForgeBody, mergeForgedSummaries } from "./codex-compact.js"; -import { stripAcpPanelMessages, stripAcpPanelResponsesInput, stripAcpStatusMarkers } from "./acp-panel.js"; +import { stripAcpPanelMessages, stripAcpStatusMarkers } from "./acp-panel.js"; import { emitPreflightError } from "./stream-error.js"; import { agentProviderRecipes } from "./agent-providers.js"; import { affinityToken, claudeSubagentAgentId, claudeSubagentSplit, clientConversationHeader, codexTurnIdentity, conversationHeaderSource, dshPersonaFingerprintApplies, instructionsFingerprintApplies, openaiSystemTextForPersona, preferPromptCacheKeyIdentity, shouldStampRelayAffinityPck, type ConversationIdentity } from "./session-id.js"; @@ -1792,6 +1792,16 @@ export function prepareCountTokens( ): Prepared { const sessionId = session.id; try { + // The measurement must match what prepareAnthropic forwards (#2798): + // upstream counts the payload the proxy would actually send. + const strippedPanels = stripAcpPanelMessages(parsed.messages); + if (strippedPanels > 0) { + log("info", `[${sessionId}] stripped ${strippedPanels} ACP panel message(s) before projection (UI-only, issue #359)`); + } + const strippedMarkerLines = stripAcpStatusMarkers(parsed.messages); + if (strippedMarkerLines > 0) { + log("info", `[${sessionId}] stripped ${strippedMarkerLines} ACP status marker line(s) from incoming history (ephemeral proxy status, issue #1029)`); + } const { msgs, cacheControls } = anthropicToCore(parsed); // Read-only preview: same policy as the google twin above. const turn = core.processTurn({ messages: msgs, state: session.state, config: ccrLoopConfig(session, config), tokenCount: usageGradeInputBaseline(session), renderTags: knobRenderNone() ? "none" : "text-only", contentStore: contentStoreOf(session) }); diff --git a/tests/count-tokens.test.ts b/tests/count-tokens.test.ts index 40b73b2c98..5fb5bb68d5 100644 --- a/tests/count-tokens.test.ts +++ b/tests/count-tokens.test.ts @@ -1,6 +1,7 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import { createCore, createInitialState, defaultConfig, coveredMessageIds } from "acp-kernel"; +import { buildStatusPanel } from "acp-kernel/panel"; import type { Session } from "../src/session.ts"; import { prepareCountTokens, isCountTokensRequest } from "../src/server.ts"; import { anthropicToCore, type AnthropicRequestBody } from "acp-kernel/wire"; @@ -194,3 +195,37 @@ test("isCountTokensRequest honors ACP_COUNT_TOKENS_PASSTHROUGH=1 escape hatch", } assert.ok(isCountTokensRequest("POST", "/v1/messages/count_tokens", true), "restored env re-enables compression"); }); + +// #2798 (in-passing): the count_tokens preview is forwarded to upstream for +// measurement, so it must measure what prepareAnthropic forwards — panels and +// ephemeral marker lines included. The main lane strips both before projection. +test("#2798 prepareCountTokens strips ACP panels + status markers before measuring", () => { + const session = makeSession(); + const core = createCore(); + const config = defaultConfig(200000); + const logs: string[] = []; + const panel = buildStatusPanel({ + version: "billion-context@0.1.64", + tokenCount: 100000, + systemPromptTokens: 0, + state: createInitialState(), + nudge: undefined, + modelContextLimit: 200000, + }); + const body: AnthropicRequestBody = { + model: "claude-test", + messages: [ + { role: "user", content: panel }, + { role: "assistant", content: `answer\n❌ [ACP] Compression FAILED: no valid ranges parsed.` }, + { role: "user", content: "count me" }, + ], + }; + const prepared = prepareCountTokens(body, core, config, (_l, m) => logs.push(m), session); + const out = JSON.parse(prepared.body.toString()) as { messages: unknown[] }; + assert.equal(out.messages.length, 2, "panel message not measured"); + assert.ok(!JSON.stringify(out.messages).includes("ACP Context Analysis"), "panel absent from measured payload"); + assert.ok(!JSON.stringify(out.messages).includes("[ACP]"), "markers absent from measured payload"); + assert.ok(logs.some((l) => /stripped 1 ACP panel message\(s\) before projection/.test(l)), "panel strip logged"); + assert.ok(logs.some((l) => /stripped 1 ACP status marker line\(s\) from incoming history/.test(l)), "marker strip logged"); + assert.equal(session.state.blocks.length, 0, "count_tokens stays read-only"); +}); From 670d5865d5f60444fa67715157efc7f3751e6602 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sun, 11 Oct 2026 22:31:30 +0800 Subject: [PATCH 3/4] test: regenerate google storage-render golden for the #2798 strip (intentional drift) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The golden-google drive runs compress.injectTool+injectNudge, so after the fold at body #9 the proxy injects its ephemeral [ACP] status-marker line into the next request's history (#717/#1029). Pre-fix that line rode into the google lane's projection on every subsequent turn; post-fix prepareGoogle strips it before projection — which is exactly what #2798 changes, now observable in the pin set. Delta is exactly the strip effect and nothing else: outbound bodies #10-#14 each shrink by a constant 268 bytes (the fixed marker content removed from contents); stateDigest/sectionDigests(state,stats,metadata)/stateKeyDigests(messageRefs,tokenSnapshot,lastPassIds) move accordingly; blockContents digest, blocks/nudge/deadRefs/hiddenOrphanRefs/terminalStreak digests, foldAt/editedAt/summaryFrom and stateShape (incl. refs count) are unchanged. The other three wires' goldens regenerate byte-identical (no git diff for chat/anthropic/responses), confirming no cross-lane effect. Regenerated with node --import tsx scripts/update-storage-render-goldens.ts (#2798, Closes via PR #2805). --- tests/golden/storage-render/google.json | 34 ++++++++++++------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/golden/storage-render/google.json b/tests/golden/storage-render/google.json index bbdb39e033..17c7274f5d 100644 --- a/tests/golden/storage-render/google.json +++ b/tests/golden/storage-render/google.json @@ -53,49 +53,49 @@ }, { "i": 10, - "sha": "f590857aa1861019975450034e902372e067427f23e5b4102f8123ccb7e8f1a9", - "bytes": 57716 + "sha": "e7e835984db26b6a0993458ad8242484a7bf23c54bf17cb6ecf4b6087d903ccd", + "bytes": 57448 }, { "i": 11, - "sha": "8022e731d4b6cd22aff8aee5a0599a8b4636b2edf6a0be7d03f910a82fe80886", - "bytes": 63465 + "sha": "2aac1b0cc58cd9a22f4a597b275f3c1262fa418c55408b0699b35fa02f732f18", + "bytes": 63197 }, { "i": 12, - "sha": "df03f5b73f7c9d7b76ec8c5f74e6d5fd2d3b552dd8c40a1fdd29d29a17657baf", - "bytes": 77470 + "sha": "4c28ff1ee929e4e9e4ed74272275b2036deebb8aa8a3c58e75121ace845ab27e", + "bytes": 77202 }, { "i": 13, - "sha": "08f2bdbafe6e11662420f13fa762804bcb1f1a17c3d084f829315421c5fcb3b2", - "bytes": 83219 + "sha": "a9a8697d432d96f5399b2749b52641ea4d0ff088a2d8899b43b615d8a2aafda3", + "bytes": 82951 }, { "i": 14, - "sha": "692bfbf9c4369f658b93ade34163b8e65ba720bc7acc1d918a616698fe546fcc", - "bytes": 91749 + "sha": "14b6a2a1f47c2f90ba6808d8eef65ff5a11c865b6b099e271f871ad0618e1ecb", + "bytes": 91481 } ], "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "cf121db625ff815cf5718937ca4b368b11153d032076c039fa2a37620fcada72", + "stateDigest": "dc97221ca3659144631b3db27cad9f1a128622264a4d4f82e09d92e4996ba7ca", "sectionDigests": { - "state": "3d8afb23f4d63b31", + "state": "545456567a64d474", "blockContents": "4cc83a808a659f28", - "stats": "c95acb7dd4c8d0b4", - "metadata": "7881c5c6d215d78f" + "stats": "b957f5c6f301c229", + "metadata": "83d091613c7b64bc" }, "stateKeyDigests": { "blocks": "9adb53303cd4b786", - "messageRefs": "88c0c8bd0af470a2", + "messageRefs": "c0808ac44ca02780", "nudge": "eb1e2250bf4d050d", "stats": "a9fb72796521427d", "nextBlockId": "d4735e3a265e16ee", "nextRunId": "d4735e3a265e16ee", - "tokenSnapshot": "7b878c9bb77cf91a", - "lastPassIds": "c167fddb9a47e6ca", + "tokenSnapshot": "5080181ee1a25a8c", + "lastPassIds": "011421d6cea66b93", "hiddenOrphanRefs": "4f53cda18c2baa0c", "deadRefs": "74234e98afe7498f", "terminalStreak": "5feceb66ffc86f38", From a48d5e099d9634cbb3c814d580ce09bcd58b5ecf Mon Sep 17 00:00:00 2001 From: ework-agent Date: Tue, 13 Oct 2026 09:00:07 +0800 Subject: [PATCH 4/4] test: adapt the #2798 google strip suite to the #2797 acp_status block semantics The merged master changed marker stripping from line-granular (stripMarkerLines) to block-granular (stripAcpStatusBlocks): an acp_status head now consumes its wrapped [acp-status]/[/acp-status] body or legacy report grammar, and end-of-part markers no longer leave a trailing newline. The google lane shares stripAcpStatusBlocks verbatim with the siblings (adapted in the merge commit), so this suite now pins the same shapes tests/acp-status-marker-block.test.ts does: wrapped whole-block removal, legacy grammar stop-at-prose, cut-mid-report consume-to-end, plus a signed-part-with-full-block byte-exact case (KDD #10). --- tests/issue2798-google-acp-strip.test.ts | 71 ++++++++++++++++++++---- 1 file changed, 61 insertions(+), 10 deletions(-) diff --git a/tests/issue2798-google-acp-strip.test.ts b/tests/issue2798-google-acp-strip.test.ts index 95a377b5b8..c823c74194 100644 --- a/tests/issue2798-google-acp-strip.test.ts +++ b/tests/issue2798-google-acp-strip.test.ts @@ -1,7 +1,7 @@ // #2798 regressions: the Google (Gemini native) lane never ran the incoming // ACP panel/marker strip passes — prepare-google imported both strippers but // never called them, so panels (#359) and ephemeral [ACP] status markers -// (#717/#1029, incl. multi-line acp_status reports #2789/#2797) rode straight +// (#717/#1029, incl. block-form acp_status reports #2789/#2797) rode straight // into the compression state on every /v1beta turn and were billed/replayed // forever while the three sibling lanes stripped them. Covers: the new // parts-aware strippers (incl. KDD #10 thinking-part protection), the @@ -18,7 +18,8 @@ import type { GoogleRequestBody } from "acp-kernel/wire"; import type { Session } from "../src/session.ts"; import { startServer } from "../src/server.ts"; import { prepareGoogleCountTokens } from "../src/server/prepare-google.ts"; -import { stripAcpPanelGoogleContents, stripAcpStatusMarkersGoogleContents, wrapCacheReport } from "../src/acp-panel.ts"; +import { stripAcpPanelGoogleContents, stripAcpStatusMarkersGoogleContents, wrapCacheReport, STATUS_REPORT_OPEN, STATUS_REPORT_CLOSE } from "../src/acp-panel.ts"; +import { buildVisibilityMarker } from "../src/loop/core.ts"; import { incomingCoreMessages } from "../src/fork-adoption.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; @@ -39,6 +40,31 @@ const FALLBACK = "📊 ACP status\n context: 12.3K / 200.0K (6.2%)\n requests: const MARKER_FAIL = "❌ [ACP] Compression FAILED: no valid ranges parsed."; const MARKER_OK = "📦 [ACP] Compressed m00001–m00010."; +// Same report-body fixtures as tests/acp-status-marker-block.test.ts (#2789): +// a realistic renderOverview body plus handleAcpStatus extras, in both the +// new sentinel-wrapped form and the pre-#2789 legacy unwrapped grammar. +const REPORT = [ + "ACTIVE SURFACE: pack=default | host=billion-context 0.1.192 (abc1234)", + "", + "CONTEXT BREAKDOWN", + " 0 tool (0%) | 0 text (0%) | 27.1K summaries (100%)", + "", + "COMPRESSED BLOCKS — 2 active (1.2K summary, 95.0K original)", + "", + ' b1 (T1) 474.4K→169 3 msgs "preflight overflow compress"', + ' b2 (T1) 400.0K→150 5 msgs "exploration"', + "", + 'Tip: buildStatusReport({scope:"uncompressed", view:"messages", tool:"bash"}) for per-message listing', +].join("\n"); +const EXTRAS = [ + "", + "BILLED INPUT (upstream usage): 912625 tok · est-view total 516609 tok · ratio 1.8×", + "Nudge: ACTIVE — EMERGENCY T2 distill: max pending 27093, usage 122%", + "Compressible ranges (1, oldest first):", + " m00005–m00040 36 msgs 48.2K [tool 90% | text 10%]", + "STORAGE — ok", +].join("\n"); + function makeSession(): Session { return { id: `i2798-${Math.random().toString(36).slice(2)}`, @@ -113,28 +139,53 @@ test("#2798 stripAcpStatusMarkersGoogleContents strips marker lines from user/mo ]; assert.equal(stripAcpStatusMarkersGoogleContents(contents), 3, "three marker lines removed"); assert.equal((contents[0]!.parts as Array<{ text: string }>)[0]!.text, "继续\n后面还有内容"); - assert.equal((contents[1]!.parts as Array<{ text: string }>)[0]!.text, "answer\n"); - assert.equal((contents[2]!.parts as Array<{ text: string }>)[0]!.text, "role-less defaults to user side\n"); + assert.equal((contents[1]!.parts as Array<{ text: string }>)[0]!.text, "answer", "a marker at end of part leaves no trailing newline (stripAcpStatusBlocks split/join, #2797)"); + assert.equal((contents[2]!.parts as Array<{ text: string }>)[0]!.text, "role-less defaults to user side"); assert.equal((contents[3]!.parts as Array<{ text: string }>)[0]!.text, "clean model text"); }); -test("#2798 multi-line acp_status report loses every marker line (#2789/#2797 shape)", () => { - const report = `${MARKER_OK}\n📊 [ACP] acp_status:\ntotal tokens 12345\n`; - const contents: Array> = [{ role: "user", parts: [{ text: report }] }]; - assert.equal(stripAcpStatusMarkersGoogleContents(contents), 2, "only marker-prefixed lines match"); - assert.equal((contents[0]!.parts as Array<{ text: string }>)[0]!.text, "total tokens 12345\n"); +test("#2798 wrapped acp_status block is removed whole from google parts (#2789/#2797)", () => { + const marker = buildVisibilityMarker("acp_status", REPORT); + const contents: Array> = [{ role: "user", parts: [{ text: `check state\n${marker}\nnow fold that range.` }] }]; + assert.equal(stripAcpStatusMarkersGoogleContents(contents), 1, "one marker head counted"); + const out = (contents[0]!.parts as Array<{ text: string }>)[0]!.text; + assert.ok(out.includes("check state"), "prose before survives"); + assert.ok(out.includes("now fold that range."), "prose after survives"); + assert.ok(!out.includes("acp_status result:"), "head gone"); + assert.ok(!out.includes(STATUS_REPORT_OPEN) && !out.includes(STATUS_REPORT_CLOSE), "sentinels gone"); + assert.ok(!out.includes("ACTIVE SURFACE"), "report body gone"); +}); + +test("#2798 legacy unwrapped acp_status body consumed by report grammar and stops at prose", () => { + const legacy = `intro\n📊 [ACP] acp_status result:\n${REPORT}${EXTRAS}\n\nAlright, let me fold that range.`; + const contents: Array> = [{ role: "model", parts: [{ text: legacy }] }]; + assert.equal(stripAcpStatusMarkersGoogleContents(contents), 1); + const out = (contents[0]!.parts as Array<{ text: string }>)[0]!.text; + assert.ok(out.startsWith("intro"), `intro kept, got: ${JSON.stringify(out)}`); + assert.ok(out.includes("Alright, let me fold that range."), "prose after the report survives"); + assert.ok(!out.includes("ACTIVE SURFACE") && !out.includes("BILLED INPUT") && !out.includes("STORAGE — ok"), "legacy report body gone"); +}); + +test("#2798 cut mid-report (open sentinel without close) consumes to end of part", () => { + const cut = `start\n📊 [ACP] acp_status result:\n${STATUS_REPORT_OPEN}\nACTIVE SURFACE: pack=default`; + const contents: Array> = [{ role: "user", parts: [{ text: cut }] }]; + assert.equal(stripAcpStatusMarkersGoogleContents(contents), 1); + assert.equal((contents[0]!.parts as Array<{ text: string }>)[0]!.text, "start", "everything after the open sentinel is consumed"); }); test("#2798 thinking-family parts are byte-exact untouched (KDD #10)", () => { + const signedBlock = `state\n${buildVisibilityMarker("acp_status", REPORT)}`; const contents: Array> = [ { role: "model", parts: [{ text: MARKER_FAIL, thought: true }] }, { role: "model", parts: [{ text: `${MARKER_FAIL}\nreal`, thoughtSignature: "abc" }] }, + { role: "model", parts: [{ text: signedBlock, thoughtSignature: "sig2" }] }, { role: "model", parts: [{ text: `${MARKER_FAIL}\nreal` }] }, ]; assert.equal(stripAcpStatusMarkersGoogleContents(contents), 1, "only the unsigned plain part is touched"); assert.equal((contents[0]!.parts as Array<{ text: string }>)[0]!.text, MARKER_FAIL, "thought part byte-exact"); assert.equal((contents[1]!.parts as Array<{ text: string }>)[0]!.text, `${MARKER_FAIL}\nreal`, "signed part byte-exact"); - assert.equal((contents[2]!.parts as Array<{ text: string }>)[0]!.text, "real", "unsigned part stripped (marker line incl. newline removed)"); + assert.equal((contents[2]!.parts as Array<{ text: string }>)[0]!.text, signedBlock, "signed part with a full acp_status block inside stays byte-exact"); + assert.equal((contents[3]!.parts as Array<{ text: string }>)[0]!.text, "real", "unsigned part stripped (marker line incl. newline removed)"); }); test("#2798 marker-only parts degrade to a space and are never deleted", () => {