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
//! Shared scaffolding for the `ingot` end-to-end tests.
//!
//! A stub HTTP server stands in for the model provider, so the tests exercise
//! the real HTTP path with no API key and no network. Each test binary uses a
//! different subset of this, hence the blanket allow.

#![allow(dead_code)]

use std::io::{BufRead, BufReader, Read, Write};
use std::net::{TcpListener, TcpStream};
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::thread;

use serde_json::{json, Value};

pub const EXIT_OK: i32 = 0;
pub const EXIT_DIAGNOSTICS: i32 = 1;
/// An operational failure: something outside the source went wrong.
pub const EXIT_FAILURE: i32 = 2;

pub fn binary() -> &'static str {
    env!("CARGO_BIN_EXE_ingot")
}

pub fn repo_root() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .and_then(Path::parent)
        .expect("the crate must live two levels below the repository root")
        .to_path_buf()
}

pub struct TempDir(PathBuf);

impl TempDir {
    pub fn new(tag: &str) -> TempDir {
        static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

        let unique = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("system clock is before the epoch")
            .as_nanos();
        // The clock alone is not enough. `as_nanos` reports whatever resolution
        // the platform has, and on macOS that is microseconds — so two tests
        // starting in the same microsecond got the same directory and quietly
        // overwrote each other's fixtures. Diagnosing that from the failure is
        // very hard: the symptom is one test reading another test's file.
        let counter = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        let path = std::env::temp_dir().join(format!(
            "ingot-run-{tag}-{unique}-{}-{counter}",
            std::process::id()
        ));
        std::fs::create_dir_all(&path).expect("creating the scratch directory");
        TempDir(path)
    }

    pub fn path(&self) -> &Path {
        &self.0
    }
}

impl Drop for TempDir {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.0);
    }
}

/// A stub provider that answers `replies` in order, then stops accepting.
pub struct StubProvider {
    pub url: String,
    pub served: Arc<AtomicUsize>,
}

pub fn stub_provider(replies: Vec<Value>) -> StubProvider {
    let listener = TcpListener::bind("127.0.0.1:0").expect("binding a local port");
    let port = listener.local_addr().unwrap().port();
    let served = Arc::new(AtomicUsize::new(0));
    let counter = Arc::clone(&served);

    thread::spawn(move || {
        for stream in listener.incoming().take(replies.len()) {
            let Ok(stream) = stream else { break };
            let index = counter.fetch_add(1, Ordering::SeqCst);
            let reply = replies.get(index).cloned().unwrap_or(Value::Null);
            let _ = answer(stream, &reply);
        }
    });

    StubProvider {
        url: format!("http://127.0.0.1:{port}/v1/messages"),
        served,
    }
}

fn answer(mut stream: TcpStream, reply: &Value) -> std::io::Result<()> {
    let mut reader = BufReader::new(stream.try_clone()?);
    let mut line = String::new();
    reader.read_line(&mut line)?;

    let mut content_length = 0usize;
    loop {
        let mut header = String::new();
        if reader.read_line(&mut header)? == 0 {
            break;
        }
        let header = header.trim_end();
        if header.is_empty() {
            break;
        }
        if let Some((name, value)) = header.split_once(':') {
            if name.trim().eq_ignore_ascii_case("content-length") {
                content_length = value.trim().parse().unwrap_or(0);
            }
        }
    }
    let mut body = vec![0u8; content_length];
    reader.read_exact(&mut body)?;
    let request: Value = serde_json::from_slice(&body).unwrap_or(Value::Null);

    // The provider streams, so the stub has to. Rather than keep a second set
    // of fixtures, the same reply is re-framed as the event stream that would
    // have produced it — which is also the sharpest test of the property the
    // providers are built on: one parser, two transports, same answer.
    if request.get("stream") == Some(&Value::Bool(true)) {
        let (content_type, payload) = (String::from("text/event-stream"), as_event_stream(reply));
        write!(
            stream,
            "HTTP/1.1 200 OK\r\nContent-Type: {content_type}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
            payload.len()
        )?;
        stream.write_all(payload.as_bytes())?;
        return stream.flush();
    }

    let payload = serde_json::to_vec(reply)?;
    write!(
        stream,
        "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
        payload.len()
    )?;
    stream.write_all(&payload)?;
    stream.flush()
}

/// Re-frame a whole reply as the event stream that would have produced it.
///
/// The vendor is read off the reply's own shape, so a test writes one fixture
/// and gets both transports. A reply in neither shape is served as-is, which is
/// how an error payload reaches the provider unchanged.
fn as_event_stream(reply: &Value) -> String {
    let mut out = String::new();
    let mut push = |name: &str, data: Value| {
        if !name.is_empty() {
            out.push_str(&format!("event: {name}\n"));
        }
        out.push_str(&format!("data: {data}\n\n"));
    };

    if let Some(content) = reply.get("content") {
        let text: String = content
            .as_array()
            .map(|blocks| {
                blocks
                    .iter()
                    .filter(|block| block.get("type").and_then(Value::as_str) == Some("text"))
                    .filter_map(|block| block.get("text").and_then(Value::as_str))
                    .collect::<Vec<_>>()
                    .join("")
            })
            .unwrap_or_default();
        push(
            "message_start",
            json!({ "type": "message_start", "message": {
                "id": reply.get("id").cloned().unwrap_or(Value::Null),
                "model": reply.get("model").cloned().unwrap_or(Value::Null),
                "usage": reply.get("usage").cloned().unwrap_or(json!({})),
            }}),
        );
        for fragment in fragments(&text) {
            push(
                "content_block_delta",
                json!({ "type": "content_block_delta",
                        "delta": { "type": "text_delta", "text": fragment } }),
            );
        }
        push(
            "message_delta",
            json!({ "type": "message_delta", "delta": {
                "stop_reason": reply.get("stop_reason").cloned().unwrap_or(Value::Null),
                "stop_details": reply.get("stop_details").cloned().unwrap_or(Value::Null),
            }, "usage": reply.get("usage").cloned().unwrap_or(json!({})) }),
        );
        push("message_stop", json!({ "type": "message_stop" }));
        return out;
    }

    if let Some(choice) = reply
        .get("choices")
        .and_then(Value::as_array)
        .and_then(|choices| choices.first())
    {
        let text = choice
            .get("message")
            .and_then(|message| message.get("content"))
            .and_then(Value::as_str)
            .unwrap_or_default();
        let model = reply.get("model").cloned().unwrap_or(Value::Null);
        for fragment in fragments(text) {
            push(
                "",
                json!({ "model": model, "choices": [
                    { "index": 0, "delta": { "content": fragment }, "finish_reason": Value::Null }
                ]}),
            );
        }
        push(
            "",
            json!({ "model": model, "choices": [{
                "index": 0,
                "delta": {},
                "finish_reason": choice.get("finish_reason").cloned().unwrap_or(Value::Null),
            }], "usage": reply.get("usage").cloned().unwrap_or(Value::Null) }),
        );
        push("", json!("[DONE]"));
        // `[DONE]` is framing rather than JSON, and quoting it would make it a
        // string the provider tries to parse as a chunk.
        return out.replace("data: \"[DONE]\"", "data: [DONE]");
    }

    format!("data: {reply}\n\n")
}

/// A few pieces rather than one, so the streaming path is actually exercised.
fn fragments(text: &str) -> Vec<&str> {
    if text.is_empty() {
        return Vec::new();
    }
    let mut pieces = Vec::new();
    let mut rest = text;
    while !rest.is_empty() {
        let mut cut = rest.len().min(16);
        while cut > 0 && !rest.is_char_boundary(cut) {
            cut -= 1;
        }
        let (piece, tail) = rest.split_at(cut.max(1).min(rest.len()));
        pieces.push(piece);
        rest = tail;
    }
    pieces
}

pub fn text_reply(text: &str) -> Value {
    json!({
        "id": "msg_stub",
        "model": "claude-opus-5",
        "stop_reason": "end_turn",
        "content": [{ "type": "text", "text": text }],
        "usage": { "input_tokens": 120, "output_tokens": 40 },
    })
}

/// A Chat Completions reply, for the OpenAI-compatible provider.
pub fn openai_reply(text: &str) -> Value {
    json!({
        "id": "chatcmpl-stub",
        "model": "gpt-test",
        "choices": [{
            "index": 0,
            "message": { "role": "assistant", "content": text },
            "finish_reason": "stop",
        }],
        "usage": { "prompt_tokens": 120, "completion_tokens": 40 },
    })
}

pub fn run(args: &[&str], base_url: Option<&str>) -> Output {
    match base_url {
        Some(url) => run_env(
            args,
            &[
                ("ANTHROPIC_API_KEY", "stub-key"),
                ("INGOT_ANTHROPIC_BASE_URL", url),
            ],
        ),
        None => run_env(args, &[]),
    }
}

/// Run with exactly these provider variables set, and no others.
///
/// Every key is cleared first: a run must never pick up a real credential from
/// the developer's shell and reach a real service.
pub fn run_env(args: &[&str], env: &[(&str, &str)]) -> Output {
    let mut command = Command::new(binary());
    command.args(args).arg("--color").arg("never");
    for name in [
        "ANTHROPIC_API_KEY",
        "INGOT_ANTHROPIC_BASE_URL",
        "OPENAI_API_KEY",
        "INGOT_OPENAI_BASE_URL",
    ] {
        command.env_remove(name);
    }
    for (name, value) in env {
        command.env(name, value);
    }
    command.output().expect("the ingot binary must be runnable")
}

/// The same, from a chosen working directory.
///
/// For commands whose behaviour depends on where they were typed — a run
/// outside any checkout is the case `ingot conform` most needs to work.
pub fn run_in(cwd: &Path, args: &[&str]) -> Output {
    let mut command = Command::new(binary());
    command
        .args(args)
        .arg("--color")
        .arg("never")
        .current_dir(cwd);
    for name in [
        "ANTHROPIC_API_KEY",
        "INGOT_ANTHROPIC_BASE_URL",
        "OPENAI_API_KEY",
        "INGOT_OPENAI_BASE_URL",
    ] {
        command.env_remove(name);
    }
    command.output().expect("the ingot binary must be runnable")
}

pub fn stdout(output: &Output) -> String {
    String::from_utf8_lossy(&output.stdout).into_owned()
}

pub fn stderr(output: &Output) -> String {
    String::from_utf8_lossy(&output.stderr).into_owned()
}

pub fn code(output: &Output) -> i32 {
    output
        .status
        .code()
        .expect("the process must exit normally")
}

/// Where the reference MCP server binary is, building it if this test binary was
/// invoked in a way that did not.
///
/// `CARGO_BIN_EXE_*` covers this package's own binaries only, and the server
/// belongs to `ingot-mcp`, so it has to be found — and sometimes built — by hand.
pub fn fs_server() -> PathBuf {
    static BUILD: std::sync::Once = std::sync::Once::new();

    let mut dir = std::env::current_exe().expect("the test binary has a path");
    dir.pop();
    if dir.ends_with("deps") {
        dir.pop();
    }
    let path = dir.join(format!("ingot-mcp-fs{}", std::env::consts::EXE_SUFFIX));

    BUILD.call_once(|| {
        if path.is_file() {
            return;
        }
        // `cargo test -p ingot-cli` builds the ingot-mcp library but not its
        // binaries, so build it rather than failing with a puzzle.
        let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
        let mut command = Command::new(cargo);
        command.current_dir(repo_root()).args([
            "build",
            "-p",
            "ingot-mcp",
            "--bin",
            "ingot-mcp-fs",
        ]);
        if dir.ends_with("release") {
            command.arg("--release");
        }
        let status = command.status().expect("cargo must be runnable");
        assert!(status.success(), "building ingot-mcp-fs failed");
    });

    assert!(
        path.is_file(),
        "expected the reference MCP server at {}",
        path.display()
    );
    path
}

/// A TOML string literal. Windows paths are full of backslashes and a manifest
/// written without escaping them parses as something else entirely.
pub fn toml_string(value: &str) -> String {
    format!("\"{}\"", value.replace('\\', "\\\\").replace('"', "\\\""))
}

/// A recorded authoring session: one reply per proposal, in order.
///
/// Authoring replays leniently, so a fixture stays valid when the authoring
/// prompt changes. What it pins is the source a model proposed — which the
/// compiler and the routing table then judge — not the prompt that asked.
pub fn authoring_cassette(dir: &Path, name: &str, replies: &[&str]) -> PathBuf {
    let interactions: Vec<Value> = replies
        .iter()
        .enumerate()
        .map(|(index, reply)| {
            json!({
                "index": index,
                "node": format!("authoring.{index}"),
                "requestDigest": "0".repeat(64),
                "responseType": "text",
                "value": format!("```ingot\n{reply}```"),
                "usage": { "inputTokens": 800, "outputTokens": 200 },
                "model": "test/authoring",
            })
        })
        .collect();
    let cassette = json!({
        "cassetteVersion": "0.1",
        "agent": "ingot.authoring",
        "interactions": interactions,
    });

    let path = dir.join(name);
    std::fs::write(
        &path,
        serde_json::to_string_pretty(&cassette).expect("a cassette is serializable"),
    )
    .expect("writing the authoring cassette");
    path
}