oomtop-core 0.2.0

oomtop core: pure data model, units, attribution, headroom, forecast, throttle, query and ranking logic (no I/O)
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
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
//! The oomtop data model (SPEC §5). **FROZEN**: additive changes only.
//!
//! New fields must be `Option<_>` or carry a serde default (every struct here is `#[serde(default)]` and
//! implements `Default`), so older JSON keeps deserializing and older consumers ignore new fields.
//!
//! Invariants:
//! - Every *measured* number is a [`Measured<T>`] carrying `source` and `quality`; an unavailable value is
//!   `value: None` + `Quality::Unavailable(reason)` and is never rendered as zero.
//! - Bytes are `u64`, times are milliseconds since the Unix epoch (`*_ms`) or seconds (`*_s`).
//! - Process identity is always [`ProcId`] `(pid, start_time)`, so pid reuse never merges two processes.
//! - Command lines held in a live `Snapshot` are raw (needed for rule matching and adapter argv parsing);
//!   every export path (json, ndjson, serve, MCP) must pass through [`crate::redact::redact_snapshot`].
//! - Environments are never stored: only [`Markers`] derived from allowlisted keys, with values hashed.

use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

/// Output schema version for `json`, `ndjson`, `serve` and MCP (SPEC §6.2).
pub const SCHEMA_VERSION: u32 = 1;

/// Byte counts are always integers.
pub type Bytes = u64;

// ---------------------------------------------------------------------------------------------------------
// Measured<T>
// ---------------------------------------------------------------------------------------------------------

/// How much a measured value can be trusted.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum Quality {
    /// Read directly from an authoritative OS source.
    Exact,
    /// Derived, interpolated between expensive reads, or a heuristic / lower bound.
    Estimate,
    /// Not available; the string says why (e.g. "needs root", "not supported on this OS").
    Unavailable(String),
}

impl Default for Quality {
    fn default() -> Self {
        Quality::Unavailable("not collected".to_string())
    }
}

/// A measured value with its provenance (SPEC §5). Helpers live in [`crate::measured`].
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(default)]
pub struct Measured<T> {
    /// `None` whenever `quality` is `Unavailable`.
    pub value: Option<T>,
    /// Where the value came from, e.g. `"proc_pid_rusage.ri_phys_footprint"`, `"/proc/meminfo:MemAvailable"`.
    pub source: String,
    pub quality: Quality,
}

impl<T> Default for Measured<T> {
    fn default() -> Self {
        Measured {
            value: None,
            source: String::new(),
            quality: Quality::default(),
        }
    }
}

/// Availability of one data source for the current snapshot (SPEC §6.1).
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Default)]
#[serde(rename_all = "snake_case")]
pub enum SourceStatus {
    #[default]
    Available,
    Partial(String),
    Unavailable(String),
}

// ---------------------------------------------------------------------------------------------------------
// Host
// ---------------------------------------------------------------------------------------------------------

#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
#[serde(rename_all = "snake_case")]
pub enum OsKind {
    Macos,
    Linux,
    #[default]
    Other,
}

/// Static-ish facts about the machine.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct HostInfo {
    pub hostname: String,
    pub os: OsKind,
    /// e.g. "macOS 26.6", "Linux 6.8.0".
    pub os_version: String,
    /// e.g. "aarch64", "x86_64".
    pub arch: String,
    /// Hardware model, e.g. "Mac17,3".
    pub model: Option<String>,
    /// e.g. "Apple M5".
    pub cpu_brand: Option<String>,
    pub cores_logical: u32,
    pub cores_performance: Option<u32>,
    pub cores_efficiency: Option<u32>,
    pub mem_total: Bytes,
    /// Unified memory (Apple Silicon) vs discrete VRAM.
    pub unified_memory: bool,
    /// `Some(true)` for fanless machines (thermal-prone), `None` if unknown.
    pub fanless: Option<bool>,
    pub page_size: u64,
    pub boot_time_ms: Option<u64>,
}

/// Host memory pressure level (macOS memorystatus / Linux derived from PSI).
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[serde(rename_all = "snake_case")]
pub enum PressureLevel {
    #[default]
    Normal,
    Warn,
    Critical,
}

/// Linux PSI (`/proc/pressure/memory`), percentages 0..100.
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Default)]
#[serde(default)]
pub struct Psi {
    pub some_avg10: f64,
    pub some_avg60: f64,
    pub some_avg300: f64,
    pub full_avg10: f64,
    pub full_avg60: f64,
    pub full_avg300: f64,
}

/// Host memory (SPEC §5, §8.1).
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct HostMemory {
    pub total: Measured<Bytes>,
    /// Headroom input `available_now` (SPEC §8.1). macOS: `(free − speculative) + external/file-backed
    /// (⊇ speculative) + purgeable` — speculative pages counted once; anonymous inactive pages are NOT
    /// counted. Linux: MemAvailable (capped by own cgroup when set).
    pub available: Measured<Bytes>,
    pub free: Measured<Bytes>,
    pub cached: Measured<Bytes>,
    pub wired: Measured<Bytes>,
    /// Physical memory occupied by the compressor (macOS) / zswap+zram pool (Linux).
    pub compressed: Measured<Bytes>,
    /// Logical (uncompressed) bytes held in the compressor; `compressed / compressed_logical` = ratio.
    pub compressed_logical: Measured<Bytes>,
    /// Anonymous "app" memory (macOS Activity Monitor "App Memory"; Linux AnonPages).
    pub app: Measured<Bytes>,
    pub swap_used: Measured<Bytes>,
    pub swap_total: Measured<Bytes>,
    /// Swap-ins per minute (pages→bytes), from deltas (SPEC §9 swap storm).
    pub swap_in_per_min: Measured<Bytes>,
    pub swap_out_per_min: Measured<Bytes>,
    pub pressure: Measured<PressureLevel>,
    /// macOS `kern.memorystatus_level` (% available), 0..100.
    pub memorystatus_level: Measured<u8>,
    pub psi: Measured<Psi>,
    /// Linux: `memory.max` of oomtop's own cgroup when set.
    pub own_cgroup_limit: Measured<Bytes>,
    /// macOS: how far swap can grow before jetsam runs out of swap space — swap used + free space on the
    /// swap volume (`/System/Volumes/VM`), an estimate. macOS adds swap files on demand, so `swap_total` is
    /// not a limit there. Unavailable on Linux (fixed-size swap: `swap_total` is the limit).
    #[serde(default)]
    pub swap_limit: Measured<Bytes>,
}

/// Host CPU.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct HostCpu {
    /// Whole-machine utilization, 0..100 (100 = all cores busy).
    pub total_pct: Measured<f64>,
    pub load_avg_1: Measured<f64>,
    pub load_avg_5: Measured<f64>,
    pub load_avg_15: Measured<f64>,
    /// Per-logical-core utilization, 0..100 each, in OS core order (htop-style meters). Empty until two samples
    /// exist or when the source is unavailable.
    pub per_core_pct: Vec<f64>,
    /// Kind of each core, parallel to `per_core_pct` when known (Apple Silicon P/E clusters); empty if unknown.
    pub core_kinds: Vec<CoreKind>,
    /// Process / thread counts for the header "Tasks" line.
    pub tasks: TaskCounts,
}

/// Core class on heterogeneous CPUs.
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
#[serde(rename_all = "snake_case")]
pub enum CoreKind {
    Performance,
    Efficiency,
    #[default]
    Unknown,
}

/// Counts shown in the htop-style header ("Tasks: 867, 1625 thr; 1 running").
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Default)]
#[serde(default)]
pub struct TaskCounts {
    pub processes: u32,
    /// Sum of per-process thread counts where readable; `None` when no process reported threads.
    pub threads: Option<u32>,
    pub running: u32,
}

// ---------------------------------------------------------------------------------------------------------
// OOM (SPEC §8.3)
// ---------------------------------------------------------------------------------------------------------

#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
#[serde(rename_all = "snake_case")]
pub enum OomKiller {
    /// Linux kernel OOM killer (always applies on Linux).
    #[default]
    Kernel,
    SystemdOomd,
    Earlyoom,
    /// macOS memorystatus / jetsam.
    Jetsam,
}

#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
#[serde(rename_all = "snake_case")]
pub enum ThresholdMetric {
    /// Swap used as % of swap total.
    #[default]
    SwapUsedPct,
    /// Memory pressure %: the forecast evaluates it on PSI `some avg10` (SPEC §21, the conservative
    /// series: `some ≥ full`).
    MemPressurePct,
    /// Available memory in bytes.
    AvailableBytes,
    /// Available memory as % of total.
    AvailablePct,
}

/// A threshold at which a killer acts, e.g. systemd-oomd "swap used > 90 %".
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct OomThreshold {
    pub killer: OomKiller,
    pub metric: ThresholdMetric,
    pub value: f64,
    /// Sustained duration required, if any (oomd: 30 s).
    pub duration_s: Option<u32>,
    /// Where the threshold came from ("oomd.conf", "earlyoom argv", "default").
    pub source: String,
}

#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
#[serde(rename_all = "snake_case")]
pub enum ForecastTarget {
    /// Swap used reaches swap total.
    #[default]
    SwapExhaustion,
    /// Available memory reaches zero (after which the killer acts).
    AvailableExhaustion,
    /// A killer threshold (see `Oom::thresholds`) is crossed.
    KillerThreshold,
}

/// OOM forecast. Present only when the trend is consistent (R² ≥ 0.6) and ETA < 30 min (SPEC §8.3).
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct Forecast {
    pub target: ForecastTarget,
    /// Which killer acts at the target.
    pub killer: Option<OomKiller>,
    pub eta_s: u64,
    /// 0..1, the R² of the linear fit.
    pub confidence: f64,
    /// Trend rate in bytes per minute (positive = growing towards the target).
    pub rate_per_min: f64,
    /// Window used for the fit, seconds.
    pub window_s: u64,
}

/// Who the OOM killer would likely pick.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct Victim {
    pub id: ProcId,
    pub name: String,
    pub group_id: Option<String>,
    /// e.g. "highest oom_score (812)" or "largest background app (heuristic)".
    pub reason: String,
    /// true on macOS (jetsam bands unreadable without root).
    pub heuristic: bool,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct OomKill {
    pub at_ms: Option<u64>,
    pub killer: OomKiller,
    pub victim_name: Option<String>,
    pub victim_pid: Option<u32>,
    /// e.g. "memory.events:oom_kill", "JetsamEvent-2026-09-29-101010.ips".
    pub source: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct Oom {
    /// The nearest applicable killer.
    pub killer: OomKiller,
    /// All killers that apply on this host (kernel is always present on Linux).
    pub killers: Vec<OomKiller>,
    pub thresholds: Vec<OomThreshold>,
    /// `None` = stable / not enough data.
    pub forecast: Option<Forecast>,
    pub likely_victim: Option<Victim>,
    pub recent_kills: Vec<OomKill>,
}

// ---------------------------------------------------------------------------------------------------------
// Accelerators & thermal (SPEC §5, §9)
// ---------------------------------------------------------------------------------------------------------

#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
#[serde(rename_all = "snake_case")]
pub enum GpuVendor {
    Apple,
    Nvidia,
    Amd,
    Intel,
    #[default]
    Other,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct Accelerator {
    /// Stable id within the host, e.g. "gpu0".
    pub id: String,
    pub vendor: GpuVendor,
    pub name: String,
    /// Shares host RAM (Apple Silicon, iGPUs).
    pub unified: bool,
    pub util_pct: Measured<f64>,
    pub mem_used: Measured<Bytes>,
    pub mem_total: Measured<Bytes>,
    /// Metal working-set limit (`recommendedMaxWorkingSetSize` / `iogpu.wired_limit_mb`) (SPEC §8.1).
    pub gpu_budget: Measured<Bytes>,
    pub power_w: Measured<f64>,
    pub temp_c: Measured<f64>,
    pub clock_mhz: Measured<f64>,
    pub max_clock_mhz: Measured<f64>,
    /// e.g. "SW power cap", "HW slowdown", "thermal".
    pub throttle_reasons: Vec<String>,
}

/// Thermal pressure levels (macOS `OSThermalNotification.h`; Linux derived from trip points).
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[serde(rename_all = "snake_case")]
pub enum ThermalPressure {
    #[default]
    Nominal,
    Moderate,
    Heavy,
    Trapping,
    Sleeping,
}

/// Frequency/residency of one CPU cluster or GPU (input for the throttle factor, SPEC §9).
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct ClusterFreq {
    /// e.g. "P-cluster", "E-cluster", "cpu0", "gpu".
    pub name: String,
    pub cur_mhz: f64,
    pub max_mhz: f64,
    /// Active residency 0..100.
    pub active_pct: f64,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct TempSensor {
    pub name: String,
    pub celsius: f64,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct Thermal {
    pub pressure: Measured<ThermalPressure>,
    /// 0..1 = observed ÷ max frequency, only while busy; `Unavailable("idle")` otherwise (SPEC §9).
    pub throttle_factor: Measured<f64>,
    pub low_power_mode: Measured<bool>,
    pub on_battery: Measured<bool>,
    /// 0..100.
    pub battery_pct: Measured<f64>,
    pub adapter_watts: Measured<f64>,
    pub package_power_w: Measured<f64>,
    pub clusters: Vec<ClusterFreq>,
    pub temps: Vec<TempSensor>,
    /// Linux: a thermal trip point was hit.
    pub trip_point_hit: Measured<bool>,
}

// ---------------------------------------------------------------------------------------------------------
// Processes
// ---------------------------------------------------------------------------------------------------------

/// Process identity: `(pid, start_time)`. `start_time` is milliseconds since the Unix epoch
/// (Linux: btime + starttime/CLK_TCK; macOS: `pbi_start_tvsec/usec`). Used for identity only.
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub struct ProcId {
    pub pid: u32,
    pub start_time: u64,
}

impl ProcId {
    pub fn new(pid: u32, start_time: u64) -> Self {
        ProcId { pid, start_time }
    }
}

/// Per-process memory (SPEC §5 memory taxonomy).
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct MemBreakdown {
    pub resident: Measured<Bytes>,
    /// macOS `ri_phys_footprint`; Linux `Pss` (estimate `RSS − Shared` between smaps reads).
    pub footprint_or_pss: Measured<Bytes>,
    pub gpu: Measured<Bytes>,
    pub compressed: Measured<Bytes>,
    pub swapped: Measured<Bytes>,
    /// macOS: `max(0, footprint − resident)`, "compressed or swapped (est.)".
    pub non_resident_est: Measured<Bytes>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct DiskIo {
    /// Cumulative bytes read/written since process start.
    pub read_bytes: Measured<Bytes>,
    pub write_bytes: Measured<Bytes>,
    /// Bytes per second over the last interval.
    pub read_rate: Measured<f64>,
    pub write_rate: Measured<f64>,
}

#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
#[serde(rename_all = "snake_case")]
pub enum ProcState {
    Running,
    Sleeping,
    Idle,
    Stopped,
    Zombie,
    #[default]
    Unknown,
}

/// Session markers derived from allowlisted environment keys (SPEC §7.4). Values are never stored raw.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Default)]
#[serde(default)]
pub struct Markers {
    /// Hashed session id (e.g. of `CLAUDE_CODE_SESSION_ID`), see [`crate::redact::hash_marker`].
    pub session_id: Option<String>,
    /// Agent name inferred from marker keys, e.g. "claude-code".
    pub agent: Option<String>,
    /// Allowlisted marker keys that were present (keys only, no values).
    pub keys: Vec<String>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct Process {
    pub id: ProcId,
    pub ppid: Option<u32>,
    /// macOS responsible pid (helpers/XPC → launching app).
    pub responsible_pid: Option<u32>,
    /// Short name (comm / basename).
    pub name: String,
    /// Executable path (may be empty when unreadable).
    pub exe: String,
    /// argv. Raw in a live snapshot; redacted in every export.
    pub cmdline: Vec<String>,
    pub cwd: Option<String>,
    pub uid: Option<u32>,
    pub user: Option<String>,
    /// Per-core percent (100 = one core busy); needs two samples.
    pub cpu_pct: Measured<f64>,
    pub mem: MemBreakdown,
    pub disk_io: DiskIo,
    pub state: ProcState,
    pub threads: Option<u32>,
    /// Seconds idle; `Estimate` quality means "≥ observed window" (no lineage data).
    pub idle_for_s: Measured<u64>,
    pub markers: Markers,
    /// Linux cgroup v2 path.
    pub cgroup: Option<String>,
    /// Linux `/proc/<pid>/oom_score`.
    pub oom_score: Option<i32>,
    /// macOS bundle id of the owning app, when known.
    pub bundle_id: Option<String>,
    /// Model weight files mapped/open (`*.gguf`, `*.safetensors`), when known.
    pub model_files: Vec<String>,
    // --- time and staleness (SPEC §5 "Time and staleness"; filled by the decoders and
    // `crate::staleness::apply`; left out of JSON until computed) ---
    /// Seconds since the process started (now − start time).
    #[serde(skip_serializing_if = "crate::staleness::not_collected")]
    pub age_s: Measured<u64>,
    /// Total CPU time (user + system) since the process started, seconds.
    #[serde(skip_serializing_if = "crate::staleness::not_collected")]
    pub cpu_time_s: Measured<f64>,
    /// Duty cycle: CPU time ÷ age, in percent of one core (100 = one core busy for its whole life).
    #[serde(skip_serializing_if = "crate::staleness::not_collected")]
    pub duty_pct: Measured<f64>,
    /// Idle rent: GiB-hours of footprint held while idle (footprint × idle time); always an estimate.
    #[serde(skip_serializing_if = "crate::staleness::not_collected")]
    pub idle_rent_gib_h: Measured<f64>,
    /// `Some(true)`: the working directory no longer exists; `None`: not checked / not readable.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cwd_missing: Option<bool>,
    /// The executable was deleted or replaced since the process started; `None`: unchanged or unknown.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exe_change: Option<crate::staleness::ExeChange>,
    /// Project directory: git root of the cwd when known, else the cwd.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub project: Option<String>,
    /// Agent session roots: modification time (ms since the epoch) of the session's transcript file. The
    /// file's content is never read.
    #[serde(skip_serializing_if = "crate::staleness::not_collected")]
    pub transcript_mtime_ms: Measured<u64>,
    /// **Reserved for network data** (not collected yet): `Some(true)` when the process listens on a port
    /// and has no clients. Feeds the `listening_no_clients` staleness signal.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub listening_no_clients: Option<bool>,
    /// Linux namespace identities (`/proc/<pid>/ns/*`, own processes only); `None` = not read or not Linux
    /// (SPEC §11 "Namespaces", `crate::namespaces`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ns: Option<crate::namespaces::NsIds>,
}

// ---------------------------------------------------------------------------------------------------------
// Groups (SPEC §5, §7)
// ---------------------------------------------------------------------------------------------------------

/// Group kinds with short aliases used in queries, themes and rules.
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
#[serde(rename_all = "snake_case")]
pub enum GroupKind {
    #[serde(alias = "agent")]
    AgentSession,
    App,
    #[serde(alias = "model")]
    ModelServer,
    Sandbox,
    #[serde(alias = "daemon")]
    BuildDaemon,
    System,
    #[default]
    Other,
}

impl GroupKind {
    pub const ALL: [GroupKind; 7] = [
        GroupKind::AgentSession,
        GroupKind::App,
        GroupKind::ModelServer,
        GroupKind::Sandbox,
        GroupKind::BuildDaemon,
        GroupKind::System,
        GroupKind::Other,
    ];

    /// Short alias: agent | app | model | sandbox | daemon | system | other.
    pub fn alias(self) -> &'static str {
        match self {
            GroupKind::AgentSession => "agent",
            GroupKind::App => "app",
            GroupKind::ModelServer => "model",
            GroupKind::Sandbox => "sandbox",
            GroupKind::BuildDaemon => "daemon",
            GroupKind::System => "system",
            GroupKind::Other => "other",
        }
    }

    /// Canonical serde name: agent_session | app | model_server | sandbox | build_daemon | system | other.
    pub fn as_str(self) -> &'static str {
        match self {
            GroupKind::AgentSession => "agent_session",
            GroupKind::App => "app",
            GroupKind::ModelServer => "model_server",
            GroupKind::Sandbox => "sandbox",
            GroupKind::BuildDaemon => "build_daemon",
            GroupKind::System => "system",
            GroupKind::Other => "other",
        }
    }

    /// Parses either the canonical name or the short alias (case-insensitive).
    pub fn parse(s: &str) -> Option<GroupKind> {
        let s = s.trim().to_ascii_lowercase();
        GroupKind::ALL
            .into_iter()
            .find(|k| k.alias() == s || k.as_str() == s)
    }
}

#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
#[serde(rename_all = "snake_case")]
pub enum Confidence {
    #[default]
    Low,
    Medium,
    High,
}

/// Which attribution signal placed a process in its group (SPEC §7, strongest first).
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
#[serde(rename_all = "snake_case")]
pub enum AttributionSignal {
    Adapter,
    Cgroup,
    Responsible,
    Marker,
    Ancestry,
    Lineage,
    Rule,
    #[default]
    Heuristic,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct Member {
    pub id: ProcId,
    pub confidence: Confidence,
    pub via: AttributionSignal,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct GroupTotals {
    /// Σ `footprint_or_pss` of members.
    pub footprint: Measured<Bytes>,
    pub resident: Measured<Bytes>,
    pub gpu: Measured<Bytes>,
    pub swapped: Measured<Bytes>,
    /// Σ per-core CPU % of members.
    pub cpu_pct: Measured<f64>,
    pub process_count: u32,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct Group {
    /// Unique within a snapshot and stable across samples for the same root, e.g. "agent:3f2a9c01".
    pub id: String,
    pub kind: GroupKind,
    pub label: String,
    /// Entity fingerprint (UX §4), hex.
    pub fingerprint: String,
    pub root: Option<ProcId>,
    pub members: Vec<Member>,
    pub totals: GroupTotals,
    /// RAM freed if stopped (SPEC §8.1); always `Estimate`.
    pub reclaim_gain: Measured<Bytes>,
    /// Swap freed if stopped, reported separately (SPEC §8.1).
    pub swap_gain: Measured<Bytes>,
    pub confidence: Confidence,
    /// Group that started/owns this one (e.g. sandbox started by an agent session).
    pub owner_group: Option<String>,
    pub orphan: bool,
    pub idle: bool,
    pub idle_for_s: Option<u64>,
    /// Protected groups are never offered for actions (SPEC §13).
    pub protected: bool,
    /// oomtop's own processes.
    pub is_self: bool,
    /// Footprint is a lower bound (Virtualization.framework VMs, SPEC §5).
    pub lower_bound: bool,
    /// Configured VM memory for sandbox groups.
    pub configured_mem: Option<Bytes>,
    /// Rule/adapter that defined this group, for "why" explanations.
    pub matched_by: Option<String>,
    // --- time and staleness (filled by `crate::staleness::apply`; left out of JSON until computed) ---
    /// Age of the root process, else of the oldest member (the source says which), seconds.
    #[serde(skip_serializing_if = "crate::staleness::not_collected")]
    pub age_s: Measured<u64>,
    /// Σ CPU time of the live members, seconds.
    #[serde(skip_serializing_if = "crate::staleness::not_collected")]
    pub cpu_time_s: Measured<f64>,
    /// Duty cycle: `cpu_time_s ÷ age_s`, percent of one core.
    #[serde(skip_serializing_if = "crate::staleness::not_collected")]
    pub duty_pct: Measured<f64>,
    /// Idle time of the longest-idle member (`idle_for_s` is the shortest: the whole group's).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub longest_idle_s: Option<u64>,
    /// The longest-idle member.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub longest_idle: Option<ProcId>,
    /// Idle rent: Σ GiB-hours held by idle members; always an estimate.
    #[serde(skip_serializing_if = "crate::staleness::not_collected")]
    pub idle_rent_gib_h: Measured<f64>,
    /// Staleness score (0..1) and the ordered evidence behind it.
    #[serde(skip_serializing_if = "crate::staleness::no_staleness")]
    pub staleness: crate::staleness::Staleness,
    /// Project directory of the root, else the most common among the members.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub project: Option<String>,
    /// Id of the [`crate::staleness::Cohort`] this group belongs to: the agent session that left it has
    /// ended.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cohort: Option<String>,
}

// ---------------------------------------------------------------------------------------------------------
// Model servers (SPEC §10) and sandboxes (SPEC §11)
// ---------------------------------------------------------------------------------------------------------

#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
#[serde(rename_all = "snake_case")]
pub enum ModelServerKind {
    Ollama,
    LlamaCpp,
    SdCpp,
    Vllm,
    LmStudio,
    Mlx,
    #[default]
    Generic,
}

#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
#[serde(rename_all = "snake_case")]
pub enum Device {
    Gpu,
    Cpu,
    Mixed,
    #[default]
    Unknown,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct LoadedModel {
    pub name: String,
    pub file: Option<String>,
    pub weights_bytes: Measured<Bytes>,
    pub kv_bytes: Measured<Bytes>,
    pub device: Device,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct JobProgress {
    pub done: u32,
    pub total: u32,
    /// Human label, e.g. "generating".
    pub label: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct ModelServer {
    pub id: String,
    pub kind: ModelServerKind,
    /// Always a loopback URL, e.g. "http://127.0.0.1:11434".
    pub endpoint: Option<String>,
    pub pids: Vec<ProcId>,
    pub group_id: Option<String>,
    pub models: Vec<LoadedModel>,
    pub tok_s: Measured<f64>,
    pub s_per_step: Measured<f64>,
    pub queue: Measured<u32>,
    pub busy: Measured<bool>,
    pub progress: Option<JobProgress>,
    pub status: SourceStatus,
}

#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
#[serde(rename_all = "snake_case")]
pub enum SandboxKind {
    Container,
    Vm,
    MicroVm,
    #[default]
    ProcessSandbox,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct SandboxLimits {
    pub mem_max: Option<Bytes>,
    pub cpus: Option<f64>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[serde(default)]
pub struct Sandbox {
    pub id: String,
    pub kind: SandboxKind,
    /// e.g. "docker", "podman", "virtualization.framework", "firecracker", "bwrap".
    pub runtime: String,
    pub label: String,
    pub host_pids: Vec<ProcId>,
    pub configured_mem: Measured<Bytes>,
    pub guest_mem: Measured<Bytes>,
    pub limits: SandboxLimits,
    pub started_by_group: Option<String>,
    /// Host footprint under-counts this sandbox (SPEC §5).
    pub footprint_lower_bound: bool,
}

// ---------------------------------------------------------------------------------------------------------
// Snapshot
// ---------------------------------------------------------------------------------------------------------

/// One consistent view of the machine. Output of the sampler + attribution + adapters.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(default)]
pub struct Snapshot {
    /// Always [`SCHEMA_VERSION`].
    pub schema_version: u32,
    pub taken_at_ms: u64,
    pub host: HostInfo,
    pub memory: HostMemory,
    pub cpu: HostCpu,
    pub oom: Oom,
    pub accelerators: Vec<Accelerator>,
    pub thermal: Thermal,
    pub processes: Vec<Process>,
    pub groups: Vec<Group>,
    pub model_servers: Vec<ModelServer>,
    pub sandboxes: Vec<Sandbox>,
    /// Per source name (e.g. "macos.procs", "linux.meminfo", "adapter.ollama").
    pub source_status: BTreeMap<String, SourceStatus>,
    /// pid of the oomtop process that produced this snapshot.
    pub self_pid: Option<u32>,
    /// Leftovers bucketed by the agent session that ended (`crate::staleness::cohorts`).
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub cohorts: Vec<crate::staleness::Cohort>,
    /// More than one live instance of one fingerprint in one project (`crate::staleness::duplicates`).
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub duplicates: Vec<crate::staleness::DuplicateSet>,
    /// Ports and "who talks to whom" (`crate::net`): sockets from the socket source, listeners, links and
    /// findings computed by `crate::net::analyze`. Left out of JSON when no socket source ran.
    #[serde(skip_serializing_if = "crate::net::Net::is_empty")]
    pub net: crate::net::Net,
    /// Linux cgroup v2 memory accounting of container / pod cgroups and of every cgroup with a memory limit
    /// (SPEC §8.3 per-limit forecast, §11).
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub cgroups: Vec<crate::limits::CgroupMem>,
    /// Containers, local Kubernetes clusters, per-limit forecasts, VM slack, namespace sets and stale
    /// containers (SPEC §8.3, §11; `crate::cluster`).
    #[serde(skip_serializing_if = "crate::cluster::Isolation::is_empty")]
    pub isolation: crate::cluster::Isolation,
}

impl Default for Snapshot {
    fn default() -> Self {
        Snapshot {
            schema_version: SCHEMA_VERSION,
            taken_at_ms: 0,
            host: HostInfo::default(),
            memory: HostMemory::default(),
            cpu: HostCpu::default(),
            oom: Oom::default(),
            accelerators: Vec::new(),
            thermal: Thermal::default(),
            processes: Vec::new(),
            groups: Vec::new(),
            model_servers: Vec::new(),
            sandboxes: Vec::new(),
            source_status: BTreeMap::new(),
            self_pid: None,
            cohorts: Vec::new(),
            duplicates: Vec::new(),
            net: crate::net::Net::default(),
            cgroups: Vec::new(),
            isolation: Default::default(),
        }
    }
}

impl Snapshot {
    /// Finds a process by identity.
    pub fn process(&self, id: ProcId) -> Option<&Process> {
        self.processes.iter().find(|p| p.id == id)
    }

    /// Finds a process by pid only (for ppid/responsible lookups inside one snapshot).
    pub fn process_by_pid(&self, pid: u32) -> Option<&Process> {
        self.processes.iter().find(|p| p.id.pid == pid)
    }

    pub fn group(&self, id: &str) -> Option<&Group> {
        self.groups.iter().find(|g| g.id == id)
    }

    /// The group containing a process, if attributed.
    pub fn group_of(&self, id: ProcId) -> Option<&Group> {
        self.groups.iter().find(|g| g.members.iter().any(|m| m.id == id))
    }
}

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

    #[test]
    fn default_snapshot_roundtrips_and_has_schema_version() {
        let s = Snapshot::default();
        assert_eq!(s.schema_version, SCHEMA_VERSION);
        let json = serde_json::to_string(&s).unwrap();
        let back: Snapshot = serde_json::from_str(&json).unwrap();
        assert_eq!(back, s);
    }

    #[test]
    fn missing_fields_deserialize_with_defaults() {
        let back: Snapshot = serde_json::from_str(r#"{"taken_at_ms": 5}"#).unwrap();
        assert_eq!(back.taken_at_ms, 5);
        assert_eq!(back.schema_version, SCHEMA_VERSION);
        assert!(back.memory.total.value.is_none());
    }

    #[test]
    fn group_kind_aliases() {
        for k in GroupKind::ALL {
            assert_eq!(GroupKind::parse(k.alias()), Some(k));
            assert_eq!(GroupKind::parse(k.as_str()), Some(k));
        }
        let k: GroupKind = serde_json::from_str("\"daemon\"").unwrap();
        assert_eq!(k, GroupKind::BuildDaemon);
        assert_eq!(
            serde_json::to_string(&GroupKind::ModelServer).unwrap(),
            "\"model_server\""
        );
    }

    #[test]
    fn quality_serde_shape() {
        assert_eq!(serde_json::to_string(&Quality::Exact).unwrap(), "\"exact\"");
        assert_eq!(
            serde_json::to_string(&Quality::Unavailable("needs root".into())).unwrap(),
            r#"{"unavailable":"needs root"}"#
        );
    }
}