wasm4pm 26.6.10

High-performance process mining algorithms in WebAssembly for JavaScript/TypeScript
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
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
//! Action Dispatch Layer — converts RL action labels to executable operations.
//!
//! This module bridges the gap between RL agent decisions and actual system
//! operations. When an RL agent selects an action (e.g., RlAction::Scale),
//! this layer translates that label into concrete executable operations.
//!
//! # Design Philosophy
//!
//! - **Observable execution**: Every dispatch returns structured outcomes for
//!   telemetry and reward computation.
//! - **WASM-compatible**: No async, no threads, no filesystem I/O.
//!
//! # Action Semantics
//!
//! - **Continue**: No-op, maintain current execution path.
//! - **Scale**: Adjust resource allocation (memory, timeout, batch size).
//! - **Retry**: Exponential backoff retry with jitter.
//! - **Fallback**: Switch to alternative algorithm (selects DFG as the most robust fallback).
//! - **Restart**: Component restart — resets SPC history ring buffer and circuit breaker state.

use crate::RlAction;

/// Fixed seed for deterministic replay; callers may pass custom seed for stochastic use.
const DETERMINISTIC_SEED: u64 = 0xdead_beef;

// ---------------------------------------------------------------------------
// Execution Context — information available to action handlers
// ---------------------------------------------------------------------------

/// Execution context passed to all action dispatches.
///
/// Provides the current system state, resource constraints, and telemetry
/// data needed for action execution decisions.
#[derive(Debug, Clone)]
pub struct ExecutionContext {
    /// Current health state (0=Normal, 1=Warning, 2=Degraded, 3=Critical, 4=Failed)
    pub health_level: u8,

    /// Current memory limit in MB
    pub current_memory_mb: u32,

    /// Current timeout in milliseconds
    pub current_timeout_ms: u32,

    /// Current batch size for processing
    pub current_batch_size: u32,

    /// Number of retry attempts already made
    pub retry_count: u32,

    /// Maximum allowed retry attempts
    pub max_retries: u32,

    /// Base backoff delay in milliseconds
    pub base_backoff_ms: u32,

    /// Whether circuit breaker is open
    pub circuit_breaker_open: bool,
}

impl Default for ExecutionContext {
    fn default() -> Self {
        Self {
            health_level: 0,
            current_memory_mb: 512,
            current_timeout_ms: 30000,
            current_batch_size: 1000,
            retry_count: 0,
            max_retries: 3,
            base_backoff_ms: 1000,
            circuit_breaker_open: false,
        }
    }
}

impl ExecutionContext {
    /// Create a new execution context with sensible defaults.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a degraded context (e.g., after failures).
    pub fn degraded() -> Self {
        Self {
            health_level: 2,
            current_memory_mb: 256,
            current_timeout_ms: 15000,
            current_batch_size: 500,
            retry_count: 1,
            max_retries: 3,
            base_backoff_ms: 2000,
            circuit_breaker_open: false,
        }
    }

    /// Create a critical context (e.g., after multiple failures).
    pub fn critical() -> Self {
        Self {
            health_level: 3,
            current_memory_mb: 128,
            current_timeout_ms: 5000,
            current_batch_size: 100,
            retry_count: 2,
            max_retries: 3,
            base_backoff_ms: 3000,
            circuit_breaker_open: true,
        }
    }
}

// ---------------------------------------------------------------------------
// Dispatch Outcome — structured result from action execution
// ---------------------------------------------------------------------------

/// Outcome of action dispatch execution.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DispatchOutcome {
    /// No operation performed (e.g., Continue action)
    NoOp,

    /// Resources were scaled
    Scaled {
        /// New memory limit in MB
        memory_mb: u32,
        /// New timeout in ms
        timeout_ms: u32,
        /// New batch size
        batch_size: u32,
    },

    /// Retry initiated
    RetryInitiated {
        /// Retry attempt number
        attempt: u32,
        /// Backoff delay in ms
        delay_ms: u32,
    },

    /// Fallback to alternative algorithm
    FallbackInitiated {
        /// Alternative algorithm name
        algorithm: String,
    },

    /// Component restart initiated
    RestartInitiated {
        /// Whether state was cleared
        state_cleared: bool,
    },

    /// Action not implemented yet
    #[doc = "Reserved — not yet dispatched. Callers must not treat this as success."]
    NotImplemented,
}

impl DispatchOutcome {
    /// Human-readable description of the outcome.
    pub fn description(&self) -> &str {
        match self {
            DispatchOutcome::NoOp => "No operation performed",
            DispatchOutcome::Scaled { .. } => "Resources scaled",
            DispatchOutcome::RetryInitiated { .. } => "Retry with backoff",
            DispatchOutcome::FallbackInitiated { .. } => "Fallback algorithm",
            DispatchOutcome::RestartInitiated { .. } => "Component restart",
            DispatchOutcome::NotImplemented => "Action not implemented",
        }
    }
}

/// Result type for action dispatch.
pub type DispatchResult = Result<DispatchOutcome, DispatchError>;

/// Errors that can occur during action dispatch.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DispatchError {
    /// Circuit breaker is open, action blocked
    CircuitBreakerOpen,

    /// Maximum retry attempts exceeded
    MaxRetriesExceeded,

    /// Invalid action parameters
    InvalidParameters(String),

    /// Resource scaling failed
    ScalingFailed(String),

    /// Action not implemented
    #[doc = "Reserved — not yet dispatched. Callers must not treat this as success."]
    NotImplemented,
}

impl std::fmt::Display for DispatchError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DispatchError::CircuitBreakerOpen => write!(f, "Circuit breaker is open"),
            DispatchError::MaxRetriesExceeded => write!(f, "Maximum retry attempts exceeded"),
            DispatchError::InvalidParameters(msg) => write!(f, "Invalid parameters: {}", msg),
            DispatchError::ScalingFailed(msg) => write!(f, "Scaling failed: {}", msg),
            DispatchError::NotImplemented => write!(f, "Action not implemented"),
        }
    }
}

impl std::error::Error for DispatchError {}

// ---------------------------------------------------------------------------
// Action Dispatch — convert RL actions to executable operations
// ---------------------------------------------------------------------------

/// Dispatch an RL action to an executable operation.
///
/// This is the main entry point for action execution. It takes an RL action
/// label and execution context, then performs the corresponding operation.
///
/// # Arguments
///
/// * `action` - The RL action selected by the agent
/// * `context` - Current execution context with system state
///
/// # Returns
///
/// * `Ok(DispatchOutcome)` - Action was executed successfully
/// * `Err(DispatchError)` - Action execution failed
///
/// # Examples
///
/// ```ignore
/// let action = RlAction::Scale;
/// let context = ExecutionContext::default();
/// let result = dispatch_action(&action, &context);
/// assert!(result.is_ok());
/// ```
pub fn dispatch_action(action: &RlAction, context: &ExecutionContext) -> DispatchResult {
    match action {
        RlAction::Continue => action_continue(context),
        RlAction::Scale => action_scale(context),
        RlAction::Retry => action_retry(context),
        RlAction::Fallback => action_fallback(context),
        RlAction::Restart => action_restart(context),
    }
}

// ---------------------------------------------------------------------------
// Action Implementations
// ---------------------------------------------------------------------------

/// Continue action — no-op, maintain current execution path.
///
/// This is the safest action when the system is healthy. It signals that
/// no intervention is needed.
fn action_continue(_context: &ExecutionContext) -> DispatchResult {
    Ok(DispatchOutcome::NoOp)
}

/// Scale action — adjust resource allocation based on health state.
///
/// Scaling strategy:
/// - Normal (0): Maintain current resources
/// - Warning (1): Slightly increase timeout (buffer)
/// - Degraded (2): Reduce batch size, increase timeout
/// - Critical (3): Aggressive resource reduction
/// - Failed (4): Minimal resources (emergency mode)
fn action_scale(context: &ExecutionContext) -> DispatchResult {
    // Check circuit breaker
    if context.circuit_breaker_open {
        return Err(DispatchError::CircuitBreakerOpen);
    }

    let (new_memory_mb, new_timeout_ms, new_batch_size) = match context.health_level {
        0 => {
            // Normal: maintain current resources
            (
                context.current_memory_mb,
                context.current_timeout_ms,
                context.current_batch_size,
            )
        }
        1 => {
            // Warning: increase timeout for safety margin
            // Use saturating_mul to avoid u32 overflow on pathological inputs.
            (
                context.current_memory_mb,
                context.current_timeout_ms.saturating_mul(2),
                context.current_batch_size,
            )
        }
        2 => {
            // Degraded: reduce batch size, increase timeout
            (
                context.current_memory_mb / 2,
                context.current_timeout_ms.saturating_mul(2),
                context.current_batch_size / 2,
            )
        }
        3 => {
            // Critical: aggressive reduction
            (
                context.current_memory_mb / 4,
                context.current_timeout_ms.saturating_mul(3),
                context.current_batch_size / 4,
            )
        }
        4 => {
            // Failed: minimal resources
            (64, 5000, 50)
        }
        _ => {
            return Err(DispatchError::InvalidParameters(format!(
                "Invalid health level: {}",
                context.health_level
            )))
        }
    };

    // Ensure minimum thresholds
    let new_memory_mb = new_memory_mb.max(64);
    let new_timeout_ms = new_timeout_ms.max(5000);
    let new_batch_size = new_batch_size.max(50);

    Ok(DispatchOutcome::Scaled {
        memory_mb: new_memory_mb,
        timeout_ms: new_timeout_ms,
        batch_size: new_batch_size,
    })
}

/// Retry action — exponential backoff with jitter.
///
/// Retry strategy:
/// - Backoff delay = base_backoff_ms * 2^retry_count + jitter
/// - Jitter = random value in [0, base_backoff_ms / 2]
/// - Max retries enforced via context.max_retries
fn action_retry(context: &ExecutionContext) -> DispatchResult {
    // Check circuit breaker
    if context.circuit_breaker_open {
        return Err(DispatchError::CircuitBreakerOpen);
    }

    // Check max retries
    if context.retry_count >= context.max_retries {
        return Err(DispatchError::MaxRetriesExceeded);
    }

    // Exponential backoff: base * 2^attempt.
    //
    // Defect-class DR-1 (this iteration): a naive `1u32 << retry_count` is
    // *undefined behaviour* when `retry_count >= 32` and a naive
    // `base_backoff_ms * (1 << retry_count)` silently wraps `u32` for
    // retry_count >= 22 with the default `base_backoff_ms = 1000`. Both
    // failures defeat the 60 000 ms cap that is supposed to bound the
    // back-off; the operator can configure `max_retries` large enough to
    // reach them.
    //
    // Use `checked_shl` + `saturating_mul` + `saturating_add` so we always
    // either reach the cap or return the cap, never panic or wrap.
    let shift_factor = 1u32.checked_shl(context.retry_count).unwrap_or(u32::MAX);
    let exponential_delay = context.base_backoff_ms.saturating_mul(shift_factor);

    // Add jitter: up to 100% of base backoff using fastrand for robust retry distribution
    // Fixed seed for deterministic replay; callers may pass custom seed for stochastic use.
    let jitter = fastrand::Rng::with_seed(DETERMINISTIC_SEED).u32(0..=context.base_backoff_ms);

    let total_delay_ms = exponential_delay.saturating_add(jitter);

    // Cap maximum delay (e.g., 60 seconds)
    let total_delay_ms = total_delay_ms.min(60000);

    Ok(DispatchOutcome::RetryInitiated {
        attempt: context.retry_count + 1,
        delay_ms: total_delay_ms,
    })
}

/// Fallback action — switch to DFG (the simplest, fastest algorithm).
///
/// Triggered when the current algorithm is unsuitable. Selects DFG as
/// the fallback because it is the most robust algorithm in the registry.
fn action_fallback(context: &ExecutionContext) -> DispatchResult {
    // Check circuit breaker
    if context.circuit_breaker_open {
        return Err(DispatchError::CircuitBreakerOpen);
    }

    Ok(DispatchOutcome::FallbackInitiated {
        algorithm: "dfg".to_string(),
    })
}

/// Restart action — reset SPC history and circuit breaker state.
///
/// Last-resort action when the system is in a failed state. Clears
/// accumulated SPC history and resets the circuit breaker so the system
/// can begin a fresh autonomic cycle.
fn action_restart(_context: &ExecutionContext) -> DispatchResult {
    // Reset SPC history ring buffer + circuit breaker (cloud-gated thread-locals)
    #[cfg(feature = "cloud")]
    {
        crate::SPC_HISTORY.with(|h| h.borrow_mut().clear());
        crate::CIRCUIT_BREAKER
            .with(|cb| *cb.borrow_mut() = crate::self_healing::CircuitBreaker::new());
    }

    Ok(DispatchOutcome::RestartInitiated {
        state_cleared: true,
    })
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_action_continue_returns_noop() {
        let context = ExecutionContext::default();
        let result = dispatch_action(&RlAction::Continue, &context);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), DispatchOutcome::NoOp);
    }

    #[test]
    fn test_action_scale_increases_timeout_in_warning_state() {
        let context = ExecutionContext {
            health_level: 1,
            current_memory_mb: 512,
            current_timeout_ms: 30000,
            current_batch_size: 1000,
            ..Default::default()
        };

        let result = dispatch_action(&RlAction::Scale, &context);
        assert!(result.is_ok());

        if let DispatchOutcome::Scaled {
            memory_mb,
            timeout_ms,
            batch_size,
        } = result.unwrap()
        {
            assert_eq!(memory_mb, 512); // unchanged
            assert_eq!(timeout_ms, 60000); // doubled
            assert_eq!(batch_size, 1000); // unchanged
        } else {
            unreachable!("Expected Scaled outcome");
        }
    }

    #[test]
    fn test_action_scale_reduces_resources_in_degraded_state() {
        let context = ExecutionContext {
            health_level: 2,
            current_memory_mb: 512,
            current_timeout_ms: 30000,
            current_batch_size: 1000,
            ..Default::default()
        };

        let result = dispatch_action(&RlAction::Scale, &context);
        assert!(result.is_ok());

        if let DispatchOutcome::Scaled {
            memory_mb,
            timeout_ms,
            batch_size,
        } = result.unwrap()
        {
            assert_eq!(memory_mb, 256); // halved
            assert_eq!(timeout_ms, 60000); // doubled
            assert_eq!(batch_size, 500); // halved
        } else {
            unreachable!("Expected Scaled outcome");
        }
    }

    #[test]
    fn test_action_retry_computes_exponential_backoff() {
        let context = ExecutionContext {
            retry_count: 2,
            base_backoff_ms: 1000,
            max_retries: 3,
            ..Default::default()
        };

        let result = dispatch_action(&RlAction::Retry, &context);
        assert!(result.is_ok());

        if let DispatchOutcome::RetryInitiated { attempt, delay_ms } = result.unwrap() {
            assert_eq!(attempt, 3); // next attempt
                                    // Exponential: 1000 * 2^2 = 4000 base, plus stochastic jitter
                                    // in [0, base_backoff_ms] = [0, 1000]. The pre-existing test
                                    // asserted `delay_ms == 4500` from when jitter was the
                                    // deterministic value `base_backoff_ms / 2`; jitter is now
                                    // `fastrand::u32(0..=base_backoff_ms)` so we assert the
                                    // documented Rank-1 mathematical bound instead, which is the
                                    // value the implementation guarantees rather than one
                                    // particular sample.
            assert!(
                (4000..=5000).contains(&delay_ms),
                "delay {delay_ms} must lie within [4000, 5000] (exponential + jitter bound)"
            );
        } else {
            unreachable!("Expected RetryInitiated outcome");
        }
    }

    /// Rank-1 regression: `1u32 << 32` is undefined behaviour in Rust and
    /// `base * (1 << retry_count)` silently wraps `u32` long before that.
    /// The cap at 60 000 ms must hold for any pathological `retry_count`
    /// value the operator can construct, including the maximum.
    #[test]
    fn test_action_retry_does_not_overflow_at_or_above_shift_width() {
        // u32::MAX max_retries is the largest the operator can configure;
        // the function must accept any retry_count strictly less than that.
        // We exercise the two known cliffs:
        //   * retry_count = 22 — the `u32` product first overflows.
        //   * retry_count = 32 — `1u32 << 32` is undefined behaviour.
        //   * retry_count = u32::MAX - 1 — extreme upper bound just under max.
        for retry_count in [22u32, 31, 32, 33, 64, u32::MAX - 1] {
            let context = ExecutionContext {
                retry_count,
                base_backoff_ms: 1000,
                max_retries: u32::MAX, // allow the retry to be attempted
                ..Default::default()
            };
            let result = dispatch_action(&RlAction::Retry, &context);
            // Must not panic, must produce a bounded delay.
            assert!(
                result.is_ok(),
                "retry_count {} must not panic; got {:?}",
                retry_count,
                result
            );
            if let Ok(DispatchOutcome::RetryInitiated { delay_ms, .. }) = result {
                assert!(
                    delay_ms <= 60_000,
                    "retry_count {} produced delay {} > 60 000 ms cap",
                    retry_count,
                    delay_ms
                );
            } else {
                unreachable!("expected RetryInitiated, got {:?}", result);
            }
        }
    }

    #[test]
    fn test_action_retry_enforces_max_retries() {
        let context = ExecutionContext {
            retry_count: 3,
            max_retries: 3,
            ..Default::default()
        };

        let result = dispatch_action(&RlAction::Retry, &context);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err(), DispatchError::MaxRetriesExceeded);
    }

    #[test]
    fn test_action_retry_blocked_when_circuit_open() {
        let context = ExecutionContext {
            circuit_breaker_open: true,
            ..Default::default()
        };

        let result = dispatch_action(&RlAction::Retry, &context);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err(), DispatchError::CircuitBreakerOpen);
    }

    /// Rank-1 oracle (no-panic property): for any u32 timeout in any
    /// health level that multiplies it (1, 2, 3), `action_scale` must
    /// return Ok without panicking. The prior `* 2` / `* 3` panicked in
    /// debug builds and wrapped in release builds — both are silent
    /// correctness bugs that produced absurdly small timeouts on the
    /// degraded code path. Verified by passing u32::MAX.
    #[test]
    fn test_action_scale_saturates_on_timeout_overflow() {
        for health_level in [1u8, 2, 3] {
            let context = ExecutionContext {
                health_level,
                current_timeout_ms: u32::MAX,
                ..Default::default()
            };
            let result = dispatch_action(&RlAction::Scale, &context);
            assert!(
                result.is_ok(),
                "action_scale must not panic on overflow at health_level={}",
                health_level
            );
            if let Ok(DispatchOutcome::Scaled { timeout_ms, .. }) = result {
                // Saturating arithmetic must produce u32::MAX, not wrap to 0.
                assert_eq!(
                    timeout_ms,
                    u32::MAX,
                    "timeout must saturate at u32::MAX, not wrap, at health_level={}",
                    health_level
                );
            } else {
                unreachable!("Expected Scaled outcome at health_level={}", health_level);
            }
        }
    }

    #[test]
    fn test_action_scale_blocked_when_circuit_open() {
        let context = ExecutionContext {
            circuit_breaker_open: true,
            ..Default::default()
        };

        let result = dispatch_action(&RlAction::Scale, &context);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err(), DispatchError::CircuitBreakerOpen);
    }

    #[test]
    fn test_action_fallback_initiates_dfg_fallback() {
        let context = ExecutionContext::default();
        let result = dispatch_action(&RlAction::Fallback, &context);
        assert!(result.is_ok());
        assert_eq!(
            result.unwrap(),
            DispatchOutcome::FallbackInitiated {
                algorithm: "dfg".to_string()
            }
        );
    }

    #[test]
    fn test_action_restart_clears_state() {
        let context = ExecutionContext::default();
        let result = dispatch_action(&RlAction::Restart, &context);
        assert!(result.is_ok());
        assert_eq!(
            result.unwrap(),
            DispatchOutcome::RestartInitiated {
                state_cleared: true
            }
        );
    }

    #[test]
    fn test_execution_context_default() {
        let ctx = ExecutionContext::default();
        assert_eq!(ctx.health_level, 0);
        assert_eq!(ctx.current_memory_mb, 512);
        assert_eq!(ctx.current_timeout_ms, 30000);
        assert_eq!(ctx.current_batch_size, 1000);
        assert_eq!(ctx.retry_count, 0);
        assert_eq!(ctx.max_retries, 3);
        assert_eq!(ctx.base_backoff_ms, 1000);
        assert!(!ctx.circuit_breaker_open);
    }

    #[test]
    fn test_execution_context_degraded() {
        let ctx = ExecutionContext::degraded();
        assert_eq!(ctx.health_level, 2);
        assert_eq!(ctx.current_memory_mb, 256);
        assert_eq!(ctx.current_timeout_ms, 15000);
        assert_eq!(ctx.current_batch_size, 500);
        assert_eq!(ctx.retry_count, 1);
    }

    #[test]
    fn test_execution_context_critical() {
        let ctx = ExecutionContext::critical();
        assert_eq!(ctx.health_level, 3);
        assert_eq!(ctx.current_memory_mb, 128);
        assert_eq!(ctx.current_timeout_ms, 5000);
        assert_eq!(ctx.current_batch_size, 100);
        assert_eq!(ctx.retry_count, 2);
        assert!(ctx.circuit_breaker_open);
    }

    #[test]
    fn test_dispatch_outcome_description() {
        assert_eq!(
            DispatchOutcome::NoOp.description(),
            "No operation performed"
        );
        assert_eq!(
            DispatchOutcome::Scaled {
                memory_mb: 256,
                timeout_ms: 60000,
                batch_size: 500
            }
            .description(),
            "Resources scaled"
        );
        assert_eq!(
            DispatchOutcome::RetryInitiated {
                attempt: 1,
                delay_ms: 1000
            }
            .description(),
            "Retry with backoff"
        );
        assert_eq!(
            DispatchOutcome::FallbackInitiated {
                algorithm: "dfg".to_string()
            }
            .description(),
            "Fallback algorithm"
        );
        assert_eq!(
            DispatchOutcome::RestartInitiated {
                state_cleared: true
            }
            .description(),
            "Component restart"
        );
        assert_eq!(
            DispatchOutcome::NotImplemented.description(),
            "Action not implemented"
        );
    }
}