omk 0.5.0

A Rust runtime for Kimi CLI. Turns prompts into proof-backed engineering runs with gates, worktrees, and replay.
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use crate::runtime::goal::review::pass::ReviewPass;
use crate::runtime::goal::review::slice::{
    SliceReviewArtifact, SliceReviewContext, SliceReviewOutcome,
};

/// Performance review pass — detects benchmark regressions by comparing
/// current criterion estimates against a recorded baseline.
///
/// Defaults:
/// - `worktree_path`: `std::env::current_dir()` or `"."`
/// - `regression_threshold_pct`: 5.0  (mean estimate increase > 5% =
///   regression finding)
/// - `tracked_benches`: empty → discover from `benches/*.rs` filenames
/// - `baseline_path`: `<worktree>/proof/baselines/perf.json`
pub struct PerformanceReviewPass {
    worktree_path: PathBuf,
    regression_threshold_pct: f64,
    tracked_benches: Vec<String>,
    baseline_path: Option<PathBuf>,
    /// Test injection: bypass file I/O and use these maps directly.
    injected_baseline: Option<BaselineMap>,
    injected_current: Option<BaselineMap>,
}

#[derive(Debug, Clone, Default)]
pub(crate) struct BaselineMap {
    // bench_id → mean estimate in nanoseconds
    entries: BTreeMap<String, f64>,
}

#[allow(dead_code)]
impl PerformanceReviewPass {
    pub fn new() -> Self {
        let worktree_path = match std::env::current_dir() {
            Ok(p) => p,
            Err(_) => PathBuf::from("."),
        };
        Self {
            worktree_path,
            regression_threshold_pct: 5.0,
            tracked_benches: Vec::new(),
            baseline_path: None,
            injected_baseline: None,
            injected_current: None,
        }
    }

    pub fn with_worktree_path(mut self, p: impl AsRef<Path>) -> Self {
        self.worktree_path = p.as_ref().to_path_buf();
        self
    }

    pub fn with_regression_threshold_pct(mut self, pct: f64) -> Self {
        self.regression_threshold_pct = pct;
        self
    }

    pub fn with_tracked_benches(mut self, names: Vec<String>) -> Self {
        self.tracked_benches = names;
        self
    }

    pub fn with_baseline_path(mut self, p: impl AsRef<Path>) -> Self {
        self.baseline_path = Some(p.as_ref().to_path_buf());
        self
    }

    #[cfg(test)]
    pub(crate) fn with_injected_baseline(mut self, m: BaselineMap) -> Self {
        self.injected_baseline = Some(m);
        self
    }

    #[cfg(test)]
    pub(crate) fn with_injected_current(mut self, m: BaselineMap) -> Self {
        self.injected_current = Some(m);
        self
    }

    fn resolve_baseline(&self) -> (BaselineMap, Option<String>) {
        if let Some(baseline) = &self.injected_baseline {
            return (baseline.clone(), None);
        }
        let path = match &self.baseline_path {
            Some(p) => p.clone(),
            None => self
                .worktree_path
                .join("proof")
                .join("baselines")
                .join("perf.json"),
        };
        match read_baseline_file(&path) {
            Ok(Some(b)) => (b, None),
            Ok(None) => (BaselineMap::default(), None),
            Err(e) => (BaselineMap::default(), Some(e)),
        }
    }

    fn resolve_current(&self) -> BaselineMap {
        if let Some(current) = &self.injected_current {
            return current.clone();
        }
        let benches = if self.tracked_benches.is_empty() {
            discover_benches(&self.worktree_path)
        } else {
            self.tracked_benches.clone()
        };
        let mut entries = BTreeMap::new();
        for bench_id in benches {
            if let Some(ns) = read_criterion_estimate(&self.worktree_path, &bench_id) {
                entries.insert(bench_id, ns);
            }
        }
        BaselineMap { entries }
    }
}

impl Default for PerformanceReviewPass {
    fn default() -> Self {
        Self::new()
    }
}

impl ReviewPass for PerformanceReviewPass {
    fn name(&self) -> &'static str {
        "performance"
    }

    fn run(&self, _ctx: &SliceReviewContext) -> SliceReviewOutcome {
        let (baseline, baseline_err) = self.resolve_baseline();
        let current = self.resolve_current();

        if baseline.entries.is_empty() && current.entries.is_empty() {
            let msg = if let Some(e) = baseline_err {
                format!("Performance review: no benchmark data ({e})")
            } else {
                "Performance review: no benchmark data".to_string()
            };
            return build_performance_outcome(true, msg.clone(), Some(msg), "low");
        }

        if baseline.entries.is_empty() {
            let mut msg = "Performance review: no baseline recorded; record one to enable regression detection".to_string();
            if let Some(e) = baseline_err {
                msg.push_str(&format!(" ({e})"));
            }
            return build_performance_outcome(true, msg.clone(), Some(msg), "low");
        }

        if current.entries.is_empty() {
            return build_performance_outcome(
                true,
                "Performance review: no current bench data; nothing to compare".to_string(),
                Some("Performance review: no current bench data; nothing to compare".to_string()),
                "low",
            );
        }

        let mut findings: Vec<String> = Vec::new();
        for (bench_id, baseline_ns) in &baseline.entries {
            if let Some(current_ns) = current.entries.get(bench_id) {
                if *baseline_ns > 0.0 {
                    let delta_pct = ((current_ns - baseline_ns) / baseline_ns) * 100.0;
                    // Strictly greater than threshold counts as regression.
                    if delta_pct > self.regression_threshold_pct {
                        findings.push(format!(
                            "regression in {}: {:.0} ns → {:.0} ns (+{:.1}%)",
                            bench_id, baseline_ns, current_ns, delta_pct
                        ));
                    }
                }
            }
        }

        let passed = findings.is_empty();
        let feedback = if passed {
            format!(
                "Performance review passed: {} benchmark(s) within {:.1}% threshold",
                baseline.entries.len().min(current.entries.len()),
                self.regression_threshold_pct
            )
        } else {
            format!("Performance review blocked: {}", findings.join("; "))
        };
        let severity = if passed { "low" } else { "medium" };

        build_performance_outcome(
            passed,
            feedback.clone(),
            if passed { None } else { Some(feedback) },
            severity,
        )
    }
}

fn build_performance_outcome(
    passed: bool,
    artifact_feedback: String,
    outcome_feedback: Option<String>,
    severity: &str,
) -> SliceReviewOutcome {
    SliceReviewOutcome {
        passed,
        review_path: None,
        security_review_path: None,
        feedback: outcome_feedback,
        artifacts: vec![SliceReviewArtifact {
            kind: "performance".to_string(),
            passed,
            feedback: artifact_feedback,
            severity: severity.to_string(),
        }],
        slop_findings: Vec::new(),
    }
}

fn discover_benches(worktree: &Path) -> Vec<String> {
    let benches_dir = worktree.join("benches");
    let mut names = Vec::new();
    if let Ok(dir_entries) = std::fs::read_dir(&benches_dir) {
        for entry in dir_entries.flatten() {
            let path = entry.path();
            if path.extension().map(|e| e == "rs").unwrap_or(false) {
                if let Some(stem) = path.file_stem() {
                    names.push(stem.to_string_lossy().to_string());
                }
            }
        }
    }
    names.sort();
    names
}

fn read_baseline_file(path: &Path) -> Result<Option<BaselineMap>, String> {
    if !path.exists() {
        return Ok(None);
    }
    let content =
        std::fs::read_to_string(path).map_err(|e| format!("cannot read baseline: {e}"))?;
    let entries: BTreeMap<String, f64> =
        serde_json::from_str(&content).map_err(|e| format!("malformed baseline: {e}"))?;
    Ok(Some(BaselineMap { entries }))
}

fn read_criterion_estimate(worktree: &Path, bench_id: &str) -> Option<f64> {
    let top = worktree
        .join("target")
        .join("criterion")
        .join(bench_id)
        .join("new")
        .join("estimates.json");
    if let Ok(content) = std::fs::read_to_string(&top) {
        if let Some(val) = parse_estimates_json(&content) {
            return Some(val);
        }
    }

    // Walk one level of subdirectories and sum (aggregate).
    let bench_dir = worktree.join("target").join("criterion").join(bench_id);
    let mut sum = 0.0;
    let mut found = false;
    if let Ok(dir_entries) = std::fs::read_dir(&bench_dir) {
        for entry in dir_entries.flatten() {
            let sub = entry.path();
            if sub.is_dir() {
                let estimates = sub.join("new").join("estimates.json");
                if let Ok(content) = std::fs::read_to_string(&estimates) {
                    if let Some(val) = parse_estimates_json(&content) {
                        sum += val;
                        found = true;
                    }
                }
            }
        }
    }

    if found {
        Some(sum)
    } else {
        None
    }
}

fn parse_estimates_json(content: &str) -> Option<f64> {
    let v: serde_json::Value = serde_json::from_str(content).ok()?;
    v.get("mean")?.get("point_estimate")?.as_f64()
}

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

    fn bm(entries: &[(&str, f64)]) -> BaselineMap {
        BaselineMap {
            entries: entries.iter().map(|(k, v)| (k.to_string(), *v)).collect(),
        }
    }

    #[test]
    fn name_is_stable() {
        assert_eq!("performance", PerformanceReviewPass::new().name());
    }

    #[test]
    fn passes_when_baseline_empty() {
        let pass = PerformanceReviewPass::new()
            .with_injected_baseline(bm(&[]))
            .with_injected_current(bm(&[("bench_a", 1000.0)]));
        let outcome = pass.run(&SliceReviewContext);
        assert!(outcome.passed);
        assert!(outcome.feedback.unwrap().contains("no baseline"));
    }

    #[test]
    fn passes_when_current_empty() {
        let pass = PerformanceReviewPass::new()
            .with_injected_baseline(bm(&[("bench_a", 1000.0)]))
            .with_injected_current(bm(&[]));
        let outcome = pass.run(&SliceReviewContext);
        assert!(outcome.passed);
        assert!(outcome.feedback.unwrap().contains("no current bench data"));
    }

    #[test]
    fn passes_when_no_regression() {
        let pass = PerformanceReviewPass::new()
            .with_regression_threshold_pct(5.0)
            .with_injected_baseline(bm(&[("bench_a", 1000.0)]))
            .with_injected_current(bm(&[("bench_a", 1020.0)]));
        let outcome = pass.run(&SliceReviewContext);
        assert!(outcome.passed);
        assert!(outcome
            .artifacts
            .iter()
            .any(|a| a.kind == "performance" && a.passed));
    }

    #[test]
    fn fails_when_regression_exceeds_threshold() {
        let pass = PerformanceReviewPass::new()
            .with_regression_threshold_pct(5.0)
            .with_injected_baseline(bm(&[("bench_a", 1000.0)]))
            .with_injected_current(bm(&[("bench_a", 1100.0)]));
        let outcome = pass.run(&SliceReviewContext);
        assert!(!outcome.passed);
        let fb = outcome.feedback.unwrap();
        assert!(fb.contains("regression in bench_a"), "{fb}");
        assert!(fb.contains("+10.0%"), "{fb}");
    }

    #[test]
    fn handles_multiple_benches_some_regressed() {
        let pass = PerformanceReviewPass::new()
            .with_regression_threshold_pct(5.0)
            .with_injected_baseline(bm(&[
                ("bench_a", 1000.0),
                ("bench_b", 500.0),
                ("bench_c", 200.0),
            ]))
            .with_injected_current(bm(&[
                ("bench_a", 1010.0),
                ("bench_b", 600.0),
                ("bench_c", 220.0),
            ]));
        let outcome = pass.run(&SliceReviewContext);
        assert!(!outcome.passed);
        let fb = outcome.feedback.unwrap();
        assert!(fb.contains("regression in bench_b"), "{fb}");
        assert!(fb.contains("regression in bench_c"), "{fb}");
        assert!(!fb.contains("regression in bench_a"), "{fb}");
    }

    #[test]
    fn handles_missing_bench_in_current_gracefully() {
        let pass = PerformanceReviewPass::new()
            .with_regression_threshold_pct(5.0)
            .with_injected_baseline(bm(&[("bench_a", 1000.0), ("bench_b", 500.0)]))
            .with_injected_current(bm(&[("bench_a", 1010.0)]));
        assert!(pass.run(&SliceReviewContext).passed);
    }

    #[test]
    fn handles_missing_baseline_for_some_benches() {
        let pass = PerformanceReviewPass::new()
            .with_regression_threshold_pct(5.0)
            .with_injected_baseline(bm(&[("bench_a", 1000.0)]))
            .with_injected_current(bm(&[("bench_a", 1010.0), ("bench_b", 5000.0)]));
        assert!(pass.run(&SliceReviewContext).passed);
    }

    #[test]
    fn threshold_boundary_at_exactly_threshold() {
        // Policy: strictly greater than threshold counts as regression.
        let pass = PerformanceReviewPass::new()
            .with_regression_threshold_pct(5.0)
            .with_injected_baseline(bm(&[("bench_a", 1000.0)]))
            .with_injected_current(bm(&[("bench_a", 1050.0)]));
        assert!(
            pass.run(&SliceReviewContext).passed,
            "exactly at threshold should pass"
        );
    }

    #[test]
    fn negative_delta_is_improvement_not_regression() {
        let pass = PerformanceReviewPass::new()
            .with_regression_threshold_pct(5.0)
            .with_injected_baseline(bm(&[("bench_a", 1000.0)]))
            .with_injected_current(bm(&[("bench_a", 800.0)]));
        assert!(pass.run(&SliceReviewContext).passed);
    }

    #[test]
    fn discover_benches_reads_filenames() {
        let tmp = tempfile::tempdir().unwrap();
        let benches = tmp.path().join("benches");
        std::fs::create_dir_all(&benches).unwrap();
        std::fs::write(benches.join("foo.rs"), "").unwrap();
        std::fs::write(benches.join("bar.rs"), "").unwrap();
        assert_eq!(discover_benches(tmp.path()), vec!["bar", "foo"]);
    }

    #[test]
    fn real_estimates_json_parsing() {
        let tmp = tempfile::tempdir().unwrap();
        let dir = tmp
            .path()
            .join("target")
            .join("criterion")
            .join("test_bench")
            .join("new");
        std::fs::create_dir_all(&dir).unwrap();
        let json = r#"{"mean":{"point_estimate":12345.0,"confidence_interval":{"lower_bound":12000.0,"upper_bound":12600.0}}}"#;
        std::fs::write(dir.join("estimates.json"), json).unwrap();

        let pass = PerformanceReviewPass::new()
            .with_worktree_path(tmp.path())
            .with_tracked_benches(vec!["test_bench".to_string()]);
        let outcome = pass.run(&SliceReviewContext);
        assert!(outcome.passed);
        assert!(outcome.feedback.unwrap().contains("no baseline"));
    }
}