Skip to content

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

spec: address codex R1 findings on conversation fork spec

xf gong7dc182c1b1ab
diff --git a/dev/specs/2026-06-05-conversation-fork.md b/dev/specs/2026-06-05-conversation-fork.md
index e54b3bb..c2e029c 100644
--- a/dev/specs/2026-06-05-conversation-fork.md
+++ b/dev/specs/2026-06-05-conversation-fork.md
@@ -131,63 +131,150 @@ for every call before the next assistant turn. Forking mid-tool-call
would leave the new thread in a state the provider rejects on the next
`prompt()` call ("`tool_use` block without matching `tool_result`").
-`snapshot()` and `fork()` both validate the cut: if message
-`message_count - 1` is an `AssistantMessage` with unresolved
-`ToolCall`s (i.e. one of its tool_call_ids has no corresponding
-`ToolResultMessage` in the prefix), they raise `ForkBoundaryError`.
-The error message lists the unresolved `tool_call_id`s so the caller
-can re-pick a later message_count.
-
-### 3.4 Atomicity
+`snapshot()` and `fork()` validate the **entire prefix**
+`[0..message_count)`, not just the last message. The prefix is valid
+iff all of the following hold:
+
+1. Every `ToolCall.id` produced by an `AssistantMessage` in the prefix
+ has exactly one `ToolResultMessage.tool_call_id` later in the
+ prefix that matches it.
+2. Every `ToolResultMessage.tool_call_id` in the prefix matches a
+ `ToolCall.id` produced by some earlier `AssistantMessage` in the
+ prefix.
+3. Tool-result messages for the same producing assistant turn appear
+ contiguously after that assistant turn (no other assistant turn
+ interleaves).
+
+Examples that MUST raise `ForkBoundaryError`:
+
+- `[assistant(tc-1), user(...)]` — assistant emits `tc-1` but the
+ prefix never carries its tool_result before another user turn.
+- `[assistant(tc-1), tool_result(tc-1), assistant(tc-2)]` with
+ `message_count=3` — the last assistant has an unresolved `tc-2`.
+- `[tool_result(tc-99)]` — orphan tool_result; no producer.
+- `[assistant(tc-1, tc-2), tool_result(tc-1)]` — partial close of a
+ multi-call turn; `tc-2` is unresolved.
+- `[assistant(tc-1), assistant(...)]` — two assistants in a row with
+ `tc-1` unresolved between them.
+
+The error payload lists the offending state (`unresolved_tool_call_ids`,
+`orphan_tool_result_ids`) so the caller can re-pick a valid
+`message_count`. `ForkBoundaryError` is raised before any write
+happens.
+
+The check runs in O(message_count) time and a single pass over the
+prefix; it is performed by `snapshot()` and reused by `fork()`.
+
+### 3.4 Atomicity and concurrency
`fork()` is atomic per backend:
-- **Memory**: holds `asyncio.Lock`; copies under the lock.
-- **SQLite**: single `BEGIN…COMMIT`.
-- **Postgres**: single transaction; thread row INSERT + messages
- `INSERT…SELECT WHERE thread_id=$1 AND seq <= $2`. Per-thread advisory
- lock on the source thread (the same lock `append()` already uses for
- monotonic seq allocation) prevents racing appends from being included
- half-and-half.
-- **MySQL**: single transaction; thread row INSERT + `INSERT…SELECT`.
- Uses the same locking discipline already in `append()`.
-
-If the new thread already exists, `fork()` raises
-`ThreadAlreadyExistsError` and writes nothing.
-
-If the source thread does not exist, `fork()` raises `ThreadNotFoundError`.
+- **Memory**: holds `asyncio.Lock` for the source thread (a single
+ shared lock is acceptable for this backend's scale); copies under
+ the lock. Memory backend is **single-process only by definition**;
+ this is already documented for the existing methods and applies to
+ fork unchanged. The fork doc page calls it out explicitly so users
+ do not expect fork lineage to survive a process restart.
+- **SQLite**: opens its transaction with `BEGIN IMMEDIATE` to take a
+ RESERVED lock on the database for the duration of the fork. This
+ blocks concurrent writers (`append`, `save_extra`,
+ `save_pending_request`, other `fork`s) — including writers from
+ other processes sharing the same DB file — until the fork commits
+ or rolls back. Readers (`load`, `load_pending_request`, `snapshot`)
+ continue under WAL. The existing `append()` does NOT currently use
+ `BEGIN IMMEDIATE`; this spec promotes it to `BEGIN IMMEDIATE` as
+ well so writer-vs-writer races are uniformly serialized. (The
+ promotion is a strict subset of correctness — current behavior
+ relies on aiosqlite's single-connection serialization within one
+ process; the upgrade makes the contract explicit and
+ cross-process-safe.)
+- **Postgres**: single transaction. Inside it:
+ 1. `pg_advisory_xact_lock(hashtext($src_thread_id))` — the same
+ per-thread advisory lock `append()` uses, but here taken on the
+ SOURCE thread to fence racing appends to the source for the
+ duration of the fork.
+ 2. `INSERT INTO cubepi_threads` for the new thread.
+ 3. `INSERT INTO cubepi_messages (...) SELECT ... FROM cubepi_messages
+ WHERE thread_id=$src AND seq <= $cut_seq ORDER BY seq`.
+ 4. Commit (releases the advisory lock).
+
+ `save_extra`, `save_pending_request`, and `compaction` on the source
+ also take this same per-thread advisory lock, so they serialize
+ against the fork.
+
+- **MySQL**: single transaction with `SELECT ... FOR UPDATE` on the
+ source thread row in `cubepi_threads` (MySQL has no advisory lock
+ equivalent that fits the model). The existing `append()` already
+ takes the same row lock; this spec confirms `save_extra`,
+ `save_pending_request`, and compaction follow suit. Then the same
+ thread-row INSERT + messages `INSERT…SELECT` as Postgres.
+
+Concurrent forks from the same source (`fork(src=X, new=A)` and
+`fork(src=X, new=B)`) serialize cleanly on the source-thread
+lock/row-lock in every backend; the two new threads end up with
+identical or differently-prefixed message sets depending on which
+fork ran first relative to any concurrent append, and either ordering
+is correct.
+
+Error pre-checks: if the new thread already exists, `fork()` raises
+`ThreadAlreadyExistsError` and writes nothing. If the source thread
+does not exist, `fork()` raises `ThreadNotFoundError`. Both checks
+happen inside the transaction so they see the same world the copy
+will see.
### 3.5 What gets copied
| Field | Copied? | Notes |
|---|---|---|
-| `messages` `[0..message_count)` | yes | physical copy; new thread's seqs equal the copied parent seqs |
-| `extra` | yes | deep copy of the source JSON object |
+| `messages` `[0..message_count)` | yes | physical copy; Postgres/MySQL preserve the source seq values for the copied range |
+| `extra` | yes | deep copy of the source JSON object (`json.loads(json.dumps(extra))`) |
| `parent_thread_id` | written (new) | set to `src_thread_id` on new row |
-| `forked_at_seq` | written (new) | the seq of message `message_count - 1` (Postgres/MySQL); equals `message_count` for Memory/SQLite |
-| `extra['fork']` | written (new) | `= metadata` argument when caller supplies it |
+| `forked_at_seq` | written (new, Postgres/MySQL only) | seq of the last copied message. NULL for Memory and SQLite — those backends have no per-message seq column and forging a value would invite false continuation logic. The column exists in the SQLite schema for parity but is always NULL there. |
+| `extra['fork']` | written (new) when `metadata` is supplied | merge rule below |
| `pending_request` | **no** | new thread starts clean; HITL is run-state, not history |
| `run_id` | **no** | host-side run identifier; new thread has none |
| `created_at` / `updated_at` | new | server-default to fork time |
+**`extra['fork']` merge rule.** The source's existing `extra` is
+deep-copied first. Then, if the caller passes `metadata`, fork writes
+`new_extra['fork'] = metadata` — unconditionally overwriting whatever
+key was at `extra['fork']` in the source. Rationale: a fork's `fork`
+key describes THIS fork, not the parent's fork ancestry. Lineage is
+already recoverable from the `parent_thread_id` chain in the threads
+table, so callers that want full ancestry walk the chain rather than
+reading nested `fork` blobs. Callers who want to preserve the source's
+`extra['fork']` under a different key are free to do so themselves via
+a follow-up `save_extra()` call.
+
### 3.6 Tracing for `fork_once()`
-`fork_once()` runs an in-memory `Agent` instance that is never persisted.
-It emits its own root span (it does **not** nest under the source
-thread's last run span — those spans are completed and unrelated):
-
-- Span name: `cubepi.agent.fork_once`
-- Attributes:
- - `cubepi.fork.src_thread_id`
- - `cubepi.fork.src_message_count`
- - `cubepi.fork.src_seq` (the storage seq of the last copied message,
- when the source backend has one)
- - `cubepi.model.id`, etc. — the standard cubepi tracing attributes
-- The in-process child `Agent` it runs subscribes to the standard
- tracing middleware if the host has one bound; child events nest under
- this span as normal.
-- The persistent `fork()` does not need a special span; existing
- checkpointer instrumentation (if any) covers it.
+`fork_once()` emits a span named `cubepi.agent.fork_once` that:
+
+- **Inherits the active OTel parent context** if one is bound when
+ `fork_once()` is called (e.g., the FastAPI request span, a
+ cubebox stream span). This is standard OTel convention; "fork_once
+ emits its own logical root" does NOT mean "detach from the
+ surrounding trace_id". A `fork_once` invoked outside any active
+ context becomes a true trace root.
+- Does **not** attempt to attach to or replay spans from the source
+ thread's prior runs. Those spans are completed and live in
+ different traces; we only carry forward the source thread's
+ identity via attributes.
+
+Attributes on the span:
+
+- `cubepi.fork.src_thread_id`
+- `cubepi.fork.src_message_count`
+- `cubepi.fork.src_seq` (Postgres/MySQL only — the storage seq of the
+ last copied message; absent for Memory/SQLite for the same reason
+ `forked_at_seq` is NULL there)
+- The standard cubepi tracing attributes (`cubepi.model.id`, etc.)
+
+The in-process child `Agent` it runs nests its agent / turn / tool
+spans under the `cubepi.agent.fork_once` span as normal.
+
+The persistent `fork()` does not need a special span; existing
+checkpointer instrumentation (if any) covers it.
### 3.7 API
@@ -265,7 +352,7 @@ class Agent(Generic[TMessage]):
async def fork_once(
self,
src_thread_id: str,
- prompt: str | list[Content],
+ message: str | Message | list[Message],
*,
message_count: int | None = None,
) -> ForkOnceResult:
@@ -274,6 +361,14 @@ class Agent(Generic[TMessage]):
`tools`, `middleware`, `system_prompt`; runs one full turn
(including any tool calls); returns the result. Never writes
to the checkpointer.
+
+ The `message` parameter accepts the same types as
+ `Agent.prompt()` for consistency. The transient agent receives
+ the snapshot as pre-seeded history, then `prompt(message)` runs
+ the single turn.
+
+ Raises `RuntimeError` if any inherited tool or middleware
+ requires HITL (see §3.9.1).
"""
```
@@ -295,9 +390,20 @@ class ForkOnceResult:
`snapshot()` slices the list. Boundary validation walks the prefix
once.
- **SQLite** (`cubepi/checkpointer/sqlite.py`): a thread is currently
- one row keyed by `thread_id`. Schema needs a small migration to add
- `parent_thread_id`, `forked_at_seq` columns (NULL for existing
- rows). `fork()` re-serializes the prefix into a new row.
+ one row keyed by `thread_id`. Schema needs an additive migration to
+ add `parent_thread_id` (TEXT, NULL) and `forked_at_seq` (INTEGER,
+ always NULL). The existing checkpointer initialises its tables via
+ `CREATE TABLE IF NOT EXISTS`; the new columns are added at connect
+ time using the existing pattern (`PRAGMA table_info` probe, then
+ `ALTER TABLE … ADD COLUMN` if missing — exact form decided by the
+ plan to match what the file already does for schema upgrades).
+ `fork()` opens a transaction with `BEGIN IMMEDIATE` (RESERVED
+ lock), re-serializes the prefix into a new row, writes the
+ `parent_thread_id` reference, leaves `forked_at_seq` NULL, commits.
+ `forked_at_seq` exists in the schema purely for parity with the SQL
+ backends — SQLite stores the messages as a single serialized blob,
+ there are no per-message seqs to record, and synthesising one would
+ give callers a false invariant to depend on.
- **Postgres** (`cubepi/checkpointer/postgres/`): the columns already
exist (schema version 3). `fork()` is a single transaction:
- `INSERT INTO cubepi_threads (thread_id, parent_thread_id, forked_at_seq, extra, …) …`
@@ -321,40 +427,102 @@ the same pattern applies to the new columns).
### 3.9 `Agent.fork_once()` execution detail
-1. `self._require_checkpointer()` — raises with a clear message when
- the agent has no checkpointer bound.
-2. `snapshot = await self.checkpointer.snapshot(src_thread_id, message_count=...)`
- — performs boundary validation.
-3. Build a transient `Agent` configured with this agent's `model`,
+1. `self._require_checkpointer()` — raises `RuntimeError` with a
+ clear message when the agent has no checkpointer bound.
+2. **HITL pre-flight check** (see §3.9.1) — raises before doing any
+ work if the inherited toolset is HITL-capable.
+3. `snapshot = await self.checkpointer.snapshot(src_thread_id, message_count=...)`
+ — performs the full-prefix boundary validation in §3.3.
+4. Build a transient `Agent` configured with this agent's `model`,
`system_prompt`, `tools`, `middleware`, and `convert_to_llm`, with
- no `checkpointer` and no `thread_id`. Pre-seed its message history
- with `snapshot`. The exact pre-seed hook (constructor arg vs.
- dedicated method) is left to the implementation plan — the
- constraint is that `fork_once()` must not reach into private
- attributes of `Agent`.
-4. Start a `cubepi.agent.fork_once` root span. Attach a one-shot
- listener that captures the child's new messages (everything after
- the pre-seed length).
-5. `await child.prompt(prompt)` — runs the full turn (any tool calls
+ `checkpointer=None` and `thread_id=None`. Pre-seed its message
+ history with `snapshot`.
+
+ **This requires a new public Agent surface for seeding initial
+ history.** The exact form (constructor arg `messages=...` vs. a
+ dedicated `Agent.preload(messages)` method) is decided in the
+ implementation plan, but it is in scope for THIS spec — `fork_once()`
+ MUST NOT reach into private attributes of `Agent`. The plan SHOULD
+ prefer a constructor arg because it keeps the agent's "ready to run"
+ state machine single-phase; if the plan picks a separate method,
+ document that the method must be called before any `prompt()`.
+
+5. Start a `cubepi.agent.fork_once` span (inheriting the surrounding
+ OTel context per §3.6). Capture the pre-seed length.
+6. Cancellation is propagated: if the surrounding task is cancelled
+ (or `asyncio.CancelledError` is raised), the transient agent's
+ abort signal fires and the cancellation re-raises after the agent
+ loop returns — same semantics as cancelling `Agent.prompt()`. The
+ tracing span is closed in a `finally` block with the failure
+ status set.
+7. `await child.prompt(message)` — runs the full turn (any tool calls
included).
-6. Read final assistant text + new messages from the child; close the
- span.
-7. Return `ForkOnceResult(text, new_messages, stop_reason)`.
+8. Read final assistant text + the messages added after the pre-seed
+ length from the child; close the span.
+9. Return `ForkOnceResult(text, new_messages, stop_reason)`.
The transient `Agent` is local to the call frame and dropped on return.
No state leak to `self`.
+#### 3.9.1 HITL is not supported inside `fork_once()`
+
+Two reasons HITL cannot work in `fork_once()`:
+
+1. **No persistence target.** HITL pending requests are written via
+ `Checkpointer.save_pending_request(thread_id, ...)`. The transient
+ agent has no checkpointer and no thread_id; there is nowhere to
+ persist the pause.
+2. **Worse: inherited HITL channels would write to the SOURCE thread.**
+ A real cubepi host typically constructs a
+ `CheckpointedChannel(checkpointer=cp, thread_id=conversation_id, …)`
+ and binds it to `ask_user_tool(channel)`. The channel object holds
+ the source `thread_id` by reference. Reusing such a tool inside
+ `fork_once()` would persist the fork's pending HITL request to the
+ SOURCE conversation, contaminating it. Silent failure mode.
+
+`fork_once()` therefore pre-checks `self.tools` and `self.middleware`
+for HITL involvement. Detection rule:
+
+- Any tool whose `name` is `ask_user` (cubepi's built-in HITL tool name).
+- Any middleware that is an instance of `cubepi.hitl.middleware.HitlMiddleware`
+ (or any subclass).
+- Any tool whose `name` matches a configurable host-supplied set —
+ the spec defers the exact extension hook to the plan.
+
+If any match, `fork_once()` raises:
+
+```
+RuntimeError(
+ "fork_once() does not support HITL. Found HITL-bearing tools/"
+ "middleware: <names>. Construct a different Agent without these "
+ "for ephemeral probes."
+)
+```
+
+The error is raised BEFORE the snapshot is read. The
+recommended pattern for callers that need probes with most of the
+host's tools but no HITL is to build a second `Agent` with a filtered
+toolset and call `fork_once()` on that one.
+
+This is documented in the user guide page as a known limitation.
+Lifting it (e.g., supporting in-memory HITL via a special transient
+channel) is explicit follow-up scope.
+
### 3.10 Error semantics summary
| Situation | Raised |
|---|---|
| `self.checkpointer is None` (fork or fork_once) | `RuntimeError("fork requires a checkpointer")` |
+| `fork_once()` finds HITL-bearing tool/middleware (§3.9.1) | `RuntimeError("fork_once() does not support HITL: <names>")` |
| `src_thread_id` does not exist | `ThreadNotFoundError(src_thread_id)` |
| `new_thread_id` already exists (fork) | `ThreadAlreadyExistsError(new_thread_id)` |
| `message_count < 0` | `ValueError` |
| `message_count > len(messages)` | `ValueError` (caller asked for more than exists) |
-| cut would orphan a tool_call | `ForkBoundaryError(message_count, unresolved_ids)` |
+| `message_count = 0` | **valid**: clones an empty starter thread. Boundary check passes vacuously. For Postgres/MySQL, `forked_at_seq IS NULL`. The new thread row is created with no messages and acts as if just-created with a `parent_thread_id` reference. |
+| `message_count = None` | normalized to `len(messages_in_src)` — copy everything currently present |
+| cut violates any §3.3 invariant (unresolved tool_call, orphan tool_result, partial multi-call close, two assistants in a row with unresolved calls) | `ForkBoundaryError(message_count, unresolved_tool_call_ids=[…], orphan_tool_result_ids=[…])` |
| `Agent.fork_once()` child run errors | propagates (same surface as `Agent.prompt()`) |
+| `Agent.fork_once()` is cancelled mid-turn | `asyncio.CancelledError` re-raises after transient agent abort completes |
## 4. Migration / Compatibility
@@ -376,30 +544,70 @@ No state leak to `self`.
server documented at `reference_mysql_test_server`):
- fork all messages → new thread reads back identically
- fork prefix → new thread holds prefix, source still holds full
- - fork preserves `extra`; sets `parent_thread_id` + `forked_at_seq`
- - fork copies `extra['fork']` from `metadata` arg
- - fork does NOT copy `pending_request` / `run_id`
+ - fork preserves `extra`; sets `parent_thread_id`
+ - `forked_at_seq` assertions: equals last copied seq for
+ Postgres/MySQL; IS NULL for Memory/SQLite
+ - fork copies `extra['fork']` from `metadata` arg (and overwrites
+ a pre-existing `extra['fork']` on the source — explicit test for
+ the §3.5 merge rule)
+ - fork does NOT copy `pending_request`: verified via
+ `load_pending_request(new_thread_id) is None`
+ - fork does NOT copy `run_id`
- `ThreadAlreadyExistsError` on collision; nothing written
+ (no partial thread row, no orphan messages)
- `ThreadNotFoundError` on bad source
- - `ForkBoundaryError` when cutting mid-tool-call (and the error
- payload lists unresolved tool_call_ids)
- - source thread unaffected by fork (independence test)
- - subsequent `append()` to the new thread continues seq numbering
- from `forked_at_seq + 1`
+ - `ValueError` for `message_count < 0` and `message_count > len`
+ - `message_count = 0` succeeds, produces an empty new thread with
+ `parent_thread_id` set
+ - `message_count = None` copies all current messages
+ - `ForkBoundaryError` for each illegal-prefix case enumerated in
+ §3.3: unresolved tool_call at end, orphan tool_result, partial
+ multi-call close, two assistants in a row with unresolved calls.
+ The error payload lists the offending ids.
+ - source thread unaffected by fork (independence test —
+ snapshot/load source before and after, byte-equal)
+ - Postgres/MySQL only: subsequent `append()` to the new thread
+ continues seq numbering from `forked_at_seq + 1`
- concurrent fork + append on source serializes correctly (no
- half-copied state)
+ half-copied state) — exercise with two concurrent tasks
+ - fork-of-fork: fork A → B; fork B → C. C's `parent_thread_id`
+ points to B (not A). Walking the chain via `parent_thread_id`
+ reaches A.
+ - fork after `respond()`-injected synthetic deny: the synthetic
+ messages are in the snapshot and survive the fork
+ - SQLite cross-connection: open two separate `SQLiteCheckpointer`
+ instances against the same DB file, exercise `fork()` from one
+ while `append()` runs from the other; `BEGIN IMMEDIATE`
+ serializes the two writers correctly
- **`Agent.fork_once()` (FauxProvider)**:
- simple text-only follow-up returns expected final text
- turn with tool calls completes fully, returns new messages
- source thread (and its checkpointer) is byte-for-byte unchanged
- - raises when no checkpointer bound
- - emits a `cubepi.agent.fork_once` span with the documented
- attributes (using the in-memory tracing exporter helper)
+ - raises `RuntimeError` when no checkpointer bound
+ - raises `RuntimeError` when an `ask_user` tool is in
+ `self.tools` (HITL pre-flight, §3.9.1)
+ - raises `RuntimeError` when `HitlMiddleware` is in
+ `self.middleware`
+ - cancellation: `asyncio.wait_for(agent.fork_once(...), timeout=…)`
+ raises `TimeoutError` and the transient agent's abort signal
+ fires (verified via FauxProvider abort hook)
+ - tracing span: emits `cubepi.agent.fork_once` with the documented
+ attributes. When called inside an existing span context, the
+ fork_once span is a child of the surrounding span (parent
+ propagation test). When called outside any span, it is a trace
+ root.
+ - The existing tracing test infrastructure
+ (`tests/tracing/conftest.py` in-memory exporter, if present) is
+ reused; if no such infrastructure exists, the plan stage
+ introduces a minimal `InMemorySpanExporter` fixture as a
+ prerequisite.
- **`Agent.fork()` (FauxProvider)**:
- happy path returns None; checkpointer state is correct
- - error pass-through (`ThreadNotFoundError`, etc.)
+ - error pass-through (`ThreadNotFoundError`, `ThreadAlreadyExistsError`,
+ `ForkBoundaryError`, `RuntimeError` for missing checkpointer)
+ - does NOT mutate `self.thread_id`
## 6. Open questions