Skip to content

[PR] fix: egress window fuse — stop provably-doomed bodies at the last send site (#4079) - #4090

Merged
ranxianglei merged 2 commits into
masterfrom
2026-10-21_egress-window-fuse
Oct 21, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-10-21_egress-window-fuse

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Oct 21, 2026 •

Copy link
Copy Markdown
Owner

What this fixes

Upstream 413/400 loops where bili forwarded a body that provably cannot fit any context window:

Root-cause umbrella: #4079 (统一根因分析). This is the cheapest damage-control layer from that analysis — the closed-loop calibration fix (teach the estimator from 400s) follows separately.

The hole

Every send-side check in forward() was either pure bytes (checkOutboundBody's 5GiB budget, relay.ts:569) or lived only inside preflight's two escape hatches (#2313 estimate-only probe / #3146 fresh-session forward-once). A request that reached forward() without preflight judging it — no model in the body, trigger not firing, plugin lane, a wire-dialect reconcile that re-anchored nothing and re-sent verbatim — hit the socket with zero window comparison. No token-vs-window check exists anywhere between the prepared body and the socket.

The fix (two layers, same bars)

Both layers reuse the existing doom-probe math (#2490): core-text bytes vs max(2MB, floored-window × 7.5 B/token) — 7.5 B/token is the densest tokenizer density ever observed on a live route (#2122), so only bodies no tokenizer can fit are blocked. A dense-but-fitting payload is never false-positived (pinned both-ways in tests, #3617).

The window itself is floored at 1,000,000 tokens (MIN_ASSUMED_WINDOW_TOKENS, src/util.ts, shared by both lanes so it cannot drift): a declared window can be wrong SMALL — stale modelContextLimit, a registry miss, a #3993-style catalog fallback (475K→258K) — and a catastrophic-only guard keyed to an undersized window would false-block bodies the real upstream accepts. Deliberate trade: bodies "only" 2–5× a small declared window now pass the byte guards (preflight + the calibration loop own that range); only bodies no 1M-token window could hold are stopped locally. #4068's 6,165,048-token body (~18MB core text) still trips the fuse.

  1. Relay egress fuse (src/server/relay.ts, right after checkOutboundBody): for every non-side, non-passthrough POST with a known session.metadata.effectiveContextLimit, compare the final payload's core-text byte mass against the doom bars and answer 413 {type:"request_too_large", code:"egress_window_fuse", retryable:false} ([Evidence] pi 车道切供应商(同 modelId)后 fold 不随会话迁移:裸上下文 2.23MB(≥298K est tokens)被 preflight 10 秒拒 3 次;账面 ≤157K 与裸水位背离 10.5h #2847 semantics: hosts must not retry) instead of forwarding a guaranteed 4xx. Deliberately no LocalRefusalRecord: the doomed body itself re-trips the fuse on every retry — a record would only outlive the turn and lock the route the way The context window suddenly hit its limit and threw an error. Starting a new session didn't help either. dsh | 上下文突然满了报错 新会话也没用 dsh #3974 suffered.

  2. Gate none oversize arm (src/agent/dsh-native.ts): the The input (6165048 tokens) is longer than the model's context length (1048580 tokens(todo fix)). #4068 channel specifically. When the takeover gate cannot attribute a request (none — agentless lane, quota/model-switch path without an ALS boundary, The D4.1F model accessed via the WorkBuddy reverse proxy cannot be compressed. | 使用workbuddy反代的D4.1F模型无法压缩 #2975/Why did it spike to several M tokens: after dsh attribution was lost, requests bypassed bili and were sent directly, ren | 为什么会涨到几 M tokens:dsh 归因丢失后请求绕过 bili 直发,压缩与护栏全部失效 #4082), it currently lets the raw body go DIRECT (「BUG」native-intercept 进程级 globalThis.fetch 补丁按 URL 形状接管所有模型流量,无关调用者(如 MemOS)被卷入宿主压缩流程 #1117) — where no bili check exists at all. Now an unattributed body whose optimistic byte floor (bytes/4) already exceeds the window refuses LOCALLY (422, oversize-refusal-unattributed, its own remedy wording — there is no broken scope to restart) instead of riding direct into the upstream-rejection loop. This mirrors the existing threw arm ([dsh-native] takeover-gate refusal (threw state) fails open with the FULL uncompressed transcript; a >window local history dies upstream with 400 (maximum context length) #2924) with identical never-false-refuse reasoning; unknown window or unreadable/stream body keeps 「BUG」native-intercept 进程级 globalThis.fetch 补丁按 URL 形状接管所有模型流量,无关调用者(如 MemOS)被卷入宿主压缩流程 #1117 semantics, and attributed traffic is untouched (it goes to the proxy where preflight's own doom math decides).

What this PR deliberately does NOT do

Tests

  • tests/issue4079-egress-window-fuse.test.ts (new): e2e both-ways — a no-model 8.68MB body (preflight skipped via !model) answers 413 egress_window_fuse with the byte math and the 1M floor note in the message and zero upstream calls; bodies under the bars — including a 1.63MB one against a 20K declared window (over its own ×7.5 ratio bar, under the floored bar) — still forward: a mis-sized small window never false-blocks.
  • tests/dsh-native.test.ts: gate none + threw arms — refusal fires at 4,000,004B (bytes/4 = 1,000,001 > floored bar 1M), false at exactly the bar, false for over-declared-but-under-floor bodies (4004B vs a 1000-token declared window), false on unreadable body / unknown window, true for attributed traffic, durable LOCAL-refusal line in bili.log.

Local: typecheck ✅, issue4079 suite 2/2 ✅, ACP_TEST_E2E_FAKE=1 8/8 ✅. The dsh-native attach-flow tests cannot run in my sandbox (they time out identically on clean master — environment); relying on the CI test lane per repo discipline.

Refs #4079. Related: #4068, #3872, #3918, #4082, #2924, #2490.

…d site (#4079)

Every send-side check was either pure bytes (5GiB budget) or lived only
inside preflight's two escape hatches (#2313/#3146); a request that
reached forward() without preflight judging it hit the socket with zero
window comparison (#4068: a 6,165,048-token send against a 1,048,580
window; #3872: a 5.35x re-send after a wire switch).

Two layers, same doom math as #2490 (core text bytes vs the densest
observed tokenizer, 7.5 B/token #2122 — only bodies NO tokenizer can fit
are blocked, so a dense-but-fitting payload never false-positives):

- relay egress fuse in forward(): pre-send check against
  session.metadata.effectiveContextLimit for every non-side,
  non-passthrough POST; answers 413 request_too_large/egress_window_fuse
  (retryable:false, #2847 semantics) instead of forwarding a guaranteed
  4xx. No LocalRefusalRecord — the body itself re-trips the fuse on every
  retry, a record would only lock the route the way #3974 suffered.
- gate `none` oversize arm: an UNATTRIBUTED body whose optimistic byte
  floor (bytes/4) already exceeds the window refuses LOCALLY (422,
  oversize-refusal-unattributed) instead of riding #1117's direct send
  into the #4068 upstream-rejection loop. Mirrors the threw arm (#2924);
  unknown window or unreadable body keeps #1117 semantics, attributed
  traffic is untouched.

Refs #4079. Local: typecheck, issue4079 suite 2/2, ACP_TEST_E2E_FAKE 8/8.
@ranxianglei ranxianglei changed the title fix: egress window fuse — stop provably-doomed bodies at the last send site (#4079) [PR] fix: egress window fuse — stop provably-doomed bodies at the last send site (#4079) Oct 21, 2026
@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-21_egress-window-fuse (e17e8df)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.4887-pr.4090.e44d356

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr4090.tgz
npm install -g package

Install instructions are refreshed on each push.

A declared window can be wrong SMALL — stale modelContextLimit, a
registry miss, a model-catalog fallback (#3993: 475K->258K), a plugin
under-reporting — and a catastrophic-only byte-floor guard keyed to an
undersized window would false-block bodies the real upstream accepts.

Both guards now size against max(declared, MIN_ASSUMED_WINDOW_TOKENS =
1,000,000) (src/util.ts, shared by the relay lane and the dsh gate so
the floor cannot drift):
- relay egress fuse: bar = max(2MB, max(declared, 1M) x 7.5 B/token);
  the refusal message states the floor for the operator.
- dsh gate threw + none oversize arms: refuse only when bytes/4 exceeds
  the floored bar; log lines print declared and floored values.

Trade (deliberate): bodies 'only' 2-5x a small declared window now pass
the byte guards — preflight + the calibration loop own that range; only
bodies no 1M-token window could hold are stopped locally. #4068's
6.16M-token body (~18MB core text) still trips the fuse.

Tests: e2e positive moves to 8.68MB (> 7.5MB floored bar) and pins the
floor note in the message; the under-bar case doubles as the
mis-sized-window both-ways pin (1.63MB vs a 20K declared window
forwards). dsh gate tests move the refusal boundary to 4,000,004 B and
add the over-declared-but-under-floor negative.

Refs #4079. Related: #4068, #3993.
@ranxianglei

Copy link
Copy Markdown
Owner Author

(ai: zai-org/GLM-5.3)

按 owner 要求收紧了拦截线的口径(e17e8df6):两条闸的窗口一律以 100 万 token 打底(MIN_ASSUMED_WINDOW_TOKENS = 1_000_000,src/util.ts 共享常量,relay 闸和 dsh 门不会漂移)。

理由:声明/配置的窗口可能被拿小(用户手滑设小 modelContextLimit、registry 拿错、#3993 那种目录 fallback 475K→258K)——灾难兜底闸如果信了一个偏小的窗口,会错杀真实上游装得下的请求。

新拦截线:

  • relay 出口闸:max(2MB, max(声明窗口, 1M) × 7.5 B/token) → 实际上 ≥7.5MB 核心文本才拦(1M 窗口也按 7.5MB 算);
  • dsh 门(threw + none 两臂):字节数/4 > max(声明窗口, 1M) → 4MB+ 才拦。

代价(刻意):小窗口会话 2–5 倍超窗的 body 现在会放行冲上游吃一次 400——这个量级归 preflight + #4093 校准闭环管,字节闸只管"1M 窗口都装不下"的灾难级(#4068 的 6.16M tok ≈18MB 依旧拦住)。拒绝文案里同时打出 declared 和 floored 两个值,方便运维看出窗口声明有问题。

测试已同步:e2e 正例改 8.68MB(>7.5MB floored 线),负例 1.63MB 对 20K 声明窗口放行(错杀防护的 both-ways 钉子);dsh 门边界移到 4,000,004B,并新增"超声明窗口但低于 1M 地板→不拒"的反向断言。

@ranxianglei
ranxianglei merged commit 37afa81 into master Oct 21, 2026
49 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.705-master.37afa81 (rolling master tag).

npm install -g billion-context@0.0.705-master.37afa81  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

本 PR(#4098,发送侧窗口闸)是 #4090 的重复实现,按先来后到关闭。

说明:#4098 于昨晚开出时做了出站查重但未命中 #4090(标题措辞「egress window fuse」与我的检索词不匹配——按失败签名/机制匹配的教训记下了)。两者同症状、同机制、同根因:在最终发送点用同一套 doomed-probe 字节数学(max(2MB, window×7.5))拦下任何 tokenizer 都装不下的载荷。

#4090 胜出的三点:

  1. 1M 窗口打底(MIN_ASSUMED_WINDOW_TOKENS)——灾难闸不信可能被拿小的声明窗口(D 组坏分母),我的版本信声明窗口,偏小声明下会错杀真实上游装得下的请求;
  2. 第二层 dsh 门 none-arm——把 The input (6165048 tokens) is longer than the model's context length (1048580 tokens(todo fix)). #4068 的 DIRECT 通道也管住了;
  3. 不记 LocalRefusalRecord 的取舍更干净——doomed 体每次重试自触发熔断,记录只会跨 turn 锁路由(与 [PR] remove the #726 dead-end cooldown; expire #3846 local-refusal records after 10m (#4079) #4105 给记录加 TTL 的方向一致)。

无内容需要保留:我分支里的机械重构(DOOMED 常量移位、local-refusal 模块抽取)依附于闸门本身,闸门被吸收后无独立价值。关闭。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant