Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/fold-reconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,19 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt
// either — otherwise "model switch + one honest user edit" (10 dead
// reasoning ids + 1 dead user id) would cry destruction three passes
// later while the fold is healthy.
const nonReasoningUnmatched = plan.unmatched.filter((id) => anchors[id]?.c !== "reasoning").length;
// #2903: also exclude LEGACY anchors — persisted by ≤0.1.191 builds before
// FoldAnchor.c existed (no content-type class). Their keys sit outside the
// current id space, so they read as missing every pass and can never be
// backfilled once off the wire (the #2732 backfill needs live bytes). The
// old `!== "reasoning"` classified that permanent bili self-debt as
// non-reasoning and latched foldDriftEscalated on healthy long sessions
// after 3 passes. Only a KNOWN non-reasoning class counts: a genuine
// client-side rewrite/deletion still carries a concrete `c`, so real loss
// still escalates — pure-legacy debt alone never does.
const nonReasoningUnmatched = plan.unmatched.filter((id) => {
const cls = anchors[id]?.c;
return cls !== undefined && cls !== "reasoning";
}).length;
const totalDrift = plan.claims.size === 0 && plan.unmatched.length > 0 && !reasoningOnlyDrop;
const unfoldedDrift = mode === "repair"
? Math.max(0, (plan.churnTokens ?? 0) - (plan.claimedChurnTokens ?? 0))
Expand Down
66 changes: 66 additions & 0 deletions tests/fold-drift.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import assert from "node:assert/strict";
import { createCore, createInitialState, defaultConfig, type CoreMessage } from "acp-kernel";
import type { Session } from "../src/session.ts";
import { foldCoverage } from "../src/session.ts";
import { reconcileFoldCoverage, METADATA_DRIFT_ESCALATED, type FoldAnchor } from "../src/fold-reconcile.ts";

function makeSession(): Session {
return {
Expand Down Expand Up @@ -103,3 +104,68 @@ test("server-side materialization check stays silent on stable resend and fires
}
}
});

// #2903: legacy foldAnchors persisted by ≤0.1.191 builds carry no content-type
// class (`c`), so once their id leaves the wire the reconciler can never match
// them again — they read as "missing" every pass and can never be backfilled
// (the #2732 backfill needs live bytes). That permanent bili self-debt must NOT
// count as non-reasoning loss in the destroy classifier, or a healthy long
// session latches foldDriftEscalated after 3 passes; but a genuine client-side
// rewrite/deletion still carries a concrete class and MUST keep escalating.
// Drives reconcileFoldCoverage directly over a synthetic pre-0.1.192 ledger.
function escalationFixture(legacyCount: number, realCount: number): { session: Session; resend: CoreMessage[] } {
const session = makeSession();
const coveredIds: string[] = [];
const anchors: Record<string, FoldAnchor> = {};
for (let i = 0; i < legacyCount; i++) {
const id = `cov_legacy_${i}`;
coveredIds.push(id);
anchors[id] = { n: `anc_legacy_${i}`, r: "user", b: 128 }; // ≤0.1.191 shape: no `c`
}
for (let i = 0; i < realCount; i++) {
const id = `cov_real_${i}`;
coveredIds.push(id);
anchors[id] = { n: `anc_real_${i}`, r: "user", c: "text", b: 128 }; // known non-reasoning
}
session.state.blocks = [{
blockId: "b0", runId: "r0", tier: 1, summary: "fold", directMessageIds: coveredIds,
effectiveMessageIds: coveredIds, directBlockIds: [], compressedTokens: 1000, createdAt: Date.now(),
survivedCount: 0, generation: "young", active: true,
}];
// Mirror the victim lane (dsh plugin): disables the proxy-only orphan-reap so
// the assertion isolates the drift-escalation path.
session.metadata = { pluginAgent: "dsh", foldAnchors: anchors, foldAnchorOrder: [...coveredIds] };
// Resend carrying none of the covered ids → every covered id is unmatched each
// pass and nothing reanchors (the fake `n` values never equal a live sha256).
const resend: CoreMessage[] = [];
for (let i = 0; i < 24; i++) resend.push({ id: `wire_${i}`, role: "user", contentType: "text", text: `fresh turn ${i} ${"y".repeat(64)}` });
return { session, resend };
}

function runEscalationPasses(session: Session, resend: CoreMessage[], passes: number): void {
for (let i = 0; i < passes; i++) reconcileFoldCoverage(session, resend, { mode: "repair" });
}

test("#2903: pure-legacy anchor debt never latches foldDriftEscalated", () => {
const { session, resend } = escalationFixture(20, 0);
runEscalationPasses(session, resend, 4);
// Prove the total-loss path actually ran (streak reached the escalate pass) —
// otherwise the not-escalated assertion would be vacuous.
assert.ok((session.metadata["foldDriftStreak"] as number) >= 3, "fixture must drive the total-loss streak to the escalate pass");
assert.notEqual(session.metadata[METADATA_DRIFT_ESCALATED], true,
"legacy (no-class) anchors are bili self-debt, not client destruction — must not escalate");
});

test("#2903: legacy debt + real missing still escalates (no wholesale silencing)", () => {
const { session, resend } = escalationFixture(40, 10);
runEscalationPasses(session, resend, 4);
assert.equal(session.metadata[METADATA_DRIFT_ESCALATED], true,
"a concrete-class missing message alongside legacy debt still counts toward the destroy threshold");
});

test("#2903: genuine client-side loss alone still escalates (detection intact)", () => {
const { session, resend } = escalationFixture(0, 10);
runEscalationPasses(session, resend, 4);
assert.equal(session.metadata[METADATA_DRIFT_ESCALATED], true,
"pure known-class missing ids (real rewrite/deletion) must still escalate");
});
Loading