Skip to content

fix: skip dead-pid proxy-origin records in MCP shell origin resolution (#405) - #522

Merged
ranxianglei merged 1 commit into
masterfrom
2026-09-04_mcp-dead-pid-origin
Sep 7, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-09-04_mcp-dead-pid-origin

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Sep 4, 2026 •

Copy link
Copy Markdown
Owner

Closes #599 — tracking issue for this PR.

Split from #514 (fix 1 of 5, the only part still a gap on master — see review there). Fixes #405 (MCP half).

What

resolveProxyOrigin() (src/mcp.ts) resolved env → instance file → default origin with no liveness check. When a proxy crashes or is killed, its JSON instance record stays behind naming a port nothing listens on — spawned MCP shells then dial the dead origin forever, and every tool call fails until the host client is restarted (or a new proxy happens to bind that exact port).

Rebuilt on the src/instance.ts primitives (#394/#403/#417) rather than the HTTP-probe chain from #514:

  • A JSON instance record with a live pid (isPidAlive, kill(0) — synchronous, no network on the hot path) is trusted as today.
  • A record whose pid is dead is stale: skip to the default origin (http://127.0.0.1:8787, where a replacement proxy binds) with a one-time stderr note suggesting BILI_MCP_PROXY if the proxy runs elsewhere. Self-healing: the next tool call re-resolves, so a restarted proxy is picked up with no shell restart.
  • pid: 0 (record makes no liveness claim, foreign writer) and legacy plain-URL pointers keep the old trust semantics — deliberate asymmetry vs plugin-install, which refuses them.
  • BILI_MCP_PROXY env still wins over everything (operator intent).

Why pid-check instead of #514's /__bili/health probe: the JSON record already carries the writer's pid, the check is synchronous (probe would have to become async + cached), and it exactly matches how the launcher attach and plugin-install already gate on the same file.

Tests

tests/instance-governance.test.ts: dead-pid JSON record → default origin (re-resolved on every call); live pid → record origin; pid 0 → trusted; stderr note fires once and mentions BILI_MCP_PROXY.

Pre-flight

  • typecheck ✅
  • full suite 947/948 — the single failure is the known env-dependent resolveClientCommand: codex/claude resolve to themselves test (machine has /usr/bin/codex), identical on clean master (baseline 946/947)
  • build ✅ (dist/mcp.js contains the new behavior)
  • manual E2E on built dist/mcp.js: stale record (dead pid, unrouted port) → one-time stderr note → tool-call error names the fallback origin http://127.0.0.1:8787, not the dead port

Notes

  • Known limitation (accepted): PID reuse — if the dead pid is later reused by an unrelated process, the stale record looks live again. Same exposure as the existing plugin-install/launcher gating.
  • Shells spawned with an explicit BILI_MCP_PROXY env pointing at a dead port still dial it — env is operator intent by design; recovery = repoint/reinstall.
  • No version bump, no lockfile change.

@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-04_mcp-dead-pid-origin (4e206c8)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-522

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

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr522.tgz
npm install -g package

This comment is automatically updated on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

Review vs master 3cd85b6: ✅ merge-ready.

  • src/mcp.ts resolveProxyOrigin() is unchanged on master since the PR — applies clean.
  • Semantics verified: isProxyInstanceFile(discovered) && discovered.pid > 0 && !isPidAlive(pid) → one-time stderr note + fallback to DEFAULT_PROXY_ORIGIN (8787). pid 0 (foreign writer / legacy plain URL) keeps old trust semantics — deliberate and documented.
  • Test covers all three cases (dead → fallback + one-time note, live → trusted, pid 0 → no liveness claim).

Behavior question (non-blocking): when the dead record is skipped we dial 8787 by default; if the user runs the proxy on a custom port this still fails — but that was equally true pre-PR (dialing a dead origin), and the stderr note now tells them to set BILI_MCP_PROXY. Strictly better than before.

(tracking: #599)

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · qwen3.8-27b

Confirmed your review independently against current master (1330fc5). One thing worth adding beyond it:

Your check covered src/mcp.ts being unchanged on master (true — resolveProxyOrigin() is untouched since the branch point). But the PR was pre-flighted off 09ef208, and master has moved under it since: #527 reworked src/instance.ts (the module this PR now imports from) and both #527 and this PR touch tests/instance-governance.test.ts. So "applies clean" wasn't guaranteed by the mcp.ts check alone. Re-verified against current master:

So it's merge-ready against current master, not just the pre-#527 base. (I ran typecheck + the directly-affected governance file, not the whole ~948-test suite — CI re-runs that on merge.)

Behavior question — agreed, no action needed: the custom-port case was equally broken pre-PR (dialing a dead origin), and now at least emits an actionable BILI_MCP_PROXY hint. Strictly better than before.

Next step is yours to take — I can't merge (human-only per AGENTS.md). It's a content PR (no version bump, no lockfile), so on merge it lands on master and ships with the next release.

@ranxianglei
ranxianglei merged commit ddc165e into master Sep 7, 2026
6 checks passed
@ranxianglei ranxianglei mentioned this pull request Sep 7, 2026
@ranxianglei
ranxianglei deleted the 2026-09-04_mcp-dead-pid-origin 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

bug Something isn't working

Projects

None yet

1 participant