CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.
Python agent-frameworkagent-harnessagent-runtimeagentic-aiai-agentsai-frameworkanthropicharnessharness-engineeringharness-frameworklanggraphlanggraph-alternativellmmcpopenaipi-agentpi-agent-corepythonpython-agent
feat(agent): enforce HITL channel run_id binding at prompt() entry
xf gonga7fc7e30b637
diff --git a/cubepi/agent/agent.py b/cubepi/agent/agent.pyindex 2ae0059..a3bbb9b 100644--- a/cubepi/agent/agent.py+++ b/cubepi/agent/agent.py@@ -311,6 +311,30 @@ class Agent(Generic[TMessage]):"Agent is already processing a prompt. ""Use steer() or follow_up() to queue messages.")+ bound: set[str] = set()+ for elem in (*self._state.tools, *self._middleware):+ binding = getattr(elem, "hitl", None)+ if binding is None or not binding.checkpointed:+ continue+ if binding.run_id is None:+ raise ValueError(+ f"Checkpointed HITL element {elem!r} has no run_id bound; "+ "construct CheckpointedChannel(run_id=...) before passing "+ "it to ask_user_tool/HITL middleware"+ )+ bound.add(binding.run_id)+ if bound:+ if run_id is None:+ raise ValueError(+ f"Agent has checkpointed HITL elements bound to "+ f"run_ids {sorted(bound)!r}; prompt(run_id=...) "+ "must be explicitly supplied (generate-mode rejected)"+ )+ if any(b != run_id for b in bound):+ raise ValueError(+ f"prompt(run_id={run_id!r}) does not match "+ f"HITL-bound run_ids {sorted(bound)!r}"+ )effective_run_id = run_id or uuid.uuid4().hex# Reject mismatched caller-supplied Message.run_id BEFORE any state# mutation so the supplied run_id remains reusable (Task 25 will adddiff --git a/tests/agent/test_hitl_binding_enforcement.py b/tests/agent/test_hitl_binding_enforcement.pynew file mode 100644index 0000000..d58ca28--- /dev/null+++ b/tests/agent/test_hitl_binding_enforcement.py@@ -0,0 +1,82 @@+import pytest++from cubepi.agent.agent import Agent+from cubepi.agent.types import AgentTool+from cubepi.hitl.binding import HitlBinding+from cubepi.providers.base import AssistantMessage, TextContent+from cubepi.providers.faux import FauxProvider+from pydantic import BaseModel+++def _ok_faux() -> FauxProvider:+ p = FauxProvider()+ p.set_responses(+ [AssistantMessage(content=[TextContent(text="ok")], stop_reason="end_turn")]+ )+ return p+++class _NoArgs(BaseModel):+ pass+++async def _noop(tool_call_id, args, *, signal=None, on_update=None):+ raise NotImplementedError+++def _tool_with_hitl(binding):+ return AgentTool(+ name="ask_user",+ description="d",+ parameters=_NoArgs,+ execute=_noop,+ hitl=binding,+ )+++def _agent(tools=None, middleware=None):+ return Agent(+ model=_ok_faux().model("faux-model"),+ tools=tools or [],+ middleware=middleware or [],+ )+++async def test_checkpointed_hitl_with_none_run_id_raises():+ tool = _tool_with_hitl(HitlBinding(checkpointed=True, run_id=None))+ a = _agent(tools=[tool])+ with pytest.raises(ValueError, match="no run_id bound"):+ await a.prompt("hi", run_id="R1")+++async def test_checkpointed_hitl_requires_explicit_run_id():+ tool = _tool_with_hitl(HitlBinding(checkpointed=True, run_id="R1"))+ a = _agent(tools=[tool])+ with pytest.raises(ValueError, match="generate-mode rejected"):+ await a.prompt("hi", run_id=None)+++async def test_checkpointed_hitl_run_id_mismatch_raises():+ tool = _tool_with_hitl(HitlBinding(checkpointed=True, run_id="R1"))+ a = _agent(tools=[tool])+ with pytest.raises(ValueError, match="does not match"):+ await a.prompt("hi", run_id="R2")+++async def test_checkpointed_hitl_run_id_match_succeeds():+ tool = _tool_with_hitl(HitlBinding(checkpointed=True, run_id="R1"))+ a = _agent(tools=[tool])+ got = await a.prompt("hi", run_id="R1")+ assert got == "R1"+++async def test_in_memory_hitl_no_constraint():+ tool = _tool_with_hitl(HitlBinding(checkpointed=False, run_id=None))+ a = _agent(tools=[tool])+ got = await a.prompt("hi") # generate-mode allowed+ assert isinstance(got, str)