Skip to main content

Pool

Struct Pool 

Source
pub struct Pool<S: PoolStore> { /* private fields */ }
Expand description

A pool of Claude CLI slots.

Created via Pool::builder. Manages slot lifecycle, task routing, and budget enforcement.

Implementations§

Source§

impl<S: PoolStore + 'static> Pool<S>

Source

pub async fn auto(&self, prompt: &str) -> Result<AutoResult>

Auto-route a task: let an LLM decide whether to run, fan_out, or chain.

Sends prompt to a single routing call that classifies the work, then executes via the chosen pool method.

§Decomposition boundary

For parallel and chain routes, the router decomposes the task into subtasks/steps. This works well when the decomposition is obvious from the prompt (e.g. “review these 5 files” -> one task per file). For ambiguous decompositions, prefer explicit Pool::fan_out or Pool::submit_chain where the caller controls the split.

§Fallback

If the routing LLM returns unparseable output, the original prompt is executed as a single task rather than returning an error. Wrong routing is suboptimal, not catastrophic.

Source

pub async fn auto_with_hints( &self, prompt: &str, hints: &AutoHint, ) -> Result<AutoResult>

Auto-route with structured hints.

Hints inform the routing decision without overriding it. See AutoHint for available fields.

Source

pub async fn auto_with_config( &self, prompt: &str, config: Option<&AutoConfig>, ) -> Result<AutoResult>

Auto-route with full configuration.

Use this when you need the escape hatch of a custom prompt or when combining a custom prompt with hints.

Source

pub async fn route(&self, prompt: &str) -> Result<AutoRoute>

Route only: get the routing decision without executing.

Useful for debugging, logging, or prompt iteration — see what the router would choose without spending slots on execution.

Source

pub async fn route_with_hints( &self, prompt: &str, hints: &AutoHint, ) -> Result<AutoRoute>

Route with structured hints (no execution).

Source

pub async fn route_with_config( &self, prompt: &str, config: Option<&AutoConfig>, ) -> Result<AutoRoute>

Route with full configuration (no execution).

Source

pub async fn execute_route(&self, route: AutoRoute) -> Result<AutoResult>

Execute an already-decided route.

Normalizes degenerate routes before execution:

  • Parallel with 0-1 prompts becomes Single
  • Chain with 0-1 steps becomes Single
  • Empty prompts are rejected
Source§

impl Pool<InMemoryStore>

Source

pub fn builder(claude: Claude) -> PoolBuilder<InMemoryStore>

Create a builder with the default in-memory store.

Source§

impl<S: PoolStore + 'static> Pool<S>

Source

pub fn builder_with_store(claude: Claude, store: S) -> PoolBuilder<S>

Create a builder with a custom store.

Source

pub fn run<'pool>( &'pool self, prompt: impl Into<String>, ) -> RunOptions<'pool, S>

Begin building a synchronous task execution.

Returns a RunOptions builder. Call .await immediately for the simple case, or chain builder methods before awaiting:

// Simple usage — identical to the old pool.run("prompt").await
let result = pool.run("write a haiku about rust").await?;

// With overrides
let result = pool
    .run("refactor this file")
    .config(TaskOverrides { model: Some("claude-opus-4-6".into()), ..Default::default() })
    .working_dir("/tmp/myproject")
    .on_output(|chunk| print!("{chunk}"))
    .await?;
Source

pub async fn run_with_config( &self, prompt: &str, task_config: Option<TaskOverrides>, ) -> Result<TaskResult>

👎Deprecated since 0.1.0:

use pool.run(prompt).config(config).await

Run a task with per-task config overrides.

§Deprecated

Use Pool::run with the builder instead: pool.run(prompt).config(config).await

Source

pub async fn run_with_config_and_dir( &self, prompt: &str, task_config: Option<TaskOverrides>, working_dir: Option<PathBuf>, ) -> Result<TaskResult>

👎Deprecated since 0.1.0:

use pool.run(prompt).config(config).working_dir(dir).await

Run a task with per-task config overrides and an optional working directory override.

§Deprecated

Use Pool::run with the builder instead: pool.run(prompt).config(config).working_dir(dir).await

Source

pub async fn submit(&self, prompt: &str) -> Result<TaskId>

Submit a task for async execution, returning the task ID immediately.

Use Pool::result to poll for completion.

Source

pub async fn submit_with_config( &self, prompt: &str, task_config: Option<TaskOverrides>, tags: Vec<String>, ) -> Result<TaskId>

Submit a task with config overrides and tags.

Source

pub async fn submit_with_review( &self, prompt: &str, task_config: Option<TaskOverrides>, tags: Vec<String>, max_rejections: Option<u32>, ) -> Result<TaskId>

Submit a task that requires coordinator review before completion.

When the task finishes execution, it transitions to PendingReview instead of Completed. Use Pool::approve_result to accept or Pool::reject_result to reject with feedback and re-queue.

Source

pub async fn approve_result(&self, task_id: &TaskId) -> Result<()>

Approve a task that is pending review, transitioning it to Completed.

Source

pub async fn reject_result( &self, task_id: &TaskId, feedback: &str, ) -> Result<()>

Reject a task that is pending review, re-queuing it with feedback appended.

The original prompt is preserved and the feedback is appended. If the rejection count reaches max_rejections, the task is marked as Failed.

Source

pub async fn result(&self, task_id: &TaskId) -> Result<Option<TaskResult>>

Get the result of a submitted task.

Returns None if the task is still pending/running.

Source

pub async fn cancel(&self, task_id: &TaskId) -> Result<()>

Cancel a pending or running task.

Source

pub async fn claim(&self, slot_id: &SlotId) -> Result<Option<TaskId>>

Claim the next pending task for a specific slot.

Atomically finds the oldest pending task (with no slot assigned), assigns it to the given slot, and executes it in the background. Returns the claimed task ID, or None if no pending tasks are available.

Source

pub async fn cancel_chain(&self, task_id: &TaskId) -> Result<()>

Cancel a running chain, skipping remaining steps.

Sets the chain’s task state to Cancelled. The currently-executing step (if any) runs to completion; remaining steps are then skipped. Partial results are available via Pool::result once the chain finishes.

Source

pub async fn fan_out(&self, prompts: &[&str]) -> Result<Vec<TaskResult>>

Execute tasks in parallel across available slots, collecting all results.

Queues excess prompts until a slot becomes idle. Returns once all prompts complete or timeout waiting for slot availability.

Source

pub async fn submit_chain( &self, steps: Vec<ChainStep>, options: ChainOptions, ) -> Result<TaskId>

Submit a chain for async execution, returning a task ID immediately.

Use Pool::chain_progress to check per-step progress, or Pool::result to get the final crate::ChainResult (serialized as JSON) once complete.

Source

pub async fn fan_out_chains( &self, chains: Vec<Vec<ChainStep>>, options: ChainOptions, ) -> Result<Vec<TaskId>>

Submit multiple chains for parallel execution, returning all task IDs immediately.

Each chain runs on its own slot concurrently. Use Pool::chain_progress to check per-step progress, or Pool::result to get the final result once complete.

Source

pub fn chain_progress(&self, task_id: &TaskId) -> Option<ChainProgress>

Get the progress of an in-flight chain.

Returns None if no chain is tracked for this task ID.

Source

pub fn list_chain_progress(&self) -> Vec<(TaskId, ChainProgress)>

List all tracked chain progress entries.

Returns (chain_id, progress) pairs for every chain the pool is tracking, including completed and failed chains that haven’t been cleaned up yet.

Source

pub fn set_context(&self, key: impl Into<String>, value: impl Into<String>)

Set a shared context value.

Context is injected into slot system prompts at task start.

Source

pub fn get_context(&self, key: &str) -> Option<String>

Get a shared context value.

Source

pub fn delete_context(&self, key: &str) -> Option<String>

Remove a shared context value.

Source

pub fn list_context(&self) -> Vec<(String, String)>

List all context keys and values.

Source

pub fn send_message(&self, from: SlotId, to: SlotId, content: String) -> String

Send a message from one slot to another.

Returns the message ID.

Source

pub async fn broadcast_message( &self, from: SlotId, content: String, ) -> Result<Vec<String>>

Broadcast a message from one slot to all other active slots.

Returns the list of message IDs created (one per recipient).

Source

pub async fn find_slots( &self, name: Option<&str>, role: Option<&str>, state: Option<SlotState>, ) -> Result<Vec<SlotRecord>>

Find slots matching optional name, role, and/or state filters.

All filters are optional; omitted filters match everything.

Source

pub fn read_messages(&self, slot_id: &SlotId) -> Vec<Message>

Read and drain all messages for a slot.

Returns messages in order, removing them from the inbox.

Source

pub fn peek_messages(&self, slot_id: &SlotId) -> Vec<Message>

Peek at all messages for a slot without removing them.

Returns messages in order without draining the inbox.

Source

pub fn message_count(&self, slot_id: &SlotId) -> usize

Get the count of messages in a slot’s inbox.

Source

pub async fn drain(&self) -> Result<DrainSummary>

Gracefully shut down the pool.

Marks the pool as shut down so no new tasks are accepted, then waits for in-flight tasks to complete.

Source

pub async fn status(&self) -> Result<PoolStatus>

Get a snapshot of pool status.

Source

pub fn store(&self) -> &S

Get a reference to the store.

Source

pub fn config(&self) -> &PoolConfig

Get a reference to the pool configuration.

Source

pub fn claude(&self) -> &Claude

Get a reference to the underlying Claude client.

Source

pub async fn session_metrics( &self, filter: &MetricsFilter, ) -> Result<SessionMetrics>

Compute aggregated session metrics from all tasks.

Scans all tasks in the store and computes cost, timing, and model breakdowns useful for developer insights. Accepts an optional filter to narrow results by time window, tags, or model.

Source

pub fn start_supervisor(&self) -> Option<SupervisorHandle>

Start the background supervisor loop.

The supervisor periodically checks for errored slots and restarts them (up to PoolConfig::max_restarts). Returns a SupervisorHandle that can be used to stop the loop.

Returns None if PoolConfig::supervisor_enabled is false.

Source

pub async fn scale_up(&self, count: usize) -> Result<usize>

Scale up the pool by adding N new slots.

Returns the new total slot count. Fails if the new count exceeds max_slots.

Source

pub async fn scale_down(&self, count: usize) -> Result<usize>

Scale down the pool by removing N slots.

Removes idle slots first. If not enough idle slots are available, waits for busy slots to complete (with timeout) before removing them. Returns the new total slot count. Fails if the new count drops below min_slots.

Source

pub async fn set_target_slots(&self, target: usize) -> Result<usize>

Set the target number of slots, scaling up or down as needed.

Trait Implementations§

Source§

impl<S: PoolStore> Clone for Pool<S>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<S> Freeze for Pool<S>

§

impl<S> !RefUnwindSafe for Pool<S>

§

impl<S> Send for Pool<S>

§

impl<S> Sync for Pool<S>

§

impl<S> Unpin for Pool<S>

§

impl<S> UnsafeUnpin for Pool<S>

§

impl<S> !UnwindSafe for Pool<S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more