rkat 0.6.21

CLI for the Meerkat agent platform — run LLM agents from the terminal
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
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
#![cfg(feature = "integration-real-tests")]
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

use std::path::PathBuf;
use tokio::process::Command;
use tokio::time::{Duration, timeout};

use meerkat::Config;
use tempfile::TempDir;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn rkat_binary_path() -> Option<PathBuf> {
    if let Some(path) = std::env::var_os("CARGO_BIN_EXE_rkat") {
        let path = PathBuf::from(path);
        if path.exists() {
            return Some(path.canonicalize().unwrap_or(path));
        }
    }

    if let Some(target_dir) = std::env::var_os("CARGO_TARGET_DIR") {
        let target_dir = PathBuf::from(target_dir);
        let debug = target_dir.join("debug/rkat");
        if debug.exists() {
            return Some(debug);
        }
        let release = target_dir.join("release/rkat");
        if release.exists() {
            return Some(release);
        }
    }

    let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    let workspace_root = manifest_dir.parent()?;
    let codex_debug = workspace_root.join("target-codex/debug/rkat");
    if codex_debug.exists() {
        return Some(codex_debug);
    }
    let codex_release = workspace_root.join("target-codex/release/rkat");
    if codex_release.exists() {
        return Some(codex_release);
    }
    let debug = workspace_root.join("target/debug/rkat");
    if debug.exists() {
        return Some(debug);
    }
    let release = workspace_root.join("target/release/rkat");
    if release.exists() {
        return Some(release);
    }
    None
}

fn anthropic_api_key() -> Option<String> {
    first_env(&["RKAT_ANTHROPIC_API_KEY", "ANTHROPIC_API_KEY"])
}

fn openai_api_key() -> Option<String> {
    first_env(&["RKAT_OPENAI_API_KEY", "OPENAI_API_KEY"])
}

fn first_env(vars: &[&str]) -> Option<String> {
    for name in vars {
        if let Ok(value) = std::env::var(name)
            && !value.is_empty()
        {
            return Some(value);
        }
    }
    None
}

fn smoke_model() -> String {
    std::env::var("SMOKE_MODEL").unwrap_or_else(|_| "claude-sonnet-4-5".to_string())
}

async fn read_jsonl_files_under(
    root: &std::path::Path,
) -> Result<String, Box<dyn std::error::Error>> {
    let mut stack = vec![root.to_path_buf()];
    let mut combined = String::new();
    while let Some(dir) = stack.pop() {
        let mut entries = match tokio::fs::read_dir(&dir).await {
            Ok(entries) => entries,
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => continue,
            Err(err) => return Err(Box::new(err)),
        };
        while let Some(entry) = entries.next_entry().await? {
            let path = entry.path();
            let file_type = entry.file_type().await?;
            if file_type.is_dir() {
                stack.push(path);
            } else if path
                .extension()
                .and_then(|ext| ext.to_str())
                .is_some_and(|ext| ext == "jsonl")
            {
                combined.push_str(&tokio::fs::read_to_string(path).await?);
                combined.push('\n');
            }
        }
    }
    Ok(combined)
}

/// Returns `true` if prerequisites are missing and the test should be skipped.
fn skip_if_no_prereqs() -> bool {
    if rkat_binary_path().is_none() {
        eprintln!("Skipping: rkat binary not found (build with `cargo build -p meerkat-cli`)");
        return true;
    }
    false
}

/// Returns `true` if API prereqs (binary + key) are missing.
fn skip_if_no_api_prereqs() -> bool {
    if skip_if_no_prereqs() {
        return true;
    }
    if anthropic_api_key().is_none() {
        eprintln!(
            "Skipping: no Anthropic API key (set ANTHROPIC_API_KEY or RKAT_ANTHROPIC_API_KEY)"
        );
        return true;
    }
    false
}

/// Returns `true` if OpenAI API prereqs (binary + key) are missing.
fn skip_if_no_openai_api_prereqs() -> bool {
    if skip_if_no_prereqs() {
        return true;
    }
    if openai_api_key().is_none() {
        eprintln!("Skipping: no OpenAI API key (set OPENAI_API_KEY or RKAT_OPENAI_API_KEY)");
        return true;
    }
    false
}

/// Write a minimal config.toml into the `.rkat/` directory inside `project_dir`.
async fn write_smoke_config(
    project_dir: &std::path::Path,
) -> Result<(), Box<dyn std::error::Error>> {
    let rkat_dir = project_dir.join(".rkat");
    tokio::fs::create_dir_all(&rkat_dir).await?;

    let mut config = Config::default();
    config.agent.max_tokens_per_turn = 256;
    config.agent.model = smoke_model();
    let config_toml = toml::to_string_pretty(&config)?;
    tokio::fs::write(rkat_dir.join("config.toml"), config_toml).await?;
    Ok(())
}

// ===========================================================================
// Scenario 26: CLI run + resume + verify persistence
// ===========================================================================

#[tokio::test]
#[ignore = "lane:e2e-live"]
async fn e2e_scenario_26_cli_run_resume_persistence() -> Result<(), Box<dyn std::error::Error>> {
    if skip_if_no_api_prereqs() {
        return Ok(());
    }

    // Outer test: spawn inner with isolated HOME / XDG dirs
    if std::env::var("RUN_TEST_E2E_SMOKE_11_INNER").is_ok() {
        return inner_e2e_cli_run_resume_persistence().await;
    }

    let temp_dir = TempDir::new()?;
    let project_dir = temp_dir.path().join("project");
    tokio::fs::create_dir_all(project_dir.join(".rkat")).await?;

    let data_dir = temp_dir.path().join("data");
    tokio::fs::create_dir_all(&data_dir).await?;
    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;

    let status = Command::new(std::env::current_exe()?)
        .arg("e2e_scenario_26_cli_run_resume_persistence")
        .arg("--ignored")
        .env("RUN_TEST_E2E_SMOKE_11_INNER", "1")
        .env("CARGO_BIN_EXE_rkat", &rkat)
        .env("HOME", temp_dir.path())
        .env("XDG_DATA_HOME", &data_dir)
        .env("TEST_PROJECT_DIR", &project_dir)
        .env("TEST_DATA_DIR", &data_dir)
        .status()
        .await?;

    assert!(status.success(), "inner test failed");
    Ok(())
}

async fn inner_e2e_cli_run_resume_persistence() -> Result<(), Box<dyn std::error::Error>> {
    let project_dir = PathBuf::from(std::env::var("TEST_PROJECT_DIR")?);
    let _data_dir = PathBuf::from(std::env::var("TEST_DATA_DIR")?);

    std::env::set_current_dir(&project_dir)?;
    write_smoke_config(&project_dir).await?;

    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;

    // --- Step 1: Initial run ---
    let output = timeout(
        Duration::from_secs(120),
        Command::new(&rkat)
            .current_dir(&project_dir)
            .args([
                "run",
                "My name is RkatBot. Remember my name.",
                "--tools",
                "safe",
                "--output",
                "json",
            ])
            .output(),
    )
    .await??;

    if !output.status.success() {
        return Err(format!(
            "rkat run failed (exit {:?}): {}",
            output.status.code(),
            String::from_utf8_lossy(&output.stderr)
        )
        .into());
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    let parsed: serde_json::Value = serde_json::from_str(stdout.trim())
        .map_err(|e| format!("Failed to parse JSON output: {e}\nstdout: {stdout}"))?;

    let session_id = parsed["session_id"]
        .as_str()
        .ok_or("session_id missing in initial run response")?
        .to_string();

    assert!(!session_id.is_empty(), "session_id should be non-empty");

    // --- Step 2: Resume and ask about name ---
    let output = timeout(
        Duration::from_secs(120),
        Command::new(&rkat)
            .current_dir(&project_dir)
            .args([
                "run",
                "--resume",
                &session_id,
                "What is my name? Reply with just the name.",
            ])
            .output(),
    )
    .await??;

    if !output.status.success() {
        return Err(format!(
            "rkat run --resume failed (exit {:?}): {}",
            output.status.code(),
            String::from_utf8_lossy(&output.stderr)
        )
        .into());
    }

    // Resume outputs plain text (no --output json on resume command)
    let resume_stdout = String::from_utf8_lossy(&output.stdout);
    let resume_text = resume_stdout.trim().to_lowercase();

    assert!(
        resume_text.contains("rkatbot"),
        "Resume response should mention 'RkatBot', got: {resume_stdout}"
    );

    Ok(())
}

// ===========================================================================
// Scenario 27: CLI shell + structured output
// ===========================================================================

#[tokio::test]
#[ignore = "lane:e2e-smoke"]
async fn e2e_scenario_27_cli_shell_and_structured_output() -> Result<(), Box<dyn std::error::Error>>
{
    if skip_if_no_api_prereqs() {
        return Ok(());
    }

    // Outer test
    if std::env::var("RUN_TEST_E2E_SMOKE_12_INNER").is_ok() {
        return inner_e2e_cli_shell_tool().await;
    }

    let temp_dir = TempDir::new()?;
    let project_dir = temp_dir.path().join("project");
    tokio::fs::create_dir_all(project_dir.join(".rkat")).await?;

    let data_dir = temp_dir.path().join("data");
    tokio::fs::create_dir_all(&data_dir).await?;
    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;

    let status = Command::new(std::env::current_exe()?)
        .arg("e2e_scenario_27_cli_shell_and_structured_output")
        .arg("--ignored")
        .env("RUN_TEST_E2E_SMOKE_12_INNER", "1")
        .env("CARGO_BIN_EXE_rkat", &rkat)
        .env("HOME", temp_dir.path())
        .env("XDG_DATA_HOME", &data_dir)
        .env("TEST_PROJECT_DIR", &project_dir)
        .env("TEST_DATA_DIR", &data_dir)
        .status()
        .await?;

    assert!(status.success(), "inner test failed");
    Ok(())
}

async fn inner_e2e_cli_shell_tool() -> Result<(), Box<dyn std::error::Error>> {
    let project_dir = PathBuf::from(std::env::var("TEST_PROJECT_DIR")?);

    std::env::set_current_dir(&project_dir)?;
    write_smoke_config(&project_dir).await?;

    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;

    let output = timeout(
        Duration::from_secs(120),
        Command::new(&rkat)
            .current_dir(&project_dir)
            .args([
                "run",
                "Use the shell to run 'echo SMOKE_OK_42' and tell me the output",
                "--tools",
                "workspace",
                "--output",
                "json",
            ])
            .output(),
    )
    .await??;

    assert!(
        output.status.success(),
        "rkat run with shell failed (exit {:?}): {}",
        output.status.code(),
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    let parsed: serde_json::Value = serde_json::from_str(stdout.trim())
        .map_err(|e| format!("Failed to parse JSON output: {e}\nstdout: {stdout}"))?;

    let tool_calls = parsed["tool_calls"].as_u64().unwrap_or(0);
    assert!(
        tool_calls > 0,
        "Expected at least one tool call, got {tool_calls}"
    );

    let text = parsed["text"].as_str().unwrap_or("");
    assert!(
        text.contains("SMOKE_OK_42"),
        "Response text should contain 'SMOKE_OK_42', got: {text}"
    );

    let schema =
        r#"{"type":"object","properties":{"answer":{"type":"integer"}},"required":["answer"]}"#;
    let structured_output = timeout(
        Duration::from_secs(120),
        Command::new(&rkat)
            .current_dir(&project_dir)
            .args([
                "run",
                "What is 6 times 7? Give just the number.",
                "--schema",
                schema,
                "--output",
                "json",
            ])
            .output(),
    )
    .await??;

    assert!(
        structured_output.status.success(),
        "rkat structured output run failed (exit {:?}): {}",
        structured_output.status.code(),
        String::from_utf8_lossy(&structured_output.stderr)
    );

    let structured_stdout = String::from_utf8_lossy(&structured_output.stdout);
    let structured: serde_json::Value =
        serde_json::from_str(structured_stdout.trim()).map_err(|e| {
            format!("Failed to parse structured output JSON: {e}\nstdout: {structured_stdout}")
        })?;
    assert!(
        structured["structured_output"]["answer"].is_number(),
        "structured output should contain a numeric answer, got: {structured}"
    );

    Ok(())
}

// ===========================================================================
// Scenario 73: CLI generate_image blob save smoke
// ===========================================================================

#[tokio::test]
#[ignore = "lane:e2e-smoke"]
async fn e2e_scenario_73_cli_generate_image_openai_default()
-> Result<(), Box<dyn std::error::Error>> {
    if skip_if_no_openai_api_prereqs() {
        return Ok(());
    }

    if std::env::var("RUN_TEST_E2E_SMOKE_73_INNER").is_ok() {
        return inner_e2e_cli_generate_image_openai_default().await;
    }

    let temp_dir = TempDir::new()?;
    let project_dir = temp_dir.path().join("project");
    tokio::fs::create_dir_all(project_dir.join(".rkat")).await?;

    let data_dir = temp_dir.path().join("data");
    tokio::fs::create_dir_all(&data_dir).await?;
    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;

    let status = Command::new(std::env::current_exe()?)
        .arg("e2e_scenario_73_cli_generate_image_openai_default")
        .arg("--ignored")
        .env("RUN_TEST_E2E_SMOKE_73_INNER", "1")
        .env("CARGO_BIN_EXE_rkat", &rkat)
        .env("HOME", temp_dir.path())
        .env("XDG_DATA_HOME", &data_dir)
        .env("TEST_PROJECT_DIR", &project_dir)
        .env("TEST_DATA_DIR", &data_dir)
        .status()
        .await?;

    assert!(status.success(), "inner test failed");
    Ok(())
}

async fn inner_e2e_cli_generate_image_openai_default() -> Result<(), Box<dyn std::error::Error>> {
    let project_dir = PathBuf::from(std::env::var("TEST_PROJECT_DIR")?);

    std::env::set_current_dir(&project_dir)?;
    // Image generation needs more output tokens than the shared smoke config's
    // 256 — the model must emit web search calls, a generate_image tool call
    // with a detailed prompt, and a blob_save_file call.
    let rkat_dir = project_dir.join(".rkat");
    tokio::fs::create_dir_all(&rkat_dir).await?;
    let mut config = Config::default();
    config.agent.max_tokens_per_turn = 4096;
    config.agent.model = smoke_model();
    let config_toml = toml::to_string_pretty(&config)?;
    tokio::fs::write(rkat_dir.join("config.toml"), config_toml).await?;

    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;
    let output = timeout(
        Duration::from_secs(600),
        Command::new(&rkat)
            .current_dir(&project_dir)
            .args([
                "run",
                "Check today's news and generate an infographic image with the top news. Save it to top-news-infographic.png",
                "--yolo",
                "-m",
                "gpt-5.5",
            ])
            .output(),
    )
    .await??;

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    let combined = format!("{stdout}\n{stderr}");
    assert!(
        output.status.success(),
        "rkat generate_image run failed (exit {:?}): {combined}",
        output.status.code()
    );
    for failure in [
        "unsupported_target",
        "guard rejected",
        "invalid_arguments",
        "execution_failed",
        "internal image operation state error",
        "currently unavailable",
        "don't currently have access",
        "isn't currently available",
        "couldn't create",
        "failed in this session",
        "\"terminal\":\"denied\"",
        "\"terminal\": \"denied\"",
    ] {
        assert!(
            !combined.to_ascii_lowercase().contains(failure),
            "CLI generate_image smoke hit failure marker {failure:?}: {combined}"
        );
    }
    let saved_path = project_dir.join("top-news-infographic.png");
    let saved = tokio::fs::read(&saved_path)
        .await
        .map_err(|err| format!("expected saved PNG at '{}': {err}", saved_path.display()))?;
    assert!(
        !saved.is_empty(),
        "saved infographic should be nonempty at '{}'",
        saved_path.display()
    );
    assert!(
        saved.len() >= 8,
        "saved infographic should be long enough to include a PNG signature"
    );
    assert_eq!(
        &saved[..8],
        &[0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A],
        "saved infographic should be a PNG file; output was: {combined}"
    );
    let temp_root = project_dir
        .parent()
        .ok_or("project dir should have a parent temp root")?;
    let events = read_jsonl_files_under(temp_root).await?;
    assert!(
        events.contains(r#""name":"generate_image""#),
        "smoke should exercise generate_image, not just create a PNG by other means; output was: {combined}"
    );
    assert!(
        events.contains(r#""name":"blob_save_file""#),
        "smoke should exercise blob_save_file to materialize the generated blob; output was: {combined}"
    );

    Ok(())
}

// ===========================================================================
// Scenario 28: CLI capabilities + config
// ===========================================================================

#[tokio::test]
#[ignore = "lane:e2e-system"]
async fn e2e_scenario_28_cli_capabilities_and_config() -> Result<(), Box<dyn std::error::Error>> {
    if skip_if_no_prereqs() {
        return Ok(());
    }

    // Outer test
    if std::env::var("RUN_TEST_E2E_SMOKE_13_INNER").is_ok() {
        return inner_e2e_cli_capabilities_and_config().await;
    }

    let temp_dir = TempDir::new()?;
    let project_dir = temp_dir.path().join("project");
    tokio::fs::create_dir_all(project_dir.join(".rkat")).await?;

    let data_dir = temp_dir.path().join("data");
    tokio::fs::create_dir_all(&data_dir).await?;
    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;

    let status = Command::new(std::env::current_exe()?)
        .arg("e2e_scenario_28_cli_capabilities_and_config")
        .arg("--ignored")
        .env("RUN_TEST_E2E_SMOKE_13_INNER", "1")
        .env("CARGO_BIN_EXE_rkat", &rkat)
        .env("HOME", temp_dir.path())
        .env("XDG_DATA_HOME", &data_dir)
        .env("TEST_PROJECT_DIR", &project_dir)
        .env("TEST_DATA_DIR", &data_dir)
        .status()
        .await?;

    assert!(status.success(), "inner test failed");
    Ok(())
}

async fn inner_e2e_cli_capabilities_and_config() -> Result<(), Box<dyn std::error::Error>> {
    let project_dir = PathBuf::from(std::env::var("TEST_PROJECT_DIR")?);

    std::env::set_current_dir(&project_dir)?;
    write_smoke_config(&project_dir).await?;

    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;

    // --- Step 1: capabilities ---
    let output = timeout(
        Duration::from_secs(30),
        Command::new(&rkat)
            .current_dir(&project_dir)
            .args(["capabilities"])
            .output(),
    )
    .await??;

    assert!(
        output.status.success(),
        "rkat capabilities failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    let caps: serde_json::Value = serde_json::from_str(stdout.trim())
        .map_err(|e| format!("Failed to parse capabilities JSON: {e}\nstdout: {stdout}"))?;

    // Verify contract_version is present
    assert!(
        caps.get("contract_version").is_some(),
        "capabilities response should have contract_version, got: {caps}"
    );
    let contract_version = &caps["contract_version"];
    assert!(
        contract_version.get("major").is_some(),
        "contract_version should have major field"
    );

    // Verify "sessions" capability is present
    let capabilities = caps["capabilities"]
        .as_array()
        .ok_or("capabilities should be an array")?;
    let has_sessions = capabilities
        .iter()
        .any(|c| c["id"].as_str() == Some("sessions"));
    assert!(
        has_sessions,
        "capabilities should include 'sessions', got: {:?}",
        capabilities
            .iter()
            .filter_map(|c| c["id"].as_str())
            .collect::<Vec<_>>()
    );

    // --- Step 2: config get ---
    let output = timeout(
        Duration::from_secs(30),
        Command::new(&rkat)
            .current_dir(&project_dir)
            .args(["config", "get"])
            .output(),
    )
    .await??;

    assert!(
        output.status.success(),
        "rkat config get failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let config_stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        !config_stdout.trim().is_empty(),
        "config get output should be non-empty"
    );

    // Default format is TOML; verify it looks like TOML (has section headers or key=value)
    assert!(
        config_stdout.contains('[') || config_stdout.contains('='),
        "config get output should look like TOML, got: {config_stdout}"
    );

    // --- Step 3: config get --format json --with-generation ---
    let output = timeout(
        Duration::from_secs(30),
        Command::new(&rkat)
            .current_dir(&project_dir)
            .args(["config", "get", "--format", "json", "--with-generation"])
            .output(),
    )
    .await??;

    assert!(
        output.status.success(),
        "rkat config get --with-generation failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let envelope_stdout = String::from_utf8_lossy(&output.stdout);
    let envelope: serde_json::Value =
        serde_json::from_str(envelope_stdout.trim()).map_err(|e| {
            format!("Failed to parse config envelope JSON: {e}\nstdout: {envelope_stdout}")
        })?;
    let baseline_generation = envelope["generation"]
        .as_u64()
        .ok_or("generation missing in config envelope")?;
    assert!(
        envelope.get("config").is_some(),
        "config envelope should include config field"
    );

    // --- Step 4: config patch with matching expected_generation succeeds ---
    let output = timeout(
        Duration::from_secs(30),
        Command::new(&rkat)
            .current_dir(&project_dir)
            .args([
                "config",
                "patch",
                "--json",
                r#"{"agent":{"max_tokens_per_turn":333}}"#,
                "--expected-generation",
                &baseline_generation.to_string(),
            ])
            .output(),
    )
    .await??;

    assert!(
        output.status.success(),
        "rkat config patch with expected-generation failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let patch_stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        patch_stdout.contains("generation="),
        "config patch output should contain generation marker, got: {patch_stdout}"
    );

    // --- Step 5: stale expected_generation fails deterministically ---
    let output = timeout(
        Duration::from_secs(30),
        Command::new(&rkat)
            .current_dir(&project_dir)
            .args([
                "config",
                "set",
                "--toml",
                "[agent]\nmodel = \"claude-opus-4-6\"\nmax_tokens_per_turn = 256\nbudget_warning_threshold = 0.8\n",
                "--expected-generation",
                &baseline_generation.to_string(),
            ])
            .output(),
    )
    .await??;

    assert!(
        !output.status.success(),
        "stale expected-generation write should fail"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.to_lowercase().contains("generation") || stderr.to_lowercase().contains("expected"),
        "stale generation failure should mention generation mismatch, got: {stderr}"
    );

    Ok(())
}

// ===========================================================================
// Supplemental: CLI structured output only
// ===========================================================================

#[tokio::test]
#[ignore = "lane:e2e-live"]
async fn e2e_cli_structured_output() -> Result<(), Box<dyn std::error::Error>> {
    if skip_if_no_api_prereqs() {
        return Ok(());
    }

    // Outer test
    if std::env::var("RUN_TEST_E2E_SMOKE_14_INNER").is_ok() {
        return inner_e2e_cli_structured_output().await;
    }

    let temp_dir = TempDir::new()?;
    let project_dir = temp_dir.path().join("project");
    tokio::fs::create_dir_all(project_dir.join(".rkat")).await?;

    let data_dir = temp_dir.path().join("data");
    tokio::fs::create_dir_all(&data_dir).await?;
    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;

    let status = Command::new(std::env::current_exe()?)
        .arg("e2e_cli_structured_output")
        .arg("--ignored")
        .env("RUN_TEST_E2E_SMOKE_14_INNER", "1")
        .env("CARGO_BIN_EXE_rkat", &rkat)
        .env("HOME", temp_dir.path())
        .env("XDG_DATA_HOME", &data_dir)
        .env("TEST_PROJECT_DIR", &project_dir)
        .env("TEST_DATA_DIR", &data_dir)
        .status()
        .await?;

    assert!(status.success(), "inner test failed");
    Ok(())
}

async fn inner_e2e_cli_structured_output() -> Result<(), Box<dyn std::error::Error>> {
    let project_dir = PathBuf::from(std::env::var("TEST_PROJECT_DIR")?);

    std::env::set_current_dir(&project_dir)?;
    write_smoke_config(&project_dir).await?;

    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;

    let schema =
        r#"{"type":"object","properties":{"answer":{"type":"integer"}},"required":["answer"]}"#;

    let output = timeout(
        Duration::from_secs(120),
        Command::new(&rkat)
            .current_dir(&project_dir)
            .args([
                "run",
                "What is 6 times 7? Give just the number.",
                "--schema",
                schema,
                "--output",
                "json",
            ])
            .output(),
    )
    .await??;

    assert!(
        output.status.success(),
        "rkat run with structured output failed (exit {:?}): {}",
        output.status.code(),
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    let parsed: serde_json::Value = serde_json::from_str(stdout.trim())
        .map_err(|e| format!("Failed to parse JSON output: {e}\nstdout: {stdout}"))?;

    // Verify structured_output is present and has the answer field
    let structured_output = &parsed["structured_output"];
    assert!(
        !structured_output.is_null(),
        "structured_output should be present in response, got: {parsed}"
    );

    let answer = structured_output.get("answer");
    assert!(
        answer.is_some(),
        "structured_output should have 'answer' field, got: {structured_output}"
    );

    // The answer should be a number (the LLM should return 42)
    let answer_val = answer.unwrap();
    assert!(
        answer_val.is_number(),
        "answer should be a number, got: {answer_val}"
    );

    Ok(())
}

// ===========================================================================
// E2E-001: Background job active-turn completion surfacing
// ===========================================================================

#[tokio::test]
#[ignore = "lane:e2e-smoke"]
async fn e2e_001_background_job_active_turn_completion() -> Result<(), Box<dyn std::error::Error>> {
    if skip_if_no_api_prereqs() {
        return Ok(());
    }

    if std::env::var("RUN_TEST_E2E_BG_001_INNER").is_ok() {
        return inner_e2e_001_bg_active_turn().await;
    }

    let temp_dir = TempDir::new()?;
    let project_dir = temp_dir.path().join("project");
    tokio::fs::create_dir_all(project_dir.join(".rkat")).await?;

    let data_dir = temp_dir.path().join("data");
    tokio::fs::create_dir_all(&data_dir).await?;
    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;

    let status = Command::new(std::env::current_exe()?)
        .arg("e2e_001_background_job_active_turn_completion")
        .arg("--ignored")
        .env("RUN_TEST_E2E_BG_001_INNER", "1")
        .env("CARGO_BIN_EXE_rkat", &rkat)
        .env("HOME", temp_dir.path())
        .env("XDG_DATA_HOME", &data_dir)
        .env("TEST_PROJECT_DIR", &project_dir)
        .env("TEST_DATA_DIR", &data_dir)
        .status()
        .await?;

    assert!(status.success(), "inner test failed");
    Ok(())
}

async fn inner_e2e_001_bg_active_turn() -> Result<(), Box<dyn std::error::Error>> {
    let project_dir = PathBuf::from(std::env::var("TEST_PROJECT_DIR")?);

    std::env::set_current_dir(&project_dir)?;
    write_smoke_config(&project_dir).await?;

    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;

    // Ask the agent to run a fast background job and wait for it to complete.
    // The agent should see the completion via [SYSTEM NOTICE][BG_JOB].
    let output = timeout(
        Duration::from_secs(120),
        Command::new(&rkat)
            .current_dir(&project_dir)
            .args([
                "run",
                "Run `echo bg_done` in the background using the shell tool with background=true. \
                 Then call shell_job_status to check on it. \
                 Report the final status including the job_id.",
                "--tools",
                "full",
                "--output",
                "json",
                "--stream",
            ])
            .output(),
    )
    .await??;

    assert!(
        output.status.success(),
        "rkat run failed (exit {:?}): {}",
        output.status.code(),
        String::from_utf8_lossy(&output.stderr),
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    let combined = format!("{stdout}\n{stderr}");

    // The agent should surface a background job completion notice or mention
    // the job result. Look for evidence of the notification path.
    let has_bg_notice =
        combined.contains("[BG_JOB]") || combined.contains("background_job_completed");
    let has_job_id = combined.contains("job_id") || combined.contains("j_");
    let has_bg_done = combined.contains("bg_done");

    assert!(
        has_bg_notice || has_bg_done,
        "E2E-001: expected background job completion notice or output in transcript.\n\
         stdout: {stdout}\nstderr: {stderr}"
    );

    // CONTRACT-002: surfaced handle is job_id, not operation_id
    if has_bg_notice {
        assert!(
            has_job_id,
            "E2E-001: background notice should reference job_id (CONTRACT-002)"
        );
    }

    Ok(())
}

// ===========================================================================
// E2E-002: Background job idle keep-alive completion surfacing
// ===========================================================================

#[tokio::test]
#[ignore = "lane:e2e-smoke"]
async fn e2e_002_background_job_idle_keepalive_completion() -> Result<(), Box<dyn std::error::Error>>
{
    if skip_if_no_api_prereqs() {
        return Ok(());
    }

    if std::env::var("RUN_TEST_E2E_BG_002_INNER").is_ok() {
        return inner_e2e_002_bg_keepalive().await;
    }

    let temp_dir = TempDir::new()?;
    let project_dir = temp_dir.path().join("project");
    tokio::fs::create_dir_all(project_dir.join(".rkat")).await?;

    let data_dir = temp_dir.path().join("data");
    tokio::fs::create_dir_all(&data_dir).await?;
    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;

    let status = Command::new(std::env::current_exe()?)
        .arg("e2e_002_background_job_idle_keepalive_completion")
        .arg("--ignored")
        .env("RUN_TEST_E2E_BG_002_INNER", "1")
        .env("CARGO_BIN_EXE_rkat", &rkat)
        .env("HOME", temp_dir.path())
        .env("XDG_DATA_HOME", &data_dir)
        .env("TEST_PROJECT_DIR", &project_dir)
        .env("TEST_DATA_DIR", &data_dir)
        .status()
        .await?;

    assert!(status.success(), "inner test failed");
    Ok(())
}

async fn inner_e2e_002_bg_keepalive() -> Result<(), Box<dyn std::error::Error>> {
    let project_dir = PathBuf::from(std::env::var("TEST_PROJECT_DIR")?);

    std::env::set_current_dir(&project_dir)?;
    write_smoke_config(&project_dir).await?;

    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;

    // Start a keep-alive session via --keep-alive. The agent stays alive after
    // its initial turn. We launch a background job, let the agent finish its
    // turn, then wait for the background job to complete while idle. The runtime
    // should wake and surface the completion without user input.
    //
    // We redirect output to a temp file and read it after killing the process,
    // because SIGKILL does not flush piped stdout/stderr.
    let stdout_file = project_dir.join("e2e_002_stdout.txt");
    let stderr_file = project_dir.join("e2e_002_stderr.txt");
    let stdout_f = std::fs::File::create(&stdout_file)?;
    let stderr_f = std::fs::File::create(&stderr_file)?;

    let mut child = Command::new(&rkat)
        .current_dir(&project_dir)
        .args([
            "run",
            "Run `sleep 2 && echo keepalive_bg_done` in the background using shell with background=true. \
             Then say 'Waiting for background job.' and stop.",
            "--tools",
            "full",
            "--keep-alive",
            "--stream",
        ])
        .stdin(std::process::Stdio::null())
        .stdout(std::process::Stdio::from(stdout_f))
        .stderr(std::process::Stdio::from(stderr_f))
        .spawn()?;

    // Wait for the background job to complete (2s) plus margin for the
    // agent to process the initial turn + wake cycle.
    tokio::time::sleep(Duration::from_secs(20)).await;

    // Kill the process and read captured output from files
    child.kill().await.ok();
    let _ = child.wait().await;

    let stdout = tokio::fs::read_to_string(&stdout_file)
        .await
        .unwrap_or_default();
    let stderr = tokio::fs::read_to_string(&stderr_file)
        .await
        .unwrap_or_default();
    let combined = format!("{stdout}\n{stderr}");

    // The session should have woken up and surfaced the completion.
    let has_bg_notice =
        combined.contains("[BG_JOB]") || combined.contains("background_job_completed");
    let has_bg_done = combined.contains("keepalive_bg_done");

    assert!(
        has_bg_notice || has_bg_done,
        "E2E-002: expected background job completion surfaced during idle keep-alive.\n\
         The runtime should have woken the session without user input (REQ-002).\n\
         stdout: {stdout}\nstderr: {stderr}"
    );

    Ok(())
}