Skip to main content

Module session_loop

Module session_loop 

Source
Expand description

Supervised session loop for the managed agent runtime.

The SessionLoop is the core execution engine. It runs as a tokio::spawned background task, dequeues UserEvents from an mpsc channel, processes each turn, and broadcasts SessionEvents to stream subscribers.

§Architecture

The loop composes:

  • SequenceCounter — assigns monotonically increasing seq to each event
  • ToolParkingLot — parks on custom_tool_use until client delivers a result
  • CheckpointManager — atomic checkpoint after each event
  • tokio::broadcast — fan-out to stream subscribers
  • Runner — drives the agent through the real LLM
  • SessionUsageTracker — tracks per-turn and cumulative token usage

§Control Flow

Dequeue UserEvent → emit status.running → invoke Runner
  → for each output event: classify, map, assign seq, checkpoint, broadcast
  → if custom tool call: park, wait for result, resume
  → track usage → emit status.idle → loop

§Interrupt and Pause

  • Interrupt: A CancellationToken signals the loop to stop at the next boundary. On interrupt, the loop emits status.idle and exits.
  • Pause/Resume: A pause flag + Notify allow the loop to park until resumed.

Structs§

SessionLoop
Supervised session loop — one per active session.