csift 0.12.3

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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! `plan --audit`'s four BINDING facts, each with a fixture where it fires and one where it
//! does not: the slug's change points, whether the bound file is on disk, plan text held with
//! no binding, and the first slug-carrying record against the plan file's birth instant.

use crate::harness::*;

const ENC: &str = "-Users-dev-example-project";
const SESSION: &str = "11111111-2222-4333-8444-555555555555";

/// A user record carrying a top-level `slug` - the binding key, as the harness stamps it.
fn slugged(uuid: &str, ts: &str, slug: &str, text: &str) -> String {
    format!(
        r#"{{"type":"user","uuid":"{uuid}","timestamp":"{ts}","slug":"{slug}","message":{{"role":"user","content":"{text}"}}}}"#
    )
}

/// The same record BEFORE the slug is minted: no such key.
fn unslugged(uuid: &str, ts: &str, text: &str) -> String {
    format!(
        r#"{{"type":"user","uuid":"{uuid}","timestamp":"{ts}","message":{{"role":"user","content":"{text}"}}}}"#
    )
}

/// The Plan-Mode entry attachment: the authoritative binding.
fn plan_mode(uuid: &str, ts: &str, slug: &str, plan_file: &str) -> String {
    let escaped = plan_file.replace('\\', "\\\\");
    format!(
        r#"{{"type":"attachment","uuid":"{uuid}","timestamp":"{ts}","slug":"{slug}","attachment":{{"type":"plan_mode","isSubAgent":false,"planExists":false,"planFilePath":"{escaped}"}}}}"#
    )
}

/// The post-compaction re-injection of the BOUND plan's whole content.
fn plan_reference(uuid: &str, ts: &str, plan_file: &str) -> String {
    let escaped = plan_file.replace('\\', "\\\\");
    format!(
        r##"{{"type":"attachment","uuid":"{uuid}","timestamp":"{ts}","attachment":{{"type":"plan_file_reference","planFilePath":"{escaped}","content":"# the plan"}}}}"##
    )
}

fn rows(out: &Output, kind: &str) -> Vec<serde_json::Value> {
    out.stdout
        .lines()
        .filter_map(|l| serde_json::from_str::<serde_json::Value>(l).ok())
        .filter(|v| v["kind"] == kind)
        .collect()
}

fn one(out: &Output, kind: &str) -> serde_json::Value {
    let r = rows(out, kind);
    assert_eq!(r.len(), 1, "expected exactly one {kind} row: {r:?}");
    r.into_iter().next().unwrap()
}

// ── (a) the slug's change points ──────────────────────────────────────────────

#[test]
fn the_slug_mint_point_is_reported_as_a_change_from_none() {
    // The measured corpus shape: a run of unslugged records, then the mint, then one stable
    // value - exactly one change point, whose `from` is absent.
    let h = Home::new();
    let plan = h.write_claude("plans/quiet-harbor-relay.md", "# the plan\n");
    h.write(
        &format!("{ENC}/{SESSION}.jsonl"),
        &format!(
            "{}\n{}\n{}\n{}\n",
            unslugged("u1", "2020-06-07T05:00:00.000Z", "before the mint"),
            slugged(
                "u2",
                "2020-06-07T05:01:00.000Z",
                "quiet-harbor-relay",
                "after"
            ),
            plan_mode(
                "a1",
                "2020-06-07T05:02:00.000Z",
                "quiet-harbor-relay",
                plan.to_str().unwrap()
            ),
            slugged(
                "u3",
                "2020-06-07T05:03:00.000Z",
                "quiet-harbor-relay",
                "more"
            ),
        ),
    );

    let out = h.run(&["plan", "--audit", &format!("@{SESSION}")]);
    assert!(out.success, "stderr: {}", out.stderr);
    assert!(
        out.stdout
            .contains("slug     L2  none -> quiet-harbor-relay  (the slug is minted here)"),
        "the mint is the change point and is labelled as one:\n{}",
        out.stdout
    );

    let b = one(
        &h.run(&[
            "plan",
            "--audit",
            &format!("@{SESSION}"),
            "--format",
            "json",
        ]),
        "binding",
    );
    assert_eq!(
        b["slug_changes"],
        serde_json::json!([{"line": 2, "from": null, "to": "quiet-harbor-relay"}]),
        "{b}"
    );
    assert_eq!(b["first_slug_line"], 2, "{b}");
}

#[test]
fn a_slug_that_moves_is_reported_as_a_second_change_point() {
    // The shape the check exists for and that the corpus does not contain: a binding that
    // MOVED mid-file. One change point per transition, in file order.
    let h = Home::new();
    let plan = h.write_claude("plans/second-harbor-relay.md", "# the plan\n");
    h.write(
        &format!("{ENC}/{SESSION}.jsonl"),
        &format!(
            "{}\n{}\n{}\n",
            slugged(
                "u1",
                "2020-06-07T05:00:00.000Z",
                "first-harbor-relay",
                "one"
            ),
            slugged(
                "u2",
                "2020-06-07T05:01:00.000Z",
                "second-harbor-relay",
                "two"
            ),
            plan_mode(
                "a1",
                "2020-06-07T05:02:00.000Z",
                "second-harbor-relay",
                plan.to_str().unwrap()
            ),
        ),
    );

    let out = h.run(&["plan", "--audit", &format!("@{SESSION}")]);
    assert!(
        out.stdout.contains(
            "slug     L2  first-harbor-relay -> second-harbor-relay  (the binding MOVED here)"
        ),
        "a move reads differently from a mint:\n{}",
        out.stdout
    );
    let b = one(
        &h.run(&[
            "plan",
            "--audit",
            &format!("@{SESSION}"),
            "--format",
            "json",
        ]),
        "binding",
    );
    let changes = b["slug_changes"].as_array().expect("array");
    assert_eq!(changes.len(), 2, "{b}");
    assert!(changes[0]["from"].is_null(), "{b}");
    assert_eq!(changes[1]["from"], "first-harbor-relay", "{b}");
}

#[test]
fn a_slug_nested_in_a_payload_is_not_a_change_point() {
    // The walk is depth-1, so a tool input that happens to carry the key mints nothing.
    let h = Home::new();
    let plan = h.write_claude("plans/quiet-harbor-relay.md", "# the plan\n");
    h.write(
        &format!("{ENC}/{SESSION}.jsonl"),
        &format!(
            "{}\n{}\n",
            plan_mode(
                "a1",
                "2020-06-07T05:00:00.000Z",
                "quiet-harbor-relay",
                plan.to_str().unwrap()
            ),
            r#"{"type":"assistant","uuid":"a2","timestamp":"2020-06-07T05:01:00.000Z","message":{"role":"assistant","content":[{"type":"tool_use","id":"t1","name":"Bash","input":{"slug":"not-a-binding","command":"true"}}]}}"#,
        ),
    );
    let b = one(
        &h.run(&[
            "plan",
            "--audit",
            &format!("@{SESSION}"),
            "--format",
            "json",
        ]),
        "binding",
    );
    let changes = b["slug_changes"].as_array().expect("array");
    assert_eq!(changes.len(), 1, "only the attachment's own slug: {b}");
    assert_eq!(changes[0]["to"], "quiet-harbor-relay", "{b}");
}

// ── (b) whether the bound plan file is on disk ────────────────────────────────

#[test]
fn the_binding_row_says_whether_the_bound_file_exists() {
    let h = Home::new();
    let plan = h.write_claude("plans/quiet-harbor-relay.md", "# the plan\n");
    h.write(
        &format!("{ENC}/{SESSION}.jsonl"),
        &format!(
            "{}\n",
            plan_mode(
                "a1",
                "2020-06-07T05:00:00.000Z",
                "quiet-harbor-relay",
                plan.to_str().unwrap()
            )
        ),
    );
    let out = h.run(&["plan", "--audit", &format!("@{SESSION}")]);
    assert!(out.stdout.contains("[exists]"), "{}", out.stdout);
    let b = one(
        &h.run(&[
            "plan",
            "--audit",
            &format!("@{SESSION}"),
            "--format",
            "json",
        ]),
        "binding",
    );
    assert_eq!(b["plan_exists"], true, "{b}");
}

#[test]
fn a_bound_file_that_was_never_written_reads_missing() {
    // The name is minted at Plan-Mode entry and the file lands only when content is first
    // written, so `[missing]` is an ordinary state and not a fault.
    let h = Home::new();
    let absent = h.root.join(".claude/plans/never-written-plan.md");
    h.write(
        &format!("{ENC}/{SESSION}.jsonl"),
        &format!(
            "{}\n",
            plan_mode(
                "a1",
                "2020-06-07T05:00:00.000Z",
                "never-written-plan",
                absent.to_str().unwrap()
            )
        ),
    );
    let out = h.run(&["plan", "--audit", &format!("@{SESSION}")]);
    assert!(out.stdout.contains("[missing]"), "{}", out.stdout);
    let b = one(
        &h.run(&[
            "plan",
            "--audit",
            &format!("@{SESSION}"),
            "--format",
            "json",
        ]),
        "binding",
    );
    assert_eq!(b["plan_exists"], false, "{b}");
}

// ── (c) plan text held with no binding ────────────────────────────────────────

#[test]
fn a_plan_reference_with_no_slug_anywhere_is_flagged_as_unbound_text() {
    // The fork-stripped shape: the re-injected plan content is here, but no record carries a
    // slug, so nothing binds it and nothing will re-inject it again.
    let h = Home::new();
    let plan = h.write_claude("plans/quiet-harbor-relay.md", "# the plan\n");
    h.write(
        &format!("{ENC}/{SESSION}.jsonl"),
        &format!(
            "{}\n{}\n",
            unslugged("u1", "2020-06-07T05:00:00.000Z", "carry on"),
            plan_reference("a1", "2020-06-07T05:01:00.000Z", plan.to_str().unwrap()),
        ),
    );
    let out = h.run(&["plan", "--audit", &format!("@{SESSION}")]);
    assert!(out.success, "stderr: {}", out.stderr);
    assert!(
        out.stdout.contains(
            "holds a plan_file_reference attachment (L2) while NO record carries a slug - plan \
             text without a binding"
        ),
        "the unbound-text warning names the line:\n{}",
        out.stdout
    );

    let out = h.run(&[
        "plan",
        "--audit",
        &format!("@{SESSION}"),
        "--format",
        "json",
    ]);
    let r = one(&out, "plan-unbound-text");
    assert_eq!(
        r["plan_file_reference_lines"],
        serde_json::json!([2]),
        "{r}"
    );
    let s = one(&out, "summary");
    assert_eq!(s["unbound_plan_text"], 1, "{s}");
    assert_eq!(
        s["warnings"], 1,
        "the unbound text counts as a warning: {s}"
    );
}

#[test]
fn a_plan_reference_beside_a_slug_is_the_ordinary_bound_case() {
    // Same attachment, one slug present: nothing to warn about.
    let h = Home::new();
    let plan = h.write_claude("plans/quiet-harbor-relay.md", "# the plan\n");
    h.write(
        &format!("{ENC}/{SESSION}.jsonl"),
        &format!(
            "{}\n{}\n",
            slugged("u1", "2020-06-07T05:00:00.000Z", "quiet-harbor-relay", "hi"),
            plan_reference("a1", "2020-06-07T05:01:00.000Z", plan.to_str().unwrap()),
        ),
    );
    let out = h.run(&[
        "plan",
        "--audit",
        &format!("@{SESSION}"),
        "--format",
        "json",
    ]);
    assert!(
        rows(&out, "plan-unbound-text").is_empty(),
        "a slug is present, so nothing is unbound:\n{}",
        out.stdout
    );
    assert_eq!(one(&out, "summary")["unbound_plan_text"], 0);
}

#[test]
fn a_prose_mention_of_the_attachment_type_is_not_a_plan_reference() {
    // The check reads the attachment's own `type`, never the byte match that admitted the
    // line - the same rule the `plan_mode` binding follows.
    let h = Home::new();
    h.write(
        &format!("{ENC}/{SESSION}.jsonl"),
        &format!(
            "{}\n",
            unslugged(
                "u1",
                "2020-06-07T05:00:00.000Z",
                "what does plan_file_reference do"
            )
        ),
    );
    let out = h.run(&[
        "plan",
        "--audit",
        &format!("@{SESSION}"),
        "--format",
        "json",
    ]);
    assert!(
        rows(&out, "plan-unbound-text").is_empty(),
        "prose is not an attachment:\n{}",
        out.stdout
    );
}

// ── (d) the slug against the plan file's birth instant ────────────────────────

/// Whether this host records a file birth time. A musl target does not (`created()` errors
/// there), so the comparison can read nothing but `unknown` with that reason - the answer the
/// fact documents for such a platform, and the arm these tests assert there.
fn host_records_birth_time(plan_file: &std::path::Path) -> bool {
    std::fs::metadata(plan_file)
        .and_then(|m| m.created())
        .is_ok()
}

#[test]
fn a_slug_older_than_the_plan_file_reads_before() {
    // The ordinary order: the name is minted at Plan-Mode entry, the file lands later. The
    // fixture's 2020 timestamps are necessarily older than a file written by this test.
    let h = Home::new();
    let plan = h.write_claude("plans/quiet-harbor-relay.md", "# the plan\n");
    h.write(
        &format!("{ENC}/{SESSION}.jsonl"),
        &format!(
            "{}\n",
            plan_mode(
                "a1",
                "2020-06-07T05:00:00.000Z",
                "quiet-harbor-relay",
                plan.to_str().unwrap()
            )
        ),
    );
    let out = h.run(&["plan", "--audit", &format!("@{SESSION}")]);
    let b = one(
        &h.run(&[
            "plan",
            "--audit",
            &format!("@{SESSION}"),
            "--format",
            "json",
        ]),
        "binding",
    );
    if host_records_birth_time(&plan) {
        assert!(
            out.stdout.contains(
                "birth    the first slug-carrying record is before the plan file's birth"
            ),
            "{}",
            out.stdout
        );
        assert_eq!(b["slug_vs_plan_file"], "before", "{b}");
        assert!(b["slug_vs_plan_file_reason"].is_null(), "{b}");
    } else {
        assert!(
            out.stdout
                .contains("birth    unknown - this platform records no file birth time"),
            "{}",
            out.stdout
        );
        assert_eq!(b["slug_vs_plan_file"], "unknown", "{b}");
        assert_eq!(
            b["slug_vs_plan_file_reason"], "this platform records no file birth time",
            "{b}"
        );
    }
}

#[test]
fn a_slug_newer_than_the_plan_file_reads_after() {
    // The inherited-plan order: this transcript bound a file that already existed. A 2099
    // timestamp is necessarily newer than a file written by this test.
    let h = Home::new();
    let plan = h.write_claude("plans/quiet-harbor-relay.md", "# the plan\n");
    h.write(
        &format!("{ENC}/{SESSION}.jsonl"),
        &format!(
            "{}\n",
            plan_mode(
                "a1",
                "2099-06-07T05:00:00.000Z",
                "quiet-harbor-relay",
                plan.to_str().unwrap()
            )
        ),
    );
    let b = one(
        &h.run(&[
            "plan",
            "--audit",
            &format!("@{SESSION}"),
            "--format",
            "json",
        ]),
        "binding",
    );
    if host_records_birth_time(&plan) {
        assert_eq!(b["slug_vs_plan_file"], "after", "{b}");
    } else {
        assert_eq!(b["slug_vs_plan_file"], "unknown", "{b}");
        assert_eq!(
            b["slug_vs_plan_file_reason"], "this platform records no file birth time",
            "{b}"
        );
    }
}

#[test]
fn an_absent_plan_file_makes_the_comparison_unknown_with_its_reason() {
    // Unknown is a real answer and it names why, rather than picking a direction.
    let h = Home::new();
    let absent = h.root.join(".claude/plans/never-written-plan.md");
    h.write(
        &format!("{ENC}/{SESSION}.jsonl"),
        &format!(
            "{}\n",
            plan_mode(
                "a1",
                "2020-06-07T05:00:00.000Z",
                "never-written-plan",
                absent.to_str().unwrap()
            )
        ),
    );
    let out = h.run(&["plan", "--audit", &format!("@{SESSION}")]);
    assert!(
        out.stdout
            .contains("birth    unknown - the plan file is not on disk"),
        "{}",
        out.stdout
    );
    let b = one(
        &h.run(&[
            "plan",
            "--audit",
            &format!("@{SESSION}"),
            "--format",
            "json",
        ]),
        "binding",
    );
    assert_eq!(b["slug_vs_plan_file"], "unknown", "{b}");
    assert_eq!(
        b["slug_vs_plan_file_reason"], "the plan file is not on disk",
        "{b}"
    );
}

#[test]
fn a_binding_whose_records_carry_no_slug_says_so_instead_of_inventing_one() {
    // An older binding record predates the slug field. The binding still stands; the three
    // slug-derived facts report their own absence.
    let h = Home::new();
    let plan = h.write_claude("plans/quiet-harbor-relay.md", "# the plan\n");
    // A hand-built record: the path is JSON-escaped like the fixture helpers do, or a Windows
    // path's backslashes make the line unparseable and the binding vanishes with it.
    let escaped = plan.to_str().unwrap().replace('\\', "\\\\");
    h.write(
        &format!("{ENC}/{SESSION}.jsonl"),
        &format!(
            "{{\"type\":\"attachment\",\"uuid\":\"a1\",\"timestamp\":\"2020-06-07T05:00:00.000Z\",\"attachment\":{{\"type\":\"plan_mode\",\"isSubAgent\":false,\"planExists\":true,\"planFilePath\":\"{escaped}\"}}}}\n"
        ),
    );
    let out = h.run(&["plan", "--audit", &format!("@{SESSION}")]);
    assert!(
        out.stdout
            .contains("slug     none carried by this transcript's records"),
        "{}",
        out.stdout
    );
    assert!(
        out.stdout
            .contains("birth    unknown - no slug-carrying record has a timestamp"),
        "{}",
        out.stdout
    );
    let b = one(
        &h.run(&[
            "plan",
            "--audit",
            &format!("@{SESSION}"),
            "--format",
            "json",
        ]),
        "binding",
    );
    assert_eq!(b["slug_changes"], serde_json::json!([]), "{b}");
    assert!(b["first_slug_line"].is_null(), "{b}");
    assert_eq!(b["slug_vs_plan_file"], "unknown", "{b}");
}