clawft-kernel 0.5.3

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
//! Reconciliation controller (K2b-G1).
//!
//! Compares desired state (from `AppManifest`) against actual state
//! (from `ProcessTable`) and takes corrective action: spawning
//! missing agents, stopping extra agents, and logging state
//! mismatches.
//!
//! Gated behind `cfg(feature = "os-patterns")`.

use std::sync::Arc;
use std::time::Duration;

use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use tokio::sync::RwLock;
use tracing::debug;

use crate::capability::AgentCapabilities;
use crate::process::{Pid, ProcessState, ProcessTable};

/// Desired state for a single agent.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DesiredAgentState {
    /// Application that owns this agent.
    pub app_id: String,
    /// Agent identifier.
    pub agent_id: String,
    /// Agent type label.
    pub agent_type: String,
    /// Number of replicas desired.
    pub replicas: u32,
    /// Capabilities to assign.
    pub capabilities: AgentCapabilities,
}

/// A drift event detected by the reconciler.
#[non_exhaustive]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DriftEvent {
    /// A desired agent is not running.
    AgentMissing {
        agent_id: String,
        app_id: String,
    },
    /// An agent is running but not in the desired state.
    ExtraAgent {
        pid: Pid,
        agent_id: String,
    },
    /// An agent is in the wrong state.
    WrongState {
        pid: Pid,
        expected: String,
        actual: String,
    },
    /// An agent was spawned to correct drift.
    AgentSpawned {
        agent_id: String,
        app_id: String,
    },
    /// An extra agent was stopped.
    AgentStopped {
        pid: Pid,
        agent_id: String,
    },
}

/// Reconciliation controller: desired state vs actual state.
///
/// Registered as a `SystemService` with `ServiceType::Core`.
/// Runs a background tick every `interval` to detect and correct
/// drift between desired and actual process states.
pub struct ReconciliationController {
    interval: Duration,
    desired: DashMap<String, DesiredAgentState>,
    drifts: Arc<RwLock<Vec<DriftEvent>>>,
    process_table: Arc<ProcessTable>,
    max_drift_history: usize,
}

impl ReconciliationController {
    /// Create a new reconciliation controller.
    pub fn new(
        process_table: Arc<ProcessTable>,
        interval: Duration,
    ) -> Self {
        Self {
            interval,
            desired: DashMap::new(),
            drifts: Arc::new(RwLock::new(Vec::new())),
            process_table,
            max_drift_history: 100,
        }
    }

    /// Get the tick interval.
    pub fn interval(&self) -> Duration {
        self.interval
    }

    /// Set desired state for an agent.
    pub fn set_desired(&self, key: String, state: DesiredAgentState) {
        self.desired.insert(key, state);
    }

    /// Remove desired state for an agent.
    pub fn remove_desired(&self, key: &str) -> Option<DesiredAgentState> {
        self.desired.remove(key).map(|(_, v)| v)
    }

    /// Get all desired states.
    pub fn list_desired(&self) -> Vec<DesiredAgentState> {
        self.desired.iter().map(|e| e.value().clone()).collect()
    }

    /// Get recent drift events.
    pub async fn recent_drifts(&self) -> Vec<DriftEvent> {
        self.drifts.read().await.clone()
    }

    /// Run a single reconciliation tick.
    ///
    /// Compares desired state against process table and returns
    /// detected drifts. Does NOT take corrective action (that
    /// requires a supervisor reference and governance gate).
    pub async fn tick(&self) -> Vec<DriftEvent> {
        let mut drifts = Vec::new();

        // Check desired agents exist in process table
        for entry in self.desired.iter() {
            let desired = entry.value();
            let matching: Vec<_> = self
                .process_table
                .list()
                .into_iter()
                .filter(|p| p.agent_id == desired.agent_id)
                .collect();

            let running_count = matching
                .iter()
                .filter(|p| p.state == ProcessState::Running)
                .count() as u32;

            if running_count < desired.replicas {
                for _ in 0..(desired.replicas - running_count) {
                    drifts.push(DriftEvent::AgentMissing {
                        agent_id: desired.agent_id.clone(),
                        app_id: desired.app_id.clone(),
                    });
                }
            }
        }

        // Check for extra agents (not in desired state)
        for entry in self.process_table.list() {
            if entry.state != ProcessState::Running {
                continue;
            }
            // Skip kernel and system processes
            if entry.agent_id.starts_with("kernel.") || entry.agent_id.starts_with("system.") {
                continue;
            }
            // Skip PID 0 (kernel)
            if entry.pid == 0 {
                continue;
            }

            let is_desired = self
                .desired
                .iter()
                .any(|d| d.value().agent_id == entry.agent_id);

            if !is_desired {
                drifts.push(DriftEvent::ExtraAgent {
                    pid: entry.pid,
                    agent_id: entry.agent_id.clone(),
                });
            }
        }

        // Store drift history (bounded)
        {
            let mut history = self.drifts.write().await;
            history.extend(drifts.clone());
            while history.len() > self.max_drift_history {
                history.remove(0);
            }
        }

        debug!(drift_count = drifts.len(), "reconciliation tick completed");
        drifts
    }

    /// Record a corrective action in drift history.
    pub async fn record_action(&self, event: DriftEvent) {
        let mut history = self.drifts.write().await;
        history.push(event);
        while history.len() > self.max_drift_history {
            history.remove(0);
        }
    }

    /// Clear all desired state (used during shutdown).
    pub fn clear_desired(&self) {
        self.desired.clear();
    }

    /// Get the number of desired agent entries.
    pub fn desired_count(&self) -> usize {
        self.desired.len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::capability::AgentCapabilities;
    use crate::process::{ProcessEntry, ResourceUsage};
    use tokio_util::sync::CancellationToken;

    fn make_process_table() -> Arc<ProcessTable> {
        Arc::new(ProcessTable::new(64))
    }

    fn make_controller(pt: Arc<ProcessTable>) -> ReconciliationController {
        ReconciliationController::new(pt, Duration::from_secs(5))
    }

    fn insert_running_agent(pt: &ProcessTable, agent_id: &str) -> Pid {
        let entry = ProcessEntry {
            pid: 0,
            agent_id: agent_id.to_owned(),
            state: ProcessState::Starting,
            capabilities: AgentCapabilities::default(),
            resource_usage: ResourceUsage::default(),
            cancel_token: CancellationToken::new(),
            parent_pid: None,
        };
        let pid = pt.insert(entry).unwrap();
        pt.update_state(pid, ProcessState::Running).unwrap();
        pid
    }

    fn make_desired(app_id: &str, agent_id: &str) -> DesiredAgentState {
        DesiredAgentState {
            app_id: app_id.to_owned(),
            agent_id: agent_id.to_owned(),
            agent_type: "worker".to_owned(),
            replicas: 1,
            capabilities: AgentCapabilities::default(),
        }
    }

    #[tokio::test]
    async fn detects_missing_agent() {
        let pt = make_process_table();
        let ctrl = make_controller(pt);

        ctrl.set_desired("app/worker-1".into(), make_desired("app", "worker-1"));

        let drifts = ctrl.tick().await;
        assert_eq!(drifts.len(), 1);
        assert!(matches!(&drifts[0], DriftEvent::AgentMissing { agent_id, .. } if agent_id == "worker-1"));
    }

    #[tokio::test]
    async fn no_drift_when_agent_running() {
        let pt = make_process_table();
        insert_running_agent(&pt, "worker-1");
        let ctrl = make_controller(pt);

        ctrl.set_desired("app/worker-1".into(), make_desired("app", "worker-1"));

        let drifts = ctrl.tick().await;
        // No missing agents, but worker-1 is desired so no extra either
        assert!(drifts.is_empty());
    }

    #[tokio::test]
    async fn detects_extra_agent() {
        let pt = make_process_table();
        insert_running_agent(&pt, "rogue-agent");
        let ctrl = make_controller(pt);

        // No desired state set -- rogue-agent is extra
        let drifts = ctrl.tick().await;
        assert_eq!(drifts.len(), 1);
        assert!(matches!(&drifts[0], DriftEvent::ExtraAgent { agent_id, .. } if agent_id == "rogue-agent"));
    }

    #[tokio::test]
    async fn skips_kernel_and_system_agents() {
        let pt = make_process_table();
        insert_running_agent(&pt, "kernel.boot");
        insert_running_agent(&pt, "system.health");
        let ctrl = make_controller(pt);

        let drifts = ctrl.tick().await;
        assert!(drifts.is_empty());
    }

    #[tokio::test]
    async fn detects_multiple_missing_replicas() {
        let pt = make_process_table();
        let ctrl = make_controller(pt);

        let mut desired = make_desired("app", "worker-1");
        desired.replicas = 3;
        ctrl.set_desired("app/worker-1".into(), desired);

        let drifts = ctrl.tick().await;
        assert_eq!(drifts.len(), 3);
    }

    #[tokio::test]
    async fn partial_missing_replicas() {
        let pt = make_process_table();
        insert_running_agent(&pt, "worker-1");
        let ctrl = make_controller(pt);

        let mut desired = make_desired("app", "worker-1");
        desired.replicas = 3;
        ctrl.set_desired("app/worker-1".into(), desired);

        let drifts = ctrl.tick().await;
        // 3 desired, 1 running = 2 missing
        let missing: Vec<_> = drifts
            .iter()
            .filter(|d| matches!(d, DriftEvent::AgentMissing { .. }))
            .collect();
        assert_eq!(missing.len(), 2);
    }

    #[tokio::test]
    async fn drift_history_bounded() {
        let pt = make_process_table();
        let ctrl = make_controller(pt);

        // Insert 150 drifts manually via record_action
        for i in 0..150 {
            ctrl.record_action(DriftEvent::AgentMissing {
                agent_id: format!("agent-{i}"),
                app_id: "app".into(),
            })
            .await;
        }

        let history = ctrl.recent_drifts().await;
        assert!(history.len() <= 100);
    }

    #[tokio::test]
    async fn set_and_remove_desired() {
        let pt = make_process_table();
        let ctrl = make_controller(pt);

        ctrl.set_desired("app/w1".into(), make_desired("app", "w1"));
        assert_eq!(ctrl.desired_count(), 1);

        let removed = ctrl.remove_desired("app/w1");
        assert!(removed.is_some());
        assert_eq!(ctrl.desired_count(), 0);
    }

    #[tokio::test]
    async fn clear_desired() {
        let pt = make_process_table();
        let ctrl = make_controller(pt);

        ctrl.set_desired("app/w1".into(), make_desired("app", "w1"));
        ctrl.set_desired("app/w2".into(), make_desired("app", "w2"));
        assert_eq!(ctrl.desired_count(), 2);

        ctrl.clear_desired();
        assert_eq!(ctrl.desired_count(), 0);
    }

    #[tokio::test]
    async fn interval_accessor() {
        let pt = make_process_table();
        let ctrl = ReconciliationController::new(pt, Duration::from_secs(10));
        assert_eq!(ctrl.interval(), Duration::from_secs(10));
    }

    #[test]
    fn drift_event_serde_roundtrip() {
        let events = vec![
            DriftEvent::AgentMissing {
                agent_id: "w1".into(),
                app_id: "app".into(),
            },
            DriftEvent::ExtraAgent {
                pid: 42,
                agent_id: "rogue".into(),
            },
            DriftEvent::WrongState {
                pid: 5,
                expected: "running".into(),
                actual: "stopped".into(),
            },
        ];
        for event in events {
            let json = serde_json::to_string(&event).unwrap();
            let _: DriftEvent = serde_json::from_str(&json).unwrap();
        }
    }

    #[test]
    fn desired_agent_state_serde() {
        let state = make_desired("app", "worker-1");
        let json = serde_json::to_string(&state).unwrap();
        let restored: DesiredAgentState = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.agent_id, "worker-1");
        assert_eq!(restored.app_id, "app");
    }
}