Files
hermes-agent/hermes_cli/gateway_runtime_cli.py
Teknium 785f1c86a6 fix(desktop): restart a stale attached gateway instead of re-attaching it (review M7)
What: `gateway ensure --json` now publishes the owner's boot commit
(`endpoint.code_sha`, from its control-socket `identify`) and the ensure
client's own commit (`client_code_sha`, same `get_code_identity` resolver).
Desktop compares them in ensureLocalGateway: a ready owner that provably runs
other code is restarted once with a supervised `hermes --profile P gateway
restart` (P = the profile owning the process: `default` for a served
secondary), then re-ensured. The Models-page "Restart Hermes"
(hermes:backend:recycle) runs the same supervised restart for a local
descriptor before dropping it. Both reuse runGatewayEnsure (resolver, spawn
env scrub, Windows cmd.exe quoting) with a 120 s bounded client.

Why: main's 55b51fa8ad8..61b69853eec made Desktop attach only to a backend
booted from its checkout. The attach model dropped that, so after
`hermes update` the running gateway answered model calls 503 "Restart
required", and Restart only tore down processes Desktop owns (none) — the next
ensure re-attached the same stale gateway forever.

Guards: unknown identity on either side (non-git install, older gateway, older
CLI) keeps the plain attach. One restart per (gateway, client) commit pair per
process, shared by concurrent dials; an owner a restart cannot move (a unit
pinned to another checkout) is then attached as-is rather than restarted on
every dial. A failed restart is logged; the re-ensure reports what answers.

Red: local-gateway.test.ts 'a ready owner running other code than this Hermes
is restarted once, then re-ensured' (pre-fix: 1 failed | 13 passed);
backend-recycle.test.ts 'kills the owned SSH serve before the primary child…'
and 'recycles a pooled profile…' (2 failed | 3 passed);
test_gateway_runtime_home_identity.py::test_ready_endpoint_carries_the_owner_boot_commit
(AttributeError: no code_sha). Green: vitest 20/20 (local-gateway,
backend-recycle, local-gateway-python); pytest gateway_runtime* 16 passed,
3 skipped. Live (fake HOME): ensure payload carries matching code_sha /
client_code_sha; `gateway restart` on an unsupervised owner drained it and
the next ensure attached a fresh instance_id.

(cherry picked from commit d8c5356625168e582c5c2ebc64f4950ecf7e0972)
2026-10-06 21:58:29 -07:00

38 lines
1.7 KiB
Python

"""Machine-readable ensure boundary. No bootstrap credentials leave this command."""
from dataclasses import asdict
import json
import sys
from contextlib import redirect_stdout
def ensure_exit_code(result) -> int:
if result.reason_code == "deadline":
return 5
if result.reason_code in {"authorization", "profile_mismatch"}:
return 4
return {"ready": 0, "incompatible": 3, "draining": 6,
"inaccessible": 7, "conflict": 7, "starting": 5, "absent": 5}[result.state]
def cmd_gateway_ensure(args) -> None:
from hermes_constants import get_hermes_home
from hermes_cli.gateway_runtime import ensure_gateway_runtime
try:
# Legacy imported utilities can print; protocol stdout belongs only to us.
with redirect_stdout(sys.stderr):
result = ensure_gateway_runtime(get_hermes_home(), timeout=float(args.timeout))
payload = asdict(result)
if result.endpoint:
payload["endpoint"]["capabilities"] = sorted(result.endpoint.capabilities)
# The code THIS client (and any gateway it starts) runs, resolved exactly like the owner's
# ``endpoint.code_sha``: a client that sees them differ is attached to a gateway that
# outlived ``hermes update`` and must restart it rather than re-attach.
from hermes_cli.version_info import get_code_identity
payload["client_code_sha"] = get_code_identity().get("sha")
code = ensure_exit_code(result)
except (ValueError, TypeError):
payload, code = {"state": "inaccessible", "reason_code": "invalid_invocation", "endpoint": None}, 2
print(json.dumps(payload, separators=(",", ":"), sort_keys=True))
raise SystemExit(code)