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
docs(spec): incorporate codex review pass 1 (15 findings)
xf gongbafe7044387b
diff --git a/dev/specs/2026-05-28-hitl-channel.md b/dev/specs/2026-05-28-hitl-channel.mdindex 36da853..565537d 100644--- a/dev/specs/2026-05-28-hitl-channel.md+++ b/dev/specs/2026-05-28-hitl-channel.md@@ -33,6 +33,20 @@ Concretely:- **Channel emits events** so hosts that prefer event-stream subscription (rather than synchronous coroutines) can also consume.- **Prompt-cache prefix invariant (acceptance criterion).** Between pause and resume, the `messages` list must change **only by appending tool-result message(s) and the next assistant turn at the tail.** No inserting, reordering, mutating, or rewriting prior messages — that would invalidate the provider-side prompt cache and cost a fresh prefix re-tokenization on every resume. Every code path that touches `_state._messages` during HITL pause/resume must respect this; the resume tests assert it byte-exactly (see §9.2 `test_resume_preserves_cache_prefix`).+### 2.1 Scope of durable (cross-process) HITL++cubepi's pause/resume gives durable execution **only at two well-defined suspension points** where the cubepi loop can guarantee no replay of agent-side side effects:++1. **`before_tool_call` approval gate** — when `ApprovalPolicyMiddleware` (or any user middleware) calls `channel.approve(...)` *before* the tool's `execute()` body has run, no tool side effects exist yet. The loop can suspend cleanly and resume by either running the (possibly edited) tool body or substituting a synthetic deny tool_result.+2. **`ask_user` built-in tool** — whose entire `execute()` body is `return await channel.ask(...)`. The tool body is a pure HITL wait; resume replays nothing because nothing else happened.++**Custom tools that call `await channel.{ask,confirm,approve}(...)` from inside their `execute()` body alongside other work are explicitly NOT durable across processes.** If such a tool's process dies mid-execute, anything that ran before the channel call would be lost; if cubepi tried to re-enter the tool body on resume, side effects would repeat. cubepi will:++- Support such tools in the **same-process** mode (await blocks, host answers, loop resumes — no replay needed because the process never died).+- Reject them in the **cross-process** mode: a `CheckpointedChannel` started inside such a tool raises `HitlDurabilityNotGuaranteed` unless the channel is constructed with `allow_inside_custom_tool=True`, which the caller uses to acknowledge the idempotency contract (the tool body must be a pure HITL wait at that point, with no preceding observable side effects).++The two built-in suspension points are wired by cubepi itself and carry the `allow_inside_custom_tool` flag implicitly. Documentation will say plainly: **for durable HITL, use `ConfirmToolCallMiddleware` / `ApprovalPolicyMiddleware` or the `ask_user` tool. Anything else is best-effort same-process.**+## 3. Surface AreaTwo surfaces, both backed by the same channel.@@ -94,7 +108,8 @@ class AskRequest(BaseModel):questions: list[Question]class HitlRequest(BaseModel):- question_id: str # uuid4 string+ question_id: str # For ApproveRequest: identical to payload.tool_call_id.+ # For ConfirmRequest / AskRequest: uuid4 generated by channel.thread_id: str | Nonepayload: ConfirmRequest | ApproveRequest | AskRequestcreated_at: float@@ -113,28 +128,32 @@ class ApproveAnswer(BaseModel):#### `tool_call_id` ↔ `question_id` for approve requests-For `ApproveRequest` (the dangerous-tool-confirm flow), `tool_call_id` is provided by the LLM's tool call and is **1:1 with `question_id`** for the request's lifetime. Hosts may correlate frontend ↔ backend by either ID: cubebox-style hosts that already track `call_id` from the tool stream can pass that same value through to `Agent.respond()` (see §5.2) and the channel's `answer()` — passing `tool_call_id=…` is accepted as an alias for `question_id=…` in those APIs to make the migration painless.+For `ApproveRequest` (the dangerous-tool-confirm flow), the channel sets `question_id = tool_call_id` — they are **the same string**, not aliases of distinct UUIDs. The two field names exist for clarity at the call site (`question_id` in the channel/host APIs, `tool_call_id` in the inner payload), but no mapping or translation is needed anywhere. Hosts that already track `call_id` from the tool stream pass it directly to `channel.answer(question_id=call_id, ...)` and `agent.respond(question_id=call_id, ...)` — no alias kwarg required.-For `ConfirmRequest` and `AskRequest`, there is no associated tool call, so `question_id` is the only key.+For `ConfirmRequest` and `AskRequest`, there is no associated tool call, so `question_id` is a freshly-generated uuid4.### 4.2 `HitlChannel` Protocol (`cubepi/hitl/channel.py`)```pythonfrom typing import Protocol, AsyncIterator, Any+import asyncioclass HitlChannel(Protocol):# ---- agent side ----async def confirm(self, prompt: str, *,details: dict | None = None,tool_call_id: str | None = None,- timeout: float | None = None) -> bool: ...+ timeout: float | None = None,+ signal: asyncio.Event | None = None) -> bool: ...async def approve(self, tool_name: str, tool_call_id: str, args: dict, *,details: dict | None = None,- timeout: float | None = None) -> ApproveAnswer: ...+ timeout: float | None = None,+ signal: asyncio.Event | None = None) -> ApproveAnswer: ...async def ask(self, questions: list[Question], *,- timeout: float | None = None) -> dict[str, str | list[str]]: ...+ timeout: float | None = None,+ signal: asyncio.Event | None = None) -> dict[str, str | list[str]]: ...# ---- host side ----@property@@ -143,10 +162,44 @@ class HitlChannel(Protocol):async def answer(self, question_id: str, answer: Any) -> None: ...async def cancel(self, question_id: str, reason: str = "cancelled") -> None: ...- # ---- resume support (used by Agent.resume) ----+ # ---- resume support (used by Agent.respond) ----def attach_resume_answer(self, question_id: str, answer: Any) -> None: ...```+The `signal` kwarg lets channel calls participate in the agent's abort signal. Channels race the answer future against `signal.wait()`; if signal fires first, the channel raises `HitlAborted` (a subclass of `HitlControlException`, not `Exception`) and clears `_pending`. The built-in middleware / tool factories thread the current `signal` through automatically; custom callers pass it explicitly.++#### Exception hierarchy (`cubepi/hitl/exceptions.py`)++```python+class HitlControlException(BaseException):+ """Base for HITL-driven control-flow exceptions.++ Inherits BaseException, NOT Exception — so the existing broad+ `except Exception` catches in cubepi.agent.tools._prepare_tool_call+ and _execute_prepared do NOT swallow these. The agent loop catches+ HitlControlException explicitly at the right layer."""++class HitlCancelled(HitlControlException):+ def __init__(self, reason: str): ...++class HitlTimedOut(HitlControlException):+ def __init__(self, seconds: float): ...++class HitlDetached(HitlControlException): ...+class HitlAborted(HitlControlException): ...++# Regular Exceptions (caller-fixable misuse, not control flow):+class HitlError(Exception): ...+class HitlConcurrencyError(HitlError): ...+class HitlStaleAnswer(HitlError): ...+class HitlNoPendingRequest(HitlError): ...+class HitlMissingAnswer(HitlError): ...+class HitlInconsistentState(HitlError): ...+class HitlDurabilityNotGuaranteed(HitlError): ...+```++The `BaseException` choice is deliberate and matches the precedent set by `asyncio.CancelledError`. Loop changes (§6.2) explicitly catch `HitlControlException` *before* the broad `except Exception` handlers in `_prepare_tool_call` and `_execute_prepared`, so HITL control flow is never converted into a tool error.+#### Single-pending invariantIf `confirm/approve/ask` is called while `_pending is not None`, the channel raises `HitlConcurrencyError`. The agent loop's sequential execution makes this logically unreachable; the check exists to catch implementation bugs early.@@ -197,15 +250,15 @@ The "process died, web client posts answer hours later" case is handled by `Agen### 4.5 `Agent.detach()` (graceful suspend)-`Agent.detach()` causes any in-flight HITL `await` to raise `HitlDetached`, which the agent loop catches and treats like a clean stop (assistant message keeps its unresolved tool_calls; `pending_request` stays persisted; `Agent.run()` returns `AgentResult(state="suspended", pending_request=...)`).+`Agent.detach()` causes any in-flight HITL `await` to raise `HitlDetached`, which the agent loop catches and treats like a clean stop (assistant message keeps its unresolved tool_calls; `pending_request` stays persisted; `agent.prompt()` returns normally and an `AgentSuspendedEvent(pending_request=...)` fires on the event stream so listeners can react).Without `detach()`, a CheckpointedChannel agent simply blocks until an answer comes in via the same process or the process is killed externally. `detach()` is the explicit "I'm done waiting in this process" signal for hosts that want long-lived suspension across requests.## 5. Suspend / Resume Protocol-### 5.1 Persisted state+### 5.1 Persisted state — per-backend storage plan-`Checkpointer` (existing protocol) gains two optional methods:+`Checkpointer` (existing protocol) gains two optional methods (defined as default `None` returners in the base so existing checkpointers don't have to opt in to compile):```pythonasync def save_pending_request(self, thread_id: str,@@ -213,16 +266,52 @@ async def save_pending_request(self, thread_id: str,async def load_pending_request(self, thread_id: str) -> HitlRequest | None: ...```-- `MemoryCheckpointer`: a dict keyed by `thread_id`.-- `SQLiteCheckpointer` / `PostgresCheckpointer` / `MySQLCheckpointer`: new column `pending_request JSON NULL` (or equivalent) on the existing thread row. Each backend gets a migration step.+`HitlRequest` is serialized via `model_dump_json()` (pydantic) — discriminated union on `payload.kind` round-trips cleanly through any JSON storage.++#### `MemoryCheckpointer`++Add `_pending: dict[str, HitlRequest] = {}` alongside the existing in-memory message store. Trivial.++#### `SQLiteCheckpointer`++SQLite has no thread row today (only `messages` + `thread_extra`). Add a new table:++```sql+CREATE TABLE IF NOT EXISTS thread_pending_request (+ thread_id TEXT PRIMARY KEY,+ request_json TEXT NOT NULL,+ created_at REAL NOT NULL DEFAULT (julianday('now'))+);+```++`save_pending_request(thread_id, request)`: `INSERT OR REPLACE`; if `request is None`, `DELETE WHERE thread_id = ?`. `load_pending_request(thread_id)`: `SELECT request_json …`. No migration needed — `CREATE TABLE IF NOT EXISTS` is idempotent on existing dbs.-Backwards compat: existing data without the column reads as `None`; no behavior change for non-HITL flows.+#### `PostgresCheckpointer`++`cubepi_threads` has a row per thread. Add a column:++```sql+ALTER TABLE cubepi_threads ADD COLUMN pending_request JSONB NULL;+```++This requires a schema-version bump: `EXPECTED_SCHEMA_VERSION` goes from `1` to `2`. A new migration helper `migrate_v1_to_v2(connection)` adds the column. Backends that find `cubepi_schema_version.version == 1` on startup will refuse to start unless the caller runs the migration first (matching the existing strict version check). The migration is a single `ALTER TABLE … ADD COLUMN` with `NULL` default — non-blocking on Postgres.++`save_pending_request` / `load_pending_request` are `UPDATE cubepi_threads SET pending_request = … WHERE thread_id = …` and `SELECT pending_request FROM cubepi_threads WHERE thread_id = …`. Since the row is created lazily when the first message arrives (existing behavior), HITL writes assume the row exists by the time pending is being persisted (which is always true — `_pending` only fires inside an active turn that already produced an assistant message).++#### `MySQLCheckpointer`++Same as Postgres: add `pending_request JSON NULL` to `cubepi_threads`, bump `EXPECTED_SCHEMA_VERSION` to `2`, ship a `migrate_v1_to_v2()` helper. MySQL 8.0+ JSON type round-trips pydantic JSON cleanly.++#### Backwards compatibility++- SQLite: no migration needed (new independent table).+- Postgres / MySQL: explicit migration step. Documented in the release notes for this PR. The schema-version assertion fails loudly on old data, so users can't silently lose pending state by upgrading without migrating.### 5.2 Two resume paths`Agent` already exposes `resume()` for steering / follow-up resumption. We **do not** overload it. Instead:-**Same-process path (no Agent API needed).** While `agent.run(...)` is awaiting an in-flight HITL call, another coroutine in the same process calls `await channel.answer(question_id, answer)`. The channel resolves its internal future; the awaiting tool / middleware returns; the loop continues. `agent.run()` returns when the conversation completes normally.+**Same-process path (no Agent API needed).** While `agent.prompt(...)` is awaiting an in-flight HITL call, another coroutine in the same process calls `await channel.answer(question_id, answer)`. The channel resolves its internal future; the awaiting tool / middleware returns; the loop continues. `agent.prompt()` returns when the conversation completes normally.**Cross-process / post-detach path: new `Agent.respond(...)`.**@@ -231,110 +320,154 @@ async def respond(self,*,question_id: str | None = None,- tool_call_id: str | None = None, # alias for approve-kind pending; see §4.1answer: Any,) -> None:"""Resume an agent whose previous run suspended on a pending HITL request.Required when the original channel's in-flight future no longer exists —i.e. the original process died, or detach() was called.++ For approve-kind pending, question_id == tool_call_id (see §4.1); pass+ whichever the host already tracks."""+ if self._channel is None:+ raise HitlError("agent has no channel bound; pass channel= to Agent()")if not (self.thread_id and self.checkpointer):raise RuntimeError("respond() requires thread_id + checkpointer")- if self._state.is_streaming:- raise RuntimeError("Agent is already running")-- # Load history if not already loaded.- if not self._state._messages:- data = await self.checkpointer.load(self.thread_id)- if data:- self._state._messages = list(data.messages or [])- self._extra = dict(data.extra or {})-- pending = await self.checkpointer.load_pending_request(self.thread_id)- if pending is None:- raise HitlNoPendingRequest("no pending request on this thread")-- # Resolve question_id from either arg or the alias.- if question_id is None and tool_call_id is not None:- if not isinstance(pending.payload, ApproveRequest):- raise HitlStaleAnswer("tool_call_id is only valid for approve-kind pending")- if pending.payload.tool_call_id != tool_call_id:++ async with self._run_lock: # see "Concurrency guard" below+ # Load history if not already loaded.+ if not self._state._messages:+ data = await self.checkpointer.load(self.thread_id)+ if data:+ self._state._messages = list(data.messages or [])+ self._extra = dict(data.extra or {})++ pending = await self.checkpointer.load_pending_request(self.thread_id)+ if pending is None:+ raise HitlNoPendingRequest("no pending request on this thread")+ if question_id is None:+ question_id = pending.question_id+ if question_id != pending.question_id:raise HitlStaleAnswer(- f"tool_call_id mismatch: answer for {tool_call_id}, "- f"pending tool_call_id is {pending.payload.tool_call_id}"+ f"answer for {question_id}, pending is {pending.question_id}")- question_id = pending.question_id- if question_id is None:- question_id = pending.question_id- if question_id != pending.question_id:- raise HitlStaleAnswer(f"answer for {question_id}, pending is {pending.question_id}")-- self._channel.attach_resume_answer(question_id, answer)- await self.checkpointer.save_pending_request(self.thread_id, None)- await self._run_hitl_resume() # wraps run_agent_loop_resume; see §5.3++ self._channel.attach_resume_answer(question_id, answer)+ # NOTE: pending_request is NOT cleared yet — see ordering rules below.+ await self._run_hitl_resume() # wraps run_agent_loop_resume; see §5.3```-Note on async semantics (design decision §2 in the cubebox alignment): `respond()` is `async` but does **not** return immediately — it runs the loop forward and returns when the next pause or `AgentEndEvent` fires. Hosts that want to free the request thread for an SSE stream wrap it in `asyncio.create_task(agent.respond(...))` and consume events via `agent.add_listener(...)` (existing API) — exactly the pattern used today for `agent.run(...)`.+#### Pending-clear ordering (crash safety)++`pending_request` must be cleared **only after** the resulting `ToolResultMessage` (or synthetic deny) has been appended and persisted. Otherwise a crash between clear-pending and tool-execute would leave the thread in a state with neither pending nor tool_result, and `respond()` re-runs would raise `HitlNoPendingRequest`. The implementation:++1. `attach_resume_answer(qid, answer)` — channel's resume-answer slot now holds the answer.+2. Call `_run_hitl_resume()` which dispatches into `run_agent_loop_resume(...)`.+3. Inside the loop, the gated tool runs (or synthetic deny is built) and the resulting `ToolResultMessage` is checkpointed via the existing `MessageEndEvent` → `checkpointer.append(...)` path.+4. **Immediately after** the `ToolResultMessage` is checkpointed, the resume code clears `pending_request` via `checkpointer.save_pending_request(thread_id, None)` in the same checkpointer transaction where possible. For SQLite this is two separate writes; for Postgres/MySQL we wrap both writes in a transaction so they're atomic.++If the process crashes before step 4: on the next `respond()` attempt, the resume-answer slot is gone (in-memory only) but the channel sees the pending is still persisted **and** the conversation messages already include the tool_result — `attach_resume_answer` is idempotent and the loop skips re-execution of the gated tool (it sees the tool_result is already there in messages list). `pending_request` is cleared on this second attempt. **The invariant is: `pending_request` cleared ⇔ corresponding tool_result message exists in checkpointer.**++#### Concurrency guard++`Agent` gains `self._run_lock: asyncio.Lock`. `prompt()`, `resume()` (existing), and `respond()` all acquire it. The existing `_state.is_streaming` flag becomes a debug-only signal — the lock is the source of truth. Two concurrent `respond()` calls from different coroutines on the same `Agent` instance now block on the lock instead of racing the `is_streaming` check.++Cross-process concurrency (two processes both calling `respond()` on the same `thread_id`) is NOT protected here — that's the existing checkpointer concurrency story. Hosts that worry about it use their checkpointer's locking primitives (e.g. Postgres advisory lock on `thread_id`).++#### Async semantics-`agent.run(...)`, like today, returns `None`; persistent state lives in `agent.state` and the checkpointer, and the suspended state is observable by inspecting `_state._messages` (last message is an `AssistantMessage` with unresolved tool calls) and by calling `await checkpointer.load_pending_request(thread_id)`.+`respond()` is `async` and **does not return immediately** — it runs the loop forward and returns when the next pause or `AgentEndEvent` fires. Hosts that want to free the request thread for an SSE stream wrap it in `asyncio.create_task(agent.respond(...))` and consume events via `agent.subscribe()` (existing API) — exactly the pattern used today for `agent.prompt(...)`.-#### Detecting suspension from `agent.run(...)`+`agent.prompt(...)`, like today, returns `None`; persistent state lives in `agent.state` and the checkpointer, and the suspended state is observable by inspecting `agent.state.messages` (last message is an `AssistantMessage` with unresolved tool calls) and by calling `await checkpointer.load_pending_request(thread_id)`.++#### Detecting suspension from `agent.prompt(...)`When `Agent.detach()` is called during a pending HITL request (see §4.5), the loop catches `HitlDetached`, exits cleanly, and emits a new `AgentSuspendedEvent(pending_request=...)` so listeners can react. The assistant message keeps its unresolved tool calls; the next `respond()` will pick up from there.-For hosts that prefer a synchronous-style probe (instead of subscribing to the event stream), `Agent` exposes:+For hosts that need to probe pending state, `Agent` exposes **two** APIs — sync and async — because the in-memory channel slot is cheap to read but the checkpointer is not:```python@property-def pending_hitl_request(self) -> HitlRequest | None:- """Returns the pending HITL request for this agent's thread, or None.+def in_flight_hitl_request(self) -> HitlRequest | None:+ """Synchronous read of the channel's in-memory pending slot.- Reads from the channel's in-memory slot if the run is currently awaiting;- otherwise (post-detach / cross-process) falls back to a checkpointer lookup.+ Returns the pending request iff a HITL `await` is currently outstanding+ in THIS process. Returns None when the agent has detached or the+ process is a fresh resume that hasn't called channel.{ask,approve,confirm}() yet.+ """++async def load_pending_hitl_request(self) -> HitlRequest | None:+ """Async lookup that consults the checkpointer.++ Returns the pending HITL request persisted for this thread, or None.+ Use this after detach() / in fresh resume processes where the in-flight+ slot may be empty but pending data lives in the checkpointer."""```-A typical post-`run()` check:+A typical post-`prompt()` check:```python-await agent.run(prompt)-if (pending := agent.pending_hitl_request) is not None:+await agent.prompt(message)+pending = agent.in_flight_hitl_request or await agent.load_pending_hitl_request()+if pending is not None:# render pending.payload to the user; later call agent.respond(...)...```#### Aborting a suspended thread-For "user closed the conversation" / "admin kill switch", a method beyond per-question `channel.cancel()` is needed: clear persisted pending state and unblock the conversation so the model sees a synthetic denial.+For "user closed the conversation" / "admin kill switch", a method beyond per-question `channel.cancel()` is needed. Semantics: **abort closes the conversation.** No further model turn is engaged; the thread is terminal until the host explicitly continues with a new `prompt()`.```pythonasync def abort_pending(self, reason: str = "aborted by host") -> None:"""Cancel any pending HITL request on this thread, fake-deny the gated tool call,- and persist the resulting conversation state.+ and CLOSE the conversation (no further model call).- If a request is in-flight in the same process: channel.cancel(qid, reason)- (the awaiting tool / middleware will surface a deny tool_result naturally).- - If suspended cross-process: load checkpoint + pending, synthesize a- ToolResultMessage with is_error=True content="aborted: <reason>"- (and details.hitl={"decision":"aborted","reason":...}) for the gated- tool_call, append it, clear pending_request, emit AgentEndEvent- with stop_reason="aborted".- - No model call is made; the conversation is effectively closed.+ causes the awaiting tool / middleware to surface a deny tool_result via the+ normal path. The loop then sees the conversation has an aborted tool_result+ and exits via a synthesized terminal AssistantMessage(stop_reason="aborted").+ - If suspended cross-process (in-flight slot empty, checkpointer pending set):+ load checkpoint + pending; synthesize a ToolResultMessage with+ is_error=True, content=f"aborted: {reason}", details.hitl={"decision":"aborted","reason":...}+ and append it for the gated tool_call_id; then append a terminal+ AssistantMessage with empty content and stop_reason="aborted";+ clear pending_request; emit AgentSuspendedEvent or a new AgentAbortedEvent.++ No new model call is made during abort. The conversation history ends with+ a stop_reason="aborted" assistant turn, which any subsequent prompt() can+ follow naturally (the new user message simply appends after the aborted+ assistant turn — providers tolerate this)."""```-The synthetic deny path uses the same `hitl_trace` schema as policy/human deny so trace and audit semantics stay uniform.+The synthetic deny path uses the same `hitl_trace` schema as policy/human deny so trace and audit semantics stay uniform. `AgentEndEvent` does not currently carry a `stop_reason`; we add a new `AgentAbortedEvent` (or extend `AgentEndEvent` with an optional `stop_reason` field) so listeners can distinguish normal completion from abort.### 5.3 The resume code path-The resume path **never re-streams a model response that has already been streamed once.** Instead it inspects the last message and dispatches:+The resume path **never re-streams a model response that has already been streamed once.** The only durable suspension points (per §2.1) are before-tool-call approval and the `ask_user` tool body, both of which produce the same observable state: the last persisted message is an `AssistantMessage` containing at least one `ToolCall` whose `tool_call_id` has no matching `ToolResultMessage`. Resume thus has a single case:++- **Last message is `AssistantMessage` with one or more unresolved `ToolCall`s** — call `execute_tool_calls(...)` directly on that assistant message. The `ask_user` tool / `ApprovalPolicyMiddleware` calls `channel.{ask,approve,confirm}`, which pops the pre-loaded answer and returns immediately. Tool results flow into the normal loop; the next iteration re-streams a fresh model response with the tool results in context.+- **Anything else** — `HitlInconsistentState`. The only way to reach a state where pending is set but the last message is something other than an unresolved-tool-call AssistantMessage is data corruption or external mutation; we fail loudly.-- **Last message is `AssistantMessage` with at least one `ToolCall` whose `tool_call_id` has no matching `ToolResultMessage` later in the list** — call `execute_tool_calls(...)` directly on that assistant message. The `ask_user` tool / `ConfirmToolCallMiddleware` calls `channel.{ask,approve,confirm}`, which pops the pre-loaded answer and returns immediately. Tool results flow into the normal loop; the next iteration re-streams a fresh model response with the tool results in context.-- **Last message is `ToolResultMessage`** — a HITL request was made *during* `after_tool_call` or another non-execute path (rare). Re-enter the normal `_run_loop`; the channel still has its pre-loaded answer for whatever code path will call `ask()` next.-- **Anything else** — `HitlInconsistentState`, since pending should only have been written from inside an active turn.+(Earlier drafts allowed a "last message is `ToolResultMessage`" case for HITL invoked from `after_tool_call`. That's removed: `after_tool_call` runs *before* the `ToolResultMessage` is emitted in the current loop, so this case is unreachable.)-Implementation strategy: a new function `run_agent_loop_resume(...)` in `cubepi/agent/loop.py` that wraps `_run_loop` and pre-positions the "execute pending tool_calls" step. Most of `_run_loop` is reused; the only new logic is "skip the first `_stream_assistant_response` call if the last message is an unresolved-tool-call assistant message; jump straight to `execute_tool_calls`." It is exposed via `Agent._run_hitl_resume()` (called from `respond()`) which wires it into `_run_with_lifecycle` exactly like the existing `_run_prompt` / `_run_continuation` paths, so checkpointing on every `MessageEndEvent` continues to work.+Implementation strategy: a new function `run_agent_loop_resume(...)` in `cubepi/agent/loop.py` that:++1. Asserts the last-message invariant (raise `HitlInconsistentState` otherwise).+2. Skips the initial `_stream_assistant_response` call.+3. Calls `execute_tool_calls(...)` on the last assistant message — the channel pops its pre-loaded answer, the tool executes (or synthetic deny is built), `ToolResultMessage`(s) are appended and checkpointed via the normal `MessageEndEvent` path.+4. Clears `pending_request` (see §5.2 ordering).+5. Emits `TurnEndEvent`.+6. Runs `should_stop_after_turn` if configured.+7. **Now** drains any queued steering messages (matching the existing `_run_loop` ordering — steering MUST come after tool_results to preserve tool_use/tool_result adjacency).+8. Falls through to the normal `_run_loop` for the next model call.++Step 7 ordering matters: appending steering before tool_results would break the Anthropic-strict adjacency invariant the existing loop already protects. The resume path inherits that constraint.++`run_agent_loop_resume` is exposed via `Agent._run_hitl_resume()` (called from `respond()`), which wires it into `_run_with_lifecycle` exactly like the existing `_run_prompt` / `_run_continuation` paths, so checkpointing on every `MessageEndEvent` continues to work and the `_run_lock` is held for the duration.### 5.4 Same-process suspend (no resume needed)@@ -355,12 +488,49 @@ class BeforeToolCallResult(BaseModel):`reason` is the existing field — the message surfaced to the model when a tool call is blocked. `deny_reason` is new and is mirrored into `hitl_trace` for audit; we keep them distinct so the wording shown to the model and the wording stored for human audit can differ (the middleware fills both, usually with the same string).+### 6.1.1 `Agent` channel wiring++`Agent.__init__` gains a new keyword argument:++```python+def __init__(+ self,+ *,+ provider, model, ...,+ channel: HitlChannel | None = None, # NEW+ **kwargs,+):+ ...+ self._channel = channel+ if channel is not None:+ # Channel emits HitlRequestEvent / HitlAnswerEvent through this agent's+ # event listeners; bind during construction.+ channel._bind_emit(lambda e: self._process_event(e))+```++`agent.channel` (read-only property) returns the bound channel or `None`. Built-in factories pull from `agent.channel` by default:++```python+agent = Agent(provider=..., model=..., channel=InMemoryChannel())+agent.tools.append(ask_user_tool(agent.channel))+agent.middlewares.append(ConfirmToolCallMiddleware(agent.channel, require_confirm={"bash"}))+```++`Agent.respond()`, `Agent.detach()`, `Agent.abort_pending()`, `agent.in_flight_hitl_request` all require `self._channel is not None` — they raise `HitlError("agent has no channel bound")` if called on a non-HITL agent.+### 6.2 `loop.py` changes-In `_prepare_tool_call`:+In `_prepare_tool_call`, the existing broad `except Exception` becomes selective — `HitlControlException` (from §4.2) is allowed to propagate; only regular `Exception` is captured as a tool error:```python-if before_result := await before_tool_call(before_ctx, signal=signal):+try:+ before_result = await before_tool_call(before_ctx, signal=signal)+except HitlControlException:+ raise # HitlAborted / HitlDetached / etc bubble to _run_loop+except Exception as exc:+ return _ImmediateOutcome(result=_error_result(str(exc)), is_error=True)++if before_result:if before_result.block:return _ImmediateOutcome(result=_error_result(before_result.reason or "Tool execution was blocked"),@@ -373,16 +543,29 @@ if before_result := await before_tool_call(before_ctx, signal=signal):validated_args = tool.parameters.model_validate(before_result.edited_args)except ValidationError as exc:return _ImmediateOutcome(result=_error_result(str(exc)), is_error=True)- # carry hitl_trace forwardhitl_trace_for_finalize = before_result.hitl_trace```-`_PreparedToolCall` and `_ImmediateOutcome`/`_FinalizedOutcome` gain a `hitl_trace: dict | None = None` field. `_make_tool_result_message` merges it:+Same selective catch is added around `tool.execute(...)` in `_execute_prepared`.++At the `_run_loop` level, a new outer try-block wraps the inner `_run_loop` body to catch `HitlDetached` / `HitlAborted` and exit cleanly (emit `AgentSuspendedEvent` for detach, `AgentAbortedEvent` for abort).++`_PreparedToolCall` and `_ImmediateOutcome`/`_FinalizedOutcome` gain a `hitl_trace: dict | None = None` field. `_make_tool_result_message` merges it defensively (the existing `AgentToolResult.details` is typed `Any`, not `dict` — see SHOULD-FIX 13):```python-details = dict(finalized.result.details or {})-if finalized.hitl_trace:- details["hitl"] = finalized.hitl_trace+def _merge_hitl_details(base: Any, hitl: dict | None) -> Any:+ if hitl is None:+ return base+ if base is None:+ return {"hitl": hitl}+ if isinstance(base, dict):+ merged = dict(base)+ merged["hitl"] = hitl+ return merged+ # base is some other type (rare); preserve under reserved key+ return {"_non_dict_details": base, "hitl": hitl}++details = _merge_hitl_details(finalized.result.details, finalized.hitl_trace)return ToolResultMessage(..., details=details, ...)```@@ -507,6 +690,65 @@ class ConfirmToolCallMiddleware(ApprovalPolicyMiddleware):Neither middleware implements `after_tool_call` — `hitl_trace` flows through `BeforeToolCallResult` and is merged into `tool_result.details` by the loop (§6.2), so there is no per-tool-call dict state in the middleware itself.+### 6.3.4 `compose_middleware` redesign++The existing `compose_middleware()` in `cubepi/middleware/base.py` shortcuts `before_tool_call` results: it returns the first one whose `.block` is `True` and **discards** non-blocking results. That drops `edited_args` and `hitl_trace`, which we need. New behavior for `composed_before`:++```python+async def composed_before(ctx, *, signal=None):+ accumulated_hitl_trace: dict = {}+ edited_args: dict | None = None+ deny_reason: str | None = None+ block_reason: str | None = None+ blocked = False++ for mw in before_chain:+ # If a prior middleware edited args, the next middleware in the chain+ # sees the EDITED ctx (we rebuild ctx with new args so subsequent+ # middlewares can re-validate / re-deny against the edited form).+ cur_ctx = ctx if edited_args is None else _rebuild_ctx_with_args(ctx, edited_args)+ result = await mw.before_tool_call(cur_ctx, signal=signal)+ if result is None:+ continue+ if result.hitl_trace:+ # Last writer wins per key; earlier traces preserved under "_chain".+ if accumulated_hitl_trace:+ accumulated_hitl_trace.setdefault("_chain", []).append(dict(accumulated_hitl_trace))+ accumulated_hitl_trace.update(result.hitl_trace)+ if result.edited_args is not None:+ edited_args = result.edited_args+ if result.block:+ blocked = True+ block_reason = result.reason or block_reason+ deny_reason = result.deny_reason or deny_reason+ break # short-circuit on block; later middlewares don't run++ if not blocked and edited_args is None and not accumulated_hitl_trace:+ return None+ return BeforeToolCallResult(+ block=blocked,+ reason=block_reason,+ deny_reason=deny_reason,+ edited_args=edited_args,+ hitl_trace=accumulated_hitl_trace or None,+ )+```++Conflict semantics:+- **edited_args**: last writer in the chain wins; subsequent middlewares see the edit.+- **block**: short-circuits — any blocking middleware stops the chain immediately. Edits accumulated before the block are discarded (since the tool won't run).+- **hitl_trace**: merge by key; last writer wins per key; previous values archived under `_chain` so audit can see the full sequence.++These semantics are documented inline in the `compose_middleware` source. Tests cover all three cases (`test_compose_before_edit_chain`, `test_compose_before_block_after_edit`, `test_compose_before_hitl_trace_merge`).++### 6.3.5 HITL-capable tools must be sequential++Tools that call `await channel.{ask,confirm,approve}(...)` from inside their `execute()` body MUST declare `execution_mode="sequential"`. The built-in `ask_user_tool(channel)` factory sets it automatically. When the loop assembles a turn's tool_calls and any HITL-capable tool is among them, it falls back to `_execute_sequential` (this is already the existing behavior — `has_sequential` in `execute_tool_calls`).++For `before_tool_call`-based HITL (the `ApprovalPolicyMiddleware` / `ConfirmToolCallMiddleware` path), the existing loop **already serializes** `_prepare_tool_call` calls — both `_execute_parallel` and `_execute_sequential` call `_prepare_tool_call` in their outer for-loop before kicking off tool tasks. So even with parallel tools, only one approval prompt is outstanding at a time. We add a regression test (`test_parallel_tools_serialize_approvals`) to lock this in.++Subagent tools are themselves tools from the parent loop's POV. If a subagent uses HITL, its containing tool must be sequential at the parent level — `subagent_tool(...)` factory will set `execution_mode="sequential"` when constructed with a channel.+### 6.4 New eventsIn `cubepi/agent/types.py`:@@ -547,24 +789,46 @@ The trace CLI (`cubepi trace view`) renders these spans inline in the run tree,| Scenario | Behavior ||---|---|-| `channel.cancel(qid, reason)` in **ask_user tool** context | Pending future raises `HitlCancelled(reason)`; tool's `execute` lets it propagate; loop catches → `tool_result.is_error=True, content="cancelled by user: <reason>"`, `details["hitl"]={"outcome":"cancelled","reason":...}`. `pending_request` cleared from checkpointer. |+| `channel.cancel(qid, reason)` in **ask_user tool** context | Channel raises `HitlCancelled(reason)` (subclass of `HitlControlException`, see §4.2); `_execute_prepared`'s selective handler lets it propagate to `_run_loop`, which catches and produces `tool_result.is_error=True, content="cancelled by user: <reason>"`, `details["hitl"]={"outcome":"cancelled","reason":...}`. `pending_request` cleared from checkpointer. || `channel.cancel(qid, reason)` in **approve middleware** context | `ApprovalPolicyMiddleware` catches `HitlCancelled` and returns `BeforeToolCallResult(block=True, deny_reason=f"cancelled: {reason}", hitl_trace={"decision":"cancelled", ...})` — the gated tool never executes, model sees a clean denial (not a raw error). |-| `timeout` exceeded in **ask_user tool** context | Same shape as cancel-in-ask, but `HitlTimedOut`; `details["hitl"]={"outcome":"timed_out","seconds":N}`. |+| `timeout` exceeded in **ask_user tool** context | Same shape as cancel-in-ask, but `HitlTimedOut(seconds=N)`; `details["hitl"]={"outcome":"timed_out","seconds":N}`. || `timeout` exceeded in **approve middleware** context | Translated to `deny_reason="approval_timeout"` + `hitl_trace={"decision":"timed_out"}` — a clean fake-deny, *not* a tool error. Matches cubebox's "timeout → fake-deny so LLM keeps reasoning" requirement. |-| `signal.set()` during pending | Channel observes signal (it's passed in via tool's `execute(signal=...)` chain and `Middleware.before_tool_call(signal=...)`) and raises `asyncio.CancelledError`-equivalent; surrounding tool/MW lets it bubble; loop's existing abort path produces `AssistantMessage(stop_reason="aborted")`. `pending_request` is cleared. |-| `Agent.detach()` | Pending future raises `HitlDetached`; loop catches, exits cleanly with assistant message intact (tool_calls still unresolved); `pending_request` stays persisted; `Agent.run()` returns suspended result. |-| `answer(qid)` with unknown / stale qid | `HitlStaleAnswer`. Host code is expected to log / discard. |-| `resume(answer=...)` but no `pending_request` | `HitlNoPendingRequest`. |-| `resume()` (no answer) when there IS a pending | `HitlMissingAnswer`. |-| `confirm/approve/ask` while `_pending is not None` | `HitlConcurrencyError`. (Should be unreachable in practice; presence-check is a guardrail.) |-| Two processes concurrently call `resume()` on the same thread | Out of scope; the existing checkpointer concurrency story applies. Each backend's behavior is documented separately. |-| Resume but last message shape is unexpected | `HitlInconsistentState`. |+| `signal.set()` during pending | Channel observes the `asyncio.Event` (passed in via tool's `execute(signal=...)` and `Middleware.before_tool_call(signal=...)`) racing it against the answer future; if signal wins, channel raises `HitlAborted`. `HitlAborted` propagates through the selective handlers up to `_run_loop`, which emits `AgentAbortedEvent`. `pending_request` is cleared. |+| `Agent.detach()` | Channel raises `HitlDetached`. `_run_loop` catches at the outer level, emits `AgentSuspendedEvent(pending_request=...)`, exits cleanly with assistant message intact (tool_calls still unresolved). `pending_request` stays persisted; the next `respond()` picks up. |+| `answer(qid)` with unknown / stale qid | `HitlStaleAnswer` (`HitlError`, regular `Exception`). Host code is expected to log / discard. |+| `respond(answer=...)` but no `pending_request` | `HitlNoPendingRequest`. |+| `respond()` (no answer) when there IS a pending | `HitlMissingAnswer`. |+| `confirm/approve/ask` while `_pending is not None` | `HitlConcurrencyError`. (Should be unreachable in practice — see §6.3.5 sequential-execution guarantees; presence-check is a guardrail.) |+| Two processes concurrently call `respond()` on the same thread | Out of scope at the channel level; existing checkpointer concurrency story applies. Postgres/MySQL hosts can use advisory locks; SQLite hosts run a single writer by convention. |+| Resume but last message shape is not unresolved-tool-call AssistantMessage | `HitlInconsistentState` — unreachable in normal operation; indicates corruption or external mutation. |+| Custom tool starts `CheckpointedChannel.ask()` from inside `execute()` body | `HitlDurabilityNotGuaranteed` unless the channel was constructed with `allow_inside_custom_tool=True`. See §2.1. |## 8. Subagents-Subagents (spawned via parent's tool calls; see existing subagent trace nesting in `dev/specs/2026-05-13-cubepi-cubebox-readiness-design.md` and related) **inherit the parent agent's channel by default**. A subagent's `ask`/`confirm`/`approve` surfaces to the same host. Subagent constructors may override by passing an explicit `channel=` — e.g. a subagent that should auto-approve without prompting can be given a `NoopChannel` that returns canned answers.+cubepi today does not ship a built-in subagent tool — subagents are constructed by users wrapping an inner `Agent` instance inside a tool's `execute()` body. Since that pattern puts the subagent's run **inside a parent tool's execute()**, two constraints follow directly from §2.1 and §6.3.5:++1. **The parent tool wrapping a subagent that may use HITL must declare `execution_mode="sequential"`.** Otherwise multiple parallel subagents could all suspend at once and violate the single-pending invariant.+2. **The subagent's channel should be the parent's channel.** The natural pattern: the subagent-tool factory accepts `channel=parent.channel` from the constructor:++ ```python+ def subagent_tool(name, provider, model, *, channel=None, **kwargs):+ async def execute(call_id, args, *, signal, on_update):+ inner = Agent(provider=provider, model=model, channel=channel, ...)+ await inner.prompt(args.task)+ return AgentToolResult(content=[TextContent(text=inner.state.last_text)])+ return AgentTool(+ name=name,+ parameters=...,+ execute=execute,+ execution_mode="sequential" if channel else "parallel",+ )+ ```++3. **`HitlControlException`s bubble out of the inner `prompt()` call into the parent's tool execute.** The parent loop's selective handler in `_execute_prepared` (see §6.2) lets them propagate further up so the parent itself can be detached / aborted along with the subagent. The subagent's pending request lives in **the subagent's own thread_id** in the checkpointer (subagents that need durable resume must have their own `thread_id`); the parent's `pending_request` slot is unaffected.++For users who want a built-in "no-prompt always approve" channel for subagents (a common testing pattern), `cubepi.hitl.testing.NoopChannel` returns canned answers and never blocks.-Single-pending semantics still hold per channel — if a subagent is asking, the parent loop is blocked in the subagent's `execute_tool` call, so no parallel HITL ever materializes.+Single-pending semantics still hold per channel — if a subagent is asking, the parent loop is blocked in the subagent's `execute_tool` call, so no parallel HITL ever materializes at the channel level.## 9. Testing Strategy@@ -627,8 +891,23 @@ A `Callable` answer can inspect the request and dynamically produce a response (| `test_abort_pending_in_flight` | abort_pending() while same-process await → channel.cancel happens, normal cancel path || `test_abort_pending_cross_process` | abort_pending() on a thread with checkpoint-only pending → synthetic deny tool_result appended; pending cleared; AgentEndEvent stop_reason="aborted"; no model call made || `test_hitl_request_envelope_carries_timeout` | `channel.approve(..., timeout=42)` → emitted `HitlRequest.timeout_seconds == 42`; channel default applies when per-call omitted |-| `test_resume_preserves_cache_prefix` | Pause-and-resume on a dangerous-tool flow: serialize the messages list before pause and again before the model call after resume; assert the prefix (everything except the new tool_result + new assistant turn) is byte-identical |-| `test_tool_call_id_alias_for_respond` | `agent.respond(tool_call_id=..., answer=...)` accepted as alias for question_id when the pending request is an ApproveRequest |+| `test_resume_preserves_cache_prefix_memory` | (MemoryCheckpointer) Pause-and-resume on a dangerous-tool flow: serialize the messages list before pause and again before the model call after resume; assert the prefix (everything except the new tool_result + new assistant turn) is byte-identical |+| `test_resume_preserves_cache_prefix_sqlite` | Same, but using SQLiteCheckpointer so the messages round-trip through JSON storage. Asserts byte-identical prefix in `provider.stream(...)` calls captured by FauxProvider's recording mode |+| `test_resume_preserves_cache_prefix_postgres` | Same, gated on the Postgres E2E test env (mirrors existing checkpointer E2E patterns) |+| `test_question_id_equals_tool_call_id_for_approve` | For ApproveRequest, channel emits `HitlRequest.question_id == payload.tool_call_id` |+| `test_hitl_control_exceptions_not_swallowed_by_tool_handler` | Raise `HitlAborted` from inside a tool's execute; assert it propagates out of `_run_loop` (does NOT become a tool error result) |+| `test_compose_before_edit_chain` | Two middlewares: first edits args, second sees edited args and adds a hitl_trace; assert tool runs with second-edited args, details.hitl carries both traces |+| `test_compose_before_block_after_edit` | First middleware edits args; second middleware blocks. Assert tool does not run; tool_result.is_error=True; edited_args from first MW are discarded |+| `test_compose_before_hitl_trace_merge` | Two middlewares each add hitl_trace; assert merged dict contains keys from both, with earlier values preserved under `_chain` |+| `test_parallel_tools_serialize_approvals` | tool_execution="parallel" + ApprovalPolicyMiddleware on two dangerous tools → approvals issued sequentially, not concurrently; channel never sees concurrent ask |+| `test_hitl_tool_must_be_sequential` | ask_user_tool factory sets execution_mode="sequential"; manually constructing a parallel HITL tool raises at first invocation |+| `test_channel_signal_abort` | `signal.set()` while channel awaits → channel raises `HitlAborted`; loop emits `AgentAbortedEvent` |+| `test_durability_not_guaranteed_for_custom_tool` | Custom tool calls CheckpointedChannel.ask() inside execute() → raises HitlDurabilityNotGuaranteed; can be bypassed with allow_inside_custom_tool=True |+| `test_respond_concurrent_calls_serialize` | Two concurrent respond() calls on same agent block on _run_lock; second one sees HitlNoPendingRequest after first completes |+| `test_respond_crash_recovery_idempotent` | Simulate crash between attach_resume_answer and tool execution → on retry, attach_resume_answer is idempotent and the loop sees pending still set with tool_result already in messages → completes correctly |+| `test_postgres_schema_v2_migration` | (Postgres E2E) Existing v1 schema + pending_request column added via migrate_v1_to_v2 → reads/writes work; old threads without pending_request behave as None |+| `test_mysql_schema_v2_migration` | (MySQL E2E) Same |+| `test_sqlite_pending_table_idempotent` | Re-running CREATE TABLE IF NOT EXISTS is safe; multiple connections to existing DB don't break |All tests use `FauxProvider`. Resume tests use `MemoryCheckpointer`. Per-backend resume tests (SQLite/Postgres/MySQL) are E2E and gated like existing checkpointer tests.@@ -675,6 +954,7 @@ Durable workflow runtimes solve the "suspend across processes" problem in genera## 11. Open Questions / Out of Scope+- **Durable HITL from inside custom tool bodies.** See §2.1: only the `before_tool_call` approval gate and the `ask_user` built-in tool body are durable across processes. Custom tools that mix HITL with other work are same-process-only unless they opt in to `allow_inside_custom_tool` and accept the idempotency contract themselves.- **Multi-host fanout** (same channel routed to multiple human approvers, M-of-N). Not supported; channel has a single delivery point per `question_id`. A future extension could subclass `HitlChannel` with consensus semantics, but it's not in this spec.- **Approval caching / "don't ask again for N minutes."** Not in core channel. Hosts can layer.- **Approval signing / commandHash binding.** Not in core channel. Hosts can layer.@@ -695,14 +975,15 @@ Per CLAUDE.md ("a feature without docs is not done"), the implementation PR shipRough phases, finalized in the writing-plans step:-1. Types + `HitlChannel` protocol + `InMemoryChannel` + tests (no agent integration yet).-2. `BeforeToolCallResult` extension + `loop.py` `hitl_trace` merge + tests.-3. `ApprovalDecision` types + `ApprovalPolicyMiddleware` + `ConfirmToolCallMiddleware` shim + `ask_user_tool` + integration tests with `FauxProvider`.-4. New events (`HitlRequestEvent`, `HitlAnswerEvent`, `AgentSuspendedEvent`) + agent wiring of channel-to-emit + `Agent.pending_hitl_request` property.-5. `Checkpointer` `save_pending_request` / `load_pending_request` + per-backend migrations.-6. `CheckpointedChannel` + `Agent.detach()` + `Agent.respond()` resume path + `Agent.abort_pending()` + tests (including `test_resume_preserves_cache_prefix`).-7. Trace integration (lazy OTel) + trace CLI rendering tweaks if needed.-8. Subagent channel inheritance + tests.-9. Documentation (guide, recipes, README).+1. Types + `HitlChannel` protocol + `HitlControlException` hierarchy + `InMemoryChannel` + tests (no agent integration yet).+2. `BeforeToolCallResult` extension + `compose_middleware` redesign + `loop.py` selective-exception + `hitl_trace` merge + sequential-HITL enforcement + tests.+3. `Agent.__init__(channel=...)` wiring + `agent.channel` property + `agent.in_flight_hitl_request` + emit binding.+4. `ApprovalDecision` types + `ApprovalPolicyMiddleware` + `ConfirmToolCallMiddleware` shim + `ask_user_tool` (sequential) + integration tests with `FauxProvider`.+5. New events (`HitlRequestEvent`, `HitlAnswerEvent`, `AgentSuspendedEvent`, `AgentAbortedEvent`).+6. `Checkpointer.save_pending_request` / `load_pending_request` per backend: Memory dict; SQLite new table; Postgres schema v1→v2 migration; MySQL schema v1→v2 migration; tests including v2 migration E2E for SQL backends.+7. `CheckpointedChannel` (with `allow_inside_custom_tool` guard) + `agent.load_pending_hitl_request()` + `Agent.detach()` + `Agent.respond()` (incl. `_run_lock` + pending-clear ordering) + `Agent.abort_pending()` + tests (including all `test_resume_preserves_cache_prefix_*` variants and `test_respond_crash_recovery_idempotent`).+8. Trace integration (lazy OTel) + trace CLI rendering tweaks if needed.+9. Subagent channel inheritance + sequential constraint at parent level + tests.+10. Documentation (guide, recipes, README).Each phase has its own test suite that must pass before moving on; codex local review per CLAUDE.md after each milestone.