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
Plan — Parallel Tool-Batch Fault Isolation & Checkpoint Load Hardening
- Date: 2026-07-05
- Spec:
dev/specs/2026-07-05-tool-batch-fault-isolation.md - Branch:
2026-07-05-tool-batch-fault-isolation
Step 1 — broaden the narrow catches (cubepi/agent/tools.py)
_execute_prepared(:301-304): replace theexcept HitlControlException: raise / except Exceptionpair with a control-flow re-raise (HitlControlException,asyncio.CancelledError,KeyboardInterrupt,SystemExit) followed byexcept BaseException→_error_result(str(exc)), True._finalize'safter_tool_callguard (:353): same shape. Sequential executor inherits both fixes (shared helpers).
Step 2 — rewrite _execute_parallel collect stage (tools.py:630-651)
Keep the two-phase prepare and _run untouched. Replace the bare-await loop:
- Build
scheduledas today (aligned index-wise withentries, so a task slot's_PreparedToolCall— hencetool_callid/name andhitl_trace— is recoverable for synthesis). await asyncio.gather(*tasks, return_exceptions=True).except asyncio.CancelledError(outer cancel): re-gather to settle the now-cancelled children; best-effort (except Exception: pass) emit theToolResultMessages of slots that completed before the cancel (immediate outcomes included); re-raise. Agent-layer_complete_cancelled_tool_callsbackfills only the truly-unanswered ids.
- Classify settled slots in batch order:
- result present → normal
_FinalizedOutcome. HitlControlException/KeyboardInterrupt/SystemExit→ record first ascontrol_exc; slot stays unanswered, no synthesized End event (matches sequential detach shape).CancelledErrorwithout outer cancel (tool self-cancel) → synthesized error outcome, text[Tool execution cancelled].- any other exception → synthesized error outcome, text
str(exc). - Synthesized outcomes emit a
ToolExecutionEndEventto pair with the Start emitted inside_run(keepspending_tool_calls/trace balanced).
- result present → normal
- If
control_excis set: best-effort emit theToolResultMessages for all finalized/synthesized slots, thenraise control_exc. - Otherwise: existing tail (build messages, emit, return
ToolCallBatch).
Step 3 — CheckpointCorruptionError (cubepi/checkpointer/)
exceptions.py: addCheckpointCorruptionError(CheckpointerError)withthread_id,backend,row_ref,cause(spec Part 3 shape).sqlite.py:load()(:154-173):SELECT id, message_json; per-rowexcept Exception→CheckpointCorruptionError(backend="sqlite", row_ref=f"messages.id={id}").- completed-prefix read (
:415-425): same (SELECT id, message_json). _deserialize_message(:529-537): unknown role now raisesValueErrorinstead of returning the raw dict (wrapped by the callers' guards; only the two call sites above exist).
postgres/checkpointer.py:load()row loop (:172-183) and completed-prefix loop (:392-403): per-row guard,row_ref=f"seq={r['seq']}"; the existing unknown-roleValueErrormoves inside the guarded block so it wraps uniformly.mysql/checkpointer.py: same at:212-219and:475-483,row_ref=f"seq={seq}".- Guards catch
Exceptiononly (never wrapCancelledError).
Step 4 — tests
tests/agent/test_tools.py, newTestParallelFaultIsolation:- HITL raise mid-batch → siblings' results emitted (ordered), exception re-raised, HITL slot unanswered w/o End event, no still-pending tasks.
after_tool_callraising a bareBaseExceptionsubclass → error result for that call only (parallel and sequential variants).- tool-body
CancelledError(no outer cancel) →[Tool execution cancelled]error result, siblings unaffected. - outer cancellation mid-batch → completed slots' messages emitted before
CancelledErrorre-raises; unfinished slots unanswered. - Start/End pairing for synthesized outcomes.
- Checkpointer corruption tests (sqlite in
test_sqlite.py; postgres/mysql in their suites behind the existing skip-if-no-DB fixtures): corrupt payload row →CheckpointCorruptionErrorwith thread_id/backend/row_ref; unknown role → same (including sqlite's former silent fallthrough); bothload()and the fork-source read.
Step 5 — docs (required by workflow)
website/docs/guides/agents/tool-use.md: short "Fault isolation in parallel batches" note — one failing tool never drops sibling results; HITL/cancel semantics preserved.website/docs/guides/checkpointing/{sqlite,postgres,mysql}.md: troubleshooting entry forCheckpointCorruptionError(what it means, thatrow_reflocates the bad row).
Step 6 — gates & PR
uv run pytest tests/ + ruff check + ruff format --check + mypy cubepi;
commit, push, gh pr create; then drive the PR codex review loop
(poll ~2 min → fix → @codex) until clean.