Skip to content

fix: hermes plugin attach paths register their host pid as a proxy watchdog owner (#1199) - #1219

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

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

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Fixes #1199.

Problem

hermes-plugin/__init__.py has two attach paths that bypass the TS-side watcher registration introduced by the #1183/#1190 fix (PRs #1184–#1186):

  1. explicit BILLION_CONTEXT_ATTACH env target;
  2. discover_instance() finding a healthy shared instance (including via the marker-wait poll loop).

Neither registered a host pid at POST /__bili/watcher, so when multiple hermes sessions shared a proxy, the first session's exit killed the proxy's only watched owner (BILI_PARENT_PID) and took the remaining sessions down with it — same symptom as #1183/#1190.

Change

  • New register_watcher(origin) in hermes-plugin/__init__.py: after a successful attach, POST {origin}/__bili/watcher with {"pid": os.getpid()} — the same endpoint and policy as the TS launcher (src/claude-native-bootstrap.ts): 409 = daemon proxy (no watchdog) → silent; any other failure logs a warning and never blocks session start. 2s timeout (same as the health probe).
  • All three attach sites in ensure_origin now funnel through a single _attach(origin) helper (state update + watcher registration), so no future attach site can skip registration. The spawn path is unchanged: the spawner's own pid is already the proxy's initial watcher via BILI_PARENT_PID.

Behavior change (intended)

  • Old: attaching hermes sessions did not extend the shared proxy's lifetime — the proxy exited when its first (spawning) owner exited, disconnecting all remaining sessions.
  • New: each attaching session registers its host pid; the proxy survives until ALL owners are gone (then exits after the existing WATCHER_IDLE_GRACE_MS grace window). Daemon proxies (no parent pid) refuse with 409 and are unaffected. Worst case, plugin startup gains one synchronous HTTP call with a 2s timeout, only on the attach path.

Tests

  • Strengthened the existing attach driver scenario: asserts the stub proxy received exactly one watcher registration carrying the plugin process's own pid.
  • New scenarios: attach-daemon (stub returns 409 → silent, session starts, tools registered) and attach-watcher-fail (stub returns 500 → warning path, session still starts). Both pin the never-block contract.

Pre-flight

  • npm run typecheck — clean
  • npm run build — clean
  • npm test — 2379 pass / 3 fail / 2 skip. The 3 failures are in tests/claude-native.test.ts hook-e2e tests that spawn real proxy processes and walk live ps; they fail identically when that file runs alone, and this diff touches no src/ file (only hermes-plugin/ + its test harness), so they are unrelated to this change. All 15 hermes tests pass.

Auto-merge gate (§7.4): single-module fix (Python plugin + its test harness), regression tests added, green on rebased head, no config/persistence/wire/cross-repo changes, pure fix:, clean diff, references its issue, does not touch load-bearing infra.

@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: wt-1199-ework-daemon (8eba64c)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-1219

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

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr1219.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 complete — verified mergeable. This is prior ework-daemon work (per #1199 floor 2), so I did a full verification pass rather than redoing it. Everything checks out; all evidence below reproduced locally at head 8eba64cc.

① Original issue (direction) — #1199 is real and this fixes the root cause, not a symptom. On master I confirmed: _spawn_child sets BILI_PARENT_PID=os.getpid(), so the proxy watchdog (src/server.ts:614-645) tracks only the first session's plugin process; the two Python attach paths (BILLION_CONTEXT_ATTACH, discover_instance() incl. the marker-wait poll loop) never called POST /__bili/watcher → first session exit killed the shared proxy under remaining sessions. The fix registers each attaching session's own pid at the existing endpoint — same pattern as the merged TS-side fix (src/claude-native-bootstrap.ts:262-283).

② Regression review (existing behaviors touched by the diff)

  • Spawn path: byte-unchanged in ensure_origin (verified full function at head) — spawner pid remains the initial watcher via BILI_PARENT_PID. ✅ preserved
  • Attach path: state update (origin, mode="attach") unchanged; adds exactly one synchronous HTTP POST capped at the existing HEALTH_PROBE_TIMEOUT_S = 2.0. Disclosed old→new in the PR body per policy. ✅ intentional, disclosed
  • Server side / wire / config schema / persistence: zero src/ changes in the diff. ✅ untouched
  • Daemon proxies: server returns 409 when no parent watchdog is armed (src/server.ts:927-945); plugin treats 409 as silent no-op. ✅ preserved
  • Worst case: +2s plugin startup latency, attach path only — disclosed.

③ Code review — register_watcher mirrors the TS launcher policy point-for-point: same endpoint, {pid} payload, 2s timeout, 409 silent, any other failure logs a warning and never blocks (http_post_json swallows transport exceptions → (None, None) → warn branch). Funneling all three attach sites through _attach() means no future attach site can skip registration — good structural fix. One non-blocking nit: TS accepts any 2xx (!res.ok) while Python requires exactly 200; the server returns exactly 200 on success, so there is no practical difference. URL concat is safe (read_instance_file rstrips /).

④ Test validity — real tests with teeth, not tautologies:

  • Main regression: assert.deepEqual(out.watcher_calls, [{ pid: out.pid }]) — catches missing registration ([]), double registration, or a wrong pid. Without the fix the stub records zero POSTs and this fails.
  • attach-daemon (409) and attach-watcher-fail (500) pin the never-block contract. I checked the harness for vacuity: the stub is an in-process ThreadingHTTPServer reading BC_WATCHER_STATUS from the driver's own env (no child-process hop where propagation could silently break), so both scenarios genuinely exercise the failure paths.
  • All 15 hermes tests pass locally at head.

⑤ Diff cleanliness — 2 files, +66/-8, every line related to #1199. No whitespace churn, no lockfile/version changes.

Freshness & CI — branch is exactly at current master tip (0 behind); CI green on head commit across all jobs (test matrix ubuntu 22/24 + windows 22/24, codex fake-upstream e2e, version-guard, build-artifact).

Local pre-flight at head — npm run typecheck clean, npm run build clean, npm test = 2379 pass / 3 fail / 2 skip, matching the PR body exactly. I proved the 3 failures are unrelated: they are the tests/claude-native.test.ts hook-e2e tests (real proxy spawn + live ps walk) and they fail byte-identically on master in this sandbox — pre-existing environmental failures, not regressions.

Duplicate screening — no duplicate. Sibling open PR #1200 fixes the same bug class for the TS launcher (#1190/kimi) and touches disjoint files (src/claude-native-bootstrap.ts, src/launcher.ts); open PR #1212 touches web/thirdparty-scan files only. No hot-file contention with either.

Auto-merge gate (§7.4) — all 8 criteria met: single-module scope (Python plugin + its test harness), regression tests added and passing, green on rebased head, no config/persistence/wire/cross-repo changes, pure fix:, clean diff, references #1199, no load-bearing infra touched. Merging itself stays human-only.

中文摘要:修复了 hermes 插件两条附着路径(显式 ATTACH 环境变量、发现共享实例)不注册宿主 pid 导致首个会话退出即杀死共享代理的问题(#1199),注册策略与 TS 端完全一致(409 静默、失败只告警不阻塞),已本地复验 typecheck/build/测试全绿且 CI 全绿,测试断言有牙,diff 干净无重复无冲突——可以合并。

@ranxianglei
ranxianglei merged commit 6cb5591 into master Sep 24, 2026
8 checks passed
@ranxianglei
ranxianglei deleted the wt-1199-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

1 participant