The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): charge the Windows relaunch wait to the process being replaced
wkwee0d600567299a
diff --git a/hermes_cli/update_cmd_windows.py b/hermes_cli/update_cmd_windows.pyindex 3ca4a4694d7..7df5b13435c 100644--- a/hermes_cli/update_cmd_windows.py+++ b/hermes_cli/update_cmd_windows.py@@ -1448,22 +1448,41 @@ _READY_INTERVAL_S = 0.4 # gateway_windows._wait_for_gateway_ready's poll interv_READY_CONFIRM_S = 2.0 # confirmation window: one hit proves a spawn, not a survived startup-def _poll_until_ready(targets: list, deadline: float, taken: set) -> dict:+def _poll_until_ready(targets: list, deadline: float, taken: set, respawn_window: float | None = None) -> dict:"""``{key: pids}`` for each ``(key, probe, claim_all)`` target whose gateway stayed visible fora whole confirmation window. All targets share one loop, so N targets cost one window, not N:one fleet process scan per tick (``probe(scan)`` calls ``scan()`` only if it needs the fleet),and each target is credited only on its own hits. *taken* holds, while a target is probed, thePIDs earlier targets vouched for (all of a profile's, the first of an unmapped entry's) so onegateway never retires two debts. A hit provisional at the deadline still finishes its window;- a probe that raises leaves its target unready."""+ a probe that raises leaves its target unready.++ *respawn_window*: a target's own liveness budget, in two parts. The restart watcher only spawns+ once the paused PID is gone, and that wait is charged to the *old* process — 25–35 s of a shared+ 30 s deadline on a busy update — while a cold gateway then needs >11 s before its control socket+ answers. So (a) a target first seen late gets ``respawn_window`` counted from its sighting, and+ (b) a target the deadline expires on without ever being seen gets ONE extra window, because the+ spawn it is waiting for may not have happened yet. Charging that wait to the new process is what+ made an update report "relaunch was not verified alive" for a gateway that was up seconds later;+ the verdict is committed and never revisited, so the stale debt surfaced as a manual follow-up on+ the next launch. Both parts are bounded, so a relaunch that truly never happens still fails+ closed. ``None`` keeps the single shared budget.+ """from hermes_cli.gateway import find_gateway_pidsready: dict = {}since: dict = {}failed: set = set()+ graced: set = set()while True:now = _time.monotonic()pending = [t for t in targets if t[0] not in ready and t[0] not in failed and (now < deadline or t[0] in since)]if not pending:+ starved = [key for key, _probe, _all in targets+ if key not in ready and key not in failed and key not in since and key not in graced]+ if respawn_window is not None and starved:+ graced.update(starved)+ deadline = now + respawn_window+ continuereturn readyfleet: list = []@@ -1485,6 +1504,11 @@ def _poll_until_ready(targets: list, deadline: float, taken: set) -> dict:continuevouched = pids if claim_all else pids[:1]taken.update(vouched)+ if respawn_window is not None and key not in since:+ # The replacement did not exist before this tick, so the wait that preceded it belongs+ # to the process it replaced: this target's budget starts now, not at the shared+ # deadline's origin.+ deadline = max(deadline, now + respawn_window)if now - since.setdefault(key, now) >= _READY_CONFIRM_S:ready[key] = vouchedif len(ready) + len(failed) < len(targets):@@ -1509,7 +1533,7 @@ def _verify_relaunched_gateways_alive(token: dict, profiles: dict, unmapped: lishome=h, pid_filter=lambda ps: [p for p in ps if int(p) != o]), True) for name, old_pid in sorted(profiles.items())]targets += [(("unmapped", i), lambda scan, f=_unmapped_ready_filter(entry, taken): f(scan()), False)for i, entry in enumerate(unmapped)]- ready = _poll_until_ready(targets, deadline, taken)+ ready = _poll_until_ready(targets, deadline, taken, respawn_window=_RELAUNCH_VERIFY_TIMEOUT_S)ready_profiles = [name for name in sorted(profiles) if ("profile", name) in ready]ready_unmapped = [entry for i, entry in enumerate(unmapped) if ("unmapped", i) in ready]ready_pids = [pid for pids in ready.values() for pid in pids]diff --git a/tests/hermes_cli/test_relaunch_verify_budget_starts_at_respawn.py b/tests/hermes_cli/test_relaunch_verify_budget_starts_at_respawn.pynew file mode 100644index 00000000000..14583b44cd3--- /dev/null+++ b/tests/hermes_cli/test_relaunch_verify_budget_starts_at_respawn.py@@ -0,0 +1,113 @@+"""The relaunch liveness budget has to start when the replacement process appears.++Regression for the Desktop dialog *"The update finished, but needs one more step (gateway_restart)"*.++``_spawn_gateway_restart_watcher`` respawns the gateway only once the paused PID is gone, and that+graceful exit takes ~25-35 s on a busy update. The shared 30 s ``deadline`` in ``_poll_until_ready``+is computed when the resume starts, so the wait for the *old* process could consume the whole budget:+in both occurrences on this host the replacement was first seen 6-7 s before the deadline expired or+just after it (``gateway-exit-diag.log``: restart 04:50:30 -> first ``gateway.start`` 04:51:03 ->+follow-up recorded 04:51:10). A cold gateway then needs >11 s before its control socket answers - the+hand-off printed "not published yet (its control socket has not answered)" 11 s after the retry's own+spawn. ``_verify_relaunched_gateways_alive`` raised, the updater committed a ``gateway_restart``+follow-up, its in-run retry 20 s later succeeded, and nothing revisited the verdict - so the Desktop+replayed the stale debt at the next boot gate.++These cases drive the poll with a fake clock, so none of them depends on real Windows timing.+"""++from __future__ import annotations++import pytest++from hermes_cli import update_cmd_windows as ucw++# Measured on the reported host: the replacement appears this far into a 30 s budget (restart stage+# 04:50:30 -> first gateway.start 04:51:03, budget origin ~04:50:40).+LATE_RESPAWN_S = 33.0+++class _Clock:+ """``_time`` stand-in: only ``sleep`` advances it, so the poll runs as fast as the CPU allows."""++ def __init__(self) -> None:+ self.t = 0.0++ def monotonic(self) -> float:+ return self.t++ def sleep(self, seconds: float) -> None:+ self.t += seconds+++def clock(monkeypatch):+ c = _Clock()+ monkeypatch.setattr(ucw, "_time", c)+ return c+++def _target(clock: _Clock, appears_at: float, pid: int = 4242, profile: str = "default"):+ """A profile target whose replacement process becomes visible at ``appears_at``."""++ def probe(_scan):+ return [pid] if clock.t >= appears_at else []++ return (("profile", profile), probe, True)+++def test_a_late_replacement_is_still_verified(clock):+ """A respawn that lands after the shared deadline is given its own budget and is credited."""+ ready = ucw._poll_until_ready(+ [_target(clock, appears_at=LATE_RESPAWN_S)], 30.0, set(), respawn_window=30.0)++ assert ready.get(("profile", "default")) == [4242]+ # It still had to be stable for the confirmation window before being credited.+ assert clock.t >= LATE_RESPAWN_S + ucw._READY_CONFIRM_S+ # ...and the extension is bounded: one budget after the sighting, not later.+ assert clock.t <= LATE_RESPAWN_S + 30.0 + ucw._READY_CONFIRM_S+++def test_shared_budget_times_out_when_the_replacement_is_late(clock):+ """Negative control: the pre-fix behaviour must fail exactly this way, or the bug was never real.++ Without the per-target budget the target is dropped by the ``now < deadline`` filter before its+ process exists, so ``_verify_relaunched_gateways_alive`` reports it unready - the reported bug.+ """+ ready = ucw._poll_until_ready([_target(clock, appears_at=LATE_RESPAWN_S)], 30.0, set())++ assert ready == {}+ assert clock.t >= 30.0+++def test_a_replacement_inside_the_original_window_is_unaffected(clock):+ """No overreach: a respawn that lands inside the shared budget behaves exactly as before."""+ ready = ucw._poll_until_ready([_target(clock, appears_at=23.0)], 30.0, set())++ assert ready.get(("profile", "default")) == [4242]+ assert clock.t <= 30.0+++def test_a_replacement_that_never_appears_still_times_out(clock):+ """Fail-closed: the extension cannot keep a relaunch that never happened alive.++ One grace window is granted (the spawn may not have happened yet), then the target is reported+ unready — the verdict is late, never wrong.+ """+ ready = ucw._poll_until_ready(+ [_target(clock, appears_at=float("inf"))], 30.0, set(), respawn_window=30.0)++ assert ready == {}+ assert 2 * 30.0 <= clock.t <= 2 * 30.0 + ucw._READY_CONFIRM_S + 1.0+++def test_one_late_target_does_not_cost_the_early_one_its_pass(clock):+ """The extension belongs to the late target only; a target visible at t=0 is credited as before."""+ early = _target(clock, appears_at=0.0, pid=1111, profile="default")+ late = _target(clock, appears_at=LATE_RESPAWN_S, pid=2222, profile="other")++ ready = ucw._poll_until_ready([early, late], 30.0, set(), respawn_window=30.0)++ assert ready[("profile", "default")] == [1111]+ assert ready[("profile", "other")] == [2222]+ assert clock.t <= LATE_RESPAWN_S + 30.0 + ucw._READY_CONFIRM_S