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    /// Recording a change in the bound tools failed; the edit's inputs are
104    /// retained.
105    #[error("the tool change could not be committed: {error}")]
106    #[non_exhaustive]
107    ToolChangeCommitFailed {
108        /// The logical invocation associated with this stage.
109        invocation_id: InvocationId,
110        /// Original typed failure, including the change block.
111        error: EditFailure,
112    },
113    /// The logical gateway call failed; its original classification is retained.
114    #[error("the model call failed: {error}")]
115    #[non_exhaustive]
116    ModelCallFailed {
117        /// The logical invocation associated with this stage.
118        invocation_id: InvocationId,
119        /// Original typed failure, including any inputs owned by that error.
120        error: ModelInvokeError,
121    },
122    /// Generation reached its token limit; the complete output was not committed.
123    #[error("the model reached its output token limit")]
124    #[non_exhaustive]
125    ModelMaxTokens {
126        /// The logical invocation associated with this stage.
127        invocation_id: InvocationId,
128        /// The complete uncommitted model output, including usage and reasoning.
129        output: ModelOutput,
130    },
131    /// The model refused; the complete output was not committed.
132    #[error("the model refused the request")]
133    #[non_exhaustive]
134    ModelRefusal {
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 stopped on the context window; the complete output was not
141    /// committed.
142    #[error("the request no longer fits the model's context window")]
143    #[non_exhaustive]
144    ModelContextWindowExceeded {
145        /// The logical invocation associated with this stage.
146        invocation_id: InvocationId,
147        /// The complete uncommitted model output, including usage and reasoning.
148        output: ModelOutput,
149    },
150    /// The provider reported a stop value the kernel has no meaning for; the
151    /// complete output was not committed. The wire value is on
152    /// `output.raw_stop_reason` and is kept out of this message, because it
153    /// is provider text of any length and content.
154    #[error("the provider stopped for a reason this version does not recognize")]
155    #[non_exhaustive]
156    ModelStopUnrecognized {
157        /// The logical invocation associated with this stage.
158        invocation_id: InvocationId,
159        /// The complete uncommitted model output, including usage and reasoning.
160        output: ModelOutput,
161    },
162    /// Pure conversion failed before commit, retaining output and supplied IDs.
163    #[error("the model output could not be converted to context blocks: {error}")]
164    #[non_exhaustive]
165    ModelConversionFailed {
166        /// The logical invocation associated with this stage.
167        invocation_id: InvocationId,
168        /// Original typed failure, including any inputs owned by that error.
169        error: ModelBlockError,
170        /// The complete uncommitted model output, including usage and reasoning.
171        output: ModelOutput,
172        /// All IDs supplied to the failed pure conversion.
173        block_ids: Vec<BlockId>,
174    },
175    /// Fresh batch construction failed before the converted blocks were committed.
176    #[error("the model's tool calls could not form a batch: {error}")]
177    #[non_exhaustive]
178    ModelBatchFailed {
179        /// The logical invocation associated with this stage.
180        invocation_id: InvocationId,
181        /// Original typed failure, including any inputs owned by that error.
182        error: BatchError,
183        /// The complete uncommitted model output, including usage and reasoning.
184        output: ModelOutput,
185        /// All converted blocks rejected before model commit.
186        blocks: Vec<ContextBlock>,
187    },
188    /// Atomic model append failed; the edit error retains every submitted block.
189    #[error("the model output could not be committed: {error}")]
190    #[non_exhaustive]
191    ModelCommitFailed {
192        /// The logical invocation associated with this stage.
193        invocation_id: InvocationId,
194        /// Original typed failure, including any inputs owned by that error.
195        error: EditFailure,
196        /// The complete uncommitted model output, including usage and reasoning.
197        output: ModelOutput,
198    },
199    /// Tool processing failed; the complete current batch is returned in the outcome.
200    #[error("tool processing failed: {error}")]
201    #[non_exhaustive]
202    ToolProcessingFailed {
203        /// The logical invocation associated with this stage.
204        invocation_id: InvocationId,
205        /// Original typed failure, including any inputs owned by that error.
206        error: ToolProcessingError,
207    },
208    /// Result scope validation failed; the complete batch remains uncommitted.
209    #[error("tool results failed validation: {error}")]
210    #[non_exhaustive]
211    ToolResultValidationFailed {
212        /// The logical invocation associated with this stage.
213        invocation_id: InvocationId,
214        /// Original typed failure, including any inputs owned by that error.
215        error: ToolResultError,
216    },
217    /// Atomic tool-result append failed; both the batch and edit inputs are retained.
218    #[error("tool results could not be committed: {error}")]
219    #[non_exhaustive]
220    ToolCommitFailed {
221        /// The logical invocation associated with this stage.
222        invocation_id: InvocationId,
223        /// Original typed failure, including any inputs owned by that error.
224        error: EditFailure,
225    },
226    /// The full result batch committed, then execution stopped on an unknown result.
227    #[error("a tool call ended with an unknown outcome")]
228    #[non_exhaustive]
229    UnknownToolOutcome {
230        /// The logical invocation associated with this stage.
231        invocation_id: InvocationId,
232        /// Declaration of the first unknown result in final postprocessor order.
233        call_block_id: BlockId,
234    },
235}