Skip to main content

Crate causa_runtime

Crate causa_runtime 

Source
Expand description

causa-runtime — optional components over the context kernel.

The kernel is facts (context) + contracts (ports) only. This crate is a set of optional, reference components on top of them: adopt the execution stack, take a single piece, or read them as a reference and build your own. Each component documents the policy it bakes in and what adopting it brings along.

  • Driver stack (driver, executor, hook, config, control): TurnRunner orchestrates turns over the kernel’s ports — retry scheduling, tool batch dispatch, artifact spill, traces, and run control.
  • Budget & interaction components (budget, interaction): the frame-materialization policy (FramePolicy, window budget, compaction, token counting) and the host↔driver TurnInteraction seam — the component’s documented opinions, not fact-layer invariants. A custom host composes the kernel’s lossless TurnContext::frame differently.
  • Session aggregate (conversation): ConversationState (single active slot, completed-only history, commit-time TurnSequence as HistoryEntry), the SealedResult stamp, and the ConversationStore archive port. These are component decisions, not fact-layer invariants; a custom host composes the kernel facts differently.
  • Session coordination (session): one Session owner per conversation drives accepted work through the assembled TurnRunner — cloneable SessionHandles submit / observe / wait / resume / cancel, one active work at a time, per-work TurnIds assigned at acceptance and never reused, local request_key dedup, explicit retained-work capacity, and an observable Faulted state. The material-free save loop is implemented: an idle or paused session exports a versioned SessionCheckpoint, and SessionCheckpoint::restore registers it against freshly assembled capabilities without calling the model or a tool. Multi-session coordination and per-work materials are out of scope.
  • Tool-use filters (DedupFilter, AllowAllFilter, DenyAllFilter, FilterChain) implement this crate’s ToolUseHook directly — the trait, its consumer, and its policies share one crate. The default is PassthroughHook (no opinion); composition and policy are opt-in.
  • Events (ContextEvent / project_turn) project a finished turn’s facts (TurnContext + TurnResult + TurnTrace) into an IPC-ready event sequence for UI / observability / audit consumers.

Implementation-side note: implementing a ModelGateway or a Tool requires only causa-kernel; this crate exists to use the optional execution components, not to fill the kernel’s ports.

§Optional components

Everything here is optional, and each piece is offered as a reference: adopt it whole, or read it and build your own. The four responsibilities below are how the crate is organized — not a promise about how finely it can be split. What adopting a piece brings along is noted as cost, not as a contract.

  1. Execution stack — TurnRunner plus its config (TurnRunOptions, policy axes), resume (resume_turn), hook seam, the budget (FramePolicy) and interaction (TurnInteraction) components, and the session owner (Session / SessionHandle / SessionCheckpoint). Adopting it brings those policy components along; the defaults they bake in are documented in the policy table below, not fact-layer invariants.
  2. Tool execution — ToolExecutor::execute_with_limits runs one call (panic isolation, deadline backstop, limit truncation, error mapping) with no runner and no session; adopting it also brings the config and budget vocabulary. Its catalog cache and static-name priority are executor policies, documented at the impl site.
  3. Session aggregate — ConversationState owns the single-active slot, completed-only history, and commit ordering (HistoryEntry). A host with different session needs composes kernel facts directly.
  4. Observation — traces and ContextEvent are projections of a finished turn for UI / audit consumers; reading them brings the execution stack along. The host chooses what to persist; there is no separate wire model.

§Policy surface

Every default the driver bakes in is one of two kinds — a swappable policy object, or a documented opinion. No trait seams exist for single-implementation policies; a seam is added only when a second real shape appears.

PolicyKindWhere
Retry schedule (disabled by default; when enabled, 500 ms base, 8 s ceiling, exponential)config object RetryPolicyconfig
Turn limits (10 model rounds, 64 tool calls by default)config object TurnLimits in TurnPolicyconfig
Frame compaction (none by default; host supplies compactor, token counter and thresholds)config object FramePolicybudget
Interaction / approval gateport object NoopInteraction defaultconfig
Tool-use filteringport object HookCtx / ToolUseHook, default PassthroughHookhook, filter
Tool-output token estimation fallback (chars/4; frame estimation defaults to zero)documented opinion, single homedefaults::placeholder_token_estimate_value (crate-internal), FramePolicy
Output truncation shape (retained head 60% + tail 40% sized to the declared token budget — notice and JSON-string wrapping measured, notice-only floor at tiny budgets; artifact spill; content replaced by a JSON string; Truncation::Middle marker)documented opinionexecutor::ToolExecutor::execute_with_limits
Unknown-outcome continuation (default Stop, per-executed-name overrides, explicit per-call host decisions)config object UnknownOutcomeConfig + checkpoint UnknownDecision dataconfig, hook, driver
Batch semantics (dedup-then-parallel, per-call panic isolation → Failed, call-deadline backstop → UnknownOutcome)documented opinionexecutor
Session retention (256 works, rejects new work at capacity) and work deadline (none by default)config object SessionConfigsession
Session admission (one active work, no queue), completed-only history and checkpoint eligibilitydocumented component opinionsSession, ConversationState, SessionCheckpoint

This table summarizes the main execution and session policies. Component and configuration docs describe further defaults and constraints; absence from this table does not make a runtime decision a kernel invariant.

Re-exports§

pub use budget::Compaction;
pub use budget::CompactionError;
pub use budget::CompactionInput;
pub use budget::CompactionOutput;
pub use budget::FrameError;
pub use budget::FramePolicy;
pub use budget::TokenCounter;
pub use budget::WindowBudget;
pub use interaction::BatchDecision;
pub use interaction::TurnInteraction;
pub use composition::ToolBridge;
pub use config::ExecutionOptions;
pub use config::NoopInteraction;
pub use config::RetryPolicy;
pub use config::ToolOutputLimits;
pub use config::TurnInvocation;
pub use config::TurnLimits;
pub use config::TurnPolicy;
pub use config::TurnRunOptions;
pub use config::UnknownOutcomeConfig;
pub use config::UnknownOutcomePolicy;
pub use control::RunControl;
pub use conversation::ConversationError;
pub use conversation::ConversationState;
pub use conversation::ConversationStore;
pub use conversation::ConversationStoreError;
pub use conversation::ConversationVersion;
pub use conversation::HistoryEntry;
pub use conversation::SealedResult;
pub use conversation::TurnSequence;
pub use driver::AttemptTrace;
pub use driver::Continuation;
pub use driver::ConversationOutcome;
pub use driver::ModelRoundTrace;
pub use driver::OutputSummary;
pub use driver::PausePoint;
pub use driver::PreparedApproval;
pub use driver::ToolBatchTrace;
pub use driver::ToolCallTrace;
pub use driver::TurnInterruption;
pub use driver::TurnOutcome;
pub use driver::TurnResult;
pub use driver::TurnRunner;
pub use driver::TurnTrace;
pub use executor::ToolExecutor;
pub use executor::ToolRegistryError;
pub use hook::HookCtx;
pub use hook::HookOutcome;
pub use hook::PassthroughHook;
pub use hook::ToolUseHook;
pub use hook::UnknownDecision;
pub use resume::ResumeRejection;
pub use resume::ResumeRequest;
pub use resume::resume_turn;
pub use session::CancelOutcome;
pub use session::CancelReceipt;
pub use session::CheckpointPhase;
pub use session::FinishedKind;
pub use session::SESSION_CHECKPOINT_VERSION;
pub use session::SavedCancelKey;
pub use session::SavedResumeKey;
pub use session::SavedSubmitKey;
pub use session::SavedWork;
pub use session::Session;
pub use session::SessionBuildRejection;
pub use session::SessionCheckpoint;
pub use session::SessionConfig;
pub use session::SessionConfigDescription;
pub use session::SessionError;
pub use session::SessionHandle;
pub use session::SessionRestoreRejection;
pub use session::SubmitRequest;
pub use session::WaitEnd;
pub use session::WaitOutcome;
pub use session::WorkObservation;
pub use session::WorkReceipt;
pub use session::WorkRef;
pub use session::WorkState;
pub use event::ContextEvent;
pub use event::ContextEventKind;
pub use event::StreamEventCollector;
pub use event::project_streaming_turn;
pub use event::project_turn;
pub use filter::AllowAllFilter;
pub use filter::DedupFilter;
pub use filter::DenyAllFilter;
pub use filter::FilterChain;

Modules§

budget
Reference frame-materialization policy — window budget, compaction seam, token counter, and the canonical FramePolicy carrier. These are this component’s budget opinions, not fact-layer invariants. A custom host composes the kernel’s lossless projection (TurnContext::frame) differently.
composition
Composition layer — adapters bridging kernel ports into each other.
config
Run configuration axes — what to ask (TurnInvocation), when the loop gives up (TurnPolicy), how tools execute (ExecutionOptions), and how context is materialized (FramePolicy, canonical carrier in budget). Deliberately four focused units instead of one universal context config.
control
Turn-level run control — the driver’s bundling of the shared cancellation token and the turn deadline. No port signature consumes this type (the gateway takes AttemptControl, tools take CallControl), so it lives here rather than in the kernel’s ports.
conversation
The session aggregate — ConversationState, its ordering vocabulary, and the ConversationStore archive port. The single active slot, completed-only history admission, and commit-time sequence assignment are this component’s decisions, not fact-layer invariants, so they live with the execution stack. The kernel keeps the facts (causa_kernel::TurnContext / causa_kernel::TurnSnapshot), the validated recovery entries, and the shared causa_kernel::merged_frame projection; it never depends back on this crate.
driver
The execution stack here — retry scheduling, tool batch dispatch, artifact spill, control plumbing, and trace construction. The canonical consumer of the kernel’s contracts lives here, one layer up.
event
ContextEvent projection — a turn’s facts projected into a sequence for UI, observability, and audit consumers.
executor
Tool batch dispatch — dedup-then-parallel execution with panic isolation, call-deadline backstop, and token-limit truncation with artifact spill. Also the tool-catalog composition point: static Rust tools and dynamic sources (DynamicToolSource, e.g. MCP servers) merge into one dispatch path; the driver only consumes the assembled surface.
filter
Tool-use filter policies — the framework’s concrete behavior for the tool-use batch between model output and tool dispatch.
hook
The tool-use filter seam.
interaction
TurnInteraction port — the single host↔driver interaction boundary during a turn (batch decisions and steering injection). When it fires is execution-stack policy, so the contract lives with its only consumer (the driver) rather than in the kernel.
resume
resume_turn — the continuation half of a paused turn: both resume entries consume the complete paused outcome plus a request that carries only what is new.
session
One owner per conversation: the session aggregate that accepts work and drives it through the execution stack.