unit 0.26.2

A self-replicating software nanobot — minimal Forth interpreter that is also a networked mesh agent
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
//! JSON-based state persistence for unit.
//!
//! Saves and loads a unit's state as human-readable JSON so hackers can
//! inspect and hand-edit a unit's brain. Zero dependencies -- hand-written
//! JSON serializer/parser.

#[cfg(not(target_arch = "wasm32"))]
use crate::mesh::NodeId;
use crate::types::{Cell, Entry, Instruction};

// ---------------------------------------------------------------------------
// UnitSnapshot — the JSON-serializable state of a unit
// ---------------------------------------------------------------------------

/// The JSON-serializable state of a unit, capturing stack, dictionary, energy, and more.
#[derive(Clone, Debug)]
pub struct UnitSnapshot {
    pub node_id: String,
    pub timestamp: u64,
    pub stack: Vec<Cell>,
    pub fitness: i64,
    pub tasks_completed: u32,
    pub generation: u32,
    pub mutation_stats: MutStats,
    pub words: Vec<(String, String)>, // (name, decompiled source)
    pub memory_here: usize,
    pub memory: Vec<Cell>, // only up to `here`
    // Energy state
    pub energy: i64,
    pub energy_max: i64,
    pub energy_earned: u64,
    pub energy_spent: u64,
    // Landscape state
    pub landscape_depth: u32,
    pub landscape_generated: u64,
}

/// Mutation outcome statistics: counts of neutral, beneficial, harmful, and lethal mutations.
#[derive(Clone, Debug, Default)]
pub struct MutStats {
    pub total: u32,
    pub neutral: u32,
    pub beneficial: u32,
    pub harmful: u32,
    pub lethal: u32,
}

// ---------------------------------------------------------------------------
// JSON serializer (no serde)
// ---------------------------------------------------------------------------

fn escape_json_string(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 8);
    for c in s.chars() {
        match c {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if (c as u32) < 0x20 => {
                out.push_str(&format!("\\u{:04x}", c as u32));
            }
            _ => out.push(c),
        }
    }
    out
}

/// Serializes a `UnitSnapshot` to a human-readable JSON string.
pub fn to_json(snap: &UnitSnapshot) -> String {
    let mut j = String::with_capacity(4096);
    j.push_str("{\n");
    j.push_str("  \"version\": 1,\n");
    j.push_str(&format!(
        "  \"node_id\": \"{}\",\n",
        escape_json_string(&snap.node_id)
    ));
    j.push_str(&format!("  \"timestamp\": {},\n", snap.timestamp));
    j.push_str(&format!("  \"fitness\": {},\n", snap.fitness));
    j.push_str(&format!(
        "  \"tasks_completed\": {},\n",
        snap.tasks_completed
    ));
    j.push_str(&format!("  \"generation\": {},\n", snap.generation));

    // Energy
    j.push_str(&format!("  \"energy\": {},\n", snap.energy));
    j.push_str(&format!("  \"energy_max\": {},\n", snap.energy_max));
    j.push_str(&format!("  \"energy_earned\": {},\n", snap.energy_earned));
    j.push_str(&format!("  \"energy_spent\": {},\n", snap.energy_spent));

    // Landscape
    j.push_str(&format!(
        "  \"landscape_depth\": {},\n",
        snap.landscape_depth
    ));
    j.push_str(&format!(
        "  \"landscape_generated\": {},\n",
        snap.landscape_generated
    ));

    // Stack
    j.push_str("  \"stack\": [");
    for (i, v) in snap.stack.iter().enumerate() {
        if i > 0 {
            j.push_str(", ");
        }
        j.push_str(&format!("{}", v));
    }
    j.push_str("],\n");

    // Mutation stats
    j.push_str("  \"mutation_stats\": {\n");
    j.push_str(&format!("    \"total\": {},\n", snap.mutation_stats.total));
    j.push_str(&format!(
        "    \"neutral\": {},\n",
        snap.mutation_stats.neutral
    ));
    j.push_str(&format!(
        "    \"beneficial\": {},\n",
        snap.mutation_stats.beneficial
    ));
    j.push_str(&format!(
        "    \"harmful\": {},\n",
        snap.mutation_stats.harmful
    ));
    j.push_str(&format!("    \"lethal\": {}\n", snap.mutation_stats.lethal));
    j.push_str("  },\n");

    // Words (user-defined)
    j.push_str("  \"words\": {\n");
    for (i, (name, source)) in snap.words.iter().enumerate() {
        j.push_str(&format!(
            "    \"{}\": \"{}\"",
            escape_json_string(name),
            escape_json_string(source)
        ));
        if i + 1 < snap.words.len() {
            j.push(',');
        }
        j.push('\n');
    }
    j.push_str("  },\n");

    // Memory
    j.push_str(&format!("  \"memory_here\": {},\n", snap.memory_here));
    j.push_str("  \"memory\": [");
    for (i, v) in snap.memory.iter().enumerate() {
        if i > 0 {
            j.push_str(", ");
        }
        j.push_str(&format!("{}", v));
    }
    j.push_str("]\n");
    j.push_str("}\n");
    j
}

// ---------------------------------------------------------------------------
// JSON parser (minimal, for known UnitSnapshot structure)
// ---------------------------------------------------------------------------

/// Parses a JSON string into a `UnitSnapshot`, returning `None` on invalid input.
pub fn from_json(input: &str) -> Option<UnitSnapshot> {
    let mut snap = UnitSnapshot {
        node_id: String::new(),
        timestamp: 0,
        stack: Vec::new(),
        fitness: 0,
        tasks_completed: 0,
        generation: 0,
        mutation_stats: MutStats::default(),
        words: Vec::new(),
        memory_here: 0,
        memory: Vec::new(),
        energy: crate::energy::INITIAL_ENERGY,
        energy_max: crate::energy::MAX_ENERGY,
        energy_earned: 0,
        energy_spent: 0,
        landscape_depth: 0,
        landscape_generated: 0,
    };

    let input = input.trim();
    if !input.starts_with('{') || !input.ends_with('}') {
        return None;
    }

    // Simple line-by-line parsing for our known JSON structure.
    let mut in_words = false;
    let mut in_mutation = false;

    for line in input.lines() {
        let line = line.trim();

        // Detect section transitions
        if line.starts_with("\"words\"") && line.contains('{') {
            in_words = true;
            continue;
        }
        if in_words && line.starts_with('}') {
            in_words = false;
            continue;
        }
        if line.starts_with("\"mutation_stats\"") && line.contains('{') {
            in_mutation = true;
            continue;
        }
        if in_mutation && line.starts_with('}') {
            in_mutation = false;
            continue;
        }

        // Parse stack array on one line
        if line.starts_with("\"stack\"") {
            if let Some(arr) = extract_array(line) {
                snap.stack = parse_i64_array(&arr);
            }
            continue;
        }

        // Parse memory array on one line
        if line.starts_with("\"memory\"") && line.contains('[') {
            if let Some(arr) = extract_array(line) {
                snap.memory = parse_i64_array(&arr);
            }
            continue;
        }

        if in_words {
            // Parse "NAME": "source"
            if let Some((key, val)) = parse_kv_string(line) {
                snap.words.push((key, val));
            }
            continue;
        }

        if in_mutation {
            if let Some((key, val)) = parse_kv_number(line) {
                match key.as_str() {
                    "total" => snap.mutation_stats.total = val as u32,
                    "neutral" => snap.mutation_stats.neutral = val as u32,
                    "beneficial" => snap.mutation_stats.beneficial = val as u32,
                    "harmful" => snap.mutation_stats.harmful = val as u32,
                    "lethal" => snap.mutation_stats.lethal = val as u32,
                    _ => {}
                }
            }
            continue;
        }

        // Top-level scalar fields
        if let Some((key, val)) = parse_kv_number(line) {
            match key.as_str() {
                "timestamp" => snap.timestamp = val as u64,
                "fitness" => snap.fitness = val,
                "tasks_completed" => snap.tasks_completed = val as u32,
                "generation" => snap.generation = val as u32,
                "memory_here" => snap.memory_here = val as usize,
                "energy" => snap.energy = val,
                "energy_max" => snap.energy_max = val,
                "energy_earned" => snap.energy_earned = val as u64,
                "energy_spent" => snap.energy_spent = val as u64,
                "landscape_depth" => snap.landscape_depth = val as u32,
                "landscape_generated" => snap.landscape_generated = val as u64,
                "version" => {} // ignore
                _ => {}
            }
        } else if let Some((key, val)) = parse_kv_string(line) {
            if key.as_str() == "node_id" {
                snap.node_id = val
            }
        }
    }

    Some(snap)
}

// Extract the contents between [ and ] from a line
fn extract_array(line: &str) -> Option<String> {
    let start = line.find('[')?;
    let end = line.rfind(']')?;
    Some(line[start + 1..end].to_string())
}

fn parse_i64_array(s: &str) -> Vec<i64> {
    s.split(',')
        .filter(|p| !p.trim().is_empty())
        .filter_map(|p| p.trim().parse().ok())
        .collect()
}

// Parse "key": "value" (possibly with trailing comma)
fn parse_kv_string(line: &str) -> Option<(String, String)> {
    let line = line.trim().trim_end_matches(',');
    let colon = line.find(':')?;
    let key = line[..colon].trim().trim_matches('"');
    let val_part = line[colon + 1..].trim();
    if val_part.starts_with('"') && val_part.ends_with('"') {
        let inner = &val_part[1..val_part.len() - 1];
        Some((key.to_string(), unescape_json_string(inner)))
    } else {
        None
    }
}

// Parse "key": number (possibly with trailing comma)
fn parse_kv_number(line: &str) -> Option<(String, i64)> {
    let line = line.trim().trim_end_matches(',');
    let colon = line.find(':')?;
    let key = line[..colon].trim().trim_matches('"');
    let val = line[colon + 1..].trim().parse::<i64>().ok()?;
    Some((key.to_string(), val))
}

fn unescape_json_string(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut chars = s.chars();
    while let Some(c) = chars.next() {
        if c == '\\' {
            match chars.next() {
                Some('"') => out.push('"'),
                Some('\\') => out.push('\\'),
                Some('n') => out.push('\n'),
                Some('r') => out.push('\r'),
                Some('t') => out.push('\t'),
                Some(other) => {
                    out.push('\\');
                    out.push(other);
                }
                None => out.push('\\'),
            }
        } else {
            out.push(c);
        }
    }
    out
}

// ---------------------------------------------------------------------------
// Decompile a word to Forth source
// ---------------------------------------------------------------------------

/// Decompiles a dictionary entry back into readable Forth source code.
pub fn decompile_word(
    entry: &Entry,
    dictionary: &[Entry],
    primitive_names: &[(String, usize)],
) -> String {
    let mut out = format!(": {} ", entry.name);
    for instr in &entry.body {
        match instr {
            Instruction::Primitive(id) => {
                let pname = primitive_names
                    .iter()
                    .find(|(_, pid)| pid == id)
                    .map(|(n, _)| n.as_str())
                    .unwrap_or("?PRIM");
                out.push_str(pname);
                out.push(' ');
            }
            Instruction::Literal(val) => {
                out.push_str(&format!("{} ", val));
            }
            Instruction::Call(idx) => {
                if *idx < dictionary.len() {
                    out.push_str(&dictionary[*idx].name);
                    out.push(' ');
                }
            }
            Instruction::StringLit(s) => {
                out.push_str(&format!(".\" {}\" ", s));
            }
            Instruction::Branch(off) => {
                out.push_str(&format!("BRANCH({}) ", off));
            }
            Instruction::BranchIfZero(off) => {
                out.push_str(&format!("0BRANCH({}) ", off));
            }
        }
    }
    out.push(';');
    out
}

// ---------------------------------------------------------------------------
// File I/O helpers
// ---------------------------------------------------------------------------

/// Returns the directory path for storing snapshot files.
#[cfg(not(target_arch = "wasm32"))]
pub fn snapshot_dir(_node_id: &NodeId) -> String {
    let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
    format!("{}/.unit/snapshots", home)
}

/// Returns the full file path for a node's snapshot JSON file.
#[cfg(not(target_arch = "wasm32"))]
pub fn snapshot_path(node_id: &NodeId) -> String {
    let id_hex: String = node_id.iter().map(|b| format!("{:02x}", b)).collect();
    format!("{}/{}.json", snapshot_dir(node_id), id_hex)
}

/// Writes a JSON snapshot to disk, creating directories as needed.
#[cfg(not(target_arch = "wasm32"))]
pub fn save_json_snapshot(node_id: &NodeId, json: &str) -> Result<String, String> {
    let dir = snapshot_dir(node_id);
    std::fs::create_dir_all(&dir).map_err(|e| format!("mkdir: {}", e))?;
    let path = snapshot_path(node_id);
    std::fs::write(&path, json).map_err(|e| format!("write: {}", e))?;
    Ok(path)
}

/// Loads a JSON snapshot from disk, returning `None` if missing.
#[cfg(not(target_arch = "wasm32"))]
pub fn load_json_snapshot(node_id: &NodeId) -> Option<String> {
    let path = snapshot_path(node_id);
    std::fs::read_to_string(&path).ok()
}

/// Lists all snapshot node IDs found in the snapshot directory.
#[cfg(not(target_arch = "wasm32"))]
pub fn list_json_snapshots() -> Vec<String> {
    let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
    let dir = format!("{}/.unit/snapshots", home);
    let mut names = Vec::new();
    if let Ok(entries) = std::fs::read_dir(&dir) {
        for entry in entries.flatten() {
            let name = entry.file_name().to_string_lossy().to_string();
            if name.ends_with(".json") {
                names.push(name.trim_end_matches(".json").to_string());
            }
        }
    }
    names.sort();
    names
}

// ---------------------------------------------------------------------------
// WASM: in-memory snapshot storage (no filesystem)
// ---------------------------------------------------------------------------

#[cfg(target_arch = "wasm32")]
mod wasm_store {
    use std::cell::RefCell;

    thread_local! {
        static SNAPSHOT: RefCell<Option<String>> = RefCell::new(None);
    }

    pub fn save(json: &str) {
        SNAPSHOT.with(|s| *s.borrow_mut() = Some(json.to_string()));
    }

    pub fn load() -> Option<String> {
        SNAPSHOT.with(|s| s.borrow().clone())
    }

    pub fn has_snapshot() -> bool {
        SNAPSHOT.with(|s| s.borrow().is_some())
    }
}

#[cfg(target_arch = "wasm32")]
pub fn snapshot_path(_node_id: &[u8; 8]) -> String {
    "(in-memory)".to_string()
}

#[cfg(target_arch = "wasm32")]
pub fn save_json_snapshot(_node_id: &[u8; 8], json: &str) -> Result<String, String> {
    wasm_store::save(json);
    Ok("(in-memory)".to_string())
}

#[cfg(target_arch = "wasm32")]
pub fn load_json_snapshot(_node_id: &[u8; 8]) -> Option<String> {
    wasm_store::load()
}

#[cfg(target_arch = "wasm32")]
pub fn list_json_snapshots() -> Vec<String> {
    if wasm_store::has_snapshot() {
        vec!["(in-memory)".to_string()]
    } else {
        Vec::new()
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn make_test_snapshot() -> UnitSnapshot {
        UnitSnapshot {
            node_id: "abcdef0123456789".to_string(),
            timestamp: 1711900800,
            stack: vec![42, -7, 100],
            fitness: 55,
            tasks_completed: 12,
            generation: 3,
            mutation_stats: MutStats {
                total: 20,
                neutral: 10,
                beneficial: 5,
                harmful: 3,
                lethal: 2,
            },
            words: vec![
                ("SQUARE".to_string(), ": SQUARE DUP * ;".to_string()),
                ("CUBE".to_string(), ": CUBE DUP SQUARE * ;".to_string()),
            ],
            memory_here: 3,
            memory: vec![0, 42, -1],
            energy: 800,
            energy_max: 5000,
            energy_earned: 500,
            energy_spent: 700,
            landscape_depth: 2,
            landscape_generated: 5,
        }
    }

    #[test]
    fn test_roundtrip() {
        let snap = make_test_snapshot();
        let json = to_json(&snap);
        let restored = from_json(&json).unwrap();
        assert_eq!(snap.node_id, restored.node_id);
        assert_eq!(snap.timestamp, restored.timestamp);
        assert_eq!(snap.stack, restored.stack);
        assert_eq!(snap.fitness, restored.fitness);
        assert_eq!(snap.tasks_completed, restored.tasks_completed);
        assert_eq!(snap.generation, restored.generation);
        assert_eq!(snap.mutation_stats.total, restored.mutation_stats.total);
        assert_eq!(
            snap.mutation_stats.beneficial,
            restored.mutation_stats.beneficial
        );
        assert_eq!(snap.words, restored.words);
        assert_eq!(snap.memory_here, restored.memory_here);
        assert_eq!(snap.memory, restored.memory);
    }

    #[test]
    fn test_empty_snapshot() {
        let snap = UnitSnapshot {
            node_id: "0000000000000000".to_string(),
            timestamp: 0,
            stack: vec![],
            fitness: 0,
            tasks_completed: 0,
            generation: 0,
            mutation_stats: MutStats::default(),
            words: vec![],
            memory_here: 0,
            memory: vec![],
            energy: 1000,
            energy_max: 5000,
            energy_earned: 0,
            energy_spent: 0,
            landscape_depth: 0,
            landscape_generated: 0,
        };
        let json = to_json(&snap);
        let restored = from_json(&json).unwrap();
        assert_eq!(restored.stack.len(), 0);
        assert_eq!(restored.words.len(), 0);
        assert_eq!(restored.memory.len(), 0);
    }

    #[test]
    fn test_escape_roundtrip() {
        let snap = UnitSnapshot {
            node_id: "test".to_string(),
            timestamp: 0,
            stack: vec![],
            fitness: 0,
            tasks_completed: 0,
            generation: 0,
            mutation_stats: MutStats::default(),
            words: vec![(
                "HELLO".to_string(),
                ": HELLO .\" hello\\nworld\" ;".to_string(),
            )],
            memory_here: 0,
            memory: vec![],
            energy: 1000,
            energy_max: 5000,
            energy_earned: 0,
            energy_spent: 0,
            landscape_depth: 0,
            landscape_generated: 0,
        };
        let json = to_json(&snap);
        let restored = from_json(&json).unwrap();
        assert_eq!(snap.words[0].1, restored.words[0].1);
    }

    #[test]
    fn test_corrupt_json() {
        assert!(from_json("not json").is_none());
        assert!(from_json("").is_none());
        // Minimal valid JSON
        assert!(from_json("{}").is_some());
    }

    #[test]
    fn test_json_is_human_readable() {
        let snap = make_test_snapshot();
        let json = to_json(&snap);
        assert!(json.contains("\"node_id\""));
        assert!(json.contains("SQUARE"));
        assert!(json.contains("CUBE"));
        assert!(json.contains("\"fitness\": 55"));
    }

    #[test]
    fn test_decompile_word() {
        use crate::types::Instruction;
        let entry = Entry {
            name: "TEST".to_string(),
            immediate: false,
            hidden: false,
            body: vec![
                Instruction::Literal(42),
                Instruction::Primitive(7), // P_ADD
            ],
        };
        let prims = vec![("+".to_string(), 7usize)];
        let dict = vec![entry.clone()];
        let source = decompile_word(&entry, &dict, &prims);
        assert_eq!(source, ": TEST 42 + ;");
    }

    #[test]
    fn test_msg_snapshot_sexp() {
        let sexp = crate::sexp::parse("(snapshot :id \"abc\" :fitness 42 :gen 0)").unwrap();
        assert_eq!(crate::sexp::msg_type(&sexp), Some("snapshot"));
        assert_eq!(sexp.get_key(":fitness").unwrap().as_number(), Some(42));
    }

    #[test]
    fn test_msg_resurrect_sexp() {
        let sexp = crate::sexp::parse(
            "(resurrect :id \"abc\" :fitness 42 :gen 0 :saved-at \"1711900800\")",
        )
        .unwrap();
        assert_eq!(crate::sexp::msg_type(&sexp), Some("resurrect"));
        assert_eq!(
            sexp.get_key(":saved-at").unwrap().as_str(),
            Some("1711900800")
        );
    }
}