Skip to main content

causa_runtime/driver/
outcome.rs

1use crate::executor::{ToolCatalogError, ToolProcessingError};
2use causa_kernel::{
3    BatchError, BlockId, Context, ContextBlock, EditFailure, InvocationId, ModelBlockError,
4    ModelInvokeError, ModelOutput, PrepareError, ToolBatch, ToolResultError, TurnId,
5};
6
7/// Materials returned on completion or controlled interruption.
8#[derive(Debug)]
9#[non_exhaustive]
10pub struct TurnOutcome {
11    /// Identity supplied by the caller.
12    pub turn_id: TurnId,
13    /// All successfully committed materials; still editable.
14    pub context: Context,
15    /// The selected terminal result.
16    pub result: TurnResult,
17    /// Complete current batch whose declarations, but not results, were committed.
18    ///
19    /// Present when the run stopped before the batch finished processing, or
20    /// when its results failed validation or the commit. A batch whose tools
21    /// all returned and whose after processors all ran is committed even if a
22    /// cancellation or deadline is seen at that point, and is not returned here.
23    pub uncommitted_tool_batch: Option<ToolBatch>,
24}
25/// The selected execution result, without an implicit execution history.
26///
27/// Exhaustive on purpose: a run completes or is interrupted, and a host
28/// handles both.
29#[derive(Debug)]
30pub enum TurnResult {
31    /// A final model output was committed.
32    Completed {
33        /// Last complete output, including usage and reasoning.
34        final_output: ModelOutput,
35    },
36    /// Controlled termination with all committed materials returned.
37    Interrupted {
38        /// The precise stage failure or control reason.
39        cause: TurnInterruption,
40    },
41}
42/// Typed interruption causes retaining the relevant failure inputs.
43///
44/// `Display` gives one sentence a host can show. More causes may be added; a
45/// match needs a wildcard arm.
46#[derive(Debug, thiserror::Error)]
47#[non_exhaustive]
48pub enum TurnInterruption {
49    /// Caller cancellation stopped an unselected execution stage.
50    #[error("the run was cancelled")]
51    #[non_exhaustive]
52    Cancelled {
53        /// Logical invocation, or none when stopping between invocations.
54        invocation_id: Option<InvocationId>,
55    },
56    /// The inherited absolute execution deadline stopped a stage.
57    #[error("the run's deadline passed")]
58    #[non_exhaustive]
59    DeadlineExceeded {
60        /// Logical invocation, or none when stopping between invocations.
61        invocation_id: Option<InvocationId>,
62    },
63    /// The logical invocation limit was reached before creating a new invocation.
64    #[error("the limit of {limit} model rounds was reached")]
65    #[non_exhaustive]
66    MaxModelRounds {
67        /// Configured maximum for this execution.
68        limit: u32,
69    },
70    /// New declarations committed, but their cumulative count exceeds the limit.
71    #[error(
72        "the limit of {limit} tool calls was exceeded: {declared_before} declared earlier, {declared_this_round} in this round"
73    )]
74    #[non_exhaustive]
75    MaxToolCalls {
76        /// The logical invocation associated with this stage.
77        invocation_id: InvocationId,
78        /// Configured maximum for this execution.
79        limit: u32,
80        /// Number of declarations accepted in earlier invocations.
81        declared_before: u32,
82        /// Untruncated number of newly committed declarations.
83        declared_this_round: usize,
84    },
85    /// Tool binding failed before preparation or model invocation.
86    #[error("tool binding failed: {error}")]
87    #[non_exhaustive]
88    ToolCatalogFailed {
89        /// The logical invocation associated with this stage.
90        invocation_id: InvocationId,
91        /// Original typed failure, including any inputs owned by that error.
92        error: ToolCatalogError,
93    },
94    /// Preparation failed; any edits already committed by the preparer remain.
95    #[error("context preparation failed: {error}")]
96    #[non_exhaustive]
97    PrepareFailed {
98        /// The logical invocation associated with this stage.
99        invocation_id: InvocationId,
100        /// Original typed failure, including any inputs owned by that error.
101        error: PrepareError,
102    },
103    /// The logical gateway call failed; its original classification is retained.
104    #[error("the model call failed: {error}")]
105    #[non_exhaustive]
106    ModelCallFailed {
107        /// The logical invocation associated with this stage.
108        invocation_id: InvocationId,
109        /// Original typed failure, including any inputs owned by that error.
110        error: ModelInvokeError,
111    },
112    /// Generation reached its token limit; the complete output was not committed.
113    #[error("the model reached its output token limit")]
114    #[non_exhaustive]
115    ModelMaxTokens {
116        /// The logical invocation associated with this stage.
117        invocation_id: InvocationId,
118        /// The complete uncommitted model output, including usage and reasoning.
119        output: ModelOutput,
120    },
121    /// The model refused; the complete output was not committed.
122    #[error("the model refused the request")]
123    #[non_exhaustive]
124    ModelRefusal {
125        /// The logical invocation associated with this stage.
126        invocation_id: InvocationId,
127        /// The complete uncommitted model output, including usage and reasoning.
128        output: ModelOutput,
129    },
130    /// The provider stopped on the context window; the complete output was not
131    /// committed.
132    #[error("the request no longer fits the model's context window")]
133    #[non_exhaustive]
134    ModelContextWindowExceeded {
135        /// The logical invocation associated with this stage.
136        invocation_id: InvocationId,
137        /// The complete uncommitted model output, including usage and reasoning.
138        output: ModelOutput,
139    },
140    /// The provider reported a stop value the kernel has no meaning for; the
141    /// complete output was not committed. The wire value is on
142    /// `output.raw_stop_reason` and is kept out of this message, because it
143    /// is provider text of any length and content.
144    #[error("the provider stopped for a reason this version does not recognize")]
145    #[non_exhaustive]
146    ModelStopUnrecognized {
147        /// The logical invocation associated with this stage.
148        invocation_id: InvocationId,
149        /// The complete uncommitted model output, including usage and reasoning.
150        output: ModelOutput,
151    },
152    /// Pure conversion failed before commit, retaining output and supplied IDs.
153    #[error("the model output could not be converted to context blocks: {error}")]
154    #[non_exhaustive]
155    ModelConversionFailed {
156        /// The logical invocation associated with this stage.
157        invocation_id: InvocationId,
158        /// Original typed failure, including any inputs owned by that error.
159        error: ModelBlockError,
160        /// The complete uncommitted model output, including usage and reasoning.
161        output: ModelOutput,
162        /// All IDs supplied to the failed pure conversion.
163        block_ids: Vec<BlockId>,
164    },
165    /// Fresh batch construction failed before the converted blocks were committed.
166    #[error("the model's tool calls could not form a batch: {error}")]
167    #[non_exhaustive]
168    ModelBatchFailed {
169        /// The logical invocation associated with this stage.
170        invocation_id: InvocationId,
171        /// Original typed failure, including any inputs owned by that error.
172        error: BatchError,
173        /// The complete uncommitted model output, including usage and reasoning.
174        output: ModelOutput,
175        /// All converted blocks rejected before model commit.
176        blocks: Vec<ContextBlock>,
177    },
178    /// Atomic model append failed; the edit error retains every submitted block.
179    #[error("the model output could not be committed: {error}")]
180    #[non_exhaustive]
181    ModelCommitFailed {
182        /// The logical invocation associated with this stage.
183        invocation_id: InvocationId,
184        /// Original typed failure, including any inputs owned by that error.
185        error: EditFailure,
186        /// The complete uncommitted model output, including usage and reasoning.
187        output: ModelOutput,
188    },
189    /// Tool processing failed; the complete current batch is returned in the outcome.
190    #[error("tool processing failed: {error}")]
191    #[non_exhaustive]
192    ToolProcessingFailed {
193        /// The logical invocation associated with this stage.
194        invocation_id: InvocationId,
195        /// Original typed failure, including any inputs owned by that error.
196        error: ToolProcessingError,
197    },
198    /// Result scope validation failed; the complete batch remains uncommitted.
199    #[error("tool results failed validation: {error}")]
200    #[non_exhaustive]
201    ToolResultValidationFailed {
202        /// The logical invocation associated with this stage.
203        invocation_id: InvocationId,
204        /// Original typed failure, including any inputs owned by that error.
205        error: ToolResultError,
206    },
207    /// Atomic tool-result append failed; both the batch and edit inputs are retained.
208    #[error("tool results could not be committed: {error}")]
209    #[non_exhaustive]
210    ToolCommitFailed {
211        /// The logical invocation associated with this stage.
212        invocation_id: InvocationId,
213        /// Original typed failure, including any inputs owned by that error.
214        error: EditFailure,
215    },
216    /// The full result batch committed, then execution stopped on an unknown result.
217    #[error("a tool call ended with an unknown outcome")]
218    #[non_exhaustive]
219    UnknownToolOutcome {
220        /// The logical invocation associated with this stage.
221        invocation_id: InvocationId,
222        /// Declaration of the first unknown result in final postprocessor order.
223        call_block_id: BlockId,
224    },
225}