mesh-llm-system 0.78.0

Host system inspection and update helpers for mesh-llm
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
//! Cross-platform host activity abstraction and process priority control.
//!
//! This module intentionally does not inspect raw detector internals in logs or
//! telemetry. Backends should surface only coarse `HostActivity` values.

#[cfg(target_os = "linux")]
mod linux;

#[cfg(target_os = "macos")]
mod macos;

#[cfg(target_os = "windows")]
mod windows;

#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
mod unsupported;

use std::time::{Duration, Instant};

/// Coarse host activity inferred from platform signals.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum HostActivity {
    /// Recent user-visible activity is detected.
    Active,
    /// No activity has been observed for the configured threshold.
    Idle,
    /// Activity could not be inferred (unsupported/headless/platform/operation error).
    Unknown,
}

/// Session-only manual override for activity state.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ActivityOverride {
    /// Use detector output.
    #[default]
    Auto,
    /// Force active state for this process session.
    Active,
    /// Force idle state for this process session.
    Idle,
}

impl ActivityOverride {
    pub fn resolve(self, detected: HostActivity) -> HostActivity {
        match self {
            Self::Auto => detected,
            Self::Active => HostActivity::Active,
            Self::Idle => HostActivity::Idle,
        }
    }
}

/// Provides host activity samples from the platform.
pub trait HostActivityDetector {
    fn sample(&mut self) -> HostActivity;
}

/// Provides an injectable clock used by the debounce policy.
pub trait ActivityClock {
    fn now(&self) -> Instant;
}

/// Minimal clock implementation backed by monotonic process time.
#[derive(Default, Debug, Clone, Copy)]
pub struct SystemClock;

impl ActivityClock for SystemClock {
    fn now(&self) -> Instant {
        Instant::now()
    }
}

/// Provides capture/apply/restore around low-priority execution mode.
pub trait PriorityController {
    /// Capture the current priority and best-effort apply reduced priority.
    fn reduce_priority(&mut self) -> Result<(), PriorityFailure>;

    /// Best-effort restore priority state captured by `reduce_priority`.
    fn restore_priority(&mut self) -> Result<(), PriorityFailure>;
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PriorityFailure {
    /// Platform back-end is unsupported for this process.
    Unsupported,
    /// Reduction could not be applied.
    ApplyFailed,
    /// Previously reduced state could not be restored.
    RestoreFailed,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PriorityStatus {
    /// Priority control currently healthy.
    Healthy,
    /// Priority control is degraded and best-effort behavior is active.
    Degraded(PriorityFailure),
}

impl PriorityStatus {
    pub const fn is_degraded(self) -> bool {
        matches!(self, Self::Degraded(_))
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ActivityPolicy {
    /// Host considered idle after this amount of wall observed idle time.
    pub idle_after: Duration,
    /// Debounce duration before accepting an idle transition.
    pub resume_debounce: Duration,
}

impl ActivityPolicy {
    pub const fn new(idle_after: Duration, resume_debounce: Duration) -> Self {
        Self {
            idle_after,
            resume_debounce,
        }
    }

    fn transition_delay(&self) -> Duration {
        self.idle_after.saturating_add(self.resume_debounce)
    }
}

impl Default for ActivityPolicy {
    fn default() -> Self {
        Self {
            idle_after: Duration::from_secs(300),
            resume_debounce: Duration::from_secs(30),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct HostActivitySample {
    pub observed: HostActivity,
    pub effective: HostActivity,
    pub changed: bool,
}

/// Debounces host-activity transitions and applies session-only override policy.
pub struct HostActivityMonitor<D, C>
where
    D: HostActivityDetector,
    C: ActivityClock,
{
    detector: D,
    clock: C,
    policy: ActivityPolicy,
    override_mode: ActivityOverride,
    effective_activity: HostActivity,
    detected_idle_since: Option<Instant>,
}

impl<D, C> HostActivityMonitor<D, C>
where
    D: HostActivityDetector,
    C: ActivityClock,
{
    pub fn new(detector: D, clock: C, policy: ActivityPolicy) -> Self {
        Self {
            detector,
            clock,
            policy,
            override_mode: ActivityOverride::Auto,
            effective_activity: HostActivity::Unknown,
            detected_idle_since: None,
        }
    }

    pub fn set_override(&mut self, override_mode: ActivityOverride) {
        self.override_mode = override_mode;
    }

    pub fn activity_override(&self) -> ActivityOverride {
        self.override_mode
    }

    pub fn effective_activity(&self) -> HostActivity {
        self.effective_activity
    }

    pub fn sample(&mut self) -> HostActivitySample {
        let observed = self.override_mode.resolve(self.detector.sample());
        let next = self.next_activity(observed, self.clock.now());
        let changed = next != self.effective_activity;
        self.effective_activity = next;

        HostActivitySample {
            observed,
            effective: next,
            changed,
        }
    }

    fn next_activity(&mut self, observed: HostActivity, now: Instant) -> HostActivity {
        match observed {
            HostActivity::Active => {
                self.detected_idle_since = None;
                HostActivity::Active
            }
            HostActivity::Unknown => {
                self.detected_idle_since = None;
                HostActivity::Unknown
            }
            HostActivity::Idle => {
                if self.effective_activity == HostActivity::Idle {
                    return HostActivity::Idle;
                }

                let started_idle = self.detected_idle_since.get_or_insert(now);
                if now.duration_since(*started_idle) >= self.policy.transition_delay() {
                    HostActivity::Idle
                } else {
                    self.effective_activity
                }
            }
        }
    }
}

/// Applies/reverts priority reduction safely with fail-closed reporting.
pub struct PrioritySession<C>
where
    C: PriorityController,
{
    controller: C,
    reduced: bool,
    status: PriorityStatus,
}

impl<C> PrioritySession<C>
where
    C: PriorityController,
{
    pub fn new(controller: C) -> Self {
        Self {
            controller,
            reduced: false,
            status: PriorityStatus::Healthy,
        }
    }

    pub fn status(&self) -> PriorityStatus {
        self.status
    }

    pub fn is_reduced(&self) -> bool {
        self.reduced
    }

    pub fn is_degraded(&self) -> bool {
        self.status.is_degraded()
    }

    pub fn reduce(&mut self) -> PriorityStatus {
        if self.reduced {
            return self.status;
        }

        if let Err(reason) = self.controller.reduce_priority() {
            self.status = PriorityStatus::Degraded(reason);
            return self.status;
        }

        self.reduced = true;
        self.status = PriorityStatus::Healthy;
        self.status
    }

    pub fn restore(&mut self) -> PriorityStatus {
        if !self.reduced {
            return self.status;
        }

        if let Err(reason) = self.controller.restore_priority() {
            self.status = PriorityStatus::Degraded(reason);
            return self.status;
        }

        self.reduced = false;
        self.status = PriorityStatus::Healthy;
        self.status
    }

    pub fn clear_degraded(&mut self) {
        self.status = PriorityStatus::Healthy;
    }
}

#[cfg(target_os = "linux")]
pub type NativeHostActivityDetector = linux::NativeHostActivityDetector;

#[cfg(target_os = "linux")]
pub type NativePriorityController = linux::NativePriorityController;

#[cfg(target_os = "macos")]
pub type NativeHostActivityDetector = macos::MacHostActivityDetector;

#[cfg(target_os = "macos")]
pub type NativePriorityController = macos::MacPriorityController;

#[cfg(target_os = "windows")]
pub type NativeHostActivityDetector = windows::WindowsHostActivityDetector;

#[cfg(target_os = "windows")]
pub type NativePriorityController = windows::WindowsPriorityController;

#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
pub type NativeHostActivityDetector = unsupported::UnsupportedHostActivityDetector;

#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
pub type NativePriorityController = unsupported::UnsupportedPriorityController;

#[cfg(test)]
mod tests {
    use super::*;
    use std::cell::Cell;
    use std::collections::VecDeque;
    use std::rc::Rc;

    #[derive(Debug, Clone)]
    struct FakeClock {
        now: Rc<Cell<Instant>>,
    }

    impl FakeClock {
        fn new(start: Instant) -> Self {
            Self {
                now: Rc::new(Cell::new(start)),
            }
        }

        fn advance(&self, by: Duration) {
            self.now.set(self.now.get() + by);
        }
    }

    impl ActivityClock for FakeClock {
        fn now(&self) -> Instant {
            self.now.get()
        }
    }

    #[derive(Debug)]
    struct FakeHostActivityDetector {
        sequence: VecDeque<HostActivity>,
    }

    impl FakeHostActivityDetector {
        fn new(sequence: &[HostActivity]) -> Self {
            Self {
                sequence: VecDeque::from(sequence.to_vec()),
            }
        }
    }

    impl HostActivityDetector for FakeHostActivityDetector {
        fn sample(&mut self) -> HostActivity {
            self.sequence.pop_front().unwrap_or(HostActivity::Unknown)
        }
    }

    struct FakePriorityController {
        reduce_results: VecDeque<Result<(), PriorityFailure>>,
        restore_results: VecDeque<Result<(), PriorityFailure>>,
    }

    impl FakePriorityController {
        fn new(
            reduce_results: Vec<Result<(), PriorityFailure>>,
            restore_results: Vec<Result<(), PriorityFailure>>,
        ) -> Self {
            Self {
                reduce_results: VecDeque::from(reduce_results),
                restore_results: VecDeque::from(restore_results),
            }
        }
    }

    impl PriorityController for FakePriorityController {
        fn reduce_priority(&mut self) -> Result<(), PriorityFailure> {
            self.reduce_results.pop_front().unwrap_or(Ok(()))
        }

        fn restore_priority(&mut self) -> Result<(), PriorityFailure> {
            self.restore_results.pop_front().unwrap_or(Ok(()))
        }
    }

    #[test]
    fn activity_detector_debounce_and_override() {
        let detector = FakeHostActivityDetector::new(&[
            HostActivity::Active,
            HostActivity::Idle,
            HostActivity::Active,
            HostActivity::Idle,
            HostActivity::Idle,
            HostActivity::Idle,
        ]);

        let start = Instant::now();
        let clock = FakeClock::new(start);
        let mut monitor = HostActivityMonitor::new(
            detector,
            clock.clone(),
            ActivityPolicy::new(Duration::from_secs(2), Duration::from_secs(2)),
        );

        let sample = monitor.sample();
        assert_eq!(sample.observed, HostActivity::Active);
        assert_eq!(sample.effective, HostActivity::Active);
        assert!(sample.changed);

        monitor.set_override(ActivityOverride::Idle);
        let sample = monitor.sample();
        assert_eq!(sample.observed, HostActivity::Idle);
        assert_eq!(sample.effective, HostActivity::Active);
        assert_eq!(monitor.activity_override(), ActivityOverride::Idle);

        monitor.set_override(ActivityOverride::Auto);
        let sample = monitor.sample();
        assert_eq!(sample.observed, HostActivity::Active);
        assert_eq!(sample.effective, HostActivity::Active);

        let sample = monitor.sample();
        assert_eq!(sample.observed, HostActivity::Idle);
        assert_eq!(sample.effective, HostActivity::Active);

        clock.advance(Duration::from_secs(3));
        let sample = monitor.sample();
        assert!(!sample.changed);

        clock.advance(Duration::from_secs(2));
        let sample = monitor.sample();
        assert!(sample.changed);
        assert_eq!(sample.effective, HostActivity::Idle);

        let sample = monitor.sample();
        assert!(sample.changed);
        assert_eq!(sample.effective, HostActivity::Unknown);
    }

    #[test]
    fn activity_detector_and_priority_fail_closed_without_leak() {
        let detector = FakeHostActivityDetector::new(&[HostActivity::Unknown]);
        let mut monitor = HostActivityMonitor::new(
            detector,
            SystemClock,
            ActivityPolicy::new(Duration::from_secs(1), Duration::from_secs(1)),
        );

        let sample = monitor.sample();
        assert_eq!(sample.effective, HostActivity::Unknown);

        let mut priority = PrioritySession::new(FakePriorityController::new(
            vec![Ok(()), Err(PriorityFailure::Unsupported)],
            vec![Err(PriorityFailure::RestoreFailed), Ok(())],
        ));

        // Given reduced priority with a transient restore failure queued.
        let reduce = priority.reduce();
        assert_eq!(reduce, PriorityStatus::Healthy);
        assert!(priority.is_reduced());

        let reduce = priority.reduce();
        assert_eq!(reduce, PriorityStatus::Healthy);

        // When restore fails, then the session stays reduced and retryable.
        let restore = priority.restore();
        assert_eq!(
            restore,
            PriorityStatus::Degraded(PriorityFailure::RestoreFailed)
        );
        assert!(priority.is_reduced());

        // When a later restore succeeds, then reduced state is cleared.
        let restore = priority.restore();
        assert_eq!(restore, PriorityStatus::Healthy);
        assert!(!priority.is_reduced());
        assert_eq!(priority.status(), PriorityStatus::Healthy);

        let mut priority = PrioritySession::new(FakePriorityController::new(
            vec![Err(PriorityFailure::Unsupported)],
            vec![Err(PriorityFailure::RestoreFailed)],
        ));

        let reduced = priority.reduce();
        assert_eq!(
            reduced,
            PriorityStatus::Degraded(PriorityFailure::Unsupported)
        );
        assert!(priority.is_degraded());

        let restored = priority.restore();
        assert_eq!(
            restored,
            PriorityStatus::Degraded(PriorityFailure::Unsupported)
        );

        let rendered = format!("{:?}", priority.status());
        assert!(rendered.contains("Unsupported"));
        assert!(!rendered.contains("SENTINEL"));
        assert!(!rendered.contains("raw-error:"));
        assert!(!rendered.contains("panic"));

        let mut retry = PrioritySession::new(FakePriorityController::new(
            vec![Err(PriorityFailure::ApplyFailed), Ok(())],
            vec![Ok(())],
        ));
        assert!(retry.reduce().is_degraded());
        assert_eq!(retry.reduce(), PriorityStatus::Healthy);
        assert!(retry.is_reduced());
    }

    #[test]
    fn default_platform_aliases_are_type_checkable() {
        let _detector = NativeHostActivityDetector::default();
        let _ = _detector;
        let _controller = NativePriorityController::default();
        let _ = _controller;
        let _ = std::mem::size_of::<NativeHostActivityDetector>();
        let _ = std::mem::size_of::<NativePriorityController>();
    }
}