The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(kanban): stamp the request-scoped session, not the process-global env
teknium11667840d1115
diff --git a/hermes_cli/kanban_db.py b/hermes_cli/kanban_db.pyindex 8fa12250404..6ac24a3f532 100644--- a/hermes_cli/kanban_db.py+++ b/hermes_cli/kanban_db.py@@ -945,8 +945,9 @@ CREATE TABLE IF NOT EXISTS tasks (-- Originating chat/agent session id when the task was created from-- inside an agent loop that propagated ``HERMES_SESSION_ID``. NULL-- for tasks created from the CLI, dashboard, or any path that doesn't- -- set the env var. Indexed so per-session list queries stay cheap on- -- larger boards.+ -- set the env var, and for an id with no ``sessions`` row in this+ -- profile's state.db (kanban_create verifies before stamping). Indexed+ -- so per-session list queries stay cheap on larger boards.session_id TEXT,-- Typed block reason set by ``block_task`` (one of VALID_BLOCK_KINDS, or-- NULL for legacy/un-typed blocks). Drives routing: ``dependency`` neverdiff --git a/tests/tools/test_kanban_provenance.py b/tests/tools/test_kanban_provenance.pyindex 00dd3b0e328..cd3d2d9097e 100644--- a/tests/tools/test_kanban_provenance.py+++ b/tests/tools/test_kanban_provenance.py@@ -75,17 +75,42 @@ def test_tool_create_only_stamps_persisted_ambient_session(tmp_path, monkeypatch"""Ambient worker ids are provenance only after their state.db row exists."""from hermes_cli import kanban_db as kb, kanban_db_connect as kbcfrom tools import kanban_tools as kt+ from gateway.session_context import scoped_current_session_idmonkeypatch.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"}))+ # Bound the way agent construction publishes it (ContextVar); a bare os.environ value is+ # masked once a surface has cleared its session vars, so it is not a stand-in here.+ with scoped_current_session_id(session_id):+ 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)+++def test_tool_create_stamps_request_scoped_session_over_process_env(tmp_path, monkeypatch):+ """In a multi-session process os.environ holds the LAST agent built; the request-scoped+ binding names the conversation that actually ordered the card."""+ from hermes_cli import kanban_db as kb, kanban_db_connect as kbc+ from tools import kanban_tools as kt+ from gateway.session_context import scoped_current_session_id++ monkeypatch.setenv("HERMES_HOME", str(tmp_path))+ monkeypatch.delenv("HERMES_KANBAN_TASK", raising=False)+ monkeypatch.setenv("HERMES_SESSION_ID", "other-session")+ kb.init_db()+ state = SessionDB(db_path=tmp_path / "state.db")+ for sid in ("other-session", "ordering-session"):+ state.create_session(sid, source="cli")+ state.close()++ with scoped_current_session_id("ordering-session"):+ result = json.loads(kt._handle_create({"title": "child", "assignee": "default"}))+ with kbc.connect_closing() as conn:+ assert kb.get_task(conn, result["task_id"]).session_id == "ordering-session"diff --git a/tools/kanban_tools.py b/tools/kanban_tools.pyindex fb6bf331486..152907eb6d6 100644--- a/tools/kanban_tools.py+++ b/tools/kanban_tools.py@@ -903,15 +903,19 @@ def _handle_create(args: dict, **kw) -> str:_check(model_override or not provider_override, "'provider' requires 'model' to be set as well")parents = _coerce_str_list(args.get("parents") or [], "parents", "task ids")with _board(args.get("board")) as (kb, conn):+ from gateway.session_context import get_session_envfrom tools.async_delegation import _current_origin_session_idself_tid = (os.environ.get("HERMES_KANBAN_TASK")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.+ # The ambient id is the request-scoped ContextVar binding, not the process-global+ # os.environ: in a multi-session gateway the env holds the LAST agent built, and an+ # id that never reached its ``sessions`` row is provenance pointing at nothing.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")))+ or _persisted_session_id(get_session_env("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.iddiff --git a/website/docs/user-guide/features/kanban.md b/website/docs/user-guide/features/kanban.mdindex 21d138279ef..cabc5d4248d 100644--- a/website/docs/user-guide/features/kanban.md+++ b/website/docs/user-guide/features/kanban.md@@ -1030,8 +1030,11 @@ for a bare CLI session or a worker whose owning task has no subscriptions.For `kanban_create`, session lineage resolves in this order: explicit `session_id`,the owning worker task's durable session, request-scoped API origin, then the-current process session. Built-in decomposition also inherits its root's durable-session. Session lineage is not itself a notification destination: changing+current session. A candidate id is stamped only when it has a row in the active+profile's `state.db` `sessions` table; an id that resolves nowhere (a session whose+row was never written, or an id inherited from another process) leaves+`session_id` NULL rather than pointing at a session no database can resolve.+Built-in decomposition also inherits its root's durable session. Session lineage is not itself a notification destination: changing`session_id` does not replace existing subscriptions; use `notify-subscribe` and`notify-unsubscribe` to change where events are delivered.