Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions scripts/check-test-import-graph.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions scripts/check-unused-exports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions src/compress-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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": <bare line-form text>` 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})`);
}
Expand Down
33 changes: 23 additions & 10 deletions src/plugin-compress-salvage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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 };
Expand Down
25 changes: 25 additions & 0 deletions src/salvage.ts
Original file line number Diff line number Diff line change
@@ -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;
}
96 changes: 96 additions & 0 deletions tests/issue4889-bare-content-execution-salvage.test.ts
Original file line number Diff line number Diff line change
@@ -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": <bare line-form text>` — 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);
});
2 changes: 1 addition & 1 deletion tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading