orch-cli 0.91.2

Orch, the agent orchestration engine: runs coding agents on tasks and cron jobs
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
423
424
425
426
427
428
429
430
431
//! Integration tests for the full review agent flow.
//!
//! ## Approach
//!
//! Real end-to-end review integration tests (spawning agents in tmux, reading
//! real GitHub PRs, etc.) require live API keys and installed CLIs. Instead,
//! this module tests the **review parsing pipeline** end-to-end using pre-captured
//! NDJSON fixtures that represent each agent's output format:
//!
//! ```text
//! Agent NDJSON output → find_agent_result() → extract text → parse_review_response() → ReviewResponse
//! ```
//!
//! This verifies the same code paths used by review.rs for each agent:
//!   - `find_agent_result(agent, ndjson)` — per-agent envelope extraction
//!   - `parse_review_response(&text)` — JSON/markdown parsing
//!   - `infer_review_response(&text)` — keyword-based fallback for plain text
//!
//! ## Fixtures
//!
//! Each fixture is a pre-captured NDJSON stream from a real agent invocation.
//! Fixtures cover both happy paths and error conditions:
//!
//! | Fixture | Agent | Decision |
//! |---------|-------|----------|
//! | `review_claude_approve.jsonl` | Claude | approve |
//! | `review_claude_request_changes.jsonl` | Claude | request_changes |
//! | `review_claude_rate_limit.jsonl` | Claude | (error) |
//! | `review_opencode_approve.jsonl` | OpenCode | approve |
//! | `review_opencode_request_changes.jsonl` | OpenCode | request_changes |
//! | `review_opencode_plain_text.jsonl` | OpenCode | (plain text → infer approve) |
//! | `review_codex_approve.jsonl` | Codex | approve |
//! | `review_codex_request_changes.jsonl` | Codex | request_changes |
//! | `review_codex_plain_text.jsonl` | Codex | (plain text → infer approve) |
//! | `review_kimi_approve.jsonl` | Kimi | approve |
//! | `review_minimax_request_changes.jsonl` | MiniMax | request_changes |
//!
//! ## Running
//!
//! ```bash
//! cargo test --test integration_review -- --nocapture
//! cargo test --test integration_review response_tests -- --nocapture  # subset
//! ```
//!
//! ## Adding new Fixtures
//!
//! To add a fixture for a new agent or format:
//! 1. Capture real agent NDJSON output (run agent with `--format json` / `stream-json`)
//! 2. Save as `tests/fixtures/review_{agent}_{decision}.jsonl`
//! 3. Add a test function that mirrors `verify_review_fixture()`
//!
//! ## End-to-End Tests (manual)
//!
//! For true end-to-end tests with live agents, the `#[ignore]` tests call
//! real agents. Run with:
//! ```bash
//! cargo test --test integration_review -- --ignored --nocapture
//! ```

use orch::engine::runner::agents::find_agent_result;
use orch::engine::runner::response::{
    infer_review_response, parse_review_response, ReviewResponse,
};

/// Full review parsing pipeline for an agent's NDJSON output.
///
/// Mirrors the production path in review.rs:
/// ```text
/// find_agent_result(agent, ndjson) → extract text → parse_review_response → infer_review_response
/// ```
fn parse_review_output(agent: &str, ndjson: &str) -> anyhow::Result<ReviewResponse> {
    let text = find_agent_result(agent, ndjson)
        .map(|r| r.result_text)
        .filter(|t| !t.is_empty())
        .unwrap_or_else(|| ndjson.to_string());

    parse_review_response(&text).or_else(|_| {
        infer_review_response(&text)
            .ok_or_else(|| anyhow::anyhow!("failed to parse review from {agent} output"))
    })
}

fn verify_review_fixture(agent: &str, fixture_name: &str, expected_decision: &str, ndjson: &str) {
    eprintln!("\n=== {agent} / {fixture_name} ===");
    eprintln!("expected decision: {expected_decision}");

    let result = find_agent_result(agent, ndjson);

    if let Some(ref r) = result {
        eprintln!("find_agent_result: is_error={}", r.is_error);
        eprintln!(
            "result_text (first 200 chars): {}",
            &r.result_text[..r.result_text.len().min(200)]
        );
        if let (Some(i), Some(o)) = (r.input_tokens, r.output_tokens) {
            eprintln!("tokens: input={i}, output={o}");
        }
    } else {
        eprintln!("find_agent_result returned None");
    }

    let review = parse_review_output(agent, ndjson);
    if let Ok(ref r) = review {
        eprintln!("decision: {:?} (expected: {expected_decision})", r.decision);
        eprintln!("notes: {:?}", r.notes);
        eprintln!("issues: {}", r.issues.len());
    } else if let Err(ref e) = review {
        eprintln!("parse FAILED: {e}");
    }

    let review = review.expect(
        "parse_review_output should succeed — if it fails, the review \
         pipeline cannot handle this agent's output format",
    );
    assert_eq!(
        review.decision, expected_decision,
        "{agent}/{fixture_name}: decision mismatch"
    );
    eprintln!("=== {agent} / {fixture_name} PASSED ===\n");
}

fn error_fixture(agent: &str, fixture_name: &str, ndjson: &str) {
    eprintln!("\n=== {agent} / {fixture_name} (error) ===");
    let result = find_agent_result(agent, ndjson).expect("should find result");
    assert!(
        result.is_error,
        "{agent}/{fixture_name}: expected is_error=true, got false"
    );
    eprintln!("is_error: true (correct)");
    eprintln!(
        "error text (first 200 chars): {}",
        &result.result_text[..result.result_text.len().min(200)]
    );
    eprintln!("=== {agent} / {fixture_name} PASSED ===\n");
}

// ── Claude fixtures ─────────────────────────────────────────────────────────────

#[test]
fn review_claude_approve() {
    let ndjson = include_str!("fixtures/review_claude_approve.jsonl");
    verify_review_fixture("claude", "approve", "approve", ndjson);
}

#[test]
fn review_claude_request_changes() {
    let ndjson = include_str!("fixtures/review_claude_request_changes.jsonl");
    verify_review_fixture("claude", "request_changes", "request_changes", ndjson);
}

#[test]
fn review_claude_rate_limit() {
    let ndjson = include_str!("fixtures/review_claude_rate_limit.jsonl");
    error_fixture("claude", "rate_limit", ndjson);
}

// ── OpenCode fixtures ────────────────────────────────────────────────────────────

#[test]
fn review_opencode_approve() {
    let ndjson = include_str!("fixtures/review_opencode_approve.jsonl");
    verify_review_fixture("opencode", "approve", "approve", ndjson);
}

#[test]
fn review_opencode_request_changes() {
    let ndjson = include_str!("fixtures/review_opencode_request_changes.jsonl");
    verify_review_fixture("opencode", "request_changes", "request_changes", ndjson);
}

#[test]
fn review_opencode_plain_text() {
    // Plain-text review: OpenCode returns "Review complete. All tests passed, LGTM."
    // Should infer "approve" via keyword detection.
    let ndjson = include_str!("fixtures/review_opencode_plain_text.jsonl");
    verify_review_fixture("opencode", "plain_text", "approve", ndjson);
}

// ── Codex fixtures ───────────────────────────────────────────────────────────────

#[test]
fn review_codex_approve() {
    let ndjson = include_str!("fixtures/review_codex_approve.jsonl");
    verify_review_fixture("codex", "approve", "approve", ndjson);
}

#[test]
fn review_codex_request_changes() {
    let ndjson = include_str!("fixtures/review_codex_request_changes.jsonl");
    verify_review_fixture("codex", "request_changes", "request_changes", ndjson);
}

#[test]
fn review_codex_plain_text() {
    // Plain-text review: Codex returns "All checks passed, LGTM."
    // Should infer "approve" via keyword detection.
    let ndjson = include_str!("fixtures/review_codex_plain_text.jsonl");
    verify_review_fixture("codex", "plain_text", "approve", ndjson);
}

// ── Kimi fixtures ─────────────────────────────────────────────────────────────

#[test]
fn review_kimi_approve() {
    let ndjson = include_str!("fixtures/review_kimi_approve.jsonl");
    verify_review_fixture("kimi", "approve", "approve", ndjson);
}

// ── MiniMax fixtures ───────────────────────────────────────────────────────────

#[test]
fn review_minimax_request_changes() {
    let ndjson = include_str!("fixtures/review_minimax_request_changes.jsonl");
    verify_review_fixture("minimax", "request_changes", "request_changes", ndjson);
}

// ── End-to-end tests (manual only — require live API keys and installed CLIs) ──
//
// These tests spawn real agents in tmux with a review prompt. They are `#[ignore]`d
// because they require API keys, installed CLIs, and a real GitHub PR. Run locally with:
//
// ```bash
// cargo test --test integration_review -- --ignored --nocapture
// ```

use std::process::Command;

const REVIEW_PROMPT: &str = r#"You are a code review agent. Review this trivial change and respond with ONLY this JSON (no markdown, no explanation):
{"decision":"approve","notes":"LGTM","test_results":"pass","issues":[]}"#;

fn is_available(binary: &str) -> bool {
    which::which(binary).is_ok()
}

/// Run the full review parsing flow on agent output.
/// Uses the same code path as review.rs: find_agent_result → parse_review_from_output.
fn verify_review_output(
    agent_binary: &str,
    label: &str,
    exit_code: i32,
    stdout: &str,
    stderr: &str,
) {
    eprintln!("\n=== {label} ===");
    eprintln!("exit_code: {exit_code}");
    eprintln!("stdout length: {}", stdout.len());
    eprintln!("stderr length: {}", stderr.len());
    if !stdout.is_empty() {
        eprintln!(
            "stdout first 300 chars:\n{}",
            &stdout[..stdout.len().min(300)]
        );
    }
    if !stderr.is_empty() {
        eprintln!(
            "stderr first 200 chars:\n{}",
            &stderr[..stderr.len().min(200)]
        );
    }

    // Step 1: abort on non-zero exit or empty output (mirrors review.rs line 638)
    assert!(
        exit_code == 0,
        "{label}: non-zero exit code {exit_code}\nstderr: {}",
        &stderr[..stderr.len().min(500)]
    );
    assert!(
        !stdout.is_empty(),
        "{label}: stdout is empty! exit_code={exit_code}, stderr: {}",
        &stderr[..stderr.len().min(500)]
    );

    // Step 2: per-agent envelope extraction — same as review.rs stage 1
    let text = match find_agent_result(agent_binary, stdout) {
        Some(result) if result.is_error => {
            panic!(
                "{label}: agent reported is_error=true: {}",
                result.result_text
            );
        }
        Some(result) if !result.result_text.is_empty() => {
            eprintln!("extracted text length: {}", result.result_text.len());
            eprintln!(
                "extracted first 300 chars:\n{}",
                &result.result_text[..result.result_text.len().min(300)]
            );
            result.result_text
        }
        _ => {
            eprintln!("find_agent_result returned None/empty, falling back to raw stdout");
            stdout.to_string()
        }
    };

    assert!(
        !text.is_empty(),
        "{label}: text is empty after extraction! The review parser would fail.\nstdout first 500:\n{}",
        &stdout[..stdout.len().min(500)]
    );

    // Step 3: parse as ReviewResponse — same as review.rs stage 2
    let review = parse_review_response(&text)
        .ok()
        .or_else(|| infer_review_response(&text));
    assert!(
        review.is_some(),
        "{label}: parse_review_response and infer_review_response both failed\nextracted text:\n{}",
        &text[..text.len().min(500)]
    );

    eprintln!(
        "=== {label} PASSED: extracted {} chars, parsed review decision={:?} ===\n",
        text.len(),
        review.unwrap().decision
    );
}

/// Run a claude-family agent (claude, kimi, minimax)
fn run_claude_agent(binary: &str, model: &str) -> (i32, String, String) {
    eprintln!("Running {binary} --model {model}...");

    let mut cmd = Command::new(binary);
    cmd.env_remove("CLAUDECODE");
    cmd.args([
        "-p",
        "--verbose",
        "--output-format",
        "stream-json",
        "--permission-mode",
        "bypassPermissions",
        "--model",
        model,
    ]);

    // Pipe prompt via stdin
    let mut echo = Command::new("printf")
        .arg(REVIEW_PROMPT)
        .stdout(std::process::Stdio::piped())
        .spawn()
        .expect("printf");

    cmd.stdin(echo.stdout.take().unwrap());
    let _ = echo.wait(); // avoid zombie
    cmd.stdout(std::process::Stdio::piped());
    cmd.stderr(std::process::Stdio::piped());

    let output = cmd
        .output()
        .unwrap_or_else(|e| panic!("{binary}: failed to run: {e}"));

    (
        output.status.code().unwrap_or(-1),
        String::from_utf8_lossy(&output.stdout).to_string(),
        String::from_utf8_lossy(&output.stderr).to_string(),
    )
}

#[test]
#[ignore]
fn review_flow_claude_haiku() {
    if !is_available("claude") {
        eprintln!("claude not installed, skipping");
        return;
    }
    let (exit, stdout, stderr) = run_claude_agent("claude", "haiku");
    verify_review_output("claude", "claude:haiku", exit, &stdout, &stderr);
}

#[test]
#[ignore]
fn review_flow_kimi() {
    if !is_available("kimi") {
        eprintln!("kimi not installed, skipping");
        return;
    }
    let (exit, stdout, stderr) = run_claude_agent("kimi", "sonnet");
    verify_review_output("kimi", "kimi:sonnet", exit, &stdout, &stderr);
}

#[test]
#[ignore]
fn review_flow_minimax() {
    if !is_available("minimax") {
        eprintln!("minimax not installed, skipping");
        return;
    }
    let (exit, stdout, stderr) = run_claude_agent("minimax", "sonnet");
    verify_review_output("minimax", "minimax:sonnet", exit, &stdout, &stderr);
}

fn run_opencode_agent(model: &str) -> (i32, String, String) {
    eprintln!("Running opencode run --model {model}...");

    let mut cmd = Command::new("opencode");
    cmd.env_remove("CLAUDECODE");
    cmd.args(["run", "--format", "json"]);
    if !model.is_empty() {
        cmd.arg("--model").arg(model);
    }

    // Pipe prompt via stdin
    let mut echo = Command::new("printf");
    echo.arg(REVIEW_PROMPT).stdout(std::process::Stdio::piped());

    let mut echo_child = echo.spawn().expect("printf");
    cmd.stdin(echo_child.stdout.take().unwrap());
    let _ = echo_child.wait(); // avoid zombie
    cmd.stdout(std::process::Stdio::piped());
    cmd.stderr(std::process::Stdio::piped());

    let output = cmd
        .output()
        .unwrap_or_else(|e| panic!("opencode: failed to run: {e}"));

    (
        output.status.code().unwrap_or(-1),
        String::from_utf8_lossy(&output.stdout).to_string(),
        String::from_utf8_lossy(&output.stderr).to_string(),
    )
}

#[test]
#[ignore]
fn review_flow_opencode() {
    if !is_available("opencode") {
        eprintln!("opencode not installed, skipping");
        return;
    }
    let (exit, stdout, stderr) = run_opencode_agent("openai/gpt-4.1");
    verify_review_output("opencode", "opencode:gpt-4.1", exit, &stdout, &stderr);
}