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
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
use crate::harness::*;

#[test]
fn stats_spans_subagents_by_default_and_restricts() {
    // Mutation pin on the span contract (ยง subcommand spanning default): `stats` spans the
    // session's subagent transcripts by default; `--no-subagents` restricts to the top level.
    let h = Home::new();
    subagents_only_scenario(&h);
    let span = h.run(&["stats", at(SESS).as_str()]);
    assert!(span.success, "stderr: {}", span.stderr);
    assert!(
        span.stdout.contains("sub111"),
        "stats spans subagents by default: {}",
        span.stdout
    );
    let top = h.run(&["stats", at(SESS).as_str(), "--no-subagents"]);
    assert!(top.success, "stderr: {}", top.stderr);
    assert!(
        !top.stdout.contains("sub111"),
        "--no-subagents restricts stats to the top level: {}",
        top.stdout
    );
}

#[test]
fn stats_cap_arithmetic_and_uncapped_zero() {
    // Mutation pins: dropped = len - n (NOT len / n: 4 sessions, cap 1 => exactly 3), and
    // `--max-count 0` stays UNCAPPED (the n > 0 filter, not n >= 0).
    let h = Home::new();
    for i in 0..4u8 {
        h.write(
            &format!("-Users-testuser-Projects-statcap/cccc000{i}-aaaa-4bbb-8ccc-00000000000{i}.jsonl"),
            &format!("{{\"type\":\"user\",\"uuid\":\"u0\",\"timestamp\":\"2026-06-07T0{i}:00:00.000Z\",\"message\":{{\"role\":\"user\",\"content\":\"work {i}\"}}}}\n"),
        );
    }
    let capped = h.run(&[
        "stats",
        "-Users-testuser-Projects-statcap",
        "--max-count",
        "1",
        "--format",
        "json",
    ]);
    assert!(capped.success, "stderr: {}", capped.stderr);
    assert_eq!(
        capped.stdout.matches("\"lines\":").count(),
        1,
        "one row kept:\n{}",
        capped.stdout
    );
    assert!(
        capped.stdout.contains(":3") && capped.stdout.contains("dropped"),
        "exactly 3 dropped disclosed:\n{}",
        capped.stdout
    );
    let uncapped = h.run(&[
        "stats",
        "-Users-testuser-Projects-statcap",
        "--max-count",
        "0",
        "--format",
        "json",
    ]);
    assert_eq!(
        uncapped.stdout.matches("\"lines\":").count(),
        4,
        "uncapped shows all four:\n{}",
        uncapped.stdout
    );
}

#[test]
fn stats_split_compactions_and_physical_lines_exact() {
    // Mutation pins: top = rows - sub (not +), compactions accumulate (2 exactly), and the
    // physical line count adds the torn-final-fragment ONLY when the file lacks a trailing
    // newline (3 records, no trailing newline => lines == 3).
    let enc = "-Users-testuser-Projects-statsplit";
    let sess = "dddd0000-aaaa-4bbb-8ccc-000000000001";
    let h = Home::new();
    let body = concat!(
        r#"{"type":"user","uuid":"u0","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"go"}}"#,
        "\n",
        r#"{"type":"user","uuid":"c1","isCompactSummary":true,"isVisibleInTranscriptOnly":true,"timestamp":"2026-06-07T05:01:00.000Z","message":{"role":"user","content":"summary one"}}"#,
        "\n",
        r#"{"type":"user","uuid":"c2","isCompactSummary":true,"isVisibleInTranscriptOnly":true,"timestamp":"2026-06-07T05:02:00.000Z","message":{"role":"user","content":"summary two"}}"#
    );
    h.write(&format!("{enc}/{sess}.jsonl"), body); // NO trailing newline
    h.write(
        &format!("{enc}/{sess}/subagents/agent-e0e1e2e3e4e5e6e7.jsonl"),
        "{\"type\":\"user\",\"isSidechain\":true,\"timestamp\":\"2026-06-07T05:03:00.000Z\",\"message\":{\"role\":\"user\",\"content\":\"sub\"}}\n",
    );
    let o = h.run(&["stats", enc, "--format", "json"]);
    assert!(o.success, "stderr: {}", o.stderr);
    assert!(
        o.stdout.contains("\"compactions\":2"),
        "compactions == 2:\n{}",
        o.stdout
    );
    assert!(
        o.stdout.contains("\"lines\":3"),
        "torn-fragment line count == 3:\n{}",
        o.stdout
    );
    assert!(
        o.stdout.contains("\"lines\":1"),
        "trailing-newline subagent file counts exactly 1 line:\n{}",
        o.stdout
    );
    let t = h.run(&["stats", enc]);
    assert!(
        t.stdout.contains("1 top-level") && t.stdout.contains("1 subagent"),
        "exact top/sub split:\n{}",
        t.stdout
    );
}

#[test]
fn stats_aggregates_records_turns_tools_and_tokens() {
    let h = Home::new();
    let enc = "-Users-testuser-Projects-statsy";
    let sess = "0a1b2c3d-4e5f-4a6b-8c7d-9e0f1a2b3c4d";
    h.write(
        &format!("{enc}/{sess}.jsonl"),
        concat!(
            r#"{"type":"user","uuid":"u0","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"first ask"}}"#, "\n",
            r#"{"type":"assistant","uuid":"a0","parentUuid":"u0","timestamp":"2026-06-07T05:00:05.000Z","message":{"role":"assistant","model":"claude-opus-4-8","usage":{"input_tokens":10,"output_tokens":20,"cache_read_input_tokens":100,"cache_creation_input_tokens":5},"content":[{"type":"tool_use","id":"t1","name":"Bash","input":{"command":"ls"}}]}}"#, "\n",
            r#"{"type":"user","uuid":"c0","parentUuid":"a0","timestamp":"2026-06-07T05:00:06.000Z","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"t1","content":"ok"}]}}"#, "\n",
            r#"{"type":"user","uuid":"u1","timestamp":"2026-06-07T06:00:00.000Z","message":{"role":"user","content":"second ask"}}"#, "\n",
            r#"{"type":"assistant","uuid":"a1","parentUuid":"u1","timestamp":"2026-06-07T06:00:09.000Z","message":{"role":"assistant","model":"claude-opus-4-8","usage":{"input_tokens":1,"output_tokens":2},"content":[{"type":"text","text":"done"}]}}"#, "\n",
        ),
    );
    let out = h.run(&["stats", &format!("@{sess}")]);
    assert!(out.success, "stderr: {}", out.stderr);
    assert!(out.stdout.contains("turns 2"), "{}", out.stdout);
    assert!(out.stdout.contains("Bashร—1"), "{}", out.stdout);
    assert!(
        out.stdout
            .contains("claude-opus-4-8: in 11 ยท out 22 ยท cache-read 100 ยท cache-write 5"),
        "token sums: {}",
        out.stdout
    );

    let j = h.run(&["stats", &format!("@{sess}"), "--format", "json"]);
    assert!(j.success, "stderr: {}", j.stderr);
    let row = json_rows(&j.stdout, "session").remove(0);
    assert_eq!(row["turns"], 2);
    assert_eq!(row["user_records"], 3); // 2 genuine + 1 tool_result carrier
    assert_eq!(row["assistant_records"], 2);
    assert_eq!(row["tools"]["Bash"], 1);
    assert_eq!(row["tokens"]["claude-opus-4-8"]["output"], 22);
    let sum = json_summary(&j.stdout);
    assert_eq!(sum["turns"], 2);

    // --since bounds the counted records (only the second turn's records remain).
    let win = h.run(&[
        "stats",
        &format!("@{sess}"),
        "--since",
        "2026-06-07T05:30:00Z",
        "--format",
        "json",
    ]);
    let row = json_rows(&win.stdout, "session").remove(0);
    assert_eq!(row["turns"], 1, "window admits only the later turn");
    assert_eq!(row["tokens"]["claude-opus-4-8"]["output"], 2);
}

#[test]
fn stats_turn_range_windows_the_aggregates() {
    let h = Home::new();
    let enc = "-Users-testuser-Projects-statturn";
    let sess = "0a1b2c3d-4e5f-4a6b-8c7d-9e0f1a2b3c4d";
    h.write(
        &format!("{enc}/{sess}.jsonl"),
        concat!(
            // Turn 0: one Read tool call.
            r#"{"type":"user","uuid":"u0","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"first ask"}}"#, "\n",
            r#"{"type":"assistant","uuid":"a0","parentUuid":"u0","timestamp":"2026-06-07T05:00:01.000Z","message":{"role":"assistant","content":[{"type":"tool_use","id":"t0","name":"Read","input":{}}]}}"#, "\n",
            // Turn 1: one Edit tool call.
            r#"{"type":"user","uuid":"u1","timestamp":"2026-06-07T05:01:00.000Z","message":{"role":"user","content":"second ask"}}"#, "\n",
            r#"{"type":"assistant","uuid":"a1","parentUuid":"u1","timestamp":"2026-06-07T05:01:01.000Z","message":{"role":"assistant","content":[{"type":"tool_use","id":"t1","name":"Edit","input":{}}]}}"#, "\n",
        ),
    );
    // Bare-N shorthand: turn 1 only - Edit counted, Read not, turns == 1.
    let out = h.run(&["stats", enc, "--turn", "1", "--format", "json"]);
    assert!(out.success, "stderr: {}", out.stderr);
    let rows = json_rows(&out.stdout, "session");
    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0]["turns"], 1, "one turn in window: {}", out.stdout);
    assert!(
        rows[0]["tools"].get("Edit").is_some() && rows[0]["tools"].get("Read").is_none(),
        "only turn 1's tool calls count: {}",
        out.stdout
    );
}

#[test]
fn stats_aggregates_are_exact() {
    // Mutation pin on the stats aggregation core: token sums per model, tool CALL
    // counts, the span label, and the JSON id trio must carry REAL values - an emptied
    // merge map or a += degraded to *= zeroed them with no test on the numbers.
    let h = Home::new();
    let enc = "-Users-dev-example-project";
    let sess = "8899aabb-ccdd-4000-8000-00000000000d";
    h.write(
        &format!("{enc}/{sess}.jsonl"),
        concat!(
            r#"{"type":"user","uuid":"u0","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"go"}}"#, "\n",
            r#"{"type":"assistant","uuid":"a0","timestamp":"2026-06-07T05:00:30.000Z","message":{"role":"assistant","model":"relay-model-x","usage":{"input_tokens":111,"output_tokens":44,"cache_read_input_tokens":5,"cache_creation_input_tokens":4},"content":[{"type":"tool_use","id":"w1","name":"Write","input":{"file_path":"/p/a.md","content":"x"}}]}}"#, "\n",
            r#"{"type":"user","uuid":"c0","timestamp":"2026-06-07T05:00:40.000Z","toolUseResult":{"type":"create","filePath":"/p/a.md"},"message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"w1","content":"ok"}]}}"#, "\n",
            r#"{"type":"assistant","uuid":"a1","timestamp":"2026-06-07T05:01:05.000Z","message":{"role":"assistant","model":"relay-model-x","usage":{"input_tokens":222,"output_tokens":55},"content":[{"type":"text","text":"done"}]}}"#, "\n",
        ),
    );
    // A subagent transcript so the MERGED (multi-transcript) rollup path renders too -
    // the scoped --iterate verification showed merged_tools/merged_tokens survived a
    // single-transcript fixture (the merge path was never invoked).
    h.write(
        &format!("{enc}/{sess}/subagents/agent-aggr222.jsonl"),
        concat!(
            r#"{"type":"user","isSidechain":true,"agentId":"aggr222","timestamp":"2026-06-07T05:00:10.000Z","message":{"role":"user","content":"sub work"}}"#, "\n",
            r#"{"type":"assistant","timestamp":"2026-06-07T05:00:20.000Z","message":{"role":"assistant","model":"relay-model-x","usage":{"input_tokens":1000,"output_tokens":500,"cache_read_input_tokens":7,"cache_creation_input_tokens":3},"content":[{"type":"tool_use","id":"sw1","name":"Write","input":{"file_path":"/s/b.md","content":"y"}}]}}"#, "\n",
        ),
    );
    let out = h.run(&["stats", &format!("@{sess}")]);
    assert!(out.success, "stderr: {}", out.stderr);
    assert!(
        out.stdout.contains("relay-model-x"),
        "model row present: {}",
        out.stdout
    );
    assert!(
        out.stdout.contains("333") && out.stdout.contains("99"),
        "token sums 111+222=333 in / 44+55=99 out: {}",
        out.stdout
    );
    assert!(out.stdout.contains("Write"), "tool tally: {}", out.stdout);
    assert!(
        out.stdout.contains("1m05s"),
        "span label for the 65s session: {}",
        out.stdout
    );
    assert!(
        out.stdout.contains("1333") && out.stdout.contains("599"),
        "MERGED token sums 333+1000 / 99+500 across the two transcripts: {}",
        out.stdout
    );
    let js = h.run(&["stats", &format!("@{sess}"), "--format", "json"]);
    assert!(js.success, "stderr: {}", js.stderr);
    assert!(
        js.stdout
            .contains(&format!(r#""parent_session_id":"{sess}""#)),
        "the JSON id trio is populated: {}",
        js.stdout
    );
    assert!(
        js.stdout.contains(r#""Write":2"#),
        "MERGED tool calls across the two transcripts: {}",
        js.stdout
    );
    // The asserted merged values are UNIQUE to the merged object (5+7 / 4+3 - no single
    // row carries them): a per-row field must never be able to satisfy the merge pin.
    assert!(
        js.stdout.contains(r#""cache_read":12"#) && js.stdout.contains(r#""cache_creation":7"#),
        "the cache accumulators merge too: {}",
        js.stdout
    );
    assert!(
        js.stdout.contains(r#""is_subagent":true"#),
        "the subagent row's id-domain discriminator is real, never defaulted: {}",
        js.stdout
    );
}

#[test]
fn stats_line_type_census_counts_every_physical_line() {
    // Every physical line lands in the `types` census by its top-level `type`; a
    // {โ€ฆ}-framed line with an INVALID interior is counted malformed (the probe upgrades
    // the O(1) shape check to an exact whole-file census on stats).
    let h = Home::new();
    h.write(
        &format!("{ENC}/{SESS}.jsonl"),
        concat!(
            r#"{"type":"user","uuid":"u1","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"chart the harbor"}}"#, "\n",
            r#"{"type":"assistant","uuid":"a1","parentUuid":"u1","timestamp":"2026-06-07T05:00:01.000Z","message":{"role":"assistant","content":[{"type":"text","text":"charted"}]}}"#, "\n",
            r#"{"type":"attachment","uuid":"att1","attachment":{"type":"edited_text_file","filePath":"/p/x.rs","snippet":"fn y() {}"}}"#, "\n",
            r#"{"type":"file-history-snapshot","messageId":"m1"}"#, "\n",
            r#"{"type":"system","subtype":"compact_boundary","timestamp":"2026-06-07T05:00:02.000Z","compactMetadata":{"trigger":"auto","preTokens":10,"postTokens":2,"durationMs":5}}"#, "\n",
            r#"{"type":"attachment","broken":}"#, "\n",
        ),
    );
    let out = h.run(&["stats", at(SESS).as_str()]);
    assert!(out.success, "stderr: {}", out.stderr);
    assert!(
        out.stdout.contains("types"),
        "census line present:\n{}",
        out.stdout
    );
    for key in [
        "userร—1",
        "assistantร—1",
        "attachmentร—1",
        "file-history-snapshotร—1",
        "systemร—1",
    ] {
        assert!(out.stdout.contains(key), "missing {key}:\n{}", out.stdout);
    }
    assert!(
        out.stdout.contains("1 malformed line(s) skipped"),
        "framed-invalid interior on a non-candidate line is COUNTED:\n{}",
        out.stdout
    );

    let outj = h.run(&["stats", at(SESS).as_str(), "--format", "json"]);
    let rows: Vec<serde_json::Value> = outj
        .stdout
        .lines()
        .map(|l| serde_json::from_str(l).unwrap())
        .collect();
    let session = rows
        .iter()
        .find(|r| r["kind"] == "session")
        .expect("session row");
    assert_eq!(session["lines"], 6, "physical line count");
    assert_eq!(session["line_types"]["user"], 1);
    assert_eq!(session["line_types"]["attachment"], 1);
    assert_eq!(session["line_types"]["file-history-snapshot"], 1);
    assert_eq!(session["line_types"]["system"], 1);
    assert_eq!(session["skipped_lines"], 1);
    let summary = rows.last().unwrap();
    assert_eq!(
        summary["line_types"]["assistant"], 1,
        "summary merges the census: {}",
        outj.stdout
    );
}

const SESS2: &str = "b2c3d4e5-f6a7-4b8c-9d0e-1f2a3b4c5d6e";

#[test]
fn stats_total_block_merges_census_and_books_malformed_notes_exactly() {
    // Session 1 carries one tool call and one shape-malformed line; session 2 is clean
    // with OUT-OF-ORDER timestamps. Pins: the TOTAL block appears only for >1 session
    // and merges the types census + tools; the malformed note prints per-session AND as
    // the scope total (exactly twice, never a "(0 malformed" line); first/last span
    // derives from timestamp COMPARISON, not file order; the duration label is exact.
    let h = Home::new();
    h.write(
        &format!("{ENC}/{SESS}.jsonl"),
        concat!(
            r#"{"type":"user","uuid":"u1","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"go"}}"#, "\n",
            r#"{"type":"assistant","uuid":"a1","parentUuid":"u1","timestamp":"2026-06-07T05:00:01.000Z","message":{"role":"assistant","content":[{"type":"tool_use","id":"t1","name":"Read","input":{}}]}}"#, "\n",
            "not json here\n",
        ),
    );
    h.write(
        &format!("{ENC}/{SESS2}.jsonl"),
        concat!(
            r#"{"type":"user","uuid":"u1","timestamp":"2026-06-07T06:00:00.000Z","message":{"role":"user","content":"mid"}}"#, "\n",
            r#"{"type":"user","uuid":"u2","timestamp":"2026-06-07T05:00:00.000Z","message":{"role":"user","content":"earliest, recorded late"}}"#, "\n",
            r#"{"type":"assistant","uuid":"a1","parentUuid":"u2","timestamp":"2026-06-07T06:30:00.000Z","message":{"role":"assistant","content":[{"type":"text","text":"latest"}]}}"#, "\n",
        ),
    );
    let both = h.run(&["stats", ENC]);
    assert!(both.success, "stderr: {}", both.stderr);
    assert!(both.stdout.contains("TOTAL"), "{}", both.stdout);
    let total_tail = &both.stdout[both.stdout.find("TOTAL").unwrap()..];
    assert!(
        total_tail.contains("types") && total_tail.contains("userร—3"),
        "TOTAL merges the census:\n{}",
        both.stdout
    );
    assert!(
        total_tail.contains("tools") && total_tail.contains("Readร—1"),
        "TOTAL merges tools:\n{}",
        both.stdout
    );
    assert_eq!(
        both.stdout.matches("malformed line(s) skipped").count(),
        2,
        "per-session note + scope total, exactly twice:\n{}",
        both.stdout
    );
    assert!(
        !both.stdout.contains("(0 malformed"),
        "a clean session never prints a zero note:\n{}",
        both.stdout
    );

    // Single session: no TOTAL block, and a clean file prints no malformed note at all.
    let one = h.run(&["stats", at(SESS2).as_str()]);
    assert!(!one.stdout.contains("TOTAL"), "{}", one.stdout);
    assert!(!one.stdout.contains("malformed"), "{}", one.stdout);
    // Span is comparison-derived: earliest 05:00Z (recorded second), latest 06:30Z. The
    // JSON pair is raw UTC (tz-independent; text renders local).
    let onej = h.run(&["stats", at(SESS2).as_str(), "--format", "json"]);
    let row: serde_json::Value = onej
        .stdout
        .lines()
        .map(|l| serde_json::from_str(l).unwrap())
        .find(|o: &serde_json::Value| o["kind"] == "session")
        .expect("session row");
    assert_eq!(
        row["first_utc"], "2026-06-07T05:00:00.000Z",
        "first from comparison, not file order: {}",
        onej.stdout
    );
    assert_eq!(
        row["last_utc"], "2026-06-07T06:30:00.000Z",
        "{}",
        onej.stdout
    );
    assert!(
        one.stdout.contains("(1h30m"),
        "exact duration:\n{}",
        one.stdout
    );
}

// โ”€โ”€ usage dedupe by message.id (one API message spans multiple per-block records) โ”€โ”€

const DEDUP_ENC: &str = "-Users-dev-example-project";
const DEDUP_SESS: &str = "3f2e1d0c-9b8a-4765-b432-10fedcba9876";

#[test]
fn stats_usage_counts_one_api_message_once() {
    // Two records share one message.id with the IDENTICAL usage echo (the normal CC
    // per-block shape); a third record carries its own id.
    let h = Home::new();
    h.write(
        &format!("{DEDUP_ENC}/{DEDUP_SESS}.jsonl"),
        concat!(
            r#"{"type":"assistant","uuid":"a1","timestamp":"2026-06-07T05:00:01.000Z","message":{"role":"assistant","id":"msg_p1","model":"claude-test-1","usage":{"input_tokens":3,"output_tokens":40,"cache_read_input_tokens":100,"cache_creation_input_tokens":7},"content":[{"type":"thinking","thinking":"placeholder reasoning"}]}}"#, "\n",
            r#"{"type":"assistant","uuid":"a2","timestamp":"2026-06-07T05:00:02.000Z","message":{"role":"assistant","id":"msg_p1","model":"claude-test-1","usage":{"input_tokens":3,"output_tokens":40,"cache_read_input_tokens":100,"cache_creation_input_tokens":7},"content":[{"type":"text","text":"charted"}]}}"#, "\n",
            r#"{"type":"assistant","uuid":"a3","timestamp":"2026-06-07T05:00:03.000Z","message":{"role":"assistant","id":"msg_p2","model":"claude-test-1","usage":{"input_tokens":1,"output_tokens":2,"cache_read_input_tokens":0,"cache_creation_input_tokens":0},"content":[{"type":"text","text":"done"}]}}"#, "\n",
        ),
    );
    let out = h.run(&["stats", &at(DEDUP_SESS), "--format", "json"]);
    assert!(out.success, "stderr: {}", out.stderr);
    let row: serde_json::Value = out
        .stdout
        .lines()
        .filter_map(|l| serde_json::from_str(l).ok())
        .find(|v: &serde_json::Value| v["kind"] == "session")
        .expect("session row");
    let t = &row["tokens"]["claude-test-1"];
    assert_eq!(t["output"], 42, "40 once + 2, never 82: {}", out.stdout);
    assert_eq!(t["input"], 4, "{}", out.stdout);
    assert_eq!(t["cache_read"], 100, "{}", out.stdout);
    assert_eq!(t["cache_creation"], 7, "{}", out.stdout);
}

#[test]
fn stats_usage_zeroed_replay_never_wins() {
    // The compaction-replay shape: the same message.id written again with ZEROED
    // usage. The per-field MAX keeps the real numbers regardless of order.
    let h = Home::new();
    h.write(
        &format!("{DEDUP_ENC}/{DEDUP_SESS}.jsonl"),
        concat!(
            r#"{"type":"assistant","uuid":"z1","timestamp":"2026-06-07T05:00:01.000Z","message":{"role":"assistant","id":"msg_r1","model":"claude-test-1","usage":{"input_tokens":0,"output_tokens":0,"cache_read_input_tokens":0,"cache_creation_input_tokens":0},"content":[{"type":"text","text":"replayed copy"}]}}"#, "\n",
            r#"{"type":"assistant","uuid":"z2","timestamp":"2026-06-07T05:00:02.000Z","message":{"role":"assistant","id":"msg_r1","model":"claude-test-1","usage":{"input_tokens":2,"output_tokens":708,"cache_read_input_tokens":474834,"cache_creation_input_tokens":3997},"content":[{"type":"text","text":"original"}]}}"#, "\n",
        ),
    );
    let out = h.run(&["stats", &at(DEDUP_SESS), "--format", "json"]);
    let row: serde_json::Value = out
        .stdout
        .lines()
        .filter_map(|l| serde_json::from_str(l).ok())
        .find(|v: &serde_json::Value| v["kind"] == "session")
        .expect("session row");
    let t = &row["tokens"]["claude-test-1"];
    assert_eq!(
        t["output"], 708,
        "the zeroed replay never wins: {}",
        out.stdout
    );
    assert_eq!(t["cache_read"], 474834, "{}", out.stdout);
}

#[test]
fn stats_usage_dedupe_is_per_file_and_total_sums_files() {
    // The spawn message is copied into a child transcript with DIFFERENT usage: each
    // file keeps its own value (a genuine per-transcript fact); TOTAL sums both.
    let h = Home::new();
    h.write(
        &format!("{DEDUP_ENC}/{DEDUP_SESS}.jsonl"),
        concat!(
            r#"{"type":"assistant","uuid":"m1","timestamp":"2026-06-07T05:00:01.000Z","message":{"role":"assistant","id":"msg_x1","model":"claude-test-1","usage":{"input_tokens":0,"output_tokens":6945,"cache_read_input_tokens":0,"cache_creation_input_tokens":0},"content":[{"type":"text","text":"spawning"}]}}"#, "\n",
        ),
    );
    h.write(
        &format!("{DEDUP_ENC}/{DEDUP_SESS}/subagents/agent-1234567890abcdef.jsonl"),
        concat!(
            r#"{"type":"assistant","uuid":"c1","timestamp":"2026-06-07T05:00:02.000Z","message":{"role":"assistant","id":"msg_x1","model":"claude-test-1","usage":{"input_tokens":0,"output_tokens":10,"cache_read_input_tokens":0,"cache_creation_input_tokens":0},"content":[{"type":"text","text":"child copy"}]}}"#, "\n",
        ),
    );
    let out = h.run(&["stats", &at(DEDUP_SESS), "--format", "json"]);
    let outputs: Vec<u64> = out
        .stdout
        .lines()
        .filter_map(|l| serde_json::from_str::<serde_json::Value>(l).ok())
        .filter(|v| v["kind"] == "session")
        .filter_map(|v| v["tokens"]["claude-test-1"]["output"].as_u64())
        .collect();
    let mut sorted = outputs.clone();
    sorted.sort_unstable();
    assert_eq!(
        sorted,
        vec![10, 6945],
        "per-file values kept: {}",
        out.stdout
    );
    let summary: serde_json::Value = out
        .stdout
        .lines()
        .filter_map(|l| serde_json::from_str(l).ok())
        .find(|v: &serde_json::Value| v["kind"] == "summary")
        .expect("summary");
    assert_eq!(
        summary["tokens"]["claude-test-1"]["output"], 6955,
        "TOTAL sums files, never dedupes across them: {}",
        out.stdout
    );
}

#[test]
fn stats_usage_idless_records_count_individually() {
    // A record with no message.id keeps the old per-record semantics beside an
    // id-bearing pair (every pre-id transcript takes this path).
    let h = Home::new();
    h.write(
        &format!("{DEDUP_ENC}/{DEDUP_SESS}.jsonl"),
        concat!(
            r#"{"type":"assistant","uuid":"n1","timestamp":"2026-06-07T05:00:01.000Z","message":{"role":"assistant","model":"claude-test-1","usage":{"input_tokens":0,"output_tokens":5,"cache_read_input_tokens":0,"cache_creation_input_tokens":0},"content":[{"type":"text","text":"old format"}]}}"#, "\n",
            r#"{"type":"assistant","uuid":"n2","timestamp":"2026-06-07T05:00:02.000Z","message":{"role":"assistant","model":"claude-test-1","usage":{"input_tokens":0,"output_tokens":5,"cache_read_input_tokens":0,"cache_creation_input_tokens":0},"content":[{"type":"text","text":"old format two"}]}}"#, "\n",
            r#"{"type":"assistant","uuid":"n3","timestamp":"2026-06-07T05:00:03.000Z","message":{"role":"assistant","id":"msg_q1","model":"claude-test-1","usage":{"input_tokens":0,"output_tokens":30,"cache_read_input_tokens":0,"cache_creation_input_tokens":0},"content":[{"type":"text","text":"new format"}]}}"#, "\n",
        ),
    );
    let out = h.run(&["stats", &at(DEDUP_SESS), "--format", "json"]);
    let row: serde_json::Value = out
        .stdout
        .lines()
        .filter_map(|l| serde_json::from_str(l).ok())
        .find(|v: &serde_json::Value| v["kind"] == "session")
        .expect("session row");
    assert_eq!(
        row["tokens"]["claude-test-1"]["output"], 40,
        "5 + 5 individually + 30 once: {}",
        out.stdout
    );
}