Skip to content

fix(#2218/#1931): forward dsh plugin-lane session signal to relays (x-session-id + gated prompt_cache_key stamp) - #2240

Merged
ranxianglei merged 5 commits into
masterfrom
2026-10-06_relay-affinity-signal
Oct 6, 2026
Merged

ranxianglei merged 5 commits into
masterfrom
2026-10-06_relay-affinity-signal

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

是什么问题

dsh 桌面经 bili 连第三方中转(relay)时,上游侧拿到的会话信号为零,导致中转的账号粘性/缓存路由全部失效:

根本原因(代码级)

dsh lane 的唯一身份信号是 bili 内部协议头 x-bili-plugin-conversation(与 marker x-bili-plugin 成对,dsh-native.ts 构造)。两条既有通路对它全部失效:

  1. src/server.ts buildForwardTarget 的 x-session-id 兜底:if (affinity && !clientConversationHeader(req.headers)) —— clientConversationHeader 的名单第一项就是 x-bili-plugin-conversation(session-id.ts:69),所以插件头反而抑制了兜底;而第三方上游根本读不懂这个内部头 → header 路失效。
  2. src/agent/pi.ts stampPromptCacheKey 的 body prompt_cache_key 注入:if (agent !== "omp") return([Bug] bili omp 扩展 stampPromptCacheKey 无条件注入顶层 prompt_cache_key —— 严格 schema 上游(opencode zen anthropic 端点)全量 400 #1403 门控)—— dsh 不是 omp → body 路也失效。

探针实证(tests/zz-probe,本 PR 已转正为回归测试):dsh lane 请求到达 mock 上游时 x-session-id 与 body prompt_cache_key 双双为 undefined;对照普通客户端 + body pck 两信号俱全。

workbuddy 侧读法(决定修复矩阵):

  • hub(Python):读头 X-Conversation-Id/X-Session-Id 等(不读 body pck)→ 靠头修复;
  • panel(Go):ExtractKey 只读 body(metadata→conversation_id→prompt_cache_key→内容派生),头完全不参与 → 靠 body 修复;
  • sub2api OpenAI wire:头优先 → 靠头修复。

如何解决

两处互补修复,互不重叠:

  1. 头修复(src/server.ts buildForwardTarget):x-bili-plugin-conversation 是 bili 内部头,上游读不到 → 视同"无头",把客户端亲和值照常转发为 x-session-id(hub/sub2api 家族恢复粘性)。普通可读头(codex session-id 等)保持原抑制逻辑不变(不造重复头)。
  2. body 修复(src/server.ts prepareOpenai + 新助手 src/session-id.ts shouldStampRelayAffinityPck):插件 lane 持有身份、客户端没发自带 prompt_cache_key 时,把会话 id 盖进出站 body prompt_cache_key(panel 家族恢复粘性)。门控(三条件同时满足):

副作用收益:本机 glm-proxy(读 x-session-id 做粘性)同样恢复稳定账号路由。

是否能合并

  • 新增 tests/issue2218-relay-affinity-signal.test.ts 6 用例全绿:门控矩阵(6 断言)/普通客户端 body pck 回归(双信号)/dsh lane 双信号(loopback+无 route 腿)/客户端自带 pck 不覆盖/可读头不造重复 x-session-id 且不盖 pck。
  • 相邻套件全绿:proxy-session-id 21、plugin-protocol 16、e2e-session-identity 12、tag-echo 57、issue2171 10、claude-subagent-split 5、dsh-persona-fingerprint 7、e2e-plugin-grow-compress 1;typecheck 0 错。
  • 行为面:仅插件 lane 的出站信号增强;非插件流量字节不变。

URL

ranxianglei and others added 3 commits October 6, 2026 13:49
The built-in pi-lane acp_delegate surface reads its config from bili's own
config file (pi.subagents section) instead of pi's acp.json. The four
acp.json keys (delegate/delegatePrompt/displayUsage/debug) become a
deprecated fallback: still read while the section is absent (one-time
warning), ignored once it exists. prompt replaces delegatePrompt; debug is
scoped to the sub-agent subsystem. The standalone package reads the same
section through its own loader (billion-context-pi-subagents#5) — the file
format is the contract, no shared code.

Co-Authored-By: Claude <noreply@anthropic.com>
The docs-sync drift guard (#2107) caught hand-edited generated blocks —
the quick-ref and env-map rows now come from the seed in
website/config-reference/server.yaml like every other key; the narrative
pi section (field table + env overrides) stays hand-written outside the
generated blocks.
dsh desktop's only conversation identity is the bili-internal
x-bili-plugin-conversation header, which no third-party upstream can
read: header-keyed relays (workbuddy hub, sub2api openai wire) lost
sticky routing and body-keyed relays (workbuddy panel) fell to a
content digest, so every account migration froze the provider prefix
cache (#2218 80% hit-rate sessions; #1931 seed-drift class).

- buildForwardTarget: treat the plugin conversation header like 'no
  header' — forward the client affinity as x-session-id
- prepareOpenai: stamp prompt_cache_key = conversation id for the
  plugin lane when the client sent none, gated to admin-rewritten or
  loopback destinations (strict direct APIs can 400 unknown body
  params, #1403); the client's own value always wins
@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-06_relay-affinity-signal (0cee385)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2317-pr.2240.0390c00

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

Install instructions are refreshed on each push.

ework-agent and others added 2 commits October 6, 2026 14:36
…p gate

Review finding on 76c6fe9: isLoopbackAddress only matches 127.x/::1
literals, so an upstream configured as http://localhost:N would silently
fail the destination gate and the body stamp would not fire — reproducing
exactly the zero-signal absence this fix removes. The codebase already
treats "localhost" as loopback in upstream-proxy.ts / launcher.ts /
config.ts; align the gate locally (shared util untouched).
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 PR #2240 Review 结论:代码侧通过;等新 head CI 转绿即可合并(4 个 test job 红为已知基线问题 #2238,非本 PR 引入)

① 是什么问题:dsh 桌面插件 lane 的请求到达第三方中转时零可用会话信号——其唯一身份载体是 bili 内部头 x-bili-plugin-conversation(与 marker 成对),第三方中转读不懂 → 读头家族(workbuddy hub / sub2api openai wire)完全失去粘性路由,只读 body 家族(workbuddy panel ExtractKey)退化成内容摘要,账号池每次迁移都冻结整段前缀缓存(#2218 的 80% 命中会话;#1931 种子漂移的最坏情形)。

② 什么原因导致的:bili 两条既有出站信号通路对插件 lane 恰好双双失效——(a) buildForwardTarget 的 x-session-id 兜底条件 !clientConversationHeader(...) 的名单第一项就是内部头 → 插件头反而抑制了兜底;(b) pi.ts 的 body pck 注入被 agent !== "omp" 门控(#1403)挡掉 dsh。已逐条核对代码路径,探针证据已转正为回归测试(mock 上游捕获真实出站字节)。

③ 怎么修复的:双路互补、互不重叠——内部头视同"无头",把客户端亲和值原样转发为 x-session-id(覆盖读头家族);插件 lane 持身份且客户端未发自带 pck 时,把会话 id 盖进出站 body prompt_cache_key(覆盖读 body 家族)。三重门控逐条审过:身份来源必须是内部协议头(非插件 lane 永不盖)/ 客户端自带值永远优先 / 目的地限管理员 route 改写或 loopback(严格直连不盖,#1403 教训继承;URL 解析失败保守返回 false,fail-fast 未变吞错)。两种压缩模式均推理:仅出站路由信号增强,非插件流量字节不变(test 6 钉住);插件模式下 stamp 同样生效(e2e 用例即插件模式)。另审了后续 commit 0a1956e12(localhost 字面量补进 loopback 判定):正确且必要——isLoopbackAddress 只匹配 127.x/::1 字面量,配置成 http://localhost:N 的中转会静默落空门控、复现零信号 bug;修法与本仓既有约定(upstream-proxy/launcher/config 均把 "localhost" 视同 loopback)对齐,共享 util 未动。行为变更披露:出站新增 x-session-id 头(仅插件 lane,旧行为为抑制)+ 新增 body prompt_cache_key(仅门控内:route 改写 / loopback / localhost 字面量目的地,无客户端 pck,身份来自内部头);非插件流量零变化。

④ 是否有回归问题:触碰路径既有行为逐项清点——x-session-id 转发:仅插件 lane 行为变化(抑制→转发),可读头(codex session-id 等)的抑制逻辑保持不变、不造重复头(test 6 钉住);prepareOpenai body:仅新增字段,既有 prompt_cache_retention 删除逻辑与客户端 pck 透传不变(tests 2/5 钉住);session-id.ts 纯新增(import + 导出函数),既有函数零改动;conversationHeaderSource 优先级表未动(marker 成对信任边界保持)。高危模式逐项过:无 fail-fast→吞错、无阈值/时序/输出格式漂移、无持久化格式/config schema/kernel/update.ts 触碰。

⑤ 是否破坏缓存:否。stamp 是顶层 body 参数 + 出站头,模型不可见,不进消息重建/压缩状态/session key;bili 自身前缀缓存、重试、去重、增量游标均不受影响。改善对象正是上游账号池粘性命中率(修复目标本身)。

⑥ 是否改了设计方向:否。沿用既有 affinity 设计原则——「只转发客户端已提供的身份值」(session-id.ts doc + README);pck stamp 是插件 lane 的新出站载体而非新信号源,目的地类型门控防止污染严格直连 API。

⑦ 是否引入新的配置:否。门控复用现有 providers/route 配置 + loopback 判定,无新 env var / config 项 / 默认值。

测试有效性(质量关):6 用例是真 e2e(startServer + mock 上游捕获实际出站 headers/body,即 #2218 探针转正),断言精确值有牙(bug 回归时双 undefined 必被抓),listen(0) 确定性端口,hermetic store/registry reset。本地定向复跑(全量归 CI):新套件 + proxy-session-id + plugin-protocol + openai-pck-identity + dsh-persona-fingerprint + e2e-session-identity = 65/65 绿;最终 head(含 localhost 修复)6/6 + typecheck 0 错。

两个小发现:

  1. test 4 注释过度声明:/bili/ 嵌入 URL 形式必然解析出 route(resolveUpstream 设 rewrittenUrl),该例实际钉的是门控的 route-rewrite 腿而非 loopback 腿(loopback 腿仅由单测矩阵覆盖)→ 已在 PR 分支就地修正注释(独立标注 commit 0cee3850c,已推)。
  2. loopback 腿缺 e2e 负例互补(需 bare-path 请求 + 非 loopback 直连断言不盖);谓词分支已被单测矩阵全覆盖,布线风险低,非阻塞。

CI 状态(关键):head 76c6fe963 上 4 个 test matrix job 红 = 已知 master 基线系统性问题 #2238(同一失败集合横跨多个无关分支、含本 PR base bd33b11;agent PAT 无法下载 check-run 日志,404,该 issue 正在处理中)。本 PR 其余全部 lane(build-artifact / 各 e2e / kernel-guard / version-guard / lane-guard / docs)绿;新 master(#2233 合并后,2f6fe8bce)的 4 个 test job 已转绿。base 滞后即为已合并的 #2233,与本 PR 文件集零交集,无冲突预期、无需 rebase。最终 head 0cee3850c 已触发新一轮 CI——新 head 上 4 个 test job 转绿即可合并。

判定:根因定位准确、修在机制层而非症状层、门控保守、测试有牙——review 通过,待 CI 复跑绿后由人合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 CI 状态更新:最终 head 0cee3850c 全绿,可合并。

全部 25 个 lane completed / success——包括此前受基线问题(#2238)影响的 4 个 test matrix job(ubuntu/windows × node 22/24)本轮全部转绿;build-artifact、各 e2e lane、kernel-guard、version-guard、lane-guard、docs-* 亦全绿。

分支当前栈:76c6fe963(主修复)→ 0a1956e12(localhost loopback 门控补漏)→ 0cee3850c(测试注释修正)。Review 结论(上一条七问回复)维持:代码侧通过,请人工合并 #2240 。

@ranxianglei
ranxianglei merged commit c23f365 into master Oct 6, 2026
25 checks passed
@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.71-master.c23f365 (rolling master tag).

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

This was referenced Oct 7, 2026
@ranxianglei
ranxianglei deleted the 2026-10-06_relay-affinity-signal branch October 15, 2026 05:32
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.

上游侧失效导致缓存命中只有80%用的api反代 workbuddy glm5.3

1 participant