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
//! Timeline items, span math, footers, and the Replay report impl.

use super::*;

/// A timeline item for `--patches` ordering (segments + boundaries interleaved).
#[derive(Debug)]
pub(crate) enum TimelineItem {
    Seg(Segment),
    Bound(Boundary),
}

impl TimelineItem {
    pub(crate) fn sort_key(&self) -> usize {
        match self {
            TimelineItem::Seg(s) => s.line_no_start,
            TimelineItem::Bound(b) => b.line_no,
        }
    }
}

/// Dense line vector of a buffer (gaps omitted) restricted to `--file-lines`.
pub(crate) fn filter_lines(
    buf: &SparseBuffer,
    line_range: Option<crate::text::RangeSpec>,
) -> Vec<String> {
    apply_line_range(buf.known_lines(), line_range)
        .into_iter()
        .map(|(_, t)| t)
        .collect()
}

/// The contiguous spans of a `(line_no, text)` vector, as inclusive `(lo, hi)`.
pub(crate) fn covered_spans(lines: &[(usize, String)]) -> Vec<(usize, usize)> {
    let mut spans: Vec<(usize, usize)> = Vec::new();
    for (n, _) in lines {
        match spans.last_mut() {
            Some(last) if last.1 + 1 == *n => last.1 = *n,
            _ => spans.push((*n, *n)),
        }
    }
    spans
}

pub(crate) fn fmt_spans(spans: &[(usize, usize)]) -> String {
    if spans.is_empty() {
        return "(none)".to_string();
    }
    spans
        .iter()
        .map(|(a, b)| format!("[{a}..{b}]"))
        .collect::<Vec<_>>()
        .join(" ")
}

pub(crate) fn fmt_counts(c: &EventCounts) -> String {
    let mut parts: Vec<String> = Vec::new();
    let reads = c.read_full + c.read_windowed;
    if reads > 0 {
        parts.push(format!(
            "{reads} read ({} full, {} windowed)",
            c.read_full, c.read_windowed
        ));
    }
    if c.edit > 0 {
        let unanch = if c.edit_unanchorable > 0 {
            format!(" ({} un-anchorable)", c.edit_unanchorable)
        } else {
            String::new()
        };
        parts.push(format!("{} edit{unanch}", c.edit));
    }
    if c.write > 0 {
        parts.push(format!("{} write", c.write));
    }
    if c.bash > 0 {
        parts.push(format!("{} bash (heuristic)", c.bash));
    }
    if c.bash_read_anchor > 0 {
        parts.push(format!("{} bash-read-anchor", c.bash_read_anchor));
    }
    if c.bash_write_anchor > 0 {
        parts.push(format!("{} bash-write-anchor", c.bash_write_anchor));
    }
    if c.external_edit > 0 {
        parts.push(format!("{} external-edit", c.external_edit));
    }
    if c.history_snapshot > 0 {
        parts.push(format!("{} history-snapshot", c.history_snapshot));
    }
    if c.integrity_error > 0 {
        parts.push(format!("{} integrity-error", c.integrity_error));
    }
    if c.stale_hint > 0 {
        parts.push(format!("{} modified-file-hint", c.stale_hint));
    }
    if c.stale_recovered > 0 {
        parts.push(format!("{} stale-recovered", c.stale_recovered));
    }
    if parts.is_empty() {
        "0".to_string()
    } else {
        parts.join(" · ")
    }
}

pub(crate) fn print_footer(ctx: &RenderCtx) {
    let mode = match ctx.mode {
        RecoverMode::Patches => "patches",
        RecoverMode::At => "at",
        RecoverMode::Salvage => "salvage",
        RecoverMode::Coverage => "coverage",
        RecoverMode::Restore => "restore", // never reached - render_restore returns before the footer
    };
    println!();
    println!(
        "mode={mode}  (reconstruction is partial: unknown lines are explicit, never fabricated)"
    );
    if ctx.skipped_lines > 0 {
        println!("({})", crate::text::malformed_note(ctx.skipped_lines));
    }
}

impl Replay {
    /// Boundaries that INVALIDATED reconstruction state (content across them cannot be
    /// stitched): a modified-since-read (the replayed buffer is cleared), an external
    /// edit (disk changed outside the tool stream), and a hint_modified (Claude Code's
    /// own modified-file attribution). An originalFile disagreement and a
    /// stale_recovered are authoritative ANNOTATIONS (nothing is invalidated), and a
    /// bash mutation is a soft heuristic split; none of those belong in the hard
    /// count, so the coverage `fragments` figure never inflates on soft signals.
    pub(crate) fn boundaries_hard_count(&self) -> usize {
        self.boundaries
            .iter()
            .filter(|b| {
                matches!(
                    b.kind,
                    "modified_since_read" | "external_edit" | "hint_modified" | "external_write"
                )
            })
            .count()
    }

    /// The complement of [`Replay::boundaries_hard_count`]: annotation + heuristic
    /// boundaries (originalFile disagreement, stale_recovered, bash mutation).
    pub(crate) fn boundaries_soft_count(&self) -> usize {
        self.boundaries.len() - self.boundaries_hard_count()
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// JSON rendering (NDJSON - one object per line, trailing summary)
// ─────────────────────────────────────────────────────────────────────────────

pub(crate) fn render_json(
    ctx: &RenderCtx,
    sessions: &[ScanResult],
    out_path: Option<&Path>,
) -> Result<()> {
    use serde_json::json;
    if matches!(ctx.mode, RecoverMode::Restore) {
        return render_restore(ctx, sessions, out_path, true);
    }
    let mut session_count = 0usize;

    // envelope v2: header (always) → kind-tagged rows → summary (always).
    println!(
        "{}",
        serde_json::to_string(&crate::text::envelope_scope_header(
            "recover",
            ctx.scope_top,
            ctx.scope_sub,
            json!({})
        ))?
    );

    match ctx.mode {
        RecoverMode::Restore => unreachable!("Restore handled above in render_json"),
        RecoverMode::Coverage => {
            for s in sessions {
                if s.events.is_empty() {
                    continue;
                }
                session_count += 1;
                let rep = replay(&s.events, None);
                let known = apply_line_range(rep.final_buffer.known_lines(), ctx.line_range);
                let spans = covered_spans(&known);
                let d = window_disclosure(s, ctx.file.as_deref());
                let obj = json!({
                    "kind": "coverage",
                    "session_id": s.session_id,
                    "is_subagent": s.is_subagent,
                    "parent_session_id": s.parent_session_id,
                    "file": ctx.file,
                    "recoverable_lines": known.len(),
                    "seen_total_lines": rep.final_buffer.seen_total_lines,
                    "covered_ranges": spans.iter().map(|(a,b)| [*a,*b]).collect::<Vec<_>>(),
                    "fragments": rep.boundaries_hard_count() + 1,
                    "hard_boundaries": rep.boundaries_hard_count(),
                    "soft_boundaries": rep.boundaries_soft_count(),
                    "events": counts_json(&rep.counts),
                    "boundaries": rep.boundaries.iter().map(|b| boundary_json_sourced(s, b)).collect::<Vec<_>>(),
                    "opaque_commands": d.opaque_classes,
                    "powershell_commands": d.powershell,
                    "suggested_search": d.suggested,
                });
                println!("{}", serde_json::to_string(&obj)?);
            }
        }
        RecoverMode::Patches => {
            let mut out_blob = String::new();
            for s in sessions {
                if s.events.is_empty() {
                    continue;
                }
                let rep = replay(&s.events, None);
                if rep.segments.is_empty() && rep.boundaries.is_empty() {
                    continue;
                }
                session_count += 1;
                let mut items: Vec<TimelineItem> = Vec::new();
                for seg in &rep.segments {
                    items.push(TimelineItem::Seg(seg.clone()));
                }
                for b in &rep.boundaries {
                    items.push(TimelineItem::Bound(b.clone()));
                }
                items.sort_by_key(TimelineItem::sort_key);
                for item in &items {
                    match item {
                        TimelineItem::Seg(seg) => {
                            let old = filter_lines(&seg.start_buffer, ctx.line_range);
                            let new = filter_lines(&seg.end_buffer, ctx.line_range);
                            let diff = unified_diff(&old, &new, usize::MAX);
                            out_blob.push_str(&diff);
                            let obj = json!({
                                "kind": "segment",
                                "session_id": s.session_id,
                                "is_subagent": s.is_subagent,
                                "parent_session_id": s.parent_session_id,
                                "segment_index": seg.index,
                                "line": seg.line_no_start,
                                "line_start": seg.line_no_start,
                                "line_end": seg.line_no_end,
                                "turn_start": seg.turn_start,
                                "turn_end": seg.turn_end,
                                "ts_utc": seg.ts_start,
                                "ts_local": seg.ts_start.as_deref().and_then(local_iso),
                                "pre_state_known": seg.pre_state_known,
                                "anchor_source": seg.anchor_source.map(SnapSource::label),
                                "unified_diff": diff,
                            });
                            println!("{}", serde_json::to_string(&obj)?);
                        }
                        TimelineItem::Bound(b) => {
                            let mut obj = boundary_json_sourced(s, b);
                            obj["kind"] = json!("boundary");
                            obj["session_id"] = json!(s.session_id);
                            obj["is_subagent"] = json!(s.is_subagent);
                            obj["parent_session_id"] = json!(s.parent_session_id);
                            println!("{}", serde_json::to_string(&obj)?);
                        }
                    }
                }
            }
            if let Some(p) = out_path {
                write_out_guarded(p, &out_blob)?;
            }
        }
        RecoverMode::At | RecoverMode::Salvage => {
            // Salvage feeds an empty `when` (ctx.at is None) → @latest (no cutoff).
            let when = ctx.at.as_deref().unwrap_or("");
            let mut out_blob = String::new();
            for s in sessions {
                if s.events.is_empty() {
                    continue;
                }
                let cutoff = resolve_cutoff(when, &s.events)?;
                let rep = replay(&s.events, cutoff);
                let known = apply_line_range(rep.final_buffer.known_lines(), ctx.line_range);
                if known.is_empty() && rep.final_buffer.seen_total_lines.is_none() {
                    continue;
                }
                session_count += 1;
                let total = rep.final_buffer.seen_total_lines;
                let gaps = gap_ranges(&known, total.unwrap_or(0));
                // Provenance: which jsonl line last set each known file line.
                let prov: BTreeMap<usize, usize> = rep
                    .final_buffer
                    .known_lines_with_provenance()
                    .into_iter()
                    .map(|(n, _, set_at)| (n, set_at))
                    .collect();
                let d = window_disclosure(s, ctx.file.as_deref());
                let obj = json!({
                    "kind": "snapshot",
                    "session_id": s.session_id,
                    "is_subagent": s.is_subagent,
                    "parent_session_id": s.parent_session_id,
                    "file": ctx.file,
                    // The jsonl-line CUTOFF this snapshot reflects (`--at` resolved).
                    "line": cutoff,
                    "lines": known.iter().map(|(n,t)| json!({
                        "n": n,
                        "text": t,
                        "set_at_line": prov.get(n),
                    })).collect::<Vec<_>>(),
                    "gaps": gaps.iter().map(|(a,b)| [*a,*b]).collect::<Vec<_>>(),
                    "seen_total_lines": total,
                    "boundaries": rep.boundaries.iter().map(|b| boundary_json_sourced(s, b)).collect::<Vec<_>>(),
                    "opaque_commands": d.opaque_classes,
                    "powershell_commands": d.powershell,
                    "suggested_search": d.suggested,
                });
                println!("{}", serde_json::to_string(&obj)?);
                out_blob.push_str(&render_snapshot_body(&known, total.unwrap_or(0), false));
                out_blob.push('\n');
            }
            if let Some(p) = out_path {
                write_out_guarded(p, &out_blob)?;
            }
        }
    }

    // envelope v2 summary (flat - the old nested {"summary":{…}} wrapper is gone).
    let summary = crate::text::envelope_summary(json!({
        "sessions": session_count,
        "file": ctx.file,
        "mode": match ctx.mode {
            RecoverMode::Patches => "patches",
            RecoverMode::At => "at",
            RecoverMode::Salvage => "salvage",
            RecoverMode::Coverage => "coverage",
            RecoverMode::Restore => "restore",
        },
        "skipped_lines": ctx.skipped_lines,
    }));
    println!("{}", serde_json::to_string(&summary)?);
    Ok(())
}

pub(crate) fn counts_json(c: &EventCounts) -> serde_json::Value {
    serde_json::json!({
        "read_full": c.read_full,
        "read_windowed": c.read_windowed,
        "edit": c.edit,
        "edit_unanchorable": c.edit_unanchorable,
        "write": c.write,
        "bash": c.bash,
        "bash_read_anchor": c.bash_read_anchor,
        "bash_write_anchor": c.bash_write_anchor,
        "external_edit": c.external_edit,
        "history_snapshot": c.history_snapshot,
        "integrity_error": c.integrity_error,
        "stale_hint": c.stale_hint,
        "stale_recovered": c.stale_recovered,
    })
}

pub(crate) fn boundary_json(b: &Boundary) -> serde_json::Value {
    serde_json::json!({
        "line": b.line_no,
        "turn_index": b.turn_index,
        "ts_utc": b.timestamp_utc,
        "ts_local": b.timestamp_utc.as_deref().and_then(local_iso),
        // WHAT invalidated the buffer (modified-since-read / external edit / bash / …) -
        // named `cause` so `kind` stays the envelope discriminator exclusively.
        "cause": b.kind,
        "confidence": b.confidence.json(),
        "detail": b.detail,
    })
}

/// [`boundary_json`] plus the boundary's REAL source location: `source_session_id` +
/// `source_line` name the transcript and its actual jsonl line. On an un-merged result
/// they mirror the row's own id and `line`; after a cross-transcript merge they carry
/// the pre-merge truth while `line` stays the `--at @line:` cutoff coordinate of the
/// merged stream.
pub(crate) fn boundary_json_sourced(s: &ScanResult, b: &Boundary) -> serde_json::Value {
    let mut obj = boundary_json(b);
    let (sid, real) = boundary_source(s, b.line_no);
    obj["source_session_id"] = serde_json::json!(sid);
    obj["source_line"] = serde_json::json!(real);
    obj["detail"] = serde_json::json!(boundary_detail_with_clue(s, b));
    obj
}

/// The explicit gap ranges of a known-line vector up to `total` (1-based, inclusive).
pub(crate) fn gap_ranges(known: &[(usize, String)], total: usize) -> Vec<(usize, usize)> {
    let mut gaps = Vec::new();
    let mut prev = 0usize;
    let last = known.last().map(|(n, _)| *n).unwrap_or(0);
    for (n, _) in known {
        if *n > prev + 1 {
            gaps.push((prev + 1, n - 1));
        }
        prev = *n;
    }
    if total > last && last > 0 {
        gaps.push((last + 1, total));
    } else if known.is_empty() && total > 0 {
        gaps.push((1, total));
    }
    gaps
}