devsql 0.5.0

Code Mode across AI coding history, shell history, Git, source code, and worklogs
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
use assert_cmd::Command;
use data_encoding::BASE32_NOPAD;
use serde_json::{json, Value};
use std::path::{Path, PathBuf};
use tempfile::TempDir;

const ACCOUNT: &str = "github%7C4741454";
const BOT_A: &str = "69884c90-a572-4638-9b6d-5753ea878c88";
const BOT_B: &str = "a63c857d-3252-47f5-8087-f75ea908a6d6";

struct GrokFixtures {
    root: TempDir,
    grok_dir: PathBuf,
}

impl GrokFixtures {
    fn new() -> Self {
        let root = TempDir::new().expect("temp");
        let grok_dir = root.path().join("Grok Bot");
        std::fs::create_dir_all(grok_dir.join("sand-client-persistence")).expect("persistence dir");
        Self { root, grok_dir }
    }

    fn persistence(&self) -> PathBuf {
        self.grok_dir.join("sand-client-persistence")
    }

    /// Encode a logical key exactly as the desktop app names its blob files.
    fn blob_path(&self, logical_key: &str) -> PathBuf {
        let encoded = BASE32_NOPAD.encode(logical_key.as_bytes()).to_lowercase();
        self.persistence().join(format!("{encoded}.blob"))
    }

    fn write_blob(&self, logical_key: &str, schema_version: u64, value: Value) {
        let body = json!({"schemaVersion": schema_version, "value": value});
        std::fs::write(
            self.blob_path(logical_key),
            serde_json::to_string(&body).expect("blob json"),
        )
        .expect("write blob");
    }

    fn write_roster(&self, rows: Value) {
        self.write_blob(
            &format!("sand.client.slice.account.{ACCOUNT}.roster.last-roster"),
            4,
            json!({"rows": rows}),
        );
    }

    fn write_transcript(&self, bot_id: &str, entries: Value, persisted_at: i64) {
        self.write_blob(
            &format!("sand.client.slice.account.{ACCOUNT}.transcript.replicas.{bot_id}"),
            1,
            json!({
                "entries": entries,
                "epochHint": Value::Null,
                "acceptedSequenceHint": Value::Null,
                "persistedAt": persisted_at,
            }),
        );
    }

    fn command(&self) -> Command {
        let mut command = Command::new(env!("CARGO_BIN_EXE_devsql"));
        command
            .env("DEVSQL_GROK_DIR", &self.grok_dir)
            // Keep the real caches out of the test entirely.
            .env("CODEX_HOME", self.root.path().join("codex"))
            .env("XDG_CACHE_HOME", self.root.path().join("cache"))
            .env("HOME", self.root.path());
        command
    }

    fn query(&self, sql: &str) -> Value {
        let output = self
            .command()
            .args([sql, "--format", "json"])
            .assert()
            .success()
            .get_output()
            .stdout
            .clone();
        serde_json::from_slice(&output).expect("json output")
    }

    fn status(&self) -> Value {
        let output = self
            .command()
            .args(["grok", "status", "--format", "json"])
            .assert()
            .success()
            .get_output()
            .stdout
            .clone();
        serde_json::from_slice(&output).expect("status json")
    }

    fn search(&self, args: &[&str]) -> Value {
        let mut command = self.command();
        command.args(["grok", "search"]);
        command.args(args);
        let output = command
            .args(["--format", "json"])
            .assert()
            .success()
            .get_output()
            .stdout
            .clone();
        serde_json::from_slice(&output).expect("search json")
    }

    fn scalar(&self, sql: &str) -> String {
        let value = self.query(sql);
        let rows = value.as_array().cloned().unwrap_or_default();
        let first = rows.first().cloned().unwrap_or(Value::Null);
        let obj = first.as_object().cloned().unwrap_or_default();
        obj.values()
            .next()
            .map(render_scalar)
            .unwrap_or_else(|| "".to_string())
    }
}

fn render_scalar(value: &Value) -> String {
    match value {
        Value::String(text) => text.clone(),
        other => other.to_string(),
    }
}

fn user_entry(turn: u32, text: &str, ts: i64) -> Value {
    json!({
        "kind": "message",
        "id": format!("t{turn}u"),
        "role": "user",
        "content": text,
        "isStreaming": false,
        "timestampMs": ts,
    })
}

fn send_entry(turn: u32, index: u32, text: &str, ts: i64) -> Value {
    json!({
        "kind": "send-message",
        "id": format!("t{turn}s{index}"),
        "message": {"type": "text", "content": text},
        "timestampMs": ts,
    })
}

fn roster_row(bot_id: &str, name: &str, last_activity: i64) -> Value {
    json!({
        "id": bot_id,
        "name": name,
        "title": name,
        "description": format!("{name} bot"),
        "isGroup": false,
        "origin": "user",
        "path": format!("/home/box/sand-data/agents/{bot_id}/store.db"),
        "createdAt": 1787347952382i64,
        "updatedAt": last_activity,
        "lastActivityAt": last_activity,
        "newestEntryId": "t1u",
    })
}

fn seeded() -> GrokFixtures {
    let fx = GrokFixtures::new();
    fx.write_roster(json!([
        roster_row(BOT_A, "Grokctl", 1_788_704_108_440i64),
        roster_row(BOT_B, "Terri", 1_788_786_153_841i64),
    ]));
    fx.write_transcript(
        BOT_A,
        json!([
            user_entry(0, "can you use grokctl?", 1_787_875_800_675i64),
            send_entry(
                0,
                0,
                "Yes. Here is the command graph.",
                1_787_875_801_000i64
            ),
        ]),
        1_788_704_108_440i64,
    );
    fx.write_transcript(
        BOT_B,
        json!([user_entry(1, "standup card please", 1_788_786_153_837i64)]),
        1_788_786_153_841i64,
    );
    fx
}

#[test]
fn ingests_bots_and_entries_from_local_replicas() {
    let fx = seeded();

    assert_eq!(fx.scalar("SELECT COUNT(*) FROM grok_bots"), "2");
    assert_eq!(fx.scalar("SELECT COUNT(*) FROM grok_entries"), "3");
    assert_eq!(
        fx.scalar(
            "SELECT name FROM grok_bots WHERE bot_id = '69884c90-a572-4638-9b6d-5753ea878c88'"
        ),
        "Grokctl"
    );
    assert_eq!(
        fx.scalar(
            "SELECT remote_store_path FROM grok_bots WHERE bot_id = 'a63c857d-3252-47f5-8087-f75ea908a6d6'"
        ),
        format!("/home/box/sand-data/agents/{BOT_B}/store.db")
    );

    // Epoch millis are projected to RFC3339 so substr(x,1,10) works like the
    // codex_* tables.
    assert_eq!(
        fx.scalar("SELECT substr(timestamp,1,10) FROM grok_entries WHERE entry_id = 't0u'"),
        "2026-08-28"
    );
    assert_eq!(
        fx.scalar("SELECT direction FROM grok_entries WHERE entry_id = 't0s0'"),
        "outbound"
    );
    assert_eq!(
        fx.scalar("SELECT provenance FROM grok_entries WHERE entry_id = 't0u'"),
        "local_replica"
    );
}

#[test]
fn grok_messages_view_exposes_only_text_entries() {
    let fx = GrokFixtures::new();
    fx.write_roster(json!([roster_row(BOT_A, "Grokctl", 1i64)]));
    fx.write_transcript(
        BOT_A,
        json!([
            user_entry(0, "hello", 10i64),
            json!({
                "kind": "send-message",
                "id": "t0s0",
                "message": {"type": "widget", "content": {"nested": true}},
                "timestampMs": 11i64,
            }),
            json!({
                "kind": "event",
                "id": "event-15e92eb2-0000-0000-0000-000000000000",
                "event": {"type": "automation-changed"},
                "timestampMs": 12i64,
            }),
        ]),
        100i64,
    );

    assert_eq!(fx.scalar("SELECT COUNT(*) FROM grok_entries"), "3");
    // A widget must never be JSON-stringified into `text`.
    assert_eq!(fx.scalar("SELECT COUNT(*) FROM grok_messages"), "1");
    assert_eq!(fx.scalar("SELECT text FROM grok_messages"), "hello");
    assert_eq!(fx.scalar("SELECT bot_name FROM grok_messages"), "Grokctl");
    assert_eq!(
        fx.scalar("SELECT event_type FROM grok_entries WHERE kind = 'event'"),
        "automation-changed"
    );
}

#[test]
fn missing_grok_directory_yields_an_empty_table() {
    let root = TempDir::new().expect("temp");
    let mut command = Command::new(env!("CARGO_BIN_EXE_devsql"));
    command
        .env("DEVSQL_GROK_DIR", root.path().join("nonexistent"))
        .env("CODEX_HOME", root.path().join("codex"))
        .env("XDG_CACHE_HOME", root.path().join("cache"))
        .env("HOME", root.path());
    let output = command
        .args(["SELECT COUNT(*) AS n FROM grok_bots", "--format", "json"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let value: Value = serde_json::from_slice(&output).expect("json");
    let rows = value.as_array().expect("rows");
    assert_eq!(rows[0]["n"], json!(0));
}

#[test]
fn malformed_and_undecodable_blobs_do_not_block_healthy_ones() {
    let fx = seeded();

    // Not base32 at all: skipped silently, it is not one of ours.
    std::fs::write(fx.persistence().join("not-base32!!.blob"), "{}").expect("junk blob");
    // Decodable name, invalid JSON body: recorded, other blobs still ingest.
    let broken_key =
        format!("sand.client.slice.account.{ACCOUNT}.transcript.replicas.11111111-1111-1111-1111-111111111111");
    std::fs::write(fx.blob_path(&broken_key), "{not json").expect("broken blob");

    assert_eq!(fx.scalar("SELECT COUNT(*) FROM grok_bots"), "2");
    assert_eq!(fx.scalar("SELECT COUNT(*) FROM grok_entries"), "3");
    // Errors are keyed on (source, error_kind), so a blob that stays broken
    // across runs stays one row.
    assert_eq!(
        fx.scalar("SELECT COUNT(*) FROM grok_ingest_errors WHERE error_kind = 'blob_parse'"),
        "1"
    );
}

#[test]
fn unknown_entry_kinds_are_stored_rather_than_dropped() {
    let fx = GrokFixtures::new();
    fx.write_roster(json!([roster_row(BOT_A, "Grokctl", 1i64)]));
    fx.write_transcript(
        BOT_A,
        json!([json!({
            "kind": "brand-new-kind",
            "id": "t9z1",
            "somethingNovel": {"a": 1},
            "timestampMs": 42i64,
        })]),
        100i64,
    );

    assert_eq!(fx.scalar("SELECT COUNT(*) FROM grok_entries"), "1");
    assert_eq!(
        fx.scalar("SELECT kind FROM grok_entries WHERE entry_id = 't9z1'"),
        "brand-new-kind"
    );
    assert_eq!(fx.scalar("SELECT COUNT(*) FROM grok_messages"), "0");
    // raw_json is what makes an unknown kind recoverable later.
    let raw = fx.scalar("SELECT raw_json FROM grok_entries WHERE entry_id = 't9z1'");
    assert!(raw.contains("somethingNovel"), "raw_json preserved: {raw}");
}

/// The headline invariant: local replicas are truncated client-side windows, so
/// entries the desktop app evicts must survive in devsql.
#[test]
fn entries_survive_local_replica_truncation() {
    let fx = GrokFixtures::new();
    fx.write_roster(json!([roster_row(BOT_A, "Grokctl", 1i64)]));

    let full: Vec<Value> = (1..=10)
        .map(|turn| user_entry(turn, &format!("message {turn}"), 1_000 + i64::from(turn)))
        .collect();
    fx.write_transcript(BOT_A, json!(full), 1_000i64);
    assert_eq!(fx.scalar("SELECT COUNT(*) FROM grok_entries"), "10");

    // The app evicts the first five and rewrites the blob with a newer stamp.
    let truncated: Vec<Value> = (6..=10)
        .map(|turn| user_entry(turn, &format!("message {turn}"), 1_000 + i64::from(turn)))
        .collect();
    fx.write_transcript(BOT_A, json!(truncated), 2_000i64);

    assert_eq!(
        fx.scalar("SELECT COUNT(*) FROM grok_entries"),
        "10",
        "evicted entries must not disappear from the index"
    );
    assert_eq!(
        fx.scalar("SELECT text FROM grok_entries WHERE entry_id = 't1u'"),
        "message 1"
    );
    assert_eq!(fx.scalar("SELECT entry_count FROM grok_bots"), "10");
}

#[test]
fn a_bot_removed_from_the_roster_keeps_its_entries() {
    let fx = seeded();
    assert_eq!(fx.scalar("SELECT COUNT(*) FROM grok_bots"), "2");

    // Bot B is deleted in the Grok UI: it leaves the roster, but its replica
    // and history stay.
    fx.write_roster(json!([roster_row(BOT_A, "Grokctl", 2i64)]));

    assert_eq!(fx.scalar("SELECT COUNT(*) FROM grok_bots"), "2");
    assert_eq!(
        fx.scalar(&format!(
            "SELECT roster_present FROM grok_bots WHERE bot_id = '{BOT_B}'"
        )),
        "0"
    );
    assert_eq!(
        fx.scalar(&format!(
            "SELECT COUNT(*) FROM grok_entries WHERE bot_id = '{BOT_B}'"
        )),
        "1"
    );
}

#[test]
fn a_regressing_persisted_at_is_skipped_and_recorded() {
    let fx = GrokFixtures::new();
    fx.write_roster(json!([roster_row(BOT_A, "Grokctl", 1i64)]));
    fx.write_transcript(BOT_A, json!([user_entry(1, "first", 10i64)]), 5_000i64);
    assert_eq!(fx.scalar("SELECT COUNT(*) FROM grok_entries"), "1");

    // Same bot, older stamp: a stale rewrite, not new truth.
    fx.write_transcript(
        BOT_A,
        json!([user_entry(2, "stale rewrite", 20i64)]),
        1_000i64,
    );

    assert_eq!(fx.scalar("SELECT COUNT(*) FROM grok_entries"), "1");
    assert_eq!(
        fx.scalar(
            "SELECT COUNT(*) FROM grok_ingest_errors WHERE error_kind = 'persisted_at_regression'"
        ),
        "1"
    );
}

#[test]
fn unchanged_blobs_are_not_reparsed_on_a_second_run() {
    let fx = seeded();

    let first = fx.status();
    assert_eq!(
        first["parsed_blobs"],
        json!(3),
        "roster plus two transcripts"
    );
    assert_eq!(first["unchanged_blobs"], json!(0));

    // Nothing on disk moved, so the size/mtime watermark must short-circuit.
    let second = fx.status();
    assert_eq!(
        second["parsed_blobs"],
        json!(0),
        "no blob should be reparsed"
    );
    assert_eq!(second["unchanged_blobs"], json!(3));
    assert_eq!(second["entries"], first["entries"]);
}

#[test]
fn status_reports_coverage_and_roster_state() {
    let fx = seeded();
    let status = fx.status();

    assert_eq!(status["bots"], json!(2));
    assert_eq!(status["bots_in_roster"], json!(2));
    assert_eq!(status["entries"], json!(3));
    assert_eq!(status["messages"], json!(3));
    assert_eq!(status["persistence_dir_present"], json!(true));
    assert_eq!(status["ingest_errors"].as_array().map(Vec::len), Some(0));

    let names: Vec<&str> = status["bot_rows"]
        .as_array()
        .expect("bot rows")
        .iter()
        .filter_map(|row| row["name"].as_str())
        .collect();
    assert!(names.contains(&"Terri"), "bot rows: {names:?}");
}

#[test]
fn search_is_global_and_can_filter_by_bot() {
    let fx = seeded();

    let all = fx.search(&["grokctl"]);
    assert_eq!(all["scope"], json!("global"));
    assert_eq!(all["total"], json!(1));
    assert_eq!(all["matches"][0]["bot_name"], json!("Grokctl"));

    let scoped = fx.search(&["standup", "--bot", "Terri"]);
    assert_eq!(scoped["total"], json!(1));
    assert_eq!(scoped["matches"][0]["bot_name"], json!("Terri"));

    let empty = fx.search(&["nothing-matches-this"]);
    assert_eq!(empty["total"], json!(0));
}

#[test]
fn search_rejects_an_unknown_bot() {
    let fx = seeded();
    fx.command()
        .args(["grok", "search", "anything", "--bot", "NoSuchBot"])
        .assert()
        .failure();
}

#[test]
fn entry_id_shapes_are_parsed_or_left_null() {
    let fx = GrokFixtures::new();
    fx.write_roster(json!([roster_row(BOT_A, "Grokctl", 1i64)]));
    fx.write_transcript(
        BOT_A,
        json!([
            send_entry(121, 3, "ordinal", 10i64),
            json!({
                "kind": "send-message",
                "id": "tbs0",
                "message": {"type": "text", "content": "non-numeric turn"},
                "timestampMs": 11i64,
            }),
            json!({
                "kind": "event",
                "id": "event-15e92eb2-0000-0000-0000-000000000000",
                "event": {"type": "automation-changed"},
                "timestampMs": 12i64,
            }),
        ]),
        100i64,
    );

    assert_eq!(
        fx.scalar("SELECT turn_ordinal FROM grok_entries WHERE entry_id = 't121s3'"),
        "121"
    );
    assert_eq!(
        fx.scalar("SELECT entry_suffix FROM grok_entries WHERE entry_id = 't121s3'"),
        "s"
    );
    assert_eq!(
        fx.scalar("SELECT suffix_index FROM grok_entries WHERE entry_id = 't121s3'"),
        "3"
    );

    // A non-numeric turn token must not fabricate an ordinal.
    assert_eq!(
        fx.scalar(
            "SELECT COUNT(*) FROM grok_entries WHERE entry_id = 'tbs0' AND turn_ordinal IS NULL"
        ),
        "1"
    );
    assert_eq!(
        fx.scalar("SELECT turn_token FROM grok_entries WHERE entry_id = 'tbs0'"),
        "b"
    );

    // UUID-shaped ids parse to nothing; ordering falls back to source_order.
    assert_eq!(
        fx.scalar(
            "SELECT COUNT(*) FROM grok_entries
              WHERE entry_id LIKE 'event-%' AND turn_ordinal IS NULL AND turn_token IS NULL"
        ),
        "1"
    );
    assert_eq!(
        fx.scalar("SELECT entry_id FROM grok_entries ORDER BY source_order DESC LIMIT 1"),
        "event-15e92eb2-0000-0000-0000-000000000000"
    );
}

/// Grok is a chat corpus with no command, cwd, or exit code. It must stay out
/// of the cross-source command_events view, whose whole contract is those
/// columns.
#[test]
fn grok_rows_never_leak_into_command_events() {
    let fx = seeded();
    assert_eq!(
        fx.scalar("SELECT COUNT(*) FROM command_events WHERE source = 'grok'"),
        "0"
    );

    let columns = fx.query("SELECT * FROM command_events LIMIT 1");
    if let Some(first) = columns.as_array().and_then(|rows| rows.first()) {
        let obj = first.as_object().expect("row object");
        assert_eq!(
            obj.len(),
            20,
            "command_events must keep its 20-column contract"
        );
    }
}

/// Grok is an explicit search target, never part of default recall/gather
/// output.
#[test]
fn grok_stays_out_of_recall_and_gather_defaults() {
    let fx = seeded();

    let recall = fx
        .command()
        .args(["recall", "grokctl", "--format", "json"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let recall: Value = serde_json::from_slice(&recall).expect("recall json");
    let recall_obj = recall.as_object().expect("recall object");
    assert!(
        !recall_obj.contains_key("grok"),
        "recall must not gain a grok section by default"
    );

    let gather = fx
        .command()
        .args(["gather", "grokctl", "--format", "json"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let gather: Value = serde_json::from_slice(&gather).expect("gather json");
    let gather_obj = gather.as_object().expect("gather object");
    assert!(
        !gather_obj.contains_key("grok"),
        "gather must not gain a grok section: {:?}",
        gather_obj.keys().collect::<Vec<_>>()
    );

    // Check the rows themselves, not the serialized text: a substring search
    // would match any transcript that merely discusses grok.
    let kinds: Vec<&str> = gather_obj
        .get("prior_work")
        .and_then(Value::as_array)
        .map(|rows| {
            rows.iter()
                .filter_map(|row| row.get("kind").and_then(Value::as_str))
                .collect()
        })
        .unwrap_or_default();
    assert!(
        !kinds.iter().any(|kind| kind.contains("grok")),
        "gather prior_work must not contain grok rows: {kinds:?}"
    );
}

// ---------------------------------------------------------------------------
// Gateway backfill, driven by a fake grokctl. No real network.
// ---------------------------------------------------------------------------

impl GrokFixtures {
    /// Install a stub `grokctl` that replays canned responses.
    ///
    /// `script_body` is shell, receives the real argv, and must print the
    /// envelope devsql expects on stdout.
    fn fake_grokctl(&self, script_body: &str) -> PathBuf {
        let path = self.root.path().join("fake-grokctl");
        std::fs::write(&path, format!("#!/bin/sh\n{script_body}\n")).expect("write stub");
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755))
                .expect("chmod stub");
        }
        path
    }

    fn sync(&self, grokctl: &Path, extra: &[&str]) -> Value {
        let mut command = self.command();
        command.args(["grok", "sync", "--grokctl"]);
        command.arg(grokctl);
        command.args(extra);
        let output = command
            .args(["--format", "json"])
            .assert()
            .success()
            .get_output()
            .stdout
            .clone();
        serde_json::from_slice(&output).expect("sync json")
    }
}

/// Two pages, then a response without a cursor, which ends the walk.
const TWO_PAGE_STUB: &str = r#"
case "$*" in
  *--version*) echo "fake 0.0.0"; exit 0 ;;
esac
case "$*" in
  *beforeSeq*)
    cat <<'JSON'
{"result":{"entries":[
  {"kind":"message","id":"t5u","role":"user","content":"older question","timestampMs":500}
]}}
JSON
    ;;
  *)
    cat <<'JSON'
{"result":{"entries":[
  {"kind":"message","id":"t9u","role":"user","content":"newest question","timestampMs":900},
  {"kind":"send-message","id":"t9s0","message":{"type":"text","content":"newest answer"},"timestampMs":901}
],"nextBeforeSeq":40}}
JSON
    ;;
esac
"#;

#[test]
fn gateway_backfill_pages_until_the_cursor_runs_out() {
    let fx = seeded();
    let stub = fx.fake_grokctl(TWO_PAGE_STUB);

    let result = fx.sync(&stub, &["--bot", "Grokctl"]);
    assert_eq!(result["gateway"]["reachable"], json!(true));

    let bot = &result["bots"][0];
    assert_eq!(bot["name"], json!("Grokctl"));
    assert_eq!(bot["pages"], json!(2), "second page has no cursor");
    assert_eq!(bot["complete"], json!(true));

    // Two local entries plus three from the gateway.
    assert_eq!(
        fx.scalar(&format!(
            "SELECT COUNT(*) FROM grok_entries WHERE bot_id = '{BOT_A}'"
        )),
        "5"
    );
    assert_eq!(
        fx.scalar("SELECT text FROM grok_entries WHERE entry_id = 't5u'"),
        "older question"
    );
}

#[test]
fn gateway_entries_dedupe_against_local_rows_by_entry_id() {
    let fx = seeded();
    // The gateway returns the same entry_id the local replica already holds.
    let stub = fx.fake_grokctl(
        r#"
case "$*" in
  *--version*) echo "fake 0.0.0"; exit 0 ;;
esac
cat <<'JSON'
{"result":{"entries":[
  {"kind":"message","id":"t0u","role":"user","content":"can you use grokctl?","timestampMs":1787875800675}
]}}
JSON
"#,
    );

    let before = fx.scalar("SELECT COUNT(*) FROM grok_entries");
    fx.sync(&stub, &["--bot", "Grokctl"]);
    assert_eq!(
        fx.scalar("SELECT COUNT(*) FROM grok_entries"),
        before,
        "a duplicate entry_id must merge, not insert"
    );
    // Seen from both paths, which is how a lagging local cache is detected.
    assert_eq!(
        fx.scalar("SELECT provenance FROM grok_entries WHERE entry_id = 't0u'"),
        "both"
    );
}

#[test]
fn a_non_decreasing_cursor_terminates_the_walk() {
    let fx = seeded();
    // Always answers with the same cursor: a naive loop would never stop.
    let stub = fx.fake_grokctl(
        r#"
case "$*" in
  *--version*) echo "fake 0.0.0"; exit 0 ;;
esac
cat <<'JSON'
{"result":{"entries":[
  {"kind":"message","id":"t7u","role":"user","content":"stuck","timestampMs":700}
],"nextBeforeSeq":40}}
JSON
"#,
    );

    let result = fx.sync(
        &stub,
        &["--bot", "Grokctl", "--max-pages=99", "--full", "true"],
    );
    let pages = result["bots"][0]["pages"].as_u64().expect("pages");
    assert!(
        pages <= 2,
        "the guard must stop the walk quickly, got {pages}"
    );
}

#[test]
fn a_failing_grokctl_still_commits_local_ingest() {
    let fx = seeded();
    let stub = fx.fake_grokctl(
        r#"
case "$*" in
  *--version*) echo "fake 0.0.0"; exit 0 ;;
esac
echo "gateway unreachable" >&2
exit 1
"#,
    );

    // The command succeeds: local replicas are already committed.
    let result = fx.sync(&stub, &["--bot", "Grokctl"]);
    assert_eq!(result["local"]["entries"], json!(3));
    assert!(
        result["bots"][0]["note"].is_string(),
        "the failure must be reported as a note"
    );
    assert_eq!(fx.scalar("SELECT COUNT(*) FROM grok_entries"), "3");
    assert_eq!(
        fx.scalar("SELECT COUNT(*) FROM grok_ingest_errors WHERE error_kind = 'gateway_page'"),
        "1"
    );
}

#[test]
fn non_json_grokctl_output_is_reported_not_fatal() {
    let fx = seeded();
    let stub = fx.fake_grokctl(
        r#"
case "$*" in
  *--version*) echo "fake 0.0.0"; exit 0 ;;
esac
echo "this is not json"
"#,
    );

    let result = fx.sync(&stub, &["--bot", "Grokctl"]);
    assert_eq!(fx.scalar("SELECT COUNT(*) FROM grok_entries"), "3");
    assert!(result["bots"][0]["note"].is_string());
}

/// An empty transcript comes back with exit 0, so emptiness must never be read
/// as an error.
#[test]
fn an_empty_transcript_is_not_an_error() {
    let fx = seeded();
    let stub = fx.fake_grokctl(
        r#"
case "$*" in
  *--version*) echo "fake 0.0.0"; exit 0 ;;
esac
echo '{"result":{"entries":[]}}'
"#,
    );

    let result = fx.sync(&stub, &["--bot", "Grokctl"]);
    assert_eq!(result["gateway"]["reachable"], json!(true));
    assert_eq!(result["bots"][0]["entries_written"], json!(0));
    assert!(
        result["bots"][0]["note"].is_null(),
        "empty is not a failure"
    );
}

#[test]
fn a_missing_grokctl_binary_degrades_to_local_only() {
    let fx = seeded();
    let missing = fx.root.path().join("definitely-not-installed");

    let result = fx.sync(&missing, &[]);
    assert_eq!(result["gateway"]["reachable"], json!(false));
    assert!(result["gateway"]["reason"].is_string());
    assert_eq!(result["local"]["entries"], json!(3));
    assert_eq!(fx.scalar("SELECT COUNT(*) FROM grok_entries"), "3");
}

// ---------------------------------------------------------------------------
// In-box per-bot store.db (the server of record inside a Grok Bot sandbox)
// ---------------------------------------------------------------------------

impl GrokFixtures {
    /// Build `<grok_dir>/agents/<bot_id>/store.db` with the real schema.
    fn write_store_db(&self, bot_id: &str, entries: &[(i64, &str, Value)], title: Option<&str>) {
        let dir = self.grok_dir.join("agents").join(bot_id);
        std::fs::create_dir_all(&dir).expect("agent dir");
        let db = dir.join("store.db");
        let conn = rusqlite::Connection::open(&db).expect("open store.db");
        conn.execute_batch(
            "CREATE TABLE IF NOT EXISTS transcript_entries (
                 seq INTEGER PRIMARY KEY,
                 id TEXT NOT NULL UNIQUE,
                 entry TEXT NOT NULL
             ) STRICT;
             CREATE TABLE IF NOT EXISTS kv (
                 key TEXT PRIMARY KEY,
                 value TEXT NOT NULL
             ) STRICT;",
        )
        .expect("store schema");

        for (seq, id, entry) in entries {
            conn.execute(
                "INSERT OR REPLACE INTO transcript_entries (seq, id, entry) VALUES (?1, ?2, ?3)",
                rusqlite::params![seq, id, entry.to_string()],
            )
            .expect("insert entry");
        }

        if let Some(title) = title {
            let snapshot = json!({
                "version": 1,
                "profileSection": format!(
                    "Agent profile:\nTitle: {title}\nYour agent name is \"{title}\"."
                ),
            });
            conn.execute(
                "INSERT OR REPLACE INTO kv (key, value) VALUES ('agentProfilePromptSnapshot', ?1)",
                rusqlite::params![snapshot.to_string()],
            )
            .expect("insert kv");
            conn.execute(
                "INSERT OR REPLACE INTO kv (key, value) VALUES ('origin', 'user')",
                [],
            )
            .expect("insert origin");
        }
    }
}

#[test]
fn reads_per_bot_store_db_without_any_replicas() {
    // In-box there is no desktop app and no persistence directory at all.
    let fx = GrokFixtures::new();
    std::fs::remove_dir_all(fx.persistence()).expect("drop persistence dir");

    fx.write_store_db(
        BOT_A,
        &[
            (1, "t0u", user_entry(0, "in-box question", 100)),
            (2, "t0s0", send_entry(0, 0, "in-box answer", 101)),
        ],
        Some("Grokctl"),
    );

    assert_eq!(fx.scalar("SELECT COUNT(*) FROM grok_entries"), "2");
    // Identity comes from the store's kv table when no roster exists.
    assert_eq!(fx.scalar("SELECT name FROM grok_bots"), "Grokctl");
    assert_eq!(fx.scalar("SELECT origin FROM grok_bots"), "user");
    assert_eq!(
        fx.scalar("SELECT provenance FROM grok_entries WHERE entry_id = 't0u'"),
        "store_db"
    );
    // `seq` is a real monotonic ordinal, so it drives ordering directly.
    assert_eq!(
        fx.scalar("SELECT source_order FROM grok_entries WHERE entry_id = 't0s0'"),
        "2"
    );
    assert_eq!(
        fx.scalar("SELECT text FROM grok_messages WHERE entry_id = 't0s0'"),
        "in-box answer"
    );
}

#[test]
fn store_db_ingest_resumes_from_the_seq_watermark() {
    let fx = GrokFixtures::new();
    std::fs::remove_dir_all(fx.persistence()).expect("drop persistence dir");
    fx.write_store_db(
        BOT_A,
        &[(1, "t0u", user_entry(0, "first", 100))],
        Some("Grokctl"),
    );
    assert_eq!(fx.scalar("SELECT COUNT(*) FROM grok_entries"), "1");

    // The bot keeps talking; only rows past the watermark are re-read.
    fx.write_store_db(
        BOT_A,
        &[
            (1, "t0u", user_entry(0, "first", 100)),
            (2, "t1u", user_entry(1, "second", 200)),
            (3, "t2u", user_entry(2, "third", 300)),
        ],
        Some("Grokctl"),
    );

    assert_eq!(fx.scalar("SELECT COUNT(*) FROM grok_entries"), "3");
    let status = fx.status();
    assert_eq!(status["entries"], json!(3));
}

#[test]
fn store_db_and_local_replica_merge_for_the_same_bot() {
    let fx = seeded();
    // Same entry_id the replica already holds, now also in the store.
    fx.write_store_db(
        BOT_A,
        &[(
            1,
            "t0u",
            user_entry(0, "can you use grokctl?", 1_787_875_800_675),
        )],
        Some("Grokctl"),
    );

    assert_eq!(
        fx.scalar(&format!(
            "SELECT COUNT(*) FROM grok_entries WHERE bot_id = '{BOT_A}'"
        )),
        "2",
        "a duplicate entry_id must merge across sources"
    );
    assert_eq!(
        fx.scalar("SELECT provenance FROM grok_entries WHERE entry_id = 't0u'"),
        "both"
    );
    // The roster name wins over the store's kv snapshot.
    assert_eq!(
        fx.scalar(&format!(
            "SELECT name FROM grok_bots WHERE bot_id = '{BOT_A}'"
        )),
        "Grokctl"
    );
}

/// store.db carries kinds the desktop replica never shows, `tool-call` among
/// them. An unknown kind must be stored, not dropped.
#[test]
fn store_db_tool_call_entries_are_preserved() {
    let fx = GrokFixtures::new();
    std::fs::remove_dir_all(fx.persistence()).expect("drop persistence dir");
    fx.write_store_db(
        BOT_A,
        &[(
            1,
            "t0c0",
            json!({
                "kind": "tool-call",
                "id": "t0c0",
                "tool": {"name": "run_terminal_command"},
                "timestampMs": 100,
            }),
        )],
        Some("Grokctl"),
    );

    assert_eq!(fx.scalar("SELECT kind FROM grok_entries"), "tool-call");
    assert_eq!(fx.scalar("SELECT COUNT(*) FROM grok_messages"), "0");
    let raw = fx.scalar("SELECT raw_json FROM grok_entries");
    assert!(
        raw.contains("run_terminal_command"),
        "raw_json preserved: {raw}"
    );
}

#[test]
fn a_corrupt_store_db_is_recorded_and_skipped() {
    let fx = seeded();
    let dir = fx.grok_dir.join("agents").join(BOT_B);
    std::fs::create_dir_all(&dir).expect("agent dir");
    std::fs::write(dir.join("store.db"), b"this is not a sqlite database").expect("corrupt store");

    // Local replicas still ingest; the bad store is reported.
    assert_eq!(fx.scalar("SELECT COUNT(*) FROM grok_entries"), "3");
    assert_eq!(
        fx.scalar("SELECT COUNT(*) FROM grok_ingest_errors WHERE error_kind = 'store_db_read'"),
        "1"
    );
}