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 dispatch | Every pending call of a batch at once on the caller's task: concurrent, not parallel, unbounded, nothing spawned; panic becomes failed result |
30//! | Tool timeout | None; local timeout becomes unknown outcome |
31//! | 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)) |
32//! | Unknown outcomes | Commit the successful batch, then stop |
33//! | Stop after a batch finished processing | Commit its results, then report the stop |
34//! | Output retention and persistence | Caller-owned policies and storage |
35//!
36//! # Optional retries
37//!
38//! Retries are off by default: the runner makes one logical gateway call per
39//! round and the first error ends it. A host that wants bounded retries wraps
40//! its gateway in [`RetryingGateway`] with a [`RetryPolicy`]. The wrapper
41//! is an ordinary [`causa_kernel::ModelGateway`]: every attempt sends the
42//! same request, only `Transient` and `TimedOut` failures are retried, waits
43//! stay inside the caller's cancellation and deadline, and the call is never
44//! retried once any stream delta has been yielded. Only the final error
45//! reaches the runner.
46//!
47//! The runner does not interpret imported tool declarations as work to resume.
48//! It creates work only from the current model output. Session admission,
49//! history, input queues, retries, budget strategies and observation storage
50//! belong to callers. Dropping a run future or panicking bypasses the normal
51//! material-return guarantee; stopping local waits cannot undo remote effects.
52//!
53//! # Target path of one model call
54//!
55//! ```text
56//! host
57//!   TurnRunner (causa-runtime), per round:
58//!     ToolExecutor::bind            -> ToolSurface
59//!     commit ToolChange             when the bound tools differ from the context's
60//!     ContextPreparer (host, opt.)  -> ContextFrame; its tool state must equal the surface
61//!     build ModelRequest { invocation_id, frame, model, generation, cache }
62//!     ModelGateway::invoke | stream                      <- contract, causa-kernel
63//!       [optional] retry wrapper (causa-runtime)         same request each attempt; waits
64//!                                                        retry_delay or backs off; never
65//!                                                        retries after a delta was yielded
66//!         HTTP gateway (causa-provider)
67//!           MediaResolver                 -> MediaSet
68//!           render (causa-protocol)       frame -> wire JSON; rejects unsupported
69//!                                         generation options before any HTTP
70//!           send (reqwest), raced against CallControl
71//!           status / headers              -> ModelInvokeErrorKind, retry_delay
72//!           parse | StreamDecoder (causa-protocol)   wire -> ModelOutput, one stop
73//!                                         mapping per protocol for both entry points
74//!     <- ModelOutput { response, usage, stop_reason, raw_stop_reason, reasoning }
75//!     stop policy: continue on EndTurn / ToolUse; any other reason returns an
76//!                  interruption that carries the uncommitted output
77//!     commit: ModelResponse::to_blocks -> Context
78//! ```
79//!
80//! Ownership by layer: the kernel defines the shapes and the trait; the
81//! protocol crate owns every wire rule; the provider crate owns HTTP and the
82//! classification of HTTP outcomes; the runtime owns ordering, stop policy and
83//! the optional retry; the host owns preparation, limits and persistence.
84
85#![deny(unsafe_code)]
86#![deny(missing_docs)]
87
88pub mod composition;
89pub mod config;
90mod control;
91pub mod driver;
92pub mod event;
93pub mod executor;
94pub mod ids;
95mod processors;
96mod retry;
97
98pub use composition::ToolBridge;
99pub use config::{TurnLimits, TurnRunOptions};
100pub use driver::{TurnInterruption, TurnOutcome, TurnResult, TurnRunner};
101pub use event::{RunEvent, RunObserver};
102pub use executor::{
103    BoundTools, ToolCatalogError, ToolExecutor, ToolExecutorOptions, ToolProcessingError,
104    ToolProcessorPhase, ToolRegistryError,
105};
106pub use ids::new_block_id;
107pub use retry::{RetryPolicy, RetryingGateway};