Skip to main content

causa_runtime/executor/
error.rs

1use causa_kernel::{
2    BatchError, BlockId, ControlError, ProcessorError, SourceError, ToolResultPayload,
3};
4
5/// Failure to construct or change a tool registry.
6#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
7#[non_exhaustive]
8pub enum ToolRegistryError {
9    /// Static tool definitions must have distinct names.
10    #[error("duplicate static tool: {name}")]
11    #[non_exhaustive]
12    DuplicateTool {
13        /// Conflicting advertised name.
14        name: String,
15    },
16    /// A source with this identity is already registered.
17    #[error("dynamic source already registered: {source_id}")]
18    #[non_exhaustive]
19    DuplicateSource {
20        /// Conflicting source identity.
21        source_id: String,
22    },
23    /// A requested source is not registered.
24    #[error("no dynamic source registered: {source_id}")]
25    #[non_exhaustive]
26    UnknownSource {
27        /// Requested source identity.
28        source_id: String,
29    },
30}
31
32/// Failure to bind a complete, unambiguous invocation catalog.
33#[derive(Debug, thiserror::Error)]
34#[non_exhaustive]
35pub enum ToolCatalogError {
36    /// The parent invocation control stopped catalog acquisition.
37    #[error(transparent)]
38    Control(ControlError),
39    /// A catalog could not be listed; its original error is retained.
40    #[error("source {source_id} failed: {error}")]
41    #[non_exhaustive]
42    Source {
43        /// Identity of the failed source registration.
44        source_id: String,
45        /// Original listing failure.
46        error: SourceError,
47    },
48    /// A name occurred more than once in the assembled catalog.
49    #[error("duplicate advertised tool: {name}")]
50    #[non_exhaustive]
51    DuplicateTool {
52        /// Conflicting advertised name.
53        name: String,
54    },
55    /// A static tool changed the name registered at construction.
56    #[error("static tool name changed from {registered} to {actual}")]
57    #[non_exhaustive]
58    StaticNameChanged {
59        /// Name registered at construction.
60        registered: String,
61        /// Name returned during this binding.
62        actual: String,
63    },
64}
65
66/// Position of a processor in the executor's fixed processing flow.
67///
68/// Exhaustive on purpose: the flow has these two positions.
69#[derive(Debug, Clone, Copy, PartialEq, Eq)]
70pub enum ToolProcessorPhase {
71    /// Before dispatch; pending inputs may be changed or completed.
72    Before,
73    /// After dispatch; completed output may be edited or reordered.
74    After,
75}
76
77/// Why controlled processing returned the caller's current batch early.
78// The confirmed API retains the actual rejected payload without indirection.
79#[allow(clippy::large_enum_variant)]
80#[derive(Debug, thiserror::Error)]
81#[non_exhaustive]
82pub enum ToolProcessingError {
83    /// The binding's parent cancellation or deadline stopped processing.
84    #[error(transparent)]
85    Control(ControlError),
86    /// The input or internally produced batch is structurally invalid.
87    #[error("invalid tool batch: {0}")]
88    InvalidBatch(BatchError),
89    /// This entry point only accepts new, entirely pending declarations.
90    #[error("tool batch already contains {completed} completed calls")]
91    #[non_exhaustive]
92    NotFreshBatch {
93        /// Number of completed calls at entry.
94        completed: usize,
95    },
96    /// A processor returned its own error.
97    #[error("{phase:?} processor {index} failed: {error}")]
98    #[non_exhaustive]
99    Processor {
100        /// Processing phase.
101        phase: ToolProcessorPhase,
102        /// Zero-based index within that phase's configured processors.
103        index: usize,
104        /// Original processor error.
105        error: ProcessorError,
106    },
107    /// A processor returned a structurally invalid batch.
108    #[error("{phase:?} processor {index} returned an invalid batch: {error}")]
109    #[non_exhaustive]
110    InvalidProcessorBatch {
111        /// Processing phase.
112        phase: ToolProcessorPhase,
113        /// Zero-based processor index.
114        index: usize,
115        /// Original batch validation error.
116        error: BatchError,
117    },
118    /// A processor violated fixed membership or immutable completed facts.
119    #[error("{phase:?} processor {index} violated its handoff: {message}")]
120    #[non_exhaustive]
121    Handoff {
122        /// Processing phase.
123        phase: ToolProcessorPhase,
124        /// Zero-based processor index.
125        index: usize,
126        /// Specific violated handoff constraint.
127        message: String,
128    },
129    /// An actual tool result could not be associated with its declaration.
130    #[error("tool result rejected for declaration {call_block_id:?}: {error}")]
131    #[non_exhaustive]
132    ResultRejected {
133        /// Declaration actually dispatched.
134        call_block_id: BlockId,
135        /// Original association or installation error.
136        error: BatchError,
137        /// Actual rejected payload, preserved for diagnosis.
138        result: ToolResultPayload,
139    },
140}