Skip to content

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

docs(hitl): user-facing guide + sandbox-confirm + ask-user-form recipes + arch tree

xf gong4b6d1f679e78
diff --git a/README.md b/README.md
index fa4adde..ef247c1 100644
--- a/README.md
+++ b/README.md
@@ -111,6 +111,7 @@ cubepi/
│ ├── memory.py # In-memory (dev/test)
│ ├── sqlite.py # SQLite (lightweight persistence)
│ └── postgres/ # Postgres (production persistence)
+├── hitl/ # Human-in-the-Loop channel, middlewares, ask_user tool
├── mcp/ # MCP tool loaders (HTTP + stdio transports)
└── tracing/ # OpenTelemetry tracing + metrics (optional extra)
├── tracer.py # Tracer entry point — TracerProvider + recorder wiring
diff --git a/website/docs/guides/hitl.md b/website/docs/guides/hitl.md
new file mode 100644
index 0000000..193ebfb
--- /dev/null
+++ b/website/docs/guides/hitl.md
@@ -0,0 +1,82 @@
+---
+title: Human-in-the-Loop (HITL)
+sidebar_position: 10
+---
+
+# Human-in-the-Loop (HITL)
+
+cubepi ships a HITL channel for two recurring scenarios:
+
+1. **Sandbox tool confirmation** — a dangerous tool needs human approve / deny / edit before running.
+2. **Mid-run structured questions** — the agent needs a specific selection or form before proceeding.
+
+The channel is one primitive with two implementations:
+- `InMemoryChannel` — for CLI, notebook, tests.
+- `CheckpointedChannel` — for web services where the agent process may die between question and answer; pairs with any `Checkpointer`.
+
+## Quick start (in-process)
+
+```python
+import asyncio
+from cubepi.agent.agent import Agent
+from cubepi.hitl import (
+ ApproveAnswer, ConfirmToolCallMiddleware, InMemoryChannel, ask_user_tool,
+)
+
+channel = InMemoryChannel()
+
+agent = Agent(
+ provider=..., model=...,
+ tools=[bash_tool, ask_user_tool(channel)],
+ middleware=[ConfirmToolCallMiddleware(channel, require_confirm={"bash"})],
+ channel=channel,
+)
+
+# Host coroutine renders pending requests and posts answers.
+# For approve-kind requests, the answer is an ApproveAnswer; for ask-kind it's
+# a dict[question.key, str | list[str]]; for confirm-kind it's a bool.
+async def host():
+ async for req in channel.subscribe():
+ if req.payload.kind == "approve":
+ user_decision = await my_ui.show_approve(req) # returns ApproveAnswer
+ await channel.answer(req.question_id, user_decision)
+ elif req.payload.kind == "ask":
+ answers = await my_ui.show_form(req.payload.questions)
+ await channel.answer(req.question_id, answers)
+ else: # confirm
+ await channel.answer(req.question_id, await my_ui.show_confirm(req))
+
+# Run agent in parallel with host, then exit once the agent finishes.
+async def main():
+ host_task = asyncio.create_task(host())
+ try:
+ await agent.prompt("…")
+ finally:
+ host_task.cancel()
+
+asyncio.run(main())
+```
+
+## Cross-process (web service) flow
+
+1. HTTP POST /chat starts `agent.prompt(...)`. Inside, `channel.approve` / `channel.ask` persists `pending_request` to the checkpointer and emits `HitlRequestEvent` on the SSE stream.
+2. Frontend renders the pending; user clicks approve/deny/edit.
+3. HTTP POST /respond calls `await agent.respond(question_id=..., answer=...)` which loads checkpoint, attaches the answer to the channel, and re-enters the loop. The previously-gated tool runs (or synthetic deny) and the conversation continues. Pending is cleared only after the tool_result is checkpointed.
+
+If the user closes the tab without answering, the host calls `await agent.abort_pending(reason="user closed")` which closes the conversation with a synthetic deny + terminal `stop_reason="aborted"` assistant.
+
+## When to use `ask_user` vs end of turn
+
+| Goal | Use |
+|------|-----|
+| Free-text follow-up question to user | Just end the turn with the question as text; user's next message is the answer. |
+| Structured selection (one of N) | `ask_user` tool with `options` and (optionally) `multi_select` |
+| Confirm/edit a tool's args before run | `ConfirmToolCallMiddleware` or `ApprovalPolicyMiddleware` |
+
+## Durable scope
+
+Durable cross-process resume is supported at two safe suspension points:
+1. `before_tool_call` approval gate (via `ApprovalPolicyMiddleware` / `ConfirmToolCallMiddleware`)
+2. The `ask_user` tool body
+
+Custom tools that mix HITL with other side effects are **same-process only** unless they pass `allow_inside_custom_tool=True` to `CheckpointedChannel` and accept the idempotency contract.
diff --git a/website/docs/recipes/ask-user-form.md b/website/docs/recipes/ask-user-form.md
new file mode 100644
index 0000000..a6f55ad
--- /dev/null
+++ b/website/docs/recipes/ask-user-form.md
@@ -0,0 +1,39 @@
+---
+title: Multi-question Form via ask_user
+---
+
+# Recipe: Multi-question Form via `ask_user`
+
+```python
+from cubepi.hitl import ask_user_tool, InMemoryChannel
+
+channel = InMemoryChannel()
+agent = Agent(
+ provider=..., model=...,
+ tools=[ask_user_tool(channel)],
+ channel=channel,
+)
+```
+
+The model invokes `ask_user` like any other tool. Example parameters the model can pass:
+
+```json
+{
+ "questions": [
+ {"key": "framework", "prompt": "Which framework?",
+ "options": [
+ {"label": "React", "value": "react"},
+ {"label": "Vue", "value": "vue"},
+ {"label": "Other", "value": "other", "allow_input": true}
+ ]},
+ {"key": "features", "prompt": "Which features?",
+ "multi_select": true,
+ "options": [
+ {"label": "Auth", "value": "auth"},
+ {"label": "Payments", "value": "payments"}
+ ]}
+ ]
+}
+```
+
+Answer shape: `{"framework": "react", "features": ["auth", "payments"]}` — or for `Other` with `allow_input`, the value is the free-text string the user typed.
diff --git a/website/docs/recipes/sandbox-confirm.md b/website/docs/recipes/sandbox-confirm.md
new file mode 100644
index 0000000..fa8312f
--- /dev/null
+++ b/website/docs/recipes/sandbox-confirm.md
@@ -0,0 +1,34 @@
+---
+title: Sandbox Confirm with ApprovalPolicyMiddleware
+---
+
+# Recipe: Sandbox Confirm with `ApprovalPolicyMiddleware`
+
+Use case: cubebox-style web service where every bash command has a rule engine that classifies it as auto-allow, hard-deny, or human-confirm.
+
+```python
+from cubepi.hitl import (
+ Approve, ApprovalPolicyMiddleware, AskUser, CheckpointedChannel, Deny,
+)
+
+def policy(ctx):
+ cmd = ctx.args.get("cmd", "") if isinstance(ctx.args, dict) else ctx.args.cmd
+ rule = command_rule_engine.classify(cmd)
+ if rule.tier == "allow": return Approve()
+ if rule.tier == "block": return Deny(reason=rule.reason)
+ return AskUser(
+ timeout_seconds=180,
+ details={"rule": rule.matched_pattern, "impact": rule.impact},
+ )
+
+channel = CheckpointedChannel(checkpointer=cp, thread_id=thread_id)
+agent = Agent(
+ provider=..., model=..., tools=[bash_tool],
+ middleware=[ApprovalPolicyMiddleware(channel, policy)],
+ channel=channel, checkpointer=cp, thread_id=thread_id,
+)
+```
+
+`HitlRequest.timeout_seconds` is embedded in the emitted event so the frontend can render a countdown.
+
+On timeout: middleware translates to `BeforeToolCallResult(block=True, deny_reason="approval_timeout")`. The model sees `tool_result.is_error=True` with `details.hitl.decision == "timed_out"` and naturally produces a follow-up turn explaining the timeout.