unit 0.28.0

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
//! HTTP bridge — exposes the VM and mesh over localhost.
//!
//! A minimal, hand-rolled HTTP/1.1 server. Zero dependencies. Binds to
//! 127.0.0.1 only. One std::thread per connection; `Connection: close`
//! after every response. Requests over 64 KiB are rejected with 413.
//! Socket reads time out after 5 seconds.
//!
//! The bridge is thin: endpoints translate to existing VM and mesh APIs,
//! they do not add behavior. Keep it that way.

use std::io::{Read, Write};
use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpListener, TcpStream};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

use crate::mesh;
use crate::sexp;
use crate::snapshot;
use crate::vm::VM;

pub const DEFAULT_PORT: u16 = 9898;

const MAX_REQUEST_BYTES: usize = 64 * 1024;
const READ_TIMEOUT: Duration = Duration::from_secs(5);
const WRITE_TIMEOUT: Duration = Duration::from_secs(5);

/// Start serving HTTP requests on 127.0.0.1:port. Blocks forever.
///
/// Prints the bound address (resolves port 0) and then accepts forever.
/// Each connection is handled on its own thread; the VM is shared via
/// Arc<Mutex<VM>>. There is no keep-alive — connections close after one
/// request-response cycle.
pub fn serve(vm: VM, port: u16) -> ! {
    let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port);
    let listener = match TcpListener::bind(addr) {
        Ok(l) => l,
        Err(e) => {
            eprintln!("http: bind {}: {}", addr, e);
            std::process::exit(1);
        }
    };
    let bound = listener.local_addr().unwrap_or(addr);
    eprintln!("http: listening on http://{}", bound);

    let vm = Arc::new(Mutex::new(vm));

    for incoming in listener.incoming() {
        match incoming {
            Ok(stream) => {
                let vm = Arc::clone(&vm);
                thread::spawn(move || {
                    let _ = handle_connection(stream, vm);
                });
            }
            Err(e) => {
                eprintln!("http: accept: {}", e);
            }
        }
    }
    std::process::exit(0);
}

// ---------------------------------------------------------------------------
// Request parsing
// ---------------------------------------------------------------------------

struct Request {
    method: String,
    path: String,
    body: String,
}

struct HttpError {
    status: u16,
    msg: String,
}

fn handle_connection(mut stream: TcpStream, vm: Arc<Mutex<VM>>) -> std::io::Result<()> {
    let _ = stream.set_read_timeout(Some(READ_TIMEOUT));
    let _ = stream.set_write_timeout(Some(WRITE_TIMEOUT));

    let (status, body) = match read_request(&mut stream) {
        Ok(req) => route(&req, vm),
        Err(e) => (e.status, error_json(&e.msg)),
    };
    write_response(&mut stream, status, "application/json", &body)
}

fn read_request(stream: &mut TcpStream) -> Result<Request, HttpError> {
    let mut buf: Vec<u8> = Vec::with_capacity(1024);
    let mut tmp = [0u8; 1024];
    let header_end = loop {
        let n = stream.read(&mut tmp).map_err(|e| HttpError {
            status: 400,
            msg: format!("read: {}", e),
        })?;
        if n == 0 {
            return Err(HttpError {
                status: 400,
                msg: "empty request".into(),
            });
        }
        buf.extend_from_slice(&tmp[..n]);
        if buf.len() > MAX_REQUEST_BYTES {
            return Err(HttpError {
                status: 413,
                msg: "request too large".into(),
            });
        }
        if let Some(pos) = find_header_end(&buf) {
            break pos;
        }
    };

    let header_str = std::str::from_utf8(&buf[..header_end]).map_err(|_| HttpError {
        status: 400,
        msg: "non-utf8 headers".into(),
    })?;
    let mut lines = header_str.split("\r\n");
    let start_line = lines.next().ok_or_else(|| HttpError {
        status: 400,
        msg: "missing start line".into(),
    })?;
    let mut parts = start_line.split_whitespace();
    let method = parts
        .next()
        .ok_or_else(|| HttpError {
            status: 400,
            msg: "missing method".into(),
        })?
        .to_string();
    let raw_path = parts.next().ok_or_else(|| HttpError {
        status: 400,
        msg: "missing path".into(),
    })?;
    let path = raw_path.split('?').next().unwrap_or("/").to_string();

    let mut content_length = 0usize;
    for line in lines {
        if line.is_empty() {
            continue;
        }
        if let Some((name, value)) = line.split_once(':') {
            if name.trim().eq_ignore_ascii_case("content-length") {
                content_length = value.trim().parse().map_err(|_| HttpError {
                    status: 400,
                    msg: "bad content-length".into(),
                })?;
            }
        }
    }

    if content_length > MAX_REQUEST_BYTES {
        return Err(HttpError {
            status: 413,
            msg: "body too large".into(),
        });
    }

    let body_start = header_end + 4;
    let mut body_bytes: Vec<u8> = if body_start < buf.len() {
        buf[body_start..].to_vec()
    } else {
        Vec::new()
    };
    while body_bytes.len() < content_length {
        let need = content_length - body_bytes.len();
        let cap = tmp.len();
        let n = stream
            .read(&mut tmp[..need.min(cap)])
            .map_err(|e| HttpError {
                status: 400,
                msg: format!("read body: {}", e),
            })?;
        if n == 0 {
            return Err(HttpError {
                status: 400,
                msg: "short body".into(),
            });
        }
        body_bytes.extend_from_slice(&tmp[..n]);
        if body_bytes.len() + body_start > MAX_REQUEST_BYTES {
            return Err(HttpError {
                status: 413,
                msg: "body too large".into(),
            });
        }
    }
    body_bytes.truncate(content_length);
    let body = String::from_utf8(body_bytes).map_err(|_| HttpError {
        status: 400,
        msg: "non-utf8 body".into(),
    })?;

    Ok(Request {
        method,
        path,
        body,
    })
}

fn find_header_end(buf: &[u8]) -> Option<usize> {
    if buf.len() < 4 {
        return None;
    }
    (0..=buf.len() - 4).find(|&i| &buf[i..i + 4] == b"\r\n\r\n")
}

fn write_response(
    stream: &mut TcpStream,
    status: u16,
    content_type: &str,
    body: &str,
) -> std::io::Result<()> {
    let reason = match status {
        200 => "OK",
        400 => "Bad Request",
        404 => "Not Found",
        405 => "Method Not Allowed",
        413 => "Payload Too Large",
        500 => "Internal Server Error",
        503 => "Service Unavailable",
        _ => "OK",
    };
    let head = format!(
        "HTTP/1.1 {} {}\r\nContent-Type: {}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
        status,
        reason,
        content_type,
        body.len()
    );
    stream.write_all(head.as_bytes())?;
    stream.write_all(body.as_bytes())?;
    stream.flush()?;
    Ok(())
}

// ---------------------------------------------------------------------------
// Routing
// ---------------------------------------------------------------------------

fn route(req: &Request, vm: Arc<Mutex<VM>>) -> (u16, String) {
    let path = req.path.as_str();
    match (req.method.as_str(), path) {
        ("POST", "/eval") => endpoint_eval(&req.body, vm),
        ("POST", "/sexp") => endpoint_sexp(&req.body, vm),
        ("GET", "/status") => endpoint_status(vm),
        ("GET", "/words") => endpoint_words(vm),
        ("GET", "/mesh/peers") => endpoint_mesh_peers(vm),
        ("POST", "/mesh/broadcast") => endpoint_mesh_broadcast(&req.body, vm),
        ("GET", p) if p.starts_with("/word/") => endpoint_word(&p[6..], vm),
        (_, p) if is_known_path(p) => (405, error_json("method not allowed")),
        _ => (404, error_json("not found")),
    }
}

fn is_known_path(p: &str) -> bool {
    matches!(
        p,
        "/eval" | "/sexp" | "/status" | "/words" | "/mesh/peers" | "/mesh/broadcast"
    ) || p.starts_with("/word/")
}

// ---------------------------------------------------------------------------
// Endpoints — translation, not logic
// ---------------------------------------------------------------------------

fn endpoint_eval(body: &str, vm: Arc<Mutex<VM>>) -> (u16, String) {
    let source = match json_get_string(body, "source") {
        Some(s) => s,
        None => return (400, error_json("missing or invalid \"source\" field")),
    };
    let mut vm = vm.lock().unwrap();
    let output = vm.eval(&source);
    (200, json_object_str("output", &output))
}

fn endpoint_sexp(body: &str, vm: Arc<Mutex<VM>>) -> (u16, String) {
    let expr = match json_get_string(body, "expr") {
        Some(s) => s,
        None => return (400, error_json("missing or invalid \"expr\" field")),
    };
    let parsed = match sexp::parse(&expr) {
        Ok(p) => p,
        Err(e) => return (400, error_json(&format!("sexp parse: {}", e.0))),
    };
    let forth = sexp::to_forth(&parsed);
    let mut vm = vm.lock().unwrap();
    let before = vm.stack.len();
    let output = vm.eval(&forth);
    // If the expression produced printed output, that IS the result.
    // Otherwise fall back to peeking the top of the stack — S-expressions
    // like `(* 6 7)` leave their value there. Don't pop: matches SEXP".
    let trimmed = output.trim_end();
    let result = if !trimmed.is_empty() {
        trimmed.to_string()
    } else if vm.stack.len() > before {
        vm.stack
            .last()
            .map(|c| c.to_string())
            .unwrap_or_default()
    } else {
        String::new()
    };
    (200, json_object_str("result", &result))
}

fn endpoint_status(vm: Arc<Mutex<VM>>) -> (u16, String) {
    let vm = vm.lock().unwrap();
    let id = vm
        .node_id_cache
        .map(|b| mesh::id_to_hex(&b))
        .unwrap_or_default();
    let peers = vm.mesh.as_ref().map(|m| m.peer_count()).unwrap_or(0);
    let mut out = String::with_capacity(256);
    out.push('{');
    out.push_str(&format!(
        "\"id\":\"{}\",",
        snapshot::escape_json_string(&id)
    ));
    out.push_str(&format!("\"fitness\":{},", vm.fitness.score));
    out.push_str(&format!("\"energy\":{},", vm.energy.energy));
    out.push_str(&format!("\"peers\":{},", peers));
    out.push_str(&format!("\"words\":{},", vm.dictionary.len()));
    out.push_str(&format!("\"generation\":{}", vm.spawn_state.generation));
    out.push('}');
    (200, out)
}

fn endpoint_words(vm: Arc<Mutex<VM>>) -> (u16, String) {
    let vm = vm.lock().unwrap();
    let mut out = String::with_capacity(1024);
    out.push_str("{\"words\":[");
    for (i, entry) in vm.dictionary.iter().enumerate() {
        if i > 0 {
            out.push(',');
        }
        out.push('"');
        out.push_str(&snapshot::escape_json_string(&entry.name));
        out.push('"');
    }
    out.push_str("]}");
    (200, out)
}

fn endpoint_word(name_part: &str, vm: Arc<Mutex<VM>>) -> (u16, String) {
    let name = match percent_decode(name_part) {
        Some(n) => n,
        None => return (400, error_json("bad percent-encoding")),
    };
    let vm = vm.lock().unwrap();
    let entry = match vm.dictionary.iter().find(|e| e.name == name) {
        Some(e) => e,
        None => return (404, error_json("word not found")),
    };
    let def = snapshot::decompile_word(entry, &vm.dictionary, &vm.primitive_names);
    let mut out = String::with_capacity(name.len() + def.len() + 64);
    out.push('{');
    out.push_str(&format!(
        "\"name\":\"{}\",",
        snapshot::escape_json_string(&name)
    ));
    out.push_str(&format!(
        "\"definition\":\"{}\"",
        snapshot::escape_json_string(&def)
    ));
    out.push('}');
    (200, out)
}

fn endpoint_mesh_peers(vm: Arc<Mutex<VM>>) -> (u16, String) {
    let vm = vm.lock().unwrap();
    let mut out = String::with_capacity(256);
    out.push_str("{\"peers\":[");
    if let Some(ref m) = vm.mesh {
        for (i, (id, fitness, addr)) in m.peer_details().into_iter().enumerate() {
            if i > 0 {
                out.push(',');
            }
            out.push_str(&format!(
                "{{\"id\":\"{}\",\"addr\":\"{}\",\"fitness\":{}}}",
                snapshot::escape_json_string(&id),
                snapshot::escape_json_string(&addr),
                fitness
            ));
        }
    }
    out.push_str("]}");
    (200, out)
}

fn endpoint_mesh_broadcast(body: &str, vm: Arc<Mutex<VM>>) -> (u16, String) {
    let expr = match json_get_string(body, "expr") {
        Some(s) => s,
        None => return (400, error_json("missing or invalid \"expr\" field")),
    };
    let vm = vm.lock().unwrap();
    match vm.mesh.as_ref() {
        Some(m) => {
            m.send_sexp(&expr);
            (200, "{\"broadcast\":true}".to_string())
        }
        None => (503, error_json("mesh is not running")),
    }
}

// ---------------------------------------------------------------------------
// Minimal JSON helpers (hand-rolled — matches the aesthetic of snapshot.rs)
// ---------------------------------------------------------------------------

fn error_json(msg: &str) -> String {
    format!("{{\"error\":\"{}\"}}", snapshot::escape_json_string(msg))
}

fn json_object_str(key: &str, value: &str) -> String {
    format!(
        "{{\"{}\":\"{}\"}}",
        snapshot::escape_json_string(key),
        snapshot::escape_json_string(value)
    )
}

/// Extract a string-valued field from a flat JSON object. Handles
/// whitespace, common escape sequences, and ignores other fields.
/// Good enough for the one-or-two-key bodies this bridge accepts;
/// not a full JSON parser.
fn json_get_string(body: &str, key: &str) -> Option<String> {
    let needle = format!("\"{}\"", key);
    let key_pos = body.find(&needle)?;
    let after_key = &body[key_pos + needle.len()..];
    let colon_pos = after_key.find(':')?;
    let after_colon = &after_key[colon_pos + 1..];
    let mut chars = after_colon.chars();
    let mut c = chars.next()?;
    while c.is_whitespace() {
        c = chars.next()?;
    }
    if c != '"' {
        return None;
    }
    let mut out = String::new();
    while let Some(c) = chars.next() {
        match c {
            '"' => return Some(out),
            '\\' => match chars.next()? {
                '"' => out.push('"'),
                '\\' => out.push('\\'),
                '/' => out.push('/'),
                'n' => out.push('\n'),
                'r' => out.push('\r'),
                't' => out.push('\t'),
                other => {
                    out.push('\\');
                    out.push(other);
                }
            },
            other => out.push(other),
        }
    }
    None
}

/// Decode a percent-encoded URL path segment. ASCII-only; high bytes
/// pass through as latin-1 chars. Forth word names are ASCII.
fn percent_decode(s: &str) -> Option<String> {
    let bytes = s.as_bytes();
    let mut out = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        let b = bytes[i];
        if b == b'%' {
            if i + 2 >= bytes.len() {
                return None;
            }
            let hex = std::str::from_utf8(&bytes[i + 1..i + 3]).ok()?;
            let v = u8::from_str_radix(hex, 16).ok()?;
            out.push(v);
            i += 3;
        } else if b == b'+' {
            out.push(b' ');
            i += 1;
        } else {
            out.push(b);
            i += 1;
        }
    }
    String::from_utf8(out).ok()
}

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

    #[test]
    fn parse_simple_string_field() {
        let body = r#"{"source": "2 3 + ."}"#;
        assert_eq!(json_get_string(body, "source"), Some("2 3 + .".into()));
    }

    #[test]
    fn parse_escapes() {
        let body = r#"{"s":"a\nb\"c"}"#;
        assert_eq!(json_get_string(body, "s"), Some("a\nb\"c".into()));
    }

    #[test]
    fn missing_field_returns_none() {
        let body = r#"{"other":"x"}"#;
        assert_eq!(json_get_string(body, "source"), None);
    }

    #[test]
    fn decode_percent() {
        assert_eq!(percent_decode("HELLO").as_deref(), Some("HELLO"));
        assert_eq!(percent_decode("a%20b").as_deref(), Some("a b"));
        assert_eq!(percent_decode("a+b").as_deref(), Some("a b"));
        assert_eq!(percent_decode("%GG"), None);
    }

    #[test]
    fn find_headers_delimiter() {
        let data = b"GET / HTTP/1.1\r\nHost: x\r\n\r\nbody";
        let pos = find_header_end(data).unwrap();
        assert_eq!(&data[..pos], b"GET / HTTP/1.1\r\nHost: x");
        assert_eq!(&data[pos + 4..], b"body");
    }
}