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
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;

fn single_gate_config(
    name: &str,
    command: &str,
    args: Vec<String>,
    required: bool,
    timeout_secs: u64,
) -> omk::runtime::gates::VerificationConfig {
    omk::runtime::gates::VerificationConfig {
        gates: vec![omk::runtime::gates::GateDef {
            name: name.to_string(),
            command: command.to_string(),
            args,
            required,
            timeout_secs,
        }],
    }
}

#[cfg(unix)]
fn write_unix_script(path: &std::path::Path, body: &str) {
    // Synchronous I/O on purpose: a 100-byte test fixture has no benefit from
    // tokio's blocking-pool indirection, and using std::fs avoids subtle
    // file-handle / close-ordering interactions with the subsequent spawn
    // under heavy parallel-test load.
    std::fs::write(path, body).unwrap();
    std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).unwrap();
}

#[tokio::test]
async fn test_detect_gates_rust() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    tokio::fs::write(dir.join("Cargo.toml"), "[package]\n")
        .await
        .unwrap();

    let config = omk::runtime::gates::detect_gates(dir);
    let names: Vec<_> = config.gates.iter().map(|g| g.name.as_str()).collect();
    assert!(
        names.contains(&"format"),
        "Rust preset should include format gate"
    );
    assert!(
        names.contains(&"lint"),
        "Rust preset should include lint gate"
    );
    assert!(
        names.contains(&"check"),
        "Rust preset should include check gate"
    );
    assert!(
        names.contains(&"tests"),
        "Rust preset should include tests gate"
    );
}

#[tokio::test]
async fn test_detect_gates_node() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    tokio::fs::write(dir.join("package.json"), "{}\n")
        .await
        .unwrap();

    let config = omk::runtime::gates::detect_gates(dir);
    let names: Vec<_> = config.gates.iter().map(|g| g.name.as_str()).collect();
    assert!(
        names.contains(&"tests"),
        "Node preset should include tests gate"
    );
    assert!(
        names.contains(&"lint"),
        "Node preset should include lint gate"
    );
}

#[tokio::test]
async fn test_detect_gates_unknown() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();

    let config = omk::runtime::gates::detect_gates(dir);
    assert!(
        config.gates.is_empty(),
        "Unknown project should have no gates"
    );
}

#[tokio::test]
async fn test_gates_passed_all_required() {
    let results = vec![
        omk::runtime::gates::GateResult {
            name: "fmt".to_string(),
            passed: true,
            stdout: String::new(),
            stderr: String::new(),
            duration_ms: 100,
            required: true,
            command_line: "cargo fmt --check".to_string(),
            exit_code: Some(0),
            timed_out: false,
            stdout_summary: None,
            stderr_summary: None,
            output_path: None,
            timeout_secs: 120,
        },
        omk::runtime::gates::GateResult {
            name: "clippy".to_string(),
            passed: true,
            stdout: String::new(),
            stderr: String::new(),
            duration_ms: 200,
            required: true,
            command_line: "cargo clippy -- -D warnings".to_string(),
            exit_code: Some(0),
            timed_out: false,
            stdout_summary: None,
            stderr_summary: None,
            output_path: None,
            timeout_secs: 120,
        },
    ];
    assert!(omk::runtime::gates::gates_passed(&results));
}

#[tokio::test]
async fn test_gates_passed_optional_failure_ok() {
    let results = vec![
        omk::runtime::gates::GateResult {
            name: "fmt".to_string(),
            passed: true,
            stdout: String::new(),
            stderr: String::new(),
            duration_ms: 100,
            required: true,
            command_line: "cargo fmt --check".to_string(),
            exit_code: Some(0),
            timed_out: false,
            stdout_summary: None,
            stderr_summary: None,
            output_path: None,
            timeout_secs: 120,
        },
        omk::runtime::gates::GateResult {
            name: "coverage".to_string(),
            passed: false,
            stdout: String::new(),
            stderr: String::new(),
            duration_ms: 200,
            required: false,
            command_line: "cargo tarpaulin".to_string(),
            exit_code: Some(1),
            timed_out: false,
            stdout_summary: None,
            stderr_summary: None,
            output_path: None,
            timeout_secs: 120,
        },
    ];
    assert!(omk::runtime::gates::gates_passed(&results));
}

#[tokio::test]
async fn test_gates_passed_required_failure_fails() {
    let results = vec![
        omk::runtime::gates::GateResult {
            name: "fmt".to_string(),
            passed: true,
            stdout: String::new(),
            stderr: String::new(),
            duration_ms: 100,
            required: true,
            command_line: "cargo fmt --check".to_string(),
            exit_code: Some(0),
            timed_out: false,
            stdout_summary: None,
            stderr_summary: None,
            output_path: None,
            timeout_secs: 120,
        },
        omk::runtime::gates::GateResult {
            name: "clippy".to_string(),
            passed: false,
            stdout: String::new(),
            stderr: String::new(),
            duration_ms: 200,
            required: true,
            command_line: "cargo clippy -- -D warnings".to_string(),
            exit_code: Some(1),
            timed_out: false,
            stdout_summary: None,
            stderr_summary: None,
            output_path: None,
            timeout_secs: 120,
        },
    ];
    assert!(!omk::runtime::gates::gates_passed(&results));
}

#[tokio::test]
async fn test_done_contract_save_and_load() {
    let tmp = tempfile::tempdir().unwrap();
    let path = tmp.path().join("contract.json");

    let contract = omk::runtime::gates::DoneContract {
        run_name: "test-run".to_string(),
        mode: "autopilot".to_string(),
        started_at: chrono::Utc::now(),
        completed_at: chrono::Utc::now(),
        gates: vec![],
        changed_files: vec!["src/main.rs".to_string()],
        known_gaps: vec!["docs".to_string()],
        passed: true,
    };

    contract.save(&path).await.unwrap();
    let loaded = omk::runtime::gates::DoneContract::load(&path)
        .await
        .unwrap();

    assert_eq!(loaded.run_name, "test-run");
    assert_eq!(loaded.mode, "autopilot");
    assert!(loaded.passed);
    assert_eq!(loaded.changed_files, vec!["src/main.rs"]);
}

#[tokio::test]
async fn test_run_gates_success() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();

    let script = dir.join("success.sh");
    #[cfg(unix)]
    write_unix_script(&script, "#!/bin/sh\necho ok\n");
    #[cfg(windows)]
    tokio::fs::write(&script, "@echo ok\n").await.unwrap();

    let config = single_gate_config("success", script.to_str().unwrap(), vec![], true, 5);

    let results = omk::runtime::gates::run_gates(&config, dir).await;
    assert_eq!(results.len(), 1);
    assert!(results[0].passed, "Success script should pass");
}

#[tokio::test]
async fn test_run_gates_failure() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();

    let script = dir.join("fail.sh");
    #[cfg(unix)]
    write_unix_script(&script, "#!/bin/sh\necho error >&2\nexit 1\n");
    #[cfg(windows)]
    tokio::fs::write(&script, "@echo error\nexit /b 1\n")
        .await
        .unwrap();

    let config = single_gate_config("fail", script.to_str().unwrap(), vec![], true, 5);

    let results = omk::runtime::gates::run_gates(&config, dir).await;
    assert_eq!(results.len(), 1);
    assert!(!results[0].passed, "Fail script should not pass");
}

#[tokio::test]
async fn test_run_gates_with_evidence_writes_output_artifact_and_metadata() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    let artifacts = dir.join("artifacts");
    tokio::fs::create_dir_all(&artifacts).await.unwrap();

    let script = dir.join("evidence.sh");
    #[cfg(unix)]
    write_unix_script(
        &script,
        "#!/bin/sh\necho line-1\necho line-2\necho err-1 >&2\nexit 7\n",
    );
    #[cfg(windows)]
    tokio::fs::write(
        &script,
        "@echo line-1\r\n@echo line-2\r\n@echo err-1 1>&2\r\nexit /b 7\r\n",
    )
    .await
    .unwrap();

    let config = single_gate_config("evidence", script.to_str().unwrap(), vec![], true, 5);

    let results =
        omk::runtime::gates::run_gates_with_evidence(&config, dir, Some(&artifacts)).await;
    assert_eq!(results.len(), 1);
    let gate = &results[0];
    assert!(!gate.passed);
    assert_eq!(gate.exit_code, Some(7));
    assert!(!gate.timed_out);
    assert!(gate.command_line.contains("evidence"));
    assert!(gate
        .stdout_summary
        .as_deref()
        .unwrap_or_default()
        .contains("line-1"));
    assert!(gate
        .stderr_summary
        .as_deref()
        .unwrap_or_default()
        .contains("err-1"));
    let output_path = gate.output_path.as_ref().expect("expected output path");
    assert!(
        std::path::Path::new(output_path).exists(),
        "full output artifact should exist"
    );
    let full_output = std::fs::read_to_string(output_path).unwrap();
    assert!(full_output.contains("line-1"));
    assert!(full_output.contains("err-1"));
}

#[cfg(unix)]
#[tokio::test]
async fn test_run_gates_with_evidence_drains_large_output_before_waiting() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    let artifacts = dir.join("artifacts");
    tokio::fs::create_dir_all(&artifacts).await.unwrap();

    let script = dir.join("large-output.sh");
    write_unix_script(
        &script,
        r#"#!/bin/sh
awk 'BEGIN {
  for (i = 0; i < 2000; i++) {
    printf "stdout-line-%05d\n", i
    printf "stderr-line-%05d\n", i > "/dev/stderr"
  }
  exit 7
}'
"#,
    );

    let config = single_gate_config("large-output", script.to_str().unwrap(), vec![], true, 30);

    let results =
        omk::runtime::gates::run_gates_with_evidence(&config, dir, Some(&artifacts)).await;
    assert_eq!(results.len(), 1);
    let gate = &results[0];
    assert!(!gate.passed);
    assert_eq!(gate.exit_code, Some(7));
    assert!(!gate.timed_out);
    let output_path = gate.output_path.as_ref().expect("expected output path");
    let full_output = std::fs::read_to_string(output_path).unwrap();
    assert!(full_output.contains("stdout-line-00000"));
    assert!(full_output.contains("stderr-line-00000"));
}

#[tokio::test]
async fn test_run_gates_with_evidence_marks_timeout_without_artifact_dir() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    let script = dir.join("timeout.sh");
    #[cfg(unix)]
    write_unix_script(&script, "#!/bin/sh\nsleep 3600\necho done\n");
    #[cfg(windows)]
    tokio::fs::write(&script, "@ping 127.0.0.1 -n 3601 > nul\r\n@echo done\r\n")
        .await
        .unwrap();

    let config = single_gate_config("timeout", script.to_str().unwrap(), vec![], true, 3);

    let results = omk::runtime::gates::run_gates_with_evidence(&config, dir, None).await;
    assert_eq!(results.len(), 1);
    let gate = &results[0];
    assert!(!gate.passed);
    assert!(gate.timed_out);
    assert_eq!(gate.exit_code, None);
    assert!(gate.output_path.is_none());
    assert!(gate
        .stderr_summary
        .as_deref()
        .unwrap_or_default()
        .contains("Timed out after 3s"));
}

#[tokio::test]
async fn test_run_gates_keeps_compatibility_without_evidence_artifact() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();

    let script = dir.join("compat.sh");
    #[cfg(unix)]
    write_unix_script(&script, "#!/bin/sh\necho ok\n");
    #[cfg(windows)]
    tokio::fs::write(&script, "@echo ok\n").await.unwrap();

    let config = single_gate_config("compat", script.to_str().unwrap(), vec![], true, 5);

    let results = omk::runtime::gates::run_gates(&config, dir).await;
    assert_eq!(results.len(), 1);
    assert!(results[0].passed);
    assert!(results[0].output_path.is_none());
}

#[tokio::test]
async fn test_load_or_detect_gates_supports_custom_gate_without_args() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    let omk_dir = dir.join(".omk");
    tokio::fs::create_dir_all(&omk_dir).await.unwrap();
    tokio::fs::write(
        omk_dir.join("gates.toml"),
        r#"
[[gates]]
name = "custom"
command = "echo"
required = true
timeout_secs = 9
"#,
    )
    .await
    .unwrap();

    let config = omk::runtime::gates::load_or_detect_gates(dir).await;
    assert_eq!(config.gates.len(), 1);
    let gate = &config.gates[0];
    assert_eq!(gate.name, "custom");
    assert_eq!(gate.command, "echo");
    assert!(gate.args.is_empty());
    assert!(gate.required);
    assert_eq!(gate.timeout_secs, 9);
}

#[tokio::test]
async fn test_load_or_detect_gates_supports_allow_fail_and_skip_semantics() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    let omk_dir = dir.join(".omk");
    tokio::fs::create_dir_all(&omk_dir).await.unwrap();

    let fail_script = dir.join("fail.sh");
    #[cfg(unix)]
    write_unix_script(&fail_script, "#!/bin/sh\nexit 1\n");
    #[cfg(windows)]
    tokio::fs::write(&fail_script, "@exit /b 1\n")
        .await
        .unwrap();

    tokio::fs::write(
        omk_dir.join("gates.toml"),
        format!(
            r#"
[[gates]]
name = "allow-fail"
command = "{}"
allow-fail = true
timeout_secs = 5

[[gates]]
name = "skipped"
command = "definitely-not-a-real-command-omk"
skip = true
required = true
timeout_secs = 5
"#,
            fail_script.display()
        ),
    )
    .await
    .unwrap();

    let config = omk::runtime::gates::load_or_detect_gates(dir).await;
    assert_eq!(config.gates.len(), 2);

    let results = omk::runtime::gates::run_gates(&config, dir).await;
    assert_eq!(results.len(), 2);

    let allow_fail = results.iter().find(|g| g.name == "allow-fail").unwrap();
    assert!(!allow_fail.passed);
    assert!(!allow_fail.required);

    let skipped = results.iter().find(|g| g.name == "skipped").unwrap();
    assert!(skipped.passed);
    assert!(!skipped.required);
    assert!(skipped.command_line.contains("skipped"));
    assert!(omk::runtime::gates::gates_passed(&results));
}

#[tokio::test]
async fn test_run_gates_empty_config_returns_no_results() {
    let tmp = tempfile::tempdir().unwrap();
    let config = omk::runtime::gates::VerificationConfig { gates: vec![] };
    let results = omk::runtime::gates::run_gates(&config, tmp.path()).await;
    assert!(
        results.is_empty(),
        "empty gate config should yield no results, got {results:?}"
    );
}

#[tokio::test]
async fn test_run_gates_missing_command_with_timeout_reports_run_error() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    let config = single_gate_config(
        "missing",
        "this-binary-definitely-does-not-exist-omk-test",
        vec!["--probe".to_string()],
        true,
        5,
    );

    let results = omk::runtime::gates::run_gates(&config, dir).await;
    assert_eq!(results.len(), 1);
    let g = &results[0];
    assert!(!g.passed);
    assert_eq!(g.exit_code, None);
    assert!(!g.timed_out);
    assert!(g.output_path.is_none());
    assert!(g.required);
    assert_eq!(g.timeout_secs, 5);
    assert_eq!(
        g.command_line, "this-binary-definitely-does-not-exist-omk-test --probe",
        "command_line must equal render_command_line(cmd, args) exactly",
    );
    assert!(
        g.stderr.starts_with("Run error: "),
        "stderr should start with the documented explicit-timeout error prefix, got {:?}",
        g.stderr
    );
    assert_eq!(
        g.stderr_summary.as_deref(),
        Some(g.stderr.as_str()),
        "error-path summary must equal raw stderr (make_gate_error contract)",
    );
    assert!(g.stdout.is_empty(), "stdout should be empty on spawn error");
    assert!(
        g.stdout_summary.is_none(),
        "stdout_summary should be None when stdout is empty",
    );
}

#[tokio::test]
async fn test_run_gates_missing_command_default_timeout_reports_spawn_error() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    let config = single_gate_config(
        "missing-default-timeout",
        "this-binary-definitely-does-not-exist-omk-test",
        vec![],
        false,
        0,
    );

    let results = omk::runtime::gates::run_gates(&config, dir).await;
    assert_eq!(results.len(), 1);
    let g = &results[0];
    assert!(!g.passed);
    assert_eq!(g.exit_code, None);
    assert!(!g.timed_out);
    assert!(g.output_path.is_none());
    assert!(!g.required);
    assert_eq!(
        g.timeout_secs, 0,
        "timeout_secs == 0 must propagate verbatim"
    );
    assert_eq!(
        g.command_line, "this-binary-definitely-does-not-exist-omk-test",
        "render_command_line with empty args must equal the bare command",
    );
    assert!(
        g.stderr.starts_with("Spawn error: "),
        "stderr should start with the documented default-timeout error prefix, got {:?}",
        g.stderr
    );
    assert_eq!(
        g.stderr_summary.as_deref(),
        Some(g.stderr.as_str()),
        "error-path summary must equal raw stderr (make_gate_error contract)",
    );
    assert!(g.stdout.is_empty(), "stdout should be empty on spawn error");
    assert!(g.stdout_summary.is_none());
}

#[tokio::test]
async fn test_run_gates_skipped_gate_has_stable_result_shape() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    let omk_dir = dir.join(".omk");
    tokio::fs::create_dir_all(&omk_dir).await.unwrap();
    tokio::fs::write(
        omk_dir.join("gates.toml"),
        r#"
[[gates]]
name = "skipped-only"
command = "echo"
args = ["should-not-appear", "neither-this"]
skip = true
required = true
timeout_secs = 3
"#,
    )
    .await
    .unwrap();

    let config = omk::runtime::gates::load_or_detect_gates(dir).await;
    let results = omk::runtime::gates::run_gates(&config, dir).await;
    assert_eq!(results.len(), 1);
    let gate = &results[0];
    assert_eq!(gate.name, "skipped-only");
    assert!(gate.passed, "skipped gate must report passed=true");
    assert!(!gate.required, "skip forces required=false");
    assert!(gate.stdout.is_empty());
    assert_eq!(gate.stderr, "Skipped by gate config");
    // The skipped-gate command_line is a fixed placeholder — configured args
    // must NOT leak into the rendered command line, or downstream proof
    // consumers would treat skipped gates as if they had executed.
    assert_eq!(gate.command_line, "<skipped by config>");
    assert!(
        !gate.command_line.contains("should-not-appear"),
        "skipped gate must hide args from command_line, got {:?}",
        gate.command_line,
    );
    assert!(!gate.timed_out);
    assert_eq!(gate.exit_code, None);
    assert!(gate.output_path.is_none());
    assert_eq!(gate.timeout_secs, 3);
    assert!(gate.stdout_summary.is_none());
    assert_eq!(
        gate.stderr_summary.as_deref(),
        Some("Skipped by gate config")
    );
}

#[tokio::test]
async fn test_load_or_detect_gates_falls_back_on_empty_gate_name() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    let omk_dir = dir.join(".omk");
    tokio::fs::create_dir_all(&omk_dir).await.unwrap();
    tokio::fs::write(
        omk_dir.join("gates.toml"),
        r#"
[[gates]]
name = ""
command = "echo"
required = true
timeout_secs = 5
"#,
    )
    .await
    .unwrap();

    let config = omk::runtime::gates::load_or_detect_gates(dir).await;
    assert!(
        config.gates.is_empty(),
        "invalid gates.toml (empty name) must fall back to auto-detect for unknown project"
    );
}

#[tokio::test]
async fn test_load_or_detect_gates_falls_back_on_empty_command() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    let omk_dir = dir.join(".omk");
    tokio::fs::create_dir_all(&omk_dir).await.unwrap();
    tokio::fs::write(
        omk_dir.join("gates.toml"),
        r#"
[[gates]]
name = "bad-cmd"
command = ""
required = true
timeout_secs = 5
"#,
    )
    .await
    .unwrap();

    let config = omk::runtime::gates::load_or_detect_gates(dir).await;
    assert!(
        config.gates.is_empty(),
        "invalid gates.toml (empty command) must fall back to auto-detect for unknown project"
    );
}

#[tokio::test]
async fn test_load_or_detect_gates_falls_back_on_duplicate_names() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    let omk_dir = dir.join(".omk");
    tokio::fs::create_dir_all(&omk_dir).await.unwrap();
    tokio::fs::write(
        omk_dir.join("gates.toml"),
        r#"
[[gates]]
name = "dup"
command = "echo"
required = true
timeout_secs = 5

[[gates]]
name = "dup"
command = "echo"
required = true
timeout_secs = 5
"#,
    )
    .await
    .unwrap();

    let config = omk::runtime::gates::load_or_detect_gates(dir).await;
    assert!(
        config.gates.is_empty(),
        "invalid gates.toml (duplicate names) must fall back to auto-detect for unknown project"
    );
}

#[tokio::test]
async fn test_load_or_detect_gates_falls_back_on_timeout_too_large() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    let omk_dir = dir.join(".omk");
    tokio::fs::create_dir_all(&omk_dir).await.unwrap();
    tokio::fs::write(
        omk_dir.join("gates.toml"),
        r#"
[[gates]]
name = "huge-timeout"
command = "echo"
required = true
timeout_secs = 86401
"#,
    )
    .await
    .unwrap();

    let config = omk::runtime::gates::load_or_detect_gates(dir).await;
    assert!(
        config.gates.is_empty(),
        "invalid gates.toml (timeout > 86400) must fall back to auto-detect for unknown project"
    );
}

#[tokio::test]
async fn test_load_or_detect_gates_accepts_max_timeout() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    let omk_dir = dir.join(".omk");
    tokio::fs::create_dir_all(&omk_dir).await.unwrap();
    tokio::fs::write(
        omk_dir.join("gates.toml"),
        r#"
[[gates]]
name = "max-timeout"
command = "echo"
required = true
timeout_secs = 86400
"#,
    )
    .await
    .unwrap();

    let config = omk::runtime::gates::load_or_detect_gates(dir).await;
    assert_eq!(config.gates.len(), 1);
    assert_eq!(config.gates[0].name, "max-timeout");
    assert_eq!(config.gates[0].timeout_secs, 86400);
}

#[cfg(unix)]
#[tokio::test]
async fn test_run_gates_captures_exit_code_stdout_stderr_and_args() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();

    let script = dir.join("nonzero.sh");
    write_unix_script(
        &script,
        "#!/bin/sh\necho \"out:$1\"\necho \"err:$2\" >&2\nexit 42\n",
    );

    let config = single_gate_config(
        "nonzero",
        script.to_str().unwrap(),
        vec!["alpha".to_string(), "beta".to_string()],
        true,
        5,
    );

    let results = omk::runtime::gates::run_gates(&config, dir).await;
    assert_eq!(results.len(), 1);
    let g = &results[0];
    assert!(!g.passed);
    assert_eq!(g.exit_code, Some(42));
    assert!(!g.timed_out);
    assert!(g.required);
    assert_eq!(g.timeout_secs, 5);
    assert!(
        g.stdout.contains("out:alpha"),
        "stdout should capture script output verbatim, got {:?}",
        g.stdout
    );
    assert!(
        g.stderr.contains("err:beta"),
        "stderr should capture script stderr verbatim, got {:?}",
        g.stderr
    );
    assert!(
        g.stdout_summary
            .as_deref()
            .unwrap_or_default()
            .contains("out:alpha"),
        "stdout_summary should reflect the first lines"
    );
    assert!(
        g.stderr_summary
            .as_deref()
            .unwrap_or_default()
            .contains("err:beta"),
        "stderr_summary should reflect the first lines"
    );
    let expected_command_line = format!("{} alpha beta", script.display());
    assert_eq!(
        g.command_line, expected_command_line,
        "command_line must equal render_command_line(cmd, args) exactly",
    );
    assert!(g.output_path.is_none(), "no artifact dir → no output_path");
}

#[tokio::test]
async fn test_gate_result_json_uses_stable_field_names() {
    // Canonical on-disk shape that proof/state consumers depend on. Any rename
    // (e.g. command_line → commandLine via #[serde(rename)]) must break this
    // test — a symmetric round-trip would not, because it would silently
    // serialize and deserialize through the same alias.
    let canonical = r#"{
        "name": "fmt",
        "passed": false,
        "stdout": "out-line-1\nout-line-2",
        "stderr": "err-line-1",
        "duration_ms": 1234,
        "required": true,
        "command_line": "cargo fmt --check",
        "exit_code": 101,
        "timed_out": false,
        "stdout_summary": "out-line-1",
        "stderr_summary": "err-line-1",
        "output_path": "/tmp/log",
        "timeout_secs": 30
    }"#;
    let parsed: omk::runtime::gates::GateResult =
        serde_json::from_str(canonical).expect("canonical JSON must deserialize");

    assert_eq!(parsed.name, "fmt");
    assert!(!parsed.passed);
    assert_eq!(parsed.stdout, "out-line-1\nout-line-2");
    assert_eq!(parsed.stderr, "err-line-1");
    assert_eq!(parsed.duration_ms, 1234);
    assert!(parsed.required);
    assert_eq!(parsed.command_line, "cargo fmt --check");
    assert_eq!(parsed.exit_code, Some(101));
    assert!(!parsed.timed_out);
    assert_eq!(parsed.stdout_summary.as_deref(), Some("out-line-1"));
    assert_eq!(parsed.stderr_summary.as_deref(), Some("err-line-1"));
    assert_eq!(parsed.output_path.as_deref(), Some("/tmp/log"));
    assert_eq!(parsed.timeout_secs, 30);

    let serialized = serde_json::to_string(&parsed).expect("serialize GateResult");
    // The serialized JSON must use exactly this canonical, closed key set. A
    // rename (e.g. command_line → commandLine) and an *unannounced addition*
    // (e.g. a new `pub field: T`) are both wire-shape drift that downstream
    // proof/state consumers cannot absorb silently — assert key set equality,
    // not just presence.
    let value: serde_json::Value =
        serde_json::from_str(&serialized).expect("serialized JSON must reparse");
    let actual_keys: std::collections::BTreeSet<&str> = value
        .as_object()
        .expect("GateResult must serialize as a JSON object")
        .keys()
        .map(String::as_str)
        .collect();
    let expected_keys: std::collections::BTreeSet<&str> = [
        "command_line",
        "duration_ms",
        "exit_code",
        "name",
        "output_path",
        "passed",
        "required",
        "stderr",
        "stderr_summary",
        "stdout",
        "stdout_summary",
        "timed_out",
        "timeout_secs",
    ]
    .into_iter()
    .collect();
    assert_eq!(
        actual_keys, expected_keys,
        "GateResult on-disk key set must stay closed; any rename or addition breaks proof/state consumers",
    );
}

#[tokio::test]
async fn test_gate_result_deserializes_with_legacy_minimal_fields() {
    let json = r#"{
        "name": "legacy",
        "passed": true,
        "stdout": "",
        "stderr": "",
        "duration_ms": 17,
        "required": true
    }"#;
    let parsed: omk::runtime::gates::GateResult =
        serde_json::from_str(json).expect("legacy GateResult JSON must deserialize");
    assert_eq!(parsed.name, "legacy");
    assert!(parsed.passed);
    assert!(parsed.required);
    assert_eq!(parsed.duration_ms, 17);
    assert_eq!(parsed.command_line, "");
    assert_eq!(parsed.exit_code, None);
    assert!(!parsed.timed_out);
    assert!(parsed.stdout_summary.is_none());
    assert!(parsed.stderr_summary.is_none());
    assert!(parsed.output_path.is_none());
    assert_eq!(parsed.timeout_secs, 0);
}

#[cfg(unix)]
#[tokio::test]
async fn test_run_gates_summary_truncates_to_three_lines_with_overflow_marker() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    let script = dir.join("many-lines.sh");
    write_unix_script(
        &script,
        "#!/bin/sh\nfor i in 1 2 3 4 5; do echo \"out-$i\"; done\nfor i in 1 2 3 4 5; do echo \"err-$i\" >&2; done\nexit 1\n",
    );

    let config = single_gate_config("many-lines", script.to_str().unwrap(), vec![], true, 30);

    let results = omk::runtime::gates::run_gates(&config, dir).await;
    assert_eq!(results.len(), 1);
    let g = &results[0];
    assert!(!g.passed);
    assert_eq!(g.exit_code, Some(1));

    let stdout_summary = g
        .stdout_summary
        .as_deref()
        .expect("stdout_summary must be set");
    let stdout_lines: Vec<&str> = stdout_summary.lines().collect();
    assert_eq!(
        stdout_lines,
        vec!["out-1", "out-2", "out-3", "..."],
        "stdout summary must keep first 3 lines plus '...' overflow marker",
    );

    let stderr_summary = g
        .stderr_summary
        .as_deref()
        .expect("stderr_summary must be set");
    let stderr_lines: Vec<&str> = stderr_summary.lines().collect();
    assert_eq!(
        stderr_lines,
        vec!["err-1", "err-2", "err-3", "..."],
        "stderr summary must keep first 3 lines plus '...' overflow marker",
    );

    assert!(
        g.stdout.lines().count() >= 5,
        "raw stdout must keep all 5 lines verbatim, got {} lines",
        g.stdout.lines().count(),
    );
    assert!(
        g.stderr.lines().count() >= 5,
        "raw stderr must keep all 5 lines verbatim, got {} lines",
        g.stderr.lines().count(),
    );
}

#[cfg(unix)]
#[tokio::test]
async fn test_run_gates_summary_truncates_long_lines_to_240_chars() {
    let tmp = tempfile::tempdir().unwrap();
    let dir = tmp.path();
    let script = dir.join("long-line.sh");
    // 300 'x' characters on one line — well past the documented 240-char cap.
    // The script must not depend on `seq`/`printf` subprocesses so it stays
    // deterministic when many tests spawn children in parallel.
    let body = format!("#!/bin/sh\necho '{}'\nexit 0\n", "x".repeat(300));
    write_unix_script(&script, &body);

    let config = single_gate_config(
        "long-line",
        "/bin/sh",
        vec![script.to_string_lossy().into_owned()],
        true,
        5,
    );

    let results = omk::runtime::gates::run_gates(&config, dir).await;
    assert_eq!(results.len(), 1);
    let g = &results[0];
    assert!(
        g.passed,
        "long-line gate must succeed; exit_code={:?}, stderr={:?}",
        g.exit_code, g.stderr,
    );
    let summary = g
        .stdout_summary
        .as_deref()
        .expect("stdout_summary must be set");
    let first_line = summary.lines().next().expect("summary has at least 1 line");
    assert!(
        first_line.ends_with("..."),
        "summary line longer than 240 chars must end with '...': {first_line:?}",
    );
    assert_eq!(
        first_line.chars().count(),
        243,
        "summary line cap is 240 chars + trailing '...' (3 chars)",
    );
    assert!(
        g.stdout.lines().next().unwrap().chars().count() >= 300,
        "raw stdout must keep the full 300-char line verbatim",
    );
}