Skip to content

[PR] refactor: unify relay recovery re-sends through one egress chokepoint (#6003) - #6028

Merged
ranxianglei merged 1 commit into
masterfrom
2026-11-09_6003-unified-egress-chokepoint
Nov 9, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-11-09_6003-unified-egress-chokepoint

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What

Unifies the five recovery re-send rungs in forwardCaptured (src/server/relay.ts) through a single egress chokepoint, finalizeEgress(). Each rung used to re-inline the same two-step egress tail before its fetchWithTimeout — re-sign (#1884) then v1internal re-wrap (#3129); that tail now lives in one place instead of five copies.

This is groundwork for the user-registerable egress-transform pipeline (#6003): it gives the future pipeline exactly one hook to attach to (inside finalizeEgress, before the re-sign, so the signature always covers the shipped bytes). The pipeline itself is NOT added here.

Zero behavior change

Byte-identical bodies, headers, and fetch calls across all five resends (ws-byte-shed, role-ladder, pck-strip, image-count-shed, context-overflow refold). Only the duplication is removed. Verified locally: typecheck clean + 119 targeted tests green on the rebased head (the five mechanisms + fake-completion + egress-wiring + window-fuse + main-path-retry). Full-suite verdict comes from CI.

Why the main send & fake-completion stay inline

  • Main send keeps its inline re-sign → wrap because the pre-send gate stack (outbound-body check, egress window fuse, seam forensics, image-fact capture) consumes the signed inner body between the two steps — merging them atomically would change what those gates see.
  • fake-completion (resolveFakeCompletion) is a separate option-based function whose wrap → sign order differs from every other site; preserving it byte-for-byte is intentional. Its differing order is flagged for the pipeline-phase hook-point design.

The compress-loop lane (src/loop/core.ts fetchUpstream) is already a single chokepoint receiving resign/wireTransform as options — the pipeline will pass its transform the same way; not touched here.

Related to #6003 (phase 1 of 2 — unification groundwork; the pipeline is pending owner sign-off on direction/naming/trust model).

…#6003)

The five recovery re-send rungs in forwardCaptured (ws-byte-shed, role-ladder,
pck-strip, image-count-shed, context-overflow refold) each re-inlined the same
two-step egress tail before their fetchWithTimeout: re-sign (#1884) then
v1internal re-wrap (#3129). Extract that tail into a single finalizeEgress()
so the final re-sign -> wrap decision lives in one place instead of five
copies, and so a future user egress-transform pipeline has exactly one hook to
attach to (before the re-sign, so the signature always covers the shipped
bytes).

Zero behavior change: byte-identical bodies, headers, and fetch calls; only the
duplication is removed. Groundwork for the user-registerable egress pipeline
(#6003); the pipeline itself is not added here. The main send keeps its inline
re-sign/wrap because the pre-send gate stack consumes the signed inner body
between the two steps; fake-completion keeps its own option-based path (its
wrap->sign order differs from every other site).
@ranxianglei ranxianglei changed the title refactor: unify relay recovery re-sends through one egress chokepoint (#6003) [PR] refactor: unify relay recovery re-sends through one egress chokepoint (#6003) Nov 9, 2026
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-11-09_6003-unified-egress-chokepoint (6b4757a)

Latest build — install this exact version

npm install -g billion-context@0.0.8801-pr.6028.b8c1191

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

Recent builds (commit → version)

commit version build
6b4757a billion-context@0.0.8801-pr.6028.b8c1191 build

Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr6028.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

Duplicate check: not a duplicate. No other open PR targets the #6003 egress-transform groundwork or the relay egress chokepoint — PR #6028 is the only one (checked the mirror's open [PR] rows + GitHub).

I pulled the branch and did an independent review of the diff (single commit, src/server/relay.ts only, +13/−10). Short version: the refactor itself is correct and genuinely zero-behavior-change, but the branch needs a rebase and one more site picked up before it can merge.

What I verified (zero behavior change holds)

  • finalizeEgress(variant) is exactly the two-step tail each rung used to inline: applyResign(headers, variant); return v1Wrap(variant);.
  • All five converted sites feed the same variable into both steps (shrunkWire, fixed.body, strippedPckBody, countShed.body, wireRefolded), so the sign-the-inner-body → ship-the-wrapped-body ordering is preserved byte-for-byte. The JS eval-order shift (object literal built around the call vs. two sequential statements) is immaterial: by the time fetchWithTimeout runs, headers is re-signed on that variable and the body is v1Wrap(...) either way; neither applyResign nor v1Wrap reads init.
  • The main send (applyResign at relay.ts:697 / v1Wrap at :822) and resolveFakeCompletion are not in the diff, so they're untouched — trivially unchanged.
  • Your note that fake-completion can't share the chokepoint is correct and independently confirmed: it runs wrapBody first, then resign on the already-wrapped bytes (sign-over-wrapped, ship-wrapped) — the inverse of the other sites' sign-inner→ship-wrap, so forcing it through finalizeEgress would change what gets signed. Good catch leaving it out.

Two things to handle before merge (neither needs a design decision)

A. Rebase to current master. The head is stale (behind master by several commits incl. #5998 and #6006/#6009). I dry-ran the rebase: it's clean — the result is still only src/server/relay.ts, +13/−10, no conflicts.

B. Pick up the sixth rung (#5998). Master's #5998 (already merged) added a new recovery re-send — the strict thinking-signature strip-and-resend probe — which uses the identical applyResign(headers, dropped.body); … body: v1Wrap(dropped.body) pattern. On the rebased tree it sits at src/server/relay.ts:1371-1372 and does not go through finalizeEgress. So once rebased there are six matching rungs but only five route through the chokepoint — which undercuts the stated purpose ("gives the future pipeline exactly one hook"): when #6003 lands, a user transform would apply to five rungs and silently skip the thinking-probe rung. It's the same controlled-experiment resend discipline as the pck ladder you did unify. The fix is the same 2-line swap as the other five (byte-equal); alternatively, state explicitly in the PR why the probe is excluded.

Design note for the #6003 phase (non-blocking here)

The main send stays inline because the pre-send gate stack (outbound-body check, egress window fuse, image-fact capture) sits between the sign and the wrap/fetch. Consequence: the chokepoint covers only recovery re-sends, not the primary send. If the future user-registerable transform is meant to hit all outbound bodies, the main send needs its own hook-point decision relative to that gate stack — worth settling in the pipeline phase, not this groundwork.

CI

No failures on the current head — the core CI lane + Windows codex overlay + pi-codex-ws e2e are green; a couple of e2e shards were cancelled (concurrency churn, not red). It'll need a fresh run once rebased to master.


结构化八问(PR review 结论)

  1. 是什么问题:这是一个重构(非缺陷修复)——把 forwardCaptured 里五处「恢复重发」(ws-byte-shed / role-ladder / pck-strip / image-count-shed / context-overflow refold)各自内联的「重签(bili 改写带 body 签名的模型请求,CodeArts(华为 APIG SDK-HMAC-SHA256)全部 401 #1884)→v1internal 重包([bug] bili antigravity (#2115 / #2134) 真机实测必现 HTTP 400:CloudCode v1internal 请求/响应信封未拆包 + sessionId 恒为 FNV-1a 空串哈希 #3129)」两步尾收拢成一个出口函数 finalizeEgress,为后续用户可注册的出站变换管道(# [Feature] Support user-registerable egress request body rewriting plugin pipeline | # [Feature] 支持用户可注册的出站(egress)请求体改写插件管线 #6003)留唯一挂钩点。
  2. 什么原因导致的:历史上五个恢复重发档位各自复制粘贴了同一套「重签+重包」尾,属重复代码;本 PR 做去重,不修任何行为 bug。
  3. 影响范围与严重程度:纯内部实现去重,零行为变化(我逐处核实过字节等价)。对最终用户无可见影响;不涉及可用性/缓存缺陷。
  4. 怎么修复的:抽一个 finalizeEgress(variant){ applyResign(headers,variant); return v1Wrap(variant); },把五处的两行替换成一次调用;主发送与 fake-completion 因语义不同故意保持内联(fake-completion 是「先包后签」且签的是包外字节,顺序与其它档位相反,不能共用)。
  5. 是否有回归问题:无。五处转换前后都是「先对内层体签名、再发包外体」,变量逐处一致;JS 求值序差异不影响最终发出的请求。主发送/fake-completion 不在 diff、原样未动。高危模式逐项过:无语义漂移、无 fail-fast 变吞错、无阈值/时序/输出格式变化。
  6. 是否破坏缓存:否。签名覆盖的字节与发出的 body 字节逐一不变,v1internal 信封结构不变,前缀缓存不受影响。
  7. 是否改了设计方向:本 PR 不改方向,是 # [Feature] Support user-registerable egress request body rewriting plugin pipeline | # [Feature] 支持用户可注册的出站(egress)请求体改写插件管线 #6003 的地基(第 1/2 阶段)。但有两点要在管道阶段拍板:(a) 主发送目前不走 chokepoint(前置门栈夹在签名与发包之间),若未来用户变换要覆盖「所有出站体」,主发送需单独设计挂钩点;(b) 见下方合入前提 B——Violates Anthropic's constraint that "preserved thinking must be replayed verbatim." | 违反了 Anthropic「preserved thinking 必须原样重放」的约束 #5998 新增的第 6 处同模式重发没走 chokepoint。
  8. 是否引入了新的配置:否。无新 env/config/默认值。

合入判定:重构本身正确、零回归、diff 干净、方向合理。合入前必须处理两点机械收尾(都不依赖决策取舍):A rebase 到当前 master(已验证干净);B 把 #5998 那个第 6 处 thinking 探针重发也转成 finalizeEgress(两行、字节等价),或显式写明为何排除它。按 [PR] 规则(CI 未红、你尚未要求改动)我先不动分支——要我直接把 rebase + 第 6 处转换推上去吗?一句话确认我就做。

@ranxianglei
ranxianglei merged commit 539c56c into master Nov 9, 2026
92 of 132 checks passed
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.1125-master.539c56c (rolling master tag).

npm install -g billion-context@0.0.1125-master.539c56c  # 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 ranxianglei mentioned this pull request Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant