Repository navigation
[PR] Register pi/omp bili tools before round 1 so request 1 caches the full prefix - #5465
ranxianglei wants to merge 1 commit into
Conversation
…e full prefix pi snapshots each round's payload (tools + system prompt) BEFORE its provider hooks run, so tool registration that only started inside round 1's awaited hooks landed after the snapshot: request 1 went out without bili's tools while already stamped x-bili-plugin (plugin mode suppresses wire-side injection), and request 2's gained tools broke the provider prefix cache for the whole session (~19k token re-write per session start, #5459; capability-gap facet #4998 / PR #5009). Register as soon as the manifest source exists instead of waiting for a request: launcher mode now consumes its load-time manifest prime immediately; native mode registers when the bootstrap publishes the proxy origin. pi's synchronous registry refresh auto-activates the new tools and rebuilds the system prompt, so round 1's natural snapshot already carries the full tool table. Identity registration is untouched (still gated on a real session id; eager entries carry none). Residual: a -p cold start whose first dispatch beats the bootstrap still loses the race for that one round (#1217-class residual, strictly improved, not widened).
📦 Built Package ArtifactBranch: Latest build — install this exact versionnpm install -g billion-context@0.0.7728-pr.5465.1e331feThis exact version is immutable and reproducible. The rolling Recent builds (commit → version)
Download artifact
tar xzf billion-context-pr5465.tgz
npm install -g packageInstall instructions are refreshed on each push. |
(ai: vllm-qwen/qwen3.8-27b) 查重:不重复(related-not-duplicate)。 与在途 PR #5009 同根因家族(pi 的 ACP 工具只在第 2 个请求才出现),但层次不同:#5465 修客户端接缝(pi 提前注册工具,与 wire 无关);#5009 是服务端广告门控( Reviewed the diff against master — single commit, based exactly on current head,
测试有效性(④): 有牙。正例(native/launcher)在修复前代码会失败(注册只发生在请求事件时);竞态用例数的是总调用次数而非去重后的存在性,能抓住双循环;负例钉住「放弃 origin 不注册、不崩、不泄漏未处理 rejection」。确定性 OK(polling 而非固定 sleep、隔离 HOME/tmpdir、env 还原、finally 关 proxy)。小缺口(非阻塞):① 无专门的 omp 车道用例——但工厂共享,early-reg 关注点上结构等价;② 无端到端断言「round 1 线上真的带全工具」(需真 pi snapshotting,单测只能钉住时序契约这一必要条件),交给 CI 的 real-pi 车道兜底。 CI(镜像缓存读): 当前 0 failed / 13 pending;主 行为变更(旧→新,PR 表已披露): round 1 工具面 host-only → 全套;round 1→2 system prompt 重建 → 从 round 1 起稳定;缓存 每会话一次 ~19k 前缀重写 → 该原因下归零。残留: 顺带一提(非阻塞,issue 卫生): PR 称「both symptoms disappear」覆盖 #4998(OpenAI-wire 版同症状)。客户端早注册确实 wire-无关,应能一并消掉 #4998 的表现;但 #5009 是服务端门控,合入后值得回看它现在是防御纵深还是已被取代——各自留在线程里处理,不扩本 PR 范围。 结构化八问
自动合并判定: 基本满足 §7.4(单模块 fix: + 回归测试 + 干净 diff + 引用 issue + 无 kernel/config/version),唯一保留人工的理由是它改变了出站 round-1 工具面/注册时序(插件模式下出站消息形态变更)——低风险,但属产品可见行为变更,建议人工确认后再合。 判定:可合(待 CI 主 test 矩阵全绿;当前 0 failed / 13 pending)。方向正确、根因修复、测试有牙、diff 干净、无内核/配置/版本改动。 |
Closes #5459. Related: #4998 / PR #5009 (same mechanism, different symptom).
What this fixes
In native mode (
pi -e dist/agent/pi-native.js), bili's tools only joined the request from the 2nd request of each session. pi snapshots each round's payload (tools + system prompt) before its provider hooks run, and in native mode tool registration could only start inside round 1's awaited hooks (no load-time manifest prime exists there — #1217 recorded that residual for wire mode). Result per session:x-bili-plugin→ proxy runs plugin mode → no wire-side tool injection → zero bili tools on the wire.Fix
Register as soon as the manifest source exists, not when a request arrives:
awaitNativeProxyOrigin()resolves at bootstrap completion — long before any model request).Both go through the existing
registerToolspath with an undefined ctx: plain pi/omp without a parent do no identity POST (identity stays gated on a real session id), and the first real request re-enters cleanly (pending dedupe + toolsFor guard → each tool registered exactly once, pinned by test). The factory is shared by all four entries (pi.ts,pi-native.ts,omp.ts,omp-native.ts), so the omp lane gets the same fix; dsh has its own separate registration path and is untouched.pi's synchronous registry refresh auto-activates newly registered tools and rebuilds the system prompt, so round 1's natural snapshot already carries the full tool table + final system prompt → stable prefix from request 1.
Behavior change (old → new)
Residual (documented, strictly improved): a
-pone-shot whose first dispatch beats bootstrap completion still loses the race for that one round (#1217-class). The payload-append alternative was rejected: pi normalizesinput_schemaon the wire (verified against @earendil-works/pi-ai@1.4.0convertTools), so raw manifest bytes would not be byte-identical to pi's later serialization and would break the prefix anyway.Tests
New
tests/issue5459-pi-native-early-tools.test.ts(4 tests): registration happens with no request event fired (positive native + launcher); fast-first-request race never double-registers or clobbers the real sid (guard); give-up origin registers nothing and leaks no unhandled rejection (negative direction, #3617 discipline). Negative control verified: on pre-fix code tests 1–2 fail (registration only ever happened on a request event), 3–4 pass.Local targeted green: new file 4/4,
tests/plugin-agent.test.ts+tests/issue1333-derived-inheritance.test.ts84/84, typecheck clean. Full-suite verdict = CI on this head.No config surface change. No
kernel/change. No version bump.查重: related-not-duplicate — same mechanism family as #4998 (capability gap on OpenAI wire, fixed server-side by PR #5009, still open); this fix removes the client-side seam itself, so both symptoms disappear with it; no file overlap between the two PRs.