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    Cancelled {
52        /// Logical invocation, or none when stopping between invocations.
53        invocation_id: Option<InvocationId>,
54    },
55    /// The inherited absolute execution deadline stopped a stage.
56    #[error("the run's deadline passed")]
57    DeadlineExceeded {
58        /// Logical invocation, or none when stopping between invocations.
59        invocation_id: Option<InvocationId>,
60    },
61    /// The logical invocation limit was reached before creating a new invocation.
62    #[error("the limit of {limit} model rounds was reached")]
63    MaxModelRounds {
64        /// Configured maximum for this execution.
65        limit: u32,
66    },
67    /// New declarations committed, but their cumulative count exceeds the limit.
68    #[error(
69        "the limit of {limit} tool calls was exceeded: {declared_before} declared earlier, {declared_this_round} in this round"
70    )]
71    MaxToolCalls {
72        /// The logical invocation associated with this stage.
73        invocation_id: InvocationId,
74        /// Configured maximum for this execution.
75        limit: u32,
76        /// Number of declarations accepted in earlier invocations.
77        declared_before: u32,
78        /// Untruncated number of newly committed declarations.
79        declared_this_round: usize,
80    },
81    /// Tool binding failed before preparation or model invocation.
82    #[error("tool binding failed: {error}")]
83    ToolCatalogFailed {
84        /// The logical invocation associated with this stage.
85        invocation_id: InvocationId,
86        /// Original typed failure, including any inputs owned by that error.
87        error: ToolCatalogError,
88    },
89    /// Preparation failed; any edits already committed by the preparer remain.
90    #[error("context preparation failed: {error}")]
91    PrepareFailed {
92        /// The logical invocation associated with this stage.
93        invocation_id: InvocationId,
94        /// Original typed failure, including any inputs owned by that error.
95        error: PrepareError,
96    },
97    /// The logical gateway call failed; its original classification is retained.
98    #[error("the model call failed: {error}")]
99    ModelCallFailed {
100        /// The logical invocation associated with this stage.
101        invocation_id: InvocationId,
102        /// Original typed failure, including any inputs owned by that error.
103        error: ModelInvokeError,
104    },
105    /// Generation reached its token limit; the complete output was not committed.
106    #[error("the model reached its output token limit")]
107    ModelMaxTokens {
108        /// The logical invocation associated with this stage.
109        invocation_id: InvocationId,
110        /// The complete uncommitted model output, including usage and reasoning.
111        output: ModelOutput,
112    },
113    /// The model refused; the complete output was not committed.
114    #[error("the model refused the request")]
115    ModelRefusal {
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 provider stopped on the context window; the complete output was not
122    /// committed.
123    #[error("the request no longer fits the model's context window")]
124    ModelContextWindowExceeded {
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 reported a stop value the kernel has no meaning for; the
131    /// complete output was not committed. The wire value is on
132    /// `output.raw_stop_reason` and is kept out of this message, because it
133    /// is provider text of any length and content.
134    #[error("the provider stopped for a reason this version does not recognize")]
135    ModelStopUnrecognized {
136        /// The logical invocation associated with this stage.
137        invocation_id: InvocationId,
138        /// The complete uncommitted model output, including usage and reasoning.
139        output: ModelOutput,
140    },
141    /// Pure conversion failed before commit, retaining output and supplied IDs.
142    #[error("the model output could not be converted to context blocks: {error}")]
143    ModelConversionFailed {
144        /// The logical invocation associated with this stage.
145        invocation_id: InvocationId,
146        /// Original typed failure, including any inputs owned by that error.
147        error: ModelBlockError,
148        /// The complete uncommitted model output, including usage and reasoning.
149        output: ModelOutput,
150        /// All IDs supplied to the failed pure conversion.
151        block_ids: Vec<BlockId>,
152    },
153    /// Fresh batch construction failed before the converted blocks were committed.
154    #[error("the model's tool calls could not form a batch: {error}")]
155    ModelBatchFailed {
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: BatchError,
160        /// The complete uncommitted model output, including usage and reasoning.
161        output: ModelOutput,
162        /// All converted blocks rejected before model commit.
163        blocks: Vec<ContextBlock>,
164    },
165    /// Atomic model append failed; the edit error retains every submitted block.
166    #[error("the model output could not be committed: {error}")]
167    ModelCommitFailed {
168        /// The logical invocation associated with this stage.
169        invocation_id: InvocationId,
170        /// Original typed failure, including any inputs owned by that error.
171        error: EditFailure,
172        /// The complete uncommitted model output, including usage and reasoning.
173        output: ModelOutput,
174    },
175    /// Tool processing failed; the complete current batch is returned in the outcome.
176    #[error("tool processing failed: {error}")]
177    ToolProcessingFailed {
178        /// The logical invocation associated with this stage.
179        invocation_id: InvocationId,
180        /// Original typed failure, including any inputs owned by that error.
181        error: ToolProcessingError,
182    },
183    /// Result scope validation failed; the complete batch remains uncommitted.
184    #[error("tool results failed validation: {error}")]
185    ToolResultValidationFailed {
186        /// The logical invocation associated with this stage.
187        invocation_id: InvocationId,
188        /// Original typed failure, including any inputs owned by that error.
189        error: ToolResultError,
190    },
191    /// Atomic tool-result append failed; both the batch and edit inputs are retained.
192    #[error("tool results could not be committed: {error}")]
193    ToolCommitFailed {
194        /// The logical invocation associated with this stage.
195        invocation_id: InvocationId,
196        /// Original typed failure, including any inputs owned by that error.
197        error: EditFailure,
198    },
199    /// The full result batch committed, then execution stopped on an unknown result.
200    #[error("a tool call ended with an unknown outcome")]
201    UnknownToolOutcome {
202        /// The logical invocation associated with this stage.
203        invocation_id: InvocationId,
204        /// Declaration of the first unknown result in final postprocessor order.
205        call_block_id: BlockId,
206    },
207}