From 4c6d761e57bd8b4844d2ba73484b5aabf35da7d4 Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 27 Oct 2026 08:00:43 +0800 Subject: [PATCH 1/3] fix: salvage bad-shape-A compress args at the execution layer, all lanes (#4889) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit parseCompressInput now wraps a bare line-form content value ({"content": }) as a JSON string and re-parses it when the kernel ladder recovers nothing — the same rescue the plugin relay performed on the Anthropic wire only (#2591), now shared by every lane: proxy-mode loop/JSON rewrite paths and any host that executes compress through the proxy. Adoption discipline matches #3780/#1494: fires only after a failed parse, adopted only when strictly more ranges result. The wrap step is extracted from salvageCompressArgs into tryBareContentWrap so both call sites run one implementation instead of two regex copies (salvageCompressArgs behavior unchanged, pinned by the #2580 suite). New subpath export billion-context/salvage (repairCompressArgs): provider plugins sitting between model and host can repair compress arguments before the host's strict JSON.parse with the same ladder — strict JSON in, null = leave as-is out (#1039 boundary preserved). --- package.json | 3 +- scripts/check-unused-exports.mjs | 1 + src/compress-tool.ts | 16 ++++ src/plugin-compress-salvage.ts | 33 +++++--- src/salvage.ts | 18 ++++ ...889-bare-content-execution-salvage.test.ts | 82 +++++++++++++++++++ tsup.config.ts | 2 +- 7 files changed, 143 insertions(+), 12 deletions(-) create mode 100644 src/salvage.ts create mode 100644 tests/issue4889-bare-content-execution-salvage.test.ts diff --git a/package.json b/package.json index ea30cea0b4..81f803f3a4 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "./dsh-compaction-gate": "./dist/agent/dsh-compaction-gate.js", "./package.json": "./package.json", "./dsh/package.json": "./package.json", - "./client": "./dist/agent/dsh-native-client.js" + "./client": "./dist/agent/dsh-native-client.js", + "./salvage": "./dist/salvage.js" }, "bin": { "bili": "./dist/index.js", diff --git a/scripts/check-unused-exports.mjs b/scripts/check-unused-exports.mjs index ea21f73e74..33f25b36a0 100644 --- a/scripts/check-unused-exports.mjs +++ b/scripts/check-unused-exports.mjs @@ -40,6 +40,7 @@ const ENTRY_FILES = new Set([ "src/zcode/mcp-entry.ts", "src/zcode/bootstrap-hook.ts", "src/agent/dsh-native-client.ts", + "src/salvage.ts", ]); // `--others --exclude-standard` pulls in NEW, not-yet-committed files too: diff --git a/src/compress-tool.ts b/src/compress-tool.ts index 4a94fee657..08fa5150a1 100644 --- a/src/compress-tool.ts +++ b/src/compress-tool.ts @@ -50,6 +50,7 @@ import type { CompressPromptSections, Prompts, SectionOverride, ToolPrompts } fr import { log as loggerLog } from "./logger.js"; import { maxShrinkPerCompress } from "./fetch-util.js"; import { EXTERNAL_SUMMARY_NOTE } from "./external-summary-surface.js"; +import { tryBareContentWrap } from "./plugin-compress-salvage.js"; export { COMPRESS_TOOL_NAME, @@ -560,6 +561,21 @@ export function parseCompressInput(input: unknown, callId?: string) { } } } + // #4889: bad shape A (#2580/#2579) reaching lanes without the plugin relay's + // wire rewrite — `{"content": ` with no opening quote. + // The kernel ladder recovers nothing here (its truncated-entry salvage + // requires the literal `"content": [`), so wrap the bare value as a JSON + // string and re-parse; adopt only if strictly more ranges, same discipline + // as #3780 — a payload that parses today is never touched. Regex/candidates + // shared with the plugin-lane rewrite (plugin-compress-salvage.ts); the + // ultimate home for this step is the kernel's parse path pending a change. + if (!parsed.diagnostics.ok && parsed.diagnostics.kind !== "ok" && typeof normalized === "string") { + const wrapped = tryBareContentWrap(normalized, callId); + if (wrapped !== undefined && wrapped.parsed.ranges.length > parsed.ranges.length) { + loggerLog("warn", `[acp-compress-input] bare-content salvage: recovered ${wrapped.parsed.ranges.length} range(s) by wrapping the unquoted content value as a JSON string (kind was ${parsed.diagnostics.kind}) (#4889)`); + parsed = wrapped.parsed; + } + } if (parsed.diagnostics.quoteSalvage === true) { loggerLog("warn", `[acp-compress-input] quote-salvage: recovered ${parsed.ranges.length} range(s) after single->double quote normalization (kind=${parsed.diagnostics.kind})`); } diff --git a/src/plugin-compress-salvage.ts b/src/plugin-compress-salvage.ts index 9284be755e..3d45096074 100644 --- a/src/plugin-compress-salvage.ts +++ b/src/plugin-compress-salvage.ts @@ -8,7 +8,7 @@ // happen only after strict JSON.parse fails, and every rescue is // acceptance-tested by re-parsing through parseCompressArgs. -import { parseCompressArgs } from "acp-kernel"; +import { parseCompressArgs, type ParsedCompressInput } from "acp-kernel"; type CompressArgSalvageAction = | "verbatim" // strict JSON — untouched (#1039 invariant) @@ -48,6 +48,25 @@ function canonicalize(ranges: readonly CanonicalRange[]): string { }); } +// #4889: bad-shape A wrap step extracted so this wire rewrite AND the shared +// execution-layer parser (parseCompressInput) run ONE implementation instead of +// two regex copies. Undefined when the shape doesn't match or no candidate +// survives the acceptance re-parse — callers keep their own result. +interface BareContentWrap { + wrapped: string; // strict JSON: {"content":"…"} + parsed: ParsedCompressInput; +} +export function tryBareContentWrap(raw: string, callId?: string): BareContentWrap | undefined { + const m = BARE_CONTENT_RE.exec(raw); + if (!m || !REF_TOKEN_RE.test(m[1])) return undefined; + for (const candidate of [m[1], m[1].replace(/[\s"}\]]+$/, "")]) { + const wrapped = JSON.stringify({ content: candidate }); + const parsed = parseCompressArgs(wrapped, callId !== undefined ? { callId } : undefined); + if (parsed.ranges.length > 0) return { wrapped, parsed }; + } + return undefined; +} + export function salvageCompressArgs(raw: string, callId: string): CompressArgSalvage { try { JSON.parse(raw); @@ -59,15 +78,9 @@ export function salvageCompressArgs(raw: string, callId: string): CompressArgSal return { out: canonicalize(lenient.ranges), action: "canonicalized", kind: lenient.diagnostics.kind, ranges: lenient.ranges.length }; } - const m = BARE_CONTENT_RE.exec(raw); - if (m && REF_TOKEN_RE.test(m[1])) { - for (const candidate of [m[1], m[1].replace(/[\s"}\]]+$/, "")]) { - const wrapped = JSON.stringify({ content: candidate }); - const reparsed = parseCompressArgs(wrapped, { callId }); - if (reparsed.ranges.length > 0) { - return { out: wrapped, action: "wrapped", kind: reparsed.diagnostics.kind, ranges: reparsed.ranges.length }; - } - } + const bare = tryBareContentWrap(raw, callId); + if (bare !== undefined) { + return { out: bare.wrapped, action: "wrapped", kind: bare.parsed.diagnostics.kind, ranges: bare.parsed.ranges.length }; } return { out: raw, action: "unrecoverable", ranges: 0 }; diff --git a/src/salvage.ts b/src/salvage.ts new file mode 100644 index 0000000000..9e1b27a6bb --- /dev/null +++ b/src/salvage.ts @@ -0,0 +1,18 @@ +// #4889: public repair entry for layers that sit BETWEEN the model and bili's +// tool execution — provider plugins / gateways that assemble tool_call +// arguments and hand them to a host doing ONE strict JSON.parse per call. +// They hold the complete argument bytes before any parse; running them through +// salvageCompressArgs there recovers bad-shape-A compress args (#2580) on the +// lanes where bili's own wire rewrite never sees them (third-party providers, +// non-Anthropic protocols). Same implementation as the plugin relay's rewrite +// (plugin-compress-salvage.ts) — no second regex copy. +// #1039 boundary inherited from the ladder: strict-JSON input returns null +// (leave it byte-identical), unrecoverable input returns null too (the host +// rejects it as today); non-null is always strict JSON ready to parse. + +import { salvageCompressArgs } from "./plugin-compress-salvage.js"; + +export function repairCompressArgs(raw: string, callId?: string): string | null { + const s = salvageCompressArgs(raw, callId ?? "external-salvage"); + return s.action === "canonicalized" || s.action === "wrapped" ? s.out : null; +} diff --git a/tests/issue4889-bare-content-execution-salvage.test.ts b/tests/issue4889-bare-content-execution-salvage.test.ts new file mode 100644 index 0000000000..66848d97fc --- /dev/null +++ b/tests/issue4889-bare-content-execution-salvage.test.ts @@ -0,0 +1,82 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { parseCompressInput } from "../src/compress-tool.ts"; +import { salvageCompressArgs } from "../src/plugin-compress-salvage.ts"; +import { repairCompressArgs } from "../src/salvage.ts"; +import { parseCompressArgs } from "acp-kernel"; + +process.env.NODE_ENV = "test"; + +// #4889 incident C shape: `{"content": ` — NO opening +// quote, real newlines, bare double quotes inside, balanced closing `}` (the +// stream was NOT truncated). Built from pieces so the source stays greppable. +const INCIDENT_C = [ + '{"content": m00186\u2013m00289 正式程序编写与构建(阶段二)', + "TASK AS OF THIS BLOCK(历史):探针全部结论已定,正在写正式程序并首次编译。", + '关键决策:用户说 "先跑通再说";构建走 npm run build。', + "⑤端到端验证(待做)⑥打包分发(待做)。", + "}", +].join("\n"); + +test("#4889 parseCompressInput: execution layer rescues bare-content bad shape A", () => { + const { ranges, diagnostics } = parseCompressInput(INCIDENT_C, "call_4889"); + assert.equal(diagnostics.ok, true); + assert.equal(diagnostics.kind, "ok"); + assert.equal(ranges.length, 1); + assert.equal(ranges[0].startRef, "m00186"); + assert.equal(ranges[0].endRef, "m00289"); + assert.ok(ranges[0].summary.includes("探针全部结论已定")); +}); + +test("#4889 parseCompressInput: valid JSON object-array input is untouched", () => { + const input = JSON.stringify({ content: [{ startId: "m00150", endId: "m00220", summary: "s" }] }); + const { ranges, diagnostics } = parseCompressInput(input, "call_neg1"); + assert.equal(diagnostics.kind, "ok"); + assert.equal(ranges.length, 1); + assert.equal(ranges[0].startRef, "m00150"); +}); + +test("#4889 parseCompressInput: valid line-form string input is untouched", () => { + const input = JSON.stringify({ content: "m00150\u2013m00220 topic\nsummary body" }); + const { ranges, diagnostics } = parseCompressInput(input, "call_neg2"); + assert.equal(diagnostics.kind, "ok"); + assert.equal(ranges.length, 1); +}); + +test("#4889 parseCompressInput: bare content without ref tokens stays rejected", () => { + const { ranges, diagnostics } = parseCompressInput('{"content": hello world, no refs at all\n}', "call_neg3"); + assert.equal(ranges.length, 0); + assert.notEqual(diagnostics.kind, "ok"); +}); + +test("#4889 salvageCompressArgs: wrapped action unchanged after helper extraction", () => { + const s = salvageCompressArgs(INCIDENT_C, "call_4889"); + assert.equal(s.action, "wrapped"); + const obj = JSON.parse(s.out) as { content: unknown }; + assert.equal(typeof obj.content, "string"); + assert.ok(parseCompressArgs(s.out, { callId: "verify" }).ranges.length >= 1); +}); + +test("#4889 salvageCompressArgs: strict JSON still verbatim byte-identical (#1039)", () => { + const valid = JSON.stringify({ content: [{ startId: "m00150", endId: "m00220", summary: "s" }] }); + const s = salvageCompressArgs(valid, "call_1039"); + assert.equal(s.action, "verbatim"); + assert.equal(s.out, valid); +}); + +test("#4889 repairCompressArgs: wraps bad shape A into strict JSON", () => { + const out = repairCompressArgs(INCIDENT_C, "call_ext"); + assert.ok(out !== null); + const obj = JSON.parse(out!) as { content: unknown }; + assert.equal(typeof obj.content, "string"); + assert.ok(parseCompressArgs(out!, { callId: "verify" }).ranges.length >= 1); +}); + +test("#4889 repairCompressArgs: strict JSON passes through as null", () => { + const valid = JSON.stringify({ content: [{ startId: "m00150", endId: "m00220", summary: "s" }] }); + assert.equal(repairCompressArgs(valid), null); +}); + +test("#4889 repairCompressArgs: unrecoverable garbage passes through as null", () => { + assert.equal(repairCompressArgs('{"content": hello world no refs'), null); +}); diff --git a/tsup.config.ts b/tsup.config.ts index f77884f5d9..fcbb7cd8ce 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -4,7 +4,7 @@ import { defineConfig } from "tsup"; export default [ defineConfig({ - entry: ["src/index.ts", "src/mcp.ts", "src/claude-mcp-entry.ts", "src/claude-native-bootstrap.ts", "src/agent/pi.ts", "src/agent/pi-native.ts", "src/agent/omp.ts", "src/agent/omp-native.ts", "src/agent/opencode.ts", "src/agent/opencode-native.ts", "src/agent/dsh-acp.ts", "src/agent/dsh-native.ts", "src/agent/dsh-compaction-gate.ts", "src/kimi/native-mcp.ts", "src/kimi/bootstrap-hook.ts", "src/zcode/mcp-entry.ts", "src/zcode/bootstrap-hook.ts"], + entry: ["src/index.ts", "src/mcp.ts", "src/claude-mcp-entry.ts", "src/claude-native-bootstrap.ts", "src/agent/pi.ts", "src/agent/pi-native.ts", "src/agent/omp.ts", "src/agent/omp-native.ts", "src/agent/opencode.ts", "src/agent/opencode-native.ts", "src/agent/dsh-acp.ts", "src/agent/dsh-native.ts", "src/agent/dsh-compaction-gate.ts", "src/kimi/native-mcp.ts", "src/kimi/bootstrap-hook.ts", "src/zcode/mcp-entry.ts", "src/zcode/bootstrap-hook.ts", "src/salvage.ts"], format: ["esm"], target: "node20", platform: "node", From 13d45a5f55e67d096b079f7268ce9dac0f902ccf Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 27 Oct 2026 08:08:13 +0800 Subject: [PATCH 2/3] fix(ci): register src/salvage.ts as a production entry in the import-graph gate (#4889) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit check-test-import-graph.mjs keeps a hardcoded ENTRY_FILES list in sync with tsup.config.ts (drift makes new entries look unreachable). The salvage PR added src/salvage.ts to tsup.config.ts, package.json exports and check-unused-exports.mjs but missed this gate's list, so the gate failed on tests/issue4889-bare-content-execution-salvage.test.ts importing the module. Master head is green — PR-specific failure, not inherited. --- scripts/check-test-import-graph.mjs | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/check-test-import-graph.mjs b/scripts/check-test-import-graph.mjs index d4e52d7c46..e0c34a21a7 100644 --- a/scripts/check-test-import-graph.mjs +++ b/scripts/check-test-import-graph.mjs @@ -51,6 +51,7 @@ const ENTRY_FILES = new Set([ "src/zcode/mcp-entry.ts", "src/zcode/bootstrap-hook.ts", "src/agent/dsh-native-client.ts", + "src/salvage.ts", ]); // Intentionally-unreachable modules a test MAY import without a marker. From f198f796d745a6de7018b1b9e0a82a9db277411e Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 27 Oct 2026 12:54:41 +0800 Subject: [PATCH 3/3] feat: repairCompressArgs returns { json, ranges } so gateways can log salvage depth (#4889) --- src/salvage.ts | 11 ++++++++-- ...889-bare-content-execution-salvage.test.ts | 20 ++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/salvage.ts b/src/salvage.ts index 9e1b27a6bb..2ebcfddde2 100644 --- a/src/salvage.ts +++ b/src/salvage.ts @@ -9,10 +9,17 @@ // #1039 boundary inherited from the ladder: strict-JSON input returns null // (leave it byte-identical), unrecoverable input returns null too (the host // rejects it as today); non-null is always strict JSON ready to parse. +// `ranges` = how many compress ranges the rescue recovered, so gateways can +// log how much was salvaged per repair (#4891 gateway feedback). import { salvageCompressArgs } from "./plugin-compress-salvage.js"; -export function repairCompressArgs(raw: string, callId?: string): string | null { +export interface RepairCompressArgsResult { + json: string; // strict JSON, ready for the host's single JSON.parse + ranges: number; // compress ranges recovered by the rescue +} + +export function repairCompressArgs(raw: string, callId?: string): RepairCompressArgsResult | null { const s = salvageCompressArgs(raw, callId ?? "external-salvage"); - return s.action === "canonicalized" || s.action === "wrapped" ? s.out : null; + return s.action === "canonicalized" || s.action === "wrapped" ? { json: s.out, ranges: s.ranges } : null; } diff --git a/tests/issue4889-bare-content-execution-salvage.test.ts b/tests/issue4889-bare-content-execution-salvage.test.ts index 66848d97fc..24aa98cc07 100644 --- a/tests/issue4889-bare-content-execution-salvage.test.ts +++ b/tests/issue4889-bare-content-execution-salvage.test.ts @@ -64,12 +64,26 @@ test("#4889 salvageCompressArgs: strict JSON still verbatim byte-identical (#103 assert.equal(s.out, valid); }); -test("#4889 repairCompressArgs: wraps bad shape A into strict JSON", () => { +test("#4889 repairCompressArgs: wraps bad shape A into strict JSON with range count", () => { const out = repairCompressArgs(INCIDENT_C, "call_ext"); assert.ok(out !== null); - const obj = JSON.parse(out!) as { content: unknown }; + assert.equal(out!.ranges, 1); + const obj = JSON.parse(out!.json) as { content: unknown }; assert.equal(typeof obj.content, "string"); - assert.ok(parseCompressArgs(out!, { callId: "verify" }).ranges.length >= 1); + assert.ok(parseCompressArgs(out!.json, { callId: "verify" }).ranges.length >= 1); +}); + +test("#4889 repairCompressArgs: reports every recovered range (N > 1)", () => { + const multi = [ + '{"content": m00001\u2013m00010 第一段', + "摘要一", + 'm00020\u2013m00030 第二段', + "摘要二", + "}", + ].join("\n"); + const out = repairCompressArgs(multi, "call_ext_multi"); + assert.ok(out !== null); + assert.equal(out!.ranges, 2); }); test("#4889 repairCompressArgs: strict JSON passes through as null", () => {