The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(kanban): bind relay-affinity scope for headless goal-judge calls
Yagna Vudathu53809f8aeace
diff --git a/hermes_cli/kanban.py b/hermes_cli/kanban.pyindex c8f029ca0ed..f738d9d66b2 100644--- a/hermes_cli/kanban.py+++ b/hermes_cli/kanban.py@@ -841,8 +841,19 @@ def _goal_mode_handoff_rejection(task: Optional[kb.Task], evidence: str):verdict, reason = "done", ""try:- verdict, reason, _, _, _ = judge_goal(goal=f"{task.title}\n\n{task.body or ''}".strip(),- last_response=evidence.strip())+ # Headless handoff checks run outside any agent turn: bind the per-task relay-affinity+ # scope (mirrors kanban_specify) so the relay does not reject the judge call (#113669).+ from agent.portal_tags import get_affinity_scope, reset_affinity_scope, set_affinity_scope+ affinity_token = None if get_affinity_scope() else set_affinity_scope(f"kanban:{task.id}")+ try:+ # Fail-open on transport failure belongs to the shared gate fix (#73119);+ # this gate only ensures the headless call carries a scope.+ verdict, reason, _, _, _ = judge_goal(+ goal=f"{task.title}\n\n{task.body or ''}".strip(),+ last_response=evidence.strip())+ finally:+ if affinity_token is not None:+ reset_affinity_scope(affinity_token)except Exception as judge_exc:import logging as _loggingdiff --git a/tests/hermes_cli/test_kanban_goal_judge_affinity.py b/tests/hermes_cli/test_kanban_goal_judge_affinity.pynew file mode 100644index 00000000000..2a2e65f30d7--- /dev/null+++ b/tests/hermes_cli/test_kanban_goal_judge_affinity.py@@ -0,0 +1,78 @@+"""Goal-judge handoff gates bind the per-task relay-affinity scope.++Both terminal-handoff gates (``hermes_cli.kanban`` for the CLI dispatcher,+``tools.kanban_tools`` for worker tool calls) run outside any agent turn, so+without a bound scope the relay rejects the judge call (400 MissingSessionID)+(#113669). Fail-open on transport failure belongs to the shared gate fix+(#73119); these tests pin only the scope binding plus unchanged genuine+verdicts.+"""++from __future__ import annotations++import sys+from pathlib import Path+from types import SimpleNamespace+from unittest.mock import patch++import pytest++sys.path.insert(0, str(Path(__file__).parent.parent.parent))++from agent.portal_tags import get_affinity_scope, set_affinity_scope+from hermes_cli import kanban as kanban_cli+from tools import kanban_tools+++def _task(tid="task-1"):+ return SimpleNamespace(id=tid, title="goal title", body="goal body", goal_mode=True)+++def _aux_client():+ return patch(+ "agent.auxiliary_client.get_text_auxiliary_client",+ return_value=(object(), "test-model"),+ )+++def test_cli_gate_genuine_continue_still_rejects():+ """A real not-done verdict keeps rejecting; only transport failure fails open."""+ with _aux_client(), patch(+ "hermes_cli.goals.judge_goal",+ return_value=("continue", "goal not met yet", False, None, False),+ ):+ assert kanban_cli._goal_mode_handoff_rejection(_task(), "evidence") == (+ "continue", "goal not met yet")+++def test_cli_gate_binds_per_task_affinity_scope():+ """Headless judge call runs under kanban:<task_id>; a bound scope is kept."""+ seen = []++ def fake_judge(**kwargs):+ seen.append(get_affinity_scope())+ return ("done", "ok", False, None, False)++ with _aux_client(), patch("hermes_cli.goals.judge_goal", side_effect=fake_judge):+ assert kanban_cli._goal_mode_handoff_rejection(_task("task-9"), "ev") == ("done", None)+ assert seen == ["kanban:task-9"]+ assert get_affinity_scope() is None++ token = set_affinity_scope("outer-conversation")+ try:+ with _aux_client(), patch("hermes_cli.goals.judge_goal", side_effect=fake_judge):+ kanban_cli._goal_mode_handoff_rejection(_task("task-9"), "ev")+ finally:+ from agent.portal_tags import reset_affinity_scope+ reset_affinity_scope(token)+ assert seen[-1] == "outer-conversation"+++def test_tool_gate_genuine_blocked_still_rejects():+ """A real unachievable verdict still raises; only transport failure fails open."""+ with patch.object(kanban_tools, "_goal_judge_available", return_value=True), patch.object(+ kanban_tools, "judge_goal",+ return_value=("blocked", "unachievable", False, None, False),+ ):+ with pytest.raises(kanban_tools._Reject):+ kanban_tools._goal_gate("kanban_complete", _task(), "task-1", "ev")diff --git a/tools/kanban_tools.py b/tools/kanban_tools.pyindex 9da18c182ad..06a510820a0 100644--- a/tools/kanban_tools.py+++ b/tools/kanban_tools.py@@ -400,8 +400,18 @@ def _goal_gate(tool_name: str, task, tid: str, evidence: str) -> None:if not task or not task.goal_mode or not _goal_judge_available():returntry:- verdict, reason, _, _, _ = judge_goal(- goal=f"{task.title}\n\n{task.body or ''}".strip(), last_response=evidence.strip())+ # Headless gate runs outside any agent turn: bind the per-task relay-affinity scope+ # (mirrors kanban_specify) so the relay does not reject the judge call (#113669).+ from agent.portal_tags import get_affinity_scope, reset_affinity_scope, set_affinity_scope+ affinity_token = None if get_affinity_scope() else set_affinity_scope(f"kanban:{tid}")+ try:+ # Fail-open on transport failure belongs to the shared gate fix (#73119);+ # this gate only ensures the headless call carries a scope.+ verdict, reason, _, _, _ = judge_goal(+ goal=f"{task.title}\n\n{task.body or ''}".strip(), last_response=evidence.strip())+ finally:+ if affinity_token is not None:+ reset_affinity_scope(affinity_token)except Exception as judge_exc:logger.warning("goal judge check failed, allowing lifecycle handoff: %s", judge_exc, exc_info=True)