HarnessRouter Community Edition: the self-hosted, Apache-2.0 edition of the unified interface for agent harnesses. Run Codex, Claude Code, Hermes, PI, DSH, and more through one API, with sessions, streaming, files, cancellation, and failure handling. Implements the Unified Harness Protocol (UHP), an open standard. Your keys, your infrastructure.
Python agent-apiagent-frameworkagent-harnessai-agentsai-infrastructurecoding-agentconformance-testingdeepseek-harnessharness-engineeringhermesllmopen-protocolopen-sourceopen-standardpi-coding-agentself-hosteduhpunified-apiunified-harness-protocolunified-interface
gateway sweep: a session is aged from its birth or its turn's start as well as its heartbeat (#529)
github-actions[bot]ea4e8ed90b90
diff --git a/gateway/app.py b/gateway/app.pyindex 21b9e972..b6973d3d 100644--- a/gateway/app.py+++ b/gateway/app.py@@ -2035,6 +2035,16 @@ def _inv_ts(created: float) -> str:return str(TRACE_TS_MAX - int(created * 1000)).zfill(14)+def _created_from_prefix(prefix: str) -> float | None:+ """'{org}/{inv}_{sid}' (or the manifest key '{org}/idx/{inv}_{sid}.json') -> epoch seconds.+ The inverse of _inv_ts: the creation time is durable in every prefix."""+ try:+ inv = prefix.rsplit("/", 1)[-1].split("_", 1)[0]+ return (TRACE_TS_MAX - int(inv)) / 1000.0+ except (ValueError, AttributeError):+ return None++def _manifest_key(base: str) -> str:"""Trace base '{org}/{inv}_{sid}' -> the FLAT manifest key '{org}/idx/{inv}_{sid}.json'. The`idx/` namespace holds exactly one object per session, so a prefix-LIST of '{org}/idx/' paginates@@ -3010,17 +3020,31 @@ async def _adopt_orphan_turn(sid: str, org: str, v: dict) -> bool:return True+def _sweep_seen(v: dict) -> float:+ """The session's last sign of life, for the sweep: its heartbeat, floored by its birth (the+ record's created_at, else the time its trace prefix carries) and by its current turn's start+ (turn_at, stamped at accept). A heartbeat of 0 read as "never" aged a session from 1970, past the+ hard cap, and the sweep settled it failed at once (hosted, 2026-10-19: a newborn settled while its+ sandbox was being placed, then completed 34 s later; the newborn trap _reconcile_response already+ guards against). 0 when the record holds none of them."""+ try:+ turn_at = float(v.get("turn_at") or 0)+ except (TypeError, ValueError):+ turn_at = 0.0+ born = _born_seconds(v) or (_created_from_prefix(str(v.get("trace_blob") or "")) or 0.0)+ return max(_hb_seconds(v), born, turn_at)++async def _reconcile_session(v: dict) -> bool:sid = v.get("id") or v.get("session_id")if not sid:return False- try:- hb = float(v.get("heartbeat") or 0)- except Exception: # noqa: BLE001- hb = 0- age = time.time() - hb+ seen = _sweep_seen(v)+ if not seen:+ return False # no time on record at all: an unknown age is past no cap+ age = time.time() - seenif age < _RECONCILE_STALE_S:- return False # fresh heartbeat -> a live turn is still being polled+ return False # fresh heartbeat (or a newborn turn) -> still being polledbase = v.get("trace_blob")status = await _trace_terminal_status(base)if status is None:@@ -9783,9 +9807,12 @@ async def create_response(body: CreateResponseBody, request: Request):# this warmup window burn seconds on a dead-handle sandbox kill before settling.# The heartbeat too: a turn that is accepted is alive, and the orphan sweep reads a# `starting` session with no runner turn and no recent heartbeat as dead (_turn_slot).+ # turn_at: the turn's start, beside the heartbeat; the sweep ages a session from it as well+ # (_sweep_seen), so a later heartbeat-less write never ages this turn from an older one.await _vertex_upsert(sid, {"status": "running", "turn_status": "starting","running_response_id": resp_id, "runner_turn_id": "",- "billing_org": billing_org, "heartbeat": str(time.time())})+ "billing_org": billing_org, "heartbeat": str(time.time()),+ "turn_at": str(time.time())})except Exception as e: # noqa: BLE001 — this write is load-bearing for cross-replica cancel: log itprint(f"[cancel] accept-time running_response_id write failed sid={sid} resp={resp_id}: {str(e)[:120]}",flush=True)diff --git a/gateway/tests/test_sweep_newborn.py b/gateway/tests/test_sweep_newborn.pynew file mode 100644index 00000000..8ba0d491--- /dev/null+++ b/gateway/tests/test_sweep_newborn.py@@ -0,0 +1,78 @@+"""The reconcile sweep ages a session from its heartbeat, its birth and its turn's start, never from+1970. A heartbeat of 0 read as "never" made every running session without one older than the hard+cap, and with no result in its trace the sweep settled it failed at once (hosted, 2026-10-19: a+newborn settled while its sandbox was being placed, then completed 34 s later). The response-level+twin of this trap was fixed in _reconcile_response (test_reconcile_newborn.py)."""+import asyncio+import os+import pathlib+import sys+import time++sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))+os.environ.setdefault("HR_BACKING", "local")+import app as gw # noqa: E402+++def _running(**props) -> dict:+ return {"id": "hsess1", "tenant": "org.p", "status": "running", "running_response_id": "",+ "runner_turn_id": "", "backend": "codex", **props}+++def _sweep(monkeypatch, v: dict) -> tuple[bool, list]:+ writes: list = []++ async def vertex_upsert(sid, props):+ writes.append((sid, dict(props)))+ return True++ async def trace_status(base):+ return None # no result in the trace++ async def sandbox(*a, **k):+ raise AssertionError("the runner is not asked about a session the sweep leaves alone")+ monkeypatch.setattr(gw, "_vertex_upsert", vertex_upsert)+ monkeypatch.setattr(gw, "_trace_terminal_status", trace_status)+ monkeypatch.setattr(gw, "_sandbox", sandbox)+ return asyncio.run(gw._reconcile_session(v)), writes+++def test_a_newborn_session_with_no_heartbeat_yet_is_not_settled(monkeypatch):+ settled, writes = _sweep(monkeypatch, _running(created_at=str(time.time() - 5)))+ assert settled is False and writes == []+++def test_a_follow_up_waiting_for_its_turn_is_aged_from_the_turns_start(monkeypatch):+ """An old task's next turn: its last heartbeat is the previous turn's, hours ago, past the cap;+ the turn was accepted half a minute ago and has not reached its first heartbeat."""+ v = _running(created_at=str(time.time() - 2 * 86400), heartbeat=str(time.time() - 8 * 3600),+ turn_at=str(time.time() - 30))+ settled, writes = _sweep(monkeypatch, v)+ assert settled is False and writes == []+++def test_a_session_dead_past_the_cap_by_a_known_time_still_settles(monkeypatch):+ v = _running(created_at=str(time.time() - 2 * 86400), heartbeat=str(time.time() - 7 * 3600),+ turn_at=str(time.time() - 7 * 3600))+ settled, writes = _sweep(monkeypatch, v)+ assert settled is True and writes == [("hsess1", {"status": "failed", "turn_status": "failed"})]+++def test_a_session_with_no_time_on_record_is_left_alone(monkeypatch):+ settled, writes = _sweep(monkeypatch, _running())+ assert settled is False and writes == []+++def test_the_trace_prefix_dates_a_session_whose_record_has_no_created_at(monkeypatch):+ inv = gw._inv_ts(time.time() - 10) # born ten seconds ago, by its prefix+ settled, writes = _sweep(monkeypatch, _running(trace_blob=f"org.p/{inv}_hsess1"))+ assert settled is False and writes == []+++def test_the_age_floor_reads_each_clock():+ now = time.time()+ assert gw._sweep_seen({}) == 0+ assert gw._sweep_seen({"heartbeat": "x", "created_at": None}) == 0+ assert gw._sweep_seen({"heartbeat": str(now - 100), "turn_at": str(now - 5)}) == now - 5+ assert abs(gw._sweep_seen({"trace_blob": f"org/{gw._inv_ts(now - 60)}_hsess1"}) - (now - 60)) < 0.01+ assert gw._created_from_prefix("not-a-prefix") is None