Skip to content

fix: prime pi/omp manifest fetch at load time so -p round 1 carries plugin headers (#1217) - #1222

Merged
ranxianglei merged 1 commit into
masterfrom
wt-1217-ework-daemon
Sep 24, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
wt-1217-ework-daemon

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Sep 23, 2026 •

Copy link
Copy Markdown
Owner

Problem (#1217)

pi -p (single-shot) sends its first model request without any x-bili-plugin* headers: the header-stamp gate in src/agent/pi.ts requires toolsReady === true, but tool registration only started an async manifest fetch at session_start. In -p mode round 1 fires within milliseconds of startup — before the fetch resolves — so the request lands in proxy mode bound to an anonymous pfa- session instead of plugin mode. TUI mode wins the race because user typing gives the fetch seconds.

Reproduced with real pi 0.83.6 through a local proxy (baseline log):

[window] model=qwen3.8-27b-w8a16 source=table-or-registry ... fallback=true   <- no x-bili-plugin-model
[prefix-affinity] new anonymous session pfa-756d7d0cbc5e64fc (depth=1)
GET /__bili/plugin/status?conversationId=<real pi sid> -> 404

Division of labor with #1228 (merged)

Per owner decision (#1222 thread), this PR is sequenced after #1228:

Fix

Prime the manifest fetch at extension load time (createBiliPlugin factory): when BILLION_CONTEXT_PROXY is set (launcher mode always sets it before spawning the client, after health-checking the proxy), start fetchManifest() immediately and store the promise on RegisterState. registerTools consumes it once (same-origin guard); a failed prime resolves to undefined and falls through to the existing event-time fetch + retry-throttle path unchanged. The ownership gate itself is untouched — it still refuses to claim plugin mode without registered tools, preserving the graceful wire-mode fallback for permanently failing manifests.

Why priming instead of relaxing the gate: stamping unconditionally would create a tool-less plugin-mode session (sticky, never downgraded) whenever the manifest fetch fails — strictly worse than today's fallback. Why not rely solely on #1228's await: correct, but it moves the manifest RTT onto the first request's critical path for every launcher session, and it covers no client without an awaitable header hook (omp).

Residual: if the manifest fetch is slower than the load→round-1 gap, pi is still covered by #1228's await (only the latency benefit is lost); omp may still miss round 1 in that case — strictly no worse than pre-fix. Native mode (pi-native) sets BILLION_CONTEXT_PROXY only after async bootstrap (#519), so no prime exists there and round 1 stays on wire mode (unchanged residual).

Behavior disclosure (OLD → NEW)

Layered on merged #1228:

Path Before this PR After
pi -p round 1 stamped by #1228's await, but awaits the full manifest RTT stamped, await resolves against the in-flight prime (RTT off the critical path)
omp -p first request unmarked unless the session_start fetch won the race marked/bound in plugin mode whenever the prime settles before round 1
TUI (pi/omp) unchanged unchanged (prime settles long before user typing)
Manual env-var proxy, healthy manifest unchanged outcome unchanged outcome, slightly earlier toolsReady
Kill switch BILLION_CONTEXT_PLUGIN=0 inert inert (detectProxyBase → undefined → no prime)
Permanently failing manifest wire-mode fallback, one log line per retry identical
Wire shape of marked requests — byte-identical (same header set)

Proxy side untouched. No config schema, persistence, or version changes.

Tests (all deterministic — arrival-based, no wall-clock margins)

  1. [pi] -p 单发模式首个请求缺少 x-bili-plugin* 头(扩展钩子注册与首请求竞态) #1217 prime in flight: asserts the manifest request is observed before any event fires (load-time prime), then the normal session_start→stamp path end-to-end. Verified RED against the pre-fix src/agent/pi.ts (test fails without the prime).
  2. [pi] -p 单发模式首个请求缺少 x-bili-plugin* 头(扩展钩子注册与首请求竞态) #1217 failed-prime fallback (rewritten for fix: pi one-shot (-p) first request claims plugin mode — await tool registration before header stamp #1228's awaited-claim semantics): count-based failure window (requests 1–2 always 404, later succeed) pins: failed prime → exactly one event-time fallback fetch (request fix: address all code review findings (condense, routing, session-id, dead code) #2) → throttled round 1 makes no extra request and rides wire mode → after the throttle expires, the next awaited event performs exactly one recovery fetch and stamps in the same call. The pre-fix: pi one-shot (-p) first request claims plugin mode — await tool registration before header stamp #1228 invariant ("the call that starts the recovery fetch cannot stamp") no longer holds by design and was replaced accordingly.
  3. fix: pi one-shot (-p) first request claims plugin mode — await tool registration before header stamp #1228 kill switch: BILLION_CONTEXT_PLUGIN=0 keeps the plugin fully inert at load time (zero manifest requests).

An earlier revision of test 2 used fixed flush intervals to detect settlement and flaked on slow Windows runners (in-flight fallback fetch crossed the assertion boundary); it is now driven by observed request arrival + deadline-bounded polling.

Pre-flight (head 502c6b87, rebased onto master db52a965)

  • npm run typecheck ✅ · npm run build ✅
  • Full suite: 2387 pass / 3 fail / 2 skipped — the 3 failures are pre-existing claude-native live-process-tree e2e tests, reproduced identically on clean origin/master (machine-local environment, unrelated to this diff)
  • tests/plugin-agent.test.ts: 50/50 across 5 consecutive runs
  • Rebase notes (per owner sequencing): one comment-block conflict in src/agent/pi.ts (kept fix: pi one-shot (-p) first request claims plugin mode — await tool registration before header stamp #1228's wording + this PR's priming sentence); two tests adapted to the awaited-claim semantics above; squashed to a single commit.

@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: wt-1217-ework-daemon (502c6b8)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-1222

Each push to this PR publishes a new version under the pr-1222 npm tag.

Option B — Download artifact

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

[bot] 🏷 Review 进行中,先报两个已确认的发现(最终结论稍后补):

① 发现并修复了 CI 红灯(windows-latest, Node 22)
PR head ac67d76e 的 CI 里 test (windows-latest, 22) 失败:新增测试 #1217: a failed manifest prime falls back to the event-time fetch and retry throttle 在慢速 runner 上竞态——固定的双 flush 沉降点(~40ms)假设 fallback fetch 已落定,但 Windows loopback 上它还在飞行中,测试翻转 manifest 端点后,在途请求拿到 200 提前注册成功,kick cannot stamp 断言看到已打标的头。已在 PR 分支直接补了修复 commit:改为计数式失败窗口(前两次请求必 404)+ 以请求到达为沉降点 + 事件驱动恢复循环,不再依赖任何定时器裕量。

② base 过期,已 rebase 到当前 master (aae4de1c)
原分支从 03dd9c78 切出,master 其后进了 ~20 个 commit(含 v0.1.141 release、#1192 manifest opt-in tools)。核对过 #1192 只动 proxy 侧(src/plugin.ts/src/server.ts),与本 PR 文件无重叠,rebase 干净。

方向性审核结论(初步):根因定位正确(-p 首请求与异步 manifest fetch 的竞态),priming 方案正确且两个被否方案(gate 放宽 / header hook 内 await)的否决理由成立。回归清点、测试有效性、diff 整洁度的完整结论等 rebase+修复后的全量验证跑完即发。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review 完成,结论:可以合并(CI 已全绿;合并按仓库规则由人工操作)。以下是完整审核记录。

① 原 issue 方向关(#1217)— 通过

根因定位正确:-p 单发模式下 round 1 在 session_start 的异步 manifest fetch 落定前发出 → 无 x-bili-plugin* 头 → 绑成匿名 pfa- 会话走 proxy 模式。priming(扩展加载即发起 fetch、promise 存入 RegisterState)修的是产生竞态的机制本身(fetch 启动时机),不是症状。两个被否方案否决理由成立:放宽 ownership gate 会在 manifest 永久失败时创建无工具的 sticky plugin-mode 会话(严格劣于现状);在 before_provider_headers 内 await 依赖宿主是否 await handler promise(duck-typed,不可靠)。残余竞态(fetch 比「加载→round 1」间隔还慢时 round 1 仍无标记)与修复前持平,且 round 2+ 照常打标——可接受。

② 回归审查(既有行为逐项清点)

既有行为 结论
TUI 模式 保持:prime 在用户输入前落定,消费路径与原来同一 fetch
手动 env 代理(非 launcher) 保持:manifest 失败时 prime→undefined 落入原有 event-time fetch,日志行/重试节流完全一致
kill switch BILLION_CONTEXT_PLUGIN=0 保持:detectProxyBase 返回 undefined → 不 prime,加载期完全 inert(test 3 钉住)
native 模式(pi-native/omp-native) 保持:加载期 proxy env 未设(#519 异步 bootstrap)→ 无 prime,round 1 仍 wire mode(文档化残余,未变)
manifest 永久失败 保持:ownership gate 未动,仍拒绝认领 plugin mode,优雅回退 wire mode
打标请求的 wire shape 保持:头集合与原来逐字节一致,仅 round 1 从「无标记」变「有标记」(这正是本 PR 的行为变更,已在 PR body 披露 OLD→NEW)
omp 通道 有意变更(同属 launcher 路径,共享工厂,同获 priming——符合 PR 标题范围)
proxy 侧 未触碰

③ 代码审查

  • consume-once 正确:消费前先置 state.manifestPrime = undefined,并发调用经 state.pending 串行化(:213),不会二次 fetch。
  • same-origin guard(prime.base === proxyBaseForCtx(ctx))正确防住「load 环境与事件环境指向不同 proxy」的错配。
  • rejection 吞成 undefined 后走原失败路径,不引入新错误面;日志不变。
  • 无 version/lockfile/生成物改动;src/update.ts 等 load-bearing 文件未触碰。

④ 测试有效性

  • test 1(prime in-flight → round 1 打标):有牙——我把 pre-fix 的 src/agent/pi.ts 换回去验证过,它确实红。
  • test 2(failed-prime fallback):发现环境竞态,已在 PR 分支直接修掉(见下),重写后每个断言都不依赖定时器裕量,gate-runs-first 语义在恢复循环的每次事件上都被钉住。
  • test 3(kill switch):钉住加载期零请求。

⑤ 我在 PR 分支上的两处干预(均已推送,head = 6b83de5c)

  1. rebase 到当前 master aae4de1c:原 base 03dd9c78 落后 ~20 个 commit(v0.1.141 release、dsh acp rule报错 #1192 manifest opt-in tools 等)。核对过 dsh acp rule报错 #1192 只动 proxy 侧 src/plugin.ts/src/server.ts,与本 PR 零文件重叠,rebase 干净。
  2. commit 6b83de5c 修复 Windows CI 红灯:head ac67d76e 上 test (windows-latest, 22) 失败——test 2 的双 flush 沉降点(~40ms)假设 fallback fetch 已落定,慢速 runner 上它还在飞行中,测试翻转端点后在途请求拿到 200 提前注册成功,kick cannot stamp 断言看到已打标头。重写为:计数式失败窗口(前两次请求必 404)+ 以请求到达为沉降点 + 事件驱动恢复轮询。

证据

作者建议与 #1215 一起合并成立:两者独立无文本冲突,合起来后 -p round 1 既带插件头又受 #1215 的内容回退豁免保护,行为自洽。

一句话中文:审核确认了 pi -p 首请求缺插件头的竞态修复方向正确、回归面干净;顺手把 PR 分支 rebase 到最新 master 并修掉了新增测试在 Windows CI 上的定时器竞态(改为计数式失败窗口+事件驱动断言),CI 现已 8/8 全绿,可以合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

与 #1228(#1214,同一竞态)的对比与合并建议已发在 #1217(comment):先合 #1228(await 硬串行化,任意延迟下都正确),本 PR rebase 后作为加载时预热的延迟优化跟进——对 omp(无 before_provider_headers 钩子)有独立价值,互补不互斥。同文件(src/agent/pi.ts + tests/plugin-agent.test.ts)必冲突,先合 #1228 可让本 PR 的 rebase 冲突最小。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 收到,顺序确认:先合 #1228(await 硬串行化保正确性),本 PR rebase 后作为加载时预热的延迟优化跟进(对 omp 有独立价值——omp 不发 before_provider_headers,#1228 的 await 路径覆盖不到它;互补不互斥)。

我已对着 #1228 的 head(ddc3574c)做了冲突预判,结论:

src/agent/pi.ts — 基本无冲突。 两个 PR 触碰的区域不相交:#1228 只改 before_provider_headers handler 区(~L466-509:async + await registerTools,删掉尾部 fire-and-forget);本 PR 改的是 RegisterState.manifestPrime(L189)、registerTools 的 consume-once + same-origin guard(L216-233)、工厂尾部 prime 启动(L286-289)。rebase 应干净落地。

tests/plugin-agent.test.ts — 两处需要语义适配(不只是文本冲突):

  1. 我的 test 2(failed-prime fallback)里有一条断言钉的是旧设计的不变量:the call that starts the recovery fetch cannot stamp — gate runs first(fire-and-forget 下发起恢复 fetch 的那次调用不阻塞、不能打标)。fix: pi one-shot (-p) first request claims plugin mode — await tool registration before header stamp #1228 把 handler 改成 await 之后,这条不变量按设计消失了——event-time 调用本身就变成恢复尝试,恢复成功即打标。rebase 时我会把它重写为钉新语义:failed prime → 首个 event 内 await 恢复 fetch → 已恢复则打标 / 仍失败则保持静默进节流窗口。
  2. 我的 test 1 的「round 1 被打标」断言与 fix: pi one-shot (-p) first request claims plugin mode — await tool registration before header stamp #1228 新增的 bili pi -p one-shot: first request carries no x-bili-plugin* headers (anonymous pfa binding, proxy-mode downgrade) #1214 测试(-p 无预热首请求经 await 打标)重叠;rebase 后 test 1 的独特价值收窄为**「manifest 请求在加载时已 in-flight(早于任何事件)」——即钉住预热本身**,标题/注释会相应更新。kill switch 测试(test 3)不受影响。

一个刻意不做的事:我不会提前 rebase 到 #1228 的 head 上——那会把它的未合并 commit 卷进本 PR 的 master..head diff,rebase 等它落到 master 再做,正好符合你说的「冲突最小」。

当前状态:本分支 6b83de5c CI 8/8 全绿、mergeable_state=clean,随时可 rebase。#1228 一合并我就执行:rebase → 适配上述两处测试 → 更新 PR body(定位改为「延迟优化 + omp 覆盖」,行为披露同步修正)→ typecheck/build/全套验证 → push 回报。我这边先盯着,#1228 合并后会自动继续;若隔得久,在任一 issue 里 @ 我一声即可。

@ranxianglei

Copy link
Copy Markdown
Owner Author

冲突

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 收到——是的,#1228/#1229 先后落 master 后本 PR 的 base(aae4de1c)落后了,GitHub 显示冲突。我正在做这个 rebase(#1228 合并后约 5 分钟内开始):src/agent/pi.ts 的冲突已解决(仅一处注释块,保留 #1228 措辞 + 本 PR 的 priming 说明),接下来适配测试文件中两处语义变化(failed-prime fallback 测试按 await 新语义重写、test 1 收窄到钉住加载时 in-flight fetch),然后 typecheck/build/全套验证 → push → CI 绿后回报。稍等。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Rebase 完成,CI 全绿,PR body 已更新——可以合并了(合并按仓库规则由人工操作)。

做了什么(head 502c6b87,单 commit,直接落在 master db52a965 上):

  1. rebase 到 fix: pi one-shot (-p) first request claims plugin mode — await tool registration before header stamp #1228/feat: /acp explains chain passthrough — verdict panel replaces the armed-idle notice (#1218) #1229 之后的 master,按你定的顺序执行。冲突如预判:src/agent/pi.ts 仅一处注释块(保留 fix: pi one-shot (-p) first request claims plugin mode — await tool registration before header stamp #1228 措辞 + 本 PR 的 priming 一句);测试文件按新语义适配。原来两个 commit(fix + test-hardening)已 squash 为一个——hardening commit 的内容被语义重写吸收,保留会误导历史。
  2. 测试适配 fix: pi one-shot (-p) first request claims plugin mode — await tool registration before header stamp #1228 的 awaited-claim 语义:
    • test 1 改题为 #1217: launcher-mode manifest fetch is primed at extension load time,独特断言收窄为「manifest 请求在任何事件之前已 in-flight」(钉住预热本身;round-1 打标的正确性已由 bili pi -p one-shot: first request carries no x-bili-plugin* headers (anonymous pfa binding, proxy-mode downgrade) #1214 测试覆盖),handler 调用改为 await + waitForTools 确定性沉降。
    • test 2 重写:计数式失败窗口(前两个请求必 404)钉住「failed prime → 恰好一次 event-time fallback fetch」;throttled round 1 不发起第三个请求、不打标(优雅降级 wire mode);节流过期后下一个 awaited event 恰好一次恢复 fetch 且同调用内打标。旧不变量 "the call that starts the recovery fetch cannot stamp" 随 fire-and-forget 设计按设计消失,已替换。
    • kill switch 测试不受影响,保持原样。
  3. PR body 重写:定位改为「fix: pi one-shot (-p) first request claims plugin mode — await tool registration before header stamp #1228 提供任意延迟下的正确性(awaited claim),本 PR 提供延迟半区(把 manifest RTT 移出 round-1 关键路径)+ omp 覆盖(omp 无 header hook,fix: pi one-shot (-p) first request claims plugin mode — await tool registration before header stamp #1228 够不到)」,行为披露表、残余竞态说明同步修正。

验证证据(head 502c6b87):

  • typecheck ✅ · build ✅ · 目标测试文件 50/50 ×5 连跑
  • 全套 2387 pass / 3 fail / 2 skipped——3 个失败是 claude-native 进程树 e2e 的本机预存环境问题,已在干净 master 上复现同样失败,与本 diff 无关
  • GitHub CI:8/8 success(含 windows-latest/22),mergeable_state=clean

一句话中文:已按「先 #1228 后本 PR」的顺序把分支 rebase 到最新 master,把两处测试适配到 await 硬串行化的新语义并 squash 成单 commit,本地全套与 GitHub CI 8/8 全绿,PR body 已改定位为延迟优化+omp 覆盖,可以合并。

@ranxianglei
ranxianglei merged commit afef6ac into master Sep 24, 2026
8 checks passed
@ranxianglei
ranxianglei deleted the wt-1217-ework-daemon branch October 15, 2026 05:33
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.

1 participant