Skip to main content

claude_pool/
lib.rs

1//! Coordinator/worker orchestration for Claude Code.
2//!
3//! `claude-pool` manages N Claude CLI instances behind a pool. A **coordinator**
4//! (an interactive Claude session with the pool MCP server in its `.mcp.json`)
5//! dispatches work to **worker** slots, monitors results, reviews outputs, and
6//! decides what merges. Workers are isolated Claude instances that execute one
7//! task at a time.
8//!
9//! This crate provides the pool library. For the MCP server binary, see
10//! `claude-pool-server`.
11//!
12//! # The Coordinator/Worker Model
13//!
14//! The coordinator/worker split is the central organizing principle:
15//!
16//! - **Coordinator**: A long-running interactive Claude session. It holds the
17//!   human's context, makes sequencing decisions, reviews outputs, and calls
18//!   `pool_*` MCP tools to delegate work. It does not execute tasks directly;
19//!   it schedules work and acts on results.
20//! - **Worker**: A Claude slot executing a single task (a `pool_run`, a chain
21//!   step, or a fan-out branch). Workers are stateless between tasks; they
22//!   receive a prompt, do the work, and return output. The pool handles slot
23//!   assignment, session resumption, and restarts.
24//!
25//! This model matters because it separates *what to do* (coordinator) from
26//! *how to do it* (worker). The coordinator stays responsive; workers run in
27//! parallel without blocking the human's session.
28//!
29//! # Vocabulary
30//!
31//! ## Nouns
32//!
33//! | Term | Meaning |
34//! |------|---------|
35//! | Coordinator | Interactive Claude session that schedules and reviews work |
36//! | Worker | A slot executing one task; returns output to the coordinator |
37//! | Slot | A managed Claude CLI instance; the execution unit |
38//! | Chain | Ordered pipeline of steps where each step feeds the next |
39//! | Fan-out | N independent tasks running in parallel across slots |
40//! | Task | A single unit of work submitted to the pool |
41//! | Tick | One iteration of the coordinator's monitor loop |
42//!
43//! ## Verbs
44//!
45//! | Term | Meaning |
46//! |------|---------|
47//! | schedule | Coordinator decides what work to dispatch next |
48//! | execute | A worker carries out the scheduled task |
49//! | monitor | Coordinator polls for results (tick loop) |
50//! | review | Coordinator inspects worker output before acting |
51//! | merge | Coordinator accepts a worker result into the main branch |
52//! | dispatch | Coordinator sends a task to the pool |
53//! | fan out | Submit N independent tasks in parallel |
54//! | chain | Submit an ordered pipeline of dependent steps |
55//!
56//! # When to Use What
57//!
58//! **Inline**: Do the work yourself without the pool. Use when the task is
59//! fast, single-step, or requires coordinator context that is impractical to
60//! inject.
61//!
62//! **`pool_run` / run**: One task, one result, blocks until done. Use for
63//! single bounded operations (file a ticket, run a check, rebase a branch).
64//!
65//! **`pool_chain` / chain**: Ordered pipeline where each step depends on the
66//! previous. Use when work has a clear linear dependency graph: draft ->
67//! review -> revise -> commit, or scaffold -> implement -> test -> PR. If you
68//! find yourself writing "and then" more than once, chain instead of run.
69//!
70//! **`pool_fan_out` / fan out**: N independent tasks in parallel. Use when the
71//! same operation applies to N independent targets (audit N crates, refactor N
72//! modules). No step depends on another; all run simultaneously.
73//!
74//! # Coordinator Rhythm
75//!
76//! A coordinator session follows a repeating rhythm:
77//!
78//! 1. **Dispatch**: Submit tasks (`pool_submit`, `pool_submit_chain`,
79//!    `pool_fan_out`). Receive task or chain IDs.
80//! 2. **Monitor**: Poll for results (`pool_result`, `pool_chain_result`).
81//!    Repeat until complete. Each poll is a tick.
82//! 3. **Review**: Inspect output. Check diffs, test results, summaries.
83//!    Decide whether to accept, reject with feedback, or escalate.
84//! 4. **Merge**: Accept the result (`pool_approve_result` for reviewed tasks,
85//!    or direct merge for unreviewed chains). Update coordinator state.
86//!
87//! Chains compress this into a single dispatch/monitor cycle. Fan-outs run
88//! monitor in parallel. With `pool_submit_with_review`, the coordinator
89//! explicitly gates on the review step before the result is finalized.
90//!
91//! # Model Selection
92//!
93//! Workers do not need the most capable model for every task. Match the model
94//! to the task complexity:
95//!
96//! - **Haiku**: Bounded single tasks -- file a ticket, run checks, tag a
97//!   release, rebase. High-volume fan-outs where throughput matters more than
98//!   depth.
99//! - **Sonnet**: Multi-file changes, code review needing judgment, planning
100//!   steps where quality matters, most chain steps.
101//! - **Opus**: Large mechanical refactors where one error breaks compilation,
102//!   complex architectural reasoning, tasks where you would send a senior
103//!   engineer.
104//!
105//! Override per-task or per-chain-step with the `model` field in
106//! [`RunOptions`]. The coordinator itself typically runs on Sonnet or Opus;
107//! workers can use cheaper models for routine work.
108//!
109//! # Architecture
110//!
111//! ```text
112//! Coordinator (interactive Claude session)
113//!   +-- .mcp.json includes "claude-pool"
114//!         +-- claude-pool MCP server
115//!               +-- slot-0 (Claude instance)
116//!               +-- slot-1 (Claude instance)
117//!               +-- slot-N
118//! ```
119//!
120//! One server. N slots. Nothing else.
121//!
122//! # Getting Started
123//!
124//! For most use cases, import the [`prelude`] module:
125//!
126//! ```rust,no_run
127//! use claude_pool::prelude::*;
128//! ```
129//!
130//! This brings in the most commonly needed types: [`Pool`], [`PoolBuilder`],
131//! [`TaskRecord`], [`TaskState`], [`ChainResult`], and related types.
132
133pub mod auto;
134pub mod chain;
135pub(crate) mod cli_parsing;
136pub mod config;
137pub mod error;
138pub(crate) mod executor;
139pub mod messaging;
140pub mod pool;
141pub mod prelude;
142pub mod route_test;
143pub mod store;
144pub mod supervisor;
145pub mod types;
146pub(crate) mod utils;
147pub mod worktree;
148
149// Core pool types
150pub use pool::{DrainSummary, Pool, PoolBuilder, PoolStatus, RunOptions};
151
152// Error handling
153pub use error::{Error, Result};
154
155// All types (backwards compatibility)
156pub use types::*;
157
158// Chain execution
159pub use chain::{
160    ChainIsolation, ChainOptions, ChainProgress, ChainResult, ChainStatus, ChainStep, StepAction,
161    StepFailurePolicy, StepResult, execute_chain,
162};
163
164// Storage
165pub use store::{InMemoryStore, JsonFileStore, PoolStore};
166
167// Supervisor
168pub use supervisor::{SupervisorHandle, check_and_restart_slots};
169
170// Messaging
171pub use messaging::{Message, MessageBus};
172
173// Worktree management
174pub use worktree::WorktreeManager;
175
176// Auto-routing
177pub use auto::{AutoConfig, AutoHint, AutoResult, AutoRoute, AutoStep, RoutePreference};