diff --git a/package.json b/package.json index cd82f6fdc0..c62453aeda 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-test-import-graph.mjs b/scripts/check-test-import-graph.mjs index 43486a39fb..cf473c7118 100644 --- a/scripts/check-test-import-graph.mjs +++ b/scripts/check-test-import-graph.mjs @@ -52,6 +52,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. diff --git a/scripts/check-unused-exports.mjs b/scripts/check-unused-exports.mjs index f7bd743ee9..d105738a6c 100644 --- a/scripts/check-unused-exports.mjs +++ b/scripts/check-unused-exports.mjs @@ -41,6 +41,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..2ebcfddde2 --- /dev/null +++ b/src/salvage.ts @@ -0,0 +1,25 @@ +// #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. +// `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 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" ? { 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 new file mode 100644 index 0000000000..24aa98cc07 --- /dev/null +++ b/tests/issue4889-bare-content-execution-salvage.test.ts @@ -0,0 +1,96 @@ +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 with range count", () => { + const out = repairCompressArgs(INCIDENT_C, "call_ext"); + assert.ok(out !== null); + assert.equal(out!.ranges, 1); + const obj = JSON.parse(out!.json) as { content: unknown }; + assert.equal(typeof obj.content, "string"); + 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", () => { + 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 0d806a1677..27344b7ebd 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/claude-subagent-hook.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/claude-subagent-hook.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",