arcbox-core 0.6.3

Core orchestration layer for ArcBox
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
//! The balloon controller: single owner of the idle balloon.
//!
//! A message-driven task, commanded by the lifecycle actor on state
//! transitions (`EnterIdle` on entering `Idle`, `ExitIdle` on leaving it
//! for any reason). Every hypervisor balloon call happens here, serially —
//! no shared balloon state, no cross-task races.
//!
//! Modes:
//!
//! ```text
//! Active ──EnterIdle──▶ probe guest (entry_decision)
//!    ▲                     ├─ NotIdle ──activity()──▶ Active
//!    │                     ├─ Keep ────────────────▶ IdleUnshrunk (retry 30s)
//!    │                     └─ Shrink ─▶ step target ─▶ Watching ◀─┐
//!    │                                                  │  Settled + more to
//!    │                                                  │  shrink: next step,
//!    │        pressure / watch error / silence ≥ 25s    │  fresh watch ──────┘
//!    └────── restore full + activity() ◀───────────┬────▼
//!                                                  └─ Polling (30s stats,
//!                                                     step on healthy poll,
//!                                                     fail open on silence)
//! ```
//!
//! The descent is staged ([`super::SHRINK_STEP`] per move) because inflation
//! speed is not under host control and each step's scarcity window should be
//! short; the guest's `SETTLED` frame gates each further step.
//!
//! Exits always ride the lifecycle state machine: pressure and fail-open
//! paths restore full memory immediately, then note activity, which exits
//! `Idle` and resets the 5-minute idle clock — so a re-shrink requires a
//! fresh quiet period plus a fresh entry probe. Oscillation is structurally
//! impossible.

use std::future::Future;
use std::sync::Arc;
use std::time::Duration;

use tokio::sync::mpsc;

use crate::error::Result;

use super::{EntryDecision, GuestStats, entry_decision};

/// Command from the lifecycle actor.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(in crate::vm_lifecycle) enum BalloonCommand {
    /// The VM entered `Idle`: probe guest usage and shrink if worthwhile.
    EnterIdle,
    /// The VM left `Idle` (activity, stop, reboot): restore full memory.
    ExitIdle,
}

/// One frame observed on a pressure watch.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(in crate::vm_lifecycle) enum WatchFrame {
    /// Agent alive, no pressure.
    Keepalive,
    /// Guest memory pressure detected.
    Pressure,
    /// The guest settled at the current balloon target (detector armed).
    Settled,
    /// The watch window elapsed; re-open to keep watching.
    WindowElapsed,
}

/// An open guest pressure watch.
pub(in crate::vm_lifecycle) trait PressureWatch: Send {
    /// Waits for the next frame, at most `max_wait`. An error is either
    /// transport failure or silence past the keepalive budget — both mean
    /// the guest cannot be trusted to report pressure anymore.
    fn next_frame(&mut self, max_wait: Duration)
    -> impl Future<Output = Result<WatchFrame>> + Send;
}

/// What the controller needs from the world, narrow enough to fake in tests.
pub(in crate::vm_lifecycle) trait BalloonDeps:
    Send + Sync + 'static
{
    /// The watch type produced by [`Self::open_pressure_watch`].
    type Watch: PressureWatch;

    /// Whether inflating the balloon actually releases host memory on the
    /// current backend. When `false`, idle shrinking is pure cost (guest
    /// scarcity + reclaim CPU) with zero host-side benefit, and the
    /// controller must not shrink at all. No macOS backend qualifies
    /// today — see the module docs for the per-backend measurements (VZ:
    /// Apple no-op; HV: Darwin-inert `MADV_DONTNEED`) and what flipping a
    /// backend requires.
    fn reclaim_capable(&self) -> bool;

    /// Configured full memory of the machine, if the machine record exists.
    fn full_memory_bytes(&self) -> Option<u64>;

    /// Applies a balloon target on the hypervisor device.
    fn set_balloon_target(&self, bytes: u64) -> Result<()>;

    /// Guest memory + load snapshot within `budget`; `None` = unreachable.
    fn guest_stats(&self, budget: Duration) -> impl Future<Output = Option<GuestStats>> + Send;

    /// Opens a guest pressure watch. An error means the watch mechanism is
    /// unavailable (old agent, transport limits) — the controller degrades
    /// to polling.
    fn open_pressure_watch(&self) -> impl Future<Output = Result<Self::Watch>> + Send;
}

/// Retry cadence while idle but not (yet) shrunk.
const IDLE_ENTRY_RETRY: Duration = Duration::from_secs(30);

/// Minimum dwell at each descent step before taking the next one.
///
/// The guest's `SETTLED` frame proves the guest is healthy at the current
/// *memory level*, not that inflation caught up with the target — inflation
/// lags and its speed is not under host control. Hardware-validated: without
/// a dwell the descent outran inflation by many GiB and the deferred squeeze
/// OOM-killed the workload faster than the 1s sampler could see. The dwell
/// keeps the un-inflated lag bounded to roughly one step.
const STEP_DWELL: Duration = Duration::from_secs(15);

/// Maximum silence on an open watch before failing open. Must exceed the
/// agent keepalive cadence (10s) with margin.
const WATCH_SILENCE_DEADLINE: Duration = Duration::from_secs(25);

/// Poll cadence in degraded (no-watch) mode.
const DEGRADED_POLL_INTERVAL: Duration = Duration::from_secs(30);

/// `MemAvailable` floor for pressure, both as the watch request threshold
/// and the degraded-poll check. Safely below the entry headroom (256 MiB).
pub(in crate::vm_lifecycle) const PRESSURE_MIN_AVAILABLE: u64 = 128 * 1024 * 1024;

/// Refault-rate threshold (pages/second) for the watch request. Disabled:
/// hardware validation showed the post-shrink page-cache rewarm produces
/// refault bursts that outlive any warm-up, and refaulting with healthy
/// `MemAvailable` is cache warming, not pressure — the floor plus the
/// silence fail-open cover the thrash class. The plumbing stays host-
/// tunable for a future smarter signal (e.g. PSI).
pub(in crate::vm_lifecycle) const PRESSURE_MAX_REFAULT_RATE: u64 = 0;

/// PSI trigger threshold requested from the agent: microseconds of full
/// (all non-idle tasks) memory stall within the agent's 1s window. Only
/// effective on kernels with `CONFIG_PSI`; older guests sample instead.
pub(in crate::vm_lifecycle) const PRESSURE_PSI_FULL_STALL_US: u64 = 100_000;

/// Watch window requested from the agent; the watch is re-opened when it
/// elapses.
pub(in crate::vm_lifecycle) const WATCH_WINDOW: Duration = Duration::from_secs(300);

/// Keepalive cadence requested from the agent.
pub(in crate::vm_lifecycle) const WATCH_KEEPALIVE: Duration = Duration::from_secs(10);

/// Budget for the entry-time guest stats probe.
const GUEST_STATS_TIMEOUT: Duration = Duration::from_secs(super::GUEST_STATS_TIMEOUT_SECS);

/// The controller task. Owns all balloon state; communicates with the world
/// only through [`BalloonDeps`] (in) and the activity callback (out).
pub(in crate::vm_lifecycle) struct BalloonController<D: BalloonDeps> {
    commands: mpsc::UnboundedReceiver<BalloonCommand>,
    deps: D,
    /// Notes lifecycle activity: resets the idle clock and exits `Idle`.
    activity: Arc<dyn Fn() + Send + Sync>,
    /// Currently applied balloon target while shrunk.
    applied: Option<u64>,
    /// Final descent target; `applied > final` means more steps pending.
    final_target: Option<u64>,
    /// When the current step was applied (dwell pacing).
    step_applied_at: Option<tokio::time::Instant>,
}

/// Controller mode; holds the open watch so drops cancel it naturally.
enum Mode<W> {
    /// VM not idle; balloon at full memory.
    Active,
    /// VM idle, balloon not shrunk (no stats yet / nothing to reclaim);
    /// entry is retried on a timer.
    IdleUnshrunk,
    /// Balloon shrunk; guest pressure watch open.
    Watching(W),
    /// Guest settled before the step dwell elapsed: keep watching for
    /// pressure, take the next step when the dwell timer fires.
    Dwelling {
        /// The open watch (pressure still exits during the dwell).
        watch: W,
        /// When the dwell completes.
        until: tokio::time::Instant,
    },
    /// Balloon shrunk; watch unavailable — polling stats instead.
    Polling,
}

impl<D: BalloonDeps> BalloonController<D> {
    pub(in crate::vm_lifecycle) fn new(
        commands: mpsc::UnboundedReceiver<BalloonCommand>,
        deps: D,
        activity: Arc<dyn Fn() + Send + Sync>,
    ) -> Self {
        Self {
            commands,
            deps,
            activity,
            applied: None,
            final_target: None,
            step_applied_at: None,
        }
    }

    /// Runs until the command channel closes (actor drop).
    pub(in crate::vm_lifecycle) async fn run(mut self) {
        let mut mode = Mode::Active;
        loop {
            mode = match mode {
                Mode::Active => match self.commands.recv().await {
                    Some(BalloonCommand::EnterIdle) => self.enter_idle().await,
                    Some(BalloonCommand::ExitIdle) => Mode::Active,
                    None => return,
                },
                Mode::IdleUnshrunk => {
                    tokio::select! {
                        cmd = self.commands.recv() => match cmd {
                            // Nothing was shrunk; nothing to restore.
                            Some(BalloonCommand::ExitIdle) => Mode::Active,
                            Some(BalloonCommand::EnterIdle) => Mode::IdleUnshrunk,
                            None => return,
                        },
                        () = tokio::time::sleep(IDLE_ENTRY_RETRY) => self.enter_idle().await,
                    }
                }
                Mode::Watching(mut watch) => {
                    tokio::select! {
                        cmd = self.commands.recv() => match cmd {
                            Some(BalloonCommand::ExitIdle) => {
                                self.restore("idle exit");
                                Mode::Active
                            }
                            Some(BalloonCommand::EnterIdle) => Mode::Watching(watch),
                            None => return,
                        },
                        frame = watch.next_frame(WATCH_SILENCE_DEADLINE) => match frame {
                            Ok(WatchFrame::Keepalive) => Mode::Watching(watch),
                            Ok(WatchFrame::Settled) => {
                                if self.next_pending_step().is_none() {
                                    // Final target reached; keep watching.
                                    Mode::Watching(watch)
                                } else {
                                    // The guest settled at this memory level,
                                    // but inflation lags the target: hold the
                                    // remaining dwell (still watching for
                                    // pressure) before the next step.
                                    let applied_at = self
                                        .step_applied_at
                                        .unwrap_or_else(tokio::time::Instant::now);
                                    Mode::Dwelling {
                                        watch,
                                        until: applied_at + STEP_DWELL,
                                    }
                                }
                            }
                            Ok(WatchFrame::WindowElapsed) => self.open_watch().await,
                            Ok(WatchFrame::Pressure) => self.fail_open("guest memory pressure"),
                            Err(e) => self.fail_open(&format!(
                                "pressure watch lost ({e}); guest may be too starved to answer"
                            )),
                        },
                    }
                }
                Mode::Dwelling { mut watch, until } => {
                    tokio::select! {
                        cmd = self.commands.recv() => match cmd {
                            Some(BalloonCommand::ExitIdle) => {
                                self.restore("idle exit");
                                Mode::Active
                            }
                            Some(BalloonCommand::EnterIdle) => Mode::Dwelling { watch, until },
                            None => return,
                        },
                        () = tokio::time::sleep_until(until) => {
                            // Each further step needs a fresh settling
                            // detector: drop this watch before moving.
                            drop(watch);
                            self.continue_descent().await
                        }
                        frame = watch.next_frame(WATCH_SILENCE_DEADLINE) => match frame {
                            Ok(WatchFrame::Keepalive | WatchFrame::Settled) => {
                                Mode::Dwelling { watch, until }
                            }
                            Ok(WatchFrame::WindowElapsed) => match self.open_watch().await {
                                Mode::Watching(watch) => Mode::Dwelling { watch, until },
                                other => other,
                            },
                            Ok(WatchFrame::Pressure) => self.fail_open("guest memory pressure"),
                            Err(e) => self.fail_open(&format!(
                                "pressure watch lost ({e}); guest may be too starved to answer"
                            )),
                        },
                    }
                }
                Mode::Polling => {
                    tokio::select! {
                        cmd = self.commands.recv() => match cmd {
                            Some(BalloonCommand::ExitIdle) => {
                                self.restore("idle exit");
                                Mode::Active
                            }
                            Some(BalloonCommand::EnterIdle) => Mode::Polling,
                            None => return,
                        },
                        () = tokio::time::sleep(DEGRADED_POLL_INTERVAL) => {
                            match self.deps.guest_stats(GUEST_STATS_TIMEOUT).await {
                                None => self.fail_open("agent unreachable while shrunk"),
                                Some(s) if s.available < PRESSURE_MIN_AVAILABLE => {
                                    self.fail_open("guest available memory below floor")
                                }
                                // A healthy poll is the degraded mode's
                                // settle signal: take the next step.
                                Some(_) => match self.next_pending_step() {
                                    Some(next) => {
                                        self.apply_step(next);
                                        Mode::Polling
                                    }
                                    None => Mode::Polling,
                                },
                            }
                        }
                    }
                }
            };
        }
    }

    /// Entry probe: size the balloon from clean (balloon-empty) stats.
    async fn enter_idle(&mut self) -> Mode<D::Watch> {
        if !self.deps.reclaim_capable() {
            // No retry timer: the answer is a property of the backend, not
            // a transient. The next real idle entry re-asks (the backend
            // can change across a VM recreate).
            tracing::info!(
                "idle balloon disabled: backend does not release ballooned memory to the host"
            );
            return Mode::Active;
        }
        let Some(full) = self.deps.full_memory_bytes() else {
            return Mode::IdleUnshrunk;
        };
        let stats = self.deps.guest_stats(GUEST_STATS_TIMEOUT).await;
        // Commands that arrived while the probe was in flight outrank its
        // result: a Docker request during the (up to 3s) stats query must
        // not see its VM shrunk right after exiting idle.
        loop {
            match self.commands.try_recv() {
                Ok(BalloonCommand::ExitIdle) => return Mode::Active,
                Ok(BalloonCommand::EnterIdle) => {}
                Err(_) => break,
            }
        }
        match entry_decision(stats, full) {
            EntryDecision::NotIdle => {
                tracing::info!("guest is busy; exiting idle instead of shrinking");
                (self.activity)();
                Mode::Active
            }
            EntryDecision::Keep => Mode::IdleUnshrunk,
            EntryDecision::Shrink(final_target) => {
                let first = super::next_step(full, final_target);
                if let Err(e) = self.deps.set_balloon_target(first) {
                    tracing::warn!("failed to shrink idle balloon: {e}");
                    return Mode::IdleUnshrunk;
                }
                self.applied = Some(first);
                self.final_target = Some(final_target);
                self.step_applied_at = Some(tokio::time::Instant::now());
                tracing::info!(
                    target_mb = first / (1024 * 1024),
                    final_mb = final_target / (1024 * 1024),
                    "idle balloon shrunk to guest usage + headroom"
                );
                self.open_watch().await
            }
        }
    }

    /// The next descent step, if the applied target is above the final one.
    fn next_pending_step(&self) -> Option<u64> {
        let (applied, final_target) = (self.applied?, self.final_target?);
        (applied > final_target).then(|| super::next_step(applied, final_target))
    }

    /// Applies one descent step (best effort — a failed step leaves the
    /// current, already-settled target in place).
    fn apply_step(&mut self, next: u64) {
        match self.deps.set_balloon_target(next) {
            Ok(()) => {
                self.applied = Some(next);
                self.step_applied_at = Some(tokio::time::Instant::now());
                tracing::info!(target_mb = next / (1024 * 1024), "idle balloon step");
            }
            Err(e) => tracing::warn!("failed to step idle balloon: {e}"),
        }
    }

    /// Continues the staged descent after the guest settled, or keeps
    /// watching at the final target.
    async fn continue_descent(&mut self) -> Mode<D::Watch> {
        if let Some(next) = self.next_pending_step() {
            self.apply_step(next);
        }
        self.open_watch().await
    }

    /// Opens (or re-opens) the pressure watch, degrading to polling when
    /// the agent doesn't support it.
    async fn open_watch(&self) -> Mode<D::Watch> {
        match self.deps.open_pressure_watch().await {
            Ok(watch) => Mode::Watching(watch),
            Err(e) => {
                tracing::info!("pressure watch unavailable ({e}); polling guest stats instead");
                Mode::Polling
            }
        }
    }

    /// Restores full memory and exits idle via the state machine.
    fn fail_open(&mut self, reason: &str) -> Mode<D::Watch> {
        tracing::info!(reason, "restoring full memory");
        self.restore(reason);
        (self.activity)();
        Mode::Active
    }

    /// Restores the balloon to the machine's full configured memory.
    ///
    /// Retried a few times on failure; if all attempts fail, the shrunk
    /// bookkeeping is kept so the next idle entry knows memory was never
    /// given back (a restore that silently "succeeds" in bookkeeping only
    /// would leave the guest squeezed with nobody retrying).
    fn restore(&mut self, reason: &str) {
        let Some(full) = self.deps.full_memory_bytes() else {
            // No machine record: the VM is gone, and so is its balloon.
            self.applied = None;
            self.final_target = None;
            self.step_applied_at = None;
            return;
        };
        const RESTORE_ATTEMPTS: u32 = 3;
        for attempt in 1..=RESTORE_ATTEMPTS {
            match self.deps.set_balloon_target(full) {
                Ok(()) => {
                    self.applied = None;
                    self.final_target = None;
                    self.step_applied_at = None;
                    tracing::info!(
                        full_mb = full / (1024 * 1024),
                        reason,
                        "balloon restored to full memory"
                    );
                    return;
                }
                Err(e) => tracing::warn!(attempt, "failed to restore balloon ({reason}): {e}"),
            }
        }
        tracing::error!(
            reason,
            "balloon restore failed; guest remains shrunk until the next idle cycle"
        );
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error::CoreError;
    use std::sync::Mutex;
    use std::sync::atomic::{AtomicUsize, Ordering};

    const MIB: u64 = 1024 * 1024;
    const GIB: u64 = 1024 * MIB;
    const FULL: u64 = 16 * GIB;

    /// Scripted watch: yields frames from a channel; `None` (closed) is a
    /// transport error.
    struct FakeWatch {
        frames: mpsc::UnboundedReceiver<WatchFrame>,
    }

    impl PressureWatch for FakeWatch {
        async fn next_frame(&mut self, max_wait: Duration) -> Result<WatchFrame> {
            match tokio::time::timeout(max_wait, self.frames.recv()).await {
                Ok(Some(frame)) => Ok(frame),
                Ok(None) => Err(CoreError::Machine("watch transport closed".into())),
                Err(_) => Err(CoreError::Machine("watch silence deadline".into())),
            }
        }
    }

    /// Test double with scripted stats/watch behavior and recorded targets.
    struct FakeDeps {
        /// Backend reclaim capability (default `true`: the shrink paths
        /// under test assume an HV-class backend).
        reclaim_capable: bool,
        full: Option<u64>,
        stats: Mutex<Vec<Option<GuestStats>>>,
        /// Injected latency for `guest_stats` (probe-race tests).
        stats_delay: Duration,
        targets: Arc<Mutex<Vec<u64>>>,
        /// When set, `set_balloon_target` fails (attempts still counted).
        fail_set_target: std::sync::atomic::AtomicBool,
        set_attempts: Arc<AtomicUsize>,
        watch_supported: bool,
        watch_frames: Mutex<Vec<mpsc::UnboundedReceiver<WatchFrame>>>,
        watch_senders: Mutex<Vec<mpsc::UnboundedSender<WatchFrame>>>,
        watches_opened: Arc<AtomicUsize>,
    }

    impl FakeDeps {
        fn new(full: Option<u64>) -> Self {
            Self {
                reclaim_capable: true,
                full,
                stats: Mutex::new(Vec::new()),
                stats_delay: Duration::ZERO,
                targets: Arc::new(Mutex::new(Vec::new())),
                fail_set_target: std::sync::atomic::AtomicBool::new(false),
                set_attempts: Arc::new(AtomicUsize::new(0)),
                watch_supported: true,
                watch_frames: Mutex::new(Vec::new()),
                watch_senders: Mutex::new(Vec::new()),
                watches_opened: Arc::new(AtomicUsize::new(0)),
            }
        }

        /// Queues the reply for the next `guest_stats` call (LIFO-safe: we
        /// only ever queue in test order and pop front).
        fn push_stats(&self, stats: Option<GuestStats>) {
            self.stats.lock().unwrap().push(stats);
        }

        /// Queues frames for the next opened watch and returns the sender.
        fn push_watch(&self) -> mpsc::UnboundedSender<WatchFrame> {
            let (tx, rx) = mpsc::unbounded_channel();
            self.watch_frames.lock().unwrap().push(rx);
            self.watch_senders.lock().unwrap().push(tx.clone());
            tx
        }
    }

    impl BalloonDeps for Arc<FakeDeps> {
        type Watch = FakeWatch;

        fn reclaim_capable(&self) -> bool {
            self.reclaim_capable
        }

        fn full_memory_bytes(&self) -> Option<u64> {
            self.full
        }

        fn set_balloon_target(&self, bytes: u64) -> Result<()> {
            self.set_attempts.fetch_add(1, Ordering::SeqCst);
            if self.fail_set_target.load(Ordering::SeqCst) {
                return Err(CoreError::Machine("injected balloon failure".into()));
            }
            self.targets.lock().unwrap().push(bytes);
            Ok(())
        }

        async fn guest_stats(&self, _budget: Duration) -> Option<GuestStats> {
            if self.stats_delay > Duration::ZERO {
                tokio::time::sleep(self.stats_delay).await;
            }
            let mut stats = self.stats.lock().unwrap();
            if stats.is_empty() {
                None
            } else {
                stats.remove(0)
            }
        }

        async fn open_pressure_watch(&self) -> Result<FakeWatch> {
            self.watches_opened.fetch_add(1, Ordering::SeqCst);
            if !self.watch_supported {
                return Err(CoreError::Agent {
                    code: 400,
                    message: "invalid request: unexpected message type".into(),
                });
            }
            let mut queued = self.watch_frames.lock().unwrap();
            if queued.is_empty() {
                // A watch with no scripted frames: stays silent (the sender
                // is leaked so the channel never reports closed).
                let (tx, rx) = mpsc::unbounded_channel();
                std::mem::forget(tx);
                Ok(FakeWatch { frames: rx })
            } else {
                Ok(FakeWatch {
                    frames: queued.remove(0),
                })
            }
        }
    }

    impl Harness {
        fn watch_senders(&self) -> Vec<mpsc::UnboundedSender<WatchFrame>> {
            self.deps.watch_senders.lock().unwrap().clone()
        }
    }

    struct Harness {
        deps: Arc<FakeDeps>,
        commands: mpsc::UnboundedSender<BalloonCommand>,
        activity: Arc<AtomicUsize>,
        task: tokio::task::JoinHandle<()>,
    }

    impl Harness {
        fn spawn(deps: FakeDeps) -> Self {
            let deps = Arc::new(deps);
            let (tx, rx) = mpsc::unbounded_channel();
            let activity = Arc::new(AtomicUsize::new(0));
            let activity_probe = Arc::clone(&activity);
            let controller = BalloonController::new(
                rx,
                Arc::clone(&deps),
                Arc::new(move || {
                    activity_probe.fetch_add(1, Ordering::SeqCst);
                }),
            );
            let task = tokio::spawn(controller.run());
            Self {
                deps,
                commands: tx,
                activity,
                task,
            }
        }

        fn targets(&self) -> Vec<u64> {
            self.deps.targets.lock().unwrap().clone()
        }

        fn activity_count(&self) -> usize {
            self.activity.load(Ordering::SeqCst)
        }

        /// Lets the paused-clock runtime drive the controller until quiescent.
        async fn settle(&self) {
            for _ in 0..50 {
                tokio::task::yield_now().await;
            }
        }
    }

    impl Drop for Harness {
        fn drop(&mut self) {
            self.task.abort();
        }
    }

    fn idle_stats() -> GuestStats {
        // 4 GiB used, quiet guest.
        GuestStats {
            total: FULL,
            available: 12 * GIB,
            loadavg1: 0.1,
        }
    }

    /// Final descent target for `idle_stats` (usage + headroom).
    const FINAL_TARGET: u64 = 4 * GIB + super::super::IDLE_BALLOON_HEADROOM;
    /// First staged step from 16 GiB full memory.
    const FIRST_STEP: u64 = FULL - super::super::SHRINK_STEP;

    /// A reclaim-incapable backend (today: every macOS backend) must make
    /// idle entry fully inert: no stats probe, no shrink, no retry timer —
    /// and the controller stays responsive to later cycles.
    #[tokio::test(start_paused = true)]
    async fn reclaim_incapable_backend_never_shrinks() {
        let mut deps = FakeDeps::new(Some(FULL));
        deps.reclaim_capable = false;
        deps.push_stats(Some(idle_stats()));
        let h = Harness::spawn(deps);

        h.commands.send(BalloonCommand::EnterIdle).unwrap();
        h.settle().await;
        // Well past IDLE_ENTRY_RETRY: no timer may have re-entered.
        tokio::time::advance(Duration::from_secs(120)).await;
        h.settle().await;

        assert_eq!(h.targets(), Vec::<u64>::new());
        assert_eq!(h.deps.set_attempts.load(Ordering::SeqCst), 0);
        // The gate fires before the stats probe: the scripted reply is
        // still queued.
        assert_eq!(h.deps.stats.lock().unwrap().len(), 1);
        assert_eq!(h.deps.watches_opened.load(Ordering::SeqCst), 0);

        // Still alive for the next cycle.
        h.commands.send(BalloonCommand::ExitIdle).unwrap();
        h.commands.send(BalloonCommand::EnterIdle).unwrap();
        h.settle().await;
        assert_eq!(h.deps.set_attempts.load(Ordering::SeqCst), 0);
        assert_eq!(h.activity_count(), 0);
    }

    #[tokio::test(start_paused = true)]
    async fn enter_idle_takes_first_staged_step_and_watches() {
        let deps = FakeDeps::new(Some(FULL));
        deps.push_stats(Some(idle_stats()));
        let _watch = deps.push_watch();
        let h = Harness::spawn(deps);

        h.commands.send(BalloonCommand::EnterIdle).unwrap();
        h.settle().await;

        // Usage-aware final, but only one SHRINK_STEP applied up front.
        assert_eq!(h.targets(), vec![FIRST_STEP]);
        assert_eq!(h.deps.watches_opened.load(Ordering::SeqCst), 1);
        assert_eq!(h.activity_count(), 0);
    }

    /// Each guest `Settled` frame gates the next step; every step gets a
    /// fresh watch (fresh settling detector).
    #[tokio::test(start_paused = true)]
    async fn staged_descent_steps_on_settled_frames() {
        const STEP: u64 = super::super::SHRINK_STEP;
        let deps = FakeDeps::new(Some(FULL));
        deps.push_stats(Some(idle_stats()));
        let watches: Vec<_> = (0..6).map(|_| deps.push_watch()).collect();
        let h = Harness::spawn(deps);

        h.commands.send(BalloonCommand::EnterIdle).unwrap();
        h.settle().await;

        let expected = [
            FULL - STEP,     // 14 GiB
            FULL - 2 * STEP, // 12 GiB
            FULL - 3 * STEP, // 10 GiB
            FULL - 4 * STEP, // 8 GiB
            FULL - 5 * STEP, // 6 GiB
            FINAL_TARGET,    // clamp
        ];
        for (i, watch) in watches.iter().enumerate().take(expected.len() - 1) {
            watch.send(WatchFrame::Settled).unwrap();
            h.settle().await;
            assert_eq!(
                h.targets(),
                expected[..=i].to_vec(),
                "settling alone must not step — the dwell paces the descent"
            );
            tokio::time::advance(STEP_DWELL).await;
            h.settle().await;
            assert_eq!(h.targets(), expected[..i + 2].to_vec());
        }
        assert_eq!(
            h.deps.watches_opened.load(Ordering::SeqCst),
            expected.len(),
            "each step must get a fresh watch"
        );
        assert_eq!(h.activity_count(), 0, "descent is not activity");

        // At the final target, further Settled frames change nothing.
        watches[5].send(WatchFrame::Settled).unwrap();
        h.settle().await;
        assert_eq!(h.targets(), expected.to_vec());
    }

    #[tokio::test(start_paused = true)]
    async fn pressure_mid_descent_restores_full() {
        let deps = FakeDeps::new(Some(FULL));
        deps.push_stats(Some(idle_stats()));
        let first = deps.push_watch();
        let _second = deps.push_watch();
        let h = Harness::spawn(deps);

        h.commands.send(BalloonCommand::EnterIdle).unwrap();
        h.settle().await;
        first.send(WatchFrame::Settled).unwrap();
        h.settle().await;
        tokio::time::advance(STEP_DWELL).await;
        h.settle().await;

        // Pressure during the second step: full restore, not a re-step.
        h.watch_senders()[1].send(WatchFrame::Pressure).unwrap();
        h.settle().await;
        assert_eq!(
            h.targets(),
            vec![FIRST_STEP, FULL - 2 * super::super::SHRINK_STEP, FULL]
        );
        assert_eq!(h.activity_count(), 1);
    }

    #[tokio::test(start_paused = true)]
    async fn enter_idle_busy_guest_notes_activity_without_shrinking() {
        let deps = FakeDeps::new(Some(FULL));
        deps.push_stats(Some(GuestStats {
            loadavg1: 3.0,
            ..idle_stats()
        }));
        let h = Harness::spawn(deps);

        h.commands.send(BalloonCommand::EnterIdle).unwrap();
        h.settle().await;

        assert!(h.targets().is_empty(), "busy guest must not be shrunk");
        assert_eq!(h.activity_count(), 1, "controller must exit idle");
    }

    /// Incident guard: no stats ⇒ no shrink; the entry probe retries later
    /// instead of squeezing an unknown guest.
    #[tokio::test(start_paused = true)]
    async fn enter_idle_without_stats_retries_later() {
        let deps = FakeDeps::new(Some(FULL));
        // First probe: unreachable. Retry: reachable.
        deps.push_stats(None);
        deps.push_stats(Some(idle_stats()));
        let _watch = deps.push_watch();
        let h = Harness::spawn(deps);

        h.commands.send(BalloonCommand::EnterIdle).unwrap();
        h.settle().await;
        assert!(h.targets().is_empty(), "no stats ⇒ no shrink");

        tokio::time::advance(IDLE_ENTRY_RETRY).await;
        h.settle().await;
        assert_eq!(h.targets(), vec![FIRST_STEP], "retry must re-probe");
    }

    #[tokio::test(start_paused = true)]
    async fn pressure_event_restores_full_and_notes_activity() {
        let deps = FakeDeps::new(Some(FULL));
        deps.push_stats(Some(idle_stats()));
        let watch = deps.push_watch();
        let h = Harness::spawn(deps);

        h.commands.send(BalloonCommand::EnterIdle).unwrap();
        h.settle().await;

        watch.send(WatchFrame::Pressure).unwrap();
        h.settle().await;

        assert_eq!(h.targets(), vec![FIRST_STEP, FULL]);
        assert_eq!(h.activity_count(), 1);
    }

    /// Incident guard: a guest too starved to answer is exactly the one that
    /// needs its memory back — silence past the keepalive budget fails open.
    #[tokio::test(start_paused = true)]
    async fn watch_silence_fails_open() {
        let deps = FakeDeps::new(Some(FULL));
        deps.push_stats(Some(idle_stats()));
        let watch = deps.push_watch();
        let h = Harness::spawn(deps);

        h.commands.send(BalloonCommand::EnterIdle).unwrap();
        h.settle().await;
        assert_eq!(h.targets(), vec![FIRST_STEP]);

        // No frames at all: silence past the deadline.
        tokio::time::advance(WATCH_SILENCE_DEADLINE + Duration::from_secs(1)).await;
        h.settle().await;

        assert_eq!(h.targets(), vec![FIRST_STEP, FULL]);
        assert_eq!(h.activity_count(), 1);
        drop(watch);
    }

    #[tokio::test(start_paused = true)]
    async fn watch_transport_error_fails_open() {
        let deps = FakeDeps::new(Some(FULL));
        deps.push_stats(Some(idle_stats()));
        let watch = deps.push_watch();
        let h = Harness::spawn(deps);

        h.commands.send(BalloonCommand::EnterIdle).unwrap();
        h.settle().await;

        // Close the channel from every end (the deps keep a clone of each
        // sender for mid-descent scripting) → transport error.
        drop(watch);
        h.deps.watch_senders.lock().unwrap().clear();
        h.settle().await;

        assert_eq!(h.targets(), vec![FIRST_STEP, FULL]);
        assert_eq!(h.activity_count(), 1);
    }

    #[tokio::test(start_paused = true)]
    async fn window_elapsed_reopens_watch() {
        let deps = FakeDeps::new(Some(FULL));
        deps.push_stats(Some(idle_stats()));
        let first = deps.push_watch();
        let _second = deps.push_watch();
        let h = Harness::spawn(deps);

        h.commands.send(BalloonCommand::EnterIdle).unwrap();
        h.settle().await;

        first.send(WatchFrame::WindowElapsed).unwrap();
        h.settle().await;

        assert_eq!(h.deps.watches_opened.load(Ordering::SeqCst), 2);
        assert_eq!(
            h.targets(),
            vec![FIRST_STEP],
            "no spurious restore, no step"
        );
        assert_eq!(h.activity_count(), 0);
    }

    /// Old agents don't implement the watch RPC: the controller must degrade
    /// to stats polling and still fail open on unreachability.
    #[tokio::test(start_paused = true)]
    async fn watch_unsupported_degrades_to_polling_and_fails_open() {
        let mut deps = FakeDeps::new(Some(FULL));
        deps.watch_supported = false;
        deps.push_stats(Some(idle_stats())); // entry probe
        // First poll: healthy. Second poll: unreachable (no queued stats).
        deps.push_stats(Some(idle_stats()));
        let h = Harness::spawn(deps);

        h.commands.send(BalloonCommand::EnterIdle).unwrap();
        h.settle().await;
        assert_eq!(h.targets(), vec![FIRST_STEP]);

        tokio::time::advance(DEGRADED_POLL_INTERVAL).await;
        h.settle().await;
        assert_eq!(h.activity_count(), 0, "healthy poll must not fail open");
        assert_eq!(
            h.targets(),
            vec![FIRST_STEP, FULL - 2 * super::super::SHRINK_STEP],
            "a healthy poll is the degraded mode's settle signal"
        );

        tokio::time::advance(DEGRADED_POLL_INTERVAL).await;
        h.settle().await;
        assert_eq!(
            h.targets(),
            vec![FIRST_STEP, FULL - 2 * super::super::SHRINK_STEP, FULL]
        );
        assert_eq!(h.activity_count(), 1);
    }

    #[tokio::test(start_paused = true)]
    async fn polling_low_available_fails_open() {
        let mut deps = FakeDeps::new(Some(FULL));
        deps.watch_supported = false;
        deps.push_stats(Some(idle_stats())); // entry probe
        deps.push_stats(Some(GuestStats {
            available: PRESSURE_MIN_AVAILABLE / 2,
            ..idle_stats()
        }));
        let h = Harness::spawn(deps);

        h.commands.send(BalloonCommand::EnterIdle).unwrap();
        h.settle().await;

        tokio::time::advance(DEGRADED_POLL_INTERVAL).await;
        h.settle().await;

        assert_eq!(h.targets(), vec![FIRST_STEP, FULL]);
        assert_eq!(h.activity_count(), 1);
    }

    /// Review finding (greptile): a Docker request that lands during the
    /// entry probe must outrank the probe's result — the VM just exited
    /// idle and must not be shrunk behind the request's back.
    #[tokio::test(start_paused = true)]
    async fn exit_idle_during_entry_probe_wins_over_shrink() {
        let mut deps = FakeDeps::new(Some(FULL));
        deps.stats_delay = Duration::from_secs(2);
        deps.push_stats(Some(idle_stats()));
        let h = Harness::spawn(deps);

        h.commands.send(BalloonCommand::EnterIdle).unwrap();
        h.settle().await;
        // The probe is mid-flight; activity exits idle.
        h.commands.send(BalloonCommand::ExitIdle).unwrap();
        tokio::time::advance(Duration::from_secs(2)).await;
        h.settle().await;

        assert!(h.targets().is_empty(), "stale probe must not shrink");
        assert_eq!(h.activity_count(), 0);
    }

    /// Review finding (greptile): a failing restore must be retried, and a
    /// still-failing one must not be booked as restored.
    #[tokio::test(start_paused = true)]
    async fn failed_restore_retries_and_keeps_bookkeeping() {
        let deps = FakeDeps::new(Some(FULL));
        deps.push_stats(Some(idle_stats()));
        let watch = deps.push_watch();
        let h = Harness::spawn(deps);

        h.commands.send(BalloonCommand::EnterIdle).unwrap();
        h.settle().await;
        assert_eq!(h.targets(), vec![FIRST_STEP]);

        let attempts_before = h.deps.set_attempts.load(Ordering::SeqCst);
        h.deps.fail_set_target.store(true, Ordering::SeqCst);
        watch.send(WatchFrame::Pressure).unwrap();
        h.settle().await;

        assert_eq!(
            h.deps.set_attempts.load(Ordering::SeqCst) - attempts_before,
            3,
            "restore must be retried"
        );
        assert_eq!(h.targets(), vec![FIRST_STEP], "no restore was booked");
        assert_eq!(
            h.activity_count(),
            1,
            "idle exit still rides the state machine"
        );
    }

    #[tokio::test(start_paused = true)]
    async fn exit_idle_restores_full() {
        let deps = FakeDeps::new(Some(FULL));
        deps.push_stats(Some(idle_stats()));
        let _watch = deps.push_watch();
        let h = Harness::spawn(deps);

        h.commands.send(BalloonCommand::EnterIdle).unwrap();
        h.settle().await;
        h.commands.send(BalloonCommand::ExitIdle).unwrap();
        h.settle().await;

        assert_eq!(h.targets(), vec![FIRST_STEP, FULL]);
        assert_eq!(h.activity_count(), 0, "plain exit is not new activity");
    }

    #[tokio::test(start_paused = true)]
    async fn exit_idle_without_shrink_touches_nothing() {
        let deps = FakeDeps::new(Some(FULL));
        // Entry probe fails → IdleUnshrunk.
        let h = Harness::spawn(deps);

        h.commands.send(BalloonCommand::EnterIdle).unwrap();
        h.settle().await;
        h.commands.send(BalloonCommand::ExitIdle).unwrap();
        h.settle().await;

        assert!(h.targets().is_empty());
        assert_eq!(h.activity_count(), 0);
    }
}