Skip to content

The agent that grows with you

fix(review): replay the launch ledger regardless of the first consumer's profile scope (#123265)

teknium1d6c919136897
diff --git a/tests/gateway/test_multiplex_unserved_shared_ingress.py b/tests/gateway/test_multiplex_unserved_shared_ingress.py
index 515bcdcfbcd..f4a3efd2d4e 100644
--- a/tests/gateway/test_multiplex_unserved_shared_ingress.py
+++ b/tests/gateway/test_multiplex_unserved_shared_ingress.py
@@ -99,3 +99,26 @@ async def test_secondary_whatsapp_served_by_default_is_not_warned(monkeypatch, c
assert not [r for r in caplog.records if r.levelno == logging.WARNING and "not being served" in r.getMessage()]
assert [r for r in caplog.records if r.levelno == logging.INFO and "not served" in r.getMessage()]
+
+
+async def test_boot_replays_the_launch_ledger_before_secondaries_and_watchers(monkeypatch, tmp_path):
+ """The gateway's idle notification loop (``_async_delegation_watcher``, spawned after this phase)
+ reads ``completion_queue`` directly, so the launch profile's durable completions must already
+ be queued by the boot hook — in the LAUNCH scope, before any secondary is bound (#123265)."""
+ from tools import async_delegation, process_registry as pr_mod
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / "launch"))
+ monkeypatch.setattr(pr_mod.process_registry, "_completions_restored", False)
+ order = []
+ monkeypatch.setattr(async_delegation, "restore_undelivered_completions",
+ lambda q: order.append(("restore", async_delegation._db_path())) or 0)
+ runner = _runner()
+
+ async def secondaries():
+ order.append(("secondaries", None))
+ return 0
+
+ runner._start_secondary_profile_adapters = secondaries
+ runner._unserved_shared_ingress_warnings = lambda: []
+ assert await runner._start_secondary_profiles(0, []) == (False, 0)
+ assert order == [("restore", tmp_path / "launch" / "state.db"), ("secondaries", None)]
diff --git a/tests/tools/test_process_registry_lazy_restore.py b/tests/tools/test_process_registry_lazy_restore.py
index 938a91e935d..f0ec61b50e8 100644
--- a/tests/tools/test_process_registry_lazy_restore.py
+++ b/tests/tools/test_process_registry_lazy_restore.py
@@ -28,3 +28,23 @@ def test_restore_runs_once_on_first_drain(monkeypatch):
registry.drain_notifications("sess")
assert registry.restore_completions() == 0
assert calls == [registry.completion_queue] # first consumer restores, exactly once
+
+
+def test_first_drain_under_secondary_scope_replays_the_launch_ledger(monkeypatch, tmp_path):
+ """Under multi-profile ``hermes serve`` the first consumer is a session bound to a secondary
+ profile (TUI poller / prompt_turn drain); the once-per-process replay must still read the LAUNCH
+ ledger, or it is never replayed for the life of the process."""
+ from hermes_constants import get_hermes_home, reset_hermes_home_override, set_hermes_home_override
+ from tools import async_delegation, process_registry as pr_mod
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / "launch"))
+ ledgers = []
+ monkeypatch.setattr(async_delegation, "restore_undelivered_completions",
+ lambda q: ledgers.append(async_delegation._db_path()) or 0)
+ registry = pr_mod.ProcessRegistry()
+ token = set_hermes_home_override(tmp_path / "launch" / "profiles" / "b")
+ try:
+ registry.drain_notifications("sess")
+ assert get_hermes_home() == tmp_path / "launch" / "profiles" / "b" # caller scope untouched
+ finally:
+ reset_hermes_home_override(token)
+ assert ledgers == [tmp_path / "launch" / "state.db"]
diff --git a/tools/process_registry.py b/tools/process_registry.py
index 2cbcb120120..ebbf2d5127c 100644
--- a/tools/process_registry.py
+++ b/tools/process_registry.py
@@ -1908,18 +1908,26 @@ class ProcessRegistry(ProcessCheckpointMixin):
return not (is_async_delegation and evt.get("restored"))
def restore_completions(self) -> int:
- """Rehydrate durable delegation completions from the launch profile's ledger, once per
+ """Rehydrate durable delegation completions from the LAUNCH profile's ledger, once per
process. Called by the first consumer that drains the queue (CLI/TUI drain, gateway boot,
- TUI poller) so a mere ``import model_tools`` never touches state.db (#123265)."""
+ TUI poller) so a mere ``import model_tools`` never touches state.db (#123265). The replay
+ always runs in the launch scope: the TUI poller / prompt_turn drain call this under the
+ session's profile binding, and a once-per-process replay taken under a secondary's scope
+ would leave the launch ledger unreplayed for the life of the process. Secondaries are
+ replayed by the gateway's ``_restore_secondary_completion_ledgers``."""
if self._completions_restored:
return 0
self._completions_restored = True
+ from hermes_constants import reset_hermes_home_override, set_hermes_home_override
+ token = set_hermes_home_override(None)
try:
from tools.async_delegation import restore_undelivered_completions
return restore_undelivered_completions(self.completion_queue)
except Exception as exc:
logger.warning("Could not restore async delegation completions: %s", exc)
return 0
+ finally:
+ reset_hermes_home_override(token)
def drain_notifications(
self, session_key: str = "", owns_event=None, *, skip_poll_observed: bool = True,