ingot-cli 0.5.1

The `ingot` command-line compiler for the Ingot agent language.
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
//! End-to-end tests for `ingot run` and `ingot test`.
//!
//! A stub HTTP server stands in for the provider, so these exercise the whole
//! chain — compile, lower, execute, record, replay — over the real HTTP path,
//! with no API key and no network.

mod support;

use std::sync::atomic::Ordering;

use serde_json::Value;
use support::{
    code, openai_reply, repo_root, run, run_env, stderr, stdout, stub_provider, text_reply,
    TempDir, EXIT_DIAGNOSTICS, EXIT_OK,
};

/// An agent that pins a vendor, so the run has to choose a provider from the
/// artifact rather than from a flag.
fn pinned_agent(reference: &str) -> String {
    format!(
        r#"language 0.1

agent Pinned(topic: string) -> brief<markdown> {{
  model exact "{reference}"

  budget {{
    steps <= 2
    tokens <= 20000
  }}

  policy {{
    network deny
  }}

  flow {{
    emit brief = ask<markdown>("Write about ${{topic}}.")
  }}
}}
"#
    )
}

fn pinned_project(tag: &str, reference: &str) -> TempDir {
    project_with(tag, reference, "")
}

fn project_with(tag: &str, reference: &str, extra_manifest: &str) -> TempDir {
    let dir = TempDir::new(tag);
    std::fs::write(dir.path().join("main.ing"), pinned_agent(reference)).unwrap();
    std::fs::write(
        dir.path().join("ingot.toml"),
        format!("[project]\nname = \"pinned\"\n{extra_manifest}"),
    )
    .unwrap();
    dir
}

#[test]
fn an_operator_can_declare_their_own_model_service() {
    // The whole point: somebody running Ollama, vLLM or llama.cpp names it,
    // pins it in the source, and needs no key and no vendor account.
    let stub = stub_provider(vec![openai_reply("# From my own server")]);
    let project = project_with(
        "own-llm",
        "local/llama-test",
        &format!(
            "\n[[model.provider]]\nname = \"local\"\nkind = \"openai\"\nbase-url = \"{}\"\n",
            stub.url
        ),
    );

    let output = run_env(
        &[
            "run",
            &project.path().display().to_string(),
            "--input",
            "topic=compilers",
            "--events",
            "quiet",
        ],
        &[],
    );

    assert_eq!(code(&output), EXIT_OK, "{}", stderr(&output));
    assert_eq!(stdout(&output).trim(), "# From my own server");
    assert!(
        stderr(&output).contains("model calls go to local"),
        "{}",
        stderr(&output)
    );
}

#[test]
fn a_declared_provider_can_take_the_place_of_a_built_in_name() {
    // Pointing the familiar name somewhere else — a company gateway that
    // fronts OpenAI, say — without every artifact having to be edited.
    let stub = stub_provider(vec![openai_reply("# Through the gateway")]);
    let project = project_with(
        "override-openai",
        "openai/gpt-test",
        &format!(
            "\n[[model.provider]]\nname = \"openai\"\nkind = \"openai\"\nbase-url = \"{}\"\n",
            stub.url
        ),
    );

    // A real key is exported and must be ignored: the declaration wins, and it
    // asks for no key at all.
    let output = run_env(
        &[
            "run",
            &project.path().display().to_string(),
            "--input",
            "topic=compilers",
            "--events",
            "quiet",
        ],
        &[("OPENAI_API_KEY", "should-not-be-used")],
    );

    assert_eq!(code(&output), EXIT_OK, "{}", stderr(&output));
    assert_eq!(stdout(&output).trim(), "# Through the gateway");
}

#[test]
fn a_declaration_naming_a_key_variable_that_is_not_set_stops_the_run() {
    // A declared provider is a stated intention, so a missing key is an error
    // rather than a provider that quietly is not there.
    let project = project_with(
        "declared-nokey",
        "local/llama-test",
        "\n[[model.provider]]\nname = \"local\"\nkind = \"openai\"\n\
         base-url = \"http://127.0.0.1:1/v1/chat/completions\"\n\
         api-key-env = \"A_KEY_NOBODY_EXPORTED\"\n",
    );

    let output = run_env(
        &[
            "run",
            &project.path().display().to_string(),
            "--input",
            "topic=compilers",
        ],
        &[],
    );

    assert_ne!(code(&output), EXIT_OK);
    assert!(
        stderr(&output).contains("A_KEY_NOBODY_EXPORTED"),
        "{}",
        stderr(&output)
    );
}

#[test]
fn a_default_naming_no_provider_is_refused_before_anything_runs() {
    let project = project_with(
        "bad-default",
        "local/llama-test",
        "\n[model]\ndefault = \"typo\"\n\n[[model.provider]]\nname = \"local\"\n\
         kind = \"openai\"\nbase-url = \"http://127.0.0.1:1/v1/chat/completions\"\n",
    );

    let output = run_env(
        &[
            "run",
            &project.path().display().to_string(),
            "--input",
            "topic=compilers",
        ],
        &[],
    );

    assert_ne!(code(&output), EXIT_OK);
    let message = stderr(&output);
    assert!(message.contains("typo"), "{message}");
    assert!(message.contains("local"), "{message}");
}

#[test]
fn an_artifact_that_pins_openai_reaches_openai() {
    // The point of pinning: the source names the vendor, and the run honours it
    // without the operator repeating it on the command line.
    let project = pinned_project("pin-openai", "openai/gpt-test");
    let stub = stub_provider(vec![openai_reply("# From OpenAI")]);

    let output = run_env(
        &[
            "run",
            &project.path().display().to_string(),
            "--input",
            "topic=compilers",
            "--events",
            "quiet",
        ],
        &[
            ("OPENAI_API_KEY", "stub-key"),
            ("INGOT_OPENAI_BASE_URL", &stub.url),
        ],
    );

    assert_eq!(code(&output), EXIT_OK, "{}", stderr(&output));
    assert_eq!(stdout(&output).trim(), "# From OpenAI");
    assert!(
        stderr(&output).contains("model calls go to openai"),
        "the run must say which service answered: {}",
        stderr(&output)
    );
}

#[test]
fn an_artifact_pinning_a_vendor_with_no_key_is_refused_rather_than_redirected() {
    // An artifact that says `openai/…` must never be answered by Anthropic and
    // come back with a plausible answer from the wrong model.
    let project = pinned_project("pin-unavailable", "openai/gpt-test");
    let stub = stub_provider(vec![text_reply("# From the wrong vendor")]);

    let output = run_env(
        &[
            "run",
            &project.path().display().to_string(),
            "--input",
            "topic=compilers",
            "--events",
            "quiet",
        ],
        &[
            ("ANTHROPIC_API_KEY", "stub-key"),
            ("INGOT_ANTHROPIC_BASE_URL", &stub.url),
        ],
    );

    assert_ne!(code(&output), EXIT_OK);
    let message = stderr(&output);
    assert!(message.contains("openai"), "{message}");
    assert!(message.contains("no provider"), "{message}");
}

#[test]
fn with_no_key_at_all_the_run_says_what_to_export() {
    let project = pinned_project("pin-nokey", "openai/gpt-test");
    let output = run_env(
        &[
            "run",
            &project.path().display().to_string(),
            "--input",
            "topic=compilers",
        ],
        &[],
    );

    assert_ne!(code(&output), EXIT_OK);
    let message = stderr(&output);
    assert!(message.contains("OPENAI_API_KEY"), "{message}");
    assert!(message.contains("--provider replay"), "{message}");
}

fn summarizer() -> String {
    repo_root()
        .join("examples/document-summarizer")
        .display()
        .to_string()
}

#[test]
fn run_executes_an_agent_against_a_provider_and_prints_the_artifact() {
    let stub = stub_provider(vec![text_reply("# Summary\n\nThe document is short.")]);
    let output = run(
        &[
            "run",
            &summarizer(),
            "--input",
            "document=A short document about compilers.",
            "--input",
            "audience=engineers",
            "--events",
            "quiet",
        ],
        Some(&stub.url),
    );

    assert_eq!(code(&output), EXIT_OK, "{}", stderr(&output));
    assert!(stdout(&output).contains("# Summary"), "{}", stdout(&output));
    assert_eq!(
        stub.served.load(Ordering::SeqCst),
        1,
        "exactly one model call expected"
    );
}

#[test]
fn a_failed_multi_node_run_names_its_provenance_without_raw_json() {
    let stub = stub_provider(vec![text_reply("A draft")]);
    let project = TempDir::new("human-trace-failure");
    std::fs::write(
        project.path().join("main.ing"),
        r#"language 0.1

tool repo.read_file(path: string) -> text !filesystem_read

agent Trace(topic: string) -> brief<markdown> {
  model exact "anthropic/claude-test"

  tools {
    mcp repo.read_file
  }

  budget {
    steps <= 4
    tokens <= 20000
  }

  policy {
    filesystem_read allow ["."]
    network deny
  }

  flow {
    draft = ask<markdown>("Draft a brief about ${topic}.")
    note = call repo.read_file("note.txt")
    emit brief = ask<markdown>("Revise ${draft} using ${note}.")
  }
}
"#,
    )
    .unwrap();
    std::fs::write(
        project.path().join("ingot.toml"),
        "[project]\nname = \"trace\"\n",
    )
    .unwrap();

    let output = run(
        &[
            "run",
            &project.path().display().to_string(),
            "--input",
            "topic=compilers",
        ],
        Some(&stub.url),
    );
    assert_eq!(code(&output), EXIT_DIAGNOSTICS, "{}", stderr(&output));
    let trace = stderr(&output);
    assert!(trace.contains("model.call"), "{trace}");
    assert!(
        trace.contains("node.started Trace:n1  tool.call"),
        "{trace}"
    );
    assert!(trace.contains("run.failed   Trace:n1"), "{trace}");
    assert!(trace.contains("repo.read_file"), "{trace}");
    assert!(trace.contains("<redacted input.topic:string>"), "{trace}");
    assert!(
        !trace.contains(r#"\"event\":"#),
        "raw JSON leaked:\n{trace}"
    );
}

#[test]
fn run_writes_artifacts_with_the_right_extension() {
    let dir = TempDir::new("out");
    let stub = stub_provider(vec![text_reply("# Written to disk")]);
    let output = run(
        &[
            "run",
            &summarizer(),
            "--input",
            "document=text",
            "--input",
            "audience=all",
            "--out-dir",
            &dir.path().display().to_string(),
            "--events",
            "quiet",
        ],
        Some(&stub.url),
    );

    assert_eq!(code(&output), EXIT_OK, "{}", stderr(&output));
    let path = dir.path().join("summary.md");
    assert!(path.is_file(), "expected {}", path.display());
    assert_eq!(std::fs::read_to_string(&path).unwrap(), "# Written to disk");
}

#[test]
fn a_missing_input_is_reported_before_any_provider_call() {
    let stub = stub_provider(vec![text_reply("never used")]);
    let output = run(
        &["run", &summarizer(), "--events", "quiet"],
        Some(&stub.url),
    );

    assert_eq!(code(&output), EXIT_DIAGNOSTICS);
    assert!(
        stderr(&output).contains("missing input"),
        "{}",
        stderr(&output)
    );
    assert_eq!(
        stub.served.load(Ordering::SeqCst),
        0,
        "nothing should be sent"
    );
}

#[test]
fn an_input_file_can_be_read_with_an_at_prefix() {
    let dir = TempDir::new("input-file");
    let doc = dir.path().join("document.txt");
    std::fs::write(&doc, "Contents loaded from a file.").unwrap();

    let stub = stub_provider(vec![text_reply("# Read it")]);
    let output = run(
        &[
            "run",
            &summarizer(),
            "--input",
            &format!("document=@{}", doc.display()),
            "--input",
            "audience=readers",
            "--events",
            "quiet",
        ],
        Some(&stub.url),
    );
    assert_eq!(code(&output), EXIT_OK, "{}", stderr(&output));
}

#[test]
fn the_event_stream_can_be_emitted_as_json_lines() {
    let stub = stub_provider(vec![text_reply("# Events")]);
    let output = run(
        &[
            "run",
            &summarizer(),
            "--input",
            "document=d",
            "--input",
            "audience=a",
            "--events",
            "json",
        ],
        Some(&stub.url),
    );
    assert_eq!(code(&output), EXIT_OK, "{}", stderr(&output));

    let events: Vec<Value> = stderr(&output)
        .lines()
        .filter(|line| line.starts_with('{'))
        .map(|line| serde_json::from_str(line).expect("each event line must be JSON"))
        .collect();
    assert!(events.iter().any(|e| e["event"] == "runStarted"));
    assert!(events.iter().any(|e| e["event"] == "modelCall"));
    assert!(events.iter().any(|e| e["event"] == "emitted"));
    assert!(events.iter().any(|e| e["event"] == "runFinished"));
}

#[test]
fn a_recorded_cassette_replays_without_a_provider() {
    let dir = TempDir::new("record");
    let cassette = dir.path().join("summarize.json");
    let stub = stub_provider(vec![text_reply("# Recorded once")]);

    let record = run(
        &[
            "run",
            &summarizer(),
            "--input",
            "document=the source",
            "--input",
            "audience=everyone",
            "--record",
            &cassette.display().to_string(),
            "--events",
            "quiet",
        ],
        Some(&stub.url),
    );
    assert_eq!(code(&record), EXIT_OK, "{}", stderr(&record));
    assert!(cassette.is_file());

    // Replay with no key and no server at all.
    let replay = run(
        &[
            "run",
            &summarizer(),
            "--input",
            "document=the source",
            "--input",
            "audience=everyone",
            "--provider",
            "replay",
            "--cassette",
            &cassette.display().to_string(),
            "--events",
            "quiet",
        ],
        None,
    );
    assert_eq!(code(&replay), EXIT_OK, "{}", stderr(&replay));
    assert_eq!(stdout(&replay).trim(), "# Recorded once");
}

#[test]
fn replaying_with_different_inputs_fails_loudly() {
    let dir = TempDir::new("mismatch");
    let cassette = dir.path().join("summarize.json");
    let stub = stub_provider(vec![text_reply("# Recorded")]);

    let record = run(
        &[
            "run",
            &summarizer(),
            "--input",
            "document=the original",
            "--input",
            "audience=everyone",
            "--record",
            &cassette.display().to_string(),
            "--events",
            "quiet",
        ],
        Some(&stub.url),
    );
    assert_eq!(code(&record), EXIT_OK, "{}", stderr(&record));

    let replay = run(
        &[
            "run",
            &summarizer(),
            "--input",
            "document=something else entirely",
            "--input",
            "audience=everyone",
            "--provider",
            "replay",
            "--cassette",
            &cassette.display().to_string(),
            "--events",
            "quiet",
        ],
        None,
    );
    assert_eq!(code(&replay), EXIT_DIAGNOSTICS);
    assert!(stderr(&replay).contains("re-record"), "{}", stderr(&replay));
}

#[test]
fn a_tool_using_agent_stops_because_no_host_provides_the_tool() {
    // Honest failure while MCP is unimplemented: the artifact needs a tool, no
    // host offers one, and the run says exactly that instead of pretending.
    //
    // The first node is `queries = ask<string[]>(...)`, so the stub has to
    // answer with a schema-shaped value for execution to reach the tool call.
    let stub = stub_provider(vec![text_reply(r#"{"value":["one","two"]}"#)]);
    let path = repo_root()
        .join("examples/research-agent")
        .display()
        .to_string();
    let output = run(
        &[
            "run",
            &path,
            "--input",
            "topic=compilers",
            "--events",
            "quiet",
        ],
        Some(&stub.url),
    );

    assert_eq!(code(&output), EXIT_DIAGNOSTICS);
    let message = stderr(&output);
    assert!(message.contains("web.search"), "{message}");
    assert!(message.contains("no host provides"), "{message}");
}

#[test]
fn replaying_with_inputs_the_recording_never_saw_fails_loudly() {
    // The example holds one cassette, so `--provider replay` finds it without
    // being told where it is. It was recorded against other inputs, and a
    // replay that answered anyway would hand back a stale answer as if it were
    // this run's — the failure mode the digest exists to make impossible.
    //
    // `--cassette` is deliberately absent: this also covers the resolution.
    let output = run(
        &[
            "run",
            &summarizer(),
            "--provider",
            "replay",
            "--input",
            "document=d",
            "--input",
            "audience=a",
        ],
        None,
    );
    assert_ne!(code(&output), EXIT_OK);
    let message = stderr(&output);
    assert!(
        message.contains("recorded for a different request"),
        "{message}"
    );
    assert!(message.contains("re-record"), "{message}");
}

#[test]
fn ingot_test_replays_the_checked_in_cassettes() {
    let path = repo_root()
        .join("examples/document-summarizer")
        .display()
        .to_string();
    let output = run(&["test", &path], None);
    assert_eq!(code(&output), EXIT_OK, "{}", stderr(&output));
    assert!(stdout(&output).contains("passed"), "{}", stdout(&output));
}

#[test]
fn ingot_test_reports_no_cassettes_rather_than_failing() {
    // `ingot init` now deliberately includes an offline starter cassette. Use
    // a hand-written project to preserve the separate contract that projects
    // with no fixture still report an empty suite rather than failing.
    let project = project_with("no-cassettes", "anthropic/claude-opus-5", "");

    let output = run(&["test", &project.path().display().to_string()], None);
    assert_eq!(code(&output), EXIT_OK);
    assert!(
        stderr(&output).contains("nothing to test"),
        "{}",
        stderr(&output)
    );
}