ilo 0.10.0

ilo — a programming language for AI agents
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
/// Integration tests for the tools infrastructure (D1d–D1f).
///
/// Unit-level tests (Value JSON round-trips, etc.) live in
/// `src/interpreter/json.rs`.  These tests exercise the CLI end-to-end.
use std::process::Command;

fn ilo() -> Command {
    Command::new(env!("CARGO_BIN_EXE_ilo"))
}

// ── Tool stub: program with a tool decl runs without a --tools config ─────────

/// A program that declares a tool and calls it from main should compile and run
/// using the built-in stub (returns ~nil).
#[test]
fn tool_call_stub_via_interp() {
    // ilo syntax: tool <name>"<desc>" params>return
    let prog = r#"tool mytool"a helper" x:t>R _ t
main x:t>R _ t;mytool x"#;
    let out = ilo()
        .args([prog, "--run-interp", "main", "hello"])
        .output()
        .expect("ilo failed to start");
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout);
    // stub returns ~nil which displays as "~nil"
    assert_eq!(stdout.trim(), "~nil");
}

/// Same program via the register VM.
#[test]
fn tool_call_stub_via_vm() {
    let prog = r#"tool mytool"a helper" x:t>R _ t
main x:t>R _ t;mytool x"#;
    let out = ilo()
        .args([prog, "--run-vm", "main", "hello"])
        .output()
        .expect("ilo failed to start");
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert_eq!(stdout.trim(), "~nil");
}

// ── --tools flag requires a path ────────────────────────────────────────────

/// Passing `--tools` with a path to a file that doesn't exist should
/// produce an error about reading the file.
#[test]
fn tools_flag_nonexistent_path() {
    let prog = r#"tool mytool"a helper" x:t>R _ t
main x:t>R _ t;mytool x"#;
    let out = ilo()
        .args([prog, "--tools", "/nonexistent/path/to/config.json", "--run-interp", "main", "hello"])
        .output()
        .expect("ilo failed to start");
    assert!(
        !out.status.success(),
        "expected failure when tools config path does not exist"
    );
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("failed to read tools config") || stderr.contains("No such file"),
        "expected file-not-found error, got: {}",
        stderr
    );
}

// ── --tools with a valid config file ─────────────────────────────────────────

/// Create a minimal tools config JSON and verify the CLI accepts it.
/// The tool call will attempt a network connection but we don't care about
/// the result here — just that parsing and dispatch don't panic.
#[test]
fn tools_flag_with_valid_config() {
    use std::io::Write;

    let (path, mut file) = tempfile_in_tmp("tools_config_valid.json");
    writeln!(
        file,
        r#"{{"tools": {{"mytool": {{"url": "http://127.0.0.1:19999/mytool"}}}}}}"#
    )
    .unwrap();
    drop(file);

    // Program that ignores the tool result
    let prog = r#"tool mytool"a helper" x:t>R _ t
main x:t>t;"hello""#;

    let out = ilo()
        .args([prog, "--tools", &path, "--run-interp", "main", "world"])
        .output()
        .expect("ilo failed to start");

    // No panic / ICE should occur
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        !stderr.contains("thread 'main' panicked"),
        "unexpected panic: {}",
        stderr
    );

    std::fs::remove_file(&path).ok();
}

// ── AST: tool declaration appears in JSON AST ─────────────────────────────

#[test]
fn tool_decl_in_ast_json() {
    let prog = r#"tool mytool"a helper" x:t>R _ t"#;
    let out = ilo()
        .args([prog])
        .output()
        .expect("ilo failed to start");
    assert!(out.status.success(), "stderr: {}", String::from_utf8_lossy(&out.stderr));
    let stdout = String::from_utf8_lossy(&out.stdout);
    // The AST JSON should contain "Tool" and "mytool"
    assert!(
        stdout.contains("Tool") || stdout.contains("mytool"),
        "expected tool in AST, got: {}",
        stdout
    );
}

// ── HTTP provider config parsing ──────────────────────────────────────────

/// Verify that an invalid JSON tools config produces a clear error message.
#[test]
fn tools_flag_invalid_json_config() {
    use std::io::Write;

    let (path, mut file) = tempfile_in_tmp("bad_config.json");
    write!(file, "not valid json").unwrap();
    drop(file);

    let prog = r#"tool mytool"a helper" x:t>R _ t
main x:t>R _ t;mytool x"#;
    let out = ilo()
        .args([prog, "--tools", &path, "--run-interp", "main", "hello"])
        .output()
        .expect("ilo failed to start");

    assert!(!out.status.success(), "expected failure for invalid JSON config");
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("failed to parse tools config") || stderr.contains("parse") || stderr.contains("JSON"),
        "expected parse error in stderr, got: {}",
        stderr
    );

    std::fs::remove_file(&path).ok();
}

// ── Tool stub returns ~nil from VM with is_tool flag ──────────────────────

/// Verify the `is_tool` flag in CompiledProgram is respected by the VM:
/// the stub chunk returns ~nil without entering a normal call frame.
#[test]
fn vm_tool_stub_returns_ok_nil() {
    let prog = r#"tool mytool"a helper" x:t>R _ t
f>R _ t;mytool "test""#;
    let out = ilo()
        .args([prog, "--run-vm", "f"])
        .output()
        .expect("ilo failed to start");
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert_eq!(String::from_utf8_lossy(&out.stdout).trim(), "~nil");
}

/// Multiple tool declarations — all return stubs.
#[test]
fn vm_multiple_tool_stubs() {
    let prog = r#"tool a"first" x:t>R _ t
tool b"second" x:t>R _ t
f>R _ t;b "test""#;
    let out = ilo()
        .args([prog, "--run-vm", "f"])
        .output()
        .expect("ilo failed to start");
    assert!(out.status.success(), "stderr: {}", String::from_utf8_lossy(&out.stderr));
    assert_eq!(String::from_utf8_lossy(&out.stdout).trim(), "~nil");
}

// ── Ignored: real HTTP test ───────────────────────────────────────────────

/// End-to-end HTTP test — ignored by default, requires a live server.
#[test]
#[ignore]
fn get_builtin_real_http() {
    let prog = "main x:t>R t t;$x";
    let out = ilo()
        .args([prog, "--run-interp", "main", "https://httpbin.org/get"])
        .output()
        .expect("ilo failed to start");
    assert!(out.status.success(), "stderr: {}", String::from_utf8_lossy(&out.stderr));
}

// ── `ilo tools --mcp` with empty config (no servers) ─────────────────────

/// `ilo tools --mcp <empty_config>` with no mcpServers: exercises the mcp_path
/// code path without connecting to any MCP servers. Covers main.rs L44-46 (mcp_path
/// set in tools_cmd arg parsing) and L114 (implicit else of http_path=None branch).
#[test]
fn tools_cmd_mcp_empty_servers() {
    use std::io::Write;
    let (path, mut file) = tempfile_in_tmp("tools_mcp_empty.json");
    writeln!(file, r#"{{"mcpServers": {{}}}}"#).unwrap();
    drop(file);

    let out = ilo()
        .args(["tools", "--mcp", &path])
        .output()
        .expect("ilo failed to start");

    // No servers to connect → exits cleanly with empty output or success
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        !stderr.contains("thread 'main' panicked"),
        "unexpected panic: {stderr}"
    );

    std::fs::remove_file(&path).ok();
}

// ── `ilo tools` subcommand output formats ─────────────────────────────────

/// Create a minimal HTTP tools config and verify `ilo tools --tools <path>` (Human format).
/// Covers: main.rs L114 (http_names loaded), L122-127 (Human render loop).
#[test]
fn tools_cmd_http_human_format() {
    use std::io::Write;
    let (path, mut file) = tempfile_in_tmp("tools_cmd_human.json");
    writeln!(file, r#"{{"tools": {{"mytool": {{"url": "http://127.0.0.1:19999/mytool"}}}}}}"#).unwrap();
    drop(file);

    let out = ilo()
        .args(["tools", "--tools", &path])
        .output()
        .expect("ilo failed to start");

    assert!(out.status.success(), "stderr: {}", String::from_utf8_lossy(&out.stderr));
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("mytool"), "expected tool name in output, got: {stdout}");

    std::fs::remove_file(&path).ok();
}

/// `ilo tools --tools <path> --full` renders extra type info for HTTP tools.
/// Covers: main.rs L123-124 (full=true branch for http tools).
#[test]
fn tools_cmd_http_human_full() {
    use std::io::Write;
    let (path, mut file) = tempfile_in_tmp("tools_cmd_human_full.json");
    writeln!(file, r#"{{"tools": {{"bigtool": {{"url": "http://127.0.0.1:19999/bigtool"}}}}}}"#).unwrap();
    drop(file);

    let out = ilo()
        .args(["tools", "--tools", &path, "--full"])
        .output()
        .expect("ilo failed to start");

    assert!(out.status.success(), "stderr: {}", String::from_utf8_lossy(&out.stderr));
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("bigtool"), "expected tool name in full output, got: {stdout}");

    std::fs::remove_file(&path).ok();
}

/// `ilo tools --tools <path> --ilo` emits ilo tool declarations.
/// Covers: main.rs L140-143 (Ilo format HTTP render).
#[test]
fn tools_cmd_http_ilo_format() {
    use std::io::Write;
    let (path, mut file) = tempfile_in_tmp("tools_cmd_ilo.json");
    writeln!(file, r#"{{"tools": {{"hello": {{"url": "http://127.0.0.1:19999/hello"}}}}}}"#).unwrap();
    drop(file);

    let out = ilo()
        .args(["tools", "--tools", &path, "--ilo"])
        .output()
        .expect("ilo failed to start");

    assert!(out.status.success(), "stderr: {}", String::from_utf8_lossy(&out.stderr));
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("hello"), "expected tool in ilo output, got: {stdout}");
    assert!(stdout.contains("tool"), "expected 'tool' keyword in ilo output, got: {stdout}");

    std::fs::remove_file(&path).ok();
}

/// `ilo tools --tools <path> --json` emits JSON array of tool metadata.
/// Covers: main.rs L149-181 (Json format HTTP render + serde_json::to_string_pretty).
#[test]
fn tools_cmd_http_json_format() {
    use std::io::Write;
    let (path, mut file) = tempfile_in_tmp("tools_cmd_json.json");
    writeln!(file, r#"{{"tools": {{"greet": {{"url": "http://127.0.0.1:19999/greet"}}}}}}"#).unwrap();
    drop(file);

    let out = ilo()
        .args(["tools", "--tools", &path, "--json"])
        .output()
        .expect("ilo failed to start");

    assert!(out.status.success(), "stderr: {}", String::from_utf8_lossy(&out.stderr));
    let stdout = String::from_utf8_lossy(&out.stdout);
    // Should be valid JSON array
    let parsed: serde_json::Value = serde_json::from_str(stdout.trim())
        .expect("expected valid JSON output");
    assert!(parsed.is_array(), "expected JSON array, got: {parsed}");
    let arr = parsed.as_array().unwrap();
    assert!(arr.iter().any(|t| t["name"] == "greet"), "expected greet tool in JSON output");

    std::fs::remove_file(&path).ok();
}

// ── Helper ────────────────────────────────────────────────────────────────

/// Create a named temp file in the system temp directory.
/// Returns (path_string, File handle).
fn tempfile_in_tmp(name: &str) -> (String, std::fs::File) {
    let mut path = std::env::temp_dir();
    path.push(format!("ilo_test_{name}"));
    let f = std::fs::File::create(&path).expect("create temp file");
    (path.to_string_lossy().into_owned(), f)
}

// ── Wiremock HTTP provider test (feature-gated) ───────────────────────────

#[cfg(feature = "tools")]
mod http_tests {
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    #[tokio::test]
    async fn http_provider_success() {
        let server = MockServer::start().await;

        Mock::given(method("POST"))
            .and(path("/double"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!(4.0)))
            .mount(&server)
            .await;

        let config_json = serde_json::json!({
            "tools": {
                "double": {
                    "url": format!("{}/double", server.uri())
                }
            }
        });

        let mut p = std::env::temp_dir();
        p.push("ilo_wiremock_test.json");
        std::fs::write(&p, config_json.to_string()).unwrap();

        let prog = r#"tool double"doubles a number" x:n>R n n
main x:n>R n n;double x"#;
        let out = std::process::Command::new(env!("CARGO_BIN_EXE_ilo"))
            .args([
                prog,
                "--tools",
                p.to_str().unwrap(),
                "--run-interp",
                "main",
                "2",
            ])
            .output()
            .expect("ilo failed to start");

        let stdout = String::from_utf8_lossy(&out.stdout);
        let stderr = String::from_utf8_lossy(&out.stderr);
        assert!(out.status.success(), "stderr: {stderr}\nstdout: {stdout}");

        std::fs::remove_file(&p).ok();
    }

    #[tokio::test]
    async fn http_provider_not_configured() {
        let config_json = serde_json::json!({ "tools": {} });
        let mut p = std::env::temp_dir();
        p.push("ilo_wiremock_not_configured.json");
        std::fs::write(&p, config_json.to_string()).unwrap();

        let prog = r#"tool double"doubles a number" x:n>R n n
main x:n>R n n;double x"#;
        let out = std::process::Command::new(env!("CARGO_BIN_EXE_ilo"))
            .args([
                prog,
                "--tools",
                p.to_str().unwrap(),
                "--run-interp",
                "main",
                "2",
            ])
            .output()
            .expect("ilo failed to start");

        // The call fails with "tool not configured: double" → RuntimeError → exit 1
        assert!(!out.status.success(), "expected failure for unconfigured tool");

        std::fs::remove_file(&p).ok();
    }

    #[tokio::test]
    async fn http_provider_server_error() {
        let server = MockServer::start().await;

        Mock::given(method("POST"))
            .and(path("/double"))
            .respond_with(
                ResponseTemplate::new(500)
                    .set_body_json(serde_json::json!({"error": "internal"})),
            )
            .mount(&server)
            .await;

        let config_json = serde_json::json!({
            "tools": {
                "double": {
                    "url": format!("{}/double", server.uri())
                }
            }
        });

        let mut p = std::env::temp_dir();
        p.push("ilo_wiremock_server_error.json");
        std::fs::write(&p, config_json.to_string()).unwrap();

        let prog = r#"tool double"doubles a number" x:n>R n n
main x:n>R n n;double x"#;
        let out = std::process::Command::new(env!("CARGO_BIN_EXE_ilo"))
            .args([
                prog,
                "--tools",
                p.to_str().unwrap(),
                "--run-interp",
                "main",
                "2",
            ])
            .output()
            .expect("ilo failed to start");

        let stderr = String::from_utf8_lossy(&out.stderr);
        assert!(
            !stderr.contains("thread 'main' panicked"),
            "unexpected panic: {stderr}"
        );

        std::fs::remove_file(&p).ok();
    }
}