ilo 26.5.0

ilo - the token-minimal programming language AI agents write
Documentation
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
/// Integration tests for CLI capability flags (`--allow-net`, `--allow-read`,
/// `--allow-write`, `--allow-run`).
///
/// Each test runs programs through `interpreter::run_with_caps` and
/// `vm::run_with_caps` to verify enforcement at both backends, then also
/// exercises the `Caps` unit helpers directly for clarity.
use ilo::caps::{Caps, Policy};
use ilo::interpreter::{self, Value};
use ilo::{lexer, parser, vm};
use std::sync::Arc;

// ── helpers ───────────────────────────────────────────────────────────────────

fn make_program(src: &str) -> ilo::ast::Program {
    let tokens = lexer::lex(src).unwrap();
    let token_spans: Vec<(ilo::lexer::Token, ilo::ast::Span)> = tokens
        .into_iter()
        .map(|(t, r)| {
            (
                t,
                ilo::ast::Span {
                    start: r.start,
                    end: r.end,
                },
            )
        })
        .collect();
    let (mut program, _) = parser::parse(token_spans);
    ilo::ast::resolve_aliases(&mut program);
    ilo::ast::desugar_dot_var_index(&mut program);
    program
}

/// Run a 0-arg function through interpreter + VM with given caps.
/// Returns the result value for both backends.
fn run_both(src: &str, caps: Caps) -> (Value, Value) {
    let caps = Arc::new(caps);
    let program = make_program(src);
    let tree_result =
        interpreter::run_with_caps(&program, None, vec![], Arc::clone(&caps)).unwrap();
    let compiled = vm::compile(&program).unwrap();
    let vm_result = vm::run_with_caps(&compiled, None, vec![], caps).unwrap();
    (tree_result, vm_result)
}

fn is_err_value(v: &Value) -> bool {
    matches!(v, Value::Err(_))
}

fn err_text(v: &Value) -> String {
    match v {
        Value::Err(inner) => match inner.as_ref() {
            Value::Text(s) => s.to_string(),
            other => format!("{other:?}"),
        },
        other => panic!("expected Err value, got {other:?}"),
    }
}

// ── default permissive mode ───────────────────────────────────────────────────

#[test]
fn default_caps_does_not_block_file_read() {
    // Write a temp file and read it — should succeed with no caps flags.
    let path = "/tmp/ilo_cap_test_read.txt";
    std::fs::write(path, "hello caps").unwrap();
    let src = format!("f>R t t;rd \"{path}\"");
    let (tree, vm) = run_both(&src, Caps::default());
    // With permissive caps, rd returns Ok(text) — not Err.
    assert!(
        !is_err_value(&tree),
        "tree: unexpected Err with default caps"
    );
    assert!(!is_err_value(&vm), "vm: unexpected Err with default caps");
}

// ── --allow-net: empty list blocks all network ────────────────────────────────

/// Core test from the task brief: `--allow-net=` (empty list) + `get` → Err.
/// Network call is blocked before any HTTP request is issued.
#[test]
fn allow_net_empty_blocks_get() {
    let caps = Caps::Restricted {
        net: Policy::List(vec![]),
        read: Policy::All,
        write: Policy::All,
        run: Policy::All,
        env: Policy::All,
    };
    let src = "f>R t t;get \"https://example.com\"";
    let (tree, vm_val) = run_both(src, caps);
    assert!(is_err_value(&tree), "tree: expected Err, got {tree:?}");
    assert!(is_err_value(&vm_val), "vm: expected Err, got {vm_val:?}");
    let msg = err_text(&tree);
    assert!(
        msg.contains("ILO-CAP-001"),
        "err should include ILO-CAP-001 code, got: {msg}"
    );
    assert!(
        msg.contains("--allow-net"),
        "err should mention --allow-net, got: {msg}"
    );
    assert!(
        msg.contains("example.com"),
        "err should mention host, got: {msg}"
    );
}

#[test]
fn allow_net_empty_blocks_get_vm_message() {
    let caps = Caps::Restricted {
        net: Policy::List(vec![]),
        read: Policy::All,
        write: Policy::All,
        run: Policy::All,
        env: Policy::All,
    };
    let src = "f>R t t;get \"https://example.com\"";
    let program = make_program(src);
    let compiled = vm::compile(&program).unwrap();
    let result = vm::run_with_caps(&compiled, None, vec![], Arc::new(caps)).unwrap();
    let msg = err_text(&result);
    assert!(
        msg.contains("--allow-net"),
        "vm err should mention --allow-net; got: {msg}"
    );
}

// ── --allow-net: specific host allowlist ─────────────────────────────────────

#[test]
fn allow_net_blocks_non_allowlisted_host() {
    let caps = Caps::Restricted {
        net: Policy::List(vec!["allowed.example".to_owned()]),
        read: Policy::All,
        write: Policy::All,
        run: Policy::All,
        env: Policy::All,
    };
    let src = "f>R t t;get \"https://evil.example\"";
    let (tree, vm_val) = run_both(src, caps);
    assert!(
        is_err_value(&tree),
        "tree: expected Err for non-allowlisted host"
    );
    assert!(
        is_err_value(&vm_val),
        "vm: expected Err for non-allowlisted host"
    );
}

// ── --allow-read: prefix enforcement ─────────────────────────────────────────

/// Core test from the task brief: `--allow-read=/tmp` + `rd "/etc/passwd"` → Err.
#[test]
fn allow_read_blocks_outside_prefix() {
    let caps = Caps::Restricted {
        net: Policy::All,
        read: Policy::List(vec!["/tmp".to_owned()]),
        write: Policy::All,
        run: Policy::All,
        env: Policy::All,
    };
    let src = "f>R t t;rd \"/etc/passwd\"";
    let (tree, vm_val) = run_both(src, caps);
    assert!(
        is_err_value(&tree),
        "tree: expected Err for /etc/passwd when read limited to /tmp"
    );
    assert!(
        is_err_value(&vm_val),
        "vm: expected Err for /etc/passwd when read limited to /tmp"
    );
    let msg = err_text(&tree);
    assert!(
        msg.contains("ILO-CAP-001"),
        "err should include ILO-CAP-001 code, got: {msg}"
    );
    assert!(
        msg.contains("--allow-read"),
        "err should mention --allow-read, got: {msg}"
    );
}

#[test]
fn allow_read_permits_inside_prefix() {
    let path = "/tmp/ilo_cap_test_read2.txt";
    std::fs::write(path, "ok").unwrap();
    let caps = Caps::Restricted {
        net: Policy::All,
        read: Policy::List(vec!["/tmp".to_owned()]),
        write: Policy::All,
        run: Policy::All,
        env: Policy::All,
    };
    let src = format!("f>R t t;rd \"{path}\"");
    let (tree, vm_val) = run_both(&src, caps);
    // /tmp is in the allowlist so rd should succeed (returns Ok, not Err).
    assert!(
        !is_err_value(&tree),
        "tree: /tmp should be permitted, got {tree:?}"
    );
    assert!(
        !is_err_value(&vm_val),
        "vm: /tmp should be permitted, got {vm_val:?}"
    );
}

// ── --allow-write: enforcement ────────────────────────────────────────────────

#[test]
fn allow_write_blocks_outside_prefix() {
    let caps = Caps::Restricted {
        net: Policy::All,
        read: Policy::All,
        write: Policy::List(vec!["/tmp/ilo_allowed".to_owned()]),
        run: Policy::All,
        env: Policy::All,
    };
    let src = "f>R t t;wr \"/etc/evil.txt\" \"data\"";
    let (tree, vm_val) = run_both(src, caps);
    assert!(
        is_err_value(&tree),
        "tree: expected Err for write outside prefix"
    );
    assert!(
        is_err_value(&vm_val),
        "vm: expected Err for write outside prefix"
    );
    let msg = err_text(&tree);
    assert!(
        msg.contains("ILO-CAP-001"),
        "err should include ILO-CAP-001 code, got: {msg}"
    );
    assert!(
        msg.contains("--allow-write"),
        "err should mention --allow-write, got: {msg}"
    );
}

// ── --allow-run: enforcement ──────────────────────────────────────────────────

#[test]
fn allow_run_empty_blocks_run() {
    let caps = Caps::Restricted {
        net: Policy::All,
        read: Policy::All,
        write: Policy::All,
        run: Policy::List(vec![]),
        env: Policy::All,
    };
    let src = "f>R (M t t) t;run \"echo\" [\"hello\"]";
    // Only test via tree interpreter (run goes through tree-bridge in VM).
    let program = make_program(src);
    let result = interpreter::run_with_caps(&program, None, vec![], Arc::new(caps)).unwrap();
    assert!(
        is_err_value(&result),
        "expected Err when run allowlist is empty, got {result:?}"
    );
    let msg = err_text(&result);
    assert!(
        msg.contains("ILO-CAP-001"),
        "err should include ILO-CAP-001 code, got: {msg}"
    );
    assert!(
        msg.contains("--allow-run"),
        "err should mention --allow-run, got: {msg}"
    );
}

#[test]
fn allow_run_permits_allowlisted_cmd() {
    let caps = Caps::Restricted {
        net: Policy::All,
        read: Policy::All,
        write: Policy::All,
        run: Policy::List(vec!["echo".to_owned()]),
        env: Policy::All,
    };
    let src = "f>R (M t t) t;run \"echo\" [\"hello\"]";
    let program = make_program(src);
    let result = interpreter::run_with_caps(&program, None, vec![], Arc::new(caps)).unwrap();
    // echo is in the allowlist — should return Ok(...), not Err.
    assert!(
        !is_err_value(&result),
        "echo should be permitted, got {result:?}"
    );
}

// ── No --allow-* flags → permissive (backwards compat) ───────────────────────

#[test]
fn no_allow_flags_is_permissive_for_file_read() {
    let path = "/tmp/ilo_cap_permissive.txt";
    std::fs::write(path, "permissive").unwrap();
    let src = format!("f>R t t;rd \"{path}\"");
    let (tree, vm_val) = run_both(&src, Caps::Permissive);
    assert!(
        !is_err_value(&tree),
        "tree: Permissive caps should allow rd, got {tree:?}"
    );
    assert!(
        !is_err_value(&vm_val),
        "vm: Permissive caps should allow rd, got {vm_val:?}"
    );
}

// ── `Caps::parse_allow` round-trip ───────────────────────────────────────────

#[test]
fn parse_allow_star_is_all() {
    assert_eq!(Caps::parse_allow("*"), Policy::All);
}

#[test]
fn parse_allow_empty_is_empty_list() {
    assert_eq!(Caps::parse_allow(""), Policy::List(vec![]));
}

#[test]
fn parse_allow_comma_list() {
    assert_eq!(
        Caps::parse_allow("a.com,b.com"),
        Policy::List(vec!["a.com".to_owned(), "b.com".to_owned()])
    );
}

// ── `build_caps` semantics through Caps API ───────────────────────────────────

/// When no --allow-* flags are present, caps is Permissive (no restriction).
#[test]
fn permissive_caps_allow_everything() {
    let caps = Caps::Permissive;
    assert!(caps.check_net("https://any.host").is_ok());
    assert!(caps.check_read("/etc/passwd").is_ok());
    assert!(caps.check_write("/etc/passwd").is_ok());
    assert!(caps.check_run("rm").is_ok());
}

// ── --allow-read: fs-metadata builtins (fsize/mtime/isfile/isdir) ─────────────

/// `fsize` blocked by --allow-read → returns Err containing ILO-CAP-001.
#[test]
fn allow_read_blocks_fsize() {
    let path = "/tmp/ilo_cap_fsize.txt";
    std::fs::write(path, "hello").unwrap();
    let caps = Caps::Restricted {
        net: Policy::All,
        read: Policy::List(vec![]),
        write: Policy::All,
        run: Policy::All,
        env: Policy::All,
    };
    let src = format!("f>R n t;fsize \"{path}\"");
    let (tree, _vm_val) = run_both(&src, caps);
    assert!(
        is_err_value(&tree),
        "tree: fsize should be blocked by read cap"
    );
    let msg = err_text(&tree);
    assert!(
        msg.contains("ILO-CAP-001"),
        "expected ILO-CAP-001, got: {msg}"
    );
}

/// `mtime` blocked by --allow-read → returns Err containing ILO-CAP-001.
#[test]
fn allow_read_blocks_mtime() {
    let path = "/tmp/ilo_cap_mtime.txt";
    std::fs::write(path, "hello").unwrap();
    let caps = Caps::Restricted {
        net: Policy::All,
        read: Policy::List(vec![]),
        write: Policy::All,
        run: Policy::All,
        env: Policy::All,
    };
    let src = format!("f>R n t;mtime \"{path}\"");
    let (tree, _vm_val) = run_both(&src, caps);
    assert!(
        is_err_value(&tree),
        "tree: mtime should be blocked by read cap"
    );
    let msg = err_text(&tree);
    assert!(
        msg.contains("ILO-CAP-001"),
        "expected ILO-CAP-001, got: {msg}"
    );
}

/// `isfile` blocked by --allow-read → returns false (collapses to bool false).
#[test]
fn allow_read_blocks_isfile_returns_false() {
    let path = "/tmp/ilo_cap_isfile.txt";
    std::fs::write(path, "hello").unwrap();
    let caps = Caps::Restricted {
        net: Policy::All,
        read: Policy::List(vec![]),
        write: Policy::All,
        run: Policy::All,
        env: Policy::All,
    };
    let src = format!("f>b;isfile \"{path}\"");
    let (tree, _vm_val) = run_both(&src, caps);
    assert_eq!(
        tree,
        Value::Bool(false),
        "isfile should return false when blocked by read cap"
    );
}

/// `isdir` blocked by --allow-read → returns false.
#[test]
fn allow_read_blocks_isdir_returns_false() {
    let caps = Caps::Restricted {
        net: Policy::All,
        read: Policy::List(vec![]),
        write: Policy::All,
        run: Policy::All,
        env: Policy::All,
    };
    let src = "f>b;isdir \"/tmp\"";
    let (tree, _vm_val) = run_both(src, caps);
    assert_eq!(
        tree,
        Value::Bool(false),
        "isdir should return false when blocked by read cap"
    );
}

/// When any --allow-* flag is set, undefined dimensions default to Policy::All.
#[test]
fn one_flag_restricts_only_that_dimension() {
    let caps = Caps::Restricted {
        net: Policy::List(vec![]), // net blocked
        read: Policy::All,         // read unrestricted
        write: Policy::All,        // write unrestricted
        run: Policy::All,          // run unrestricted
        env: Policy::All,          // env unrestricted
    };
    assert!(
        caps.check_net("https://any.host").is_err(),
        "net should be blocked"
    );
    assert!(
        caps.check_read("/etc/passwd").is_ok(),
        "read should be unrestricted"
    );
    assert!(
        caps.check_write("/tmp/x").is_ok(),
        "write should be unrestricted"
    );
    assert!(caps.check_run("rm").is_ok(), "run should be unrestricted");
}