Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
42e4a18
fix(preflight): continue the multi-round convergence walk past the do…
Oct 27, 2026
25f4949
test(golden): regenerate storage-render goldens for additive lastFold…
Oct 27, 2026
c5426d1
Merge remote-tracking branch 'origin/master' into pr4928-sync
Oct 29, 2026
a3a1f3e
Merge remote-tracking branch 'refs/remotes/origin/master' into pr4928…
Oct 30, 2026
06d4d26
Merge remote-tracking branch 'refs/remotes/origin/ci-fixes' into pr49…
Oct 30, 2026
2e7724a
Merge remote-tracking branch 'refs/remotes/origin/master' into pr4928…
Oct 31, 2026
0ef19bd
Merge remote-tracking branch 'refs/remotes/origin/master' into pr4928…
Oct 31, 2026
981d891
Merge origin/master into PR #4928 head — resolve fold-reconcile clean…
Nov 12, 2026
e74a506
Merge origin/master (f648217cb8) into PR #4928 head — resolve storage…
Nov 15, 2026
d479033
fix: key the #4920 fold-drift record on realUnmatched, not plan.unmat…
Nov 15, 2026
e738a01
Merge origin/master (6a641f3785) into PR #4928 head — resolve storage…
Nov 17, 2026
aa713af
Merge origin/master (548e396022) into PR #4928 head — resolve preflig…
Nov 18, 2026
3ae51e5
Merge origin/master (3f0c1d2110) into PR #4928 head — resolve storage…
Nov 18, 2026
cd8f5f2
Merge origin/master (a446a3c1c8) into PR #4928 head — resolve storage…
Nov 18, 2026
0cae5a0
fix: correct stale window number in the #4920 seed comment (20K -> 60…
Nov 18, 2026
d29d7c4
Merge origin/master (6453feeeb84) into PR #4928 head — resolve storag…
Nov 19, 2026
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
10 changes: 9 additions & 1 deletion src/acp-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { coveredRefSpan } from "./decompress-shared.js";
import { describeUncoveredRuns, shortTokenCount, uncoveredHistoryRuns } from "./uncovered.js";
import { withoutTurnSeparators } from "./turn-separator.js";
import { forkAdoptionSkippedOf } from "./fork-adoption.js";
import { preCompactionArchiveOf, statusInputBaseline, type Session } from "./session.js";
import { foldDriftOf, preCompactionArchiveOf, statusInputBaseline, type Session } from "./session.js";
import { compressBreakerDetail, compressLastFailureCause } from "./stream.js";
import { describeAdvisory, getAdvisoryState } from "./advisory.js";
import { getResolverWedgeState } from "./resolver-wedge.js";
Expand Down Expand Up @@ -500,6 +500,14 @@ export function handleAcpStatus(args: Record<string, unknown>, ctx: AcpStatusCtx
extra.push("");
extra.push(...formatConflictSection(cevents, Date.now(), conflictClientOf(ctx.session)));
}
// #4920: lost fold coverage must be visible where agents actually look —
// the [acp-drift] warn alone lives in logs users never open, and a silently
// unfolded session is exactly how the provider-switch incident went dead.
const drift = foldDriftOf(ctx.session);
if (drift) {
extra.push("");
extra.push(`FOLD DRIFT (session-level): ${drift.expected - drift.matched} covered message id(s) lost fold coverage since ${drift.at} (#4920) — ${drift.matched}/${drift.expected} covered id(s) retain fold coverage; the lost id(s)' folded content rides the wire raw again until coverage re-anchors; see the [acp-drift] lines in the bili log.`);
}
const adv = getAdvisoryState();
if (adv.active) {
// #1577: native/plugin lanes spawn the proxy with stdio→log file on an
Expand Down
14 changes: 13 additions & 1 deletion src/fold-reconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ import { defaultCountTokens, type CoreMessage } from "acp-kernel";
import { FORMS as THINKING_MERGE_FORMS } from "../kernel/src/wire/demoted-thinking.js";
import { simhashHex, hammingHex } from "./affinity-simhash.js";
import { reapDestroyedSubstrate, type Session } from "./session.js";
import { coveredRealHistoryIds, isResponsesTurnSeparatorId } from "./session.js";
import { clearFoldDrift, coveredRealHistoryIds, isResponsesTurnSeparatorId, noteFoldDrift } from "./session.js";
import { dshNativeCompactionWitness, recordConflict } from "./conflict-watch.js";
import { dshCarrierRawMarkers, oneShotCarrierPaths } from "./durable-message-guards.js";
import { log as loggerLog } from "./logger.js";
Expand Down Expand Up @@ -2523,6 +2523,9 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt
// so it reports like a plain resend (the drift-warn cascade below must not
// fire for carrier retirement / offload rewrites).
if (realUnmatched.length === 0 && plan.claims.size === 0) {
// #4920: fully aligned pass — drop any stale lost-coverage record so
// acp_status reflects the re-anchored reality, not a past episode.
clearFoldDrift(session);
return { kind: "resend", missing: 0, claims: 0, byPos: 0, byTool: 0, byTwin: 0, bySim: 0, byOrder: 0, byNorm: 0, byTurn: 0, byImage: 0, byShape: 0, reaped: reapedCount, unmatched: 0, expectedRetired: expectedSize };
}
if (opts.log !== undefined) {
Expand Down Expand Up @@ -2572,6 +2575,15 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt
`${tag}[fold-reconcile] resent history drifted: ${realUnmatched.length} covered id(s) missing with no anchor match${expectedNote} — mutation (content edit invalidates content-hash refs, fold silently lost) or client-side deletion/truncation (benign, message no longer on the wire)${plan.idRewriteSuspects > 0 ? `; ${plan.idRewriteSuspects} of them have an inbound twin with identical normalized content under a different toolCallId — consistent with the host rewriting authoritative tool-call ids across provider projections (host contract: keep tool-call ids byte-stable per conversation, #2396)` : ""} (#1921)${driftTok}`);
}
}
// #4920: surface the TRULY-lost coverage on the session so acp_status can
// show it. The set is realUnmatched, not plan.unmatched: claims were
// re-anchored onto churned bytes (their folds live on) and expected rebases
// (carrier retirement / offload rewrites) are not loss (#5142) — the same
// set the clean-pass early return above and the drift-warn cascade key on.
// Recording here (not in the lanes' post-reconcile snapshots) covers every
// verdict class, including mutation passes that deactivate the drifted
// blocks before the lane snapshot runs.
if (realUnmatched.length > 0) noteFoldDrift(session, { expected: covered.size, matched: covered.size - realUnmatched.length });
return {
kind: plan.claims.size > 0 ? (mode === "repair" ? "reanchored" : "unmatched") : "unmatched",
missing: plan.claims.size + realUnmatched.length,
Expand Down
34 changes: 31 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4093,8 +4093,31 @@ export async function preflightCompressIfNeeded(
// would block cannot acquire upstream evidence: use the normal fold
// path instead, without recording a persistent route refusal.
const coreTextBytes = estimateCoreMessagesUpperBytes(prepared.processedMessages);
if (coreTextBytes > Math.max(DOOMED_PROBE_ABSOLUTE_BYTES, limit * DOOMED_PROBE_BYTES_PER_TOKEN)) {
const byteFloorTokens = Math.ceil(coreTextBytes / DOOMED_PROBE_BYTES_PER_TOKEN);
const byteFloorTokens = Math.ceil(coreTextBytes / DOOMED_PROBE_BYTES_PER_TOKEN);
const overDoomBar = coreTextBytes > Math.max(DOOMED_PROBE_ABSOLUTE_BYTES, limit * DOOMED_PROBE_BYTES_PER_TOKEN);
// #4920: an over-bar RAW payload is not terminal while a multi-round
// convergence campaign is already in flight — this session HAS FOLD
// HISTORY (it has been compressed before) and compressible ranges still
// remain visible. The budget-exhaustion 502 promises "the ranges folded
// this turn persist — the next attempt starts from a smaller baseline"
// (#3918), and keeping that promise requires this gate to keep walking
// instead of refusing. As shipped, the gate priced the RAW inbound
// bytes instead of the post-fold payload, so the very request after a
// progress-continues 502 died here with a terminal 413 and the campaign
// could never converge (field incident: 80K-tok session → provider
// switch → 6M raw resend → one 31-round fold pass saved 2.5M tok → next
// attempt 413 → dead). The check is on fold HISTORY, not ACTIVE blocks:
// coverage drift deactivates the drifted blocks during prepare — before
// this gate runs — yet that is exactly the moment a re-fold on the new
// history ("a fresh fold starts a new epoch") is needed. Fresh
// sessions without any fold history keep the immediate 413
// (#2490/#2847) — there is no campaign to continue there. Falling out
// of this block enters the fold-first regime below; the post-fold doom
// check still guards every actual forward.
const multiRoundInFlight = overDoomBar
&& session.state.blocks.length > 0
&& (prepared.nudge?.compressibleRanges ?? []).length > 0;
if (overDoomBar && !multiRoundInFlight) {
log("error", `[${session.id}] preflight 413 core-text profile: ${describeCoreTextDuplication(prepared.processedMessages)}`);
const doomedMessage = `payload core text alone is ~${coreTextBytes} bytes — even at the densest tokenizer density observed on a live route (~${DOOMED_PROBE_BYTES_PER_TOKEN} bytes/token, #2122) that is ≥ ~${byteFloorTokens} tokens vs the model window ${limit} (model=${model}); no upstream can accept it, so it was NOT forwarded even for evidence. Remove or trim the oversized content (e.g. a giant paste or tool result) and retry.`;
// #2847: the rejection is mathematically deterministic, so the STATUS
Expand All @@ -4116,7 +4139,12 @@ export async function preflightCompressIfNeeded(
? measuredOutbound(prepared) : undefined;
const probeBlockedByEgress = probeMeasurement !== undefined
&& probeMeasurement.tokens > Math.max(declaredWindow, MIN_ASSUMED_WINDOW_TOKENS);
if (localRefusalHere) {
if (multiRoundInFlight) {
// #4920: fall out of the probe block into the fold-first regime below —
// do NOT forward the raw payload (that is the bet this bar exists to
// prevent), and do NOT refuse (the campaign continues).
log("warn", `[${session.id}] preflight trigger fired on estimate only (~${Math.round(textChannel)} text + ~${imageTokens} image vs window ${limit}) and the raw payload is past the doom bar (~${coreTextBytes} B ≈ ≥~${byteFloorTokens} tok; ${describeCoreTextDuplication(prepared.processedMessages)}) — continuing the multi-round convergence walk instead of refusing: active fold state plus compressible range(s) still visible, so folding can still close the gap (#4920); the post-fold doom check guards the actual forward`);
} else if (localRefusalHere) {
// #3846: bili already refused this scope locally — the estimate-only
// bet is spent. Fall through to fold/fail-fast instead of re-forwarding.
log("warn", `[${session.id}] preflight trigger fired on estimate only (~${Math.round(textChannel)} text + ~${imageTokens} image vs window ${limit}) but this origin/model was locally refused (413) at an equal or wider window — NOT re-forwarding for evidence; falling through to fold/fail-fast (#3846)`);
Expand Down
25 changes: 25 additions & 0 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2129,6 +2129,31 @@ export function foldCoverage(
return matched < coveredBefore.size ? { expected: coveredBefore.size, matched } : null;
}

// #4920: last observed fold-coverage drift, surfaced by acp_status — the
// [acp-drift] warn lands in logs users never open; the panel is the surface
// agents actually look at (same reasoning as the #1206 conflict section).
const METADATA_LAST_FOLD_DRIFT = "lastFoldDrift";

export function noteFoldDrift(session: Pick<Session, "metadata">, gap: { expected: number; matched: number }): void {
if (!(gap.expected > gap.matched)) return;
// Epoch ms like savedAt/lastSeen — canonicalize masks wall-clock numbers
// (__TS__) so storage digests stay stable across saves (#2671).
session.metadata[METADATA_LAST_FOLD_DRIFT] = { matched: gap.matched, expected: gap.expected, at: Date.now() };
}

export function foldDriftOf(session: Pick<Session, "metadata">): { matched: number; expected: number; at: string } | undefined {
const v = session.metadata?.[METADATA_LAST_FOLD_DRIFT];
if (!v || typeof v !== "object") return undefined;
const d = v as Record<string, unknown>;
if (typeof d.matched !== "number" || typeof d.expected !== "number" || typeof d.at !== "number") return undefined;
if (!(d.expected > d.matched)) return undefined;
return { matched: d.matched, expected: d.expected, at: new Date(d.at).toISOString() };
}

export function clearFoldDrift(session: Pick<Session, "metadata">): void {
delete session.metadata[METADATA_LAST_FOLD_DRIFT];
}

// #2627 turn-separator helpers moved to ./turn-separator.ts (zero-dependency leaf
// — the storage seam needs them without session.ts's built-acp-kernel import;
// the doc comments live there now). Re-exported so existing consumers keep the
Expand Down
6 changes: 3 additions & 3 deletions tests/golden/storage-render/anthropic.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@
"foldAt": 9,
"editedAt": 12,
"summaryFrom": 9,
"stateDigest": "ba0f75f6ddc882d00d5c456facd79166b62e3f2cacadd60744aa370d6b247105",
"stateDigestAligned": "66991d5cb8864f3ca029914aa9afc20cc8b9687cfd4391ffe200017c68057946",
"stateDigest": "38deb402fefa9ae49a43b8fb52f37d646748a6c8f5434dbce622412d97795a54",
"stateDigestAligned": "dd11594c62761a6771744a31284336b436b07836f46f95f9be0da9025e12b9dc",
"sectionDigests": {
"state": "37b26b4f6898afff",
"blockContents": "dc7d13d250c94bb4",
"stats": "7caf981f99159be9",
"metadata": "ba51746c6749d0cf"
"metadata": "2cbcbfa8576a2d38"
},
"stateKeyDigests": {
"blocks": "3a454b9a16e4f0b9",
Expand Down
6 changes: 3 additions & 3 deletions tests/golden/storage-render/chat.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@
"foldAt": 9,
"editedAt": 12,
"summaryFrom": 9,
"stateDigest": "2c0b36206756617ae1e32cf0d221ddd8bf7f3e06994b49d11578bd1a35c90f26",
"stateDigestAligned": "3011800706759958cfeab6520289f1ffe257cf4f7c3275d846c03a991f9ed6b8",
"stateDigest": "80bf6d82d64d9a34de9181566c61de350105ac229cea52fc34880f221e4fafb5",
"stateDigestAligned": "7c2a5fd0dfaa7885b1f24c5ea8a86fee27ca1c3a3adb68ac4df11b2a667dd1be",
"sectionDigests": {
"state": "90857fbf82001a2b",
"blockContents": "94a58f96d7c5cfc3",
"stats": "7b6162aef3873d31",
"metadata": "7218c1e3430c3d21"
"metadata": "560a86123869a725"
},
"stateKeyDigests": {
"blocks": "d9770feee91dafc6",
Expand Down
6 changes: 3 additions & 3 deletions tests/golden/storage-render/google.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@
"foldAt": 9,
"editedAt": 12,
"summaryFrom": 9,
"stateDigest": "c5eefef6525f180ab54a7ea57c4ddf004a568db0fc2d6d227ede31dc960706db",
"stateDigestAligned": "5217aa02331256a419837cd91105d3e3172f2ff38f0f3914c0e01128e221f012",
"stateDigest": "d78de456028d13a8707a0ec1a3e11df1cdaccbe6840fbcd4714de72abd094e9c",
"stateDigestAligned": "672aeb55d0d8c823609fe0414efa21df52bc3c033929d38bf3522d2a332733eb",
"sectionDigests": {
"state": "c5243ba17364d3ac",
"blockContents": "473264d9df8ba338",
"stats": "c90671e484dc5d37",
"metadata": "2980a8959aa0a39c"
"metadata": "326f64fe1cbdd176"
},
"stateKeyDigests": {
"blocks": "0cfd0b9c0fc53073",
Expand Down
6 changes: 3 additions & 3 deletions tests/golden/storage-render/responses.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@
"foldAt": 9,
"editedAt": 12,
"summaryFrom": 9,
"stateDigest": "ff092bc66e11c35b15ad98acf6206629115825b62168da2e5542e06297f6d47a",
"stateDigestAligned": "db5f08a7d57c14952c31571e45b4dbc6c2ed35fe8a97c58cda6fa854d411eb25",
"stateDigest": "707f30ffd74f390b045bce44a2839474c539d588fd9969fc092d4a5d54dcd246",
"stateDigestAligned": "91ac8b4d0232d94f2d445542e65552823cc5ebc56a2f391ffca0bac5d23988e8",
"sectionDigests": {
"state": "bee6e1ba0c6fba62",
"blockContents": "b6701268a0bdcd8a",
"stats": "1b56c993f03cd122",
"metadata": "77714417e484ba89"
"metadata": "9606d7ec2ce829a5"
},
"stateKeyDigests": {
"blocks": "34b0dc17f8c24595",
Expand Down
Loading
Loading