Skip to content

fix: re-sign the preflight summarization egress over its own body (#3226) - #3234

Merged
ranxianglei merged 2 commits into
masterfrom
2026-10-15_preflight-resign-egress
Oct 15, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-10-15_preflight-resign-egress

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

是什么问题

CodeArts / 华为 APIG 签名车道(SDK-HMAC-SHA256)上,会话一旦超过模型窗口,预检压缩(preflight compression)必然失败:用户看到 502 preflight_compress_failed … summarization call was rejected by the upstream (HTTP 401): … body hash mismatch x-sdk-content-sha256 header。#1884/#1934 的重签名分支在主转发上是激活的(日志有 re-sign arm engaged),但预检摘要这条出口没有重签——压缩在签名车道上超窗即死。

根因(已在当前 master 源码核实,非仅看 reporter 引用的 0.1.197 dist)

主转发有重签名:src/server/relay.ts 的 applyResign 闭包对每个出口体调用 resignApig(...)。nudge-decide 侧信道(src/server.ts runNudgeDecision,#2228)也早已对它自建的重建体重签——这是本修复镜像的既有先例。reasoning-guard(src/reasoning-guard.ts)同样已带 resign 回调。

缺口是孤立的:requestSummaryBody(src/preflight.ts:1226)发送时用的是 { "content-type": "application/json", ...summaryHeaders(deps) },没有任何重签钩子。summaryHeaders 克隆了几乎所有入站头(含 authorization 和针对原始请求体计算的 x-sdk-content-sha256),却配上一个 bili 自己新建的摘要体 → 网关按新体重算 body hash → 不匹配 → 401 → bili 把 401 当作不可重试的鉴权拒绝 → fail-fast 502。

修法(策略层)

让预检摘要出口走与主转发同一条重签路径,对它自己的字节重签,而不是沿用入站签名:

  • PreflightDeps 增加可选 resign?(headers, body) 钩子;requestSummaryBody 先把出站头集合构好,再让调用方对摘要自己的字节重签后才 fetch。
  • 抽出共享的 inboundResignCredential(reqHeaders, opts, upstreamUrl),主转发、nudge-decide 侧信道、预检出口三条体覆盖车道统一从同一凭据源解析;relay/nudge 里原先内联的判定成为纯去重(行为不变)。
  • 回归测试把一个签名武装请求驱动到超窗,经一个逐次校验每个签名的 mock upstream:修复前精确复现上报的 401 "declared payload hash != sha256(body)" → 502;修复后压缩+转发成功(200),无 401、无 marker 泄漏。

验证

  • 修复前(stash 掉 src/ 改动后跑新测试):失败,错误正是 issue 的症状——[preflight] summarization failed: HTTP 401 … declared payload hash != sha256(body) → 502 preflight_compress_failed。
  • 修复后:同一测试通过,日志显示 preflight 折叠 3 段(~11841 tokens saved)、re-sign arm active、转发 200。
  • 定向套件全绿(本地只跑相关范围,全量交给 CI):tests/apig-resign.test.ts(39)、tests/issue2260-inert-resign-passthrough.test.ts(4)、tests/issue1993-preflight-400.test.ts(5)、tests/preflight-compress.test.ts(5)、新测试(1) = 54 pass / 0 fail。tsc --noEmit 干净。

Config surface

无。没有新增/改名/改语义任何配置字段、env var、CLI flag。复用的是既有 resign 凭据通道(x-bili-resign / x-bili-resign-credential),不引入新形状。

Behavior change(有意变更披露)

  • 签名车道:预检摘要出口现在会携带针对摘要自身体计算的新鲜签名,而不是沿用入站(针对原始体)的旧签名。旧值→新值:出站 authorization/x-sdk-date/x-sdk-content-sha256 从「入站克隆」变为「按摘要体重算」。原因:旧值会让网关 body hash 校验失败(401),这正是本 bug。
  • 未签名流量:完全不变(resign 钩子仅在凭据可解析时注入)。
  • 未触碰 wire 消息形状、协议结构、persistence、kernel/。

Closes #3226

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-15_preflight-resign-egress (d039926)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.3766-pr.3234.0783cd0

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-pr3234.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],开始独立审核(不照抄 issue 侧结论)。

查重:不重复。 按失败签名(preflight + resign / body hash mismatch / x-sdk-content-sha256)检索 open+closed issue 与 PR:在途修同一机制的只有本 PR;同族的 #1884/#1934/#1885/#1886/#2090 均已关闭且修的是「签名请求能否进 bili / 主转发能否重签」,与「预检摘要出口漏重签」是不同出口、不同机制。无并行重复工作。

初步状态:分支 2026-10-15_preflight-resign-egress 单 commit 3e5c48b,base 为当前 master(无陈旧基线);GitHub 侧 mergeable=True、merge_state=behind(需复核 master 是否已前进);CI 多数 lane 刚入队(build-artifact / theorem / real-codex 已绿)。

接下来按规范走完整审核:原 issue 方向关 → 回归审查 → 代码审查 → 测试有效性 → diff 整洁度,本地只跑定向套件 + typecheck,全量以远程 CI 为准。结论稍后按结构化八问给出。

)

On CodeArts/Huawei-APIG signed lanes (SDK-HMAC-SHA256), the #1884 re-sign arm
covered the main forward but NOT the preflight summarization call. That call
sends a body bili built itself while still carrying the inbound signature
(computed over the ORIGINAL request body), so the gateway recomputes the body
hash, sees a mismatch and answers 401 -- which bili treats as a non-retryable
auth rejection, so the whole turn fail-fasts 502 preflight_compress_failed.
Compression was dead on signed lanes the moment a session crossed the window.

- PreflightDeps gains an optional resign(headers, body) hook; requestSummaryBody
  builds the outbound header set first and lets the caller re-sign it over the
  summary's OWN bytes before fetch.
- Extract inboundResignCredential() shared by the main forward, the nudge-decide
  side channel and now the preflight egress, so every body-covering lane resolves
  the same credential source; the relay/nudge inline computations become pure dedup.
- Regression test drives a signed armed request past the window through a mock
  upstream that validates every signature: pre-fix it fails with exactly the
  reported 401 "declared payload hash != sha256(body)" -> 502; post-fix it
  compresses + forwards (200) with no 401 and no marker leak.
@ranxianglei
ranxianglei force-pushed the 2026-10-15_preflight-resign-egress branch from 3e5c48b to 64e5e33 Compare October 15, 2026 03:51
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

审核进展(续上条):分支已 rebase 到当前 master(effd544,含 #3218/#3210/#3213/#3195 等 ~30 个新提交)并推送,无冲突、单 commit 保持干净。rebase 前我核对过:最初 diff origin/master..branch 里出现的 kernel/google.ts 变更与大量"被删测试文件"是分支基于旧 master 的陈旧基线假象,不是本 PR 的内容——实际提交只碰 4 个文件(src/preflight.ts、src/server.ts、src/server/relay.ts、新增回归测试)。

独立复现了红/绿两端,不采信 PR 自述:

代码层面已核完的关键点:inboundResignCredential 抽取与 relay/nudge-decide 原内联逻辑逐行等价(短路顺序、输入完全一致,两条既有车道行为不变);signApigHeaders 重签前先大小写不敏感地删掉全部旧签名头(authorization/x-sdk-date/x-sdk-content-sha256/x-security-token/maas_type),不存在双头泄漏;重试循环每次重建头集并重签(x-sdk-date 不会跨重试过期);钩子仅在凭据可解析时注入,未签名流量零变化;错误处置与 relay applyResign 先例一致(warn + 沿用旧签,让上游 401 原样可见)。新测试的校验器是独立按 wire 格式重算签名(与签字端零共享代码),断言有牙:200 而非 502、preflight 确实触发、summary+forward 两次出口调用、零 401、arm marker 不出网。

现在等 CI 在推送头上跑完,绿了给最终八问结论。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

Review 结论:可合并 ✅(八问体例)

1. 是什么问题:CodeArts / 华为 APIG 这类 SDK-HMAC-SHA256 签名车道上,会话一旦超过模型窗口,预检压缩必然失败——用户看到 502 preflight_compress_failed,上游回 401 body hash mismatch。这类车道压缩在超窗那一刻就死了。

2. 什么原因导致的:三条「覆盖请求体」的出口车道里,只有预检摘要这一条缺重签钩子。主转发(relay.ts applyResign)和 nudge-decide 侧信道(#2228)早已对各自重建的体重签;而 src/preflight.ts 的 requestSummaryBody 用 summaryHeaders() 克隆全部入站头(含针对原始请求体算好的 authorization / x-sdk-content-sha256)去发 bili 自己新建的摘要体 → 网关按新体重算 body hash → 不匹配 → 401 → bili 视为不可重试鉴权拒绝 → fail-fast 502。孤立缺口,不是系统性缺陷;reporter 给的方案方向(对摘要自己的字节重签)打到根因,正确。

3. 影响范围与严重程度:仅 SDK-HMAC-SHA256 签名车道 × 会话超窗;该场景下压缩完全不可用(整轮 turn 502)。未签名流量、未超窗会话、其他 provider 不受影响。严重度高、场景面窄。

4. 怎么修复的:让预检摘要出口走与主转发同一条重签路径——PreflightDeps 增加可选 resign(headers, body) 钩子,fetch 前对摘要自身体重签;抽出共享 inboundResignCredential(reqHeaders, opts, upstreamUrl),主转发 / nudge-decide / 预检三条车道统一凭据源,relay/nudge 原内联逻辑变纯去重(短路顺序与输入逐行等价,行为不变);错误处置沿用 relay applyResign 既有先例(warn + 沿用旧签,让上游错误原样可见,不自造错误)。

5. 是否有回归问题(既有行为逐项清点):

  • 主转发重签:helper 抽取逐行等价,apig-resign 套件 39/39 绿 → 保持;
  • nudge-decide 侧信道:输入不变(req.headers / opts / target.upstreamUrl)→ 保持;
  • 未签名流量:钩子仅在凭据可解析时注入,零变化(issue2260 inert-passthrough 4/4)→ 保持;
  • 预检非签名路径:issue1993-preflight-400 5/5 + preflight-compress 5/5 → 保持;
  • 重试语义:重试循环每次重建头集并重签(x-sdk-date 不跨重试过期),401 仍 fail-fast → 保持;
  • arm marker(x-bili-resign / credential)不出网:新测试对每次出口调用逐一断言 → 保持;
  • wire 消息形状 / 协议结构 / persistence / kernel/:未触碰;
  • 高危模式逐项过:无语义漂移、无 fail-fast 变吞错、无阈值/时序/输出格式漂移。结论:无回归。

6. 是否破坏缓存:不涉及。前缀缓存路由(session id / system-hash 键控)未动,重试预算、去重键、增量游标均未受影响。

7. 是否改了设计方向:没有。按既有 #1884 重签臂的设计精确补上孤立缺口,不引入新机制,架构/边界/数据流不变。

8. 是否引入了新的配置:无。复用既有 x-bili-resign / x-bili-resign-credential 凭据通道,零新形状。

行为变更(有意披露):签名车道上,预检摘要出口的出站 authorization / x-sdk-date / x-sdk-content-sha256 从「入站克隆(针对原始体)」变为「按摘要自身体重算」。旧值→新值:网关 401 body-hash-mismatch → 正常受理。原因:旧值正是本 bug 根因。未签名流量完全不变。

验证证据(全部独立复现,未采信 PR 自述):

判定:可合并。 请手动合并:#3234

…#3226)

The #2952 bounded tier-distillation recovery funnels through the SAME
requestSummaryBody egress as the normal preflight round, so the signed-lane
401 kills it too -- but its failure log only printed error.message ("upstream
error 401"), dropping the UpstreamHttpError.body that names the real cause
("body hash mismatch x-sdk-content-sha256 header"). That made the turn's
fail-fast 502 read as "futile round / net ~0 tokens saved" (exhaustion) instead
of a signature rejection. Surface a bounded body snippet like the sibling
normal-round log already does, so this second visible form self-diagnoses.
@ranxianglei
ranxianglei merged commit 6b5b283 into master Oct 15, 2026
43 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.452-master.6b5b283 (rolling master tag).

npm install -g billion-context@0.0.452-master.6b5b283  # 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.

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