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