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