From 6a23c0aa4bb6890372b7ebaf3d78a95e7173cf10 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 3 Oct 2026 13:14:07 +0800 Subject: [PATCH 1/3] fix(loop): replay redacted_thinking blocks byte-exact on the compress re-request (#1960) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Anthropic rejects a modified latest assistant message with 400 "thinking or redacted_thinking blocks ... cannot be modified". The in-turn compress re-request rebuilt that message from loop records, but a redacted_thinking block arrives whole in content_block_start (no delta carries its payload), so nothing was recorded and the rebuild silently dropped it — every request in the session then 400'd. - adapter-anthropic: capture the opaque redacted_thinking payload at content_block_start and emit a dedicated redacted_thinking event at block stop instead of sealing an empty reasoning segment - core: accumulate one round's assistant parts (reasoning / text / redacted) in STREAM order and emit them position-exact on re-request; redacted parts ride the rawAnthropicBlock sidecar, which coreToAnthropic re-emits verbatim - recovery: a 400 matching "cannot be modified" skips the degraded strip retry (it can only produce a second guaranteed 400) and surfaces the original upstream error Tests: tests/issue1960-loop-redacted.test.ts (byte-exact redacted replay, plain signed-thinking regression guard, no-retry recovery). Preflight: typecheck clean, targeted loop/thinking suites 185/185 green, build OK. --- src/loop/adapter-anthropic.ts | 17 +- src/loop/core.ts | 99 ++++++++--- tests/issue1960-loop-redacted.test.ts | 243 ++++++++++++++++++++++++++ 3 files changed, 334 insertions(+), 25 deletions(-) create mode 100644 tests/issue1960-loop-redacted.test.ts diff --git a/src/loop/adapter-anthropic.ts b/src/loop/adapter-anthropic.ts index 4fdb2c77c9..c61d283978 100644 --- a/src/loop/adapter-anthropic.ts +++ b/src/loop/adapter-anthropic.ts @@ -304,6 +304,10 @@ export function createAnthropicAdapter(requestBody: Record, ori let usageYielded = false; const indexMap = new Map(); const thinkingIndexes = new Set(); + // #1960: a redacted_thinking block arrives whole in content_block_start + // (no deltas) — capture its opaque payload so the rebuilt tail replays it + // byte-exact in position. + const redactedPayloads = new Map(); // #206: strip model-imitated render tags from PROSE deltas before // they reach the client (and before the loop accumulates them for // re-request rounds). Flush at the owning block's stop so held-back @@ -405,6 +409,9 @@ export function createAnthropicAdapter(requestBody: Record, ori if (block.type === "thinking" || block.type === "redacted_thinking") { thinkingIndexes.add(upstreamIndex); sawThinking = true; + if (block.type === "redacted_thinking" && typeof block.data === "string") { + redactedPayloads.set(upstreamIndex, block.data); + } } const ci = clientIndex++; indexMap.set(upstreamIndex, ci); @@ -469,6 +476,8 @@ export function createAnthropicAdapter(requestBody: Record, ori } else if (thinkingIndexes.delete(upstreamIndex)) { // Seal the current thinking segment so interleaved thinking // blocks each keep their own signature on rebuild. + // #1960: a redacted block has no captured reasoning — replay its + // opaque payload instead of sealing an empty segment. const ci = indexMap.get(upstreamIndex) ?? upstreamIndex; removeOpenBlock(ci); if (lastThinkingIndex !== null) { @@ -479,7 +488,13 @@ export function createAnthropicAdapter(requestBody: Record, ori lastThinkingIndex = null; } yield { kind: "meta", chunk: remapIndexInEvent(eventStr, ci), firstRoundOnly: false } as ParsedStreamEvent; - yield { kind: "reasoning", delta: "", blockEnd: true } as ParsedStreamEvent; + const redactedData = redactedPayloads.get(upstreamIndex); + if (redactedData !== undefined) { + redactedPayloads.delete(upstreamIndex); + yield { kind: "redacted_thinking", data: redactedData } as ParsedStreamEvent; + } else { + yield { kind: "reasoning", delta: "", blockEnd: true } as ParsedStreamEvent; + } } else { const ci = indexMap.get(upstreamIndex) ?? upstreamIndex; removeOpenBlock(ci); diff --git a/src/loop/core.ts b/src/loop/core.ts index 9b288e74d6..53c6eb3a36 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -81,6 +81,17 @@ function stripLoopThinking(messages: CoreMessage[]): CoreMessage[] { return messages.filter((m) => !isLoopThinking(m)); } +// #1960: Anthropic's "cannot be modified" 400 means the latest assistant +// message we sent differs from its own original response in a thinking-family +// block. The degraded strip-retry cannot undo that — the message is still +// rewritten (e.g. a redacted_thinking block lost in rebuild), so the retry +// guarantees a second 400 plus a swapped error. Propagate the original error +// immediately; the NEXT client request carries the client's own faithful +// history, which is what recovers the session. +function isThinkingModifiedError(e: unknown): boolean { + return e instanceof UpstreamHttpError && /cannot be modified/i.test(e.body); +} + // #1453 client-facing labels for transport failures that end the turn in-band. // Deliberately kind-level only: raw error text can embed endpoint hostnames/IPs, // which must never reach the client stream (the full masked chain goes to the @@ -183,9 +194,24 @@ export interface RequestOptions { resign?: (headers: Record, body: string | Buffer) => void; } +// #1960: one round's assistant message parts, in STREAM order. The rebuilt +// latest assistant message must match the upstream response byte-for-byte +// (Anthropic verifies thinking-family blocks verbatim), so reasoning +// segments, text runs and redacted_thinking blocks are recorded as they +// arrive instead of being re-laid out as [reasoning…, text, tool…]. +type RoundPart = + | { kind: "reasoning"; text: string; signature: string } + | { kind: "text"; text: string } + | { kind: "redacted"; data: string }; + export type ParsedStreamEvent = | { kind: "text"; delta: string; raw?: Buffer } | { kind: "reasoning"; delta: string; raw?: Buffer; signature?: string; blockEnd?: boolean } + // #1960: Anthropic redacted_thinking arrives whole in content_block_start + // (opaque base64 `data`). The loop must rebuild it byte-exact in position + // on re-request — Anthropic rejects modified thinking-family blocks — so + // it is its own event instead of riding the (empty) reasoning seal. + | { kind: "redacted_thinking"; data: string } | { kind: "tool_call"; name: string; callId: string; arguments: string; passthrough?: boolean; signature?: string } | { kind: "usage"; inputTokens?: number; outputTokens?: number; cachedTokens?: number; creationTokens?: number } // #1766: keys on the upstream terminal frame that bili does not model @@ -425,7 +451,7 @@ export async function* runCompressLoop( if (signal?.aborted) break; let assistantText = ""; let assistantReasoning = ""; - const reasoningSegments: { text: string; signature: string }[] = []; + const roundParts: RoundPart[] = []; let reasoningSealed = true; const calls: ToolCallEmit[] = []; let usage: { inputTokens?: number; outputTokens?: number; cachedTokens?: number; creationTokens?: number } = {}; @@ -479,7 +505,7 @@ export async function* runCompressLoop( for (;;) { assistantText = ""; assistantReasoning = ""; - reasoningSegments.length = 0; + roundParts.length = 0; reasoningSealed = true; calls.length = 0; usage = {}; @@ -497,6 +523,12 @@ export async function* runCompressLoop( if (signal?.aborted) break; if (ev.kind === "text") { assistantText += ev.delta; + let tp = roundParts[roundParts.length - 1]; + if (!tp || tp.kind !== "text") { + tp = { kind: "text", text: "" }; + roundParts.push(tp); + } + tp.text += ev.delta; if (!ctx.textProtocol && ev.raw) { yield fwd(ev.raw, true); } else if (!ctx.textProtocol && round > 1 && ev.delta.length > 0) { @@ -504,10 +536,10 @@ export async function* runCompressLoop( } } else if (ev.kind === "reasoning") { assistantReasoning += ev.delta; - let seg = reasoningSegments[reasoningSegments.length - 1]; - if (reasoningSealed || !seg) { - seg = { text: "", signature: "" }; - reasoningSegments.push(seg); + let seg = roundParts[roundParts.length - 1]; + if (reasoningSealed || !seg || seg.kind !== "reasoning") { + seg = { kind: "reasoning", text: "", signature: "" }; + roundParts.push(seg); reasoningSealed = false; } seg.text += ev.delta; @@ -520,6 +552,8 @@ export async function* runCompressLoop( yield fwd(adapter.emitReasoning(ev.delta)); } } + } else if (ev.kind === "redacted_thinking") { + roundParts.push({ kind: "redacted", data: ev.data }); } else if (ev.kind === "tool_call") { calls.push({ name: ev.name, callId: ev.callId, arguments: ev.arguments, passthrough: ev.passthrough, signature: ev.signature }); } else if (ev.kind === "usage") { @@ -830,33 +864,49 @@ export async function* runCompressLoop( // must be passed back"). Relies on the invariant that the single // production call site (server.ts) always populates ctx.protocol. const requiresThinkingSignature = ctx.protocol === "anthropic" || ctx.protocol === "google"; - if (reasoningSegments.length > 0) { - for (let i = 0; i < reasoningSegments.length; i++) { - const seg = reasoningSegments[i]; - if (seg.text.length === 0 || (requiresThinkingSignature && seg.signature.length === 0)) continue; + let reasoningIdx = 0; + let textIdx = 0; + let redactedIdx = 0; + for (const part of roundParts) { + if (part.kind === "reasoning") { + if (part.text.length === 0 || (requiresThinkingSignature && part.signature.length === 0)) continue; + reasoningIdx += 1; const reasoningMsg: BiliMessage = { - id: i === 0 ? `acp_loop_r${round}_reasoning` : `acp_loop_r${round}_reasoning_${i + 1}`, + id: reasoningIdx === 1 ? `acp_loop_r${round}_reasoning` : `acp_loop_r${round}_reasoning_${reasoningIdx}`, role: "assistant", contentType: "reasoning", - text: seg.text, - reasoningContent: seg.text, - ...(seg.signature.length > 0 + text: part.text, + reasoningContent: part.text, + ...(part.signature.length > 0 ? ctx.protocol === "google" - ? { googleThoughtSignature: seg.signature } - : { thinkingSignature: seg.signature } + ? { googleThoughtSignature: part.signature } + : { thinkingSignature: part.signature } : {}), }; coreMessages.push(reasoningMsg); + } else if (part.kind === "text") { + if (part.text.length === 0) continue; + textIdx += 1; + coreMessages.push({ + id: textIdx === 1 ? `acp_loop_r${round}_asst` : `acp_loop_r${round}_asst_${textIdx}`, + role: "assistant", + contentType: "text", + text: part.text, + }); + } else { + // #1960: byte-exact, position-exact replay of the opaque + // block; coreToAnthropic re-emits rawAnthropicBlock verbatim. + redactedIdx += 1; + const redactedMsg: BiliMessage = { + id: redactedIdx === 1 ? `acp_loop_r${round}_redacted` : `acp_loop_r${round}_redacted_${redactedIdx}`, + role: "assistant", + contentType: "text", + text: "[redacted_thinking]", + rawAnthropicBlock: { type: "redacted_thinking", data: part.data }, + }; + coreMessages.push(redactedMsg); } } - if (assistantText.length > 0) { - coreMessages.push({ - id: `acp_loop_r${round}_asst`, - role: "assistant", - contentType: "text", - text: assistantText, - }); - } for (const pr of proxyResults) { if (functionCallIds.has(pr.callId)) { coreMessages.push({ @@ -1029,6 +1079,7 @@ export async function* runCompressLoop( e.status < 400 || e.status >= 500 || degradedRetried || + isThinkingModifiedError(e) || (ctx.protocol !== "anthropic" && ctx.protocol !== "google") || !coreMessages.some(isLoopThinking) ) { diff --git a/tests/issue1960-loop-redacted.test.ts b/tests/issue1960-loop-redacted.test.ts new file mode 100644 index 0000000000..996383051c --- /dev/null +++ b/tests/issue1960-loop-redacted.test.ts @@ -0,0 +1,243 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { CoreMessage } from "acp-kernel"; +import { createCore, createInitialState, defaultConfig, assignRefs, emptyRefMap } from "acp-kernel"; +import { anthropicToCore } from "acp-kernel/wire"; +import type { Session } from "../src/session.ts"; +import { runCompressLoop, createAnthropicAdapter } from "../src/loop/index.ts"; +import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; + +// #1960: on Anthropic extended thinking, an in-turn compress re-request rebuilt the +// latest assistant message WITHOUT the redacted_thinking block(s) it contained — +// the block arrives whole in content_block_start and no delta ever carries its +// payload, so nothing was recorded for rebuild. Anthropic rejects a modified latest +// assistant message with 400 "thinking or redacted_thinking blocks ... cannot be +// modified", and every later request re-sends the same lossy shape, stranding the +// session. +// +// Facts these tests pin (verified against the unfixed loop): +// - a redacted_thinking block emitted between thinking and tool_use replays +// byte-exact AND position-exact in the re-request's last assistant message; +// - the plain signed-thinking tail (no redacted block) is unchanged by the +// ordered-parts restructure (regression guard for #539/#221 behavior); +// - a 400 whose body matches the "cannot be modified" class skips the degraded +// strip retry (which can only produce a second guaranteed 400) and surfaces the +// original upstream error to the client. + +type WireMessage = Record; + +const THINKING_TEXT = "I should compress the old turns first before continuing."; +const SIG = "sig-round1-abc123"; +const RDT_DATA = "EvAFCkYIBxgCKkBkcm9kdGVkLXRoaW5raW5nLXByb2JlLTE5NjAAAA=="; +const COMPRESS_CALL_ID = "toolu_c1960"; +const SYSTEM_TEXT = "You are a coding agent."; + +function sse(type: string, data: unknown): string { + return `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; +} + +function makeSession(): Session { + return { + id: "issue1960-loop-test", + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 100, contextTokens: 100 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + }; +} + +const BIG = (tag: string) => `${tag}\n${"x".repeat(4000)}`; + +// Realistic pi mid-session inbound: prior turn had thinking+sig+real tool_use +// followed by its tool_result; big mid messages leave the two foldables outside +// the kernel's preserve-recent zone; ends with a plain user message. +function inbound(): Record { + return { + model: "claude-sonnet-5", + stream: true, + system: SYSTEM_TEXT, + messages: [ + { role: "user", content: "seed: audit the migration, then answer follow-ups." }, + { role: "assistant", content: [ + { type: "thinking", thinking: "HIST-THINK-A", signature: "sig-hist-a" }, + { type: "text", text: "Let me look at the files." }, + { type: "tool_use", id: "toolu_hist_1", name: "bash", input: { command: "ls -la" } }, + ] }, + { role: "user", content: [{ type: "tool_result", tool_use_id: "toolu_hist_1", content: "file_a.ts\nfile_b.ts" }] }, + { role: "user", content: BIG("FOLDABLE-ONE") }, + { role: "assistant", content: BIG("OLD-ANSWER") }, + { role: "user", content: BIG("FOLDABLE-TWO") }, + { role: "assistant", content: "short answer A" }, + { role: "user", content: BIG("MID-Q1") }, + { role: "assistant", content: BIG("MID-A1") }, + { role: "user", content: BIG("MID-Q2") }, + { role: "assistant", content: BIG("MID-A2") }, + { role: "user", content: BIG("MID-Q3") }, + { role: "assistant", content: BIG("MID-A3") }, + { role: "user", content: "and now the follow-up question, please continue" }, + ], + }; +} + +// Mirror of server.prepareAnthropic: anthropicToCore → processTurn. +function prepare(body: Record, session: Session) { + const core = createCore(); + const config = defaultConfig(200000); + const { msgs } = anthropicToCore(body) as { msgs: CoreMessage[] }; + const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount: 100, renderTags: "text-only" }); + session.state = turn.state; + return { core, config, processed: turn.messages, original: msgs }; +} + +function reFetchProbe(respond: (n: number) => Response): { calls: () => number; bodies: () => string[]; restore: () => void } { + let n = 0; + const bodies: string[] = []; + const orig = globalThis.fetch; + globalThis.fetch = (async (_u: unknown, init?: RequestInit) => { + n++; + if (init?.body) bodies.push(String(init.body)); + return respond(n); + }) as typeof fetch; + return { calls: () => n, bodies: () => bodies, restore: () => { globalThis.fetch = orig; } }; +} + +const ROUND2_SSE = [ + sse("message_start", { type: "message_start", message: { id: "msg_r2", usage: { input_tokens: 50 } } }), + sse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }), + sse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "Continued after compression." } }), + sse("content_block_stop", { type: "content_block_stop", index: 0 }), + sse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 5 } }), + sse("message_stop", { type: "message_stop" }), +].join(""); + +// Round 1 shape A: thinking + signature, then a WHOLE redacted_thinking block +// (real Anthropic wire form: full payload in content_block_start, no deltas), +// then the compress tool_use. +function round1Redacted(compressArgs: string): string { + return [ + sse("message_start", { type: "message_start", message: { id: "msg_r1", usage: { input_tokens: 100 } } }), + sse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "thinking", thinking: "" } }), + sse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "thinking_delta", thinking: THINKING_TEXT.slice(0, 20) } }), + sse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "thinking_delta", thinking: THINKING_TEXT.slice(20) } }), + sse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "signature_delta", signature: SIG } }), + sse("content_block_stop", { type: "content_block_stop", index: 0 }), + sse("content_block_start", { type: "content_block_start", index: 1, content_block: { type: "redacted_thinking", data: RDT_DATA } }), + sse("content_block_stop", { type: "content_block_stop", index: 1 }), + sse("content_block_start", { type: "content_block_start", index: 2, content_block: { type: "tool_use", id: COMPRESS_CALL_ID, name: "compress", input: {} } }), + sse("content_block_delta", { type: "content_block_delta", index: 2, delta: { type: "input_json_delta", partial_json: compressArgs } }), + sse("content_block_stop", { type: "content_block_stop", index: 2 }), + sse("message_delta", { type: "message_delta", delta: { stop_reason: "tool_use" }, usage: { output_tokens: 7 } }), + sse("message_stop", { type: "message_stop" }), + ].join(""); +} + +// Round 1 shape B (the common case): thinking + signature, then the compress +// tool_use — no redacted block. +function round1Plain(compressArgs: string): string { + return [ + sse("message_start", { type: "message_start", message: { id: "msg_r1", usage: { input_tokens: 100 } } }), + sse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "thinking", thinking: "" } }), + sse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "thinking_delta", thinking: THINKING_TEXT.slice(0, 20) } }), + sse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "thinking_delta", thinking: THINKING_TEXT.slice(20) } }), + sse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "signature_delta", signature: SIG } }), + sse("content_block_stop", { type: "content_block_stop", index: 0 }), + sse("content_block_start", { type: "content_block_start", index: 1, content_block: { type: "tool_use", id: COMPRESS_CALL_ID, name: "compress", input: {} } }), + sse("content_block_delta", { type: "content_block_delta", index: 1, delta: { type: "input_json_delta", partial_json: compressArgs } }), + sse("content_block_stop", { type: "content_block_stop", index: 1 }), + sse("message_delta", { type: "message_delta", delta: { stop_reason: "tool_use" }, usage: { output_tokens: 7 } }), + sse("message_stop", { type: "message_stop" }), + ].join(""); +} + +async function runLoop(round1: (args: string) => string, respond: (n: number) => Response): Promise<{ out: string; bodies: string[]; calls: number; compressArgs: string }> { + const session = makeSession(); + const body = inbound(); + const { core, config, processed, original } = prepare(body, session); + // The loop does not run the kernel ref-assignment pipeline (in production the + // proxy populates refs before the loop) — mirror issue539's test so the + // compress range resolves. + const refMap = assignRefs(processed, { existing: emptyRefMap(), nextIndex: 0 }).map; + session.state.messageRefs = refMap; + const refsOf = (needle: string) => Object.entries(refMap.byRaw ?? {}) + .filter(([raw]) => processed.find((x) => x.id === raw)?.text?.includes(needle)) + .map(([, r]) => r); + const f1 = refsOf("FOLDABLE-ONE")[0]; + const f2 = refsOf("FOLDABLE-TWO")[0]; + assert.ok(f1 && f2, "foldable payloads must resolve to refs"); + const compressArgs = JSON.stringify({ content: [{ startId: f1, endId: f2, summary: "SUMMARY-1960: two large old user payloads and one old answer, all fully consumed by later turns." }] }); + + const probe = reFetchProbe(respond); + let out = ""; + try { + const adapter = createAnthropicAdapter(body, SYSTEM_TEXT); + const ctx = { + core, config, + messages: processed, + session, + log: () => {}, + protocol: "anthropic" as const, + // Host mirror of server.ts refreshFolded: fresh fold from the ORIGINAL + // messages with the post-compress state + this round's acp_loop_* + // records riding on top. + refreshFolded: (current: CoreMessage[]) => { + const t = core.processTurn({ messages: original, state: session.state, config, tokenCount: 100, renderTags: "text-only" }); + session.state = t.state; + const records = current.filter((m) => typeof m.id === "string" && m.id.startsWith("acp_loop_")); + return [...t.messages, ...records]; + }, + }; + for await (const c of runCompressLoop(new Response(round1(compressArgs), { status: 200 }).body!, ctx, body, { url: "http://mock", headers: {} }, adapter, buildCompressSystemPrompt(config))) { + out += c.toString("utf8"); + } + } finally { + probe.restore(); + } + return { out, bodies: probe.bodies(), calls: probe.calls(), compressArgs }; +} + +const okResponse = () => new Response(ROUND2_SSE, { status: 200, headers: { "content-type": "text/event-stream" } }); + +test("#1960: redacted_thinking replays byte-exact and position-exact on the compress re-request", async () => { + const { bodies, calls, compressArgs } = await runLoop(round1Redacted, okResponse); + assert.equal(calls, 1, "exactly one re-request after the compress executes"); + const rb = JSON.parse(bodies[0]) as { messages: WireMessage[] }; + const lastAsst = [...rb.messages].reverse().find((m) => m.role === "assistant") as WireMessage | undefined; + assert.ok(lastAsst && Array.isArray(lastAsst.content), "re-request must end with the reconstructed assistant message"); + // The exact block order and bytes the real round-1 response carried: + assert.deepStrictEqual(lastAsst!.content, [ + { type: "thinking", thinking: THINKING_TEXT, signature: SIG }, + { type: "redacted_thinking", data: RDT_DATA }, + { type: "tool_use", id: COMPRESS_CALL_ID, name: "compress", input: JSON.parse(compressArgs) }, + ]); +}); + +test("#1960 regression guard: plain signed-thinking tail is unchanged by the ordered-parts restructure", async () => { + const { bodies, calls, compressArgs } = await runLoop(round1Plain, okResponse); + assert.equal(calls, 1, "exactly one re-request after the compress executes"); + const rb = JSON.parse(bodies[0]) as { messages: WireMessage[] }; + const lastAsst = [...rb.messages].reverse().find((m) => m.role === "assistant") as WireMessage | undefined; + assert.ok(lastAsst && Array.isArray(lastAsst.content)); + assert.deepStrictEqual(lastAsst!.content, [ + { type: "thinking", thinking: THINKING_TEXT, signature: SIG }, + { type: "tool_use", id: COMPRESS_CALL_ID, name: "compress", input: JSON.parse(compressArgs) }, + ]); +}); + +test("#1960 recovery: 'cannot be modified' 400 skips the guaranteed-failing strip retry", async () => { + const rejectBody = JSON.stringify({ + type: "error", + error: { + type: "invalid_request_error", + message: "messages.19.content.1: `thinking` or `redacted_thinking` blocks in the latest assistant message cannot be modified.", + }, + }); + const { out, calls } = await runLoop(round1Plain, (n) => + n === 1 ? new Response(rejectBody, { status: 400, headers: { "content-type": "application/json" } }) : okResponse()); + assert.equal(calls, 1, "no degraded strip retry after a 'cannot be modified' rejection"); + assert.ok(out.includes("cannot be modified"), "client sees the original upstream error text"); +}); From 29f60d3c7afbd7dd3f0082dcdc41aa2e77a5dd66 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 3 Oct 2026 16:08:21 +0800 Subject: [PATCH 2/3] fix(wire): keep redacted_thinking blocks verbatim on the Anthropic steady path (#1960) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The steady request path (client echoes the round back) silently dropped redacted_thinking blocks because anthropicToCore had no case for them, so every later request rebuilt the latest assistant message without the block and Anthropic answered 400 "cannot be modified" forever — the half of #1960 that kept the session stuck after the compress turn. - acp-kernel pin 0.0.99 → 0.0.101 (exact), which carries kernel PR #500 (redacted_thinking sidecar round-trip). Also rides along from v0.0.100+: #483 ref capacity widened to 9,999,999, #484 case-insensitive protected tool matching, #478 nudge cadence re-anchor, #493 language-preservation behind an opt-in flag (default off), and #499 prune/recommend shared per-turn index (hundreds of ms saved on ordinary requests in large sessions — release-note material). - tests/issue1960-steady-redacted.test.ts: client-echoed [thinking(sig), redacted(data), tool_use] must survive prepare→outbound byte-identical in order. Verified failing on 0.0.99, passing on 0.0.101. Preflight: typecheck clean; targeted loop/thinking/reasoning suites 186/186 green incl. both new #1960 files; build OK. --- package-lock.json | 8 +-- package.json | 2 +- tests/issue1960-steady-redacted.test.ts | 79 +++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 tests/issue1960-steady-redacted.test.ts diff --git a/package-lock.json b/package-lock.json index 42818f98cc..4e491844d6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,7 +19,7 @@ "@types/node": "^22.0.0", "@types/node-forge": "^1.3.14", "@types/semver": "^7.7.1", - "acp-kernel": "0.0.100", + "acp-kernel": "0.0.101", "fzstd": "0.1.1", "jsonc-parser": "^3.3.1", "node-forge": "^1.4.0", @@ -1973,9 +1973,9 @@ } }, "node_modules/acp-kernel": { - "version": "0.0.100", - "resolved": "https://registry.npmjs.org/acp-kernel/-/acp-kernel-0.0.100.tgz", - "integrity": "sha512-v+SoFJe+k5qYwvFfDdHZ46KK9VAlJ0PfZjLPPtVdXuwzUiRKDI02j1nh2OnqbD7HGO4e7lToEsWlU+Olme5WAg==", + "version": "0.0.101", + "resolved": "https://registry.npmjs.org/acp-kernel/-/acp-kernel-0.0.101.tgz", + "integrity": "sha512-zKqi1mW+oTcpSY79lKkQXdz9AXFrmRj7JL6Mfpuo6JXM0WqRebXHBkQUZz9VGGi1SsOOjK0D9NghZ1/mIakdAA==", "dev": true, "license": "MIT", "engines": { diff --git a/package.json b/package.json index c28a537294..76464457ff 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,7 @@ "@types/node": "^22.0.0", "@types/node-forge": "^1.3.14", "@types/semver": "^7.7.1", - "acp-kernel": "0.0.100", + "acp-kernel": "0.0.101", "fzstd": "0.1.1", "jsonc-parser": "^3.3.1", "node-forge": "^1.4.0", diff --git a/tests/issue1960-steady-redacted.test.ts b/tests/issue1960-steady-redacted.test.ts new file mode 100644 index 0000000000..e6132492bf --- /dev/null +++ b/tests/issue1960-steady-redacted.test.ts @@ -0,0 +1,79 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { CoreMessage } from "acp-kernel"; +import { createCore, createInitialState, defaultConfig } from "acp-kernel"; +import { anthropicToCore, coreToAnthropic } from "acp-kernel/wire"; +import type { Session } from "../src/session.ts"; + +// #1960 (steady path): after an in-turn compress, the client (pi) re-sends the +// FULL round-1 response as history — including the redacted_thinking block that +// sat between the signed thinking and the compress tool_use. The steady prepare +// path (anthropicToCore → processTurn → coreToAnthropic) used to drop that block +// silently (anthropicToCore had no case for it), so every later request rebuilt +// the latest assistant message without it and Anthropic answered 400 "thinking +// or redacted_thinking blocks ... cannot be modified" forever. +// +// This pins the steady contract: a client-echoed redacted_thinking block must +// come back out byte-identical and position-identical. Requires acp-kernel +// >= 0.0.101 (kernel PR ranxianglei/acp-kernel#500); fails on 0.0.99. + +const THINKING_TEXT = "I should compress the old turns first before continuing."; +const SIG = "sig-steady-xyz789"; +const RDT_DATA = "EvAFCkYIBxgCKkBkcm9kdGVkLXRoaW5raW5nLXByb2JlLTE5NjAAAA=="; +const COMPRESS_CALL_ID = "toolu_c1960s"; + +const COMPRESS_CONTENT = [ + { type: "thinking", thinking: THINKING_TEXT, signature: SIG }, + { type: "redacted_thinking", data: RDT_DATA }, + { type: "tool_use", id: COMPRESS_CALL_ID, name: "compress", input: { ranges: ["m00003–m00004"] } }, +] as const; + +function makeSession(): Session { + return { + id: "issue1960-steady-test", + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 100, contextTokens: 100 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + }; +} + +// pi's next request after the compress turn: the full round-1 assistant message +// (thinking + redacted + compress tool_use) echoed back, answered by its +// tool_result, then the user's follow-up. +function inbound(): Record { + return { + model: "claude-sonnet-5", + stream: true, + system: "You are a coding agent.", + messages: [ + { role: "user", content: "seed: audit the migration." }, + { role: "assistant", content: [{ type: "text", text: "Working on it." }] }, + { role: "user", content: "the context is getting long, please compress the old turns" }, + { role: "assistant", content: [...COMPRESS_CONTENT] }, + { role: "user", content: [{ type: "tool_result", tool_use_id: COMPRESS_CALL_ID, content: "[ACP] Compressed m00003–m00004 (shrink 20%)" }] }, + { role: "user", content: "and now the follow-up question, please continue" }, + ], + }; +} + +test("#1960 steady path: client-echoed redacted_thinking survives prepare→outbound verbatim", () => { + const session = makeSession(); + const core = createCore(); + const config = defaultConfig(200000); + const { msgs } = anthropicToCore(inbound()) as { msgs: CoreMessage[] }; + const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount: 100, renderTags: "text-only" }); + + const outbound = coreToAnthropic(turn.messages) as Array<{ role: string; content: Array> }>; + const holder = outbound.find((m) => + m.role === "assistant" && + m.content.some((b) => b.type === "tool_use" && (b as { id?: string }).id === COMPRESS_CALL_ID) + ); + assert.ok(holder, "outbound must still contain the assistant message holding the compress tool_use"); + assert.deepEqual(holder!.content, [...COMPRESS_CONTENT], "thinking + redacted_thinking + tool_use must round-trip byte-identical in order"); +}); From 00cf054c58b008b7d04199aa0cd6d61098fda232 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sat, 3 Oct 2026 18:27:13 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix(loop):=20signed=20thinking=20replays=20?= =?UTF-8?q?VERBATIM=20=E2=80=94=20drop=20the=20#1882=20thinking=20filters?= =?UTF-8?q?=20(#1960=20filter=20variant)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Anthropic thinking blocks and Gemini thoughtSignature parts are verified byte-for-byte when the latest assistant message is replayed, and the bytes the proxy forwards become the client's persisted history. The #1882 prose filters on the thinking/thought channels desynchronized text from signature on BOTH sides: the loop's re-request rebuild carried cleaned text, and the client echoed the cleaned bytes forever — the session 400s on every request after the first filtered thinking delta. Remove the thinking/thought filter instances; the TEXT channel keeps the #206/#1881 strips. Cosmetic tag/marker echoes inside a thinking pane are accepted (#1960 verdict). - adapter-anthropic: thinking_delta forwards + accumulates verbatim (raw chunk passthrough, no flush tail); redacted capture from #1980 unchanged - adapter-google: thought parts forward verbatim with their thoughtSignature; no editedParts rewrite for thought - tests: issue1960-thinking-verbatim (loop-level byte equality on re-request AND client-visible stream, both wires); issue1881 pins flipped for the two SIGNED channels (openai reasoning_content / responses summaries keep the strip) - AGENTS.md: Key Design Decision #10 — thinking-family payloads are signature-verified, never rewrite them; future changes need loop-level byte-equality tests on both signed wires --- AGENTS.md | 2 + src/loop/adapter-anthropic.ts | 75 ++------ src/loop/adapter-google.ts | 41 ++-- tests/issue1881-reasoning-tag-leak.test.ts | 15 +- tests/issue1960-thinking-verbatim.test.ts | 214 +++++++++++++++++++++ 5 files changed, 259 insertions(+), 88 deletions(-) create mode 100644 tests/issue1960-thinking-verbatim.test.ts diff --git a/AGENTS.md b/AGENTS.md index 420271bad9..68258a3388 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -47,6 +47,8 @@ The full file-by-file map lives in [reference/architecture.md](reference/archite 8. **Nudge cadence is flat 50K by design (kernel contract)** — acp-kernel pins the growth interval at 50000 for every window size (`nudge.growthFloor == nudge.growthCap == 50000`; window-percentage scaling was deliberately removed — #379/#380 settled "growth-driven, no usage/count proxy gates"). Do NOT re-scale the interval with the context window or reintroduce percentage gates; a 1M-window session folding every 50K of growth is intended lean-context behavior. User escape hatch: `compress.nudgeGrowthTokens` (flattens `growthFloor`+`growthCap` to a fixed step) — big-window users who want a lazier cadence set it explicitly. Prompt wording is kernel-owned (`src/nudge-text.ts`). 9. **Hosts that reuse ONE session id across personas get id + system-hash session keys ("hash 不一样自动分裂")** — when a plugin host stamps the SAME conversation id on every model request of a session INCLUDING auxiliary personas (dsh auto-review `classifyRisk()`: fixed REVIEW_POLICY system + a fresh flattened blob before every tool call — #1916/#1307/#1314), those auxiliary requests must be keyed by `subagentNamespace(id, systemText)` so the persona forks onto its own `|sub:` session instead of overwriting the main session's baseline/snapshots. The FIRST system seen under the id keeps the raw key (kernel anchor), so main turns never fork; an empty system is non-anchoring (verbatim). This is an EVIDENCE-PERMITLIST (`dshPersonaFingerprintApplies` in `src/session-id.ts`), the same discipline as the instructions fingerprint (#1104/#1107): do NOT enable it wholesale — for everyone else system drift mid-id means "same conversation, evolved", and forking resets compression for no defending bug (#1106). A future host found reusing session ids across personas gets added to that predicate WITH traffic evidence (shape + stability proof: the host's main system must be session-stable, e.g. dsh composes it once per session and puts volatile time-context in a USER message). The fork must record itself under its suffixed id (`personaForked` → `recordPluginSession`) so the single-valued conversations map keeps the raw key owned by the main session (#970 discipline). Tests: `tests/dsh-persona-fingerprint.test.ts`. +10. **Thinking-family blocks are signature-verified — NEVER rewrite them (#1960)** — Anthropic `thinking`/`redacted_thinking` blocks and Gemini `thoughtSignature` parts are verified byte-for-byte when the latest assistant message is replayed ("thinking blocks ... cannot be modified"). The bytes the proxy forwards become the client's persisted history, and the bytes the loop accumulates become the re-request rebuild — BOTH must stay verbatim. Any transformation (tag/marker stripping, dedup, trimming, re-laying-out block order, dropping empty segments) desynchronizes text from signature and bricks the session: first on the in-turn re-request, then on every subsequent request via the client's echo. #1882 put a prose filter on the thinking channel and it was exactly this bug. Treat ANY future change touching thinking-family payloads as high-risk: it needs loop-level byte-equality tests (re-request body AND client-visible stream) on both signed wires, not just adapter-level ones. The prose filters (#206/#1881) are for the TEXT channel only. + ### Install-Lane & Update-Ownership Contract (#1196) Every bili presence on a machine follows ONE contract — decide changes against it, not ad hoc: diff --git a/src/loop/adapter-anthropic.ts b/src/loop/adapter-anthropic.ts index c61d283978..3cc73f6bb5 100644 --- a/src/loop/adapter-anthropic.ts +++ b/src/loop/adapter-anthropic.ts @@ -152,37 +152,6 @@ function buildTextDeltaEvent(index: number, text: string): Buffer { ); } -function rewriteThinkingDeltaEvent(eventStr: string, newIndex: number, newThinking: string): Buffer { - const lines = eventStr.split("\n"); - const rebuilt: string[] = []; - for (const l of lines) { - if (l.startsWith("data:")) { - const jsonStr = l.slice(5).replace(/^ /, ""); - try { - const obj = JSON.parse(jsonStr) as Record; - if (typeof obj === "object" && obj !== null && typeof obj.index === "number") { - obj.index = newIndex; - const d = obj.delta as Record | undefined; - if (d && typeof d.thinking === "string") d.thinking = newThinking; - rebuilt.push(`data: ${JSON.stringify(obj)}`); - continue; - } - } catch { - } - } - rebuilt.push(l); - } - return Buffer.from(rebuilt.join("\n") + "\n\n", "utf8"); -} - -function buildThinkingDeltaEvent(index: number, thinking: string): Buffer { - return Buffer.from( - `event: content_block_delta\n` + - `data: ${JSON.stringify({ type: "content_block_delta", index, delta: { type: "thinking_delta", thinking } })}\n\n`, - "utf8", - ); -} - export function createAnthropicAdapter(requestBody: Record, originalSystem?: AnthropicRequestBody["system"], notes?: string[], errorShape: "protocol" | "completion" = "protocol", cacheMarks?: Map): CompressLoopAdapter { const model = (requestBody.model as string) ?? undefined; let messageId: string | undefined; @@ -329,7 +298,6 @@ export function createAnthropicAdapter(requestBody: Record, ori }), ); const tagFilter = makeFilter(); - const thinkingFilter = makeFilter(); let lastTextIndex: number | null = null; let lastThinkingIndex: number | null = null; let sawThinking = false; @@ -434,20 +402,23 @@ export function createAnthropicAdapter(requestBody: Record, ori yield { kind: "text", delta: clean, raw } as ParsedStreamEvent; } } else if (delta.type === "thinking_delta" && typeof delta.thinking === "string" && delta.thinking.length > 0) { - // #1881: thinking prose goes through its own filter — - // the raw passthrough here was the leak path. Signature - // deltas below stay verbatim (opaque, not prose). + // #1960: thinking is a SIGNED payload — Anthropic verifies it + // byte-for-byte when the latest assistant message is replayed + // ("thinking blocks ... cannot be modified"), and Gemini's + // thoughtSignature has the same contract. Any filtering here — + // of the bytes forwarded to the client OR of the text the loop + // accumulates for the re-request rebuild — desynchronizes the + // text from its signature and bricks the session (the client + // persists the filtered bytes and re-sends them forever). So the + // #1881/#1882 prose filters NEVER touch thinking; cosmetic + // tag/marker echoes inside a thinking pane are accepted. const ci = indexMap.get(upstreamIndex) ?? upstreamIndex; lastThinkingIndex = ci; - const clean = thinkingFilter.push(delta.thinking); - if (clean.length > 0) { - const raw = clean === delta.thinking ? remapIndexInEvent(eventStr, ci) : rewriteThinkingDeltaEvent(eventStr, ci, clean); - yield { - kind: "reasoning", - delta: clean, - raw, - } as ParsedStreamEvent; - } + yield { + kind: "reasoning", + delta: delta.thinking, + raw: remapIndexInEvent(eventStr, ci), + } as ParsedStreamEvent; } else if (delta.type === "signature_delta" && typeof delta.signature === "string") { const ci = indexMap.get(upstreamIndex) ?? upstreamIndex; yield { @@ -480,13 +451,7 @@ export function createAnthropicAdapter(requestBody: Record, ori // opaque payload instead of sealing an empty segment. const ci = indexMap.get(upstreamIndex) ?? upstreamIndex; removeOpenBlock(ci); - if (lastThinkingIndex !== null) { - const tail = thinkingFilter.flush(); - if (tail.length > 0) { - yield { kind: "reasoning", delta: tail, raw: buildThinkingDeltaEvent(lastThinkingIndex, tail) } as ParsedStreamEvent; - } - lastThinkingIndex = null; - } + lastThinkingIndex = null; yield { kind: "meta", chunk: remapIndexInEvent(eventStr, ci), firstRoundOnly: false } as ParsedStreamEvent; const redactedData = redactedPayloads.get(upstreamIndex); if (redactedData !== undefined) { @@ -563,13 +528,7 @@ export function createAnthropicAdapter(requestBody: Record, ori } lastTextIndex = null; } - if (lastThinkingIndex !== null) { - const tail = thinkingFilter.flush(); - if (tail.length > 0) { - yield { kind: "reasoning", delta: tail, raw: buildThinkingDeltaEvent(lastThinkingIndex, tail) } as ParsedStreamEvent; - } - lastThinkingIndex = null; - } + lastThinkingIndex = null; const stopExtras = terminalExtrasOf(data, MESSAGE_STOP_KNOWN_KEYS); if (!usageYielded) { usageYielded = true; diff --git a/src/loop/adapter-google.ts b/src/loop/adapter-google.ts index f562c3d81b..bc10dbb358 100644 --- a/src/loop/adapter-google.ts +++ b/src/loop/adapter-google.ts @@ -286,9 +286,14 @@ export function createGoogleAdapter( const rawCallChunks: { json: string; parsed: Record; callIndexes: number[] }[] = []; // #206/#717: strip model-imitated render tags and forged ACP // confirmation markers from prose parts; filters may hold back a - // short tail, flushed on finish (and at stream end). #1881: thought - // parts get their own instance — interleaved text/thought deltas on - // one shared state would misattribute held tails between fields. + // short tail, flushed on finish (and at stream end). + // #1960: there is deliberately NO filter instance for thought parts — + // thought text is signed (thoughtSignature is verified byte-for-byte + // on replay, the same fatality class as Anthropic thinking), so any + // filtering of the forwarded or accumulated bytes desynchronizes + // text from signature and bricks the session. Thought echoes are + // forwarded verbatim; cosmetic leaks inside a thought pane are + // accepted (#1960 verdict). const makeFilter = () => composeStreamFilters( composeStreamFilters( createTagEchoFilter((snippet) => { @@ -303,16 +308,11 @@ export function createGoogleAdapter( }), ); const tagFilter = makeFilter(); - const thoughtFilter = makeFilter(); const flushFilter = function* (): Generator { const tail = tagFilter.flush(); if (tail.length > 0) { yield { kind: "text", delta: tail, raw: buildChunk([{ text: tail }]) } as ParsedStreamEvent; } - const ttail = thoughtFilter.flush(); - if (ttail.length > 0) { - yield { kind: "reasoning", delta: ttail, raw: buildChunk([{ text: ttail, thought: true }]) } as ParsedStreamEvent; - } }; let callIndex = 0; let sawReasoning = false; @@ -500,28 +500,17 @@ export function createGoogleAdapter( if (typeof part.text !== "string" || part.text.length === 0) continue; if (part.thought === true) { sawReasoning = true; - const clean = thoughtFilter.push(part.text); - if (clean.length === 0) continue; + // #1960: thought parts replay VERBATIM — never filtered + // (thoughtSignature is verified byte-for-byte; see the + // makeFilter note above). emitted = true; let raw: Buffer | undefined; - if (clean === part.text) { - // A chunk that also carries functionCall parts is replayed - // whole at settle, so its text must not be forwarded twice. - if (!hasCallPart) raw = finishReason ? sseFrame(cloneChunk(parsed, { dropFinishReason: true })) : rawBuf; - } else { - editedParts = (editedParts ?? [...parts]).map((p, j) => (j === i ? { ...p, text: clean } : p)); - if (!hasCallPart) { - raw = sseFrame(cloneChunk(parsed, { - parts: parts - .map((p, j) => (j === i ? { ...p, text: clean } : p)) - .filter((p) => !p || typeof p !== "object" || p.functionCall === undefined), - dropFinishReason: true, - })); - } - } + // A chunk that also carries functionCall parts is replayed + // whole at settle, so its text must not be forwarded twice. + if (!hasCallPart) raw = finishReason ? sseFrame(cloneChunk(parsed, { dropFinishReason: true })) : rawBuf; yield { kind: "reasoning", - delta: clean, + delta: part.text, ...(raw ? { raw } : {}), ...(typeof part.thoughtSignature === "string" && part.thoughtSignature.length > 0 ? { signature: part.thoughtSignature } diff --git a/tests/issue1881-reasoning-tag-leak.test.ts b/tests/issue1881-reasoning-tag-leak.test.ts index 224185bf83..bfcda4a221 100644 --- a/tests/issue1881-reasoning-tag-leak.test.ts +++ b/tests/issue1881-reasoning-tag-leak.test.ts @@ -6,6 +6,12 @@ // D. google all-proxy call chunks still delivering their sibling prose // E. openai dropped proxy-only frames still delivering their sibling prose // F. google finish-stub chunks never duplicating already-settled prose +// #1960 REVERSAL: the two SIGNED channels (anthropic thinking, google thought) +// are no longer filtered — thought text is signature-verified byte-for-byte +// on replay, so stripping desynchronizes text from signature and bricks the +// session. Their pins below now assert VERBATIM passthrough; the unsigned +// channels (openai reasoning_content, responses reasoning summaries) keep +// the #1881 strip. import { test } from "node:test"; import assert from "node:assert/strict"; import { createOpenaiAdapter, createAnthropicAdapter, createResponsesAdapter, createGoogleAdapter } from "../src/loop/index.ts"; @@ -83,19 +89,20 @@ test("#1881 openai: a tool-frame replay carries cleaned reasoning bytes, argumen assert.ok(out.includes(JSON.stringify(args)), `arguments stay byte-exact (#1039): ${out}`); }); -test("#1881 anthropic: thinking_delta echo is stripped, thinking prose survives", async () => { +test("#1960 anthropic: signed thinking_delta echo rides VERBATIM (filtering bricks the session)", async () => { const adapter = createAnthropicAdapter({ model: "test" }); const events = await collect( adapter, sseEv("message_start", { type: "message_start", message: { id: "msg_1", usage: { input_tokens: 1 } } }) + sseEv("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "thinking", thinking: "" } }) + sseEv("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "thinking_delta", thinking: `${TAG}plan first` } }) + + sseEv("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "signature_delta", signature: "sig-1" } }) + sseEv("content_block_stop", { type: "content_block_stop", index: 0 }) + sseEv("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 1 } }) + sseEv("message_stop", { type: "message_stop" }), ); const out = clientBytes(events); - assert.ok(!out.includes("\x3cacp"), `no render tag may reach the client, got: ${out}`); + assert.ok(out.includes("\x3cacp"), `thinking is a SIGNED payload — the tag must ride verbatim or the signature breaks (#1960), got: ${out}`); assert.ok(out.includes("plan first"), `thinking prose survives: ${out}`); }); @@ -111,7 +118,7 @@ test("#1881 responses: reasoning_summary_text.delta echo is stripped, prose surv assert.ok(out.includes("think more"), `reasoning prose survives: ${out}`); }); -test("#1881 google: thought-part echo is stripped, thought prose survives once", async () => { +test("#1960 google: signed thought-part echo rides VERBATIM once (filtering bricks the session)", async () => { const adapter = createGoogleAdapter({ model: "gemini-3-pro-preview" }, undefined, "bili_absorb", "gemini-3-pro-preview"); const events = await collect( adapter, @@ -119,7 +126,7 @@ test("#1881 google: thought-part echo is stripped, thought prose survives once", sse({ candidates: [{ content: { role: "model", parts: [] }, finishReason: "STOP", index: 0 }], usageMetadata: { promptTokenCount: 1, candidatesTokenCount: 1 } }), ); const out = clientBytes(events); - assert.ok(!out.includes("\x3cacp"), `no render tag may reach the client, got: ${out}`); + assert.ok(out.includes("\x3cacp"), `thought text carries thoughtSignature — the tag must ride verbatim (#1960), got: ${out}`); assert.equal((out.match(/plan/g) ?? []).length, 1, `thought prose arrives once, got: ${out}`); }); diff --git a/tests/issue1960-thinking-verbatim.test.ts b/tests/issue1960-thinking-verbatim.test.ts new file mode 100644 index 0000000000..c376bd5d9a --- /dev/null +++ b/tests/issue1960-thinking-verbatim.test.ts @@ -0,0 +1,214 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { CoreMessage } from "acp-kernel"; +import { createCore, createInitialState, defaultConfig, assignRefs, emptyRefMap } from "acp-kernel"; +import { anthropicToCore } from "acp-kernel/wire"; +import { createGoogleAdapter, runCompressLoop, createAnthropicAdapter } from "../src/loop/index.ts"; +import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; +import type { Session } from "../src/session.ts"; + +// #1960 (filter variant): signed thinking/thought text must NEVER be filtered. +// Anthropic's thinking blocks and Gemini's thoughtSignature parts are verified +// byte-for-byte when the latest assistant message is replayed, and the bytes the +// proxy forwards to the client become the client's persisted history — so a +// #1881/#1882-style tag/marker strip on the thinking channel desynchronizes the +// text from its signature and bricks the session (first on the in-turn +// re-request, then on every subsequent request via the client's echo). These +// tests pin: thinking that quotes a render tag AND a marker line rides the +// re-request and the client-visible stream VERBATIM, on both signed wires. + +const TAG = "\x3cacp tokens=\"2\" type=\"text\"\x3em00176\x3c/acp\x3e"; +const THINKING_TEXT = `Plan: the old turns ${TAG} are stale; the marker line below is quoted too.\n[ACP] Compressed m00176\u2013m00177 (shrink 20%)\nI will call compress now.`; +const SIG = "sig-verbatim-1960"; +const COMPRESS_CALL_ID = "toolu_c1960f"; +const SYSTEM_TEXT = "You are a coding agent."; + +function sseEv(type: string, data: unknown): string { + return `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; +} + +function gSse(obj: unknown): string { + return `data: ${JSON.stringify(obj)}\n\n`; +} + +function makeSession(): Session { + return { + id: "issue1960-verbatim-test", + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 100, contextTokens: 100 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + }; +} + +const BIG = (tag: string) => `${tag}\n${"x".repeat(4000)}`; + +function inbound(): Record { + return { + model: "claude-sonnet-5", + stream: true, + system: SYSTEM_TEXT, + messages: [ + { role: "user", content: "seed: audit the migration, then answer follow-ups." }, + { role: "user", content: BIG("FOLDABLE-ONE") }, + { role: "assistant", content: BIG("OLD-ANSWER") }, + { role: "user", content: BIG("FOLDABLE-TWO") }, + { role: "assistant", content: "short answer A" }, + { role: "user", content: BIG("MID-Q1") }, + { role: "assistant", content: BIG("MID-A1") }, + { role: "user", content: BIG("MID-Q2") }, + { role: "assistant", content: BIG("MID-A2") }, + { role: "user", content: "and now the follow-up question, please continue" }, + ], + }; +} + +function prepare(body: Record, session: Session) { + const core = createCore(); + const config = defaultConfig(200000); + const { msgs } = anthropicToCore(body) as { msgs: CoreMessage[] }; + const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount: 100, renderTags: "text-only" }); + session.state = turn.state; + return { core, config, processed: turn.messages, original: msgs }; +} + +const ROUND2_SSE = [ + sseEv("message_start", { type: "message_start", message: { id: "msg_r2", usage: { input_tokens: 50 } } }), + sseEv("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }), + sseEv("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "Continued after compression." } }), + sseEv("content_block_stop", { type: "content_block_stop", index: 0 }), + sseEv("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 5 } }), + sseEv("message_stop", { type: "message_stop" }), +].join(""); + +function round1(compressArgs: string): string { + // Thinking quotes a render tag AND a full marker line — exactly the shapes + // the #1881/#1882 filters strip. With the filter removed the text must + // arrive whole on both the client stream and the re-request rebuild. + return [ + sseEv("message_start", { type: "message_start", message: { id: "msg_r1", usage: { input_tokens: 100 } } }), + sseEv("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "thinking", thinking: "" } }), + sseEv("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "thinking_delta", thinking: THINKING_TEXT.slice(0, 30) } }), + sseEv("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "thinking_delta", thinking: THINKING_TEXT.slice(30) } }), + sseEv("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "signature_delta", signature: SIG } }), + sseEv("content_block_stop", { type: "content_block_stop", index: 0 }), + sseEv("content_block_start", { type: "content_block_start", index: 1, content_block: { type: "tool_use", id: COMPRESS_CALL_ID, name: "compress", input: {} } }), + sseEv("content_block_delta", { type: "content_block_delta", index: 1, delta: { type: "input_json_delta", partial_json: compressArgs } }), + sseEv("content_block_stop", { type: "content_block_stop", index: 1 }), + sseEv("message_delta", { type: "message_delta", delta: { stop_reason: "tool_use" }, usage: { output_tokens: 7 } }), + sseEv("message_stop", { type: "message_stop" }), + ].join(""); +} + +async function runLoop(): Promise<{ out: string; bodies: string[]; calls: number; compressArgs: string }> { + const session = makeSession(); + const body = inbound(); + const { core, config, processed, original } = prepare(body, session); + const refMap = assignRefs(processed, { existing: emptyRefMap(), nextIndex: 0 }).map; + session.state.messageRefs = refMap; + const refsOf = (needle: string) => Object.entries(refMap.byRaw ?? {}) + .filter(([raw]) => processed.find((x) => x.id === raw)?.text?.includes(needle)) + .map(([, r]) => r); + const f1 = refsOf("FOLDABLE-ONE")[0]; + const f2 = refsOf("FOLDABLE-TWO")[0]; + assert.ok(f1 && f2, "foldable payloads must resolve to refs"); + const compressArgs = JSON.stringify({ content: [{ startId: f1, endId: f2, summary: "SUMMARY-1960F: two large old user payloads and one old answer, all fully consumed by later turns." }] }); + + let n = 0; + const bodies: string[] = []; + const orig = globalThis.fetch; + globalThis.fetch = (async (_u: unknown, init?: RequestInit) => { + n++; + if (init?.body) bodies.push(String(init.body)); + return new Response(ROUND2_SSE, { status: 200, headers: { "content-type": "text/event-stream" } }); + }) as typeof fetch; + let out = ""; + try { + const adapter = createAnthropicAdapter(body, SYSTEM_TEXT); + const ctx = { + core, config, + messages: processed, + session, + log: () => {}, + protocol: "anthropic" as const, + refreshFolded: (current: CoreMessage[]) => { + const t = core.processTurn({ messages: original, state: session.state, config, tokenCount: 100, renderTags: "text-only" }); + session.state = t.state; + const records = current.filter((m) => typeof m.id === "string" && m.id.startsWith("acp_loop_")); + return [...t.messages, ...records]; + }, + }; + for await (const c of runCompressLoop(new Response(round1(compressArgs), { status: 200 }).body!, ctx, body, { url: "http://mock", headers: {} }, adapter, buildCompressSystemPrompt(config))) { + out += c.toString("utf8"); + } + } finally { + globalThis.fetch = orig; + } + return { out, bodies, calls: n, compressArgs }; +} + +test("#1960 anthropic: signed thinking that quotes a render tag + marker line replays VERBATIM on the re-request", async () => { + const { bodies, calls, compressArgs } = await runLoop(); + assert.equal(calls, 1, "exactly one re-request after the compress executes"); + const rb = JSON.parse(bodies[0]) as { messages: Array<{ role: string; content: unknown }> }; + const lastAsst = [...rb.messages].reverse().find((m) => m.role === "assistant"); + assert.ok(lastAsst && Array.isArray(lastAsst.content), "re-request must end with the reconstructed assistant message"); + const thinking = (lastAsst.content as Array>).find((b) => b.type === "thinking"); + assert.ok(thinking, "thinking block present on the re-request"); + assert.equal(thinking!.thinking, THINKING_TEXT, "thinking text byte-identical to the model's original (tag AND marker line intact)"); + assert.equal(thinking!.signature, SIG, "signature unchanged"); + const toolUse = (lastAsst.content as Array>).find((b) => b.type === "tool_use"); + assert.ok(toolUse && toolUse.id === COMPRESS_CALL_ID, "compress tool_use still replayed"); + assert.deepEqual(toolUse!.input, JSON.parse(compressArgs), "tool_use arguments intact"); +}); + +// Reassemble the thinking text the CLIENT would reconstruct from the stitched +// stream: pull every thinking_delta payload out of the forwarded SSE frames. +function clientThinking(out: string): string { + let text = ""; + for (const m of out.matchAll(/data: (\{[^\n]*\})\n/g)) { + try { + const obj = JSON.parse(m[1]!) as { delta?: { type?: string; thinking?: string } }; + if (obj.delta?.type === "thinking_delta" && typeof obj.delta.thinking === "string") text += obj.delta.thinking; + } catch { + // non-JSON frames are irrelevant here + } + } + return text; +} + +test("#1960 anthropic: the client-visible stream carries the SAME verbatim thinking bytes", async () => { + const { out } = await runLoop(); + // The stitched client stream forwards the round-1 thinking deltas raw; if + // the filter were still in place these bytes would be gone (the tag and the + // marker line stripped) and the client's persisted history would diverge + // from the signature forever. The client reassembles the deltas, so the + // reassembled text is the fidelity that matters. + assert.equal(clientThinking(out), THINKING_TEXT, "client-visible thinking bytes reassemble to the model's original text"); +}); + +test("#1960 google: thought parts quoting a render tag reach reasoning VERBATIM with their signature", async () => { + const gThought = `Plan: fold ${TAG} now.`; + const adapter = createGoogleAdapter({}, undefined, undefined, "gemini-3-pro-preview"); + let deltas = ""; + let signature: string | undefined; + let rawText = ""; + for await (const ev of adapter.parseStream(new Response( + gSse({ candidates: [{ content: { role: "model", parts: [{ thought: true, text: gThought, thoughtSignature: "sig-g-1960" }] }, index: 0 }] }) + + gSse({ candidates: [{ content: { role: "model", parts: [] }, finishReason: "STOP", index: 0 }], usageMetadata: { promptTokenCount: 1, candidatesTokenCount: 1 } }), + { status: 200 }).body!, 1)) { + if (ev.kind === "reasoning") { + deltas += ev.delta; + if (ev.signature) signature = (signature ?? "") + ev.signature; + if (ev.raw) rawText += ev.raw.toString("utf8"); + } + } + assert.equal(deltas, gThought, "thought text unfiltered"); + assert.equal(signature, "sig-g-1960", "thoughtSignature rides the event"); + assert.ok(rawText.includes("acp tokens="), "client-visible raw frame carries the verbatim thought bytes (JSON-escaped inside the frame)"); +});