fakecloud-stepfunctions 0.33.0

Step Functions implementation for FakeCloud
Documentation
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! `StepFunctionsService` `executions` family — extracted from service.rs by audit-2026-05-19.

use super::*;

impl StepFunctionsService {
    // ─── Execution Lifecycle ──────────────────────────────────────────

    pub(super) fn start_execution(&self, req: &AwsRequest) -> Result<AwsResponse, AwsServiceError> {
        let body = req.json_body();
        validate_required("stateMachineArn", &body["stateMachineArn"])?;
        let sm_arn = body["stateMachineArn"]
            .as_str()
            .ok_or_else(|| missing("stateMachineArn"))?;
        validate_arn(sm_arn)?;

        let input = body["input"].as_str().map(|s| s.to_string());

        // Validate input is valid JSON if provided
        if let Some(ref input_str) = input {
            let _: serde_json::Value = serde_json::from_str(input_str).map_err(|_| {
                AwsServiceError::aws_error(
                    StatusCode::BAD_REQUEST,
                    "InvalidExecutionInput",
                    "Invalid execution input: must be valid JSON".to_string(),
                )
            })?;
        }

        let execution_name = body["name"]
            .as_str()
            .map(|s| s.to_string())
            .unwrap_or_else(|| uuid::Uuid::new_v4().to_string());

        if let Some(name) = body["name"].as_str() {
            validate_name(name)?;
        }

        let mut accounts = self.state.write();
        let state = accounts.get_or_create(&req.account_id);
        let sm = state
            .state_machines
            .get(sm_arn)
            .ok_or_else(|| state_machine_not_found(sm_arn))?;

        let sm_name = sm.name.clone();
        let sm_type = sm.machine_type;
        let definition = sm.definition.clone();
        let exec_arn = state.execution_arn(&sm_name, &execution_name);

        // Handle name collisions. For STANDARD workflows, StartExecution is
        // idempotent: re-issuing the same name AND the same input returns the
        // existing executionArn with HTTP 200, while a different input is a
        // 400 ExecutionAlreadyExists. (EXPRESS has no idempotency.)
        if let Some(existing) = state.executions.get(&exec_arn) {
            let same_input = sm_type == crate::state::StateMachineType::Standard
                && execution_input_matches(existing.input.as_deref(), input.as_deref());
            if same_input {
                return Ok(AwsResponse::ok_json(json!({
                    "executionArn": existing.execution_arn,
                    "startDate": existing.start_date.timestamp() as f64,
                })));
            }
            return Err(AwsServiceError::aws_error(
                StatusCode::CONFLICT,
                "ExecutionAlreadyExists",
                format!("Execution Already Exists: '{exec_arn}'"),
            ));
        }

        let now = Utc::now();
        let execution = Execution {
            execution_arn: exec_arn.clone(),
            state_machine_arn: sm_arn.to_string(),
            state_machine_name: sm_name,
            name: execution_name,
            status: ExecutionStatus::Running,
            input: input.clone(),
            output: None,
            start_date: now,
            stop_date: None,
            error: None,
            cause: None,
            history_events: vec![],
            parent_execution_arn: None,
            is_sync: false,
            billed_duration_ms: None,
            billed_memory_mb: None,
        };

        state.executions.insert(exec_arn.clone(), execution);
        let logging_config = sm.logging_configuration.clone();
        drop(accounts);

        // Spawn async execution
        let shared_state = self.state.clone();
        let exec_arn_clone = exec_arn.clone();
        let input_clone = input;
        let delivery = self.delivery.clone();
        let dynamodb_state = self.dynamodb_state.clone();
        let registry = self.registry.clone();
        // The interpreter flips the execution to its terminal status
        // (Succeeded/Failed/TimedOut) directly in shared state, outside the
        // action-dispatch path that snapshots -- so the post-StartExecution
        // save below only ever captured the RUNNING execution. Without writing
        // through when the interpreter finishes, a completed execution reported
        // stale RUNNING (with no output/history) after a restart, and an
        // in-flight one came back RUNNING forever with no driver to advance it
        // (bug-audit 2026-06-20, 0.A2).
        let snapshot_store = self.snapshot_store.clone();
        let snapshot_lock = self.snapshot_lock.clone();
        tokio::spawn(async move {
            interpreter::execute_state_machine(
                shared_state.clone(),
                exec_arn_clone,
                definition,
                input_clone,
                delivery,
                dynamodb_state,
                registry,
                logging_config,
            )
            .await;
            super::save_stepfunctions_snapshot(&shared_state, snapshot_store, &snapshot_lock).await;
        });

        Ok(AwsResponse::ok_json(json!({
            "executionArn": exec_arn,
            "startDate": now.timestamp() as f64,
        })))
    }

    pub(super) fn stop_execution(&self, req: &AwsRequest) -> Result<AwsResponse, AwsServiceError> {
        let body = req.json_body();
        validate_required("executionArn", &body["executionArn"])?;
        let exec_arn = body["executionArn"]
            .as_str()
            .ok_or_else(|| missing("executionArn"))?;

        let error = body["error"].as_str().map(|s| s.to_string());
        let cause = body["cause"].as_str().map(|s| s.to_string());

        let mut accounts = self.state.write();
        let state = accounts.get_or_create(&req.account_id);
        let exec = state
            .executions
            .get_mut(exec_arn)
            .ok_or_else(|| execution_not_found(exec_arn))?;

        if exec.status != ExecutionStatus::Running {
            return Err(AwsServiceError::aws_error(
                StatusCode::BAD_REQUEST,
                "ExecutionNotRunning",
                format!("Execution is not running: '{exec_arn}'"),
            ));
        }

        let now = Utc::now();
        exec.status = ExecutionStatus::Aborted;
        exec.stop_date = Some(now);
        exec.error = error;
        exec.cause = cause;

        Ok(AwsResponse::ok_json(json!({
            "stopDate": now.timestamp() as f64,
        })))
    }

    pub(super) fn describe_execution(
        &self,
        req: &AwsRequest,
    ) -> Result<AwsResponse, AwsServiceError> {
        let body = req.json_body();
        validate_required("executionArn", &body["executionArn"])?;
        let exec_arn = body["executionArn"]
            .as_str()
            .ok_or_else(|| missing("executionArn"))?;

        let accounts = self.state.read();
        let empty = StepFunctionsState::new(&req.account_id, &req.region);
        let state = accounts.get(&req.account_id).unwrap_or(&empty);
        let exec = state
            .executions
            .get(exec_arn)
            .ok_or_else(|| execution_not_found(exec_arn))?;

        Ok(AwsResponse::ok_json(execution_to_json(exec)))
    }

    pub(super) fn list_executions(&self, req: &AwsRequest) -> Result<AwsResponse, AwsServiceError> {
        let body = req.json_body();
        validate_required("stateMachineArn", &body["stateMachineArn"])?;
        let sm_arn = body["stateMachineArn"]
            .as_str()
            .ok_or_else(|| missing("stateMachineArn"))?;
        validate_arn(sm_arn)?;

        let max_results = body["maxResults"].as_i64().unwrap_or(100) as usize;
        validate_range_i64("maxResults", max_results as i64, 1, 1000)?;
        let next_token = body["nextToken"].as_str();
        let status_filter = body["statusFilter"].as_str();

        let accounts = self.state.read();
        let empty = StepFunctionsState::new(&req.account_id, &req.region);
        let state = accounts.get(&req.account_id).unwrap_or(&empty);

        // Verify state machine exists
        if !state.state_machines.contains_key(sm_arn) {
            return Err(state_machine_not_found(sm_arn));
        }

        let mut executions: Vec<&Execution> = state
            .executions
            .values()
            .filter(|e| e.state_machine_arn == sm_arn)
            .filter(|e| {
                status_filter
                    .map(|sf| e.status.as_str() == sf)
                    .unwrap_or(true)
            })
            .collect();

        // Sort by start date descending (most recent first)
        executions.sort_by_key(|e| std::cmp::Reverse(e.start_date));

        let items: Vec<Value> = executions
            .iter()
            .map(|e| {
                let mut item = json!({
                    "executionArn": e.execution_arn,
                    "stateMachineArn": e.state_machine_arn,
                    "name": e.name,
                    "status": e.status.as_str(),
                    "startDate": e.start_date.timestamp() as f64,
                });
                if let Some(stop) = e.stop_date {
                    item["stopDate"] = json!(stop.timestamp() as f64);
                }
                item
            })
            .collect();

        let (page, token) =
            paginate_checked(&items, next_token, max_results).map_err(|_| invalid_token())?;

        let mut resp = json!({ "executions": page });
        if let Some(t) = token {
            resp["nextToken"] = json!(t);
        }
        Ok(AwsResponse::ok_json(resp))
    }

    pub(super) fn get_execution_history(
        &self,
        req: &AwsRequest,
    ) -> Result<AwsResponse, AwsServiceError> {
        let body = req.json_body();
        validate_required("executionArn", &body["executionArn"])?;
        let exec_arn = body["executionArn"]
            .as_str()
            .ok_or_else(|| missing("executionArn"))?;
        validate_arn_length("executionArn", exec_arn, 256)?;

        if let Some(mr) = body["maxResults"].as_i64() {
            validate_max_results(mr)?;
        }
        let max_results = body["maxResults"].as_i64().unwrap_or(100) as usize;
        let next_token = body["nextToken"].as_str();
        if let Some(t) = next_token {
            validate_page_token(t)?;
        }
        let reverse_order = body["reverseOrder"].as_bool().unwrap_or(false);

        let accounts = self.state.read();
        let empty = StepFunctionsState::new(&req.account_id, &req.region);
        let state = accounts.get(&req.account_id).unwrap_or(&empty);
        let exec = state
            .executions
            .get(exec_arn)
            .ok_or_else(|| execution_not_found(exec_arn))?;

        let mut events: Vec<Value> = exec
            .history_events
            .iter()
            .map(|e| {
                json!({
                    "id": e.id,
                    "type": e.event_type,
                    "timestamp": e.timestamp.timestamp() as f64,
                    "previousEventId": e.previous_event_id,
                    format!("{}EventDetails", camel_to_details_key(&e.event_type)): e.details,
                })
            })
            .collect();

        if reverse_order {
            events.reverse();
        }

        let (page, token) =
            paginate_checked(&events, next_token, max_results).map_err(|_| invalid_token())?;

        let mut resp = json!({ "events": page });
        if let Some(t) = token {
            resp["nextToken"] = json!(t);
        }
        Ok(AwsResponse::ok_json(resp))
    }

    // ─── Execution lifecycle extras ─────────────────────────────────────

    pub(super) fn redrive_execution(
        &self,
        req: &AwsRequest,
    ) -> Result<AwsResponse, AwsServiceError> {
        let body = req.json_body();
        let arn = body["executionArn"]
            .as_str()
            .ok_or_else(|| missing("executionArn"))?
            .to_string();
        let mut accounts = self.state.write();
        let state = accounts.get_or_create(&req.account_id);
        let exec = state.executions.get_mut(&arn).ok_or_else(|| {
            AwsServiceError::aws_error(
                StatusCode::BAD_REQUEST,
                "ExecutionDoesNotExist",
                format!("Execution does not exist: {arn}"),
            )
        })?;
        exec.status = crate::state::ExecutionStatus::Running;
        exec.stop_date = None;
        Ok(AwsResponse::ok_json(json!({
            "redriveDate": chrono::Utc::now().timestamp(),
        })))
    }

    pub(super) async fn start_sync_execution(
        &self,
        req: &AwsRequest,
    ) -> Result<AwsResponse, AwsServiceError> {
        let body = req.json_body();
        let sm_arn = body["stateMachineArn"]
            .as_str()
            .ok_or_else(|| missing("stateMachineArn"))?
            .to_string();
        let input = body["input"].as_str().unwrap_or("{}").to_string();
        if serde_json::from_str::<serde_json::Value>(&input).is_err() {
            return Err(AwsServiceError::aws_error(
                StatusCode::BAD_REQUEST,
                "InvalidExecutionInput",
                "Execution input is not valid JSON.",
            ));
        }
        let (exec_arn, definition, logging_config) = {
            let mut accounts = self.state.write();
            let state = accounts.get_or_create(&req.account_id);
            let sm = state
                .state_machines
                .get(&sm_arn)
                .ok_or_else(|| state_machine_not_found(&sm_arn))?;
            if sm.machine_type != crate::state::StateMachineType::Express {
                return Err(AwsServiceError::aws_error(
                    StatusCode::BAD_REQUEST,
                    "StateMachineTypeNotSupported",
                    "StartSyncExecution is only supported for EXPRESS state machines.",
                ));
            }
            let now = chrono::Utc::now();
            // Mint a unique execution name with a UUID rather than a
            // millisecond timestamp: Express workflows start concurrently, so a
            // ms-resolution name collides and same-ms starts overwrote each
            // other (bug-audit 2026-05-28, 4.2). Loop on the (vanishingly
            // unlikely) UUID collision, mirroring the async StartExecution path.
            let (exec_name, exec_arn) = loop {
                let candidate = format!("sync-{}", uuid::Uuid::new_v4());
                let arn = format!(
                    "arn:aws:states:{}:{}:express:{}:{}",
                    state.region, state.account_id, sm.name, candidate
                );
                if !state.executions.contains_key(&arn) {
                    break (candidate, arn);
                }
            };
            let execution = Execution {
                execution_arn: exec_arn.clone(),
                state_machine_arn: sm_arn.clone(),
                state_machine_name: sm.name.clone(),
                name: exec_name.clone(),
                status: ExecutionStatus::Running,
                input: Some(input.clone()),
                output: None,
                start_date: now,
                stop_date: None,
                error: None,
                cause: None,
                history_events: vec![],
                parent_execution_arn: None,
                is_sync: true,
                billed_duration_ms: None,
                billed_memory_mb: None,
            };
            state.executions.insert(exec_arn.clone(), execution);
            (
                exec_arn,
                sm.definition.clone(),
                sm.logging_configuration.clone(),
            )
        };

        interpreter::execute_state_machine(
            self.state.clone(),
            exec_arn.clone(),
            definition,
            Some(input),
            self.delivery.clone(),
            self.dynamodb_state.clone(),
            self.registry.clone(),
            logging_config,
        )
        .await;

        // Persist billing details on the stored execution so introspection
        // endpoints can replay the same numbers later.
        {
            let mut accounts = self.state.write();
            if let Some(state) = accounts.get_mut(&req.account_id) {
                if let Some(exec) = state.executions.get_mut(&exec_arn) {
                    let duration_ms = exec
                        .stop_date
                        .map_or(0, |stop| (stop - exec.start_date).num_milliseconds())
                        .max(0);
                    exec.billed_duration_ms = Some(duration_ms);
                    exec.billed_memory_mb = Some(64);
                }
            }
        }

        let accounts = self.state.read();
        let state = accounts.get(&req.account_id).unwrap();
        let exec = state
            .executions
            .get(&exec_arn)
            .ok_or_else(|| execution_not_found(&exec_arn))?;

        let mut resp = json!({
            "executionArn": exec.execution_arn,
            "stateMachineArn": exec.state_machine_arn,
            "name": exec.name,
            "startDate": exec.start_date.timestamp(),
            "stopDate": exec.stop_date.map(|d| d.timestamp()),
            "status": exec.status.as_str(),
            "input": exec.input.as_deref().unwrap_or("{}"),
        });

        if let Some(ref output) = exec.output {
            resp["output"] = json!(output);
        }
        if let Some(ref error) = exec.error {
            resp["error"] = json!(error);
        }
        if let Some(ref cause) = exec.cause {
            resp["cause"] = json!(cause);
        }

        let duration_ms = exec
            .stop_date
            .map_or(0, |stop| (stop - exec.start_date).num_milliseconds());
        resp["billingDetails"] = json!({
            "billedMemoryUsedInMB": 64,
            "billedDurationInMilliseconds": duration_ms.max(0),
        });

        Ok(AwsResponse::ok_json(resp))
    }
}