ctxone 1.3.2

The `ctx` CLI for CTXone, memory and context for AI agents
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
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
//! Session sources: the seam between "where transcripts live" and "what we
//! do with them".
//!
//! Ingestion was originally Claude Code all the way down — discovery walked
//! `~/.claude/projects`, the parser assumed Claude's JSONL, and `metrics.rs`
//! carried a second copy of the same directory walk. Adding Codex, Cursor or
//! Gemini to that shape would have meant a fourth and fifth copy.
//!
//! A [`SessionSource`] owns the two things that genuinely differ per agent:
//! **where** its transcripts live and **how** to turn one into [`Turn`]s.
//! Everything downstream — memory extraction, token posting, title
//! derivation — works on `Turn` and does not care which agent produced it.
//!
//! Claude Code is the only implementation today; it is a straight move of the
//! previous behaviour, not a rewrite.
//!
//! ## Adding a source
//!
//! Implement the trait and add the source to [`all_sources`]. Two shapes are
//! already known not to fit perfectly:
//!
//! - **Cursor** keeps conversations in a SQLite db, not one file per session,
//!   so its [`SessionRef::path`] will point at the db and `native_id` will
//!   carry the composer id that selects a row. The trait is deliberately
//!   `parse(&self, &SessionRef)` rather than `parse(&Path)` so that works.
//! - **Gemini CLI** nests sub-agent sessions inside a per-session directory,
//!   so its `discover_all` will emit more refs than there are top-level files.

use crate::ingest::Turn;
use serde_json::Value;
use std::path::{Path, PathBuf};

/// One importable session, as located by a [`SessionSource`].
#[derive(Debug, Clone)]
pub struct SessionRef {
    /// Human-readable project grouping, e.g. `Project/CTXone`. Used for
    /// per-project reporting in `ctx ingest-session --all`.
    pub label: String,
    /// What to read. Usually the transcript file; for db-backed sources it is
    /// the database, with `native_id` selecting the conversation.
    pub path: PathBuf,
    /// The agent's own id for this session, when it is known before parsing.
    /// `None` means the caller derives one (Claude Code uses the file stem).
    pub native_id: Option<String>,
    /// Absolute working directory the session ran in, when the source records
    /// one. This is what routes a session to its workspace — it is resolved
    /// against the project registry, unlike [`label`](Self::label), which is
    /// derived from Claude Code's hashed directory name and cannot tell a
    /// literal dash from a path separator.
    ///
    /// `None` for sources that do not record it; such sessions fall back to
    /// the default namespace rather than being guessed at.
    pub cwd: Option<String>,
    /// A fingerprint computed cheaply at discovery, when the source already
    /// had the data in hand. Db-backed sources set this so the incremental
    /// skip does not re-open the database once per session — the Cursor store
    /// is a single multi-GB file, and re-opening it per conversation cost more
    /// than the entire rest of a sync. `None` means "compute it the normal way"
    /// (see [`SessionSource::fingerprint`]).
    pub precomputed_fp: Option<String>,
}

impl SessionRef {
    /// The session id to store, falling back to the file stem.
    ///
    /// Ids are only unique *within* a source — Codex uses a uuid, Cursor a
    /// composer id — so callers that mix sources must namespace this.
    pub fn session_id(&self) -> String {
        self.native_id.clone().unwrap_or_else(|| {
            self.path
                .file_stem()
                .map(|s| s.to_string_lossy().to_string())
                .unwrap_or_default()
        })
    }

    /// The stored session id, namespaced by source so two agents cannot
    /// collide on the same underlying id.
    ///
    /// Claude Code is deliberately **not** prefixed: its ids are already in
    /// the wild, keyed into every existing session row, memory tag and
    /// `/sessions/{id}/turns` path. Prefixing it would orphan all of that and
    /// re-import every session as a duplicate. New sources pay the prefix;
    /// the incumbent keeps its ids.
    pub fn namespaced_id(&self, source_id: &str) -> String {
        let id = self.session_id();
        if source_id == "claude" {
            id
        } else {
            format!("{}:{}", source_id, id)
        }
    }
}

/// Files under `dir` and its immediate subdirectories whose file name matches
/// `pred`. Bounded to two levels so a `--scan-dir` can't kick off a deep
/// filesystem walk — it covers both "a folder of transcripts" and "a projects
/// root with one subdirectory per project".
fn scan_files(dir: &Path, pred: impl Fn(&str) -> bool) -> Vec<PathBuf> {
    let mut out = vec![];
    let mut collect = |d: &Path| {
        if let Ok(rd) = std::fs::read_dir(d) {
            for entry in rd.flatten() {
                let p = entry.path();
                if p.is_file()
                    && let Some(name) = p.file_name().and_then(|n| n.to_str())
                    && pred(name)
                {
                    out.push(p.clone());
                }
            }
        }
    };
    collect(dir);
    if let Ok(rd) = std::fs::read_dir(dir) {
        for entry in rd.flatten() {
            let p = entry.path();
            if p.is_dir() {
                collect(&p);
            }
        }
    }
    out
}

/// An agent whose sessions we can import.
pub trait SessionSource {
    /// Stable machine id used by `--source` and stored on the session.
    fn id(&self) -> &'static str;

    /// Human label shown in Lens (the `source` field on a session).
    fn label(&self) -> &'static str;

    /// Whether this agent leaves anything on this machine. Callers use it to
    /// skip sources the user does not have installed rather than reporting
    /// them as empty.
    fn is_available(&self) -> bool;

    /// Every session this source can see, grouped by project label.
    fn discover_all(&self) -> Vec<SessionRef>;

    /// Sessions belonging to one project directory. Sources with no notion of
    /// a per-project transcript may return an empty vec.
    fn discover_for_project(&self, project_dir: &Path) -> Vec<SessionRef>;

    /// Read one session into source-neutral turns. Returns an empty vec on
    /// unreadable or unparseable input — a bad transcript must not abort a
    /// whole-machine scan.
    fn parse(&self, session: &SessionRef) -> Vec<Turn>;

    /// A cheap change-detector for incremental sync: if this equals the value
    /// stored on the session's last ingest, the transcript is unchanged and
    /// the whole session can be skipped without parsing or writing it.
    ///
    /// Default is the file's `mtime:size` — no parsing, catches any edit, and
    /// works for the file-per-session sources. Db-backed sources (Cursor)
    /// MUST override: every conversation shares one db file, so `mtime:size`
    /// would be identical across them and skip all but the first.
    ///
    /// `None` disables the skip for that session (it re-ingests every time),
    /// which is the safe default when no fingerprint can be computed.
    fn fingerprint(&self, session: &SessionRef) -> Option<String> {
        // A fingerprint captured at discovery wins — it is why db-backed
        // sources need not override this method or re-open their store.
        if let Some(fp) = &session.precomputed_fp {
            return Some(fp.clone());
        }
        let m = std::fs::metadata(&session.path).ok()?;
        let mtime = m
            .modified()
            .ok()?
            .duration_since(std::time::UNIX_EPOCH)
            .ok()?
            .as_secs();
        Some(format!("{}:{}", mtime, m.len()))
    }

    /// Approximate epoch-seconds of the session's last activity, computed
    /// WITHOUT parsing — used by `--since` to skip whole old sessions cheaply.
    ///
    /// Default is the transcript file's mtime. `None` means "unknown", and the
    /// caller then imports the session rather than risk skipping it. Db-backed
    /// sources (Cursor) MUST override — every conversation shares one file, so
    /// its mtime says nothing about an individual chat.
    fn last_activity(&self, session: &SessionRef) -> Option<i64> {
        let m = std::fs::metadata(&session.path).ok()?;
        let secs = m
            .modified()
            .ok()?
            .duration_since(std::time::UNIX_EPOCH)
            .ok()?
            .as_secs();
        Some(secs as i64)
    }

    /// Discover sessions under a user-specified directory (from `--scan-dir`),
    /// probing it against this source's on-disk shape. A directory that is not
    /// this source's format yields an empty vec, so every source can be asked
    /// about every extra dir and only the matching one responds.
    ///
    /// Default is empty (the source has no notion of a relocatable store).
    fn discover_in(&self, _dir: &Path) -> Vec<SessionRef> {
        Vec::new()
    }
}

// ── Claude Code ───────────────────────────────────────────────────────────────

/// Claude Code: one JSONL file per session under
/// `~/.claude/projects/<path-with-slashes-replaced-by-dashes>/`.
pub struct ClaudeCode;

impl ClaudeCode {
    fn projects_dir() -> PathBuf {
        dirs::home_dir()
            .unwrap_or_else(|| PathBuf::from("~"))
            .join(".claude")
            .join("projects")
    }

    /// Recover a readable label from Claude Code's hashed directory name.
    ///
    /// The hash is `project_path.replace('/', "-")`, so an absolute path leads
    /// with '-'. We take the last two components, which is enough to tell
    /// projects apart without printing the whole path.
    fn label_for(hash: &str) -> String {
        let Some(stripped) = hash.strip_prefix('-') else {
            return hash.to_string();
        };
        let parts: Vec<&str> = stripped.split('-').collect();
        if parts.len() >= 2 {
            format!("{}/{}", parts[parts.len() - 2], parts[parts.len() - 1])
        } else {
            hash.to_string()
        }
    }

    /// Read `cwd` from the head of a transcript without parsing all of it.
    ///
    /// Every Claude Code entry carries top-level `cwd` and `gitBranch`; the
    /// first one that has it is enough to route the session to a workspace.
    /// Whole-machine scans touch every transcript and these reach tens of MB,
    /// so this stops at the first hit — the same reasoning as Codex's
    /// [`read_meta`](Codex::read_meta).
    ///
    /// Early lines are occasionally summary/meta records without `cwd`, hence
    /// scanning a few rather than trusting line 1.
    fn read_cwd(path: &Path) -> Option<String> {
        use std::io::{BufRead, BufReader};
        let f = std::fs::File::open(path).ok()?;
        for line in BufReader::new(f).lines().take(20).map_while(Result::ok) {
            let Ok(v) = serde_json::from_str::<Value>(&line) else {
                continue;
            };
            if let Some(cwd) = v.get("cwd").and_then(|x| x.as_str()) {
                if !cwd.is_empty() {
                    return Some(cwd.to_string());
                }
            }
        }
        None
    }

    /// `.jsonl` files in `dir`, oldest first by mtime.
    fn jsonl_files(dir: &Path) -> Vec<PathBuf> {
        let mut files: Vec<PathBuf> = std::fs::read_dir(dir)
            .into_iter()
            .flatten()
            .flatten()
            .map(|e| e.path())
            .filter(|p| p.extension().map(|e| e == "jsonl").unwrap_or(false))
            .collect();
        files.sort_by_key(|p| p.metadata().and_then(|m| m.modified()).ok());
        files
    }
}

impl SessionSource for ClaudeCode {
    fn id(&self) -> &'static str {
        "claude"
    }

    fn label(&self) -> &'static str {
        "Claude Code"
    }

    fn is_available(&self) -> bool {
        Self::projects_dir().is_dir()
    }

    fn discover_all(&self) -> Vec<SessionRef> {
        let Ok(entries) = std::fs::read_dir(Self::projects_dir()) else {
            return vec![];
        };

        // Collect per project first so projects stay sorted by label and files
        // stay oldest-first within a project — callers print per-project counts.
        let mut per_project: Vec<(String, Vec<PathBuf>)> = entries
            .flatten()
            .map(|e| e.path())
            .filter(|p| p.is_dir())
            .filter_map(|path| {
                let hash = path.file_name()?.to_string_lossy().to_string();
                let files = Self::jsonl_files(&path);
                (!files.is_empty()).then(|| (Self::label_for(&hash), files))
            })
            .collect();
        per_project.sort_by(|a, b| a.0.cmp(&b.0));

        per_project
            .into_iter()
            .flat_map(|(label, files)| {
                files.into_iter().map(move |path| SessionRef {
                    label: label.clone(),
                    cwd: Self::read_cwd(&path),
                    path,
                    native_id: None, // file stem is the session id
                    precomputed_fp: None,
                })
            })
            .collect()
    }

    fn discover_for_project(&self, project_dir: &Path) -> Vec<SessionRef> {
        let hash = project_dir.to_string_lossy().replace('/', "-");
        let dir = Self::projects_dir().join(&hash);
        if !dir.exists() {
            return vec![];
        }
        let label = Self::label_for(&hash);
        // The caller already named the directory, so prefer it over re-reading
        // the transcript; fall back to the file for the odd session whose cwd
        // differs from the directory it was filed under.
        let dir_cwd = project_dir.to_string_lossy().to_string();
        Self::jsonl_files(&dir)
            .into_iter()
            .map(|path| SessionRef {
                label: label.clone(),
                cwd: Self::read_cwd(&path).or_else(|| Some(dir_cwd.clone())),
                path,
                native_id: None,
                precomputed_fp: None,
            })
            .collect()
    }

    /// A `--scan-dir` of Claude transcripts: `*.jsonl` files that are not
    /// Codex rollouts (those are the Codex source's to claim). The cwd is read
    /// from each transcript; the label is the containing directory's name.
    fn discover_in(&self, dir: &Path) -> Vec<SessionRef> {
        scan_files(dir, |n| n.ends_with(".jsonl") && !n.starts_with("rollout-"))
            .into_iter()
            .map(|path| {
                let label = path
                    .parent()
                    .and_then(|p| p.file_name())
                    .map(|n| n.to_string_lossy().to_string())
                    .unwrap_or_else(|| "scan".to_string());
                SessionRef {
                    cwd: Self::read_cwd(&path),
                    label,
                    path,
                    native_id: None,
                    precomputed_fp: None,
                }
            })
            .collect()
    }

    fn parse(&self, session: &SessionRef) -> Vec<Turn> {
        crate::ingest::parse_turns(&session.path)
    }
}

// ── Codex ─────────────────────────────────────────────────────────────────────

/// Codex CLI / Desktop: one "rollout" JSONL per session under
/// `~/.codex/sessions/` (live) and `~/.codex/archived_sessions/`.
///
/// Every line is `{timestamp, type, payload}`. The types that matter:
///
/// - `session_meta` — one per file, first line: session id, `cwd`,
///   `originator`, `cli_version`, `model_provider`.
/// - `response_item` — the conversation. `payload.type` is `message`
///   (with `role`), `function_call`, `function_call_output`, `reasoning`, …
/// - `event_msg` with `payload.type == "token_count"` — usage. Carries both
///   `total_token_usage` (cumulative for the session) and `last_token_usage`
///   (this turn). We read the latter and sum, so a truncated file still
///   yields the usage of the turns it does contain.
pub struct Codex;

impl Codex {
    fn roots() -> Vec<PathBuf> {
        let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("~"));
        let codex = home.join(".codex");
        vec![codex.join("sessions"), codex.join("archived_sessions")]
    }

    /// `rollout-*.jsonl` anywhere under `dir` — Codex nests live sessions in
    /// dated subdirectories, so this recurses rather than reading one level.
    fn rollout_files(dir: &Path) -> Vec<PathBuf> {
        let mut out = vec![];
        let mut stack = vec![dir.to_path_buf()];
        while let Some(d) = stack.pop() {
            let Ok(entries) = std::fs::read_dir(&d) else {
                continue;
            };
            for e in entries.flatten() {
                let p = e.path();
                if p.is_dir() {
                    stack.push(p);
                } else if p.extension().map(|x| x == "jsonl").unwrap_or(false)
                    && p.file_name()
                        .map(|n| n.to_string_lossy().starts_with("rollout-"))
                        .unwrap_or(false)
                {
                    out.push(p);
                }
            }
        }
        out.sort_by_key(|p| p.metadata().and_then(|m| m.modified()).ok());
        out
    }

    /// Read the `session_meta` line without parsing the whole file.
    ///
    /// Discovery runs over every rollout on the machine, and these files reach
    /// tens of MB — reading only the first lines keeps a scan cheap. The meta
    /// record is the first line in practice; we scan a few in case that
    /// changes rather than assuming position.
    fn read_meta(path: &Path) -> Option<(String, String)> {
        use std::io::{BufRead, BufReader};
        let f = std::fs::File::open(path).ok()?;
        for line in BufReader::new(f).lines().take(10).map_while(Result::ok) {
            let v: Value = match serde_json::from_str(&line) {
                Ok(v) => v,
                Err(_) => continue,
            };
            if v.get("type").and_then(|t| t.as_str()) != Some("session_meta") {
                continue;
            }
            let p = v.get("payload")?;
            let id = p
                .get("id")
                .and_then(|x| x.as_str())
                .unwrap_or("")
                .to_string();
            let cwd = p
                .get("cwd")
                .and_then(|x| x.as_str())
                .unwrap_or("")
                .to_string();
            return Some((id, cwd));
        }
        None
    }

    /// `/Users/user/Apps/Thing` -> `Apps/Thing`, matching how the Claude Code
    /// source labels projects so mixed listings read consistently.
    fn label_for_cwd(cwd: &str) -> String {
        let parts: Vec<&str> = cwd
            .trim_end_matches('/')
            .split('/')
            .filter(|s| !s.is_empty())
            .collect();
        match parts.len() {
            0 => "unknown".to_string(),
            1 => parts[0].to_string(),
            n => format!("{}/{}", parts[n - 2], parts[n - 1]),
        }
    }
}

impl SessionSource for Codex {
    fn id(&self) -> &'static str {
        "codex"
    }

    fn label(&self) -> &'static str {
        "Codex"
    }

    fn is_available(&self) -> bool {
        Self::roots().iter().any(|r| r.is_dir())
    }

    fn discover_all(&self) -> Vec<SessionRef> {
        let mut refs: Vec<SessionRef> = Self::roots()
            .iter()
            .filter(|r| r.is_dir())
            .flat_map(|r| Self::rollout_files(r))
            .map(|path| {
                let (id, cwd) = Self::read_meta(&path).unwrap_or_default();
                SessionRef {
                    label: if cwd.is_empty() {
                        "unknown".to_string()
                    } else {
                        Self::label_for_cwd(&cwd)
                    },
                    cwd: (!cwd.is_empty()).then_some(cwd),
                    // Fall back to the filename's uuid when meta is missing;
                    // session_id() handles the None case via the file stem.
                    native_id: (!id.is_empty()).then_some(id),
                    path,
                    precomputed_fp: None,
                }
            })
            .collect();
        // Group projects together and keep them label-sorted, matching Claude.
        refs.sort_by(|a, b| a.label.cmp(&b.label));
        refs
    }

    fn discover_for_project(&self, project_dir: &Path) -> Vec<SessionRef> {
        let want = project_dir.to_string_lossy().to_string();
        let want = want.trim_end_matches('/').to_string();
        self.discover_all()
            .into_iter()
            .filter(|r| {
                // Match on the recorded cwd, not the label, which is lossy.
                // `discover_all` already read it, so this no longer re-opens
                // every rollout on the machine to answer one project.
                r.cwd
                    .as_deref()
                    .map(|c| c.trim_end_matches('/') == want)
                    .unwrap_or(false)
            })
            .collect()
    }

    /// A `--scan-dir` of Codex rollouts: `rollout-*.jsonl` files, each mapped
    /// through the same `read_meta` path as the built-in scan.
    fn discover_in(&self, dir: &Path) -> Vec<SessionRef> {
        let mut refs: Vec<SessionRef> =
            scan_files(dir, |n| n.starts_with("rollout-") && n.ends_with(".jsonl"))
                .into_iter()
                .map(|path| {
                    let (id, cwd) = Self::read_meta(&path).unwrap_or_default();
                    SessionRef {
                        label: if cwd.is_empty() {
                            "unknown".to_string()
                        } else {
                            Self::label_for_cwd(&cwd)
                        },
                        cwd: (!cwd.is_empty()).then_some(cwd),
                        native_id: (!id.is_empty()).then_some(id),
                        path,
                        precomputed_fp: None,
                    }
                })
                .collect();
        refs.sort_by(|a, b| a.label.cmp(&b.label));
        refs
    }

    fn parse(&self, session: &SessionRef) -> Vec<Turn> {
        crate::codex::parse_rollout(&session.path)
    }
}

// ── Gemini CLI ──────────────────────────────────────────────────────────────

/// Gemini CLI: one JSON file per session under
/// `~/.gemini/tmp/<project>/chats/session-*.json`. The working directory is
/// not on the messages, so it is recovered from `~/.gemini/projects.json`,
/// which maps an absolute path to the `<project>` folder name.
pub struct Gemini;

impl Gemini {
    fn gemini_dir() -> PathBuf {
        dirs::home_dir()
            .unwrap_or_else(|| PathBuf::from("~"))
            .join(".gemini")
    }

    /// `<project-folder-name>` → absolute cwd, inverted from projects.json
    /// (`{ "projects": { "/abs/path": "folder-name" } }`).
    fn project_dirs() -> std::collections::HashMap<String, String> {
        let mut out = std::collections::HashMap::new();
        let path = Self::gemini_dir().join("projects.json");
        if let Ok(text) = std::fs::read_to_string(&path)
            && let Ok(v) = serde_json::from_str::<Value>(&text)
            && let Some(map) = v.get("projects").and_then(|p| p.as_object())
        {
            for (abs, name) in map {
                if let Some(name) = name.as_str() {
                    out.insert(name.to_string(), abs.clone());
                }
            }
        }
        out
    }

    /// `session-*.json` files under `tmp/<project>/chats/`, with their project
    /// folder name (the parent of `chats`).
    fn session_files() -> Vec<(String, PathBuf)> {
        let tmp = Self::gemini_dir().join("tmp");
        let Ok(projects) = std::fs::read_dir(&tmp) else {
            return vec![];
        };
        let mut out = vec![];
        for proj in projects.flatten() {
            let name = proj.file_name().to_string_lossy().to_string();
            let chats = proj.path().join("chats");
            let Ok(files) = std::fs::read_dir(&chats) else {
                continue;
            };
            for f in files.flatten() {
                let p = f.path();
                let is_session = p
                    .file_name()
                    .map(|n| {
                        let n = n.to_string_lossy();
                        n.starts_with("session-") && n.ends_with(".json")
                    })
                    .unwrap_or(false);
                if is_session {
                    out.push((name.clone(), p));
                }
            }
        }
        out.sort_by(|a, b| {
            a.1.metadata()
                .and_then(|m| m.modified())
                .ok()
                .cmp(&b.1.metadata().and_then(|m| m.modified()).ok())
        });
        out
    }

    /// The session id from the file's `sessionId`, without parsing the whole
    /// (multi-MB) transcript — read the head and pull the first field.
    fn read_session_id(path: &Path) -> Option<String> {
        use std::io::Read;
        let mut f = std::fs::File::open(path).ok()?;
        let mut buf = vec![0u8; 4096];
        let n = f.read(&mut buf).ok()?;
        let head = String::from_utf8_lossy(&buf[..n]);
        // "sessionId":"<uuid>"
        let key = "\"sessionId\"";
        let i = head.find(key)? + key.len();
        let rest = &head[i..];
        let start = rest.find('"')? + 1;
        let end = rest[start..].find('"')? + start;
        Some(rest[start..end].to_string())
    }
}

impl SessionSource for Gemini {
    fn id(&self) -> &'static str {
        "gemini"
    }

    fn label(&self) -> &'static str {
        "Gemini"
    }

    fn is_available(&self) -> bool {
        Self::gemini_dir().join("tmp").is_dir()
    }

    fn discover_all(&self) -> Vec<SessionRef> {
        let dirs = Self::project_dirs();
        let mut refs: Vec<SessionRef> = Self::session_files()
            .into_iter()
            .map(|(project, path)| {
                let cwd = dirs.get(&project).cloned();
                SessionRef {
                    label: cwd
                        .as_deref()
                        .map(Codex::label_for_cwd)
                        .unwrap_or_else(|| project.clone()),
                    cwd,
                    native_id: Self::read_session_id(&path),
                    path,
                    precomputed_fp: None,
                }
            })
            .collect();
        refs.sort_by(|a, b| a.label.cmp(&b.label));
        refs
    }

    fn discover_for_project(&self, project_dir: &Path) -> Vec<SessionRef> {
        let want = project_dir
            .to_string_lossy()
            .trim_end_matches('/')
            .to_string();
        self.discover_all()
            .into_iter()
            .filter(|r| {
                r.cwd
                    .as_deref()
                    .map(|c| c.trim_end_matches('/') == want)
                    .unwrap_or(false)
            })
            .collect()
    }

    /// A `--scan-dir` of Gemini transcripts: `session-*.json` files. The cwd is
    /// not recorded in the file, so these route to the default workspace unless
    /// the dir sits under a normal Gemini layout (best-effort label only).
    fn discover_in(&self, dir: &Path) -> Vec<SessionRef> {
        let mut refs: Vec<SessionRef> =
            scan_files(dir, |n| n.starts_with("session-") && n.ends_with(".json"))
                .into_iter()
                .map(|path| {
                    let label = path
                        .parent()
                        .and_then(|p| p.file_name())
                        .map(|n| n.to_string_lossy().to_string())
                        .unwrap_or_else(|| "gemini".to_string());
                    SessionRef {
                        native_id: Self::read_session_id(&path),
                        label,
                        cwd: None,
                        path,
                        precomputed_fp: None,
                    }
                })
                .collect();
        refs.sort_by(|a, b| a.label.cmp(&b.label));
        refs
    }

    fn parse(&self, session: &SessionRef) -> Vec<Turn> {
        let mut turns = crate::gemini::parse_session(&session.path);
        // Stamp the resolved cwd onto every turn so provenance and workspace
        // routing work exactly as for the file-per-session sources.
        if let Some(cwd) = &session.cwd {
            for t in &mut turns {
                if t.cwd.is_none() {
                    t.cwd = Some(cwd.clone());
                }
            }
        }
        turns
    }
}

// ── Cursor ────────────────────────────────────────────────────────────────────

/// Cursor: chats live in a SQLite db, one row per message, not one file per
/// session. The [`SessionRef`] `path` is the db and `native_id` is the
/// composer id that selects the conversation within it.
///
/// The working directory is not in the global chat db, so Cursor sessions
/// currently route to the default workspace.
pub struct Cursor;

impl Cursor {
    /// The global chat store. Per-workspace dbs also exist but the global one
    /// holds the composer/bubble chat data.
    fn global_db() -> PathBuf {
        dirs::home_dir()
            .unwrap_or_else(|| PathBuf::from("~"))
            .join("Library/Application Support/Cursor/User/globalStorage/state.vscdb")
    }

    /// Map every composer in a `state.vscdb` to a [`SessionRef`]. Shared by the
    /// built-in scan and `--scan-dir`. `list_sessions` reads `lastUpdatedAt` in
    /// its one index-only pass, so the incremental-skip fingerprint is captured
    /// here for free rather than re-opening the multi-GB store per conversation.
    fn sessions_in_db(db: &Path) -> Vec<SessionRef> {
        crate::cursor::list_sessions(db)
            .into_iter()
            .map(|s| SessionRef {
                label: s.name.clone().unwrap_or_else(|| "Cursor".to_string()),
                cwd: None, // not in the global db
                native_id: Some(s.composer_id),
                path: db.to_path_buf(),
                precomputed_fp: Some(format!("cursor:{}", s.last_updated_at)),
            })
            .collect()
    }
}

impl SessionSource for Cursor {
    fn id(&self) -> &'static str {
        "cursor"
    }

    fn label(&self) -> &'static str {
        "Cursor"
    }

    fn is_available(&self) -> bool {
        Self::global_db().is_file()
    }

    fn discover_all(&self) -> Vec<SessionRef> {
        Self::sessions_in_db(&Self::global_db())
    }

    fn discover_for_project(&self, _project_dir: &Path) -> Vec<SessionRef> {
        // No per-project cwd mapping yet, so a project-scoped scan can't tell
        // which Cursor sessions belong to it. Return none rather than guess.
        vec![]
    }

    /// A `--scan-dir` pointing at a `state.vscdb` file, or a directory holding
    /// one, is scanned like the global store. Anything else yields nothing.
    fn discover_in(&self, dir: &Path) -> Vec<SessionRef> {
        let db = if dir.is_file() {
            dir.to_path_buf()
        } else {
            dir.join("state.vscdb")
        };
        if db.is_file() {
            Self::sessions_in_db(&db)
        } else {
            vec![]
        }
    }

    fn parse(&self, session: &SessionRef) -> Vec<Turn> {
        let Some(id) = &session.native_id else {
            return vec![];
        };
        crate::cursor::parse_session(&session.path, id)
    }

    // No `fingerprint` override: `discover_all` captured `lastUpdatedAt` per
    // composer in its single scan and stored it in `precomputed_fp`, so the
    // trait default returns it without re-opening the database.

    /// Cursor's per-chat `lastUpdatedAt`, carried on `precomputed_fp` from the
    /// discovery scan (`cursor:<epoch-ms>`). A missing or zero timestamp is
    /// `None`, so `--since` imports rather than wrongly skips it. The db file's
    /// own mtime is useless here — every chat shares it.
    fn last_activity(&self, session: &SessionRef) -> Option<i64> {
        let ms: i64 = session
            .precomputed_fp
            .as_ref()?
            .strip_prefix("cursor:")?
            .parse()
            .ok()?;
        (ms > 0).then_some(ms / 1000)
    }
}

// ── Registry ──────────────────────────────────────────────────────────────────

/// Every known source, in the order they should be scanned.
// ── Generic (import) ──────────────────────────────────────────────────────────

/// A neutral JSON transcript format for importing sessions from clients with no
/// local artifacts (e.g. server-side tools like Copilot). Import with
/// `ctx ingest-session --source generic --file <transcript.json>`; extraction +
/// analysis then run source-agnostically. No auto-discovery — import only.
///
/// Format:
/// ```json
/// { "turns": [
///     {"user": "...", "assistant": "...", "timestamp": "2026-..", "model": "..",
///      "tool_calls": ["..."]} ] }
/// ```
pub struct Generic;

#[derive(serde::Deserialize)]
struct GenericTranscript {
    #[serde(default)]
    turns: Vec<GenericTurn>,
}

#[derive(serde::Deserialize)]
struct GenericTurn {
    #[serde(default)]
    user: String,
    #[serde(default)]
    assistant: String,
    #[serde(default)]
    timestamp: String,
    #[serde(default)]
    model: String,
    #[serde(default)]
    tool_calls: Vec<String>,
}

impl SessionSource for Generic {
    fn id(&self) -> &'static str {
        "generic"
    }
    fn label(&self) -> &'static str {
        "Imported"
    }
    fn is_available(&self) -> bool {
        // A format, not an install — always available for `--file` import.
        true
    }
    fn discover_all(&self) -> Vec<SessionRef> {
        vec![] // import-only; nothing to auto-discover
    }
    fn discover_for_project(&self, _project_dir: &Path) -> Vec<SessionRef> {
        vec![]
    }
    /// A `--scan-dir` of `*.json` generic transcripts.
    fn discover_in(&self, dir: &Path) -> Vec<SessionRef> {
        scan_files(dir, |n| n.ends_with(".json"))
            .into_iter()
            .map(|path| SessionRef {
                label: "Imported".to_string(),
                path,
                native_id: None,
                cwd: None,
                precomputed_fp: None,
            })
            .collect()
    }
    fn parse(&self, session: &SessionRef) -> Vec<Turn> {
        let Ok(text) = std::fs::read_to_string(&session.path) else {
            return vec![];
        };
        let Ok(t) = serde_json::from_str::<GenericTranscript>(&text) else {
            eprintln!(
                "  warn: {} is not a valid generic transcript",
                session.path.display()
            );
            return vec![];
        };
        t.turns
            .into_iter()
            .map(|g| Turn {
                user_text: g.user,
                assistant_text: g.assistant,
                tool_calls: g.tool_calls,
                tool_calls_raw: vec![],
                tokens: crate::ingest::TurnTokens::default(),
                model: g.model,
                timestamp: g.timestamp,
                cwd: None,
                git_branches: vec![],
                git_commit: None,
            })
            .collect()
    }
}

pub fn all_sources() -> Vec<Box<dyn SessionSource>> {
    vec![
        Box::new(ClaudeCode),
        Box::new(Codex),
        Box::new(Gemini),
        Box::new(Cursor),
        Box::new(Generic),
    ]
}

/// Look up a source by its `--source` id.
pub fn source_by_id(id: &str) -> Option<Box<dyn SessionSource>> {
    all_sources().into_iter().find(|s| s.id() == id)
}

/// Detect which source owns a single transcript `path` and return it alongside
/// a fully-resolved [`SessionRef`] (native id + cwd), or `None` if no source
/// recognises it.
///
/// This is the seam the Stop-hook capture path uses: a hook hands us one
/// transcript file (Claude `<uuid>.jsonl`, Codex `rollout-*.jsonl`, Gemini
/// `session-*.json`, …) and we must parse it with the right adapter instead of
/// assuming Claude JSONL. Each source's filename predicates are mutually
/// exclusive (Claude's `discover_in` excludes `rollout-`, Codex requires it,
/// Gemini uses `session-*.json`), so the first match is unambiguous. We reuse
/// `discover_in(parent)` so the source's own metadata reader fills `native_id`
/// and `cwd` — the fields that route the session to its workspace.
pub fn source_ref_for_path(path: &Path) -> Option<(Box<dyn SessionSource>, SessionRef)> {
    let dir = path.parent()?;
    let want = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
    for src in all_sources() {
        let hit = src
            .discover_in(dir)
            .into_iter()
            .find(|r| std::fs::canonicalize(&r.path).unwrap_or_else(|_| r.path.clone()) == want);
        if let Some(r) = hit {
            return Some((src, r));
        }
    }
    None
}

/// Group refs by project label, preserving discovery order within a group.
///
/// Discovery returns a flat list because that is the useful shape for
/// importing; the CLI wants `(label, files)` pairs for its per-project
/// reporting, which this restores without a second directory walk.
pub fn group_by_label(refs: Vec<SessionRef>) -> Vec<(String, Vec<PathBuf>)> {
    let mut out: Vec<(String, Vec<PathBuf>)> = vec![];
    for r in refs {
        match out.last_mut() {
            Some((label, files)) if *label == r.label => files.push(r.path),
            _ => out.push((r.label.clone(), vec![r.path])),
        }
    }
    out
}

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

    #[test]
    fn generic_source_parses_neutral_transcript() {
        let dir = std::env::temp_dir().join(format!("ctx-gen-{}", std::process::id()));
        let _ = std::fs::create_dir_all(&dir);
        let p = dir.join("s.json");
        std::fs::write(
            &p,
            r#"{"turns":[
                {"user":"do X","assistant":"did X with enough text to be substantial here","model":"gpt-5","tool_calls":["Edit: a.rs"]},
                {"user":"do Y","assistant":"did Y"}
            ]}"#,
        )
        .unwrap();
        let sref = SessionRef {
            label: "Imported".into(),
            path: p,
            native_id: None,
            cwd: None,
            precomputed_fp: None,
        };
        let turns = Generic.parse(&sref);
        assert_eq!(turns.len(), 2);
        assert_eq!(turns[0].user_text, "do X");
        assert_eq!(turns[0].model, "gpt-5");
        assert_eq!(turns[0].tool_calls, vec!["Edit: a.rs".to_string()]);
        assert_eq!(turns[1].assistant_text, "did Y");
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn source_ref_for_path_routes_codex_and_claude_by_filename() {
        // A dir holding one Codex rollout and one Claude transcript. Detection
        // is by filename predicate, so empty files are enough — this is the
        // seam the Stop-hook capture path relies on to not run a Codex rollout
        // through the Claude parser.
        let dir = std::env::temp_dir().join(format!("ctx-src-test-{}", std::process::id()));
        let _ = std::fs::create_dir_all(&dir);
        let codex =
            dir.join("rollout-2026-06-15T17-37-13-019ecda5-753a-7e31-87d2-a78b2cb024e8.jsonl");
        let claude = dir.join("abc-123.jsonl");
        std::fs::write(&codex, "").unwrap();
        std::fs::write(&claude, "").unwrap();

        let (src, _) = source_ref_for_path(&codex).expect("codex rollout should be detected");
        assert_eq!(src.id(), "codex");
        let (src, _) = source_ref_for_path(&claude).expect("claude transcript should be detected");
        assert_eq!(src.id(), "claude");

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn label_recovers_last_two_components() {
        assert_eq!(
            ClaudeCode::label_for("-Users-user-Documents-Project-CTXone"),
            "Project/CTXone"
        );
    }

    #[test]
    fn label_passes_through_unhashed_names() {
        assert_eq!(ClaudeCode::label_for("plain"), "plain");
    }

    #[test]
    fn session_id_falls_back_to_file_stem() {
        let r = SessionRef {
            label: "x/y".into(),
            cwd: None,
            path: PathBuf::from("/tmp/abc-123.jsonl"),
            native_id: None,
            precomputed_fp: None,
        };
        assert_eq!(r.session_id(), "abc-123");
    }

    #[test]
    fn session_id_prefers_native_id() {
        let r = SessionRef {
            label: "x/y".into(),
            cwd: None,
            path: PathBuf::from("/tmp/state.vscdb"),
            native_id: Some("composer-7".into()),
            precomputed_fp: None,
        };
        assert_eq!(r.session_id(), "composer-7");
    }

    #[test]
    fn claude_ids_are_not_namespaced_so_existing_rows_keep_working() {
        let r = SessionRef {
            label: "a/b".into(),
            cwd: None,
            path: PathBuf::from("/x/9117346d.jsonl"),
            native_id: None,
            precomputed_fp: None,
        };
        assert_eq!(r.namespaced_id("claude"), "9117346d");
    }

    #[test]
    fn other_sources_are_namespaced_to_avoid_collisions() {
        let r = SessionRef {
            label: "a/b".into(),
            cwd: None,
            path: PathBuf::from("/x/rollout-abc.jsonl"),
            native_id: Some("019e5540".into()),
            precomputed_fp: None,
        };
        assert_eq!(r.namespaced_id("codex"), "codex:019e5540");
    }

    #[test]
    fn codex_label_matches_the_claude_two_component_style() {
        assert_eq!(Codex::label_for_cwd("/Users/user/Apps/Thing"), "Apps/Thing");
        assert_eq!(Codex::label_for_cwd("/solo"), "solo");
        assert_eq!(Codex::label_for_cwd(""), "unknown");
    }

    #[test]
    fn group_by_label_keeps_projects_together() {
        let mk = |label: &str, p: &str| SessionRef {
            label: label.into(),
            cwd: None,
            path: PathBuf::from(p),
            native_id: None,
            precomputed_fp: None,
        };
        let grouped = group_by_label(vec![
            mk("a/b", "/1.jsonl"),
            mk("a/b", "/2.jsonl"),
            mk("c/d", "/3.jsonl"),
        ]);
        assert_eq!(grouped.len(), 2);
        assert_eq!(grouped[0].1.len(), 2);
        assert_eq!(grouped[1].0, "c/d");
    }
}