clawft-kernel 0.5.2

WeftOS kernel: boot sequence, process table, service registry, and health subsystem
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
//! Kernel metrics collection with lock-free counters, gauges, and histograms.
//!
//! [`MetricsRegistry`] provides a lock-free metrics subsystem. Counters and
//! gauges use atomic operations on the hot path. Histograms use fixed bucket
//! boundaries with atomic counters -- O(buckets) per record.

use std::sync::atomic::{AtomicI64, AtomicU64, Ordering};

use dashmap::DashMap;
use serde::{Deserialize, Serialize};

/// Default histogram bucket boundaries (in milliseconds for latency metrics).
pub const DEFAULT_BUCKETS: &[f64] = &[1.0, 5.0, 10.0, 25.0, 50.0, 100.0, 250.0, 500.0, 1000.0];

// ── Built-in metric names ───────────────────────────────────────

/// Counter: total messages sent via A2ARouter.
pub const METRIC_MESSAGES_SENT: &str = "kernel.messages_sent";
/// Counter: total messages successfully delivered.
pub const METRIC_MESSAGES_DELIVERED: &str = "kernel.messages_delivered";
/// Counter: total messages dropped (dead-lettered).
pub const METRIC_MESSAGES_DROPPED: &str = "kernel.messages_dropped";
/// Counter: total agent spawns.
pub const METRIC_AGENT_SPAWNS: &str = "kernel.agent_spawns";
/// Counter: total agent crashes.
pub const METRIC_AGENT_CRASHES: &str = "kernel.agent_crashes";
/// Counter: total tool executions.
pub const METRIC_TOOL_EXECUTIONS: &str = "kernel.tool_executions";
/// Gauge: currently active agents.
pub const METRIC_ACTIVE_AGENTS: &str = "kernel.active_agents";
/// Gauge: currently active services.
pub const METRIC_ACTIVE_SERVICES: &str = "kernel.active_services";
/// Gauge: exochain length.
pub const METRIC_CHAIN_LENGTH: &str = "kernel.chain_length";
/// Histogram: IPC latency in milliseconds.
pub const METRIC_IPC_LATENCY_MS: &str = "kernel.ipc_latency_ms";
/// Histogram: tool execution time in milliseconds.
pub const METRIC_TOOL_EXECUTION_MS: &str = "kernel.tool_execution_ms";
/// Histogram: governance evaluation time in milliseconds.
pub const METRIC_GOVERNANCE_EVAL_MS: &str = "kernel.governance_evaluation_ms";

/// A fixed-bucket histogram for recording distributions.
///
/// Each bucket tracks a count of values <= the bucket boundary.
/// The `sum` and `count` fields track totals for mean calculation.
pub struct Histogram {
    /// Bucket boundaries and their counts. Each bucket counts values
    /// that are <= the boundary (cumulative).
    buckets: Vec<(f64, AtomicU64)>,
    /// Sum of all recorded values (stored as u64 bits of f64).
    sum_bits: AtomicU64,
    /// Total number of recorded values.
    count: AtomicU64,
}

impl Histogram {
    /// Create a histogram with the given bucket boundaries.
    ///
    /// Boundaries are sorted ascending. An implicit +Inf bucket is
    /// always present.
    pub fn new(boundaries: &[f64]) -> Self {
        let mut sorted: Vec<f64> = boundaries.to_vec();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
        sorted.dedup();

        let buckets = sorted
            .into_iter()
            .map(|b| (b, AtomicU64::new(0)))
            .collect();

        Self {
            buckets,
            sum_bits: AtomicU64::new(0),
            count: AtomicU64::new(0),
        }
    }

    /// Create a histogram with default bucket boundaries.
    pub fn with_defaults() -> Self {
        Self::new(DEFAULT_BUCKETS)
    }

    /// Record a value in the histogram.
    pub fn record(&self, value: f64) {
        // Increment the count for each bucket whose boundary >= value.
        // This gives us cumulative counts.
        for (bound, count) in &self.buckets {
            if value <= *bound {
                count.fetch_add(1, Ordering::Relaxed);
            }
        }

        // Add to sum (atomic f64 addition via CAS loop)
        loop {
            let old_bits = self.sum_bits.load(Ordering::Relaxed);
            let old_val = f64::from_bits(old_bits);
            let new_val = old_val + value;
            let new_bits = new_val.to_bits();
            if self
                .sum_bits
                .compare_exchange_weak(old_bits, new_bits, Ordering::Relaxed, Ordering::Relaxed)
                .is_ok()
            {
                break;
            }
        }

        self.count.fetch_add(1, Ordering::Relaxed);
    }

    /// Get the total count of recorded values.
    pub fn total_count(&self) -> u64 {
        self.count.load(Ordering::Relaxed)
    }

    /// Get the sum of all recorded values.
    pub fn sum(&self) -> f64 {
        f64::from_bits(self.sum_bits.load(Ordering::Relaxed))
    }

    /// Get a snapshot of bucket boundaries and their cumulative counts.
    pub fn bucket_snapshot(&self) -> Vec<(f64, u64)> {
        self.buckets
            .iter()
            .map(|(bound, count)| (*bound, count.load(Ordering::Relaxed)))
            .collect()
    }

    /// Estimate a percentile value from the histogram buckets.
    ///
    /// Uses linear interpolation between bucket boundaries.
    /// Returns `None` if no values have been recorded.
    pub fn percentile(&self, p: f64) -> Option<f64> {
        let total = self.total_count();
        if total == 0 {
            return None;
        }

        let target = (p * total as f64).ceil() as u64;
        let buckets = self.bucket_snapshot();

        // Find the first bucket whose cumulative count >= target
        for (i, (bound, cumulative)) in buckets.iter().enumerate() {
            if *cumulative >= target {
                if i == 0 {
                    return Some(*bound);
                }
                // Linear interpolation between previous and current bucket
                let prev_bound = buckets[i - 1].0;
                let prev_count = buckets[i - 1].1;
                let remaining = target.saturating_sub(prev_count) as f64;
                let bucket_count = cumulative.saturating_sub(prev_count) as f64;
                if bucket_count <= 0.0 {
                    return Some(*bound);
                }
                let fraction = remaining / bucket_count;
                return Some(prev_bound + fraction * (bound - prev_bound));
            }
        }

        // Value exceeds all bucket boundaries
        buckets.last().map(|(b, _)| *b)
    }
}

/// A snapshot of a single metric.
#[non_exhaustive]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MetricSnapshot {
    /// A counter metric (monotonically increasing).
    Counter {
        /// Metric name.
        name: String,
        /// Current value.
        value: u64,
    },
    /// A gauge metric (can go up or down).
    Gauge {
        /// Metric name.
        name: String,
        /// Current value.
        value: i64,
    },
    /// A histogram metric (distribution).
    Histogram {
        /// Metric name.
        name: String,
        /// Bucket boundaries and cumulative counts.
        buckets: Vec<(f64, u64)>,
        /// Sum of all values.
        sum: f64,
        /// Total count of values.
        count: u64,
    },
}

/// Registry of all kernel metrics. Lock-free on the hot path.
///
/// Counters use `AtomicU64`, gauges use `AtomicI64`, and histograms
/// use fixed-bucket atomic counters. `DashMap` provides concurrent
/// access to the registry without global locks.
pub struct MetricsRegistry {
    counters: DashMap<String, AtomicU64>,
    gauges: DashMap<String, AtomicI64>,
    histograms: DashMap<String, Histogram>,
}

impl MetricsRegistry {
    /// Create a new empty metrics registry.
    pub fn new() -> Self {
        Self {
            counters: DashMap::new(),
            gauges: DashMap::new(),
            histograms: DashMap::new(),
        }
    }

    /// Create a registry pre-populated with built-in kernel metrics.
    pub fn with_builtins() -> Self {
        let registry = Self::new();

        // Counters
        for name in [
            METRIC_MESSAGES_SENT,
            METRIC_MESSAGES_DELIVERED,
            METRIC_MESSAGES_DROPPED,
            METRIC_AGENT_SPAWNS,
            METRIC_AGENT_CRASHES,
            METRIC_TOOL_EXECUTIONS,
        ] {
            registry.counters.insert(name.to_string(), AtomicU64::new(0));
        }

        // Gauges
        for name in [
            METRIC_ACTIVE_AGENTS,
            METRIC_ACTIVE_SERVICES,
            METRIC_CHAIN_LENGTH,
        ] {
            registry.gauges.insert(name.to_string(), AtomicI64::new(0));
        }

        // Histograms
        for name in [
            METRIC_IPC_LATENCY_MS,
            METRIC_TOOL_EXECUTION_MS,
            METRIC_GOVERNANCE_EVAL_MS,
        ] {
            registry
                .histograms
                .insert(name.to_string(), Histogram::with_defaults());
        }

        registry
    }

    // ── Counter operations ──────────────────────────────────────

    /// Increment a counter by 1.
    pub fn counter_inc(&self, name: &str) {
        self.counter_add(name, 1);
    }

    /// Add a value to a counter.
    pub fn counter_add(&self, name: &str, value: u64) {
        match self.counters.get(name) {
            Some(counter) => {
                counter.fetch_add(value, Ordering::Relaxed);
            }
            None => {
                self.counters
                    .insert(name.to_string(), AtomicU64::new(value));
            }
        }
    }

    /// Get the current value of a counter.
    pub fn counter_get(&self, name: &str) -> u64 {
        self.counters
            .get(name)
            .map(|c| c.load(Ordering::Relaxed))
            .unwrap_or(0)
    }

    // ── Gauge operations ────────────────────────────────────────

    /// Set a gauge to a specific value.
    pub fn gauge_set(&self, name: &str, value: i64) {
        match self.gauges.get(name) {
            Some(gauge) => {
                gauge.store(value, Ordering::Relaxed);
            }
            None => {
                self.gauges.insert(name.to_string(), AtomicI64::new(value));
            }
        }
    }

    /// Increment a gauge by a value.
    pub fn gauge_inc(&self, name: &str, delta: i64) {
        match self.gauges.get(name) {
            Some(gauge) => {
                gauge.fetch_add(delta, Ordering::Relaxed);
            }
            None => {
                self.gauges
                    .insert(name.to_string(), AtomicI64::new(delta));
            }
        }
    }

    /// Decrement a gauge by a value.
    pub fn gauge_dec(&self, name: &str, delta: i64) {
        self.gauge_inc(name, -delta);
    }

    /// Get the current value of a gauge.
    pub fn gauge_get(&self, name: &str) -> i64 {
        self.gauges
            .get(name)
            .map(|g| g.load(Ordering::Relaxed))
            .unwrap_or(0)
    }

    // ── Histogram operations ────────────────────────────────────

    /// Record a value in a histogram.
    ///
    /// If the histogram does not exist, it is created with default buckets.
    pub fn histogram_record(&self, name: &str, value: f64) {
        match self.histograms.get(name) {
            Some(hist) => {
                hist.record(value);
            }
            None => {
                let hist = Histogram::with_defaults();
                hist.record(value);
                self.histograms.insert(name.to_string(), hist);
            }
        }
    }

    /// Register a histogram with custom bucket boundaries.
    pub fn histogram_register(&self, name: &str, boundaries: &[f64]) {
        if !self.histograms.contains_key(name) {
            self.histograms
                .insert(name.to_string(), Histogram::new(boundaries));
        }
    }

    /// Get a percentile estimate from a histogram.
    pub fn histogram_percentile(&self, name: &str, p: f64) -> Option<f64> {
        self.histograms.get(name).and_then(|h| h.percentile(p))
    }

    /// Get histogram bucket snapshot.
    pub fn histogram_buckets(&self, name: &str) -> Option<Vec<(f64, u64)>> {
        self.histograms.get(name).map(|h| h.bucket_snapshot())
    }

    // ── Snapshot ────────────────────────────────────────────────

    /// Take a snapshot of all metrics.
    pub fn snapshot_all(&self) -> Vec<MetricSnapshot> {
        let mut snapshots = Vec::new();

        for entry in self.counters.iter() {
            snapshots.push(MetricSnapshot::Counter {
                name: entry.key().clone(),
                value: entry.value().load(Ordering::Relaxed),
            });
        }

        for entry in self.gauges.iter() {
            snapshots.push(MetricSnapshot::Gauge {
                name: entry.key().clone(),
                value: entry.value().load(Ordering::Relaxed),
            });
        }

        for entry in self.histograms.iter() {
            let hist = entry.value();
            snapshots.push(MetricSnapshot::Histogram {
                name: entry.key().clone(),
                buckets: hist.bucket_snapshot(),
                sum: hist.sum(),
                count: hist.total_count(),
            });
        }

        snapshots
    }

    /// List all metric names.
    pub fn list_names(&self) -> Vec<String> {
        let mut names: Vec<String> = Vec::new();
        for e in self.counters.iter() {
            names.push(e.key().clone());
        }
        for e in self.gauges.iter() {
            names.push(e.key().clone());
        }
        for e in self.histograms.iter() {
            names.push(e.key().clone());
        }
        names
    }
}

impl Default for MetricsRegistry {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn counter_increment_and_get() {
        let registry = MetricsRegistry::new();
        registry.counter_inc("test.counter");
        registry.counter_inc("test.counter");
        assert_eq!(registry.counter_get("test.counter"), 2);
    }

    #[test]
    fn counter_add() {
        let registry = MetricsRegistry::new();
        registry.counter_add("test.counter", 10);
        registry.counter_add("test.counter", 5);
        assert_eq!(registry.counter_get("test.counter"), 15);
    }

    #[test]
    fn counter_get_nonexistent_returns_zero() {
        let registry = MetricsRegistry::new();
        assert_eq!(registry.counter_get("nope"), 0);
    }

    #[test]
    fn gauge_set_and_get() {
        let registry = MetricsRegistry::new();
        registry.gauge_set("test.gauge", 42);
        assert_eq!(registry.gauge_get("test.gauge"), 42);
    }

    #[test]
    fn gauge_increment_decrement() {
        let registry = MetricsRegistry::new();
        registry.gauge_set("test.gauge", 10);
        registry.gauge_inc("test.gauge", 5);
        assert_eq!(registry.gauge_get("test.gauge"), 15);
        registry.gauge_dec("test.gauge", 3);
        assert_eq!(registry.gauge_get("test.gauge"), 12);
    }

    #[test]
    fn gauge_negative() {
        let registry = MetricsRegistry::new();
        registry.gauge_set("test.gauge", -5);
        assert_eq!(registry.gauge_get("test.gauge"), -5);
    }

    #[test]
    fn histogram_record_and_count() {
        let registry = MetricsRegistry::new();
        registry.histogram_record("test.hist", 5.0);
        registry.histogram_record("test.hist", 15.0);
        registry.histogram_record("test.hist", 150.0);

        let buckets = registry.histogram_buckets("test.hist").unwrap();
        // 5.0 should be in bucket 5.0 and above
        // 15.0 should be in bucket 25.0 and above
        // 150.0 should be in bucket 250.0 and above

        // Bucket 5.0: count of values <= 5 = 1
        let b5 = buckets.iter().find(|(b, _)| (*b - 5.0).abs() < f64::EPSILON);
        assert_eq!(b5.unwrap().1, 1);

        // Bucket 25.0: count of values <= 25 = 2
        let b25 = buckets.iter().find(|(b, _)| (*b - 25.0).abs() < f64::EPSILON);
        assert_eq!(b25.unwrap().1, 2);

        // Bucket 250.0: count of values <= 250 = 3
        let b250 = buckets.iter().find(|(b, _)| (*b - 250.0).abs() < f64::EPSILON);
        assert_eq!(b250.unwrap().1, 3);
    }

    #[test]
    fn histogram_sum_and_count() {
        let hist = Histogram::with_defaults();
        hist.record(10.0);
        hist.record(20.0);
        hist.record(30.0);

        assert_eq!(hist.total_count(), 3);
        assert!((hist.sum() - 60.0).abs() < f64::EPSILON);
    }

    #[test]
    fn histogram_percentile_p50() {
        let hist = Histogram::new(&[10.0, 20.0, 30.0, 40.0, 50.0]);
        for v in [5.0, 15.0, 25.0, 35.0, 45.0] {
            hist.record(v);
        }

        let p50 = hist.percentile(0.50).unwrap();
        // 5 values total, p50 = 3rd value, should be in 20-30 range
        assert!(p50 >= 20.0 && p50 <= 30.0, "p50 = {p50}");
    }

    #[test]
    fn histogram_percentile_p95() {
        let hist = Histogram::new(&[10.0, 50.0, 100.0, 500.0, 1000.0]);
        for _ in 0..95 {
            hist.record(5.0);
        }
        for _ in 0..5 {
            hist.record(800.0);
        }

        let p95 = hist.percentile(0.95).unwrap();
        // 95% of values are <= 10.0
        assert!(p95 <= 10.0, "p95 = {p95}");
    }

    #[test]
    fn histogram_percentile_p99() {
        let hist = Histogram::new(&[10.0, 50.0, 100.0, 500.0, 1000.0]);
        for _ in 0..99 {
            hist.record(5.0);
        }
        hist.record(800.0);

        let p99 = hist.percentile(0.99).unwrap();
        assert!(p99 <= 10.0, "p99 = {p99}");
    }

    #[test]
    fn histogram_percentile_empty() {
        let hist = Histogram::with_defaults();
        assert!(hist.percentile(0.50).is_none());
    }

    #[test]
    fn registry_with_builtins() {
        let registry = MetricsRegistry::with_builtins();
        assert_eq!(registry.counter_get(METRIC_MESSAGES_SENT), 0);
        assert_eq!(registry.gauge_get(METRIC_ACTIVE_AGENTS), 0);
        assert!(registry.histogram_buckets(METRIC_IPC_LATENCY_MS).is_some());
    }

    #[test]
    fn snapshot_all() {
        let registry = MetricsRegistry::new();
        registry.counter_inc("c1");
        registry.gauge_set("g1", 42);
        registry.histogram_record("h1", 10.0);

        let snap = registry.snapshot_all();
        assert_eq!(snap.len(), 3);

        let has_counter = snap.iter().any(|s| matches!(s, MetricSnapshot::Counter { name, .. } if name == "c1"));
        let has_gauge = snap.iter().any(|s| matches!(s, MetricSnapshot::Gauge { name, value } if name == "g1" && *value == 42));
        let has_hist = snap.iter().any(|s| matches!(s, MetricSnapshot::Histogram { name, .. } if name == "h1"));

        assert!(has_counter);
        assert!(has_gauge);
        assert!(has_hist);
    }

    #[test]
    fn list_names() {
        let registry = MetricsRegistry::new();
        registry.counter_inc("alpha");
        registry.gauge_set("beta", 1);
        registry.histogram_record("gamma", 1.0);

        let names = registry.list_names();
        assert_eq!(names.len(), 3);
        assert!(names.contains(&"alpha".to_string()));
        assert!(names.contains(&"beta".to_string()));
        assert!(names.contains(&"gamma".to_string()));
    }

    #[test]
    fn counter_concurrent_atomicity() {
        let registry = MetricsRegistry::new();
        // Simulate concurrent increments
        for _ in 0..1000 {
            registry.counter_inc("concurrent");
        }
        assert_eq!(registry.counter_get("concurrent"), 1000);
    }

    #[test]
    fn histogram_custom_buckets() {
        let registry = MetricsRegistry::new();
        registry.histogram_register("custom", &[1.0, 2.0, 5.0]);
        registry.histogram_record("custom", 1.5);

        let buckets = registry.histogram_buckets("custom").unwrap();
        assert_eq!(buckets.len(), 3);
        // 1.5 is > 1.0 but <= 2.0
        assert_eq!(buckets[0].1, 0); // <= 1.0
        assert_eq!(buckets[1].1, 1); // <= 2.0
        assert_eq!(buckets[2].1, 1); // <= 5.0
    }

    #[test]
    fn metric_snapshot_serde() {
        let snap = MetricSnapshot::Counter {
            name: "test".into(),
            value: 42,
        };
        let json = serde_json::to_string(&snap).unwrap();
        let restored: MetricSnapshot = serde_json::from_str(&json).unwrap();
        assert!(matches!(
            restored,
            MetricSnapshot::Counter { value: 42, .. }
        ));
    }

    #[test]
    fn default_registry() {
        let registry = MetricsRegistry::default();
        assert!(registry.list_names().is_empty());
    }
}