diff --git a/src/compress-tool.ts b/src/compress-tool.ts index 19ccf2499e..64ffdf82e9 100644 --- a/src/compress-tool.ts +++ b/src/compress-tool.ts @@ -246,13 +246,74 @@ export const IMAGE_FULL_TOOL_GOOGLE = { parameters: IMAGE_FULL_TOOL_OPENAI.function.parameters, }; +// #1494: models drift off the advertised {content: [...]} schema when they +// retry a single range: `content` becomes the single entry OBJECT instead of a +// one-element array (kernel hard-rejects it: kind=content-not-array, +// dropped=0), the whole argument becomes a bare entry array (kind=not-object), +// or the same shapes arrive string-encoded through a chat wire. The kernel's +// salvage ladder already tolerates this drift at the TOP level (a bare +// {startId,…} without content is accepted as one range) but hard-rejects it +// INSIDE content — an asymmetric wall that turns one shape slip into a full +// call failure plus a wasted retry round. Normalize those shapes back to +// canonical {content:[…]} before the kernel sees them; everything else passes +// through untouched. +function isPlainObject(v: unknown): v is Record { + return typeof v === "object" && v !== null && !Array.isArray(v); +} + +function looksLikeRangeEntry(o: Record): boolean { + const hasStart = typeof o["startId"] === "string" || typeof o["startRef"] === "string" || typeof o["messageId"] === "string"; + const hasEnd = typeof o["endId"] === "string" || typeof o["endRef"] === "string" || typeof o["messageId"] === "string"; + return hasStart && hasEnd && typeof o["summary"] === "string"; +} + +function decodeCompressObject(raw: string): Record | undefined { + let s = raw.trim(); + if (s.startsWith("```")) { + const nl = s.indexOf("\n"); + if (nl === -1) return undefined; + const body = s.slice(nl + 1); + const end = body.lastIndexOf("```"); + s = (end > 0 ? body.slice(0, end) : body).trim(); + } + let v: unknown; + try { v = JSON.parse(s); } catch { return undefined; } + if (typeof v === "string") { + try { v = JSON.parse(v); } catch { return undefined; } + } + return isPlainObject(v) ? v : undefined; +} + +export function normalizeCompressInput(input: unknown): unknown { + if (Array.isArray(input)) return { content: input }; + if (isPlainObject(input)) { + const c = input["content"]; + if (isPlainObject(c)) return { ...input, content: [c] }; + return input; + } + if (typeof input === "string") { + const obj = decodeCompressObject(input); + if (!obj) return input; + const c = obj["content"]; + if (isPlainObject(c)) return { ...obj, content: [c] }; + if (looksLikeRangeEntry(obj)) return { content: [obj] }; + return input; + } + return input; +} + export function parseCompressInput(input: unknown, callId?: string) { - const parsed = parseCompressArgs(input, { callId }); + const parsed = parseCompressArgs(normalizeCompressInput(input), { callId }); 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})`); } if (!parsed.diagnostics.ok && parsed.diagnostics.kind !== "ok") { loggerLog("warn", `[acp-compress-input] rejected: kind=${parsed.diagnostics.kind} invalidItems=${parsed.diagnostics.invalidItems}${parsed.diagnostics.keys ? ` keys=[${parsed.diagnostics.keys.join(",")}]` : ""}${parsed.diagnostics.length !== undefined ? ` len=${parsed.diagnostics.length}` : ""}${parsed.diagnostics.invalidReasons && parsed.diagnostics.invalidReasons.length > 0 ? ` reasons=[${parsed.diagnostics.invalidReasons.join(" | ")}]` : ""}`); + } else if (parsed.diagnostics.invalidItems > 0) { + // #1494: partial drop — at least one range survived, so the old + // condition above never fired and the rejection was invisible on both + // the server log and the model-facing receipt. + loggerLog("warn", `[acp-compress-input] partial: ${parsed.ranges.length} range(s) applied, ${parsed.diagnostics.invalidItems} rejected (kind=${parsed.diagnostics.kind}) reasons=[${(parsed.diagnostics.invalidReasons ?? []).join(" | ")}]`); } return { ranges: parsed.ranges, diagnostics: parsed.diagnostics }; } diff --git a/src/stream.ts b/src/stream.ts index 7e799492d0..a35a949428 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -1,4 +1,4 @@ -import { collectBlockContent, defaultCountTokens, formatRanges, storeCoveredOriginals, viableRanges, type CompressionCore, type Config, type CoreMessage, type CompressionState, type NudgeDecision } from "acp-kernel"; +import { collectBlockContent, defaultCountTokens, formatRanges, storeCoveredOriginals, viableRanges, type CompressionCore, type Config, type CoreMessage, type CompressionState, type NudgeDecision, type CompressParseDiagnostics } from "acp-kernel"; import { handleAcpStatus } from "./acp-status.js"; import { handleAcpCache, recordCacheFoldsFromBlocks } from "./cache-ledger.js"; import { type Session, cacheBlockContent, markDirty } from "./session.js"; @@ -197,6 +197,19 @@ export function summaryFingerprintLine(blockId: string, summary: string): string return ` · ${blockId} summary ${summary.length}ch · head "${head}" … tail "${tail}"`; } +// #1494: entries dropped at PARSE time vanish from `ranges`, so the success +// line ("Compressed ") and the 0-blocks failure both list only the +// survivors — a 3-entry call that silently loses one reads as a clean 2-block +// success (the exact report in the issue; the kernel diagnostics carry the +// per-entry reasons but nothing surfaced them when ≥1 range survived). +function droppedEntriesNote(diagnostics: CompressParseDiagnostics): string { + if (diagnostics.invalidItems <= 0) return ""; + const reasons = (diagnostics.invalidReasons ?? []).slice(0, 3).map((r) => (r.length > 160 ? r.slice(0, 160) + "..." : r)); + const why = reasons.length > 0 ? reasons.join(" | ") : `${diagnostics.invalidItems} entr(ies) failed validation (parse kind=${diagnostics.kind})`; + const n = diagnostics.invalidItems; + return `[${n} of the submitted entr${n === 1 ? "y" : "ies"} ${n === 1 ? "was" : "were"} REJECTED and NOT compressed: ${why}. Re-issue the rejected range${n === 1 ? "" : "s"} in a new compress call.]`; +} + // #1387 (pi-side #420/#521 alignment): post-compress continuation contract. // Silence after success is not a stop signal — models extrapolate ghost endIds // past the session tail and burn a round on kernel rejection. Wording is @@ -362,7 +375,8 @@ export function applyRanges(parsed: ReturnType, ctx: const noViableAnywhere = minChars > 0 && totalChars < minChars ? ` This conversation holds only ${totalChars} char(s) — below the ${minChars}-char minimum, so NO range can succeed yet; do not retry compress or call acp_status/search_context about it — continue answering the user's task.` : ""; - return `[Compression FAILED: ${errs}${revNote}${currentRefsSnapshot(ctx)}${recordCompressFailure(ctx.session, normalizedSpecKey(ranges))}${spanHint}${noViableAnywhere}]`; + const dropped = droppedEntriesNote(diagnostics); + return `[Compression FAILED: ${errs}${revNote}${currentRefsSnapshot(ctx)}${recordCompressFailure(ctx.session, normalizedSpecKey(ranges))}${spanHint}${noViableAnywhere}${dropped ? " " + dropped : ""}]`; } clearCompressFailures(ctx.session); @@ -394,6 +408,14 @@ export function applyRanges(parsed: ReturnType, ctx: const warn = r.warnings.length > 0 ? ` ${r.warnings.join("; ")}` : ""; let msg = `[Compressed ${detail} → ${r.blocksCreated} block(s), ~${r.tokensCompressed} tokens saved.${warn}]`; + // #1494: a partial fold must not read as a clean success — surface the + // parse-dropped entries (and log them server-side) so the model + // re-issues the rejected range instead of believing it folded. + const dropped = droppedEntriesNote(diagnostics); + if (dropped !== "") { + ctx.log(`[acp-proxy: compress PARTIAL — ${diagnostics.invalidItems} entr(ies) rejected at parse: ${(diagnostics.invalidReasons ?? []).join(" | ")}]`); + msg += `\n${dropped}`; + } // #1294 P1: append a fingerprint line per created/updated block — // kernel refolds update an existing block's summary in place (same id), // so "updated" means any pre-existing block whose summary changed. diff --git a/tests/issue1494-compress-params.test.ts b/tests/issue1494-compress-params.test.ts new file mode 100644 index 0000000000..5b4fdfb3bc --- /dev/null +++ b/tests/issue1494-compress-params.test.ts @@ -0,0 +1,149 @@ +// #1494: two param-shape defects in the compress receipt path. +// (A) Entries dropped at PARSE time vanished from a partial-success call — a +// 3-entry batch that lost one read as a clean 2-block success with no +// failure note, and nothing was logged server-side either. +// (B) Retrying the lost range with `content` as the single entry object (or a +// bare top-level array / string-encoded variants of the same drift) hit +// kernel kind=content-not-array / not-object — a hard failure on a shape +// the kernel's own salvage ladder already tolerates at the TOP level. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { CoreMessage } from "acp-kernel"; +import { assignRefs, createCore, createInitialState, defaultConfig, emptyRefMap } from "acp-kernel"; +import { normalizeCompressInput, parseCompressInput } from "../src/compress-tool.ts"; +import { applyRanges, type RewriteCtx } from "../src/stream.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; + +type Ctx = Omit & { log: (m: string) => void; logs: string[] }; + +function makeCtx(): Ctx { + const logs: string[] = []; + return { + core: createCore(), + config: defaultConfig(200000), + messages: [] as CoreMessage[], + session: { + id: `i1494-${Math.random().toString(36).slice(2)}`, + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, cacheSamples: 0, lastInputTokens: 0, compressCreditTokens: 0, contextTokens: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + }, + log: (m: string) => { logs.push(m); }, + logs, + }; +} + +/** n small alternating messages appended AFTER the case's own turns so every + * test keeps a realistic "recent activity" tail; protection is disabled in + * seedTurn instead, so ranges address only the head messages. */ +function tail(n: number): Array<[string, string]> { + return Array.from({ length: n }, (_, i): [string, string] => [i % 2 === 0 ? "user" : "assistant", `recent ${i}`]); +} + +function seedTurn(ctx: Ctx, head: Array<[string, string]>, tailN = 5): void { + const turns = [...head, ...tail(tailN)]; + const msgs: CoreMessage[] = turns.map(([role, text], i) => ({ id: `raw${i}`, role: role as "user" | "assistant", contentType: "text", text })); + ctx.messages = msgs; + ctx.session.state.messageRefs = assignRefs(msgs, { existing: emptyRefMap(), nextIndex: 0 }).map; + // Disable the kernel's soft protected zone (last-N msgs / last-N tokens / + // last user message) so small deterministic seeds are compressible at any + // ref; production defaults stay asserted elsewhere. + ctx.config.preserveRecentMessages = 0; + ctx.config.preserveRecentTokens = 0; + ctx.config.compress.minCompressRange = 0; + ctx.config.compress.minSummaryLength = 0; +} + +test("#1494 B: content given as the single entry object is normalized to a one-element array", () => { + const parsed = parseCompressInput({ content: { startId: "m00001", endId: "m00002", summary: "solo" } }); + assert.equal(parsed.ranges.length, 1); + assert.equal(parsed.diagnostics.ok, true); + assert.equal(parsed.diagnostics.kind, "ok"); +}); + +test("#1494 B: bare top-level entry array is normalized to {content:[…]}", () => { + const parsed = parseCompressInput([{ startId: "m00001", endId: "m00002", summary: "solo" }]); + assert.equal(parsed.ranges.length, 1); + assert.equal(parsed.diagnostics.kind, "ok"); +}); + +test("#1494 B: string-encoded single-object content (chat-wire shape drift) is recovered", () => { + const parsed = parseCompressInput(JSON.stringify({ content: { startId: "m00001", endId: "m00002", summary: "solo" } })); + assert.equal(parsed.ranges.length, 1); + assert.equal(parsed.diagnostics.kind, "ok"); +}); + +test("#1494 B: fenced + double-stringified variant is recovered too", () => { + const inner = JSON.stringify({ content: { startId: "m00001", endId: "m00002", summary: "solo" } }); + const parsed = parseCompressInput("```json\n" + JSON.stringify(inner) + "\n```"); + assert.equal(parsed.ranges.length, 1); + assert.equal(parsed.diagnostics.kind, "ok"); +}); + +test("#1494 B: garbage object content degrades to per-entry reasons, not opaque content-not-array", () => { + const parsed = parseCompressInput({ content: { foo: 1 } }); + assert.equal(parsed.ranges.length, 0); + assert.notEqual(parsed.diagnostics.kind, "content-not-array"); + assert.ok((parsed.diagnostics.invalidReasons ?? []).some((r) => r.includes("missing range bounds")), JSON.stringify(parsed.diagnostics)); +}); + +test("#1494 B: canonical inputs pass through normalization unchanged", () => { + const canonical = { content: [{ startId: "m00001", endId: "m00002", summary: "s" }] }; + assert.deepEqual(normalizeCompressInput(canonical), canonical); + assert.deepEqual(normalizeCompressInput({}), {}); + assert.equal(normalizeCompressInput(""), ""); + const lineForm = '{"content":["m00001–m00002 topic\\nsummary body"]}'; + assert.equal(normalizeCompressInput(lineForm), lineForm, "non-object-shaped string content is untouched"); + // #1366 empty-call detection must keep seeing the original empty shape. + const emptyParsed = parseCompressInput({}); + assert.ok(emptyParsed.diagnostics.kind === "missing-content" || emptyParsed.diagnostics.kind === "not-object", emptyParsed.diagnostics.kind); +}); + +test("#1494 A: a parse-dropped entry is surfaced in the SUCCESS receipt and logged", () => { + _setStoreForTest(new SessionStore({ enabled: false })); + const ctx = makeCtx(); + seedTurn(ctx, [["user", "hello there"], ["assistant", "x".repeat(500)]], 2); + const out = applyRanges(parseCompressInput({ content: [ + { startId: "m00001", endId: "m00002", summary: "valid fold" }, + { startId: "m00003" }, + ] }), ctx); + assert.ok(out.startsWith("[Compressed m00001–m00002 → 1 block(s)"), out.split("\n")[0]); + assert.ok(out.includes("1 of the submitted entry was REJECTED and NOT compressed"), out); + assert.ok(out.includes("entry 1: missing range bounds"), out); + assert.ok(out.includes("Re-issue the rejected range in a new compress call"), out); + assert.ok(ctx.logs.some((l) => l.includes("compress PARTIAL") && l.includes("rejected")), ctx.logs.join("\n")); + assert.equal(ctx.session.state.blocks.filter((b) => b.active).length, 1); +}); + +test("#1494 A: a parse-dropped entry is surfaced in the FAILED receipt too (0-blocks apply failure)", () => { + _setStoreForTest(new SessionStore({ enabled: false })); + const ctx = makeCtx(); + seedTurn(ctx, [["user", "hello there"], ["assistant", "x".repeat(500)]], 2); + // Force the apply-time gate: one entry survives parsing but its range is + // below minCompressRange → blocksCreated === 0 (FAILED branch) while the + // garbage sibling was dropped at parse — the receipt must still name it. + ctx.config.compress.minCompressRange = 10_000; + const out = applyRanges(parseCompressInput({ content: [ + { startId: "m00001", endId: "m00002", summary: "valid but too small" }, + { startId: "m00003" }, + ] }), ctx); + assert.ok(out.startsWith("[Compression FAILED:"), out.split("\n")[0]); + assert.ok(out.includes("1 of the submitted entry was REJECTED and NOT compressed"), out); + assert.ok(out.includes("entry 1: missing range bounds"), out); + assert.equal(ctx.session.state.blocks.filter((b) => b.active).length, 0); +}); + +test("#1494 A+B: single-object content 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: { startId: "m00001", endId: "m00002", summary: "solo fold via normalized content" } }), ctx); + assert.ok(out.startsWith("[Compressed m00001–m00002 → 1 block(s)"), out.split("\n")[0]); + assert.ok(!out.includes("REJECTED"), out); +});