ai-session-search 1.0.0-rc.4

Local-first search, inspection, export, recovery, and MCP recall across supported AI coding-agent sessions.
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
// SPDX-FileCopyrightText: 2026 Andrew Hundt
// SPDX-License-Identifier: Apache-2.0

//! Incremental byte-offset tail-parse (plan ยง7) integration tests.
//!
//! The load-bearing guarantee is DIFFERENTIAL: after a file is appended to, an incremental
//! reindex (which seeks to the checkpoint and parses only the new bytes) must leave the index
//! in the SAME state as a full reindex of the final file. The fast path is a pure optimization;
//! on truncation or a rewritten head it must fall back to a full parse.

use std::fs::OpenOptions;
use std::io::Write;
use std::path::Path;

use std::io::Cursor;

use ai_session_search::config::Config;
use ai_session_search::db::Db;
use ai_session_search::indexer;
use ai_session_search::models::{Message, MessageFilters, ParsedSession, SearchFilters};
use anyhow::Result;

/// A message reduced to the fields the index search on (ts excluded: cursor uses the file mtime,
/// which legitimately differs between the full and incremental parses; per-message ts is covered
/// by the claude integration test's updated_at assertions).
fn msg_key(m: &Message) -> (String, Option<String>, String) {
    (
        m.role.as_str().to_string(),
        m.tool_name.clone(),
        m.content.clone(),
    )
}

/// Generic per-provider differential check of the tail mechanism: parsing `initial` and then
/// tail-parsing after `appended` is added yields the SAME messages and file-edit counts as a
/// full parse of `initial + appended`. Exercises each provider's real `parse_reader` over the
/// appended byte slice, including the bounded-overlap rebuild of any cross-line tool-id map.
fn assert_tail_matches_full<F>(initial: &str, appended: &str, file_name: &str, parse: F)
where
    F: Fn(Cursor<Vec<u8>>, &std::path::Path) -> Result<ParsedSession>,
{
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join(file_name);
    let final_bytes = format!("{initial}{appended}");

    // Oracle: a full parse of the final content.
    std::fs::write(&path, &final_bytes).unwrap();
    let full = parse(Cursor::new(final_bytes.clone().into_bytes()), &path).unwrap();

    // Checkpoint after `initial`, then append and tail-parse only the new bytes.
    std::fs::write(&path, initial).unwrap();
    let offset = ai_session_search::tail::complete_prefix_offset(&path).unwrap();
    let initial_parsed = parse(Cursor::new(initial.as_bytes().to_vec()), &path).unwrap();
    std::fs::write(&path, &final_bytes).unwrap();
    let tail = ai_session_search::tail::tail_parse(&path, offset, &parse)
        .unwrap()
        .expect("new complete lines were appended");

    let full_keys: Vec<_> = full.messages.iter().map(msg_key).collect();
    let init_keys: Vec<_> = initial_parsed.messages.iter().map(msg_key).collect();
    let tail_keys: Vec<_> = tail.new_messages.iter().map(msg_key).collect();
    let m0 = init_keys.len();
    assert!(m0 > 0, "the initial parse must produce some messages");
    assert!(
        !tail_keys.is_empty(),
        "the append must produce some new messages"
    );
    assert_eq!(
        &full_keys[..m0],
        &init_keys[..],
        "prefix messages match the full parse"
    );
    assert_eq!(
        &full_keys[m0..],
        &tail_keys[..],
        "appended messages match the full parse suffix"
    );
    assert_eq!(
        full.file_edits.len(),
        initial_parsed.file_edits.len() + tail.new_file_edits.len(),
        "file-edit counts add up (prefix + appended == full)"
    );
}

#[test]
fn claude_tail_matches_full_with_cross_boundary_tool_result() {
    // tool_use (tu_1) is in the INITIAL prefix; its tool_result is in the APPENDED tail. The
    // bounded overlap must rebuild the idโ†’name map so the tail tool message is tagged "Bash".
    let adapter = ai_session_search::providers::claude::ClaudeAdapter::new(vec![]);
    let initial = concat!(
        r#"{"type":"user","sessionId":"s","timestamp":"2026-06-01T10:00:00Z","cwd":"/p","message":{"role":"user","content":[{"type":"text","text":"first prompt"}]}}"#,
        "\n",
        r#"{"type":"assistant","sessionId":"s","timestamp":"2026-06-01T10:00:05Z","message":{"role":"assistant","content":[{"type":"text","text":"running bash"},{"type":"tool_use","id":"tu_1","name":"Bash","input":{"command":"ls"}}]}}"#,
        "\n",
    );
    let appended = concat!(
        r#"{"type":"user","sessionId":"s","timestamp":"2026-06-01T10:00:06Z","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"tu_1","content":"a.rs"}]}}"#,
        "\n",
        r#"{"type":"user","sessionId":"s","timestamp":"2026-06-01T10:01:00Z","message":{"role":"user","content":[{"type":"text","text":"second prompt"}]}}"#,
        "\n",
    );
    assert_tail_matches_full(
        initial,
        appended,
        "11111111-2222-3333-4444-555555555555.jsonl",
        |c, p| adapter.parse_reader(c, p),
    );
}

#[test]
fn codex_tail_matches_full_with_cross_boundary_call_output() {
    // function_call (c1) in the prefix; its function_call_output in the tail โ†’ "shell" via overlap.
    let adapter = ai_session_search::providers::codex::CodexAdapter::new(vec![]);
    let initial = concat!(
        r#"{"type":"session_meta","payload":{"id":"019efd97-d602-7922-89dd-467272106505","timestamp":"2026-06-25T07:00:00.000Z","cwd":"/tmp/proj"}}"#,
        "\n",
        r#"{"timestamp":"2026-06-25T07:00:01.000Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"text","text":"do the thing"}]}}"#,
        "\n",
        r#"{"timestamp":"2026-06-25T07:00:02.000Z","type":"response_item","payload":{"type":"function_call","name":"shell","call_id":"c1","arguments":"{}"}}"#,
        "\n",
    );
    let appended = concat!(
        r#"{"timestamp":"2026-06-25T07:00:03.000Z","type":"response_item","payload":{"type":"function_call_output","call_id":"c1","output":"shell ran ok"}}"#,
        "\n",
        r#"{"timestamp":"2026-06-25T07:00:04.000Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"text","text":"next thing"}]}}"#,
        "\n",
    );
    let file = "rollout-2026-06-25T03-04-06-019efd97-d602-7922-89dd-467272106505.jsonl";
    assert_tail_matches_full(initial, appended, file, |c, p| adapter.parse_reader(c, p));
}

#[test]
fn cursor_tail_matches_full_with_cross_boundary_tool_result() {
    // Cursor delegates to claude's idโ†’name map; tool_use (tu_e) in prefix, tool_result in tail.
    let adapter = ai_session_search::providers::cursor::CursorAdapter::new(vec![]);
    let initial = concat!(
        r#"{"role":"user","message":{"content":[{"type":"text","text":"edit the file"}]}}"#,
        "\n",
        r#"{"role":"assistant","message":{"content":[{"type":"text","text":"editing"},{"type":"tool_use","id":"tu_e","name":"Edit","input":{"file_path":"/p/app.ts","old_string":"a","new_string":"b"}}]}}"#,
        "\n",
    );
    let appended = concat!(
        r#"{"role":"user","message":{"content":[{"type":"tool_result","tool_use_id":"tu_e","content":"edit applied"}]}}"#,
        "\n",
        r#"{"role":"user","message":{"content":[{"type":"text","text":"another change please"}]}}"#,
        "\n",
    );
    assert_tail_matches_full(
        initial,
        appended,
        "22222222-3333-4444-5555-666666666666.jsonl",
        |c, p| adapter.parse_reader(c, p),
    );
}

#[test]
fn antigravity_tail_matches_full() {
    // Tail-safe: step types self-identify, no cross-line id map.
    let adapter = ai_session_search::providers::antigravity::AntigravityAdapter::new(vec![]);
    let initial = concat!(
        r#"{"step_index":1,"source":"USER_EXPLICIT","type":"USER_INPUT","status":"DONE","created_at":"2026-05-19T23:13:00Z","content":"hello agent"}"#,
        "\n",
        r#"{"step_index":2,"source":"MODEL","type":"PLANNER_RESPONSE","status":"DONE","created_at":"2026-05-19T23:13:05Z","content":"hello user"}"#,
        "\n",
    );
    let appended = concat!(
        r#"{"step_index":3,"source":"USER_EXPLICIT","type":"USER_INPUT","status":"DONE","created_at":"2026-05-19T23:14:00Z","content":"do more work"}"#,
        "\n",
        r#"{"step_index":4,"source":"MODEL","type":"PLANNER_RESPONSE","status":"DONE","created_at":"2026-05-19T23:14:05Z","content":"on it now"}"#,
        "\n",
    );
    assert_tail_matches_full(initial, appended, "transcript.jsonl", |c, p| {
        adapter.parse_reader(c, p)
    });
}

#[test]
fn pi_tail_matches_full() {
    // Tail-safe: toolResult carries its toolName inline, no cross-line id map.
    let adapter = ai_session_search::providers::pi::PiAdapter::new(vec![]);
    let initial = concat!(
        r#"{"type":"session","version":3,"id":"019edbc9-83df-72a0-a95b-64e6d810ad75","timestamp":"2026-06-18T17:31:17.343Z","cwd":"/p"}"#,
        "\n",
        r#"{"type":"message","timestamp":"2026-06-18T17:31:32.922Z","message":{"role":"user","content":[{"type":"text","text":"edit some files"}]}}"#,
        "\n",
        r#"{"type":"message","timestamp":"2026-06-18T17:31:36.595Z","message":{"role":"assistant","content":[{"type":"text","text":"writing it"},{"type":"toolCall","id":"t1","name":"write","arguments":{"path":"src/new.ts","content":"export const x = 1;"}}]}}"#,
        "\n",
    );
    let appended = concat!(
        r#"{"type":"message","timestamp":"2026-06-18T17:31:40.000Z","message":{"role":"toolResult","toolCallId":"t1","toolName":"write","content":[{"type":"text","text":"file written"}],"isError":false}}"#,
        "\n",
        r#"{"type":"message","timestamp":"2026-06-18T17:31:44.000Z","message":{"role":"user","content":[{"type":"text","text":"now run the tests"}]}}"#,
        "\n",
    );
    let file = "2026-06-18T17-31-17-343Z_019edbc9-83df-72a0-a95b-64e6d810ad75.jsonl";
    assert_tail_matches_full(initial, appended, file, |c, p| adapter.parse_reader(c, p));
}

#[test]
fn prime_agent_tail_matches_full() {
    let adapter = ai_session_search::providers::pi::PiAdapter::prime_agent(vec![]);
    let initial = concat!(
        r#"{"type":"session","version":3,"id":"019edbc9-83df-72a0-a95b-64e6d810ad75","timestamp":"2026-06-18T17:31:17.343Z","cwd":"/p"}"#,
        "\n",
        r#"{"type":"message","timestamp":"2026-06-18T17:31:32.922Z","message":{"role":"user","content":[{"type":"text","text":"inspect the project"}]}}"#,
        "\n",
    );
    let appended = concat!(
        r#"{"type":"message","timestamp":"2026-06-18T17:31:36.595Z","message":{"role":"assistant","content":[{"type":"text","text":"inspection complete"}]}}"#,
        "\n",
    );
    assert_tail_matches_full(
        initial,
        appended,
        "019edbc9-83df-72a0-a95b-64e6d810ad75.jsonl",
        |c, p| adapter.parse_reader(c, p),
    );
}

/// All sessions, no filtering (for `list_recent`).
fn all_sessions() -> SearchFilters {
    SearchFilters {
        limit: 100,
        ..SearchFilters::default()
    }
}

/// Lines 1-5 of a Claude session: user, assistant(+Bash tool_use), tool_result(Bash),
/// assistant(+Write edit), user. Each assistant turn carries text so it yields a message.
const INITIAL: &str = concat!(
    r#"{"type":"user","sessionId":"tail-sess","timestamp":"2026-06-01T10:00:00Z","cwd":"/tmp/proj","message":{"role":"user","content":[{"type":"text","text":"first user prompt about the project"}]}}"#,
    "\n",
    r#"{"type":"assistant","sessionId":"tail-sess","timestamp":"2026-06-01T10:00:05Z","message":{"role":"assistant","content":[{"type":"text","text":"running a bash command now"},{"type":"tool_use","id":"tu_1","name":"Bash","input":{"command":"ls"}}]}}"#,
    "\n",
    r#"{"type":"user","sessionId":"tail-sess","timestamp":"2026-06-01T10:00:06Z","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"tu_1","content":"a.rs b.rs"}]}}"#,
    "\n",
    r#"{"type":"assistant","sessionId":"tail-sess","timestamp":"2026-06-01T10:00:10Z","message":{"role":"assistant","content":[{"type":"text","text":"writing the file now"},{"type":"tool_use","id":"tu_2","name":"Write","input":{"file_path":"/tmp/proj/a.rs","content":"fn main() {}"}}]}}"#,
    "\n",
    r#"{"type":"user","sessionId":"tail-sess","timestamp":"2026-06-01T10:01:00Z","message":{"role":"user","content":[{"type":"text","text":"second user prompt continues the work"}]}}"#,
    "\n",
);

/// Lines 6-8 appended later: assistant(+Bash tool_use), tool_result(Bash), user.
const APPENDED: &str = concat!(
    r#"{"type":"assistant","sessionId":"tail-sess","timestamp":"2026-06-01T10:02:00Z","message":{"role":"assistant","content":[{"type":"text","text":"running another command"},{"type":"tool_use","id":"tu_3","name":"Bash","input":{"command":"cargo test"}}]}}"#,
    "\n",
    r#"{"type":"user","sessionId":"tail-sess","timestamp":"2026-06-01T10:02:01Z","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"tu_3","content":"test result ok"}]}}"#,
    "\n",
    r#"{"type":"user","sessionId":"tail-sess","timestamp":"2026-06-01T10:03:00Z","message":{"role":"user","content":[{"type":"text","text":"third user prompt after the append"}]}}"#,
    "\n",
);

fn claude_only_config(root: &Path, projects: &Path) -> Config {
    let mut cfg = Config::default();
    cfg.providers.claude.enabled = true;
    cfg.providers.claude.paths = vec![projects.to_string_lossy().to_string()];
    cfg.providers.claude_desktop.enabled = false;
    cfg.providers.codex.enabled = false;
    cfg.providers.cursor.enabled = false;
    cfg.providers.antigravity.enabled = false;
    cfg.providers.pi.enabled = false;
    cfg.providers.prime_agent.enabled = false;
    cfg.providers.aistudio.enabled = false;
    cfg.providers.gemini_cli.enabled = false;
    cfg.index.db_path = Some(root.join("index.db").to_string_lossy().to_string());
    cfg
}

/// Every message as a comparable tuple, in (session_id, seq) order.
type Row = (i64, String, Option<String>, String, Option<String>);
fn rows(db: &Db) -> Vec<Row> {
    db.search_messages("", &MessageFilters::default())
        .unwrap()
        .into_iter()
        .map(|h| {
            (
                h.seq,
                h.role.as_str().to_string(),
                h.tool_name.clone(),
                h.content.clone(),
                h.ts.map(|t| t.to_rfc3339()),
            )
        })
        .collect()
}

fn append(path: &Path, bytes: &str) {
    let mut f = OpenOptions::new().append(true).open(path).unwrap();
    f.write_all(bytes.as_bytes()).unwrap();
}

/// Index INITIAL, append, reindex (tail path). The result must equal a full reindex of the
/// final file โ€” messages, file edits, FTS/trigram sync, and session metadata.
#[test]
fn tail_append_matches_full_reindex() {
    let dir = tempfile::tempdir().unwrap();
    let projects = dir.path().join("projects");
    std::fs::create_dir_all(projects.join("proj")).unwrap();
    let file = projects.join("proj/tail-sess.jsonl");
    std::fs::write(&file, INITIAL).unwrap();

    let cfg = claude_only_config(dir.path(), &projects);
    let db = Db::open(&cfg.db_path()).unwrap();
    indexer::reindex(&cfg, &db, true, None).unwrap();
    let initial_rows = rows(&db);
    assert_eq!(
        initial_rows.len(),
        7,
        "7 messages before the append (2 user, 2 assistant, 1 tool result, 2 tool-call inputs)"
    );

    // Append three turns, then an INCREMENTAL reindex (the tail fast path).
    append(&file, APPENDED);
    let (_total, updated) = indexer::reindex(&cfg, &db, false, None).unwrap();
    assert_eq!(updated, 1, "the grown file is re-touched exactly once");
    let tail_rows = rows(&db);
    assert_eq!(
        tail_rows.len(),
        11,
        "4 new messages appended (1 tool-call input, 1 assistant, 1 tool result, 1 user)"
    );
    // The unchanged prefix rows are byte-identical to before (append, not reparse-and-replace).
    assert_eq!(
        &tail_rows[..7],
        &initial_rows[..],
        "prefix rows unchanged by the tail append"
    );

    // Oracle: a fresh DB fully reindexing the FINAL file.
    let dir2 = tempfile::tempdir().unwrap();
    let projects2 = dir2.path().join("projects");
    std::fs::create_dir_all(projects2.join("proj")).unwrap();
    std::fs::write(
        projects2.join("proj/tail-sess.jsonl"),
        format!("{INITIAL}{APPENDED}"),
    )
    .unwrap();
    let cfg2 = claude_only_config(dir2.path(), &projects2);
    let full_db = Db::open(&cfg2.db_path()).unwrap();
    indexer::reindex(&cfg2, &full_db, true, None).unwrap();

    // DIFFERENTIAL: the incrementally-appended index equals the full reindex.
    assert_eq!(
        tail_rows,
        rows(&full_db),
        "tail-append rows == full-reindex rows"
    );
    assert_eq!(
        db.message_count().unwrap(),
        full_db.message_count().unwrap()
    );
    assert_eq!(
        db.messages_fts_count().unwrap(),
        db.message_count().unwrap(),
        "FTS in sync"
    );
    assert_eq!(
        db.file_edit_count().unwrap(),
        full_db.file_edit_count().unwrap()
    );
    // The unresolved Write from line 4 is not a proven recoverable file state.
    assert_eq!(db.file_edit_count().unwrap(), 0);
    // tool_result messages are tagged with the originating tool across the boundary.
    let tools: Vec<_> = tail_rows
        .iter()
        .filter(|r| r.1 == "tool")
        .map(|r| r.2.clone())
        .collect();
    assert_eq!(
        tools,
        vec![
            Some("Bash".to_string()),
            Some("Bash".to_string()),
            Some("Write".to_string()),
            Some("Bash".to_string()),
            Some("Bash".to_string())
        ]
    );

    // Session metadata advanced: title/updated_at reflect the appended turns.
    let session = &db.list_recent(&all_sessions()).unwrap()[0];
    let full_session = &full_db.list_recent(&all_sessions()).unwrap()[0];
    assert_eq!(
        session.title, full_session.title,
        "title matches full reindex"
    );
    assert_eq!(
        session.updated_at, full_session.updated_at,
        "updated_at matches full reindex"
    );
    assert_eq!(session.message_count, Some(11));
}

/// A valid final JSONL record without a trailing newline is parsed during the initial full
/// reindex. When the provider later terminates that record and appends another one, incremental
/// reindexing must not append the already-indexed record a second time.
#[test]
fn unterminated_record_completed_by_append_matches_full_reindex() {
    let dir = tempfile::tempdir().unwrap();
    let projects = dir.path().join("projects");
    std::fs::create_dir_all(projects.join("proj")).unwrap();
    let file = projects.join("proj/tail-sess.jsonl");
    let first = r#"{"type":"user","sessionId":"tail-sess","timestamp":"2026-06-01T10:00:00Z","cwd":"/tmp/proj","message":{"role":"user","content":[{"type":"text","text":"first complete record"}]}}"#;
    let unterminated = r#"{"type":"assistant","sessionId":"tail-sess","timestamp":"2026-06-01T10:00:05Z","message":{"role":"assistant","content":[{"type":"text","text":"valid record without newline"}]}}"#;
    let appended = r#"{"type":"user","sessionId":"tail-sess","timestamp":"2026-06-01T10:01:00Z","message":{"role":"user","content":[{"type":"text","text":"new record after flush"}]}}"#;
    std::fs::write(&file, format!("{first}\n{unterminated}")).unwrap();

    let cfg = claude_only_config(dir.path(), &projects);
    let db = Db::open(&cfg.db_path()).unwrap();
    indexer::reindex(&cfg, &db, true, None).unwrap();
    assert_eq!(
        rows(&db).len(),
        2,
        "the parser accepts the final valid record"
    );

    append(&file, &format!("\n{appended}\n"));
    indexer::reindex(&cfg, &db, false, None).unwrap();
    let incremental_rows = rows(&db);

    let oracle_dir = tempfile::tempdir().unwrap();
    let oracle_projects = oracle_dir.path().join("projects");
    std::fs::create_dir_all(oracle_projects.join("proj")).unwrap();
    std::fs::write(
        oracle_projects.join("proj/tail-sess.jsonl"),
        format!("{first}\n{unterminated}\n{appended}\n"),
    )
    .unwrap();
    let oracle_cfg = claude_only_config(oracle_dir.path(), &oracle_projects);
    let oracle_db = Db::open(&oracle_cfg.db_path()).unwrap();
    indexer::reindex(&oracle_cfg, &oracle_db, true, None).unwrap();

    assert_eq!(
        incremental_rows,
        rows(&oracle_db),
        "completing an indexed final record must not duplicate it"
    );
}

fn codex_only_config(root: &Path, codex_root: &Path) -> Config {
    let mut cfg = Config::default();
    cfg.providers.claude.enabled = false;
    cfg.providers.claude_desktop.enabled = false;
    cfg.providers.codex.enabled = true;
    cfg.providers.codex.paths = vec![codex_root.to_string_lossy().to_string()];
    cfg.providers.cursor.enabled = false;
    cfg.providers.antigravity.enabled = false;
    cfg.providers.pi.enabled = false;
    cfg.providers.prime_agent.enabled = false;
    cfg.providers.aistudio.enabled = false;
    cfg.providers.gemini_cli.enabled = false;
    cfg.index.db_path = Some(root.join("index.db").to_string_lossy().to_string());
    cfg
}

const CODEX_FILE: &str = "rollout-2026-06-25T03-04-06-019efd97-d602-7922-89dd-467272106505.jsonl";
const CODEX_INITIAL: &str = concat!(
    r#"{"type":"session_meta","payload":{"id":"019efd97-d602-7922-89dd-467272106505","timestamp":"2026-06-25T07:00:00.000Z","cwd":"/tmp/proj"}}"#,
    "\n",
    r#"{"timestamp":"2026-06-25T07:00:01.000Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"text","text":"do the thing"}]}}"#,
    "\n",
    r#"{"timestamp":"2026-06-25T07:00:02.000Z","type":"response_item","payload":{"type":"function_call","name":"shell","call_id":"c1","arguments":"{}"}}"#,
    "\n",
);
const CODEX_APPENDED: &str = concat!(
    r#"{"timestamp":"2026-06-25T07:00:03.000Z","type":"response_item","payload":{"type":"function_call_output","call_id":"c1","output":"shell ran ok"}}"#,
    "\n",
    r#"{"timestamp":"2026-06-25T07:00:04.000Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"text","text":"next thing please"}]}}"#,
    "\n",
);

/// Drive the REAL indexer reindex tail path for codex (non-claude dispatch), end-to-end: a tail
/// append after the initial index must match a full reindex of the final file.
#[test]
fn codex_tail_reindex_matches_full_reindex() {
    let dir = tempfile::tempdir().unwrap();
    let codex_root = dir.path().join("codex");
    std::fs::create_dir_all(&codex_root).unwrap();
    let file = codex_root.join(CODEX_FILE);
    std::fs::write(&file, CODEX_INITIAL).unwrap();

    let cfg = codex_only_config(dir.path(), &codex_root);
    let db = Db::open(&cfg.db_path()).unwrap();
    indexer::reindex(&cfg, &db, true, None).unwrap();
    let before = rows(&db);
    // The user message plus the function_call input; session_meta produces no message row.
    assert_eq!(
        before.len(),
        2,
        "2 messages before the append (the user turn and tool-call input)"
    );

    append(&file, CODEX_APPENDED);
    let (_total, updated) = indexer::reindex(&cfg, &db, false, None).unwrap();
    assert_eq!(
        updated, 1,
        "the grown codex file is re-touched once via the tail path"
    );
    let after = rows(&db);
    assert!(after.len() > before.len(), "the tail append added messages");
    // The cross-boundary function_call_output is tagged with the originating tool via the overlap.
    let tool = after
        .iter()
        .find(|r| r.1 == "tool")
        .expect("tool output indexed");
    assert_eq!(
        tool.2,
        Some("shell".to_string()),
        "tool output tagged with shell across the boundary"
    );

    let dir2 = tempfile::tempdir().unwrap();
    let codex_root2 = dir2.path().join("codex");
    std::fs::create_dir_all(&codex_root2).unwrap();
    std::fs::write(
        codex_root2.join(CODEX_FILE),
        format!("{CODEX_INITIAL}{CODEX_APPENDED}"),
    )
    .unwrap();
    let cfg2 = codex_only_config(dir2.path(), &codex_root2);
    let full_db = Db::open(&cfg2.db_path()).unwrap();
    indexer::reindex(&cfg2, &full_db, true, None).unwrap();

    assert_eq!(
        after,
        rows(&full_db),
        "codex tail-append reindex == full reindex"
    );
    assert_eq!(
        db.messages_fts_count().unwrap(),
        db.message_count().unwrap(),
        "FTS in sync"
    );
}

/// Truncation (file shrinks below the checkpoint) must fall back to a full parse, not a tail.
#[test]
fn truncation_falls_back_to_full_parse() {
    let dir = tempfile::tempdir().unwrap();
    let projects = dir.path().join("projects");
    std::fs::create_dir_all(projects.join("proj")).unwrap();
    let file = projects.join("proj/tail-sess.jsonl");
    std::fs::write(&file, format!("{INITIAL}{APPENDED}")).unwrap();

    let cfg = claude_only_config(dir.path(), &projects);
    let db = Db::open(&cfg.db_path()).unwrap();
    indexer::reindex(&cfg, &db, true, None).unwrap();
    assert_eq!(rows(&db).len(), 11);

    // Shrink the file to just the first 3 lines (a copytruncate-style rotation).
    std::fs::write(&file, INITIAL).unwrap();
    indexer::reindex(&cfg, &db, false, None).unwrap();
    // A full reparse re-derived the (now shorter) session: 7 messages, durable archive keeps none
    // of the removed tail (delete+insert replace on shrink).
    assert_eq!(
        rows(&db).len(),
        7,
        "shrink โ†’ full reparse of the truncated file"
    );
    assert_eq!(
        db.messages_fts_count().unwrap(),
        db.message_count().unwrap()
    );
}

/// A rewritten head (different first bytes โ†’ fingerprint mismatch) must full-parse, not tail.
#[test]
fn rewritten_head_falls_back_to_full_parse() {
    let dir = tempfile::tempdir().unwrap();
    let projects = dir.path().join("projects");
    std::fs::create_dir_all(projects.join("proj")).unwrap();
    let file = projects.join("proj/tail-sess.jsonl");
    std::fs::write(&file, INITIAL).unwrap();

    let cfg = claude_only_config(dir.path(), &projects);
    let db = Db::open(&cfg.db_path()).unwrap();
    indexer::reindex(&cfg, &db, true, None).unwrap();

    // Rewrite the whole file with a DIFFERENT first line (same session id, new content), longer
    // than the original so it is not caught by the truncation guard โ€” only the fingerprint differs.
    let new_head = r#"{"type":"user","sessionId":"tail-sess","timestamp":"2026-06-01T09:00:00Z","cwd":"/tmp/proj","message":{"role":"user","content":[{"type":"text","text":"completely different opening prompt now"}]}}"#;
    let rewritten = format!("{new_head}\n{APPENDED}{APPENDED}");
    std::fs::write(&file, &rewritten).unwrap();
    indexer::reindex(&cfg, &db, false, None).unwrap();

    // Oracle full reindex of the rewritten file.
    let dir2 = tempfile::tempdir().unwrap();
    let projects2 = dir2.path().join("projects");
    std::fs::create_dir_all(projects2.join("proj")).unwrap();
    std::fs::write(projects2.join("proj/tail-sess.jsonl"), &rewritten).unwrap();
    let cfg2 = claude_only_config(dir2.path(), &projects2);
    let full_db = Db::open(&cfg2.db_path()).unwrap();
    indexer::reindex(&cfg2, &full_db, true, None).unwrap();

    assert_eq!(
        rows(&db),
        rows(&full_db),
        "rewritten head โ†’ full reparse equals oracle"
    );
}

fn claude_message_line(content: &str) -> String {
    format!(
        "{{\"type\":\"user\",\"sessionId\":\"integrity-sess\",\"timestamp\":\"2026-06-01T10:00:00Z\",\"cwd\":\"/p\",\"message\":{{\"role\":\"user\",\"content\":{}}}}}\n",
        serde_json::to_string(content).unwrap()
    )
}

fn message_contents(db: &Db) -> Vec<String> {
    db.read_session_messages(
        &MessageFilters {
            session_id: Some("claude:integrity-sess".to_string()),
            ..MessageFilters::default()
        },
        ai_session_search::db::MessageOrder::OldestFirst,
    )
    .unwrap()
    .into_iter()
    .map(|message| message.content)
    .collect()
}

#[test]
fn total_reparse_failure_preserves_the_last_successful_archive() {
    let dir = tempfile::tempdir().unwrap();
    let projects = dir.path().join("projects");
    std::fs::create_dir_all(projects.join("proj")).unwrap();
    let file = projects.join("proj/integrity-sess.jsonl");
    std::fs::write(&file, claude_message_line("archived before failure")).unwrap();

    let cfg = claude_only_config(dir.path(), &projects);
    let db = Db::open(&cfg.db_path()).unwrap();
    indexer::reindex(&cfg, &db, false, None).unwrap();
    assert_eq!(message_contents(&db), ["archived before failure"]);

    std::fs::write(&file, "{this is a partial JSON write\n").unwrap();
    let (_seen, updated) = indexer::reindex(&cfg, &db, false, None).unwrap();
    assert_eq!(updated, 0, "a failed parse must not publish a replacement");
    assert_eq!(
        message_contents(&db),
        ["archived before failure"],
        "the last successful archive must remain searchable"
    );

    std::fs::write(&file, claude_message_line("recovered after failure")).unwrap();
    let (_seen, updated) = indexer::reindex(&cfg, &db, false, None).unwrap();
    assert_eq!(updated, 1, "the stale checkpoint must retry after recovery");
    assert_eq!(message_contents(&db), ["recovered after failure"]);
}

#[test]
fn same_size_body_rewrite_past_the_head_fingerprint_is_reparsed() {
    let dir = tempfile::tempdir().unwrap();
    let projects = dir.path().join("projects");
    std::fs::create_dir_all(projects.join("proj")).unwrap();
    let file = projects.join("proj/integrity-sess.jsonl");
    let stable_head = claude_message_line(&"x".repeat(5_000));
    let original = format!("{stable_head}{}", claude_message_line("body-aaaa"));
    let rewritten = format!("{stable_head}{}", claude_message_line("body-bbbb"));
    assert_eq!(original.len(), rewritten.len());
    std::fs::write(&file, &original).unwrap();

    let cfg = claude_only_config(dir.path(), &projects);
    let db = Db::open(&cfg.db_path()).unwrap();
    indexer::reindex(&cfg, &db, false, None).unwrap();

    std::thread::sleep(std::time::Duration::from_millis(2));
    std::fs::write(&file, &rewritten).unwrap();
    let (_seen, updated) = indexer::reindex(&cfg, &db, false, None).unwrap();

    assert_eq!(updated, 1);
    let contents = message_contents(&db);
    assert!(contents.iter().any(|content| content == "body-bbbb"));
    assert!(!contents.iter().any(|content| content == "body-aaaa"));
}

#[test]
fn full_parse_replaces_a_changed_prefix_even_when_its_boundary_is_unchanged() {
    let dir = tempfile::tempdir().unwrap();
    let projects = dir.path().join("projects");
    std::fs::create_dir_all(projects.join("proj")).unwrap();
    let file = projects.join("proj/integrity-sess.jsonl");
    let initial = ["opening-old", "middle-stable", "boundary-stable"]
        .into_iter()
        .map(claude_message_line)
        .collect::<String>();
    std::fs::write(&file, initial).unwrap();

    let cfg = claude_only_config(dir.path(), &projects);
    let db = Db::open(&cfg.db_path()).unwrap();
    indexer::reindex(&cfg, &db, false, None).unwrap();

    let rewritten = [
        "opening-new",
        "middle-stable",
        "boundary-stable",
        "appended-message",
    ]
    .into_iter()
    .map(claude_message_line)
    .collect::<String>();
    std::fs::write(&file, rewritten).unwrap();
    let (_seen, updated) = indexer::reindex(&cfg, &db, false, None).unwrap();

    assert_eq!(updated, 1);
    assert_eq!(
        message_contents(&db),
        [
            "opening-new",
            "middle-stable",
            "boundary-stable",
            "appended-message",
        ]
    );
}

#[test]
fn claude_desktop_sidecar_change_refreshes_without_audit_append() {
    let dir = tempfile::tempdir().unwrap();
    let root = dir.path().join("local-agent-mode-sessions");
    let parent = root.join("install/account");
    let session_dir = parent.join("local_meta-refresh");
    std::fs::create_dir_all(&session_dir).unwrap();
    let sidecar = parent.join("local_meta-refresh.json");
    std::fs::write(
        &sidecar,
        r#"{"sessionId":"meta-refresh","cwd":"/tmp/old","title":"Old Title","createdAt":"2026-04-01T00:00:00Z","lastActivityAt":"2026-04-01T00:00:00Z"}"#,
    )
    .unwrap();
    std::fs::write(
        session_dir.join("audit.jsonl"),
        r#"{"type":"user","session_id":"meta-refresh","message":{"role":"user","content":"hello desktop"},"_audit_timestamp":"2026-04-01T00:00:00Z"}"#,
    )
    .unwrap();

    let cfg = claude_only_config(dir.path(), &root);
    let db = Db::open(&cfg.db_path()).unwrap();
    indexer::reindex(&cfg, &db, false, None).unwrap();
    let first = db
        .resolve_session("claude-desktop:meta-refresh")
        .unwrap()
        .session;
    assert_eq!(first.provider.as_str(), "claude-desktop");
    assert_eq!(first.title.as_deref(), Some("Old Title"));
    assert_eq!(first.cwd.as_deref(), Some("/tmp/old"));
    assert_eq!(
        db.search_messages(
            "",
            &MessageFilters {
                providers: Some(vec![ai_session_search::models::Provider::ClaudeDesktop,]),
                ..MessageFilters::default()
            },
        )
        .unwrap()
        .len(),
        1
    );
    assert_eq!(
        db.search_messages(
            "",
            &MessageFilters {
                providers: Some(vec![ai_session_search::models::Provider::Claude]),
                ..MessageFilters::default()
            },
        )
        .unwrap()
        .len(),
        0,
        "claude-desktop messages must not be returned by provider=claude"
    );

    std::fs::write(
        &sidecar,
        r#"{"sessionId":"meta-refresh","cwd":"/tmp/new","title":"New Title","createdAt":"2026-04-01T00:00:00Z","lastActivityAt":"2026-04-01T00:05:00Z"}"#,
    )
    .unwrap();
    indexer::reindex(&cfg, &db, false, None).unwrap();

    let refreshed = db
        .resolve_session("claude-desktop:meta-refresh")
        .unwrap()
        .session;
    assert_eq!(refreshed.title.as_deref(), Some("New Title"));
    assert_eq!(refreshed.cwd.as_deref(), Some("/tmp/new"));
    assert_eq!(
        rows(&db),
        vec![(
            0,
            "user".to_string(),
            None,
            "hello desktop".to_string(),
            Some("2026-04-01T00:00:00+00:00".to_string())
        )],
        "sidecar-only refresh must not duplicate messages"
    );
}

/// Perf benchmark (opt-in: `cargo test --test tail_parse -- --ignored --nocapture`). Builds a
/// large session, then compares an INCREMENTAL reindex after a small append (the tail fast path)
/// against a FULL reindex of the same final file. The tail path reads only ~1 MiB of overlap plus
/// the appended bytes, so it should be dramatically faster than re-reading the whole file.
#[test]
#[ignore]
fn bench_incremental_vs_full_on_large_session() {
    use std::time::Instant;

    let dir = tempfile::tempdir().unwrap();
    let projects = dir.path().join("projects");
    std::fs::create_dir_all(projects.join("proj")).unwrap();
    let file = projects.join("proj/tail-sess.jsonl");

    // ~40 MB session: many user/assistant turns.
    let mut big = String::with_capacity(42 * 1024 * 1024);
    let turn = |i: usize| {
        format!(
            "{}\n{}\n",
            format_args!(
                r#"{{"type":"user","sessionId":"tail-sess","timestamp":"2026-06-01T10:00:00Z","cwd":"/tmp/proj","message":{{"role":"user","content":[{{"type":"text","text":"user turn {i} with a reasonably long body of text to make the file sizable for the benchmark padding padding padding"}}]}}}}"#
            ),
            format_args!(
                r#"{{"type":"assistant","sessionId":"tail-sess","timestamp":"2026-06-01T10:00:01Z","message":{{"role":"assistant","content":[{{"type":"text","text":"assistant reply {i} also with a reasonably long body of text to make the file sizable for the benchmark padding padding padding"}}]}}}}"#
            ),
        )
    };
    let mut i = 0;
    while big.len() < 40 * 1024 * 1024 {
        big.push_str(&turn(i));
        i += 1;
    }
    std::fs::write(&file, &big).unwrap();
    let mb = big.len() as f64 / (1024.0 * 1024.0);

    let cfg = claude_only_config(dir.path(), &projects);
    let db = Db::open(&cfg.db_path()).unwrap();
    let t0 = Instant::now();
    indexer::reindex(&cfg, &db, true, None).unwrap();
    let initial_full = t0.elapsed();

    // Append two small turns, then time the INCREMENTAL reindex (tail path).
    append(&file, &turn(i));
    let t1 = Instant::now();
    indexer::reindex(&cfg, &db, false, None).unwrap();
    let incremental = t1.elapsed();

    // Time a FULL reindex of the same final file for comparison.
    let t2 = Instant::now();
    indexer::reindex(&cfg, &db, true, None).unwrap();
    let full = t2.elapsed();

    println!(
        "tail-parse benchmark ({mb:.1} MB session):\n  initial full index: {initial_full:?}\n  \
         incremental (tail) after append: {incremental:?}\n  full reindex of final file: {full:?}\n  \
         speedup (full / incremental): {:.1}x",
        full.as_secs_f64() / incremental.as_secs_f64().max(1e-9)
    );
    assert!(
        incremental < full,
        "incremental tail reindex must beat a full reindex"
    );
}

/// A partially written trailing line (no newline yet) adds no message; once it is completed the
/// next reindex appends it.
#[test]
fn partial_trailing_line_is_indexed_only_once_complete() {
    let dir = tempfile::tempdir().unwrap();
    let projects = dir.path().join("projects");
    std::fs::create_dir_all(projects.join("proj")).unwrap();
    let file = projects.join("proj/tail-sess.jsonl");
    std::fs::write(&file, INITIAL).unwrap();

    let cfg = claude_only_config(dir.path(), &projects);
    let db = Db::open(&cfg.db_path()).unwrap();
    indexer::reindex(&cfg, &db, true, None).unwrap();
    assert_eq!(rows(&db).len(), 7);

    // Append a partial line (no trailing newline) โ€” mid-flush.
    let partial = r#"{"type":"user","sessionId":"tail-sess","timestamp":"2026-06-01T10:02:00Z","message":{"role":"user","content":[{"type":"text","text":"a half-written prompt"#;
    append(&file, partial);
    indexer::reindex(&cfg, &db, false, None).unwrap();
    assert_eq!(rows(&db).len(), 7, "a partial line yields no message yet");

    // Complete the line (close the JSON + newline) โ†’ the next reindex appends exactly one message.
    append(&file, "\"}]}}\n");
    indexer::reindex(&cfg, &db, false, None).unwrap();
    let r = rows(&db);
    assert_eq!(r.len(), 8, "the completed line is appended once");
    assert_eq!(
        r[7].3, "a half-written prompt",
        "the completed user message content is correct"
    );
}

/// A complete line in the appended tail whose bytes are not valid UTF-8 (e.g. binary embedded in a
/// tool-output line, or a bounded overlap window that begins mid-character) must NOT abort the
/// reindex. It is recovered IN THE TAIL by `util::lines_replacing_invalid_utf8`: the stray bad
/// byte becomes U+FFFD and the message is kept โ€” no full rebuild, and the fast path is retained.
/// Regression test โ€” a `parse_slice` error ("stream did not contain valid UTF-8") used to propagate
/// out of `try_tail` โ†’ `reindex`, breaking every read command (which auto-reindexes first). The
/// `try_tail` โ†’ `FullParse` branch remains as defense-in-depth for non-content (I/O) tail errors.
#[test]
fn tail_parse_invalid_utf8_recovered_in_tail_consistent_with_full() {
    let dir = tempfile::tempdir().unwrap();
    let projects = dir.path().join("projects");
    std::fs::create_dir_all(projects.join("proj")).unwrap();
    let file = projects.join("proj/tail-sess.jsonl");
    std::fs::write(&file, INITIAL).unwrap();

    let cfg = claude_only_config(dir.path(), &projects);
    let db = Db::open(&cfg.db_path()).unwrap();
    indexer::reindex(&cfg, &db, true, None).unwrap();
    assert_eq!(rows(&db).len(), 7);

    // Append valid turns, then a COMPLETE line carrying a raw 0xFF byte (never valid in UTF-8).
    {
        let mut f = OpenOptions::new().append(true).open(&file).unwrap();
        f.write_all(APPENDED.as_bytes()).unwrap();
        let mut bad = br#"{"type":"user","sessionId":"tail-sess","timestamp":"2026-06-01T10:04:00Z","message":{"role":"user","content":[{"type":"text","text":"bad "#.to_vec();
        bad.push(0xFF);
        bad.extend_from_slice(br#" byte"}]}}"#);
        bad.push(b'\n');
        f.write_all(&bad).unwrap();
    }

    // The incremental reindex must succeed (recover lossily in the tail), not return Err.
    let res = indexer::reindex(&cfg, &db, false, None);
    assert!(
        res.is_ok(),
        "an invalid-UTF-8 tail must be recovered, not abort the reindex: {res:?}"
    );
    // The appended messages (including the bad-byte line) were recovered, not dropped.
    let recovered = rows(&db);
    assert_eq!(
        recovered.len(),
        12,
        "7 initial + 4 appended + 1 recovered bad-byte line"
    );
    assert!(
        recovered.iter().any(|r| r.3.contains('\u{FFFD}')),
        "the invalid byte was recovered as U+FFFD, not dropped"
    );

    // DIFFERENTIAL: the degraded incremental index equals a full reindex of the identical final
    // file (the panic-safe full parse reduces the now-invalid file to the same state in both).
    let dir2 = tempfile::tempdir().unwrap();
    let projects2 = dir2.path().join("projects");
    std::fs::create_dir_all(projects2.join("proj")).unwrap();
    std::fs::copy(&file, projects2.join("proj/tail-sess.jsonl")).unwrap();
    let cfg2 = claude_only_config(dir2.path(), &projects2);
    let full_db = Db::open(&cfg2.db_path()).unwrap();
    indexer::reindex(&cfg2, &full_db, true, None).unwrap();
    assert_eq!(
        rows(&db),
        rows(&full_db),
        "lossy-recovered tail-append index == full reindex of the same final file"
    );
}

/// A Codex rollout without a `session_meta` line (an old-format file, a copy from another
/// machine, or the tail slice past the 1 MiB overlap of any grown rollout) yields the last
/// activity but no start. That must index as a point span at the last activity and must never
/// stop the reindex: it did, because the missing start was filled from the file mtime, which
/// follows the native end, and the reversed span was refused with `?` for every source after it.
#[test]
fn codex_rollout_without_session_meta_indexes_as_a_point_span_and_the_reindex_continues() {
    let dir = tempfile::tempdir().unwrap();
    let codex_root = dir.path().join("codex");
    std::fs::create_dir_all(&codex_root).unwrap();
    const META_LESS: &str =
        "rollout-2026-06-24T01-00-00-019efd00-0000-7000-8000-000000000001.jsonl";
    std::fs::write(
        codex_root.join(META_LESS),
        concat!(
            r#"{"timestamp":"2026-06-24T01:00:01.000Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"text","text":"start without meta"}]}}"#,
            "\n",
            r#"{"timestamp":"2026-06-24T01:00:02.000Z","type":"response_item","payload":{"type":"message","role":"assistant","content":[{"type":"output_text","text":"working"}]}}"#,
            "\n",
        ),
    )
    .unwrap();
    // A healthy sibling that sorts after the meta-less file and must still be indexed.
    std::fs::write(codex_root.join(CODEX_FILE), CODEX_INITIAL).unwrap();

    let cfg = codex_only_config(dir.path(), &codex_root);
    let db = Db::open(&cfg.db_path()).unwrap();
    let (files_seen, sessions_updated) = indexer::reindex(&cfg, &db, true, None)
        .expect("one span-less file must not stop the reindex");
    assert_eq!(files_seen, 2);
    assert_eq!(sessions_updated, 2, "the healthy sibling is indexed too");

    let meta_less = db
        .resolve_session_record("019efd00-0000-7000-8000-000000000001")
        .unwrap();
    let end = meta_less.updated_at.expect("the last activity is known");
    assert_eq!(
        meta_less.created_at,
        Some(end),
        "an unknown start becomes a point span at the known end, never the file mtime"
    );
    assert!(
        meta_less.parse_warning.as_deref().unwrap_or("").is_empty(),
        "a point span is ordinary, not a warning: {:?}",
        meta_less.parse_warning
    );
}