The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(kanban): delegate_task children never act on the parent's HERMES_KANBAN_TASK
teknium9977df914dd1
diff --git a/agent/title_generator.py b/agent/title_generator.pyindex b43fe10b3f4..a84ab93af31 100644--- a/agent/title_generator.py+++ b/agent/title_generator.py@@ -14,6 +14,7 @@ from typing import Any, Callable, Optionalfrom agent.auxiliary_client import call_llmfrom agent.context_compressor import LEGACY_SUMMARY_PREFIX+from agent.delegation_context import is_dispatcher_owned_worker_contextfrom agent.message_content import flatten_message_textlogger = logging.getLogger(__name__)@@ -467,9 +468,10 @@ def _session_is_untitled(session_db, session_id: str) -> bool:def _kanban_task_title() -> Optional[str]:- """Kanban worker: the card's title, or ``Kanban task <id>`` when the board can't be read; None elsewhere."""+ """Kanban worker: the card's title, or ``Kanban task <id>`` when the board can't be read; None elsewhere+ (including delegate_task children of the worker, which inherit the env var but are not the card)."""task_id = (os.environ.get("HERMES_KANBAN_TASK") or "").strip()- if not task_id:+ if not task_id or not is_dispatcher_owned_worker_context():return Nonetry:from hermes_cli import kanban_db, kanban_db_connectdiff --git a/agent/turn_finalizer.py b/agent/turn_finalizer.pyindex 516cb8d141c..f466ce35cbf 100644--- a/agent/turn_finalizer.py+++ b/agent/turn_finalizer.py@@ -12,6 +12,7 @@ from contextlib import suppressfrom typing import Any, Callable, List, Optional, Tuplefrom agent.codex_responses_adapter import _summarize_user_message_for_log+from agent.delegation_context import is_dispatcher_owned_worker_contextfrom agent.turn_failure_copy import exit_reason_failure, stamp_failurefrom agent.context_compressor import _DB_PERSISTED_MARKERfrom agent.message_content import flatten_message_text@@ -155,8 +156,14 @@ def _resolve_budget_fallback(final_response = agent._handle_max_iterations(messages, api_call_count)# A kanban worker must record a terminal outcome whether or not a fallback path- # was eligible, so the dispatcher learns the worker could not complete.- _kanban_task = os.environ.get("HERMES_KANBAN_TASK") if budget_exhausted else None+ # was eligible, so the dispatcher learns the worker could not complete. Only the+ # dispatcher-owned worker owns the task: an in-process delegate_task child or cron run+ # inherits ``HERMES_KANBAN_TASK`` via os.environ but exhausting ITS budget must not+ # close the parent's run and release its claim (#112817).+ _kanban_task = (+ os.environ.get("HERMES_KANBAN_TASK")+ if budget_exhausted and is_dispatcher_owned_worker_context() else None+ )# If running as a kanban worker, signal the dispatcher that the worker could not complete (rather than# treating it as a protocol violation). This applies whether the user-facing fallback came from the# summary call or an explicitly pending continuation; both exhausted the task budget and must advancediff --git a/tests/agent/test_title_generator.py b/tests/agent/test_title_generator.pyindex 468d8793257..e32fbf59fd3 100644--- a/tests/agent/test_title_generator.py+++ b/tests/agent/test_title_generator.py@@ -406,6 +406,21 @@ class TestMaybeAutoTitle:assert db.get_session_title("sess-1") == "Kanban task t_missing"mock_auto.assert_not_called()+ def test_delegated_child_of_a_worker_is_not_named_after_the_card(self, tmp_path, monkeypatch):+ """A delegate_task child inherits ``HERMES_KANBAN_TASK`` but is not the card's session;+ it takes the ordinary title path instead of the parent's card title (#112817)."""+ from agent.delegation_context import delegated_child_context++ monkeypatch.setenv("HERMES_KANBAN_TASK", "t_parent")+ db = SessionDB(tmp_path / "state.db")+ db.create_session(session_id="child-1", source="kanban")++ with delegated_child_context(), patch("agent.title_generator.auto_title_session") as mock_auto:+ maybe_auto_title(db, "child-1", "research the auth flow for the parent", [])++ assert db.get_session_title("child-1") != "Kanban task t_parent"+ mock_auto.assert_called_once()+def test_writes_instant_title_before_the_model_runs(self, tmp_path):"""The derived title lands synchronously — no LLM, no waiting."""db = SessionDB(tmp_path / "state.db")diff --git a/tests/agent/test_turn_finalizer_iteration_limit_exit.py b/tests/agent/test_turn_finalizer_iteration_limit_exit.pyindex 8594afe63ef..caa76e7eb93 100644--- a/tests/agent/test_turn_finalizer_iteration_limit_exit.py+++ b/tests/agent/test_turn_finalizer_iteration_limit_exit.py@@ -373,3 +373,37 @@ def test_bounded_fallback_does_not_fire_when_budget_not_exhausted(monkeypatch):record.assert_not_called()[email protected]("scope", ["child", "non-owner"])+def test_budget_exhausted_child_does_not_record_parent_kanban_timeout(monkeypatch, scope):+ """An in-process delegate_task child (or cron run) inherits ``HERMES_KANBAN_TASK`` from+ the dispatcher worker; exhausting ITS budget must not record ``timed_out`` against the+ parent's task or release the parent's claim (#112817)."""+ from agent.delegation_context import delegated_child_context, non_dispatcher_owned_context++ monkeypatch.setattr("hermes_cli.plugins.invoke_hook", lambda *_a, **_kw: [])+ monkeypatch.setenv("HERMES_KANBAN_TASK", "t_parent")+ record = MagicMock(name="record_task_failure")+ conn = SimpleNamespace(close=lambda: None)+ monkeypatch.setattr("hermes_cli.kanban_db_connect.connect", lambda: conn)+ monkeypatch.setattr("hermes_cli.kanban_db_dispatch._record_task_failure", record)+ agent = _LimitAgent()++ ctx = delegated_child_context if scope == "child" else non_dispatcher_owned_context+ with ctx():+ finalize_turn(+ agent,+ final_response=None,+ api_call_count=60,+ interrupted=True,+ failed=False,+ messages=[{"role": "user", "content": "task"}],+ conversation_history=[],+ effective_task_id="task",+ turn_id="turn",+ user_message="task",+ original_user_message="task",+ _should_review_memory=False,+ _turn_exit_reason="interrupted_by_user",+ )++ record.assert_not_called()diff --git a/tests/tools/test_kanban_comment_injection.py b/tests/tools/test_kanban_comment_injection.pyindex 63f0b5a8869..98938c7e7ff 100644--- a/tests/tools/test_kanban_comment_injection.py+++ b/tests/tools/test_kanban_comment_injection.py@@ -123,3 +123,38 @@ def test_skips_own_authored_comments(worker_home, monkeypatch):_unthrottle()assert kt.inject_new_comments_from_env(agent) is Falseassert agent.steers == []+++def test_delegated_child_in_worker_process_neither_receives_nor_consumes_notes(worker_home, monkeypatch):+ """A delegate_task child inherits the worker's ``HERMES_KANBAN_TASK``; operator notes+ address the worker, so the child must not be steered by them and must not advance the+ shared watermark (which would make the worker miss them) (#112817)."""+ from agent.delegation_context import delegated_child_context++ conn = kbc.connect()+ try:+ tid = kb.create_task(conn, title="live task")+ finally:+ conn.close()+ monkeypatch.setenv("HERMES_KANBAN_TASK", tid)+ monkeypatch.setenv("HERMES_PROFILE", "worker-bot")++ worker = FakeAgent()+ _unthrottle()+ assert kt.inject_new_comments_from_env(worker) is False # seed++ conn = kbc.connect()+ try:+ kb.add_comment(conn, tid, author="desktop", body="operator note for the worker")+ finally:+ conn.close()++ child = FakeAgent()+ with delegated_child_context():+ _unthrottle()+ assert kt.inject_new_comments_from_env(child) is False+ assert child.steers == []++ _unthrottle()+ assert kt.inject_new_comments_from_env(worker) is True+ assert "operator note" in worker.steers[0]diff --git a/tools/kanban_tools.py b/tools/kanban_tools.pyindex 298e467dc0a..e847f0359c9 100644--- a/tools/kanban_tools.py+++ b/tools/kanban_tools.py@@ -475,7 +475,9 @@ def inject_new_comments_from_env(agent: Any) -> bool:"""Steer new operator comments on the worker's task into ``agent``; True iff asteer was injected; never raises. Own comments (``HERMES_PROFILE``) are skipped."""global _comment_poll_last_attempt- tid = os.environ.get("HERMES_KANBAN_TASK")+ # Operator notes address the dispatcher-owned worker; a delegate_task child sharing+ # this process must neither receive them nor advance the shared watermark (#112817).+ tid = os.environ.get("HERMES_KANBAN_TASK") if _is_dispatcher_owned_worker() else Nonenow = time.monotonic()if (not tid or agent is None or not hasattr(agent, "steer")or (now - _comment_poll_last_attempt) < _COMMENT_POLL_MIN_INTERVAL_SECONDS):