From afa1c5479b6d779147c5320cc99626f40b990a8a Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sun, 27 Sep 2026 22:14:31 +0800 Subject: [PATCH] fix: salvage single-entry object in the compress content slot (#470) --- src/parse-compress-input.ts | 16 +++ tests/compress-schema-contract.test.ts | 25 +++- tests/parse-compress-input.test.ts | 171 +++++++++++++++++++++++++ 3 files changed, 205 insertions(+), 7 deletions(-) diff --git a/src/parse-compress-input.ts b/src/parse-compress-input.ts index ae49a5d..da986e8 100644 --- a/src/parse-compress-input.ts +++ b/src/parse-compress-input.ts @@ -10,6 +10,8 @@ // billion-context#176) // - single-quoted JSON: {'content': [...]} (weak local models, // billion-context#603 / omp#121) +// - a single entry OBJECT where the content array belongs (retry drift, +// billion-context#1494 / acp-kernel#470) // - the stream cut off mid-arguments, leaving a truncated JSON prefix // // Hosts parse this on their own today: rebuild.ts (strict, silent skip), @@ -42,6 +44,9 @@ export interface CompressParseDiagnostics { kind: CompressParseKind; /** True when the winning parse came from single→double quote repair. */ quoteSalvage?: boolean; + /** 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). */ rawPrefix?: string; /** Raw string input length (string inputs only). */ @@ -212,6 +217,17 @@ function parseObjectValue( entries = parsed.entries; salvaged = parsed.salvaged; if (parsed.quoteRepaired) diag.quoteSalvage = true; + } 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 + // (bare entry without content, above). Wrap it so the shared + // validation applies unchanged; a nested `ranges` array is unwrapped + // instead. Mirrors the proxy-lane normalization (billion-context#1497) + // so both lanes converge on this ladder. + const obj = content as Record; + const nested = obj["ranges"]; + entries = Array.isArray(nested) ? nested : [obj]; + diag.contentSalvage = true; } else { diag.kind = "content-not-array"; return finish([], diag); diff --git a/tests/compress-schema-contract.test.ts b/tests/compress-schema-contract.test.ts index a36afbc..db70a88 100644 --- a/tests/compress-schema-contract.test.ts +++ b/tests/compress-schema-contract.test.ts @@ -161,15 +161,26 @@ test("empty-ish calls are the parser's job, not the schema's (bili #1299)", () = } }); -test("an object under content stays rejected (parser yields zero ranges for it)", () => { +test("an object under content: schema stays strict, the parser salvages (#470)", () => { // The trap shape: {content: {startId,endId,summary}} looks like a natural - // single-range call, but parseCompressArgs treats a non-array/non-string - // content as "content-not-array" and recovers nothing — so the schema must - // not widen to accept it either (that would trade a loud rejection for a - // silent no-op). - rejectedByBoth({ + // single-range call. The schema cannot widen to accept it (top-level + // alternations are off-limits — see the empty-ish-calls test above), so + // structural rejection at the schema layer stands. #470 removed the old + // hard content-not-array wall in the parser: the object is wrapped into a + // one-element array and validated like any other entry, so a well-formed + // one now recovers instead of dying. + const trap = { content: { startId: "m00150", endId: "m00220", summary: "S" }, - }); + }; + assert.equal( + validate(COMPRESS_PARAMETERS as Record, trap), + false, + "schema must stay strict: object under content", + ); + const { ranges, diagnostics } = parseCompressArgs(trap); + assert.equal(ranges.length, 1); + assert.ok(diagnostics.ok); + assert.equal(diagnostics.contentSalvage, true); }); test("empty content array passes the schema; the parser degrades gracefully", () => { diff --git a/tests/parse-compress-input.test.ts b/tests/parse-compress-input.test.ts index 1a02bd4..8f32df1 100644 --- a/tests/parse-compress-input.test.ts +++ b/tests/parse-compress-input.test.ts @@ -997,3 +997,174 @@ test("parseCompressArgs resets the adoption pairing at an object entry (#1001)", assert.equal(ranges[0]?.summary, "good"); assert.equal(diagnostics.invalidItems, 1); }); + +// --------------------------------------------------------------------------- +// #470: content given as a single entry OBJECT instead of a one-element array +// (retry drift observed via billion-context #1494/#1495). The top level already +// recovers a bare entry object without content; the SAME shape inside content +// used to hard-fail with content-not-array. Both lanes (proxy normalizer +// billion-context#1497 and plugin-mode bundled kernel) must agree here. +// --------------------------------------------------------------------------- + +test("parseCompressArgs wraps a single entry object in the content slot (#470)", () => { + const { ranges, diagnostics } = parseCompressArgs({ + content: { + startId: "m04083", + endId: "m04092", + summary: "lost range retry", + }, + }); + assert.equal(diagnostics.kind, "ok"); + assert.equal(diagnostics.ok, true); + assert.equal(diagnostics.contentSalvage, true); + assert.equal(diagnostics.invalidItems, 0); + assert.equal(ranges.length, 1); + assert.equal(ranges[0]?.startRef, "m04083"); + assert.equal(ranges[0]?.endRef, "m04092"); + assert.equal(ranges[0]?.summary, "lost range retry"); +}); + +test("parseCompressArgs keeps per-entry topic/summaryMaxChars on a wrapped entry (#470)", () => { + const { ranges, diagnostics } = parseCompressArgs({ + content: { + startRef: "m00001", + endRef: "m00002", + summary: "s", + topic: "Own", + summaryMaxChars: 1234, + }, + }); + assert.equal(diagnostics.kind, "ok"); + assert.equal(ranges.length, 1); + assert.equal(ranges[0]?.topic, "Own"); + assert.equal(ranges[0]?.summaryMaxChars, 1234); +}); + +test("parseCompressArgs applies a top-level topic to a wrapped single entry (#470)", () => { + const { ranges } = parseCompressArgs({ + topic: "Top", + content: { startRef: "m00001", endRef: "m00002", summary: "s" }, + }); + assert.equal(ranges.length, 1); + assert.equal(ranges[0]?.topic, "Top"); +}); + +test("parseCompressArgs stamps compressCallId through the wrap (#470)", () => { + const { ranges } = parseCompressArgs( + { content: { startRef: "m00001", endRef: "m00002", summary: "s" } }, + { callId: "call-470" }, + ); + assert.equal(ranges[0]?.compressCallId, "call-470"); +}); + +test("parseCompressArgs recovers string-encoded single-object content (#470)", () => { + const input = JSON.stringify({ + content: { startId: "m04083", endId: "m04092", summary: "str" }, + }); + const { ranges, diagnostics } = parseCompressArgs(input); + assert.equal(diagnostics.kind, "ok"); + assert.equal(diagnostics.contentSalvage, true); + assert.equal(ranges.length, 1); + assert.equal(ranges[0]?.startRef, "m04083"); + assert.equal(ranges[0]?.summary, "str"); +}); + +test("parseCompressArgs recovers fenced double-stringified single-object content (#470)", () => { + const inner = JSON.stringify( + JSON.stringify({ + content: { startId: "m00001", endId: "m00002", summary: "solo" }, + }), + ); + const { ranges, diagnostics } = parseCompressArgs( + "```json\n" + inner + "\n```", + ); + assert.equal(diagnostics.kind, "ok"); + assert.equal(diagnostics.contentSalvage, true); + assert.equal(ranges.length, 1); + assert.equal(ranges[0]?.summary, "solo"); +}); + +test("parseCompressArgs unwraps a nested ranges array in the content slot (#470)", () => { + const { ranges, diagnostics } = parseCompressArgs({ + content: { + ranges: [ + { startRef: "m00001", endRef: "m00002", summary: "a" }, + { startRef: "m00003", endRef: "m00004", summary: "b" }, + ], + }, + }); + assert.equal(diagnostics.kind, "ok"); + assert.equal(diagnostics.contentSalvage, true); + assert.equal(ranges.length, 2); + assert.equal(ranges[0]?.summary, "a"); + assert.equal(ranges[1]?.summary, "b"); +}); + +test("parseCompressArgs degrades a wrapped garbage object to per-entry reasons, not opaque content-not-array (#470)", () => { + const { ranges, diagnostics } = parseCompressArgs({ content: { foo: 1 } }); + assert.equal(ranges.length, 0); + assert.equal(diagnostics.ok, false); + assert.equal(diagnostics.kind, "no-valid-ranges"); + assert.equal(diagnostics.contentSalvage, true); + assert.equal(diagnostics.invalidItems, 1); + assert.ok( + diagnostics.invalidReasons?.[0]?.includes("missing range bounds"), + JSON.stringify(diagnostics), + ); +}); + +test("parseCompressArgs reports missing fields on an incomplete wrapped entry (#470)", () => { + const { ranges, diagnostics } = parseCompressArgs({ + content: { startId: "m00001", endId: "m00002" }, + }); + assert.equal(ranges.length, 0); + assert.equal(diagnostics.kind, "no-valid-ranges"); + assert.equal(diagnostics.invalidItems, 1); + assert.ok( + diagnostics.invalidReasons?.[0]?.includes("missing summary"), + JSON.stringify(diagnostics), + ); +}); + +test("parseCompressArgs drops a truncated single-object content entry without guessing (#470)", () => { + const { ranges, diagnostics } = parseCompressArgs( + '{"content": {"startId": "m00001", "endId": "m0', + ); + assert.equal(ranges.length, 0); + assert.equal(diagnostics.ok, false); + assert.equal(diagnostics.kind, "truncated"); +}); + +test("parseCompressArgs keeps content-not-array for non-object content values (#470)", () => { + for (const input of [{ content: 42 }, { content: null }, { content: true }]) { + const { ranges, diagnostics } = parseCompressArgs(input); + assert.equal(diagnostics.kind, "content-not-array", JSON.stringify(input)); + assert.equal(diagnostics.ok, false); + assert.equal(diagnostics.contentSalvage, undefined, JSON.stringify(input)); + assert.equal(ranges.length, 0); + } +}); + +test("parseCompressArgs leaves canonical array inputs unmarked by contentSalvage (#470)", () => { + const { ranges, diagnostics } = parseCompressArgs({ + content: [{ startRef: "m00001", endRef: "m00002", summary: "clean" }], + }); + assert.equal(diagnostics.kind, "ok"); + assert.equal(diagnostics.contentSalvage, undefined); + assert.equal(ranges.length, 1); +}); + +test("rebuildCompressionState rebuilds from a single-object content tool-call (#470)", () => { + const args = JSON.stringify({ + content: { startId: "m00001", endId: "m00002", summary: "single object" }, + }); + const result = rebuildCompressionState( + createInitialState(), + rebuildMessages(args, "call1"), + rebuildConfig(), + ); + assert.equal(result.blocksRebuilt, 1); + assert.ok( + result.state.blocks.find((b) => b.summary.includes("single object")), + ); +});