Skip to content

The agent that grows with you

fix: a recovering launch pays an unready gateway relaunch once per backoff window

Tekniumdd23f9c7b22f
diff --git a/hermes_cli/update_pause_record.py b/hermes_cli/update_pause_record.py
index e7fa2e1db44..160dec5f1d5 100644
--- a/hermes_cli/update_pause_record.py
+++ b/hermes_cli/update_pause_record.py
@@ -895,6 +895,8 @@ def _resume_fenced(claim_path: Path, body: dict) -> None:
body = read(claim_path) or body
token = drop_never_stopped(dict(body["token"]))
draining = split_draining(token)
+ _hold_backing_off(token, draining)
+ attempted = _relaunch_keys(token)
token.update(resume_needed=True, recovery=True)
try:
if _has_work(token):
@@ -907,13 +909,56 @@ def _resume_fenced(claim_path: Path, body: dict) -> None:
print(f" ⚠ Could not restart every paused gateway: {exc}. Run `hermes update` or "
"`hermes gateway start`.", file=sys.stderr)
finally:
+ _back_off_unready(token, attempted, draining)
_hand_back(claim_path, body, token, draining)
+# A recovering launch runs before every command (chat, doctor, a Desktop-spawned serve) and waits
+# for each relaunched gateway to become ready. One that does not is re-launched only after this
+# backoff (``relaunch_retry``), so an interactive command pays that wait at most once per window;
+# the debt itself stays owed, and ``hermes update`` (which drops the backoff) retries at once.
+_RELAUNCH_RETRY_BASE_S = 60.0
+_RELAUNCH_RETRY_MAX_S = 3600.0
+
+
+def _relaunch_keys(token: dict) -> set[str]:
+ return ({f"profile:{name}" for name in token.get("profiles") or {}}
+ | {f"unmapped:{u.get('pid')}" for u in token.get("unmapped") or [] if u.get("argv")})
+
+
+def _hold_backing_off(token: dict, held: dict) -> None:
+ """Move the profile/unmapped entries still inside their relaunch backoff from *token* into *held*
+ (beside the draining ones): owed, not relaunched by this launch."""
+ now = time.time()
+ waiting = {key for key, state in (token.get("relaunch_retry") or {}).items() if now < float(state.get("next_at") or 0)}
+ held["profiles"].update({n: p for n, p in token["profiles"].items() if f"profile:{n}" in waiting})
+ held["unmapped"].extend(u for u in token["unmapped"] if u.get("argv") and f"unmapped:{u.get('pid')}" in waiting)
+ token["profiles"] = {n: p for n, p in token["profiles"].items() if f"profile:{n}" not in waiting}
+ token["unmapped"] = [u for u in token["unmapped"] if u not in held["unmapped"]]
+
+
+def _back_off_unready(token: dict, attempted: set[str], held: dict) -> None:
+ """Start or extend the backoff of each attempted entry the resume left owed (not ready, not
+ launched); forget it for one that came back. A tree-gate deferral attempted nothing."""
+ retry = dict(token.get("relaunch_retry") or {})
+ if not token.get("resume_deferred"):
+ unready = _relaunch_keys(token)
+ for key in attempted:
+ if key not in unready:
+ retry.pop(key, None)
+ continue
+ attempts = int((retry.get(key) or {}).get("attempts") or 0) + 1
+ retry[key] = {"attempts": attempts, "next_at": time.time() + min(
+ _RELAUNCH_RETRY_BASE_S * 2 ** min(attempts - 1, 6), _RELAUNCH_RETRY_MAX_S)}
+ owed = _relaunch_keys(token) | _relaunch_keys(held)
+ token["relaunch_retry"] = {key: state for key, state in retry.items() if key in owed}
+
+
def _hand_back(claim_path: Path, body: dict, token: dict, draining: dict) -> None:
- """Retire the claim when nothing is owed; else hand it back unowned so the next launch retries
- at once — this launch may live for hours (a chat). A draining process stays owed until it has
- exited and been restarted. A failed rewrite leaves our claimer line, dead once we exit."""
+ """Retire the claim when nothing is owed; else hand it back unowned for the next launch — this
+ launch may live for hours (a chat) — which retries at once, except a relaunch that did not
+ become ready (its backoff, :func:`_back_off_unready`). A draining process stays owed until it
+ has exited and been restarted. A failed rewrite leaves our claimer line, dead once we exit."""
token["profiles"] = {**(token.get("profiles") or {}), **draining["profiles"]}
token["unmapped"] = [*(token.get("unmapped") or []), *draining["unmapped"]]
owed = bool(token.get("resume_needed") or token.get("resume_deferred") or draining["profiles"] or draining["unmapped"])
diff --git a/tests/hermes_cli/test_update_pause_recover_backoff.py b/tests/hermes_cli/test_update_pause_recover_backoff.py
new file mode 100644
index 00000000000..a48da682b8d
--- /dev/null
+++ b/tests/hermes_cli/test_update_pause_recover_backoff.py
@@ -0,0 +1,52 @@
+"""A launch's paused-gateway recovery never makes every command wait on the same unready relaunch.
+
+``recover()`` runs before every command (chat, doctor, a Desktop-spawned serve). The Windows resume
+it calls waits out a readiness budget per relaunched gateway; that resume cannot run off Windows, so
+a stand-in reports what it was handed and fails the way an unready relaunch does. The record, the
+claim, the hand-back and the backoff are the real ones, on real files.
+"""
+
+from __future__ import annotations
+
+import json
+import sys
+
+import pytest
+
+from hermes_cli import update_pause_record as pause_record
+
+pytestmark = pytest.mark.skipif(sys.platform == "win32", reason="the stand-in replaces the native resume")
+
+
+def _claims() -> list:
+ return pause_record._claims(pause_record.record_path())
+
+
+def test_an_unready_relaunch_stalls_one_launch_per_backoff_window_and_stays_owed(tmp_path, monkeypatch):
+ import hermes_cli.update_cmd_windows as w
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ pause_record.write(pause_record.stamp_tree({"resume_needed": True, "profiles": {"alpha": 4194000}}),
+ owner=pause_record.UNOWNED) # a killed update's set; pid 4194000 is long gone
+ handed = []
+
+ def resume(token):
+ handed.append(sorted(token.get("profiles") or {}))
+ print("relaunched; waiting for readiness")
+ raise RuntimeError("Windows gateway relaunch after update was not verified alive: alpha")
+
+ monkeypatch.setattr(w, "_resume_windows_gateways_after_update", resume)
+ pause_record.recover(["status"])
+ pause_record.recover(["chat"])
+ assert handed == [["alpha"]], "a launch inside the backoff waited on the unready relaunch again"
+ [claim] = _claims()
+ token = pause_record.read(claim)["token"]
+ assert token["profiles"] == {"alpha": 4194000}, "the unready gateway's restart debt was dropped"
+ assert token["relaunch_retry"]["profile:alpha"]["attempts"] == 1
+
+ body = json.loads(claim.read_text(encoding="utf-8"))
+ body["token"]["relaunch_retry"]["profile:alpha"]["next_at"] = 0 # the window elapsed
+ claim.write_text(json.dumps(body), encoding="utf-8")
+ pause_record.recover(["status"])
+ assert handed == [["alpha"], ["alpha"]], "the debt was not retried once its backoff elapsed"
+ [claim] = _claims()
+ assert pause_record.read(claim)["token"]["relaunch_retry"]["profile:alpha"]["attempts"] == 2