Skip to content

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

feat(hitl): add HitlBinding attribute to AgentTool and Middleware

xf gong84ddd684f9e0
diff --git a/cubepi/agent/types.py b/cubepi/agent/types.py
index f852d07..be33b6b 100644
--- a/cubepi/agent/types.py
+++ b/cubepi/agent/types.py
@@ -6,6 +6,7 @@ from typing import Awaitable, Callable, Generic, Literal, TypeVar
from pydantic import BaseModel
+from cubepi.hitl.binding import HitlBinding
from cubepi.hitl.types import HitlRequest
from cubepi.providers.base import (
AssistantMessage,
@@ -38,6 +39,7 @@ class AgentTool(Generic[TParams]):
label: str = ""
execution_mode: Literal["sequential", "parallel"] | None = None
hitl_builtin: bool = False
+ hitl: HitlBinding | None = None
def to_definition(self) -> ToolDefinition:
schema = self.parameters.model_json_schema()
diff --git a/cubepi/hitl/binding.py b/cubepi/hitl/binding.py
new file mode 100644
index 0000000..31f183e
--- /dev/null
+++ b/cubepi/hitl/binding.py
@@ -0,0 +1,26 @@
+"""HitlBinding — structural attribute on AgentTool and Middleware
+declaring how a HITL element integrates with the checkpointer.
+
+See dev/specs/2026-06-05-conversation-fork.md §3.6.3.1.
+"""
+
+from __future__ import annotations
+
+from dataclasses import dataclass
+
+
+@dataclass(frozen=True)
+class HitlBinding:
+ """How a tool/middleware integrates with HITL.
+
+ Attributes:
+ checkpointed: True iff backed by ``CheckpointedChannel`` (writes
+ ``pending_request`` to the source thread on pause).
+ run_id: The channel's bound run_id. For checkpointed HITL this
+ MUST be a non-empty string; ``None`` is a configuration
+ error and is rejected at ``Agent.prompt()`` entry. For
+ in-memory HITL it is ``None``.
+ """
+
+ checkpointed: bool
+ run_id: str | None
diff --git a/cubepi/middleware/base.py b/cubepi/middleware/base.py
index 24f8f3b..7cd6888 100644
--- a/cubepi/middleware/base.py
+++ b/cubepi/middleware/base.py
@@ -12,6 +12,7 @@ from cubepi.agent.types import (
BeforeToolCallContext,
BeforeToolCallResult,
)
+from cubepi.hitl.binding import HitlBinding
from cubepi.providers.base import AssistantMessage, Message, Model, Provider
from cubepi.types import JsonObject, StructuredObject
@@ -31,6 +32,8 @@ class TurnAction:
class Middleware:
+ hitl: HitlBinding | None = None
+
async def transform_context(
self,
messages: list[Message],
diff --git a/tests/agent/test_hitl_binding.py b/tests/agent/test_hitl_binding.py
new file mode 100644
index 0000000..33a9786
--- /dev/null
+++ b/tests/agent/test_hitl_binding.py
@@ -0,0 +1,63 @@
+from cubepi.agent.types import AgentTool
+from cubepi.hitl.binding import HitlBinding
+from cubepi.middleware.base import Middleware
+from pydantic import BaseModel
+
+
+class _NoArgs(BaseModel):
+ pass
+
+
+async def _noop(tool_call_id, args, *, signal=None, on_update=None):
+ raise NotImplementedError
+
+
+def test_agent_tool_hitl_default_none():
+ t = AgentTool(
+ name="t",
+ description="d",
+ parameters=_NoArgs,
+ execute=_noop,
+ )
+ assert t.hitl is None
+
+
+def test_agent_tool_hitl_can_be_set():
+ binding = HitlBinding(checkpointed=True, run_id="r-1")
+ t = AgentTool(
+ name="t",
+ description="d",
+ parameters=_NoArgs,
+ execute=_noop,
+ hitl=binding,
+ )
+ assert t.hitl is binding
+ assert t.hitl.checkpointed is True
+ assert t.hitl.run_id == "r-1"
+
+
+def test_middleware_hitl_default_none():
+ class _Mw(Middleware):
+ pass
+
+ assert _Mw().hitl is None
+
+
+def test_middleware_hitl_can_be_set_in_subclass():
+ class _Mw(Middleware):
+ def __init__(self) -> None:
+ self.hitl = HitlBinding(checkpointed=False, run_id=None)
+
+ mw = _Mw()
+ assert mw.hitl is not None
+ assert mw.hitl.checkpointed is False
+ assert mw.hitl.run_id is None
+
+
+def test_hitl_binding_is_frozen():
+ b = HitlBinding(checkpointed=True, run_id="r-1")
+ try:
+ b.checkpointed = False # type: ignore[misc]
+ except Exception:
+ return
+ assert False, "HitlBinding should be frozen"