diff --git a/src/acp-status.ts b/src/acp-status.ts index dd474184c7..ef55475915 100644 --- a/src/acp-status.ts +++ b/src/acp-status.ts @@ -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"; @@ -500,6 +500,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): ${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 diff --git a/src/fold-reconcile.ts b/src/fold-reconcile.ts index c82af5da17..e442aa11ca 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 { dshCarrierRawMarkers, oneShotCarrierPaths } from "./durable-message-guards.js"; import { log as loggerLog } from "./logger.js"; @@ -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) { @@ -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, diff --git a/src/server.ts b/src/server.ts index 3fb954b5ff..11a4062f6e 100644 --- a/src/server.ts +++ b/src/server.ts @@ -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 @@ -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)`); diff --git a/src/session.ts b/src/session.ts index ca5e7cddf3..f8d258009b 100644 --- a/src/session.ts +++ b/src/session.ts @@ -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, 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): { 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 !== "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): 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/golden/storage-render/anthropic.json b/tests/golden/storage-render/anthropic.json index 7c01ccf504..9e7ee695a1 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": "ba0f75f6ddc882d00d5c456facd79166b62e3f2cacadd60744aa370d6b247105", - "stateDigestAligned": "66991d5cb8864f3ca029914aa9afc20cc8b9687cfd4391ffe200017c68057946", + "stateDigest": "38deb402fefa9ae49a43b8fb52f37d646748a6c8f5434dbce622412d97795a54", + "stateDigestAligned": "dd11594c62761a6771744a31284336b436b07836f46f95f9be0da9025e12b9dc", "sectionDigests": { "state": "37b26b4f6898afff", "blockContents": "dc7d13d250c94bb4", "stats": "7caf981f99159be9", - "metadata": "ba51746c6749d0cf" + "metadata": "2cbcbfa8576a2d38" }, "stateKeyDigests": { "blocks": "3a454b9a16e4f0b9", diff --git a/tests/golden/storage-render/chat.json b/tests/golden/storage-render/chat.json index 5ecde5722b..ace7da1559 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": "2c0b36206756617ae1e32cf0d221ddd8bf7f3e06994b49d11578bd1a35c90f26", - "stateDigestAligned": "3011800706759958cfeab6520289f1ffe257cf4f7c3275d846c03a991f9ed6b8", + "stateDigest": "80bf6d82d64d9a34de9181566c61de350105ac229cea52fc34880f221e4fafb5", + "stateDigestAligned": "7c2a5fd0dfaa7885b1f24c5ea8a86fee27ca1c3a3adb68ac4df11b2a667dd1be", "sectionDigests": { "state": "90857fbf82001a2b", "blockContents": "94a58f96d7c5cfc3", "stats": "7b6162aef3873d31", - "metadata": "7218c1e3430c3d21" + "metadata": "560a86123869a725" }, "stateKeyDigests": { "blocks": "d9770feee91dafc6", diff --git a/tests/golden/storage-render/google.json b/tests/golden/storage-render/google.json index 26c5f73b94..216c45cdc4 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": "c5eefef6525f180ab54a7ea57c4ddf004a568db0fc2d6d227ede31dc960706db", - "stateDigestAligned": "5217aa02331256a419837cd91105d3e3172f2ff38f0f3914c0e01128e221f012", + "stateDigest": "d78de456028d13a8707a0ec1a3e11df1cdaccbe6840fbcd4714de72abd094e9c", + "stateDigestAligned": "672aeb55d0d8c823609fe0414efa21df52bc3c033929d38bf3522d2a332733eb", "sectionDigests": { "state": "c5243ba17364d3ac", "blockContents": "473264d9df8ba338", "stats": "c90671e484dc5d37", - "metadata": "2980a8959aa0a39c" + "metadata": "326f64fe1cbdd176" }, "stateKeyDigests": { "blocks": "0cfd0b9c0fc53073", diff --git a/tests/golden/storage-render/responses.json b/tests/golden/storage-render/responses.json index 8041cd5155..90a4205151 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": "ff092bc66e11c35b15ad98acf6206629115825b62168da2e5542e06297f6d47a", - "stateDigestAligned": "db5f08a7d57c14952c31571e45b4dbc6c2ed35fe8a97c58cda6fa854d411eb25", + "stateDigest": "707f30ffd74f390b045bce44a2839474c539d588fd9969fc092d4a5d54dcd246", + "stateDigestAligned": "91ac8b4d0232d94f2d445542e65552823cc5ebc56a2f391ffca0bac5d23988e8", "sectionDigests": { "state": "bee6e1ba0c6fba62", "blockContents": "b6701268a0bdcd8a", "stats": "1b56c993f03cd122", - "metadata": "77714417e484ba89" + "metadata": "9606d7ec2ce829a5" }, "stateKeyDigests": { "blocks": "34b0dc17f8c24595", diff --git a/tests/issue4920-multiround-doombar.test.ts b/tests/issue4920-multiround-doombar.test.ts new file mode 100644 index 0000000000..7ce01af838 --- /dev/null +++ b/tests/issue4920-multiround-doombar.test.ts @@ -0,0 +1,286 @@ +// #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 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)); + 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: 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"); +});