The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): retry a hook re-sync until it applies; scope standalone syncs
Teknium8f9aae913072
diff --git a/gateway/run_profile_reconcile.py b/gateway/run_profile_reconcile.pyindex 7288d90d5e1..16d88c239ce 100644--- a/gateway/run_profile_reconcile.py+++ b/gateway/run_profile_reconcile.py@@ -14,7 +14,7 @@ changed since its adapters were last built is re-scanned too: creators make thebot token afterwards, and without this an adapter-less profile would stay adapter-less forever.The same watcher tick (on every gateway, multiplexed or not) re-syncs a home's shell hooks when its-``shell-hooks-allowlist.json`` changed: a non-TTY gateway skips unconsented hooks, so consent recorded+``shell-hooks-allowlist.json`` (or ``config.yaml``) changed: a non-TTY gateway skips unconsented hooks, so consent recordedafterwards must register them without a restart (#132993). That re-runs hook registration only, neveran adapter rebuild."""@@ -40,6 +40,13 @@ _PROFILE_SIGNATURE_FILES = ("config.yaml", ".env")_OWN_GATEWAY_PROBE_TIMEOUT_SECS = 5.0+def _stat_signature(path: Path) -> Optional[tuple]:+ try:+ return file_signature(os.stat(path))+ except OSError:+ return None++def profile_serve_signature(home: Path) -> tuple:"""Cheap change detector for a served profile's credentials/config: file signature per file."""sig = []@@ -103,8 +110,8 @@ class GatewayProfileReconcileMixin:logger.warning("Served-profile reconcile failed; retrying next cycle", exc_info=True)async def _sync_hooks_on_consent_change(self) -> None:- """Re-sync the shell hooks of each home (launch + served) whose consent allowlist changed since- the last tick, under that home's scope: hooks consented since start register, revoked ones stop+ """Re-sync the shell hooks of each home (launch + served) whose consent allowlist or config+ changed since the last tick, under that home's scope: hooks consented since start register, revoked ones stopfiring. A home seen for the first time syncs once (consent may have landed after startupregistered). Hook registration only: adapters and sessions are never touched."""from hermes_constants import get_process_hermes_home@@ -113,24 +120,28 @@ class GatewayProfileReconcileMixin:self._resync_changed_hook_consent, homes, self._hook_consent_signatures or {})def _resync_changed_hook_consent(self, homes, seen: dict) -> dict:- """Blocking half of ``_sync_hooks_on_consent_change``; returns the signatures it observed."""+ """Blocking half of ``_sync_hooks_on_consent_change``; returns the signatures it applied. The+ key covers ``config.yaml`` too: a sync that read an empty/unreadable config applied nothing,+ so its recovery must re-run it. A failed sync is left unrecorded and retried next tick."""from agent.shell_hooks import ALLOWLIST_FILENAME, sync_from_configfrom gateway.run import _load_gateway_config, _profile_runtime_scope+ from gateway.run_turn import GatewayTurnMixincurrent = {}for home in (h.resolve() for h in homes):- try:- current[str(home)] = sig = file_signature(os.stat(home / ALLOWLIST_FILENAME))- except OSError:- current[str(home)] = sig = None- if str(home) in seen and seen[str(home)] == sig:+ sig = tuple(_stat_signature(home / name) for name in (ALLOWLIST_FILENAME, "config.yaml"))+ if seen.get(str(home), ()) == sig:+ current[str(home)] = sigcontinueload = partial(_load_gateway_config, home / "config.yaml")- with _log_suppressed(logging.WARNING, "shell-hook re-sync failed for %s", home, exc_info=True):- if not self._multiplex_on():- sync_from_config(load)- continue- with _profile_runtime_scope(home):+ scope = (_profile_runtime_scope(home) if self._multiplex_on()+ else GatewayTurnMixin._standalone_launch_scope())+ try:+ with scope:sync_from_config(load)+ except Exception: # health: allow BLE001 -- one home's failure never blocks the others+ logger.warning("shell-hook re-sync failed for %s; retrying next cycle", home, exc_info=True)+ continue+ current[str(home)] = sigreturn current# ── reconcile ─────────────────────────────────────────────────────────────────────────────────diff --git a/tests/gateway/test_hook_consent_resync.py b/tests/gateway/test_hook_consent_resync.pyindex 64839fc1f99..ffa899f7d36 100644--- a/tests/gateway/test_hook_consent_resync.py+++ b/tests/gateway/test_hook_consent_resync.py@@ -77,6 +77,16 @@ async def test_consent_change_registers_and_revokes_hook_without_restart(tmp_pat_approve(target, command, False)await async_wait_until(lambda: _hooks_in(target) == [], timeout=10, what="hook unregistered")assert bool(passes) == multiplex++ # Consent landing while config.yaml is unreadable applies nothing; its recovery alone,+ # with no further allowlist edit, must still register the hook.+ (target / "config.yaml").rename(target / "config.yaml.bak")+ _approve(target, command, True)+ if multiplex:+ await _passes(2)+ assert _hooks_in(target) == []+ (target / "config.yaml.bak").rename(target / "config.yaml")+ await async_wait_until(lambda: len(_hooks_in(target)) == 1, timeout=10, what="hook after config recovery")finally:runner._running = Falsewatcher.cancel()diff --git a/website/docs/user-guide/features/hooks.md b/website/docs/user-guide/features/hooks.mdindex cdddeb48bed..b59ade9aea9 100644--- a/website/docs/user-guide/features/hooks.md+++ b/website/docs/user-guide/features/hooks.md@@ -2070,7 +2070,7 @@ printf '{}\n'### Consent model-Each unique `(event, command)` pair prompts the user for approval the first time Hermes sees it, then persists the decision to `~/.hermes/shell-hooks-allowlist.json`. Subsequent runs (CLI or gateway) skip the prompt. The classic CLI asks on its terminal before it opens its gateway session; the gateway re-reads `hooks:` and the allowlist whenever a local session (CLI, TUI, Desktop) is created or resumed, so a hook added, approved, removed or revoked since the gateway started takes effect then. A change to a profile's allowlist alone (an approval or `hermes hooks revoke`) also takes effect within about 30 seconds without any session opening, on every profile a gateway serves.+Each unique `(event, command)` pair prompts the user for approval the first time Hermes sees it, then persists the decision to `~/.hermes/shell-hooks-allowlist.json`. Subsequent runs (CLI or gateway) skip the prompt. The classic CLI asks on its terminal before it opens its gateway session; the gateway re-reads `hooks:` and the allowlist whenever a local session (CLI, TUI, Desktop) is created or resumed, so a hook added, approved, removed or revoked since the gateway started takes effect then. A change to a profile's allowlist or `hooks:` block (an approval, `hermes hooks revoke`, an edit) also takes effect within about 30 seconds without any session opening, on every profile a gateway serves.Three escape hatches bypass the interactive prompt — any one is sufficient: