Skip to content

fix: break the proxy-origin hijack chain (#405) - #514

Closed
ranxianglei wants to merge 1 commit into
masterfrom
2026-09-04_proxy-origin-liveness
Closed

ranxianglei wants to merge 1 commit into
masterfrom
2026-09-04_proxy-origin-liveness

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What

Fixes #405 — four independent defects in the proxy-origin lifecycle, each with its own fix + regression tests:

  1. Read side had zero liveness (src/mcp.ts) — new async resolveLiveOrigin() probes candidates env → pointer file → default via GET /__bili/health, caches after success, and invalidates the cache on fetch failure (incl. timeout). A dead ephemeral pointer self-heals on the next MCP call — no host-client restart. Rebinds are declared on stderr with the instance id.
  2. Write side clobbered unconditionally (src/server.ts + new src/proxy-origin.ts) — the listen callback now claims instead of clobbers: a different existing origin that is healthy keeps the pointer (logged); otherwise it is reclaimed. Main file format unchanged (plain URL line, old shells keep working); a new sidecar lease (proxy-origin.lease: origin/pid/bootedAt/instanceId) carries ownership proof. Graceful shutdown releases the pointer only if it still points at this instance. /__bili/health now reports instanceId.
  3. Launcher spawned a second instance even when one was healthy (src/launcher.ts) — ensureProxyRunning probes the preferred port first and reuses a healthy one (ProxyHandle.child now optional; stopProxy no-ops on reused handles). Warns if MITM domains were requested on a reused shared instance (domains only apply to instances started with them).
  4. plugin-install froze the transient origin into persistent configs (src/plugin-install.ts) — all four bake sites (claude/codex/opencode) now bake the stable DEFAULT_PROXY_ORIGIN; explicit BILI_MCP_PROXY/--origin still wins. Custom-port instances keep working at runtime because the shell's env→file→default chain finds them (and dead pointers now heal per fix: keep system prompt byte-stable for prefix cache #1).
  5. Dual-instance snapshot rollback (src/persist.ts, the concrete landing spot for 无单实例守卫:多个 bili 代理进程并存共写 sessions 目录,伴随孤儿 ref、有注册表无 transcript、计数自相矛盾的污染簇 #394's direction) — when two processes share BILI_SESSIONS_DIR, a stale full snapshot (monotonic signals stats.requests / state.nextBlockId regressing) is rejected with a throttled warn and the disk state is kept; flushSync on a stale copy reports success so eviction drops the stale in-memory copy (self-heal).

Acceptance criteria

  • kill the launch-spawned ephemeral proxy (:8787 alive) → next MCP call recovers — tests/mcp-live-origin.test.ts (self-heal test); cache invalidation covers ECONNREFUSED/timeouts
  • bili claude with healthy :8787 present → no second spawn, no origin rewrite — tests/launcher.test.ts reuse test + tests/proxy-origin.test.ts claim tests
  • dual instances: later-saved older snapshot rejected + warn, counters do not roll back — three new tests in tests/proxy-persist.test.ts
  • any plugin install artifact bakes the stable BILI_MCP_PROXY — new test in tests/plugin-agent.test.ts (pointer file holding an ephemeral port → bakes :8787; explicit env still honored)

Pre-flight

  • npm run typecheck ✅
  • npm test: 887/888 pass. The single failure (resolveClientCommand: codex/claude resolve to themselves) is pre-existing and environment-dependent: it fails identically on clean master here because this machine has /usr/bin/codex while the test hardcodes a bare-name expectation for PATH=/usr/bin. Not caused by this change (reported below as a minor issue).
  • npm run build ✅ (tsup bundles proxy-origin.ts inline into dist/index.js)
  • E2E preflight (E2E_CHECK=1): codex CLI + dist OK; full e2e skipped — the 127.0.0.1:8199 upstream is not reachable from this sandbox (expected, see tests/e2e/README.md).

Notes

  • Deliberate deviation from the issue proposal: an explicitly set BILI_MCP_PROXY (or --origin) still wins at bake time — operator intent preserved; the default path (the actual bug) bakes the stable port.
  • No lockfile changes, no version bump.

- src/proxy-origin.ts (new): shared pointer protocol — claim with liveness probe, lease sidecar (origin/pid/bootedAt/instanceId), ownership-checked release
- server.ts: listen callback claims instead of clobbers; healthy existing owner keeps the pointer; /__bili/health reports instanceId; graceful shutdown releases only our own pointer
- mcp.ts: async resolveLiveOrigin() probes env->file->default candidates, caches after success, invalidates on fetch failure so the next MCP call re-resolves (dead ephemeral pointer heals without restarting the host client); stderr declares rebinds
- launcher.ts: ensureProxyRunning probes the preferred port first and reuses a healthy instance (no second spawn); ProxyHandle.child optional, stopProxy no-op on reused handles
- plugin-install.ts: bake stable DEFAULT_PROXY_ORIGIN into claude/codex/opencode configs instead of the transiently resolved origin; explicit BILI_MCP_PROXY/--origin still honored
- persist.ts: reject stale full snapshots (requests/nextBlockId rollback) when two processes share BILI_SESSIONS_DIR; throttled warn
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · qwen3.8-27b

[bot] 🏷 Picked up. Reviewing 2026-09-04_proxy-origin-liveness (fixes #405): checking out the branch, verifying typecheck/test/build claims, and reviewing each of the 5 changes for correctness (both compression modes + dual-instance edge cases) before giving a verdict. Full report to follow in this thread.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · qwen3.8-27b

[bot] 🏷 Review verdict: cannot merge as-is — the branch is based on stale master and overlaps with already-merged PR #419 (instance governance). The code itself is well-built and its pre-flight claims check out; the problem is where it lands, not what it does. Details below.

1. Verification (all run locally on 2026-09-04_proxy-origin-liveness @ 195d0cc)

  • npm run typecheck ✅ — clean
  • npm test → 887/888 pass, exactly as claimed. The single failure is resolveClientCommand: codex/claude resolve to themselves (tests/launcher.test.ts:628, actual /usr/bin/codex vs expected bare codex). I reproduced it on a clean checkout of master (b97de8d): identical failure there too (120/121 in that file). "Pre-existing and environment-dependent" claim confirmed.
  • npm run build ✅ — tsup inlines proxy-origin.ts into dist/index.js as claimed.
  • New tests are real-behavior, not mock theater: tests/mcp-live-origin.test.ts spins up actual HTTP stubs on loopback ephemeral ports with real pointer files + env manipulation (self-heal, cache single-probe, all-dead fallback); tests/proxy-origin.test.ts covers claim/release/idempotency/foreign-pointer cases against live stubs; the three tests/proxy-persist.test.ts additions drive two SessionStores over one dir (true dual-instance) covering writeNow rejection + warn capture, flushSync self-heal, tie-break on equal requests, and the debounced scheduleSave path.
  • I also verified fix feat: persistent + namespaced sessions, multi-tenant session isolation, stream robustness #5's load-bearing assumption: kernel StateStore.loadSync() (acp-kernel 0.0.47, dist/persist/index.js) reads directly from disk per call — no in-memory payload cache — so staleDiskPayload genuinely compares memory vs disk. The mechanism is sound.

2. Blocker A — stale base, hard conflicts

Merge-base with current master is 4729609 (v0.1.67, 2026-08-31) — the branch is 131 commits behind b97de8d. A local merge drill (git merge --no-commit origin/master) fails with content conflicts in six files: src/launcher.ts, src/mcp.ts, src/plugin-install.ts, src/server.ts, tests/launcher.test.ts, tests/proxy-persist.test.ts (add/add — master added its own version of that file after your base). Merging as-is would revert or double-apply #419's work.

3. Blocker B — overlap with merged PR #419 (umbrella #417)

#419 landed "instance governance" on Aug 31 with a different design for much of this same surface: JSON instance file (src/instance.ts: origin/instanceId/pid/startedAt/host/port/passthrough/mitmDomains/modelWindows/launchToken), attach-or-spawn launcher, handshake bind (#407), graceful exit, atomic writes. Fix-by-fix status vs current master:

Fix Status on current master Assessment
#1 mcp read-side liveness (resolveLiveOrigin) Still missing — resolveProxyOrigin() trusts the instance file blindly Genuinely additive. Port to current master: probe chain env → readProxyInstanceFile().origin → default, same cache/invalidation mechanics.
#2 write-side claim/lease (proxy-origin.ts) Master clobbers the instance file unconditionally (registry warns on duals) Claim semantics are a real improvement, but this PR introduces a second discovery surface (plain pointer + lease sidecar) competing with #419's JSON instance file. Should be folded into announceListening's write path operating on ProxyInstanceFile; the parallel sidecar format should not ship.
#3 launcher reuse Already implemented, broader — master attaches to any healthy config-compatible instance (registry-based), plus handshake bind; stopProxy no-ops via attached flag Redundant. Drop it; master supersedes it.
#4 plugin-install stable bake Master bakes env ?? recorded origin ?? throw Conflicting policy. My recommendation: adopt this PR's bakedProxyOrigin() (explicit env wins, else stable :8787) — baking a possibly-ephemeral recorded origin repeats the freeze bug, and throwing mid-install is harsher than needed since the shell resolves at runtime anyway. Small, self-contained change.
#5 persist stale-snapshot rejection Still missing Genuinely additive and the concrete landing of #394 item 4. Cherry-picks onto current master essentially as-is (no dependency on the other fixes).

So ~half the PR (fixes #1, #5, arguably #4) is net-new value that current master still lacks; half (#3, and the design of #2) collides with what just shipped.

4. Wrong issue reference (auto-close hazard)

The title, commit message, README and all code comments cite #405 — but GitHub #405 is the unrelated open ACP_PASSTHROUGH issue. The hijack-chain issue is #403 (closed/completed, folded into umbrella #417), with #394 as the persist direction. As written, a merge would auto-close the wrong issue. Every (#405) in the diff should become (#403) / (#394) where applicable.

5. Minor notes (report only, no separate issues)

  • _resetMcpLiveOriginForTest duplicates forgetLiveOrigin body (test-only export, acceptable but redundant).
  • resolveLiveOrigin invalidates the cache on network errors/timeouts but not on non-ok HTTP (e.g. 404 health) — edge case, fine to leave.
  • Worst-case first MCP call latency when all candidates are dead: up to 3 × 1.5 s probes before falling back to candidates[0].
  • Fix fix: address all code review findings (condense, routing, session-id, dead code) #2 edge: an instance that failed to claim loses discoverability when the claiming owner shuts down gracefully (pointer deleted, no handoff). Rare; acceptable if documented.
  • guardedBuild rewrites the disk payload on every stale save attempt (extra I/O while a stale copy keeps saving); warn is throttled 60 s/session, which is fine.

Recommendation

Close #514 without merging, then land a rebased branch off current master containing: fix #5 as-is, fix #1 adapted to the env→instance-file→default chain, fix #4's bake policy, and optionally fix #2's claim semantics folded into the JSON instance-file write path (drop the lease sidecar). Drop fix #3 entirely. Happy to do that rework if you confirm the scope — say the word and I'll cut the branch, port the pieces, re-run the full pre-flight, and open the new PR.

@ranxianglei

Copy link
Copy Markdown
Owner Author

Reviewed against master @8496194 (v0.1.83). Verdict: do not merge as-is — 4 of the 5 fixes are superseded by the instance registry that landed on master (#394/#403/#417, src/instance.ts), and merging would regress those. The branch is based on 4729609 (v0.1.67-era), which is why it rebuilds mechanisms master already has in a richer form.

Superseded by master

Still valuable — please rebase the scope down to these two

  1. Fix 1 (MCP shell liveness, src/mcp.ts) — real gap on master: resolveProxyOrigin is env → instance file → default with no liveness check, so after a crash leaves a stale instance file the spawned MCP shells dial a dead port forever. Worth keeping, but rebuilt on instance.ts primitives (e.g. isPidAlive on the instance pid is cheaper than an HTTP probe; keep the probe as fallback for legacy plain-URL pointers).
  2. Fix 5 (persist rollback guard, src/persist.ts) — nothing equivalent on master. staleDiskPayload (incoming stats.requests vs disk, state.nextBlockId monotonicity) + guardedBuild rewrite + throttled warn + flushSync(stale) so eviction drops the stale copy is a good self-healing design and orthogonal to the instance registry.

Everything else in the diff (launcher attach model, install origin policy, lease file) should be dropped rather than ported. The test claim (887/888) also predates the current baseline (1046/1048 with 2 known env fails) — the branch needs a fresh rebase pass anyway.

Happy to re-review once rescoped to fix #1 + fix #5.

@ranxianglei

Copy link
Copy Markdown
Owner Author

Per the review above, this PR is now superseded and split into two focused PRs rebuilt on current master:

Fixes 2–4 (pointer lease, launcher port reuse, baked install origin) remain intentionally dropped — master's instance registry (#394/#403/#417) covers them in a richer form, and #516/#517 are built on top of it.

Closing as superseded — thanks for the investigation and the rollback-guard design, which carried over almost verbatim. If you'd like to keep iterating on any of the dropped parts, the review comment above explains the overlap with master.

@ranxianglei ranxianglei closed this Sep 4, 2026
cskwork pushed a commit to cskwork/billion-context that referenced this pull request Oct 21, 2026
ranxianglei#405, split from ranxianglei#514 fix 1)

A crashed/killed proxy leaves its JSON instance record behind naming a port
nothing listens on; MCP shells then dial the dead origin forever. resolveProxyOrigin
now checks pid liveness (kill-0) on JSON records before trusting them and falls
back to the default origin with a one-time stderr note. pid 0 (foreign record
without a liveness claim) and legacy plain-URL pointers keep the old trust.
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.

ACP_PASSTHROUGH 一键静默关死全部压缩:无日志/无横幅/面板不可见且不可清除,同族 env 旋钮全部生效于无声

1 participant