Skip to main content

causa_runtime/
lib.rs

1//! Optional execution components over the Causa material kernel.
2//!
3//! [`TurnRunner`] owns only a gateway and [`ToolExecutor`]. Each run consumes
4//! a context and returns it in [`TurnOutcome`], with any uncommitted tool batch.
5//! Model identity, generation, limits, optional preparation and observation
6//! are explicit per-run choices. Both entry points share the same loop.
7//!
8//! [`ToolExecutor::bind`] fixes the actual tool surface and targets before
9//! preparation. When the bound tools differ from the context's tool state, the
10//! runner appends one [`causa_kernel::ToolChange`] block (the first round of a
11//! context without tool facts appends the declaration), so the context records
12//! which tools the model saw when. The prepared frame must still add up to the
13//! bound tools; otherwise the round stops with
14//! [`TurnInterruption::PrepareFailed`]. Processing owns controlled collection
15//! of started calls; the runner validates and atomically appends results only
16//! after success.
17//! Unknown results are committed and stop further model rounds.
18//!
19//! # Policy surface
20//!
21//! | Choice | Default and owner |
22//! |---|---|
23//! | Model rounds / tool declarations | [`TurnLimits`]: 10 / 64 |
24//! | Model retries | None; optional [`RetryingGateway`] around the chosen gateway |
25//! | Material preparation | None; use the current context frame |
26//! | Tool changes | Appended to the context after each bind that changes the bound tools; a prepared frame that drops them fails the round |
27//! | Observation | None; an optional synchronous borrowed callback |
28//! | Tool processing | Executor-owned ordered before/after processors |
29//! | Tool hints | Carried from each tool or source to processors at bind time, never interpreted, rendered or stored in the context |
30//! | Processor data | None; optional host value in [`TurnRunOptions::processor_data`], passed to every processor unchanged and never inspected |
31//! | Tool dispatch | Every pending call of a batch at once on the caller's task: concurrent, not parallel, unbounded, nothing spawned; panic becomes failed result |
32//! | Tool result order | The order results arrived in (calls resolved before dispatch first, then completion order); an after-processor sorting [`ToolBatch::results_mut`](causa_kernel::ToolBatch::results_mut) by [`ProcessorContext::declaration_order`](causa_kernel::ProcessorContext::declaration_order) commits them in declaration order |
33//! | Tool timeout | None; local timeout becomes unknown outcome |
34//! | Stopping a tool | At the tool's next yield point; a tool that blocks the thread is not stopped or timed out (see [`Tool`](causa_kernel::Tool)) |
35//! | Unknown outcomes | Commit the successful batch, then stop |
36//! | Stop after a batch finished processing | Commit its results, then report the stop |
37//! | Output retention and persistence | Caller-owned policies and storage |
38//!
39//! # Optional retries
40//!
41//! Retries are off by default: the runner makes one logical gateway call per
42//! round and the first error ends it. A host that wants bounded retries wraps
43//! its gateway in [`RetryingGateway`] with a [`RetryPolicy`]. The wrapper
44//! is an ordinary [`causa_kernel::ModelGateway`]: every attempt sends the
45//! same request, only `Transient` and `TimedOut` failures are retried, waits
46//! stay inside the caller's cancellation and deadline, and the call is never
47//! retried once any stream delta has been yielded. Only the final error
48//! reaches the runner.
49//!
50//! The runner does not interpret imported tool declarations as work to resume.
51//! It creates work only from the current model output. Session admission,
52//! history, input queues, retries, budget strategies and observation storage
53//! belong to callers. Dropping a run future or panicking bypasses the normal
54//! material-return guarantee; stopping local waits cannot undo remote effects.
55//!
56//! # Target path of one model call
57//!
58//! ```text
59//! host
60//!   TurnRunner (causa-runtime), per round:
61//!     ToolExecutor::bind            -> ToolSurface
62//!     commit ToolChange             when the bound tools differ from the context's
63//!     ContextPreparer (host, opt.)  -> ContextFrame; its tool state must equal the surface
64//!     build ModelRequest { invocation_id, frame, model, generation, cache }
65//!     ModelGateway::invoke | stream                      <- contract, causa-kernel
66//!       [optional] retry wrapper (causa-runtime)         same request each attempt; waits
67//!                                                        retry_delay or backs off; never
68//!                                                        retries after a delta was yielded
69//!         HTTP gateway (causa-provider)
70//!           MediaResolver                 -> MediaSet
71//!           render (causa-protocol)       frame -> wire JSON; rejects unsupported
72//!                                         generation options before any HTTP
73//!           send (reqwest), raced against CallControl
74//!           status / headers              -> ModelInvokeErrorKind, retry_delay
75//!           parse | StreamDecoder (causa-protocol)   wire -> ModelOutput, one stop
76//!                                         mapping per protocol for both entry points
77//!     <- ModelOutput { response, usage, stop_reason, raw_stop_reason, reasoning }
78//!     stop policy: continue on EndTurn / ToolUse; any other reason returns an
79//!                  interruption that carries the uncommitted output
80//!     commit: ModelResponse::to_blocks -> Context
81//! ```
82//!
83//! Ownership by layer: the kernel defines the shapes and the trait; the
84//! protocol crate owns every wire rule; the provider crate owns HTTP and the
85//! classification of HTTP outcomes; the runtime owns ordering, stop policy and
86//! the optional retry; the host owns preparation, limits and persistence.
87
88#![deny(unsafe_code)]
89#![deny(missing_docs)]
90
91pub mod composition;
92pub mod config;
93mod control;
94pub mod driver;
95pub mod event;
96pub mod executor;
97pub mod ids;
98mod processors;
99mod retry;
100
101pub use composition::ToolBridge;
102pub use config::{TurnLimits, TurnRunOptions};
103pub use driver::{TurnInterruption, TurnOutcome, TurnResult, TurnRunner};
104pub use event::{RunEvent, RunObserver};
105pub use executor::{
106    BoundTools, ToolCatalogError, ToolExecutor, ToolExecutorOptions, ToolProcessingError,
107    ToolProcessorPhase, ToolRegistryError,
108};
109pub use ids::new_block_id;
110pub use retry::{RetryPolicy, RetryingGateway};