Skip to content

[PR] fix: re-sign CodeArts traffic under the request's own account (#3106) - #3335

Merged
ranxianglei merged 3 commits into
masterfrom
wt-3106-ework-daemon
Oct 20, 2026
Merged

ranxianglei merged 3 commits into
masterfrom
wt-3106-ework-daemon

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

背景

dsh-codearts-auth(jet-hub 里的反代)配 bili 时「一半模型废掉」。根因在 bili 侧重签路径:bili 对每个出站 body 重新签名时,不沿用入站请求原本用的那个账号,而是自己在账号池里挑第一个能解析出来的凭据,再用一个全局单槽 10s 缓存把它钉死。多账号池因此塌缩成一个凭据——家账号 ≠ 池里第一个可用账号的那些模型,重签出来用的是别人的 AK/SK,上游按签名校验直接拒(表现为这些模型全挂)。单账号用户不受影响。

对应 owner 的「缓存 miss」直觉:是 bili 自己的凭据缓存把池子塌缩了,不是上游 KV。

修复思路

让 bili 用入站请求自己签名的那个账号去重签(AK 就在入站 Authorization: SDK-HMAC-SHA256 Access=<ak>,… 里),凭据缓存改为按 ak 分键。只有当池里找不到与入站 AK 匹配的账号、或请求没带 AK 时,才退回原来的「池序第一个可用」兜底。

改动点:

  • src/agent/native-intercept.ts:抽出共享的 headerRecordOf;新增 inboundApigAccessKey()(从 Authorization 解析 Access=<ak>);resignCredentialFor 类型加可选第三参 inboundAk(向后兼容)。
  • src/agent/dsh-native.ts:抽出纯函数 pickCodeartsResignCred()(同账号优先 → 兜底池序首可用,按 ak 分键缓存,逐候选 fail-soft),state.resignCredentialFor 收敛成薄封装。
  • tests/apig-resign.test.ts:4 个回归用例(AK 解析;同账号压过池序;按 ak 缓存跨账号不串号 + 命中不重复 resolve;未知/缺失 AK 回退首可用)。

⚠️ 行为变更(必读)

旧行为 新行为
重签用哪个账号 池里第一个能解析的 codearts 账号(全局单槽 10s 缓存,忽略请求本身的账号) 入站请求签名所用的那个账号(从 Access=<ak> 解析),缓存按 ak 分键
兜底 — 仅当无账号匹配入站 AK、或请求未带 AK 时,才退回首可用(旧语义)
单账号用户 X X(字节级一致,无变化)
多账号用户 非家账号模型被错误重签 → 上游拒 各模型用各自账号重签 → 正常

原因:旧实现把「请求该用哪个账号」和「bili 手里有哪些账号」两件事混为一谈,多账号下必然错配。属 wire-behavior 变更,需人工 review(不走 auto-merge)。

配置面

未新增任何配置/env/默认值。 账号选择由入站请求自身携带,无需用户配置。

测试

  • 定向跑 tests/apig-resign.test.ts:44/44 通过(含 4 个新回归用例;既有 40 个重签用例不变,无回归)。
  • tsc --noEmit 干净。
  • 全量套件以远程 CI 为准。

结构化八问

  1. 是什么问题:多账号 jet-hub 池里,bili 重签时用错了账号(用了池内首个而非请求自己的),导致非家账号的模型被上游拒。
  2. 什么原因导致的:重签凭据选择是「池序首可用 + 全局单槽缓存」,与「请求实际签名账号」脱钩。
  3. 影响范围与严重程度:仅多账号 codearts 池 + 触发压缩重签的请求;单账号不受影响。严重度=特定条件下不可用(条件:多账号且家账号≠池首可用)。
  4. 怎么修复的:从重签侧对齐到请求自身的账号(AK 从入站头解析),缓存按 ak 分键;保留首可用兜底。
  5. 是否有回归问题:单账号字节级一致;既有 40 个重签用例全绿;类型向后兼容(可选参数)。高危模式逐项:无语义漂移(同账号重签本就是插件原生行为)、无吞错(逐候选仍 try/catch)、无阈值/时序/输出格式变化、凭据缓存策略改为按 ak 分键(见第 6 问)。
  6. 是否破坏缓存:凭据缓存从「全局单槽」改为「按 ak 分键」,TTL 仍 10s——这是本次修复的一部分(单槽正是 bug 来源之一),不是意外破坏;上游 KV/前缀缓存不受影响(重签只改签名头,body 内容不变)。
  7. 是否改了设计方向:否。仍是「检测→取凭据→打 loopback marker→proxy 重签每个出站 body」的原架构,只是取哪个凭据更忠实于请求本身。
  8. 是否引入新配置:否。

Fixes #3106

@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: wt-3106-ework-daemon (ecaebbf)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.4647-pr.3335.f8a17e3

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-pr3335.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(#3335,head 5584b7256)修 #3106「多账号 codearts 池被凭据缓存塌缩成单凭据」。检索 open+closed 的 issue/PR(codearts / resign / resignCache / account-pool 一族):同机制无并列条目;家族里的 #1884/#1934/#2090 是更早的不同缺陷(401→重签臂→形状检测+二元契约),均已合入,与本 PR 是「同路径不同断点」,不重复。

身份确认:这是 ework-daemon 自己提的 PR(分支 wt-3106-ework-daemon)。按自 PR 规则不重做,只做独立 review + CI 核对 + 结论报告。

已核完的部分:

  • diff 与描述逐条对上(3 文件,+166/-38);headerRecordOf 抽取对 master 原文逐行忠实;接线 resignCredentialFor(url, scheme, inboundApigAccessKey(input, init)) 正确;全仓 grep 确认无遗漏的旧签名调用点;类型加的是可选第三参,向后兼容。
  • CI 在 head 5584b7256 上全绿:4 条 test lane(ubuntu 22/24 × shards + windows 22)success;static-gates + 全部 guard job(kernel-guard / pi-subagents-guard / version-guard / config-surface-guard / unused-exports / lane-guard / test-import-graph / docs-sync / lock-sync)success;build-artifact success。真客户端 e2e lane 按 env gate 正常 skipped。

发现一个测试覆盖缺口(正在补):新增的 4 个用例只把两半各自当纯函数测了(inboundApigAccessKey、pickCodeartsResignCred),但接缝没钉住——没有任何测试断言 intercept 真的把解析出的入站 AK 作为第三参传给 resolver(现有 armedState() 的 stub 是 async () => CRED,忽略入参)。万一将来这行接线漏传第三参,所有测试仍会绿、#3106 又静默退回旧的「池首」行为——而这正是本 PR 要修的那条链。我会在本 PR 分支补一个聚焦的接缝回归用例再给最终八问结论。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

Review 完成。独立走了一遍 diff(方向 / 回归 / 代码 / 测试有效性 / 整洁度),没有照抄 issue 侧结论。发现一个覆盖缺口并当场补进了本 PR。

补充动作(review 发现的小问题,已当场修进本 PR):新增 4 个用例只把两半各自当纯函数测了,接缝没钉住——没有任何测试断言 intercept 真把解析出的入站 AK 作为第三参传给 resolver(现有 armedState() stub 是 async () => CRED,忽略入参)。万一将来接线漏传第三参,所有既有测试仍会绿、多账号池又静默退回旧的「池首」行为——而这正是本 PR 要修的那条链。已在分支补一个聚焦接缝回归用例(commit 66f54e7b8):stub 捕获第三参并断言 == 请求自带的 Access=CLIENT。定向跑 tests/apig-resign.test.ts 45/45 通过(原 44 + 新接缝 1,含全部 5 个 #3106 用例)。CI 将在新 head 66f54e7b8 上重跑。

结构化八问(独立结论)

  1. 是什么问题:多账号 jet-hub codearts 池下,bili 对出站 body 重签时用了「池里第一个能解析的账号」而非「该请求原本签名的账号」,家账号≠池首的那批模型被上游签名校验拒(表现=这批模型全挂)。单账号不受影响。
  2. 什么原因导致的:重签取凭据=「池序首可用 + 全局单槽 10s 缓存」,跟「入站请求实际用哪个账号签的」完全脱钩;单槽缓存又把选中账号全局钉死,把整个池塌缩成一个凭据。(对上 owner 的「缓存 miss」直觉:是 bili 自己的凭据缓存塌缩了池子,不是上游 KV。)
  3. 影响范围与严重程度:仅多账号 codearts 池、且触发压缩重签的请求;单账号字节级不受影响。严重度=特定条件下不可用(条件:多账号且某模型家账号≠池首可用)。
  4. 怎么修复的:让重签对齐到请求自身账号——AK 明写在入站 Authorization: SDK-HMAC-SHA256 Access=<ak> 里,直接解析;凭据缓存改按 ak 分键;仅当无账号匹配入站 AK 或请求没带 AK 时才回退旧的「池序首可用」。改动集中在抽出的纯函数 pickCodeartsResignCred(dsh-native.ts)+ 新增 inboundApigAccessKey 解析器(native-intercept.ts)。
  5. 是否有回归问题:无意外回归。 逐项审计了触碰路径的既有行为:
    • 单账号路径:字节级一致(快路径找到的唯一账号 == 旧的首可用;无 AK 请求走兜底 == 旧语义,逐行等价)。
    • 无凭据 / 解析失败:仍逐候选 try/catch fail-soft 前进;credentialsService.resolve===undefined 守卫保留 → 仍 403 bili_resign_unavailable。
    • bodySignedSchemeOf 检测:经 headerRecordOf 抽取重构,Request.headers 分支逐行忠实保留、同一检测器委托,行为不变(resign 保护漏识别 dsh-our-free-model 的 x-ofm-signature,其 EAC 请求被改写后 401(建议:未知签名 scheme 默认直送) #2090 等检测用例全绿)。
    • marker 形状/编码(x-bili-resign*)、TTL 值(10s)均未变。
    • 类型加的是可选第三参,向后兼容;全仓 grep 确认仅一处调用点 + dsh-native 实现,无遗漏的旧签名调用者。
    • 高危模式逐项:无语义漂移(同账号重签本就是插件原生语义)、无吞错(fail-soft 同旧)、无阈值/时序/输出格式漂移(TTL 值不变;冷缓存最多多解析几个本地 ref,受 10s 缓存约束,可忽略)。
  6. 是否破坏缓存:凭据缓存从「全局单槽」改为「按 ak 分键」,TTL 仍 10s——这是修复的一部分(单槽正是 bug 来源之一),属有意变更、非意外破坏;上游 KV/前缀缓存不受影响(重签只改签名头,body 内容不变)。
  7. 是否改了设计方向:否。仍是「检测→取凭据→打 loopback marker→proxy 逐体重签」的原架构,只是取的凭据更忠实于请求本身。
  8. 是否引入新配置:否。账号选择由入站请求自身携带,无任何新配置/env/默认值。

⚠️ 行为变更确认(reviewer 复核):旧=重签固定用池首账号;新=重签用请求自己的账号(无匹配才兜底)。wire-behavior 变更,披露完整,方向正确。

判定:方向打到根因(不是治标),代码 / 测试 / diff 均达标,接缝缺口已补。可合——但按仓库规则属 wire-behavior + 凭据选择逻辑,不走 auto-merge,需人工合并;合并前等 CI 在新 head 66f54e7b8 上转绿即可(上一 head 5584b7256 的 CI 已全绿:4 条 test lane + 全部 guard job + build-artifact success)。

@rahul05ranjan rahul05ranjan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed this on a spare host — couldn't run the repo suite there (no dependency install), so I extracted the two new functions verbatim from the PR branch (pickCodeartsResignCred and inboundApigAccessKey) and ran them standalone under Node's type stripping, covering your four new scenarios plus extra edges. Everything below is what I actually executed.

What I confirmed:

  • The mechanics match the described bug. Old code kept one global cache slot and always returned the first resolvable pool credential; new code prefers the account named in the inbound Access=<ak> and only falls back to pool order when nothing matches. The single-account path is byte-identical (verified same result both ways), and the old first-valid semantics survive for unknown/missing AKs, exactly as the behavior table promises.
  • The regex handles the shapes that matter: Access=AK,Signature=…, Access= at end-of-string, and it does not bite on a XAccess= lookalike (\b holds up). Empty/absent Access= returns undefined and lands in the fallback rather than throwing mid-dispatch.
  • Cache behavior: per-ak keying no longer clobbers across accounts within the TTL, a hit genuinely skips re-resolution, and an expired entry (11.5s > 10s TTL) re-resolves. I also checked that a fallback request with no inbound AK can't accidentally get a different account's cached entry — the fallback's cachedOf(cred.ak) is keyed on the account it just resolved, so that's sound.
  • Fail-soft is intact: a resolver that throws for the first candidate is skipped and the loop continues, in both the match loop and the fallback loop; all-candidates-failing returns undefined into the refusal lane, not a crash.
  • Wiring: native-intercept.ts:794 passes the parsed AK as the third arg inside the existing try/catch, and the new seam test pins that — good call, since dropping the argument would pass every other test while silently regressing multi-account pools.

Two small things, neither blocking:

  1. Access= is matched case-sensitively (src/agent/native-intercept.ts:227). The Huawei SDK always emits capital-A Access=, so real traffic is fine, but a hand-rolled client using lowercase access= would silently drop back to pool-order re-signing — i.e., the exact bug this PR fixes. A /i on the key name (not the scheme word) would cover it.
  2. When inboundAk doesn't match any pool account (removed mid-flight), every candidate gets resolved twice within the same call — once in the match loop, again in the fallback loop. Harmless for the rare edge and a cheap local resolve, just noting it.

@ranxianglei
ranxianglei merged commit be3c58a into master Oct 20, 2026
21 checks passed
@ranxianglei ranxianglei changed the title fix: re-sign CodeArts traffic under the request's own account (#3106) [PR] fix: re-sign CodeArts traffic under the request's own account (#3106) Oct 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.645-master.be3c58a (rolling master tag).

npm install -g billion-context@0.0.645-master.be3c58a  # 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

Development

Successfully merging this pull request may close these issues.

Reverse proxy compatible | 反向代理兼容

2 participants