From 16f4cf1a099e30de31a23da67466e5c9b492a927 Mon Sep 17 00:00:00 2001 From: yanhuojiyusheng <2398249916@qq.com> Date: Sun, 25 Oct 2026 15:49:31 +0800 Subject: [PATCH] fix(hermes): recognize spawned proxy in per-instance registry --- hermes-plugin/__init__.py | 11 ++++++++--- tests/hermes-native.test.ts | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/hermes-plugin/__init__.py b/hermes-plugin/__init__.py index 75099f0a7c..5e12432506 100644 --- a/hermes-plugin/__init__.py +++ b/hermes-plugin/__init__.py @@ -987,9 +987,14 @@ def ensure_origin(sidecar: Dict[str, str]) -> Optional[str]: deadline = time.monotonic() + SPAWN_WAIT_S while time.monotonic() < deadline: inst = read_instance_file() - if inst and inst.get("launch_token") == token and probe_proxy(inst["origin"]): - _state.update(origin=inst["origin"], mode="spawn") - return inst["origin"] + candidates = ([inst] if inst else []) + read_registry_entries() + for candidate in candidates: + if candidate.get("launch_token") != token: + continue + if not pid_alive(candidate.get("pid")) or not probe_proxy(candidate["origin"]): + continue + _state.update(origin=candidate["origin"], mode="spawn", instance_id=candidate["instance_id"]) + return candidate["origin"] if child.poll() is not None: logger.warning("billion-context: proxy child exited before becoming healthy " "(code %s); log: %s", child.returncode, diff --git a/tests/hermes-native.test.ts b/tests/hermes-native.test.ts index 0ff8532d7d..3703ed8353 100644 --- a/tests/hermes-native.test.ts +++ b/tests/hermes-native.test.ts @@ -460,6 +460,29 @@ elif SCENARIO == "no-sidecar": elif SCENARIO == "spawn-fail": install_sidecar(True) mod.register(ctx) +elif SCENARIO.startswith("spawn-registry-"): + install_sidecar(False) + class Child: + returncode = None + def poll(self): + return None + def terminate(self): + self.returncode = -15 + child = Child() + def spawn(sidecar, port, token): + own = inst_rec(False, token=token if SCENARIO != "spawn-registry-wrong-token" else "foreign") + own["instanceId"] = "own-child" + if SCENARIO == "spawn-registry-dead": + own["pid"] = -1 + write_registry("own-child", own) + write_origin(inst_rec(False, token="foreign", instance_id="last-writer")) + return child + mod._spawn_child = spawn + mod.pid_alive = lambda pid: pid == os.getpid() + mod.SPAWN_WAIT_S = 0.3 + origin = mod.ensure_origin(mod.load_sidecar()) + out["env_before_ready"] = origin + out["state_after_fail"] = "terminated" if child.returncode == -15 else "running" elif SCENARIO.startswith("attach-window"): # #3261: the host knows the model window (context_length_cache.yaml) — verify it now # rides both report channels; the ambiguous leg pins "no guess" on conflicting entries. @@ -679,6 +702,8 @@ print(json.dumps(out)) `; interface DriverOut { + env_before_ready?: string | null; + state_after_fail?: string; scenario: string; origin: string; pid: number; @@ -1044,6 +1069,17 @@ describe("python plugin runtime (subprocess)", () => { assert.equal(out!.env_https_proxy, null); }); + for (const scenario of ["spawn-registry-ready", "spawn-registry-wrong-token", "spawn-registry-dead"]) { + test(`${scenario}: spawn readiness survives a foreign proxy-origin without accepting foreign or dead entries`, () => { + if (!PY) return; + const { out, err } = runDriver(scenario); + assert.ok(out, err); + const ready = scenario === "spawn-registry-ready"; + assert.equal(out.env_before_ready, ready ? out.origin : null); + assert.equal(out.state_after_fail, ready ? "running" : "terminated"); + }); + } + test("spawn failure: child dies -> inert, env untouched, starting marker cleaned up", () => { if (!PY) { console.warn("skip: no python3/python interpreter on this machine (hermes requires Python 3.11+)");