From 4986739c30377c71b18d527ea1d6d9ff50a9d016 Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 17 Nov 2026 01:31:26 +0800 Subject: [PATCH 1/3] kernel: classify tail-truncated stringified content arrays as truncated, not content-not-array (#6946) The vLLM-style gateway stringifies the compress content array, so content arrives as a JSON string of the array. When that string is cut mid-stream the prefix is a clean JSON-array start followed by EOF. The line-form fallback misread the JSON prefix as headerless prose and reported content-not-array / no-valid-ranges, steering the model away from a format that was legal all along. Three facets in parse-compress-input.ts: 1. salvageContentEntries recovers complete depth-0 STRING entries (not just {...} objects); a partial trailing string is dropped, never guessed. 2. An EOF gate in the string-content fallback: when the fence-stripped content starts with '[' and looksTruncated (unbalanced brackets / unterminated string = mid-stream cutoff), classify as truncated via finishSalvage instead of the line-form ladder. Balanced-but-malformed input keeps today's path (no regression). 3. Record diag.length + rawPrefix for a truncated stringified content array so the host argCorruption receipt fires instead of the misleading array-wrap hint. kernel-change: fix input-parse misclassification of truncated stringified content arrays; the host argCorruption receipt already exists, no host change needed. (cherry picked from commit 4ba234d5a29ae83633ca4e9b87e92a5dbe7292b6) --- kernel/package.json | 2 +- kernel/src/parse-compress-input.ts | 53 +++++++++++-- kernel/tests/parse-compress-input.test.ts | 92 +++++++++++++++++++++++ 3 files changed, 140 insertions(+), 7 deletions(-) diff --git a/kernel/package.json b/kernel/package.json index a58d31360b..f65ce78fdd 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.198", + "version": "0.0.199", "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/parse-compress-input.ts b/kernel/src/parse-compress-input.ts index 7ebc49c20a..9b67749638 100644 --- a/kernel/src/parse-compress-input.ts +++ b/kernel/src/parse-compress-input.ts @@ -51,9 +51,11 @@ export interface CompressParseDiagnostics { /** True when a non-array `content` value (single entry object or nested * `ranges` array) was recovered into the canonical array form (#470). */ contentSalvage?: boolean; - /** First 800 chars of the raw string input (string inputs only). */ + /** First 800 chars of the raw string input, or of the stringified + * content array when it was truncated (host argCorruption receipt). */ rawPrefix?: string; - /** Raw string input length (string inputs only). */ + /** Raw string input length, or the stringified content-array length when + * it was truncated (host argCorruption receipt). */ length?: number; /** Top-level keys of the parsed object — catches `content` vs `ranges` drift. */ keys?: string[]; @@ -202,6 +204,23 @@ function parseObjectValue( parsed === null || (parsed.entries.length === 0 && content.trim().length > 0) ) { + // A truncated JSON array — content opens with `[` and is structurally + // cut off (unbalanced brackets / an unterminated string, the + // mid-stream cutoff signature) — is NOT a line-form attempt. The + // line-form ladder below would misread the JSON prefix as headerless + // prose and report content-not-array / no-valid-ranges, steering the + // model away from a format that was legal all along. Classify it as + // truncated (finishSalvage already establishes "truncated input → + // kind=truncated" even with zero ranges) and record the length so the + // host's argCorruption receipt fires instead of the array-wrap hint. + // The gate fires ONLY on the truncation signature: a balanced-but- + // malformed document keeps today's line-form path (no regression). + const fenceStripped = stripFence(content.trim()); + if (fenceStripped.startsWith("[") && looksTruncated(fenceStripped)) { + diag.length = content.length; + diag.rawPrefix = clampPrefix(content, 800); + return finishSalvage(parsed?.entries ?? [], callId, diag, true); + } // Not a JSON array (or nothing salvageable): bare line-form text. // Strip JSON-wrapper residue (stringified ["…"] with broken // escaping) — the line form needs structure only in the refs @@ -255,6 +274,13 @@ function parseObjectValue( entries = parsed.entries; salvaged = parsed.salvaged; if (parsed.quoteRepaired) diag.quoteSalvage = true; + // A truncated content string that DID salvage complete entries (object + // or string) is still a truncated input; record the length so the host + // receipt reads as arg-corruption, not a generic no-valid-ranges. + if (salvaged) { + diag.length = content.length; + diag.rawPrefix = clampPrefix(content, 800); + } } else if (content !== null && typeof content === "object") { // #470: a single entry OBJECT in the content slot instead of a // one-element array — the SAME drift the top level already recovers @@ -977,9 +1003,11 @@ function looksTruncated(s: string): boolean { /** * Recover the complete entries of the `content` array from a truncated JSON * prefix. Walks the prefix with a small state machine (string / escape / - * bracket depth); every object that opens and closes at depth 1 is - * re-parsed leniently and kept only if it still parses. Partial entries are - * dropped — the parser never invents model content. + * bracket depth); every entry — an object `{…}` or a string `"…"` — that + * opens and closes at the top level of the content array (depth 0) is + * re-parsed leniently and kept only if it still parses. Partial entries + * (an unclosed trailing object or string) are dropped — the parser never + * invents model content. */ function salvageContentEntries(raw: string): unknown[] { const match = /"content"\s*:\s*\[/.exec(raw); @@ -995,11 +1023,24 @@ function salvageContentEntries(raw: string): unknown[] { if (inString) { if (escaped) escaped = false; else if (ch === "\\") escaped = true; - else if (ch === '"') inString = false; + else if (ch === '"') { + inString = false; + // A string that opened at depth 0 is a complete string entry (the + // line-form sibling of the object entries). Capture it; a partial + // trailing string never closes, so it is dropped, never guessed. + if (depth === 0 && entryStart !== -1) { + const entrySlice = raw.slice(entryStart, i + 1); + entryStart = -1; + const parsed = tryParseLenient(entrySlice); + if (parsed !== undefined) entries.push(parsed); + } + } continue; } if (ch === '"') { inString = true; + // A string opening at depth 0 is the start of a string entry. + if (depth === 0 && entryStart === -1) entryStart = i; continue; } if (ch === "{" || ch === "[") { diff --git a/kernel/tests/parse-compress-input.test.ts b/kernel/tests/parse-compress-input.test.ts index 2e548cc831..fd89753e34 100644 --- a/kernel/tests/parse-compress-input.test.ts +++ b/kernel/tests/parse-compress-input.test.ts @@ -292,6 +292,98 @@ test("parseCompressArgs does not fabricate partial entries", () => { assert.equal(ranges.length, 0); }); +// --------------------------------------------------------------------------- +// Truncated stringified content array (object input) — #6947 +// +// The vLLM-style gateway stringifies the content array, so `content` arrives +// as a JSON string of the array. When that string is cut mid-stream (the +// model's call truncated in flight), the prefix is a clean JSON-array start +// followed by EOF. Before this fix the line-form fallback misread the JSON +// prefix as headerless prose and reported content-not-array / no-valid-ranges, +// steering the model away from a format that was legal all along. The EOF +// gate classifies it as truncated; salvage recovers the complete entries +// (object or string). +// --------------------------------------------------------------------------- + +test("case A: object content string cut in the first object → truncated, not content-not-array", () => { + const content = + '[{"startRef": "m00001", "endRef": "m00002", "summary": "a long summary that is cut off mid-sentence without ever closing the string or the array'; + const { ranges, diagnostics } = parseCompressArgs({ content }); + assert.equal(diagnostics.kind, "truncated"); + assert.equal(diagnostics.ok, false); + assert.equal(ranges.length, 0); + // length recorded so the host argCorruption receipt (not the array-wrap + // hint) fires. + assert.equal(diagnostics.length, content.length); + assert.equal(diagnostics.rawPrefix, content); +}); + +test("case B: object content string cut in the second object → first range recovered, truncated", () => { + const content = + '[{"startRef": "m00001", "endRef": "m00002", "summary": "first complete"}, {"startRef": "m00003", "endRef": "m0'; + const { ranges, diagnostics } = parseCompressArgs({ content }); + assert.equal(diagnostics.kind, "truncated"); + assert.equal(diagnostics.ok, true); + assert.equal(ranges.length, 1); + assert.equal(ranges[0]?.summary, "first complete"); + assert.equal(diagnostics.length, content.length); +}); + +test("case C: object content string cut in the second STRING entry → first line-form entry recovered (salvage)", () => { + const content = + '["m00001–m00002 topic one\\nsummary one", "m00003–m00004 topic two\\nsummary two that got cut'; + const { ranges, diagnostics } = parseCompressArgs({ content }); + assert.equal(diagnostics.kind, "truncated"); + assert.equal(diagnostics.ok, true); + assert.equal(ranges.length, 1); + assert.equal(ranges[0]?.startRef, "m00001"); + assert.equal(ranges[0]?.endRef, "m00002"); + assert.equal(ranges[0]?.summary, "summary one"); + assert.equal(diagnostics.length, content.length); +}); + +test("case D: object content string cut in the first STRING entry → truncated, not no-valid-ranges", () => { + const content = + '["m00001–m00002 topic\\na long summary that is cut off mid-sentence without ever closing the string or the array'; + const { ranges, diagnostics } = parseCompressArgs({ content }); + assert.equal(diagnostics.kind, "truncated"); + assert.equal(diagnostics.ok, false); + assert.equal(ranges.length, 0); + assert.equal(diagnostics.length, content.length); +}); + +test("case E: object content string with complete string entries → ok (no regression)", () => { + const content = + '["m00001–m00002 topic one\\nsummary one", "m00003–m00004 topic two\\nsummary two"]'; + const { ranges, diagnostics } = parseCompressArgs({ content }); + assert.equal(diagnostics.kind, "ok"); + assert.equal(diagnostics.ok, true); + assert.equal(ranges.length, 2); + assert.equal(ranges[0]?.startRef, "m00001"); + assert.equal(ranges[1]?.startRef, "m00003"); + // Not truncated → no length recorded. + assert.equal(diagnostics.length, undefined); +}); + +test("no-regression: balanced empty array content keeps content-not-array, not truncated", () => { + const { ranges, diagnostics } = parseCompressArgs({ content: "[]" }); + assert.equal(diagnostics.kind, "content-not-array"); + assert.equal(diagnostics.ok, false); + assert.equal(ranges.length, 0); + // The EOF gate must not fire on balanced input → no length recorded. + assert.equal(diagnostics.length, undefined); +}); + +test("no-regression: non-array prose content keeps content-not-array, not truncated", () => { + const { ranges, diagnostics } = parseCompressArgs({ + content: "just some prose without any brackets", + }); + assert.equal(diagnostics.kind, "content-not-array"); + assert.equal(diagnostics.ok, false); + assert.equal(ranges.length, 0); + assert.equal(diagnostics.length, undefined); +}); + // --------------------------------------------------------------------------- // Error classification // --------------------------------------------------------------------------- From ccbca143a10cc8aa696d753844d1315915a8ac4c Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 17 Nov 2026 04:23:58 +0800 Subject: [PATCH 2/3] kernel: sync lockfiles to the 0.0.199 version bump (#6946) The kernel/package.json version bump (0.0.198 -> 0.0.199, cherry-picked from #6970) must be mirrored in BOTH the root package-lock.json's acp-kernel entry and the kernel's standalone package-lock.json (#3782), or the lock-sync gate fails. Version-only sync, no other churn. --- kernel/package-lock.json | 4 ++-- package-lock.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/package-lock.json b/kernel/package-lock.json index 9437e48cf1..3e25df6ca0 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.198", + "version": "0.0.199", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.198", + "version": "0.0.199", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/package-lock.json b/package-lock.json index d12b556eb1..006bda1e14 100644 --- a/package-lock.json +++ b/package-lock.json @@ -51,7 +51,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.198", + "version": "0.0.199", "dev": true, "license": "MIT", "devDependencies": { From 49f19da0898603fbf2ea38bbf8011baa0be34096 Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 17 Nov 2026 04:34:05 +0800 Subject: [PATCH 3/3] kernel: pin the #5455 compress-orphan boundary (B+C', tests only) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #5455: stripOrphanedToolCalls (prune.ts:625) exempts every compress call by name, so it cannot distinguish a genuinely-pending call from one that was answered in the input but whose result was covered away. The state is currently UNREACHABLE through the pipeline (no producer), but the exemption means it would ship an unanswered compress tool_call if it ever became reachable. No behavior change — this pins the boundary with tests only: B (prune side): fix the misleading comment in carrier-advertise-validate.test.ts that claimed stripOrphanedToolCalls 'drops any call whose result left the view' (untrue for compress calls, which are exempt). Add a tripwire test showing an answered compress call whose result is covered still survives the orphan strip, contrasted with an ordinary answered call whose result is covered (stripped). C' (apply side): pin the pair-atomicity invariant at the source — applyCompression never emits a block with a tool-result lacking its adjacent tool-call. This is the invariant that keeps the #5455 state unreachable. No kernel/src change, no version bump (tests only). --- .../tests/carrier-advertise-validate.test.ts | 11 +++-- kernel/tests/prune-orphan-mcp.test.ts | 36 ++++++++++++++ kernel/tests/tool-pair-integrity.test.ts | 48 +++++++++++++++++++ 3 files changed, 91 insertions(+), 4 deletions(-) diff --git a/kernel/tests/carrier-advertise-validate.test.ts b/kernel/tests/carrier-advertise-validate.test.ts index 9eba792dff..2402301a0f 100644 --- a/kernel/tests/carrier-advertise-validate.test.ts +++ b/kernel/tests/carrier-advertise-validate.test.ts @@ -299,10 +299,13 @@ test("archivable and ref-based media fold like text; only refless payloads stay }); // Partial-pair visibility (call visible, result covered) is UNREACHABLE -// through the pipeline today: prune's orphan stripping (stripOrphanedToolCalls -// drops any call whose result left the view) plus apply-side pair widening and -// the integrity gate keep pairs atomic end to end. The risk is structural — -// two screening sites judging two different array classes with nothing +// through the pipeline today: apply-side pair widening and the integrity gate +// keep pairs atomic end to end, and for ordinary tools prune's orphan +// stripping (stripOrphanedToolCalls) drops any call whose result left the view +// as a second line. Compress calls are EXEMPT from that strip (prune.ts, the +// isCompressTool continue), so for them the atomicity rests on the apply-side +// widening + integrity gate ALONE — the #5455 boundary. The risk is structural +// — two screening sites judging two different array classes with nothing // enforcing they agree. Pin the contract at the function level with a // hand-crafted view pair: the judgment must flip with the array class, and // the default (omitted source) must keep judging the passed array for diff --git a/kernel/tests/prune-orphan-mcp.test.ts b/kernel/tests/prune-orphan-mcp.test.ts index 8ea19caef7..ec11a75a6e 100644 --- a/kernel/tests/prune-orphan-mcp.test.ts +++ b/kernel/tests/prune-orphan-mcp.test.ts @@ -75,3 +75,39 @@ test("default options stay byte-identical to pre-fix behavior (bare-name only)", const bare = prune(seq("compress"), stateWithActiveCoverage()); assert.ok(bare.some((m) => m.id === "pend"), "bare-name exemption unchanged under defaults"); }); + +// #5455 boundary: an ANSWERED compress call whose result is covered still +// survives the orphan strip (the isCompressTool exemption at prune.ts:625). +// This "answered but result covered" state is currently UNREACHABLE through +// the pipeline (no producer), but the exemption means it would ship an +// unanswered compress tool_call if it ever became reachable. Compress-call +// atomicity rests on the apply-side pair widening + integrity gate ALONE — +// this test pins that boundary. Contrast: an ordinary answered call whose +// result is covered IS stripped. +test("#5455 boundary: answered compress call whose result is covered survives the orphan exemption", () => { + const messages: CoreMessage[] = [ + { id: "u0", role: "user", contentType: "text", text: "summarize" }, + { id: "call", role: "assistant", contentType: "tool-call", toolName: "compress", toolCallId: "fixture-call", text: "{}" }, + { id: "result", role: "tool", contentType: "tool-result", toolCallId: "fixture-call", text: "completed" }, + { id: "u1", role: "user", contentType: "text", text: "continue" }, + ]; + const state = createInitialState(); + state.blocks.push(block({ blockId: "fixture-block", effectiveMessageIds: ["result"] })); + const out = prune(messages, state); + assert.ok(!out.some((m) => m.id === "result"), "covered result removed"); + assert.ok(out.some((m) => m.id === "call"), "answered compress call survives the orphan exemption (#5455 boundary)"); +}); + +test("#5455 contrast: an ordinary answered call whose result is covered is stripped", () => { + const messages: CoreMessage[] = [ + { id: "u0", role: "user", contentType: "text", text: "a" }, + { id: "call", role: "assistant", contentType: "tool-call", toolName: "bash", toolCallId: "fixture-call", text: "{}" }, + { id: "result", role: "tool", contentType: "tool-result", toolCallId: "fixture-call", text: "out" }, + { id: "u1", role: "user", contentType: "text", text: "b" }, + ]; + const state = createInitialState(); + state.blocks.push(block({ blockId: "fixture-block", effectiveMessageIds: ["result"] })); + const out = prune(messages, state); + assert.ok(!out.some((m) => m.id === "result"), "covered result removed"); + assert.ok(!out.some((m) => m.id === "call"), "ordinary answered call whose result is covered is stripped"); +}); diff --git a/kernel/tests/tool-pair-integrity.test.ts b/kernel/tests/tool-pair-integrity.test.ts index 6551aa77db..316f737bc6 100644 --- a/kernel/tests/tool-pair-integrity.test.ts +++ b/kernel/tests/tool-pair-integrity.test.ts @@ -340,4 +340,52 @@ describe("tool-pair integrity", () => { ); assert.deepEqual(unansweredToolCalls(view.messages), []); }); + + // #5455 C': pin the pair-atomicity invariant at the SOURCE (apply side). + // The existing tests pin the "call without result" direction (a protected + // result refuses/withdraws the call). This pins the reverse: a block must + // never contain a tool-result lacking its adjacent tool-call. This is the + // invariant that keeps the #5455 "answered compress call whose result is + // covered" state UNREACHABLE — the apply side never emits such a block, so + // prune's compress orphan exemption (prune.ts:625) never sees it. + it("#5455 C': never folds a tool-result without its adjacent tool-call (pair atomicity at source)", () => { + const core = createCore(); + const messages: CoreMessage[] = [ + user("m1"), + toolCall("m2", "c-a", "read"), + toolResult("m3", "c-a", "read"), + user("m4"), + user("m5"), + ]; + const out = core.processTurn({ + messages, + state: createInitialState(), + config: config(), + tokenCount: (t) => Math.ceil(t.length / 4), + }); + const refs = out.state.messageRefs.byRaw; + // Protect the CALL (m2), cover both call+result. The result must not fold + // without its call — that would be a "result lacking call" (#5455 state). + const res = core.applyCompression({ + state: out.state, + messages, + config: config(), + protectedMessageIds: new Set([refs["m2"]!]), + ranges: [ + { startRef: "m00002", endRef: "m00003", summary: "S".repeat(400) }, + ], + }); + // The pair is atomic: no block may contain the result without the call. + for (const b of res.state.blocks) { + const hasResult = b.effectiveMessageIds.includes("m3"); + const hasCall = b.effectiveMessageIds.includes("m2"); + assert.ok( + !(hasResult && !hasCall), + `a block must not contain a tool-result lacking its tool-call (#5455 C'): ${JSON.stringify(b.effectiveMessageIds)}`, + ); + } + // The result must not fold on its own (the call is protected). + const foldedResult = res.state.blocks.some((b) => b.effectiveMessageIds.includes("m3")); + assert.ok(!foldedResult, "a protected call's result must not fold without it"); + }); });