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
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)]
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.
pub uncommitted_tool_batch: Option<ToolBatch>,
}
/// The selected execution result, without an implicit execution history.
#[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.
#[derive(Debug)]
pub enum TurnInterruption {
/// Caller cancellation stopped an unselected execution stage.
Cancelled {
/// Logical invocation, or none when stopping between invocations.
invocation_id: Option<InvocationId>,
},
/// The inherited absolute execution deadline stopped a stage.
DeadlineExceeded {
/// Logical invocation, or none when stopping between invocations.
invocation_id: Option<InvocationId>,
},
/// The logical invocation limit was reached before creating a new invocation.
MaxModelRounds {
/// Configured maximum for this execution.
limit: u32,
},
/// New declarations committed, but their cumulative count exceeds the limit.
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.
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.
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.
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.
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.
ModelRefusal {
/// 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.
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.
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.
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.
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.
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.
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.
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,
},
}
impl std::fmt::Display for TurnInterruption {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
}
}