koda-cli 0.2.24

A high-performance AI coding agent for macOS and Linux
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
422
//! Smoke tests: headless mode with MockProvider.
//!
//! These tests exercise the full binary pipeline without a real LLM.
//! They run `koda -p "..." --provider mock --output-format json`
//! with scripted responses via KODA_MOCK_RESPONSES env var.
//!
//! CI-safe: no network, no API keys, no LLM required.

use std::process::Command;

// ── Helpers ─────────────────────────────────────────────────

fn koda_bin() -> String {
    let mut path = std::env::current_exe().unwrap();
    path.pop(); // test binary name
    path.pop(); // deps/
    path.push("koda");
    path.to_string_lossy().to_string()
}

fn run_mock(prompt: &str, responses: &str) -> (String, String, bool) {
    run_mock_with_mode(prompt, responses, None)
}

fn run_mock_with_mode(prompt: &str, responses: &str, mode: Option<&str>) -> (String, String, bool) {
    let tmp = tempfile::tempdir().unwrap();
    let mut cmd = Command::new(koda_bin());
    cmd.args([
        "-p",
        prompt,
        "--provider",
        "mock",
        "--output-format",
        "json",
        "--project-root",
    ])
    .arg(tmp.path())
    .env("XDG_CONFIG_HOME", tmp.path())
    .env("KODA_MOCK_RESPONSES", responses);
    if let Some(m) = mode {
        cmd.args(["--mode", m]);
    }
    let output = cmd.output().expect("Failed to run koda");

    let stdout = String::from_utf8_lossy(&output.stdout).to_string();
    let stderr = String::from_utf8_lossy(&output.stderr).to_string();
    (stdout, stderr, output.status.success())
}

fn extract_json(stdout: &str) -> serde_json::Value {
    // The JSON is pretty-printed across multiple lines.
    // Find the opening '{' and collect everything from there.
    let start = stdout
        .find('{')
        .unwrap_or_else(|| panic!("No JSON object in stdout:\n{stdout}"));
    serde_json::from_str(&stdout[start..])
        .unwrap_or_else(|e| panic!("Invalid JSON: {e}\nfrom: {}", &stdout[start..]))
}
// ── Headless MockProvider tests ──────────────────────────────

#[test]
fn mock_text_response_returns_json() {
    let (stdout, stderr, success) = run_mock("say hi", r#"[{"text":"Hello from mock!"}]"#);
    assert!(success, "Process failed.\nstderr: {stderr}");
    let json = extract_json(&stdout);
    assert_eq!(json["success"], true);
    let response = json["response"].as_str().unwrap_or("");
    assert!(
        response.contains("Hello from mock"),
        "Expected 'Hello from mock' in response, got: {response}"
    );
}

// ── Headless safety: destructive commands auto-approved in sandbox (#855) ────

#[test]
fn mock_destructive_bash_auto_approved_in_headless() {
    // Model tries to run `rm -rf /tmp/nonexistent_test_dir` — with `--mode auto`,
    // headless auto-approves all actions within the project sandbox (#855).
    // The sandbox kernel enforcement is the safety boundary, not the approval prompt.
    //
    // Note: pre-#982 this test passed without `--mode auto` because headless
    // hardcoded `TrustMode::Auto` regardless of the flag. Post-fix, the flag
    // must be passed explicitly — which is the entire point of having a flag.
    let responses = r#"[
        {"tool":"Bash","args":{"command":"rm -rf /tmp/nonexistent_test_dir"}},
        {"text":"Done."}
    ]"#;

    let (_stdout, stderr, success) =
        run_mock_with_mode("delete everything", responses, Some("auto"));
    assert!(
        success,
        "Process should succeed (Auto mode approves within sandbox).\nstderr: {stderr}"
    );
    // In Auto mode, destructive actions should NOT be rejected
    assert!(
        !stderr.contains("Rejected destructive action"),
        "Should not reject destructive action in Auto mode.\nstderr: {stderr}"
    );
}

#[test]
fn mock_destructive_bash_rejected_in_safe_mode_in_headless() {
    // End-to-end regression test for #982. Pre-fix, headless hardcoded
    // `TrustMode::Auto`, so `--mode safe` was silently ignored and destructive
    // actions ran without confirmation. Post-fix, `--mode safe` is honored
    // and the same destructive Bash invocation is rejected by the trust layer.
    //
    // We test BOTH: explicit `--mode safe` AND default (no flag, since CLI
    // default_value = "safe"). Both must reject.
    let responses = r#"[
        {"tool":"Bash","args":{"command":"rm -rf /tmp/nonexistent_test_dir"}},
        {"text":"Done."}
    ]"#;

    for mode in [Some("safe"), None] {
        let label = mode.unwrap_or("<default>");
        let (_stdout, stderr, _success) = run_mock_with_mode("delete everything", responses, mode);
        assert!(
            stderr.contains("Rejected destructive action"),
            "[mode={label}] Safe mode (or default) MUST reject destructive Bash. \
             If this fails, #982 has regressed.\nstderr: {stderr}"
        );
    }
}

#[test]
fn mock_empty_responses_succeeds() {
    let (stdout, stderr, success) = run_mock("say hi", "[]");
    assert!(success, "Process failed.\nstderr: {stderr}");
    let json = extract_json(&stdout);
    assert_eq!(json["success"], true);
}

#[test]
fn mock_tool_use_read_file() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("hello.txt"), "mock test content").unwrap();

    let responses = r#"[
        {"tool":"Read","args":{"path":"hello.txt"}},
        {"text":"I read the file."}
    ]"#;

    let output = Command::new(koda_bin())
        .args([
            "-p",
            "read hello.txt",
            "--provider",
            "mock",
            "--output-format",
            "json",
            "--project-root",
        ])
        .arg(tmp.path())
        .env("XDG_CONFIG_HOME", tmp.path())
        .env("KODA_MOCK_RESPONSES", responses)
        .output()
        .expect("Failed to run koda");

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

    let json = extract_json(&stdout);
    assert_eq!(json["success"], true);
    let response = json["response"].as_str().unwrap_or("");
    assert!(
        response.contains("read the file"),
        "Expected tool result in response, got: {response}\nstderr: {stderr}"
    );
}

#[test]
fn mock_error_response_handled() {
    let (stdout, _stderr, _success) = run_mock("say hi", r#"[{"error":"Simulated LLM failure"}]"#);
    let json = extract_json(&stdout);
    // Provider error → success=false or empty response
    let response = json["response"].as_str().unwrap_or("");
    assert!(
        json["success"] == false || response.is_empty(),
        "Expected failure indication in: {json}"
    );
}

#[test]
fn mock_session_id_returned() {
    let (stdout, stderr, _) = run_mock("say hi", r#"[{"text":"ok"}]"#);
    let json = extract_json(&stdout);
    let session_id = json["session_id"].as_str();
    assert!(
        session_id.is_some() && !session_id.unwrap().is_empty(),
        "Expected session_id in JSON.\nJSON: {json}\nstderr: {stderr}"
    );
}

#[test]
fn mock_model_name_in_json() {
    let (stdout, _, _) = run_mock("say hi", r#"[{"text":"ok"}]"#);
    let json = extract_json(&stdout);
    let model = json["model"].as_str().unwrap_or("");
    // Mock provider reports "mock-model" but config may load "auto-detect" first
    assert!(!model.is_empty(), "Expected model name in JSON, got empty");
}

#[test]
fn mock_at_file_reference() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("data.txt"), "important data").unwrap();

    let output = Command::new(koda_bin())
        .args([
            "-p",
            "analyze @data.txt",
            "--provider",
            "mock",
            "--output-format",
            "json",
            "--project-root",
        ])
        .arg(tmp.path())
        .env("XDG_CONFIG_HOME", tmp.path())
        .env("KODA_MOCK_RESPONSES", r#"[{"text":"analyzed"}]"#)
        .output()
        .expect("Failed to run koda");

    assert!(
        output.status.success(),
        "@file processing failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn mock_multi_turn_tool_use() {
    let responses = r#"[
        {"tool":"Bash","args":{"command":"echo hello"}},
        {"text":"Command output was hello."}
    ]"#;
    let (stdout, stderr, success) = run_mock("run echo hello", responses);
    assert!(success, "Multi-turn failed.\nstderr: {stderr}");
    let json = extract_json(&stdout);
    assert_eq!(json["success"], true);
}

#[test]
fn mock_session_resume() {
    let tmp = tempfile::tempdir().unwrap();

    // Turn 1
    let output1 = Command::new(koda_bin())
        .args([
            "-p",
            "turn one",
            "--provider",
            "mock",
            "--output-format",
            "json",
            "--project-root",
        ])
        .arg(tmp.path())
        .env("XDG_CONFIG_HOME", tmp.path())
        .env("KODA_MOCK_RESPONSES", r#"[{"text":"first"}]"#)
        .output()
        .expect("Turn 1 failed");

    let stdout1 = String::from_utf8_lossy(&output1.stdout);
    let json1 = extract_json(&stdout1);
    let session_id = json1["session_id"].as_str().expect("No session_id");

    // Turn 2: resume
    let output2 = Command::new(koda_bin())
        .args([
            "-p",
            "turn two",
            "--provider",
            "mock",
            "--output-format",
            "json",
            "--session",
            session_id,
            "--project-root",
        ])
        .arg(tmp.path())
        .env("XDG_CONFIG_HOME", tmp.path())
        .env("KODA_MOCK_RESPONSES", r#"[{"text":"second"}]"#)
        .output()
        .expect("Turn 2 failed");

    let stdout2 = String::from_utf8_lossy(&output2.stdout);
    let stderr2 = String::from_utf8_lossy(&output2.stderr);
    assert!(
        !stdout2.is_empty(),
        "Turn 2 produced no stdout.\nstderr: {stderr2}"
    );
    let json2 = extract_json(&stdout2);
    assert_eq!(json2["success"], true);
    assert_eq!(
        json2["session_id"].as_str().unwrap(),
        session_id,
        "Resumed session should keep same ID"
    );
}

/// Regression: --resume flag must work as an alias for --session (#505/#507).
#[test]
fn mock_session_resume_via_resume_flag() {
    let tmp = tempfile::tempdir().unwrap();

    // Turn 1: create session
    let output1 = Command::new(koda_bin())
        .args([
            "-p",
            "turn one",
            "--provider",
            "mock",
            "--output-format",
            "json",
            "--project-root",
        ])
        .arg(tmp.path())
        .env("XDG_CONFIG_HOME", tmp.path())
        .env("KODA_MOCK_RESPONSES", r#"[{"text":"first"}]"#)
        .output()
        .expect("Turn 1 failed");

    let stdout1 = String::from_utf8_lossy(&output1.stdout);
    let json1 = extract_json(&stdout1);
    let session_id = json1["session_id"].as_str().expect("No session_id");

    // Turn 2: resume via --resume (not --session)
    let output2 = Command::new(koda_bin())
        .args([
            "-p",
            "turn two",
            "--provider",
            "mock",
            "--output-format",
            "json",
            "--resume",
            session_id,
            "--project-root",
        ])
        .arg(tmp.path())
        .env("XDG_CONFIG_HOME", tmp.path())
        .env("KODA_MOCK_RESPONSES", r#"[{"text":"second via resume"}]"#)
        .output()
        .expect("Turn 2 with --resume failed");

    let stdout2 = String::from_utf8_lossy(&output2.stdout);
    let stderr2 = String::from_utf8_lossy(&output2.stderr);
    assert!(
        output2.status.success(),
        "--resume should work as --session alias.\nstderr: {stderr2}"
    );
    let json2 = extract_json(&stdout2);
    assert_eq!(json2["success"], true);
    assert_eq!(
        json2["session_id"].as_str().unwrap(),
        session_id,
        "--resume should resume the same session"
    );
}

/// Regression: -s short flag must work for --resume (#505).
#[test]
fn mock_session_resume_via_short_s_flag() {
    let tmp = tempfile::tempdir().unwrap();

    // Turn 1
    let output1 = Command::new(koda_bin())
        .args([
            "-p",
            "turn one",
            "--provider",
            "mock",
            "--output-format",
            "json",
            "--project-root",
        ])
        .arg(tmp.path())
        .env("XDG_CONFIG_HOME", tmp.path())
        .env("KODA_MOCK_RESPONSES", r#"[{"text":"first"}]"#)
        .output()
        .expect("Turn 1 failed");

    let stdout1 = String::from_utf8_lossy(&output1.stdout);
    let json1 = extract_json(&stdout1);
    let session_id = json1["session_id"].as_str().expect("No session_id");

    // Turn 2: resume via -s
    let output2 = Command::new(koda_bin())
        .args([
            "-p",
            "turn two",
            "--provider",
            "mock",
            "--output-format",
            "json",
            "-s",
            session_id,
            "--project-root",
        ])
        .arg(tmp.path())
        .env("XDG_CONFIG_HOME", tmp.path())
        .env("KODA_MOCK_RESPONSES", r#"[{"text":"second via -s"}]"#)
        .output()
        .expect("Turn 2 with -s failed");

    assert!(
        output2.status.success(),
        "-s should work as --resume alias.\nstderr: {}",
        String::from_utf8_lossy(&output2.stderr)
    );
    let json2 = extract_json(&String::from_utf8_lossy(&output2.stdout));
    assert_eq!(
        json2["session_id"].as_str().unwrap(),
        session_id,
        "-s should resume the same session"
    );
}