Skip to main content

causa_runtime/
interaction.rs

1//! `TurnInteraction` port — the single host↔driver interaction boundary
2//! during a turn (batch decisions and steering injection). When it fires is
3//! execution-stack policy, so the contract lives with its only consumer
4//! (the driver) rather than in the kernel.
5//!
6//! One port with default no-ops replaces per-entry callback channels:
7//! entry signatures never grow interaction parameters, and new
8//! interactions (progress, per-call approval) arrive as new default
9//! methods without breaking third-party implementors. The driver consumes
10//! exactly one of these per turn via `TurnRunOptions.interaction`
11//! (`config`); the noop default is [`crate::config::NoopInteraction`],
12//! which stays with the config axes.
13
14use async_trait::async_trait;
15
16use crate::hook::UnknownDecision;
17use causa_kernel::{RoundId, StreamDelta, TextPayload, ToolCallPayload, ToolResultPayload};
18
19/// Host observations and decisions while a turn is in flight.
20#[async_trait]
21pub trait TurnInteraction: Send + Sync {
22    /// Streaming-delta observation. Called once per provider delta in the
23    /// model phase, with the round the delta belongs to (round attribution
24    /// is driver state — the port carries it so hosts never have to guess
25    /// round boundaries). Deltas observed here are advisory: a retried
26    /// attempt re-streams the same frame and the host decides how to
27    /// present the partial-then-reset flow.
28    async fn on_delta(&self, _round_id: RoundId, _delta: &StreamDelta) {}
29
30    /// Batch decision gate: called after the tool-use hook has
31    /// filtered the model-emitted batch and before executor dispatch.
32    /// Default [`BatchDecision::Proceed`] — the literal absence of
33    /// opinion. Returning [`BatchDecision::Pause`] suspends the turn
34    /// ([`crate::driver::TurnResult::Paused`], context left open) until
35    /// the host resumes it through [`crate::resume::resume_turn`] with
36    /// the withheld [`crate::hook::HookOutcome`] — approve is
37    /// passthrough, reject is all-rejected, rewrite is the edited batch;
38    /// the HookOutcome constructors cover every decision, so no separate
39    /// resume-decision enum exists.
40    async fn decide_batch(&self, _calls: &[ToolCallPayload]) -> BatchDecision {
41        BatchDecision::Proceed
42    }
43
44    /// Steering inputs, pulled by the driver at every round
45    /// boundary before the frame materializes; each non-empty entry is
46    /// appended to the active turn with the `user.steering` source label
47    /// and the next model round sees it. Default: empty (zero-cost
48    /// pull). The queue itself is host vocabulary — the runtime holds no
49    /// channel type.
50    async fn pending_inputs(&self) -> Vec<TextPayload> {
51        Vec::new()
52    }
53}
54
55/// The driver's action for a tool-use batch.
56#[derive(Debug, Clone)]
57pub enum BatchDecision {
58    /// Dispatch the batch to the executor unchanged.
59    Proceed,
60    /// Skip execution entirely; the supplied results become the tool
61    /// results (the host supplies the error copy). Result `call_id`s
62    /// must pair with the batch — the fact machine rejects unpaired
63    /// results. Despite the variant's name, any recorded status is
64    /// accepted; a precomputed `UnknownOutcome` result takes its action
65    /// from `unknown_decisions` (or the runner's unknown-outcome
66    /// configuration when omitted).
67    Reject {
68        /// Results standing in for the skipped batch; their `call_id`s must
69        /// pair with the batch.
70        results: Vec<ToolResultPayload>,
71        /// Explicit unknown-outcome actions for precomputed results whose
72        /// status is `UnknownOutcome` (see [`UnknownDecision`]); entries
73        /// may be omitted.
74        unknown_decisions: Vec<UnknownDecision>,
75    },
76    /// Execute the rewritten payloads instead of the model-emitted ones.
77    /// Call ids are expected to be preserved so results pair with the
78    /// committed tool-call blocks.
79    Rewrite(Vec<ToolCallPayload>),
80    /// Suspend the turn: context stays open, the batch is neither
81    /// executed nor rejected, and the outcome carries
82    /// [`crate::driver::TurnResult::Paused`]. `deadline` is the advisory
83    /// decision budget the host grants itself (remaining time;
84    /// host-side anchoring).
85    Pause {
86        /// Advisory remaining decision budget the host grants itself
87        /// (host-side anchoring).
88        deadline: Option<std::time::Duration>,
89    },
90}