Skip to content

The agent that grows with you

fix(kanban): trim auto-decompose scope shim and add an invariant test

teknium19541317aeefd
diff --git a/gateway/kanban_watchers_dispatcher.py b/gateway/kanban_watchers_dispatcher.py
index c01cf8c213d..5b3ef08fdbb 100644
--- a/gateway/kanban_watchers_dispatcher.py
+++ b/gateway/kanban_watchers_dispatcher.py
@@ -12,6 +12,7 @@ import os
import sqlite3
import time
from dataclasses import asdict, dataclass
+from pathlib import Path
from typing import Any, Optional
from gateway.kanban_watchers_common import _board_slugs, _positive_int_setting, logger
@@ -247,12 +248,6 @@ class _KanbanDispatcher:
return 0
attempted = 0
successes = 0
- # This tick runs via _to_thread_process_service in a FRESH context, so no
- # per-turn profile scope is installed. With gateway.multiplex_profiles on,
- # agent.secret_scope.get_secret() fails closed without a scope and every
- # decompose attempt dies with UnscopedSecretError before the aux LLM call.
- # Scope the aux-LLM credential reads to the gateway's default profile —
- # the same home load_gateway_config_for_runner() uses for the runner.
with _default_profile_secret_scope():
for slug in self._board_slugs():
if attempted >= auto_decompose_per_tick:
@@ -300,34 +295,21 @@ class _KanbanDispatcher:
@contextlib.contextmanager
def _default_profile_secret_scope():
- """Install the gateway default profile's secret scope when multiplexing is on
- and no scope is active (background ticks run in a fresh context). No-op
- otherwise, so single-profile gateways keep reading os.environ as before.
+ """Install the gateway launch profile's secret scope while multiplexing is on.
+
+ The tick runs via ``_to_thread_process_service`` in a fresh context, so no
+ per-turn scope exists and ``get_secret`` fails closed. The decomposer's aux
+ LLM reads ``auxiliary.*`` from ``get_hermes_home()``, so its credentials come
+ from that same home. No-op for single-profile gateways.
"""
- try:
- from pathlib import Path
-
- from agent.secret_scope import (
- build_profile_secret_scope,
- current_secret_scope,
- is_multiplex_active,
- reset_secret_scope,
- set_secret_scope,
- )
- from hermes_constants import get_hermes_home
- except Exception: # pragma: no cover
- yield
- return
- if not is_multiplex_active() or current_secret_scope() is not None:
- yield
- return
- try:
- secrets = build_profile_secret_scope(Path(get_hermes_home()))
- except Exception:
- logger.debug("kanban auto-decompose: could not build default profile secret scope", exc_info=True)
+ from agent.secret_scope import (
+ build_profile_secret_scope, is_multiplex_active, reset_secret_scope, set_secret_scope)
+ from hermes_constants import get_hermes_home
+
+ if not is_multiplex_active():
yield
return
- token = set_secret_scope(secrets)
+ token = set_secret_scope(build_profile_secret_scope(Path(get_hermes_home())))
try:
yield
finally:
diff --git a/tests/gateway/test_kanban_auto_decompose_secret_scope.py b/tests/gateway/test_kanban_auto_decompose_secret_scope.py
new file mode 100644
index 00000000000..86f725e4a4e
--- /dev/null
+++ b/tests/gateway/test_kanban_auto_decompose_secret_scope.py
@@ -0,0 +1,49 @@
+"""Auto-decompose tick under multiplex.
+
+Regression for #107955 / #57837: the tick runs off-turn in a fresh Context, so
+``get_secret`` fails closed unless the tick installs the launch profile's scope.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import sys
+from types import SimpleNamespace
+
+from agent import secret_scope as ss
+from gateway import kanban_watchers_dispatcher as kwd
+from gateway.kanban_watchers_common import _to_thread_process_service
+
+
+def _dispatcher():
+ settings = kwd._DispatcherSettings(60.0, None, None, 2, 0, True, None, None)
+ return kwd._KanbanDispatcher(SimpleNamespace(DEFAULT_BOARD="default"), settings)
+
+
+def test_auto_decompose_tick_reads_launch_profile_secrets_under_multiplex(monkeypatch, tmp_path):
+ import hermes_cli
+
+ (tmp_path / ".env").write_text("ANTHROPIC_API_KEY=launch-profile-key\n", encoding="utf-8")
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ monkeypatch.setattr(kwd, "_board_slugs", lambda kb: ["default"])
+
+ seen = {}
+
+ def fake_decompose(task_id, author=None):
+ seen["value"] = ss.get_secret("ANTHROPIC_API_KEY")
+ return SimpleNamespace(ok=True, fanout=False, child_ids=None, reason=None)
+
+ fake = SimpleNamespace(list_triage_ids=lambda: ["t1"], decompose_task=fake_decompose)
+ monkeypatch.setitem(sys.modules, "hermes_cli.kanban_decompose", fake)
+ monkeypatch.setattr(hermes_cli, "kanban_decompose", fake, raising=False)
+
+ ss.set_multiplex_active(True)
+ try:
+ # Same hop the gateway uses: fresh Context, no inherited per-turn scope.
+ decomposed = asyncio.run(_to_thread_process_service(_dispatcher().auto_decompose_tick, 5))
+ finally:
+ ss.set_multiplex_active(False)
+
+ assert decomposed == 1
+ assert seen["value"] == "launch-profile-key"
+ assert ss.current_secret_scope() is None
diff --git a/tests/hermes_cli/test_kanban_worker_spawn_toolsets.py b/tests/hermes_cli/test_kanban_worker_spawn_toolsets.py
index 29cd7458a6d..a3c1dce230a 100644
--- a/tests/hermes_cli/test_kanban_worker_spawn_toolsets.py
+++ b/tests/hermes_cli/test_kanban_worker_spawn_toolsets.py
@@ -137,17 +137,9 @@ def test_default_spawn_model_override_survives_real_cli_parse(monkeypatch, tmp_p
def test_default_spawn_resolves_env_passthrough_under_multiplex(monkeypatch, tmp_path):
- """Under multiplex, a worker spawn must not crash when ``terminal.env_passthrough``
- is configured, and the forwarded value must come from the ASSIGNEE profile's own
- secret scope, not the dispatcher's ambient os.environ.
-
- Regression guard: ``_default_spawn`` built the worker env via
- ``build_subprocess_env(scrub_secrets=True)`` with no profile secret scope
- installed. Any registered ``env_passthrough`` var made
- ``resolve_passthrough_value()`` call ``get_secret()`` with no scope while
- multiplexing was active, which raises ``UnscopedSecretError`` and crashed the
- spawn for every task, every profile, as soon as ``terminal.env_passthrough``
- was configured anywhere.
+ """Under multiplex a worker spawn with ``terminal.env_passthrough`` configured must
+ forward the ASSIGNEE profile's own value, never crash on an unscoped read or leak the
+ dispatcher's ambient os.environ (#109494).
"""
root = tmp_path / ".hermes"
profile = root / "profiles" / "elias"