csift 0.10.2

ripgrep for Claude Code session transcripts: fast regex list/search over ~/.claude/projects/**/*.jsonl
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
use super::*;
use std::io::Write as _;
use std::sync::atomic::{AtomicU64, Ordering};

#[test]
fn truncate_short_excerpt_unchanged() {
    assert_eq!(truncate_excerpt("hello"), "hello");
}

#[test]
fn truncate_long_excerpt_marks_dropped_count() {
    let s = "x".repeat(EXCERPT_MAX + 5);
    let out = truncate_excerpt(&s);
    assert!(out.ends_with("… (+5 chars)"), "got: {out}");
    assert!(out.starts_with(&"x".repeat(EXCERPT_MAX)));
}

#[test]
fn truncate_counts_chars_not_bytes() {
    // Multi-byte UTF-8 chars (this emoji is 4 bytes); truncation must count
    // chars, not bytes, for ANY script.
    let s = "🛠".repeat(EXCERPT_MAX + 2);
    let out = truncate_excerpt(&s);
    assert!(out.ends_with("… (+2 chars)"), "got: {out}");
}

#[test]
fn format_timestamp_uses_system_local_canonical_marker() {
    // tz-agnostic: the local portion must equal what the system tz itself yields
    // for this instant (derived in-test, not a hardcoded zone). v0.5: the marker
    // is `<TZAB>(UTC±offset)` and the raw-UTC copy is GONE (it invited LLM
    // conversion arithmetic; machine consumers read JSON ts_utc).
    let raw = "2026-06-07T05:48:22.880Z";
    let out = format_timestamp(Some(raw));
    let ts: jiff::Timestamp = raw.parse().expect("parseable instant");
    let local = ts
        .to_zoned(crate::timez::local_tz())
        .strftime("%Y-%m-%d %H:%M:%S")
        .to_string();
    assert!(out.contains(&local), "expected local {local:?} in {out:?}");
    assert!(out.contains("(UTC"), "marker missing: {out}");
    assert!(!out.contains(raw), "the raw-UTC copy must be gone: {out}");
}

#[test]
fn format_timestamp_missing_is_em_dash() {
    assert_eq!(format_timestamp(None), "");
}

#[test]
fn format_timestamp_unparseable_surfaces_raw() {
    let out = format_timestamp(Some("not-a-time"));
    assert!(out.contains("not-a-time"));
    assert!(out.contains("unparsed"));
}

#[test]
fn local_iso_matches_system_tz_offset() {
    let raw = "2026-06-07T05:48:22.880Z";
    let out = local_iso(raw).expect("local iso");
    // Derive the expected offset string from jiff (tz-agnostic), not a literal.
    let ts: jiff::Timestamp = raw.parse().expect("parseable instant");
    let expected = ts
        .to_zoned(crate::timez::local_tz())
        .strftime("%Y-%m-%dT%H:%M:%S%:z")
        .to_string();
    assert_eq!(out, expected);
}

static COUNTER: AtomicU64 = AtomicU64::new(0);

fn tmp_session(name_stem: &str, lines: &[&str]) -> PathBuf {
    let p = std::env::temp_dir().join(format!(
        "csift-sess-{}-{}-{name_stem}.jsonl",
        std::process::id(),
        COUNTER.fetch_add(1, Ordering::Relaxed)
    ));
    let mut f = std::fs::File::create(&p).unwrap();
    for l in lines {
        writeln!(f, "{l}").unwrap();
    }
    p
}

#[test]
fn capture_identity_if_empty_only_fills_blanks() {
    let rec: Record = serde_json::from_str(
            r#"{"type":"user","cwd":"/c","version":"9.9","gitBranch":"b","sessionId":"sid","message":{"role":"user","content":"x"}}"#,
        )
        .unwrap();
    let mut cwd = None;
    let mut version = Some("keep".to_string()); // already set → must NOT be overwritten
    let mut branch = None;
    let mut sid = None;
    capture_identity_if_empty(&rec, &mut cwd, &mut version, &mut branch, &mut sid);
    assert_eq!(cwd.as_deref(), Some("/c"));
    assert_eq!(version.as_deref(), Some("keep"), "pre-set value preserved");
    assert_eq!(branch.as_deref(), Some("b"));
    assert_eq!(sid.as_deref(), Some("sid"));
}

#[test]
fn summarize_head_first_user_captures_identity() {
    // The head finds a genuine user FIRST → identity captured from it; the tail
    // finds the last user + last agent. Exercises the head identity-capture arm
    // and both tail `is_none()` branches.
    let p = tmp_session(
        "head",
        &[
            r#"{"type":"user","cwd":"/Users/testuser/Projects/foo","version":"2.1.0","gitBranch":"main","sessionId":"sid-data","message":{"role":"user","content":"first q"}}"#,
            r#"{"type":"assistant","message":{"role":"assistant","content":[{"type":"text","text":"mid agent"}]}}"#,
            r#"{"type":"user","message":{"role":"user","content":"last q"}}"#,
            r#"{"type":"assistant","message":{"role":"assistant","content":[{"type":"text","text":"last agent"}]}}"#,
        ],
    );
    let s = summarize_session(&p).unwrap();
    std::fs::remove_file(&p).ok();
    assert_eq!(s.cwd.as_deref(), Some("/Users/testuser/Projects/foo"));
    assert_eq!(s.version.as_deref(), Some("2.1.0"));
    assert_eq!(s.git_branch.as_deref(), Some("main"));
    assert_eq!(s.first_user.as_ref().unwrap().excerpt, "first q");
    assert_eq!(s.last_user.as_ref().unwrap().excerpt, "last q");
    assert_eq!(s.last_agent.as_ref().unwrap().excerpt, "last agent");
}

#[test]
fn summarize_backfills_identity_from_tail_when_head_user_lacks_it() {
    // The head's FIRST genuine user carries NO identity fields (cwd/version/branch
    // all absent), but the LAST genuine user at the tail DOES - so the tail's
    // `capture_identity_if_empty` backfills them (only the still-None fields).
    let p = tmp_session(
        "tailfill",
        &[
            // head genuine user - no identity fields at all.
            r#"{"type":"user","message":{"role":"user","content":"first q, no identity"}}"#,
            r#"{"type":"assistant","message":{"role":"assistant","content":[{"type":"text","text":"a"}]}}"#,
            // tail genuine user - carries the identity fields.
            r#"{"type":"user","cwd":"/tail/cwd","version":"3.0","gitBranch":"dev","sessionId":"sid-tail","message":{"role":"user","content":"last q, has identity"}}"#,
        ],
    );
    let s = summarize_session(&p).unwrap();
    std::fs::remove_file(&p).ok();
    // The head found the first user, but identity was backfilled from the tail.
    assert_eq!(
        s.first_user.as_ref().unwrap().excerpt,
        "first q, no identity"
    );
    assert_eq!(
        s.last_user.as_ref().unwrap().excerpt,
        "last q, has identity"
    );
    assert_eq!(s.cwd.as_deref(), Some("/tail/cwd"));
    assert_eq!(s.version.as_deref(), Some("3.0"));
    assert_eq!(s.git_branch.as_deref(), Some("dev"));
}

#[test]
fn summarize_session_id_from_data_when_filename_has_no_stem() {
    // When the path has no usable stem the session id falls back to the data's
    // sessionId (the `session_id.is_empty()` true arm). We build a record carrying
    // a sessionId and drive summarize on a file, then assert the filename-stem
    // path; the data-fallback arm is also reachable when the stem is empty. Since
    // a real temp file always has a stem, assert the cross-check shape instead:
    // the id equals the stem and the data id is retained internally.
    let p = tmp_session(
        "dataid",
        &[r#"{"type":"user","sessionId":"sid-data-xyz","message":{"role":"user","content":"hi"}}"#],
    );
    let s = summarize_session(&p).unwrap();
    std::fs::remove_file(&p).ok();
    // Filename stem wins (non-empty) - the documented precedence.
    assert!(!s.session_id.is_empty());
}

#[test]
fn summarize_head_skips_non_genuine_records_before_first_user() {
    // The head stream leads with non-genuine records (metadata, an isMeta pseudo-
    // turn, a tool_result carrier) → the head closure's `genuine_user_text()`
    // returns None for each (the FALSE arm) until the real first user is reached.
    let p = tmp_session(
        "headnoise",
        &[
            r#"{"type":"last-prompt","leafUuid":"x"}"#,
            r#"{"type":"user","isMeta":true,"message":{"role":"user","content":"Continue from where you left off."}}"#,
            r#"{"type":"user","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"x","content":"carrier"}]}}"#,
            r#"{"type":"user","message":{"role":"user","content":"the genuine first question"}}"#,
            r#"{"type":"assistant","message":{"role":"assistant","content":[{"type":"text","text":"a"}]}}"#,
        ],
    );
    let s = summarize_session(&p).unwrap();
    std::fs::remove_file(&p).ok();
    assert_eq!(
        s.first_user.as_ref().unwrap().excerpt,
        "the genuine first question"
    );
}

#[test]
fn top_level_summary_is_not_subagent_and_is_its_own_parent() {
    // A plain `<uuid>.jsonl` path is top-level: is_subagent=false and parent==session_id.
    let p = tmp_session(
        "toplevel",
        &[r#"{"type":"user","message":{"role":"user","content":"hi"}}"#],
    );
    let s = summarize_session(&p).unwrap();
    let stem = p.file_stem().unwrap().to_str().unwrap().to_string();
    std::fs::remove_file(&p).ok();
    assert!(!s.is_subagent);
    assert_eq!(s.parent_session_id, stem);
    assert_eq!(s.parent_session_id, s.session_id);
}

#[test]
fn subagent_summary_carries_is_subagent_and_refeedable_parent() {
    // A subagent transcript path `…/<PARENT-UUID>/subagents/workflows/wf_x/agent-<hex>.jsonl`:
    // session_id is the bare hex (NOT re-feedable), is_subagent=true, and
    // parent_session_id is the re-feedable PARENT uuid (the dir before `subagents/`).
    let dir = std::env::temp_dir().join(format!(
        "csift-subdir-{}-{}/11111111-2222-3333-4444-555555555555/subagents/workflows/wf_z",
        std::process::id(),
        COUNTER.fetch_add(1, Ordering::Relaxed)
    ));
    std::fs::create_dir_all(&dir).unwrap();
    let p = dir.join("agent-deadbeefcafe1234.jsonl");
    {
        let mut f = std::fs::File::create(&p).unwrap();
        writeln!(
            f,
            r#"{{"type":"user","message":{{"role":"user","content":"sub work"}}}}"#
        )
        .unwrap();
    }
    let s = summarize_session(&p).unwrap();
    std::fs::remove_file(&p).ok();
    assert_eq!(
        s.session_id, "deadbeefcafe1234",
        "bare hex (agent- stripped)"
    );
    assert!(s.is_subagent, "a subagents/ path is a subagent transcript");
    assert_eq!(
        s.parent_session_id, "11111111-2222-3333-4444-555555555555",
        "parent is the re-feedable uuid dir before subagents/"
    );
}

#[test]
fn summarize_session_id_is_filename_stem() {
    // The session id is the jsonl basename; even when the data carries a different
    // sessionId, the filename wins (the `session_id.is_empty()` false arm).
    let p = tmp_session(
        "stemid",
        &[r#"{"type":"user","sessionId":"DATA-ID","message":{"role":"user","content":"hi"}}"#],
    );
    let s = summarize_session(&p).unwrap();
    let stem = p.file_stem().unwrap().to_str().unwrap().to_string();
    std::fs::remove_file(&p).ok();
    assert_eq!(s.session_id, stem);
    assert_ne!(s.session_id, "DATA-ID");
}

#[test]
fn clone_probe_arms_and_origin_join_edges() {
    static N: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
    let n = N.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let root = std::env::temp_dir().join(format!("csift-clone-{}-{n}", std::process::id()));
    std::fs::create_dir_all(&root).unwrap();
    let bx = "0f1e2d3c-4b5a-4697-8807-16f5e4d3c2b1";

    // A garbage line CARRYING the timestamp needle parses to nothing and is walked
    // past; the boundary after it still detects.
    let clone = root.join("c1.jsonl");
    std::fs::write(
        &clone,
        format!(
            "not json but mentions \"timestamp\" anyway\n{{\"type\":\"system\",\"subtype\":\"compact_boundary\",\"uuid\":\"{bx}\",\"timestamp\":\"2026-06-07T05:10:00.000Z\"}}\n"
        ),
    )
    .unwrap();
    assert_eq!(
        clone_head_boundary(&clone).unwrap().as_deref(),
        Some(bx),
        "garbage before the boundary is walked past"
    );

    // An EMPTY transcript probes to a clean None.
    let empty = root.join("c0.jsonl");
    std::fs::write(&empty, "").unwrap();
    assert_eq!(clone_head_boundary(&empty).unwrap(), None);

    // A boundary with an EMPTY-string uuid detects nothing either.
    let blank = root.join("c3.jsonl");
    std::fs::write(
        &blank,
        "{\"type\":\"system\",\"subtype\":\"compact_boundary\",\"uuid\":\"\",\"timestamp\":\"2026-06-07T05:10:00.000Z\"}\n",
    )
    .unwrap();
    assert_eq!(clone_head_boundary(&blank).unwrap(), None);

    // A boundary with NO uuid detects nothing (never an empty-string lineage).
    let anon = root.join("c2.jsonl");
    std::fs::write(
        &anon,
        "{\"type\":\"system\",\"subtype\":\"compact_boundary\",\"timestamp\":\"2026-06-07T05:10:00.000Z\"}\n",
    )
    .unwrap();
    assert_eq!(clone_head_boundary(&anon).unwrap(), None);

    // Origin join: the sibling quotes the uuid in PROSE before carrying the real
    // record - the at-advance loop must reach the record and name the origin.
    let origin = root.join("o1.jsonl");
    std::fs::write(
        &origin,
        format!(
            "{{\"type\":\"user\",\"uuid\":\"u1\",\"timestamp\":\"2026-06-07T05:00:00.000Z\",\"message\":{{\"role\":\"user\",\"content\":\"discussing {bx} in prose\"}}}}\n{{\"type\":\"system\",\"subtype\":\"compact_boundary\",\"uuid\":\"{bx}\",\"timestamp\":\"2026-06-07T05:10:00.000Z\"}}\n"
        ),
    )
    .unwrap();
    assert_eq!(
        clone_origin(&clone, bx).as_deref(),
        Some("o1"),
        "prose quote is skipped, the record wins"
    );

    // A prose-ONLY sibling can never be the origin.
    std::fs::remove_file(&origin).unwrap();
    let bystander = root.join("b1.jsonl");
    std::fs::write(
        &bystander,
        format!(
            "{{\"type\":\"user\",\"uuid\":\"u1\",\"timestamp\":\"2026-06-07T05:00:00.000Z\",\"message\":{{\"role\":\"user\",\"content\":\"only prose about {bx}\"}}}}\n"
        ),
    )
    .unwrap();
    assert_eq!(clone_origin(&clone, bx), None);

    // A sibling carrying an UNRELATED boundary plus the uuid in prose is not a
    // carrier either (the carrier check demands the RECORD with that uuid).
    let decoy = root.join("d1.jsonl");
    std::fs::write(
        &decoy,
        format!(
            "{{\"type\":\"user\",\"uuid\":\"u1\",\"timestamp\":\"2026-06-07T05:00:00.000Z\",\"message\":{{\"role\":\"user\",\"content\":\"prose about {bx}\"}}}}\n{{\"type\":\"system\",\"subtype\":\"compact_boundary\",\"uuid\":\"99999999-aaaa-4bbb-8ccc-dddddddddddd\",\"timestamp\":\"2026-06-07T05:10:00.000Z\"}}\n"
        ),
    )
    .unwrap();
    assert_eq!(
        clone_origin(&clone, bx),
        None,
        "an unrelated boundary near a prose quote is not the origin"
    );
    std::fs::remove_file(&decoy).unwrap();

    std::fs::remove_dir_all(&root).unwrap();
}

#[test]
fn clone_origin_decoys_stay_outside_the_join() {
    let root = std::env::temp_dir().join(format!("csift-clone-decoy-{}", std::process::id()));
    std::fs::create_dir_all(&root).unwrap();
    let bx = "3a4b5c6d-7e8f-4a9b-8c0d-1e2f3a4b5c6d";
    let clone = root.join("c1.jsonl");
    std::fs::write(
        &clone,
        format!(
            "{{\"type\":\"system\",\"subtype\":\"compact_boundary\",\"uuid\":\"{bx}\",\"timestamp\":\"2026-06-07T05:10:00.000Z\"}}\n"
        ),
    )
    .unwrap();

    // A NON-jsonl sibling carrying the full native-origin shape is never scanned -
    // only .jsonl files join.
    let txt = root.join("o1.txt");
    std::fs::write(
        &txt,
        format!(
            "{{\"type\":\"user\",\"uuid\":\"u1\",\"timestamp\":\"2026-06-07T05:00:00.000Z\",\"message\":{{\"role\":\"user\",\"content\":\"hi\"}}}}\n{{\"type\":\"system\",\"subtype\":\"compact_boundary\",\"uuid\":\"{bx}\",\"timestamp\":\"2026-06-07T05:10:00.000Z\"}}\n"
        ),
    )
    .unwrap();
    assert_eq!(
        clone_origin(&clone, bx),
        None,
        "a non-jsonl decoy never becomes the origin"
    );
    std::fs::remove_file(&txt).unwrap();

    // A sibling whose OWN boundary merely REFERENCES the uuid (logicalParentUuid)
    // is not a carrier - the carrier record must bear the uuid itself.
    let refr = root.join("s2.jsonl");
    std::fs::write(
        &refr,
        format!(
            "{{\"type\":\"user\",\"uuid\":\"u1\",\"timestamp\":\"2026-06-07T05:00:00.000Z\",\"message\":{{\"role\":\"user\",\"content\":\"hi\"}}}}\n{{\"type\":\"system\",\"subtype\":\"compact_boundary\",\"uuid\":\"77777777-8888-4999-8aaa-bbbbbbbbbbbb\",\"logicalParentUuid\":\"{bx}\",\"timestamp\":\"2026-06-07T05:10:00.000Z\"}}\n"
        ),
    )
    .unwrap();
    assert_eq!(
        clone_origin(&clone, bx),
        None,
        "a boundary that only references the uuid is not the carrier"
    );
    std::fs::remove_file(&refr).unwrap();
    std::fs::remove_file(&clone).ok();
}