Skip to content

The agent that grows with you

fix(kanban): reject phantom session provenance

KoNit-K3c33231096a8
diff --git a/tests/tools/test_kanban_provenance.py b/tests/tools/test_kanban_provenance.py
index eeb69a0bbea..00dd3b0e328 100644
--- a/tests/tools/test_kanban_provenance.py
+++ b/tests/tools/test_kanban_provenance.py
@@ -3,6 +3,8 @@ import json
import pytest
+from hermes_state import SessionDB
+
@pytest.mark.parametrize("linked,explicit", [(False, None), (True, None), (False, "override")])
def test_worker_create_keeps_durable_origin(tmp_path, monkeypatch, linked, explicit):
@@ -19,6 +21,10 @@ def test_worker_create_keeps_durable_origin(tmp_path, monkeypatch, linked, expli
user_id="user", notifier_profile="default", delivery_mode="notify",
delivery_metadata={"scope_id": "guild", "parent_chat_id": "forum"})
expected = kn.list_notify_subs(conn, owner)[0]
+ if explicit:
+ state = SessionDB(db_path=tmp_path / "state.db")
+ state.create_session(explicit, source="cli")
+ state.close()
monkeypatch.setenv("HERMES_KANBAN_TASK", owner)
monkeypatch.setenv("HERMES_SESSION_ID", "ephemeral")
monkeypatch.setattr(async_delegation, "_current_origin_session_id", lambda: "api-origin")
@@ -59,3 +65,27 @@ def test_tool_subscription_captures_conversation_anchors(tmp_path, monkeypatch):
metadata = kn.list_notify_subs(conn, result["task_id"])[0]["delivery_metadata"]
assert metadata["scope_id"] == "guild"
assert metadata["parent_chat_id"] == "forum"
+
+
[email protected](("session_id", "persisted"), [
+ ("phantom-session", False),
+ ("persisted-session", True),
+])
+def test_tool_create_only_stamps_persisted_ambient_session(tmp_path, monkeypatch, session_id, persisted):
+ """Ambient worker ids are provenance only after their state.db row exists."""
+ from hermes_cli import kanban_db as kb, kanban_db_connect as kbc
+ from tools import kanban_tools as kt
+
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ monkeypatch.delenv("HERMES_KANBAN_TASK", raising=False)
+ monkeypatch.setenv("HERMES_SESSION_ID", session_id)
+ kb.init_db()
+ state = SessionDB(db_path=tmp_path / "state.db")
+ if persisted:
+ state.create_session(session_id, source="cli")
+ state.close()
+
+ result = json.loads(kt._handle_create({"title": "child", "assignee": "default"}))
+ with kbc.connect_closing() as conn:
+ task = kb.get_task(conn, result["task_id"])
+ assert task.session_id == (session_id if persisted else None)
diff --git a/tools/kanban_tools.py b/tools/kanban_tools.py
index 229464685b2..fb6bf331486 100644
--- a/tools/kanban_tools.py
+++ b/tools/kanban_tools.py
@@ -862,6 +862,24 @@ def _handle_attachments(args: dict, **kw) -> str:
_fields(a, _ATTACHMENT_FIELDS) for a in kb.list_attachments(conn, tid)]})
+def _persisted_session_id(session_id: Optional[str]) -> Optional[str]:
+ """Return a session id only when it is present in this profile's state.db."""
+ if not session_id:
+ return None
+ try:
+ from hermes_state import SessionDB
+ from hermes_constants import get_hermes_home
+
+ state = SessionDB(db_path=get_hermes_home() / "state.db", read_only=True)
+ except Exception: # state.db may not exist for a CLI/dashboard invocation
+ logger.debug("Could not open state.db to verify Kanban provenance", exc_info=True)
+ return None
+ try:
+ return session_id if state.get_session(session_id) else None
+ finally:
+ state.close()
+
+
@_kanban_handler("kanban_create")
def _handle_create(args: dict, **kw) -> str:
"""Create a (child) task; orchestrator workers use this to fan out."""
@@ -890,8 +908,10 @@ def _handle_create(args: dict, **kw) -> str:
if _is_dispatcher_owned_worker() else None)
self_task = kb.get_task(conn, self_tid) if self_tid else None
# The worker/API runtime may be transient; the owning task's origin is durable.
- session_id = (args.get("session_id") or (self_task.session_id if self_task else None)
- or _current_origin_session_id() or os.environ.get("HERMES_SESSION_ID"))
+ session_id = (_persisted_session_id(args.get("session_id"))
+ or (self_task.session_id if self_task else None)
+ or _persisted_session_id(_current_origin_session_id())
+ or _persisted_session_id(os.environ.get("HERMES_SESSION_ID")))
if project_id is None and workspace_kind is None and workspace_path is None:
if self_task is not None and self_task.project_id:
project_id, project_source_task_id = self_task.project_id, self_task.id