clawft-kernel 0.6.0

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
//! Unified persistence coordinator for kernel state.
//!
//! Provides a single entry point to save and restore all kernel
//! subsystems (CausalGraph, HNSW index, ExoChain) to a data directory.
//! Uses file-based JSON persistence — no external database required.

use std::path::PathBuf;

use serde::{Deserialize, Serialize};

use crate::causal::CausalGraph;
use crate::hnsw_service::{HnswService, HnswServiceConfig};

/// Configuration for the persistence coordinator.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PersistenceConfig {
    /// Root directory for all persisted state.
    pub data_dir: PathBuf,
    /// If set, auto-save interval in seconds (for future use with a
    /// background timer).
    pub auto_save_interval_secs: Option<u64>,
}

impl Default for PersistenceConfig {
    fn default() -> Self {
        Self {
            data_dir: PathBuf::from(".weftos/state"),
            auto_save_interval_secs: None,
        }
    }
}

impl PersistenceConfig {
    /// Path for the causal graph snapshot.
    pub fn causal_graph_path(&self) -> PathBuf {
        self.data_dir.join("causal_graph.json")
    }

    /// Path for the HNSW index snapshot.
    pub fn hnsw_index_path(&self) -> PathBuf {
        self.data_dir.join("hnsw_index.json")
    }

    /// Path for the ExoChain snapshot.
    pub fn chain_path(&self) -> PathBuf {
        self.data_dir.join("exochain.jsonl")
    }
}

/// Save the causal graph to the configured data directory.
pub fn save_causal_graph(
    config: &PersistenceConfig,
    graph: &CausalGraph,
) -> Result<(), std::io::Error> {
    graph.save_to_file(&config.causal_graph_path())
}

/// Load a causal graph from the configured data directory.
///
/// Returns a new empty graph if the file does not exist.
pub fn load_causal_graph(config: &PersistenceConfig) -> Result<CausalGraph, std::io::Error> {
    let path = config.causal_graph_path();
    if !path.exists() {
        return Ok(CausalGraph::new());
    }
    CausalGraph::load_from_file(&path)
}

/// Save the HNSW service state to the configured data directory.
pub fn save_hnsw(
    config: &PersistenceConfig,
    service: &HnswService,
) -> Result<(), std::io::Error> {
    service.save_to_file(&config.hnsw_index_path())
}

/// Load an HNSW service from the configured data directory.
///
/// Returns a new empty service if the file does not exist.
pub fn load_hnsw(config: &PersistenceConfig) -> Result<HnswService, std::io::Error> {
    let path = config.hnsw_index_path();
    if !path.exists() {
        return Ok(HnswService::new(HnswServiceConfig::default()));
    }
    HnswService::load_from_file(&path)
}

/// Save all kernel state to the configured data directory.
pub fn save_all(
    config: &PersistenceConfig,
    graph: &CausalGraph,
    hnsw: &HnswService,
) -> Result<(), std::io::Error> {
    std::fs::create_dir_all(&config.data_dir)?;
    save_causal_graph(config, graph)?;
    save_hnsw(config, hnsw)?;
    Ok(())
}

/// Restore all kernel state from the configured data directory.
///
/// Components that have no saved state are returned as fresh instances.
pub fn load_all(
    config: &PersistenceConfig,
) -> Result<(CausalGraph, HnswService), std::io::Error> {
    let graph = load_causal_graph(config)?;
    let hnsw = load_hnsw(config)?;
    Ok((graph, hnsw))
}

// ── Tests ────────────────────────────────────────────────────────────────

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

    fn tmp_config() -> PersistenceConfig {
        let dir = std::env::temp_dir().join(format!(
            "weftos_persist_test_{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        PersistenceConfig {
            data_dir: dir,
            auto_save_interval_secs: None,
        }
    }

    #[test]
    fn config_paths() {
        let cfg = PersistenceConfig {
            data_dir: PathBuf::from("/tmp/test"),
            auto_save_interval_secs: None,
        };
        assert_eq!(cfg.causal_graph_path(), PathBuf::from("/tmp/test/causal_graph.json"));
        assert_eq!(cfg.hnsw_index_path(), PathBuf::from("/tmp/test/hnsw_index.json"));
        assert_eq!(cfg.chain_path(), PathBuf::from("/tmp/test/exochain.jsonl"));
    }

    #[test]
    fn load_missing_returns_defaults() {
        let cfg = tmp_config();
        let graph = load_causal_graph(&cfg).unwrap();
        assert_eq!(graph.node_count(), 0);
        let hnsw = load_hnsw(&cfg).unwrap();
        assert!(hnsw.is_empty());
    }

    #[test]
    fn save_and_load_all_roundtrip() {
        let cfg = tmp_config();

        let graph = CausalGraph::new();
        let a = graph.add_node("A".into(), serde_json::json!({"x": 1}));
        let b = graph.add_node("B".into(), serde_json::json!({}));
        graph.link(a, b, crate::causal::CausalEdgeType::Causes, 0.9, 100, 1);

        let hnsw = HnswService::new(HnswServiceConfig::default());
        hnsw.insert("v1".into(), vec![1.0, 0.0, 0.0], serde_json::json!({"tag": "first"}));

        save_all(&cfg, &graph, &hnsw).unwrap();

        let (loaded_graph, loaded_hnsw) = load_all(&cfg).unwrap();
        assert_eq!(loaded_graph.node_count(), 2);
        assert_eq!(loaded_graph.edge_count(), 1);
        assert_eq!(loaded_hnsw.len(), 1);

        // Cleanup.
        let _ = std::fs::remove_dir_all(&cfg.data_dir);
    }

    // ── Corrupt file recovery ───────────────────────────────────────

    #[test]
    fn corrupt_causal_graph_file_returns_error() {
        let cfg = tmp_config();
        std::fs::create_dir_all(&cfg.data_dir).unwrap();

        // Write garbage bytes to the causal graph file.
        std::fs::write(cfg.causal_graph_path(), b"{{{{not json at all!").unwrap();

        let result = load_causal_graph(&cfg);
        assert!(result.is_err(), "loading corrupt causal graph should fail");
        let err = result.unwrap_err();
        assert_eq!(err.kind(), std::io::ErrorKind::InvalidData);

        let _ = std::fs::remove_dir_all(&cfg.data_dir);
    }

    #[test]
    fn corrupt_hnsw_file_returns_error() {
        let cfg = tmp_config();
        std::fs::create_dir_all(&cfg.data_dir).unwrap();

        // Write garbage bytes to the HNSW file.
        std::fs::write(cfg.hnsw_index_path(), b"\x00\x01\x02binary garbage").unwrap();

        let result = load_hnsw(&cfg);
        assert!(result.is_err(), "loading corrupt HNSW index should fail");
        let err = result.unwrap_err();
        assert_eq!(err.kind(), std::io::ErrorKind::InvalidData);

        let _ = std::fs::remove_dir_all(&cfg.data_dir);
    }

    #[test]
    fn truncated_json_returns_error() {
        let cfg = tmp_config();
        std::fs::create_dir_all(&cfg.data_dir).unwrap();

        // Write truncated but plausible JSON.
        std::fs::write(cfg.causal_graph_path(), b"{\"next_node_id\":5,\"nodes\":").unwrap();

        let result = load_causal_graph(&cfg);
        assert!(result.is_err(), "loading truncated JSON should fail");

        let _ = std::fs::remove_dir_all(&cfg.data_dir);
    }

    #[test]
    fn empty_file_returns_error() {
        let cfg = tmp_config();
        std::fs::create_dir_all(&cfg.data_dir).unwrap();

        // Write zero-length file.
        std::fs::write(cfg.causal_graph_path(), b"").unwrap();

        let result = load_causal_graph(&cfg);
        assert!(result.is_err(), "loading empty file should fail");

        let _ = std::fs::remove_dir_all(&cfg.data_dir);
    }

    // ── Concurrent write handling ───────────────────────────────────

    #[test]
    fn concurrent_saves_do_not_corrupt() {
        let cfg = tmp_config();
        std::fs::create_dir_all(&cfg.data_dir).unwrap();

        let graph = CausalGraph::new();
        for i in 0..100 {
            graph.add_node(format!("node-{i}"), serde_json::json!({"i": i}));
        }
        let hnsw = HnswService::new(HnswServiceConfig::default());
        for i in 0..50 {
            hnsw.insert(format!("v{i}"), vec![i as f32, 0.0, 0.0], serde_json::json!({}));
        }

        // Save from two threads simultaneously.
        let cfg1 = cfg.clone();
        let cfg2 = cfg.clone();
        let graph_ref = &graph;
        let hnsw_ref = &hnsw;

        std::thread::scope(|s| {
            let h1 = s.spawn(|| save_all(&cfg1, graph_ref, hnsw_ref));
            let h2 = s.spawn(|| save_all(&cfg2, graph_ref, hnsw_ref));

            // Both saves should succeed (last-writer-wins on file I/O).
            h1.join().unwrap().unwrap();
            h2.join().unwrap().unwrap();
        });

        // The resulting files should be loadable.
        let (loaded_graph, loaded_hnsw) = load_all(&cfg).unwrap();
        assert_eq!(loaded_graph.node_count(), 100);
        assert_eq!(loaded_hnsw.len(), 50);

        let _ = std::fs::remove_dir_all(&cfg.data_dir);
    }

    // ── Disk-full / read-only simulation ────────────────────────────

    #[test]
    fn save_to_nonexistent_deep_path_creates_dirs() {
        let cfg = PersistenceConfig {
            data_dir: std::env::temp_dir()
                .join(format!("weftos_deep_{}",
                    std::time::SystemTime::now()
                        .duration_since(std::time::UNIX_EPOCH)
                        .unwrap()
                        .as_nanos()))
                .join("a")
                .join("b")
                .join("c"),
            auto_save_interval_secs: None,
        };

        let graph = CausalGraph::new();
        let hnsw = HnswService::new(HnswServiceConfig::default());

        // save_all creates intermediate directories.
        save_all(&cfg, &graph, &hnsw).unwrap();
        assert!(cfg.causal_graph_path().exists());

        let _ = std::fs::remove_dir_all(
            cfg.data_dir.parent().unwrap().parent().unwrap().parent().unwrap(),
        );
    }

    #[test]
    fn save_to_readonly_dir_fails() {
        // Create a directory, then make it read-only.
        let base = std::env::temp_dir().join(format!(
            "weftos_ro_{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        std::fs::create_dir_all(&base).unwrap();

        // Set directory permissions to read-only.
        let mut perms = std::fs::metadata(&base).unwrap().permissions();
        #[allow(clippy::permissions_set_readonly_false)]
        perms.set_readonly(true);
        std::fs::set_permissions(&base, perms.clone()).unwrap();

        let cfg = PersistenceConfig {
            data_dir: base.join("state"),
            auto_save_interval_secs: None,
        };

        let graph = CausalGraph::new();
        let hnsw = HnswService::new(HnswServiceConfig::default());

        let result = save_all(&cfg, &graph, &hnsw);
        assert!(result.is_err(), "saving to read-only dir should fail");

        // Restore permissions for cleanup.
        perms.set_readonly(false);
        let _ = std::fs::set_permissions(&base, perms);
        let _ = std::fs::remove_dir_all(&base);
    }

    // ── Large data roundtrip ────────────────────────────────────────

    #[test]
    fn save_load_roundtrip_large_graph() {
        let cfg = tmp_config();

        let graph = CausalGraph::new();
        let mut node_ids = Vec::with_capacity(1000);
        for i in 0..1000 {
            let nid = graph.add_node(
                format!("node-{i}"),
                serde_json::json!({"index": i, "data": "x".repeat(50)}),
            );
            node_ids.push(nid);
        }

        // Create edges between consecutive nodes.
        for window in node_ids.windows(2) {
            graph.link(
                window[0],
                window[1],
                crate::causal::CausalEdgeType::Follows,
                0.8,
                window[0],
                0,
            );
        }

        let hnsw = HnswService::new(HnswServiceConfig::default());
        for i in 0..1000 {
            hnsw.insert(
                format!("vec-{i}"),
                vec![i as f32, (i * 2) as f32, (i * 3) as f32],
                serde_json::json!({"i": i}),
            );
        }

        save_all(&cfg, &graph, &hnsw).unwrap();

        let (loaded_graph, loaded_hnsw) = load_all(&cfg).unwrap();
        assert_eq!(loaded_graph.node_count(), 1000);
        assert_eq!(loaded_graph.edge_count(), 999);
        assert_eq!(loaded_hnsw.len(), 1000);

        let _ = std::fs::remove_dir_all(&cfg.data_dir);
    }

    #[test]
    fn save_load_roundtrip_preserves_node_data() {
        let cfg = tmp_config();

        let graph = CausalGraph::new();
        let nid = graph.add_node(
            "important-node".into(),
            serde_json::json!({"key": "value", "nested": {"a": [1,2,3]}}),
        );

        let hnsw = HnswService::new(HnswServiceConfig::default());

        save_all(&cfg, &graph, &hnsw).unwrap();

        let (loaded_graph, _) = load_all(&cfg).unwrap();
        assert_eq!(loaded_graph.node_count(), 1);
        // Verify the node data is intact by checking we can retrieve by ID.
        let nodes = loaded_graph.get_node(nid);
        assert!(nodes.is_some(), "loaded graph should contain the saved node");
        assert_eq!(nodes.unwrap().label, "important-node");

        let _ = std::fs::remove_dir_all(&cfg.data_dir);
    }

    #[test]
    fn double_save_load_is_idempotent() {
        let cfg = tmp_config();

        let graph = CausalGraph::new();
        graph.add_node("A".into(), serde_json::json!({}));
        let hnsw = HnswService::new(HnswServiceConfig::default());

        save_all(&cfg, &graph, &hnsw).unwrap();
        save_all(&cfg, &graph, &hnsw).unwrap();

        let (loaded, _) = load_all(&cfg).unwrap();
        assert_eq!(loaded.node_count(), 1);

        let _ = std::fs::remove_dir_all(&cfg.data_dir);
    }
}