fantastic 0.0.6

Aisixteen Fantastic — a kernel of agents: the fantastic CLI with its in-process kernel, browser canvas and MCP face
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
//! A terminal is a PTY JOB (terminal_backend merged in) — twins of python
//! `exec_bridge/tests/test_pty_jobs.py`, plus the record-owned environment
//! (`test_the_kernel_env_reaches_a_child_only_by_name`).

use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;

use crate::fantastic_kernel::{Agent, AgentId, Item, Kernel};
use serde_json::{json, Map, Value};
use tempfile::TempDir;

use crate::fantastic_exec_bridge::sys::ENTER;
use crate::fantastic_exec_bridge::{ExecBridgeBundle, HANDLER_MODULE};

/// A tiny program that proves it sits on a terminal: its stdin is a tty, TERM is
/// set, the window size is the one asked for, and a typed line reaches it. A
/// resize is announced where the OS signals one (SIGWINCH).
const CHILD: &str = "import os,sys,signal
def size():
    return tuple(os.get_terminal_size(1))
if hasattr(signal, 'SIGWINCH'):
    signal.signal(signal.SIGWINCH, lambda *a: print('WINCH %d %d' % size(), flush=True))
print('TTY', os.isatty(0), os.environ.get('TERM'), flush=True)
print('SIZE %d %d' % size(), flush=True)
line = sys.stdin.readline()
print('GOT', line.strip(), flush=True)
";

fn python() -> Option<PathBuf> {
    which::which("python3").ok()
}

async fn kernel_with(tmp: &TempDir, id: &str, extra: Value) -> Arc<Kernel> {
    let mut kernel = Kernel::new();
    kernel.bundles.register(HANDLER_MODULE, ExecBridgeBundle);
    let kernel = Arc::new(kernel);
    let root = Agent::new(
        AgentId::from("kernel_state"),
        None,
        None,
        Map::new(),
        tmp.path().join(".fantastic"),
        false,
    );
    kernel.register(Arc::clone(&root));
    kernel.set_root(Arc::clone(&root));
    let mut create = json!({
        "type": "create_agent", "parent_id": "kernel", "id": id,
        "handler_module": HANDLER_MODULE, "ingress_rule": "allow_all_ingress",
        "cwd": tmp.path().to_string_lossy(), "allowed": [".*"],
    });
    for (k, v) in extra.as_object().cloned().unwrap_or_default() {
        create[k] = v;
    }
    let r = kernel
        .send(vec![Item::new(AgentId::from("kernel_state"), create)])
        .await
        .remove(0)
        .result;
    assert_eq!(r["id"], json!(id), "{r}");
    kernel
}

async fn call(kernel: &Arc<Kernel>, id: &str, p: Value) -> Value {
    kernel
        .send(vec![Item::new(AgentId::from(id), p)])
        .await
        .remove(0)
        .result
}

/// Collect `output` text until `pred`, returning it.
async fn output_until(
    rx: &mut crate::fantastic_kernel::inbox::InboxReceiver,
    pred: impl Fn(&str) -> bool,
) -> String {
    let mut text = String::new();
    let deadline = tokio::time::Instant::now() + Duration::from_secs(15);
    while !pred(&text) {
        assert!(
            tokio::time::Instant::now() < deadline,
            "timed out; got {text:?}"
        );
        if let Ok(Some(item)) = tokio::time::timeout(Duration::from_millis(100), rx.recv()).await {
            if item.payload["type"] == "output" {
                text.push_str(item.payload["data"].as_str().unwrap_or(""));
            }
        }
    }
    text
}

async fn next_of(rx: &mut crate::fantastic_kernel::inbox::InboxReceiver, ty: &str) -> Value {
    let deadline = tokio::time::Instant::now() + Duration::from_secs(15);
    loop {
        assert!(tokio::time::Instant::now() < deadline, "no {ty}");
        if let Ok(Some(item)) = tokio::time::timeout(Duration::from_millis(100), rx.recv()).await {
            if item.payload["type"] == ty {
                return item.payload;
            }
        }
    }
}

async fn start(kernel: &Arc<Kernel>, id: &str, py: &Path) -> i64 {
    let r = call(
        kernel,
        id,
        json!({"type": "start", "command": py.to_string_lossy(), "args": ["-c", CHILD],
               "pty": {"cols": 91, "rows": 37}}),
    )
    .await;
    assert_eq!(r["pty"], json!(true), "{r}");
    r["job_id"].as_i64().unwrap()
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_pty_job_runs_on_a_terminal_and_takes_keystrokes() {
    let Some(py) = python() else { return };
    let tmp = TempDir::new().unwrap();
    let k = kernel_with(&tmp, "term_a", json!({})).await;
    let mut rx = k.ensure_inbox(&AgentId::from("term_a"));
    let jid = start(&k, "term_a", &py).await;
    let text = output_until(&mut rx, |t| t.contains("SIZE")).await;
    assert!(text.contains("TTY True xterm-256color"), "{text}");
    assert!(text.contains("SIZE 91 37"), "{text}");
    let w = call(
        &k,
        "term_a",
        json!({"type": "write", "job_id": jid, "data": format!("hello{ENTER}")}),
    )
    .await;
    assert_eq!(w, json!({"written": 6}));
    output_until(&mut rx, |t| t.contains("GOT hello")).await;
    let done = next_of(&mut rx, "job_done").await;
    assert_eq!(
        done,
        json!({"type": "job_done", "job_id": jid, "exit_code": 0, "ok": true})
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn resize_reaches_the_program_and_status_reports_it() {
    let Some(py) = python() else { return };
    let tmp = TempDir::new().unwrap();
    let k = kernel_with(&tmp, "term_r", json!({})).await;
    let mut rx = k.ensure_inbox(&AgentId::from("term_r"));
    let jid = start(&k, "term_r", &py).await;
    output_until(&mut rx, |t| t.contains("SIZE")).await;
    let r = call(
        &k,
        "term_r",
        json!({"type": "resize", "job_id": jid, "cols": 120, "rows": 40}),
    )
    .await;
    assert_eq!(r, json!({"resized": true, "cols": 120, "rows": 40}));
    // SIGWINCH is unix's: a Windows console program is not signalled a resize.
    #[cfg(unix)]
    output_until(&mut rx, |t| t.contains("WINCH 120 40")).await;
    let snap = call(&k, "term_r", json!({"type": "status", "job_id": jid})).await;
    let s = &snap["jobs"][0];
    assert_eq!(
        (&s["pty"], &s["cols"], &s["rows"]),
        (&json!(true), &json!(120), &json!(40)),
        "{snap}"
    );
    assert!(
        s.get("stdout").is_none(),
        "a PTY job has no stdout/stderr split: {snap}"
    );
    call(
        &k,
        "term_r",
        json!({"type": "write", "job_id": jid, "data": format!("x{ENTER}")}),
    )
    .await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn output_replays_the_ring_for_a_client_that_re_attaches() {
    let Some(py) = python() else { return };
    let tmp = TempDir::new().unwrap();
    let k = kernel_with(&tmp, "term_o", json!({})).await;
    let mut rx = k.ensure_inbox(&AgentId::from("term_o"));
    let jid = start(&k, "term_o", &py).await;
    output_until(&mut rx, |t| t.contains("SIZE")).await;
    // the ring once the guest's last line is all in (it then waits on input):
    // the line's end can land a moment after its text
    let mut ring = String::new();
    loop {
        let now = call(&k, "term_o", json!({"type": "output", "job_id": jid})).await;
        let now = now["output"].as_str().unwrap().to_string();
        if now == ring {
            break;
        }
        ring = now;
        tokio::time::sleep(Duration::from_millis(200)).await;
    }
    assert!(
        ring.contains("TTY True") && ring.contains("SIZE 91 37"),
        "{ring}"
    );
    let tail = call(
        &k,
        "term_o",
        json!({"type": "output", "job_id": jid, "max_bytes": 8}),
    )
    .await;
    let tail = tail["output"].as_str().unwrap();
    assert!(tail.len() <= 8 && ring.ends_with(tail), "{tail:?}");
    call(
        &k,
        "term_o",
        json!({"type": "write", "job_id": jid, "data": format!("x{ENTER}")}),
    )
    .await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_pty_job_is_gated_like_any_start_and_its_verbs_want_a_pty_job() {
    let Some(py) = python() else { return };
    let tmp = TempDir::new().unwrap();
    let k = kernel_with(
        &tmp,
        "term_g",
        // the shell-joined line, the interpreter's path regex-escaped (a Windows
        // path's `\` is a regex escape)
        json!({"allowed": [format!(
            "{} -c .*",
            fancy_regex::escape(&crate::fantastic_exec_bridge::shlex_join(&[py.to_string_lossy().to_string()]))
        )]}),
    )
    .await;
    let r = call(
        &k,
        "term_g",
        json!({"type": "start", "command": "/bin/sh", "args": ["-i"], "pty": {"cols": 80, "rows": 24}}),
    )
    .await;
    assert_eq!(r["reason"], "unauthorized", "{r}");
    let r = call(
        &k,
        "term_g",
        json!({"type": "start", "command": py.to_string_lossy(), "args": ["-c", "0"], "pty": {"cols": 0}}),
    )
    .await;
    assert_eq!(r["reason"], "bad_arg", "{r}");
    let r = call(
        &k,
        "term_g",
        json!({"type": "write", "job_id": 999_999, "data": "x"}),
    )
    .await;
    assert_eq!(r["reason"], "not_found", "{r}");
    let pipe = call(
        &k,
        "term_g",
        json!({"type": "start", "command": py.to_string_lossy(), "args": ["-c", "input()"]}),
    )
    .await;
    let r = call(
        &k,
        "term_g",
        json!({"type": "write", "job_id": pipe["job_id"], "data": "x"}),
    )
    .await;
    assert_eq!(
        (&r["reason"], &r["arg"]),
        (&json!("bad_arg"), &json!("job_id")),
        "{r}"
    );
    call(
        &k,
        "term_g",
        json!({"type": "stop", "job_id": pipe["job_id"]}),
    )
    .await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn paste_image_types_a_saved_path() {
    let Some(py) = python() else { return };
    let tmp = TempDir::new().unwrap();
    let k = kernel_with(&tmp, "term_p", json!({})).await;
    let mut rx = k.ensure_inbox(&AgentId::from("term_p"));
    let jid = start(&k, "term_p", &py).await;
    output_until(&mut rx, |t| t.contains("SIZE")).await;
    // The mime is the caller's to state — never guessed.
    let item = Item::new(
        AgentId::from("term_p"),
        json!({"type": "paste_image", "job_id": jid}),
    )
    .with_binary_key("data", b"\x89PNG".to_vec());
    let r = k.send(vec![item]).await.remove(0).result;
    assert_eq!(
        (&r["reason"], &r["arg"]),
        (&json!("missing_arg"), &json!("mime")),
        "{r}"
    );
    let item = Item::new(
        AgentId::from("term_p"),
        json!({"type": "paste_image", "job_id": jid, "mime": "image/png"}),
    )
    .with_binary_key("data", b"\x89PNG....".to_vec());
    let r = k.send(vec![item]).await.remove(0).result;
    assert_eq!(r["bytes"], json!(8), "{r}");
    let path = r["path"].as_str().unwrap().to_string();
    assert!(path.ends_with(".png"), "{r}");
    call(
        &k,
        "term_p",
        json!({"type": "write", "job_id": jid, "data": ENTER}),
    )
    .await;
    let text = output_until(&mut rx, |t| t.contains("GOT") && t.contains(".png")).await;
    assert!(text.contains(&path), "{text}");
}

/// The kernel's environment reaches a child only by NAME (`inherit_env`); `["*"]`
/// is the operator's wildcard; nothing is inherited by default.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn the_kernel_env_reaches_a_child_only_by_name() {
    std::env::set_var("FT_SECRET_RS", "s3cret");
    std::env::set_var("FT_OPEN_RS", "open");
    let (command, args) = crate::fantastic_exec_bridge::sys::print_env_job();
    for (id, inherit, want) in [
        ("env_bare", Value::Null, "none/none"),
        ("env_named", json!(["FT_OPEN_RS"]), "none/open"),
        ("env_all", json!(["*"]), "s3cret/open"),
    ] {
        let tmp = TempDir::new().unwrap();
        let k = kernel_with(&tmp, id, json!({"inherit_env": inherit})).await;
        let mut rx = k.ensure_inbox(&AgentId::from(id));
        call(
            &k,
            id,
            json!({"type": "start", "command": command, "args": args}),
        )
        .await;
        let done = next_of(&mut rx, "job_done").await;
        // a console program on Windows ends its line with `\r\n`
        let printed = crate::fantastic_exec_bridge::jobs::printed(&AgentId::from(id), &done, "stdout");
        assert_eq!(
            json!(printed.replace("\r\n", "\n")),
            json!(format!("{want}\n")),
            "{id}: {done}"
        );
    }
}

/// A leg with no `cwd` runs nothing — where a command runs is never guessed.
#[tokio::test]
async fn a_leg_with_no_cwd_runs_nothing() {
    let tmp = TempDir::new().unwrap();
    let k = kernel_with(&tmp, "nocwd", json!({"cwd": null})).await;
    let r = call(
        &k,
        "nocwd",
        json!({"type": "start", "command": "/bin/sh", "args": ["-c", "true"]}),
    )
    .await;
    assert_eq!(
        (&r["reason"], &r["arg"]),
        (&json!("missing_arg"), &json!("cwd")),
        "{r}"
    );
}

/// The processes in the job `leader` leads, other than the leader: its session
/// on unix, its Job Object on Windows.
fn in_session(leader: u32) -> Vec<u32> {
    crate::fantastic_exec_bridge::sys::job_members(leader)
}

fn exists(pid: u32) -> bool {
    crate::fantastic_kernel::lock::pid_alive(pid)
}

/// A shell puts each job it starts in its OWN process group inside the PTY
/// session, so killing the leader alone (and the tty's hangup, which reaches the
/// foreground group only) would leave `sleep &` and a `nohup` running,
/// reparented to init. `stop` and `delete_agent` (the kernel's shutdown runs the
/// same teardown) kill the whole session — on Windows, the job's Job Object. Twin
/// of python `test_every_teardown_kills_the_whole_session`.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn every_teardown_kills_the_whole_session() {
    let (command, args, env, typed) = crate::fantastic_exec_bridge::sys::interactive_shell();
    for (aid, end) in [("sh_stop", "stop"), ("sh_delete", "delete_agent")] {
        let tmp = TempDir::new().unwrap();
        let k = kernel_with(&tmp, aid, json!({"env": env})).await;
        let r = call(
            &k,
            aid,
            json!({"type": "start", "command": command, "args": args,
                   "pty": {"cols": 80, "rows": 24}}),
        )
        .await;
        let jid = r["job_id"].as_i64().expect("job_id");
        let leader = crate::fantastic_exec_bridge::jobs::jobs()
            .get(&(AgentId::from(aid), crate::fantastic_kernel::TaskId(jid)))
            .expect("job row")
            .pid;
        call(
            &k,
            aid,
            json!({"type": "write", "job_id": jid, "data": typed}),
        )
        .await;
        let mut members = Vec::new();
        for _ in 0..100 {
            members = in_session(leader);
            if members.len() >= 3 {
                break;
            }
            tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        }
        assert!(members.len() >= 3, "{end}: {members:?}");
        if end == "stop" {
            call(&k, aid, json!({"type": "stop", "job_id": jid})).await;
        } else {
            call(
                &k,
                "kernel_state",
                json!({"type": "delete_agent", "id": aid}),
            )
            .await;
        }
        let mut alive: Vec<u32> = std::iter::once(leader).chain(members).collect();
        for _ in 0..100 {
            alive.retain(|p| exists(*p));
            if alive.is_empty() {
                break;
            }
            tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        }
        assert!(alive.is_empty(), "{end}: survivors {alive:?}");
    }
}

/// A plain (pipe) job leads its own session too, so `stop` and `delete_agent`
/// take what it launched — a script's `sleep &` and a `nohup` included.
/// Twin of python `test_stop_and_delete_kill_everything_a_job_launched`.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn stop_and_delete_kill_everything_a_job_launched() {
    let (command, args, env) = crate::fantastic_exec_bridge::sys::launcher_job();
    for (aid, end) in [("pipe_stop", "stop"), ("pipe_delete", "delete_agent")] {
        let tmp = TempDir::new().unwrap();
        let k = kernel_with(&tmp, aid, json!({"env": env})).await;
        let r = call(
            &k,
            aid,
            json!({"type": "start", "command": command, "args": args}),
        )
        .await;
        let jid = r["job_id"].as_i64().expect("job_id");
        let leader = crate::fantastic_exec_bridge::jobs::jobs()
            .get(&(AgentId::from(aid), crate::fantastic_kernel::TaskId(jid)))
            .expect("job row")
            .pid;
        let mut members = Vec::new();
        for _ in 0..100 {
            members = in_session(leader);
            if members.len() >= 3 {
                break;
            }
            tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        }
        assert!(members.len() >= 3, "{end}: {members:?}");
        if end == "stop" {
            call(&k, aid, json!({"type": "stop", "job_id": jid})).await;
        } else {
            call(
                &k,
                "kernel_state",
                json!({"type": "delete_agent", "id": aid}),
            )
            .await;
        }
        let mut alive: Vec<u32> = std::iter::once(leader).chain(members).collect();
        for _ in 0..100 {
            alive.retain(|p| exists(*p));
            if alive.is_empty() {
                break;
            }
            tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        }
        assert!(alive.is_empty(), "{end}: survivors {alive:?}");
    }
}

/// A `<call>` strips a scalar like `data`, so a key ending in Enter rides
/// `bytes`, written verbatim; exactly one of the two.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn write_bytes_is_verbatim_so_enter_reaches_the_program() {
    let Some(py) = python() else { return };
    let tmp = TempDir::new().unwrap();
    let k = kernel_with(&tmp, "term_b", json!({})).await;
    let mut rx = k.ensure_inbox(&AgentId::from("term_b"));
    let jid = start(&k, "term_b", &py).await;
    output_until(&mut rx, |t| t.contains("SIZE")).await;
    let both = call(
        &k,
        "term_b",
        json!({"type": "write", "job_id": jid, "data": "a", "bytes": "b"}),
    )
    .await;
    assert_eq!(both["arg"], json!("data"), "{both}");
    let w = call(
        &k,
        "term_b",
        json!({"type": "write", "job_id": jid, "bytes": format!("hello{ENTER}")}),
    )
    .await;
    assert_eq!(w, json!({"written": 6}));
    output_until(&mut rx, |t| t.contains("GOT hello")).await;
}

/// Floods past the high-water mark, then prints a marker and waits on a line.
const FLOOD: &str = "import sys
sys.stdout.write('x' * 150000 + '\\nDONE\\n')
sys.stdout.flush()
sys.stdin.readline()
";

/// A viewer that streamed but never acked (a reloaded page) leaves `unacked` past
/// HIGH and the reader paused. The new viewer's `output` replay hands it everything
/// so far, so it zeroes the count and resumes the reader — python
/// `test_a_replay_clears_the_backlog_a_gone_viewer_left`.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_replay_clears_the_backlog_a_gone_viewer_left() {
    let Some(py) = python() else { return };
    let tmp = TempDir::new().unwrap();
    let k = kernel_with(&tmp, "term_f", json!({})).await;
    let mut rx = k.ensure_inbox(&AgentId::from("term_f"));
    let r = call(
        &k,
        "term_f",
        json!({"type": "start", "command": py.to_string_lossy(), "args": ["-c", FLOOD],
               "pty": {"cols": 80, "rows": 24}}),
    )
    .await;
    let jid = r["job_id"].as_i64().unwrap();
    let probe = json!({"type": "ack", "job_id": jid, "chars": 0});
    let mut st = json!({});
    for _ in 0..250 {
        st = call(&k, "term_f", probe.clone()).await;
        if st["paused"] == json!(true) {
            break;
        }
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
    assert!(
        st["paused"] == json!(true)
            && st["unacked"].as_u64().unwrap() > crate::fantastic_exec_bridge::pty::HIGH_WATERMARK as u64,
        "{st}"
    );
    call(&k, "term_f", json!({"type": "output", "job_id": jid})).await;
    let st = call(&k, "term_f", probe).await;
    // the count restarted at 0; the resumed reader may already have counted more
    assert!(
        st["paused"] == json!(false)
            && st["unacked"].as_u64().unwrap() < crate::fantastic_exec_bridge::pty::HIGH_WATERMARK as u64,
        "{st}"
    );
    // the rest of the flood (under HIGH) arrives with no further ack
    output_until(&mut rx, |t| t.contains("DONE")).await;
    call(
        &k,
        "term_f",
        json!({"type": "write", "job_id": jid, "data": ENTER}),
    )
    .await;
}