keel-harness 0.6.2

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
//! The gate contract (PLAN.md P1, ยง4.4).
//!
//! > A gate is a predicate over artefacts that returns `pass | fail | blocked`,
//! > with the evidence attached. If a gate cannot fail, it is documentation,
//! > not a gate.
//!
//! `blocked` is deliberately distinct from `fail`: the check could not run at
//! all (missing tool, no network, absent index). A blocked check never silently
//! passes, and โ€” per P6 โ€” never counts as an agentic failure either. Collapsing
//! it into `fail` teaches the failure taxonomy to lie; collapsing it into
//! `pass` is the gate theatre this whole design exists to prevent.

pub mod g0;
pub mod g1;
pub mod g2;
pub mod g25;
pub mod g3;
pub mod g4;
pub mod diff;
pub mod oracle_exec;
pub mod ratchet;

use crate::config::{CheckPlugin, Config};
use crate::paths::Paths;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};

pub const GATE_SCHEMA: &str = "keel.gate/1";

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Verdict {
    Pass,
    Fail,
    Blocked,
}

impl Verdict {
    pub fn glyph(&self) -> &'static str {
        match self {
            Verdict::Pass => "pass",
            Verdict::Fail => "FAIL",
            Verdict::Blocked => "BLOCKED",
        }
    }

    /// The same word, coloured when stdout is a terminal.
    ///
    /// Separate from [`Verdict::glyph`] because that one also produces the wire
    /// value written into `run.json` and the trajectory, where an escape
    /// sequence would be corruption rather than decoration.
    pub fn glyph_styled(&self) -> String {
        let g = self.glyph();
        match self {
            Verdict::Pass => crate::ui::green(g),
            Verdict::Fail => crate::ui::red(g),
            Verdict::Blocked => crate::ui::yellow(g),
        }
    }

    /// Exit code for a gate verdict. `blocked` is distinct from `fail` on the
    /// wire too, so a caller can tell "you broke it" from "I could not look".
    pub fn exit_code(&self) -> i32 {
        match self {
            Verdict::Pass => 0,
            Verdict::Fail => 1,
            Verdict::Blocked => 3,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Check {
    pub id: String,
    pub verdict: Verdict,
    /// What the check required.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub expected: Option<String>,
    /// What it found.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub actual: Option<String>,
    /// One line a human can act on.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub detail: Option<String>,
    /// Path to the artefact backing this verdict, relative to the gate file.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub evidence: Option<String>,
    /// Where this check came from, e.g. `lesson:L-0012`. Answers "why?".
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub from: Option<String>,
}

impl Check {
    pub fn pass(id: &str, detail: impl Into<String>) -> Self {
        Self { id: id.into(), verdict: Verdict::Pass, expected: None, actual: None,
               detail: Some(detail.into()), evidence: None, from: None }
    }

    pub fn fail(id: &str, expected: impl Into<String>, actual: impl Into<String>) -> Self {
        Self { id: id.into(), verdict: Verdict::Fail, expected: Some(expected.into()),
               actual: Some(actual.into()), detail: None, evidence: None, from: None }
    }

    pub fn blocked(id: &str, why: impl Into<String>) -> Self {
        Self { id: id.into(), verdict: Verdict::Blocked, expected: None, actual: None,
               detail: Some(why.into()), evidence: None, from: None }
    }

    /// A single line for the terminal.
    pub fn line(&self) -> String {
        // Pad on the plain word: escape sequences have width on the wire but
        // not on the screen, so colouring before padding skews every column.
        let pad = " ".repeat(8usize.saturating_sub(self.verdict.glyph().len()));
        let mut s = format!("  {}{pad} {}", self.verdict.glyph_styled(), self.id);
        if let Some(d) = &self.detail {
            s.push_str(&format!(" โ€” {d}"));
        } else if let (Some(e), Some(a)) = (&self.expected, &self.actual) {
            s.push_str(&format!(
                "\n           {} {e}\n           {}   {a}",
                crate::ui::dim("expected:"),
                crate::ui::dim("actual:")
            ));
        }
        if let Some(from) = &self.from {
            s.push_str(&crate::ui::dim(&format!("  [{from}]")));
        }
        s
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GateResult {
    pub schema: String,
    pub gate: String,
    pub run: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub spec: Option<String>,
    pub verdict: Verdict,
    pub generated_at: String,
    pub checks: Vec<Check>,
}

impl GateResult {
    pub fn new(paths: &Paths, gate: &str, spec: Option<String>, checks: Vec<Check>) -> Self {
        Self {
            schema: GATE_SCHEMA.to_string(),
            gate: gate.to_string(),
            run: run_id(paths),
            spec,
            verdict: roll_up(&checks),
            generated_at: chrono::Local::now().to_rfc3339(),
            checks,
        }
    }

    pub fn write(&self, dir: &Path) -> Result<PathBuf> {
        std::fs::create_dir_all(dir)?;
        let path = dir.join(format!("{}.json", self.gate));
        let json = serde_json::to_string_pretty(self)?;
        crate::atomic::write(&path, &format!("{json}\n"))?;
        Ok(path)
    }

    pub fn read(path: &Path) -> Result<Self> {
        let raw = std::fs::read_to_string(path)
            .with_context(|| format!("reading {}", path.display()))?;
        serde_json::from_str(&raw).with_context(|| format!("parsing {}", path.display()))
    }

    pub fn counts(&self) -> (usize, usize, usize) {
        let p = self.checks.iter().filter(|c| c.verdict == Verdict::Pass).count();
        let f = self.checks.iter().filter(|c| c.verdict == Verdict::Fail).count();
        let b = self.checks.iter().filter(|c| c.verdict == Verdict::Blocked).count();
        (p, f, b)
    }
}

/// Any failure fails the gate; otherwise any blocked check blocks it.
///
/// A gate with no checks at all is `blocked`, not `pass` โ€” an empty gate is a
/// misconfiguration, and reporting it as success is precisely how a pipeline
/// ends up with gates that cannot fail.
pub fn roll_up(checks: &[Check]) -> Verdict {
    if checks.is_empty() {
        return Verdict::Blocked;
    }
    if checks.iter().any(|c| c.verdict == Verdict::Fail) {
        return Verdict::Fail;
    }
    if checks.iter().any(|c| c.verdict == Verdict::Blocked) {
        return Verdict::Blocked;
    }
    Verdict::Pass
}

/// `2026-08-21-7c1` โ€” sortable by date, and the suffix is a per-day counter
/// derived from the highest existing suffix for today, so lexicographic order
/// matches creation order within a day (not just a hash that happens to look
/// sortable).
pub fn run_id(paths: &Paths) -> String {
    let today = crate::store::today();
    let next = next_run_seq(paths, &today);
    format!("{today}-{next:03x}")
}

/// One past the highest run-id suffix already on disk for `today`.
fn next_run_seq(paths: &Paths, today: &str) -> u64 {
    let prefix = format!("{today}-");
    std::fs::read_dir(paths.runs())
        .into_iter()
        .flatten()
        .filter_map(|e| e.ok())
        .filter_map(|e| e.file_name().into_string().ok())
        .filter_map(|name| name.strip_prefix(prefix.as_str()).map(str::to_string))
        .filter_map(|suffix| u64::from_str_radix(&suffix, 16).ok())
        .max()
        .map_or(0, |m| m + 1)
}

/// Directory holding gate results for a spec.
pub fn dir_for(paths: &Paths, slug: &str) -> PathBuf {
    crate::spec::Spec::dir(paths, slug).join("gates")
}

/// Load a previously recorded verdict, if any.
pub fn previous(paths: &Paths, slug: &str, gate: &str) -> Option<GateResult> {
    let p = dir_for(paths, slug).join(format!("{gate}.json"));
    GateResult::read(&p).ok()
}

// ---------------------------------------------------------------------------
// External checks (P7)
// ---------------------------------------------------------------------------

/// Run the configured plugin checks for a gate.
///
/// A plugin that cannot be executed yields `blocked`, never `fail`: the
/// distinction is the whole reason the third verdict exists.
pub fn run_plugins(paths: &Paths, cfg: &Config, gate: &str, slug: Option<&str>) -> Vec<Check> {
    let Some(gate_cfg) = cfg.gate.get(gate) else { return vec![] };
    gate_cfg
        .checks
        .iter()
        .map(|plugin| run_plugin(paths, plugin, gate, slug))
        .collect()
}

fn run_plugin(paths: &Paths, plugin: &CheckPlugin, gate: &str, slug: Option<&str>) -> Check {
    let mut parts = plugin.cmd.split_whitespace().map(|s| s.to_string()).collect::<Vec<_>>();
    if parts.is_empty() {
        return Check::blocked(&plugin.id, "check has an empty cmd");
    }
    let program = parts.remove(0);

    let mut command = std::process::Command::new(&program);
    command
        .args(&parts)
        .current_dir(&paths.repo)
        .env("KEEL_REPO", &paths.repo)
        .env("KEEL_STORE", paths.store())
        .env("KEEL_GATE", gate);
    if let Some(s) = slug {
        command.env("KEEL_SPEC", s).env("KEEL_SPEC_DIR", crate::spec::Spec::dir(paths, s));
    }

    let output = match command.output() {
        Ok(o) => o,
        Err(e) => {
            return with_from(
                Check::blocked(&plugin.id, format!("could not run `{}`: {e}", plugin.cmd)),
                plugin,
            );
        }
    };

    let stdout = String::from_utf8_lossy(&output.stdout);
    let check = match serde_json::from_str::<Check>(stdout.trim()) {
        Ok(mut c) => {
            // The plugin does not get to rename itself.
            c.id = plugin.id.clone();
            c
        }
        Err(_) if stdout.trim().is_empty() && output.status.success() => {
            Check::pass(&plugin.id, "exited 0 with no output")
        }
        Err(e) => Check::blocked(
            &plugin.id,
            format!(
                "did not print a valid check result ({e}); stderr: {}",
                truncate(String::from_utf8_lossy(&output.stderr).trim(), 160)
            ),
        ),
    };
    with_from(check, plugin)
}

fn with_from(mut c: Check, plugin: &CheckPlugin) -> Check {
    if c.from.is_none() {
        c.from = plugin.from.clone();
    }
    c
}

/// Join a list for a gate message, capping it so one failing check cannot
/// produce a paragraph nobody reads.
pub(crate) fn join_capped(items: &[String], cap: usize) -> String {
    if items.len() <= cap {
        return items.join(", ");
    }
    format!(
        "{}, โ€ฆ and {} more",
        items[..cap].join(", "),
        items.len() - cap
    )
}

pub(crate) fn truncate(s: &str, max: usize) -> String {
    if s.chars().count() <= max { return s.to_string(); }
    s.chars().take(max.saturating_sub(1)).chain(['โ€ฆ']).collect()
}

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

    #[test]
    fn any_failure_fails_the_gate() {
        let checks = vec![
            Check::pass("a", "fine"),
            Check::blocked("b", "no network"),
            Check::fail("c", "0 drift", "1 drift"),
        ];
        assert_eq!(roll_up(&checks), Verdict::Fail);
    }

    #[test]
    fn blocked_never_silently_passes() {
        let checks = vec![Check::pass("a", "fine"), Check::blocked("b", "tool missing")];
        assert_eq!(roll_up(&checks), Verdict::Blocked);
        assert_ne!(roll_up(&checks), Verdict::Pass);
    }

    #[test]
    fn an_empty_gate_is_blocked_not_passed() {
        assert_eq!(roll_up(&[]), Verdict::Blocked);
    }

    #[test]
    fn all_passing_passes() {
        assert_eq!(roll_up(&[Check::pass("a", "x"), Check::pass("b", "y")]), Verdict::Pass);
    }

    #[test]
    fn verdicts_have_distinct_exit_codes() {
        assert_eq!(Verdict::Pass.exit_code(), 0);
        assert_eq!(Verdict::Fail.exit_code(), 1);
        assert_eq!(Verdict::Blocked.exit_code(), 3);
    }

    fn scratch_paths(name: &str) -> Paths {
        let repo = std::env::temp_dir().join(format!("keel-gate-test-{name}-{}", std::process::id()));
        Paths { repo }
    }

    #[test]
    fn gate_result_round_trips_through_json() {
        let paths = scratch_paths("round-trip");
        let r = GateResult::new(&paths, "G0", Some("rate-limit".into()), vec![
            Check::fail("oracle-presence", "every criterion has an oracle", "AC-2 has none"),
        ]);
        let json = serde_json::to_string(&r).unwrap();
        let back: GateResult = serde_json::from_str(&json).unwrap();
        assert_eq!(back.schema, GATE_SCHEMA);
        assert_eq!(back.gate, "G0");
        assert_eq!(back.verdict, Verdict::Fail);
        assert_eq!(back.checks[0].expected.as_deref(), Some("every criterion has an oracle"));
    }

    #[test]
    fn long_lists_are_capped_in_gate_messages() {
        let items: Vec<String> = (1..=9).map(|n| format!("T-{n}")).collect();
        let out = join_capped(&items, 5);
        assert!(out.starts_with("T-1, T-2, T-3, T-4, T-5"), "{out}");
        assert!(out.ends_with("and 4 more"), "{out}");
        assert_eq!(join_capped(&items[..3], 5), "T-1, T-2, T-3");
    }

    #[test]
    fn run_ids_are_dated_and_distinct() {
        let paths = scratch_paths("dated");
        let a = run_id(&paths);
        assert!(a.starts_with(&crate::store::today()), "{a}");
        assert_eq!(a.len(), crate::store::today().len() + 4);
    }

    /// Two runs created moments apart within the same day must sort in
    /// creation order โ€” the bug this replaces was a hash that had no relation
    /// to when a run was actually created.
    #[test]
    fn same_day_run_ids_sort_in_creation_order() {
        let paths = scratch_paths("same-day-order");
        std::fs::create_dir_all(paths.runs()).unwrap();

        let mut ids = Vec::new();
        for _ in 0..20 {
            let id = run_id(&paths);
            std::fs::create_dir_all(paths.runs().join(&id)).unwrap();
            ids.push(id);
        }

        let mut sorted = ids.clone();
        sorted.sort();
        assert_eq!(ids, sorted, "run ids must sort lexicographically in creation order");
    }
}