Skip to content

fix: pi one-shot (-p) first request claims plugin mode — await tool registration before header stamp - #1228

Merged
ranxianglei merged 1 commit into
masterfrom
2026-09-24_pi-oneshot-plugin-headers
Sep 24, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-09-24_pi-oneshot-plugin-headers

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Fixes #1214

Root cause

before_provider_headers stamped x-bili-plugin* only when state.toolsReady === true, but drove registerTools fire-and-forget. TUI recovered on request #2; a one-shot (pi -p) dispatches exactly ONE request — it rode wire mode forever and bound as an anonymous pfa- session (proxy-mode compression, /acp cannot bind by session id).

Fix

pi's runner awaits async handlers (emitBeforeProviderHeaders, verified in pi-stable dist/core/extensions/runner.js), so the handler is now async and awaits registerTools BEFORE the ownership gate. registerTools is idempotent (sid-cached, pending-deduped, retryAt-throttled), so the await is bounded; a permanently failing manifest fetch still degrades to wire mode unchanged.

Effect: the one-shot's single request carries the full header set (x-bili-plugin, -conversation, -context-window, -model) → plugin-mode contract + real session binding from request 1. TUI round 1 also stops riding wire mode (previously round 1 = wire, round 2+ = plugin).

Tests

@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-24_pi-oneshot-plugin-headers (ddc3574)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-1228

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

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr1228.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 started. Two things up front:

  1. This overlaps with open PR fix: hold first proxied request until ACP tools register (pi -p one-shot, #1214) #1216 (my earlier fix for the same bili pi -p one-shot: first request carries no x-bili-plugin* headers (anonymous pfa binding, proxy-mode downgrade) #1214): fix: hold first proxied request until ACP tools register (pi -p one-shot, #1214) #1216 uses a bounded 5s readiness gate over both hooks; fix: pi one-shot (-p) first request claims plugin mode — await tool registration before header stamp #1228 serializes the stamp directly behind registerTools. Same files, same region — both can't merge. I'll review fix: pi one-shot (-p) first request claims plugin mode — await tool registration before header stamp #1228 on its own merits and report which approach holds better, then defer to your call on which to land.
  2. Verifying the load-bearing claims now: (a) pi's runner actually awaits async before_provider_handlers; (b) registerTools await is genuinely bounded (concurrent-dedupe, retry-throttle, fetch timeout) so a hanging/dead manifest endpoint can't stall the one-shot request forever.

Full findings shortly.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review complete — verified mergeable (human merge; gate note at the end).

① Direction (issue #1214): root cause confirmed — a one-shot dispatches exactly one request, so the fire-and-forget registration could never win the race; the fix serializes registration before the ownership gate, i.e. it removes the producer of the defect rather than masking the symptom. Premise checked against the real host: pi's extension runner awaits async handlers in emitBeforeProviderHeaders (pi-stable dist/core/extensions/runner.js:818), and the SDK awaits transformHeaders before building the request (dist/core/sdk.js:192-197 → pi-stable-ai models.js:255-256), so making the handler async is safe here.

Boundedness: registerTools cannot reject — it swallows fetch failures internally and resolves (setting retryAt). Worst-case hold on the first request = the manifest fetch timeout (5s); the sid-cache / pending-dedupe / 10s failure-throttle make later calls instant or ≤5s. Permanently dead endpoint → request held ≤5s, then unstamped wire mode with throttled retries. No unbounded-hang path found.

② Regression inventory (existing behaviors on touched paths):

  • TUI/new-session round 1: previously rode wire mode (round 1 = wire, round 2+ = plugin) → now plugin mode from request 1. Intended, disclosed in the PR body. ✓
  • session_start resets the sid per new session, so each new session's first request re-fetches the manifest: milliseconds on loopback. Acceptable.
  • Dead-manifest case: previously unstamped immediately → now held ≤5s then unstamped. One-time ≤5s latency shift on first failure only; end state (wire mode) unchanged — implied by "degrades to wire mode unchanged".
  • Handler-exception path: the runner catches handler errors and proceeds unstamped — moot in practice since registerTools never rejects.
  • Both compression modes considered: the change only affects which mode the session binds into from request 1; the plugin-mode contract and proxy fallback are untouched; no message-shape/wire change.
  • The omp path is untouched — still fire-and-forget (scope note below).

③ Code: +16/-9, every line on-purpose. Nit (non-blocking): the try/catch around await registerTools is dead code — registerTools catches all failures internally and always resolves. Harmless defensive coding; fine to keep.

④ Tests: the new #1214 test has teeth — I ran it against origin/master's src/agent/pi.ts and it fails (reproduces the bug), passes with the fix. The updated tests correctly encode the new host-like contract (await the handler like the real runner does); the graceful-degradation test now proves the handler resolves (bounded await) on a dead manifest endpoint. Local gate on the rebased head: typecheck green, plugin-agent suite 47/47, build green; full suite green except 3 pre-existing env-dependent claude-native.test.ts failures (live ps walk + hook e2e) that fail identically on plain master.

⑤ Diff: clean — 2 files, no whitespace/lock/generated churn. Rebases cleanly onto current master (the release v0.1.142 commit since your base only touches package.json/lock).

Scope note (follow-up filed): this PR fixes only pi's header path. omp has the identical latent race on its own one-shot mode (omp -p/--print, "Non-interactive mode: process prompt and exit"): its extension runner also awaits async handlers, yet its ownership claim (identity registration) still sits behind the fire-and-forget registerTools, so bili omp -p '<prompt>' can ride the same pfa-downgrade. Filed #1230 to track it.

Overlap with #1216: my earlier open PR covered both paths with a bounded 5s readiness gate. This direct-await approach is cleaner — no arbitrary constant, boundedness derives from the existing fetch timeout + throttle. Recommendation: merge #1228, close #1216 as superseded (your call), track the omp gap in #1230.

Gate: would pass auto-merge §7.4 except item 8 (touches session-binding logic) → human merge either way.


中文摘要:修复了 pi -p 一次性请求因工具注册竞态而丢失插件模式头的问题(改为在打头前 await 注册;已验证 pi host 会等待异步 handler、等待有界、新回归测试对旧代码确实失败),可以合并;omp 的同类竞态已另立 #1230 跟踪。

@ranxianglei
ranxianglei merged commit 5792e3c into master Sep 24, 2026
8 checks passed
antclt pushed a commit to antclt/billion-context that referenced this pull request Sep 24, 2026
…ity registration before dispatch (ranxianglei#1230)

Mirror ranxianglei#1228 on the before_provider_request path: make the handler async
and await registerTools before stamping/reporting. omp's runner awaits
async handlers and sends the resolved payload (verified in the omp
18.0.3 dist), so the plugin-mode claim (the identity:true register that
runs INSIDE registerTools, after the manifest fetch) now serializes
ahead of the single -p dispatch instead of racing it. A losing race
bound the session as an anonymous pfa-*/proxy-mode session with no
round 2 to self-heal.
@ranxianglei
ranxianglei deleted the 2026-09-24_pi-oneshot-plugin-headers 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.

bili pi -p one-shot: first request carries no x-bili-plugin* headers (anonymous pfa binding, proxy-mode downgrade)

1 participant