The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(gateway): drop dead degraded token field; de-churn salvage diff
kshitij2a0d0bc698a5
diff --git a/gateway/run.py b/gateway/run.pyindex 9064ed001f9..5cced736f0c 100644--- a/gateway/run.py+++ b/gateway/run.py@@ -16663,10 +16663,9 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew# Fail-closed on timeout: never enter the transcript region without a# lease. Outer dispatch returns a bounded rejection/resend notice rather# than recreating the exact concurrent-turn corruption this lease exists- # to prevent. Released- # in _handle_message's finally via _release_turn_lease — granted per- # (routing key, run generation) so a stale unwind can't release a- # newer turn's lease.+ # to prevent. Released in _handle_message's finally via+ # _release_turn_lease — granted per (routing key, run generation) so a+ # stale unwind can't release a newer turn's lease._lease_registry = getattr(self, "_turn_leases", None)if _lease_registry is not None:try:diff --git a/gateway/turn_lease.py b/gateway/turn_lease.pyindex 012b7c93ebf..a47ad65a43d 100644--- a/gateway/turn_lease.py+++ b/gateway/turn_lease.py@@ -97,32 +97,29 @@ class TurnLeaseTimeoutError(TimeoutError):class TurnLeaseToken:"""Handle returned by :meth:`SessionTurnLeaseRegistry.acquire`.- ``degraded`` is retained for compatibility with older callers and test- doubles, but :meth:`acquire` no longer returns degraded tokens: a timeout- raises :class:`TurnLeaseTimeoutError` instead. ``released`` makes release- idempotent.+ A timeout raises :class:`TurnLeaseTimeoutError` instead of returning a+ token, so every token handed out is a held lease. ``released`` makes+ release idempotent."""- __slots__ = ("session_id", "owner_key", "generation", "degraded", "released")+ __slots__ = ("session_id", "owner_key", "generation", "released")def __init__(self,session_id: str,owner_key: str,generation: int,- degraded: bool = False,) -> None:self.session_id = session_idself.owner_key = owner_keyself.generation = generation- self.degraded = degradedself.released = Falsedef __repr__(self) -> str: # pragma: no cover - debug aidreturn (f"TurnLeaseToken(session_id={self.session_id!r}, "f"owner_key={self.owner_key!r}, generation={self.generation}, "- f"degraded={self.degraded}, released={self.released})"+ f"released={self.released})")@@ -291,7 +288,6 @@ class SessionTurnLeaseRegistry:"""if (token is None- or token.degradedor token.releasedor not new_session_idor new_session_id == token.session_id@@ -329,11 +325,11 @@ class SessionTurnLeaseRegistry:"""Release ``token``'s lease. Idempotent; ownership-checked.Returns True only when this exact token was the current holder and- the lock was freed. A degraded token, a re-release, or a stale token- whose slot has since been granted to a newer turn are all safe- no-ops — a stale unwind can never release a newer turn's lease.+ the lock was freed. A re-release or a stale token whose slot has+ since been granted to a newer turn are both safe no-ops — a stale+ unwind can never release a newer turn's lease."""- if token is None or token.degraded or token.released:+ if token is None or token.released:return Falsetoken.released = Truelease = self._leases.get(token.session_id)diff --git a/tests/gateway/test_config_env_bridge_authority.py b/tests/gateway/test_config_env_bridge_authority.pyindex 20a30246410..23824ba69e6 100644--- a/tests/gateway/test_config_env_bridge_authority.py+++ b/tests/gateway/test_config_env_bridge_authority.py@@ -21,9 +21,7 @@ import pytestPROJECT_ROOT = Path(__file__).resolve().parents[2]-def _run_gateway_import(- hermes_home: Path, initial_env: dict[str, str]-) -> dict[str, str]:+def _run_gateway_import(hermes_home: Path, initial_env: dict[str, str]) -> dict[str, str]:"""Import gateway.run in a clean subprocess and return the post-import env.The bridge runs at module-import time, so simply importing is enough@@ -100,15 +98,9 @@ def _run_gateway_import(return out-def _write_config(- home: Path,- agent_cfg: dict | None = None,- display_cfg: dict | None = None,- timezone: str | None = None,- gateway_cfg: dict | None = None,-) -> None:+def _write_config(home: Path, agent_cfg: dict | None = None, display_cfg: dict | None = None,+ timezone: str | None = None, gateway_cfg: dict | None = None) -> None:import yaml-cfg: dict = {}if agent_cfg:cfg["agent"] = agent_cfg@@ -135,22 +127,16 @@ def hermes_home(tmp_path: Path) -> Path:def test_config_gateway_timeout_wins_over_stale_env(hermes_home: Path) -> None:"""Every agent.* bridge key must be config-authoritative, not .env-authoritative."""- _write_config(- hermes_home,- agent_cfg={- "gateway_timeout": 1800,- "gateway_timeout_warning": 900,- "session_stall_timeout": 300,- },- )- _write_env(- hermes_home,- {- "HERMES_AGENT_TIMEOUT": "60",- "HERMES_AGENT_TIMEOUT_WARNING": "30",- "HERMES_SESSION_STALL_TIMEOUT": "15",- },- )+ _write_config(hermes_home, agent_cfg={+ "gateway_timeout": 1800,+ "gateway_timeout_warning": 900,+ "session_stall_timeout": 300,+ })+ _write_env(hermes_home, {+ "HERMES_AGENT_TIMEOUT": "60",+ "HERMES_AGENT_TIMEOUT_WARNING": "30",+ "HERMES_SESSION_STALL_TIMEOUT": "15",+ })env = _run_gateway_import(hermes_home, initial_env={})@@ -190,15 +176,21 @@ def test_default_turn_lease_timeout_overrides_stale_env_when_key_is_omitted(def test_default_turn_lease_timeout_matches_the_runtime_fallback() -> None:- """The advertised config default must match the fail-closed runtime."""+ """The advertised config default must match the fail-closed runtime fallback.++ Invariant, not a snapshot: whatever the default becomes, config and the+ lease registry's DEFAULT_LEASE_WAIT must move together.+ """+ from gateway.turn_lease import DEFAULT_LEASE_WAITfrom hermes_cli.config import DEFAULT_CONFIG- assert DEFAULT_CONFIG["agent"]["gateway_turn_lease_timeout"] == 1800+ assert (+ float(DEFAULT_CONFIG["agent"]["gateway_turn_lease_timeout"])+ == DEFAULT_LEASE_WAIT+ )-def test_config_platform_connect_timeout_supplies_env_when_unset(- hermes_home: Path,-) -> None:+def test_config_platform_connect_timeout_supplies_env_when_unset(hermes_home: Path) -> None:"""config.yaml:gateway.platform_connect_timeout supplies the env var whenit isn't already set (#19776 — config surface for the Discord connecttimeout, replacing the undocumented env-var-only workaround)."""diff --git a/tests/gateway/test_turn_lease.py b/tests/gateway/test_turn_lease.pyindex bd03d807e08..552fc98b2d2 100644--- a/tests/gateway/test_turn_lease.py+++ b/tests/gateway/test_turn_lease.py@@ -48,7 +48,7 @@ def test_alias_key_turn_waits_and_order_is_preserved():token = await registry.acquire("sess-1", owner_key=owner_key, generation=generation, timeout=5)- assert token is not None and not token.degraded+ assert token is not Noneevents.append(f"load:{owner_key}")await asyncio.sleep(hold) # simulate run + flushevents.append(f"flush:{owner_key}")@@ -394,9 +394,7 @@ def test_rebind_moves_serialization_to_new_session_id():async def scenario():registry = SessionTurnLeaseRegistry()- token = await registry.acquire(- "parent", owner_key="key-a", generation=1, timeout=5- )+ token = await registry.acquire("parent", owner_key="key-a", generation=1, timeout=5)assert registry.rebind(token, "child") is Trueassert token is not None and token.session_id == "child"@@ -409,7 +407,7 @@ def test_rebind_moves_serialization_to_new_session_id():assert registry.release(token) is Truet2 = await waiter- assert t2 is not None and not t2.degraded+ assert t2 is not Noneregistry.release(t2)_run(scenario())