From 42e4a1886c523b6dc3bd4bcd06c92cf78725d1eb Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 27 Oct 2026 14:34:10 +0800 Subject: [PATCH 1/4] fix(preflight): continue the multi-round convergence walk past the doom bar when fold state exists (#4920) A preflight pass that folds but still misses the window exits 502 with the #3918 promise 'the N ranges folded this turn persist - the next attempt starts from a smaller baseline'. The next request broke that promise: the 502 path's estimate writeback (lastInputTokens = post-fold estimate, source 'estimate') arms the evidence-probe regime, where the doomed-probe gate prices RAW inbound bytes against max(2MB, window*7.5). A raw history that is over-bar by bytes but post-fold under-window in tokens died at a terminal non-retryable 413 plus a route-scoped local refusal - before any fold ran. A 7.7x-window payload could never run its second convergence round; the session stayed stuck at 3-6M tokens forever. Fix: when the payload is over-bar AND the session has fold state with viable compressible ranges, fall through to the fold-first regime instead of refusing - the multi-round walk resumes exactly as the 502 promised. Fresh sessions (no blocks) keep the immediate 413 (#2490/#2847 preserved); the post-fold doom check on actual forwards is untouched. Also makes lost fold coverage visible: reconcileFoldCoverage records truly-lost (unmatched) coverage on the session and acp_status renders it as a FOLD DRIFT section; clean passes clear it. The [acp-drift] log lines are unchanged. Tests: tests/issue4920-multiround-doombar.test.ts (e2e convergence walk + fresh-session negative + unit round-trip). --- src/acp-status.ts | 10 +- src/fold-reconcile.ts | 11 +- src/server.ts | 34 ++- src/session.ts | 23 ++ tests/issue4920-multiround-doombar.test.ts | 285 +++++++++++++++++++++ 5 files changed, 358 insertions(+), 5 deletions(-) create mode 100644 tests/issue4920-multiround-doombar.test.ts diff --git a/src/acp-status.ts b/src/acp-status.ts index 79173aa326..340d1afa3d 100644 --- a/src/acp-status.ts +++ b/src/acp-status.ts @@ -22,7 +22,7 @@ import { ccrEnabled, ccrLoopConfig, contentStoreOf } from "./store.js"; import { coveredRefSpan } from "./decompress-shared.js"; import { describeUncoveredRuns, shortTokenCount, uncoveredHistoryRuns } from "./uncovered.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 { getUpdateVisibility, updateBannerPayload } from "./update-notes.js"; @@ -476,6 +476,14 @@ export function handleAcpStatus(args: Record, 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): only ${drift.matched}/${drift.expected} covered message id(s) are back on the resent wire — ${drift.expected - drift.matched} covered id(s) missing since ${drift.at} (#4920): their 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 diff --git a/src/fold-reconcile.ts b/src/fold-reconcile.ts index fda1ec4dc6..1f9a8235dc 100644 --- a/src/fold-reconcile.ts +++ b/src/fold-reconcile.ts @@ -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 { log as loggerLog } from "./logger.js"; @@ -2154,6 +2154,9 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt } if (plan.unmatched.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 }; } if (opts.log !== undefined) { @@ -2188,6 +2191,12 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt `${tag}[fold-reconcile] resent history drifted: ${plan.unmatched.length} covered id(s) missing with no anchor match — 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 (the unmatched tail — claims were + // re-anchored onto churned bytes, their folds live on) on the session so + // acp_status can show it; 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 (plan.unmatched.length > 0) noteFoldDrift(session, { expected: covered.size, matched: covered.size - plan.unmatched.length }); return { kind: plan.claims.size > 0 ? (mode === "repair" ? "reanchored" : "unmatched") : "unmatched", missing: plan.claims.size + plan.unmatched.length, diff --git a/src/server.ts b/src/server.ts index 21764bb8e7..4ae4bf477c 100644 --- a/src/server.ts +++ b/src/server.ts @@ -3549,8 +3549,31 @@ export async function preflightCompressIfNeeded( // are char-cheap (1 char ≈ 1 token already) but byte-doomed; every legit // fitting English payload sits ~4× under the ratio bar. 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 @@ -3566,7 +3589,12 @@ export async function preflightCompressIfNeeded( noteLocalRefusal(session, currentOrigin, model, limit); return { failFast: true, status: 413, message: doomedMessage, retryable: false, respond: !res.writableEnded }; } - 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)`); diff --git a/src/session.ts b/src/session.ts index 9f39fcde76..051e4b2bd1 100644 --- a/src/session.ts +++ b/src/session.ts @@ -1762,6 +1762,29 @@ 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, gap: { expected: number; matched: number }): void { + if (!(gap.expected > gap.matched)) return; + session.metadata[METADATA_LAST_FOLD_DRIFT] = { matched: gap.matched, expected: gap.expected, at: new Date().toISOString() }; +} + +export function foldDriftOf(session: Pick): { 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; + if (typeof d.matched !== "number" || typeof d.expected !== "number" || typeof d.at !== "string") return undefined; + if (!(d.expected > d.matched)) return undefined; + return { matched: d.matched, expected: d.expected, at: d.at }; +} + +export function clearFoldDrift(session: Pick): 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 diff --git a/tests/issue4920-multiround-doombar.test.ts b/tests/issue4920-multiround-doombar.test.ts new file mode 100644 index 0000000000..94cd4ca87c --- /dev/null +++ b/tests/issue4920-multiround-doombar.test.ts @@ -0,0 +1,285 @@ +// #4920: after one budget-exhaustion preflight round (502 retryable=true, +// "the N ranges folded this turn persist — the next attempt starts from a +// smaller baseline", #3918), the VERY NEXT request died at the probe-regime +// doom bar: the previous round's estimate writeback armed the evidence gate, +// which priced the RAW inbound bytes (not the post-fold payload) and answered +// terminal 413 — so multi-round convergence never ran and the session sat +// permanently over-window (field incident: provider switch → 6M raw resend → +// one 31-round fold pass saved 2.5M tok → next attempt 413 → dead). +// Fix pinned here: an over-bar payload is NOT terminal while the session +// carries active fold state AND compressible ranges remain — the walk +// continues; fresh sessions without fold state keep the immediate 413 +// (#2490/#2847 semantics preserved). Plus the visibility half: lost fold +// coverage is recorded on the session for acp_status. +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { foldDriftOf, getSession, noteFoldDrift } from "../src/session.ts"; +import { setLogCapture } from "../src/logger.ts"; +import { rmrf } from "./tmp-rm.ts"; +import { mkdtempSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; + +const WINDOW = 60_000; // large enough that the irreducible core (system+tools+recent tail+carriers) fits after the walk +const SUMMARY_TEXT = + "PREFLIGHT SUMMARY: the segment held deterministic load-growth payloads across turns; every marker derives from the turn index, so folding loses nothing."; + +type Call = { summary: boolean; contentChars: number }; + +function chatSse(text: string, usage?: { prompt_tokens: number; completion_tokens: number }): string { + const chunk = (delta: Record, finish: string | null, extra?: Record): string => + `data: ${JSON.stringify({ id: "chatcmpl-1", object: "chat.completion.chunk", created: 1, model: "gpt-test", choices: [{ index: 0, delta, finish_reason: finish }], ...extra })}\n\n`; + const u = usage ? { usage } : {}; + return chunk({ role: "assistant" }, null) + chunk({ content: text }, null) + chunk({}, "stop", u) + "data: [DONE]\n\n"; +} + +// filler/tag make every call's content UNIQUE per turn: identical sequences would +// hash to the SAME message ids across turns, letting stale fold state cover the +// new payload (content-hash identity) and hiding the exact drift the incident hit. +function bigMessages(count: number, charsPerMsg = 4_400, filler = "f", tag = ""): Array<{ role: string; content: string }> { + const messages: Array<{ role: string; content: string }> = []; + const pad = Math.max(1, charsPerMsg - 12 - tag.length); + for (let i = 0; i < count; i++) { + messages.push({ role: i % 2 === 0 ? "user" : "assistant", content: `${tag}turn ${i}: ` + filler.repeat(pad) }); + } + return messages; +} + +function makeUpstream(): { server: http.Server; calls: Call[] } { + const calls: Call[] = []; + const server = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const raw = Buffer.concat(chunks).toString("utf8"); + const isSummary = /TASK: The conversation segment below/.test(raw); + try { + const parsed = JSON.parse(raw) as { messages?: Array<{ content?: unknown }> }; + const chars = (parsed.messages ?? []).reduce((n, m) => n + (typeof m.content === "string" ? m.content.length : 0), 0); + calls.push({ summary: isSummary, contentChars: chars }); + if (isSummary) { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "chatcmpl-sum", object: "chat.completion", model: "gpt-test", choices: [{ index: 0, message: { role: "assistant", content: SUMMARY_TEXT }, finish_reason: "stop" }] })); + return; + } + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(chatSse("forwarded answer", { prompt_tokens: Math.max(1, Math.round(chars / 40)), completion_tokens: 3 })); + } catch { + res.writeHead(400, { "content-type": "application/json" }); + res.end('{"error":{"message":"unparseable"}}'); + } + }); + }); + return { server, calls }; +} + +function proxyOptions(routes: Record, overrides?: Partial): ProxyOptions { + return { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: routes as ProxyOptions["routes"], + modelContextLimit: WINDOW, + kernelConfig: defaultConfig(WINDOW, { + preserveRecentMessages: 2, + preserveRecentTokens: 2000, + compress: { minCompressRange: 1000, maxSummaryLength: 20000, minSummaryLength: 50 }, + }), + compress: { injectTool: true, injectNudge: true }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + promptCache: { routing: "auto" }, + mitm: { enabled: false, domains: [] }, + ...overrides, + } as ProxyOptions; +} + +type Ctx = { url: string; headers: Record; upstreamPort: number }; + +async function setup(overrides?: Partial): Promise<{ ctx: Ctx; calls: Call[]; close: () => Promise }> { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const { server: upstream, calls } = makeUpstream(); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + const proxy = await startServer(proxyOptions({ [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-test": { context: WINDOW } } } }, overrides)); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + return { + ctx: { + url: `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/chat/completions`, + headers: { "content-type": "application/json", "x-acp-session": "" }, + upstreamPort, + }, + calls, + close: async () => { + proxy.close(); + proxy.closeIdleConnections?.(); + upstream.close(); + upstream.closeIdleConnections?.(); + await Promise.allSettled([once(proxy, "close"), once(upstream, "close")]); + }, + }; +} + +const post = (ctx: Ctx, messages: Array<{ role: string; content: string }>) => + fetch(ctx.url, { + method: "POST", + headers: { ...ctx.headers }, + body: JSON.stringify({ model: "gpt-test", stream: true, messages }), + }); + +// Turn 1 seeds the session with a usage-grade baseline on THIS route, so turn 2 +// enters the fold-first regime (probe gate off) and real fold state gets created. +async function seedFoldState(ctx: Ctx, sessionId: string): Promise { + ctx.headers["x-acp-session"] = sessionId; + const r1 = await post(ctx, [{ role: "user", content: "hello" }]); + assert.equal(r1.status, 200, "seed turn forwards"); + await r1.text(); + + // ~88K estimated tokens against a 20K window: preflight must fold. Either + // outcome (converged 200 or budget-exhausted 502) leaves ACTIVE BLOCKS — + // that is the precondition the #4920 recovery path keys on. + const r2 = await post(ctx, bigMessages(80)); + assert.ok(r2.status === 200 || r2.status === 502, `seed-fold turn settles (got ${r2.status})`); + await r2.text(); + + const s = getSession(sessionId); + assert.ok(s, "session exists"); + assert.ok(s.state.blocks.some((b) => b.active), "turn 2 left active fold state behind"); + return s.state.blocks.filter((b) => b.active).length; +} + +test("e2e #4920: over-bar payload WITH active fold state continues the convergence walk instead of dying at the doom bar", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bili-4920-a-")); + const lines: string[] = []; + setLogCapture((_level, msg) => lines.push(msg)); + try { + const { ctx, calls, close } = await setup({ log: true, logFile: path.join(root, "bili.log") }); + try { + await seedFoldState(ctx, "s-4920-a"); + + // Mirror the 502-path writeback (server.ts, #1820): after a + // progress-continues round the meter is ESTIMATE-GRADE, which arms + // the probe gate on the next request (baselineFloor drops to 0). + const s = getSession("s-4920-a")!; + s.stats.lastInputTokens = 19_000; + s.stats.lastInputTokensSource = "estimate"; + + const summariesBefore = calls.filter((c) => c.summary).length; + + // 500 msgs × 4.4K chars ≈ 2.2MB core text — past the absolute 2MB + // doom bar. Distinct filler/tag: the host re-rendered the history + // after the provider switch, so the incoming ids no longer join the + // covered set (that is the incident's drift). Pre-fix this exact + // request returned terminal 413 and the campaign never ran. + // The client keeps resending its full raw history (proxy mode: the + // ephemeral tool calls never enter client state) — recovery is a + // MULTI-ROUND campaign: each attempt folds its budget's worth, the + // folds persist, the next attempt starts from a smaller baseline + // (#3918), until the payload fits. Pre-fix the very FIRST attempt + // died at the doom bar with zero folds and no next attempt existed. + const attempts: number[] = []; + let convergedAt = -1; + for (let i = 0; i < 6 && convergedAt < 0; i++) { + const r = await post(ctx, bigMessages(500, 4_400, "x", "b")); + attempts.push(r.status); + const body = await r.text(); + if (r.status === 200) { + convergedAt = i; + } else if (r.status === 502) { + assert.match(body, /next attempt starts from a smaller baseline/, `attempt ${i}: the #3918 progress promise is intact`); + } else { + assert.fail(`attempt ${i}: unexpected status ${r.status} (${body.slice(0, 200)})`); + } + } + assert.ok(convergedAt >= 0, `campaign converged within 6 attempts (statuses: ${attempts.join(",")})`); + const summariesAfter = calls.filter((c) => c.summary).length; + assert.ok(summariesAfter > summariesBefore, `the fold walk actually ran (summaries ${summariesBefore} → ${summariesAfter})`); + assert.ok(lines.some((l) => l.includes("continuing the multi-round convergence walk")), "the new regime change is logged loudly"); + // The visibility half: the coverage gap that caused all of this is + // now recorded where acp_status can show it. + const drift = foldDriftOf(getSession("s-4920-a")!); + assert.ok(drift && drift.expected > drift.matched, "lost fold coverage is recorded on the session for acp_status"); + } finally { + await close(); + } + } finally { + setLogCapture(null); + rmrf(root); + } +}); + +test("e2e #4920: over-bar payload on a FRESH session (no fold state) still fails fast at the doom bar — #2490/#2847 preserved", async () => { + const { ctx, calls, close } = await setup(); + try { + ctx.headers["x-acp-session"] = "s-4920-b"; + const r1 = await post(ctx, [{ role: "user", content: "hello" }]); + assert.equal(r1.status, 200); + await r1.text(); + + // Stuck-meter seed (mirrors issue2490): estimate-grade baseline from a + // foreign origin → probe gate armed, no fold state anywhere. + const s = getSession("s-4920-b")!; + s.stats.lastInputTokens = 7_300_000; + s.stats.lastInputTokensSource = "estimate"; + s.stats.lastInputTokensOrigin = "http://127.0.0.1:1"; + assert.ok(!s.state.blocks.some((b) => b.active), "precondition: no fold state"); + + const r = await post(ctx, bigMessages(500)); + assert.equal(r.status, 413, "fresh-session doom refusal unchanged"); + const body = await r.text(); + assert.match(body, /NOT forwarded even for evidence/, "same operator-facing remedy wording"); + assert.equal(calls.filter((c) => c.summary).length, 0, "no fold walk runs without fold state"); + } finally { + await close(); + } +}); + +test("unit #4920: noteFoldDrift/foldDriftOf round-trip and malformed-shape rejection", () => { + const bag = { metadata: {} as Record }; + assert.equal(foldDriftOf(bag), undefined, "nothing recorded yet"); + + noteFoldDrift(bag, { expected: 6313, matched: 5674 }); + const d = foldDriftOf(bag)!; + assert.equal(d.matched, 5674); + assert.equal(d.expected, 6313); + assert.match(d.at!, /^\d{4}-\d{2}-\d{2}T/); + + // A clean coverage report (nothing missing) is a no-op — the panel line + // must not appear for healthy sessions. + const clean = { metadata: {} as Record }; + noteFoldDrift(clean, { expected: 100, matched: 100 }); + assert.equal(clean.metadata["lastFoldDrift"], undefined, "no drift recorded when coverage is complete"); + + // Stored values survive a persistence round-trip as plain JSON — validate + // defensively on read, never trust the shape. + const bad = (v: unknown) => foldDriftOf({ metadata: { lastFoldDrift: v } as Record }); + assert.equal(bad(undefined), undefined); + assert.equal(bad(null), undefined); + assert.equal(bad("drift"), undefined); + assert.equal(bad({ matched: "5", expected: 10, at: "t" }), undefined); + assert.equal(bad({ matched: 5, expected: 10 }), undefined); + assert.equal(bad({ matched: 10, expected: 5, at: "t" }), undefined, "inverted counts are not drift"); +}); From 25f4949cd3406b7d77cdfe6cd76e9d4f8dd5a4d7 Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 27 Oct 2026 15:01:02 +0800 Subject: [PATCH 2/4] test(golden): regenerate storage-render goldens for additive lastFoldDrift metadata field (#4928/#4920) The #4920 observability fix records session.metadata.lastFoldDrift {matched, expected, at} when fold coverage is lost to a resent-history drift. The scripted golden conversation hits exactly that path (fold at turn 9, content edit at turn 12), so the metadata section digest moves. Regenerated via scripts/update-storage-render-goldens.ts. The diff is metadata-only: state/blockContents/stats section digests, foldAt and editedAt are unchanged in all four lanes. The at value is epoch ms, masked __TS__ by canonicalize (#2671), so the pins stay stable across runs. --- src/session.ts | 8 +++++--- tests/golden/storage-render/anthropic.json | 6 +++--- tests/golden/storage-render/chat.json | 6 +++--- tests/golden/storage-render/google.json | 6 +++--- tests/golden/storage-render/responses.json | 6 +++--- tests/issue4920-multiround-doombar.test.ts | 1 + 6 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/session.ts b/src/session.ts index 051e4b2bd1..e5bc12b9e2 100644 --- a/src/session.ts +++ b/src/session.ts @@ -1769,16 +1769,18 @@ const METADATA_LAST_FOLD_DRIFT = "lastFoldDrift"; export function noteFoldDrift(session: Pick, gap: { expected: number; matched: number }): void { if (!(gap.expected > gap.matched)) return; - session.metadata[METADATA_LAST_FOLD_DRIFT] = { matched: gap.matched, expected: gap.expected, at: new Date().toISOString() }; + // 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): { 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; - if (typeof d.matched !== "number" || typeof d.expected !== "number" || typeof d.at !== "string") return undefined; + 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: d.at }; + return { matched: d.matched, expected: d.expected, at: new Date(d.at).toISOString() }; } export function clearFoldDrift(session: Pick): void { diff --git a/tests/golden/storage-render/anthropic.json b/tests/golden/storage-render/anthropic.json index b6ebe6f156..cdd923c450 100644 --- a/tests/golden/storage-render/anthropic.json +++ b/tests/golden/storage-render/anthropic.json @@ -95,13 +95,13 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "81ee1ad6ff722210226a6f3969162e6715dedf607f3c1bc9b9c9293d9539ca38", - "stateDigestAligned": "833d3548ede983e7c0c49c685f97c8f62b28fca779a27b211cedd13731fb5c58", + "stateDigest": "bf8663a24aaefdeb8106d7818f89248133df50b2a733fe16c366e2b07c33585a", + "stateDigestAligned": "b1b4d5e10632a1a3b4f81515de8caf5cf9e2c46367086ccb0c548c7f66ef0e16", "sectionDigests": { "state": "7f38eb530c6731be", "blockContents": "dc7d13d250c94bb4", "stats": "17f1325cb58358bb", - "metadata": "a4ab27ced8ed8346" + "metadata": "0c551e4803882013" }, "stateKeyDigests": { "blocks": "3a454b9a16e4f0b9", diff --git a/tests/golden/storage-render/chat.json b/tests/golden/storage-render/chat.json index 0a9dc0c169..ddfaa44c5e 100644 --- a/tests/golden/storage-render/chat.json +++ b/tests/golden/storage-render/chat.json @@ -95,13 +95,13 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "63744b46ebb010d6ee194aa60caff8afd03c4c03849d2b45d3a9356193a159be", - "stateDigestAligned": "24ec5c3955e73f9ebecd194aaa428f1af4c1db7c58d6a9e93cf86f42e4c3ce8a", + "stateDigest": "d4ea521c494fc469b51519568f23b6567995d7af7657f3130ca541ccf92c9e49", + "stateDigestAligned": "7731e11d0fc4d8df55b9ffb95288a23be797b749c9a4251f46359101c6e42023", "sectionDigests": { "state": "8d09f6115ebffb9d", "blockContents": "94a58f96d7c5cfc3", "stats": "95712051bd1e8b03", - "metadata": "0cbd1fda3a07b9af" + "metadata": "edb88c98b44bfda6" }, "stateKeyDigests": { "blocks": "d9770feee91dafc6", diff --git a/tests/golden/storage-render/google.json b/tests/golden/storage-render/google.json index 1d51cd4e0d..c08c6e00cf 100644 --- a/tests/golden/storage-render/google.json +++ b/tests/golden/storage-render/google.json @@ -95,13 +95,13 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "7795ffa5f895adbaeebff6eb78817b4612cf3fec5652dee92c4872a91a0ec961", - "stateDigestAligned": "aebc5d16e500e193c08652c54214ac50f8acfc6ba89d24e33371a7eb2a06d2a9", + "stateDigest": "1dba200bc3e70f73047e38424eeecea158b18a4ea3dafc3ddba1793db3e739f8", + "stateDigestAligned": "e04a8fbcc95368d078704ac4ab175223610f3ee6eec0d3c9ec5d6d5411182e22", "sectionDigests": { "state": "3b0f64f8e2b16e36", "blockContents": "473264d9df8ba338", "stats": "b215001eb0f2a5ff", - "metadata": "829b443829784726" + "metadata": "c17072266a3bc4b9" }, "stateKeyDigests": { "blocks": "0cfd0b9c0fc53073", diff --git a/tests/golden/storage-render/responses.json b/tests/golden/storage-render/responses.json index 04831f8bfa..2c3ccdd44e 100644 --- a/tests/golden/storage-render/responses.json +++ b/tests/golden/storage-render/responses.json @@ -95,13 +95,13 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "5b613a8c8a9c1cc47597779fc398bfe9ed99b524c2bd6a8e7e7188cc3c0a9798", - "stateDigestAligned": "4b725248d57d935b4a8fe03ed114684bf9421acfa8f7431dd286e3909a138086", + "stateDigest": "5e14fdbfd7fc2552d59f1a4c76a069da17f75495a5eb46ff60ca8b438f87d90a", + "stateDigestAligned": "533398717563ebca2d16ba5796bde925aaa144b0020863a25c9629bf8f5c7c0c", "sectionDigests": { "state": "543d4459ecf0fe8f", "blockContents": "b6701268a0bdcd8a", "stats": "0c75fc5429aa3048", - "metadata": "afea52381c5db9fc" + "metadata": "5d639435ca4144b0" }, "stateKeyDigests": { "blocks": "34b0dc17f8c24595", diff --git a/tests/issue4920-multiround-doombar.test.ts b/tests/issue4920-multiround-doombar.test.ts index 94cd4ca87c..71519a4f50 100644 --- a/tests/issue4920-multiround-doombar.test.ts +++ b/tests/issue4920-multiround-doombar.test.ts @@ -281,5 +281,6 @@ test("unit #4920: noteFoldDrift/foldDriftOf round-trip and malformed-shape rejec assert.equal(bad("drift"), undefined); assert.equal(bad({ matched: "5", expected: 10, at: "t" }), undefined); assert.equal(bad({ matched: 5, expected: 10 }), undefined); + assert.equal(bad({ matched: 5, expected: 10, at: "2026-01-01T00:00:00Z" }), undefined, "at must be epoch ms"); assert.equal(bad({ matched: 10, expected: 5, at: "t" }), undefined, "inverted counts are not drift"); }); From d479033af9609ede45d656660a70ed4669cb2e99 Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 16 Nov 2026 06:01:06 +0800 Subject: [PATCH 3/4] fix: key the #4920 fold-drift record on realUnmatched, not plan.unmatched MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The new session-level drift record (noteFoldDrift) counted plan.unmatched — which still includes expected rebases (carrier retirement / offload rewrites, #5142) — as lost coverage. The clean-pass early return added in the same PR, the #5142 accounting discipline, and the drift-warn cascade all key on realUnmatched (= plan.unmatched minus expected rebases). In a pass mixing genuine drift with expected rebases the acp_status line over-stated the missing count by counting expected host behavior as lost folds. Use realUnmatched for the guard and the matched computation, and reword the acp_status line to a lost/retain-fold-coverage framing so it is accurate under that caliber. Visibility-only: no fold decision, status, or persistence-format change; the e2e test is unaffected (no expected rebases in its scenario, so plan.unmatched === realUnmatched there). --- src/acp-status.ts | 2 +- src/fold-reconcile.ts | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/acp-status.ts b/src/acp-status.ts index a1aa9cd6dd..ef55475915 100644 --- a/src/acp-status.ts +++ b/src/acp-status.ts @@ -506,7 +506,7 @@ export function handleAcpStatus(args: Record, ctx: AcpStatusCtx const drift = foldDriftOf(ctx.session); if (drift) { extra.push(""); - extra.push(`FOLD DRIFT (session-level): only ${drift.matched}/${drift.expected} covered message id(s) are back on the resent wire — ${drift.expected - drift.matched} covered id(s) missing since ${drift.at} (#4920): their folded content rides the wire raw again until coverage re-anchors; see the [acp-drift] lines in the bili log.`); + 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) { diff --git a/src/fold-reconcile.ts b/src/fold-reconcile.ts index ea1b0c2f4f..069ed2e4f5 100644 --- a/src/fold-reconcile.ts +++ b/src/fold-reconcile.ts @@ -2516,12 +2516,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 (the unmatched tail — claims were - // re-anchored onto churned bytes, their folds live on) on the session so - // acp_status can show it; 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 (plan.unmatched.length > 0) noteFoldDrift(session, { expected: covered.size, matched: covered.size - plan.unmatched.length }); + // #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, From 0cae5a0e0575741fdd1a4e5a7b33c1d906e1f43a Mon Sep 17 00:00:00 2001 From: review-worker-r1 Date: Thu, 19 Nov 2026 07:56:52 +0800 Subject: [PATCH 4/4] fix: correct stale window number in the #4920 seed comment (20K -> 60K, matches WINDOW) --- tests/issue4920-multiround-doombar.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/issue4920-multiround-doombar.test.ts b/tests/issue4920-multiround-doombar.test.ts index 71519a4f50..7ce01af838 100644 --- a/tests/issue4920-multiround-doombar.test.ts +++ b/tests/issue4920-multiround-doombar.test.ts @@ -158,7 +158,7 @@ async function seedFoldState(ctx: Ctx, sessionId: string): Promise { assert.equal(r1.status, 200, "seed turn forwards"); await r1.text(); - // ~88K estimated tokens against a 20K window: preflight must fold. Either + // ~88K estimated tokens against a 60K window: preflight must fold. Either // outcome (converged 200 or budget-exhausted 502) leaves ACTIVE BLOCKS — // that is the precondition the #4920 recovery path keys on. const r2 = await post(ctx, bigMessages(80));