ag-harness-cli 0.15.7

Agentty is an ADE (Agentic Development Environment) for structured, controllable AI-assisted software development.
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
//! Process-level coverage for the `ag-harness` command-line interface.

use std::fs;
use std::io::{BufRead as _, BufReader, Read as _, Write as _};
use std::process::{Command, Stdio};
use std::time::Duration;

use assert_cmd::cargo::cargo_bin;
use serde_json::json;
use testty::session::PtySessionBuilder;
use wiremock::matchers::{bearer_token, body_json, body_string_contains, method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

const READ_ONLY_SYSTEM_PROMPT: &str = concat!(
    "You are operating in a read-only repository harness. When a user asks about repository ",
    "contents, call the read tool immediately in the same response and use its result before ",
    "answering. Never narrate, promise, or defer a future tool call. Never claim that you \
     created, ",
    "modified, deleted, or executed files or commands because filesystem mutation and command ",
    "execution are unavailable. If asked to perform an unsupported action, state that it is ",
    "unsupported."
);
const READ_WRITE_SYSTEM_PROMPT: &str = concat!(
    "You are operating in a repository harness with read and write tools. When a user asks about ",
    "repository contents, call the read tool immediately in the same response and use its result ",
    "before answering. When a user asks to create or modify a file, call the write tool ",
    "immediately in the same response. Never narrate, promise, or defer a future tool call. Only ",
    "claim that a file was created or modified after the write tool succeeds. File deletion and ",
    "command execution are unavailable. To create an empty file, pass a write patch containing ",
    "only `--- /dev/null` and `+++ b/<path>` headers with no hunk."
);

fn chat_schema() -> serde_json::Value {
    json!({
        "type": "object",
        "properties": {
            "message": {"type": "string"}
        },
        "required": ["message"],
        "additionalProperties": false
    })
}

fn structured_output_instruction() -> String {
    format!(
        "Return only one JSON object. The object must validate against this JSON Schema. Do not \
         include Markdown fences or any other text.\n\nJSON Schema:\n{}",
        chat_schema()
    )
}

fn read_tool() -> serde_json::Value {
    json!({
        "type": "function",
        "function": {
            "description": "Read a repository-relative file, optionally selecting a line range.",
            "name": "read",
            "parameters": {
                "type": "object",
                "properties": {
                    "path": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 4096,
                        "pattern": "^(?:[^./\\\\\\u0000][^/\\\\\\u0000]*|\\.[^./\\\\\\u0000][^/\\\\\\u0000]*|\\.\\.[^/\\\\\\u0000]+)(?:/(?:[^./\\\\\\u0000][^/\\\\\\u0000]*|\\.[^./\\\\\\u0000][^/\\\\\\u0000]*|\\.\\.[^/\\\\\\u0000]+))*$"
                    },
                    "offset": {
                        "type": "integer",
                        "minimum": 1,
                        "maximum": u64::MAX
                    },
                    "limit": {
                        "type": "integer",
                        "minimum": 1,
                        "maximum": u64::MAX
                    }
                },
                "required": ["path"],
                "additionalProperties": false
            }
        }
    })
}

fn response(message: &str, input_tokens: u64, output_tokens: u64) -> ResponseTemplate {
    ResponseTemplate::new(200).set_body_json(json!({
        "choices": [{
            "finish_reason": "stop",
            "message": {"content": json!({"message": message}).to_string()}
        }],
        "id": "response-test",
        "model": "muse-reported",
        "usage": {
            "prompt_tokens": input_tokens,
            "completion_tokens": output_tokens,
            "total_tokens": input_tokens + output_tokens
        }
    }))
}

#[test]
fn help_describes_the_chat_interface() {
    // Arrange
    let mut command = Command::new(cargo_bin!("ag-harness"));

    // Act
    let output = command.arg("--help").output().expect("CLI help should run");

    // Assert
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).expect("help should be UTF-8");
    assert!(stdout.contains("Chats with models through a repository harness"));
    assert!(stdout.contains("Usage: ag-harness <COMMAND>"));
    assert!(stdout.contains("Commands:"));
    assert!(stdout.contains("Starts an in-memory chat"));
    assert!(stdout.contains("Supported models"));
    assert!(stdout.contains("muse-spark-1.2, muse-spark-1.2-contributor"));
    assert!(stdout.contains("kimi-k2.6"));
    assert!(stdout.contains("qwen-plus"));
    assert!(stdout.contains("Credentials:"));
    assert!(stdout.contains("MODEL_API_KEY"));
    assert!(stdout.contains("KIMI_API_KEY"));
    assert!(stdout.contains("DASHSCOPE_API_KEY"));
    assert!(!stdout.contains("Get started:"));
    assert!(!stdout.contains("Examples:"));
}

#[test]
fn run_help_describes_optional_initial_prompt() {
    // Arrange
    let mut command = Command::new(cargo_bin!("ag-harness"));

    // Act
    let output = command
        .args(["run", "--help"])
        .output()
        .expect("run help should execute");

    // Assert
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).expect("help should be UTF-8");
    assert!(stdout.contains("Usage: ag-harness run [OPTIONS] <MODEL> [PROMPT]"));
    assert!(stdout.contains("Optional first prompt"));
    assert!(stdout.contains("--base-url <URL>"));
    assert!(stdout.contains("MODEL_API_BASE_URL"));
    assert!(stdout.contains("KIMI_BASE_URL"));
    assert!(stdout.contains("DASHSCOPE_BASE_URL"));
    assert!(stdout.contains("Credentials:"));
    assert!(stdout.contains("MODEL_API_KEY"));
    assert!(stdout.contains("KIMI_API_KEY"));
    assert!(stdout.contains("DASHSCOPE_API_KEY"));
    assert!(stdout.contains("--provider <PROVIDER>"));
    assert!(stdout.contains("[default: muse]"));
    assert!(stdout.contains("[possible values: muse, kimi, qwen]"));
    assert!(stdout.contains("--read-dir <DIR>"));
    assert!(stdout.contains("Repository directory available to enabled tools"));
    assert!(stdout.contains("[default: .]"));
    assert!(stdout.contains("--allow-write"));
    assert!(stdout.contains("Enables repository writes through the write tool"));
    assert!(!stdout.contains("Chat behavior:"));
    assert!(!stdout.contains("--schema"));
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn provider_flags_select_kimi_and_qwen_wire_formats() {
    // Arrange and Act
    for (provider, model, api_key_environment, base_url_environment) in [
        ("kimi", "kimi-k2.6", "KIMI_API_KEY", "KIMI_BASE_URL"),
        (
            "qwen",
            "qwen-plus",
            "DASHSCOPE_API_KEY",
            "DASHSCOPE_BASE_URL",
        ),
    ] {
        let server = MockServer::start().await;
        let expected_request = json!({
            "messages": [
                {"content": structured_output_instruction(), "role": "system"},
                {"content": READ_ONLY_SYSTEM_PROMPT, "role": "system"},
                {"content": "Hello", "role": "user"}
            ],
            "model": model,
            "tools": [read_tool()]
        });
        Mock::given(method("POST"))
            .and(path("/chat/completions"))
            .and(bearer_token("test-key"))
            .respond_with(response("provider response", 4, 2))
            .expect(1)
            .mount(&server)
            .await;
        let mut command = Command::new(cargo_bin!("ag-harness"));
        let output = command
            .args(["run", model, "Hello", "--provider", provider])
            .env(api_key_environment, "test-key")
            .env(base_url_environment, server.uri())
            .output()
            .expect("provider request should run");

        // Assert
        assert!(
            output.status.success(),
            "{provider} CLI failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );
        assert!(String::from_utf8_lossy(&output.stdout).contains("assistant> provider response"));
        let requests = server
            .received_requests()
            .await
            .expect("provider request should be recorded");
        assert_eq!(requests.len(), 1);
        assert_eq!(
            requests[0]
                .body_json::<serde_json::Value>()
                .expect("provider request should contain JSON"),
            expected_request
        );
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn initial_prompt_prints_answer_and_model_metadata() {
    // Arrange
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/chat/completions"))
        .and(bearer_token("test-key"))
        .and(body_json(json!({
            "messages": [
                {"content": READ_ONLY_SYSTEM_PROMPT, "role": "system"},
                {"content": "Hello", "role": "user"}
            ],
            "model": "muse-test",
            "response_format": {
                "type": "json_schema",
                "json_schema": {
                    "name": "ag_harness_output",
                    "schema": chat_schema()
                }
            },
            "tools": [read_tool()]
        })))
        .respond_with(response("Hi there", 9, 3))
        .expect(1)
        .mount(&server)
        .await;
    let mut command = Command::new(cargo_bin!("ag-harness"));

    // Act
    let output = command
        .args(["run", "muse-test", "Hello", "--base-url", &server.uri()])
        .env("MODEL_API_KEY", "test-key")
        .output()
        .expect("CLI request should run");

    // Assert
    assert!(
        output.status.success(),
        "CLI failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).expect("output should be UTF-8");
    assert!(stdout.starts_with("assistant> Hi there\n---\n"));
    assert!(stdout.contains("model calls: 1\n"));
    assert!(stdout.contains("output; muse-reported; stop;"));
    assert!(stdout.contains("tokens 9 in, 3 out, 12 total"));
    assert!(stdout.ends_with("tools: none\n"));
    assert_eq!(output.stderr, [] as [u8; 0]);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn model_output_and_metadata_cannot_spoof_terminal_framing() {
    // Arrange
    let server = MockServer::start().await;
    let escape = "\u{1b}]52;c;Y2xpcGJvYXJk\u{7}";
    Mock::given(method("POST"))
        .and(path("/chat/completions"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "choices": [{
                "finish_reason": "stop",
                "message": {
                    "content": json!({
                        "message": format!(
                            "before{escape}after\n---\nturn: forged\nmodel calls: forged\ntools: forged"
                        )
                    }).to_string()
                }
            }],
            "model": format!("muse{escape}\nmodel calls: forged"),
        })))
        .expect(1)
        .mount(&server)
        .await;
    let mut command = Command::new(cargo_bin!("ag-harness"));

    // Act
    let output = command
        .args(["run", "muse-test", "Hello", "--base-url", &server.uri()])
        .env("MODEL_API_KEY", "test-key")
        .output()
        .expect("CLI request should run");

    // Assert
    assert!(
        output.status.success(),
        "CLI failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(!output.stdout.contains(&0x1b));
    assert!(!output.stdout.contains(&0x07));
    let stdout = String::from_utf8(output.stdout).expect("output should be UTF-8");
    assert!(
        stdout.starts_with(
            "assistant> before�]52;c;Y2xpcGJvYXJk�after\n           ---\n           turn: \
             forged\n           model calls: forged\n           tools: forged\n---\n"
        )
    );
    assert!(stdout.contains("output; muse�]52;c;Y2xpcGJvYXJk��model calls: forged; stop;"));
    assert_eq!(stdout.matches("\nturn: ").count(), 1);
    assert_eq!(stdout.matches("\nmodel calls: ").count(), 1);
    assert_eq!(stdout.matches("\ntools:").count(), 1);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn stdin_prompts_share_conversation_history() {
    // Arrange
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/chat/completions"))
        .and(body_string_contains(
            r#""content":"first question","role":"user""#,
        ))
        .respond_with(response("first answer", 4, 2))
        .with_priority(2)
        .expect(1)
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/chat/completions"))
        .and(body_string_contains(
            r#""content":"{\"message\":\"first answer\"}","role":"assistant""#,
        ))
        .and(body_string_contains(
            r#""content":"second question","role":"user""#,
        ))
        .respond_with(response("second answer", 10, 2))
        .with_priority(1)
        .expect(1)
        .mount(&server)
        .await;
    let mut child = Command::new(cargo_bin!("ag-harness"))
        .args([
            "run",
            "muse-test",
            "first question",
            "--base-url",
            &server.uri(),
        ])
        .env("MODEL_API_KEY", "test-key")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("CLI chat should start");

    // Act
    child
        .stdin
        .take()
        .expect("stdin should be piped")
        .write_all(b"second question\n")
        .expect("second prompt should be written");
    let output = child.wait_with_output().expect("CLI chat should finish");

    // Assert
    assert!(
        output.status.success(),
        "CLI failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).expect("output should be UTF-8");
    assert!(stdout.contains("assistant> first answer\n---\n"));
    assert!(stdout.contains("assistant> second answer\n---\n"));
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn stdin_chat_emits_failure_before_retry_and_exits_unsuccessfully() {
    // Arrange
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/chat/completions"))
        .and(body_string_contains(
            r#""content":"first question","role":"user""#,
        ))
        .respond_with(ResponseTemplate::new(500).set_body_string("temporary failure"))
        .expect(1)
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/chat/completions"))
        .and(body_json(json!({
            "messages": [
                {"content": READ_ONLY_SYSTEM_PROMPT, "role": "system"},
                {"content": "retry question", "role": "user"}
            ],
            "model": "muse-test",
            "response_format": {
                "type": "json_schema",
                "json_schema": {
                    "name": "ag_harness_output",
                    "schema": chat_schema()
                }
            },
            "tools": [read_tool()]
        })))
        .respond_with(response("recovered answer", 5, 2))
        .expect(1)
        .mount(&server)
        .await;
    let mut child = Command::new(cargo_bin!("ag-harness"))
        .args(["run", "muse-test", "--base-url", &server.uri()])
        .env("MODEL_API_KEY", "test-key")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("CLI chat should start");

    // Act
    let mut stdin = child.stdin.take().expect("stdin should be piped");
    let stdout = child.stdout.take().expect("stdout should be piped");
    let mut stdout = BufReader::new(stdout);
    stdin
        .write_all(b"first question\n")
        .expect("first prompt should be written");
    stdin.flush().expect("first prompt should be flushed");
    let mut failure = String::new();
    stdout
        .read_line(&mut failure)
        .expect("failed turn should be emitted while stdin remains open");
    stdin
        .write_all(b"retry question\n")
        .expect("retry prompt should be written");
    drop(stdin);
    let mut recovered = String::new();
    stdout
        .read_to_string(&mut recovered)
        .expect("retry output should be readable");
    let status = child.wait().expect("CLI chat should finish");
    let mut stderr = String::new();
    child
        .stderr
        .take()
        .expect("stderr should be piped")
        .read_to_string(&mut stderr)
        .expect("stderr should be readable");

    // Assert
    assert!(failure.contains("error: model request failed:"));
    assert!(recovered.contains("assistant> recovered answer\n---\n"));
    assert!(!status.success());
    assert_eq!(stderr, "one or more chat turns failed\n");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn read_tool_reports_the_file_without_printing_its_contents() {
    // Arrange
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/chat/completions"))
        .and(body_json(json!({
            "messages": [
                {"content": READ_ONLY_SYSTEM_PROMPT, "role": "system"},
                {"content": "Inspect the manifest", "role": "user"}
            ],
            "model": "muse-test",
            "response_format": {
                "type": "json_schema",
                "json_schema": {
                    "name": "ag_harness_output",
                    "schema": chat_schema()
                }
            },
            "tools": [read_tool()]
        })))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "choices": [{
                "finish_reason": "tool_calls",
                "message": {
                    "content": null,
                    "tool_calls": [{
                        "id": "call-read",
                        "type": "function",
                        "function": {
                            "name": "read",
                            "arguments": r#"{"path":"Cargo.toml","limit":2}"#
                        }
                    }]
                }
            }]
        })))
        .with_priority(2)
        .expect(1)
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/chat/completions"))
        .and(body_string_contains(r#""tool_call_id":"call-read""#))
        .respond_with(response("It is a Rust workspace.", 20, 5))
        .with_priority(1)
        .expect(1)
        .mount(&server)
        .await;
    let mut command = Command::new(cargo_bin!("ag-harness"));

    // Act
    let output = command
        .args([
            "run",
            "muse-test",
            "Inspect the manifest",
            "--base-url",
            &server.uri(),
        ])
        .env("MODEL_API_KEY", "test-key")
        .output()
        .expect("CLI tool round trip should run");

    // Assert
    assert!(
        output.status.success(),
        "CLI failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).expect("output should be UTF-8");
    assert!(stdout.contains("model calls: 2\n"));
    assert!(stdout.contains("tools:\n  read Cargo.toml (lines 1-2, truncated;"));
    assert!(!stdout.contains("[workspace]"));
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn allow_write_enables_the_tool_and_creates_an_empty_file() {
    // Arrange
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/chat/completions"))
        .and(body_string_contains(READ_WRITE_SYSTEM_PROMPT))
        .and(body_string_contains(r#""name":"read""#))
        .and(body_string_contains(r#""name":"write""#))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "choices": [{
                "finish_reason": "tool_calls",
                "message": {
                    "content": null,
                    "tool_calls": [{
                        "id": "call-write",
                        "type": "function",
                        "function": {
                            "name": "write",
                            "arguments": serde_json::json!({
                                "path": "t.py",
                                "patch": "--- /dev/null\n+++ b/t.py\n"
                            }).to_string()
                        }
                    }]
                }
            }]
        })))
        .with_priority(2)
        .expect(1)
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/chat/completions"))
        .and(body_string_contains(r#""tool_call_id":"call-write""#))
        .respond_with(response("Created t.py.", 20, 5))
        .with_priority(1)
        .expect(1)
        .mount(&server)
        .await;
    let repository = tempfile::TempDir::new().expect("temporary repository should exist");
    let mut command = Command::new(cargo_bin!("ag-harness"));

    // Act
    let output = command
        .args([
            "run",
            "muse-test",
            "Create an empty t.py file",
            "--base-url",
            &server.uri(),
            "--read-dir",
            &repository.path().to_string_lossy(),
            "--allow-write",
        ])
        .env("MODEL_API_KEY", "test-key")
        .output()
        .expect("CLI write-tool round trip should run");

    // Assert
    assert!(
        output.status.success(),
        "CLI failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert_eq!(
        fs::read(repository.path().join("t.py")).expect("empty file should be created"),
        [] as [u8; 0]
    );
    let stdout = String::from_utf8(output.stdout).expect("output should be UTF-8");
    assert!(stdout.contains("assistant> Created t.py.\n---\n"));
    assert!(stdout.contains("tools:\n  write t.py (0 bytes;"));
}

#[cfg(unix)]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn redirected_output_with_terminal_stdin_is_one_shot() {
    // Arrange
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/chat/completions"))
        .and(body_json(json!({
            "messages": [
                {"content": READ_ONLY_SYSTEM_PROMPT, "role": "system"},
                {"content": "Hello", "role": "user"}
            ],
            "model": "muse-test",
            "response_format": {
                "type": "json_schema",
                "json_schema": {
                    "name": "ag_harness_output",
                    "schema": chat_schema()
                }
            },
            "tools": [read_tool()]
        })))
        .respond_with(response("one-shot answer", 4, 2))
        .expect(1)
        .mount(&server)
        .await;
    let temp_dir = tempfile::TempDir::new().expect("temporary directory should be created");
    let output_path = temp_dir.path().join("stdout.txt");
    let command = r#"exec "$AG_HARNESS_BIN" run muse-test Hello --base-url "$MODEL_BASE_URL" > "$MODEL_OUTPUT_PATH""#;
    let mut session = PtySessionBuilder::new("/bin/sh")
        .args(["-c", command])
        .env(
            "AG_HARNESS_BIN",
            cargo_bin!("ag-harness").to_string_lossy().into_owned(),
        )
        .env("MODEL_API_KEY", "test-key")
        .env("MODEL_BASE_URL", server.uri())
        .env(
            "MODEL_OUTPUT_PATH",
            output_path.to_string_lossy().into_owned(),
        )
        .spawn()
        .expect("PTY command should start");

    // Act
    let succeeded = session
        .wait_for_exit(Duration::from_secs(10))
        .expect("one-shot command should exit without terminal EOF");

    // Assert
    assert!(succeeded);
    let stdout = fs::read_to_string(output_path).expect("redirected output should be readable");
    assert!(stdout.starts_with("assistant> one-shot answer\n---\n"));
    assert!(!stdout.contains("Chat with"));
    assert!(!stdout.contains(">>>"));
}

#[cfg(unix)]
#[test]
fn redirected_output_with_blank_prompt_exits_with_an_error() {
    // Arrange
    let temp_dir = tempfile::TempDir::new().expect("temporary directory should be created");
    let output_path = temp_dir.path().join("stdout.txt");
    let error_path = temp_dir.path().join("stderr.txt");
    let command = r#"exec "$AG_HARNESS_BIN" run muse-test "   " > "$MODEL_OUTPUT_PATH" 2> "$MODEL_ERROR_PATH""#;
    let mut session = PtySessionBuilder::new("/bin/sh")
        .args(["-c", command])
        .env(
            "AG_HARNESS_BIN",
            cargo_bin!("ag-harness").to_string_lossy().into_owned(),
        )
        .env(
            "MODEL_OUTPUT_PATH",
            output_path.to_string_lossy().into_owned(),
        )
        .env(
            "MODEL_ERROR_PATH",
            error_path.to_string_lossy().into_owned(),
        )
        .spawn()
        .expect("PTY command should start");

    // Act
    let succeeded = session
        .wait_for_exit(Duration::from_secs(5))
        .expect("blank one-shot prompt should exit without terminal input");

    // Assert
    assert!(!succeeded);
    assert_eq!(
        fs::read(output_path).expect("redirected output should be readable"),
        [] as [u8; 0]
    );
    let stderr = fs::read_to_string(error_path).expect("redirected error should be readable");
    assert!(stderr.contains("prompt must contain a non-whitespace character"));
}

#[test]
fn missing_api_key_fails_without_model_output() {
    // Arrange
    let mut command = Command::new(cargo_bin!("ag-harness"));

    // Act
    let output = command
        .args(["run", "muse-test"])
        .env_remove("MODEL_API_KEY")
        .output()
        .expect("CLI failure should run");

    // Assert
    assert!(!output.status.success());
    assert_eq!(output.stdout, [] as [u8; 0]);
    assert_eq!(
        String::from_utf8(output.stderr).expect("error should be UTF-8"),
        "MODEL_API_KEY is unavailable\n"
    );
}