oomtop-cli 0.2.1

oomtop: see the OOM coming. Terminal resource monitor for local AI work.
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
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
//! The sampling pipeline behind every frontend (SPEC §6, §6.2):
//! `Sampler → attribution (detect) → idle/orphan → adapters → history → OOM forecast → likely victim`,
//! plus the lineage journal (`oomtop-state`). Implements `oomtop_core::provider::SnapshotProvider`.

use oomtop_adapters::{probe, ProbeOptions};
use oomtop_collect::replay::{load_fixture, replay};
use oomtop_collect::{Sampler, SamplerOptions};
use oomtop_config::Config;
use oomtop_core::actions::ProtectContext;
use oomtop_core::attribution::LineageEntry;
use oomtop_core::forecast::forecast_oom_with;
use oomtop_core::headroom::HeadroomConfig;
use oomtop_core::history::History;
use oomtop_core::idle::{IdleConfig, IdleTracker};
use oomtop_core::provider::SnapshotProvider;
use oomtop_core::throttle::throttle_factor;
use oomtop_core::{GroupKind, ModelServer, ProcId, Sandbox, SandboxKind, Snapshot};
use oomtop_detect::Detector;
use oomtop_state::StateDb;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::time::Duration;

/// Lineage is persisted at most this often (plus on drop).
const LINEAGE_FLUSH_MS: u64 = 30_000;
/// In-memory lineage entries of processes not seen for this long are dropped after they were persisted
/// (a `(pid, start_time)` never comes back, and the journal keeps them for the next run). Keeps a long TUI
/// session during builds — thousands of short-lived compilers — from growing without bound (SPEC §14).
pub const LINEAGE_EVICT_MS: u64 = 5 * 60_000;
/// Ended-session lookups in the lineage journal are repeated at most this often per session.
pub const ENDED_SESSION_TTL_MS: u64 = 60_000;
/// On-demand (MCP) calls reuse the previous call's sample as the CPU baseline when it is this recent.
pub const ON_DEMAND_BASELINE_MS: u64 = 30_000;
/// Gap between the two samples of an on-demand call without a recent baseline.
pub const ON_DEMAND_GAP_MS: u64 = 150;

enum Input {
    Live(Box<Sampler>),
    Replay { frames: Vec<Snapshot>, next: usize },
}

pub struct Engine {
    input: Input,
    detector: Detector,
    state: Option<StateDb>,
    lineage: BTreeMap<ProcId, LineageEntry>,
    idle: IdleTracker,
    idle_cfg: IdleConfig,
    /// `thresholds.stale_*` (SPEC §7 "Staleness").
    stale_cfg: oomtop_core::staleness::StalenessConfig,
    /// Ended agent sessions as the lineage journal remembers them, with when that was looked up.
    ended_sessions: BTreeMap<String, (u64, Option<oomtop_core::staleness::EndedSession>)>,
    headroom_cfg: HeadroomConfig,
    history: History,
    probe_opts: ProbeOptions,
    adapters_enabled: bool,
    /// `general.other_users = false` → drop other users' processes before attribution.
    own_uid_only: Option<u32>,
    protected_names: Vec<String>,
    /// Two samples 150 ms apart per call (MCP / one-shot commands), or one when the previous call's sample
    /// is recent enough to be the CPU baseline.
    on_demand: bool,
    last_on_demand_ms: Option<u64>,
    last_flush_ms: u64,
    /// Learning allowed (machine profile refresh); `--no-learn` turns it off. Lineage is always kept.
    learn: bool,
    model_folders: Vec<PathBuf>,
    profile_checked: bool,
    /// The journal is read lazily for the processes of the first sample (not the whole 30-day journal).
    lineage_loaded: bool,
    /// The previous probe's model servers and sandboxes: adapter truth for the next attribution (SPEC §7
    /// signal #1), since the probe itself needs attributed groups.
    adapter_truth: (Vec<ModelServer>, Vec<Sandbox>),
    /// The state store is only read (`reclaim --dry-run`): lineage is loaded so the plan matches a real run,
    /// but nothing is recorded and the machine profile is not refreshed.
    state_read_only: bool,
    pub protect: ProtectContext,
    pub rule_errors: Vec<String>,
    /// Per-limit histories and container CPU counters (SPEC §8.3, §11; `oomtop_core::cluster`).
    isolation: oomtop_core::cluster::IsolationTracker,
}

impl Engine {
    /// Live engine for this machine (user rules from the default config dir).
    pub fn live(cfg: &Config, state: Option<StateDb>, on_demand: bool, offline: bool) -> Self {
        Self::live_in(cfg, state, on_demand, offline, None)
    }

    /// Live engine reading user rules from `config_dir/rules.d` (`--config` moves the config dir).
    pub fn live_in(
        cfg: &Config,
        state: Option<StateDb>,
        on_demand: bool,
        offline: bool,
        config_dir: Option<&Path>,
    ) -> Self {
        let opts = SamplerOptions {
            cadence: oomtop_collect::Cadence {
                host_ms: cfg.general.host_refresh_ms,
                procs_ms: cfg.general.refresh_ms,
                expensive_ms: cfg.general.expensive_refresh_ms,
            },
            marker_allowlist: cfg.privacy.marker_allowlist.clone(),
            ..Default::default()
        };
        Self::with_input(
            cfg,
            state,
            on_demand,
            offline,
            config_dir,
            Input::Live(Box::new(Sampler::platform_default(&opts))),
        )
    }

    /// Engine replaying a fixture file (`--replay`).
    pub fn replay(cfg: &Config, path: &Path) -> Result<Self, String> {
        Self::replay_in(cfg, path, None)
    }

    /// Replay engine with user rules from `config_dir/rules.d`. Never persists state, never probes adapters.
    pub fn replay_in(cfg: &Config, path: &Path, config_dir: Option<&Path>) -> Result<Self, String> {
        let fx = load_fixture(path).map_err(|e| e.to_string())?;
        let frames = replay(&fx);
        if frames.is_empty() {
            return Err(format!("{}: fixture has no frames", path.display()));
        }
        Ok(Self::with_input(
            cfg,
            None,
            false,
            true,
            config_dir,
            Input::Replay { frames, next: 0 },
        ))
    }

    fn with_input(
        cfg: &Config,
        state: Option<StateDb>,
        on_demand: bool,
        offline: bool,
        config_dir: Option<&Path>,
        input: Input,
    ) -> Self {
        let paths = match config_dir {
            Some(d) => oomtop_config::paths::ConfigPaths::new(d),
            None => oomtop_config::paths::ConfigPaths::default_user(),
        };
        let (detector, errs) = Detector::with_user_rules(Some(&paths.rules_d));
        Engine {
            input,
            detector,
            state,
            lineage: BTreeMap::new(),
            idle: IdleTracker::new(),
            idle_cfg: cfg.idle_config(),
            stale_cfg: cfg.staleness_config(),
            ended_sessions: BTreeMap::new(),
            headroom_cfg: cfg.headroom_config(),
            history: History::default(),
            probe_opts: ProbeOptions {
                timeout: Duration::from_millis(cfg.adapters.timeout_ms),
                http: !offline,
                ports: cfg.adapters.ports.clone(),
            },
            adapters_enabled: cfg.adapters.enabled,
            // SAFETY: getuid never fails.
            own_uid_only: (!cfg.general.other_users).then(|| unsafe { libc::getuid() }),
            protected_names: cfg.protected.names.clone(),
            on_demand,
            last_on_demand_ms: None,
            last_flush_ms: 0,
            learn: cfg.personalization.learn,
            model_folders: cfg
                .models
                .folders
                .iter()
                .map(|f| oomtop_config::paths::expand_tilde(f))
                .collect(),
            profile_checked: false,
            lineage_loaded: false,
            adapter_truth: (Vec::new(), Vec::new()),
            state_read_only: false,
            protect: ProtectContext::default(),
            rule_errors: errs.iter().map(|e| e.to_string()).collect(),
            isolation: oomtop_core::cluster::IsolationTracker::new(),
        }
    }

    fn raw_snapshot(&mut self) -> Snapshot {
        self.raw_snapshot_with(false)
    }

    /// `force`: run every source now (the TUI's second sample, taken 500 ms after the first so CPU % shows
    /// within a second, SPEC §6.2/§14).
    fn raw_snapshot_with(&mut self, force: bool) -> Snapshot {
        match &mut self.input {
            Input::Live(s) => {
                if self.on_demand {
                    // CPU % needs two samples. A previous call's sample younger than ON_DEMAND_BASELINE_MS
                    // is the baseline (CPU is then averaged since that call); otherwise take a baseline now.
                    let now = oomtop_collect::now_ms();
                    let fresh = self
                        .last_on_demand_ms
                        .is_some_and(|t| now.saturating_sub(t) <= ON_DEMAND_BASELINE_MS);
                    if !fresh {
                        s.sample_all();
                        std::thread::sleep(Duration::from_millis(ON_DEMAND_GAP_MS));
                    }
                    let snap = s.sample_all();
                    self.last_on_demand_ms = Some(oomtop_collect::now_ms());
                    snap
                } else if force {
                    s.sample_all()
                } else {
                    s.sample_once()
                }
            }
            Input::Replay { frames, next } => {
                let i = (*next).min(frames.len() - 1);
                *next += 1;
                frames[i].clone()
            }
        }
    }

    fn update_protect(&mut self, s: &Snapshot) {
        let me = s.self_pid.and_then(|p| s.process_by_pid(p));
        let mut ancestors = Vec::new();
        let mut cur = me.and_then(|p| p.ppid);
        while let Some(pid) = cur.filter(|p| *p > 1 && ancestors.len() < 64) {
            ancestors.push(pid);
            cur = s.process_by_pid(pid).and_then(|p| p.ppid);
        }
        self.protect.self_pid = s.self_pid;
        self.protect.self_uid = me.and_then(|p| p.uid);
        self.protect.ancestor_pids = ancestors;
        self.protect.protected_names = self.protected_names.clone();
    }

    fn record_lineage(&mut self, s: &Snapshot) {
        let now = s.taken_at_ms;
        let by_id: std::collections::HashMap<ProcId, &oomtop_core::Process> =
            s.processes.iter().map(|p| (p.id, p)).collect();
        for g in &s.groups {
            let agent_group = g.kind == GroupKind::AgentSession;
            for m in &g.members {
                let p = by_id.get(&m.id).copied();
                let spawned = agent_group || p.map(|p| p.markers.session_id.is_some()).unwrap_or(false);
                let e = self.lineage.entry(m.id).or_insert_with(|| LineageEntry {
                    first_seen_ms: now,
                    ..Default::default()
                });
                let keep_agent = e.spawned_by_agent && !spawned;
                e.id = m.id;
                // Keep the original parent: the journal exists to remember it after re-parenting.
                if e.ppid.map(|pp| pp <= 1).unwrap_or(true) {
                    e.ppid = p.and_then(|p| p.ppid);
                }
                // Assign only what changed: ~700 processes × 3 strings per refresh otherwise (SPEC §14).
                if !keep_agent {
                    if e.group_id != g.id {
                        e.group_id = g.id.clone();
                    }
                    e.group_kind = g.kind;
                    if e.group_label != g.label {
                        e.group_label = g.label.clone();
                    }
                    if e.group_fingerprint != g.fingerprint {
                        e.group_fingerprint = g.fingerprint.clone();
                    }
                }
                if let Some(sid) = p.and_then(|p| p.markers.session_id.as_ref()) {
                    if e.session_id.as_ref() != Some(sid) {
                        e.session_id = Some(sid.clone());
                    }
                }
                e.spawned_by_agent |= spawned;
                e.last_seen_ms = now;
                if let Some(t) = self.idle.last_active_ms(m.id) {
                    e.last_active_ms = e.last_active_ms.max(t);
                }
            }
        }
        if now.saturating_sub(self.last_flush_ms) >= LINEAGE_FLUSH_MS || self.on_demand {
            self.flush();
            self.last_flush_ms = now;
            self.evict_lineage(now);
        }
    }

    /// Loads the journal entries of the processes in the first sample (re-parented children keep their
    /// agent across oomtop restarts) and seeds the idle tracker with them.
    fn load_lineage(&mut self, s: &Snapshot) {
        if self.lineage_loaded {
            return;
        }
        self.lineage_loaded = true;
        let Some(db) = self.state.as_ref() else { return };
        let ids: Vec<ProcId> = s.processes.iter().map(|p| p.id).collect();
        if let Ok(found) = db.lineage_for(&ids) {
            self.idle.seed(&found);
            self.lineage.extend(found);
        }
    }

    /// Drops entries not seen for [`LINEAGE_EVICT_MS`] (only after a flush, so nothing is lost).
    fn evict_lineage(&mut self, now: u64) {
        self.lineage
            .retain(|_, e| now.saturating_sub(e.last_seen_ms) < LINEAGE_EVICT_MS);
    }

    /// Rule-file lint (`doctor`): e.g. `match.env` keys outside the marker allowlist, which can never match.
    pub fn rule_warnings(&self, allowlist: &[String]) -> Vec<String> {
        self.detector
            .lint(allowlist)
            .iter()
            .map(|w| w.to_string())
            .collect()
    }

    /// In-memory lineage entries (tests, `doctor`).
    pub fn lineage_len(&self) -> usize {
        self.lineage.len()
    }

    /// Persists the lineage journal (entries seen since the last flush) and anything else the state store has
    /// queued.
    pub fn flush(&mut self) {
        let since = self.last_flush_ms;
        let mut latest = since;
        if self.state_read_only {
            return;
        }
        if let Some(db) = self.state.as_mut() {
            let entries: Vec<LineageEntry> = self
                .lineage
                .values()
                .filter(|e| e.last_seen_ms >= since)
                .cloned()
                .collect();
            latest = entries.iter().map(|e| e.last_seen_ms).max().unwrap_or(since);
            if !entries.is_empty() {
                let _ = db.record_lineage(&entries);
            }
            let _ = db.flush();
        }
        // Next flush: entries seen since the newest one written now (that sample may be re-sent once).
        self.last_flush_ms = self.last_flush_ms.max(latest);
    }

    /// `--no-learn`: the machine profile is not refreshed and the state store records no learning signals.
    /// The lineage journal is attribution, not learning, and is always kept.
    pub fn set_learning(&mut self, learn: bool) {
        self.learn = learn;
        if let Some(db) = self.state.as_mut() {
            db.set_learning(learn);
        }
    }

    /// Read the state store but never write it (`reclaim --dry-run`). Not reversible for this engine.
    pub fn set_state_read_only(&mut self) {
        self.state_read_only = true;
    }

    /// True once a replay has handed out its last frame (further snapshots repeat it); always false live.
    pub fn replay_done(&self) -> bool {
        match &self.input {
            Input::Live(_) => false,
            Input::Replay { frames, next } => *next >= frames.len(),
        }
    }

    /// True for a live engine (actions may be executed); false when replaying a fixture.
    pub fn is_live(&self) -> bool {
        matches!(self.input, Input::Live(_))
    }

    /// Refreshes the machine profile (UX §6) when it is missing or older than a day. Checked once per engine.
    fn refresh_profile(&mut self, s: &Snapshot) {
        if self.profile_checked || !self.learn || self.state_read_only || s.processes.is_empty() {
            return;
        }
        self.profile_checked = true;
        let Some(db) = self.state.as_mut() else { return };
        let now = s.taken_at_ms;
        if !db.machine_profile_stale(now).unwrap_or(false) {
            return;
        }
        let prev: Option<crate::profile::MachineProfile> = db
            .machine_profile()
            .ok()
            .flatten()
            .and_then(|(j, _)| serde_json::from_value(j).ok());
        let files = crate::profile::count_model_files(&self.model_folders);
        let p = crate::profile::build(s, files, prev.as_ref(), now);
        if let Ok(j) = serde_json::to_value(&p) {
            let _ = db.set_machine_profile(&j, now);
        }
    }

    /// Carries the previous probe's pid mapping into `s` (only pids still alive with the same identity).
    fn seed_adapter_truth(&self, s: &mut Snapshot) {
        let alive: std::collections::HashSet<ProcId> = s.processes.iter().map(|p| p.id).collect();
        s.model_servers = self
            .adapter_truth
            .0
            .iter()
            .filter(|m| m.pids.iter().any(|p| alive.contains(p)))
            .cloned()
            .collect();
        s.sandboxes = self
            .adapter_truth
            .1
            .iter()
            .filter(|b| b.host_pids.iter().any(|p| alive.contains(p)))
            .cloned()
            .collect();
    }

    /// True when a probed model server or (non app-owned) sandbox pid sits in a group of another kind, i.e.
    /// attribution ran without that adapter truth and should run again.
    fn adapter_truth_missed(s: &Snapshot) -> bool {
        let kind_of = |id: ProcId| s.group_of(id).map(|g| g.kind);
        let servers = s
            .model_servers
            .iter()
            .flat_map(|m| m.pids.iter())
            .any(|p| kind_of(*p).is_some_and(|k| k != GroupKind::ModelServer));
        let sandboxes = s
            .sandboxes
            .iter()
            .filter(|b| {
                !(b.kind == SandboxKind::Vm && b.runtime.to_ascii_lowercase().contains("virtualization"))
            })
            .flat_map(|b| b.host_pids.iter())
            .any(|p| kind_of(*p).is_some_and(|k| k != GroupKind::Sandbox));
        servers || sandboxes
    }

    /// What the lineage journal (in memory, then on disk) remembers about the agent sessions that left the
    /// snapshot's cohorts: the session's label and when any of its processes that are now gone was last
    /// seen. Looked up once per session per [`ENDED_SESSION_TTL_MS`].
    fn ended_sessions(&mut self, s: &Snapshot) -> BTreeMap<String, oomtop_core::staleness::EndedSession> {
        let live: std::collections::HashMap<&str, GroupKind> =
            s.groups.iter().map(|g| (g.id.as_str(), g.kind)).collect();
        let keys: std::collections::BTreeSet<String> = s
            .groups
            .iter()
            .filter_map(|g| oomtop_core::staleness::cohort_key(g, &live).map(str::to_string))
            .collect();
        let alive: std::collections::HashSet<ProcId> = s.processes.iter().map(|p| p.id).collect();
        let now = s.taken_at_ms;
        let mut out = BTreeMap::new();
        for key in keys {
            let fresh = self
                .ended_sessions
                .get(&key)
                .filter(|(t, _)| now.saturating_sub(*t) < ENDED_SESSION_TTL_MS);
            let found = match fresh {
                Some((_, e)) => e.clone(),
                None => {
                    let alias = key
                        .strip_prefix("agent:")
                        .filter(|h| h.len() == 12 && h.bytes().all(|b| b.is_ascii_hexdigit()));
                    let mut entries: Vec<LineageEntry> = self
                        .lineage
                        .values()
                        .filter(|e| {
                            e.group_id == key
                                || alias.is_some_and(|a| {
                                    e.session_id.as_deref().is_some_and(|sid| sid.starts_with(a))
                                })
                        })
                        .cloned()
                        .collect();
                    if let Some(db) = self.state.as_ref() {
                        entries.extend(db.session_entries(&key, 2_000).unwrap_or_default());
                    }
                    let gone: Vec<&LineageEntry> =
                        entries.iter().filter(|e| !alive.contains(&e.id)).collect();
                    let last_seen = gone.iter().map(|e| e.last_seen_ms).max();
                    let label = gone
                        .iter()
                        .filter(|e| e.group_kind == GroupKind::AgentSession && !e.group_label.is_empty())
                        .max_by_key(|e| e.last_seen_ms)
                        .map(|e| e.group_label.clone())
                        .unwrap_or_default();
                    let e = (last_seen.is_some() || !label.is_empty()).then_some(
                        oomtop_core::staleness::EndedSession {
                            label,
                            last_seen_ms: last_seen,
                        },
                    );
                    self.ended_sessions.insert(key.clone(), (now, e.clone()));
                    e
                }
            };
            if let Some(e) = found {
                out.insert(key, e);
            }
        }
        if self.ended_sessions.len() > 256 {
            self.ended_sessions
                .retain(|_, (t, _)| now.saturating_sub(*t) < ENDED_SESSION_TTL_MS);
        }
        out
    }

    /// Full enrichment pipeline on one raw snapshot.
    pub fn enrich(&mut self, mut s: Snapshot) -> Snapshot {
        if let Some(uid) = self.own_uid_only {
            s.processes.retain(|p| p.uid.is_none_or(|u| u == uid));
        }
        self.load_lineage(&s);
        self.update_protect(&s);
        if self.adapters_enabled {
            self.seed_adapter_truth(&mut s);
        }
        s.groups = self.detector.attribute(&s, &self.lineage, &self.protect);
        self.idle.observe(&s, &self.idle_cfg);
        self.idle.apply(&mut s, &self.lineage, &self.idle_cfg);
        if self.adapters_enabled {
            let p = probe(&s, &self.probe_opts);
            oomtop_adapters::apply(&mut s, p.clone());
            // First sample (or a server that just appeared): re-attribute with the adapter truth so the
            // server's processes form their own group now, not one refresh later.
            if Self::adapter_truth_missed(&s) {
                s.groups = self.detector.attribute(&s, &self.lineage, &self.protect);
                self.idle.apply(&mut s, &self.lineage, &self.idle_cfg);
                oomtop_adapters::apply(&mut s, p);
                let group_of = |s: &Snapshot, id: ProcId| s.group_of(id).map(|g| g.id.clone());
                let fixed: Vec<Option<String>> = s
                    .model_servers
                    .iter()
                    .map(|m| m.pids.iter().find_map(|p| group_of(&s, *p)))
                    .collect();
                for (m, gid) in s.model_servers.iter_mut().zip(fixed) {
                    if gid.is_some() {
                        m.group_id = gid;
                    }
                }
            }
            self.adapter_truth = (s.model_servers.clone(), s.sandboxes.clone());
        }
        // Ports and links need the groups and the model servers (SPEC §5 "Network").
        oomtop_core::net::analyze(&mut s);
        if !s.thermal.clusters.is_empty() {
            s.thermal.throttle_factor = throttle_factor(&s.thermal.clusters);
        }
        // Time and staleness (SPEC §5, §7): ages, duty cycles, idle rent, evidence, cohorts, duplicates.
        let ended = self.ended_sessions(&s);
        oomtop_core::staleness::apply(&mut s, &self.stale_cfg, &ended);
        self.history.push_snapshot(&s);
        s.oom.forecast = forecast_oom_with(&self.history, &s.oom, s.memory.total.value);
        // One definition (core): never pid 1 or an unscored process on Linux; the largest non-system,
        // non-protected group on macOS.
        s.oom.likely_victim = oomtop_core::forecast::likely_victim(&s);
        // Containers, clusters, per-limit forecasts, VM slack, namespace sets (SPEC §8.3, §11).
        self.isolation.update(&mut s);
        self.record_lineage(&s);
        self.refresh_profile(&s);
        s
    }

    /// Group id containing oomtop itself (for MCP: the calling agent's session).
    pub fn self_group(s: &Snapshot) -> Option<String> {
        let pid = s.self_pid?;
        let p = s.process_by_pid(pid)?;
        s.group_of(p.id).map(|g| g.id.clone())
    }

    pub fn state_mut(&mut self) -> Option<&mut StateDb> {
        self.state.as_mut()
    }
}

impl Drop for Engine {
    fn drop(&mut self) {
        self.flush();
    }
}

impl SnapshotProvider for Engine {
    fn snapshot(&mut self) -> Snapshot {
        let raw = self.raw_snapshot();
        self.enrich(raw)
    }
    fn snapshot_now(&mut self) -> Snapshot {
        let raw = self.raw_snapshot_with(true);
        self.enrich(raw)
    }
    fn history(&self) -> &History {
        &self.history
    }
    fn headroom_config(&self) -> HeadroomConfig {
        self.headroom_cfg.clone()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use oomtop_core::{OsKind, Process};

    fn proc(pid: u32, ppid: u32, name: &str) -> Process {
        Process {
            id: ProcId::new(pid, 1_000 + pid as u64),
            ppid: Some(ppid),
            name: name.into(),
            exe: format!("/usr/bin/{name}"),
            cmdline: vec![name.into()],
            uid: Some(501),
            ..Default::default()
        }
    }

    fn snap(at_ms: u64, procs: Vec<Process>) -> Snapshot {
        let mut s = Snapshot {
            taken_at_ms: at_ms,
            processes: procs,
            self_pid: Some(10),
            ..Default::default()
        };
        s.host.os = OsKind::Macos;
        s
    }

    fn replay_engine(frames: Vec<Snapshot>, db: StateDb) -> Engine {
        let mut cfg = Config::default();
        cfg.adapters.enabled = false;
        Engine::with_input(
            &cfg,
            Some(db),
            false,
            true,
            None,
            Input::Replay { frames, next: 0 },
        )
    }

    #[test]
    fn lineage_is_bounded_persisted_and_reloaded_lazily() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("state.db");
        let base = vec![proc(10, 1, "oomtop"), proc(20, 1, "zsh")];
        // A build spawns 500 short-lived compilers, then they're gone for good.
        let mut burst = base.clone();
        burst.extend((0..500).map(|i| proc(1_000 + i, 20, "rustc")));
        let t0 = oomtop_collect::now_ms(); // the journal prunes by wall-clock retention
        let frames = vec![
            snap(t0, burst),
            snap(t0 + 31_000, base.clone()),
            snap(t0 + 31_000 + LINEAGE_EVICT_MS + 31_000, base.clone()),
        ];
        let mut e = replay_engine(frames, StateDb::open(&path).unwrap());
        e.snapshot();
        assert_eq!(e.lineage_len(), 502);
        e.snapshot();
        assert_eq!(e.lineage_len(), 502, "recently seen entries are kept");
        e.snapshot();
        assert_eq!(
            e.lineage_len(),
            2,
            "dead processes are evicted after they were persisted"
        );
        drop(e);
        let db = StateDb::open(&path).unwrap();
        assert_eq!(
            db.lineage_all().unwrap().len(),
            502,
            "evicted entries stay in the journal"
        );
        // A new engine reads only the entries of the processes it sees, not the whole journal.
        let mut e2 = replay_engine(vec![snap(t0 + 3_600_000, base)], db);
        e2.snapshot();
        assert_eq!(e2.lineage_len(), 2);
        let zsh = e2.lineage.get(&ProcId::new(20, 1_020)).unwrap();
        assert_eq!(zsh.first_seen_ms, t0, "first_seen survives the restart");
    }

    /// SPEC §14 "CPU % within 1 s": the TUI's forced second sample 500 ms after the first has host and
    /// per-process CPU, although neither source is due yet on its cadence.
    #[cfg(any(target_os = "macos", target_os = "linux"))]
    #[test]
    fn forced_second_sample_has_cpu_within_a_second() {
        let started = std::time::Instant::now();
        let mut cfg = Config::default();
        cfg.adapters.enabled = false;
        let mut e = Engine::live(&cfg, None, false, true);
        let first = e.snapshot();
        assert!(first.cpu.total_pct.value.is_none());
        std::thread::sleep(Duration::from_millis(500));
        let mut s = e.snapshot_now();
        // Under a loaded parallel debug test run a source can blow its 50 ms budget (no baseline then); the
        // next forced sample has one. Seen once in ~20 full-suite runs at load average 3 with one retry.
        for _ in 0..3 {
            if s.cpu.total_pct.value.is_some() {
                break;
            }
            std::thread::sleep(Duration::from_millis(200));
            s = e.snapshot_now();
        }
        assert!(started.elapsed() < Duration::from_millis(1500) || cfg!(debug_assertions));
        assert!(
            s.cpu.total_pct.value.is_some(),
            "host CPU after the forced sample: {:?} · {:?}",
            s.cpu.total_pct,
            s.source_status
        );
        let with_cpu = s.processes.iter().filter(|p| p.cpu_pct.value.is_some()).count();
        assert!(with_cpu > 0, "per-process CPU after the forced sample");
        // A plain cadence-gated sample at that point would not have re-read the process listing.
        let again = e.snapshot();
        assert_eq!(again.processes.len(), s.processes.len());
    }

    #[cfg(any(target_os = "macos", target_os = "linux"))]
    #[test]
    fn live_pipeline_on_this_machine() {
        let db = StateDb::open_in_memory().unwrap();
        let mut e = Engine::live(&Config::default(), Some(db), true, true);
        let s = e.snapshot();
        assert!(!s.processes.is_empty());
        assert!(!s.groups.is_empty());
        let me = s.self_pid.unwrap();
        let grouped: usize = s.groups.iter().map(|g| g.members.len()).sum();
        assert_eq!(
            grouped,
            s.processes.len(),
            "every process attributed exactly once"
        );
        assert!(s.groups.iter().all(|g| !g.id.is_empty()));
        assert!(s.process_by_pid(me).is_some());
        assert!(e.protect.self_uid.is_some());
        e.flush();
        assert!(!e.state_mut().unwrap().lineage_all().unwrap().is_empty());
    }

    /// `reclaim --dry-run`: the journal is read (same attribution as a real run) and never written.
    #[test]
    fn read_only_state_is_loaded_but_never_written() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("state.db");
        let t0 = oomtop_collect::now_ms();
        let base = vec![proc(10, 1, "oomtop"), proc(20, 1, "zsh")];
        let mut e = replay_engine(vec![snap(t0, base.clone())], StateDb::open(&path).unwrap());
        e.snapshot();
        drop(e);
        let written = StateDb::open(&path).unwrap().lineage_all().unwrap();
        assert_eq!(written.len(), 2);

        let mut more = base.clone();
        more.push(proc(30, 20, "java"));
        let mut e = replay_engine(vec![snap(t0 + 60_000, more)], StateDb::open(&path).unwrap());
        e.set_state_read_only();
        e.snapshot();
        assert_eq!(
            e.lineage_len(),
            3,
            "the journal was loaded and the new process is tracked in memory"
        );
        let zsh = e.lineage.get(&ProcId::new(20, 1_020)).unwrap();
        assert_eq!(zsh.first_seen_ms, t0, "read from the store");
        e.flush();
        drop(e);
        let after = StateDb::open(&path).unwrap().lineage_all().unwrap();
        assert_eq!(after, written, "nothing recorded, nothing updated");
    }

    #[test]
    fn replay_reports_its_last_frame() {
        let base = vec![proc(10, 1, "oomtop")];
        let mut e = replay_engine(
            vec![snap(1_000, base.clone()), snap(3_000, base)],
            StateDb::open_in_memory().unwrap(),
        );
        assert!(!e.replay_done());
        e.snapshot();
        assert!(!e.replay_done());
        e.snapshot();
        assert!(e.replay_done());
        assert_eq!(
            e.snapshot().taken_at_ms,
            3_000,
            "further snapshots repeat the last frame"
        );
    }

    /// The engine uses core's victim rule: on Linux never pid 1 and never a process the kernel would not
    /// pick (`oom_score` 0), which the engine's own copy used to return.
    #[test]
    fn likely_victim_is_the_core_rule() {
        let mut init = proc(1, 0, "systemd");
        init.oom_score = Some(900);
        let mut unscored = proc(40, 1, "sshd");
        unscored.oom_score = Some(0);
        let mut s = snap(1_000, vec![init.clone(), unscored.clone()]);
        s.host.os = OsKind::Linux;
        let mut e = replay_engine(vec![s.clone()], StateDb::open_in_memory().unwrap());
        assert_eq!(e.snapshot().oom.likely_victim, None);

        let mut hog = proc(50, 1, "python");
        hog.oom_score = Some(700);
        s.processes.push(hog);
        let mut e = replay_engine(vec![s], StateDb::open_in_memory().unwrap());
        let out = e.snapshot();
        let v = out.oom.likely_victim.clone().expect("a scored process");
        assert_eq!((v.id.pid, v.heuristic), (50, false));
        assert_eq!(out.oom.likely_victim, oomtop_core::forecast::likely_victim(&out));
    }
}