lenso-cli 0.2.15

Lenso command-line interface for scaffolding and operating Lenso backend projects.
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
use std::collections::{BTreeMap, BTreeSet};

use lenso_service::WorkloadRole;
use serde::{Deserialize, Serialize};
use tokio::process::Command;

use super::{
    PlannedWorkload, RunningSandbox, SandboxError, SandboxPhase, SandboxWorkload, persist_state,
    rollback_launch, stop_owned_child, write_json_async,
};

const RESULT_PROTOCOL: &str = "lenso.failure-scenario-result.v1";
const STORY_PROTOCOL: &str = "lenso.story-segment.v1";
const WORKLOAD_OBSERVATION_PROTOCOL: &str = "lenso.sandbox-workload-observation.v1";

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub(super) struct FailureScenario {
    scenario_id: String,
    fault: InjectedFault,
    call_policy: ScenarioCallPolicy,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct InjectedFault {
    kind: FaultKind,
    service_id: String,
    workload_id: String,
    #[serde(default)]
    delay_ms: u64,
    #[serde(default)]
    capacity: u32,
    #[serde(default)]
    demand: u32,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
enum FaultKind {
    Timeout,
    SlowDependency,
    WorkloadCrash,
    Overload,
    PartialUnavailability,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct ScenarioCallPolicy {
    deadline_ms: u64,
    max_attempts: u32,
    idempotent: bool,
}

#[derive(Debug)]
struct InjectionObservation {
    failure: ScenarioFailure,
    attempts: u32,
    retry_attempted: bool,
    retry_reason: String,
    controlled_time_end_ms: u64,
    final_health: String,
    health_reason: String,
}

#[derive(Debug, Clone, Copy)]
enum ScenarioFailure {
    DeadlineExceeded,
    WorkloadCrashed,
    OverloadRejected,
    PartialUnavailability,
}

impl ScenarioFailure {
    const fn outcome(self) -> &'static str {
        match self {
            Self::DeadlineExceeded => "deadline_exceeded",
            Self::WorkloadCrashed => "workload_crashed",
            Self::OverloadRejected => "overload_rejected",
            Self::PartialUnavailability => "partial_unavailability_observed",
        }
    }

    const fn expected_final_health(self) -> &'static str {
        match self {
            Self::DeadlineExceeded | Self::OverloadRejected => "ready",
            Self::WorkloadCrashed | Self::PartialUnavailability => "failed",
        }
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct WorkloadScenarioObservation {
    artifact_version: String,
    outcome: String,
    attempts: u32,
    retry_attempted: bool,
    retry_reason: String,
    controlled_time_end_ms: u64,
    final_health: String,
    health_reason: String,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct FailureScenarioResult {
    artifact_version: &'static str,
    pub(super) scenario_id: String,
    injected_fault: InjectedFault,
    affected: AffectedWorkload,
    attempts: u32,
    retry_decision: RetryDecision,
    call_policy_evidence: CallPolicyEvidence,
    health_transitions: Vec<HealthTransition>,
    observed_service_behavior: ObservedServiceBehavior,
    pub(super) outcome: String,
    cleanup: CleanupResult,
    next_actions: Vec<String>,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct AffectedWorkload {
    service_id: String,
    workload_id: String,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct RetryDecision {
    attempted: bool,
    reason: String,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct CallPolicyEvidence {
    deadline_ms: u64,
    max_attempts: u32,
    idempotent: bool,
    controlled_time_start_ms: u64,
    controlled_time_end_ms: u64,
    remaining_deadline_ms: u64,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct HealthTransition {
    service_id: String,
    workload_id: String,
    from: String,
    to: String,
    controlled_time_ms: u64,
    reason: String,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct ObservedServiceBehavior {
    service_id: String,
    decision: String,
    workloads: Vec<ObservedWorkload>,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct ObservedWorkload {
    workload_id: String,
    phase: &'static str,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct CleanupResult {
    completed: bool,
    managed_processes_stopped: usize,
    sandbox_state_removed: bool,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct ScenarioStorySegment<'a> {
    artifact_version: &'static str,
    story_id: String,
    segment_id: String,
    service_id: &'a str,
    workload_id: &'a str,
    kind: &'static str,
    result: &'a FailureScenarioResult,
}

pub(super) fn validate(
    scenarios: &[FailureScenario],
    configured: &BTreeMap<(String, String), &SandboxWorkload>,
    declared: &BTreeMap<(String, String), WorkloadRole>,
) -> Result<(), SandboxError> {
    let mut ids = BTreeSet::new();
    for scenario in scenarios {
        if !super::is_safe_identity(&scenario.scenario_id) {
            return Err(invalid_scenario(format!(
                "Failure Scenario identity is not a safe path component: {}",
                scenario.scenario_id
            )));
        }
        if !ids.insert(scenario.scenario_id.as_str()) {
            return Err(invalid_scenario(format!(
                "Failure Scenario {} is duplicated.",
                scenario.scenario_id
            )));
        }
        let key = (
            scenario.fault.service_id.clone(),
            scenario.fault.workload_id.clone(),
        );
        if !configured.contains_key(&key) || !declared.contains_key(&key) {
            return Err(invalid_scenario(format!(
                "Failure Scenario {} targets undeclared Workload {}/{}.",
                scenario.scenario_id, scenario.fault.service_id, scenario.fault.workload_id
            )));
        }
        if matches!(
            scenario.fault.kind,
            FaultKind::Timeout | FaultKind::SlowDependency | FaultKind::Overload
        ) && configured[&key].scenario_command.is_empty()
        {
            return Err(invalid_scenario(format!(
                "Failure Scenario {} requires scenarioCommand on Workload {}/{}.",
                scenario.scenario_id, scenario.fault.service_id, scenario.fault.workload_id
            )));
        }
        if declared[&key] == WorkloadRole::Migration
            && matches!(
                scenario.fault.kind,
                FaultKind::WorkloadCrash | FaultKind::PartialUnavailability
            )
        {
            return Err(invalid_scenario(format!(
                "Failure Scenario {} must target a long-running API or Worker Workload.",
                scenario.scenario_id
            )));
        }
        if scenario.call_policy.deadline_ms == 0 || scenario.call_policy.max_attempts == 0 {
            return Err(invalid_scenario(format!(
                "Failure Scenario {} requires a positive Deadline and maxAttempts.",
                scenario.scenario_id
            )));
        }
        match scenario.fault.kind {
            FaultKind::Timeout if scenario.fault.delay_ms < scenario.call_policy.deadline_ms => {
                return Err(invalid_scenario(format!(
                    "Timeout Scenario {} delayMs must reach or exceed its Deadline.",
                    scenario.scenario_id
                )));
            }
            FaultKind::SlowDependency
                if scenario.fault.delay_ms < scenario.call_policy.deadline_ms =>
            {
                return Err(invalid_scenario(format!(
                    "Slow-dependency Scenario {} delayMs must reach or exceed its Deadline.",
                    scenario.scenario_id
                )));
            }
            FaultKind::Overload
                if scenario.fault.capacity == 0
                    || scenario.fault.demand <= scenario.fault.capacity =>
            {
                return Err(invalid_scenario(format!(
                    "Overload Scenario {} requires positive capacity and demand greater than capacity.",
                    scenario.scenario_id
                )));
            }
            _ => {}
        }
    }
    Ok(())
}

pub(super) fn ensure_declared(
    scenarios: &[FailureScenario],
    scenario_id: &str,
) -> Result<(), SandboxError> {
    declared_scenario(scenarios, scenario_id).map(|_| ())
}

fn declared_scenario<'a>(
    scenarios: &'a [FailureScenario],
    scenario_id: &str,
) -> Result<&'a FailureScenario, SandboxError> {
    scenarios
        .iter()
        .find(|scenario| scenario.scenario_id == scenario_id)
        .ok_or_else(|| {
            SandboxError::new(
                "scenario_not_found",
                format!("Failure Scenario is not declared: {scenario_id}"),
                "Choose a scenarioId declared in lenso.system-sandbox.json.",
            )
        })
}

pub(super) async fn run(
    running: &mut RunningSandbox,
    scenarios: &[FailureScenario],
    scenario_id: &str,
) -> Result<FailureScenarioResult, SandboxError> {
    let scenario = declared_scenario(scenarios, scenario_id)?.clone();
    let managed_processes = running.processes.len();
    let observation = match inject(running, &scenario).await {
        Ok(observation) => observation,
        Err(error) => return Err(rollback_launch(running, error).await),
    };
    if let Err(error) = persist_state(&running.plan.owned_root, &running.state).await {
        return Err(rollback_launch(running, error).await);
    }
    let outcome = observation.failure.outcome();
    let observed_service_behavior = observe_service(running, &scenario, outcome);
    running.shutdown().await?;

    let remaining_deadline_ms = scenario
        .call_policy
        .deadline_ms
        .saturating_sub(observation.controlled_time_end_ms);
    let result = FailureScenarioResult {
        artifact_version: RESULT_PROTOCOL,
        scenario_id: scenario.scenario_id.clone(),
        injected_fault: scenario.fault.clone(),
        affected: AffectedWorkload {
            service_id: scenario.fault.service_id.clone(),
            workload_id: scenario.fault.workload_id.clone(),
        },
        attempts: observation.attempts,
        retry_decision: RetryDecision {
            attempted: observation.retry_attempted,
            reason: observation.retry_reason.clone(),
        },
        call_policy_evidence: CallPolicyEvidence {
            deadline_ms: scenario.call_policy.deadline_ms,
            max_attempts: scenario.call_policy.max_attempts,
            idempotent: scenario.call_policy.idempotent,
            controlled_time_start_ms: 0,
            controlled_time_end_ms: observation.controlled_time_end_ms,
            remaining_deadline_ms,
        },
        health_transitions: vec![
            HealthTransition {
                service_id: scenario.fault.service_id.clone(),
                workload_id: scenario.fault.workload_id.clone(),
                from: "starting".to_owned(),
                to: "ready".to_owned(),
                controlled_time_ms: 0,
                reason: "sandbox_health_gate_passed".to_owned(),
            },
            HealthTransition {
                service_id: scenario.fault.service_id.clone(),
                workload_id: scenario.fault.workload_id.clone(),
                from: "ready".to_owned(),
                to: observation.final_health.clone(),
                controlled_time_ms: observation.controlled_time_end_ms,
                reason: observation.health_reason.clone(),
            },
        ],
        observed_service_behavior,
        outcome: outcome.to_owned(),
        cleanup: CleanupResult {
            completed: true,
            managed_processes_stopped: managed_processes,
            sandbox_state_removed: true,
        },
        next_actions: next_actions(scenario.fault.kind),
    };
    persist_evidence(running, &result).await?;
    Ok(result)
}

async fn inject(
    running: &mut RunningSandbox,
    scenario: &FailureScenario,
) -> Result<InjectionObservation, SandboxError> {
    match scenario.fault.kind {
        FaultKind::Timeout | FaultKind::SlowDependency | FaultKind::Overload => {
            run_workload_scenario(running, scenario).await
        }
        FaultKind::WorkloadCrash => {
            stop_target(running, &scenario.fault).await?;
            Ok(process_failure_observation(
                ScenarioFailure::WorkloadCrashed,
                "sandbox_injected_workload_crash",
                scenario.call_policy.idempotent,
            ))
        }
        FaultKind::PartialUnavailability => {
            stop_target(running, &scenario.fault).await?;
            if !other_workload_is_ready(running, &scenario.fault) {
                return Err(SandboxError::new(
                    "partial_unavailability_not_observed",
                    format!(
                        "Service {} has no other ready Workload after the injected failure.",
                        scenario.fault.service_id
                    ),
                    "Target one Workload in a Service that keeps another API or Worker ready.",
                ));
            }
            Ok(process_failure_observation(
                ScenarioFailure::PartialUnavailability,
                "target_failed_while_other_service_workloads_remained_ready",
                scenario.call_policy.idempotent,
            ))
        }
    }
}

async fn run_workload_scenario(
    running: &mut RunningSandbox,
    scenario: &FailureScenario,
) -> Result<InjectionObservation, SandboxError> {
    let state_index = target_state_index(&running.plan.workloads, &scenario.fault)?;
    let workload = &running.plan.workloads[state_index];
    let mut command = Command::new(&workload.scenario_command[0]);
    command
        .args(&workload.scenario_command[1..])
        .current_dir(&workload.cwd)
        .envs(&workload.env)
        .env("LENSO_SANDBOX_SCENARIO_ID", &scenario.scenario_id)
        .env("LENSO_SANDBOX_FAULT_KIND", fault_kind(scenario.fault.kind))
        .env(
            "LENSO_SANDBOX_DELAY_MS",
            scenario.fault.delay_ms.to_string(),
        )
        .env(
            "LENSO_SANDBOX_CAPACITY",
            scenario.fault.capacity.to_string(),
        )
        .env("LENSO_SANDBOX_DEMAND", scenario.fault.demand.to_string())
        .env(
            "LENSO_SANDBOX_DEADLINE_MS",
            scenario.call_policy.deadline_ms.to_string(),
        )
        .env(
            "LENSO_SANDBOX_MAX_ATTEMPTS",
            scenario.call_policy.max_attempts.to_string(),
        )
        .env(
            "LENSO_SANDBOX_IDEMPOTENT",
            scenario.call_policy.idempotent.to_string(),
        )
        .env(
            "LENSO_SANDBOX_OWNERSHIP_TOKEN",
            &running.state.ownership_token,
        )
        .kill_on_drop(true);
    let output = command.output().await.map_err(|error| {
        SandboxError::new(
            "scenario_command_failed",
            format!(
                "Could not run scenarioCommand for {}/{}: {error}",
                workload.service_id, workload.workload_id
            ),
            "Fix the Workload scenarioCommand and rerun the scenario.",
        )
    })?;
    if !output.status.success() {
        return Err(SandboxError::new(
            "scenario_command_failed",
            format!(
                "Workload {}/{} scenarioCommand exited with {}: {}",
                workload.service_id,
                workload.workload_id,
                output.status,
                String::from_utf8_lossy(&output.stderr)
            ),
            "Inspect the scenario command output and rerun the scenario.",
        ));
    }
    let observed: WorkloadScenarioObservation =
        serde_json::from_slice(&output.stdout).map_err(|error| {
            SandboxError::new(
                "scenario_observation_invalid",
                format!("Workload scenario observation is invalid: {error}"),
                "Return one lenso.sandbox-workload-observation.v1 JSON document.",
            )
        })?;
    validate_workload_observation(scenario, observed)
}

fn validate_workload_observation(
    scenario: &FailureScenario,
    observed: WorkloadScenarioObservation,
) -> Result<InjectionObservation, SandboxError> {
    let failure = match observed.outcome.as_str() {
        "deadline_exceeded"
            if matches!(
                scenario.fault.kind,
                FaultKind::Timeout | FaultKind::SlowDependency
            ) =>
        {
            ScenarioFailure::DeadlineExceeded
        }
        "overload_rejected" if scenario.fault.kind == FaultKind::Overload => {
            ScenarioFailure::OverloadRejected
        }
        _ => {
            return Err(SandboxError::new(
                "scenario_observation_mismatch",
                format!(
                    "Workload reported outcome {} for injected fault {}.",
                    observed.outcome,
                    fault_kind(scenario.fault.kind)
                ),
                "Make scenarioCommand report the behavior observed for the injected fault.",
            ));
        }
    };
    let valid = observed.artifact_version == WORKLOAD_OBSERVATION_PROTOCOL
        && observed.attempts > 0
        && observed.attempts <= scenario.call_policy.max_attempts
        && observed.retry_attempted == (observed.attempts > 1)
        && !observed.retry_reason.is_empty()
        && observed.final_health == failure.expected_final_health()
        && !observed.health_reason.is_empty()
        && (!matches!(failure, ScenarioFailure::DeadlineExceeded)
            || observed.controlled_time_end_ms >= scenario.call_policy.deadline_ms);
    if !valid {
        return Err(SandboxError::new(
            "scenario_observation_invalid",
            "Workload scenario observation violates the declared Call Policy or fault contract.",
            "Align attempts, controlled time, health, and protocol with the scenario declaration.",
        ));
    }
    Ok(InjectionObservation {
        failure,
        attempts: observed.attempts,
        retry_attempted: observed.retry_attempted,
        retry_reason: observed.retry_reason,
        controlled_time_end_ms: observed.controlled_time_end_ms,
        final_health: observed.final_health,
        health_reason: observed.health_reason,
    })
}

fn process_failure_observation(
    failure: ScenarioFailure,
    health_reason: &str,
    idempotent: bool,
) -> InjectionObservation {
    InjectionObservation {
        failure,
        attempts: 1,
        retry_attempted: false,
        retry_reason: if idempotent {
            "target_unavailable"
        } else {
            "unsafe_operation"
        }
        .to_owned(),
        controlled_time_end_ms: 0,
        final_health: failure.expected_final_health().to_owned(),
        health_reason: health_reason.to_owned(),
    }
}

const fn fault_kind(kind: FaultKind) -> &'static str {
    match kind {
        FaultKind::Timeout => "timeout",
        FaultKind::SlowDependency => "slow_dependency",
        FaultKind::WorkloadCrash => "workload_crash",
        FaultKind::Overload => "overload",
        FaultKind::PartialUnavailability => "partial_unavailability",
    }
}

fn other_workload_is_ready(running: &RunningSandbox, fault: &InjectedFault) -> bool {
    running.state.workloads.iter().any(|workload| {
        workload.service_id == fault.service_id
            && workload.workload_id != fault.workload_id
            && workload.phase == SandboxPhase::Ready
    })
}

fn observe_service(
    running: &RunningSandbox,
    scenario: &FailureScenario,
    outcome: &str,
) -> ObservedServiceBehavior {
    let workloads = running
        .state
        .workloads
        .iter()
        .filter(|workload| workload.service_id == scenario.fault.service_id)
        .map(|workload| ObservedWorkload {
            workload_id: workload.workload_id.clone(),
            phase: phase_name(workload.phase),
        })
        .collect();
    ObservedServiceBehavior {
        service_id: scenario.fault.service_id.clone(),
        decision: outcome.to_owned(),
        workloads,
    }
}

const fn phase_name(phase: SandboxPhase) -> &'static str {
    match phase {
        SandboxPhase::Starting => "starting",
        SandboxPhase::Ready => "ready",
        SandboxPhase::Completed => "completed",
        SandboxPhase::Stopping => "stopping",
        SandboxPhase::Stopped => "stopped",
        SandboxPhase::Failed => "failed",
    }
}

async fn stop_target(
    running: &mut RunningSandbox,
    fault: &InjectedFault,
) -> Result<(), SandboxError> {
    let state_index = target_state_index(&running.plan.workloads, fault)?;
    let process_index = running
        .processes
        .iter()
        .position(|process| process.state_index == state_index)
        .ok_or_else(|| {
            SandboxError::new(
                "scenario_target_not_running",
                format!(
                    "Failure Scenario target is not running: {}/{}",
                    fault.service_id, fault.workload_id
                ),
                "Target a long-running API or Worker Workload and rerun the scenario.",
            )
        })?;
    let process = &mut running.processes[process_index];
    stop_owned_child(&mut process.child, process.process_group_id)
        .await
        .map_err(|error| {
            SandboxError::new(
                "scenario_injection_failed",
                format!("Could not stop the target Workload: {error}"),
                "Run sandbox cleanup, inspect the target process group, and retry.",
            )
        })?;
    running.processes.remove(process_index);
    let workload = &mut running.state.workloads[state_index];
    workload.phase = SandboxPhase::Failed;
    workload.process_id = None;
    Ok(())
}

fn target_state_index(
    workloads: &[PlannedWorkload],
    fault: &InjectedFault,
) -> Result<usize, SandboxError> {
    workloads
        .iter()
        .position(|workload| {
            workload.service_id == fault.service_id && workload.workload_id == fault.workload_id
        })
        .ok_or_else(|| {
            SandboxError::new(
                "scenario_target_missing",
                format!(
                    "Failure Scenario target is missing from the Sandbox plan: {}/{}",
                    fault.service_id, fault.workload_id
                ),
                "Fix the Failure Scenario target and rerun the dry-run.",
            )
        })
}

async fn persist_evidence(
    running: &RunningSandbox,
    result: &FailureScenarioResult,
) -> Result<(), SandboxError> {
    let lenso_dir = running
        .plan
        .owned_root
        .parent()
        .and_then(std::path::Path::parent)
        .ok_or_else(|| {
            SandboxError::new(
                "scenario_evidence_failed",
                "Sandbox result root could not be derived.",
                "Keep Sandbox state under the System .lenso directory.",
            )
        })?;
    let result_dir = lenso_dir
        .join("system-sandbox-results")
        .join(&running.plan.system_id)
        .join(&result.scenario_id);
    tokio::fs::create_dir_all(&result_dir)
        .await
        .map_err(|error| {
            super::io_error(
                "scenario_evidence_failed",
                "create Failure Scenario evidence directory",
                &error,
            )
        })?;
    write_json_async(&result_dir.join("result.json"), result).await?;
    let story = ScenarioStorySegment {
        artifact_version: STORY_PROTOCOL,
        story_id: format!("sandbox:{}", result.scenario_id),
        segment_id: format!(
            "sandbox:{}:{}/{}",
            result.scenario_id, result.affected.service_id, result.affected.workload_id
        ),
        service_id: &result.affected.service_id,
        workload_id: &result.affected.workload_id,
        kind: "failure_scenario",
        result,
    };
    write_json_async(&result_dir.join("story-segment.json"), &story).await
}

fn next_actions(kind: FaultKind) -> Vec<String> {
    let inspect = match kind {
        FaultKind::Timeout => "Inspect Deadline propagation and timeout evidence.",
        FaultKind::SlowDependency => {
            "Inspect remaining Deadline behavior at the dependency boundary."
        }
        FaultKind::WorkloadCrash => "Inspect failed Workload health and retry suppression.",
        FaultKind::Overload => "Inspect declared capacity and overload rejection evidence.",
        FaultKind::PartialUnavailability => {
            "Inspect the unavailable Workload while confirming other Workloads stayed ready."
        }
    };
    vec![
        inspect.to_owned(),
        "Rerun the same scenario to verify an equivalent result.".to_owned(),
    ]
}

fn invalid_scenario(message: String) -> SandboxError {
    SandboxError::new(
        "invalid_failure_scenario",
        message,
        "Fix the declared Failure Scenario and rerun the dry-run.",
    )
}