From 6c490391484fa8e91eaf7330620a51fc9fcdf7e7 Mon Sep 17 00:00:00 2001 From: awork Date: Tue, 20 Oct 2026 17:17:48 +0800 Subject: [PATCH 1/4] fix: name the permanent resident-eviction case in compress refusals (#3918) The empty-view compress refusal (#3126) promised the view self-heals on the next model request. That promise holds only when the reloaded session survives between requests: releaseInFlight's discharge evicts any session whose live weight plus the other residents exceeds the byte budget, so a large image session ping-pongs out after every cycle and the identical refusal repeats forever - the incident's 444 MiB session under the 512 MiB default against an 87.5 MiB peer. Stamp each eviction with the session's live weight (measurable only at that moment; a disk reload under-measures by exactly the transient mass) and export residentPressure(). When weight + other residents > budget, the receipt and the operator log name the permanent case and give the exits (close the peer / raise sessions.residentMaxBytes) instead of advising an impossible retry. The transient case keeps the exact original wording. The map is advisory-only, capped at 256 entries. --- src/session.ts | 57 +++++++++ src/stream.ts | 28 ++++- ...ssue3918-resident-eviction-refusal.test.ts | 116 ++++++++++++++++++ 3 files changed, 197 insertions(+), 4 deletions(-) create mode 100644 tests/issue3918-resident-eviction-refusal.test.ts diff --git a/src/session.ts b/src/session.ts index 25d0c08ddf..a01483459a 100644 --- a/src/session.ts +++ b/src/session.ts @@ -838,6 +838,25 @@ let MAX_SESSIONS = knobMaxSessions(); let MAX_RESIDENT_BYTES = sessionResidentMaxBytes(); const residentWeights = new WeakMap(); +// #3918: last-known LIVE weight of evicted sessions, stamped at eviction time. +// The live weight (transient message caches included) is only measurable while +// the object is resident — a disk reload carries persisted state alone and +// under-measures by exactly the mass that made the eviction necessary. The map +// is advisory (it only shapes refusal wording), never load-bearing; bounded so +// a long-lived process cannot accumulate unbounded entries. +const evictedWeights = new Map(); +const EVICTED_WEIGHT_CAP = 256; + +function stampEvictedWeight(id: string, bytes: number): void { + if (evictedWeights.has(id)) evictedWeights.delete(id); + evictedWeights.set(id, bytes); + while (evictedWeights.size > EVICTED_WEIGHT_CAP) { + const oldest = evictedWeights.keys().next().value; + if (oldest === undefined) break; + evictedWeights.delete(oldest); + } +} + /** Estimate retained objects without serializing a second giant copy. Include * originals and last-message caches; counting files alone misses both. */ function deepSizeOf(value: unknown): number { @@ -940,6 +959,42 @@ function residentBytes(): number { return bytes; } +/** #3918: can this session's message view survive BETWEEN model requests? The + * empty-view compress refusal (#3126) self-heals on the next model request ONLY + * when the reloaded session stays resident afterwards. When the session's + * last-known live weight plus the other residents exceeds the byte budget, + * every request cycle ends with it evicted again (releaseInFlight discharge) + * and the refusal repeats identically forever — the incident's 444 MiB image + * session ping-ponged against an 87.5 MiB peer under the 512 MiB default. + * Returns the measured pressure so the refusal receipt says so plainly instead + * of promising a heal that will not come. */ +export interface ResidentPressure { + /** True when retrying cannot restore a usable view under the CURRENT pool state. */ + permanent: boolean; + /** Last-known LIVE weight of this session (eviction-time measurement; falls + * back to its resident weight when it is resident now). Undefined = unknown. */ + weightBytes?: number; + /** Resident bytes held by all OTHER sessions (this one excluded). */ + otherBytes: number; + residentBytes: number; + budgetBytes: number; +} + +export function residentPressure(id: string): ResidentPressure { + const resident = sessions.get(id); + const self = resident ? sessionWeight(resident) : 0; + const total = residentBytes(); + const known = evictedWeights.get(id); + const weightBytes = known !== undefined ? Math.max(known, self) : resident ? self : undefined; + return { + permanent: weightBytes !== undefined && weightBytes + (total - self) > MAX_RESIDENT_BYTES, + weightBytes, + otherBytes: total - self, + residentBytes: total, + budgetBytes: MAX_RESIDENT_BYTES, + }; +} + function makeResidentRoom(incoming = 0): void { while (sessions.size >= MAX_SESSIONS || (sessions.size > 0 && residentBytes() + incoming > MAX_RESIDENT_BYTES)) { if (!evictOldest()) { @@ -1644,6 +1699,7 @@ function evictOldest(): boolean { return false; } sessions.delete(oldestId); + stampEvictedWeight(oldestId, sessionWeight(s)); // #3126: eviction is the trigger for the empty-view compress window (the // session's message bytes live only here; state reloads from disk but the // view self-heals only on the next model request). Log it so operators can @@ -1667,6 +1723,7 @@ export function _resetSessionsForTest(max?: number, maxBytes?: number): void { if (s.inFlight > 0) s.inFlight = 0; } sessions.clear(); + evictedWeights.clear(); } export function _sessionsSizeForTest(): number { diff --git a/src/stream.ts b/src/stream.ts index 079170b960..b3071114d8 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -2,7 +2,7 @@ import { createHash, randomBytes } from "node:crypto"; import { activeBlockSpans, collectBlockContent, countMessageTokens, defaultCountTokens, formatRanges, hideConsumedCompressCalls, storeCoveredOriginals, viableRanges, type CompressionCore, type Config, type CoreMessage, type CompressionState, type NudgeDecision, type CompressParseDiagnostics } from "acp-kernel"; import { excludeDeadRefRanges, handleAcpStatus, liveCompressionRanges } from "./acp-status.js"; import { handleAcpCache, recordCacheFoldsFromBlocks } from "./cache-ledger.js"; -import { type Session, cacheBlockContent, markDirty, scrubTurnSeparatorIds, statusInputBaseline } from "./session.js"; +import { type Session, cacheBlockContent, markDirty, residentPressure, scrubTurnSeparatorIds, statusInputBaseline } from "./session.js"; import { COMPRESS_TOOL_NAME, parseCompressInput, ABSORB_TOOL_NAME, type ParsedRange } from "./compress-tool.js"; import { effectiveAbsorbConfig, executeAbsorb, isProxyToolFor } from "./absorb.js"; import { executeSearchContextTarget, resolveDecompress } from "./decompress-shared.js"; @@ -633,14 +633,34 @@ export function noVisibleContextRefusal( }); if (touchable) return null; const detail = ranges.map((r) => `${r.startRef}–${r.endRef}`).join(", "); - ctx.log(`[acp-proxy: compress REFUSED ${detail} — no visible context (view=0 message(s), ${mappedRefs} ref(s) mapped); refs left untouched and nothing recorded as dead — the view self-heals on the next model request (#3126)]`); + // #3918: the "self-heals on the next model request" promise holds ONLY when + // the reloaded session survives between requests. When this session's + // last-known live weight cannot fit alongside the current residents within + // the byte budget, every request cycle ends with it evicted again and this + // exact refusal repeats forever — say so plainly instead of advising an + // impossible retry (the incident: a ~444 MiB image session ping-ponging + // against an ~87.5 MiB peer under the 512 MiB default budget). + const pressure = residentPressure(ctx.session.id); + ctx.log(pressure.permanent + ? `[acp-proxy: compress REFUSED ${detail} — no visible context (view=0 message(s), ${mappedRefs} ref(s) mapped); refs left untouched and nothing recorded as dead — PERMANENT: last-known weight ${formatMiB(pressure.weightBytes)} + other residents ${formatMiB(pressure.otherBytes)} > budget ${formatMiB(pressure.budgetBytes)}; the session cannot stay resident between model requests (#3918)]` + : `[acp-proxy: compress REFUSED ${detail} — no visible context (view=0 message(s), ${mappedRefs} ref(s) mapped); refs left untouched and nothing recorded as dead — the view self-heals on the next model request (#3126)]`); const specKey = normalizedSpecKey(ranges); - const repeatGuard = recordCompressFailure(ctx.session, specKey, "This failure is the proxy's missing in-memory message view, not your refs — it clears on the next model request. Wait for that turn instead of re-issuing."); + const repeatGuard = recordCompressFailure(ctx.session, specKey, pressure.permanent + ? "This failure repeats until the resident pool can hold this session alongside the others — closing another open session or raising sessions.residentMaxBytes clears it; waiting for the next turn does NOT." + : "This failure is the proxy's missing in-memory message view, not your refs — it clears on the next model request. Wait for that turn instead of re-issuing."); const loopGuard = opts?.loopTracking === false ? "" : noteCompressLoopFailure(ctx, specKey, undefined, "no-visible-context"); - const receipt = `[Compression FAILED: no visible context — the proxy holds no live message view of this conversation right now (its in-memory copy was dropped between model requests, e.g. by memory eviction or a restart). Your refs are intact and NOTHING was marked dead: ${mappedRefs} ref(s) remain mapped, and this is NOT a ref problem. Do NOT retry now — every retry fails identically until the next model request re-sends the conversation and restores the view. Continue your task; after the next turn you can compress the same ranges again. [cause: no-visible-context]${repeatGuard}${loopGuard}]`; + const healClause = pressure.permanent + ? ` THIS REFUSAL IS NOT TRANSIENT: your session's last measured memory footprint (~${formatMiB(pressure.weightBytes)}) plus the other resident sessions (~${formatMiB(pressure.otherBytes)}) exceed the resident pool budget (${formatMiB(pressure.budgetBytes)}), so every reload of this session is evicted again and no usable view survives between model requests — retrying now or after the next turn fails identically. To recover: finish or close the other open session(s), or raise sessions.residentMaxBytes in ~/.config/billion-context/billion-context.json (env twin BILI_SESSION_RESIDENT_MAX_BYTES) above ~${formatMiB((pressure.weightBytes ?? 0) + pressure.otherBytes)} and restart bili.` + : "Do NOT retry now — every retry fails identically until the next model request re-sends the conversation and restores the view. Continue your task; after the next turn you can compress the same ranges again."; + const receipt = `[Compression FAILED: no visible context — the proxy holds no live message view of this conversation right now (its in-memory copy was dropped between model requests, e.g. by memory eviction or a restart). Your refs are intact and NOTHING was marked dead: ${mappedRefs} ref(s) remain mapped, and this is NOT a ref problem.${healClause} [cause: no-visible-context]${repeatGuard}${loopGuard}]`; return compressResult(receipt, "refused", 0, "gate:no-visible-context"); } +function formatMiB(bytes: number | undefined): string { + if (bytes === undefined) return "?"; + return `${(bytes / (1024 * 1024)).toFixed(1)} MiB`; +} + // #2749: display-stub re-submission guard. In plugin mode the model's only // persistent record of a folded block's summary is the ~200-char display stub // that hideConsumedCompressCalls writes into the re-sent call args diff --git a/tests/issue3918-resident-eviction-refusal.test.ts b/tests/issue3918-resident-eviction-refusal.test.ts new file mode 100644 index 0000000000..69ec1c2b2f --- /dev/null +++ b/tests/issue3918-resident-eviction-refusal.test.ts @@ -0,0 +1,116 @@ +// #3918: a session whose live footprint cannot fit the resident pool alongside +// other active sessions is evicted after EVERY model request (releaseInFlight +// discharge), so the empty-view compress refusal (#3126) repeats forever — yet +// its receipt kept promising "the next model request restores the view". These +// tests pin the honest diagnosis: when the session's last-known live weight +// plus the current residents exceeds the byte budget, the refusal names the +// permanent case and gives the actionable exits (close the peer / raise +// sessions.residentMaxBytes); when the pool CAN hold it (peer closed, restart, +// small session), the original transient self-heal wording stands. The gate +// itself (refusal, nothing marked dead) stays pinned in +// tests/issue3126-no-visible-context.test.ts. +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { mkdtempSync, rmSync } from "node:fs"; +import * as os from "node:os"; +import * as path from "node:path"; +import type { CoreMessage } from "acp-kernel"; +import { createCore, defaultConfig, assignRefs, emptyRefMap } from "acp-kernel"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { getSession, acquireInFlight, releaseInFlight, markDirty, dropSessionForGc, residentPressure, _resetSessionsForTest } from "../src/session.ts"; +import { parseCompressInput } from "../src/compress-tool.ts"; +import { applyRanges, type RewriteCtx } from "../src/stream.ts"; + +const BUDGET = 300_000; + +const MESSAGES = Array.from({ length: 80 }, (_, i) => ({ + id: `raw${i}`, + role: (i % 2 ? "assistant" : "user") as "assistant" | "user", + text: "lorem ipsum dolor sit amet conseq ".repeat(8) + i, +})) as unknown as CoreMessage[]; + +function makeCtx(session: RewriteCtx["session"], logs: string[]): RewriteCtx { + return { + core: createCore(), + // Explicit protect-zone override (same recipe as + // tests/issue3126-no-visible-context.test.ts): the bare default would + // fence m00010..m00040 under recent-message protection and mask the + // gate behavior under test. + config: defaultConfig(200000, { preserveRecentMessages: 5, preserveRecentTokens: 0 }), + messages: [], + session, + log: (m: string) => { logs.push(m); }, + }; +} + +const SPEC = () => parseCompressInput({ content: [{ startId: "m00010", endId: "m00040", summary: "y".repeat(100) }] }); + +async function fixture(run: () => Promise): Promise { + const root = mkdtempSync(path.join(os.tmpdir(), "bili-3918-")); + const store = new SessionStore({ dir: path.join(root, "sessions"), enabled: true, debounceMs: 0 }); + _resetSessionsForTest(256, BUDGET); + _setStoreForTest(store); + try { await run(); } finally { + store.closeQuietly(); + _resetSessionsForTest(256, 512 * 1024 * 1024); + _setStoreForTest(new SessionStore({ enabled: false })); + rmSync(root, { recursive: true, force: true }); + } +} + +/** Grow `big-sess` past what BUDGET can hold alongside `peer-sess`, then run + * one request cycle over it: releaseInFlight's discharge evicts the oldest + * idle session (big, touched first) and stamps its live weight. Returns the + * evicted object — alive in JS, out of the pool, refs intact on its state. */ +function growAndEvictBig(): RewriteCtx["session"] { + const big = getSession("big-sess"); + const peer = getSession("peer-sess"); + big.state.messageRefs = assignRefs(MESSAGES, { existing: emptyRefMap(), nextIndex: 0 }).map; + big.metadata.blob = "x".repeat(80_000); + peer.metadata.blob = "y".repeat(60_000); + markDirty(big); + markDirty(peer); + acquireInFlight(big); + releaseInFlight(big); + return big; +} + +test("#3918: eviction stamps the live weight; pressure is permanent while the peer holds the budget, transient once it is gone", async () => fixture(async () => { + const big = growAndEvictBig(); + const p = residentPressure(big.id); + assert.ok(p.weightBytes !== undefined && p.weightBytes > 100_000, `live weight stamped at eviction (80K-char blob ≈ 160KiB): ${p.weightBytes}`); + assert.equal(p.budgetBytes, BUDGET); + assert.ok(p.otherBytes > 50_000, `the peer still occupies budget space: ${p.otherBytes}`); + assert.ok(p.permanent, `weight ${p.weightBytes} + others ${p.otherBytes} > budget ${BUDGET} → permanent`); + assert.equal(dropSessionForGc("peer-sess"), true); + const freed = residentPressure(big.id); + assert.equal(freed.permanent, false, "without the peer the session fits the budget → transient again"); + assert.equal(residentPressure("never-seen").permanent, false, "unknown session: no permanent claim"); + assert.equal(residentPressure("never-seen").weightBytes, undefined, "unknown session: no weight to quote"); +})); + +test("#3918: the refusal receipt names the PERMANENT case and gives the exits instead of an impossible retry", async () => fixture(async () => { + const big = growAndEvictBig(); + const logs: string[] = []; + const res = applyRanges(SPEC(), makeCtx(big, logs)); + assert.equal(res.outcome, "refused"); + assert.equal(res.reason, "gate:no-visible-context"); + assert.ok(res.text.includes("NOT TRANSIENT"), `receipt names the permanent case: ${res.text}`); + assert.ok(res.text.includes("sessions.residentMaxBytes"), `receipt gives the config exit: ${res.text}`); + assert.ok(!res.text.includes("after the next turn you can compress the same ranges again"), `impossible-retry advice removed: ${res.text}`); + assert.ok(logs.some((l) => l.includes("PERMANENT")), `operator log carries the marker: ${JSON.stringify(logs)}`); + assert.equal(res.blocksCreated, 0); + assert.equal(big.state.deadRefs, undefined, "still nothing marked dead"); +})); + +test("#3918: with the peer gone the same empty-view shape keeps the transient self-heal advice (negative direction)", async () => fixture(async () => { + const big = growAndEvictBig(); + assert.equal(dropSessionForGc("peer-sess"), true); + const logs: string[] = []; + const res = applyRanges(SPEC(), makeCtx(big, logs)); + assert.equal(res.outcome, "refused"); + assert.equal(res.reason, "gate:no-visible-context"); + assert.ok(res.text.includes("Do NOT retry now"), `transient wording intact: ${res.text}`); + assert.ok(!res.text.includes("NOT TRANSIENT"), `no permanent claim when the pool fits: ${res.text}`); + assert.ok(!logs.some((l) => l.includes("PERMANENT")), "log stays on the #3126 self-heal line"); +})); From 0f65e8e1b9c2588ebba3f75bc7cfdaa50979e085 Mon Sep 17 00:00:00 2001 From: awork Date: Tue, 20 Oct 2026 17:17:48 +0800 Subject: [PATCH 2/4] fix: preflight budget exhaustion with committed folds is not terminal (#3918) A preflight walk that exhausts its summarization budget AFTER committing real folds was reported 502 retryable=false - 'doomed' - although the folds persist in the session and the next attempt starts from a smaller baseline and continues the fold (the incident ran 64 effective folds, every round declared terminal). Non-deterministic exhaustion with compressedRanges > 0 now reports retryable=true and appends a progress note (ranges folded, tokens short of the window). Zero-progress exhaustion keeps today's terminal verdict and dead-end cooldown; deterministic exhaustion keeps its 413 path byte-identical. --- src/server.ts | 16 +- ...sue3918-preflight-progress-verdict.test.ts | 184 ++++++++++++++++++ 2 files changed, 198 insertions(+), 2 deletions(-) create mode 100644 tests/issue3918-preflight-progress-verdict.test.ts diff --git a/src/server.ts b/src/server.ts index 7e0daeff80..bc0f9594f2 100644 --- a/src/server.ts +++ b/src/server.ts @@ -3633,11 +3633,23 @@ export async function preflightCompressIfNeeded( // lanes keep their 502 fail-fast contract where NOT re-forwarding IS the // protection, not a client spin. const status = f?.kind === "upstream" && f.status === 429 ? 503 : f?.kind === "exhausted" && f.deterministic && !payloadFitsWindow ? 413 : 502; + // #3918: non-deterministic exhaustion WITH committed progress is not terminal — + // the folds persist in the session, so the next attempt starts from a smaller + // baseline and continues the fold. Reporting retryable=false told hosts to stop + // spinning exactly where each retry makes real progress (incident: 64 effective + // folds, every round declared terminal). Deterministic exhaustion keeps today's + // verdict: nothing more can fold from this view, so retries are genuinely doomed. + const progressContinues = f?.kind === "exhausted" && !f.deterministic && result.compressedRanges > 0; // #2189: an explicit retryable=false from preflight (credential-shape // rejection — deterministic for the identical payload) overrides the // status-based heuristic below; every other path keeps today's mapping. - const retryable = f?.retryable === false ? false : f?.retryable === true || (f?.kind === "upstream" && f.status !== undefined && (f.status === 429 || f.status >= 500)); - const ff = failFast(status, f?.detail ?? "the payload still exceeds the window after preflight compression", retryable, result.compressedRanges > 0 ? foldedCalibratedSize : undefined, result.rangesRemaining); + const retryable = progressContinues ? true : f?.retryable === false ? false : f?.retryable === true || (f?.kind === "upstream" && f.status !== undefined && (f.status === 429 || f.status >= 500)); + let ffDetail = f?.detail ?? "the payload still exceeds the window after preflight compression"; + if (progressContinues) { + const shortfall = foldedCalibratedSize !== undefined ? Math.max(0, limit - foldedCalibratedSize) : undefined; + ffDetail += ` ${result.compressedRanges} range(s) folded this turn persist in the session — the next attempt starts from a smaller baseline and continues the fold${shortfall !== undefined ? ` (~${shortfall.toLocaleString("en-US")} tokens short of the window)` : ""}.`; + } + const ff = failFast(status, ffDetail, retryable, result.compressedRanges > 0 ? foldedCalibratedSize : undefined, result.rangesRemaining); const contentDeadEnd = f?.kind === "exhausted" || (f?.kind === "upstream" && f.status !== undefined && f.status >= 400 && f.status < 500 && !retryable); if (contentDeadEnd && result.compressedRanges === 0) { const cooldownMs = preflightDeadEndCooldownMs(); diff --git a/tests/issue3918-preflight-progress-verdict.test.ts b/tests/issue3918-preflight-progress-verdict.test.ts new file mode 100644 index 0000000000..802a98c15b --- /dev/null +++ b/tests/issue3918-preflight-progress-verdict.test.ts @@ -0,0 +1,184 @@ +// #3918: a preflight that EXHAUSTS its summarization budget after committing +// real folds was reported terminal (502 retryable=false) even though the folds +// persist in the session and the NEXT attempt starts from a smaller baseline — +// the incident's 64 effective folds each got a "doomed" verdict. Non- +// deterministic exhaustion WITH progress must now be retryable=true with a +// progress note; zero-progress exhaustion keeps today's terminal verdict +// (pinned here AND in tests/preflight-multi-range.test.ts), and deterministic +// exhaustion keeps its 413 path (tests/issue3461-overlimit-exits.test.ts). +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 { listSessions } from "../src/session.ts"; + +const SUMMARY_TEXT = + "PREFLIGHT SUMMARY: the segment covered a multi-step debugging session. " + + "Key decisions: chose the multi-range walk over lossy truncation because the payload must stay coherent. " + + "Files touched: src/a.ts:10, src/b.ts:20. Outcome: fixed and verified by tests."; + +function okSse(inputTokens: number): string { + return ( + `event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "m1", role: "assistant", usage: { input_tokens: inputTokens } } })}\n\n` + + `event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })}\n\n` + + `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } })}\n\n` + + `event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n` + + `event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 3 } })}\n\n` + + `event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n` + ); +} + +function conversation(n: number, tag: string): Array<{ role: string; content: string }> { + const msgs: Array<{ role: string; content: string }> = []; + for (let i = 0; i < n; i++) { + const role = i % 2 === 0 ? "user" : "assistant"; + msgs.push({ role, content: `${tag} Message ${i} of the long conversation. ` + `MARKER_${i}_content_`.repeat(250) }); + } + return msgs; +} + +type Call = { stream: boolean; body: string }; + +function makeUpstream(usable: (idx: number) => boolean): { server: http.Server; calls: Call[] } { + const calls: Call[] = []; + let summaryIdx = 0; + 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"); + let parsed: { stream?: boolean } = {}; + try { parsed = JSON.parse(raw); } catch { /* keep {} */ } + calls.push({ stream: !!parsed.stream, body: raw }); + if (parsed.stream) { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(okSse(1000)); + } else { + summaryIdx += 1; + const text = usable(summaryIdx) ? SUMMARY_TEXT : ""; + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ + id: "msg_summary", type: "message", role: "assistant", model: "claude-small", + content: [{ type: "text", text }], stop_reason: "max_tokens", + usage: { input_tokens: 500, output_tokens: 50 }, + })); + } + }); + }); + return { server, calls }; +} + +function startProxy(upstreamPort: number, contextWindow: number): Promise { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + return startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "claude-small": { context: contextWindow } } } }, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + compat: { roles: {} }, streamErrorShape: "protocol", passthroughSource: null, autoRestartOnUpdate: false, updateTag: "latest", advisoryCheck: false, releaseNotesCheck: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); +} + +test("#3918: budget exhaustion WITH committed folds → 502 retryable=true + progress note (not terminal)", async () => { + // First two summarization draws are usable (two real folds land), every + // later draw is unusable: the walk burns the rest of the budget on failed + // ranges while the payload stays over-window — the incident's shape. + // Window 100K vs ~230K raw payload: even generous two-fold savings leave + // it well over-window, and the deficit stays below the remaining foldable + // pool so the #1841 futility bail never pre-empts the budget. + const { server: upstream, calls } = makeUpstream((idx) => idx <= 2); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + const proxy = await startProxy(upstreamPort, 100_000); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + + try { + const r = await fetch(`http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "issue3918-progress-sess" }, + body: JSON.stringify({ model: "claude-small", max_tokens: 1024, stream: true, messages: conversation(200, "t3918-prog") }), + }); + assert.equal(r.status, 502, "still over-window after the budget → fail-fast 502"); + const json = JSON.parse(await r.text()) as { error?: { code?: string; retryable?: boolean; message?: string } }; + assert.equal(json.error?.code, "preflight_compress_failed"); + assert.equal(json.error?.retryable, true, `progress persists → the next attempt continues, so the verdict must be retryable (got: ${json.error?.retryable})`); + assert.match(json.error?.message ?? "", /summarization budget/i, `the budget variant is reported (got: ${json.error?.message})`); + assert.match(json.error?.message ?? "", /smaller baseline/i, `the progress note says the next attempt continues (got: ${json.error?.message})`); + assert.equal(calls.filter((c) => c.stream).length, 0, "the over-window payload was NOT forwarded"); + + const s = listSessions().find((x) => x.id === "issue3918-progress-sess") ?? listSessions()[0]; + const activeBlocks = (s?.state.blocks ?? []).filter((b) => b.active); + assert.ok(activeBlocks.length >= 1, `the committed folds persist in the session (got ${activeBlocks.length} active block(s))`); + } finally { + proxy.close(); + proxy.closeIdleConnections?.(); + await once(proxy, "close"); + upstream.close(); + upstream.closeIdleConnections?.(); + await once(upstream, "close"); + } +}); + +test("#3918 negative direction: budget exhaustion with ZERO progress stays terminal (retryable=false, no progress note)", async () => { + // Every draw unusable → nothing folds → today's terminal verdict stands. + // Window 230K reuses the proven sizing of + // tests/preflight-multi-range.test.ts (deficit far below the foldable + // pool, so the budget — not the futility bail — is the binding bound). + const { server: upstream, calls } = makeUpstream(() => false); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + const proxy = await startProxy(upstreamPort, 230_000); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + + try { + const r = await fetch(`http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "issue3918-noprogress-sess" }, + body: JSON.stringify({ model: "claude-small", max_tokens: 1024, stream: true, messages: conversation(200, "t3918-none") }), + }); + assert.equal(r.status, 502, "nothing folded → fail-fast 502"); + const json = JSON.parse(await r.text()) as { error?: { code?: string; retryable?: boolean; message?: string } }; + assert.equal(json.error?.code, "preflight_compress_failed"); + assert.equal(json.error?.retryable, false, `zero progress → retries are genuinely doomed (got: ${json.error?.retryable})`); + assert.match(json.error?.message ?? "", /summarization budget/i, `the budget variant is reported (got: ${json.error?.message})`); + assert.doesNotMatch(json.error?.message ?? "", /smaller baseline/, `no progress note when nothing was folded (got: ${json.error?.message})`); + assert.ok(calls.filter((c) => !c.stream).length >= 2, `the walk ran before giving up (got ${calls.filter((c) => !c.stream).length} summary call(s))`); + assert.equal(calls.filter((c) => c.stream).length, 0, "the over-window payload was NOT forwarded"); + + const s = listSessions().find((x) => x.id === "issue3918-noprogress-sess") ?? listSessions()[0]; + const activeBlocks = (s?.state.blocks ?? []).filter((b) => b.active); + assert.equal(activeBlocks.length, 0, "no blocks created when nothing could be compressed"); + } finally { + proxy.close(); + proxy.closeIdleConnections?.(); + await once(proxy, "close"); + upstream.close(); + upstream.closeIdleConnections?.(); + await once(upstream, "close"); + } +}); From be9b299eeb5fa613e6075e4723865a03bfce91ff Mon Sep 17 00:00:00 2001 From: awork Date: Tue, 20 Oct 2026 17:27:50 +0800 Subject: [PATCH 3/4] fix: keep ResidentPressure module-internal (unused-exports gate) The interface is only consumed as the inferred return type of residentPressure() across modules; no importer names it. Dropping the export satisfies the unused-exports CI gate without a waiver. --- src/session.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.ts b/src/session.ts index a01483459a..8a0b5d1e89 100644 --- a/src/session.ts +++ b/src/session.ts @@ -968,7 +968,7 @@ function residentBytes(): number { * session ping-ponged against an 87.5 MiB peer under the 512 MiB default. * Returns the measured pressure so the refusal receipt says so plainly instead * of promising a heal that will not come. */ -export interface ResidentPressure { +interface ResidentPressure { /** True when retrying cannot restore a usable view under the CURRENT pool state. */ permanent: boolean; /** Last-known LIVE weight of this session (eviction-time measurement; falls From cafb493919de4ac4cc2592af487a67cd0b70f32d Mon Sep 17 00:00:00 2001 From: awork Date: Tue, 20 Oct 2026 17:51:20 +0800 Subject: [PATCH 4/4] fix: preflight progress note measures the shortfall against the target wall, not the window (#3918) Review fix-up for the new ffDetail sentence: limit can be window minus completion reservation (#3450), so naming the gap "short of the window" would repeat the #3663 confusion class in a brand-new string. Uses the subsystem's own term ("gap to the target", src/preflight.ts). --- src/server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server.ts b/src/server.ts index bc0f9594f2..b28eb44ff9 100644 --- a/src/server.ts +++ b/src/server.ts @@ -3647,7 +3647,7 @@ export async function preflightCompressIfNeeded( let ffDetail = f?.detail ?? "the payload still exceeds the window after preflight compression"; if (progressContinues) { const shortfall = foldedCalibratedSize !== undefined ? Math.max(0, limit - foldedCalibratedSize) : undefined; - ffDetail += ` ${result.compressedRanges} range(s) folded this turn persist in the session — the next attempt starts from a smaller baseline and continues the fold${shortfall !== undefined ? ` (~${shortfall.toLocaleString("en-US")} tokens short of the window)` : ""}.`; + ffDetail += ` ${result.compressedRanges} range(s) folded this turn persist in the session — the next attempt starts from a smaller baseline and continues the fold${shortfall !== undefined ? ` (~${shortfall.toLocaleString("en-US")} tokens short of the target)` : ""}.`; } const ff = failFast(status, ffDetail, retryable, result.compressedRanges > 0 ? foldedCalibratedSize : undefined, result.rangesRemaining); const contentDeadEnd = f?.kind === "exhausted" || (f?.kind === "upstream" && f.status !== undefined && f.status >= 400 && f.status < 500 && !retryable);