diff --git a/src/acp-panel.ts b/src/acp-panel.ts index 71bc929a5e..8598775e57 100644 --- a/src/acp-panel.ts +++ b/src/acp-panel.ts @@ -276,3 +276,77 @@ 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 markers from user/model text parts of a Gemini contents +// array (in place); returns the head-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. Block semantics (acp_status +// head + wrapped/legacy report body, #2789) are shared verbatim with +// stripAcpStatusMarkers above via stripAcpStatusBlocks. +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 = stripAcpStatusBlocks(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.ts b/src/server.ts index bdad26c4b8..aeb41d0f97 100644 --- a/src/server.ts +++ b/src/server.ts @@ -105,6 +105,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, 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"; @@ -1969,6 +1970,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/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/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"); +}); diff --git a/tests/issue2798-google-acp-strip.test.ts b/tests/issue2798-google-acp-strip.test.ts new file mode 100644 index 0000000000..c823c74194 --- /dev/null +++ b/tests/issue2798-google-acp-strip.test.ts @@ -0,0 +1,319 @@ +// #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. 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 +// 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, 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"; +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."; + +// 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)}`, + 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", "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 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, 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", () => { + 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"); + } +});