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
66 changes: 63 additions & 3 deletions src/fold-reconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ const METADATA_TRUNC_NOTED = "foldTruncationNoted";
* resent wire is a prefix-cut of the pre-rewind wire). Same benign class as
* head truncation; a distinct key so the log can name which end moved. */
const METADATA_TAIL_TRUNC_NOTED = "foldTailTruncationNoted";
/** #5715: one-per-episode info latch for the surviving-direct-ID-majority
* partial-loss verdict (cleared with the rest of the drift state on recovery,
* like the #3641/#3724 benign latches). */
const METADATA_MAJORITY_LOSS_NOTED = "foldMajorityLossNoted";
/** #3641: the wire order frozen at the moment the drift episode began — the
* baseline against which truncation-vs-replacement is decided. */
const METADATA_EPISODE_ORDER = "foldEpisodeOrder";
Expand Down Expand Up @@ -338,6 +342,7 @@ function resetFoldDriftState(session: Session): void {
delete md[METADATA_SHAPE_NOTED];
delete md[METADATA_TRUNC_NOTED];
delete md[METADATA_TAIL_TRUNC_NOTED];
delete md[METADATA_MAJORITY_LOSS_NOTED];
delete md[METADATA_EPISODE_ORDER];
// #5142: the expected-rebase RECORD itself (METADATA_EXPECTED_REBASE) is
// id-level truth and survives episodes on purpose; only its one-shot log
Expand Down Expand Up @@ -1697,8 +1702,11 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt
const blocks = (session.state?.blocks ?? []) as BlockLike[];
// Active coverage first (the anchor cap's survivor order, #2334), then the
// retained view-deactivated lineage re-fed into the claim ladder (#4050);
// see recoveryCandidateIds for what is excluded and why.
const covered = coveredIdsOf(blocks);
// see recoveryCandidateIds for what is excluded and why. activeCovered keeps
// the active-only caliber intact (a separate object, never mutated) so the
// #5715 surviving-majority test below can run on live substrate alone.
const activeCovered = coveredIdsOf(blocks);
const covered = new Set(activeCovered);
for (const id of recoveryCandidateIds(blocks)) covered.add(id);
if (covered.size === 0) {
resetFoldDriftState(session);
Expand Down Expand Up @@ -2166,6 +2174,39 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt
&& plan.nearDupResidual.length === 0
&& classCarryingResidual.every((id) => absentSet.has(id))
&& tailTruncated;
// #5715: a strict majority of hard-covered ids still PRESENT VERBATIM on
// the wire is the opposite of wholesale replacement — the fold substrate
// is alive. The verdict classifier above only sees claims==0 + unmatched>0
// and never counts the surviving direct matches, so a handful of interior
// edits/deletions (neither a head cut nor a tail rewind) read as total
// drift and, after FOLD_DRIFT_ESCALATE_PASSES, falsely latch epoch-confirm
// and pause compression on a healthy session. We deliberately do NOT relax
// totalDrift itself: the #3641 drift streak + frozen witness are what keep
// a genuine mid-history edit reading as honest loss (zombie accrual)
// rather than sanctioned truncation (#4050/#2695). The surviving majority
// only voids the TERMINAL verdict (below). The base is ACTIVE coverage only
// (the coveredIdsOf caliber), NOT the full `covered`: the #4050 view-
// deactivated lineage re-fed via recoveryCandidateIds is recoverable-but-
// gone, and its legitimate post-rewind/post-drift absence must never be able
// to dilute a healthy active majority below the bar (#5715 field report).
// Denominators match the per-block coverage (separators / expected rebases /
// reasoning cores excluded); the bar is >= MIN_RESYNC_RUN and > half.
// Genuine wholesale replacement has ~0 directly-present ACTIVE covered ids,
// so this can never mask a real collapse.
let coveredHard = 0;
let presentHard = 0;
for (const id of activeCovered) {
if (isResponsesTurnSeparatorId(id) || expected[id] !== undefined || anchors[id]?.c === "reasoning") continue;
coveredHard++;
if (byId.has(id)) presentHard++;
}
const hardDirectMajority = presentHard >= MIN_RESYNC_RUN && 2 * presentHard > coveredHard;
// #5715: a strict direct-ID majority proves the fold substrate is alive on
// the wire — any pending epoch-confirm latch is stale (the histories
// realigned) and clears immediately, whether or not this pass still carries
// residual loss (a partial restore keeps totalDrift true, so the normal
// !totalDrift reset below would never fire for it).
if (hardDirectMajority && epochConfirmPending(session)) resetFoldDriftState(session);
const totalDrift = plan.claims.size === 0 && realUnmatched.length > 0 && !reasoningOnlyDrop && !markerChurnExplained
&& !shapeExplained && !truncationExplained && !tailTruncationExplained;
// Classify before reaping: the epoch-confirm gate promises to retain the
Expand Down Expand Up @@ -2362,7 +2403,11 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt
}
session.metadata[METADATA_DRIFT_STREAK] = streak;
if (prevStreak === 0) session.metadata[METADATA_DRIFT_SINCE] = Date.now();
if (streak >= FOLD_DRIFT_ESCALATE_PASSES && nonReasoningUnmatched >= FOLD_DRIFT_ESCALATE_MIN_UNMATCHED
// #5715: a strict direct-ID majority means the fold substrate is alive
// on the wire — this is partial loss, not wholesale replacement, so the
// terminal epoch-confirm verdict (and its compression pause) must not
// fire even though the #2193 streak above has accrued.
if (!hardDirectMajority && streak >= FOLD_DRIFT_ESCALATE_PASSES && nonReasoningUnmatched >= FOLD_DRIFT_ESCALATE_MIN_UNMATCHED
&& session.metadata[METADATA_DRIFT_ESCALATED] !== true) {
session.metadata[METADATA_DRIFT_ESCALATED] = true;
// #2709: take a ledger action at the destruction point (not just log) — attribute
Expand Down Expand Up @@ -2417,6 +2462,21 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt
opts.log("info",
`${tag}[fold-reconcile] ${realUnmatched.length} covered id(s) missing with no anchor match${expectedNote}, but this pass stripped ${opts.markerStripped} raw marker artifact(s) — covered ids minted from marker-laden bytes churn once the strip engine cleans the resent history; benign on stateless resend (content still on the wire, re-folds re-cover), tolerated (#2815/#2789)`);
}
} else if (hardDirectMajority && nonReasoningUnmatched >= FOLD_DRIFT_ESCALATE_MIN_UNMATCHED
&& !shapeExplained && !truncationExplained && !tailTruncationExplained) {
// #5715: a surviving direct-ID majority AT THE MASS BAR is PARTIAL
// loss, not the "fold silently lost" the generic warn below names —
// report it once per episode at info level (acceptance, not repair).
// Sub-mass-bar single edits keep the honest per-pass warn below. The
// #3641 verdict above already kept this from latching epoch-confirm.
// Already-explained episodes (shape churn / head / tail truncation)
// own their own one-per-episode note — reporting them here too would
// print a second line mislabeling a head/tail cut as "interior"
// loss, so they fall through to the original per-pass warn.
if (session.metadata[METADATA_MAJORITY_LOSS_NOTED] !== true) {
session.metadata[METADATA_MAJORITY_LOSS_NOTED] = true;
opts.log("info", `${tag}[fold-reconcile] ${realUnmatched.length} covered id(s) missing but ${presentHard}/${coveredHard} hard-covered id(s) still ride verbatim on the wire — partial interior loss, fold substrate healthy, no epoch confirmation (#5715)`);
}
} else if (session.metadata[METADATA_DRIFT_ESCALATED] !== true) {
// #2297: once the episode escalated, the single error line IS the
// report — repeating this warn per pass contradicts the #2193
Expand Down
Loading
Loading