The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): retry transiently-failed profile starts instead of parking them scanned
beardthelionac97c507797d
diff --git a/gateway/run_adapters.py b/gateway/run_adapters.pyindex fb83d9771df..5d204023d87 100644--- a/gateway/run_adapters.py+++ b/gateway/run_adapters.py@@ -862,18 +862,27 @@ class GatewayAdapterLifecycleMixin:claimed = self._primary_resource_claims(active)profile_homes = _multiplex_profile_homes(self.config)self._served_profile_signatures = {}+ transient_failed = set()for profile_name, profile_home in profile_homes:if profile_name == active:continue # handled by the primary startup loop# Preserve changes made while the initial connection is awaiting I/O.- self._served_profile_signatures[profile_name] = profile_serve_signature(profile_home)+ scan_signature = profile_serve_signature(profile_home)try:connected += await self._start_one_profile_adapters(profile_name, profile_home, claimed)except MultiplexConfigError:raiseexcept Exception as e:logger.error("Failed to start adapters for profile '%s': %s", profile_name, e, exc_info=True)+ # Not acknowledged: the reconcile watcher retries a transiently-failed profile.+ transient_failed.add(profile_name)+ else:+ self._served_profile_signatures[profile_name] = scan_signatureself._record_served_profiles(active, profile_homes)+ # ``_note_served_profiles`` fills a missing signature with the current one; that refill+ # would park a transiently-failed profile before the first watcher tick can retry it.+ for profile_name in transient_failed:+ self._served_profile_signatures.pop(profile_name, None)self._restore_secondary_completion_ledgers(profile_homes)return connecteddiff --git a/gateway/run_profile_reconcile.py b/gateway/run_profile_reconcile.pyindex b44b2417a3c..8d0b17a597c 100644--- a/gateway/run_profile_reconcile.py+++ b/gateway/run_profile_reconcile.py@@ -116,6 +116,7 @@ class GatewayProfileReconcileMixin:await self._unserve_profile(name, known[name])result["removed"].append(name)claimed = self._live_resource_claims(active)+ transient_failed = set()for name in added + changed:# Only acknowledge the configuration observed before connecting;# a setup save during an awaited handshake needs another scan.@@ -126,10 +127,15 @@ class GatewayProfileReconcileMixin:# Boot refuses to run with such a profile; at runtime we park just this profile.logger.error("[MULTIPLEX] Profile '%s' not served: %s", name, exc)connected = 0+ sigs[name] = scan_signatureexcept Exception:logger.error("[MULTIPLEX] Failed to start adapters for profile '%s'", name, exc_info=True)connected = 0- sigs[name] = scan_signature+ # A transient failure is not the deliberate park above: leave the signature+ # unacknowledged so the next reconcile retries the connect.+ transient_failed.add(name)+ else:+ sigs[name] = scan_signatureif name in added:logger.info("[MULTIPLEX] Now serving profile '%s' (%s adapter(s) connected; %s)", name, connected, reason)result["added"].append(name)@@ -145,6 +151,11 @@ class GatewayProfileReconcileMixin:result["removed"].append(name)added = [n for n in added if n != name]self._record_served_profiles(active, list(current.items()))+ # ``_note_served_profiles`` fills a missing signature with the current one; that refill+ # would park a transiently-failed profile exactly like the config-error case above.+ for name in transient_failed:+ if isinstance(self._served_profile_signatures, dict):+ self._served_profile_signatures.pop(name, None)if added:await self._after_profiles_added([(n, current[n]) for n in added])result["served_profiles"] = self.served_profile_names()diff --git a/tests/gateway/test_multiplex_hot_serve.py b/tests/gateway/test_multiplex_hot_serve.pyindex ab5fda4e82a..fda4cb68eca 100644--- a/tests/gateway/test_multiplex_hot_serve.py+++ b/tests/gateway/test_multiplex_hot_serve.py@@ -144,6 +144,208 @@ async def test_deleted_profile_is_torn_down_and_unrouted_others_untouched(tmp_paassert alpha_adapter.disconnected is False+async def test_transient_start_failure_is_retried_on_next_reconcile(tmp_path, monkeypatch):+ """A hot-added profile whose adapter start raises a transient error (secret backend+ unreachable, a half-written config.yaml) must not be recorded as scanned: the next+ reconcile retries it. Only the deliberate MultiplexConfigError park is acknowledged."""+ runner, home = _runner(tmp_path, monkeypatch)+ _mkprofile(home, "alpha", "DISCORD_BOT_TOKEN=alpha-token\n")+ with patch("hermes_cli.profiles.get_active_profile_name", return_value="default"):+ await runner._start_secondary_profile_adapters()++ _mkprofile(home, "gamma", "DISCORD_BOT_TOKEN=gamma-token\n")+ attempts = []+ real_start = runner._start_one_profile_adapters++ async def flaky(name, profile_home, claimed):+ attempts.append(name)+ if len(attempts) == 1:+ raise OSError("secret backend unreachable")+ return await real_start(name, profile_home, claimed)++ runner._start_one_profile_adapters = flaky+ first = await runner.reconcile_served_profiles()+ assert first["added"] == ["gamma"]+ assert attempts == ["gamma"]+ assert Platform.DISCORD not in runner._profile_adapters.get("gamma", {})+ # Unacknowledged: the failed scan's signature must not survive ``_note_served_profiles``.+ assert "gamma" not in runner._served_profile_signatures++ second = await runner.reconcile_served_profiles()+ assert attempts == ["gamma", "gamma"]+ assert second["rescanned"] == ["gamma"]+ assert runner._profile_adapters["gamma"][Platform.DISCORD].token.strip().endswith("gamma-token")+++async def test_failed_rescan_of_served_profile_is_retried(tmp_path, monkeypatch):+ """Same contract on the rescan path: a served profile whose re-scan start raises keeps+ retrying; its old signature must not be replaced by the failed scan's."""+ runner, home = _runner(tmp_path, monkeypatch)+ alpha_dir = _mkprofile(home, "alpha", "DISCORD_BOT_TOKEN=alpha-token\n")+ with patch("hermes_cli.profiles.get_active_profile_name", return_value="default"):+ await runner._start_secondary_profile_adapters()+ assert runner._profile_adapters["alpha"][Platform.DISCORD]++ attempts = []+ real_start = runner._start_one_profile_adapters++ async def flaky(name, profile_home, claimed):+ attempts.append(name)+ if len(attempts) == 1:+ raise RuntimeError("transient")+ return await real_start(name, profile_home, claimed)++ runner._start_one_profile_adapters = flaky+ (alpha_dir / ".env").write_text(+ "DISCORD_BOT_TOKEN=alpha-token\nTELEGRAM_BOT_TOKEN=tg\n", encoding="utf-8"+ )+ await runner.reconcile_served_profiles()+ assert attempts == ["alpha"]+ await runner.reconcile_served_profiles()+ assert attempts == ["alpha", "alpha"]+++async def test_transient_startup_failure_is_retried_by_first_reconcile(tmp_path, monkeypatch):+ """The same acknowledgement gap existed on the boot path: a profile whose start raises+ during ``_start_secondary_profile_adapters`` must stay unsigned so the reconcile watcher+ retries it instead of parking it adapter-less until a config edit or restart."""+ runner, home = _runner(tmp_path, monkeypatch)+ _mkprofile(home, "gamma", "DISCORD_BOT_TOKEN=gamma-token\n")+ attempts = []+ real_start = runner._start_one_profile_adapters++ async def flaky(name, profile_home, claimed):+ attempts.append(name)+ if len(attempts) == 1:+ raise OSError("secret backend unreachable")+ return await real_start(name, profile_home, claimed)++ runner._start_one_profile_adapters = flaky+ with patch("hermes_cli.profiles.get_active_profile_name", return_value="default"):+ await runner._start_secondary_profile_adapters()+ assert attempts == ["gamma"]+ assert "gamma" not in runner._served_profile_signatures++ result = await runner.reconcile_served_profiles()+ assert attempts == ["gamma", "gamma"]+ assert result["rescanned"] == ["gamma"]+ assert runner._profile_adapters["gamma"][Platform.DISCORD].token.strip().endswith("gamma-token")+++async def test_transient_secret_hydrate_failure_retries_through_real_start_path(tmp_path, monkeypatch):+ """End to end through the real ``_start_one_profile_adapters``: the failure is raised inside+ it at ``hydrate_profile_secret_sources`` (the network-dependent step of+ ``_load_secondary_profile_config``), not at a mocked seam. The boot failure leaves the+ profile served-but-unsigned; the next reconcile runs the real config load, profile scope,+ adapter creation and connect to success."""+ from types import SimpleNamespace++ runner, home = _runner(tmp_path, monkeypatch)+ del runner._start_one_profile_adapters # instance stub off: exercise the real method+ runner._register_config_hooks = lambda *a, **kw: None+ runner._configure_profile_adapter = lambda *a: None+ runner._sync_voice_mode_state_to_adapter = lambda *a: None+ runner._restore_secondary_completion_ledgers = lambda *a: None+ runner._adapter_credential_claim = lambda *a: None+ runner._adapter_listener_claim = lambda *a: None+ runner._busy_text_modes_by_profile = {}+ runner._busy_input_modes_by_profile = {}+ connected = []++ async def _connect(adapter, platform):+ connected.append(platform)+ return True++ async def _noop_added(profiles):+ pass++ runner._create_adapter = lambda platform, config: SimpleNamespace(platform=platform)+ runner._connect_initial_adapter_with_timeout = _connect+ runner._after_profiles_added = _noop_added++ import hermes_cli.env_loader as env_loader+ hydrate_calls = []+ real_hydrate = env_loader.hydrate_profile_secret_sources++ def _flaky(home_arg):+ hydrate_calls.append(str(home_arg))+ if len(hydrate_calls) == 1:+ raise OSError("secret backend unreachable")+ return real_hydrate(home_arg)++ monkeypatch.setattr(env_loader, "hydrate_profile_secret_sources", _flaky)++ gamma_dir = _mkprofile(home, "gamma", "DISCORD_BOT_TOKEN=gamma-token\n")+ with patch("hermes_cli.profiles.get_active_profile_name", return_value="default"):+ await runner._start_secondary_profile_adapters()+ assert connected == []+ assert "gamma" not in runner._served_profile_signatures+ assert _served_record(home) == ["default", "gamma"]++ result = await runner.reconcile_served_profiles()+ assert result["rescanned"] == ["gamma"]+ assert connected == [Platform.DISCORD]+ assert runner._profile_adapters["gamma"][Platform.DISCORD].platform == Platform.DISCORD+ assert runner._served_profile_signatures["gamma"] == profile_serve_signature(gamma_dir)+++async def test_transient_failed_profile_deleted_before_retry_is_unserved(tmp_path, monkeypatch):+ """Degenerate state: a profile that failed transiently and is deleted before the retry must+ be torn down cleanly; the signature slot it never got cannot KeyError the unserve."""+ runner, home = _runner(tmp_path, monkeypatch)+ _mkprofile(home, "alpha", "DISCORD_BOT_TOKEN=alpha-token\n")+ with patch("hermes_cli.profiles.get_active_profile_name", return_value="default"):+ await runner._start_secondary_profile_adapters()+ gamma_dir = _mkprofile(home, "gamma", "DISCORD_BOT_TOKEN=gamma-token\n")++ async def boom(name, profile_home, claimed):+ raise OSError("transient")++ runner._start_one_profile_adapters = boom+ await runner.reconcile_served_profiles()+ assert "gamma" not in runner._served_profile_signatures++ from hermes_constants import mark_named_profile_deleted+ mark_named_profile_deleted(gamma_dir)+ result = await runner.reconcile_served_profiles()+ assert result["removed"] == ["gamma"]+ assert "gamma" not in runner._served_profile_homes+ assert _served_record(home) == ["default", "alpha"]+++async def test_config_error_park_is_not_retried_until_config_changes(tmp_path, monkeypatch):+ """MultiplexConfigError is the deliberate park: acknowledged, not retried, until the+ profile's signature actually changes."""+ from gateway.run import MultiplexConfigError++ runner, home = _runner(tmp_path, monkeypatch)+ _mkprofile(home, "alpha", "DISCORD_BOT_TOKEN=alpha-token\n")+ with patch("hermes_cli.profiles.get_active_profile_name", return_value="default"):+ await runner._start_secondary_profile_adapters()++ gamma_dir = _mkprofile(home, "gamma", "DISCORD_BOT_TOKEN=gamma-token\n")+ attempts = []++ async def parked(name, profile_home, claimed):+ attempts.append(name)+ raise MultiplexConfigError("open dm_policy")++ runner._start_one_profile_adapters = parked+ await runner.reconcile_served_profiles()+ await runner.reconcile_served_profiles()+ assert attempts == ["gamma"]++ (gamma_dir / ".env").write_text("DISCORD_BOT_TOKEN=gamma-token2\n", encoding="utf-8")+ await runner.reconcile_served_profiles()+ assert attempts == ["gamma", "gamma"]++@pytest.mark.asyncioasync def test_hot_added_profile_cannot_double_claim_a_live_secondary_token(tmp_path, monkeypatch):"""Boot's duplicate-credential guard sees every profile at once; a hot add must see the LIVE