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("session {session_id} has been closed")]
31 SessionClosed {
32 session_id: SessionId,
34 },
35
36 #[error("invalid step input: {reason}")]
38 InvalidStepInput {
39 reason: &'static str,
41 },
42
43 #[error("invalid agent loop configuration: {source}")]
46 AgentLoopConfig {
47 #[source]
49 source: crate::agent_loop::AgentLoopConfigError,
50 },
51
52 #[error("child runtime construction failed: {message}")]
54 ChildRuntimeBuild {
55 message: String,
57 },
58
59 #[error("{operation} requires at least one agent id")]
61 InvalidSubagentSelection {
62 operation: &'static str,
64 },
65
66 #[error("plan error: {source}")]
68 Plan {
69 #[from]
71 source: crate::PlanError,
72 },
73
74 #[error("invalid user image input: {reason}")]
76 InvalidUserImageInput {
77 reason: String,
79 },
80
81 #[error("tool call {call_id} is not pending in session {session_id}")]
83 UnknownToolCall {
84 session_id: SessionId,
86 call_id: ToolCallId,
88 },
89
90 #[error("bridge tool result batch is empty in session {session_id}")]
92 BridgeToolResultBatchEmpty {
93 session_id: SessionId,
95 },
96
97 #[error(
99 "bridge tool result batch does not match pending calls in session {session_id}: expected {expected_call_ids:?}, received {received_call_ids:?}"
100 )]
101 BridgeToolResultBatchMismatch {
102 session_id: SessionId,
104 expected_call_ids: Vec<ToolCallId>,
106 received_call_ids: Vec<ToolCallId>,
108 },
109
110 #[error(
112 "bridge tool result batch id does not match the pending batch in session {session_id}: expected {expected_batch_id}, received {received_batch_id}"
113 )]
114 BridgeToolResultBatchIdMismatch {
115 session_id: SessionId,
117 expected_batch_id: ToolCallBatchId,
119 received_batch_id: ToolCallBatchId,
121 },
122
123 #[error(
126 "bridge tool result was rejected in session {session_id}; the tool call was recorded as failed: {message}"
127 )]
128 BridgeToolResultRejected {
129 session_id: SessionId,
131 message: String,
133 },
134
135 #[error(
137 "agent run has unresolved tool invocations in session {session_id} for batch {batch_id}"
138 )]
139 AgentRunToolInvocationsPending {
140 session_id: SessionId,
142 batch_id: ToolCallBatchId,
144 },
145
146 #[error("agent run has no pending tool invocations in session {session_id}")]
148 NoPendingAgentRunToolInvocations {
149 session_id: SessionId,
151 },
152
153 #[error("tool call {call_id} is already resolved in session {session_id}")]
155 ToolCallAlreadyResolved {
156 session_id: SessionId,
158 call_id: ToolCallId,
160 },
161
162 #[error("tool {name} is already registered")]
164 DuplicateToolRegistration {
165 name: ToolName,
167 },
168
169 #[error("tool name {name} is reserved by the runtime")]
171 ReservedToolName {
172 name: ToolName,
174 },
175
176 #[error("tool {name} input schema is invalid: {message}")]
178 InvalidToolInputSchema {
179 name: ToolName,
181 message: String,
183 },
184
185 #[error("bridge tool {name} requires explicit bridge tool opt-in")]
187 BridgeToolsNotAllowed {
188 name: ToolName,
190 },
191
192 #[error("tool call {call_id} execution was cancelled in session {session_id}")]
194 ToolExecutionCancelled {
195 session_id: SessionId,
197 call_id: ToolCallId,
199 },
200
201 #[error("tool call {call_id} executor failed in session {session_id}: {message}")]
203 ToolExecutionFailed {
204 session_id: SessionId,
206 call_id: ToolCallId,
208 message: String,
210 },
211
212 #[error("agent run closed for session {session_id}: {message}")]
214 AgentRunClosed {
215 session_id: SessionId,
217 message: &'static str,
219 },
220
221 #[error(
223 "tool call {call_id} admitted for {action_kind:?} in session {session_id} succeeded without required execution evidence"
224 )]
225 MissingActionExecutionEvidence {
226 session_id: SessionId,
228 call_id: ToolCallId,
230 action_kind: ToolActionKind,
232 },
233
234 #[error(
236 "mutating tool action {action_kind:?} for tool call {call_id} in session {session_id} requires an explicit commit lifecycle before generic execution"
237 )]
238 MutatingActionCommitLifecycleRequired {
239 session_id: SessionId,
241 call_id: ToolCallId,
243 action_kind: ToolActionKind,
245 },
246
247 #[error("tool call {call_id} result uses invalid {output_kind:?} output content")]
249 UnsupportedToolResultContent {
250 call_id: ToolCallId,
252 output_kind: ToolOutputKind,
254 },
255
256 #[error("transcript item id space is exhausted")]
258 TranscriptItemIdExhausted,
259
260 #[error("model turn id space is exhausted")]
262 ModelTurnIdExhausted,
263
264 #[error("model turn {model_turn_id} is not recorded")]
266 UnknownModelTurn { model_turn_id: u64 },
267
268 #[error("model turn {model_turn_id} cannot {attempted} from its current status")]
270 InvalidModelTurnTransition {
271 model_turn_id: u64,
272 attempted: &'static str,
273 },
274
275 #[error("tool call {call_id} has no originating transcript item")]
277 TranscriptToolCallMissing { call_id: ToolCallId },
278
279 #[error("core protocol error while constructing runtime state: {source}")]
281 Core {
282 #[from]
284 source: CoreError,
285 },
286
287 #[error("context state error: {source}")]
289 Context {
290 #[from]
292 source: ContextError,
293 },
294
295 #[error("checkpoint state error: {source}")]
297 Checkpoint {
298 #[from]
300 source: CheckpointError,
301 },
302
303 #[error("compaction state error: {source}")]
305 Compaction {
306 #[from]
308 source: CompactionError,
309 },
310
311 #[error("session store error: {source}")]
313 SessionStore {
314 #[from]
316 source: SessionStoreError,
317 },
318
319 #[error("missing model provider for runtime role {role}")]
321 MissingModelProvider {
322 role: &'static str,
324 },
325
326 #[error("compaction model request error: {message}")]
328 CompactionModelRequest {
329 message: String,
331 },
332
333 #[error(
335 "compaction model input window {compactor_window_tokens} tokens is smaller than primary context window {primary_window_tokens} tokens"
336 )]
337 CompactionModelWindowTooSmall {
338 primary_window_tokens: u64,
340 compactor_window_tokens: u64,
342 },
343
344 #[error(
346 "compaction model request estimated input {estimated_input_tokens} tokens plus max output {max_output_tokens} tokens exceeds compaction model window {compactor_window_tokens} tokens"
347 )]
348 CompactionModelRequestTooLarge {
349 estimated_input_tokens: u64,
351 max_output_tokens: u64,
353 compactor_window_tokens: u64,
355 },
356
357 #[error("compaction model output was truncated: {message}")]
359 CompactionModelTruncated {
360 message: String,
362 },
363
364 #[error("compaction model setup error: {message}")]
366 CompactionModelSetup {
367 message: String,
369 },
370
371 #[error("compaction model stream error: {message}")]
373 CompactionModelStream {
374 message: String,
376 },
377
378 #[error("agent run for session {session_id} has pending hook invocation {invocation_id}")]
380 AgentRunHookInvocationPending {
381 session_id: SessionId,
383 invocation_id: crate::HookInvocationId,
385 },
386
387 #[error("agent run for session {session_id} has no pending hook invocation")]
389 NoPendingAgentRunHookInvocation {
390 session_id: SessionId,
392 },
393
394 #[error(
396 "hook invocation id mismatch for session {session_id}: expected {expected_invocation_id}, received {received_invocation_id}"
397 )]
398 HookInvocationIdMismatch {
399 session_id: SessionId,
401 expected_invocation_id: crate::HookInvocationId,
403 received_invocation_id: crate::HookInvocationId,
405 },
406
407 #[error("hook {hook_point} failed for session {session_id}: {message}")]
409 HookFailed {
410 session_id: SessionId,
412 hook_point: crate::HookPoint,
414 message: String,
416 },
417
418 #[error("hook {hook_point} cancelled for session {session_id}")]
420 HookCancelled {
421 session_id: SessionId,
423 hook_point: crate::HookPoint,
425 },
426}
427
428impl RuntimeError {
429 #[must_use]
431 pub const fn diagnostic_code(&self) -> &'static str {
432 match self {
433 Self::StepAlreadyActive { .. } => "step_already_active",
434 Self::SessionClosed { .. } => "session_closed",
435 Self::InvalidStepInput { .. } => "invalid_step_input",
436 Self::AgentLoopConfig { .. } => "agent_loop_config",
437 Self::ChildRuntimeBuild { .. } => "child_runtime_build",
438 Self::InvalidSubagentSelection { .. } => "invalid_subagent_selection",
439 Self::Plan { .. } => "plan_error",
440 Self::InvalidUserImageInput { .. } => "invalid_user_image_input",
441 Self::UnknownToolCall { .. } => "unknown_tool_call",
442 Self::BridgeToolResultBatchEmpty { .. } => "bridge_tool_result_batch_empty",
443 Self::BridgeToolResultBatchMismatch { .. } => "bridge_tool_result_batch_mismatch",
444 Self::BridgeToolResultBatchIdMismatch { .. } => "bridge_tool_result_batch_id_mismatch",
445 Self::BridgeToolResultRejected { .. } => "bridge_tool_result_rejected",
446 Self::AgentRunToolInvocationsPending { .. } => "agent_run_tool_invocations_pending",
447 Self::NoPendingAgentRunToolInvocations { .. } => {
448 "no_pending_agent_run_tool_invocations"
449 }
450 Self::ToolCallAlreadyResolved { .. } => "tool_call_already_resolved",
451 Self::DuplicateToolRegistration { .. } => "duplicate_tool_registration",
452 Self::ReservedToolName { .. } => "reserved_tool_name",
453 Self::InvalidToolInputSchema { .. } => "invalid_tool_input_schema",
454 Self::BridgeToolsNotAllowed { .. } => "bridge_tools_not_allowed",
455 Self::ToolExecutionCancelled { .. } => "tool_execution_cancelled",
456 Self::ToolExecutionFailed { .. } => "tool_execution_failed",
457 Self::AgentRunClosed { .. } => "agent_run_closed",
458 Self::MissingActionExecutionEvidence { .. } => "missing_action_execution_evidence",
459 Self::MutatingActionCommitLifecycleRequired { .. } => {
460 "mutating_action_commit_lifecycle_required"
461 }
462 Self::UnsupportedToolResultContent { .. } => "unsupported_tool_result_content",
463 Self::TranscriptItemIdExhausted => "transcript_item_id_exhausted",
464 Self::ModelTurnIdExhausted => "model_turn_id_exhausted",
465 Self::UnknownModelTurn { .. } => "unknown_model_turn",
466 Self::InvalidModelTurnTransition { .. } => "invalid_model_turn_transition",
467 Self::TranscriptToolCallMissing { .. } => "transcript_tool_call_missing",
468 Self::Core { .. } => "core_error",
469 Self::Context { .. } => "context_error",
470 Self::Checkpoint { .. } => "checkpoint_error",
471 Self::Compaction { .. } => "compaction_error",
472 Self::SessionStore {
473 source: SessionStoreError::SessionAlreadyReserved { .. },
474 } => "session_already_reserved",
475 Self::SessionStore { .. } => "session_store",
476 Self::MissingModelProvider { .. } => "missing_model_provider",
477 Self::CompactionModelRequest { .. } => "compaction_model_request",
478 Self::CompactionModelWindowTooSmall { .. } => "compaction_model_window_too_small",
479 Self::CompactionModelRequestTooLarge { .. } => "compaction_model_request_too_large",
480 Self::CompactionModelTruncated { .. } => "compaction_model_truncated",
481 Self::CompactionModelSetup { .. } => "compaction_model_setup",
482 Self::CompactionModelStream { .. } => "compaction_model_stream",
483 Self::AgentRunHookInvocationPending { .. } => "agent_run_hook_invocation_pending",
484 Self::NoPendingAgentRunHookInvocation { .. } => "no_pending_agent_run_hook_invocation",
485 Self::HookInvocationIdMismatch { .. } => "hook_invocation_id_mismatch",
486 Self::HookFailed { .. } => "hook_failed",
487 Self::HookCancelled { .. } => "hook_cancelled",
488 }
489 }
490
491 #[must_use]
496 pub fn is_retryable_bridge_tool_result(&self) -> bool {
497 matches!(
498 self,
499 Self::BridgeToolResultBatchEmpty { .. }
500 | Self::BridgeToolResultBatchMismatch { .. }
501 | Self::BridgeToolResultBatchIdMismatch { .. }
502 | Self::UnsupportedToolResultContent { .. }
503 | Self::Core {
504 source: CoreError::InvalidToolCallResult { .. }
505 }
506 )
507 }
508}