csift 0.10.4

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
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
//! Bash content anchors: extraction gates, suppression, and replay placement.

use super::*;

#[test]
fn heredoc_write_becomes_a_content_event_and_supersedes_the_touch() {
    let records = numbered(&[
        r#"{"type":"user","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"go"}}"#,
        r#"{"type":"assistant","timestamp":"2026-06-07T05:00:01.000Z","cwd":"/work/proj","message":{"role":"assistant","content":[{"type":"tool_use","id":"b1","name":"Bash","input":{"command":"cat > notes.md <<'EOF'\nalpha\nbeta\nEOF"}}]}}"#,
        r#"{"type":"user","timestamp":"2026-06-07T05:00:02.000Z","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"b1","content":""}]}}"#,
    ]);
    let events = extract_events(&records, "/work/proj/notes.md");
    // ONE event: the content anchor; the heuristic BashTouch for the same command
    // is suppressed (never a self-inflicted boundary beside known content).
    assert_eq!(events.len(), 1, "{events:?}");
    match &events[0].kind {
        EventKind::FullSnapshot {
            content, source, ..
        } => {
            assert_eq!(content, "alpha\nbeta\n");
            assert_eq!(*source, SnapSource::BashHeredoc);
        }
        other => panic!("expected FullSnapshot, got {other:?}"),
    }
    let rep = replay(&events, None);
    assert_eq!(rep.counts.bash_write_anchor, 1);
    assert_eq!(rep.counts.bash, 0, "no boundary from the same command");
    assert_eq!(
        rep.final_buffer.known_lines(),
        vec![(1, "alpha".to_string()), (2, "beta".to_string())]
    );
}

#[test]
fn failed_bash_write_never_anchors() {
    let records = numbered(&[
        r#"{"type":"user","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"go"}}"#,
        r#"{"type":"assistant","timestamp":"2026-06-07T05:00:01.000Z","cwd":"/work/proj","message":{"role":"assistant","content":[{"type":"tool_use","id":"b1","name":"Bash","input":{"command":"cat > notes.md <<'EOF'\nalpha\nEOF"}}]}}"#,
        r#"{"type":"user","timestamp":"2026-06-07T05:00:02.000Z","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"b1","is_error":true,"content":"Permission denied"}]}}"#,
    ]);
    let events = extract_events(&records, "/work/proj/notes.md");
    // The write failed: no content anchor; the heuristic BashTouch row remains the
    // only trace (the mutation MAY still have partially landed - stay heuristic).
    assert!(
        events
            .iter()
            .all(|e| !matches!(e.kind, EventKind::FullSnapshot { .. })),
        "{events:?}"
    );
}

#[test]
fn cat_read_anchor_needs_the_clean_result_echo() {
    let mk = |result_line: &str| {
        numbered(&[
            r#"{"type":"user","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"go"}}"#,
            r#"{"type":"assistant","timestamp":"2026-06-07T05:00:01.000Z","cwd":"/work/proj","message":{"role":"assistant","content":[{"type":"tool_use","id":"b1","name":"Bash","input":{"command":"cat notes.md"}}]}}"#,
            result_line,
        ])
    };
    // Clean echo: the stdout IS the file.
    let clean = mk(
        r#"{"type":"user","timestamp":"2026-06-07T05:00:02.000Z","toolUseResult":{"stdout":"alpha\nbeta\n","stderr":"","interrupted":false},"message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"b1","content":"alpha\nbeta\n"}]}}"#,
    );
    let events = extract_events(&clean, "/work/proj/notes.md");
    assert_eq!(events.len(), 1, "{events:?}");
    assert!(
        matches!(&events[0].kind, EventKind::FullSnapshot { content, source, .. }
            if content == "alpha\nbeta\n" && *source == SnapSource::BashCat),
        "{events:?}"
    );
    // Non-empty stderr breaks the completeness gate: no anchor.
    let noisy = mk(
        r#"{"type":"user","timestamp":"2026-06-07T05:00:02.000Z","toolUseResult":{"stdout":"alpha\n","stderr":"cat: warning","interrupted":false},"message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"b1","content":"alpha\n"}]}}"#,
    );
    assert!(extract_events(&noisy, "/work/proj/notes.md").is_empty());
    // An externalized stdout is not the inline text: no anchor.
    let persisted = mk(
        r#"{"type":"user","timestamp":"2026-06-07T05:00:02.000Z","toolUseResult":{"stdout":"alpha\n","stderr":"","interrupted":false,"persistedOutputPath":"/tmp/x.txt"},"message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"b1","content":"alpha\n"}]}}"#,
    );
    assert!(extract_events(&persisted, "/work/proj/notes.md").is_empty());
    // A carrier-less (subagent-shaped) result proves nothing: no anchor.
    let bare = mk(
        r#"{"type":"user","timestamp":"2026-06-07T05:00:02.000Z","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"b1","content":"alpha\nbeta\n"}]}}"#,
    );
    assert!(extract_events(&bare, "/work/proj/notes.md").is_empty());
}

#[test]
fn sed_window_splices_and_head_to_eof_promotes_to_full() {
    let records = numbered(&[
        r#"{"type":"user","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"go"}}"#,
        r#"{"type":"assistant","timestamp":"2026-06-07T05:00:01.000Z","cwd":"/w","message":{"role":"assistant","content":[{"type":"tool_use","id":"b1","name":"Bash","input":{"command":"sed -n '3,4p' f.txt"}}]}}"#,
        r#"{"type":"user","timestamp":"2026-06-07T05:00:02.000Z","toolUseResult":{"stdout":"gamma\ndelta\n","stderr":"","interrupted":false},"message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"b1","content":"gamma\ndelta\n"}]}}"#,
    ]);
    let events = extract_events(&records, "/w/f.txt");
    assert_eq!(events.len(), 1, "{events:?}");
    assert!(
        matches!(&events[0].kind, EventKind::BashWindowRead { start_line: 3, lines }
            if lines == &vec!["gamma".to_string(), "delta".to_string()]),
        "{events:?}"
    );
    let rep = replay(&events, None);
    assert_eq!(rep.counts.bash_read_anchor, 1);
    assert_eq!(
        rep.final_buffer.known_lines(),
        vec![(3, "gamma".to_string()), (4, "delta".to_string())]
    );

    // head -n 10 returning 2 lines reached EOF from line 1: the WHOLE file.
    let head = numbered(&[
        r#"{"type":"user","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"go"}}"#,
        r#"{"type":"assistant","timestamp":"2026-06-07T05:00:01.000Z","cwd":"/w","message":{"role":"assistant","content":[{"type":"tool_use","id":"b1","name":"Bash","input":{"command":"head -n 10 f.txt"}}]}}"#,
        r#"{"type":"user","timestamp":"2026-06-07T05:00:02.000Z","toolUseResult":{"stdout":"one\ntwo\n","stderr":"","interrupted":false},"message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"b1","content":"one\ntwo\n"}]}}"#,
    ]);
    let events = extract_events(&head, "/w/f.txt");
    assert!(
        matches!(&events[0].kind, EventKind::FullSnapshot { source, .. } if *source == SnapSource::BashCat),
        "EOF-from-line-1 promotes to a full anchor: {events:?}"
    );
}

#[test]
fn append_places_on_complete_buffer_else_discloses() {
    // Complete newline-terminated buffer: the append lands at the tail.
    let events = vec![
        FileEvent {
            line_no: 1,
            turn_index: 0,
            timestamp_utc: None,
            kind: EventKind::FullSnapshot {
                content: "alpha\n".into(),
                total_lines: 1,
                source: SnapSource::Write,
            },
        },
        FileEvent {
            line_no: 2,
            turn_index: 0,
            timestamp_utc: None,
            kind: EventKind::BashAppend {
                content: "beta\n".into(),
            },
        },
    ];
    let rep = replay(&events, None);
    assert_eq!(rep.counts.bash_write_anchor, 1);
    assert!(rep.boundaries.is_empty(), "{:?}", rep.boundaries);
    assert_eq!(
        rep.final_buffer.known_lines(),
        vec![(1, "alpha".to_string()), (2, "beta".to_string())]
    );

    // An incomplete buffer (windowed knowledge only): the append point is
    // unknowable - a disclosed heuristic boundary, never a guessed placement.
    let events = vec![
        FileEvent {
            line_no: 1,
            turn_index: 0,
            timestamp_utc: None,
            kind: EventKind::BashWindowRead {
                start_line: 3,
                lines: vec!["gamma".into()],
            },
        },
        FileEvent {
            line_no: 2,
            turn_index: 0,
            timestamp_utc: None,
            kind: EventKind::BashAppend {
                content: "beta\n".into(),
            },
        },
    ];
    let rep = replay(&events, None);
    assert_eq!(rep.counts.bash_write_anchor, 0);
    assert_eq!(rep.boundaries.len(), 1);
    assert_eq!(rep.boundaries[0].kind, "bash_append_unplaced");
}

#[test]
fn compound_write_needs_the_clean_echo_and_no_same_path_second_touch() {
    let cmd = "cat > notes.md <<'EOF'\nalpha\nEOF\npython3 notes.md";
    let mk = |result: &str| {
        let use_line = format!(
            r#"{{"type":"assistant","timestamp":"2026-06-07T05:00:01.000Z","cwd":"/w","message":{{"role":"assistant","content":[{{"type":"tool_use","id":"b1","name":"Bash","input":{{"command":{cmd}}}}}]}}}}"#,
            cmd = serde_json::to_string(cmd).unwrap()
        );
        vec![
            (
                1usize,
                rec(
                    r#"{"type":"user","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"go"}}"#,
                ),
            ),
            (2usize, rec(&use_line)),
            (3usize, rec(result)),
        ]
    };
    // Clean echo: the compound-command write anchors.
    let clean = mk(
        r#"{"type":"user","timestamp":"2026-06-07T05:00:02.000Z","toolUseResult":{"stdout":"ran\n","stderr":"","interrupted":false},"message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"b1","content":"ran\n"}]}}"#,
    );
    let events = extract_events(&clean, "/w/notes.md");
    assert!(
        events
            .iter()
            .any(|e| matches!(&e.kind, EventKind::FullSnapshot { source, .. } if *source == SnapSource::BashHeredoc)),
        "{events:?}"
    );
    // Noisy stderr: the chain's exit code cannot vouch for the write - no anchor,
    // the heuristic touch row stays.
    let noisy = mk(
        r#"{"type":"user","timestamp":"2026-06-07T05:00:02.000Z","toolUseResult":{"stdout":"","stderr":"cat: cannot write","interrupted":false},"message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"b1","content":""}]}}"#,
    );
    let events = extract_events(&noisy, "/w/notes.md");
    assert!(
        events
            .iter()
            .all(|e| !matches!(e.kind, EventKind::FullSnapshot { .. })),
        "{events:?}"
    );
    assert!(
        events
            .iter()
            .any(|e| matches!(e.kind, EventKind::BashTouch { .. })),
        "the heuristic row survives when the anchor is refused: {events:?}"
    );

    // A SECOND touch of the same file later in the command kills the anchor
    // (segment order is not replayed within one line).
    let twice = "cat > notes.md <<'EOF'\nalpha\nEOF\nsed -i 's/a/b/' notes.md";
    let use_line = format!(
        r#"{{"type":"assistant","timestamp":"2026-06-07T05:00:01.000Z","cwd":"/w","message":{{"role":"assistant","content":[{{"type":"tool_use","id":"b1","name":"Bash","input":{{"command":{c}}}}}]}}}}"#,
        c = serde_json::to_string(twice).unwrap()
    );
    let records = vec![
        (
            1usize,
            rec(
                r#"{"type":"user","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"go"}}"#,
            ),
        ),
        (2usize, rec(&use_line)),
        (
            3usize,
            rec(
                r#"{"type":"user","timestamp":"2026-06-07T05:00:02.000Z","toolUseResult":{"stdout":"","stderr":"","interrupted":false},"message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"b1","content":""}]}}"#,
            ),
        ),
    ];
    let events = extract_events(&records, "/w/notes.md");
    assert!(
        events
            .iter()
            .all(|e| !matches!(e.kind, EventKind::FullSnapshot { .. })),
        "same-path second touch refuses the anchor: {events:?}"
    );
}

#[test]
fn window_gates_and_mid_file_eof() {
    let mk = |cmd: &str, stdout_json: &str| {
        let use_line = format!(
            r#"{{"type":"assistant","timestamp":"2026-06-07T05:00:01.000Z","cwd":"/w","message":{{"role":"assistant","content":[{{"type":"tool_use","id":"b1","name":"Bash","input":{{"command":{c}}}}}]}}}}"#,
            c = serde_json::to_string(cmd).unwrap()
        );
        let result = format!(
            r#"{{"type":"user","timestamp":"2026-06-07T05:00:02.000Z","toolUseResult":{{"stdout":{s},"stderr":"","interrupted":false}},"message":{{"role":"user","content":[{{"type":"tool_result","tool_use_id":"b1","content":{s}}}]}}}}"#,
            s = stdout_json
        );
        numbered(&[
            r#"{"type":"user","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"go"}}"#,
            use_line.clone().leak(),
            result.clone().leak(),
        ])
    };
    // More stdout lines than the window can print: not this command's output.
    let over = mk("sed -n '3,4p' f.txt", r#""a\nb\nc\n""#);
    assert!(extract_events(&over, "/w/f.txt").is_empty());
    // A to-EOF window from MID-file: a window splice, exact total at EOF.
    let mid = mk("sed -n '5,$p' f.txt", r#""five\nsix\n""#);
    let events = extract_events(&mid, "/w/f.txt");
    assert!(
        matches!(&events[0].kind, EventKind::BashWindowRead { start_line: 5, lines }
            if lines.len() == 2),
        "{events:?}"
    );
    // A to-EOF window from line 1 IS the whole file.
    let full = mk("sed -n '1,$p' f.txt", r#""one\n""#);
    let events = extract_events(&full, "/w/f.txt");
    assert!(
        matches!(&events[0].kind, EventKind::FullSnapshot { source, .. } if *source == SnapSource::BashCat),
        "{events:?}"
    );
    // An empty window print (past EOF) places nothing.
    let empty = mk("sed -n '9,9p' f.txt", r#""""#);
    assert!(extract_events(&empty, "/w/f.txt").is_empty());
    // A truncate write anchors an empty file.
    let trunc = mk("truncate -s 0 f.txt", r#""""#);
    let events = extract_events(&trunc, "/w/f.txt");
    assert!(
        matches!(&events[0].kind, EventKind::FullSnapshot { content, source, .. }
            if content.is_empty() && *source == SnapSource::BashWrite),
        "{events:?}"
    );
}

#[test]
fn compound_write_without_result_echo_stays_a_boundary() {
    // A compound-command write in a carrier-less (subagent-shaped) lane: the clean
    // echo cannot be proven, so no anchor - the heuristic touch survives.
    let cmd = "cat > notes.md <<'EOF'\nalpha\nEOF\npython3 notes.md";
    let use_line = format!(
        r#"{{"type":"assistant","timestamp":"2026-06-07T05:00:01.000Z","cwd":"/w","message":{{"role":"assistant","content":[{{"type":"tool_use","id":"b1","name":"Bash","input":{{"command":{c}}}}}]}}}}"#,
        c = serde_json::to_string(cmd).unwrap()
    );
    let records = numbered(&[
        r#"{"type":"user","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"go"}}"#,
        use_line.leak(),
        r#"{"type":"user","timestamp":"2026-06-07T05:00:02.000Z","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"b1","content":"ran"}]}}"#,
    ]);
    let events = extract_events(&records, "/w/notes.md");
    assert!(
        events
            .iter()
            .all(|e| !matches!(e.kind, EventKind::FullSnapshot { .. })),
        "{events:?}"
    );
    assert!(
        events
            .iter()
            .any(|e| matches!(e.kind, EventKind::BashTouch { .. })),
        "{events:?}"
    );
}

#[test]
fn no_target_and_interrupted_gates() {
    // No --file target: the anchor pass is a no-op (nothing to join against).
    let records = numbered(&[
        r#"{"type":"user","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"go"}}"#,
    ]);
    let turns =
        group_turn_indices_deduped(&records.iter().map(|(_, r)| r).collect::<Vec<_>>(), |r| *r);
    let out = collect_turn_bash_anchors(&records, &turns[0], None, &Default::default());
    assert!(out.events.is_empty() && out.suppress.is_empty());

    // An INTERRUPTED compound command cannot vouch for its write.
    let cmd = "cat > notes.md <<'EOF'\nalpha\nEOF\nsleep 60";
    let use_line = format!(
        r#"{{"type":"assistant","timestamp":"2026-06-07T05:00:01.000Z","cwd":"/w","message":{{"role":"assistant","content":[{{"type":"tool_use","id":"b1","name":"Bash","input":{{"command":{c}}}}}]}}}}"#,
        c = serde_json::to_string(cmd).unwrap()
    );
    let records = numbered(&[
        r#"{"type":"user","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"go"}}"#,
        use_line.leak(),
        r#"{"type":"user","timestamp":"2026-06-07T05:00:02.000Z","toolUseResult":{"stdout":"","stderr":"","interrupted":true},"message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"b1","content":""}]}}"#,
    ]);
    let events = extract_events(&records, "/w/notes.md");
    assert!(
        events
            .iter()
            .all(|e| !matches!(e.kind, EventKind::FullSnapshot { .. })),
        "{events:?}"
    );
}

#[test]
fn mutation_kill_pins() {
    // A same-file second touch under a DIFFERENT spelling still refuses the anchor
    // (the collision check compares RESOLVED paths, not verbatim strings).
    let twice = "cat > notes.md <<'EOF'\nalpha\nEOF\nsed -i 's/a/b/' ./notes.md";
    let use_line = format!(
        r#"{{"type":"assistant","timestamp":"2026-06-07T05:00:01.000Z","cwd":"/w","message":{{"role":"assistant","content":[{{"type":"tool_use","id":"b1","name":"Bash","input":{{"command":{c}}}}}]}}}}"#,
        c = serde_json::to_string(twice).unwrap()
    );
    let records = numbered(&[
        r#"{"type":"user","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"go"}}"#,
        use_line.leak(),
        r#"{"type":"user","timestamp":"2026-06-07T05:00:02.000Z","toolUseResult":{"stdout":"","stderr":"","interrupted":false},"message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"b1","content":""}]}}"#,
    ]);
    let events = extract_events(&records, "/w/notes.md");
    assert!(
        events
            .iter()
            .all(|e| !matches!(e.kind, EventKind::FullSnapshot { .. })),
        "a differently-spelled second touch refuses: {events:?}"
    );

    // Exact-window reads stay WINDOWS (only an EOF-short window from line 1 is the
    // whole file).
    let mk = |cmd: &str, stdout_json: &str| {
        let use_line = format!(
            r#"{{"type":"assistant","timestamp":"2026-06-07T05:00:01.000Z","cwd":"/w","message":{{"role":"assistant","content":[{{"type":"tool_use","id":"b1","name":"Bash","input":{{"command":{c}}}}}]}}}}"#,
            c = serde_json::to_string(cmd).unwrap()
        );
        let result = format!(
            r#"{{"type":"user","timestamp":"2026-06-07T05:00:02.000Z","toolUseResult":{{"stdout":{s},"stderr":"","interrupted":false}},"message":{{"role":"user","content":[{{"type":"tool_result","tool_use_id":"b1","content":{s}}}]}}}}"#,
            s = stdout_json
        );
        numbered(&[
            r#"{"type":"user","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"go"}}"#,
            use_line.clone().leak(),
            result.clone().leak(),
        ])
    };
    let head_exact = mk("head -n 2 f.txt", r#""one\ntwo\n""#);
    let events = extract_events(&head_exact, "/w/f.txt");
    assert!(
        matches!(&events[0].kind, EventKind::BashWindowRead { start_line: 1, lines } if lines.len() == 2),
        "an exact head window is not provably the whole file: {events:?}"
    );
    let sed_exact = mk("sed -n '3,5p' f.txt", r#""c\nd\ne\n""#);
    let events = extract_events(&sed_exact, "/w/f.txt");
    assert!(
        matches!(&events[0].kind, EventKind::BashWindowRead { start_line: 3, lines } if lines.len() == 3),
        "an exact sed window splices, never refuses: {events:?}"
    );
}

#[test]
fn read_anchor_counts_and_window_totals() {
    // cat-full: the read-anchor COUNT rides the replay (not just the content).
    let clean = numbered(&[
        r#"{"type":"user","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"go"}}"#,
        r#"{"type":"assistant","timestamp":"2026-06-07T05:00:01.000Z","cwd":"/w","message":{"role":"assistant","content":[{"type":"tool_use","id":"b1","name":"Bash","input":{"command":"cat f.txt"}}]}}"#,
        r#"{"type":"user","timestamp":"2026-06-07T05:00:02.000Z","toolUseResult":{"stdout":"alpha\nbeta\n","stderr":"","interrupted":false},"message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"b1","content":"alpha\nbeta\n"}]}}"#,
    ]);
    let events = extract_events(&clean, "/w/f.txt");
    let rep = replay(&events, None);
    assert_eq!(rep.counts.bash_read_anchor, 1);

    // A window splice floors seen_total to its OBSERVED extent exactly.
    let sed = numbered(&[
        r#"{"type":"user","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"go"}}"#,
        r#"{"type":"assistant","timestamp":"2026-06-07T05:00:01.000Z","cwd":"/w","message":{"role":"assistant","content":[{"type":"tool_use","id":"b1","name":"Bash","input":{"command":"sed -n '3,4p' f.txt"}}]}}"#,
        r#"{"type":"user","timestamp":"2026-06-07T05:00:02.000Z","toolUseResult":{"stdout":"gamma\ndelta\n","stderr":"","interrupted":false},"message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"b1","content":"gamma\ndelta\n"}]}}"#,
    ]);
    let events = extract_events(&sed, "/w/f.txt");
    let rep = replay(&events, None);
    assert_eq!(
        rep.final_buffer.seen_total_lines,
        Some(4),
        "extent = start 3 + 2 lines - 1"
    );
}

#[test]
fn append_placement_edges() {
    // Multi-line append content lands at consecutive positions after the base.
    let events = vec![
        FileEvent {
            line_no: 1,
            turn_index: 0,
            timestamp_utc: None,
            kind: EventKind::FullSnapshot {
                content: "alpha\n".into(),
                total_lines: 1,
                source: SnapSource::Write,
            },
        },
        FileEvent {
            line_no: 2,
            turn_index: 0,
            timestamp_utc: None,
            kind: EventKind::BashAppend {
                content: "beta\ngamma\n".into(),
            },
        },
    ];
    let rep = replay(&events, None);
    assert_eq!(
        rep.final_buffer.known_lines(),
        vec![
            (1, "alpha".to_string()),
            (2, "beta".to_string()),
            (3, "gamma".to_string())
        ]
    );
    assert_eq!(rep.final_buffer.seen_total_lines, Some(3));

    // An INCOMPLETE buffer whose newline flag is set (a full anchor followed by a
    // window splice past the end) must still refuse placement.
    let events = vec![
        FileEvent {
            line_no: 1,
            turn_index: 0,
            timestamp_utc: None,
            kind: EventKind::FullSnapshot {
                content: "alpha\n".into(),
                total_lines: 1,
                source: SnapSource::Write,
            },
        },
        FileEvent {
            line_no: 2,
            turn_index: 0,
            timestamp_utc: None,
            kind: EventKind::BashWindowRead {
                start_line: 5,
                lines: vec!["epsilon".into()],
            },
        },
        FileEvent {
            line_no: 3,
            turn_index: 0,
            timestamp_utc: None,
            kind: EventKind::BashAppend {
                content: "zeta\n".into(),
            },
        },
    ];
    let rep = replay(&events, None);
    assert_eq!(
        rep.boundaries
            .iter()
            .filter(|b| b.kind == "bash_append_unplaced")
            .count(),
        1,
        "gap between line 1 and 5: the append point is unknowable: {:?}",
        rep.boundaries
    );
}

#[test]
fn head_short_by_one_promotes_to_full() {
    // head -n 3 returning 2 lines: EOF one short of the window - the whole file.
    let records = numbered(&[
        r#"{"type":"user","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"go"}}"#,
        r#"{"type":"assistant","timestamp":"2026-06-07T05:00:01.000Z","cwd":"/w","message":{"role":"assistant","content":[{"type":"tool_use","id":"b1","name":"Bash","input":{"command":"head -n 3 f.txt"}}]}}"#,
        r#"{"type":"user","timestamp":"2026-06-07T05:00:02.000Z","toolUseResult":{"stdout":"one\ntwo\n","stderr":"","interrupted":false},"message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"b1","content":"one\ntwo\n"}]}}"#,
    ]);
    let events = extract_events(&records, "/w/f.txt");
    assert!(
        matches!(&events[0].kind, EventKind::FullSnapshot { source, .. } if *source == SnapSource::BashCat),
        "{events:?}"
    );
}