orchestratectl 0.1.1

Rust CLI for orchestrating AI-agent workflows on a developer's machine.
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
//! Reusable conformance suite for [`CodeHarness`] adapters (design.md §10: "A
//! conformance suite tests each adapter against: clean success, no-change,
//! self-check fail, malformed output, dirty worktree, transcript+usage capture,
//! …").
//!
//! The core is [`assert_result_conforms`] — the adapter-agnostic structural
//! invariants every [`ChunkResult`] must satisfy — and [`run_and_check`], which
//! runs an adapter and gates its result through those invariants. Every
//! git-inspecting adapter ([`aider`], [`claude`], [`pi`]) is "run through the
//! suite" by driving its scenarios via [`run_and_check`] in its own module's
//! tests — each against a **fixture script** (`OCTL_*_BIN` override) so the
//! contract cases (clean success, no-change, self-check fail, timeout/cancel,
//! provider spawn failure, transcript+usage capture) run deterministically with
//! no network. The [`StubHarness`] scenario matrix below is the tool-independent
//! core of that same contract.
//!
//! The scenario matrix runs against the [`StubHarness`] by default (no network,
//! no git) so CI tests the *contract* deterministically. Tests that drive a
//! **real** agent (not a fixture script) are gated behind the `OCTL_HARNESS_LIVE`
//! env var — see [`live_enabled`] and the live smoke tests below — so CI never
//! reaches for a binary/credential/network; the `harness bakeoff` command is the
//! primary live exercise and is run explicitly.
//!
//! [`stub`]: super::stub
//! [`aider`]: super::aider
//! [`claude`]: super::claude
//! [`pi`]: super::pi
//! [`StubHarness`]: super::stub::StubHarness

use std::collections::HashSet;

use super::{
    ChunkOutcome, ChunkRequest, ChunkResult, HarnessCapabilities, HARNESS_CONTRACT_VERSION,
};

/// A full git object id is 40 (SHA-1) or 64 (SHA-256) hex chars.
fn is_oid(s: &str) -> bool {
    matches!(s.len(), 40 | 64) && s.chars().all(|c| c.is_ascii_hexdigit())
}

/// Assert a [`ChunkResult`] satisfies the structural contract every adapter must
/// honour, independent of which tool produced it. `caps` is the producing
/// adapter's [`HarnessCapabilities`], needed to enforce check-completeness.
/// Returns `Err(reason)` on the first violation (tests `.unwrap()` it).
///
/// Invariants:
/// 1. `schema_version` matches the linked [`HARNESS_CONTRACT_VERSION`].
/// 2. `resulting_commit.is_some()` **iff** `outcome` is [`ChunkOutcome::Committed`],
///    the two oids agree, and the committed oid is a well-formed git object id.
/// 3. `changed_files` is non-empty only for a committed outcome.
/// 4. A [`ChunkOutcome::Failed`] carries a non-empty `reason`.
/// 5. Every `check_results` entry has a `check_id` matching a requested check,
///    with no duplicates. When the adapter `runs_checks` and execution reached
///    the check phase (any outcome but `Timeout`/`Cancelled`), the reported
///    check ids are exactly the requested set (completeness). When it does not
///    `runs_checks`, `check_results` is empty.
/// 6. A `transcript_ref`, when present, is a non-empty path.
pub fn assert_result_conforms(
    req: &ChunkRequest,
    res: &ChunkResult,
    caps: HarnessCapabilities,
) -> Result<(), String> {
    if res.schema_version != HARNESS_CONTRACT_VERSION {
        return Err(format!(
            "schema_version {} != linked contract version {HARNESS_CONTRACT_VERSION}",
            res.schema_version
        ));
    }

    match &res.outcome {
        ChunkOutcome::Committed { commit } => {
            if !is_oid(commit) {
                return Err(format!(
                    "Committed outcome carries a non-oid commit: {commit:?}"
                ));
            }
            match &res.resulting_commit {
                Some(rc) if rc == commit => {}
                Some(rc) => {
                    return Err(format!(
                        "resulting_commit ({rc}) disagrees with outcome commit ({commit})"
                    ));
                }
                None => return Err("Committed outcome but resulting_commit is None".into()),
            }
        }
        ChunkOutcome::Failed { reason } => {
            if reason.trim().is_empty() {
                return Err("Failed outcome carries an empty reason".into());
            }
        }
        ChunkOutcome::NoChange | ChunkOutcome::Timeout | ChunkOutcome::Cancelled => {}
    }

    if !matches!(res.outcome, ChunkOutcome::Committed { .. }) {
        if res.resulting_commit.is_some() {
            return Err(format!(
                "non-committed outcome {:?} must not carry resulting_commit",
                res.outcome
            ));
        }
        if !res.changed_files.is_empty() {
            return Err(format!(
                "non-committed outcome {:?} must not report changed_files",
                res.outcome
            ));
        }
    }

    // Check-result integrity: ids ⊆ requested, no duplicates.
    let requested: HashSet<&str> = req.checks.iter().map(|c| c.id.as_str()).collect();
    let mut seen: HashSet<&str> = HashSet::new();
    for cr in &res.check_results {
        if !requested.contains(cr.check_id.as_str()) {
            return Err(format!(
                "check_results contains an un-requested check_id: {:?}",
                cr.check_id
            ));
        }
        if !seen.insert(cr.check_id.as_str()) {
            return Err(format!(
                "duplicate check_id in check_results: {:?}",
                cr.check_id
            ));
        }
    }
    if caps.runs_checks {
        // Timeout/Cancelled may stop before the check phase; every other outcome
        // must report a result for every requested check.
        if !matches!(res.outcome, ChunkOutcome::Timeout | ChunkOutcome::Cancelled)
            && seen != requested
        {
            return Err(format!(
                "runs_checks adapter reported {} check results for {} requested checks",
                seen.len(),
                requested.len()
            ));
        }
    } else if !res.check_results.is_empty() {
        return Err("adapter reports runs_checks=false but returned check_results".into());
    }

    if let Some(t) = &res.transcript_ref {
        if t.as_os_str().is_empty() {
            return Err("transcript_ref is present but empty".into());
        }
    }

    Ok(())
}

/// Run `harness.run_chunk(req)` and, on success, assert the result conforms to
/// the contract (using the harness's own capabilities) before returning it. The
/// single entry point an adapter's tests use so every returned result is
/// contract-gated automatically.
///
/// Test-only: it *panics* on non-conformance, which is the right behavior for a
/// test gate but would take down a supervisor if called in production — so it is
/// `#[cfg(test)]`. Production code that wants to validate an adapter result
/// should call [`assert_result_conforms`] and handle the `Err`.
///
/// Drives the adapter with a fresh, un-cancelled [`CancelToken`]. Use
/// [`run_and_check_with_cancel`] to exercise the cancellation path.
#[cfg(test)]
pub fn run_and_check(
    harness: &dyn super::CodeHarness,
    req: &ChunkRequest,
) -> Result<ChunkResult, super::HarnessError> {
    run_and_check_with_cancel(harness, req, &super::CancelToken::new())
}

/// [`run_and_check`] with a caller-supplied [`CancelToken`], so a test can trip
/// cancellation (before or during the run, from another thread) and still gate
/// the result through the structural contract.
#[cfg(test)]
pub fn run_and_check_with_cancel(
    harness: &dyn super::CodeHarness,
    req: &ChunkRequest,
    cancel: &super::CancelToken,
) -> Result<ChunkResult, super::HarnessError> {
    let out = harness.run_chunk(req, cancel);
    if let Ok(res) = &out {
        assert_result_conforms(req, res, harness.capabilities())
            .unwrap_or_else(|e| panic!("adapter produced a non-conforming ChunkResult: {e}"));
    }
    out
}

#[cfg(test)]
mod tests {
    use super::super::stub::{StubBehavior, StubHarness};
    use super::super::{Check, ChunkOutcome, ChunkRequest, HarnessCapabilities, HarnessError};
    use super::*;
    use std::path::PathBuf;
    use std::time::Duration;

    fn req_with_checks(checks: Vec<Check>) -> ChunkRequest {
        ChunkRequest {
            run_id: "r".into(),
            chunk_id: "c".into(),
            attempt_id: "a".into(),
            worktree_path: PathBuf::from("/tmp/does-not-matter"),
            base_commit: "0".repeat(40),
            plan_rev: "v1".into(),
            brief: "b".into(),
            checks,
            files: vec![],
            timeout: None,
        }
    }

    fn one_check() -> Vec<Check> {
        vec![Check {
            id: "chk1".into(),
            desc: "d".into(),
            run: "true".into(),
            timeout: None,
        }]
    }

    /// Default (fully-capable) capabilities, matching `StubHarness::new`.
    fn full_caps() -> HarnessCapabilities {
        HarnessCapabilities {
            can_author_tests: true,
            reports_usage: true,
            honors_file_scope: true,
            runs_checks: true,
        }
    }

    // ---- The design §10 scenario matrix, run against the stub by default. ----

    #[test]
    fn scenario_clean_success() {
        let stub = StubHarness::new(StubBehavior::Commit {
            commit: "a".repeat(40),
            changed_files: vec![PathBuf::from("src/lib.rs")],
            fail_first_check: false,
        });
        let req = req_with_checks(one_check());
        let res = run_and_check(&stub, &req).unwrap();
        assert!(matches!(res.outcome, ChunkOutcome::Committed { .. }));
        assert_eq!(
            res.resulting_commit.as_deref(),
            Some("a".repeat(40).as_str())
        );
        assert_eq!(res.changed_files, vec![PathBuf::from("src/lib.rs")]);
        assert!(res.check_results.iter().all(|c| c.passed));
    }

    #[test]
    fn scenario_no_change() {
        let stub = StubHarness::new(StubBehavior::NoChange);
        let req = req_with_checks(one_check());
        let res = run_and_check(&stub, &req).unwrap();
        assert_eq!(res.outcome, ChunkOutcome::NoChange);
        assert!(res.resulting_commit.is_none());
        assert!(res.changed_files.is_empty());
    }

    #[test]
    fn scenario_self_check_failure() {
        let stub = StubHarness::new(StubBehavior::Commit {
            commit: "b".repeat(40),
            changed_files: vec![PathBuf::from("x")],
            fail_first_check: true,
        });
        let req = req_with_checks(one_check());
        let res = run_and_check(&stub, &req).unwrap();
        // Committed, but the self-check failed — the supervisor's floor decides
        // what to do; the harness just reports it.
        assert!(matches!(res.outcome, ChunkOutcome::Committed { .. }));
        assert!(!res.check_results[0].passed);
    }

    #[test]
    fn scenario_failed_run() {
        let stub = StubHarness::new(StubBehavior::Failed {
            reason: "provider 500".into(),
        });
        let req = req_with_checks(one_check());
        let res = run_and_check(&stub, &req).unwrap();
        assert!(matches!(res.outcome, ChunkOutcome::Failed { .. }));
    }

    #[test]
    fn scenario_malformed_output_is_error() {
        let stub = StubHarness::new(StubBehavior::Error(HarnessError::MalformedOutput {
            message: "not json".into(),
        }));
        let req = req_with_checks(one_check());
        let err = run_and_check(&stub, &req).unwrap_err();
        assert!(matches!(err, HarnessError::MalformedOutput { .. }));
    }

    #[test]
    fn scenario_dirty_worktree_is_error() {
        let stub = StubHarness::new(StubBehavior::Error(HarnessError::DirtyWorktree {
            details: " M foo.rs".into(),
        }));
        let req = req_with_checks(one_check());
        let err = run_and_check(&stub, &req).unwrap_err();
        assert!(matches!(err, HarnessError::DirtyWorktree { .. }));
    }

    #[test]
    fn scenario_timeout_and_cancelled() {
        for behavior in [StubBehavior::Timeout, StubBehavior::Cancelled] {
            let stub = StubHarness::new(behavior.clone());
            let req = req_with_checks(one_check());
            let res = run_and_check(&stub, &req).unwrap();
            assert!(matches!(
                res.outcome,
                ChunkOutcome::Timeout | ChunkOutcome::Cancelled
            ));
        }
    }

    #[test]
    fn scenario_transcript_and_usage_capture() {
        let stub = StubHarness::new(StubBehavior::Commit {
            commit: "c".repeat(40),
            changed_files: vec![PathBuf::from("x")],
            fail_first_check: false,
        });
        let req = req_with_checks(one_check());
        let res = run_and_check(&stub, &req).unwrap();
        assert!(res.transcript_ref.is_some());
        let usage = res.usage.expect("stub reports usage by default");
        assert_eq!(usage.total_tokens, Some(150));
    }

    #[test]
    fn capabilities_can_suppress_usage_and_checks() {
        let stub = StubHarness::new(StubBehavior::Commit {
            commit: "d".repeat(40),
            changed_files: vec![],
            fail_first_check: false,
        })
        .with_capabilities(HarnessCapabilities {
            can_author_tests: false,
            reports_usage: false,
            honors_file_scope: false,
            runs_checks: false,
        });
        let req = req_with_checks(one_check());
        let res = run_and_check(&stub, &req).unwrap();
        assert!(res.usage.is_none());
        assert!(res.check_results.is_empty());
    }

    // ---- Invariant checker itself. ----

    #[test]
    fn conformance_rejects_commit_without_oid() {
        let req = req_with_checks(vec![]);
        let mut res = ChunkResult::committed("e".repeat(40), vec![]);
        res.resulting_commit = None; // corrupt it
        assert!(assert_result_conforms(&req, &res, full_caps()).is_err());
    }

    #[test]
    fn conformance_rejects_non_oid_commit() {
        let req = req_with_checks(vec![]);
        // A non-hex, wrong-length "commit" must be rejected.
        let res = ChunkResult::committed("not-a-real-oid", vec![]);
        assert!(assert_result_conforms(&req, &res, full_caps()).is_err());
    }

    #[test]
    fn conformance_rejects_wrong_schema_version() {
        let req = req_with_checks(vec![]);
        let mut res = ChunkResult::no_change();
        res.schema_version = HARNESS_CONTRACT_VERSION + 99;
        assert!(assert_result_conforms(&req, &res, full_caps()).is_err());
    }

    #[test]
    fn conformance_rejects_unrequested_check() {
        let req = req_with_checks(one_check());
        let mut res = ChunkResult::no_change();
        res.check_results.push(super::super::CheckResult {
            check_id: "not-requested".into(),
            desc: "sneaky".into(),
            run: "rm -rf /".into(),
            passed: true,
            exit_code: Some(0),
            stdout: String::new(),
            stderr: String::new(),
        });
        assert!(assert_result_conforms(&req, &res, full_caps()).is_err());
    }

    #[test]
    fn conformance_rejects_incomplete_checks_when_runs_checks() {
        // Two checks requested, adapter claims runs_checks, but reports none.
        let req = req_with_checks(vec![
            Check {
                id: "a".into(),
                desc: "d".into(),
                run: "true".into(),
                timeout: None,
            },
            Check {
                id: "b".into(),
                desc: "d".into(),
                run: "true".into(),
                timeout: None,
            },
        ]);
        let res = ChunkResult::no_change(); // no check_results
        assert!(assert_result_conforms(&req, &res, full_caps()).is_err());
    }

    #[test]
    fn conformance_accepts_stub_default() {
        let stub = StubHarness::new(StubBehavior::NoChange);
        let req = req_with_checks(one_check());
        // run_and_check panics on non-conformance; reaching here is the assert.
        run_and_check(&stub, &req).unwrap();
    }

    // ---- Cancellation + timeout, deterministically via the stub. ----

    #[test]
    fn cancel_pretripped_yields_cancelled_for_any_behavior() {
        use super::super::CancelToken;
        // A behavior that would otherwise commit; a pre-tripped token wins.
        let stub = StubHarness::new(StubBehavior::Commit {
            commit: "a".repeat(40),
            changed_files: vec![PathBuf::from("x")],
            fail_first_check: false,
        });
        let req = req_with_checks(one_check());
        let cancel = CancelToken::new();
        cancel.cancel();
        let res = run_and_check_with_cancel(&stub, &req, &cancel).unwrap();
        assert_eq!(res.outcome, ChunkOutcome::Cancelled);
        assert!(res.resulting_commit.is_none());
        assert!(res.check_results.is_empty());
    }

    #[test]
    fn slow_run_cancelled_in_flight() {
        use super::super::CancelToken;
        use std::time::Duration;
        // A generous budget so the run only ends via the in-flight cancel.
        let stub = StubHarness::new(StubBehavior::SlowUntilCancel {
            budget: Duration::from_secs(30),
        });
        let req = req_with_checks(one_check());
        let cancel = CancelToken::new();
        let trip = cancel.clone();
        let handle = std::thread::spawn(move || {
            std::thread::sleep(Duration::from_millis(50));
            trip.cancel();
        });
        let res = run_and_check_with_cancel(&stub, &req, &cancel).unwrap();
        handle.join().unwrap();
        assert_eq!(res.outcome, ChunkOutcome::Cancelled);
    }

    #[test]
    fn slow_run_times_out_when_not_cancelled() {
        use super::super::CancelToken;
        use std::time::Duration;
        // A zero budget expires immediately; the token is never tripped.
        let stub = StubHarness::new(StubBehavior::SlowUntilCancel {
            budget: Duration::from_millis(0),
        });
        let req = req_with_checks(one_check());
        let res = run_and_check_with_cancel(&stub, &req, &CancelToken::new()).unwrap();
        assert_eq!(res.outcome, ChunkOutcome::Timeout);
    }

    // ---- Live agent smoke tests (opt-in). ----
    //
    // These drive the REAL agents (network + credentials + a real binary), so
    // they are skipped unless `OCTL_HARNESS_LIVE=1`. Each also self-skips if its
    // binary is not installed, so enabling the gate on a partial toolbox never
    // fails spuriously. They assert only the *contract* (a conforming
    // `ChunkResult`, gated by `run_and_check`) — never a specific model output,
    // which is non-deterministic.

    /// Whether the live-agent gate (`OCTL_HARNESS_LIVE=1`) is enabled.
    fn live_enabled() -> bool {
        std::env::var("OCTL_HARNESS_LIVE").as_deref() == Ok("1")
    }

    /// Whether a bare binary name resolves on `PATH` (skip a live test whose tool
    /// is not installed).
    fn on_path(bin: &str) -> bool {
        std::env::var("PATH")
            .is_ok_and(|path| std::env::split_paths(&path).any(|d| d.join(bin).is_file()))
    }

    /// A real one-commit git repo in a temp dir, returned with its HEAD oid so a
    /// live `ChunkRequest` forks from genuine state.
    fn live_repo() -> (tempfile::TempDir, String) {
        use std::process::Command;
        let dir = tempfile::TempDir::new().unwrap();
        let git = |args: &[&str]| {
            assert!(Command::new("git")
                .arg("-C")
                .arg(dir.path())
                .args(args)
                .output()
                .unwrap()
                .status
                .success());
        };
        git(&["init", "-q", "-b", "main"]);
        git(&["config", "user.email", "live@t"]);
        git(&["config", "user.name", "live"]);
        std::fs::write(dir.path().join("seed.txt"), "seed\n").unwrap();
        git(&["add", "-A"]);
        git(&["commit", "-q", "-m", "seed"]);
        let head = String::from_utf8(
            Command::new("git")
                .arg("-C")
                .arg(dir.path())
                .args(["rev-parse", "HEAD"])
                .output()
                .unwrap()
                .stdout,
        )
        .unwrap()
        .trim()
        .to_string();
        (dir, head)
    }

    /// A trivial "create a file and commit it" live request against `repo`.
    fn live_request(repo: &std::path::Path, head: &str) -> ChunkRequest {
        ChunkRequest {
            run_id: "live".into(),
            chunk_id: "c1".into(),
            attempt_id: "a1".into(),
            worktree_path: repo.to_path_buf(),
            base_commit: head.into(),
            plan_rev: "v1".into(),
            brief: "Create a file named GREETING.txt whose only content is the word \
                    `hello`. Then commit it."
                .into(),
            checks: vec![Check {
                id: "exists".into(),
                desc: "GREETING.txt exists".into(),
                run: "test -f GREETING.txt".into(),
                timeout: Some(Duration::from_secs(10)),
            }],
            files: vec![PathBuf::from("GREETING.txt")],
            timeout: Some(Duration::from_secs(600)),
        }
    }

    #[test]
    fn live_claude_deepseek_conforms() {
        if !live_enabled() || !on_path("claude-deepseek") {
            return;
        }
        use super::super::claude::ClaudeHarness;
        let (repo, head) = live_repo();
        let h = ClaudeHarness::deepseek("flash");
        // `run_and_check` gates the result through the structural contract.
        let res = run_and_check(&h, &live_request(repo.path(), &head)).unwrap();
        // A live run may commit or (rarely) no-change; either is contract-valid.
        assert!(matches!(
            res.outcome,
            ChunkOutcome::Committed { .. } | ChunkOutcome::NoChange
        ));
    }

    #[test]
    fn live_pi_conforms() {
        if !live_enabled() || !on_path("pi") || std::env::var("DEEPSEEK_API_KEY").is_err() {
            return;
        }
        use super::super::pi::{PiConfig, PiHarness};
        let (repo, head) = live_repo();
        let h = PiHarness::new(PiConfig::deepseek("deepseek-v4-flash"));
        let res = run_and_check(&h, &live_request(repo.path(), &head)).unwrap();
        assert!(matches!(
            res.outcome,
            ChunkOutcome::Committed { .. } | ChunkOutcome::NoChange
        ));
    }
}