car-memgine 0.48.0

Memgine — graph-based memory engine for Common Agent Runtime
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
//! Cost-aware knowledge cascade — U-Mem's *Evolve* escalation (arXiv 2602.22406).
//!
//! Applies *Towards Autonomous Memory Agents* to CAR — see
//! `docs/proposals/autonomous-memory-agents.md`. U-Mem's Evolve step acquires &
//! validates new knowledge by **escalating cost-aware**: try the cheap signal
//! first (the model's own reflection / a teacher), only escalate to
//! tool-verified research when that's not confident enough, and only escalate to
//! a human/expert (HITL) as a last resort — never paying for a costlier tier
//! than the confidence target requires, and never exceeding a cost budget.
//!
//! This module is the **pure decision core** for that cascade: given the current
//! confidence in a piece of knowledge and a [`CascadePolicy`] (the ordered tiers,
//! each with a relative cost and the confidence it can deliver, plus a target and
//! a budget), [`decide_cascade`] returns which tier to run — or that the answer
//! is already confident enough, or that the budget is exhausted before any tier
//! reaches the target.
//!
//! It is deterministic and side-effect-free: it does **not** run reflection, call
//! tools, or ask a human. The caller maps the chosen [`CascadeTier`] onto CAR's
//! existing machinery — `reflect()` for self, tool execution for tool-verified,
//! and the durable HITL `ApprovalLedger` (`car-policy`) for the expert tier.
//! That keeps the policy testable in isolation, exactly like
//! [`crate::utility`]'s ranking core.

use serde::{Deserialize, Serialize};

/// The escalation tiers, cheapest first. The names mirror U-Mem's cascade:
/// self/teacher → tool-verified → expert/HITL.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CascadeTier {
    /// Cheapest: the model's own reflection / a teacher signal (`reflect()`).
    SelfReflect,
    /// Medium: tool-verified research (tool execution).
    ToolVerify,
    /// Costliest, last resort: human/expert feedback via the HITL approval ledger.
    HumanExpert,
}

/// One tier the cascade may escalate to: which [`CascadeTier`] it is, its
/// relative `cost`, and the `expected_confidence` in `[0,1]` it can deliver.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct TierSpec {
    pub tier: CascadeTier,
    /// Relative cost to run this tier (any positive unit — tokens, latency, $).
    pub cost: f64,
    /// Confidence in `[0,1]` this tier is expected to deliver if run.
    pub expected_confidence: f64,
}

/// The cascade policy: the ordered tiers (cheapest → costliest), the
/// `confidence_target` at or above which escalation stops, and the total cost
/// `budget` across all tiers tried.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CascadePolicy {
    /// Stop escalating once confidence reaches this. Clamped to `[0,1]`.
    pub confidence_target: f64,
    /// Total cost budget across the cascade. A tier is only run if the
    /// *cumulative* cost of trying it (plus everything cheaper already tried)
    /// stays within this.
    pub budget: f64,
    /// Tiers in escalation order, cheapest first. The cascade walks them in the
    /// given order — it does not re-sort, so the caller controls the ordering.
    pub tiers: Vec<TierSpec>,
}

/// The cascade's decision.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "decision", rename_all = "snake_case")]
pub enum CascadeOutcome {
    /// Current confidence already meets the target — escalate to nothing, spend
    /// nothing. The autonomous "don't pay to re-learn what you already know".
    AlreadyConfident { confidence: f64 },
    /// A tier reaches the target within budget. `tier` is the one to run;
    /// `confidence` is what it delivers; `cost_spent` is the cumulative cost of
    /// this tier plus every cheaper tier tried before it.
    Accept {
        tier: CascadeTier,
        confidence: f64,
        cost_spent: f64,
    },
    /// No tier reaches the target within budget. `best_tier` is the most
    /// confident tier reachable within budget (or `None` if even the cheapest
    /// tier is unaffordable); `confidence` is the best confidence achieved
    /// (falling back to the starting confidence); `cost_spent` is what reaching
    /// that best tier costs.
    Exhausted {
        best_tier: Option<CascadeTier>,
        confidence: f64,
        cost_spent: f64,
    },
}

/// Decide the cost-aware cascade. Walks `policy.tiers` in order (cheapest first),
/// accumulating cost; the first tier that meets `confidence_target` within
/// `budget` is [`CascadeOutcome::Accept`]. If the starting confidence already
/// meets the target it's [`CascadeOutcome::AlreadyConfident`] (no spend). If the
/// budget runs out — or no tier reaches the target — it's
/// [`CascadeOutcome::Exhausted`] carrying the best tier reachable within budget.
///
/// Pure and deterministic: a given `(current_confidence, policy)` always yields
/// the same outcome.
pub fn decide_cascade(current_confidence: f64, policy: &CascadePolicy) -> CascadeOutcome {
    let target = policy.confidence_target.clamp(0.0, 1.0);
    let start = current_confidence.clamp(0.0, 1.0);

    if start >= target {
        return CascadeOutcome::AlreadyConfident { confidence: start };
    }

    let mut cumulative_cost = 0.0;
    // Best tier reachable within budget so far (highest expected confidence).
    let mut best_tier: Option<CascadeTier> = None;
    let mut best_confidence = start;
    let mut best_cost = 0.0;

    for spec in &policy.tiers {
        let next_cost = cumulative_cost + spec.cost;
        if next_cost > policy.budget {
            // Can't afford this tier (or anything costlier after it) — stop.
            break;
        }
        cumulative_cost = next_cost;
        let conf = spec.expected_confidence.clamp(0.0, 1.0);

        if conf >= target {
            return CascadeOutcome::Accept {
                tier: spec.tier,
                confidence: conf,
                cost_spent: cumulative_cost,
            };
        }

        // Track the most confident affordable tier as the fallback.
        if conf > best_confidence {
            best_confidence = conf;
            best_tier = Some(spec.tier);
            best_cost = cumulative_cost;
        }
    }

    CascadeOutcome::Exhausted {
        best_tier,
        confidence: best_confidence,
        cost_spent: best_cost,
    }
}

/// What running a tier produced: the knowledge it acquired and the confidence it
/// *actually* achieved. The observed confidence is the load-bearing difference
/// from [`decide_cascade`], which only knows each tier's *predicted*
/// `expected_confidence` — the live evolve loop escalates on what a tier really
/// delivered, not on what it was expected to.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TierResult {
    /// The knowledge the tier acquired/validated (e.g. a reflection, a
    /// tool-verified fact, an expert's answer).
    pub knowledge: String,
    /// The confidence in `[0,1]` actually achieved by running this tier.
    pub confidence: f64,
}

/// One tier that was actually run during [`run_cascade`], in order.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CascadeStep {
    pub tier: CascadeTier,
    /// Observed confidence after running this tier.
    pub confidence: f64,
    /// Cumulative cost spent through this tier.
    pub cost: f64,
}

/// The result of actually running the cascade.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CascadeRun {
    /// The tier whose result met the target, if any. `None` means the target was
    /// never reached (or the start was already confident — see `steps` empty).
    pub accepted_tier: Option<CascadeTier>,
    /// The best knowledge acquired (from the highest-confidence run so far), if
    /// any tier ran.
    pub knowledge: Option<String>,
    /// The final/best confidence achieved (falls back to the starting confidence
    /// when no tier ran).
    pub confidence: f64,
    /// Total cost spent across every tier run.
    pub cost_spent: f64,
    /// Each tier actually run, cheapest first.
    pub steps: Vec<CascadeStep>,
}

/// Run U-Mem's *Evolve* cascade for real, escalating on **observed** confidence.
///
/// Unlike [`decide_cascade`] (which picks a tier from *predicted* confidences
/// without running anything), this walks the tiers cheapest-first and actually
/// invokes `run(tier)` for each — stopping as soon as a tier's *observed*
/// confidence meets `policy.confidence_target`, or when the next tier would
/// exceed `policy.budget`. This is the genuine Retrieve-Infer-**Evolve** loop:
/// cheap signal first, escalate only on what was really delivered.
///
/// `run` is injected (kept out of this pure crate, like CWM's `EffectModel`
/// closures) so the orchestration is testable in isolation; the live engine
/// maps each [`CascadeTier`] onto `reflect()` (self), tool execution
/// (tool-verified), and the durable HITL `ApprovalLedger` (`car-policy`) for the
/// expert tier. A `run` that returns `Err` aborts the cascade and propagates the
/// error (the caller decides whether a tier failure is fatal).
///
/// If the starting confidence already meets the target, no tier runs and the
/// returned [`CascadeRun`] has empty `steps`.
///
/// See [`run_cascade_async`] for the async sibling (live tiers); the two share
/// identical escalation/budget logic and must be kept in lockstep.
pub fn run_cascade(
    current_confidence: f64,
    policy: &CascadePolicy,
    mut run: impl FnMut(CascadeTier) -> Result<TierResult, String>,
) -> Result<CascadeRun, String> {
    let target = policy.confidence_target.clamp(0.0, 1.0);
    let start = current_confidence.clamp(0.0, 1.0);

    let mut result = CascadeRun {
        accepted_tier: None,
        knowledge: None,
        confidence: start,
        cost_spent: 0.0,
        steps: Vec::new(),
    };

    if start >= target {
        return Ok(result);
    }

    let mut cumulative_cost = 0.0;
    let mut best_confidence = start;

    for spec in &policy.tiers {
        let next_cost = cumulative_cost + spec.cost;
        if next_cost > policy.budget {
            break; // can't afford this tier — stop escalating
        }
        cumulative_cost = next_cost;

        let outcome = run(spec.tier)?;
        let conf = outcome.confidence.clamp(0.0, 1.0);
        result.steps.push(CascadeStep {
            tier: spec.tier,
            confidence: conf,
            cost: cumulative_cost,
        });
        result.cost_spent = cumulative_cost;

        // Keep the best knowledge seen so far (highest observed confidence).
        if conf >= best_confidence || result.knowledge.is_none() {
            best_confidence = best_confidence.max(conf);
            result.knowledge = Some(outcome.knowledge);
        }
        result.confidence = best_confidence;

        if conf >= target {
            result.accepted_tier = Some(spec.tier);
            result.confidence = conf;
            return Ok(result);
        }
    }

    Ok(result)
}

/// Async sibling of [`run_cascade`] for live call-sites whose tier runners are
/// asynchronous — the real CAR tiers are: `SelfReflect` → `engine.reflect()`
/// (async), `ToolVerify` → tool execution (async), `HumanExpert` → an
/// [`car-policy`] `ApprovalLedger` lookup. Identical escalation/budget logic to
/// [`run_cascade`]; it just `.await`s each `run(tier)` before deciding whether
/// to escalate, so a tier's *observed* confidence drives the loop.
///
/// `run` is `FnMut(CascadeTier) -> Future`. Because the cascade awaits each
/// tier's future fully before invoking the next, the futures never overlap —
/// the daemon's runner clones the engine `Arc` and locks it inside each future
/// rather than holding a borrow across calls.
///
/// Keep the escalation/budget logic identical to the sync [`run_cascade`] — the
/// two are line-for-line equivalent bar the `.await`.
pub async fn run_cascade_async<F, Fut>(
    current_confidence: f64,
    policy: &CascadePolicy,
    mut run: F,
) -> Result<CascadeRun, String>
where
    F: FnMut(CascadeTier) -> Fut,
    Fut: std::future::Future<Output = Result<TierResult, String>>,
{
    let target = policy.confidence_target.clamp(0.0, 1.0);
    let start = current_confidence.clamp(0.0, 1.0);

    let mut result = CascadeRun {
        accepted_tier: None,
        knowledge: None,
        confidence: start,
        cost_spent: 0.0,
        steps: Vec::new(),
    };

    if start >= target {
        return Ok(result);
    }

    let mut cumulative_cost = 0.0;
    let mut best_confidence = start;

    for spec in &policy.tiers {
        let next_cost = cumulative_cost + spec.cost;
        if next_cost > policy.budget {
            break; // can't afford this tier — stop escalating
        }
        cumulative_cost = next_cost;

        let outcome = run(spec.tier).await?;
        let conf = outcome.confidence.clamp(0.0, 1.0);
        result.steps.push(CascadeStep {
            tier: spec.tier,
            confidence: conf,
            cost: cumulative_cost,
        });
        result.cost_spent = cumulative_cost;

        if conf >= best_confidence || result.knowledge.is_none() {
            best_confidence = best_confidence.max(conf);
            result.knowledge = Some(outcome.knowledge);
        }
        result.confidence = best_confidence;

        if conf >= target {
            result.accepted_tier = Some(spec.tier);
            result.confidence = conf;
            return Ok(result);
        }
    }

    Ok(result)
}

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

    fn policy(target: f64, budget: f64) -> CascadePolicy {
        CascadePolicy {
            confidence_target: target,
            budget,
            tiers: vec![
                TierSpec {
                    tier: CascadeTier::SelfReflect,
                    cost: 1.0,
                    expected_confidence: 0.6,
                },
                TierSpec {
                    tier: CascadeTier::ToolVerify,
                    cost: 5.0,
                    expected_confidence: 0.85,
                },
                TierSpec {
                    tier: CascadeTier::HumanExpert,
                    cost: 50.0,
                    expected_confidence: 0.99,
                },
            ],
        }
    }

    #[test]
    fn already_confident_spends_nothing() {
        let out = decide_cascade(0.9, &policy(0.8, 100.0));
        assert_eq!(out, CascadeOutcome::AlreadyConfident { confidence: 0.9 });
    }

    #[test]
    fn picks_cheapest_tier_that_meets_target() {
        // Target 0.6 → the cheap self-reflect tier suffices.
        let out = decide_cascade(0.2, &policy(0.6, 100.0));
        assert_eq!(
            out,
            CascadeOutcome::Accept {
                tier: CascadeTier::SelfReflect,
                confidence: 0.6,
                cost_spent: 1.0,
            }
        );
    }

    #[test]
    fn escalates_when_cheap_tier_insufficient() {
        // Target 0.8 → self-reflect (0.6) is not enough, escalate to tool-verify
        // (0.85). cost_spent is cumulative: 1.0 + 5.0.
        let out = decide_cascade(0.2, &policy(0.8, 100.0));
        assert_eq!(
            out,
            CascadeOutcome::Accept {
                tier: CascadeTier::ToolVerify,
                confidence: 0.85,
                cost_spent: 6.0,
            }
        );
    }

    #[test]
    fn escalates_to_human_for_high_target() {
        let out = decide_cascade(0.1, &policy(0.95, 100.0));
        assert_eq!(
            out,
            CascadeOutcome::Accept {
                tier: CascadeTier::HumanExpert,
                confidence: 0.99,
                cost_spent: 56.0, // 1 + 5 + 50
            }
        );
    }

    #[test]
    fn budget_caps_escalation_returns_best_affordable() {
        // Target 0.95 needs the human tier, but budget only covers self+tool.
        let out = decide_cascade(0.1, &policy(0.95, 10.0));
        assert_eq!(
            out,
            CascadeOutcome::Exhausted {
                best_tier: Some(CascadeTier::ToolVerify),
                confidence: 0.85,
                cost_spent: 6.0,
            }
        );
    }

    #[test]
    fn budget_too_small_for_any_tier() {
        // Even the cheapest tier (cost 1.0) is unaffordable.
        let out = decide_cascade(0.1, &policy(0.6, 0.5));
        assert_eq!(
            out,
            CascadeOutcome::Exhausted {
                best_tier: None,
                confidence: 0.1,
                cost_spent: 0.0,
            }
        );
    }

    #[test]
    fn empty_tiers_is_exhausted() {
        let p = CascadePolicy {
            confidence_target: 0.8,
            budget: 100.0,
            tiers: vec![],
        };
        let out = decide_cascade(0.2, &p);
        assert_eq!(
            out,
            CascadeOutcome::Exhausted {
                best_tier: None,
                confidence: 0.2,
                cost_spent: 0.0,
            }
        );
    }

    // --- run_cascade (Slice 5: observed-confidence orchestration) ---

    /// A mock tier runner: returns the observed confidence the test prescribes
    /// per tier, with knowledge tagged by tier.
    fn runner(
        observed: std::collections::HashMap<CascadeTier, f64>,
    ) -> impl FnMut(CascadeTier) -> Result<TierResult, String> {
        move |tier| {
            let confidence = *observed.get(&tier).unwrap_or(&0.0);
            Ok(TierResult {
                knowledge: format!("{tier:?} knowledge"),
                confidence,
            })
        }
    }

    #[test]
    fn run_already_confident_runs_nothing() {
        let run = run_cascade(0.9, &policy(0.8, 100.0), runner(Default::default())).unwrap();
        assert!(run.steps.is_empty());
        assert_eq!(run.accepted_tier, None);
        assert_eq!(run.knowledge, None);
        assert_eq!(run.confidence, 0.9);
        assert_eq!(run.cost_spent, 0.0);
    }

    #[test]
    fn run_stops_at_cheapest_tier_that_observes_target() {
        // Self-reflect's *observed* confidence (0.9) clears target 0.8 even
        // though its *predicted* expected_confidence (0.6) would not — proving
        // the loop escalates on observation, not prediction.
        let observed = [(CascadeTier::SelfReflect, 0.9)].into_iter().collect();
        let run = run_cascade(0.2, &policy(0.8, 100.0), runner(observed)).unwrap();
        assert_eq!(run.accepted_tier, Some(CascadeTier::SelfReflect));
        assert_eq!(run.steps.len(), 1);
        assert_eq!(run.cost_spent, 1.0);
        assert_eq!(run.confidence, 0.9);
        assert_eq!(run.knowledge.as_deref(), Some("SelfReflect knowledge"));
    }

    #[test]
    fn run_escalates_on_low_observed_confidence() {
        // Self-reflect under-delivers (0.4 < 0.8), tool-verify clears it (0.85).
        let observed = [
            (CascadeTier::SelfReflect, 0.4),
            (CascadeTier::ToolVerify, 0.85),
        ]
        .into_iter()
        .collect();
        let run = run_cascade(0.2, &policy(0.8, 100.0), runner(observed)).unwrap();
        assert_eq!(run.accepted_tier, Some(CascadeTier::ToolVerify));
        assert_eq!(run.steps.len(), 2);
        assert_eq!(run.cost_spent, 6.0); // 1 + 5
        assert_eq!(run.knowledge.as_deref(), Some("ToolVerify knowledge"));
    }

    #[test]
    fn run_budget_caps_escalation_keeps_best_knowledge() {
        // Target 0.95, but budget covers only self+tool (cost 6, not 56).
        // Neither clears the target; the best knowledge (tool, 0.85) is kept.
        let observed = [
            (CascadeTier::SelfReflect, 0.5),
            (CascadeTier::ToolVerify, 0.85),
            (CascadeTier::HumanExpert, 0.99),
        ]
        .into_iter()
        .collect();
        let run = run_cascade(0.1, &policy(0.95, 10.0), runner(observed)).unwrap();
        assert_eq!(run.accepted_tier, None); // never reached target
        assert_eq!(run.steps.len(), 2); // human tier unaffordable
        assert_eq!(run.cost_spent, 6.0);
        assert_eq!(run.confidence, 0.85); // best observed
        assert_eq!(run.knowledge.as_deref(), Some("ToolVerify knowledge"));
    }

    #[test]
    fn run_propagates_runner_error() {
        let err = run_cascade(0.2, &policy(0.8, 100.0), |_tier| {
            Err("tool exploded".to_string())
        });
        assert_eq!(err, Err("tool exploded".to_string()));
    }

    // --- run_cascade_async: same logic, awaited tier runners ---

    /// An async mock runner: returns the prescribed observed confidence per
    /// tier, tagging knowledge by tier. Records which tiers actually ran so a
    /// test can assert lazy escalation (a later tier is never awaited once an
    /// earlier one clears the target).
    fn async_runner(
        observed: std::collections::HashMap<CascadeTier, f64>,
        ran: std::rc::Rc<std::cell::RefCell<Vec<CascadeTier>>>,
    ) -> impl FnMut(
        CascadeTier,
    ) -> std::pin::Pin<
        Box<dyn std::future::Future<Output = Result<TierResult, String>>>,
    > {
        move |tier| {
            let confidence = *observed.get(&tier).unwrap_or(&0.0);
            ran.borrow_mut().push(tier);
            Box::pin(async move {
                Ok(TierResult {
                    knowledge: format!("{tier:?} knowledge"),
                    confidence,
                })
            })
        }
    }

    #[tokio::test]
    async fn async_stops_at_cheapest_tier_that_observes_target() {
        let ran = std::rc::Rc::new(std::cell::RefCell::new(Vec::new()));
        let observed = [(CascadeTier::SelfReflect, 0.9)].into_iter().collect();
        let run = run_cascade_async(
            0.2,
            &policy(0.8, 100.0),
            async_runner(observed, ran.clone()),
        )
        .await
        .unwrap();
        assert_eq!(run.accepted_tier, Some(CascadeTier::SelfReflect));
        assert_eq!(run.cost_spent, 1.0);
        // Only the self-reflect tier was awaited — escalation is lazy.
        assert_eq!(*ran.borrow(), vec![CascadeTier::SelfReflect]);
    }

    #[tokio::test]
    async fn async_escalates_on_low_observed_confidence() {
        let ran = std::rc::Rc::new(std::cell::RefCell::new(Vec::new()));
        let observed = [
            (CascadeTier::SelfReflect, 0.4),
            (CascadeTier::ToolVerify, 0.85),
        ]
        .into_iter()
        .collect();
        let run = run_cascade_async(
            0.2,
            &policy(0.8, 100.0),
            async_runner(observed, ran.clone()),
        )
        .await
        .unwrap();
        assert_eq!(run.accepted_tier, Some(CascadeTier::ToolVerify));
        assert_eq!(run.cost_spent, 6.0);
        assert_eq!(
            *ran.borrow(),
            vec![CascadeTier::SelfReflect, CascadeTier::ToolVerify]
        );
    }

    #[tokio::test]
    async fn async_propagates_runner_error() {
        let err = run_cascade_async(0.2, &policy(0.8, 100.0), |_tier| async {
            Err::<TierResult, String>("tool exploded".to_string())
        })
        .await;
        assert_eq!(err, Err("tool exploded".to_string()));
    }
}