Skip to content

The agent that grows with you

refactor(serve): share the lock settle window with the orphan reaper

kshitijk4poor091b78390169
diff --git a/hermes_cli/web_server_owner_exit.py b/hermes_cli/web_server_owner_exit.py
index d92d162b01a..d8763da5a54 100644
--- a/hermes_cli/web_server_owner_exit.py
+++ b/hermes_cli/web_server_owner_exit.py
@@ -23,15 +23,16 @@ from typing import Callable, Optional
_log = logging.getLogger(__name__)
DEFAULT_OWNER_POLL_S = 15.0
-# The Desktop writes the lock right after the spawn returns; a young process may still see an
-# older lock for a moment, so supersession only counts once the backend has settled.
-MIN_AGE_S = 90.0
_CONFIRMATIONS = 2
def should_retire_superseded(*, lock: Optional[dict], my_nonce: str, age_s: float) -> bool:
- """Retire only when a valid lock provably names another spawn of this ownership slot."""
- return lock is not None and lock["spawnNonce"] != my_nonce and age_s >= MIN_AGE_S
+ """Retire only when a valid lock provably names another spawn of this ownership slot. A young
+ process may still see the previous spawn's lock, so it must first outlive the same settle window
+ the orphan reaper gives the Desktop to write the lock."""
+ from hermes_cli.dashboard_procs import _REAP_MIN_AGE_SECONDS
+
+ return lock is not None and lock["spawnNonce"] != my_nonce and age_s >= _REAP_MIN_AGE_SECONDS
def start_owner_watchdog(server, *, lock_path: Path, nonce: str,
diff --git a/tests/hermes_cli/test_web_server_owner_exit.py b/tests/hermes_cli/test_web_server_owner_exit.py
index 0f7aeab8859..8c42b5c0cfe 100644
--- a/tests/hermes_cli/test_web_server_owner_exit.py
+++ b/tests/hermes_cli/test_web_server_owner_exit.py
@@ -9,7 +9,7 @@ from __future__ import annotations
import json
from pathlib import Path
-from hermes_cli.dashboard_procs import read_valid_backend_lock
+from hermes_cli.dashboard_procs import _REAP_MIN_AGE_SECONDS, read_valid_backend_lock
from hermes_cli.web_server_owner_exit import should_retire_superseded, start_owner_watchdog
OID, ME, NEW = "f" * 32, "a" * 16, "b" * 16
@@ -39,6 +39,8 @@ def test_retires_only_on_a_valid_lock_naming_another_spawn(tmp_path):
assert verdict("{not json") is False # unreadable
assert verdict({**_lock(NEW), "schemaVersion": 99}) is False # another Desktop build's lock
assert verdict(_lock(NEW), age_s=5.0) is False # just spawned: the lock may not name us yet
+ # Same settle window as the orphan reaper: both wait for the Desktop to write the lock.
+ assert verdict(_lock(NEW), age_s=_REAP_MIN_AGE_SECONDS - 1) is False
class _Server:
@@ -59,7 +61,7 @@ class _Fence:
def _run(fence, nonces):
server, seq = _Server(), iter(nonces)
- clock = iter(range(0, 10_000, 100)) # every poll is well past the young-process window
+ clock = iter(range(0, 100_000, 1000)) # every poll is well past the young-process window
def read_lock(_path):
nonce = next(seq, nonces[-1])
return _lock(nonce) if nonce else None