clawft-kernel 0.6.4

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
//! Kernel console: boot event types and output formatting.
//!
//! Provides [`BootEvent`], [`BootPhase`], and [`LogLevel`] types for
//! recording and displaying kernel boot output. The interactive REPL
//! loop is stubbed (requires complex stdin handling); only the event
//! types and output formatting are implemented.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// Phase of the boot sequence.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum BootPhase {
    /// Pre-boot initialization.
    Init,
    /// Loading configuration.
    Config,
    /// Registering system services.
    Services,
    /// Loading resource tree from DAG.
    ResourceTree,
    /// Spawning service agents.
    Agents,
    /// Network service discovery.
    Network,
    /// ECC cognitive substrate initialization.
    Ecc,
    /// Boot complete, ready for commands.
    Ready,
}

impl BootPhase {
    /// Short tag string for console output (e.g. `[INIT]`).
    pub fn tag(&self) -> &'static str {
        match self {
            BootPhase::Init => "INIT",
            BootPhase::Config => "CONFIG",
            BootPhase::Services => "SERVICES",
            BootPhase::ResourceTree => "TREE",
            BootPhase::Agents => "AGENTS",
            BootPhase::Network => "NETWORK",
            BootPhase::Ecc => "ECC",
            BootPhase::Ready => "READY",
        }
    }
}

impl std::fmt::Display for BootPhase {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.tag())
    }
}

/// Log level for boot events.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum LogLevel {
    /// Debug-level messages (not shown in normal boot output).
    Debug,
    /// Informational messages (standard boot output).
    Info,
    /// Warning messages.
    Warn,
    /// Error messages.
    Error,
}

/// A single boot event recorded during kernel startup.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BootEvent {
    /// When the event occurred.
    pub timestamp: DateTime<Utc>,
    /// Which boot phase generated the event.
    pub phase: BootPhase,
    /// Human-readable event message.
    pub message: String,
    /// Severity level.
    pub level: LogLevel,
}

impl BootEvent {
    /// Create a new info-level boot event.
    pub fn info(phase: BootPhase, message: impl Into<String>) -> Self {
        Self {
            timestamp: Utc::now(),
            phase,
            message: message.into(),
            level: LogLevel::Info,
        }
    }

    /// Create a new warning-level boot event.
    pub fn warn(phase: BootPhase, message: impl Into<String>) -> Self {
        Self {
            timestamp: Utc::now(),
            phase,
            message: message.into(),
            level: LogLevel::Warn,
        }
    }

    /// Create a new error-level boot event.
    pub fn error(phase: BootPhase, message: impl Into<String>) -> Self {
        Self {
            timestamp: Utc::now(),
            phase,
            message: message.into(),
            level: LogLevel::Error,
        }
    }

    /// Format this event for console display.
    ///
    /// Example: `  [INIT]      WeftOS v0.1.0 booting...`
    pub fn format_line(&self) -> String {
        let tag = self.phase.tag();
        format!("  [{tag:<10}] {}", self.message)
    }
}

/// Boot log: a recorded sequence of boot events.
///
/// Used to replay boot output when attaching to a running kernel
/// or for diagnostics.
#[derive(Debug, Clone, Default)]
pub struct BootLog {
    events: Vec<BootEvent>,
}

impl BootLog {
    /// Create an empty boot log.
    pub fn new() -> Self {
        Self { events: Vec::new() }
    }

    /// Record a boot event.
    pub fn push(&mut self, event: BootEvent) {
        self.events.push(event);
    }

    /// Get all recorded events.
    pub fn events(&self) -> &[BootEvent] {
        &self.events
    }

    /// Format all events for console display.
    pub fn format_all(&self) -> String {
        let mut output = String::new();
        for event in &self.events {
            if event.level == LogLevel::Debug {
                continue;
            }
            output.push_str(&event.format_line());
            output.push('\n');
        }
        output
    }

    /// Get the number of recorded events.
    pub fn len(&self) -> usize {
        self.events.len()
    }

    /// Check whether the log is empty.
    pub fn is_empty(&self) -> bool {
        self.events.is_empty()
    }
}

/// Default capacity of the kernel event ring buffer.
const DEFAULT_EVENT_LOG_CAPACITY: usize = 1024;

/// Thread-safe ring buffer for kernel runtime events.
///
/// Captures boot events and any post-boot events (service starts/stops,
/// agent spawns, health checks, errors). Holds at most `capacity` events
/// — when full, the oldest event is evicted.
///
/// Used by the daemon to serve `kernel.logs` RPC requests.
pub struct KernelEventLog {
    events: std::sync::Mutex<std::collections::VecDeque<BootEvent>>,
    capacity: usize,
}

impl KernelEventLog {
    /// Create a new event log with the default capacity (1024).
    pub fn new() -> Self {
        Self::with_capacity(DEFAULT_EVENT_LOG_CAPACITY)
    }

    /// Create a new event log with a specific capacity.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            events: std::sync::Mutex::new(std::collections::VecDeque::with_capacity(capacity)),
            capacity,
        }
    }

    /// Push an event into the ring buffer.
    pub fn push(&self, event: BootEvent) {
        let mut events = self.events.lock().unwrap();
        if events.len() >= self.capacity {
            events.pop_front();
        }
        events.push_back(event);
    }

    /// Push a simple info event with a source tag and message.
    pub fn info(&self, source: &str, message: impl Into<String>) {
        self.push(BootEvent {
            timestamp: Utc::now(),
            phase: BootPhase::Ready, // post-boot events use Ready phase
            message: format!("[{source}] {}", message.into()),
            level: LogLevel::Info,
        });
    }

    /// Push a warning event.
    pub fn warn(&self, source: &str, message: impl Into<String>) {
        self.push(BootEvent {
            timestamp: Utc::now(),
            phase: BootPhase::Ready,
            message: format!("[{source}] {}", message.into()),
            level: LogLevel::Warn,
        });
    }

    /// Push an error event.
    pub fn error(&self, source: &str, message: impl Into<String>) {
        self.push(BootEvent {
            timestamp: Utc::now(),
            phase: BootPhase::Ready,
            message: format!("[{source}] {}", message.into()),
            level: LogLevel::Error,
        });
    }

    /// Push an info event and optionally append to the local chain.
    #[cfg(feature = "exochain")]
    pub fn info_with_chain(
        &self,
        source: &str,
        message: impl Into<String>,
        chain: Option<&crate::chain::ChainManager>,
    ) {
        let msg = message.into();
        self.info(source, &msg);
        if let Some(cm) = chain {
            cm.append(source, "log.info", Some(serde_json::json!({ "message": msg })));
        }
    }

    /// Ingest all events from a BootLog (used to seed boot events).
    pub fn ingest_boot_log(&self, boot_log: &BootLog) {
        for event in boot_log.events() {
            self.push(event.clone());
        }
    }

    /// Get the last `n` events (or all if `n` is 0 or exceeds count).
    pub fn tail(&self, n: usize) -> Vec<BootEvent> {
        let events = self.events.lock().unwrap();
        if n == 0 || n >= events.len() {
            events.iter().cloned().collect()
        } else {
            events.iter().skip(events.len() - n).cloned().collect()
        }
    }

    /// Get all events matching a minimum log level.
    pub fn filter_level(&self, min_level: &LogLevel, n: usize) -> Vec<BootEvent> {
        let events = self.events.lock().unwrap();
        let level_rank = |l: &LogLevel| -> u8 {
            match l {
                LogLevel::Debug => 0,
                LogLevel::Info => 1,
                LogLevel::Warn => 2,
                LogLevel::Error => 3,
            }
        };
        let min_rank = level_rank(min_level);
        let filtered: Vec<BootEvent> = events
            .iter()
            .filter(|e| level_rank(&e.level) >= min_rank)
            .cloned()
            .collect();
        if n == 0 || n >= filtered.len() {
            filtered
        } else {
            filtered[filtered.len() - n..].to_vec()
        }
    }

    /// Current number of events in the buffer.
    pub fn len(&self) -> usize {
        self.events.lock().unwrap().len()
    }

    /// Check if the buffer is empty.
    pub fn is_empty(&self) -> bool {
        self.events.lock().unwrap().is_empty()
    }
}

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

impl std::fmt::Debug for KernelEventLog {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let len = self.len();
        f.debug_struct("KernelEventLog")
            .field("count", &len)
            .field("capacity", &self.capacity)
            .finish()
    }
}

/// Format the boot banner header.
pub fn boot_banner() -> String {
    let mut output = String::new();
    output.push_str("\n  WeftOS v0.1.0\n");
    output.push_str("  ");
    output.push_str(&"-".repeat(45));
    output.push('\n');
    output
}

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

    #[test]
    fn boot_phase_tags() {
        assert_eq!(BootPhase::Init.tag(), "INIT");
        assert_eq!(BootPhase::Config.tag(), "CONFIG");
        assert_eq!(BootPhase::Services.tag(), "SERVICES");
        assert_eq!(BootPhase::ResourceTree.tag(), "TREE");
        assert_eq!(BootPhase::Agents.tag(), "AGENTS");
        assert_eq!(BootPhase::Network.tag(), "NETWORK");
        assert_eq!(BootPhase::Ready.tag(), "READY");
    }

    #[test]
    fn boot_event_info() {
        let event = BootEvent::info(BootPhase::Init, "WeftOS v0.1.0 booting...");
        assert_eq!(event.phase, BootPhase::Init);
        assert_eq!(event.level, LogLevel::Info);
        assert_eq!(event.message, "WeftOS v0.1.0 booting...");
    }

    #[test]
    fn boot_event_format_line() {
        let event = BootEvent::info(BootPhase::Init, "PID 0 (kernel)");
        let line = event.format_line();
        assert!(line.contains("[INIT"));
        assert!(line.contains("PID 0 (kernel)"));
    }

    #[test]
    fn boot_log_push_and_format() {
        let mut log = BootLog::new();
        log.push(BootEvent::info(BootPhase::Init, "booting..."));
        log.push(BootEvent::info(BootPhase::Config, "config loaded"));
        log.push(BootEvent::info(BootPhase::Ready, "ready"));

        assert_eq!(log.len(), 3);
        let formatted = log.format_all();
        assert!(formatted.contains("booting..."));
        assert!(formatted.contains("config loaded"));
        assert!(formatted.contains("ready"));
    }

    #[test]
    fn boot_log_skips_debug() {
        let mut log = BootLog::new();
        log.push(BootEvent {
            timestamp: Utc::now(),
            phase: BootPhase::Init,
            message: "debug msg".into(),
            level: LogLevel::Debug,
        });
        log.push(BootEvent::info(BootPhase::Init, "info msg"));

        let formatted = log.format_all();
        assert!(!formatted.contains("debug msg"));
        assert!(formatted.contains("info msg"));
    }

    #[test]
    fn boot_log_empty() {
        let log = BootLog::new();
        assert!(log.is_empty());
        assert_eq!(log.len(), 0);
        assert!(log.format_all().is_empty());
    }

    #[test]
    fn boot_banner_format() {
        let banner = boot_banner();
        assert!(banner.contains("WeftOS v0.1.0"));
        assert!(banner.contains("---"));
    }

    #[test]
    fn boot_event_serde() {
        let event = BootEvent::info(BootPhase::Services, "[OK] message-bus");
        let json = serde_json::to_string(&event).unwrap();
        let restored: BootEvent = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.phase, BootPhase::Services);
        assert_eq!(restored.message, "[OK] message-bus");
    }

    #[test]
    fn boot_event_warn_and_error() {
        let warn_event = BootEvent::warn(BootPhase::Services, "slow start");
        assert_eq!(warn_event.level, LogLevel::Warn);

        let err_event = BootEvent::error(BootPhase::Services, "failed");
        assert_eq!(err_event.level, LogLevel::Error);
    }

    // ── KernelEventLog tests ──────────────────────────────────

    #[test]
    fn event_log_push_and_tail() {
        let log = KernelEventLog::new();
        log.info("test", "first");
        log.info("test", "second");
        log.warn("test", "third");

        assert_eq!(log.len(), 3);

        let all = log.tail(0);
        assert_eq!(all.len(), 3);
        assert!(all[0].message.contains("first"));

        let last_two = log.tail(2);
        assert_eq!(last_two.len(), 2);
        assert!(last_two[0].message.contains("second"));
    }

    #[test]
    fn event_log_ring_buffer_evicts() {
        let log = KernelEventLog::with_capacity(3);
        log.info("a", "1");
        log.info("b", "2");
        log.info("c", "3");
        log.info("d", "4"); // evicts "1"

        assert_eq!(log.len(), 3);
        let all = log.tail(0);
        assert!(all[0].message.contains("[b] 2"));
        assert!(all[2].message.contains("[d] 4"));
    }

    #[test]
    fn event_log_filter_level() {
        let log = KernelEventLog::new();
        log.info("test", "info msg");
        log.warn("test", "warn msg");
        log.error("test", "error msg");

        let warns_and_above = log.filter_level(&LogLevel::Warn, 0);
        assert_eq!(warns_and_above.len(), 2);

        let errors_only = log.filter_level(&LogLevel::Error, 0);
        assert_eq!(errors_only.len(), 1);
    }

    #[test]
    fn event_log_ingest_boot_log() {
        let mut boot_log = BootLog::new();
        boot_log.push(BootEvent::info(BootPhase::Init, "booting"));
        boot_log.push(BootEvent::info(BootPhase::Ready, "ready"));

        let event_log = KernelEventLog::new();
        event_log.ingest_boot_log(&boot_log);

        assert_eq!(event_log.len(), 2);
        let events = event_log.tail(0);
        assert!(events[0].message.contains("booting"));
    }

    #[test]
    fn event_log_empty() {
        let log = KernelEventLog::new();
        assert!(log.is_empty());
        assert_eq!(log.len(), 0);
        assert!(log.tail(10).is_empty());
    }
}