diff --git a/src/codex-compact.ts b/src/codex-compact.ts index 4c85dbdcc1..4848d6057b 100644 --- a/src/codex-compact.ts +++ b/src/codex-compact.ts @@ -1,5 +1,5 @@ import { createHash, randomUUID } from "node:crypto"; -import type { CompressionBlock } from "acp-kernel"; +import { baseIdOf, summaryMessageId, type CompressionBlock } from "acp-kernel"; import { evidenceInputBaseline, type Session } from "./session.js"; import { codexCompactMode as knobCodexCompactMode } from "./knobs.js"; @@ -334,19 +334,135 @@ export function renderForgedSummary(block: Pick(); + for (const item of raw) { + let text: string | undefined; + let blockId: string | undefined; + if (typeof item === "string") { + text = item; + } else if (item !== null && typeof item === "object") { + const rec = item as { text?: unknown; blockId?: unknown }; + if (typeof rec.text === "string") { + text = rec.text; + if (typeof rec.blockId === "string" && rec.blockId.length > 0) blockId = rec.blockId; + } + } + if (text === undefined || seen.has(text)) continue; + seen.add(text); + out.push(blockId !== undefined ? { text, blockId } : { text }); + } + return out; +} + // The forged compaction item is opaque to the model and codex truncates its // history to [compaction_item, tail…], so the kernel deactivates the blocks // covering the truncated prefix on the next replay and their summaries would // vanish from the model's view. Capture them (kernel render format) at forge -// time; the server re-injects them into the developer message every turn -// (endpoint-form semantics). Append-only + exact-text dedup: a second forge -// accumulates, re-capture of deactivated blocks adds nothing. -export function mergeForgedSummaries(existing: string[] | undefined, blocks: readonly CompressionBlock[]): string[] { - const merged = existing ? [...existing] : []; +// time; the server re-injects them into the developer message whenever the +// echo is absent (#1064). Exact-text dedup: re-capture of an already-listed +// block adds nothing; each NEW active block appends one entry per forge. +export function mergeForgedSummaries(existing: unknown, blocks: readonly CompressionBlock[]): ForgedSummaryEntry[] { + const merged = normalizeForgedEntries(existing); + const byText = new Set(merged.map((e) => e.text)); for (const block of blocks) { if (!block.active) continue; const rendered = renderForgedSummary(block); - if (!merged.includes(rendered)) merged.push(rendered); + if (byText.has(rendered)) continue; + byText.add(rendered); + merged.push({ text: rendered, blockId: block.blockId }); } return merged; } + +// #4600: drop ledger entries that are provably redundant. An entry is +// redundant once a newer-generation block folded the message carrying its +// summary (summaryMessageId ∈ consumer.effectiveMessageIds) AND that consumer +// is itself represented — still active (its summary renders live in the +// conversation) or holding its own ledger entry. Entries whose block was +// merely deactivated by the post-forge truncation have no consumer: they are +// the load-bearing fallback debt and stay. Unknown-provenance entries (legacy +// strings), blocks absent from state, and still-active blocks are kept — +// pruning is strictly shrinking and never speculative. Returns the same array +// reference when nothing changed. +export function pruneForgedEntries(entries: ForgedSummaryEntry[], blocks: readonly CompressionBlock[]): ForgedSummaryEntry[] { + if (entries.length === 0) return entries; + const byBlockId = new Map(blocks.map((b) => [b.blockId, b])); + const represented = new Set(entries.filter((e) => e.blockId !== undefined).map((e) => e.blockId as string)); + // Only summary ids the ledger actually references need coverage lookups. + const consumers = new Map(); + for (const e of entries) { + if (e.blockId === undefined) continue; + const self = byBlockId.get(e.blockId); + if (!self || self.active) continue; + consumers.set(summaryMessageId(e.blockId), []); + } + if (consumers.size > 0) { + for (const b of blocks) { + for (const id of b.effectiveMessageIds ?? []) { + const list = consumers.get(baseIdOf(id)); + if (list) list.push(b); + } + } + } + let changed = false; + const kept: ForgedSummaryEntry[] = []; + for (const e of entries) { + if (e.blockId === undefined) { kept.push(e); continue; } + const self = byBlockId.get(e.blockId); + if (!self || self.active) { kept.push(e); continue; } + const superseded = (consumers.get(summaryMessageId(e.blockId)) ?? []).some((c) => c.active || represented.has(c.blockId)); + if (superseded) { changed = true; continue; } + kept.push(e); + } + return changed ? kept : entries; +} + +// #4600: evidence that a request replays an actual conversation (completed +// turns) rather than context-only auxiliary traffic (prewarms, sidecars, +// probes — the #4594 shape). The fallback re-injection exists to protect +// replayed history from losing its pre-compaction handoff; a request that +// replays no conversation has nothing to protect and must not pay the +// accumulated-ledger tax — which used to fail such requests outright with a +// 413 once the ledger grew past the window. +const CONVERSATION_ITEM_TYPES = new Set([ + "function_call", + "function_call_output", + "custom_tool_call", + "custom_tool_call_output", + "computer_call", + "mcp_call", + "reasoning", +]); + +export function carriesConversationTurns(input: unknown): boolean { + if (!Array.isArray(input)) return false; + for (const item of input) { + if (!item || typeof item !== "object") continue; + const it = item as { type?: unknown; role?: unknown }; + // Type-less assistant roles count too (OpenAI easy-input shorthand): + // a false positive only wastes tokens, a false negative loses the + // handoff — the predicate errs toward injecting. + if ((it.type === "message" || it.type === undefined) && it.role === "assistant") return true; + if (typeof it.type === "string" && CONVERSATION_ITEM_TYPES.has(it.type)) return true; + } + return false; +} + +// #4600: observability threshold — warn when the fallback re-injection eats a +// significant slice of the window (the field case hit ~1.2M tokens against a +// 375K window before failing preflight). 10% keeps ordinary sessions quiet. +export const FORGED_SUMMARY_INJECT_WARN_PCT = 0.1; diff --git a/src/server.ts b/src/server.ts index 3fb954b5ff..c7c0457895 100644 --- a/src/server.ts +++ b/src/server.ts @@ -112,7 +112,7 @@ import { isStrictReasoningEcho, modelIdOf, normalizeStrictEchoReasoning, normali export { isStrictReasoningEcho, normalizeStrictEchoReasoning, normalizeStrictEchoResponsesInput }; import { sanitizeResponsesInputIds, dropWhitespaceResponsesMessages, normalizeResponsesMessageItems } from "./loop/adapter-responses.js"; import type { CommandcodeEnvelopeMeta } from "./commandcode-wire.js"; -import { CODEX_COMPACT_HEALTH_RATIO, codexCompactMode, codexHealthBarApplies, hasCompactionTrigger, stripBiliCompactionItems, replaceBiliCompactionItems, codexCompactGate, codexCompactGatePre, buildTriggerForgeBody, mergeForgedSummaries } from "./codex-compact.js"; +import { CODEX_COMPACT_HEALTH_RATIO, codexCompactMode, codexHealthBarApplies, hasCompactionTrigger, stripBiliCompactionItems, replaceBiliCompactionItems, codexCompactGate, codexCompactGatePre, buildTriggerForgeBody } from "./codex-compact.js"; import { emitPreflightError } from "./stream-error.js"; import { agentProviderRecipes } from "./agent-providers.js"; import { affinityToken, claudeSubagentAgentId, claudeSubagentSplit, clientConversationHeader, codexTurnIdentity, conversationHeaderSource, dshPersonaFingerprintApplies, openaiSystemTextForPersona, preferPromptCacheKeyIdentity, type ConversationIdentity } from "./session-id.js"; diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index 94e16bfb2f..6e2dd69209 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -39,7 +39,7 @@ import { estimateCoreMessages, estimateCoreMessagesUpper } from "../preflight.js import { hoistTrappedToolItems } from "../tool-pair-order.js"; import { isStrictReasoningEcho, modelIdOf, normalizeStrictEchoResponsesInput } from "../strict-echo.js"; import { reconcileSystemAnchor } from "../system-anchor.js"; -import { buildPromptForgeBody, buildTriggerForgeBody, carriesCodexLocalCompactionSummary, codexCompactGate, codexCompactGatePre, codexCompactMode, hasCompactionPrompt, hasCompactionTrigger, isCodexClient, isCodexHost, mergeForgedSummaries, nativeCompactionKey, replaceBiliCompactionItems, stripBiliCompactionItems, CODEX_COMPACT_HEALTH_RATIO, CODEX_LOCAL_COMPACTION_MIN_MISSING } from "../codex-compact.js"; +import { buildPromptForgeBody, buildTriggerForgeBody, carriesCodexLocalCompactionSummary, carriesConversationTurns, codexCompactGate, codexCompactGatePre, codexCompactMode, hasCompactionPrompt, hasCompactionTrigger, isCodexClient, isCodexHost, mergeForgedSummaries, nativeCompactionKey, normalizeForgedEntries, pruneForgedEntries, replaceBiliCompactionItems, stripBiliCompactionItems, CODEX_COMPACT_HEALTH_RATIO, CODEX_LOCAL_COMPACTION_MIN_MISSING, FORGED_SUMMARY_INJECT_WARN_PCT } from "../codex-compact.js"; import type { ConversationIdentity } from "../session-id.js"; import { pckKeyAbated } from "../pck-probe.js"; import { reconcilePluginFoldCallIds } from "../plugin.js"; @@ -474,9 +474,35 @@ reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStr // message so the pre-compaction content is never lost. When the echo // DID come back, the replacement message carries the summaries and // the injection is suppressed to avoid duplicating them. - const forgedSummaries = echoReplaced - ? [] - : (session.metadata.codexForgedSummaries as string[] | undefined) ?? []; + // #4600: the ledger is debt with a lifecycle — prune entries whose + // block's summary was folded into a represented newer block (provably + // redundant, persisted), and inject nothing into requests that replay + // no conversation (context-only probes/sidecars have no replayed + // history to protect and must not pay the accumulated tax, which used + // to fail them with 413 once the ledger exceeded the window). + let forgedSummaries: string[] = []; + if (!echoReplaced) { + const forgedEntries = normalizeForgedEntries(session.metadata.codexForgedSummaries); + if (forgedEntries.length > 0) { + if (carriesConversationTurns(parsed.input)) { + const kept = pruneForgedEntries(forgedEntries, session.state.blocks); + forgedSummaries = kept.map((e) => e.text); + if (kept.length < forgedEntries.length) { + session.metadata.codexForgedSummaries = kept; + markDirty(session); + log("info", `[${sessionId}] [inject] pruned ${forgedEntries.length - kept.length} superseded forged-summary entrie(s) from the handoff ledger (${kept.length} remain)`); + } + } else { + log("debug", `[${sessionId}] [inject] skipped ${forgedEntries.length} forged-summary entrie(s): request replays no conversation (auxiliary/probe traffic)`); + } + } + } + if (forgedSummaries.length > 0 && config.modelContextLimit > 0) { + const injectedTokens = defaultCountTokens(forgedSummaries.join("\n\n---\n\n")); + if (injectedTokens >= config.modelContextLimit * FORGED_SUMMARY_INJECT_WARN_PCT) { + log("warn", `[${sessionId}] [inject] large forged-summary re-injection: ${forgedSummaries.length} entrie(s) ≈ ${injectedTokens} tokens (${Math.round((injectedTokens / config.modelContextLimit) * 100)}% of the ${config.modelContextLimit}-token window)`); + } + } // #1881: the NEVER-echo prohibition follows the rendered tags, not the tool switch. const tagsOnlyPrompt = !compressSurfaceOn && !isCompactionTrigger && !knobNoCompressPrompt() && !knobRenderNone() ? buildAcpTagsOnlyPrompt(responsesTextProtocol ? "hybrid" : "function", prompts, messageTimestampSections(surface?.promptSections, responsesTextProtocol ? "hybrid" : "function")) @@ -621,9 +647,11 @@ reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStr && isCodexHost(req.headers) // #3686: host policy, not wire profile && codexCompactGate(session, config.modelContextLimit, transformOk)) { const summaries = session.state.blocks.filter((b) => b.active).map((b) => b.summary); - const prevForged = session.metadata.codexForgedSummaries as string[] | undefined; - const captured = mergeForgedSummaries(prevForged, session.state.blocks); - if (captured.length !== (prevForged?.length ?? 0)) { + // #4600: capture + prune in one pass so the persisted ledger never + // keeps entries superseded by an already-represented newer block. + const prevEntries = normalizeForgedEntries(session.metadata.codexForgedSummaries); + const captured = pruneForgedEntries(mergeForgedSummaries(session.metadata.codexForgedSummaries, session.state.blocks), session.state.blocks); + if (captured.length !== prevEntries.length) { session.metadata.codexForgedSummaries = captured; markDirty(session); } diff --git a/tests/codex-compact-e2e.test.ts b/tests/codex-compact-e2e.test.ts index bb0d17df9b..95c9450696 100644 --- a/tests/codex-compact-e2e.test.ts +++ b/tests/codex-compact-e2e.test.ts @@ -5,7 +5,7 @@ import test from "node:test"; process.env.NODE_ENV = "test"; -import { defaultConfig } from "acp-kernel"; +import { defaultConfig, type CompressionBlock } from "acp-kernel"; import { startServer } from "../src/server.ts"; import type { ProxyOptions } from "../src/config.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; @@ -346,9 +346,9 @@ test("e2e E2 (trigger form): post-forge turn — echo replaced by a history-born assert.equal(h.bodies.length, afterSetup + 1, "post-forge turn forwarded to upstream exactly once"); const s = listSessions().find((x) => x.meta.label === SESSION)!; - const captured = s.metadata.codexForgedSummaries as string[] | undefined; + const captured = s.metadata.codexForgedSummaries as Array<{ text: string }> | undefined; assert.ok(Array.isArray(captured) && captured.length > 0, "forge captured the active block summaries"); - assert.ok(captured.some((t) => t.includes("MAIN-SUMMARY-SETUP")), "captured summary is the setup block's"); + assert.ok(captured.some((t) => t.text.includes("MAIN-SUMMARY-SETUP")), "captured summary is the setup block's"); const fwd = h.bodies[h.bodies.length - 1]; assert.ok(!fwd.includes("fc_bili_"), "echoed bili compaction item replaced before forwarding"); @@ -391,8 +391,8 @@ test("e2e E2 (trigger form): drop-only legacy marker echo still re-injects forge await r2.text(); const s = listSessions().find((x) => x.meta.label === SESSION)!; - const captured = s.metadata.codexForgedSummaries as string[] | undefined; - assert.ok(Array.isArray(captured) && captured.some((t) => t.includes("MAIN-SUMMARY-SETUP")), "forge captured the active block summaries"); + const captured = s.metadata.codexForgedSummaries as Array<{ text: string }> | undefined; + assert.ok(Array.isArray(captured) && captured.some((t) => t.text.includes("MAIN-SUMMARY-SETUP")), "forge captured the active block summaries"); // Turn 3: codex echoes back a LEGACY compaction item — id-prefix only, // no sentinel blob (older build) — so nothing is extractable and the @@ -419,6 +419,90 @@ test("e2e E2 (trigger form): drop-only legacy marker echo still re-injects forge }); }); +// #4600: the ledger is debt with a lifecycle. Two directions must hold in a +// real processTurn: (A) requests that replay no conversation pay nothing — +// the accumulated list used to be injected into every echo-less request, +// which failed context-only probes with 413 once it outgrew the window; +// (B) forge-time capture prunes entries whose block's summary was folded +// into an already-represented newer block, so the persisted debt stops +// accumulating. + +test("e2e E2 (#4600): request replaying no conversation gets no forged-summary re-injection", async () => { + await withHarness({ mode: "intercept", firstTurnTokens: 1000 }, async (h) => { + const afterSetup = await setupCompressedSession(h); + + // Turn 2: native auto-compact trigger → intercepted + forged. + const r2 = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [...conversation(), { type: "compaction_trigger" }] }), + }); + assert.equal(r2.status, 200); + await r2.text(); + + const s = listSessions().find((x) => x.meta.label === SESSION)!; + const before = s.metadata.codexForgedSummaries as Array<{ text: string }> | undefined; + assert.ok(Array.isArray(before) && before.some((e) => e.text.includes("MAIN-SUMMARY-SETUP")), "forge captured the ledger"); + + // Turn 3: context-only auxiliary traffic (title-gen shape — user + // context, no completed turns). It replays no conversation, so the + // accumulated ledger must NOT be re-injected into it. + const r3 = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [{ type: "message", role: "user", content: "Create a short title for this session" }] }), + }); + assert.equal(r3.status, 200); + await r3.text(); + assert.equal(h.bodies.length, afterSetup + 1, "probe forwarded to upstream exactly once"); + const fwd = h.bodies[h.bodies.length - 1]; + assert.ok(!fwd.includes("MAIN-SUMMARY-SETUP"), "no re-injection into a request that replays no conversation"); + assert.deepEqual(s.metadata.codexForgedSummaries, before, "the view-only gate leaves the persisted ledger untouched"); + }); +}); + +test("e2e E2 (#4600): forge-time capture prunes superseded ledger entries before persisting", async () => { + await withHarness({ mode: "intercept", firstTurnTokens: 1000 }, async (h) => { + const afterSetup = await setupCompressedSession(h); + const s = listSessions().find((x) => x.meta.label === SESSION)!; + const liveBlock = (s.state.blocks ?? []).find((b) => b.active); + assert.ok(liveBlock, "setup left an active block"); + + // Stale reality: two older-generation blocks whose summary messages + // the live block has folded into its own effective span, plus their + // ledger entries from an earlier forge. + const stale = (blockId: string): CompressionBlock => ({ + blockId, runId: `r_${blockId}`, tier: 1, summary: `STALE-${blockId}-SUMMARY-BODY`, + directMessageIds: [], effectiveMessageIds: [`orig_${blockId}`], directBlockIds: [], + compressedTokens: 10, createdAt: 0, survivedCount: 0, generation: "old", active: false, + }); + s.state.blocks = [...(s.state.blocks ?? []), stale("b_stale_1"), stale("b_stale_2")]; + liveBlock.effectiveMessageIds.push("acp_summary_b_stale_1", "acp_summary_b_stale_2"); + s.metadata.codexForgedSummaries = [ + { text: "[Compressed conversation section]\nSTALE-b_stale_1-SUMMARY-BODY", blockId: "b_stale_1" }, + { text: "[Compressed conversation section]\nSTALE-b_stale_2-SUMMARY-BODY", blockId: "b_stale_2" }, + ]; + + // Trigger-form compact over the FULL conversation: the live block + // survives syncBlocks, the forge branch runs, and both stale entries + // must be pruned from the persisted ledger. + const r2 = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [...conversation(), { type: "compaction_trigger" }] }), + }); + assert.equal(r2.status, 200); + const text = await r2.text(); + assert.ok(text.includes("fc_bili_"), "compact forged"); + assert.equal(h.bodies.length, afterSetup, "upstream untouched"); + + const after = s.metadata.codexForgedSummaries as Array<{ text: string; blockId?: string }> | undefined; + assert.ok(Array.isArray(after), "ledger persisted as entries"); + assert.ok(after.every((e) => !e.text.includes("STALE-")), "superseded entries pruned at forge time"); + assert.ok(after.some((e) => e.text.includes("MAIN-SUMMARY-SETUP")), "current forge's own capture kept"); + }); +}); + test("e2e E2 (endpoint form): intercept + healthy ACP → forged JSON {output}, upstream untouched", async () => { await withHarness({ mode: "intercept", firstTurnTokens: 1000 }, async (h) => { const afterSetup = await setupCompressedSession(h); @@ -631,7 +715,7 @@ test("prompt-form auto-compact is intercepted and forged locally (#3008)", async assert.equal(h.bodies.length, after, "upstream not contacted for the intercepted auto-compact"); const s = listSessions().find((x) => x.meta.label === SESSION); assert.ok(s, "session tracked"); - const captured = s!.metadata.codexForgedSummaries as string[] | undefined; + const captured = s!.metadata.codexForgedSummaries as Array<{ text: string }> | undefined; assert.ok(captured && captured.length > 0, "summaries captured for post-compaction re-injection"); }); }); diff --git a/tests/codex-compact.test.ts b/tests/codex-compact.test.ts index 63babfc5c1..ee4935dfe6 100644 --- a/tests/codex-compact.test.ts +++ b/tests/codex-compact.test.ts @@ -17,6 +17,9 @@ import { buildTriggerForgeBody, renderForgedSummary, mergeForgedSummaries, + normalizeForgedEntries, + pruneForgedEntries, + carriesConversationTurns, CODEX_COMPACT_ID_PREFIX, CODEX_COMPACT_SENTINEL, } from "../src/codex-compact.ts"; @@ -241,23 +244,130 @@ test("renderForgedSummary: kernel render format (topic, no topic, empty body)", assert.equal(renderForgedSummary({ summary: " " }), "[Compressed conversation section]"); }); -test("mergeForgedSummaries: append active, skip inactive, dedup, accumulate across forges", () => { - const a = blockWith("SUMMARY-A", true, "Topic A"); - const b = blockWith("SUMMARY-B", true); - const c = blockWith("SUMMARY-C", false); +test("mergeForgedSummaries: append active, skip inactive, dedup, accumulate across forges (#4600 entries)", () => { + const mk = (blockId: string, summary: string, active: boolean, topic?: string): CompressionBlock => { + const b = blockWith(summary, active, topic); + b.blockId = blockId; + return b; + }; + const a = mk("b_a", "SUMMARY-A", true, "Topic A"); + const b = mk("b_b", "SUMMARY-B", true); + const c = mk("b_c", "SUMMARY-C", false); let merged = mergeForgedSummaries(undefined, [a, b, c]); assert.equal(merged.length, 2, "inactive block skipped"); - assert.equal(merged[0], "[Compressed conversation section] — Topic A\nSUMMARY-A"); - assert.equal(merged[1], "[Compressed conversation section]\nSUMMARY-B"); + assert.deepEqual(merged[0], { text: "[Compressed conversation section] — Topic A\nSUMMARY-A", blockId: "b_a" }); + assert.deepEqual(merged[1], { text: "[Compressed conversation section]\nSUMMARY-B", blockId: "b_b" }); merged = mergeForgedSummaries(merged, [a, b]); assert.equal(merged.length, 2, "exact-text dedup on re-capture"); - const d = blockWith("SUMMARY-D", true); + const d = mk("b_d", "SUMMARY-D", true); merged = mergeForgedSummaries(merged, [d]); assert.equal(merged.length, 3, "second forge accumulates"); - assert.equal(merged[2], "[Compressed conversation section]\nSUMMARY-D"); + assert.deepEqual(merged[2], { text: "[Compressed conversation section]\nSUMMARY-D", blockId: "b_d" }); assert.deepEqual(mergeForgedSummaries(undefined, []), []); }); +test("normalizeForgedEntries: legacy strings, object entries, garbage (#4600)", () => { + assert.deepEqual(normalizeForgedEntries(undefined), []); + assert.deepEqual(normalizeForgedEntries("not-an-array"), []); + assert.deepEqual(normalizeForgedEntries(["legacy-a", "legacy-b"]), [{ text: "legacy-a" }, { text: "legacy-b" }]); + assert.deepEqual(normalizeForgedEntries([ + { text: "obj-entry", blockId: "b1" }, + { text: "obj-entry", blockId: "OTHER" }, + "plain-string", + null, + 42, + { blockId: "no-text" }, + { text: 7 }, + ]), [ + { text: "obj-entry", blockId: "b1" }, + { text: "plain-string" }, + ], "duplicate text keeps first-seen provenance; non-entry garbage dropped"); +}); + +// #4600: supersession pruning. Helper builds full blocks with explicit ids. +function blk(blockId: string, active: boolean, effectiveMessageIds: string[]): CompressionBlock { + return { + blockId, runId: `r_${blockId}`, tier: 1, summary: `S-${blockId}`, + directMessageIds: [], effectiveMessageIds, directBlockIds: [], + compressedTokens: 10, createdAt: 0, survivedCount: 0, generation: "young", active, + }; +} + +test("pruneForgedEntries: supersession matrix (#4600)", () => { + // Truncation debt stays: block deactivated without any consumer folding its summary message. + const debt = pruneForgedEntries([{ text: "T-debt", blockId: "b_debt" }], [blk("b_debt", false, ["orig-x"])]); + assert.equal(debt.length, 1, "unconsumed truncation debt is load-bearing and stays"); + + const pruned = pruneForgedEntries( + [{ text: "T-old", blockId: "b_old" }, { text: "T-new", blockId: "b_new" }], + [blk("b_old", false, ["orig-1"]), blk("b_new", true, ["orig-1", "acp_summary_b_old"])], + ); + assert.deepEqual(pruned, [{ text: "T-new", blockId: "b_new" }], "entry dropped once an ACTIVE consumer carries the content"); + + const viaEntry = pruneForgedEntries( + [{ text: "T-old", blockId: "b_old" }, { text: "T-mid", blockId: "b_mid" }], + [blk("b_old", false, []), blk("b_mid", false, ["acp_summary_b_old"])], + ); + assert.deepEqual(viaEntry, [{ text: "T-mid", blockId: "b_mid" }], "represented-by-own-entry consumer also supersedes"); + + const unrep = pruneForgedEntries( + [{ text: "T-old", blockId: "b_old" }], + [blk("b_old", false, []), blk("b_mid", false, ["acp_summary_b_old"])], + ); + assert.equal(unrep.length, 1, "unrepresented consumer does not transfer representation"); + + const chain = pruneForgedEntries( + [ + { text: "T-g1", blockId: "b_g1" }, + { text: "T-g2", blockId: "b_g2" }, + { text: "T-g3", blockId: "b_g3" }, + ], + [ + blk("b_g1", false, []), + blk("b_g2", false, ["acp_summary_b_g1"]), + blk("b_g3", false, ["acp_summary_b_g2"]), + blk("b_top", true, ["acp_summary_b_g3"]), + ], + ); + assert.deepEqual(chain, [], "chain g1→g2→g3→active top: all links redundant"); + + const legacy = pruneForgedEntries([{ text: "T-legacy" }], [blk("b_top", true, [])]); + assert.deepEqual(legacy, [{ text: "T-legacy" }], "legacy entry without provenance is never pruned"); + + const ghost = pruneForgedEntries([{ text: "T-ghost", blockId: "b_ghost" }], [blk("b_top", true, ["acp_summary_b_ghost"])]); + assert.deepEqual(ghost, [{ text: "T-ghost", blockId: "b_ghost" }], "absent block cannot be verified as superseded"); + + const live = pruneForgedEntries([{ text: "T-live", blockId: "b_live" }], [blk("b_live", true, []), blk("b_other", true, ["acp_summary_b_live"])]); + assert.deepEqual(live, [{ text: "T-live", blockId: "b_live" }], "still-active block's entry is never pruned"); + + const subid = pruneForgedEntries([{ text: "T-old", blockId: "b_old" }], [blk("b_old", false, []), blk("b_new", true, ["acp_summary_b_old#r0"])]); + assert.deepEqual(subid, [], "sub-id form of the summary id counts as coverage"); + + const same = [{ text: "T-debt", blockId: "b_debt" }]; + assert.equal(pruneForgedEntries(same, [blk("b_debt", false, ["orig-x"])]), same, "no-op returns the same array reference"); + const empty: typeof same = []; + assert.equal(pruneForgedEntries(empty, []), empty, "empty input returns the same reference"); +}); + +test("carriesConversationTurns: completed-turn evidence vs context-only traffic (#4600)", () => { + const msg = (role: string) => ({ type: "message", role, content: "x" }); + assert.equal(carriesConversationTurns([msg("user"), msg("assistant")]), true, "assistant message"); + assert.equal(carriesConversationTurns([{ type: "function_call", call_id: "c1", name: "bash", arguments: "{}" }]), true, "function_call"); + assert.equal(carriesConversationTurns([{ type: "function_call_output", call_id: "c1", output: "ok" }]), true, "function_call_output"); + assert.equal(carriesConversationTurns([{ type: "custom_tool_call", call_id: "c2", name: "apply_patch", input: "" }]), true, "custom_tool_call"); + assert.equal(carriesConversationTurns([{ type: "custom_tool_call_output", call_id: "c2", output: "ok" }]), true, "custom_tool_call_output"); + assert.equal(carriesConversationTurns([{ type: "computer_call", call_id: "c3", status: "completed", actions: [] }]), true, "computer_call"); + assert.equal(carriesConversationTurns([{ type: "mcp_call", call_id: "c4", status: "completed", server_label: "s", tool: "t", arguments: {}, result: {} }]), true, "mcp_call"); + assert.equal(carriesConversationTurns([{ type: "reasoning", id: "rs_1", summary: [] }]), true, "reasoning item"); + assert.equal(carriesConversationTurns([null, 42, { role: "assistant" }]), true, "type-less assistant shorthand counts (lenient by design)"); + assert.equal(carriesConversationTurns([msg("developer"), msg("user"), { type: "additional_tools" }]), false, "context-only probe traffic"); + assert.equal(carriesConversationTurns([{ type: "compaction", id: "cmp_1", encrypted_content: "opaque" }, msg("user")]), false, "compaction item + user context only"); + assert.equal(carriesConversationTurns([null, 42, { type: "message", role: "user" }]), false, "non-object noise and user-only items are not evidence"); + assert.equal(carriesConversationTurns([]), false); + assert.equal(carriesConversationTurns("string input"), false); + assert.equal(carriesConversationTurns(null), false); +}); + // --------------------------------------------------------------------------- // #3008: prompt-form auto-compact detection + forge // ---------------------------------------------------------------------------