Skip to content

[PR] fix: arm the pi lane's direct-send window guard so a proxy outage cannot egress an oversized history - #5027

Closed
adlternative wants to merge 5 commits into
ranxianglei:masterfrom
adlternative:2026-10-28_pi-direct-send-window-guard
Closed

adlternative wants to merge 5 commits into
ranxianglei:masterfrom
adlternative:2026-10-28_pi-direct-send-window-guard

Conversation

@adlternative

Copy link
Copy Markdown
Contributor

Fixes #5025.

What went wrong

With no live proxy, directSendOrRefuse (src/agent/native-intercept.ts:1204) measures the outgoing raw body against a window it resolves from the stamped x-bili-plugin-context-window header, else state.declaredWindowForDirect. On the pi lane both were empty in exactly the state that egresses uncompressed:

So declaredWindow === undefined → nothing measured → fail-open direct send. A long session's unfurled history (multi-million tokens) went to the provider, came back input exceeds the context window, and every retry re-sent the same body; the host's overflow-recovery summarization hit the same 400, leaving the session unusable while the proxy stayed down.

Change

  • src/agent/pi.ts: record ctx.model.contextWindow at the top of the before_provider_headers handler, before any proxy-readiness early return; export it as piDeclaredWindowForDirect() plus a test reset. A missing/unknown window records undefined, so the guard still fails open rather than guessing.
  • src/agent/pi-native.ts: state.declaredWindowForDirect = piDeclaredWindowForDirect next to the existing state.prepareDirectSend.

Result: no-proxy + a body whose media-excluded core text provably cannot fit the model's window → local retryable oversize-refusal-fallback gate error (#4516) instead of an upstream 400 storm. Anything that fits, and every healthy-proxy request (stamped header outranks the reader), behaves exactly as before.

Tests

tests/issue5025-pi-direct-window.test.ts — 5 cases:

  1. window recorded even when no proxy base resolves (and no header/marker stamped — the bug premise);
  2. an unknown window clears the record (fail open, no inherited window);
  3. pi-native arms the reader (this one goes red if the arm line is removed);
  4. armed reader + not-ready intercept + oversized body → 422 oversize-refusal-fallback, zero upstream egress;
  5. same setup + a body that fits → direct send unchanged.

Also run locally: npm run typecheck, tests/issue4894-direct-window-guard.test.ts, tests/pi-native.test.ts, tests/issue3433-pi-incremental-guard.test.ts, tests/issue2072-native-caller-stamping.test.ts, tests/fix-1897-leaked-tools-side.test.ts (38 pass), npm run check:unused-exports, npm run check:test-import-graph.

Out of scope

  • omp-native.ts / opencode-native.ts have the same unarmed slot; same one-line pattern, separate PRs, to keep this change surgical to the lane that hit the storm.
  • The client-side declared window (a host config advertising a window larger than the provider accepts) is a host-config concern: the guard only enforces what the client declares.

@ranxianglei ranxianglei changed the title fix: arm the pi lane's direct-send window guard so a proxy outage cannot egress an oversized history [PR] fix: arm the pi lane's direct-send window guard so a proxy outage cannot egress an oversized history Oct 28, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

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

查重: 不重复。 与既有护栏家族逐条对照:#4516(直发 oversized 护栏本体,已合入)→ #4894/PR #4904(dsh 车道未武装,已修复合入)→ 本 PR = 同一机制的 pi 车道实例。三者是「护栏存在 → dsh 武装 → pi 武装」的完整链条,各修各的车道,不重叠;open/closed 无其他覆盖 pi 直发窗口来源的条目。

标签: bug / pi / scope/wiring / p2(已打)——客户端接入层缺陷;仅在「代理不可用 + 历史超窗」条件下触发,无数据丢失、无缓存泄露,代理恢复后会话即恢复正常 → p2 而非 p1。

初步独立核验(对着 origin/master b08fbf361 逐行核过,不是照抄描述):

审核中发现的新问题 → 已立项 #5032:omp-native / opencode-native 存在同样的未武装槽位(已核实:opencode-native.ts:817 同样在盖章前 return、两文件均未设 declaredWindowForDirect;omp 甚至从不打 context-window 头)。同症状同机制,本 PR 按作者明确决定收敛到 pi 车道,#5025 合并后即关闭,故剩余两车道单列 #5032 跟踪(带来源标记)。

定向测试与 CI 跟进中,最终结论按八问体例随后贴出。

@adlternative

Copy link
Copy Markdown
Contributor Author

跟进 #5032 的 opencode 半边:已提 PR #5037(同一机制,opencode V1 车道武装 declaredWindowForDirect,5 个定向用例含「零出网」与「放得下不误拒」两方向)。

omp 那半边仍未动 —— 复核结论与 #5032 描述一致:src/agent/omp-native.ts 的接缝看不到任何 contextWindow(只有 body 里的 model 名字符串),src/agent/omp.ts 仅是 3 行 stub,所以「窗口从哪来」需要先定取舍(读宿主模型配置 vs. 按 provider/model id 走 registry 解析),不适合直接照抄 pi 模式。

顺带补充一条便于后续判断的事实:opencode 的 V2/native 车道本来就有自己的 #3308 refuse-oversized 分支(oversizeDirectRefusal 用 state.windows 量体,且 refreshWindows 在 route 之前跑),所以真正裸奔的只有 V1 车道 + omp。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

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

审核结论(八问) — 独立对着 origin/master b08fbf361 与 PR head 8ffff3a31 逐行核过,未照抄 issue/PR 描述:

  1. 是什么问题:pi native 车道在代理不可用时,把超窗的未压缩历史裸奔直发 provider——每次重试都重发同一份 body 吃同一个上游 400,会话在代理恢复前完全不可用。
  2. 什么原因:共享直发护栏(directSendOrRefuse,src/agent/native-intercept.ts:1204)需要一个窗口来源才能测量,而它的两个来源恰好在 not-ready 路径上全空:header 只在代理分支盖章(handler 在 proxyBase === undefined return 之前没有任何窗口记录),reader(declaredWindowForDirect)此前只有 dsh 武装过([Bug][dsh] 0.1.208: attributed-but-not-ready direct send has no window guard, and takeover is declared before readiness — long sessions 400 with prompt_too_long | 归因正常但代理未就绪时直发无窗口护栏 + 就绪前就宣告接管 → 长会话必 400 #4894/[PR] fix(#4894): arm the direct-fallback oversize guard on dsh not-ready sends #4904)。信息其实就在手边(ctx.model.contextWindow),只是没被决策点看到。
  3. 影响范围与严重程度:仅 pi native 车道;触发条件 = 代理不可用 + 会话历史超模型窗口;命中即该会话在代理宕机期间不可用(无数据丢失、无缓存泄露,代理恢复即自愈)→ p2。
  4. 怎么修复的:在 readiness early-return 之前记录本请求模型的声明窗口(验证条件 + floor 与既有盖章处 pi.ts:1366-1368 一致),导出为进程级 reader 并武装共享护栏;未知窗口记 undefined → fail-open 语义不变,绝不猜窗口;header 仍优先(?? 短路),健康代理路径字节级不变。疏不是堵:让决策点看见已有信息,不改写任何载荷。
  5. 回归审查(逐项过既有行为):健康代理请求 → header 优先,不变;one-shot/-p → 记录是 handler 顶部同步赋值,不引入新阻塞;中途换模型 → 每请求发送前重记自己的窗口,测试 2 钉死「未知窗口清除记录、不跨模型继承」;其他车道(dsh/opencode/omp)→ 各自独立的 NativeInterceptState,未武装 = 零变化;媒体排除保守 floor、不可读 body fail-open、「无窗口不缓冲大 body」原则全部保持。唯一残留面:handler 首行前抛异常会保留旧值——极窄,且 floor 估算(bytes/4)使其无法误拒。有意变更一处,见下方披露,未见无意回归。
  6. 是否破坏缓存:不涉及——未触碰任何缓存/重试/增量游标/去重键策略。
  7. 是否改了设计方向:不涉及——把 [Bug][dsh] 0.1.208: attributed-but-not-ready direct send has no window guard, and takeover is declared before readiness — long sessions 400 with prompt_too_long | 归因正常但代理未就绪时直发无窗口护栏 + 就绪前就宣告接管 → 长会话必 400 #4894/[PR] fix(#4894): arm the direct-fallback oversize guard on dsh not-ready sends #4904 建立的 hook 模式扩展到 pi 车道,架构/边界不动。
  8. 新配置:无——模块级变量 + 导出 reader,无 env/config/默认值进入配置面。

行为变更披露(必须报告):pi 车道、代理不可用路径上的超大模型请求——旧:裸奔出网 → 上游 input exceeds the context window 400 风暴 → 客户端重试反复重发同一份超大历史;新:本地 422(x-bili-gate: oversize-refusal-fallback)立即失败,文案带原因与窗口数字(#4516 同款可重试 gate 错误)。有意的语义收紧,仅限 pi 车道未就绪直发场景;窗内请求与一切健康代理流量行为完全不变。

本地定向验证(head 8ffff3a):npm run typecheck 干净;定向测试 43/43 绿(新增 5 例 + issue4894/pi-native/issue3433/issue2072/fix-1897 既有 38 例);check:unused-exports clean(6 waived)、check:test-import-graph ok。全量判决以 CI 为准。

CI 现状(如实):镜像缓存中该 head 的全部 check 为 cancelled(无判决);期间 master 已前进至 b0b0e66ff(#5017 ci-fixes 合入),PR base 过期——按仓库机制等哨兵把 master sync 进分支出新 head 后看判决,若红按 #4599 协调流程处理,不在本地长查继承红。

对跟进评论的独立核实(三条都对上了):#5037 存在(镜像确认,CI queued、base behind);opencode V2/native 车道确有自己的 #3308 refuse-oversized(oversizeDirectRefusal,opencode-native.ts:156,:243 调用,s.windows 量体、未知窗口 fail-open)——「真正裸奔的只有 V1 + omp」成立;src/agent/omp.ts 确为 3 行 stub。omp 的窗口来源取舍已在 #5032 里标为唯一待决策项。

过程披露一行:排查立项 API 路径时向镜像发过一次预期失败的写请求,返回 404、未创建任何数据,已确认无副作用(后续改走 GitHub REST 正常立项 #5032)。

判定:代码审核通过(逻辑/不变量/测试牙齿/diff 整洁逐项达标,diff 恰好 3 文件 221+/1- 单提交),未发现拦合问题;待 sync 后 head 的 CI 全量判决落地即可合并。

…not egress an oversized history (ranxianglei#5025)

Without a live proxy, directSendOrRefuse measured the raw body against a
window it read from the stamped x-bili-plugin-context-window header or from
state.declaredWindowForDirect. On the pi lane BOTH were empty on exactly that
path: before_provider_headers returns at `proxyBase === undefined` before it
stamps the header, and the lane never armed the slot (ranxianglei#4894 armed dsh only).
The check then failed open by construction, so a long session's unfurled
history (multi-million tokens) went straight to the provider, whose
"input exceeds the context window" 400 repeated on every retry and made the
session unrecoverable for as long as the proxy stayed down.

Record the per-request ctx.model.contextWindow at the TOP of the headers hook
(before the readiness return) and arm it as declaredWindowForDirect in
pi-native.ts. An unknown window records undefined — the guard keeps failing
open instead of guessing — and a healthy proxy is unchanged, since the
stamped header still outranks the reader.

Verification: tests/issue5025-pi-direct-window.test.ts (5 cases: recording
before the early return, unknown-window reset, the armed reader, local
oversize refusal with no upstream egress, and no false refusal when the body
fits); typecheck; issue4894/pi-native/issue3433/issue2072/fix-1897 suites;
unused-export and test-import-graph gates.
@adlternative
adlternative force-pushed the 2026-10-28_pi-direct-send-window-guard branch from 536f690 to cfcb3f9 Compare October 30, 2026 03:13
@adlternative

Copy link
Copy Markdown
Contributor Author

按 #5077 复核里那条建议(「合 #5027 本体前触发一次新鲜绿跑」),我把本 PR 也移到了最新 master 上,并顺手查清了上一轮那 3 个红分片是什么。

新 head cfcb3f91(base = 最新 master 6db59068,单提交、恰好 3 文件 221+/1-)。

上一轮 3 个红分片的归因(与本案无关):test (ubuntu 22, #3) / test (ubuntu 24, #3) / test (windows 22, #4) 三个红分片,全部是同一个测试:

not ok 1859 - HTTP a cold-restored non-retained parent serves its intact raw tail immediately (#5140)
tests/plugin-fork.test.ts:3:58035
Expected values to be strictly equal: 409 !== 200

这是 #5140 带进来的 fork/cold-restore 用例,与本 PR 改的 pi 直发窗口守卫(pi.ts handler 顶部记录 + pi-native.ts arm)毫无交集;而且我在那个红 head 本机跑同一文件是 54 pass / 0 fail,说明它在 CI 上是时序型 flake(分片并行下 409 冲突)。另外那个 head 的 base 停在 09a5b5b7,不含之后的 #5193(gc-combined),而 #5193 正好动了 src/storage/store.ts / src/web/sessions-data.ts 这条 fork/cold-restore 链路 —— 新 head 与 master 对齐后应能一并覆盖。

新 head 上的本地验证(重跑,不沿用旧 head 结果):

  • npm run typecheck 干净(0.1.209 基线);
  • tests/issue5025-pi-direct-window.test.ts + tests/pi-native.test.ts + tests/issue4894-direct-window-guard.test.ts + tests/issue3433-pi-incremental-guard.test.ts + tests/issue2072-native-caller-stamping.test.ts + tests/fix-1897-leaked-tools-side.test.ts → 43 pass / 0 fail;
  • npm run check:unused-exports clean(6 waived)、npm run check:test-import-graph ok;
  • 附带:tests/plugin-fork.test.ts(上面那个 flake 用例)在新 head 上本机 54/54 pass。

CI 已在新 head 上排队(22 项 pending)。合并顺序仍然是你上次给的:#5027 → #5077(与 #5037 无依赖,任意顺序)。

@adlternative

Copy link
Copy Markdown
Contributor Author

先同步一个事实:本 PR 的 head 已被哨兵重排到新 base e1938618(我本地的 cfcb3f91 已被取代,内容未变,git diff 逐字节一致)。

当前这个红 check 不是本 PR 的 diff 引起的,是 master 侧的 LIFECYCLE.md 漂移。 证据(在纯 upstream/master 上复现,无任何我的提交):

$ node scripts/lifecycle-ledger-guard.mjs      # worktree at 7528debc (master tip)
FAIL: 1 BILI_* env var(s) read from src/ but NOT in LIFECYCLE.md:
  - BILI_SESSION_CONTENT_STORE_PIN
  • BILI_SESSION_CONTENT_STORE_PIN:由 c8d7a802 fold idle session content stores between requests (#4788 phase 2) 引入,读取点在 src/knobs.ts:451(定义在 src/knobs.ts:444,另有 src/persist.ts:3204 的注释),但 LIFECYCLE.md 里 grep 不到。
  • 在 master tip 7528debc 上还多一个:BILI_SPAWN_FINGERPRINT(src/instance.ts:343 export const SPAWN_FINGERPRINT_ENV = "BILI_SPAWN_FINGERPRINT";,由 fc53fd46 fix(launch): a strictly newer same-lane holder is adopted, not deadlocked around (#4125) 引入)。也就是说漂移还在变大,不是我 rebase 的时点问题。
  • 该 gate 的判定就是「src/ 里读到的每个 BILI_* 必须在 LIFECYCLE.md 的 §1/§2 有账」,所以任何基于该 base 的 PR 都会在 8 秒内红 —— 与本 PR 的 3 个文件(pi.ts / pi-native.ts / issue5025-*)无关。

本 PR 面本身没有新增任何 env 读取(git diff 里零 env. / 零 process.env),所以补账不该塞进这里(而且会和那个 PR 冲突);正确处置是 master 侧把这两条补进 LIFECYCLE.md §2(或 §1 带 release condition),再把 master sync 进来。

我这边没有可执行的动作,除了等这个 gate 在 master 侧被修好。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

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

Sync round 3 done: merged origin/master tip afdf889c into the PR branch, pushed 5f00e99a (clean merge, zero conflicts; PR delta intact at +221/−1 across the 3 files; typecheck clean, targeted suite 5/5). Post-push state: branch no longer behind; the single red lane is lifecycle-ledger-guard, and I independently confirmed your diagnosis — running node scripts/lifecycle-ledger-guard.mjs at pristine master afdf889c (no PR commits) fails with exactly the two unlisted names (BILI_SESSION_CONTENT_STORE_PIN, BILI_SPAWN_FINGERPRINT). So this red is master-side inheritance, not this diff; nothing for this PR to do beyond waiting for the ledger entries to land. Reported with evidence in #4599 (per CI-coordination policy the fix converges on ci-fixes via the sentinel — I did not patch it in here).

@ranxianglei

Copy link
Copy Markdown
Owner

Landed via batch PR #6482 (merged) — closing as superseded.

@ranxianglei ranxianglei reopened this Nov 14, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

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

状态确认(已 live 核实,非凭旧记忆):

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

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

Acknowledged — verified #6482 is merged (2026-11-14T01:11:17Z, head d04a8ad), so this PR is superseded. Final state of this branch: last synced to master afdf889c (head 5f00e99a, clean merge, delta intact); its only red lane was the inherited lifecycle-ledger-guard drift, which I reproduced on pristine master and reported in #4599 for sentinel convergence — it stands independent of this PR. Nothing further needed from my side here.

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

Labels

bug Something isn't working p2 pi scope/wiring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pi lane: no-proxy direct send is unguarded — oversized raw history egresses and blows the provider window

2 participants