Skip to content

The agent that grows with you

fix(auth): finished moves stay finished; lock waits defer the handoff

alt-glitche29c42a8f5a4
diff --git a/hermes_cli/guest_handoff.py b/hermes_cli/guest_handoff.py
index 20f466aaaad..07ab6513560 100644
--- a/hermes_cli/guest_handoff.py
+++ b/hermes_cli/guest_handoff.py
@@ -166,8 +166,9 @@ def _run_one(path: Path, key: str) -> Optional[dict[str, Any]]:
from hermes_cli.auth_constants import AuthError
try:
bearer = _bearer()
- except AuthError as exc:
- logger.info("Free-tier handoff (%s): account token unavailable (%s)", key[:8], exc.code)
+ except (AuthError, TimeoutError) as exc: # a busy store lock defers the move like a missing token
+ logger.info("Free-tier handoff (%s): account token unavailable (%s)", key[:8],
+ getattr(exc, "code", None) or type(exc).__name__)
return None
record = _update(path, key, _reserve)
if record is None:
@@ -241,8 +242,8 @@ def _classify(status: int, payload: dict[str, Any]) -> str:
def _apply(record: dict[str, Any], outcome: str, payload: dict[str, Any], retry_after: Optional[float],
prior_attempts: int) -> dict[str, Any]:
- if record["state"] == CLAIMED:
- return record # never overwritten, whatever a racing attempt heard
+ if record["state"] not in _ACTIVE:
+ return record # a finished move stays finished, whatever a late attempt heard
record = {**record, "error": payload.get("error"), "next_attempt_at": None}
if outcome == _CLAIMED:
connectors = payload.get("connectors") if isinstance(payload.get("connectors"), dict) else None
@@ -301,10 +302,12 @@ def stop_worker(timeout: float = 5.0) -> None:
def _worker_loop() -> None:
+ from tui_gateway.launch_profile_policy import launch_profile_scope_if_multiplexed
wait = _WORKER_FIRST_TICK_SECONDS
while not _worker_stop.wait(wait):
try:
- drain()
+ with launch_profile_scope_if_multiplexed():
+ drain()
except Exception:
logger.warning("Free-tier handoff retry tick failed; the records stay for the next one", exc_info=True)
wait = _WORKER_INTERVAL_SECONDS
diff --git a/tests/hermes_cli/test_guest_handoff.py b/tests/hermes_cli/test_guest_handoff.py
index ada1e8f47c8..8ba80e32dd2 100644
--- a/tests/hermes_cli/test_guest_handoff.py
+++ b/tests/hermes_cli/test_guest_handoff.py
@@ -305,6 +305,55 @@ def test_a_claimed_record_is_never_reopened(portal):
assert len(portal.claims) == 1 and _only_record()["state"] == record["state"] == "claimed"
+def test_a_late_answer_never_reopens_a_finished_move(portal, monkeypatch):
+ from hermes_cli import guest_handoff
+ _mint_guest()
+ _save(_account())
+ # A's answer is lost; while A hangs past its lease, B hears unknown_token and finishes the move.
+ portal.claim_answers = [_err(404, "unknown_token"), httpx.ReadTimeout("lost")]
+
+ def b_runs_while_a_hangs():
+ portal.on_claim = None
+ _later(monkeypatch, guest_handoff._LEASE_SECONDS + 1)
+ guest_handoff.drain()
+ portal.on_claim = b_runs_while_a_hangs
+ guest_handoff.drain()
+ _later(monkeypatch, 7 * 3600)
+ assert guest_handoff.drain() == []
+ assert len(portal.claims) == 2 and _only_record()["state"] == "gone"
+
+
+def test_a_busy_store_lock_leaves_the_move_for_a_later_run(portal, monkeypatch):
+ from hermes_cli import guest_handoff
+ _mint_guest()
+ _save(_account())
+ real_bearer = guest_handoff._bearer
+ monkeypatch.setattr(guest_handoff, "_bearer", lambda **_kw: (_ for _ in ()).throw(TimeoutError("busy")))
+ assert guest_handoff.drain() == []
+ assert portal.claims == [] and _only_record()["state"] == "pending"
+ monkeypatch.setattr(guest_handoff, "_bearer", real_bearer)
+ guest_handoff.drain()
+ assert _only_record()["state"] == "claimed"
+
+
+def test_the_retry_worker_binds_the_launch_profile(portal, monkeypatch):
+ import threading
+ from agent.secret_scope import current_secret_scope, is_multiplex_active, set_multiplex_active
+ from hermes_cli import guest_handoff
+ ticked, scopes = threading.Event(), []
+ monkeypatch.setattr(guest_handoff, "_WORKER_FIRST_TICK_SECONDS", 0.0)
+ monkeypatch.setattr(guest_handoff, "drain", lambda: (scopes.append(current_secret_scope()), ticked.set()))
+ previous = is_multiplex_active()
+ set_multiplex_active(True)
+ try:
+ guest_handoff.start_worker()
+ assert ticked.wait(5)
+ finally:
+ guest_handoff.stop_worker()
+ set_multiplex_active(previous)
+ assert scopes[0] is not None
+
+
def test_a_run_while_an_attempt_is_in_flight_sends_nothing(portal):
from hermes_cli import guest_handoff
_mint_guest()