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: a turn the sweep settles at the cap settles its response record too, and never starts the agent afterwards (#539)
github-actions[bot]8d2c01170641
diff --git a/gateway/app.py b/gateway/app.pyindex f7ee932c..ebf0e612 100644--- a/gateway/app.py+++ b/gateway/app.py@@ -3333,10 +3333,19 @@ async def _reconcile_response(rid: str, rec: dict) -> dict:vs = str(v.get("status") or "")settled = Nonecapped = False # settled by this reconciler's own hard cap- if vs in ("done", "failed", "cancelled", "incomplete") and time.time() - _hb_seconds(v) < _RECONCILE_STALE_S:+ # The sweep settled THIS response's turn and wrote why on the session (a restart lost it, or+ # the hard cap): the session's word is final, whatever the heartbeat says.+ proven = (v.get("end_reason") in ("interrupted", "timeout")+ and rid == str(v.get("running_response_id") or ""))+ if (vs in ("done", "failed", "cancelled", "incomplete") and time.time() - _hb_seconds(v) < _RECONCILE_STALE_S+ and not proven):# The session is terminal but its owner heartbeated moments ago: the owner is finalizing# (collecting files, writing the checkpoint) and will write the full record itself,# output, usage and served model included. Only an orphan is settled from the session.+ # A proven end is not an owner finalizing: the sweep settles a turn past the cap while its+ # slot still beats (a restore that hangs, _turn_slot), and leaving the record to an owner+ # that never writes kept it `running` for as long as the slot beat (found by the hosted+ # port, 2026-10-21).return recif vs in ("done", "failed", "cancelled", "incomplete"):settled = _RESP_STATUS_MAP.get(vs)@@ -3393,8 +3402,7 @@ async def _reconcile_response(rid: str, rec: dict) -> dict:except Exception: # noqa: BLE001passrec["status"] = settled- if (settled == "failed" and vs == "failed" and v.get("end_reason") in ("interrupted", "timeout")- and rid == str(v.get("running_response_id") or "")):+ if settled == "failed" and vs == "failed" and proven:# the sweep proved why (a restart lost the turn, or the hard cap) and wrote it on the sessionrec["error"] = _interrupted_error() if v["end_reason"] == "interrupted" else _cap_error()elif settled == "failed" and capped and not rec.get("error"):@@ -4364,6 +4372,12 @@ def _refusal_message(conn: dict | None, err: str) -> str:return f"Your {provider} {what} was refused: {err or 'the provider returned an error'}"+def _fail_events(tr: "_RespTranslator", rec: dict) -> list[dict]:+ """A failed turn's closing events: the error the turn proved (rec["error"], the hard cap's),+ else its reason in words."""+ return tr.fail(_turn_failure_message(rec), rec.get("error"))++def _turn_failure_message(rec: dict) -> str:"""What a failed turn says: the org's own key's refusal in plain words when that is why, elsethe last connection's reason in words. Never the tried list itself: its JSON, with our@@ -8572,6 +8586,25 @@ async def _resp_resolve_session(org: str, member: str, prev: str | None, backendreturn sid, None+async def _past_cap_stand_down(sid: str, translator: "_RespTranslator", rec: dict) -> bool:+ """True when the turn is past the sweep's cap before it reached the runner, settled as such.++ The sweep settles a turn past the cap as timed out while it still waits (_reconcile_session:+ a slot that came late, a restore that hung, with _turn_slot beating), and a turn that went on to+ start the agent would finish over that answer, and a terminal status is final. It ends as the+ sweep said, with nothing run. Checked once, just before the agent would start, so the turn ends+ the way any turn that fails before its agent runs does: its message and card written, the trace+ finalized, so the task stays in the person's list. (A check before the turn is stamped running+ returned before any of that, and a new task settled this way vanished from the list.) The clock+ is the accept's, as the sweep's."""+ if not translator.created_at or time.time() - float(translator.created_at) <= _sweep_cap_s():+ return False+ rec["status"] = "failed"+ rec["error"] = _cap_error()+ await _vertex_upsert(sid, {"status": "failed", "turn_status": "failed", "end_reason": "timeout"})+ return True++async def _resp_execute(translator: _RespTranslator, *, org: str, member: str, sid: str,backend: str, chain: list[str], prompt: str, files_in: list[dict],resume: str | None, emit, model_req: str = "", user_text: str = "",@@ -8819,6 +8852,9 @@ async def _resp_execute(translator: _RespTranslator, *, org: str, member: str, s# environment and gives the model calls a child of it. Empty when the caller sent# none, so a turn without a trace context sends none.**(trace_ctx or {})}+ if await _past_cap_stand_down(sid, translator, rec):+ status = "failed"+ break# Stop requested while we were resolving the connection / provisioning the previous# attempt? Honor it BEFORE launching a CLI process (from any replica, via the one signal).if await _turn_cancelled(sid, org, translator.resp_id):@@ -10094,7 +10130,7 @@ async def create_response(body: CreateResponseBody, request: Request):end["status"] = status# A failed turn says why in the transcript, not only in the response record: fail()# carries the message as an error event, which the console prints under the answer.- for ev in (tr.fail(_turn_failure_message(rec)) if status == "failed" else tr.complete(status, produced)):+ for ev in (_fail_events(tr, rec) if status == "failed" else tr.complete(status, produced)):await bus_emit_bg(ev)end["told"] = Trueawait persist(status)@@ -10149,7 +10185,7 @@ async def create_response(body: CreateResponseBody, request: Request):end["status"] = status# A failed turn says why in the transcript, not only in the response record: fail()# carries the message as an error event, which the console prints under the answer.- for ev in (tr.fail(_turn_failure_message(rec)) if status == "failed" else tr.complete(status, produced)):+ for ev in (_fail_events(tr, rec) if status == "failed" else tr.complete(status, produced)):await emit(ev)end["told"] = Trueawait persist(status)@@ -10209,7 +10245,7 @@ async def create_response(body: CreateResponseBody, request: Request):trace_ctx=trace_ctx)# A failed turn says why in the transcript, not only in the response record: fail()# carries the message as an error event, which the console prints under the answer.- for ev in (tr.fail(_turn_failure_message(rec)) if status == "failed" else tr.complete(status, produced)):+ for ev in (_fail_events(tr, rec) if status == "failed" else tr.complete(status, produced)):await bus_emit(ev)obj = tr._response_obj(status)except Exception as e: # noqa: BLE001diff --git a/gateway/tests/test_turn_slot.py b/gateway/tests/test_turn_slot.pyindex f6f74e2b..479bb128 100644--- a/gateway/tests/test_turn_slot.py+++ b/gateway/tests/test_turn_slot.py@@ -6,6 +6,7 @@ settled it, session and response, as failed, and the turn's own final write late(measured 2026-10-12: a background poller read `failed`, error null, four seconds into a 28-secondwait for a slot, and `completed` at 55 s)."""import asyncio+import jsonimport osimport pathlibimport sys@@ -231,3 +232,168 @@ def test_past_the_limit_plus_margin_it_settles_timed_out_and_the_runners_turn_isassert settled is Trueassert writes[0] == {"status": "failed", "turn_status": "failed", "end_reason": "timeout"}assert ("POST", "/turn/rt_1/cancel") in calls+++def _record_sweep(monkeypatch, vertex: dict, rec: dict):+ """The sweep on `vertex` with its running response record `rec`; returns whether it settled and+ the record as it was persisted. Scaled: the cap is max(2, 2) + 1 = 3 s."""+ monkeypatch.setattr(gw, "_GW_MAX_TURN_S", 2)+ monkeypatch.setattr(gw, "_RUNNER_MAX_TURN_S", 2)+ monkeypatch.setattr(gw, "_CAP_MARGIN_S", 1)+ monkeypatch.setattr(gw, "_RECONCILE_STALE_S", 4)+ stored: dict = {}++ async def _vup(sid, props):+ vertex.update(props)++ async def _vget(sid):+ return dict(vertex)++ async def _tts(base, since_ms=0):+ return None++ async def _resp_get(rid):+ return dict(rec)++ async def _blob_get(*a, **k):+ return None++ async def _blob_put(key, data, **k):+ stored.update(json.loads(data))+ return True++ async def _nothing(*a, **k):+ return None++ async def sandbox(*a, **k):+ raise AssertionError("no runner turn on record: nothing to ask or cancel")+ for name, fn in (("_vertex_upsert", _vup), ("_vertex_get", _vget), ("_trace_terminal_status", _tts),+ ("_resp_get", _resp_get), ("_blob_get", _blob_get), ("_blob_put", _blob_put),+ ("_vg_upsert", _nothing), ("_sandbox", sandbox)):+ monkeypatch.setattr(gw, name, fn)+ return asyncio.run(gw._reconcile_session(dict(vertex))), stored+++def test_a_capped_turn_whose_slot_still_beats_settles_its_response_record_too(monkeypatch):+ """Past the cap with no runner turn on record, the sweep settles the session while the slot still+ beats (a restore that hangs), and the response record must settle in the same pass. The record's+ guard for an owner that is finalizing reads a terminal session with a fresh heartbeat as one about+ to write its own record, so the record stayed `running` for as long as the slot beat, and a+ background poller never saw the turn end (found by the hosted port, 2026-10-21)."""+ now = time.time()+ vertex = {"id": "hsess1", "tenant": "org", "status": "running", "turn_status": "starting",+ "runner_turn_id": "", "running_response_id": "resp_1", "turn_at": str(now - 10),+ "heartbeat": str(now)}+ rec = {"id": "resp_1", "status": "running", "_session_id": "hsess1", "output": [], "created_at": now - 10}+ settled, stored = _record_sweep(monkeypatch, vertex, rec)+ assert settled is True and vertex["end_reason"] == "timeout"+ assert stored.get("status") == "failed", "the response record stayed running"+ assert stored["error"] == gw._cap_error()+++def test_a_session_an_owner_is_finalizing_still_leaves_its_record_to_the_owner(monkeypatch):+ """The guard stands for what it is for: a terminal session whose owner beat moments ago, with no+ reason the sweep proved, is an owner writing its own record, and the record is left alone."""+ now = time.time()+ v = {"id": "hsess1", "status": "done", "heartbeat": str(now), "running_response_id": "resp_1"}+ rec = {"id": "resp_1", "status": "running", "_session_id": "hsess1", "output": []}++ async def _vget(sid):+ return dict(v)++ async def _blob_put(*a, **k):+ raise AssertionError("the owner writes this record, not the reconciler")+ monkeypatch.setattr(gw, "_vertex_get", _vget)+ monkeypatch.setattr(gw, "_blob_put", _blob_put)+ assert asyncio.run(gw._reconcile_response("resp_1", dict(rec)))["status"] == "running"+ # a reason on the session that belongs to another response does not open the guard either+ v.update(status="failed", end_reason="timeout", running_response_id="resp_2")+ assert asyncio.run(gw._reconcile_response("resp_1", dict(rec)))["status"] == "running"+++def _drive_a_late_turn(monkeypatch, age_s: float, hydrate=None):+ """Run `_resp_execute` for a turn accepted `age_s` seconds ago (a restore or a wait that hung),+ with the world's edges stubbed; returns (status, rec, vertex writes, runner calls). Scaled: the+ cap is max(2, 2) + 1 = 3 s."""+ monkeypatch.setattr(gw, "_GW_MAX_TURN_S", 2)+ monkeypatch.setattr(gw, "_RUNNER_MAX_TURN_S", 2)+ monkeypatch.setattr(gw, "_CAP_MARGIN_S", 1)+ calls, writes = [], []++ async def _sandbox_json(path, sid, method="POST", body=None, **kw):+ calls.append(path)+ if path == "/turn":+ return {"turn_id": "t1", "status": "running"}+ if path.startswith("/turn/"):+ return {"done": True, "status": "done", "events": [], "n_total": 0}+ return {}++ async def _nothing(*a, **kw):+ return None++ async def _vup(sid, props):+ writes.append(dict(props))++ async def _get_connection(org, name):+ return ({"provider": "anthropic", "base_url": "https://proxy.example/v1"}, "tenant-1")++ async def _harness_plugins(*a, **kw):+ return ([], [], [], [], [])++ async def _harness_env(*a, **kw):+ return ({}, [])++ async def _false(*a, **kw):+ return False++ async def _collect_produced(*a, **kw):+ return []++ async def _finalize(sid, rec):+ rec["finalized"] = rec.get("status")+ for name, fn in (("_sandbox_json", _sandbox_json), ("_vertex_get", _nothing), ("_vertex_upsert", _vup),+ ("_hydrate", hydrate or _nothing), ("_harness_plugins", _harness_plugins), ("_harness_env", _harness_env),+ ("_image_auth", _nothing), ("_get_connection", _get_connection),+ ("_auth_from_conn", lambda *a, **k: {"token": "t", "base_url": "https://proxy.example/v1"}),+ ("_turn_cancelled", _false), ("_collect_produced", _collect_produced), ("_blob_put", _nothing),+ ("_checkpoint", _nothing), ("_trace_finalize", _finalize), ("_browser_close", _nothing),+ ("_recover_trace_cursor", _nothing)):+ monkeypatch.setattr(gw, name, fn)+ monkeypatch.setattr(gw, "RESP_POLL_S", 0)+ monkeypatch.setattr(gw.control_store, "enabled", lambda: False)+ translator = gw._RespTranslator("resp_late", "m", None, True, time.time() - age_s)+ status, _produced, rec = asyncio.run(gw._resp_execute(+ translator, org="org", member="me@local", sid="sess_late", backend="claude",+ chain=["anthropic"], prompt="hi", files_in=[], resume=None, emit=lambda ev: asyncio.sleep(0)))+ return status, rec, writes, calls, translator+++def test_a_turn_that_waited_past_the_cap_for_its_slot_never_starts_the_agent(monkeypatch):+ """The sweep settled it as timed out while it waited; when the slot comes, the turn must not start+ the agent and finish over that answer (terminal is final). It ends as the sweep said: failed,+ reason timeout, nothing run, and it is finalized like any turn that fails before its agent runs,+ so the task stays in the person's list (live on the test VM, 2026-10-21: a check that returned+ before the turn's card was written left a new task out of the list)."""+ status, rec, writes, calls, tr = _drive_a_late_turn(monkeypatch, age_s=10)+ assert "/turn" not in calls, "a turn past the cap started the agent"+ assert status == "failed" and rec["error"] == gw._cap_error()+ assert writes[-1] == {"status": "failed", "turn_status": "failed", "end_reason": "timeout"}+ assert rec.get("finalized") == "failed", "the turn was not finalized, so its card never says how it ended"+ evs = gw._fail_events(tr, rec)+ assert evs[-2]["code"] == "harness_error"+ assert evs[-1]["response"]["error"] == gw._cap_error()+++def test_a_turn_inside_the_cap_starts_as_before(monkeypatch):+ status, rec, writes, calls, _tr = _drive_a_late_turn(monkeypatch, age_s=1)+ assert "/turn" in calls and status == "completed" and "error" not in rec+++def test_a_restore_that_hung_past_the_cap_ends_the_turn_before_the_agent_starts(monkeypatch):+ """The turn began inside the cap and its restore hung past it: it stops before the agent starts."""+ async def _slow_hydrate(sid, rec):+ await asyncio.sleep(2.5)+ status, rec, writes, calls, _tr = _drive_a_late_turn(monkeypatch, age_s=1, hydrate=_slow_hydrate)+ assert "/turn" not in calls and status == "failed" and rec["error"] == gw._cap_error()+ assert writes[-1] == {"status": "failed", "turn_status": "failed", "end_reason": "timeout"}+ assert rec.get("finalized") == "failed"