diff --git a/src/loop/core.ts b/src/loop/core.ts index 269bb7a600..34fc7ddfca 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -240,6 +240,9 @@ export interface LoopCtx { messages: CoreMessage[]; /** View handed to applyCompression (see RewriteCtx.compressMessages). */ compressMessages?: CoreMessage[]; + /** View the block-content ORIGINAL cache is built from (see + * RewriteCtx.blockContentSource). */ + blockContentSource?: CoreMessage[]; session: Session; reply?: HandoffReply; log: (msg: string) => void; diff --git a/src/plugin.ts b/src/plugin.ts index 867038aef8..384e8f14c1 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -2521,6 +2521,12 @@ export async function handlePluginTool( // permanent drift against the client's unchanged history // (same split as the relay loop ctx). compressMessages: mem && mem.original.length > 0 ? mem.original : undefined, + // The block-content ORIGINAL cache must be built from the + // TAGGED processed view the model saw (the fold view above is + // the raw client history without ACP tags — a full decompress + // must return originals with citable refs; session- + // continuity proof). + blockContentSource: messages, session, log: (m) => deps.log("info", `[${session.id}] [plugin] ${m}`), }, callId, undefined, deps.signal); diff --git a/src/stream.ts b/src/stream.ts index bd38ae7d71..9a420f3e24 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -31,6 +31,13 @@ export type RewriteCtx = { * `messages` view has pruned/hidden content (so block anchors can't * resolve) pass the unpruned log here (billion-context-pi#195). */ compressMessages?: CoreMessage[]; + /** View the block-content ORIGINAL cache is built from (applyRanges). + * Defaults to the fold view (`compressMessages ?? messages`, #6834). + * The plugin-tool lane passes the TAGGED processed view the model saw: + * its fold view is the raw client history WITHOUT ACP tags, and a full + * decompress must return originals with citable refs (session- + * continuity proof). */ + blockContentSource?: CoreMessage[]; session: Session; log: (msg: string) => void; debug?: boolean; @@ -1484,19 +1491,29 @@ export function applyRanges(parsed: ReturnType, ctx: const scrubbed = scrubTurnSeparatorIds(ctx.session.state.blocks); if (scrubbed > 0) ctx.log(`[acp-proxy: scrubbed ${scrubbed} turn-separator id(s) from fold coverage (#2627)]`); } - // Cache original content for newly-created blocks. At compress time the - // source messages are still in ctx.messages (this round's view, before - // the next processTurn folds them) — or in ctx.compressMessages when an - // EMPTY-view fold recovered its source from the unified store (#3982); - // collecting against [] would leave those blocks uncached for later - // decompress/range-restore. Storing the text here lets decompress work - // in later rounds where neither carries the originals. + // Cache original content for newly-created blocks. The source MUST be + // the SAME view applyCompression folded (ctx.compressMessages ?? + // ctx.messages, above): the display view (ctx.messages) can be CLIPPED + // by emergency truncation (middle of large tool-results replaced by a + // marker) while the raw resend stays complete — caching the clipped + // view would make a later whole-block full decompress serve incomplete + // originals even though the fold had the complete source (#6834). + // ctx.compressMessages also covers the EMPTY-view fold that recovered + // its source from the unified store (#3982); collecting against [] + // would leave those blocks uncached for later decompress/range-restore. + // EXCEPTION: the plugin-tool lane (plugin.ts) passes + // ctx.blockContentSource — the TAGGED processed view the model saw — + // because its fold view (mem.original) is the raw client history + // WITHOUT ACP tags, and a full decompress must return originals with + // citable refs (session-continuity proof). + // Storing the text here lets decompress work in later rounds where + // neither live view carries the originals. // Two views are cached so decompress can honor the `full` flag: `one` // (direct messages + nested child summaries) and `full` (all originals). // Leaf blocks have no active nested children, so both kernel paths // emit byte-identical text — persist a single copy in that case // (#401: the duplicate was 50% of blockContents bytes on disk). - const contentSource = ctx.messages.length > 0 ? ctx.messages : (ctx.compressMessages ?? ctx.messages); + const contentSource = ctx.blockContentSource ?? ctx.compressMessages ?? ctx.messages; for (const b of res.state.blocks) { if (beforeIds.has(b.blockId)) continue; const full = collectBlockContent(res.state, b, contentSource, { full: true }); diff --git a/tests/golden/storage-render/anthropic.json b/tests/golden/storage-render/anthropic.json index 7c01ccf504..fc040d3b16 100644 --- a/tests/golden/storage-render/anthropic.json +++ b/tests/golden/storage-render/anthropic.json @@ -95,11 +95,11 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "ba0f75f6ddc882d00d5c456facd79166b62e3f2cacadd60744aa370d6b247105", - "stateDigestAligned": "66991d5cb8864f3ca029914aa9afc20cc8b9687cfd4391ffe200017c68057946", + "stateDigest": "a882531ba02f5164119cde3582fe2432f35913f2b04936c790f1f00eb2183b64", + "stateDigestAligned": "4b461695c8096e255a59ed6e04a469605d9b9d6d3047a275f94dc5bf2ebe0499", "sectionDigests": { "state": "37b26b4f6898afff", - "blockContents": "dc7d13d250c94bb4", + "blockContents": "ae4924373cfee32c", "stats": "7caf981f99159be9", "metadata": "ba51746c6749d0cf" }, diff --git a/tests/golden/storage-render/chat.json b/tests/golden/storage-render/chat.json index 5ecde5722b..7447a1a723 100644 --- a/tests/golden/storage-render/chat.json +++ b/tests/golden/storage-render/chat.json @@ -95,11 +95,11 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "2c0b36206756617ae1e32cf0d221ddd8bf7f3e06994b49d11578bd1a35c90f26", - "stateDigestAligned": "3011800706759958cfeab6520289f1ffe257cf4f7c3275d846c03a991f9ed6b8", + "stateDigest": "d820e28a454624d66a1d36a54af15a2fd345fe31ad1d64133c69ce2d6c76cbdf", + "stateDigestAligned": "025b486ce691c33c9c0d230554f5f8c0bf08bd4c24e7f54911401c2beb602add", "sectionDigests": { "state": "90857fbf82001a2b", - "blockContents": "94a58f96d7c5cfc3", + "blockContents": "1beeb373751e1802", "stats": "7b6162aef3873d31", "metadata": "7218c1e3430c3d21" }, diff --git a/tests/golden/storage-render/google.json b/tests/golden/storage-render/google.json index 26c5f73b94..801c351d5a 100644 --- a/tests/golden/storage-render/google.json +++ b/tests/golden/storage-render/google.json @@ -95,11 +95,11 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "c5eefef6525f180ab54a7ea57c4ddf004a568db0fc2d6d227ede31dc960706db", - "stateDigestAligned": "5217aa02331256a419837cd91105d3e3172f2ff38f0f3914c0e01128e221f012", + "stateDigest": "9d2b0d82334cca96e022877d9bd73ed60c741c8b13287fef5fa1aa7aafceb155", + "stateDigestAligned": "fb33ea796c2e4136f436b6172ef164c59b78ef80dfb728424bb4995caebc78f2", "sectionDigests": { "state": "c5243ba17364d3ac", - "blockContents": "473264d9df8ba338", + "blockContents": "53facb801100a4da", "stats": "c90671e484dc5d37", "metadata": "2980a8959aa0a39c" }, diff --git a/tests/golden/storage-render/responses.json b/tests/golden/storage-render/responses.json index 8041cd5155..b0032e0ddd 100644 --- a/tests/golden/storage-render/responses.json +++ b/tests/golden/storage-render/responses.json @@ -95,11 +95,11 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "ff092bc66e11c35b15ad98acf6206629115825b62168da2e5542e06297f6d47a", - "stateDigestAligned": "db5f08a7d57c14952c31571e45b4dbc6c2ed35fe8a97c58cda6fa854d411eb25", + "stateDigest": "071239d1bd5f58b0de9af3f3bfcbb0dfa20f06da1abd6535dbb18c3daf39dcf7", + "stateDigestAligned": "f63058125564620dc5ca23abc280bffb66e95331400ed5c6431f7ec425c825b9", "sectionDigests": { "state": "bee6e1ba0c6fba62", - "blockContents": "b6701268a0bdcd8a", + "blockContents": "1f4ebc57f128d9e4", "stats": "1b56c993f03cd122", "metadata": "77714417e484ba89" }, diff --git a/tests/issue6834-emergency-truncate-block-cache.test.ts b/tests/issue6834-emergency-truncate-block-cache.test.ts new file mode 100644 index 0000000000..91fc001ffe --- /dev/null +++ b/tests/issue6834-emergency-truncate-block-cache.test.ts @@ -0,0 +1,98 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { resolveDecompress } from "../src/decompress-shared.ts"; +import { applyRanges } from "../src/stream.ts"; +import { parseCompressInput } from "../src/compress-tool.ts"; +import { createCore, createInitialState, defaultConfig, type Config, type CoreMessage } from "acp-kernel"; +import { getSession } from "../src/session.ts"; + +// #6834: emergency truncation clips the MIDDLE of large tool-results in the +// DISPLAY view (turn.messages → ctx.messages) while the raw resend +// (ctx.compressMessages) stays complete. applyCompression (stream.ts) folds +// from the complete source, but the block-content ORIGINAL cache used to prefer +// the nonempty display view — so a block folded while emergency truncation was +// armed cached the CLIPPED text, and a later whole-block full decompress served +// the wrong (clipped) cache even though compression had the complete source. +// +// The regression arms REAL emergency truncation (tokenCount past the 0.95 +// threshold), compresses a range spanning the clipped tool-result, clears both +// live views, and requests full restoration: the restored text must still carry +// the checksum that sat in the clipped middle. + +const CHECKSUM = "CHECKSUM_MID_6834_abc123"; + +function makeCtx() { + const core = createCore(); + const config = defaultConfig(200000) as Config; + const session = getSession(`t6834-${Math.random().toString(36).slice(2)}`); + return { core, config, session }; +} + +/** Build a history whose 3rd message is a large tool-result carrying CHECKSUM + * in its clipped middle, run processTurn with a tokenCount that arms REAL + * emergency truncation, and return the split views (display clipped, raw + * complete) plus the session. */ +function armEmergencyTruncate() { + const { core, config, session } = makeCtx(); + const bigToolResult = `${"A".repeat(4000)}\n${CHECKSUM}\n${"B".repeat(4000)}`; + const original: CoreMessage[] = [ + { id: "u1", role: "user", contentType: "text", text: "Run the build and report the result." }, + { id: "a1", role: "assistant", contentType: "tool-call", toolName: "bash", text: "npm run build" }, + { id: "t1", role: "tool", contentType: "tool-result", toolName: "bash", text: bigToolResult }, + { id: "a2", role: "assistant", contentType: "text", text: "The build succeeded." }, + { id: "u2", role: "user", contentType: "text", text: "Now run the tests." }, + { id: "a3", role: "assistant", contentType: "tool-call", toolName: "bash", text: "npm test" }, + { id: "t2", role: "tool", contentType: "tool-result", toolName: "bash", text: "ok 1 - all tests passed" }, + { id: "a4", role: "assistant", contentType: "text", text: "All tests passed." }, + ]; + // 0.95 * 200000 = 190000; 195000 arms the emergency-truncate node. + const turn = core.processTurn({ + messages: original, + state: session.state, + config, + tokenCount: 195000, + renderTags: "text-only", + }); + session.state = turn.state; + return { core, config, session, original, display: turn.messages }; +} + +test("#6834: emergency truncation clips the display view's tool-result middle", () => { + const { display, original } = armEmergencyTruncate(); + const clipped = display.find((m) => m.id === "t1"); + const complete = original.find((m) => m.id === "t1"); + assert.ok(clipped && complete, "tool-result present in both views"); + // The raw resend keeps the checksum; the display view lost its clipped middle. + assert.ok(complete!.text!.includes(CHECKSUM), "raw view keeps the checksum"); + assert.ok(!clipped!.text!.includes(CHECKSUM), "display view lost the checksum to emergency truncation"); + assert.ok(clipped!.text!.includes("[truncated for context space]"), "display view carries the truncation marker"); +}); + +test("#6834: full restoration after emergency truncation keeps the clipped middle (cache from complete source)", () => { + const { core, config, session, original, display } = armEmergencyTruncate(); + // Loop paths hand the folded/display view as `messages` and the raw resend + // as `compressMessages`; the original cache must be built from the latter. + const ctx = { core, config, messages: display, compressMessages: original, session, log: () => {} }; + applyRanges( + parseCompressInput({ + content: [ + { + startId: "m00001", + endId: "m00004", + summary: "Build ran and succeeded: the user asked to build, the build command was executed, and the assistant reported success before the user asked to run tests.", + }, + ], + }), + ctx as never, + ); + const blockId = [...session.state.blocks].slice(-1)[0]?.blockId; + assert.ok(blockId, "a block was created for the compressed range"); + assert.ok(session.blockContents.has(blockId!), "original cache populated at compress time"); + + // Clear BOTH live views (the cross-round case: neither carries the + // originals by the time decompress runs) and request full restoration. + ctx.messages = []; + ctx.compressMessages = []; + const out = resolveDecompress({ blockId: blockId!, full: true }, ctx as never).text; + assert.ok(out.includes(CHECKSUM), `full restoration must keep the clipped middle — checksum absent means the original cache was built from the truncated display view (#6834):\n${out.slice(0, 400)}`); +});