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