From d013ea46954b5e5254c446659fb8dda594140cd0 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sun, 27 Sep 2026 21:37:56 +0800 Subject: [PATCH] fix: route unparseable compress tool args to the lenient parser instead of degrading to {} (#1502) When a model's compress arguments failed strict JSON.parse, three call sites (loop/core.ts streaming, compress-loop-responses.ts x2) discarded the raw string and executed against {}, bypassing the kernel's lenient salvage ladder (trailing comma / single quotes / fence / truncated prefix) and mislabeling corrupt calls as empty ones in the client-facing receipt. Retain the raw string on parse failure and feed it to parseCompressInput; give unsalvageable string input its own corruption receipt (safe length-only label, no payload echo) instead of the #1384 empty-call verdict; log head+tail to separate truncation from mid-string corruption. --- src/compress-loop-responses.ts | 16 +- src/loop/core.ts | 15 +- src/stream.ts | 18 +- tests/issue1502-raw-args-salvage.test.ts | 212 +++++++++++++++++++++++ 4 files changed, 252 insertions(+), 9 deletions(-) create mode 100644 tests/issue1502-raw-args-salvage.test.ts diff --git a/src/compress-loop-responses.ts b/src/compress-loop-responses.ts index 818c7976e0..9bd6cddd5c 100644 --- a/src/compress-loop-responses.ts +++ b/src/compress-loop-responses.ts @@ -97,16 +97,19 @@ async function surfaceProxyJson( for (const call of proxyCalls) { const mutating = MUTATING_PROXY_TOOLS.has(call.name); let args: Record = {}; + let rawArgs: string | undefined; try { args = JSON.parse(call.arguments) as Record; } catch { - args = {}; + // #1502: same root cause as loop/core.ts — keep the raw string so corrupt compress arguments reach the kernel's lenient salvage ladder instead of {}. + rawArgs = call.arguments; + loggerLog("warn", `[acp-compress-args] ${call.name} JSON.parse failed (len=${call.arguments.length}, head=${call.arguments.slice(0, 200)}, tail=${call.arguments.slice(-200)})`); } let result: string; try { result = mutating - ? await withSessionLock(ctx.session, () => executeProxyTool(call.name, args, ctx, call.callId)) - : executeProxyTool(call.name, args, ctx, call.callId); + ? await withSessionLock(ctx.session, () => executeProxyTool(call.name, args, ctx, call.callId, rawArgs)) + : executeProxyTool(call.name, args, ctx, call.callId, rawArgs); ctx.log(`[acp-proxy: responses JSON ${call.name}${mutating ? "" : " (read-only)"} → ${result.slice(0, 120).replace(/\n/g, " ")}]`); } catch (e) { result = `\u274c [ACP] ${call.name} FAILED: ${String(e)}`; @@ -162,12 +165,15 @@ export async function compressLoopResponsesJson( } for (const call of proxyCalls) { let args: Record = {}; + let rawArgs: string | undefined; try { args = JSON.parse(call.arguments) as Record; } catch (error) { - loggerLog("warn", `[acp-compress-args] ${call.name} JSON.parse failed: ${String(error)}`); + // #1502: keep the raw string so corrupt compress arguments reach the kernel's lenient salvage ladder instead of {}. + loggerLog("warn", `[acp-compress-args] ${call.name} JSON.parse failed: ${String(error)} (len=${call.arguments.length}, head=${call.arguments.slice(0, 200)}, tail=${call.arguments.slice(-200)})`); + rawArgs = call.arguments; } - const result = await withSessionLock(ctx.session, () => executeProxyTool(call.name, args, ctx, call.callId)); + const result = await withSessionLock(ctx.session, () => executeProxyTool(call.name, args, ctx, call.callId, rawArgs)); ctx.log(`[acp-proxy: responses JSON ${call.name} → ${result.slice(0, 120).replace(/\n/g, " ")}]`); if (ctx.visibilityMarkers !== false) inputItems.push({ type: "message", role: "developer", content: buildVisibilityMarker(call.name, result) }); } diff --git a/src/loop/core.ts b/src/loop/core.ts index 178f8911b6..bfa26b335b 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -11,6 +11,7 @@ import type { BiliMessage } from "acp-kernel/wire"; import { parseCompressInput, ABSORB_TOOL_NAME, + COMPRESS_TOOL_NAME, RULE_TOOL_NAME, } from "../compress-tool.js"; import { effectiveAbsorbConfig, executeAbsorb, isProxyToolFor } from "../absorb.js"; @@ -221,9 +222,14 @@ export function executeProxyTool( args: Record, ctx: LoopCtx, callId?: string, + rawArguments?: string, ): string { if (toolName === "compress") { - return applyRanges(parseCompressInput(args, callId), ctx); + // #1502: on strict-JSON.parse failure the caller passes the raw argument + // string here — the kernel's lenient parser salvages fence/trailing- + // comma/single-quote/truncated inputs that the degraded {} would drop. + const input = typeof rawArguments === "string" && rawArguments.length > 0 ? rawArguments : args; + return applyRanges(parseCompressInput(input, callId), ctx); } if (toolName === "decompress") { return resolveDecompress(args, ctx); @@ -700,14 +706,17 @@ export async function* runCompressLoop( } if (isProxyToolFor(call.name, ctx.session, ctx.config)) { let parsedArgs: Record; + let rawArgs: string | undefined; try { parsedArgs = call.arguments.length > 0 ? JSON.parse(call.arguments) : {}; } catch { // #1306: an empty/truncated arguments string is wire-loss-shaped, bad JSON is model-shaped — log the shape so the two are separable in logs. - ctx.log(`[acp-loop] proxy tool ${call.name}: arguments not parseable JSON (len=${call.arguments.length}${call.arguments.length > 0 ? `, head=${call.arguments.slice(0, 200)}` : ""}) — executing with {}`); + // #1502: tail= alongside head= separates mid-string corruption from truncation; the raw string survives for the lenient parser. + ctx.log(`[acp-loop] proxy tool ${call.name}: arguments not parseable JSON (len=${call.arguments.length}${call.arguments.length > 0 ? `, head=${call.arguments.slice(0, 200)}, tail=${call.arguments.slice(-200)}` : ""}) — ${call.name === COMPRESS_TOOL_NAME ? "routing the raw string to the lenient parser" : "executing with {}"}`); + rawArgs = call.arguments; parsedArgs = {}; } - const result = await withSessionLock(ctx.session, () => executeProxyTool(call.name, parsedArgs, ctx, call.callId)); + const result = await withSessionLock(ctx.session, () => executeProxyTool(call.name, parsedArgs, ctx, call.callId, rawArgs)); proxyResults.push({ name: call.name, callId: call.callId, result, arguments: call.arguments, signature: call.signature }); if (ctx.visibilityMarkers !== false) { const markerKey = `${call.name}\u0000${result}`; diff --git a/src/stream.ts b/src/stream.ts index 7e799492d0..580f13aacb 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -264,16 +264,32 @@ export function applyRanges(parsed: ReturnType, ctx: rawReasons.length === 0 && (diagnostics.kind === "empty-input" || (diagnostics.kind === "missing-content" && !(diagnostics.keys ?? []).includes("ranges"))); + // #1502: a raw-string argument the lenient parser could not salvage + // (malformed-json/truncated; diag.length is set only for string inputs) + // used to arrive here pre-degraded to {} and inherit the empty-call + // verdict above. Name the real cause — syntax corruption, not emptiness — + // safe label: length only, no payload echo (#1454 pattern). + const argLen = diagnostics.length; + const argCorruption = + !isEmptyCall && + argLen !== undefined && + (diagnostics.kind === "malformed-json" || diagnostics.kind === "truncated"); const guard = recordCompressFailure( ctx.session, `parse:${diagnostics.kind}:${diagnostics.invalidItems}:${rawReasons.slice(0, 3).join("|")}`, isEmptyCall ? "An empty call fails identically on every retry — drop it instead of re-issuing." - : "Fix the argument shape against the format below instead of re-issuing the same malformed call.", + : argCorruption + ? "Do not retry the same corrupt byte string — re-issue the call as one well-formed JSON object." + : "Fix the argument shape against the format below instead of re-issuing the same malformed call.", ); if (isEmptyCall) { return `[Compression FAILED: the call carried no content at all (kind=${diagnostics.kind}) — an empty compress() compresses nothing and can never succeed. Do NOT re-issue an empty call; if you meant to compress, put the non-empty 'content' array (elements {startId, endId, summary}) in that SAME single call.${guard}]`; } + if (argCorruption) { + const truncNote = diagnostics.kind === "truncated" ? " (looks truncated)" : ""; + return `[Compression FAILED: the call's arguments (${argLen} chars) were not parseable JSON${truncNote} — the intended content was lost and nothing was compressed. Re-issue the compress call as well-formed JSON: a single object with a non-empty 'content' array of {startId, endId, summary} elements.${guard}]`; + } return `[Compression FAILED: no valid ranges parsed (kind=${diagnostics.kind}, dropped=${diagnostics.invalidItems}).${why} compress requires a non-empty 'content' array where each element is EITHER an object {startId, endId, summary} OR one line-form string whose first line is 'mNNNNN–mNNNNN optional topic' with the summary markdown on the following lines (a separate summary-only element right after a bare header line is also accepted). startId/endId are mNNNNN message refs from the conversation (call acp_status to see current refs).${compressibleSpanHint(ctx.session.state)} Re-issue the compress call with a valid content array.${guard}]`; } // #847: detect reversed refs as SUBMITTED, before #1001 normalization diff --git a/tests/issue1502-raw-args-salvage.test.ts b/tests/issue1502-raw-args-salvage.test.ts new file mode 100644 index 0000000000..578812d8c7 --- /dev/null +++ b/tests/issue1502-raw-args-salvage.test.ts @@ -0,0 +1,212 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { createCore, createInitialState, assignRefs, emptyRefMap, defaultConfig } from "acp-kernel"; +import type { Config, CoreMessage } from "acp-kernel"; +import type { Session } from "../src/session.ts"; +import { executeProxyTool } from "../src/loop/core.ts"; +import { createOpenaiAdapter, runCompressLoop } from "../src/loop/index.ts"; +import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; + +// #1502: strict JSON.parse failure on proxy-tool arguments used to discard the +// raw string and execute against {} — bypassing the kernel's lenient salvage +// ladder for compress and mislabeling corrupt calls as empty ones. + +const enc = new TextEncoder(); +const sseChunk = (delta: Record, finishReason?: string) => + enc.encode( + `data: ${JSON.stringify({ + id: "c1", + object: "chat.completion.chunk", + created: 1, + model: "gpt", + choices: [{ index: 0, delta, finish_reason: finishReason ?? null }], + })}\n\n`, + ); + +const mockStream = (...chunks: Uint8Array[]): ReadableStream => + new ReadableStream({ + start(controller) { + for (const c of chunks) controller.enqueue(c); + controller.enqueue(enc.encode(`data: [DONE]\n\n`)); + controller.close(); + }, + }); + +function textMsg(id: string, role: "user" | "assistant", text: string): CoreMessage { + return { id, role, contentType: "text", text }; +} + +type Ctx = { + core: ReturnType; + config: Config; + messages: CoreMessage[]; + session: Session; + log: (m: string) => void; + logs: string[]; +}; + +function makeCtx(): Ctx { + const logs: string[] = []; + const messages = [ + textMsg("raw_1", "user", "x".repeat(20000)), + textMsg("raw_2", "assistant", "x".repeat(20000)), + textMsg("raw_3", "user", "x".repeat(5000)), + textMsg("raw_4", "assistant", "x".repeat(5000)), + textMsg("raw_5", "user", "x".repeat(5000)), + textMsg("raw_6", "assistant", "x".repeat(5000)), + textMsg("raw_7", "user", "x".repeat(5000)), + ]; + const refMap = assignRefs(messages, { existing: emptyRefMap(), nextIndex: 0 }).map; + const session: Session = { + id: "issue1502-test", + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + }; + session.state.messageRefs = refMap; + return { + core: createCore(), + config: defaultConfig(200000), + messages, + session, + log: (m: string) => { logs.push(m); }, + logs, + }; +} + +const LONG_SUMMARY = "OBS-TEST-SUMMARY-PAYLOAD-LONG-ENOUGH-FOR-THE-KERNEL-MIN-LENGTH-CHECK"; + +test("#1502: salvageable args that strict JSON.parse rejects compress via rawArguments (trailing comma)", () => { + const raw = `{"content": [{"startId": "m00001", "endId": "m00002", "summary": "${LONG_SUMMARY}"}],}`; + assert.throws(() => JSON.parse(raw), "premise: strict JSON.parse really fails"); + const ctx = makeCtx(); + const out = executeProxyTool("compress", {}, ctx, "c1", raw); + assert.ok(out.startsWith("[Compressed"), `expected success, got: ${out}`); + assert.equal(ctx.session.state.blocks.length, 1, "block created"); +}); + +test("#1502: lenient ladder variants all salvage via rawArguments (fenced / single-quote / truncated prefix)", () => { + const variants: Array<[string, string]> = [ + ["fenced", "```json\n" + `{"content": [{"startId": "m00001", "endId": "m00002", "summary": "${LONG_SUMMARY}"}]}` + "\n```"], + ["single-quote", `{'content': [{'startId': 'm00001', 'endId': 'm00002', 'summary': '${LONG_SUMMARY}'}]}`], + ["truncated prefix", `{"content": [{"startId": "m00001", "endId": "m00002", "summary": "${LONG_SUMMARY}"}`], + ]; + for (const [label, raw] of variants) { + const ctx = makeCtx(); + const out = executeProxyTool("compress", {}, ctx, `c_${label}`, raw); + assert.ok(out.startsWith("[Compressed"), `${label}: expected success, got: ${out}`); + assert.equal(ctx.session.state.blocks.length, 1, `${label}: block created`); + } +}); + +test("#1502: without rawArguments the degraded {} still gets the #1384 empty-call receipt (unchanged)", () => { + const ctx = makeCtx(); + const out = executeProxyTool("compress", {}, ctx, "c_empty"); + assert.ok(out.includes("carried no content at all"), out); + assert.ok(out.includes("Do NOT re-issue an empty call"), out); +}); + +test("#1502: unparseable garbage gets the corruption receipt, not the empty-call verdict (unbalanced → truncated)", () => { + const raw = '{"content": [{"startId": "m00001"'; + const ctx = makeCtx(); + const out = executeProxyTool("compress", {}, ctx, "c_garbage", raw); + assert.ok(out.startsWith("[Compression FAILED"), out); + assert.ok(out.includes(`(${raw.length} chars)`), "safe len label present: " + out); + assert.ok(out.includes("were not parseable JSON"), out); + assert.ok(out.includes("(looks truncated)"), out); + assert.ok(!out.includes("carried no content at all"), "not mislabeled as empty: " + out); + assert.ok(!out.includes("Do NOT re-issue an empty call"), out); + const keys = ctx.session.metadata["compressFailKeys"] as unknown[]; + assert.match(String(keys[0]), /^parse:truncated:0:/, String(keys)); +}); + +test("#1502: balanced garbage → malformed-json label without the truncation note", () => { + const raw = "{this is not json at all}"; + const ctx = makeCtx(); + const out = executeProxyTool("compress", {}, ctx, "c_garbage2", raw); + assert.ok(out.includes("were not parseable JSON"), out); + assert.ok(!out.includes("(looks truncated)"), out); + const keys = ctx.session.metadata["compressFailKeys"] as unknown[]; + assert.match(String(keys[0]), /^parse:malformed-json:0:/, String(keys)); +}); + +test("#1502 loop: unparseable-but-salvageable compress args execute instead of degrading to {}", async () => { + const rawArgs = `{"content": [{"startId": "m00001", "endId": "m00002", "summary": "Preserve every decision, file path and exact value in this range for the regression test."}],}`; + assert.throws(() => JSON.parse(rawArgs), "premise: strict JSON.parse really fails"); + const ctx = makeCtx(); + const round1 = mockStream( + sseChunk({ role: "assistant" }), + sseChunk({ tool_calls: [{ index: 0, id: "call_c", type: "function", function: { name: "compress", arguments: rawArgs } }] }, "tool_calls"), + ); + const originalFetch = globalThis.fetch; + let refetches = 0; + globalThis.fetch = (async () => { + refetches++; + return new Response( + `data: ${JSON.stringify({ id: "c2", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: { content: "ok" }, finish_reason: "stop" }] })}\n\ndata: [DONE]\n\n`, + { status: 200 }, + ); + }) as typeof fetch; + try { + const chunks: Buffer[] = []; + for await (const chunk of runCompressLoop( + round1, + ctx, + { model: "gpt", stream: true }, + { url: "http://mock", headers: {} }, + createOpenaiAdapter({ model: "gpt" }), + buildCompressSystemPrompt(), + )) chunks.push(chunk); + const output = Buffer.concat(chunks).toString("utf8"); + assert.match(output, /Compressed m00001/, "salvaged range compressed end-to-end"); + assert.doesNotMatch(output, /executing with \{\}/, "no degradation log"); + assert.doesNotMatch(output, /not parseable JSON/, "no parse-failure log"); + assert.ok(ctx.session.state.blocks.length >= 1, "block created"); + assert.equal(refetches, 1, "re-request proceeds after successful compress"); + } finally { + globalThis.fetch = originalFetch; + } +}); + +test("#1502 loop: garbage args log head+tail and yield the corruption marker, not the empty-call verdict", async () => { + const garbage = '{"content": [{"startId": "m00001", "endId": "m00002", "summary": "some summary payload'; + const ctx = makeCtx(); + const round1 = mockStream( + sseChunk({ role: "assistant" }), + sseChunk({ tool_calls: [{ index: 0, id: "call_g", type: "function", function: { name: "compress", arguments: garbage } }] }, "tool_calls"), + ); + const originalFetch = globalThis.fetch; + let refetches = 0; + globalThis.fetch = (async () => { + refetches++; + return new Response( + `data: ${JSON.stringify({ id: "c3", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: { content: "ok" }, finish_reason: "stop" }] })}\n\ndata: [DONE]\n\n`, + { status: 200 }, + ); + }) as typeof fetch; + try { + const chunks: Buffer[] = []; + for await (const chunk of runCompressLoop( + round1, + ctx, + { model: "gpt", stream: true }, + { url: "http://mock", headers: {} }, + createOpenaiAdapter({ model: "gpt" }), + buildCompressSystemPrompt(), + )) chunks.push(chunk); + const output = Buffer.concat(chunks).toString("utf8"); + assert.match(ctx.logs.join("\n"), /arguments not parseable JSON \(len=\d+, head=.*tail=/s, "diag log carries head+tail"); + assert.doesNotMatch(output, /executing with \{\}/, "old degradation wording gone"); + assert.match(output, /were not parseable JSON/, "corruption marker streamed to client"); + assert.doesNotMatch(output, /carried no content at all/, "not mislabeled as empty"); + assert.equal(refetches, 1, "re-request proceeds after failed compress too"); + } finally { + globalThis.fetch = originalFetch; + } +});