diff --git a/kernel/package-lock.json b/kernel/package-lock.json index bb7bd588bb..8a2c4491d7 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.200", + "version": "0.0.201", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.200", + "version": "0.0.201", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/kernel/package.json b/kernel/package.json index d20ac004ac..5ac694d897 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.200", + "version": "0.0.201", "description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.", "license": "MIT", "author": "ranxianglei", diff --git a/kernel/src/compress.ts b/kernel/src/compress.ts index 0357e4d239..3410e5ae8e 100644 --- a/kernel/src/compress.ts +++ b/kernel/src/compress.ts @@ -1412,6 +1412,7 @@ const hideCompressCallsNode: PipelineNode = { io.state, io.messages, ctx.config.isCompressTool, + ctx.config.strandedProtectedPredecessor, ); // #3769: fold the newly-swept orphans into the persistent monotonic set so // they stay hidden across turns/restarts/upgrades, then consume the fold diff --git a/kernel/src/hide-consumed.ts b/kernel/src/hide-consumed.ts index fd62ef7585..aafd4f6d58 100644 --- a/kernel/src/hide-consumed.ts +++ b/kernel/src/hide-consumed.ts @@ -246,11 +246,29 @@ function compactCompressText(text: string | undefined): string | null { return changed ? prefix + out : null; } +// #5864: does the assistant-message run ending at index `i` contain a protected +// (signed thinking / redacted_thinking) core anywhere before it? A run is the +// maximal stretch of consecutive assistant-role cores — exactly what +// coreToAnthropic renders as ONE assistant message — so the scan stops at the +// first non-assistant core. Lets the caller keep a compress pair whose thinking +// sibling is separated from it by intervening text/tool cores in the same turn. +function runHasStrandedProtectedPredecessor( + messages: CoreMessage[], + i: number, + pred: (m: CoreMessage) => boolean, +): boolean { + for (let j = i - 1; j >= 0 && messages[j]!.role === "assistant"; j--) { + if (pred(messages[j]!)) return true; + } + return false; +} + export function hideConsumedCompressCalls( state: CompressionState, messages: CoreMessage[], isCompressTool: (toolName: string) => boolean = (name) => name === COMPRESS_TOOL_NAME, + strandedProtectedPredecessor: (m: CoreMessage) => boolean = () => false, ): HideConsumedResult { const allBlockCallIds = new Set(); const activeCallIds = new Set(); @@ -352,6 +370,14 @@ export function hideConsumedCompressCalls( } } } + // #5864: hiding this pair would strand a signed thinking-family block that + // sits earlier in the SAME assistant message — an intermediate text/tool + // core between the thinking and the call is common, so the immediate + // predecessor alone is not enough. Signed thinking must round-trip with its + // tool_use sibling (#1658/#1960/#684) or upstream rejects the altered + // replay. Keep every compress pair whose assistant run holds a protected + // predecessor, not just the one right after the thinking. + if (hide && runHasStrandedProtectedPredecessor(messages, i, strandedProtectedPredecessor)) hide = false; if (hide) hiddenCallIds.add(callId); else keepCallIds.add(callId); } diff --git a/kernel/src/types.ts b/kernel/src/types.ts index 928504c10a..1373d8a329 100644 --- a/kernel/src/types.ts +++ b/kernel/src/types.ts @@ -585,6 +585,18 @@ export interface Config { * it (billion-context#2970). Unset = bare-name only (byte-identical to * before). Must be pure and cheap: runs once per message per pass. */ isCompressTool?: (toolName: string) => boolean; + /** Host-declared predicate marking a core whose position must never become + * the FINAL block of its assistant message — a signed Anthropic + * thinking/redacted_thinking block (#1658/#1960: signed thinking must + * round-trip with its tool_use sibling and cannot be dropped or rewritten). + * When the core immediately preceding a compress call matches, + * hideConsumedCompressCalls keeps that call+result pair VISIBLE instead of + * hiding it, so the thinking block is never stranded as a thinking-final + * assistant message (upstream 400 "the final block in an assistant message + * cannot be thinking", billion-context#5864). Unset = no protection + * (byte-identical to before). Must be pure and cheap: runs once per hidden + * compress call per pass. */ + strandedProtectedPredecessor?: (msg: CoreMessage) => boolean; /** Host-declared message-level hard protection (billion-context#2419): * when set, ANY message for which the predicate returns true is excluded * from compression exactly like a protectedTools hit — BLOCKED ref in diff --git a/package-lock.json b/package-lock.json index dd0c2f32d2..72519f7f77 100644 --- a/package-lock.json +++ b/package-lock.json @@ -51,7 +51,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.200", + "version": "0.0.201", "dev": true, "license": "MIT", "devDependencies": { diff --git a/src/config.ts b/src/config.ts index d5cba295e0..f11830da1f 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1658,6 +1658,17 @@ export function isMcpBiliCompress(toolName: string): boolean { toolName.slice(MCP_BILI_TOOL_PREFIX.length) === "compress"); } +// #5864: twin of server.ts hasSignedThinkingBlock (#1658/#1960 doctrine) — a +// signed Anthropic thinking/redacted_thinking core must never be stranded as +// the final block of its assistant message. hideConsumedCompressCalls uses it +// to keep a preceding compress pair visible instead of hiding it. Structural +// param (only the two wire fields it reads) so it stays assignable to the +// kernel's (msg: CoreMessage) => boolean Config field. +export function isStrandedProtectedPredecessor(m: { contentType?: string; thinkingSignature?: string; rawAnthropicBlock?: unknown }): boolean { + if (m.contentType === "reasoning") return typeof m.thinkingSignature === "string" && m.thinkingSignature.length > 0; + return (m.rawAnthropicBlock as { type?: unknown } | undefined)?.type === "redacted_thinking"; +} + export function loadOptions(env: NodeJS.ProcessEnv = process.env): ProxyOptions { // --- Source 1: JSON config file (~/.config/billion-context/billion-context.json) --- // The canonical, user-editable config. Loaded first so env vars below can @@ -1787,7 +1798,7 @@ export function loadOptions(env: NodeJS.ProcessEnv = process.env): ProxyOptions modelContextLimit, // #2578: host-side extension of the kernel's bare-name hard protection // to the MCP-prefixed forms of bili's own tools. - kernelConfig: { ...defaultConfig(modelContextLimit), isToolProtected: isMcpBiliHardProtected, isCompressTool: isMcpBiliCompress }, + kernelConfig: { ...defaultConfig(modelContextLimit), isToolProtected: isMcpBiliHardProtected, isCompressTool: isMcpBiliCompress, strandedProtectedPredecessor: isStrandedProtectedPredecessor }, compress: { ...(fileConfig.compress ?? {}), injectTool: (env.ACP_COMPRESS_TOOL ?? (fileConfig.compress?.injectTool === false ? "0" : "1")) !== "0", diff --git a/src/loop/core.ts b/src/loop/core.ts index 269bb7a600..4e2fe527dc 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -1653,7 +1653,7 @@ export async function* runCompressLoop( (pr) => (pr.name === "compress" || pr.name === "decompress") && proxyToolResultFailed(pr.result), ); if (!ctx.textProtocol && !anyCompressFailed) { - const hidden = hideConsumedCompressCalls(ctx.session.state, coreMessages, ctx.config.isCompressTool); + const hidden = hideConsumedCompressCalls(ctx.session.state, coreMessages, ctx.config.isCompressTool, ctx.config.strandedProtectedPredecessor); if (hidden.hidden > 0) { ctx.log(`[acp-loop] round ${round} hideConsumed hid ${hidden.hidden} compress record(s)`); replaceMessages(coreMessages, hidden.messages); diff --git a/src/stream.ts b/src/stream.ts index bd38ae7d71..2d7fa7691b 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -1,6 +1,6 @@ import { createHash, randomBytes } from "node:crypto"; import { collectBlockContent, countMessageTokens, defaultCountTokens, formatRanges, formatTagTime, hideConsumedCompressCalls, indexToRef, parseBoundary, storeCoveredOriginals, viableRanges, type CompressionCore, type Config, type CompressionBlock, type CompressibleRange, type CoreMessage, type CompressionState, type NudgeDecision, type CompressParseDiagnostics, refToIndex } from "acp-kernel"; -import { loadConfigFile, resolveMessageTimestamps } from "./config.js"; +import { isMcpBiliCompress, isStrandedProtectedPredecessor, loadConfigFile, resolveMessageTimestamps } from "./config.js"; import { excludeDeadRefRanges, handleAcpStatus, liveCompressionRanges } from "./acp-status.js"; import { handleAcpCache, prefixTokensBeforeRef, recordCacheFoldsFromBlocks } from "./cache-ledger.js"; import { type Session, cacheBlockContent, markDirty, residentPressure, scrubTurnSeparatorIds, statusInputBaseline, truncatedUpstreamOf, withoutTurnSeparators } from "./session.js"; @@ -907,19 +907,22 @@ function formatMiB(bytes: number | undefined): string { // variant: re-folding the same block silently SHRINKS its canonical summary). // The stub set is harvested from the kernel's OWN outbound rewrite of the // current messages rather than mirrored from constants: whatever fragments the -// model can actually see right now are exactly what gets rejected. Exact match +// model can actually see right now are exactly what gets rejected. The scan +// mirrors the outbound hide decision (#2970 MCP-name recognition + #5864 +// strand guard) so a pair the outbound path keeps visible is scanned here too. +// Exact match // only — legitimate prose (summaries containing an ellipsis, short summaries, // ±1-char transcription drift) can never trip it. export function visibleSummaryStubs(messages: CoreMessage[], state: CompressionState): Set { let hidden: { messages: CoreMessage[] }; try { - hidden = hideConsumedCompressCalls(state, messages); + hidden = hideConsumedCompressCalls(state, messages, isMcpBiliCompress, isStrandedProtectedPredecessor); } catch { return new Set(); } const stubs = new Set(); for (const m of hidden.messages) { - if (m.contentType !== "tool-call" || m.toolName !== COMPRESS_TOOL_NAME || typeof m.text !== "string") continue; + if (m.contentType !== "tool-call" || !m.toolName || !isMcpBiliCompress(m.toolName) || typeof m.text !== "string") continue; let parsed: ReturnType; try { // #5161: quiet — this scan re-parses past calls on every fold; in diff --git a/tests/issue5864-signed-thinking-orphan.test.ts b/tests/issue5864-signed-thinking-orphan.test.ts new file mode 100644 index 0000000000..e3faeeb4ad --- /dev/null +++ b/tests/issue5864-signed-thinking-orphan.test.ts @@ -0,0 +1,161 @@ +// #5864: Anthropic 400 "The final block in an assistant message cannot be +// `thinking`" right after a `[thinking, tool_use:compress]` turn whose +// compress block has gone inactive (a later fold consumed it). +// +// Root cause: two independent decisions about the same assistant turn can each +// be locally correct yet jointly illegal. +// 1. src/server.ts planReasoningDrop returns an EMPTY set whenever ANY signed +// thinking block is in view (hasSignedThinkingBlock gate, #1658/#1960): +// dropping a signed thinking block would desync its signature, so the +// pre-compress reasoning is NEVER dropped on extended-thinking sessions. +// 2. kernel hideConsumedCompressCalls removes an INACTIVE compress call+result +// pair independently of what precedes it. +// When both fire — the pair is hidden but the signed thinking survives — the +// assistant message collapses to `[thinking]`, which Anthropic rejects. Only +// turns where the fold boundary left the pair inactive while the signed +// thinking was still in the resent window hit it (hence 5-of-27 in the report). +// +// Fix: the hide decision must not strand a protected (signed thinking / +// redacted_thinking) block anywhere in the SAME assistant message — keep every +// compress pair whose assistant run holds one, not just the pair immediately +// after the thinking (an intervening text/tool core between the thinking and the +// call is common, and signed thinking must round-trip with its tool_use sibling +// per #1658/#1960/#684). On the Anthropic wire thinking is always signed +// (extended-thinking on ⇒ signed; off ⇒ absent), so guarding signed thinking is +// sufficient and leaves the unsigned #3222 behavior (drop the reasoning, hide the +// pair) untouched. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { createCore, createInitialState, type CompressionBlock } from "acp-kernel"; +import { coreToAnthropic, type BiliMessage } from "acp-kernel/wire"; +import { applyReasoningDrop, planReasoningDrop, stripKernelSummaries } from "../src/server.ts"; +import { loadOptions } from "../src/config.ts"; + +const THINKING = "x".repeat(3000); +const R = (id: string, sig?: string): BiliMessage => ({ id, role: "assistant", contentType: "reasoning", text: THINKING, ...(sig ? { thinkingSignature: sig } : {}) }); +const REDACTED = (id: string): BiliMessage => ({ id, role: "assistant", contentType: "text", text: "[redacted_thinking]", rawAnthropicBlock: { type: "redacted_thinking", data: "dGVzdA==" } }); +const TEXT = (id: string, text: string): BiliMessage => ({ id, role: "assistant", contentType: "text", text }); +const CALL = (toolCallId: string, id: string): BiliMessage => ({ id, role: "assistant", contentType: "tool-call", toolName: "mcp__bili__compress", toolCallId, text: "{}" }); +const RESULT = (toolCallId: string, id: string): BiliMessage => ({ id, role: "user", contentType: "tool-result", toolName: "mcp__bili__compress", toolCallId, text: "ok" }); +const USER = (id: string, text: string): BiliMessage => ({ id, role: "user", contentType: "text", text }); + +const CFG = { drop: true, threshold: 0 }; +const NOOP_LOG = (_level: string, _msg: string): void => {}; + +/** Inactive compress block(s) already consumed by an active parent — the exact + * shape that makes hideConsumedCompressCalls classify their calls "inactive". */ +function blockState(callIds: string[]): ReturnType { + const state = createInitialState(); + const consumed: string[] = []; + callIds.forEach((callId, n) => { + const blockId = `b${n + 1}`; + state.blocks.push({ + blockId, runId: `r${n}`, tier: 1, summary: `s${n}`, directMessageIds: [], + effectiveMessageIds: ["h1", "h2"], directBlockIds: [], createdAt: 0, + survivedCount: 0, generation: "young", active: false, compressCallId: callId, + compressedTokens: 0, + } as CompressionBlock); + consumed.push(blockId); + }); + state.blocks.push({ + blockId: "bTop", runId: "rTop", tier: 2, summary: "top", directMessageIds: [], + effectiveMessageIds: [], directBlockIds: consumed, createdAt: 0, + survivedCount: 0, generation: "young", active: true, compressedTokens: 0, + } as CompressionBlock); + return state; +} + +/** Drive the steady request path exactly as prepare-anthropic.ts does + * (plan before processTurn -> processTurn -> stripKernelSummaries -> + * applyReasoningDrop -> coreToAnthropic) and return the outbound wire. */ +function outboundWire(inbound: BiliMessage[], callIds: string[] = ["c1"]) { + const state = blockState(callIds); + const core = createCore(); + const config = loadOptions({}).kernelConfig; + const plan = planReasoningDrop(inbound, CFG, false); + const turn = core.processTurn({ messages: inbound, state, config, tokenCount: 100, renderTags: "text-only" }); + const processed = applyReasoningDrop(stripKernelSummaries(turn.messages as BiliMessage[], turn.state), plan, NOOP_LOG, "s5864"); + const wire = coreToAnthropic(processed, undefined) as Array<{ role: string; content: unknown }>; + return { plan, wire, turnMessages: turn.messages.map((m) => m.id) }; +} + +/** True when any assistant message's FINAL content block is a thinking-family + * block — the exact shape Anthropic rejects with the 400 in #5864. */ +function hasThinkingFinalAssistant(wire: Array<{ role: string; content: unknown }>): boolean { + for (const msg of wire) { + if (msg.role !== "assistant") continue; + if (!Array.isArray(msg.content)) continue; + const last = msg.content[msg.content.length - 1] as { type?: string } | undefined; + if (last && (last.type === "thinking" || last.type === "redacted_thinking")) return true; + } + return false; +} + +/** The first signed thinking block on the outbound wire, for byte-equality + * checks (#1960: the block must round-trip verbatim even when its sibling + * compress pair's args are compacted elsewhere in hide-consumed). */ +function thinkingBlockOf(wire: Array<{ role: string; content: unknown }>): { thinking?: string; signature?: string } | undefined { + for (const msg of wire) { + if (msg.role !== "assistant" || !Array.isArray(msg.content)) continue; + for (const b of msg.content as Array<{ type?: string; thinking?: string; signature?: string }>) { + if (b.type === "thinking") return b; + } + } + return undefined; +} + +test("#5864: signed thinking before an inactive compress call must not be stranded", () => { + const inbound = [USER("h1", "old question"), TEXT("h2", "old answer"), R("r1", "sig-abc"), CALL("c1", "c1"), RESULT("c1", "c1r"), USER("u2", "continue")]; + const { plan, wire, turnMessages } = outboundWire(inbound); + assert.equal(plan.size, 0, "signed thinking disables planReasoningDrop (#1658/#1960)"); + assert.ok(turnMessages.includes("c1"), "inactive compress call kept visible (signed-thinking predecessor)"); + assert.ok(turnMessages.includes("c1r"), "inactive compress result kept visible with its call"); + assert.equal(hasThinkingFinalAssistant(wire), false, "no assistant message ends in a thinking/redacted_thinking block (#5864)"); + // #1960: the signed thinking block itself reaches the wire byte-exact, + // independent of whatever the kept pair's args look like downstream. + const tb = thinkingBlockOf(wire); + assert.ok(tb, "a signed thinking block is present on the outbound wire"); + assert.equal(tb!.thinking, THINKING, "thinking text round-trips verbatim"); + assert.equal(tb!.signature, "sig-abc", "thinking signature round-trips verbatim"); +}); + +test("#5864: intervening text between signed thinking and compress still keeps the sibling", () => { + // [thinking, text, compress]: the immediate predecessor of the call is text, + // so an immediate-predecessor guard would hide the pair and strand the signed + // thinking (its tool_use sibling gone). The run-aware guard keeps it. + const inbound = [USER("h1", "q"), R("r1", "sig-abc"), TEXT("t1", "I'll compress now"), CALL("c1", "c1"), RESULT("c1", "c1r"), USER("u2", "continue")]; + const { plan, wire, turnMessages } = outboundWire(inbound); + assert.equal(plan.size, 0, "signed thinking disables planReasoningDrop"); + assert.ok(turnMessages.includes("c1"), "compress pair kept despite intervening text (run-aware, #5864)"); + assert.equal(hasThinkingFinalAssistant(wire), false, "no assistant message ends in a thinking block"); +}); + +test("#5864: multi-compress turn keeps the thinking's sibling, hides the rest", () => { + // [thinking, compress_1, compress_2]: compress_1 sits in the thinking's run and + // is kept; compress_2 begins a new run after compress_1's user-role result and + // is hidden. The thinking keeps a tool_use sibling either way. + const inbound = [USER("h1", "q"), R("r1", "sig-abc"), CALL("c1", "c1"), RESULT("c1", "c1r"), CALL("c2", "c2"), RESULT("c2", "c2r"), USER("u2", "continue")]; + const { plan, wire, turnMessages } = outboundWire(inbound, ["c1", "c2"]); + assert.equal(plan.size, 0, "signed thinking disables planReasoningDrop"); + assert.ok(turnMessages.includes("c1"), "first compress (same run as thinking) kept"); + assert.ok(!turnMessages.includes("c2"), "second compress (new run) hidden"); + assert.equal(hasThinkingFinalAssistant(wire), false, "no assistant message ends in a thinking block"); +}); + +test("#5864: redacted_thinking before an inactive compress call is protected too", () => { + // redacted_thinking rides a text core as rawAnthropicBlock (#1960); it arms + // both the drop gate and the strand guard identically to signed thinking. + const inbound = [USER("h1", "q"), REDACTED("rd1"), CALL("c1", "c1"), RESULT("c1", "c1r"), USER("u2", "continue")]; + const { plan, wire, turnMessages } = outboundWire(inbound); + assert.equal(plan.size, 0, "redacted_thinking disables planReasoningDrop (#1960)"); + assert.ok(turnMessages.includes("c1"), "compress pair kept (redacted_thinking predecessor)"); + assert.equal(hasThinkingFinalAssistant(wire), false, "no assistant message ends in a thinking/redacted_thinking block"); +}); + +test("#5864 control: UNSIGNED thinking takes the #3222 path (reasoning dropped, pair hidden)", () => { + const inbound = [USER("h1", "old question"), TEXT("h2", "old answer"), R("r1"), CALL("c1", "c1"), RESULT("c1", "c1r"), USER("u2", "continue")]; + const { plan, wire, turnMessages } = outboundWire(inbound); + assert.ok(plan.has("r1"), "unsigned closed-round reasoning is dropped (#3222/#651)"); + assert.ok(!turnMessages.includes("c1"), "unsigned path hides the consumed pair (#3222)"); + assert.equal(hasThinkingFinalAssistant(wire), false, "unsigned path never strands a thinking-final block"); +}); diff --git a/verif/subset-guard.ts b/verif/subset-guard.ts index 106c643bf3..dd65e0e21b 100644 --- a/verif/subset-guard.ts +++ b/verif/subset-guard.ts @@ -134,7 +134,7 @@ const GLOBAL_FNS = new Set([ "isNaN", "isFinite", "parseFloat", "parseInt", "String", "Number", "Boolean", "Object", "Array", // function-typed parameters (injected ports, uninterpreted): - "countTokens", "predicate", "isCompressTool", + "countTokens", "predicate", "isCompressTool", "strandedProtectedPredecessor", "pred", ]); /** Namespace roots (Math.x etc.) handled via NAMESPACE_MEMBERS. */