Skip to content

The agent that grows with you

refactor(gateway): name the restart_wait reason instead of repeating it

Teknium6a0cb5e64849
diff --git a/apps/desktop/electron/local-gateway.ts b/apps/desktop/electron/local-gateway.ts
index 661dc8477ea..7e791136827 100644
--- a/apps/desktop/electron/local-gateway.ts
+++ b/apps/desktop/electron/local-gateway.ts
@@ -165,6 +165,14 @@ function assertLocalGatewayEndpoint(endpoint: GatewayEndpoint): void {
/** Poll interval while a requested stale-owner restart is in flight. */
export const STALE_RESTART_POLL_MS = 1000
+/** `gateway ensure`'s drain_reason for a restart's after-turn wait (gateway/drain_control.py RESTART_WAIT_REASON). */
+const RESTART_WAIT_REASON = 'restart_wait'
+
+/** A restart's after-turn wait that still hands out its endpoint: attachable, like Python's `attachable()`. */
+function isRestartWait(payload: Record<string, any>): boolean {
+ return payload.state === 'draining' && payload.reason_code === RESTART_WAIT_REASON && Boolean(payload.endpoint)
+}
+
/** Re-ensure while a stale-owner restart runs, without waiting for it to finish (#139041): the first
* answer that is no longer the stale owner wins — its restart wait (attach to answer a parked
* prompt), or a replacement. Once the restart client exits, whatever answers next is final. */
@@ -180,7 +188,7 @@ async function reensureDuringRestart(
const last = finished
const result = await run()
const payload = parseGatewayEnsureOutput(result)
- const restartWait = payload.state === 'draining' && payload.reason_code === 'restart_wait' && Boolean(payload.endpoint)
+ const restartWait = isRestartWait(payload)
const replaced = payload.state === 'ready' && payload.endpoint?.instance_id !== staleInstance
if (last || restartWait || replaced) {return { result, payload }}
@@ -211,7 +219,7 @@ export async function ensureLocalGateway(
// A graceful restart's after-turn wait still owns its turns (#139041): attach to it so a turn parked
// on approval/clarify can be answered; new input queues for the next owner, and the next dial after
// the owner exits reaches its replacement.
- const restartWait = payload.state === 'draining' && payload.reason_code === 'restart_wait' && Boolean(payload.endpoint)
+ const restartWait = isRestartWait(payload)
if (!restartWait && (result.code !== 0 || payload.state !== 'ready')) {
// A refused replacement (another install's or a newer Hermes's gateway) names its remedy in
diff --git a/hermes_cli/gateway_runtime.py b/hermes_cli/gateway_runtime.py
index 0e56427fecc..f54cb472a68 100644
--- a/hermes_cli/gateway_runtime.py
+++ b/hermes_cli/gateway_runtime.py
@@ -57,7 +57,8 @@ def attachable(result: GatewayDiscovery) -> bool:
Shared by every client bootstrap (``connect_gateway`` and Ink's ``gateway_bootstrap.py``)."""
if result.endpoint is None:
return False
- return result.state == "ready" or (result.state == "draining" and result.reason_code == "restart_wait")
+ from gateway.drain_control import RESTART_WAIT_REASON
+ return result.state == "ready" or (result.state == "draining" and result.reason_code == RESTART_WAIT_REASON)
def _canonical_home(home: str | Path) -> str:
@@ -81,7 +82,8 @@ def _endpoint(payload: dict, home: Path, control_home: Path | None = None) -> Ga
# A graceful restart's after-turn wait still owns its in-flight turns (#139041): it stays
# ``draining`` (exit 6, never a fresh-start target) but carries its endpoint, so an interactive
# client can attach to answer a turn parked on approval/clarify. New input queues for the next owner.
- restart_wait = state == "draining" and payload.get("drain_reason") == "restart_wait"
+ from gateway.drain_control import RESTART_WAIT_REASON
+ restart_wait = state == "draining" and payload.get("drain_reason") == RESTART_WAIT_REASON
if state in {"starting", "draining", "conflict"} and not restart_wait:
# An auto-started gateway ending through its idle exit names that, so its clients wait
# for it to go and start a fresh one instead of reporting a refusal.
@@ -117,7 +119,7 @@ def _endpoint(payload: dict, home: Path, control_home: Path | None = None) -> Ga
supervisor=supervisor, capabilities=frozenset(capabilities),
control_home=str(control_home) if control_home is not None else None,
code_sha=code_sha if isinstance(code_sha := payload.get("code_sha"), str) and code_sha else None,
- ), reason_code="restart_wait" if restart_wait else None)
+ ), reason_code=RESTART_WAIT_REASON if restart_wait else None)
def control_home_for(home: Path, endpoint: GatewayEndpoint | None) -> Path: