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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//! Engine error types.
use rust_decimal::Decimal;
use thiserror::Error;
use ironflow_artifacts::error::ArtifactError;
use ironflow_core::error::OperationError;
use ironflow_store::error::StoreError;
use crate::guard::{WORKFLOW_GUARD_REJECTED_CODE, WorkflowRejection};
/// Business error code carried by [`EngineError::RunBudgetExceeded`].
pub const RUN_BUDGET_EXCEEDED_CODE: &str = "RUN_BUDGET_EXCEEDED";
/// Business error code carried by [`EngineError::MonthlyBudgetExceeded`].
pub const MONTHLY_BUDGET_EXCEEDED_CODE: &str = "MONTHLY_BUDGET_EXCEEDED";
/// Business error code for handler-version mismatch on retry.
pub const HANDLER_VERSION_MISMATCH_CODE: &str = "HANDLER_VERSION_MISMATCH";
/// Errors produced by the workflow engine.
#[derive(Debug, Error)]
pub enum EngineError {
/// An operation (Shell, Http, Agent) failed during step execution.
#[error("operation failed: {0}")]
Operation(#[from] OperationError),
/// The backing store returned an error.
#[error("store error: {0}")]
Store(#[from] StoreError),
/// The workflow definition is invalid.
#[error("invalid workflow: {0}")]
InvalidWorkflow(String),
/// A step configuration could not be deserialized for execution.
#[error("step config error: {0}")]
StepConfig(String),
/// A decision answer was accessed by name with the wrong type or a missing key.
#[error("decision error: {0}")]
Decision(#[from] ironflow_core::error::DecisionError),
/// A decision step was reached but no [`DecisionProvider`](ironflow_core::decision::DecisionProvider)
/// is wired into the engine.
#[error(
"decision step '{step}' requires a decision provider; \
wire one with Engine::with_decision_provider(...)"
)]
NoDecisionProvider {
/// The decision step that could not run.
step: String,
},
/// JSON serialization error.
#[error("serialization error: {0}")]
Serialization(#[from] serde_json::Error),
/// The run reached its cumulative cost cap before launching an agent step.
///
/// Raised *before* the step is created, so no work and no spend happen.
/// The engine transitions the run to
/// [`Cancelled`](ironflow_store::entities::RunStatus::Cancelled).
#[error(
"{RUN_BUDGET_EXCEEDED_CODE}: run {run_id} would exceed its cost cap \
(spent {spent_usd} USD + next step {step_budget_usd} USD > cap {limit_usd} USD)"
)]
RunBudgetExceeded {
/// The run that hit its cap.
run_id: uuid::Uuid,
/// The configured cap, in USD.
limit_usd: Decimal,
/// Cost already accumulated by this run and its ancestors, in USD.
spent_usd: Decimal,
/// Declared budget of the step that was about to run, in USD.
step_budget_usd: Decimal,
},
/// The global monthly cost quota is exhausted; no new run may be created.
///
/// Runs already in flight are never interrupted by this error.
#[error(
"{MONTHLY_BUDGET_EXCEEDED_CODE}: monthly cost quota exhausted \
({spent_usd} USD spent of {limit_usd} USD)"
)]
MonthlyBudgetExceeded {
/// The configured monthly quota, in USD.
limit_usd: Decimal,
/// Cost already spent during the current calendar month, in USD.
spent_usd: Decimal,
},
/// A step declared an output that produced no file.
///
/// Raised only when the step itself succeeded: a declared output that never
/// materialised is a broken contract, and failing here beats failing later
/// in whichever step tried to consume it.
#[error("step {step:?} declared output {pattern:?} but no file matched")]
MissingArtifact {
/// Name of the step that declared the output.
step: String,
/// The unmatched pattern.
pattern: String,
},
/// A handle was asked for an artifact the step never declared.
#[error("step {step:?} declares no artifact output named {name:?}")]
ArtifactNotDeclared {
/// Name of the step the handle was asked from.
step: String,
/// Name of the artifact that was asked for.
name: String,
},
/// A step asked for an artifact that no earlier step produced.
#[error("no artifact {name:?} produced by step {step:?} before this point")]
ArtifactNotFound {
/// Name of the producing step that was searched for.
step: String,
/// Name of the artifact that was searched for.
name: String,
},
/// Artifacts were used but no storage backend is configured.
#[error("artifact storage is not configured: {0}")]
ArtifactsUnavailable(String),
/// The artifact storage backend failed.
#[error("artifact storage error: {0}")]
Artifact(#[from] ArtifactError),
/// The run requires human approval before continuing.
#[error("approval required for run {run_id}, step {step_id}: {message}")]
ApprovalRequired {
/// The run that is awaiting approval.
run_id: uuid::Uuid,
/// The approval step that triggered the pause.
step_id: uuid::Uuid,
/// The approval message.
message: String,
},
/// An approval gate was rejected instead of granted.
///
/// Raised when a [`StepInterceptor`](crate::executor::StepInterceptor) resolves
/// the gate with [`ApprovalOutcome::Rejected`](crate::executor::ApprovalOutcome::Rejected).
/// The engine fails the run; the rejection is deterministic, so the run is
/// never replayed.
#[error("approval rejected for run {run_id}, step {step_id}: {reason}")]
ApprovalRejected {
/// The run that was stopped.
run_id: uuid::Uuid,
/// The approval step that was rejected.
step_id: uuid::Uuid,
/// Why the gate was refused.
reason: String,
},
/// The run waits for a typed human input before continuing.
///
/// Raised by [`WorkflowContext::human_input`](crate::context::WorkflowContext::human_input)
/// when no answer has been given yet. The engine transitions the run to
/// [`AwaitingApproval`](ironflow_store::entities::RunStatus::AwaitingApproval).
#[error("human input required for run {run_id}, step {step_id}: {message}")]
HumanInputRequired {
/// The run that is awaiting input.
run_id: uuid::Uuid,
/// The human input step that triggered the pause.
step_id: uuid::Uuid,
/// The message displayed to the person answering.
message: String,
},
/// A human input request was rejected instead of answered.
///
/// Returned to the handler by
/// [`WorkflowContext::human_input`](crate::context::WorkflowContext::human_input),
/// which decides what happens next. A propagated rejection fails the run,
/// and the run is never retried.
#[error("human input rejected for run {run_id}, step {step_id}: {reason}")]
HumanInputRejected {
/// The run the input belongs to.
run_id: uuid::Uuid,
/// The human input step that was rejected.
step_id: uuid::Uuid,
/// Why the input was refused.
reason: String,
},
/// A delay step suspended the run until the given time.
///
/// The engine transitions the run to
/// [`Sleeping`](ironflow_store::entities::RunStatus::Sleeping) and sets
/// `scheduled_at` so the worker re-queues it automatically.
#[error("delay sleeping for run {run_id}, step {step_id}: wake at {wake_at}")]
DelaySleeping {
/// The run that is sleeping.
run_id: uuid::Uuid,
/// The delay step.
step_id: uuid::Uuid,
/// When the run should be woken up.
wake_at: chrono::DateTime<chrono::Utc>,
},
/// A workflow invocation was rejected by the [workflow guard](crate::guard).
///
/// The run is transitioned to
/// [`Cancelled`](ironflow_store::entities::RunStatus::Cancelled) when this
/// error is raised.
#[error("{WORKFLOW_GUARD_REJECTED_CODE}: {0}")]
WorkflowGuardRejected(#[from] WorkflowRejection),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn invalid_workflow_display() {
let err = EngineError::InvalidWorkflow("unknown-handler".to_string());
assert!(err.to_string().contains("invalid workflow"));
assert!(err.to_string().contains("unknown-handler"));
}
#[test]
fn step_config_display() {
let err = EngineError::StepConfig("bad shell config".to_string());
assert!(err.to_string().contains("step config error"));
assert!(err.to_string().contains("bad shell config"));
}
#[test]
fn human_input_required_display() {
let err = EngineError::HumanInputRequired {
run_id: uuid::Uuid::nil(),
step_id: uuid::Uuid::nil(),
message: "Answer the questions".to_string(),
};
let text = err.to_string();
assert!(text.contains("human input required"));
assert!(text.contains("Answer the questions"));
}
#[test]
fn human_input_rejected_display() {
let err = EngineError::HumanInputRejected {
run_id: uuid::Uuid::nil(),
step_id: uuid::Uuid::nil(),
reason: "not relevant".to_string(),
};
let text = err.to_string();
assert!(text.contains("human input rejected"));
assert!(text.contains("not relevant"));
}
#[test]
fn store_error_from_conversion() {
let store_err = StoreError::RunNotFound(uuid::Uuid::nil());
let engine_err = EngineError::from(store_err);
assert!(engine_err.to_string().contains("store error"));
}
#[test]
fn run_budget_exceeded_display_carries_code_and_amounts() {
let err = EngineError::RunBudgetExceeded {
run_id: uuid::Uuid::nil(),
limit_usd: Decimal::new(200, 2),
spent_usd: Decimal::new(180, 2),
step_budget_usd: Decimal::new(50, 2),
};
let msg = err.to_string();
assert!(msg.contains(RUN_BUDGET_EXCEEDED_CODE));
assert!(msg.contains("2.00"));
assert!(msg.contains("1.80"));
assert!(msg.contains("0.50"));
}
#[test]
fn monthly_budget_exceeded_display_carries_code_and_amounts() {
let err = EngineError::MonthlyBudgetExceeded {
limit_usd: Decimal::new(10000, 2),
spent_usd: Decimal::new(10500, 2),
};
let msg = err.to_string();
assert!(msg.contains(MONTHLY_BUDGET_EXCEEDED_CODE));
assert!(msg.contains("100.00"));
assert!(msg.contains("105.00"));
}
#[test]
fn missing_artifact_display_names_the_step_and_pattern() {
let err = EngineError::MissingArtifact {
step: "build".to_string(),
pattern: "target/report.html".to_string(),
};
let msg = err.to_string();
assert!(msg.contains("\"build\""));
assert!(msg.contains("target/report.html"));
}
#[test]
fn artifact_not_declared_display_names_the_step_and_artifact() {
let err = EngineError::ArtifactNotDeclared {
step: "build".to_string(),
name: "report.htm".to_string(),
};
let msg = err.to_string();
assert!(msg.contains("\"build\""));
assert!(msg.contains("\"report.htm\""));
}
#[test]
fn artifact_not_found_display_names_the_producer() {
let err = EngineError::ArtifactNotFound {
step: "build".to_string(),
name: "report.html".to_string(),
};
let msg = err.to_string();
assert!(msg.contains("\"build\""));
assert!(msg.contains("report.html"));
}
#[test]
fn artifacts_unavailable_display() {
let err = EngineError::ArtifactsUnavailable("no blob store".to_string());
assert!(err.to_string().contains("not configured"));
}
#[test]
fn artifact_error_from_conversion() {
let engine_err = EngineError::from(ArtifactError::NotFound("a/b".to_string()));
assert!(engine_err.to_string().contains("artifact storage error"));
}
#[test]
fn serialization_error_from_conversion() {
let serde_err = serde_json::from_str::<String>("not json").unwrap_err();
let engine_err = EngineError::from(serde_err);
assert!(engine_err.to_string().contains("serialization error"));
}
#[test]
fn workflow_guard_rejected_display_carries_code_and_detail() {
use crate::guard::WorkflowRejection;
let rejection = WorkflowRejection::MaxDepthExceeded { depth: 6, max: 5 };
let err = EngineError::from(rejection);
let msg = err.to_string();
assert!(msg.contains(WORKFLOW_GUARD_REJECTED_CODE));
assert!(msg.contains("max call depth exceeded"));
assert!(msg.contains("6/5"));
}
#[test]
fn workflow_guard_rejected_from_conversion() {
use crate::guard::WorkflowRejection;
let rejection = WorkflowRejection::CycleDetected {
target: "wf-b".to_string(),
chain: vec!["wf-a".to_string(), "wf-b".to_string()],
};
let engine_err = EngineError::from(rejection);
assert!(engine_err.to_string().contains("cycle detected"));
}
}