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)]
9pub struct TurnOutcome {
10    /// Identity supplied by the caller.
11    pub turn_id: TurnId,
12    /// All successfully committed materials; still editable.
13    pub context: Context,
14    /// The selected terminal result.
15    pub result: TurnResult,
16    /// Complete current batch whose declarations, but not results, were committed.
17    pub uncommitted_tool_batch: Option<ToolBatch>,
18}
19/// The selected execution result, without an implicit execution history.
20#[derive(Debug)]
21pub enum TurnResult {
22    /// A final model output was committed.
23    Completed {
24        /// Last complete output, including usage and reasoning.
25        final_output: ModelOutput,
26    },
27    /// Controlled termination with all committed materials returned.
28    Interrupted {
29        /// The precise stage failure or control reason.
30        cause: TurnInterruption,
31    },
32}
33/// Typed interruption causes retaining the relevant failure inputs.
34#[derive(Debug)]
35pub enum TurnInterruption {
36    /// Caller cancellation stopped an unselected execution stage.
37    Cancelled {
38        /// Logical invocation, or none when stopping between invocations.
39        invocation_id: Option<InvocationId>,
40    },
41    /// The inherited absolute execution deadline stopped a stage.
42    DeadlineExceeded {
43        /// Logical invocation, or none when stopping between invocations.
44        invocation_id: Option<InvocationId>,
45    },
46    /// The logical invocation limit was reached before creating a new invocation.
47    MaxModelRounds {
48        /// Configured maximum for this execution.
49        limit: u32,
50    },
51    /// New declarations committed, but their cumulative count exceeds the limit.
52    MaxToolCalls {
53        /// The logical invocation associated with this stage.
54        invocation_id: InvocationId,
55        /// Configured maximum for this execution.
56        limit: u32,
57        /// Number of declarations accepted in earlier invocations.
58        declared_before: u32,
59        /// Untruncated number of newly committed declarations.
60        declared_this_round: usize,
61    },
62    /// Tool binding failed before preparation or model invocation.
63    ToolCatalogFailed {
64        /// The logical invocation associated with this stage.
65        invocation_id: InvocationId,
66        /// Original typed failure, including any inputs owned by that error.
67        error: ToolCatalogError,
68    },
69    /// Preparation failed; any edits already committed by the preparer remain.
70    PrepareFailed {
71        /// The logical invocation associated with this stage.
72        invocation_id: InvocationId,
73        /// Original typed failure, including any inputs owned by that error.
74        error: PrepareError,
75    },
76    /// The logical gateway call failed; its original classification is retained.
77    ModelCallFailed {
78        /// The logical invocation associated with this stage.
79        invocation_id: InvocationId,
80        /// Original typed failure, including any inputs owned by that error.
81        error: ModelInvokeError,
82    },
83    /// Generation reached its token limit; the complete output was not committed.
84    ModelMaxTokens {
85        /// The logical invocation associated with this stage.
86        invocation_id: InvocationId,
87        /// The complete uncommitted model output, including usage and reasoning.
88        output: ModelOutput,
89    },
90    /// The model refused; the complete output was not committed.
91    ModelRefusal {
92        /// The logical invocation associated with this stage.
93        invocation_id: InvocationId,
94        /// The complete uncommitted model output, including usage and reasoning.
95        output: ModelOutput,
96    },
97    /// Pure conversion failed before commit, retaining output and supplied IDs.
98    ModelConversionFailed {
99        /// The logical invocation associated with this stage.
100        invocation_id: InvocationId,
101        /// Original typed failure, including any inputs owned by that error.
102        error: ModelBlockError,
103        /// The complete uncommitted model output, including usage and reasoning.
104        output: ModelOutput,
105        /// All IDs supplied to the failed pure conversion.
106        block_ids: Vec<BlockId>,
107    },
108    /// Fresh batch construction failed before the converted blocks were committed.
109    ModelBatchFailed {
110        /// The logical invocation associated with this stage.
111        invocation_id: InvocationId,
112        /// Original typed failure, including any inputs owned by that error.
113        error: BatchError,
114        /// The complete uncommitted model output, including usage and reasoning.
115        output: ModelOutput,
116        /// All converted blocks rejected before model commit.
117        blocks: Vec<ContextBlock>,
118    },
119    /// Atomic model append failed; the edit error retains every submitted block.
120    ModelCommitFailed {
121        /// The logical invocation associated with this stage.
122        invocation_id: InvocationId,
123        /// Original typed failure, including any inputs owned by that error.
124        error: EditFailure,
125        /// The complete uncommitted model output, including usage and reasoning.
126        output: ModelOutput,
127    },
128    /// Tool processing failed; the complete current batch is returned in the outcome.
129    ToolProcessingFailed {
130        /// The logical invocation associated with this stage.
131        invocation_id: InvocationId,
132        /// Original typed failure, including any inputs owned by that error.
133        error: ToolProcessingError,
134    },
135    /// Result scope validation failed; the complete batch remains uncommitted.
136    ToolResultValidationFailed {
137        /// The logical invocation associated with this stage.
138        invocation_id: InvocationId,
139        /// Original typed failure, including any inputs owned by that error.
140        error: ToolResultError,
141    },
142    /// Atomic tool-result append failed; both the batch and edit inputs are retained.
143    ToolCommitFailed {
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: EditFailure,
148    },
149    /// The full result batch committed, then execution stopped on an unknown result.
150    UnknownToolOutcome {
151        /// The logical invocation associated with this stage.
152        invocation_id: InvocationId,
153        /// Declaration of the first unknown result in final postprocessor order.
154        call_block_id: BlockId,
155    },
156}
157impl std::fmt::Display for TurnInterruption {
158    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
159        write!(f, "{self:?}")
160    }
161}