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
40 changes: 39 additions & 1 deletion src/compress-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,46 @@ export function normalizeCompressInput(input: unknown): unknown {
return input;
}

// #3780: the documented line-form string can arrive DOUBLE-ESCAPED — a model
// that serializes its args as text escapes the header/summary separator twice,
// so `content` carries a LITERAL backslash+n instead of a real line break. The
// kernel's parseLineEntry then finds no newline, yields an empty summary, and
// rejects with kind=content-not-array while hiding the real missing-summary
// reason. Decode the newline escapes ONCE and return the corrected value — but
// only when the string has NO real newline at all (a payload that already
// carried a real break is left untouched, so nothing that parses today can be
// mangled). Returns undefined when there is nothing to decode.
function decodeDoubleEscapedContent(input: unknown): Record<string, unknown> | undefined {
if (!isPlainObject(input)) return undefined;
const c = input["content"];
if (typeof c !== "string") return undefined;
if (c.includes("\n")) return undefined;
if (!c.includes("\\n")) return undefined;
const decoded = c.replace(/\\r\\n/g, "\r\n").replace(/\\n/g, "\n");
if (decoded === c) return undefined;
return { ...input, content: decoded };
}

export function parseCompressInput(input: unknown, callId?: string) {
const parsed = parseCompressArgs(normalizeCompressInput(input), { callId });
const normalized = normalizeCompressInput(input);
let parsed = parseCompressArgs(normalized, { callId });
// #3780: retry the double-escaped line form once. Adopt the result only if
// it parses strictly MORE ranges than the undecoded attempt — a no-real-
// newline string always yields zero ranges undecoded, so this can never
// degrade a working parse. #1494 precedent: host-side pre-normalization of
// compress-arg drift before the kernel. The ultimate shared home for this
// decode is the kernel's parseLineEntry (kernel/src/parse-compress-input.ts);
// it stays there pending a kernel change — this salvage covers every lane now.
if (!parsed.diagnostics.ok && parsed.diagnostics.kind !== "ok") {
const decoded = decodeDoubleEscapedContent(normalized);
if (decoded !== undefined) {
const retry = parseCompressArgs(decoded, { callId });
if (retry.ranges.length > parsed.ranges.length) {
loggerLog("warn", `[acp-compress-input] double-escape salvage: recovered ${retry.ranges.length} range(s) after decoding literal backslash-n separators once (kind was ${parsed.diagnostics.kind}) (#3780)`);
parsed = retry;
}
}
}
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
45 changes: 45 additions & 0 deletions tests/issue1494-compress-params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,48 @@ test("#1494 A+B: single-object content folds end-to-end through applyRanges", ()
assert.ok(out.startsWith("[Compressed m00001–m00002 → 1 block(s)"), out.split("\n")[0]);
assert.ok(!out.includes("REJECTED"), out);
});

// #3780: the documented line-form string can arrive DOUBLE-ESCAPED — a model
// serializing its args as text escapes the header/summary separator twice, so
// `content` holds a LITERAL backslash+n instead of a real break. Undecoded the
// kernel finds no newline, yields an empty summary, and rejects with
// kind=content-not-array while hiding the real missing-summary reason. The host
// salvage decodes once and retries, adopting the result only if it parses more
// ranges (a no-real-newline string always yields zero undecoded, so a working
// parse can never be degraded).
test("#3780: double-escaped line-form string (literal backslash-n separator) is decoded once and recovered", () => {
// Runtime .content value: 'm00001–m00002 example topic' + <backslash>n + 'folded summary body'.
const parsed = parseCompressInput({ content: "m00001–m00002 example topic\\nfolded summary body" });
assert.equal(parsed.diagnostics.ok, true, JSON.stringify(parsed.diagnostics));
assert.equal(parsed.ranges.length, 1);
assert.equal(parsed.ranges[0].startRef, "m00001");
assert.equal(parsed.ranges[0].endRef, "m00002");
assert.equal(parsed.ranges[0].summary, "folded summary body");
assert.equal(parsed.ranges[0].topic, "example topic");
});

test("#3780: double-escaped line-form string folds end-to-end through applyRanges", () => {
_setStoreForTest(new SessionStore({ enabled: false }));
const ctx = makeCtx();
seedTurn(ctx, [["user", "hello there"], ["assistant", "x".repeat(500)]]);
const out = applyRanges(parseCompressInput({ content: "m00001–m00002 example topic\\nfolded summary body" }), ctx).text;
assert.ok(out.startsWith("[Compressed m00001–m00002 → 1 block(s)"), out.split("\n")[0]);
assert.ok(!out.includes("FAILED"), out);
assert.ok(!out.includes("content-not-array"), out);
assert.equal(ctx.session.state.blocks.filter((b) => b.active).length, 1);
});

test("#3780: a payload that already carries a real newline is NOT re-decoded (literal backslash-n in the body survives)", () => {
// Real newline separates header from summary; the 'a<b>n' in the summary is a
// LITERAL backslash+n the user meant as text. The salvage must leave it untouched.
const parsed = parseCompressInput({ content: "m00001–m00002 t\na\\nb" });
assert.equal(parsed.diagnostics.ok, true, JSON.stringify(parsed.diagnostics));
assert.equal(parsed.ranges.length, 1);
assert.equal(parsed.ranges[0].summary, "a\\nb");
});

test("#3780: an escaped string with no valid refs header is NOT salvaged into a fake range", () => {
const parsed = parseCompressInput({ content: "no refs in this line\\njust some text" });
assert.equal(parsed.ranges.length, 0);
assert.notEqual(parsed.diagnostics.kind, "ok");
});
Loading