Skip to content

The agent that grows with you

fix(update): a flapping relaunch replacement cannot renew its liveness budget forever

Teknium34133f27403e
diff --git a/hermes_cli/update_cmd_windows.py b/hermes_cli/update_cmd_windows.py
index 7df5b13435c..f1142b9574b 100644
--- a/hermes_cli/update_cmd_windows.py
+++ b/hermes_cli/update_cmd_windows.py
@@ -1466,19 +1466,25 @@ def _poll_until_ready(targets: list, deadline: float, taken: set, respawn_window
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.
+ closed. ``None`` keeps the single shared budget. Each target earns its sighting extension once,
+ and the whole poll stops at the entry deadline plus both windows and one confirmation tick: a
+ replacement that flaps in and out must not renew its budget forever.
"""
from hermes_cli.gateway import find_gateway_pids
ready: dict = {}
since: dict = {}
failed: set = set()
graced: set = set()
+ first_seen: set = set()
+ hard_stop = deadline + 2 * (respawn_window or 0.0) + _READY_CONFIRM_S + _READY_INTERVAL_S
while True:
now = _time.monotonic()
+ if now >= hard_stop:
+ return ready
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 key not in ready and key not in failed and key not in first_seen and key not in graced]
if respawn_window is not None and starved:
graced.update(starved)
deadline = now + respawn_window
@@ -1504,10 +1510,11 @@ def _poll_until_ready(targets: list, deadline: float, taken: set, respawn_window
continue
vouched = pids if claim_all else pids[:1]
taken.update(vouched)
- if respawn_window is not None and key not in since:
+ if respawn_window is not None and key not in first_seen:
# 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.
+ first_seen.add(key)
deadline = max(deadline, now + respawn_window)
if now - since.setdefault(key, now) >= _READY_CONFIRM_S:
ready[key] = vouched
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.py
index 14583b44cd3..00c04158883 100644
--- a/tests/hermes_cli/test_relaunch_verify_budget_starts_at_respawn.py
+++ b/tests/hermes_cli/test_relaunch_verify_budget_starts_at_respawn.py
@@ -30,13 +30,19 @@ LATE_RESPAWN_S = 33.0
class _Clock:
"""``_time`` stand-in: only ``sleep`` advances it, so the poll runs as fast as the CPU allows."""
+ MAX_SLEEPS = 10_000 # a poll that never ends fails here instead of hanging the suite
+
def __init__(self) -> None:
self.t = 0.0
+ self.sleeps = 0
def monotonic(self) -> float:
return self.t
def sleep(self, seconds: float) -> None:
+ self.sleeps += 1
+ if self.sleeps > self.MAX_SLEEPS:
+ raise AssertionError(f"poll still running at t={self.t:.1f}s")
self.t += seconds
@@ -111,3 +117,17 @@ def test_one_late_target_does_not_cost_the_early_one_its_pass(clock):
assert ready[("profile", "default")] == [1111]
assert ready[("profile", "other")] == [2222]
assert clock.t <= LATE_RESPAWN_S + 30.0 + ucw._READY_CONFIRM_S
+
+
+def test_a_flapping_replacement_cannot_renew_its_budget_forever(clock):
+ """A replacement that keeps reappearing (visible ~1 s, gone ~1 s) never survives a confirmation
+ window. Each reappearance must not grant a fresh sighting extension: the poll ends, unready,
+ within the entry deadline plus both windows and one confirmation tick."""
+
+ def probe(_scan):
+ return [4242] if int(clock.t) % 2 == 0 else []
+
+ ready = ucw._poll_until_ready([(("profile", "default"), probe, True)], 30.0, set(), respawn_window=30.0)
+
+ assert ready == {}
+ assert clock.t <= 30.0 + 2 * 30.0 + ucw._READY_CONFIRM_S + ucw._READY_INTERVAL_S + 1e-6