Skip to content

fix: hold first proxied request until ACP tools register (pi -p one-shot, #1214) - #1216

Closed
ranxianglei wants to merge 1 commit into
masterfrom
2026-09-24_pi-one-shot-readiness-gate
Closed

ranxianglei wants to merge 1 commit into
masterfrom
2026-09-24_pi-one-shot-readiness-gate

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Sep 23, 2026 •

Copy link
Copy Markdown
Owner

#1417 全批次深审定案:关闭——已被 master 完全覆盖(superseded)。

本 PR(2026-09-24 波)想修的 pi one-shot(pi -p)读门问题,其覆盖矩阵已由合并链 #1228(ddc3574)+#1217+#1230/#1243 全部落地;树级零 diff 证实其内容在 master 上已无增量。评论说明存档于 #1417 追踪板。

…hot, #1214)

In -p one-shot runs pi dispatches round 1 immediately after session_start,
before the manifest fetch settles. The header hook then saw toolsReady=false
and skipped the x-bili-plugin* stamp, so the headerless request made the
proxy bind an anonymous prefix-affinity session in proxy mode (compression
contract silently downgraded; /acp could not bind by session id).

The host awaits both before_provider_headers and before_provider_request
before the request leaves, so replace skip-stamp with a bounded readiness
gate (READY_WAIT_TIMEOUT_MS=5000): hold the request until registerTools()
flips toolsReady, then stamp. A permanently failing manifest endpoint still
degrades to wire mode after the timeout; a per-session latch avoids
re-waiting on every later request. Applies to omp too (its ownership claim
is the identity register inside registerTools).

Behavior change (old -> new):
- old: -p/fast-boot round 1 left headerless -> anonymous pfa session,
  proxy-mode compression, /acp fallback=latest
- new: first proxied request holds up to 5s for tool registration, leaves
  stamped -> plugin mode from round 1, real session binding
- degraded case (manifest endpoint permanently failing): identical wire-mode
  fallback as today, after one bounded wait per session
- normal-case latency delta ~0 (TUI: fetch long settled before typing;
  -p: covers the fetch RTT)

Verified: typecheck + full suite (2351 pass/0 fail/2 skip) + build, plus a
real 'pi -p' end-to-end run through the built proxy against a loopback
upstream: first request carries x-bili-plugin/conversation/window headers
with the real pi session id; proxy resolves window source=plugin.
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-24_pi-one-shot-readiness-gate (3d542b0)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-1216

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

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr1216.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] 🏷 Picked up. This is a completed [PR] notification — I will NOT redo the work. Plan: fetch the PR branch, rebase-check against current master, review the diff against the issue claims (readiness gate, latch, both hook sites), run typecheck + full suite + build locally, check CI status, then report my conclusion here. Give me a few minutes.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review complete — LGTM, mergeable (human merge per policy). I did not re-do the work; I fetched pr-1216 (= master + 1 commit 3d542b0c, fully rebased, no drift) and verified it.

① Direction (issue #1214) — confirmed. The root cause analysis holds: -p print-mode dispatches the prompt same-tick after session_start, so round 1 hit the deliberate skip-stamp invariant and silently downgraded to proxy mode ([pfa-…] session, /acp fallback=latest). The fix removes the consequence of the invariant (silent mode downgrade) without weakening the invariant itself — stamping still never happens pre-registration; it just waits for registration with a bound instead of giving up. Choosing the hook-level gate over fetch-layer stamping is the right call: it consumes an ordering guarantee the host already provides, and the degradation test proves the old end-state survives a dead manifest endpoint.

② Critical assumption verified against host source. The whole fix rides on "pi awaits these hooks." Checked the installed pi extension runner directly: emitBeforeProviderRequest does const handlerResult = await handler(event, ctx) (and replaces the payload when the result is defined — your stampPromptCacheKey return-value contract is intact), and emitBeforeProviderHeaders does await handler(event, ctx). Both awaited before the request leaves. Bonus safety property: if a future host ever stopped awaiting, the new code degrades to exactly the old skip-stamp end state (stamp lands post-dispatch = no-op) — there is no regression path through that assumption.

③ Regression review of existing behaviors the diff touches:

  • Stamping invariant ("never claim native-tool ownership before tools register"): preserved exactly — post-gate code re-checks toolsReady fresh; the latch only skips waiting, never blocks stamping.
  • Normal TUI sessions: unchanged — gate returns immediately once tools are ready; latency delta ≈ 0.
  • Permanent manifest failure: end state identical (wire mode); only the latency of first detection changes from immediate → one bounded 5s wait per session (disclosed in your table).
  • before_provider_request payload replacement: the awaited handler's return value is still used to replace the omp payload; ordering vs registerTools()/runtime-info preserved.
  • New behavior (intended, disclosed): fast-boot round 1 now held up to READY_WAIT_TIMEOUT_MS = 5000 and leaves stamped → plugin mode from round 1. This is a timing default change → per §7.4 this stays a human-merge decision regardless of my LGTM.
  • Tests: the new regression test has real teeth (same-tick hook call with provably empty pi.tools, asserts the awaited Promise releases post-registration with real sid — reverting to skip-stamp fails it). Degradation test pins both the ≥window wait and the latch. Two remaining un-awaited call sites in tests are safe (malformed-shape early returns; already-ready loop where the gate resolves synchronously).

Two non-blocking observations (not fixed, no action requested):

  1. omp identity-register race (theoretical): waiter drain fires at toolsReady = true, which for omp precedes the /__bili/plugin/register POST. So a held omp round 1 can in principle leave before its identity register lands → that single round could bind via content fingerprint instead of prompt_cache_key. In practice the register fetch is issued synchronously before any woken continuation yields, so it wins essentially always; and if lost, round 2 self-heals (register stays queued). Net window shrank from "whole manifest fetch" to "~1 loopback RTT" — strict improvement either way. If you want it airtight, moving the drain after the omp register block closes it, but that's a timing-semantics change I'd leave to you.
  2. Cosmetic: non-proxied requests in a process whose tools registered under another ctx can now carry x-bili-plugin* headers to the direct upstream (old code early-returned there). Unknown x- headers are ignored upstream and the request never reaches bili, so no binding effect — noise only.

Pre-flight (local, on rebased head): npm run typecheck clean · full suite 2351 pass / 0 fail / 2 skipped (matches your numbers) · npm run build ok.
CI: all 8 checks on 3d542b0c green (test matrix ubuntu/windows × 22/24, version-guard, build-artifact, zero-token codex e2e, codex MCP registration).


一句话中文摘要:修复了 bili pi -p 一次性运行首轮请求因工具未注册而被静默降级为代理模式的问题(改为有界等待工具注册后再打插件头,最长 5s,超时仍优雅退回线模式),已核对 pi 宿主源码确认钩子确实被 await、本地 typecheck/全量测试/构建与 CI 全部通过,可以合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Cross-reference from the #1228 review: I finished reviewing #1228 (the pi-only direct-await fix) and it verified clean on all fronts — premise confirmed against the real host, bounded await proven, regression test has teeth, green on the rebased head. Its approach is simpler than this PR's (no arbitrary timeout constant; boundedness derives from the existing manifest-fetch timeout + failure throttle).

Recommendation: land #1228 and close this one as superseded — with two caveats:

  1. This PR is the only implementation covering the omp one-shot path (omp -p); fix: pi one-shot (-p) first request claims plugin mode — await tool registration before header stamp #1228 leaves that race open, now tracked in omp one-shot (-p) first request claims proxy mode — identity registration races the single dispatch (same class as #1214) #1230. If you'd rather keep this PR alive as the fix for omp one-shot (-p) first request claims proxy mode — identity registration races the single dispatch (same class as #1214) #1230 instead of a follow-up patch, that also works.
  2. Either way, please confirm before I close anything — the disposition is yours.

中文摘要:#1228 审查通过且方案更简洁,建议合并 #1228 后关闭本 PR;但 omp 的同类竞态(#1230)只有本 PR 覆盖,是否改由本 PR 承接由你决定。

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