oomtop-config 0.2.1

oomtop configuration: layered config, themes, keymaps, JSON Schema, comment-preserving writes
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
//! The `Config` struct (UX §12.5, SPEC §15). Every field has a default and a doc line in
//! [`crate::docs::DOCS`] (enforced by a test), and appears in the JSON Schema and `oomtop config init`.

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

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(default, deny_unknown_fields)]
pub struct General {
    /// TUI/serve refresh for per-process data, ms.
    pub refresh_ms: u64,
    /// Host stats refresh, ms.
    pub host_refresh_ms: u64,
    /// Expensive reads (smaps rotation, adapters), ms.
    pub expensive_refresh_ms: u64,
    pub mouse: bool,
    /// Reload config/themes/keymap/layouts on save (debounced 200 ms).
    pub live_reload: bool,
    /// 0 = native file events (FSEvents/inotify); > 0 = poll every N ms (network filesystems).
    pub watch_poll_ms: u64,
    /// Show other users' processes (like htop). `false` keeps only this user's processes; host totals stay
    /// whole-machine.
    pub other_users: bool,
}

impl Default for General {
    fn default() -> Self {
        General {
            refresh_ms: 2000,
            host_refresh_ms: 1000,
            expensive_refresh_ms: 10_000,
            mouse: true,
            live_reload: true,
            watch_poll_ms: 0,
            other_users: true,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(default, deny_unknown_fields)]
pub struct Thresholds {
    pub idle_cpu_pct: f64,
    pub idle_after_s: u64,
    pub orphan_age_s: u64,
    /// Staleness (SPEC §7 "Staleness"): a group idle this long shows `idle_long` evidence; 0 = off.
    pub stale_idle_s: u64,
    /// An agent session whose transcript was not modified this long shows `transcript_quiet`; 0 = off.
    pub stale_transcript_s: u64,
    /// Score (0–1) at or above which a group is flagged stale.
    pub stale_score: f64,
    /// Groups younger than this are never flagged stale.
    pub stale_min_age_s: u64,
}

impl Default for Thresholds {
    fn default() -> Self {
        Thresholds {
            idle_cpu_pct: 0.5,
            idle_after_s: 1800,
            orphan_age_s: 600,
            stale_idle_s: 4 * 3600,
            stale_transcript_s: 12 * 3600,
            stale_score: 0.5,
            stale_min_age_s: 600,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(default, deny_unknown_fields)]
pub struct HeadroomSection {
    /// Minimum safety margin, e.g. "1.5G".
    pub min_margin: String,
    pub margin_pct: f64,
    pub pressure_boost_pct: f64,
    /// Fixed margin override, e.g. "2G"; empty = computed.
    pub margin_override: String,
}

impl Default for HeadroomSection {
    fn default() -> Self {
        HeadroomSection {
            min_margin: "1.5G".into(),
            margin_pct: 8.0,
            pressure_boost_pct: 50.0,
            margin_override: String::new(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema, Default)]
#[serde(default, deny_unknown_fields)]
pub struct Protected {
    /// Extra process names never offered for actions.
    pub names: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(default, deny_unknown_fields)]
pub struct Models {
    /// Folders indexed for model files.
    pub folders: Vec<String>,
}

impl Default for Models {
    fn default() -> Self {
        Models {
            folders: vec![
                "~/.cache/huggingface".into(),
                "~/.ollama/models".into(),
                "~/.lmstudio/models".into(),
            ],
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(default, deny_unknown_fields)]
pub struct Adapters {
    pub enabled: bool,
    pub timeout_ms: u64,
    /// Port overrides per server kind (host is always 127.0.0.1), e.g. ollama = 11434.
    pub ports: BTreeMap<String, u16>,
}

impl Default for Adapters {
    fn default() -> Self {
        Adapters {
            enabled: true,
            timeout_ms: 200,
            ports: BTreeMap::new(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(default, deny_unknown_fields)]
pub struct Privacy {
    /// Environment keys read as session markers (values hashed, never stored).
    pub marker_allowlist: Vec<String>,
    /// Redact command lines in exports (json, ndjson, serve, MCP).
    pub redact_exports: bool,
}

impl Default for Privacy {
    fn default() -> Self {
        Privacy {
            marker_allowlist: oomtop_core::redact::default_allowlist(),
            redact_exports: true,
        }
    }
}

macro_rules! str_enum {
    ($name:ident { $($(#[$vm:meta])* $var:ident = $s:literal),+ $(,)? }) => {
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Default)]
        pub enum $name {
            $( $(#[$vm])* #[serde(rename = $s)] $var, )+
        }
        impl $name {
            pub fn as_str(self) -> &'static str {
                match self { $( $name::$var => $s, )+ }
            }
        }
    };
}

str_enum!(AppearanceMode { #[default] Auto = "auto", Dark = "dark", Light = "light" });
str_enum!(ColorMode { #[default] Auto = "auto", Truecolor = "truecolor", C256 = "256", C16 = "16", None = "none" });
str_enum!(Background { #[default] Transparent = "transparent", Theme = "theme" });
str_enum!(Glyphs { #[default] Unicode = "unicode", Nerd = "nerd", Ascii = "ascii" });
str_enum!(Borders { #[default] Rounded = "rounded", Plain = "plain", Thick = "thick", Double = "double", None = "none" });
str_enum!(Density { #[default] Comfortable = "comfortable", Compact = "compact" });
str_enum!(Motion { #[default] Auto = "auto", Marks = "marks", None = "none" });
str_enum!(Sparklines { #[default] Braille = "braille", Blocks = "blocks", None = "none" });
str_enum!(MemoryUnits { #[default] Iec = "iec", Si = "si" });
str_enum!(CpuFormat { #[default] PerCore = "per-core", Total = "total" });
str_enum!(TimeFormat { #[default] Relative = "relative", Clock = "clock" });

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(default, deny_unknown_fields)]
pub struct Appearance {
    pub theme: String,
    pub appearance: AppearanceMode,
    pub color: ColorMode,
    pub background: Background,
    pub glyphs: Glyphs,
    pub borders: Borders,
    pub density: Density,
    pub motion: Motion,
    pub sparklines: Sparklines,
    /// Header rows in display order.
    pub header: Vec<String>,
}

impl Default for Appearance {
    fn default() -> Self {
        Appearance {
            theme: "terminal".into(),
            appearance: AppearanceMode::Auto,
            color: ColorMode::Auto,
            background: Background::Transparent,
            glyphs: Glyphs::Unicode,
            borders: Borders::Rounded,
            density: Density::Comfortable,
            motion: Motion::Auto,
            sparklines: Sparklines::Braille,
            header: ["meters", "headline", "memory", "swap", "accelerators", "thermal"]
                .iter()
                .map(|s| s.to_string())
                .collect(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(default, deny_unknown_fields)]
pub struct Format {
    pub memory_units: MemoryUnits,
    pub decimals: u8,
    pub cpu: CpuFormat,
    pub time: TimeFormat,
}

impl Default for Format {
    fn default() -> Self {
        Format {
            memory_units: MemoryUnits::Iec,
            decimals: 1,
            cpu: CpuFormat::PerCore,
            time: TimeFormat::Relative,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(default, deny_unknown_fields)]
pub struct Keys {
    /// default | vim | emacs | htop
    pub preset: String,
}

impl Default for Keys {
    fn default() -> Self {
        Keys {
            preset: "default".into(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema, Default)]
#[serde(default, deny_unknown_fields)]
pub struct LayoutSel {
    /// Layout name from layouts/<name>.toml; empty = built-in adaptive layout.
    pub name: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(default, deny_unknown_fields)]
pub struct Weights {
    pub salience: f64,
    pub affinity: f64,
    pub actionability: f64,
    pub query: f64,
    pub noise: f64,
}

impl Default for Weights {
    fn default() -> Self {
        let w = oomtop_core::ranking::RankWeights::default();
        Weights {
            salience: w.salience,
            affinity: w.affinity,
            actionability: w.actionability,
            query: w.query,
            noise: w.noise,
        }
    }
}

impl Weights {
    pub fn to_core(&self) -> oomtop_core::ranking::RankWeights {
        oomtop_core::ranking::RankWeights {
            salience: self.salience,
            affinity: self.affinity,
            actionability: self.actionability,
            query: self.query,
            noise: self.noise,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(default, deny_unknown_fields)]
pub struct Personalization {
    pub learn: bool,
    pub half_life_days: f64,
    /// Impression/selection log retention in days (UX §5.6).
    pub retention_days: u32,
    /// Impression/selection log size cap in MB (UX §5.6).
    pub log_max_mb: u32,
    pub weights: Weights,
}

impl Default for Personalization {
    fn default() -> Self {
        Personalization {
            learn: true,
            half_life_days: 7.0,
            retention_days: 30,
            log_max_mb: 5,
            weights: Weights::default(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(default, deny_unknown_fields)]
pub struct Serve {
    /// Listen address; 127.0.0.1 unless explicitly changed (SPEC §13).
    pub listen: String,
}

impl Default for Serve {
    fn default() -> Self {
        Serve {
            listen: "127.0.0.1:9469".into(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema, Default)]
#[serde(default, deny_unknown_fields)]
pub struct Mcp {
    /// Register the elicitation-gated `reclaim` tool.
    pub allow_actions: bool,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema, Default)]
#[serde(default, deny_unknown_fields)]
pub struct SavedView {
    pub name: String,
    pub query: String,
    pub layout: String,
}

/// The whole configuration.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema, Default)]
#[serde(default, deny_unknown_fields)]
pub struct Config {
    pub general: General,
    pub thresholds: Thresholds,
    pub headroom: HeadroomSection,
    pub protected: Protected,
    pub models: Models,
    pub adapters: Adapters,
    pub privacy: Privacy,
    pub appearance: Appearance,
    pub format: Format,
    pub keys: Keys,
    pub layout: LayoutSel,
    pub personalization: Personalization,
    pub serve: Serve,
    pub mcp: Mcp,
    /// Query aliases (UX §12.7), e.g. llm = "kind:model,agent".
    pub aliases: BTreeMap<String, String>,
    /// Saved views (UX §12.7).
    pub views: Vec<SavedView>,
}

impl Config {
    /// Core headroom tunables.
    pub fn headroom_config(&self) -> oomtop_core::headroom::HeadroomConfig {
        let d = oomtop_core::headroom::HeadroomConfig::default();
        oomtop_core::headroom::HeadroomConfig {
            min_margin: oomtop_core::units::parse_bytes(&self.headroom.min_margin).unwrap_or(d.min_margin),
            margin_pct: self.headroom.margin_pct,
            pressure_boost_pct: self.headroom.pressure_boost_pct,
            margin_override: if self.headroom.margin_override.trim().is_empty() {
                None
            } else {
                oomtop_core::units::parse_bytes(&self.headroom.margin_override).ok()
            },
        }
    }

    /// Core idle/orphan tunables.
    pub fn idle_config(&self) -> oomtop_core::idle::IdleConfig {
        oomtop_core::idle::IdleConfig {
            cpu_pct_threshold: self.thresholds.idle_cpu_pct,
            idle_after_s: self.thresholds.idle_after_s,
            orphan_age_s: self.thresholds.orphan_age_s,
        }
    }

    /// Staleness thresholds (`thresholds.stale_*`; idle rent accrues after `thresholds.idle_after_s`).
    pub fn staleness_config(&self) -> oomtop_core::staleness::StalenessConfig {
        oomtop_core::staleness::StalenessConfig {
            idle_s: self.thresholds.stale_idle_s,
            transcript_s: self.thresholds.stale_transcript_s,
            stale_score: self.thresholds.stale_score,
            min_age_s: self.thresholds.stale_min_age_s,
            rent_after_s: self.thresholds.idle_after_s,
        }
    }
}