Skip to content

[PR] fix: give-up direct send must not ship the lane-rewritten payload (#3304) - #3535

Closed
ranxianglei wants to merge 4 commits into
masterfrom
2026-10-17_issue3304-incremental-giveup-restore
Closed

ranxianglei wants to merge 4 commits into
masterfrom
2026-10-17_issue3304-incremental-giveup-restore

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #3304

What was broken

When the bili proxy dies mid-session, the fetch intercept degrades to a DIRECT send of the model request (the documented #1130 fallback). If an incremental lane had already swapped that request's body into its envelope (seed / delta / resync — {conversationId, sinceSeq, path, request{...}, messages}), the bare upstream cannot parse it: the old code shipped the rewritten body anyway. Same for a pre-fetch swap (opencode) or the pi extension's payload replacement. Result: silent content corruption on exactly the turns where the user is already losing compression.

The fix

Every one of the six direct-send exits in src/agent/native-intercept.ts now consults an optional state.directGuard armed by each lane that swaps bodies:

  • dsh / omp capture (body, headers) at swap time, keyed by init identity (WeakMap); the guard restores them.
  • opencode captures the pre-replacement Request in a WeakMap<Request, Request>; the guard rebuilds a direct request from it (refuses if its body stream was already consumed).
  • pi notes the original event.payload in a shared per-sid ledger (incremental-client.ts, TTL-bounded); the pi-native guard reads the sid from x-bili-plugin-conversation and restores. The ledger entry is cleared on every non-swap path so an entry only ever exists while the current request was actually swapped.

Verdict semantics: restored pair → sent verbatim; unrestorable or throwing guard → fail closed with a local 503 + x-bili-gate:giveup-restore-refusal (+ retry-after: 1, provider-shaped error body) instead of shipping a lane envelope to a provider that cannot read it. A dead proxy is transient infrastructure state, not a request error — the next turn re-plans against the respawned proxy.

Behavior change (old → new)

Path Old New
Proxy death / bootstrap failure / unattributed passthrough with a SWAPPED body lane envelope shipped bare to the upstream (silent corruption) original payload restored and sent; if unrestorable → local 503 retryable
Same paths with an UNswapped body sent verbatim byte-identical (pinned by regression tests, incl. the two existing #1130 degradation tests)
Recovery-retry onto a live proxy delta body unchanged (delta is correct for a live bili)

No config surface added. No kernel change. No wire-format change for any working path.

Evidence

New suite tests/issue3304-giveup-restore.test.ts (10 tests): intercept-level coverage of every exit class (rewrite give-up restore, unswapped byte-identical anchor, refuse → local 503 with zero upstream traffic, throwing guard → fail closed, routed give-up, pre-swap bootstrap failure) plus lane-level restore proofs for omp / opencode (real probe server + real swap) and the pi ledger semantics (note/consume/clear/TTL) + pi-native guard header routing.

Local targeted run on the rebased head (origin/master @ 3f513c2): typecheck clean, 217/217 across issue3304-giveup-restore, native-intercept, native-intercept-selfheal, incremental-lane, incremental-client-recovery, opencode-incremental, omp-native-incremental, issue3343-incremental-divert-authz, apig-resign, pi-omp-fork-adopt, plugin-fork; build green incl. the dist zod gate. Full-suite verdict: remote CI.

@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-17_issue3304-incremental-giveup-restore (ee5e9d6)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.4209-pr.3535.59c1064

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-pr3535.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

收到,开始独立审阅本 PR。先报查重结论(动手前):

查重: 重复 —— 与 PR #3529 同 issue(#3304)、同症状、同机制。 两个 PR 都 Closes #3304、都在修「give-up 降级直连把增量车道改写后的 envelope 原样发给裸上游」这一个根因,只是实现形态不同(#3529 = prepareDirectSend 钩子 + turn-id token stash;#3535 = directGuard 状态钩子 + 换体瞬间 WeakMap 捕获 + pi per-sid 账本)。按「一个 issue 至多一个 PR」的 loop guard,二者只能留一个——留哪个我审完代码和 CI 再给结论(先到的 #3529 目前 mergeable_state=dirty 与 master 冲突,#3535 声称已 rebase 到 3f513c2,质量与可合性会一起纳入判断),不会两个都挂着等合并。

接下来按 PR Review 规范走:①原 issue 方向核对 → ②既有行为回归清点 → ③逐行代码审查 → ④测试有效性 → ⑤diff 整洁度/代码质量,本地只做定向测试,全量看 CI。有结论按八问体例跟帖。

ranxianglei added a commit that referenced this pull request Oct 17, 2026
…re suite

Review supplement for #3535: the intercept-level suite covered the
respawn-fail, routed give-up and bootstrap-fail exits, but not the
unattributed gate-fail direct send — the third exit reachable with an
opencode pre-swapped envelope (the V2 hook swaps before fetch, so the
body is already the lane envelope when the attribution gate refuses the
caller). Add the missing case: refused attribution + pre-swapped body
must travel upstream as the original payload.
@ranxianglei
ranxianglei force-pushed the 2026-10-17_issue3304-incremental-giveup-restore branch from 3b4ab02 to 8d8d985 Compare October 17, 2026 11:03
…re suite

Review supplement for #3535: the intercept-level suite covered the
respawn-fail, routed give-up and bootstrap-fail exits, but not the
unattributed gate-fail direct send — the third exit reachable with an
opencode pre-swapped envelope (the V2 hook swaps before fetch, so the
body is already the lane envelope when the attribution gate refuses the
caller). Add the missing case: refused attribution + pre-swapped body
must travel upstream as the original payload.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

冲突已解决,rebase 完成并推送。分支现在在 master d92478944 之上,共 3 个提交:

cc1db2887 test(#3304): pin the unattributed gate-fail give-up exit in the restore suite
8d8d985f2 style(#3304): fix one-space indent drift on the beforeSend field (#3304)
bac862b46 fix(#3304): restore the original payload before any direct-send fallback (#3304)

冲突点(仅一处):src/agent/dsh-native.ts 的 import 行——master 侧(#3394/#3395 live-state slot)加了 liveStateSlotEffect,本 PR 加了 fetchMethodOf,按两侧取并集解决。native-intercept.ts 自动合并成功,已逐段核对:六个 direct-send 出口全部挂上 directGuardVerdict(本 PR 语义完整保留),#3394 的 live-state slot 跟随逻辑未受影响。

⚠️ 过程披露:我 fetch 之后、推送之前,分支上被追加了一个评审补充测试提交(3b4ab0287,unattributed gate-fail 出口的回归钉住)。首次 force-push 把它覆盖了——发现后立即 cherry-pick 回 rebased 分支(现 cc1db2887),用显式 lease 二次推送。三个提交全部在位,无内容丢失。教训记下:rebase 前必须对目标分支做最后一次 fetch 比对,不能依赖会话开始时的快照。

顺手小修(独立标注 commit 8d8d985f2):beforeSend 字段一行缩进漂移(多一个空格)——这是 PR 原提交里就有的,不是合并引入的,已修正。

验证(rebased head 本地定向):typecheck clean;build green(含 dist-external-zod: OK);定向套件 issue3304-giveup-restore、native-intercept、native-intercept-selfheal、incremental-lane、incremental-client-recovery、opencode-incremental、omp-native-incremental、issue3343-incremental-divert-authz、apig-resign、pi-omp-fork-adopt、plugin-fork 连续两轮 220/220 全绿(首轮出现过 1 个失败,重跑不复现,判定本地 flake,以 CI 为准)。cherry-pick 回来的新测试单独跑过,通过。全量回归看远程 CI。


八问

  1. 是什么问题:上游 PR 与 master 产生合并冲突(dirty),分支无法进入可合状态——本质是 [BUG] DSH 插件热替换后 fetch 拦截仍绑在旧模块实例上 → 模型请求全部直连、压缩静默失效直到重启 #3394/fix(agent): follow the live intercept state across host hot-reimport (#3394) #3395 与本 PR 同时改了 native-intercept.ts/dsh-native.ts 的同一区域。
  2. 什么原因导致的:master 在 PR base(3f513c234)之后合入了 fix(agent): follow the live intercept state across host hot-reimport (#3394) #3395(dsh hot-replace live intercept)+ ui: move the dsh settings Open-WebUI button up to the tab row (after the logs tab) #3543 + fix: dsh desktop settings pane — admit the dsh-app://app Electron origin through the admin trust gate (#3513 desktop variant) #3537,其中 [BUG] DSH 插件热替换后 fetch 拦截仍绑在旧模块实例上 → 模型请求全部直连、压缩静默失效直到重启 #3394/fix(agent): follow the live intercept state across host hot-reimport (#3394) #3395 改动了本 PR 也改的 import 行与 intercept 状态机,文本冲突落在 import 行。
  3. 影响范围与严重程度:仅本 PR 分支不可合;不影响任何已发布行为。属于流程性阻塞,非功能缺陷。
  4. 怎么修复的:rebase 到当前 master,import 行按「两侧符号并集」解决(两个符号在合并后的 native-intercept.ts 中都存在且都被使用),语义级核对确认双方改动无相互覆盖;期间覆盖的评审补充提交 cherry-pick 找回。
  5. 是否有回归问题:无。既有行为逐项过:unswapped body 直连路径字节不变(directGuard 返回 undefined 时走原默认构造)、[BUG] DSH 插件热替换后 fetch 拦截仍绑在旧模块实例上 → 模型请求全部直连、压缩静默失效直到重启 #3394 live-state slot dispose/generation-guard 逻辑原样保留、give-up 恢复/fail-closed 语义由 220 个定向测试钉住。
  6. 是否破坏缓存:不涉及——本次只动 agent 插件侧的 fetch 拦截层,不碰压缩状态、持久化或任何缓存键。
  7. 是否改了设计方向:不涉及——纯冲突解决 + 一行缩进小修,方向与 PR 原设计一致。
  8. 是否引入了新的配置:不涉及——无新 env/config/默认值。

收工判定:分支已与 master 干净对齐、可合状态待 CI 绿灯;评审结论(含与 #3529 的去留取舍)仍由评审线程给出,本次仅完成冲突处理。

…dVerdict aliases (#3304)

The unused-exports gate (scripts/check-unused-exports.mjs) flags both type
aliases: they are referenced only inside native-intercept.ts (the lane guards
are typed structurally through NativeInterceptState.directGuard). Dropping
the leading `export` is the gate's prescribed INTERNAL_ONLY fix; no import
changes anywhere. Reviewer-side CI fix on top of cc1db28.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

Review 结论:可合并(两件待你拍板的事见文末)

结构化八问

  1. 是什么问题:bili proxy 会话中途挂掉时,fetch 拦截层按 native attach 模式无运行时 respawn:共享代理的属主退出后,attach 方全部模型请求永久硬失败 #1130 文档化兜底「直发上游」。但如果增量 lane 已把该请求 body 换成自己的信封格式({conversationId, sinceSeq, path, request{…}, messages}),裸上游解析不了——旧代码照样把信封发出去,用户在已经丢压缩的回合上再吃一次静默内容损坏。dsh/omp/opencode/pi 四条 lane 全中。当前 rollout 0%,线上零暴露。
  2. 什么原因导致的:换 body 发生在 fetch 层之前/之内,give-up 直发出口在 fetch 层内部——两层互相不知情。native-intercept.ts 六个直发出口(gate-fail、routed give-up、signed passthrough、unattributed、proxy-not-ready、respawn-fail)都默认「body 还是原请求」。根因是层间缺少恢复交接,不是某一个出口写错。
  3. 影响范围与严重程度:触发条件窄(server gate 开 + unified-storage sidecar + probe 30s 内活 + 发送时 proxy 死且 respawn 失败);触发后 opencode/omp 坏一个回合(有 30s singleflight 保护),dsh/pi 连续坏 ≤30s;无数据丢失(host 留全量历史,delta 没到过 bili,server canonical 未动),下一轮自愈。定性:潜伏缺陷,今天零暴露,rollout 一开就是真事故。
  4. 怎么修复的:谁换了 body 谁负责还原——每个 lane 在 intercept state 上挂一个可选 directGuard:dsh/omp 用 WeakMap 在 swap 时捕获 (body, headers);opencode 用 WeakMap 捕获替换前的 Request;pi 用 per-sid TTL ledger(每条非 swap 路径都清 entry,保证 entry 只存在于当前请求确实被换过期间)。六个出口统一走单一 chokepoint:还原成功 → 原样发出;不可还原/guard 抛错 → fail closed,本地回 503 + x-bili-gate:giveup-restore-refusal + retry-after: 1(provider 形状错误体),绝不把信封发给读不懂的上游。proxy 死是瞬态基础设施状态,不是请求错误——host 下一轮对着复活的 proxy 重发。
  5. 是否有回归问题:逐项清点过——
    • 六个出口在未 swap 路径:与旧行为逐字节一致(PR 带回归测试钉住,含两个既有 native attach 模式无运行时 respawn:共享代理的属主退出后,attach 方全部模型请求永久硬失败 #1130 degradation 测试);
    • routed give-up 保留 makeTarget(routedTarget) 的 Request-attribute 语义(guard 三值契约 pair/refuse/undefined 正是为此保住它);
    • 唯一新行为只在「swap 过 + proxy 死」路径:旧=信封裸发上游(损坏),新=原始 payload 发出或本地可重试 503(行为变更披露:old→new 如上,原因=不再让上游收到不可解析体);
    • onDispatch 分类新增 refused 一类,只在 fail-closed 时触发;
    • native-intercept.ts 全部 send() 调用点逐一枚举:proxy-bound 发送、!isModelApiUrl passthrough、url-undefined 早退按构造不可能携带信封(各 lane 只换 model 请求)——无遗漏直发出口;
    • 与 master 上 fix(agent): follow the live intercept state across host hot-reimport (#3394) #3395(hot-replace live-state)交互验证:单次 fetch 内 state 解析一致,无跨 state 孤儿;文本合并仅 dsh-native.ts import 行一处 union(已由 rebase 解决)。
      远程 CI 全绿(见下)。
  6. 是否破坏缓存:不涉及——不动 prefix cache、重试、增量游标、去重键;recovery-retry 对活 proxy 仍发 delta,语义不变。
  7. 是否改了设计方向:无。guard 是 NativeInterceptState 上的可选字段,非 lane host 零感知,不引入新边界。
  8. 是否引入新配置:无。无新 env/config/默认值。

测试有效性:tests/issue3304-giveup-restore.test.ts(11 例)断言有牙——fakeFetch sink 证明「routed 尝试携带 ENVELOPE、直发携带 ORIGINAL」;refuse 例断言上游流量为零;throwing-guard 例断言 fail closed;omp/opencode 是 lane 级真实 probe server 证明;pi ledger 覆盖 note/consume/clear/TTL。原 bug 回归时抓得住。

review 中发现的小问题,已直接修进 PR 分支(标注 commit):

  • test(#3304): pin the unattributed gate-fail give-up exit…(cc1db28):unattributed 出口经 opencode pre-swap 形状可达但无测试,补一例 intercept 级测试;
  • fix(ci)(#3304): stop exporting the internal DirectSendPair/DirectGuardVerdict aliases(ee5e9d6):两个 type alias 被 export 但无任何模块 import,unused-exports 门禁红并卡住全部 test lane,按门禁 INTERNAL_ONLY 处方去掉 export。
    另两个同模式出口(gate-fail routed passthrough、signed-passthrough opt-in)同样缺专属 intercept 级测试,但与已覆盖出口共用同一 chokepoint 代码路径,不拦合并。

CI 证据(远程,head ee5e9d6):主 CI 19/19 jobs 全绿(unused-exports/typecheck/build/全部 test shards ubuntu+windows × node22+24/red-signal/static-gates)+ e2e 全 lane 全绿(real codex、native-dsh/opencode/pi、codex-persona、fake-codex、local-registry、windows overlay、pi-codex-ws)。唯一红的 .github/workflows/ci-gate-plugins.yml 是 manual-only workflow 记录的空 push-event run(0 jobs 即 fail),历次 push 皆有,与本 diff 无关。分支已 rebase 到 master d924789;master 其后仅前进 release v0.1.198 的版本号 plumbing(package.json/package-lock 两文件),merge-tree 验证对最新 master b10c525 零冲突。

重复 PR 处置建议(待你拍板):#3529 与本 PR 重复(同 issue #3304、同症状、同机制,首条回复已给查重结论)。建议保留本 PR、关闭 #3529 为 superseded:本 PR 已 rebase 干净、覆盖全部四条 lane(含 triage 后期才发现的 pi)、fail-closed 语义更强(本地 503 vs TypeError)、CI 全绿;#3529 对 master dirty,其设计(prepareDirectSend hook + turn-id token stash TTL 5min + pi envelope sniffing)已被团队自己的后续重做取代。注意:这偏离字面「先来后到」(会保留更早的 #3529),理由是后者在实现与状态上均严格更优——请确认或另行指示。

治理点(非阻塞,一句话):#3304 floor 14 写「收到拍板:方案一(A+B 混合)」,但 thread 里 floor 13→14 之间没有 owner 消息。实现方向与 issue 自身建议的方案 A + B 安全网一致,也符合你 floor 5 的指示(「这个master修复了吗 没有修复提交pr」),所以不拦;若你其实倾向纯 B(不还原、只拒绝),说一下我再调。

判定:方向对(根因修在源头层间交接,不在症状层堵),回归逐项清点无漂移,CI 全绿,发现的小问题已直接修——可合并。待你:① 合 PR(我不能合,请自行操作:https://github.com/ranxianglei/billion-context/pull/3535);② 确认关 #3529;③ 如介意,补一句 A+B 方案的正式确认。

@ranxianglei

Copy link
Copy Markdown
Owner Author

(ai: zhipuai-lb/glm-5.3)

陈旧票清理(第三批动作①):目标 issue #3304 已关。为避免重复劳动关闭本 PR;如判断有误请直接 reopen 并留言。

@ranxianglei ranxianglei changed the title fix: give-up direct send must not ship the lane-rewritten payload (#3304) [PR] fix: give-up direct send must not ship the lane-rewritten payload (#3304) Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant