Skip to main content

agent_loop_parallel

Function agent_loop_parallel 

Source
pub async fn agent_loop_parallel(
    prompts: Vec<AgentMessage>,
    base_context: AgentContext,
    configs: Vec<AgentLoopConfig>,
    strategy: Arc<dyn EvaluationStrategy>,
    tx: UnboundedSender<AgentEvent>,
    cancel: CancellationToken,
) -> ParallelLoopResult
Expand description

Run multiple agent loop configurations concurrently from a shared base context, evaluate the results with the supplied strategy, and return the selected outcome.

This is the foundation for evaluational parallelism. The standard single-loop case is a transparent special case: one config + [crate::evaluation::TransparentEvaluation].

§Branch cloning

base_context is cloned once per config entry. Tools are Arc-shared (zero copy); the message history is deep-cloned so branches start from identical state but diverge independently. All branches inherit the same session_id for traceability.

§Loop IDs

Each branch receives a distinct loop_id:

"{session_id}.{config_segment}.{N}"

where config_segment is derived from config.config_id or auto-derived from provider + model + thinking level via [derive_config_segment].

§Events

Events from all branches are forwarded to tx interleaved. Each branch’s AgentStart carries a distinct loop_id for demultiplexing. A AgentEvent::ParallelLoopStart / AgentEvent::ParallelLoopEnd pair brackets the entire parallel execution.

§Session continuity

Feed selected_context into agent_loop_continue to resume the session normally after the parallel evaluation — this is a single-loop operation, not a special session mode.

§agent_loop_continue mode

When prompts is empty, each branch is dispatched via agent_loop_continue instead of agent_loop. This supports the “resume from existing context” pattern where the user query is already the last message in base_context. The same preconditions as agent_loop_continue apply: base_context.messages must be non-empty and must not end on an assistant message.