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