diff --git a/tests/tools/test_kanban_tools.py b/tests/tools/test_kanban_tools.py index 20deaeae9e4c..5751d082a7b2 100644 --- a/tests/tools/test_kanban_tools.py +++ b/tests/tools/test_kanban_tools.py @@ -2416,6 +2416,7 @@ def _sub_index(subs): "thread_id": getattr(s, "thread_id", None), "user_id": getattr(s, "user_id", None), "delivery_metadata": getattr(s, "delivery_metadata", None), + "notifier_profile": getattr(s, "notifier_profile", None), }) return out @@ -2457,6 +2458,30 @@ def test_create_subscribes_gateway_session(monkeypatch, worker_env): } +def test_create_subscribes_gateway_session_with_active_profile_when_env_missing(monkeypatch, worker_env): + """Gateway auto-subscribe rows must be owned by the active profile even + when session/env profile markers are missing. Otherwise every Telegram + gateway with the same chat_id can deliver another bot's Kanban event.""" + from tools import kanban_tools as kt + monkeypatch.setenv("HERMES_SESSION_PLATFORM", "telegram") + monkeypatch.setenv("HERMES_SESSION_CHAT_ID", "chat-42") + monkeypatch.delenv("HERMES_SESSION_PROFILE", raising=False) + monkeypatch.delenv("HERMES_PROFILE", raising=False) + monkeypatch.setattr("hermes_cli.profiles.get_active_profile_name", lambda: "spanorama") + + out = kt._handle_create({ + "title": "auto-sub active profile", + "assignee": "peer", + }) + d = json.loads(out) + assert d["ok"] is True + assert d["subscribed"] is True, d + + subs = _sub_index(_list_subs_for_task(d["task_id"])) + assert len(subs) == 1 + assert subs[0]["notifier_profile"] == "spanorama" + + def test_create_subscribes_tui_session_via_session_key(monkeypatch, worker_env): """TUI / desktop sessions don't have a platform/chat_id (single local channel), but the parent process exports HERMES_SESSION_KEY. diff --git a/tools/kanban_tools.py b/tools/kanban_tools.py index c781ff884573..c3188a198983 100644 --- a/tools/kanban_tools.py +++ b/tools/kanban_tools.py @@ -1339,6 +1339,12 @@ def _maybe_auto_subscribe(conn: Any, task_id: str) -> bool: get_session_env("HERMES_SESSION_PROFILE", "") or os.environ.get("HERMES_PROFILE") ) + if not notifier_profile: + try: + from hermes_cli.profiles import get_active_profile_name + notifier_profile = get_active_profile_name() or "default" + except Exception: + notifier_profile = "default" delivery_metadata: dict[str, Any] = {} if thread_id: delivery_metadata["thread_id"] = thread_id