diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4bd57bf68f..bef6191fb7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -57,6 +57,8 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag
- **中断后/usage 丢失后的「幻影紧急压缩」:本轮拿不到计费样本时,压力档压缩延后一轮再决定**:中断的流(用户 ESC)或上游短 TTL 缓存把 usage 报告弄丢时,本轮的「计量表针」只能落回字符级本地估算——它量的是即将被改写丢掉的入站字节,实测比真实计费高约 3 倍(0.1.212 当天两起真实现场:ESC 的会话从 9478 跳成 228376、长跑会话被连续两轮注入 153% 幻影压力带,徒劳改写并烧掉前缀缓存)。此前这种坏表针直接按 EMERGENCY/OVER-LIMIT 扣扳机。现在:内核收到以「估算值」计量的压力带读数(宿主标记 tokenCountSource="estimate")时,本轮压力档注压延后一轮——下一次请求必然带着新鲜计费报告,拿真数再决定,代价≈0;计费级读数("usage")与不传 source 的老调用方字节不变,非压力档(增长/节拍)完全不受影响,硬溢出兜底臂全程照旧武装。宿主侧提前广告臂(emergencyNudge)同步镜像该位,不会拿着同一个坏读数抢跑。逃生阀:`compress.requireUsageSampleForPressure: false` 恢复旧的估算驱动行为。tests:kernel/tests/nudge-pressure-no-sample-hold.test.ts(6 例)、tests/nudge-pressure-no-sample-armed.test.ts(3 例);kernel 既有套件 1582/1582 绿。
- **External-summary mode no longer reports protected or already-folded ranges as failed compression.** An unchanged kernel preview now returns the normal idempotent no-op or kernel-error receipt without a paid summarizer call; validated internal probe summaries prevent an irrelevant submitted-hint length from blocking that receipt. Probe-created blocks with no recoverable content still refuse rather than folding an unreviewed hint. Regression: `tests/external-summary-runtime.test.ts` (24/24).
+
+* **dsh 车道的输出预算被「被拒压缩回放的字节数」踩死在 4096 地板、整会话不恢复(#5596)**:#1729 守卫拒发 dsh 的原生压缩回放时会把字节数记成会话级粘性高水位(`dshCompactionRefusedBytes`),#2490 规划下限把它换算成输出预算的规划下限(#3384 门控后在「请求 ≥ 25% 窗口或视图已超 headroom」时生效)——但被拒的字节**从未发出**,不是未来输入。高水位粘住会话生命周期内最大的一次回放,而请求默认 ≥ 25% 窗口的客户端(恰是常见情形)整条车道被踩死:真实事故 256K 窗口/128384 请求下 866760 字节被拒回放 → floor 216690 → cap 1172 → wire max_tokens=4096(#3071 地板),模型在恰好 4096 output tokens 处句中截断,直到泳道结束。修复 = clamp 只按**本请求实测输入**规划(溢出防御本来就是逐请求的:preflight/egress fuse #4079/#4185);记录保留为诊断水印(acp_status 单列 `DSH COMPACTION REPLAYS REFUSED` 行,明标不参与规划)。行为变更(旧→新):有拒绝记录的车道上,max_tokens 从地板踩死的值(如 4096/62797)回到实测输入决定的诚实值(至多到请求值);#2490 怪物回放形状(floor≈900K/窗口 1M)此前预算被压到 55K,现在跟随实测值——真实溢出由 preflight/fuse 处理,不再静默写出。tests/issue5596-refusal-floor-no-planning.test.ts(事故形状双向钉 + e2e:拒绝记账后下一回合 30K 预算原样通过);既有 pins issue2490/issue3384/issue3071/issue3148 改锚到新契约。
* **#5412: 异步存储后端的「压缩冻结会话」现在也能自愈。** #5408 事故(一个会话连续 17 次摘要漂移后被保护机制冻住、8 小时无人知晓)的主修复 #5409 只教了同步存储路径(SQLite 直写)自救:漂移挂起期间保存会自动升级为全量重导入并清掉索引里的残留行。异步路径(opt-in 网络后端,如 mysql)那份孪生代码没搬——同样被冻住的会话在那上面永远救不回来,每次保存都重写不完整视图、下轮采样继续漂移。本 PR 在 `src/storage/shadow.ts` 的异步入队路径补上同一行对齐(`completeLog = hasRawTail || driftedSessions.has(id)`),行为与同步路径逐字一致;回归测试同时钉住正门(latch 挂起的折叠保存清掉外来行)与负门(未挂起保存不清扫)。
- **中断频繁的会话压缩提醒几乎不触发,积到几十万 token 才压一次(#5415,0.1.210 引入)**:#5293 的修复把每次用户中断后的增长参照点直接抬到当前读数,连「中断前已实测的真实增长」也一起清零;高频中断的客户端(实测一份日志 4.5 小时 85 次中断)永远攒不到增长台阶,30 万可压内容 idle 数小时,最后一次性压 33 万。现在吸收时把被清零的实测增长记入「欠账」(`nudge.absorbedDebt`)并累计吸收次数(`nudge.absorbCount`),两者都只在**第 2 次起**的中断后生效:单次中断字节级保持 #5293 的安静(不打扰用户刚改的主意——欠账再多也不当场弹提醒);第二次中断才证明会话是「高频中断型」而非「改主意」,欠账从此与新增量一起计入增长台阶,恢复 209 时代的节奏。另加「大积压排水」兑底:欠账>0 且可压量≥2×阈值且当前无未消化提醒时不再等增长台阶(与 #194 同哲学);无欠账/单次中断的会话决策字节不变。压缩成功时欠账与计数随其它基线一起清零;idle 日志同步显示 `growth X + absorbed Y across N aborts < floor Z`(单次中断显示 `held (single abort, #5293)`)便于排查。tests:kernel/tests/issue5415-absorb-debt-nudge.test.ts(欠账付台阶/单次中断钉死不弹/排水兑底/忽略提醒不重发/无欠账字节不变/压缩后清账+清计数);tests/issue5415-absorb-debt.test.ts(吸收记账算术、欠账+计数、累次累计、参照点已越过 anchor 不记账也计数、陈旧 flag 丢弃)。
- **长会话每回合把 Node 主线程卡死十几秒、网页面板冻结(#5337)**:每回合的压缩筛查把历史切成若干可折叠单元,逐个跑「能否折叠」判定——而每次判定都从零重扫全量消息数组(id 查找无索引,五处线性 `.find()`/`includes`),还重复重算全套整史统计(受保护/过期工具调用集合、工具配对表、末尾锚点、完整性预扫)。总工作量≈单元数×全史长度=历史规模平方级,且同步跑在主线程:2.3万条核心消息实测单回合约17秒(事件循环被占死,请求排队;#5304 同签名报告)。修复=把这批 O(n) 推导提升为每轮筛查**一次**构建(`buildProjectionShared`)、共享给同一轮所有单元;id→序号一次性建表替五处线性查找;「已丢弃」成员判断从 includes-in-some 改为 Set。判定逻辑本身一字未动(#3236 单一判定纪律),只是输入到达判定的方式变了。同机同语料实测:2万条消息完整推荐链 9028ms→约572ms(≈16×),回落到线性量级。单次调用路径(apply/report)保持原有内联计算,输出逐字节不变。kernel v0.0.175。tests/issue5337-shared-screening.test.ts(共享/非共享投影逐字段等价+降级边界一致+n=20000 耗时上界钉);既有 issue3406 曲线钉与 #3236 一致性回归不变。
@@ -236,7 +238,7 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag
### Changes
-- **bili-authored dsh client compaction: the #1729 guard can now ANSWER instead of refuse (#3733, fixes #3723's growth loop).** dsh's own compaction was refused locally since #1729 because a landed dsh checkpoint durably shadows the raw history; post-#3644 that substrate concern is gone (the server snapshot is the base), but a native checkpoint is still a foreign summary and — worse — with the refusal standing, dsh's context gauge only grows while its session file balloons unboundedly (#3723: a 39.7 MB / 9001-message file choked the desktop UI). With `BILI_DSH_AUTHORED_COMPACTION=1` (env-only rollout window, read per-request), when the guard fires AND the session holds an active fold block, bili answers the compaction summarize call LOCALLY — zero egress, the autoModeForge discipline — with OUR folded snapshot as the payload: one identity header line `` plus every active block's summary in the kernel render format. dsh records that as its checkpoint, so the client file shrinks while the recorded content is byte-identical to what the upstream already reads — client display, model context and server state finally agree. The identity tag rides in the message text, so replay recognition is declarative (`carriesBiliSnapshotCheckpoint`, the #2621 carrier+novelty discipline): the #2432 rebase handles the landing exactly like a foreign checkpoint but records `dshAuthoredCheckpointLanded` and logs info instead of warn. Precedence: an explicit `allowDshCompaction` (forward natively) still wins; no active blocks → falls through to the same 422; the passthrough-lane guard (no session) is untouched. Originals stay recoverable via CCR/export/fork; `dshCompactionRefusedBytes` keeps recording the raw-ledger high-water on authored answers too, so the #2490 output-clamp regimes keep planning against the raw scale.
+- **bili-authored dsh client compaction: the #1729 guard can now ANSWER instead of refuse (#3733, fixes #3723's growth loop).** dsh's own compaction was refused locally since #1729 because a landed dsh checkpoint durably shadows the raw history; post-#3644 that substrate concern is gone (the server snapshot is the base), but a native checkpoint is still a foreign summary and — worse — with the refusal standing, dsh's context gauge only grows while its session file balloons unboundedly (#3723: a 39.7 MB / 9001-message file choked the desktop UI). With `BILI_DSH_AUTHORED_COMPACTION=1` (env-only rollout window, read per-request), when the guard fires AND the session holds an active fold block, bili answers the compaction summarize call LOCALLY — zero egress, the autoModeForge discipline — with OUR folded snapshot as the payload: one identity header line `` plus every active block's summary in the kernel render format. dsh records that as its checkpoint, so the client file shrinks while the recorded content is byte-identical to what the upstream already reads — client display, model context and server state finally agree. The identity tag rides in the message text, so replay recognition is declarative (`carriesBiliSnapshotCheckpoint`, the #2621 carrier+novelty discipline): the #2432 rebase handles the landing exactly like a foreign checkpoint but records `dshAuthoredCheckpointLanded` and logs info instead of warn. Precedence: an explicit `allowDshCompaction` (forward natively) still wins; no active blocks → falls through to the same 422; the passthrough-lane guard (no session) is untouched. Originals stay recoverable via CCR/export/fork; `dshCompactionRefusedBytes` kept recording the raw-ledger high-water on authored answers too so the #2490 output clamp could plan against the raw scale (that planning consumer was removed in #5596 — the recording is now a diagnostic watermark only, no longer fed to the budget clamp).
- **存储收尾六修+缺省模式翻 full(#3818,修复 #3135)**:`storage.unified` 缺省从 shadow 翻为 full——shadow 干跑自 #2815 起对账干净,索引成为默认加载路径;JSON 信封照写,legacy 仍是一行逃生口。同车修六项:①writerOff 存档在边车 3-strike 自禁用的当拍立即补写 JSON 信封(消除「两边都没有」窗口);②BILI_PERSIST=0/持久化关闭视为有意退出(banner 不再假报降级);③存档印章改为实例真实印章(边车开不了/自禁用→盖 legacy,不再把请求模式盖到只存在 JSON 上的记录上);④verifyDrift 计数上 /__bili/status wire,漂移会话回退信封供数直到下次采样干净;⑤自举缓存损坏(Node 大版本升级等)从「警告并永不再试」改为删缓存重下一次钉死下载(sha512 保证重试安全);⑥有界 boot 并入纯索引会话(jsonWriter=off 用户的会话重启后可枚举;并入仍受 maxSessions+解码字节双预算约束,预算口径=序列化记录字节,与文件路径 #2803 同律)。加保:索引无法忠实表达的记录(blockContents 带孤儿 id、state.blocks 无对应块——手搓档案/web seeds 形状)跳过镜像与 viewFor 重建,永远从 JSON 信封供数(orphanSkips 计数可见)——修掉该形状首载后 contents 静默丢失。真实 #2695 收割形状(块从 state.blocks 移除、原文保留给 decompress 兜底)同样命中该守卫并被钉为『永不入索引』语义;writerOff 下该形状触发安全阀强制落一份 JSON 信封(索引跳过+无文件=零副本死亡的窗口不存在)。
- **`bili claude` stops leaking a proxy and a CA into the child it spawns.** The claude lane routes over a plain http loopback URL (`ANTHROPIC_BASE_URL` → `/bili/…`), so there is no TLS hop for a proxy to tunnel and no certificate for `NODE_EXTRA_CA_CERTS` to vouch for. What the bili-owned `HTTPS_PROXY` did there depended on the client build — older ones ignored it, Claude Code ≥ 2.1.301 honours it and the `/bili/` leg self-tunnelled (#3446) — and in every case the variable was inherited by each process the child spawned, so in-session `curl`/`git`/`npm` issued CONNECT through bili's MITM, logging CONNECT destinations the compression pipeline never sees. Both are gone from the claude env; a user's own proxy variables now ride through untouched, and a child that has none of its own is handed the proxy THIS instance resolves for auxiliary egress (file-config `upstreamProxy`, or the shell proxy the launcher forwards as `BILI_INHERITED_*`) via a new `auxProxy` field on `/__bili/health` — so egress survives without bili sitting in the child's path. Keeping the `/bili/` leg direct when the child DOES carry a proxy is the `NO_PROXY` loopback merge in `buildClientEnv` (#3446), which holds whatever proxy the child brings. **Attaching to a proxy instance started by an older build** reports no `auxProxy`, and then nothing is handed over rather than guessing — restart the proxy on this build if your only proxy comes from the config file.
diff --git a/src/acp-status.ts b/src/acp-status.ts
index dd474184c7..e3638fb563 100644
--- a/src/acp-status.ts
+++ b/src/acp-status.ts
@@ -428,9 +428,13 @@ export function handleAcpStatus(args: Record, ctx: AcpStatusCtx
}
const ledgerFloor = dshLedgerFloorTokens(ctx.session.metadata);
const lastClamp = ctx.session.stats.lastOutputClamp;
- if (ledgerFloor > 0 || lastClamp !== undefined) {
+ if (lastClamp !== undefined) {
extra.push("");
- extra.push(`OUTPUT BUDGET CLAMP —${lastClamp !== undefined ? ` last: max_tokens ${lastClamp.raw} -> ${lastClamp.capped} (window ${lastClamp.window}, input~${Math.round(lastClamp.planningInput)}) @ ${new Date(lastClamp.at).toISOString()}` : ""} · planning floor ${ledgerFloor} tok from the refused dsh compaction replay (#2490, sticky for the session lifetime). Only compression can recover the input; this clamp guarantees native-window fit.`);
+ extra.push(`OUTPUT BUDGET CLAMP — last: max_tokens ${lastClamp.raw} -> ${lastClamp.capped} (window ${lastClamp.window}, input~${Math.round(lastClamp.planningInput)}) @ ${new Date(lastClamp.at).toISOString()}. Only compression can recover the input; this clamp guarantees native-window fit.`);
+ }
+ if (ledgerFloor > 0) {
+ extra.push("");
+ extra.push(`DSH COMPACTION REPLAYS REFUSED — raw-ledger high-water ${ledgerFloor} tok (recorded by the #1729 guard; diagnostic only — refusals never drive the output budget, #5596).`);
}
// #1097: the processTurn above already resolved the envelope when armed
// (contentStoreOf is idempotent); when disarmed skip the disk read.
diff --git a/src/server/budget.ts b/src/server/budget.ts
index 06275265f2..1d0d7b0d41 100644
--- a/src/server/budget.ts
+++ b/src/server/budget.ts
@@ -87,20 +87,6 @@ const OUTPUT_CLAMP_ESTIMATE_SAFETY = 1.12;
const OUTPUT_CLAMP_FLOOR = 1024;
// #3071/#2490: capability floor for the clamped budget — see clampOutputBudget.
const OUTPUT_CLAMP_MIN_OUTPUT = 4096;
-// #3384: giant-request line for applying the refused-dsh-ledger planning floor
-// (#2490). A requested output budget at or above this fraction of the native
-// window can push a near-wall raw ledger past the window in ONE turn — the
-// #2490 shape (384K requested against a 1M window / ~900K ledger produced the
-// 7.5MB un-foldable turn). Below the line no single-turn output can overflow
-// the ledger, so applying the floor there only reserves output budget against
-// bytes bili never sends (its folds keep the wire small while the client's raw
-// ledger grows because compaction is refused) — the #3384 starvation: folded
-// wire at 8–11% of a 1M window, budget clamped 50670 -> 5909, every byte eaten
-// by thinking on an onlyReasoning model -> stopReason=length -> blank turns.
-// Aligned with the #896 default outputHeadroomMaxPct (0.25) and the #2096 note
-// below ("when max_tokens > 25% of the window the overflow boundary lies BELOW
-// the headroom target").
-const OUTPUT_LEDGER_FLOOR_GIANT_REQUEST_PCT = 0.25;
// #453 mitigation: host-side escalation line. The kernel already force-injects
// every turn once usage >= nudge.maxContextLimitPct (its pressure branch has no
// cadence gate), so the only cadence-silent zone is BELOW that line. Force the
@@ -328,13 +314,13 @@ export function estimateWireOverhead(protocol: "anthropic" | "openai" | "respons
* undefined when no reduction is needed (requested already fits, or the cap
* drops below OUTPUT_CLAMP_FLOOR — i.e. input alone nearly fills the window,
* which is preflight/self-heal territory, not output starvation).
- * #2490 ledger regime: when the planning basis is the refused dsh raw ledger
- * (dshLedgerFloorTokens), cap < 1024 means the HOST's own counter already
- * sits at the window wall — bili's folds cannot shrink it (refused
- * checkpoints never land), so no output budget recovers the session; a
- * sub-floor clamp would only starve a terminal turn. That surrender is
- * deliberate, not a gap (pinned in
- * tests/issue2490-ledger-floor-doomed-probe.test.ts).
+ * #5596: the planning basis is the MEASURED input of this request only. The
+ * refused-dsh-replay ledger floor that #2490 used to feed it was dropped:
+ * those bytes are refused and never forwarded, so they are not a future
+ * input — planning against them pinned healthy lanes onto the 4096 capability
+ * floor for the session's life (the #5596 field incident). Future requests
+ * measure their own input; un-foldable overflow is owned by the per-request
+ * defenses (preflight / egress fuse, #4079/#4185).
* #3071: where a clamp DOES fire (cap >= OUTPUT_CLAMP_FLOOR), the 5% margin drags
* cap into the low thousands near the window (observed 1479/2623), starving the
* reply to an empty finish=length turn even though raw headroom still fits a normal
@@ -374,7 +360,7 @@ export function clampOutputBudget(requested: number, inputEstimate: number, nati
if (safe !== undefined) return safe;
// The inflated estimate dropped the cap below the capability floor (input alone
// nearly fills the window — preflight/self-heal territory) while the raw formula
- // would still clamp (the #2490/#3071 pinned ledger shapes). Re-run on the raw
+ // would still clamp (the #3071 pinned near-wall shapes). Re-run on the raw
// estimate so we never surrender a clamp the raw formula applies — those regimes
// stay byte-identical. Every other safe-path no-op implies the same no-op on the
// raw path: a looser estimate only raises every term.
@@ -382,22 +368,19 @@ export function clampOutputBudget(requested: number, inputEstimate: number, nati
}
/** #2490: translate the refused dsh compaction replay size (recorded on the
- * session by the guard in handle.ts) into a token floor for the clamp's
- * planning basis. bytes/4 mirrors the default estimator's chars/4 caliber
- * (the replay is JSON text riding the same wires). 0 when nothing was
- * recorded — legacy behavior.
+ * session by the guard in handle.ts) into a token count. bytes/4 mirrors the
+ * default estimator's chars/4 caliber (the replay is JSON text riding the
+ * same wires). 0 when nothing was recorded — legacy behavior.
* STICKY BY DESIGN: the guard records a high-water mark and nothing decays
- * or clears it while the session lives. While compaction stays refused the
- * raw ledger only grows (refused checkpoints never land), so the max bounds
- * every future re-send; decaying the floor would re-open the #2490 kill
- * chain. Lifetime ends with the session (metadata eviction). Known
- * conservative residue: if a checkpoint lands anyway (direct-bypass path;
- * the #2028 opt-out was removed, #3666) and shrinks the ledger, the floor
- * stays at its old high until eviction — fail-closed over-tightening,
- * accepted.
- * Note (#3384): recording stays sticky; what changed is APPLICATION — the
- * clamp gates where this floor may drive planning (see clampOutgoingOutput),
- * so a stale high-water mark can no longer starve a healthy session. */
+ * or clears it while the session lives. It is a DIAGNOSTIC watermark of the
+ * host's raw-ledger scale (acp_status, refusal-storm logs), not a planning
+ * input: #5596 removed the one consumer that treated it as such — the output
+ * clamp now plans against the measured input of each request, because the
+ * refused bytes never leave bili's door and therefore are never a future
+ * forwarded input (the #2490 floor built on them pinned healthy lanes onto
+ * the 4096 budget floor for the session's life; #3384's gate did not catch
+ * it because the giant-request ground passes for any max_tokens >= 25% of
+ * the window). Lifetime ends with the session (metadata eviction). */
export function dshLedgerFloorTokens(metadata: Record | undefined | null): number {
const bytes = metadata?.["dshCompactionRefusedBytes"];
if (typeof bytes !== "number" || !Number.isFinite(bytes) || bytes <= 0) return 0;
@@ -517,19 +500,6 @@ export function clampOutgoingOutput(
* window the overflow boundary lies BELOW the headroom target, so capping
* against it would no-op exactly when post-compression turns need the
* guarantee most (#453). */ headroomWindow?: number;
- /** #2490: floor for the PLANNING input when the host (dsh native compaction)
- * keeps a raw ledger bili's folded view cannot see. The guard's refusals
- * record the replayed envelope size on the session; that ledger will be
- * re-sent whole after this turn (and after every turn — the compaction
- * never lands), so a turn planned only against the folded input invites
- * output-sized overhang the next raw replay cannot survive. Raising the
- * planning basis only ever tightens the cap (fail-closed direction).
- * #3384: APPLIED ONLY WHEN GATED — the floor engages iff the folded view
- * already sits at the enforced headroom target OR the requested budget is
- * giant (>= OUTPUT_LEDGER_FLOOR_GIANT_REQUEST_PCT of the window); below
- * both lines it would reserve output budget against bytes never sent and
- * starve healthy sessions (see clampOutgoingOutput). */
- ledgerFloorTokens?: number;
/** #2122/#1933 F1: per-route estimator calibration — scales the local-est
* side of estimateInputTokens so the clamp judges the payload on the same
* provider-billed scale as preflight/nudge. Absent → legacy raw behavior. */ kFactor?: number; kOrigin?: string; origin?: string;
@@ -543,7 +513,7 @@ export function clampOutgoingOutput(
/** #3148: optional visibility hook — invoked with the exact clamp event when
* a cap is applied (callers record it on session.stats for acp_status).
* Never affects the wire decision. */
- onClamped?: (info: { raw: number; capped: number; window: number; planningInput: number; ledgerFloor: number }) => void,
+ onClamped?: (info: { raw: number; capped: number; window: number; planningInput: number }) => void,
): void {
const raw = readOutputBudget(rebuilt, field);
if (typeof raw !== "number") return;
@@ -554,38 +524,23 @@ export function clampOutgoingOutput(
// reserve) caliber as preflight/metering instead of double-counting every image.
const loadedToolTokens = field === "max_output_tokens" ? countLoadedToolTokens(rebuilt) : 0;
const inputEstimate = estimateInputTokens(ctx.processedMessages, ctx.systemText, ctx.tools, ctx.lastInputTokens, ctx.lastInputTokensSource, ctx.kFactor, ctx.kOrigin, ctx.origin, loadedToolTokens, ctx.imageTokens);
- // #2490: judge headroom against the larger of the folded view and the refused
- // raw ledger — see ctx.ledgerFloorTokens. Keeps the shape of estimateInputTokens'
- // internal max() (usage baseline never scales DOWN), so all three calibers stay
- // conservative in the same direction.
- const ledgerFloor = typeof ctx.ledgerFloorTokens === "number" && Number.isFinite(ctx.ledgerFloorTokens) ? Math.max(0, Math.floor(ctx.ledgerFloorTokens)) : 0;
- // #3384: the floor only applies where it can still defend something — two
- // independent grounds, either suffices:
- // - viewAtEnforcedTarget: bili's OWN folded view has reached the
- // headroom-adjusted target nudge/preflight enforce — the session really
- // is at the wall, so planning against the worst-case ledger is right;
- // - giantRequestedBudget: the requested output alone can push a near-wall
- // ledger past the window in one turn (the #2490 shape).
- // Otherwise the floor is pure over-reservation: the client's raw ledger only
- // grows because bili refuses compaction, while the wire bili actually sends
- // stays small via its folds — reserving output budget against bytes never
- // sent starves the session's own replies (#3384: folded wire 8–11% of a 1M
- // window, budget clamped to 5909, all eaten by thinking -> blank turns).
- // Both pinned protection regimes survive: the #2490 incident request (384K
- // >= 25% of 1M) takes giantRequestedBudget; near-wall sessions take
- // viewAtEnforcedTarget.
- const viewAtEnforcedTarget = typeof ctx.headroomWindow === "number" && Number.isFinite(ctx.headroomWindow) && ctx.headroomWindow > 0 && inputEstimate >= ctx.headroomWindow;
- const giantRequestedBudget = ctx.nativeWindow > 0 && raw >= OUTPUT_LEDGER_FLOOR_GIANT_REQUEST_PCT * ctx.nativeWindow;
- const planningInput = viewAtEnforcedTarget || giantRequestedBudget ? Math.max(inputEstimate, ledgerFloor) : inputEstimate;
- const capped = clampOutputBudget(raw, planningInput, ctx.nativeWindow, ctx.knownOutputCeiling);
+ // #5596: plan against the MEASURED input of THIS request only. The refused
+ // dsh compaction replay size (session.metadata.dshCompactionRefusedBytes,
+ // dshLedgerFloorTokens) used to raise this basis (#2490, gated since
+ // #3384) — but those bytes are refused and never forwarded, so they are
+ // not a future input; the sticky high-water pinned healthy lanes onto the
+ // 4096 capability floor for the session's life (the #5596 field incident).
+ // Future requests measure their own input; un-foldable overflow is owned
+ // by the per-request defenses (preflight / egress fuse, #4079/#4185).
+ const capped = clampOutputBudget(raw, inputEstimate, ctx.nativeWindow, ctx.knownOutputCeiling);
if (capped !== undefined) {
writeOutputBudget(rebuilt, field, capped);
// #2096: in the band (headroom target, native window) the clamp can only
// guarantee native-window fit — preflight/compression is what can pull
// the input back under the enforced target. Say so instead of advertising
// rescue right before the turn dies.
- const overTarget = ctx.headroomWindow !== undefined && ctx.headroomWindow < ctx.nativeWindow && planningInput >= ctx.headroomWindow;
- log("info", `[${sessionId}] output budget clamped ${raw} -> ${capped} (input~${inputEstimate}${ledgerFloor > inputEstimate ? `, planning floor ${ledgerFloor} from the refused dsh compaction replay (#2490)` : ""}, window=${ctx.nativeWindow}); prevents input+output overflow (#453)${overTarget ? `; input already exceeds the headroom-adjusted target ~${ctx.headroomWindow} that nudge/preflight enforce — only compression can recover it, this clamp guarantees native-window fit only (#2096)` : ""}`);
- onClamped?.({ raw, capped, window: ctx.nativeWindow, planningInput, ledgerFloor });
+ const overTarget = ctx.headroomWindow !== undefined && ctx.headroomWindow < ctx.nativeWindow && inputEstimate >= ctx.headroomWindow;
+ log("info", `[${sessionId}] output budget clamped ${raw} -> ${capped} (input~${inputEstimate}, window=${ctx.nativeWindow}); prevents input+output overflow (#453)${overTarget ? `; input already exceeds the headroom-adjusted target ~${ctx.headroomWindow} that nudge/preflight enforce — only compression can recover it, this clamp guarantees native-window fit only (#2096)` : ""}`);
+ onClamped?.({ raw, capped, window: ctx.nativeWindow, planningInput: inputEstimate });
}
}
diff --git a/src/server/handle.ts b/src/server/handle.ts
index 82b4dc21ce..4f4251a120 100644
--- a/src/server/handle.ts
+++ b/src/server/handle.ts
@@ -2654,22 +2654,20 @@ async function handleRequest(
const shapeDrift = inboundMsgs !== null && inboundMsgs > DSH_COMPACTION_SHAPE_MSGS ? `, shape drifted from the ≤${DSH_COMPACTION_SHAPE_MSGS}-msg rc.1 envelope — rc.2 replays the full shadowed region (#2193)` : "";
log("warn", `[${session.id}] dsh native compaction call identified (${identifiedAs}, ${inboundMsgs} msgs${shapeDrift}) — REFUSED, not forwarded: bili owns compression on this lane; a landed dsh checkpoint would durably shadow the raw history (#1729, cf. #1206/#1772)`);
}
- // #2490: remember the replayed envelope SIZE. The host's raw ledger is
- // invisible to bili's folded view (folds never shrink what dsh itself
- // counts), so the next turn's output clamp must plan against the raw
- // scale or a monster output rides a small folded input out the door
- // (the 384K-token/7.5MB turn-37 single-message kill chain). High-water
- // mark, not a sum: the replay is the whole ledger every time, so the
- // max already bounds every future re-send; a sum would compound on each
- // retry. Write-only by design: nothing decays or clears this value —
- // dshLedgerFloorTokens() in budget.ts documents why the mark stays
- // sticky and consumes it.
+ // #2490/#5596: remember the replayed envelope SIZE as a DIAGNOSTIC
+ // watermark of the host's raw-ledger scale (acp_status, storm logs).
+ // High-water mark, not a sum: the replay is the whole ledger every
+ // time, so the max already bounds every future re-send; a sum would
+ // compound on each retry. Write-only by design: nothing decays or
+ // clears this value while the session lives. #5596: no longer consumed
+ // by the output clamp — the refused bytes never leave bili's door, so
+ // they are not a future forwarded input (the #2490 planning floor built
+ // on them pinned healthy lanes onto the 4096 budget floor; budget.ts).
const prevBytes = typeof session.metadata["dshCompactionRefusedBytes"] === "number" ? session.metadata["dshCompactionRefusedBytes"] as number : 0;
session.metadata["dshCompactionRefusedBytes"] = Math.max(prevBytes, inboundBytes);
if (authored !== null) {
- // #3733 counters + the same raw-ledger high-water as a refusal
- // (the envelope still proves the raw scale the output clamp must
- // plan against); NOT counted in dshCompactionRefusals.
+ // #3733 counters + the same diagnostic raw-scale watermark as a
+ // refusal; NOT counted in dshCompactionRefusals.
session.metadata["dshAuthoredCompactions"] = (typeof session.metadata["dshAuthoredCompactions"] === "number" ? session.metadata["dshAuthoredCompactions"] as number : 0) + 1;
markDirty(session);
if (!res.headersSent && !res.writableEnded && !res.destroyed) {
diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts
index d12f08249d..897cae0350 100644
--- a/src/server/prepare-google.ts
+++ b/src/server/prepare-google.ts
@@ -35,7 +35,7 @@ import { reconcileSystemAnchor } from "../system-anchor.js";
import { logRawStripCounts, markerArtifactCount, stripRawStage } from "../marker/engine.js";
import { imageRescaleMaxDim, renderNone as knobRenderNone } from "../knobs.js";
import { applyImageRescalePass } from "../image-rescale.js";
-import { clampOutgoingOutput, countSystemAndToolsTokens, dshLedgerFloorTokens, emergencyNudge, emergencyNudgeArmed } from "./budget.js";
+import { clampOutgoingOutput, countSystemAndToolsTokens, emergencyNudge, emergencyNudgeArmed } from "./budget.js";
import { estimateCoreMessages } from "../preflight.js";
import { effectiveAbsorbBlock, effectiveBlockOpsEnabled } from "./prepare-responses.js";
import { injectGoogleTool, injectTool } from "./inject.js";
@@ -400,7 +400,7 @@ export async function prepareGoogle(
}
const rebuilt: GoogleRequestBody = { ...parsed, contents: rebuiltContents, tools: toolsOut, systemInstruction };
- clampOutgoingOutput(rebuilt as Record, "generationConfig.maxOutputTokens", { systemText: googleClientSystem, tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, lastInputTokensSource: session.stats.lastInputTokensSource, nativeWindow, headroomWindow: config.modelContextLimit, imageTokens: imageReserveFor(session, "google", rebuilt, opts, upstreamOrigin), kFactor: currentCalibrationFactor(session.stats, session.metadata?.lastModel), kOrigin: session.stats.calibratedEstimateOrigin, origin: upstreamOrigin, ledgerFloorTokens: dshLedgerFloorTokens(session.metadata), knownOutputCeiling: resolveKnownOutputCeiling(req.headers, parsed as Record, opts.routes, upstreamOrigin, opts.sessionHeader, model) }, sessionId, log, (c) => { session.stats.lastOutputClamp = { ...c, at: Date.now() }; });
+ clampOutgoingOutput(rebuilt as Record, "generationConfig.maxOutputTokens", { systemText: googleClientSystem, tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, lastInputTokensSource: session.stats.lastInputTokensSource, nativeWindow, headroomWindow: config.modelContextLimit, imageTokens: imageReserveFor(session, "google", rebuilt, opts, upstreamOrigin), kFactor: currentCalibrationFactor(session.stats, session.metadata?.lastModel), kOrigin: session.stats.calibratedEstimateOrigin, origin: upstreamOrigin, knownOutputCeiling: resolveKnownOutputCeiling(req.headers, parsed as Record, opts.routes, upstreamOrigin, opts.sessionHeader, model) }, sessionId, log, (c) => { session.stats.lastOutputClamp = { ...c, at: Date.now() }; });
// #532: title-gen side requests carry their own tiny system — skip them.
if (!isTitleGen && googleOutboundSystem !== undefined) {
session.metadata.systemPromptTokens = countSystemAndToolsTokens(googleOutboundSystem, toolsOut);
diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts
index b69e92d319..218a2cbac8 100644
--- a/src/server/prepare-openai.ts
+++ b/src/server/prepare-openai.ts
@@ -43,7 +43,7 @@ import { reconcileSystemAnchor } from "../system-anchor.js";
import { logRawStripCounts, markerArtifactCount, stripRawStage, type RawStripCounts } from "../marker/engine.js";
import { imageRescaleMaxDim, renderNone as knobRenderNone } from "../knobs.js";
import { applyImageRescalePass } from "../image-rescale.js";
-import { clampOutgoingOutput, countSystemAndToolsTokens, dshLedgerFloorTokens, emergencyNudge, emergencyNudgeArmed, outputBudgetField, readOutputBudget, writeOutputBudget } from "./budget.js";
+import { clampOutgoingOutput, countSystemAndToolsTokens, emergencyNudge, emergencyNudgeArmed, outputBudgetField, readOutputBudget, writeOutputBudget } from "./budget.js";
import { effectiveAbsorbBlock, effectiveBlockOpsEnabled } from "./prepare-responses.js";
import { injectOpenaiTool, injectTool } from "./inject.js";
import { nudgeOn, resolveLaneDecisions, surfaceOffDetail } from "./lane-decisions.js";
@@ -599,7 +599,7 @@ export async function prepareOpenai(
rebuiltMessages = normalizeStrictEchoReasoning(rebuiltMessages, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed)), log, sessionId);
const rebuilt: OpenAIRequestBody = { ...parsed, messages: rebuiltMessages, tools: toolsOut as OpenAITool[] | undefined };
warnReasoningPairs(rebuiltMessages, log, sessionId);
- clampOutgoingOutput(rebuilt as Record, typeof (parsed as Record).max_completion_tokens === "number" ? "max_completion_tokens" : "max_tokens", { systemText: openaiSystemText, tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, lastInputTokensSource: session.stats.lastInputTokensSource, nativeWindow, headroomWindow: config.modelContextLimit, imageTokens: imageReserveFor(session, "openai", rebuilt, opts, billingUpstream ?? upstreamOrigin), kFactor: currentCalibrationFactor(session.stats, session.metadata?.lastModel), kOrigin: session.stats.calibratedEstimateOrigin, origin: billingUpstream ?? upstreamOrigin, ledgerFloorTokens: dshLedgerFloorTokens(session.metadata), knownOutputCeiling: resolveKnownOutputCeiling(req.headers, parsed as Record, opts.routes, billingUpstream ?? upstreamOrigin, opts.sessionHeader, modelIdOf(parsed)) }, sessionId, log, (c) => { session.stats.lastOutputClamp = { ...c, at: Date.now() }; });
+ clampOutgoingOutput(rebuilt as Record, typeof (parsed as Record).max_completion_tokens === "number" ? "max_completion_tokens" : "max_tokens", { systemText: openaiSystemText, tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, lastInputTokensSource: session.stats.lastInputTokensSource, nativeWindow, headroomWindow: config.modelContextLimit, imageTokens: imageReserveFor(session, "openai", rebuilt, opts, billingUpstream ?? upstreamOrigin), kFactor: currentCalibrationFactor(session.stats, session.metadata?.lastModel), kOrigin: session.stats.calibratedEstimateOrigin, origin: billingUpstream ?? upstreamOrigin, knownOutputCeiling: resolveKnownOutputCeiling(req.headers, parsed as Record, opts.routes, billingUpstream ?? upstreamOrigin, opts.sessionHeader, modelIdOf(parsed)) }, sessionId, log, (c) => { session.stats.lastOutputClamp = { ...c, at: Date.now() }; });
// prompt_cache_retention is an OpenAI-host-only cache directive; the dsh
// launcher forces PI_CACHE_RETENTION=long (for the session-id
// prompt_cache_key) which makes the client also emit it. Third-party
diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts
index 94e16bfb2f..a160575bf7 100644
--- a/src/server/prepare-responses.ts
+++ b/src/server/prepare-responses.ts
@@ -46,7 +46,7 @@ import { reconcilePluginFoldCallIds } from "../plugin.js";
import { logRawStripCounts, markerArtifactCount, stripRawStage } from "../marker/engine.js";
import { imageRescaleMaxDim, keepResponseId as knobKeepResponseId, noCompressPrompt as knobNoCompressPrompt, noInjectTool as knobNoInjectTool, renderNone as knobRenderNone } from "../knobs.js";
import { applyImageRescaleItems } from "../image-rescale.js";
-import { clampOutgoingOutput, countLoadedToolTokens, countSystemAndToolsTokens, dshLedgerFloorTokens, emergencyNudge, emergencyNudgeArmed, modelVisibleTools } from "./budget.js";
+import { clampOutgoingOutput, countLoadedToolTokens, countSystemAndToolsTokens, emergencyNudge, emergencyNudgeArmed, modelVisibleTools } from "./budget.js";
import { FORCE_TEXT_PROTOCOL, injectResponsesTool, injectTool } from "./inject.js";
import { nudgeOn, resolveLaneDecisions, surfaceOffDetail } from "./lane-decisions.js";
import { pluginAgentHeader } from "../plugin.js";
@@ -652,7 +652,7 @@ reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStr
const rebuilt: ResponsesRequestBody = { ...parsed, input: rebuiltInput, tools: toolsOut };
warnResponsesReasoningPairs(Array.isArray(rebuiltInput) ? rebuiltInput : [], log, sessionId);
if (!isCompactionTrigger) {
- clampOutgoingOutput(rebuilt as Record, "max_output_tokens", { systemText: (responsesProjection?.systemParts ?? []).join("\n"), tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, lastInputTokensSource: session.stats.lastInputTokensSource, nativeWindow, headroomWindow: config.modelContextLimit, imageTokens: imageReserveFor(session, "responses", rebuilt, opts, billingUpstream ?? upstreamOrigin), kFactor: currentCalibrationFactor(session.stats, session.metadata?.lastModel), kOrigin: session.stats.calibratedEstimateOrigin, origin: billingUpstream ?? upstreamOrigin, ledgerFloorTokens: dshLedgerFloorTokens(session.metadata), knownOutputCeiling: resolveKnownOutputCeiling(req.headers, parsed as Record, opts.routes, billingUpstream ?? upstreamOrigin, opts.sessionHeader, modelIdOf(parsed)) }, sessionId, log, (c) => { session.stats.lastOutputClamp = { ...c, at: Date.now() }; });
+ clampOutgoingOutput(rebuilt as Record, "max_output_tokens", { systemText: (responsesProjection?.systemParts ?? []).join("\n"), tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, lastInputTokensSource: session.stats.lastInputTokensSource, nativeWindow, headroomWindow: config.modelContextLimit, imageTokens: imageReserveFor(session, "responses", rebuilt, opts, billingUpstream ?? upstreamOrigin), kFactor: currentCalibrationFactor(session.stats, session.metadata?.lastModel), kOrigin: session.stats.calibratedEstimateOrigin, origin: billingUpstream ?? upstreamOrigin, knownOutputCeiling: resolveKnownOutputCeiling(req.headers, parsed as Record, opts.routes, billingUpstream ?? upstreamOrigin, opts.sessionHeader, modelIdOf(parsed)) }, sessionId, log, (c) => { session.stats.lastOutputClamp = { ...c, at: Date.now() }; });
}
// Route with the upstream THIS request goes to — session.meta.upstreamOrigin
// is first-wins and would keep injecting pck toward a relay we switched
diff --git a/src/session.ts b/src/session.ts
index ca5e7cddf3..9424bdae2b 100644
--- a/src/session.ts
+++ b/src/session.ts
@@ -445,10 +445,9 @@ export type Session = {
* it. Cleared by the next real usage report (settleUsageReport). */
emergencyShrinkAt?: number;
emergencyShrinkEstimate?: number;
- /** #3148: most recent outgoing output-budget clamp event — the #2490
- * planning-floor clamps were previously visible only in log lines.
- * Display-only: no decision path reads it. */
- lastOutputClamp?: { raw: number; capped: number; window: number; planningInput: number; ledgerFloor: number; at: number };
+ /** #3148: most recent outgoing output-budget clamp event — previously
+ * visible only in log lines. Display-only: no decision path reads it. */
+ lastOutputClamp?: { raw: number; capped: number; window: number; planningInput: number; at: number };
/** #1110: one-shot emergency ceiling armed by an upstream context-
* overflow 400 (server.ts overflow handler) — live evidence this
* session cannot exceed that size. Kept SEPARATE from lastInputTokens
diff --git a/tests/issue2490-ledger-floor-doomed-probe.test.ts b/tests/issue2490-ledger-floor-doomed-probe.test.ts
index dc76d6cb04..c28f1a75dd 100644
--- a/tests/issue2490-ledger-floor-doomed-probe.test.ts
+++ b/tests/issue2490-ledger-floor-doomed-probe.test.ts
@@ -1,15 +1,16 @@
// #2490: the dsh-vs-bili double-bookkeeping deadlock had TWO load-bearing
// lies, pinned here end-to-end:
-// (a) the ledger floor — the #1729/#1835 guard refuses dsh's native
-// compaction envelope (a FULL replay of the host's raw history), but
-// that raw ledger stays invisible to bili's folded view, so the output
-// clamp planned turn budgets against the SMALL folded input while the
-// host was about to re-send the whole raw ledger next turn. Incident:
-// window 1M, folded input ~200K, refused replay ~900K tokens → clamp
-// let max_tokens=384_000 through → one 7.5MB assistant turn landed →
-// un-folding kill chain (turn 37→40 death). Fix: the guard records the
-// replay SIZE on the session (high-water bytes + count), and
-// clampOutgoingOutput plans against max(folded estimate, ledger floor).
+// (a) the refusal record — the #1729/#1835 guard refuses dsh's native
+// compaction envelope (a FULL replay of the host's raw history), which
+// stays invisible to bili's folded view. The #2490 fix recorded the
+// replay SIZE on the session (high-water bytes + count) and planned the
+// output budget against it; #5596 removed that planning consumption —
+// refused bytes never leave bili's door, so they are not a future
+// forwarded input, and the sticky high-water pinned healthy lanes onto
+// the 4096 budget floor for the session's life (see
+// tests/issue5596-refusal-floor-no-planning.test.ts). The record stays
+// as a diagnostic watermark; per-request defenses own overflow
+// (measured-input planning + preflight / egress fuse #4079/#4185).
// (b) the doomed probe — the #2313 forward-once-for-evidence bet assumes
// some tokenizer might make the payload fit. Beyond window×7.5 BYTES
// of core text (densest live-route billing observed: ~6.8 B/token,
@@ -169,12 +170,12 @@ async function seedStuckMeter(ctx: Ctx, sessionId: string, lastInputTokens = 7_3
}
// ---------------------------------------------------------------------------
-// (a) ledger floor
+// (a) refusal record
test("unit #2490: dshLedgerFloorTokens — bytes/4 caliber, high-water, legacy-safe", () => {
assert.equal(dshLedgerFloorTokens(undefined), 0, "no metadata bag (partial fixture) is legacy-neutral");
assert.equal(dshLedgerFloorTokens({}), 0, "nothing recorded yet");
- assert.equal(dshLedgerFloorTokens({ dshCompactionRefusedBytes: 3_600_000 }), 900_000, "the incident's 3.6MB replay floors planning at ~900K tokens");
+ assert.equal(dshLedgerFloorTokens({ dshCompactionRefusedBytes: 3_600_000 }), 900_000, "the incident's 3.6MB replay reads back as ~900K tokens");
assert.equal(dshLedgerFloorTokens({ dshCompactionRefusedBytes: 0 }), 0, "zero bytes is not a floor");
assert.equal(dshLedgerFloorTokens({ dshCompactionRefusedBytes: Number.NaN }), 0, "NaN is ignored");
assert.equal(dshLedgerFloorTokens({ dshCompactionRefusedBytes: "big" }), 0, "non-number is ignored");
@@ -196,41 +197,34 @@ const clampCtx = (over: Partial[2]>) =>
const clampLog = (lines: string[]): ((level: string, msg: string) => void) =>
(level, msg) => { if (level === "info" || level === "warn") lines.push(msg); };
-test("unit #2490: the incident clamp — ledger floor 900K vs window 1M cuts max_tokens 384K → 55K", () => {
+test("unit #2490/#5596: refused-ledger bytes no longer drive planning — measured input decides", () => {
const lines: string[] = [];
const rebuilt: Record = { max_tokens: 384_000 };
- // Without the floor (pre-fix): folded input 200K, margin 10K → cap 790K ≥
- // requested → NO clamp. This is the exact permissiveness that let the
- // 7.5MB turn-37 monster out the door.
- const before: Record = { max_tokens: 384_000 };
- clampOutgoingOutput(before, "max_tokens", clampCtx({ ledgerFloorTokens: undefined }), "s1", clampLog(lines));
- assert.equal(before.max_tokens, 384_000, "pre-fix: unclamped while the raw ledger sits at 900K");
+ // Post-#5596: the refused replay (~900K tokens in the incident) is a
+ // diagnostic watermark only; the planning basis is this request's measured
+ // input (200K usage-grade here). safe 224K, margin 11.2K -> cap 764.8K >=
+ // requested, so the 384K budget passes even with a 900K refused replay on
+ // the session. Disclosed tradeoff: the #2490 monster-request shape is no
+ // longer pre-shrunk by the clamp itself — overflow at that size is owned
+ // by the per-request defenses (preflight / egress fuse #4079/#4185).
+ clampOutgoingOutput(rebuilt, "max_tokens", clampCtx({}), "s2", clampLog(lines));
+ assert.equal(rebuilt.max_tokens, 384_000, "refused bytes are not a forwarded input (#5596)");
assert.equal(lines.length, 0, "no clamp, no log");
-
- // With the floor: planning input max(200K, 900K)=900K, margin 45K →
- // cap floor(1M − 900K − 45K) = 55K — the monster budget cannot exist.
- clampOutgoingOutput(rebuilt, "max_tokens", clampCtx({ ledgerFloorTokens: 900_000 }), "s2", clampLog(lines));
- assert.equal(rebuilt.max_tokens, 55_000, "planned against the refused raw ledger, not the folded view");
- assert.match(lines[0] ?? "", /planning floor 900000 from the refused dsh compaction replay \(#2490\)/, "the log says which basis drove the clamp");
});
-test("unit #2490: the floor only ever TIGHTENS — small floors, absent requests, legacy paths unchanged", () => {
- // Floor below the folded input: planning basis stays the folded estimate.
- const small: Record = { max_tokens: 384_000 };
- clampOutgoingOutput(small, "max_tokens", clampCtx({ ledgerFloorTokens: 10_000 }), "s3", clampLog([]));
- assert.equal(small.max_tokens, 384_000, "a ledger smaller than the folded input changes nothing");
-
- // Cap below OUTPUT_CLAMP_FLOOR surrenders to preflight (existing
- // semantics — a 990K ledger against a 1M window is preflight territory).
+test("unit #2490/#5596: measured-input rows — near-wall surrender and legacy paths unchanged", () => {
+ // Measured input 990K against a 1M window: cap < OUTPUT_CLAMP_FLOOR
+ // surrenders to preflight (existing semantics — that regime belongs to
+ // the preflight / egress-fuse layers, not the clamp).
const huge: Record = { max_tokens: 384_000 };
const lines: string[] = [];
- clampOutgoingOutput(huge, "max_tokens", clampCtx({ ledgerFloorTokens: 990_000 }), "s4", clampLog(lines));
+ clampOutgoingOutput(huge, "max_tokens", clampCtx({ lastInputTokens: 990_000 }), "s4", clampLog(lines));
assert.equal(huge.max_tokens, 384_000, "cap < 1024 → no clamp (preflight owns that regime)");
assert.equal(lines.length, 0);
- // No floor at all → byte-identical legacy behavior (frozen by #2122 tests).
+ // Healthy small request → byte-identical legacy behavior (frozen by #2122 tests).
const legacy: Record = { max_tokens: 8_000 };
- clampOutgoingOutput(legacy, "max_tokens", clampCtx({ ledgerFloorTokens: 0 }), "s5", clampLog([]));
+ clampOutgoingOutput(legacy, "max_tokens", clampCtx({}), "s5", clampLog([]));
assert.equal(legacy.max_tokens, 8_000);
});
diff --git a/tests/issue3071-planning-floor-min-output.test.ts b/tests/issue3071-planning-floor-min-output.test.ts
index 73b7e26204..a06d8e324e 100644
--- a/tests/issue3071-planning-floor-min-output.test.ts
+++ b/tests/issue3071-planning-floor-min-output.test.ts
@@ -1,11 +1,13 @@
-// #3071 path 3: the #2490 planning-floor clamp drove the outgoing output budget into
-// the low thousands (observed 1479/2623) whenever input sat near the window — the 5%
-// margin ate most of the raw headroom, starving the reply to an empty finish=length
-// turn. Owner-signed constraint (#3071): floor the clamped budget at min(4096, window
-// - input) so a normal reply always has room, WITHOUT relaxing fail-closed — the
-// sub-floor surrender (cap < OUTPUT_CLAMP_FLOOR, i.e. input+margin already past the
-// window) stays byte-identical, and min(4096, window-input) is always <= window-input
-// so input+output can never exceed the window. The #2490 STICKY pin must stay green.
+// #3071 path 3: a near-window INPUT drove the outgoing output budget into the low
+// thousands (observed 1479/2623) — the 5% margin ate most of the raw headroom,
+// starving the reply to an empty finish=length turn. Owner-signed constraint
+// (#3071): floor the clamped budget at min(4096, window - input) so a normal reply
+// always has room, WITHOUT relaxing fail-closed — the sub-floor surrender
+// (cap < OUTPUT_CLAMP_FLOOR, i.e. input+margin already past the window) stays
+// byte-identical, and min(4096, window-input) is always <= window-input so
+// input+output can never exceed the window. Since #5596 the estimate feeding the
+// clamp is the MEASURED input only (the refused-ledger planning floor is gone),
+// but the capability floor itself is byte-pinned below.
import assert from "node:assert/strict";
import test from "node:test";
@@ -16,14 +18,14 @@ import { clampOutputBudget, clampOutgoingOutput } from "../src/server/budget.ts"
const WINDOW = 262_144; // the reported DSH / ark-code-latest window
test("#3071: planning-floor clamp is floored to 4096, not the low thousands (131072 -> 2623 becomes 4096)", () => {
- // Refused dsh compaction replay floors planning input at 247162 tokens; the naive
- // cap is floor(262144 - 247162 - ceil(247162*0.05)) = 2623. Raw headroom is 14982,
- // which comfortably fits the 4096 capability floor.
+ // Near-window input 247162 tokens (post-#5596 the estimate is the measured
+ // input); the naive cap is floor(262144 - 247162 - ceil(247162*0.05)) = 2623.
+ // Raw headroom is 14982, which comfortably fits the 4096 capability floor.
assert.equal(clampOutputBudget(131_072, 247_162, WINDOW), 4_096);
});
test("#3071: the 1479 case is floored too when the incoming budget exceeds the floor", () => {
- // planning floor 248252 -> naive cap 1479. An incoming 8192 (a restored starved
+ // near-window input 248252 -> naive cap 1479. An incoming 8192 (a restored starved
// budget) is reduced to 4096, not 1479.
assert.equal(clampOutputBudget(8_192, 248_252, WINDOW), 4_096);
});
@@ -38,7 +40,7 @@ test("#3071: the clamp never RAISES — a client budget already under the floor
test("#3071: sub-floor surrender is untouched (fail-closed preserved when input+margin >= window)", () => {
// Mirrors the #2490 unit s4 shape: input+margin already past the window -> no clamp.
assert.equal(clampOutputBudget(384_000, 990_000, 1_000_000), undefined);
- // And the #2490 incident clamp (well above the floor) is unchanged.
+ // And the #2490 incident shape (well above the capability floor) is unchanged.
assert.equal(clampOutputBudget(384_000, 900_000, 1_000_000), 55_000);
});
@@ -55,7 +57,7 @@ test("#3071: a healthy cap well above the floor is unchanged", () => {
assert.equal(clampOutputBudget(200_000, 120_000, WINDOW), 121_024);
});
-test("#3071 (integration): clampOutgoingOutput plans against the refused ledger and floors to 4096", () => {
+test("#3071 (integration): clampOutgoingOutput plans against the measured input and floors to 4096", () => {
const lines: string[] = [];
const log = (level: string, msg: string): void => { if (level === "info" || level === "warn") lines.push(msg); };
const rebuilt: Record = { max_tokens: 131_072 };
@@ -63,14 +65,13 @@ test("#3071 (integration): clampOutgoingOutput plans against the refused ledger
systemText: "",
tools: undefined,
processedMessages: [{ role: "user", text: "hello" }],
- lastInputTokens: 1_000,
+ lastInputTokens: 247_162, // near-window measured input (the #3071 shape; #5596: estimate = measurement only)
lastInputTokensSource: "usage",
nativeWindow: WINDOW,
imageTokens: 0,
- ledgerFloorTokens: 247_162,
} as Parameters[2];
clampOutgoingOutput(rebuilt, "max_tokens", ctx, "s-3071", log);
- assert.equal(rebuilt.max_tokens, 4_096, "planned against the refused raw ledger, floored to the capability minimum");
- assert.match(lines[0] ?? "", /planning floor 247162 from the refused dsh compaction replay \(#2490\)/);
+ assert.equal(rebuilt.max_tokens, 4_096, "naive cap 2623 floored to the 4096 capability minimum");
+ assert.match(lines[0] ?? "", /input~247162/);
assert.match(lines[0] ?? "", /-> 4096/);
});
diff --git a/tests/issue3148-upstream-circuit.test.ts b/tests/issue3148-upstream-circuit.test.ts
index f7c61cd8c4..e3c7793f09 100644
--- a/tests/issue3148-upstream-circuit.test.ts
+++ b/tests/issue3148-upstream-circuit.test.ts
@@ -128,13 +128,12 @@ test("#3148 review: an UNCOUNTED probe failure (timeout/client abort) must not p
test("#3148: clampOutgoingOutput records the exact clamp event without changing the wire decision", () => {
const rebuilt: Record = { max_tokens: 300_000 };
- let seen: { raw: number; capped: number; window: number; planningInput: number; ledgerFloor: number } | undefined;
- // The reporter's numbers (floor 997348, window 1048576 -> margin
- // ceil(997348*0.05)=49868, cap floor(1048576-997348-49868)=1360, lifted to
- // 4096 by #3071's capability floor bounded by raw headroom 51228) are kept;
- // #3384 re-gated the floor, so the request is raised to 300K >=
- // 0.25*1048576=262144 to take the giant-request ground and keep the hook
- // path covered.
+ let seen: { raw: number; capped: number; window: number; planningInput: number } | undefined;
+ // The reporter's numbers (near-window input 997348, window 1048576 ->
+ // margin ceil(997348*0.05)=49868, cap floor(1048576-997348-49868)=1360,
+ // lifted to 4096 by #3071's capability floor bounded by raw headroom 51228)
+ // are kept, driven by the MEASURED input since #5596 (the refused-ledger
+ // floor no longer plans); the 300K request keeps the hook path covered.
clampOutgoingOutput(
rebuilt,
"max_tokens",
@@ -142,23 +141,22 @@ test("#3148: clampOutgoingOutput records the exact clamp event without changing
systemText: "",
tools: [],
processedMessages: [],
- lastInputTokens: 97730,
+ lastInputTokens: 997348,
lastInputTokensSource: "usage",
nativeWindow: 1048576,
imageTokens: 0,
- ledgerFloorTokens: 997348,
},
"t",
() => {},
(info) => { seen = info; },
);
- assert.deepEqual(seen, { raw: 300_000, capped: 4096, window: 1048576, planningInput: 997348, ledgerFloor: 997348 }, "exact clamp event captured");
+ assert.deepEqual(seen, { raw: 300_000, capped: 4096, window: 1048576, planningInput: 997348 }, "exact clamp event captured");
assert.equal(rebuilt.max_tokens, 4096, "wire decision unchanged (same cap as before the hook)");
- // #3384: the OLD expectation here (modest 65536 request against this same
- // floor clamps to 4096) IS the starvation regression — below both gate
- // lines the floor must not drive planning, or a healthy session (folded
- // wire ~9% of the window) loses its output budget to bytes never sent.
+ // The OLD expectation here (modest 65536 request against this same shape
+ // clamps to 4096) IS the #3384 starvation regression — post-#5596 the
+ // refused floor never plans at any request size, so this row holds
+ // trivially; kept as a negative-direction pin.
const modest: Record = { max_tokens: 65_536 };
let modestCalls = 0;
clampOutgoingOutput(
@@ -172,7 +170,6 @@ test("#3148: clampOutgoingOutput records the exact clamp event without changing
lastInputTokensSource: "usage",
nativeWindow: 1048576,
imageTokens: 0,
- ledgerFloorTokens: 997348,
},
"t",
() => {},
@@ -233,7 +230,7 @@ test("#3148: acp_status surfaces the open circuit, armed emergency shrink and th
const session = makeSession({
emergencyShrinkAt: Date.now() - 60_000,
emergencyShrinkEstimate: 699777,
- lastOutputClamp: { raw: 65536, capped: 1360, window: 1048576, planningInput: 997348, ledgerFloor: 997348, at: Date.now() - 30_000 },
+ lastOutputClamp: { raw: 65536, capped: 1360, window: 1048576, planningInput: 997348, at: Date.now() - 30_000 },
});
session.metadata.dshCompactionRefusedBytes = 997348 * 4;
recordUpstreamCircuitFailure(URL_A, refused(), false);
@@ -247,7 +244,9 @@ test("#3148: acp_status surfaces the open circuit, armed emergency shrink and th
assert.ok(text.includes("699777"), "shrink estimate visible");
assert.ok(text.includes("OUTPUT BUDGET CLAMP"), "clamp section visible");
assert.ok(text.includes("65536 -> 1360"), "last clamp values visible");
- assert.ok(text.includes("997348"), "planning floor visible");
+ assert.ok(text.includes("input~997348"), "clamp input basis visible");
+ assert.ok(text.includes("DSH COMPACTION REPLAYS REFUSED"), "diagnostic refusal watermark visible");
+ assert.ok(text.includes("997348 tok"), "refusal high-water visible");
});
test("#3148: acp_status stays quiet about all three when none is active", () => {
@@ -255,7 +254,8 @@ test("#3148: acp_status stays quiet about all three when none is active", () =>
const text = handleAcpStatus({}, makeCtx(makeSession())).text;
assert.ok(!text.includes("UPSTREAM CIRCUIT OPEN"), "no circuit line when closed");
assert.ok(!text.includes("EMERGENCY SHRINK ARMED"), "no shrink line when not armed");
- assert.ok(!text.includes("OUTPUT BUDGET CLAMP"), "no clamp line without ledger/clamp history");
+ assert.ok(!text.includes("OUTPUT BUDGET CLAMP"), "no clamp line without clamp history");
+ assert.ok(!text.includes("DSH COMPACTION REPLAYS REFUSED"), "no refusal line without a recorded refusal");
});
test("#3148: a real usage report clears the emergency-shrink visibility marker", () => {
diff --git a/tests/issue3384-ledger-floor-starvation.test.ts b/tests/issue3384-ledger-floor-starvation.test.ts
index e8bd7ecf4c..d82e545664 100644
--- a/tests/issue3384-ledger-floor-starvation.test.ts
+++ b/tests/issue3384-ledger-floor-starvation.test.ts
@@ -7,12 +7,14 @@
// canDisableThinking:false, so each clamped budget was eaten entirely by
// thinking -> upstream stopReason=length -> no text, no tool-call, blank GUI
// turns, and DSH (which does not auto-continue on max-tokens) just replayed
-// the same dead loop on every "continue". Fix: the floor now engages only on
-// one of two independent grounds — the folded view already sits at the
-// enforced headroom target, or the requested budget is giant (>= 25% of the
-// native window, the #2490 monster-request line). Both protection regimes
-// stay pinned here AND in tests/issue2490-ledger-floor-doomed-probe.test.ts
-// (giant request) — this file pins the starvation side they left open.
+// the same dead loop on every "continue". #3384 gated the floor behind two
+// grounds (view at the enforced headroom target, or giant request >= 25% of
+// the window); #5596 removed the consumption entirely — refused bytes never
+// leave bili's door, so they are not a future forwarded input (the sticky
+// floor pinned healthy lanes onto the 4096 budget floor; see
+// tests/issue5596-refusal-floor-no-planning.test.ts). This file now pins the
+// incident shapes post-removal: healthy views keep their full budgets at ANY
+// request size, and the measured-input clamp boundaries still hold.
import assert from "node:assert/strict";
import http from "node:http";
import { once } from "node:events";
@@ -34,7 +36,6 @@ import { tmpdir } from "node:os";
import path from "node:path";
const WINDOW = 1_000_000; // the reported deployment's declared window
-const FLOOR = 946_753; // the incident's final planning floor (refused replay bytes / 4)
type CtxArgs = Parameters[2];
@@ -59,57 +60,42 @@ const clampLog = (lines: string[]): ((level: string, msg: string) => void) =>
test("#3384: the incident shape keeps its full budget — modest request, healthy folded view, huge refused ledger", () => {
const lines: string[] = [];
const rebuilt: Record = { max_tokens: 50_670 };
- clampOutgoingOutput(rebuilt, "max_tokens", incidentCtx({ ledgerFloorTokens: FLOOR }), "s-3384-a", clampLog(lines));
- // Pre-fix this clamped to 5909 (margin ceil(946753*0.05)=47338, cap
- // floor(1000000-946753-47338)=5909) — the starvation the session died in.
- assert.equal(rebuilt.max_tokens, 50_670, "budget untouched below both gate lines");
+ clampOutgoingOutput(rebuilt, "max_tokens", incidentCtx({}), "s-3384-a", clampLog(lines));
+ // Under the #2490-era unconditional floor this clamped to 5909 (margin
+ // ceil(946753*0.05)=47338, cap floor(1000000-946753-47338)=5909) — the
+ // starvation the session died in. Post-#5596 nothing consults the floor.
+ assert.equal(rebuilt.max_tokens, 50_670, "budget untouched: refused bytes never plan (#5596)");
assert.equal(lines.length, 0, "no clamp, no log");
});
-test("#3384: the giant-request ground still plans against the ledger (#2490 regime preserved)", () => {
+test("#3384/#5596: giant-request shapes on a healthy view keep their full budget (the 25% line is gone)", () => {
const lines: string[] = [];
- const rebuilt: Record = { max_tokens: 384_000 };
- // 384K >= 0.25 * 1M — the #2490 incident's own request size.
- clampOutgoingOutput(rebuilt, "max_tokens", incidentCtx({ ledgerFloorTokens: FLOOR }), "s-3384-b", clampLog(lines));
- assert.equal(rebuilt.max_tokens, 5_909, "planned against the refused ledger: cap floor(1M-946753-47338)=5909, above the 4096 capability floor");
- assert.match(lines[0] ?? "", /planning floor 946753 from the refused dsh compaction replay \(#2490\)/);
-});
-
-test("#3384: the boundary is inclusive — exactly 25% of the window takes the giant ground", () => {
- const lines: string[] = [];
- const rebuilt: Record = { max_tokens: 250_000 };
- clampOutgoingOutput(rebuilt, "max_tokens", incidentCtx({ ledgerFloorTokens: FLOOR }), "s-3384-c", clampLog(lines));
- assert.equal(rebuilt.max_tokens, 5_909, "raw == 0.25*window engages the floor (fail-closed side of the line)");
- assert.ok(lines.length >= 1, "the clamp is logged");
-});
-
-test("#3384: one token under the giant line stays unclamped while the view is healthy", () => {
- const lines: string[] = [];
- const rebuilt: Record = { max_tokens: 249_999 };
- clampOutgoingOutput(rebuilt, "max_tokens", incidentCtx({ ledgerFloorTokens: FLOOR }), "s-3384-d", clampLog(lines));
- assert.equal(rebuilt.max_tokens, 249_999, "below the line the ledger must not drive planning");
- assert.equal(lines.length, 0);
+ // 384K (the #2490 incident's own request size), exactly 25% of the window,
+ // and one token under it: under the #3384 gate these took the giant-request
+ // ground and planned against the 946753 refused-ledger floor (cap 5909).
+ // Post-#5596 the floor is diagnostic-only — measured input 89.7K leaves
+ // ~894K headroom, so every size passes untouched. Overflow at giant sizes
+ // is owned by preflight / egress fuse (#4079/#4185), not by pre-shrinking
+ // the budget from bytes that were never forwarded.
+ for (const [req, id] of [[384_000, "b"], [250_000, "c"], [249_999, "d"]] as const) {
+ const rebuilt: Record = { max_tokens: req };
+ clampOutgoingOutput(rebuilt, "max_tokens", incidentCtx({}), `s-3384-${id}`, clampLog(lines));
+ assert.equal(rebuilt.max_tokens, req, `${req} passes on a healthy view regardless of the refused ledger`);
+ }
+ assert.equal(lines.length, 0, "no clamp, no log");
});
-test("#3384: the view-at-enforced-target ground engages when bili's OWN view reaches the wall", () => {
+test("#3384/#5596: view past the enforced target no longer re-engages anything — planning is measured-input only", () => {
const lines: string[] = [];
const rebuilt: Record = { max_tokens: 50_670 };
- // Folded view 110K already past an enforced 100K headroom target — the
- // session really is at the wall, so worst-case-ledger planning applies
- // even for a modest request.
- clampOutgoingOutput(rebuilt, "max_tokens", incidentCtx({ headroomWindow: 100_000, lastInputTokens: 110_000, ledgerFloorTokens: FLOOR }), "s-3384-e", clampLog(lines));
- assert.equal(rebuilt.max_tokens, 5_909, "view at the enforced target re-engages the floor");
- assert.match(lines[0] ?? "", /planning floor 946753/);
-});
-
-test("#3384: legacy paths stay byte-identical — no refusal recorded, or floor below the folded view", () => {
- const legacy: Record = { max_tokens: 50_670 };
- clampOutgoingOutput(legacy, "max_tokens", incidentCtx({}), "s-3384-f", clampLog([]));
- assert.equal(legacy.max_tokens, 50_670, "no floor recorded -> unchanged legacy behavior");
-
- const small: Record = { max_tokens: 50_670 };
- clampOutgoingOutput(small, "max_tokens", incidentCtx({ ledgerFloorTokens: 10_000 }), "s-3384-g", clampLog([]));
- assert.equal(small.max_tokens, 50_670, "a floor smaller than the folded view changes nothing");
+ // Folded view 110K already past an enforced 100K headroom target: under
+ // the #3384 gate this re-engaged the refused-ledger floor (cap 5909).
+ // Post-#5596 nothing consults the floor — measured input 110K against a
+ // 1M window leaves ample headroom, so the modest budget passes. When
+ // bili's own view hits the wall, compression + preflight are the defense.
+ clampOutgoingOutput(rebuilt, "max_tokens", incidentCtx({ headroomWindow: 100_000, lastInputTokens: 110_000 }), "s-3384-e", clampLog(lines));
+ assert.equal(rebuilt.max_tokens, 50_670, "view-at-wall no longer drives refused-bytes planning (#5596)");
+ assert.equal(lines.length, 0);
});
// ---------------------------------------------------------------------------
diff --git a/tests/issue5596-refusal-floor-no-planning.test.ts b/tests/issue5596-refusal-floor-no-planning.test.ts
new file mode 100644
index 0000000000..72f9da2799
--- /dev/null
+++ b/tests/issue5596-refusal-floor-no-planning.test.ts
@@ -0,0 +1,239 @@
+// #5596: the refused dsh compaction replay SIZE (session.metadata.
+// dshCompactionRefusedBytes, recorded by the #1729 guard) drove the outgoing
+// output budget via the #2490 planning floor (gated since #3384). But those
+// bytes are refused and NEVER forwarded — they are not a future input. The
+// sticky high-water + the giant-request ground (any client requesting >= 25%
+// of the window, the common default) pinned healthy lanes onto the 4096
+// capability floor for the session's life: field incident, window 256000,
+// requested 128384, refused replay 866760 bytes (floor 216690) -> cap
+// floor(256000 - ceil(216690*1.12) - max(2048, ...)) = 1172 < 4096 -> wire
+// max_tokens 4096, model cut off mid-sentence at exactly 4096 output tokens,
+// lane stuck there until restart. Fix: the clamp plans against the MEASURED
+// input of THIS request only (per-request defenses own overflow: preflight /
+// egress fuse #4079/#4185); the record stays as a diagnostic watermark
+// (acp_status). This file pins both directions: refused bytes no longer plan
+// (the incident shapes pass), AND measured near-wall inputs still clamp
+// (the #3071 capability floor still binds from measurement).
+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 { getSession } from "../src/session.ts";
+import { clampOutgoingOutput } from "../src/server/budget.ts";
+import { DSH_COMPACTION_INSTRUCTION_PREFIX } from "../src/server/dsh-compaction-guard.ts";
+import { rmrf } from "./tmp-rm.ts";
+import { mkdtempSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import path from "node:path";
+
+const ctxOf = (over: Partial[2]>) =>
+ ({
+ systemText: "",
+ tools: undefined,
+ processedMessages: [{ role: "user", text: "hello" } as never],
+ lastInputTokens: 0,
+ lastInputTokensSource: "usage",
+ nativeWindow: 256_000,
+ imageTokens: 0,
+ ...over,
+ }) as Parameters[2];
+
+const clampLog = (lines: string[]): ((level: string, msg: string) => void) =>
+ (level, msg) => { if (level === "info" || level === "warn") lines.push(msg); };
+
+test("#5596: the incident sublane shape — measured input 184K gets an honest 39614, not the 4096 plateau", () => {
+ // Window 256000, requested 128384 (50% — above the old giant line),
+ // measured input 184K. Pre-fix the 216690 refused-replay floor replaced
+ // the estimate (cap 1172) and the wire carried 4096. Post-fix the
+ // measured input decides: safe 206080, margin 10304 -> cap 39614.
+ const rebuilt: Record = { max_tokens: 128_384 };
+ const lines: string[] = [];
+ clampOutgoingOutput(rebuilt, "max_tokens", ctxOf({ lastInputTokens: 184_000 }), "s-5596-a", clampLog(lines));
+ assert.equal(rebuilt.max_tokens, 39_614, "honest measured-input cap, far above the 4096 plateau");
+ assert.match(lines[0] ?? "", /input~184000/);
+});
+
+test("#5596: the incident main-session shape — healthy input keeps the full 128384", () => {
+ // Same lane family, measured input 100K. Pre-fix the 164287 floor cut the
+ // budget to 62797 on every request; post-fix nothing consults the floor.
+ const rebuilt: Record = { max_tokens: 128_384 };
+ clampOutgoingOutput(rebuilt, "max_tokens", ctxOf({ lastInputTokens: 100_000 }), "s-5596-b", clampLog([]));
+ assert.equal(rebuilt.max_tokens, 128_384, "measured headroom suffices — full budget rides out");
+});
+
+test("#5596 (negative direction): a MEASURED near-wall input still clamps to the 4096 plateau", () => {
+ // The removal must not disarm the clamp: measured input 240K against a
+ // 256K window leaves cap 4000 < 4096 -> floored up to 4096 by the #3071
+ // capability bound (min(4096, raw headroom 16000)). This is the legitimate
+ // 4096 — driven by bytes actually on the wire, not by refused replays.
+ const rebuilt: Record = { max_tokens: 128_384 };
+ clampOutgoingOutput(rebuilt, "max_tokens", ctxOf({ lastInputTokens: 240_000 }), "s-5596-c", clampLog([]));
+ assert.equal(rebuilt.max_tokens, 4_096, "measured overflow risk still bounds the budget");
+});
+
+test("#5596 (negative direction): beyond the wall the clamp surrenders to preflight", () => {
+ const rebuilt: Record = { max_tokens: 128_384 };
+ clampOutgoingOutput(rebuilt, "max_tokens", ctxOf({ lastInputTokens: 252_000 }), "s-5596-d", clampLog([]));
+ assert.equal(rebuilt.max_tokens, 128_384, "cap < 1024 -> no clamp (preflight / egress fuse own that regime)");
+});
+
+const E2E_WINDOW = 100_000;
+
+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";
+}
+
+type Call = { maxTokens: number | null; contentChars: number };
+
+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");
+ try {
+ const parsed = JSON.parse(raw) as { messages?: Array<{ content?: unknown }>; max_tokens?: number };
+ const chars = (parsed.messages ?? []).reduce((n, m) => n + (typeof m.content === "string" ? m.content.length : 0), 0);
+ calls.push({ maxTokens: typeof parsed.max_tokens === "number" ? parsed.max_tokens : null, contentChars: chars });
+ 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): ProxyOptions {
+ return {
+ port: 0,
+ host: "127.0.0.1",
+ upstream: "http://127.0.0.1",
+ routes: routes as ProxyOptions["routes"],
+ modelContextLimit: E2E_WINDOW,
+ kernelConfig: defaultConfig(E2E_WINDOW, { preserveRecentMessages: 2, preserveRecentTokens: 2000 }),
+ 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: [] },
+ } as ProxyOptions;
+}
+
+type Ctx = { url: string; headers: Record };
+
+async function setup(): 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: E2E_WINDOW } } } }));
+ 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": "" },
+ },
+ calls,
+ close: async () => {
+ proxy.close();
+ proxy.closeIdleConnections?.();
+ upstream.close();
+ upstream.closeIdleConnections?.();
+ await Promise.allSettled([once(proxy, "close"), once(upstream, "close")]);
+ },
+ };
+}
+
+test("e2e #5596: a refused compaction envelope records the watermark but does not touch the next turn's budget", async () => {
+ const root = mkdtempSync(path.join(tmpdir(), "bili-5596-"));
+ const cfgFile = path.join(root, "billion-context.json");
+ writeFileSync(cfgFile, '{"providers":{}}\n', "utf8");
+ const prevConfig = process.env.BILI_CONFIG_FILE;
+ process.env.BILI_CONFIG_FILE = cfgFile;
+ const { ctx, calls, close } = await setup();
+ try {
+ ctx.headers["x-acp-session"] = "s-5596-e2e";
+ const postRaw = (body: string) => fetch(ctx.url, { method: "POST", headers: { ...ctx.headers }, body });
+
+ // Turn 1: seed the session with a small exchange.
+ let r = await postRaw(JSON.stringify({ model: "gpt-test", stream: true, messages: [{ role: "user", content: "hello" }] }));
+ assert.equal(r.status, 200, "seed turn accepted");
+ await r.text();
+
+ // The dsh compaction envelope (~320KB of core text -> ledger floor
+ // ~80K tokens, ABOVE the old 25% giant line of a 100K window): the
+ // guard refuses it (default posture) and records the high-water byte
+ // count. Pre-#5596 the giant-request ground would have applied this
+ // floor to the next turn's planning (cap ~5874 for a 30K request).
+ const envelope = JSON.stringify({
+ model: "gpt-test",
+ stream: true,
+ messages: [
+ { role: "system", content: "sys" },
+ { role: "user", content: `${DSH_COMPACTION_INSTRUCTION_PREFIX} ${"x".repeat(320_000)}` },
+ ],
+ });
+ r = await postRaw(envelope);
+ assert.equal(r.status, 422, "guard refuses the compaction call (default)");
+ await r.text();
+ const s = getSession("s-5596-e2e");
+ assert.ok(s, "session exists");
+ assert.equal(s.metadata.dshCompactionRefused, true, "refusal flag set");
+ assert.equal(s.metadata.dshCompactionRefusedBytes, Buffer.byteLength(envelope), "high-water bytes recorded");
+ const floor = Math.ceil(Buffer.byteLength(envelope) / 4);
+ assert.ok(floor > E2E_WINDOW * 0.25 && floor < E2E_WINDOW, `floor ${floor} sits in the discriminating band (25%, window)`);
+
+ // Turn 2: a normal short turn with a budget ABOVE the old giant line
+ // (30K > 25K). Post-fix the measured input (~a few hundred tokens)
+ // decides: the full 30K rides out untouched by the recorded watermark.
+ r = await postRaw(JSON.stringify({
+ model: "gpt-test",
+ stream: true,
+ max_tokens: 30_000,
+ messages: [
+ { role: "user", content: "hello" },
+ { role: "assistant", content: "forwarded answer" },
+ { role: "user", content: "and now?" },
+ ],
+ }));
+ assert.equal(r.status, 200, "normal turn accepted");
+ await r.text();
+
+ const forwarded = calls[calls.length - 1];
+ assert.ok(forwarded, "the normal turn reached the upstream");
+ assert.equal(forwarded.maxTokens, 30_000, "#5596: the refused watermark did not starve the budget");
+ } finally {
+ if (prevConfig === undefined) delete process.env.BILI_CONFIG_FILE;
+ else process.env.BILI_CONFIG_FILE = prevConfig;
+ await close();
+ rmrf(root);
+ }
+});