Skip to content

CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.

feat(hitl): ApprovalPolicyMiddleware populates self.hitl

xf gong367a6a457458
diff --git a/cubepi/hitl/middleware.py b/cubepi/hitl/middleware.py
index aba7463..b2487d9 100644
--- a/cubepi/hitl/middleware.py
+++ b/cubepi/hitl/middleware.py
@@ -6,7 +6,8 @@ from typing import Awaitable, Callable, Iterable, Protocol, Union, cast
from pydantic import BaseModel
from cubepi.agent.types import BeforeToolCallContext, BeforeToolCallResult
-from cubepi.hitl.channel import HitlChannel
+from cubepi.hitl.binding import HitlBinding
+from cubepi.hitl.channel import CheckpointedChannel, HitlChannel
from cubepi.hitl.exceptions import HitlCancelled, HitlTimedOut
from cubepi.hitl.policy import Approve, ApprovalDecision, AskUser, Deny
from cubepi.middleware.base import Middleware
@@ -36,6 +37,11 @@ class ApprovalPolicyMiddleware(Middleware):
):
self._channel = channel
self._policy = policy
+ checkpointed = isinstance(channel, CheckpointedChannel)
+ self.hitl = HitlBinding(
+ checkpointed=checkpointed,
+ run_id=getattr(channel, "_run_id", None) if checkpointed else None,
+ )
async def before_tool_call(
self, ctx: BeforeToolCallContext, *, signal=None
diff --git a/tests/agent/test_agent_completion.py b/tests/agent/test_agent_completion.py
index 7a513a0..8919555 100644
--- a/tests/agent/test_agent_completion.py
+++ b/tests/agent/test_agent_completion.py
@@ -115,7 +115,7 @@ def _two_turn_bash_responses():
@pytest.mark.asyncio
async def test_hitl_detached_outcome_suspended_no_mark():
cp = MemoryCheckpointer()
- ch = CheckpointedChannel(checkpointer=cp, thread_id="t")
+ ch = CheckpointedChannel(checkpointer=cp, thread_id="t", run_id="R1")
provider = FauxProvider(provider_id="faux")
provider.set_responses(_two_turn_bash_responses())
a = Agent(
@@ -155,7 +155,7 @@ async def test_hitl_detached_outcome_suspended_no_mark():
@pytest.mark.asyncio
async def test_hitl_aborted_via_abort_pending_does_not_mark():
cp = MemoryCheckpointer()
- ch = CheckpointedChannel(checkpointer=cp, thread_id="t")
+ ch = CheckpointedChannel(checkpointer=cp, thread_id="t", run_id="R1")
provider = FauxProvider(provider_id="faux")
provider.set_responses(_two_turn_bash_responses())
a = Agent(
@@ -194,7 +194,7 @@ async def test_hitl_aborted_via_abort_pending_does_not_mark():
@pytest.mark.asyncio
async def test_propagating_cancel_does_not_mark():
cp = MemoryCheckpointer()
- ch = CheckpointedChannel(checkpointer=cp, thread_id="t")
+ ch = CheckpointedChannel(checkpointer=cp, thread_id="t", run_id="R1")
provider = FauxProvider(provider_id="faux")
provider.set_responses(_two_turn_bash_responses())
a = Agent(
diff --git a/tests/hitl/test_agent_abort_pending.py b/tests/hitl/test_agent_abort_pending.py
index 3a02b66..de5bc64 100644
--- a/tests/hitl/test_agent_abort_pending.py
+++ b/tests/hitl/test_agent_abort_pending.py
@@ -39,7 +39,7 @@ def _bash_tool() -> AgentTool:
async def test_abort_pending_closes_conversation():
cp = MemoryCheckpointer()
- ch = CheckpointedChannel(checkpointer=cp, thread_id="t-1")
+ ch = CheckpointedChannel(checkpointer=cp, thread_id="t-1", run_id="R1")
provider = FauxProvider(provider_id="faux")
provider.set_responses(
[
@@ -57,7 +57,7 @@ async def test_abort_pending_closes_conversation():
checkpointer=cp,
thread_id="t-1",
)
- task = asyncio.create_task(agent.prompt("hi"))
+ task = asyncio.create_task(agent.prompt("hi", run_id="R1"))
for _ in range(200):
if ch.pending is not None:
break
diff --git a/tests/hitl/test_agent_respond.py b/tests/hitl/test_agent_respond.py
index eb0d94a..ff2d2b3 100644
--- a/tests/hitl/test_agent_respond.py
+++ b/tests/hitl/test_agent_respond.py
@@ -61,7 +61,7 @@ def _faux_with(responses) -> FauxProvider:
async def test_respond_completes_a_suspended_run():
cp = MemoryCheckpointer()
- ch = CheckpointedChannel(checkpointer=cp, thread_id="t-1")
+ ch = CheckpointedChannel(checkpointer=cp, thread_id="t-1", run_id="R1")
provider = FauxProvider(provider_id="faux")
provider.set_responses(_two_turn_bash_responses())
agent = Agent(
@@ -77,7 +77,7 @@ async def test_respond_completes_a_suspended_run():
# Start the agent — it will suspend on channel.approve.
async def run():
- await agent.prompt("hi")
+ await agent.prompt("hi", run_id="R1")
task = asyncio.create_task(run())
diff --git a/tests/hitl/test_approval_policy_binding.py b/tests/hitl/test_approval_policy_binding.py
new file mode 100644
index 0000000..c7917f1
--- /dev/null
+++ b/tests/hitl/test_approval_policy_binding.py
@@ -0,0 +1,36 @@
+from cubepi.checkpointer.memory import MemoryCheckpointer
+from cubepi.hitl.channel import CheckpointedChannel, InMemoryChannel
+from cubepi.hitl.middleware import ApprovalPolicyMiddleware, ConfirmToolCallMiddleware
+from cubepi.hitl.policy import Approve
+
+
+def _approve_policy(ctx):
+ return Approve()
+
+
+def test_approval_policy_with_checkpointed_channel_sets_binding():
+ cp = MemoryCheckpointer()
+ ch = CheckpointedChannel(checkpointer=cp, thread_id="t", run_id="R1")
+ mw = ApprovalPolicyMiddleware(channel=ch, policy=_approve_policy)
+ assert mw.hitl is not None
+ assert mw.hitl.checkpointed is True
+ assert mw.hitl.run_id == "R1"
+
+
+def test_approval_policy_with_in_memory_channel_sets_binding():
+ ch = InMemoryChannel(thread_id="t")
+ mw = ApprovalPolicyMiddleware(channel=ch, policy=_approve_policy)
+ assert mw.hitl is not None
+ assert mw.hitl.checkpointed is False
+ assert mw.hitl.run_id is None
+
+
+def test_confirm_tool_call_middleware_inherits_binding():
+ """ConfirmToolCallMiddleware is a subclass of ApprovalPolicyMiddleware.
+ It should inherit the same binding behavior."""
+ cp = MemoryCheckpointer()
+ ch = CheckpointedChannel(checkpointer=cp, thread_id="t", run_id="R1")
+ mw = ConfirmToolCallMiddleware(channel=ch)
+ assert mw.hitl is not None
+ assert mw.hitl.checkpointed is True
+ assert mw.hitl.run_id == "R1"
diff --git a/tests/hitl/test_resume_cache_prefix.py b/tests/hitl/test_resume_cache_prefix.py
index 00b8cab..cde8e1a 100644
--- a/tests/hitl/test_resume_cache_prefix.py
+++ b/tests/hitl/test_resume_cache_prefix.py
@@ -49,7 +49,7 @@ def _two_turn_bash_responses():
async def _suspend_resume_and_capture(checkpointer):
- ch = CheckpointedChannel(checkpointer=checkpointer, thread_id="t-1")
+ ch = CheckpointedChannel(checkpointer=checkpointer, thread_id="t-1", run_id="R1")
provider = FauxProvider(provider_id="faux")
provider.set_responses(_two_turn_bash_responses())
@@ -64,7 +64,7 @@ async def _suspend_resume_and_capture(checkpointer):
checkpointer=checkpointer,
thread_id="t-1",
)
- task = asyncio.create_task(agent.prompt("hi"))
+ task = asyncio.create_task(agent.prompt("hi", run_id="R1"))
for _ in range(200):
if ch.pending is not None:
break