Skip to content
74 changes: 74 additions & 0 deletions src/acp-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> | 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<string, unknown> | 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<string, unknown>;
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<string, unknown>;
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;
}
7 changes: 5 additions & 2 deletions src/fork-adoption.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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
Expand All @@ -125,6 +126,8 @@ export function incomingCoreMessages(protocol: WireProtocol, parsed: unknown): C
return responsesToCoreWithToolImages(clone as Parameters<typeof responsesToCoreWithToolImages>[0]).msgs;
}
if (protocol === "google") {
stripAcpPanelGoogleContents(clone.contents);
stripAcpStatusMarkersGoogleContents(clone.contents);
stripEmbeddedChainCarriers(clone, "google");
return googleToCore(clone as Parameters<typeof googleToCore>[0]).msgs;
}
Expand Down
11 changes: 11 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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) });
Expand Down
24 changes: 23 additions & 1 deletion src/server/prepare-google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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)`);
Expand Down Expand Up @@ -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).
Expand Down
35 changes: 35 additions & 0 deletions tests/count-tokens.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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");
});
Loading
Loading