1use crate::ToolActionKind;
8use crate::checkpoint::CheckpointError;
9use crate::compaction::CompactionError;
10use crate::context::ContextError;
11use crate::session_store::SessionStoreError;
12use merry_core::{CoreError, SessionId, ToolCallBatchId, ToolCallId, ToolName, ToolOutputKind};
13use thiserror::Error;
14
15#[derive(Debug, Error)]
21pub enum RuntimeError {
22 #[error("runtime step already active for session {session_id}")]
24 StepAlreadyActive {
25 session_id: SessionId,
27 },
28
29 #[error("invalid step input: {reason}")]
31 InvalidStepInput {
32 reason: &'static str,
34 },
35
36 #[error("invalid agent loop configuration: {source}")]
39 AgentLoopConfig {
40 #[source]
42 source: crate::agent_loop::AgentLoopConfigError,
43 },
44
45 #[error("child runtime construction failed: {message}")]
47 ChildRuntimeBuild {
48 message: String,
50 },
51
52 #[error("{operation} requires at least one agent id")]
54 InvalidSubagentSelection {
55 operation: &'static str,
57 },
58
59 #[error("plan error: {source}")]
61 Plan {
62 #[from]
64 source: crate::PlanError,
65 },
66
67 #[error("invalid user image input: {reason}")]
69 InvalidUserImageInput {
70 reason: String,
72 },
73
74 #[error("tool call {call_id} is not pending in session {session_id}")]
76 UnknownToolCall {
77 session_id: SessionId,
79 call_id: ToolCallId,
81 },
82
83 #[error("bridge tool result batch is empty in session {session_id}")]
85 BridgeToolResultBatchEmpty {
86 session_id: SessionId,
88 },
89
90 #[error(
92 "bridge tool result batch does not match pending calls in session {session_id}: expected {expected_call_ids:?}, received {received_call_ids:?}"
93 )]
94 BridgeToolResultBatchMismatch {
95 session_id: SessionId,
97 expected_call_ids: Vec<ToolCallId>,
99 received_call_ids: Vec<ToolCallId>,
101 },
102
103 #[error(
105 "bridge tool result batch id does not match the pending batch in session {session_id}: expected {expected_batch_id}, received {received_batch_id}"
106 )]
107 BridgeToolResultBatchIdMismatch {
108 session_id: SessionId,
110 expected_batch_id: ToolCallBatchId,
112 received_batch_id: ToolCallBatchId,
114 },
115
116 #[error(
119 "bridge tool result was rejected in session {session_id}; the tool call was recorded as failed: {message}"
120 )]
121 BridgeToolResultRejected {
122 session_id: SessionId,
124 message: String,
126 },
127
128 #[error(
130 "agent run has unresolved tool invocations in session {session_id} for batch {batch_id}"
131 )]
132 AgentRunToolInvocationsPending {
133 session_id: SessionId,
135 batch_id: ToolCallBatchId,
137 },
138
139 #[error("agent run has no pending tool invocations in session {session_id}")]
141 NoPendingAgentRunToolInvocations {
142 session_id: SessionId,
144 },
145
146 #[error("tool call {call_id} is already resolved in session {session_id}")]
148 ToolCallAlreadyResolved {
149 session_id: SessionId,
151 call_id: ToolCallId,
153 },
154
155 #[error("tool {name} is already registered")]
157 DuplicateToolRegistration {
158 name: ToolName,
160 },
161
162 #[error("tool name {name} is reserved by the runtime")]
164 ReservedToolName {
165 name: ToolName,
167 },
168
169 #[error("tool {name} input schema is invalid: {message}")]
171 InvalidToolInputSchema {
172 name: ToolName,
174 message: String,
176 },
177
178 #[error("bridge tool {name} requires explicit bridge tool opt-in")]
180 BridgeToolsNotAllowed {
181 name: ToolName,
183 },
184
185 #[error("tool call {call_id} execution was cancelled in session {session_id}")]
187 ToolExecutionCancelled {
188 session_id: SessionId,
190 call_id: ToolCallId,
192 },
193
194 #[error("tool call {call_id} executor failed in session {session_id}: {message}")]
196 ToolExecutionFailed {
197 session_id: SessionId,
199 call_id: ToolCallId,
201 message: String,
203 },
204
205 #[error("agent run closed for session {session_id}: {message}")]
207 AgentRunClosed {
208 session_id: SessionId,
210 message: &'static str,
212 },
213
214 #[error(
216 "tool call {call_id} admitted for {action_kind:?} in session {session_id} succeeded without required execution evidence"
217 )]
218 MissingActionExecutionEvidence {
219 session_id: SessionId,
221 call_id: ToolCallId,
223 action_kind: ToolActionKind,
225 },
226
227 #[error(
229 "mutating tool action {action_kind:?} for tool call {call_id} in session {session_id} requires an explicit commit lifecycle before generic execution"
230 )]
231 MutatingActionCommitLifecycleRequired {
232 session_id: SessionId,
234 call_id: ToolCallId,
236 action_kind: ToolActionKind,
238 },
239
240 #[error("tool call {call_id} result uses invalid {output_kind:?} output content")]
242 UnsupportedToolResultContent {
243 call_id: ToolCallId,
245 output_kind: ToolOutputKind,
247 },
248
249 #[error("transcript item id space is exhausted")]
251 TranscriptItemIdExhausted,
252
253 #[error("model turn id space is exhausted")]
255 ModelTurnIdExhausted,
256
257 #[error("model turn {model_turn_id} is not recorded")]
259 UnknownModelTurn { model_turn_id: u64 },
260
261 #[error("model turn {model_turn_id} cannot {attempted} from its current status")]
263 InvalidModelTurnTransition {
264 model_turn_id: u64,
265 attempted: &'static str,
266 },
267
268 #[error("tool call {call_id} has no originating transcript item")]
270 TranscriptToolCallMissing { call_id: ToolCallId },
271
272 #[error("core protocol error while constructing runtime state: {source}")]
274 Core {
275 #[from]
277 source: CoreError,
278 },
279
280 #[error("context state error: {source}")]
282 Context {
283 #[from]
285 source: ContextError,
286 },
287
288 #[error("checkpoint state error: {source}")]
290 Checkpoint {
291 #[from]
293 source: CheckpointError,
294 },
295
296 #[error("compaction state error: {source}")]
298 Compaction {
299 #[from]
301 source: CompactionError,
302 },
303
304 #[error("session store error: {source}")]
306 SessionStore {
307 #[from]
309 source: SessionStoreError,
310 },
311
312 #[error("missing model provider for runtime role {role}")]
314 MissingModelProvider {
315 role: &'static str,
317 },
318
319 #[error("compaction model request error: {message}")]
321 CompactionModelRequest {
322 message: String,
324 },
325
326 #[error(
328 "compaction model input window {compactor_window_tokens} tokens is smaller than primary context window {primary_window_tokens} tokens"
329 )]
330 CompactionModelWindowTooSmall {
331 primary_window_tokens: u64,
333 compactor_window_tokens: u64,
335 },
336
337 #[error(
339 "compaction model request estimated input {estimated_input_tokens} tokens plus max output {max_output_tokens} tokens exceeds compaction model window {compactor_window_tokens} tokens"
340 )]
341 CompactionModelRequestTooLarge {
342 estimated_input_tokens: u64,
344 max_output_tokens: u64,
346 compactor_window_tokens: u64,
348 },
349
350 #[error("compaction model output was truncated: {message}")]
352 CompactionModelTruncated {
353 message: String,
355 },
356
357 #[error("compaction model setup error: {message}")]
359 CompactionModelSetup {
360 message: String,
362 },
363
364 #[error("compaction model stream error: {message}")]
366 CompactionModelStream {
367 message: String,
369 },
370}
371
372impl RuntimeError {
373 #[must_use]
375 pub const fn diagnostic_code(&self) -> &'static str {
376 match self {
377 Self::StepAlreadyActive { .. } => "step_already_active",
378 Self::InvalidStepInput { .. } => "invalid_step_input",
379 Self::AgentLoopConfig { .. } => "agent_loop_config",
380 Self::ChildRuntimeBuild { .. } => "child_runtime_build",
381 Self::InvalidSubagentSelection { .. } => "invalid_subagent_selection",
382 Self::Plan { .. } => "plan_error",
383 Self::InvalidUserImageInput { .. } => "invalid_user_image_input",
384 Self::UnknownToolCall { .. } => "unknown_tool_call",
385 Self::BridgeToolResultBatchEmpty { .. } => "bridge_tool_result_batch_empty",
386 Self::BridgeToolResultBatchMismatch { .. } => "bridge_tool_result_batch_mismatch",
387 Self::BridgeToolResultBatchIdMismatch { .. } => "bridge_tool_result_batch_id_mismatch",
388 Self::BridgeToolResultRejected { .. } => "bridge_tool_result_rejected",
389 Self::AgentRunToolInvocationsPending { .. } => "agent_run_tool_invocations_pending",
390 Self::NoPendingAgentRunToolInvocations { .. } => {
391 "no_pending_agent_run_tool_invocations"
392 }
393 Self::ToolCallAlreadyResolved { .. } => "tool_call_already_resolved",
394 Self::DuplicateToolRegistration { .. } => "duplicate_tool_registration",
395 Self::ReservedToolName { .. } => "reserved_tool_name",
396 Self::InvalidToolInputSchema { .. } => "invalid_tool_input_schema",
397 Self::BridgeToolsNotAllowed { .. } => "bridge_tools_not_allowed",
398 Self::ToolExecutionCancelled { .. } => "tool_execution_cancelled",
399 Self::ToolExecutionFailed { .. } => "tool_execution_failed",
400 Self::AgentRunClosed { .. } => "agent_run_closed",
401 Self::MissingActionExecutionEvidence { .. } => "missing_action_execution_evidence",
402 Self::MutatingActionCommitLifecycleRequired { .. } => {
403 "mutating_action_commit_lifecycle_required"
404 }
405 Self::UnsupportedToolResultContent { .. } => "unsupported_tool_result_content",
406 Self::TranscriptItemIdExhausted => "transcript_item_id_exhausted",
407 Self::ModelTurnIdExhausted => "model_turn_id_exhausted",
408 Self::UnknownModelTurn { .. } => "unknown_model_turn",
409 Self::InvalidModelTurnTransition { .. } => "invalid_model_turn_transition",
410 Self::TranscriptToolCallMissing { .. } => "transcript_tool_call_missing",
411 Self::Core { .. } => "core_error",
412 Self::Context { .. } => "context_error",
413 Self::Checkpoint { .. } => "checkpoint_error",
414 Self::Compaction { .. } => "compaction_error",
415 Self::SessionStore { .. } => "session_store",
416 Self::MissingModelProvider { .. } => "missing_model_provider",
417 Self::CompactionModelRequest { .. } => "compaction_model_request",
418 Self::CompactionModelWindowTooSmall { .. } => "compaction_model_window_too_small",
419 Self::CompactionModelRequestTooLarge { .. } => "compaction_model_request_too_large",
420 Self::CompactionModelTruncated { .. } => "compaction_model_truncated",
421 Self::CompactionModelSetup { .. } => "compaction_model_setup",
422 Self::CompactionModelStream { .. } => "compaction_model_stream",
423 }
424 }
425
426 #[must_use]
431 pub fn is_retryable_bridge_tool_result(&self) -> bool {
432 matches!(
433 self,
434 Self::BridgeToolResultBatchEmpty { .. }
435 | Self::BridgeToolResultBatchMismatch { .. }
436 | Self::BridgeToolResultBatchIdMismatch { .. }
437 | Self::UnsupportedToolResultContent { .. }
438 | Self::Core {
439 source: CoreError::InvalidToolCallResult { .. }
440 }
441 )
442 }
443}