Skip to main content

causa_runtime/
session.rs

1//! One owner per conversation: the session aggregate that accepts work and
2//! drives it through the execution stack.
3//!
4//! A [`Session`] owns one
5//! [`ConversationState`](crate::conversation::ConversationState) plus an
6//! already-assembled [`TurnRunner`](crate::driver::TurnRunner) /
7//! [`TurnRunOptions`](crate::config::TurnRunOptions); cloneable
8//! [`SessionHandle`]s submit, observe, wait, resume, and cancel. The session is
9//! the coordination layer around the driver (accept → begin/append → run →
10//! commit/abort/retain → notify); it neither duplicates the model loop nor
11//! exposes a writable
12//! [`ConversationState`](crate::conversation::ConversationState).
13//!
14//! # Observable behaviour
15//!
16//! - One active work, no queue: while a work is `Accepted` / `Running` /
17//!   `Paused`, `submit` returns [`SessionError::Busy`] naming it.
18//! - Stable identity: each work gets a fresh `TurnId` at acceptance, never
19//!   reused, even after an interrupt.
20//! - Local request-key dedup, checked before the busy and closed guards: same
21//!   key and parts returns the original [`WorkReceipt`] (even after the owner
22//!   closed), same key with different parts is [`SessionError::Conflict`], and
23//!   a rejected submit keeps the key free.
24//! - Bounded retention: [`SessionConfig::retained_work_capacity`] bounds the
25//!   registry; finished results stay observable across later works.
26//! - One writable state: it moves into the worker task while a work runs.
27//! - `Completed` commits into history; `Interrupted` keeps the real facts plus
28//!   cause and stays out of history; `Paused` keeps the complete outcome for an
29//!   explicit `resume`. Observations include an independent copy of its
30//!   [`crate::driver::PausePoint`] and the revision needed to decide it.
31//! - A panic, runner rejection, or dropped worker publishes
32//!   [`WorkState::Faulted`] with the reason and refuses new work.
33//! - `resume` continues a paused work after validating the work's paused
34//!   revision and the request; a rejected request executes nothing and leaves
35//!   the paused material untouched. `cancel` fires the work's own control token
36//!   (a racing completion keeps its result, a paused work is terminated in
37//!   place with its continuation retained, and a terminal work is not
38//!   rewritten). `shutdown` stops acceptance, winds down the running work, and
39//!   retains material for inspection.
40//! - Dropping the owner stops acceptance and fires the active work's
41//!   cancellation token; retained observations stay readable.
42//! - `checkpoint` exports a versioned [`SessionCheckpoint`] only from an
43//!   idle or paused session (`Busy` while a work is accepted / running,
44//!   refused once faulted; a closed session still exports, recording
45//!   `closed`). `SessionCheckpoint::restore` validates the version, the
46//!   assembled configuration description, and the material's internal
47//!   consistency before registering — a rejection hands back the original
48//!   envelope and the by-value inputs, and a successful restore calls no
49//!   model or tool. Saved request keys replay their original receipts
50//!   (different arguments still conflict), identity allocation continues
51//!   from the saved progress, and a restored pause still needs an explicit
52//!   `resume` under its remaining — never re-granted — deadline. Opening a
53//!   conversation from history stays a different operation: it rebuilds no
54//!   dedup table.
55//!
56//! Multi-session coordination, materials, and model tools are not here.
57//!
58//! # Runtime
59//!
60//! [`SessionHandle::submit`] spawns the worker on the ambient Tokio runtime.
61//! If that runtime drops the task, even before its first poll, surviving
62//! handles observe `Faulted` and the owner can still shut down. The lost
63//! task's complete execution material cannot be recovered by this mechanism.
64//!
65//! # Layout
66//!
67//! This root file is the index only: `types` holds the public value vocabulary,
68//! `owner` the [`Session`] anchor, `handle` the [`SessionHandle`] entry, the
69//! private `execution` core the registry and worker, and `checkpoint` the
70//! versioned save envelope (`SessionCheckpoint`) its restore consumes.
71//! Convention is `foo.rs` + `foo/`, never `mod.rs`.
72
73mod checkpoint;
74mod execution;
75mod handle;
76mod owner;
77mod types;
78
79pub use checkpoint::{
80    CheckpointPhase, SESSION_CHECKPOINT_VERSION, SavedCancelKey, SavedResumeKey, SavedSubmitKey,
81    SavedWork, SessionCheckpoint, SessionConfigDescription, SessionRestoreRejection,
82};
83pub use handle::SessionHandle;
84pub use owner::{Session, SessionBuildRejection};
85pub use types::{
86    CancelOutcome, CancelReceipt, FinishedKind, SessionConfig, SessionError, SubmitRequest,
87    WaitEnd, WaitOutcome, WorkObservation, WorkReceipt, WorkRef, WorkState,
88};