Skip to content

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

feat(hitl): ScriptedChannel + NoopChannel + subagent inheritance test

xf gong82d8c6e3d666
diff --git a/cubepi/hitl/__init__.py b/cubepi/hitl/__init__.py
index fbe5c01..e8632a3 100644
--- a/cubepi/hitl/__init__.py
+++ b/cubepi/hitl/__init__.py
@@ -25,6 +25,7 @@ from cubepi.hitl.policy import (
)
from cubepi.hitl.ask_user import AskUserParams, ask_user_tool
from cubepi.hitl.channel import CheckpointedChannel, HitlChannel, InMemoryChannel
+from cubepi.hitl.testing import NoopChannel, ScriptedChannel
from cubepi.hitl.middleware import ApprovalPolicyMiddleware, ConfirmToolCallMiddleware
from cubepi.hitl.types import (
ApproveAnswer,
@@ -75,4 +76,7 @@ __all__ = [
# middleware
"ApprovalPolicyMiddleware",
"ConfirmToolCallMiddleware",
+ # testing
+ "NoopChannel",
+ "ScriptedChannel",
]
diff --git a/cubepi/hitl/testing.py b/cubepi/hitl/testing.py
new file mode 100644
index 0000000..23bc294
--- /dev/null
+++ b/cubepi/hitl/testing.py
@@ -0,0 +1,60 @@
+from __future__ import annotations
+
+import time
+from typing import Any, Callable, Union
+
+from cubepi.hitl.channel import _BaseChannel
+from cubepi.hitl.exceptions import HitlError
+from cubepi.hitl.types import HitlRequest
+
+
+class ScriptedChannel(_BaseChannel):
+ """Pre-programmed answers for deterministic tests.
+
+ answers: list of values or callables. Each call to ask/confirm/approve
+ consumes the next item. A callable receives the HitlRequest and returns
+ the answer.
+ """
+
+ def __init__(self, answers: list[Union[Any, Callable[[HitlRequest], Any]]]):
+ super().__init__()
+ self._answers = list(answers)
+ self._history: list[HitlRequest] = []
+
+ @property
+ def history(self) -> list[HitlRequest]:
+ return list(self._history)
+
+ async def _await_answer(self, payload, timeout, signal, question_id):
+ if self._resume_slot is not None and self._resume_slot[0] == question_id:
+ _, ans = self._resume_slot
+ self._resume_slot = None
+ return ans
+ if not self._answers:
+ raise HitlError(f"ScriptedChannel exhausted (received {payload!r})")
+ req = HitlRequest(
+ question_id=question_id,
+ thread_id=None,
+ payload=payload,
+ created_at=time.time(),
+ timeout_seconds=timeout,
+ )
+ self._history.append(req)
+ head = self._answers.pop(0)
+ return head(req) if callable(head) else head
+
+
+class NoopChannel(_BaseChannel):
+ """Auto-approves everything. Useful for subagents in tests."""
+
+ async def _await_answer(self, payload, timeout, signal, question_id):
+ from cubepi.hitl.types import ApproveAnswer
+
+ kind = payload.kind
+ if kind == "approve":
+ return ApproveAnswer(decision="approve")
+ if kind == "confirm":
+ return True
+ if kind == "ask":
+ return {q.key: "" for q in payload.questions}
+ raise HitlError(f"NoopChannel does not handle {kind!r}")
diff --git a/tests/hitl/test_subagent_channel_inheritance.py b/tests/hitl/test_subagent_channel_inheritance.py
new file mode 100644
index 0000000..616e26d
--- /dev/null
+++ b/tests/hitl/test_subagent_channel_inheritance.py
@@ -0,0 +1,76 @@
+from pydantic import BaseModel
+
+from cubepi.agent.agent import Agent
+from cubepi.agent.types import AgentTool, AgentToolResult
+from cubepi.hitl import ApproveAnswer
+from cubepi.hitl.channel import InMemoryChannel
+from cubepi.hitl.testing import NoopChannel, ScriptedChannel
+from cubepi.hitl.types import Question
+from cubepi.providers.base import Model, TextContent
+from cubepi.providers.faux import FauxProvider, faux_assistant_message
+
+
+def _faux_with(responses):
+ """Helper: build a FauxProvider preloaded with responses (mirrors real API)."""
+ p = FauxProvider()
+ p.set_responses(responses)
+ return p
+
+
+async def test_scripted_channel_returns_canned_answers():
+ ch = ScriptedChannel(
+ answers=[
+ ApproveAnswer(decision="approve"),
+ {"color": "red"},
+ ]
+ )
+ ans1 = await ch.approve(tool_name="bash", tool_call_id="tc-1", args={})
+ assert ans1.decision == "approve"
+ ans2 = await ch.ask([Question(key="color", prompt="?")])
+ assert ans2 == {"color": "red"}
+ assert len(ch.history) == 2
+
+
+async def test_noop_channel_auto_approves():
+ ch = NoopChannel()
+ assert (
+ await ch.approve(tool_name="x", tool_call_id="y", args={})
+ ).decision == "approve"
+ assert (await ch.confirm("?")) is True
+
+
+async def test_subagent_inherits_parent_channel():
+ # Parent has channel; subagent (constructed inside a tool) gets channel=parent.channel.
+ parent_ch = InMemoryChannel()
+
+ # Subagent tool: constructs an inner Agent with parent's channel.
+ class _NoParams(BaseModel):
+ task: str
+
+ async def subagent_execute(call_id, args, *, signal=None, on_update=None):
+ # The subagent factory uses the same channel object as parent.
+ inner = Agent(
+ provider=_faux_with([faux_assistant_message("")]),
+ model=Model(id="faux", provider="faux"),
+ channel=parent_ch,
+ )
+ # Verify same channel
+ assert inner.channel is parent_ch
+ return AgentToolResult(content=[TextContent(text="subagent done")])
+
+ subagent_tool = AgentTool(
+ name="run_subagent",
+ description="run a subagent",
+ parameters=_NoParams,
+ execute=subagent_execute,
+ execution_mode="sequential",
+ )
+
+ parent = Agent(
+ provider=_faux_with([faux_assistant_message("")]),
+ model=Model(id="faux", provider="faux"),
+ tools=[subagent_tool],
+ channel=parent_ch,
+ )
+ assert parent.channel is parent_ch
+ # We don't actually run the agent here — the assertion above is what matters.