locode-exec 0.1.6

Minimal headless runner for the locode agent engine - one JSON report on stdout
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
//! End-to-end CLI tests (Task 14 acceptance, plan ยง6): drive the built binary
//! with the mock provider โ€” keyless, CI-safe โ€” and assert the stdout contract,
//! stream reconstruction, stderr discipline, and exit codes.

use assert_cmd::Command;
use locode_core::{Event, Report, Status, reconstruct_conversation};
use predicates::prelude::*;

fn exec() -> Command {
    let mut cmd =
        Command::cargo_bin("locode-exec").unwrap_or_else(|e| panic!("binary builds: {e}"));
    // Hermetic: never inherit a real key/base-url/schema from the dev env.
    cmd.env_remove("LOCODE_API_KEY")
        .env_remove("LOCODE_BASE_URL")
        .env_remove("LOCODE_MODEL")
        .env_remove("LOCODE_API_SCHEMA")
        .env_remove("LOCODE_MOCK_SCRIPT")
        .env_remove("RUST_LOG");
    cmd
}

fn tempdir() -> tempfile::TempDir {
    tempfile::tempdir().unwrap_or_else(|e| panic!("tempdir: {e}"))
}

#[test]
fn mock_json_is_exactly_one_parseable_report() {
    let dir = tempdir();
    let assert = exec()
        .args(["say hi", "--api-schema", "mock", "--cwd"])
        .arg(dir.path())
        .assert()
        .success();
    let stdout = String::from_utf8(assert.get_output().stdout.clone()).expect("utf8");
    let lines: Vec<&str> = stdout.lines().collect();
    assert_eq!(lines.len(), 1, "exactly one stdout line: {stdout:?}");
    let report: Report = serde_json::from_str(lines[0]).expect("parses as Report");
    assert_eq!(report.status, Status::Completed);
    assert_eq!(report.harness, "grok");
    assert_eq!(report.api_schema, "mock");
    assert_eq!(report.final_message.as_deref(), Some("Mock run complete."));
    assert_eq!(report.schema_version, 1);
}

#[test]
fn text_mode_prints_final_message_only() {
    let dir = tempdir();
    exec()
        .args([
            "say hi",
            "--api-schema",
            "mock",
            "--output-format",
            "text",
            "--cwd",
        ])
        .arg(dir.path())
        .assert()
        .success()
        .stdout("Mock run complete.\n");
}

#[test]
fn stream_json_is_valid_jsonl_and_reconstructs() {
    let dir = tempdir();
    let assert = exec()
        .args([
            "say hi",
            "--api-schema",
            "mock",
            "--output-format",
            "stream-json",
            "--cwd",
        ])
        .arg(dir.path())
        .assert()
        .success();
    let stdout = String::from_utf8(assert.get_output().stdout.clone()).expect("utf8");
    let events: Vec<Event> = stdout
        .lines()
        .map(|l| serde_json::from_str(l).unwrap_or_else(|e| panic!("bad event line {l:?}: {e}")))
        .collect();
    assert!(events.len() >= 3, "init + message(s) + result");

    // First event: init, carrying the full preamble + the grok tool specs.
    match &events[0] {
        Event::Init {
            preamble, tools, ..
        } => {
            assert!(!preamble.is_empty(), "init carries the preamble");
            assert_eq!(tools.len(), 5, "the grok pack's five tools");
        }
        other => panic!("first event must be init, got {other:?}"),
    }
    // Last event: result, with the same Report shape as json mode.
    match events.last().expect("non-empty") {
        Event::Result { report } => assert_eq!(report.status, Status::Completed),
        other => panic!("last event must be result, got {other:?}"),
    }
    // The stream is self-sufficient: the full conversation rebuilds from it.
    let conversation = reconstruct_conversation(&events);
    assert!(
        conversation.messages.len() >= 3,
        "system + user_info + user prompt + assistant: {}",
        conversation.messages.len()
    );
}

#[test]
fn logs_go_to_stderr_never_stdout() {
    let dir = tempdir();
    let assert = exec()
        .args(["say hi", "--api-schema", "mock", "--cwd"])
        .arg(dir.path())
        .env("RUST_LOG", "debug")
        .assert()
        .success();
    let stdout = String::from_utf8(assert.get_output().stdout.clone()).expect("utf8");
    assert_eq!(stdout.lines().count(), 1, "stdout stays a single JSON doc");
    serde_json::from_str::<Report>(stdout.trim()).expect("still parses");
}

#[test]
fn anthropic_without_key_fails_before_running() {
    let dir = tempdir();
    exec()
        .args(["say hi", "--cwd"]) // default --api-schema anthropic
        .arg(dir.path())
        .assert()
        .code(1)
        .stdout("") // no partial report
        .stderr(predicate::str::contains("error:"));
}

#[test]
fn unknown_harness_is_a_clean_usage_error() {
    exec()
        .args(["say hi", "--harness", "bogus"])
        .assert()
        .code(2)
        .stdout("")
        .stderr(predicate::str::contains("grok"));
}

#[test]
fn empty_prompt_is_an_error() {
    let dir = tempdir();
    exec()
        .args(["--api-schema", "mock", "--cwd"])
        .arg(dir.path())
        .write_stdin("")
        .assert()
        .code(1)
        .stderr(predicate::str::contains("no prompt"));
}

#[test]
fn prompt_reads_from_stdin_when_dash() {
    let dir = tempdir();
    exec()
        .args([
            "-",
            "--api-schema",
            "mock",
            "--output-format",
            "text",
            "--cwd",
        ])
        .arg(dir.path())
        .write_stdin("from stdin\n")
        .assert()
        .success()
        .stdout("Mock run complete.\n");
}

#[test]
fn mock_script_env_overrides_the_default_turn() {
    let dir = tempdir();
    exec()
        .args([
            "say hi",
            "--api-schema",
            "mock",
            "--output-format",
            "text",
            "--cwd",
        ])
        .arg(dir.path())
        .env("LOCODE_MOCK_SCRIPT", r#"[{"text": "scripted answer"}]"#)
        .assert()
        .success()
        .stdout("scripted answer\n");
}

#[test]
fn malformed_mock_script_fails_pre_run() {
    let dir = tempdir();
    exec()
        .args(["say hi", "--api-schema", "mock", "--cwd"])
        .arg(dir.path())
        .env("LOCODE_MOCK_SCRIPT", "not json")
        .assert()
        .code(1)
        .stdout("")
        .stderr(predicate::str::contains("LOCODE_MOCK_SCRIPT"));
}

#[test]
fn strip_identity_removes_grok_from_the_stream() {
    let dir = tempdir();
    let assert = exec()
        .args([
            "say hi",
            "--api-schema",
            "mock",
            "--output-format",
            "stream-json",
            "--strip-identity",
            "--cwd",
        ])
        .arg(dir.path())
        .assert()
        .success();
    let stdout = String::from_utf8(assert.get_output().stdout.clone()).expect("utf8");
    let init_line = stdout.lines().next().expect("init event");
    assert!(
        !init_line.contains("released by xAI"),
        "identity sentence stripped from the preamble"
    );
}

// ==================== SIGTERM (ADR-0018 / Task 21 โ†’ Task 24) ====================
//
// These drive the real binary with an env-scripted mock: the model "asks" for
// `run_terminal_cmd {command: "sleep 30"}`, which holds the run open until the
// test SIGTERMs the process โ€” exercising signal โ†’ cancel handle โ†’ cooperative
// tool cancel โ†’ synthetic pairing โ†’ cancelled report, end to end.
#[cfg(unix)]
mod sigterm {
    use super::*;
    use std::io::{BufRead, BufReader};
    use std::process::{Child, Command as StdCommand, Stdio};
    use std::time::{Duration, Instant};

    /// The script that holds a run open: one slow shell turn, then a text turn
    /// the run never reaches when cancelled.
    const SLOW_SCRIPT: &str = r#"[
        {"tool": "run_terminal_cmd",
         "input": {"command": "sleep 30",
                   "description": "hold the run open for the SIGTERM test"}},
        {"text": "never reached"}
    ]"#;

    fn spawn_exec(dir: &tempfile::TempDir, extra_args: &[&str], script: Option<&str>) -> Child {
        let mut cmd = StdCommand::new(assert_cmd::cargo::cargo_bin("locode-exec"));
        cmd.args(["--api-schema", "mock", "--cwd"])
            .arg(dir.path())
            .args(extra_args)
            .env_remove("LOCODE_API_KEY")
            .env_remove("LOCODE_BASE_URL")
            .env_remove("LOCODE_MODEL")
            .env_remove("LOCODE_API_SCHEMA")
            .env_remove("LOCODE_MOCK_SCRIPT")
            .env_remove("RUST_LOG")
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());
        if let Some(script) = script {
            cmd.env("LOCODE_MOCK_SCRIPT", script);
        }
        cmd.spawn()
            .unwrap_or_else(|e| panic!("spawn locode-exec: {e}"))
    }

    fn sigterm(child: &Child) {
        let status = StdCommand::new("kill")
            .args(["-TERM", &child.id().to_string()])
            .status()
            .unwrap_or_else(|e| panic!("run kill: {e}"));
        assert!(status.success(), "kill -TERM failed");
    }

    /// Wait for exit with a hard cap so a regression can't hang the suite.
    fn wait_capped(child: &mut Child, cap: Duration) -> std::process::ExitStatus {
        let start = Instant::now();
        loop {
            if let Some(status) = child.try_wait().unwrap_or_else(|e| panic!("try_wait: {e}")) {
                return status;
            }
            assert!(
                start.elapsed() < cap,
                "locode-exec did not exit within {cap:?} after SIGTERM"
            );
            std::thread::sleep(Duration::from_millis(50));
        }
    }

    #[test]
    fn sigterm_mid_run_stream_json_ends_in_a_cancelled_result() {
        let dir = tempdir();
        let mut child = spawn_exec(
            &dir,
            &["say hi", "--output-format", "stream-json"],
            Some(SLOW_SCRIPT),
        );

        // Read the live stream until the assistant's tool_use turn appears โ€”
        // dispatch (and the 30s sleep) starts right after it.
        let stdout = child.stdout.take().expect("stdout piped");
        let mut reader = BufReader::new(stdout);
        let mut lines: Vec<String> = Vec::new();
        loop {
            let mut line = String::new();
            let n = reader.read_line(&mut line).expect("read stream line");
            assert!(n > 0, "stream ended before the tool_use turn: {lines:?}");
            let event: Event = serde_json::from_str(line.trim()).expect("valid event line");
            let is_tool_turn = matches!(
                &event,
                Event::Message { message }
                    if message.content.iter().any(|b| matches!(
                        b, locode_core::ContentBlock::ToolUse { .. }))
            );
            lines.push(line.trim().to_string());
            if is_tool_turn {
                break;
            }
        }
        // Give dispatch a beat to spawn the sleep, then SIGTERM.
        std::thread::sleep(Duration::from_millis(300));
        sigterm(&child);

        // Drain the rest of the stream to EOF, then reap the exit status.
        let mut rest = String::new();
        std::io::Read::read_to_string(&mut reader, &mut rest).expect("drain stream");
        lines.extend(rest.lines().map(|l| l.trim().to_string()));
        let status = wait_capped(&mut child, Duration::from_secs(20));

        // Exit 0: cancelled is a structured terminal state.
        assert_eq!(status.code(), Some(0), "exit code; stream: {lines:?}");

        // The tail stays valid JSONL ending in a cancelled `result`.
        let events: Vec<Event> = lines
            .iter()
            .map(|l| serde_json::from_str(l).unwrap_or_else(|e| panic!("bad line {l:?}: {e}")))
            .collect();
        match events.last().expect("non-empty stream") {
            Event::Result { report } => {
                assert_eq!(report.status, Status::Cancelled);
                assert_eq!(report.error, None);
            }
            other => panic!("last event must be result, got {other:?}"),
        }

        // Transcript validity: every tool_use id is answered by a tool_result.
        let conversation = reconstruct_conversation(&events);
        let mut used: Vec<String> = Vec::new();
        let mut answered: Vec<String> = Vec::new();
        for message in &conversation.messages {
            for block in &message.content {
                match block {
                    locode_core::ContentBlock::ToolUse { id, .. } => used.push(id.clone()),
                    locode_core::ContentBlock::ToolResult { tool_use_id, .. } => {
                        answered.push(tool_use_id.clone());
                    }
                    _ => {}
                }
            }
        }
        assert!(!used.is_empty(), "the slow tool call is in the transcript");
        for id in &used {
            assert!(answered.contains(id), "unpaired tool_use {id}: {events:?}");
        }
    }

    #[test]
    fn sigterm_mid_run_json_still_emits_one_report() {
        let dir = tempdir();
        let mut child = spawn_exec(&dir, &["say hi"], Some(SLOW_SCRIPT));

        // json mode is silent until the end โ€” no stream to key off; the 30s
        // sleep leaves a wide window for a fixed grace period.
        std::thread::sleep(Duration::from_secs(2));
        sigterm(&child);
        let status = wait_capped(&mut child, Duration::from_secs(20));

        let mut stdout = String::new();
        std::io::Read::read_to_string(child.stdout.as_mut().expect("stdout"), &mut stdout)
            .expect("read stdout");
        assert_eq!(status.code(), Some(0), "stdout: {stdout:?}");
        let lines: Vec<&str> = stdout.lines().collect();
        assert_eq!(lines.len(), 1, "exactly one report line: {stdout:?}");
        let report: Report = serde_json::from_str(lines[0]).expect("parses as Report");
        assert_eq!(report.status, Status::Cancelled);
        assert_eq!(report.error, None);
        assert_eq!(report.schema_version, 1);
    }

    #[test]
    fn sigterm_before_the_run_exits_1_with_empty_stdout() {
        let dir = tempdir();
        // No positional prompt + stdin held open: the binary blocks pre-run
        // reading the prompt from stdin.
        let mut child = spawn_exec(&dir, &[], None);
        std::thread::sleep(Duration::from_millis(500));
        sigterm(&child);
        let status = wait_capped(&mut child, Duration::from_secs(20));

        let mut stdout = String::new();
        std::io::Read::read_to_string(child.stdout.as_mut().expect("stdout"), &mut stdout)
            .expect("read stdout");
        let mut stderr = String::new();
        std::io::Read::read_to_string(child.stderr.as_mut().expect("stderr"), &mut stderr)
            .expect("read stderr");
        assert_eq!(status.code(), Some(1), "stderr: {stderr:?}");
        assert_eq!(stdout, "", "nothing on stdout pre-run");
        assert!(
            stderr.contains("SIGTERM before the run started"),
            "stderr names the cause: {stderr:?}"
        );
    }
}