keel-harness 0.4.0

A gated harness for AI-assisted delivery: auditable stopping conditions and durable memory across coding agents.
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
//! Running a task against somebody else's coding agent.
//!
//! One function owns the whole subprocess lifecycle — stdin, stdout, timeout,
//! termination — so adding a driver is a config entry, never new process code.
//!
//! The `blocked` discipline from P6 is enforced here: a driver that cannot
//! start, or that runs out of time, is `blocked`, not `failed`. Only a driver
//! that ran and could not do the job is an agentic failure. Getting this wrong
//! teaches the Phase 3 failure taxonomy to learn from noise.

pub mod builtin;
pub mod conform;
pub mod contract;

use crate::config::{Config, Driver as DriverConfig};
use crate::paths::Paths;
use anyhow::{Result, bail};
pub use contract::{DriverResult, DriverStatus, DriverTask};
use std::io::Write;
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};

/// How often to check whether the driver has exited. Short enough that a fast
/// driver is not held up, long enough not to spin.
const POLL_INTERVAL: Duration = Duration::from_millis(25);

/// How long to keep draining a timed-out driver's pipes before giving up on
/// them. Killing the process group should already have closed both.
const DRAIN_GRACE: Duration = Duration::from_millis(500);

pub struct Invocation {
    pub result: DriverResult,
    pub elapsed: Duration,
    pub stderr: String,
}

/// Execute a driver against a task.
///
/// Never returns `Err` for anything the driver did — a broken driver becomes a
/// `blocked` result so it lands in the trajectory and on the gate report rather
/// than aborting the run.
pub fn run(paths: &Paths, driver: &DriverConfig, task: &DriverTask) -> Invocation {
    run_in(paths, paths, driver, task)
}

/// Run a driver whose adapter lives in `home` against a working tree at `cwd`.
///
/// These are the same directory in normal use, and different exactly once: the
/// conformance suite runs a driver against a scratch repository. A driver
/// command like `.keel/drivers/codex` is relative to the repository that
/// *configured* it, not to wherever the child happens to be started — resolving
/// it against `cwd` made every driver unreachable under conformance.
pub fn run_in(
    home: &Paths,
    cwd: &Paths,
    driver: &DriverConfig,
    task: &DriverTask,
) -> Invocation {
    let started = Instant::now();
    let timeout = Duration::from_secs(driver.timeout_secs);

    let mut parts = driver.cmd.split_whitespace().map(|s| s.to_string()).collect::<Vec<_>>();
    if parts.is_empty() {
        return blocked(started, format!("driver `{}` has an empty cmd", driver.id));
    }
    let program = resolve_program(home, &parts.remove(0));

    let payload = match serde_json::to_string(task) {
        Ok(p) => p,
        Err(e) => return blocked(started, format!("could not serialise the task: {e}")),
    };

    let mut command = Command::new(&program);
    command
        .args(&parts)
        .current_dir(&cwd.repo)
        .env("KEEL_REPO", &cwd.repo)
        .env("KEEL_STORE", cwd.store())
        .env("KEEL_RUN", &task.run)
        .env("KEEL_SPEC", &task.spec)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());

    // Give the driver its own process group so a timeout can kill the whole
    // tree. Without this, killing the driver leaves its children holding the
    // stdout pipe open, and the read below blocks until they finish anyway —
    // which is to say, the timeout would not be a timeout.
    #[cfg(unix)]
    {
        use std::os::unix::process::CommandExt;
        command.process_group(0);
    }

    let mut child = match command.spawn() {
        Ok(c) => c,
        Err(e) => {
            return blocked(started, format!("could not start `{}`: {e}", driver.cmd));
        }
    };
    let pid = child.id();

    if let Some(mut stdin) = child.stdin.take() {
        // A driver that closed stdin early is still worth waiting for; it may
        // have read enough already.
        let _ = stdin.write_all(payload.as_bytes());
    }

    // Drain both pipes on their own threads: a driver that fills the 64KB pipe
    // buffer deadlocks against a parent that waits before reading. They hand
    // the text back over a channel rather than a JoinHandle, because a join
    // cannot be given a deadline and the timeout path must not wait on a pipe
    // that something surviving still holds open.
    let mut stdout_pipe = child.stdout.take();
    let mut stderr_pipe = child.stderr.take();
    let (out_tx, out_rx) = std::sync::mpsc::channel();
    let (err_tx, err_rx) = std::sync::mpsc::channel();
    std::thread::spawn(move || {
        let _ = out_tx.send(read_all(&mut stdout_pipe));
    });
    std::thread::spawn(move || {
        let _ = err_tx.send(read_all(&mut stderr_pipe));
    });

    // Poll for exit against the deadline, keeping the Child here so it can be
    // killed — `wait_with_output` gives that handle away and cannot be interrupted.
    let deadline = started + timeout;
    let mut timed_out = false;
    loop {
        match child.try_wait() {
            Ok(Some(_)) => break,
            Ok(None) => {
                if Instant::now() >= deadline {
                    timed_out = true;
                    kill_group(&mut child, pid);
                    let _ = child.wait();
                    break;
                }
                std::thread::sleep(POLL_INTERVAL);
            }
            Err(e) => return blocked(started, format!("could not wait on the driver: {e}")),
        }
    }

    // A driver that exited on its own closed both pipes, so these arrive at
    // once. After a timeout they may never arrive at all: killing the process
    // group is meant to close them, and a surviving grandchild would keep
    // stdout open regardless. Bounding the wait is what makes the timeout hold
    // even when killing the tree did not work — losing a driver's dying words
    // is a fair price for a deadline that is actually a deadline.
    let (stdout, stderr) = if timed_out {
        (
            out_rx.recv_timeout(DRAIN_GRACE).unwrap_or_default(),
            err_rx.recv_timeout(DRAIN_GRACE).unwrap_or_default(),
        )
    } else {
        (out_rx.recv().unwrap_or_default(), err_rx.recv().unwrap_or_default())
    };
    let stderr = stderr.trim().to_string();

    if timed_out {
        let mut inv = blocked(
            started,
            format!("driver `{}` exceeded its {}s timeout and was terminated", driver.id, driver.timeout_secs),
        );
        inv.stderr = stderr;
        return inv;
    }

    match contract::parse_result(&stdout) {
        Ok(result) => Invocation { result, elapsed: started.elapsed(), stderr },
        Err(why) => {
            // Invalid output is the driver's fault, not the environment's, but
            // it is still "keel could not learn anything", so it blocks.
            let mut inv = blocked(started, format!("invalid driver result — {why}"));
            inv.stderr = stderr;
            inv
        }
    }
}

fn read_all<R: std::io::Read>(pipe: &mut Option<R>) -> String {
    let Some(p) = pipe.as_mut() else { return String::new() };
    let mut buf = Vec::new();
    let _ = p.read_to_end(&mut buf);
    String::from_utf8_lossy(&buf).to_string()
}

/// Kill the driver and everything it started.
///
/// On Unix the child leads its own process group, so a negative pid signals the
/// whole group — which is the only way a `sleep` grandchild actually dies. The
/// earlier version of this shelled out to `pkill -f <name>`; that matched on a
/// substring of every command line on the machine, and could have killed
/// processes that had nothing to do with keel.
#[cfg(unix)]
fn kill_group(child: &mut std::process::Child, pid: u32) {
    // `libc::kill` rather than shelling out to `kill -KILL -<pgid>`: a negative
    // pid means "the whole process group" to the syscall, unambiguously, while
    // the *command* spells that differently across implementations — BSD kill
    // accepts `-KILL -123`, procps-ng reads the second argument as another
    // option and needs `-s KILL -- -123`. Shelling out therefore killed the
    // group on macOS and silently did nothing on Linux, where the surviving
    // grandchild held stdout open and the timeout stopped being a timeout.
    unsafe {
        libc::kill(-(pid as i32), libc::SIGKILL);
    }
    let _ = child.kill();
}

#[cfg(not(unix))]
fn kill_group(child: &mut std::process::Child, pid: u32) {
    // No process groups: taskkill's tree flag is the equivalent, scoped to this
    // pid rather than to a name.
    let _ = Command::new("taskkill").args(["/F", "/T", "/PID", &pid.to_string()]).output();
    let _ = child.kill();
}

/// A relative adapter path is resolved against the configuring repository; a
/// bare name (`claude`, `npx`) is left alone for `PATH` to find.
fn resolve_program(home: &Paths, program: &str) -> String {
    let p = std::path::Path::new(program);
    if p.is_absolute() || !program.contains('/') {
        return program.to_string();
    }
    let candidate = home.repo.join(p);
    if candidate.exists() {
        candidate.to_string_lossy().to_string()
    } else {
        program.to_string()
    }
}

fn blocked(started: Instant, detail: String) -> Invocation {
    Invocation {
        result: DriverResult::blocked(detail),
        elapsed: started.elapsed(),
        stderr: String::new(),
    }
}

/// The driver to use: the named one, else the configured default.
/// Write every reference driver script into `.keel/drivers/`, and append a
/// `[[driver]]` entry for any that config does not already have.
///
/// Idempotent: an existing script is kept, not overwritten, unless `force` is
/// set — the same rule `keel init` already uses for steering docs, so re-running
/// this after hand-editing a driver does not clobber the edit by accident.
///
/// `keel.toml` is edited by *appending text*, never by loading it into
/// `Config` and writing the struct back out. A full round-trip through
/// `toml::to_string_pretty` has no memory of comments or section order —
/// `Config` does not store either — so it would silently strip every comment
/// in the file and shuffle every section on a command whose only job is to
/// add what is missing. `cfg` is read to know what already exists; it is
/// never the thing written.
///
/// Returns one line per file or config entry touched, for the caller to print.
pub fn scaffold(paths: &Paths, cfg: &Config, force: bool) -> Result<Vec<String>> {
    let dir = paths.keel().join("drivers");
    std::fs::create_dir_all(&dir)?;
    let mut lines = Vec::new();
    let mut to_register: Vec<(&'static str, &'static str, bool, u64)> = Vec::new();

    for b in builtin::ALL {
        let path = dir.join(b.filename);
        if path.exists() && !force {
            lines.push(format!("  kept    drivers/{}", b.filename));
        } else {
            std::fs::write(&path, b.content)?;
            #[cfg(unix)]
            {
                use std::os::unix::fs::PermissionsExt;
                std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755))?;
            }
            lines.push(format!("  created drivers/{}", b.filename));
        }

        if let Some(id) = b.driver_id
            && !cfg.drivers.iter().any(|d| d.id == id)
        {
            to_register.push((id, b.filename, b.default, b.timeout_secs));
            lines.push(format!("  added   [[driver]] {id} to keel.toml"));
        }
    }

    if !to_register.is_empty() {
        append_driver_entries(&paths.config(), &to_register)?;
    }

    // Non-driver assets ride along: they are scripts keel ships and the user
    // owns once written, exactly like a driver, but they are wired into config
    // by their own section rather than [[driver]].
    for a in builtin::ASSETS {
        let path = paths.keel().join(a.rel);
        if let Some(dir) = path.parent() {
            std::fs::create_dir_all(dir)?;
        }
        if path.exists() && !force {
            lines.push(format!("  kept    {}", a.rel));
        } else {
            std::fs::write(&path, a.content)?;
            #[cfg(unix)]
            {
                use std::os::unix::fs::PermissionsExt;
                std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755))?;
            }
            lines.push(format!("  created {}", a.rel));
        }
    }
    Ok(lines)
}

/// Append `[[driver]]` TOML blocks to the end of `keel.toml`'s raw text.
fn append_driver_entries(
    cfg_path: &std::path::Path,
    entries: &[(&'static str, &'static str, bool, u64)],
) -> Result<()> {
    let mut existing = std::fs::read_to_string(cfg_path)
        .map_err(|e| anyhow::anyhow!("reading {}: {e}", cfg_path.display()))?;
    if !existing.ends_with('\n') {
        existing.push('\n');
    }
    for (id, filename, default, timeout_secs) in entries {
        existing.push_str(&format!(
            "\n[[driver]]\nid = \"{id}\"\ncmd = \".keel/drivers/{filename}\"\ndefault = {default}\ntimeout_secs = {timeout_secs}\n"
        ));
    }
    std::fs::write(cfg_path, existing)
        .map_err(|e| anyhow::anyhow!("writing {}: {e}", cfg_path.display()))
}

pub fn select<'a>(cfg: &'a Config, id: Option<&str>) -> Result<&'a DriverConfig> {
    if let Some(id) = id {
        return cfg
            .drivers
            .iter()
            .find(|d| d.id == id)
            .ok_or_else(|| anyhow::anyhow!("no driver `{id}` in .keel/keel.toml"));
    }
    if let Some(d) = cfg.drivers.iter().find(|d| d.default) {
        return Ok(d);
    }
    match cfg.drivers.len() {
        0 => bail!("no drivers configured — add a [[driver]] block to .keel/keel.toml"),
        1 => Ok(&cfg.drivers[0]),
        _ => bail!("several drivers configured and none is default — pass --driver <id>"),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn scaffold_writes_every_reference_script_and_registers_it() {
        let dir = std::env::temp_dir().join(format!("keel-scaffold-{}", std::process::id()));
        std::fs::create_dir_all(dir.join(".keel")).unwrap();
        let home = Paths { repo: dir.clone() };
        let cfg = Config { drivers: vec![], ..Default::default() };
        cfg.save(&home.config()).unwrap();

        let lines = scaffold(&home, &cfg, false).unwrap();
        // scaffold appends to keel.toml as text; re-load to see what landed,
        // the same way a real caller (init, `keel driver scaffold`) would.
        let cfg = Config::load(&home.config()).unwrap();
        assert!(lines.iter().any(|l| l.contains("created drivers/claude-code")));
        assert!(lines.iter().any(|l| l.contains("created drivers/kiro")));
        // _common.sh is sourced by the others, not a driver of its own.
        assert!(!cfg.drivers.iter().any(|d| d.id == "_common.sh"));

        for id in ["claude-code", "codex", "copilot", "kiro", "noop"] {
            let d = cfg.drivers.iter().find(|d| d.id == id).unwrap_or_else(|| panic!("no {id} entry"));
            let script = dir.join(&d.cmd);
            assert!(script.is_file(), "{id}'s script was not written: {}", script.display());
            #[cfg(unix)]
            {
                use std::os::unix::fs::PermissionsExt;
                let mode = std::fs::metadata(&script).unwrap().permissions().mode();
                assert_ne!(mode & 0o111, 0, "{id}'s script is not executable");
            }
        }
        assert!(cfg.drivers.iter().find(|d| d.id == "claude-code").unwrap().default);
        assert!(!cfg.drivers.iter().find(|d| d.id == "kiro").unwrap().default);

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn scaffold_neither_overwrites_a_hand_edit_nor_reregisters_it_twice() {
        let dir = std::env::temp_dir().join(format!("keel-scaffold-idem-{}", std::process::id()));
        std::fs::create_dir_all(dir.join(".keel/drivers")).unwrap();
        std::fs::write(dir.join(".keel/drivers/kiro"), "#!/bin/sh\necho mine\n").unwrap();
        let home = Paths { repo: dir.clone() };
        let cfg = Config {
            drivers: vec![DriverConfig {
                id: "kiro".into(),
                cmd: ".keel/drivers/kiro".into(),
                default: false,
                timeout_secs: 900,
            }],
            ..Default::default()
        };
        cfg.save(&home.config()).unwrap();
        let toml_before = std::fs::read_to_string(home.config()).unwrap();

        scaffold(&home, &cfg, false).unwrap();
        assert_eq!(
            std::fs::read_to_string(dir.join(".keel/drivers/kiro")).unwrap(),
            "#!/bin/sh\necho mine\n",
            "an existing script must survive a non-forced scaffold"
        );

        let toml_after = std::fs::read_to_string(home.config()).unwrap();
        // Append-only: the bytes already on disk -- including kiro's own
        // entry, in its original position -- must be an untouched prefix.
        // A full round-trip through Config would instead reformat and
        // reorder the whole file, which is the bug this guards against.
        assert!(
            toml_after.starts_with(&toml_before),
            "scaffold must only append; the original file content must survive as a prefix.\nbefore:\n{toml_before}\nafter:\n{toml_after}"
        );

        let cfg = Config::load(&home.config()).unwrap();
        assert_eq!(
            cfg.drivers.iter().filter(|d| d.id == "kiro").count(),
            1,
            "an already-configured driver must not gain a second [[driver]] entry"
        );
        // The other four were genuinely missing, so they are the ones scaffold
        // should have added.
        for id in ["claude-code", "codex", "copilot", "noop"] {
            assert_eq!(
                cfg.drivers.iter().filter(|d| d.id == id).count(),
                1,
                "{id} was missing and should have been added exactly once"
            );
        }

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn a_relative_adapter_resolves_against_the_configuring_repo() {
        let dir = std::env::temp_dir().join(format!("keel-resolve-{}", std::process::id()));
        std::fs::create_dir_all(dir.join(".keel/drivers")).unwrap();
        std::fs::write(dir.join(".keel/drivers/x"), "#!/bin/sh\n").unwrap();
        let home = Paths { repo: dir.clone() };

        let resolved = resolve_program(&home, ".keel/drivers/x");
        assert!(std::path::Path::new(&resolved).is_absolute(), "{resolved}");
        assert!(resolved.ends_with(".keel/drivers/x"));

        // A bare name is PATH's business, not keel's.
        assert_eq!(resolve_program(&home, "claude"), "claude");
        // An absolute path is left alone.
        assert_eq!(resolve_program(&home, "/usr/bin/env"), "/usr/bin/env");
        // A relative path that does not exist is passed through, so the error
        // the caller sees is the shell's, not a rewritten one.
        assert_eq!(resolve_program(&home, "./nope/x"), "./nope/x");

        let _ = std::fs::remove_dir_all(&dir);
    }
}