diff --git a/kernel/package.json b/kernel/package.json index 8eccda41ac..22c5b11f31 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.114", + "version": "0.0.115", "description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.", "license": "MIT", "author": "ranxianglei", diff --git a/kernel/src/instance-reid.ts b/kernel/src/instance-reid.ts index 99109daa3a..411773e764 100644 --- a/kernel/src/instance-reid.ts +++ b/kernel/src/instance-reid.ts @@ -36,6 +36,21 @@ function clusterRoot(id: string): string | null { * forms of a covered root, non-h_ ids, roots without a covered copy) keep the * converter's numbering untouched — renumbering those would only churn the * prefix cache. + * + * INACTIVE blocks (#2695): their covered ids join the dodge set, because a + * genuinely new instance landing on a dead block's number resurrects the + * block in sync ("some covered id present") and its summary carrier starts + * rendering — for content the instance was never part of. Renumbering + * against a dead block is gated by cluster-root continuity: only when some + * sibling of the same root was live last pass (the arriving instance is a + * NEW extension of a continuing cluster). A block that is inactive at + * pipeline start cannot itself have any covered id in `prior` (sync would + * have kept it alive otherwise), so "inactive-covered collision + sibling + * in prior" is exactly the new-extension-onto-dead-block shape. A + * whole-root arrival that is new to `prior` — a client rewinding to a + * checkpoint that still holds the folded originals — keeps the converter's + * ids and the block legitimately resurrects (carrier + prune, the #462 + * economy restored). */ export function remintCoveredLiveIds( messages: CoreMessage[], @@ -43,7 +58,14 @@ export function remintCoveredLiveIds( ): CoreMessage[] { const coveredBases = new Set(); for (const id of coveredMessageIds(state)) coveredBases.add(baseIdOf(id)); - if (coveredBases.size === 0) return messages; + const inactiveBases = new Set(); + for (const block of state.blocks) { + if (block.active) continue; + for (const id of block.effectiveMessageIds) { + inactiveBases.add(baseIdOf(id)); + } + } + if (coveredBases.size === 0 && inactiveBases.size === 0) return messages; // Pre-feature persisted state has no prior-pass snapshot: fall back to // renumber-nothing (0.0.95 semantics) for this one pass. The snapshot is // written below in processTurn, so the next pass discriminates correctly. @@ -62,12 +84,19 @@ export function remintCoveredLiveIds( const next = [...messages]; let changed = false; for (const [root, idxs] of groups) { - // Only renumber when some live instance claims an id a folded copy owns; - // otherwise the converter's numbering is already collision-free here. - const conflict = idxs.some((i) => - coveredBases.has(baseIdOf(messages[i]!.id)), + // Only renumber when some live instance claims an id a folded copy owns + // (active or inactive); otherwise the converter's numbering is already + // collision-free here. + const conflict = idxs.some( + (i) => + coveredBases.has(baseIdOf(messages[i]!.id)) || + inactiveBases.has(baseIdOf(messages[i]!.id)), ); if (!conflict) continue; + // #2695: an inactive block's covered id is only renumbered away when the + // same root shows prior-pass continuity (see header) — a whole-root + // rewind keeps its ids so the block can resurrect. + const rootContinues = idxs.some((i) => prior.has(messages[i]!.id)); // Renumbered ids must dodge both folded-claimed numbers and ids other // live instances of this root already hold (untouched ones keep theirs). const liveIds = new Set(idxs.map((i) => baseIdOf(messages[i]!.id))); @@ -75,15 +104,19 @@ export function remintCoveredLiveIds( for (const i of idxs) { const id = messages[i]!.id; const exactCovered = coveredBases.has(baseIdOf(id)); + const exactCoveredInactive = inactiveBases.has(baseIdOf(id)); // Only a genuinely new instance is renumbered: its exact id is claimed // by a folded copy AND it was absent from the previous pass. Everything // else — the folded original's own resend (prior.has), converter-numbered - // "_n" forms that no folded copy claims, the live un-folded bare — keeps - // the converter's numbering; renumbering those would only churn the + // "_n" forms that no folded copy claims, the live un-folded bare, and + // (#2695) a whole-root rewind onto an inactive block — keeps the + // converter's numbering; renumbering those would only churn the // prefix cache and let summarized content escape prune (#462). - if (!exactCovered || prior.has(id)) continue; + if (prior.has(id)) continue; + if (!exactCovered && !(exactCoveredInactive && rootContinues)) continue; while ( coveredBases.has(`${root}_${k}`) || + inactiveBases.has(`${root}_${k}`) || liveIds.has(`${root}_${k}`) || prior.has(`${root}_${k}`) ) diff --git a/kernel/tests/issue1476-reid.test.ts b/kernel/tests/issue1476-reid.test.ts index 3663eaf336..268827c540 100644 --- a/kernel/tests/issue1476-reid.test.ts +++ b/kernel/tests/issue1476-reid.test.ts @@ -250,3 +250,59 @@ test("missing lastPassIds falls back to renumber-nothing for one pass (#462)", ( ); assert.equal(out.length, 1); }); + +test("renumbers a new cluster extension off an INACTIVE block's covered ids (#2695)", () => { + const state = createInitialState(); + // b2 died when the host decimated its substrate; its covered numbers are + // h_.._5.._7 of the duplicated-reply cluster. + const dead = folded([`${HASH}_5`, `${HASH}_6`, `${HASH}_7`]); + dead.active = false; + state.blocks.push(dead); + // Last pass carried the continuing cluster h_.._0.._4 (a live sibling of + // the same root) — so h_.._5 arriving now is a new extension, not a rewind. + state.lastPassIds = [ + EARLY, + HASH, + `${HASH}_1`, + `${HASH}_2`, + `${HASH}_3`, + `${HASH}_4`, + ]; + const out = remintCoveredLiveIds( + [ + msg(EARLY, "hi"), + msg(HASH, "a"), + msg(`${HASH}_1`, "a"), + msg(`${HASH}_2`, "a"), + msg(`${HASH}_3`, "a"), + msg(`${HASH}_4`, "a"), + msg(`${HASH}_5`, "a"), + ], + state, + ); + const five = out.find((m) => (m.text ?? "") === "a" && m.id === `${HASH}_5`); + assert.ok(!five, "the dead block's h_.._5 must not be re-occupied"); + const minted = out[out.length - 1]!; + assert.equal(minted.id, `${HASH}_8`, "dodges the dead block's _5.._7"); + assert.equal(out[1]!.id, HASH, "continuing instances keep their ids"); +}); + +test("keeps a whole-root rewind onto an inactive block intact (#2695)", () => { + const state = createInitialState(); + const dead = folded([HASH, `${HASH}_1`]); + dead.active = false; + state.blocks.push(dead); + // Prior pass saw none of this root (client trimmed it away); the arrival + // is a rewind to a checkpoint that still holds the folded originals — an + // echo class, not a new extension. + state.lastPassIds = [EARLY]; + const out = remintCoveredLiveIds( + [msg(EARLY, "hi"), msg(HASH, "a"), msg(`${HASH}_1`, "a")], + state, + ); + assert.deepEqual( + out.map((m) => m.id), + [EARLY, HASH, `${HASH}_1`], + "rewind echoes keep the converter ids so the block can resurrect", + ); +}); diff --git a/src/fold-reconcile.ts b/src/fold-reconcile.ts index ae2c2cfd4f..96f6bb5c47 100644 --- a/src/fold-reconcile.ts +++ b/src/fold-reconcile.ts @@ -73,13 +73,31 @@ // trailing placeholder line stripped/appended against the anchor's // normalized identity. // +// Reap (#2695) — ZOMBIE STREAK. When a host deletes or decimates history +// WITHOUT an announced boundary (no dsh framing, no /compact), the covered +// substrate of the affected blocks never comes back, but duplicate-content +// re-derivation keeps a residual id alive so syncBlocks re-activates the +// block forever: its acp_summary carrier keeps rendering (and flapping) +// mid-list every turn — a permanent prefix-cache buster. The per-block +// coverage record (METADATA_FOLD_COVERAGE) now streak-counts passes where a +// strict MAJORITY of the block's covered ids is absent (2*(p+r) < t), gated +// on verified presence (e === 1 — never-represent substrates like +// view-folding hosts keep their #2202 status-quo semantics). After +// FOLD_DRIFT_ESCALATE_PASSES consecutive majority-loss passes the block is +// REAPED — removed from state.blocks (not deactivated: syncBlocks would +// resurrect it) with #395 archive semantics, blockContents retained for the +// derived-decompress fallback. Proxy mode only: a plugin-mode fold +// legitimately replaces the covered history (the agent's own compress runs +// against its private state), so absence there is sanctioned. +// // Modes (config `compress.reconcile`, env BILI_FOLD_RECONCILE): // "off" — disabled (pre-#1921 behavior). // "warn" — compute + log only, no rewrite. // "repair" — rewrite block ids (default). import { createHash } from "node:crypto"; import { defaultCountTokens, type CoreMessage } from "acp-kernel"; -import type { Session } from "./session.js"; +import { reapDestroyedSubstrate, type Session } from "./session.js"; +import { recordConflict } from "./conflict-watch.js"; type FoldReconcileMode = "off" | "warn" | "repair"; @@ -123,8 +141,8 @@ export const METADATA_FOLD_COVERAGE = "foldCoverageByBlock"; * side-requests (#1075: title-gen / WebSearch refinement "carry only a * handful of brand-new messages") — no drift evidence is taken from them. * Same value as REWRITE_MIN_INCOMING_TOTAL (src/session.ts), which the #1195 - * sibling warn guards with; kept local so this module stays dependency-free - * beyond its type import. */ + * sibling warn guards with; kept local so the module's only host imports + * stay the #2695 reap/ledger hooks below. */ const SIDE_REQUEST_MAX_MSGS = 10; /** pi's NON_VISION_USER_IMAGE_PLACEHOLDER (pi-stable-ai transform-messages): * replaying a conversation onto a text-only model replaces every image @@ -219,6 +237,9 @@ interface FoldReconcileResult { byNorm: number; byTurn: number; byImage: number; + /** #2695: fold blocks reaped this pass (removed from state.blocks after + * a majority-loss zombie streak). */ + reaped: number; unmatched: number; } @@ -240,6 +261,14 @@ export interface FoldBlockCoverage { * accrual) from structural absence (view-folding hosts whose resends never * carry raw originals → unverifiable, keep status-quo booking). */ e?: 1; + /** #2695: consecutive passes with a strict MAJORITY of covered ids absent + * (2*(p+r) < t), counted only for verified substrates (e === 1). A + * healthy proxy-mode fold re-receives its covered originals every turn + * (p+r = t resets this to 0); model-switch churn reclaims most ids via + * anchors (p+r ≈ t likewise). When z reaches FOLD_DRIFT_ESCALATE_PASSES + * the substrate is taken for destroyed by an unannounced client rewrite + * and the block is reaped (reapDestroyedSubstrate, src/session.ts). */ + z?: number; } const seenInvalidEnv = new Set(); @@ -921,13 +950,13 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt const mode = opts.mode ?? resolveFoldReconcileMode(process.env); if (mode === "off") { resetFoldDriftState(session); - return { kind: "off", missing: 0, claims: 0, byPos: 0, byTool: 0, byNorm: 0, byTurn: 0, byImage: 0, unmatched: 0 }; + return { kind: "off", missing: 0, claims: 0, byPos: 0, byTool: 0, byNorm: 0, byTurn: 0, byImage: 0, reaped: 0, unmatched: 0 }; } const blocks = (session.state?.blocks ?? []) as BlockLike[]; const covered = coveredIdsOf(blocks); if (covered.size === 0) { resetFoldDriftState(session); - return { kind: "noop", missing: 0, claims: 0, byPos: 0, byTool: 0, byNorm: 0, byTurn: 0, byImage: 0, unmatched: 0 }; + return { kind: "noop", missing: 0, claims: 0, byPos: 0, byTool: 0, byNorm: 0, byTurn: 0, byImage: 0, reaped: 0, unmatched: 0 }; } // #2202: auxiliary side-requests (title-gen, WebSearch refinement — #1075) // share the conversation id but do not carry the conversation. Reconciling @@ -940,9 +969,9 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt // the episode state is left exactly as found (the resets above stay // reserved for true episode boundaries: reconcile off / no folds at all). if (msgs.length < SIDE_REQUEST_MAX_MSGS) { - return { kind: "noop", missing: 0, claims: 0, byPos: 0, byTool: 0, byNorm: 0, byTurn: 0, byImage: 0, unmatched: 0 }; + return { kind: "noop", missing: 0, claims: 0, byPos: 0, byTool: 0, byNorm: 0, byTurn: 0, byImage: 0, reaped: 0, unmatched: 0 }; } - if (!session.metadata) return { kind: "noop", missing: 0, claims: 0, byPos: 0, byTool: 0, byNorm: 0, byTurn: 0, byImage: 0, unmatched: 0 }; + if (!session.metadata) return { kind: "noop", missing: 0, claims: 0, byPos: 0, byTool: 0, byNorm: 0, byTurn: 0, byImage: 0, reaped: 0, unmatched: 0 }; const anchors: Record = (session.metadata[METADATA_ANCHORS] as Record | undefined) ?? {}; @@ -1077,10 +1106,69 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt } const rec: FoldBlockCoverage = { p, r, t: ids.length }; if (prevCov[bid]?.e === 1 || p + r > 0) rec.e = 1; + // #2695: zombie streak — strict majority of covered ids absent, only + // for VERIFIED substrates (e === 1). Never-represent substrates keep + // their #2202 structural-absence semantics (no streak, no reap). + const gone = 2 * (p + r) < ids.length; + if (gone) { + if (rec.e === 1) rec.z = (prevCov[bid]?.z ?? 0) + 1; + } else { + rec.z = 0; + } if (covCount >= MAX_ANCHORS) continue; nextCov[bid] = rec; covCount++; } + + // #2695: reap blocks whose covered substrate stayed majority-absent for + // FOLD_DRIFT_ESCALATE_PASSES consecutive passes. REMOVAL from + // state.blocks — not deactivation: syncBlocks re-activates any block + // whose id set still intersects the wire (kernel/src/sync.ts sets + // active=true before the stillPresent check), and duplicate-content + // re-derivation keeps exactly that intersection alive forever, which is + // how the zombie carrier kept rendering (and flapping) every turn. + // Proxy mode only (plugin folds legitimately replace covered history — + // the agent's own compress runs against its private state); repair mode + // only (warn observes). Runs BEFORE the backbone persistence below so + // the reaped ids are stripped from the same objects that get stored. + let reapedCount = 0; + if (mode === "repair" && session.metadata.pluginAgent === undefined) { + const ripe = new Set(); + for (const block of blocks) { + const bid = block.blockId; + if (bid !== undefined && (nextCov[bid]?.z ?? 0) >= FOLD_DRIFT_ESCALATE_PASSES) ripe.add(bid); + } + if (ripe.size > 0) { + const liveRawIds = new Set(msgs.map((m) => m.id)); + const reaped = reapDestroyedSubstrate(session, ripe, liveRawIds, opts.log ?? (() => {})); + reapedCount = reaped.length; + if (reapedCount > 0) { + // Strip the reaped blocks from the coverage/backbone records + // this pass persists, so nothing seeds or positions their ids + // again (nextOrder is the array METADATA_POSITIONS already + // references — mutate in place). + const deadIds = new Set(); + for (const block of blocks) { + if (block.blockId === undefined || !ripe.has(block.blockId)) continue; + delete nextCov[block.blockId]; + for (const id of block.effectiveMessageIds ?? []) deadIds.add(id); + for (const id of block.directMessageIds ?? []) deadIds.add(id); + } + for (const id of deadIds) delete nextAnchors[id]; + for (let i = nextOrder.length - 1; i >= 0; i--) { + if (deadIds.has(nextOrder[i])) nextOrder.splice(i, 1); + } + const storedPositions = session.metadata[METADATA_POSITIONS] as { ids?: string[] } | undefined; + const positionIds = storedPositions?.ids; + if (Array.isArray(positionIds) && positionIds !== nextOrder) { + for (let i = positionIds.length - 1; i >= 0; i--) { + if (deadIds.has(positionIds[i])) positionIds.splice(i, 1); + } + } + recordConflict(session, "orphan-reap", `reaped ${reapedCount} zombie fold block(s) [${reaped.join(", ")}]: covered substrate majority-absent for ${FOLD_DRIFT_ESCALATE_PASSES} consecutive passes (#2695)`); + } + } + } session.metadata[METADATA_FOLD_COVERAGE] = nextCov; if (plan.claims.size > 0 && mode === "repair") { @@ -1138,7 +1226,7 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt } if (plan.unmatched.length === 0 && plan.claims.size === 0) { - return { kind: "resend", missing: 0, claims: 0, byPos: 0, byTool: 0, byNorm: 0, byTurn: 0, byImage: 0, unmatched: 0 }; + return { kind: "resend", missing: 0, claims: 0, byPos: 0, byTool: 0, byNorm: 0, byTurn: 0, byImage: 0, reaped: reapedCount, unmatched: 0 }; } if (opts.log !== undefined) { if (plan.claims.size > 0 && mode === "repair") { @@ -1167,6 +1255,7 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt byNorm: plan.byNorm, byTurn: plan.byTurn, byImage: plan.byImage, + reaped: reapedCount, unmatched: plan.unmatched.length, }; } diff --git a/src/session.ts b/src/session.ts index 0c7ff1f194..2040e4e8fa 100644 --- a/src/session.ts +++ b/src/session.ts @@ -1072,14 +1072,36 @@ export function applyCompactionArchive( } const { byRaw, byRef } = session.state.messageRefs; + pruneRefsToLiveIds(session, liveRawIds, byRaw, byRef); + + session.metadata.compactionBoundary = { + ...(boundary as Record), + pending: false, + archivedAt: Date.now(), + archivedBlocks: deactivated, + }; + markDirty(session); + log("info", `[${session.id}] native compaction boundary: archived ${deactivated.length} pre-compaction block(s)${deactivated.length > 0 ? ` (${deactivated.join(", ")})` : ""}; pruned ref maps to ${liveRawIds.size} live raw id(s)`); +} + +/** Prune the ref maps to `liveRawIds` in place (same maps, not new objects: + * session.state stays the object the kernel pipeline mutates). The kernel's + * ref cursor is highestUsedIndex+1, so pruning the top of the map would hand + * the freed numbers out again — the old high-water mark is re-pinned via + * REF_FLOOR_RAW_ID (refs are never reused within a session, Kernel + * Contract). Shared by the announced archive path (#395) and the zombie + * reap (#2695). */ +function pruneRefsToLiveIds( + session: Session, + liveRawIds: Set, + byRaw: Record, + byRef: Record, +): void { const highestBefore = highestUsedIndex(session.state.messageRefs); const prunedByRaw: Record = {}; for (const [rawId, ref] of Object.entries(byRaw)) { if (liveRawIds.has(rawId)) prunedByRaw[rawId] = ref; } - // The kernel's ref cursor is highestUsedIndex+1, so pruning the top of - // the map would hand the freed numbers out again — pin the old high-water - // mark (refs are never reused within a session). if (highestBefore > highestUsedIndex({ byRaw: prunedByRaw, byRef: {} })) prunedByRaw[REF_FLOOR_RAW_ID] = indexToRef(highestBefore); const prunedByRef: Record = {}; for (const [ref, rawId] of Object.entries(byRef)) { @@ -1087,15 +1109,43 @@ export function applyCompactionArchive( } session.state.messageRefs.byRaw = prunedByRaw; session.state.messageRefs.byRef = prunedByRef; +} - session.metadata.compactionBoundary = { - ...(boundary as Record), - pending: false, - archivedAt: Date.now(), - archivedBlocks: deactivated, - }; +/** #2695: remove fold blocks whose covered substrate the client demonstrably + * destroyed — a strict majority of each block's covered ids absent from the + * resent history for several consecutive passes (streak tracked in + * src/fold-reconcile.ts, METADATA_FOLD_COVERAGE.z). REMOVAL, not + * deactivation: syncBlocks re-activates every non-consumed, non-expanded + * block whose id set still intersects the wire (kernel/src/sync.ts sets + * active=true before the stillPresent check), and duplicate-content + * re-derivation keeps exactly that intersection alive forever — the zombie + * carrier that renders (and flaps) every turn (#2695). Removing the block + * from state.blocks is sticky: sync/prune/anchors all iterate state.blocks. + * blockContents are kept, so the derived-decompress fallback can still + * restore the summary text; the block is recorded in the #395 pre-compaction + * archive (direct decompress fails loudly with the archive reason); refs are + * pruned to live ids with the same high-water pin as applyCompactionArchive. */ +export function reapDestroyedSubstrate( + session: Session, + blockIds: ReadonlySet, + liveRawIds: Set, + log: (level: string, msg: string) => void, +): string[] { + if (blockIds.size === 0 || session.state.blocks.length === 0) return []; + const present = new Set(session.state.blocks.map((b) => b.blockId)); + const reaped = [...blockIds].filter((id) => present.has(id)); + if (reaped.length === 0) return []; + session.state.blocks = session.state.blocks.filter((b) => !blockIds.has(b.blockId)); + const archive = readPreCompactionArchive(session); + const at = Date.now(); + for (const id of reaped) { + archive[id] = { at, reason: "covered substrate destroyed by unannounced client history rewrite (#2695)" }; + } + session.metadata.preCompactionArchive = archive; + pruneRefsToLiveIds(session, liveRawIds, session.state.messageRefs.byRaw, session.state.messageRefs.byRef); markDirty(session); - log("info", `[${session.id}] native compaction boundary: archived ${deactivated.length} pre-compaction block(s)${deactivated.length > 0 ? ` (${deactivated.join(", ")})` : ""}; pruned ref maps to ${Object.keys(prunedByRaw).length} live raw id(s)`); + log("info", `[${session.id}] zombie fold reap: removed ${reaped.length} block(s) (${reaped.join(", ")}) from ACP state; blockContents retained, refs pruned to live ids (#2695)`); + return reaped; } // #1001: clients rewrite session history SILENTLY mid-session (opencode native diff --git a/tests/cache-friendly-plugin.test.ts b/tests/cache-friendly-plugin.test.ts index ff6a77231e..37d9d70250 100644 --- a/tests/cache-friendly-plugin.test.ts +++ b/tests/cache-friendly-plugin.test.ts @@ -44,7 +44,20 @@ // P5 marker/tag hygiene: every content text unit carries exactly one // well-formed head render tag; the marker→ref map is constant across // ALL turns (ids are never reused); refs unique per turn; OPEN/CLOSE -// balanced; the system layer is byte-stable across all turns. +// balanced; the system layer is byte-stable across all turns. At a +// SANCTIONED native-compaction boundary the era splits: refs re-seeded +// onto the compacted view belong to a new era and the old-era map does +// not constrain them (eraBreak option) — within each era the map stays +// constant; +// P6 dsh native-compaction rebase (#2596/#2658, all four wires): after a +// live fold, the host replaces its history with [checkpoint framing, +// retained tail] (the dsh compaction landing — the #2596 cache-loss +// lane). The dual-signal gate must rebase in the SAME turn: the +// forwarded body carries exactly the compacted view (framing + tail +// markers), the dead fold's summary/pair artifacts must NOT re-enter +// the wire, and every later turn is APPEND-stable from the compacted +// base — a missing lane implementation (the #2658 regression class: +// rebase shipped on only 2 of 4 wires) fails here per wire. // // Scenario (main, all wires): t1=[q1,a1,q2,a2], t2=+q3,a3, t3=+q4,a4, // foldA(m00001..m00002)+pairA@t4, t5=+q5,a5, foldB(m00003..m00005)+pairB@t6, @@ -68,6 +81,8 @@ import type { ProxyOptions } from "../src/config.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; import { _resetPluginStateForTest } from "../src/plugin.ts"; +import { getSession } from "../src/session.ts"; +import { DSH_CHECKPOINT_OPEN_TAG, DSH_CHECKPOINT_PREAMBLE_PREFIX } from "../src/server/dsh-compaction-guard.ts"; import { rmrf } from "./tmp-rm.ts"; const LT = "\x3c"; @@ -256,7 +271,7 @@ function keyOf(u: Unit): string { if (u.k === "text") { if (u.x.includes(SUM_A)) return "SA"; if (u.x.includes(SUM_B)) return "SB"; - const m = u.x.match(/\b([qa][1-8])-marker\b/); + const m = u.x.match(/\b([qa][0-9]+)-marker\b/); if (m) return m[1]!; return "TXT:" + u.x.slice(0, 24); } @@ -307,8 +322,14 @@ function assertSlotsTail(c: Canon, label: string): void { assert.ok(kinds === "" || /(?:chain|nudge|imgnote|retrnote)(?:,(?:chain|nudge|imgnote|retrnote))*$/.test(kinds), `${label}: unexpected slot kinds ${kinds}`); } -function assertTagPolicy(cans: Canon[], label: string): void { +function assertTagPolicy(cans: Canon[], label: string, eraBreak?: number): void { + // eraBreak: index of the turn whose request is the SANCTIONED + // native-compaction boundary. Refs before/after belong to different + // eras (the rebase re-seeds the ref space onto the compacted view), so + // the marker→ref map is pinned constant WITHIN each era only — + // cross-era re-issue is sanctioned, never a bug. const seen = new Map(); + const seenNext = new Map(); for (let t = 0; t < cans.length; t++) { const c = cans[t]!; const occ = c.raw.split(OPEN_TAG).length - 1; @@ -326,12 +347,13 @@ function assertTagPolicy(cans: Canon[], label: string): void { assert.ok(m, `${label} t${t + 1}: content unit missing well-formed head tag: ${JSON.stringify(u.x.slice(0, 60))}`); const ref = m[2]!; refs.push(ref); - const km = u.x.match(/\b([qa][1-8])-marker\b/); + const km = u.x.match(/\b([qa][0-9]+)-marker\b/); if (km) { const marker = km[1]!; - const prevRef = seen.get(marker); + const active = eraBreak === undefined || t < eraBreak ? seen : seenNext; + const prevRef = active.get(marker); if (prevRef) assert.equal(ref, prevRef, `${label} t${t + 1}: ref reuse — ${marker} was ${prevRef}, now ${ref}`); - else seen.set(marker, ref); + else active.set(marker, ref); } } assert.equal(new Set(refs).size, refs.length, `${label} t${t + 1}: duplicate refs in one turn`); @@ -422,11 +444,27 @@ const adapters: Record { +async function driveWire(wire: Wire, steps: Step[], opts?: DriveOpts): Promise { const adapter = adapters[wire]; - const conv = `cfp-${wire}-${Date.now().toString(36)}`; + const conv = opts?.conv ?? `cfp-${wire}-${Date.now().toString(36)}`; const captured: string[] = []; const upstream = http.createServer((req, res) => { @@ -469,7 +507,11 @@ async function driveWire(wire: Wire, steps: Step[]): Promise { const hist: CItem[] = []; try { for (const step of steps) { - for (const it of step.send) hist.push(it); + if (step.compact) { + hist.length = 0; + hist.push(...step.compact); + } + for (const it of step.send ?? []) hist.push(it); if (step.fold) { const r = await fetch(`http://127.0.0.1:${proxyPort}/__bili/plugin/tool`, { method: "POST", headers: { "content-type": "application/json" }, @@ -489,7 +531,7 @@ async function driveWire(wire: Wire, steps: Step[]): Promise { } const resp = await fetch(base, { method: "POST", - headers: { "content-type": "application/json", "x-bili-plugin": "pi-plugin/0.0.1", "x-bili-plugin-conversation": conv }, + headers: { "content-type": "application/json", "x-bili-plugin": opts?.pluginAgent ?? "pi-plugin/0.0.1", "x-bili-plugin-conversation": conv }, body: JSON.stringify(adapter.buildBody(hist)), }); if (resp.status !== 200) { @@ -734,3 +776,75 @@ testCompat(`cache-friendly plugin matrix: anthropic — 3rd fold prunes oldest p // once the oldest anchor re-carries, later turns are append-stable assertPrefix(cans[5]!.core, cans[6]!.core, L(6), ["q8", "a8"]); }, { timeout: 120_000 }); + +// P6 (#2596/#2658): the dsh native-compaction rebase lane, per wire. After a +// live fold the dsh host replaces its ENTIRE history with [checkpoint +// framing, retained tail] — exactly the landing that, when a lane misses the +// rebase (#2658 shipped it on openai/responses only), left the ACP state +// permanently unrebased and the prefix cache dead (#2596 main complaint). +// Byte-level contract, all four wires: +// - the dual-signal gate rebases in the SAME turn (state: boundary +// recorded, no active block survives); +// - the rebase-turn body carries exactly the compacted view (framing + the +// retained tail markers) and NONE of the dead fold's artifacts (no +// summary carrier, no pair quotes, no compress pair id/topic); +// - every later turn is APPEND-stable from the compacted base (the cache +// recovers instead of flapping) — the marker→ref map splits into eras at +// the sanctioned boundary and stays constant within each era. +for (const wire of ["anthropic", "openai", "responses", "google"] as Wire[]) { + testCompat(`cache-friendly plugin matrix: ${wire} — dsh native-compaction rebase keeps the wire clean and append-stable (#2596/#2658)`, async () => { + const NATIVE_SUM = "Native-compaction fold summary covering m00001..m00009 nuance"; + const NATIVE_FOLD: FoldSpec = { startId: "m00001", endId: "m00009", topic: "CFP-TOPIC-NATIVE", summary: NATIVE_SUM }; + const checkpoint: CItem = { + role: "user", + text: `${DSH_CHECKPOINT_PREAMBLE_PREFIX} of the conversation so far, as context for continuing. The work established the harness, drove one fold, and then dsh compacted natively. Continue the task directly from the messages that follow, without acknowledging this checkpoint.\n\n${DSH_CHECKPOINT_OPEN_TAG}\n## Primary Request and Intent\n- drive a fold, then replay the compacted view`, + }; + const steps: Step[] = [ + { send: [T(1), A(1), T(2), A(2), T(3), A(3)] }, + { send: [T(4), A(4), T(5), A(5), T(6), A(6)] }, + { send: [T(7), A(7)] }, + { send: [], fold: NATIVE_FOLD, pairId: "nc_pair" }, + { compact: [checkpoint, T(7), A(7)] }, + { send: [T(8), A(8)] }, + { send: [T(9), A(9)] }, + ]; + const conv = `cfp-native-${wire}`; + const cans = await driveWire(wire, steps, { pluginAgent: "dsh", conv }); + assert.equal(cans.length, 7, "expected 7 outbound bodies"); + const L = (i: number) => `cfp-native/${wire} t${i + 1}`; + + // The rebase landed in the SAME turn: boundary recorded, no active + // fold survives, refs re-seeded onto the compacted view. + const s = getSession(conv)!; + assert.ok(s, "session exists under the conversation key"); + const boundary = s.metadata["nativeCompactionBoundary"] as { pendingRebase?: boolean } | undefined; + assert.ok(boundary, "native-compaction boundary recorded"); + assert.equal(boundary?.pendingRebase, false, "rebase consumed within the rebase turn"); + assert.equal((s.state.blocks ?? []).filter((b) => b.active).length, 0, "no active fold survives the rebase"); + + for (let i = 0; i < cans.length; i++) { + assertSlotsTail(cans[i]!, L(i)); + assert.equal(countOcc(cans[i]!.raw, "acp_loop_"), 0, `${L(i)}: acp_loop_ artifact leaked`); + } + // eraBreak = 4: the compacted replay (t5) opens era B; refs re-issued + // across the boundary are sanctioned, within each era the map is law. + assertTagPolicy(cans, `cfp-native/${wire}`, 4); + + // t4 (fold turn): pair rides inbound, anchor stripped (#1567) + assert.equal(countOcc(cans[3]!.raw, SUM_MARKER), 0, `${L(3)}: carrier stripped while the pair rides`); + assert.equal(countOcc(cans[3]!.raw, NATIVE_SUM), 2, `${L(3)}: pair quotes only`); + + // t5 (rebase turn): exactly the compacted view on the wire + const rebase = cans[4]!.raw; + assert.ok(rebase.includes(DSH_CHECKPOINT_OPEN_TAG), `${L(4)}: checkpoint framing forwarded upstream`); + assert.ok(rebase.includes("q7-marker") && rebase.includes("a7-marker"), `${L(4)}: retained tail forwarded upstream`); + assert.equal(countOcc(rebase, SUM_MARKER), 0, `${L(4)}: no summary carrier may linger after the rebase`); + assert.equal(countOcc(rebase, NATIVE_SUM), 0, `${L(4)}: the dead fold's summary must not re-enter the wire`); + const pairIdOnWire = wire === "google" ? "CFP-TOPIC-NATIVE" : '"nc_pair"'; + assert.ok(!rebase.includes(pairIdOnWire), `${L(4)}: the dead fold's compress pair must be gone with the compacted history`); + + // t6/t7: growth resumes APPEND-stable from the compacted base + assertPrefix(cans[4]!.core, cans[5]!.core, L(5), ["q8", "a8"]); + assertPrefix(cans[5]!.core, cans[6]!.core, L(6), ["q9", "a9"]); + }, { timeout: 120_000 }); +} diff --git a/tests/cache-proof.test.ts b/tests/cache-proof.test.ts index 54f7b3abc3..ffa6093079 100644 --- a/tests/cache-proof.test.ts +++ b/tests/cache-proof.test.ts @@ -43,7 +43,15 @@ // chat wire): the re-sent body carries the continuation nudge // MERGED into the last user turn (appendTrailingUserText contract: // back-to-back user turns break provider replay grouping) — zero -// byte churn anywhere else, even when the proxy re-requests itself. +// byte churn anywhere else, even when the proxy re-requests itself; +// HOSTCOMPACT (decimation, chat wire, #2596 proxy-mode twin): the CLIENT +// replaces its ENTIRE history with a compacted view while folds +// are ACTIVE. Cache-safe contract: head/tail stable; the wire +// carries exactly the compacted core (shrunk); the destroyed +// folds' summary carriers must NOT linger (no orphan flapping); +// every later growth turn is append-stable from the compacted +// base; and the pipeline re-arms — a fresh fold eventually lands +// as a clean FOLD transition (self-heal, not a silent replay). // // Carrier contract (increment A): volatility is DECLARED per carrier and // located structurally instead of positionally (no "last N elements may @@ -84,6 +92,8 @@ import { once } from "node:events"; import { defaultConfig } from "acp-kernel"; import { startServer } from "../src/server.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { getSession } from "../src/session.ts"; +import { conflictEventsOf } from "../src/conflict-watch.ts"; import type { ProxyOptions } from "../src/config.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; import { DEGENERATE_RETRY_NUDGE } from "../src/degenerate-retry.ts"; @@ -1066,3 +1076,243 @@ test("cache proof (degenerate refetch, chat wire): re-request is a pure tail app rmrf(tmp); } }); + +/** HOSTCOMPACT (decimation, chat wire — #2596 proxy-mode twin): a plain + * client (no plugin headers) replaces its ENTIRE history with a compacted + * view while folds are active. Byte-level contract: + * - decimation turn: head/tail bytes stable, core carries exactly the + * compacted view (shrunk), and NONE of the destroyed folds' summary + * carriers linger on the wire (an orphan carrier would flap the prefix + * cache on every later turn — the #2596 complaint, proxy-mode shape); + * - every post-decimation growth turn is a clean GROWTH pair (append- + * stable from the compacted base — the cache recovers); + * - the pipeline re-arms: once the compacted history regrows past the + * threshold, a fresh fold lands as a clean FOLD-A/FOLD-B transition + * (self-heal, not a silent full replay). */ +test("cache proof (host compaction, chat wire): decimated replay drops orphan carriers and re-arms cleanly", { timeout: 180_000 }, async () => { + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "proof-decim-")); + const prevXdg = process.env.XDG_STATE_HOME; + process.env.XDG_STATE_HOME = tmp; + delete process.env.ACP_DUMP_BODY; + const bodies: string[] = []; + const state: JudgeState = { wire: "chat", bodies, urls: [], turn: 0, destroyOnNext: false, tagOnlyNext: false, suppressTrigger: false }; + // minTurns=8 (not MIN_FOLD_T=11): this scenario has no scripted pre-fold + // events to protect, and the regrowth phase needs the seed folds to land + // early enough that decimation provably destroys ACTIVE folds. + const trigger = makeProofTrigger(THRESHOLD, 8); + const HOST_COMPACT = "HOST-COMPACTION: earlier work covered modules 0-9; harness built, folds landed."; + let upstream: http.Server | undefined; + let proxy: http.Server | undefined; + try { + upstream = startJudgeUpstream(state, trigger); + upstream.listen(0, "127.0.0.1"); + await listen(upstream); + const upstreamPort = (upstream.address() as { port: number }).port; + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + // Not proofProxyOptions: this scenario needs the aggressive nudge + // cadence (nudgeGrowthTokens 500, as the plugin suite drives) so the + // decimated body regains enough refs fast for the trigger's + // refs>=12 gate — the re-arm must happen within the scripted turns. + proxy = await startServer({ + ...proofProxyOptions(upstreamPort, 200_000, true), + compress: { injectTool: true, injectNudge: true, nudgeGrowthTokens: 500 }, + }); + await listen(proxy); + const proxyPort = (proxy.address() as { port: number }).port; + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/chat/completions`; + const hist: Item[] = [{ role: "system", content: "You are a coding agent operating in a sandbox." }]; + const send = async (t: number, suppress: boolean): Promise => { + state.turn = t; + state.suppressTrigger = suppress; + const res = await fetch(url, { method: "POST", headers: { "content-type": "application/json", "x-acp-session": "proof-decim" }, body: JSON.stringify({ model: MODEL_A, stream: true, messages: [...hist] }) }); + if (!res.ok) throw new Error(`decim turn ${t}: HTTP ${res.status}: ${(await res.text()).slice(0, 300)}`); + const reply = extractReply("chat", await res.text()); + if (reply) hist.push({ role: "assistant", content: reply }); + state.suppressTrigger = false; + }; + // 1) seed: 12 fat turns — at least one proxy-side fold lands + for (let t = 0; t < 12; t++) { + hist.push({ role: "user", content: `Turn ${t}: please analyze module ${t}. ` + FILLER(t, 6) }); + await send(t, false); + } + assert.ok(trigger.calls() >= 1, `seed must land >=1 fold (got ${trigger.calls()})`); + const seedFolds = trigger.calls(); + // 2) decimate: [system, compacted head, retained tail] — fatter + // compacted base (≈34KB) so the regrowth phase crosses the 64KB + // trigger threshold deterministically within the scripted turns + const retainedUser = `Turn 10 recap: please analyze module 10. ` + FILLER(10, 8); + // Unique retained bytes (NOT a byte-identical seed reply): a real + // host compaction rewrites its retained tail as a fresh summary, so + // no fold anchor can re-match it. This is what keeps the post- + // decimation wire strictly append-stable: stale blocks find no + // anchor, go through destroy escalation, and never render carriers. + // (Byte-identical retention re-anchors blocks onto the survivor and + // produces a few turns of bounded carrier healing — observed during + // development, noted in the PR body, deliberately not pinned here.) + const retainedAssistant = "Retained tail: final analysis of module 11 delivered, integration green. " + FILLER(11, 8); + hist.length = 0; + hist.push({ role: "system", content: "You are a coding agent operating in a sandbox." }); + hist.push({ role: "user", content: HOST_COMPACT + " " + FILLER(90, 8) }); + hist.push({ role: "user", content: retainedUser }); + hist.push({ role: "assistant", content: retainedAssistant }); + await send(12, true); + const decimIdx = bodies.findIndex((b) => b.includes("HOST-COMPACTION")); + assert.ok(decimIdx > 0, "decimation body captured"); + // 3) regrow: enough turns to deterministically cross the threshold + // and let the fresh fold's round-2 settle (FOLD-B needs a body + // AFTER the round-2) + for (let t = 13; t < 22; t++) { + hist.push({ role: "user", content: `Turn ${t}: please analyze module ${t}. ` + FILLER(t, 6) }); + await send(t, false); + } + if (process.env.PROOF_DUMP) { + const dir = path.join(process.env.PROOF_DUMP, "decim"); + fs.mkdirSync(dir, { recursive: true }); + bodies.forEach((b, idx) => fs.writeFileSync(path.join(dir, `${String(idx).padStart(3, "0")}.json`), b)); + } + assert.ok(trigger.calls() > seedFolds, `pipeline must re-arm after decimation (folds ${seedFolds} -> ${trigger.calls()})`); + + // ---- classify every pair ---- + const lines: string[] = []; + const field = "messages"; + const r2All = bodies.map((_, i) => i).filter((i) => isRound2Body("chat", JSON.parse(bodies[i]!) as Item)); + const r2s = new Set(r2All); + const prev = bodies[decimIdx - 1]!; + const cur = bodies[decimIdx]!; + const PB = Buffer.from(prev, "utf8"); + const CB = Buffer.from(cur, "utf8"); + const P = layoutOf(PB, field); + const C = layoutOf(CB, field); + assert.ok(headEq(PB, CB, P, C), `chat pair#${decimIdx}->#${decimIdx + 1}: head bytes mutated across host compaction`); + assert.ok(tailEq(PB, CB, P, C), `chat pair#${decimIdx}->#${decimIdx + 1}: post-array suffix mutated across host compaction`); + assert.ok(coreLen(CB, C) < coreLen(PB, P), `chat pair#${decimIdx}->#${decimIdx + 1}: decimated core must SHRINK (got ${coreLen(PB, P)} -> ${coreLen(CB, C)})`); + assert.ok(!cur.includes("Cache-proof fold summary"), `chat pair#${decimIdx}->#${decimIdx + 1}: destroyed folds' summary carriers lingered on the wire (orphan flapping, #2596)`); + assert.ok(cur.includes(HOST_COMPACT) && cur.includes("Turn 10 recap"), `chat pair#${decimIdx}->#${decimIdx + 1}: compacted view must be forwarded`); + assert.ok(!cur.includes("acp_loop_"), `chat pair#${decimIdx}->#${decimIdx + 1}: acp_loop_ artifact leaked`); + lines.push(`proof[chat] pair#${decimIdx}->#${decimIdx + 1} HOSTCOMPACT ok core=${coreLen(PB, P)}->${coreLen(CB, C)} carriers-dropped lcp=${lcpBytes(PB, CB)}/${CB.length} sha=${sha16(cur)}`); + // #2695 bounded healing window: an unannounced host compaction + // orphans fold blocks whose carriers may keep riding the wire until + // the zombie-reap streak (3 majority-absent passes) removes them. + // A zombie carrier is DEFINED here as a summary element whose exact + // text already appeared pre-decimation (the destroyed seed folds' + // carriers); the window closes at the LAST body still carrying one. + // After that: strictly append-stable growth, clean fold transitions, + // and the zombie never returns. Without the reap the zombie rides + // every later fold round-2 — the bound below is the regression pin. + const seedCarrierTexts = new Set(); + for (let i = 0; i < decimIdx; i++) { + const B = Buffer.from(bodies[i]!, "utf8"); + const L = layoutOf(B, field); + for (let j = 0; j < L.elems.length; j++) { + const t = elText(B, L, j); + if (t.includes(SUMMARY_MARKER)) seedCarrierTexts.add(t); + } + } + const bodyHasZombie = (i: number): boolean => { + const B = Buffer.from(bodies[i]!, "utf8"); + const L = layoutOf(B, field); + for (let j = 0; j < L.elems.length; j++) { + const t = elText(B, L, j); + if (seedCarrierTexts.has(t)) return true; + } + return false; + }; + const bodyHasCarrierText = (i: number, text: string): boolean => { + const B = Buffer.from(bodies[i]!, "utf8"); + const L = layoutOf(B, field); + for (let j = 0; j < L.elems.length; j++) { + if (elText(B, L, j) === text) return true; + } + return false; + }; + // Kernel root-fix regression pin (#2695): the flap signature was a + // zombie carrier toggling in/out across turns — new cluster + // extensions kept re-occupying the dead block's covered ids and + // resurrecting it every other pass. With remint dodging inactive + // blocks, carrier visibility after decimation is MONOTONE: once a + // seed carrier drops off the wire it never comes back. + for (const t of seedCarrierTexts) { + let seen = false; + let dropped = false; + for (let i = decimIdx + 1; i < bodies.length; i++) { + if (bodyHasCarrierText(i, t)) { + assert.ok( + !dropped, + `seed carrier toggled back on body #${i} after dropping — zombie flap (kernel root-fix, #2695)`, + ); + seen = true; + } else if (seen) { + dropped = true; + } + } + } + let lastDirty = -1; + for (let i = decimIdx; i < bodies.length; i++) { + if (i === decimIdx) continue; // decim body is clean by the pair assert above + if (bodyHasZombie(i)) lastDirty = i; + } + assert.ok(lastDirty > decimIdx, "scenario drifted: expected zombie carriers to ride at least one post-decimation body"); + const ZOMBIE_SETTLE_BOUND = 9; // bodies after decimIdx; measured with the reap active + assert.ok(lastDirty - decimIdx <= ZOMBIE_SETTLE_BOUND, `chat zombie flap not bounded: zombie carrier last seen on body #${lastDirty}, ${lastDirty - decimIdx} bodies after decimation #${decimIdx} (bound ${ZOMBIE_SETTLE_BOUND}, #2695)`); + + let postFold = false; + for (let i = 1; i < bodies.length; i++) { + if (i === decimIdx) continue; + const inWindow = i > decimIdx && i <= lastDirty; + if (inWindow) { + if (r2s.has(i)) { + // Settling fold: the rearm round-2 may still carry zombie + // carriers from the destroyed seed folds (the reap lands + // after this body is forwarded). The rearm fold itself + // must still work: carrier present, core shrunk, and no + // NEW zombie spawned beyond what round-1 already had. + const PB = Buffer.from(bodies[i - 1]!, "utf8"); + const RB = Buffer.from(bodies[i]!, "utf8"); + const P = layoutOf(PB, field); + const R = layoutOf(RB, field); + assert.ok(firstSummaryEl(RB, R) >= 0, `chat fold@req#${i}: rearm round-2 lacks its summary carrier (no self-heal)`); + assert.ok(countSummaryEls(RB, R) <= countSummaryEls(PB, P) + 1, `chat fold@req#${i}: settling round-2 spawned a NEW zombie carrier (#2695)`); + assert.ok(coreLen(RB, R) < coreLen(PB, P), `chat fold@req#${i}: rearm round-2 must shrink the core`); + lines.push(`proof[chat] pair#${i - 1}->#${i} SETTLING-FOLD ok summaries=${countSummaryEls(PB, P)}->${countSummaryEls(RB, R)} (zombie ride bounded, #2695)`); + postFold = true; + } else { + lines.push(`proof[chat] pair#${i}->#${i + 1} SETTLING (bounded zombie churn, #2695)`); + } + continue; + } + if (r2s.has(i)) { + checkFoldPre("chat", bodies, field, i, lines); + if (i + 1 < bodies.length && i + 1 !== decimIdx) checkFoldNext("chat", bodies, field, i, lines); + if (i > decimIdx) postFold = true; + } else if (r2s.has(i - 1)) { + continue; // classified by checkFoldNext above + } else { + checkGrowth("chat", bodies, field, i, lines); + } + } + assert.ok(postFold, "post-decimation fold must occur (self-heal)"); + + // Direct teeth on the fix (#2695): the reap must have fired, archived + // the orphaned block, and KEPT its content for derived decompress. + const session = getSession("proof-decim"); + const reaps = conflictEventsOf(session).filter((e) => e.kind === "orphan-reap"); + assert.ok(reaps.length > 0, "orphan-reap conflict event recorded (#2695)"); + const reapedIds = reaps.flatMap((e) => e.detail.match(/\[([^\]]+)\]/)?.[1]?.split(", ") ?? []); + assert.ok(reapedIds.length > 0, `orphan-reap detail names the reaped blocks (got: ${reaps[0]?.detail})`); + for (const id of reapedIds) { + assert.ok(session.blockContents.has(id), `reaped block ${id} content retained for decompress (#395 semantics)`); + const coverage = session.metadata.foldCoverageByBlock as Record | undefined; + assert.ok(!coverage || coverage[id] === undefined, `reaped block ${id} stripped from foldCoverageByBlock`); + } + lines.push(`proof[chat] REAP ok blocks=${reapedIds.join(",")} content-retained coverage-stripped`); + for (const l of lines) console.log(l); + console.log(`proof[chat-decim] VERDICT bodies=${bodies.length} decimIdx=${decimIdx} folds=${trigger.calls()} unexplainedDivergences=0`); + } finally { + await closeServer(proxy); + await closeServer(upstream); + if (prevXdg === undefined) delete process.env.XDG_STATE_HOME; + else process.env.XDG_STATE_HOME = prevXdg; + rmrf(tmp); + } +}); diff --git a/tests/fold-reconcile.test.ts b/tests/fold-reconcile.test.ts index ae046a9d2f..6fc570efba 100644 --- a/tests/fold-reconcile.test.ts +++ b/tests/fold-reconcile.test.ts @@ -19,6 +19,7 @@ import { import { defaultCountTokens } from "acp-kernel"; import type { CoreMessage } from "acp-kernel"; import type { Session } from "../src/session.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; function msg(id: string, role: string, text: string, extra?: Partial): CoreMessage { return { id, role, contentType: "text", text, ...extra } as CoreMessage; @@ -405,9 +406,14 @@ describe("reconcileFoldCoverage drift escalation (#2193)", () => { describe("reconcileFoldCoverage coverage evidence + side-request guard (#2202)", () => { type LogLine = { level: string; msg: string }; + // #2695: the majority-loss streak reaps blocks on the third consecutive + // loss pass — reapDestroyedSubstrate calls markDirty, so a disabled store + // must be injected before any mock session flows through it. + _setStoreForTest(new SessionStore({ enabled: false })); function blockSession(blockId: string, ids: string[]): Session { return { - state: { blocks: [{ active: true, blockId, effectiveMessageIds: ids }] }, + id: "s-cov", + state: { blocks: [{ active: true, blockId, effectiveMessageIds: ids }], messageRefs: { byRaw: {}, byRef: {} } }, metadata: {}, } as unknown as Session; } @@ -429,7 +435,7 @@ describe("reconcileFoldCoverage coverage evidence + side-request guard (#2202)", reconcileFoldCoverage(session, first, makeOpts()); let cov = covOf(session)["blk1"]; assert.ok(cov, "record written on the first qualifying pass"); - assert.deepEqual(cov, { p: 5, r: 0, t: 5, e: 1 }); + assert.deepEqual(cov, { p: 5, r: 0, t: 5, e: 1, z: 0 }); // Pass 2: x2 churns (claimable via normalized identity), x3 is genuinely // edited (unmatchable), the rest ride along verbatim. const second = [ @@ -446,11 +452,13 @@ describe("reconcileFoldCoverage coverage evidence + side-request guard (#2202)", assert.equal(cov.p, 3, "x1/x4/x5 present verbatim"); assert.equal(cov.r, 1, "x2 reclaimed through a claim"); assert.equal(cov.e, 1); - // Pass 3: total loss — nothing covered rides the wire anymore. + // Pass 3: total loss — nothing covered rides the wire anymore. The + // majority-loss zombie streak (#2695) starts counting: z=1 this pass + // (pass 2 was fully covered, z reset to 0). reconcileFoldCoverage(session, filler("f3"), makeOpts()); cov = covOf(session)["blk1"]; assert.ok(cov); - assert.deepEqual(cov, { p: 0, r: 0, t: 5, e: 1 }); + assert.deepEqual(cov, { p: 0, r: 0, t: 5, e: 1, z: 1 }); }); test("never-present class stays unverifiable (structural absence keeps status quo)", () => { @@ -486,9 +494,18 @@ describe("reconcileFoldCoverage coverage evidence + side-request guard (#2202)", assert.equal(JSON.stringify(session.metadata[METADATA_FOLD_COVERAGE]), covBefore, "coverage records untouched"); assert.equal(JSON.stringify(session.metadata.foldAnchors), anchorsBefore, "anchors untouched"); assert.equal(JSON.stringify(session.metadata.foldAnchorOrder), orderBefore, "backbone untouched"); - // The next conversation-sized total-loss pass escalates on its own merit. - reconcileFoldCoverage(session, filler("h3"), opts); + // The next conversation-sized total-loss pass escalates on its own merit + // AND completes the #2695 zombie streak (z was 2 before the side pass; + // h3 makes it 3): the block is reaped — removed from state.blocks, the + // loss recorded as a conflict event, blockContents semantics preserved + // by the reap helper (kept, so derived decompress still works). + const h3 = reconcileFoldCoverage(session, filler("h3"), opts); assert.equal(logs.filter((l) => l.level === "error").length, 1); + assert.equal(h3.reaped, 1, "third consecutive majority-loss pass reaps the zombie block"); + assert.equal(session.state.blocks.length, 0, "reaped block removed from state.blocks"); + assert.ok(!covOf(session)["blk3"], "coverage record for the reaped block dropped"); + const conflict = (session.metadata.conflictEvents as Array<{ kind: string }>)[0]; + assert.equal(conflict?.kind, "orphan-reap"); }); test("short passes do not skew the next pass's alignment", () => { @@ -658,7 +675,7 @@ describe("reconcileFoldCoverage anchor cap boundary (#2334)", () => { const snapshot = JSON.stringify(session.metadata); resetNormalizedIdentityWork(); const second = reconcileFoldCoverage(session, msgs, opts); - assert.deepEqual(second, { kind: "resend", missing: 0, claims: 0, byPos: 0, byTool: 0, byNorm: 0, byTurn: 0, byImage: 0, unmatched: 0 }); + assert.deepEqual(second, { kind: "resend", missing: 0, claims: 0, byPos: 0, byTool: 0, byNorm: 0, byTurn: 0, byImage: 0, reaped: 0, unmatched: 0 }); assert.equal(normalizedIdentityWorkCount(), 0, "the overflow tail must not be normalized+hashed and dropped AGAIN every pass (#2334)"); assert.equal(JSON.stringify(session.metadata), snapshot, "steady-state resend leaves metadata byte-stable"); diff --git a/tests/issue2202-coverage-accrual.test.ts b/tests/issue2202-coverage-accrual.test.ts index 868515e8de..8509097bf4 100644 --- a/tests/issue2202-coverage-accrual.test.ts +++ b/tests/issue2202-coverage-accrual.test.ts @@ -73,12 +73,12 @@ test("verified loss freezes accrual; recovery resumes; legacy and never-present } assert.equal(led.folds[0].requestsAfter, 3, "full coverage books one request per sample"); // Re-read after each pass: reconcile REPLACES the record object. - const covOf = () => (session.metadata["foldCoverageByBlock"] as Record | undefined); - assert.deepEqual(covOf()?.b1, { p: 7, r: 0, t: 7, e: 1 }); + const covOf = () => (session.metadata["foldCoverageByBlock"] as Record | undefined); + assert.deepEqual(covOf()?.b1, { p: 7, r: 0, t: 7, e: 1, z: 0 }); // ---- shadowed: host drops the covered originals off the wire ---- reconcileFoldCoverage(session, shadowedPass(), { mode: "repair", sessionId: session.id }); - assert.deepEqual(covOf()?.b1, { p: 0, r: 0, t: 7, e: 1 }, "ever flag survives the loss"); + assert.deepEqual(covOf()?.b1, { p: 0, r: 0, t: 7, e: 1, z: 1 }, "ever flag survives the loss; zombie streak starts (#2695)"); for (let i = 0; i < 3; i++) { recordCacheSample(session, { at: T0 + 5000 + i * 1000, input: 10000, cached: 9000 }); }