Skip to content
Merged
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
16 changes: 11 additions & 5 deletions src/compress-loop-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,19 @@ async function surfaceProxyJson(
for (const call of proxyCalls) {
const mutating = MUTATING_PROXY_TOOLS.has(call.name);
let args: Record<string, unknown> = {};
let rawArgs: string | undefined;
try {
args = JSON.parse(call.arguments) as Record<string, unknown>;
} 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)}`;
Expand Down Expand Up @@ -162,12 +165,15 @@ export async function compressLoopResponsesJson(
}
for (const call of proxyCalls) {
let args: Record<string, unknown> = {};
let rawArgs: string | undefined;
try {
args = JSON.parse(call.arguments) as Record<string, unknown>;
} 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) });
}
Expand Down
15 changes: 12 additions & 3 deletions src/loop/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -221,9 +222,14 @@ export function executeProxyTool(
args: Record<string, unknown>,
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);
Expand Down Expand Up @@ -700,14 +706,17 @@ export async function* runCompressLoop(
}
if (isProxyToolFor(call.name, ctx.session, ctx.config)) {
let parsedArgs: Record<string, unknown>;
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}`;
Expand Down
18 changes: 17 additions & 1 deletion src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,16 +264,32 @@ export function applyRanges(parsed: ReturnType<typeof parseCompressInput>, 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
Expand Down
212 changes: 212 additions & 0 deletions tests/issue1502-raw-args-salvage.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>, 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<Uint8Array> =>
new ReadableStream<Uint8Array>({
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<typeof createCore>;
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;
}
});
Loading