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
//! Hit rendering: glyphs, labels, excerpt centering and truncation, sibling caps.

use super::*;

// ── Branch-completeness for the pure render helpers ──

#[test]
fn class_path_and_role_glyph_cover_every_leaf() {
    // Every Class::ALL leaf round-trips through path() (the rendered/JSON label) and maps to a
    // role glyph (◂ user, ▸ agent, ⚙ harness) - the cutover replacement for the old flat
    // category_label/glyph table.
    for &c in Class::ALL {
        assert!(!c.path().is_empty());
        let g = role_glyph(Some(c));
        assert!(matches!(g, '' | '' | ''), "{} -> {g}", c.path());
    }
    assert_eq!(role_glyph(Some(Class::UserMessage)), '');
    assert_eq!(role_glyph(Some(Class::AgentMessage)), '');
    assert_eq!(role_glyph(Some(Class::CommInbox)), ''); // comm is agent-side
    assert_eq!(role_glyph(Some(Class::NotificationWorkflow)), '');
    // An addressed record with no modeled leaf takes its own marker (the refetch law).
    assert_eq!(role_glyph(None), '?');
}

#[test]
fn render_label_decorates_pairing_and_direction() {
    // ▹ pairing: a paired tool.use renders the two-sided form; a pending use / orphan result
    // render their notes. ⇨ direction: a comm hit appends `from ⇨ to`.
    let paired = Hit {
        class: Some(Class::AgentToolUse),
        labels: vec!["agent.tool.use"],
        excerpt: String::new(),
        body: None,
        timestamp_utc: None,
        tool_name: None,
        model: None,
        attachment_type: None,
        version: None,
        is_error: None,
        denial_kind: None,
        direction: None,
        tool_use_id: Some("t1".into()),
        pair: Some(Pairing::Paired),
        line: 0,
        uuid: None,
        raw: None,
        image_ids: Vec::new(),
        from_sidecar: false,
        queue_operation: None,
        queue_reason: None,
        task_ids: Vec::new(),
        orphan_kind: None,
        delivery: None,
        resume_paired: None,
        scheduled_at: None,
        survival: crate::model::Survival::Live,
        rewound_branch: false,
        replay_copy_of: None,
        compaction: None,
        truncated: false,
    };
    assert_eq!(render_label(&paired), "agent.tool.use ▹ agent.tool.result");
    let pending = Hit {
        pair: Some(Pairing::PendingNoResult),
        ..paired.clone()
    };
    assert_eq!(
        render_label(&pending),
        "agent.tool.use (no result — pending)"
    );
    let orphan = Hit {
        class: Some(Class::AgentToolResult),
        pair: Some(Pairing::OrphanResult),
        ..paired.clone()
    };
    assert_eq!(
        render_label(&orphan),
        "agent.tool.result (use not in scope)"
    );
    let comm = Hit {
        class: Some(Class::CommInbox),
        direction: Some(("VSMultiRegion".into(), "self".into())),
        tool_use_id: None,
        pair: None,
        ..paired.clone()
    };
    assert_eq!(
        render_label(&comm),
        "agent.communication.inbox  VSMultiRegion ⇨ self"
    );
}

#[test]
fn render_tool_use_name_only_input_only_both_neither() {
    assert_eq!(render_tool_use(Some("Bash"), None), "Bash");
    let v = serde_json::json!({"k":"v"});
    // input only (no name) → leading space then the json.
    assert_eq!(render_tool_use(None, Some(&v)), " {\"k\":\"v\"}");
    // both
    assert_eq!(
        render_tool_use(Some("Read"), Some(&v)),
        "Read {\"k\":\"v\"}"
    );
    // neither → empty
    assert_eq!(render_tool_use(None, None), "");
}

#[test]
fn truncate_excerpt_long_and_short() {
    assert_eq!(truncate_excerpt("short"), "short");
    let s = "y".repeat(EXCERPT_MAX + 3);
    let out = truncate_excerpt(&s);
    assert!(out.ends_with("… (+3 chars)"), "got: {out}");
}

#[test]
fn match_excerpt_centers_on_a_deep_match() {
    // The needle sits ~800 chars in - far past EXCERPT_MAX. The OLD head-only
    // excerpt hid it entirely (the bug that forced raw-jsonl reads); centering
    // must surface it, with explicit clipping markers on both sides.
    // Synthetic multi-byte placeholder (neutral emoji) + neutral padding.
    let needle = "🤖🎉✅🚀🌟";
    let text = format!("{}{needle}{}", "🔵".repeat(800), "🟥".repeat(800));
    let m = build_matcher(&args(needle)).unwrap();
    let span = m.locate(&text).expect("matches").expect("has a span");
    let (ex, truncated) = match_excerpt(&text, Some(span), EXCERPT_MAX);
    assert!(ex.contains(needle), "excerpt must show the match: {ex}");
    assert!(
        ex.starts_with(''),
        "content precedes the window → leading …: {ex}"
    );
    assert!(
        ex.contains("chars)"),
        "content follows → trailing count: {ex}"
    );
    assert!(truncated, "a clipped match-centered window is truncated");
}

#[test]
fn match_excerpt_short_message_is_shown_whole() {
    let text = "a short hit here";
    let m = build_matcher(&args("hit")).unwrap();
    let span = m.locate(text).unwrap();
    let (ex, truncated) = match_excerpt(text, span, EXCERPT_MAX);
    assert_eq!(ex, "a short hit here");
    assert!(!truncated, "a message that fits the cap is not truncated");
}

#[test]
fn match_excerpt_early_match_keeps_the_head() {
    let text = format!("needle {}", "z".repeat(EXCERPT_MAX));
    let m = build_matcher(&args("needle")).unwrap();
    let span = m.locate(&text).unwrap();
    let (ex, truncated) = match_excerpt(&text, span, EXCERPT_MAX);
    assert!(!ex.starts_with(''), "match at char 0 → no leading …: {ex}");
    assert!(ex.starts_with("needle"), "got: {ex}");
    assert!(truncated, "the tail past the window was dropped");
}

#[test]
fn match_excerpt_pure_filter_falls_back_to_head() {
    let text = "X".repeat(EXCERPT_MAX + 50);
    let m = build_matcher(&args("")).unwrap(); // empty pattern = pure filter
    let span = m.locate(&text).expect("pure filter matches");
    assert_eq!(span, None, "pure filter has no locatable span");
    let (ex, truncated) = match_excerpt(&text, span, EXCERPT_MAX);
    assert!(!ex.starts_with(''), "head form has no leading …");
    assert!(ex.ends_with("… (+50 chars)"), "got: {ex}");
    assert!(truncated, "the head form clipped 50 chars");
}

#[test]
fn match_excerpt_full_budget_emits_whole_message() {
    // `--no-truncate` passes `usize::MAX` as the budget: a message longer than EXCERPT_MAX is
    // emitted whole, with NO truncation marker - whereas the default budget truncates.
    let n = EXCERPT_MAX + 200;
    let text = "🤖".repeat(n);
    let (capped, capped_truncated) = match_excerpt(&text, None, EXCERPT_MAX);
    assert!(
        capped.contains("… (+"),
        "default budget truncates: {capped}"
    );
    assert!(capped_truncated, "default budget reports truncation");
    let (full, full_truncated) = match_excerpt(&text, None, usize::MAX);
    assert!(
        !full.contains("… (+"),
        "full budget has no truncation marker"
    );
    assert!(
        !full_truncated,
        "--no-truncate's usize::MAX budget never truncates — the signal the caution note keys on"
    );
    assert_eq!(full.chars().count(), n, "full text length preserved");
}

#[test]
fn uncapped_body_is_keyed_on_the_budget_and_keeps_newlines() {
    // The `body` companion of `match_excerpt`: present ONLY at the uncapped budget, and
    // then the text VERBATIM - the excerpt's whitespace collapse is not applied.
    let text = "first line\n\nthird line\twith a tab";
    assert_eq!(uncapped_body(text, usize::MAX).as_deref(), Some(text));
    assert_eq!(
        uncapped_body(text, usize::MAX)
            .expect("body")
            .matches('\n')
            .count(),
        2,
        "both newlines survive - a body is not one line"
    );
    // Any real budget yields nothing, including a budget wide enough for this text: the
    // pairing is with the MODE (`--no-truncate` / an address), never with whether this
    // particular record happened to fit.
    assert_eq!(uncapped_body(text, EXCERPT_MAX), None);
    assert_eq!(uncapped_body(text, usize::MAX - 1), None);
    assert_eq!(uncapped_body("", usize::MAX).as_deref(), Some(""));
}

#[test]
fn a_channel_delivery_renders_verbatim_as_a_message() {
    // The record-text view of `agent.communication.channel` is the injected string
    // VERBATIM (header line + body): the header is the receipt a reader audits, and a
    // verbatim byte substring is what keeps the 7d/7f prefilter laws sound.
    let rec: Record = serde_json::from_str(
        r#"{"type":"attachment","uuid":"att1","attachment":{"type":"hook_additional_context","content":["[csift-channel v1 id=abc part=1/1 mode=steer from=aRelay-0123456789abcdef]\nthrottlebeacon the region queue","a second block"]}}"#,
    )
    .unwrap();
    let labels = rec.classify(&crate::model::ClassifyCtx::top_level());
    let (class, text) =
        record_text_emission(&rec, &labels, LabelFilter::all(), &PlanIndex::default())
            .expect("a channel delivery emits");
    assert_eq!(class, Class::CommChannel, "the message view is the richest");
    assert_eq!(
        text,
        "[csift-channel v1 id=abc part=1/1 mode=steer from=aRelay-0123456789abcdef]\nthrottlebeacon the region queue"
    );
    // Under a hook-only selector the SAME record renders the harness view instead: the
    // joined content, exactly as every other hook context renders.
    let include = vec!["harness.meta.hook".to_string()];
    let hook_only = LabelFilter::new(&include, &[]);
    let (class, text) =
        record_text_emission(&rec, &labels, hook_only, &PlanIndex::default()).expect("hook view");
    assert_eq!(class, Class::MetaHook);
    assert!(
        text.ends_with("\na second block"),
        "the hook view is the joined content: {text:?}"
    );
    // A delivery renders as a message: the agent glyph, the comm direction, and no
    // sibling cap (harness leaves are capped at 2 per turn; a message never is).
    assert_eq!(role_glyph(Some(Class::CommChannel)), '');
    assert_eq!(sibling_cap(Class::CommChannel), None);
    let hit = Hit {
        class: Some(Class::CommChannel),
        labels: vec!["agent.communication.channel", "harness.meta.hook"],
        excerpt: String::new(),
        body: None,
        timestamp_utc: None,
        tool_name: None,
        model: None,
        attachment_type: Some("hook_additional_context".into()),
        version: None,
        is_error: None,
        denial_kind: None,
        direction: Some(("aRelay-0123456789abcdef".into(), "self".into())),
        tool_use_id: None,
        pair: None,
        line: 2,
        uuid: None,
        raw: None,
        image_ids: Vec::new(),
        from_sidecar: false,
        queue_operation: None,
        queue_reason: None,
        task_ids: Vec::new(),
        orphan_kind: None,
        delivery: None,
        resume_paired: None,
        scheduled_at: None,
        survival: crate::model::Survival::Live,
        rewound_branch: false,
        replay_copy_of: None,
        compaction: None,
        truncated: false,
    };
    assert_eq!(
        render_label(&hit),
        "agent.communication.channel  aRelay-0123456789abcdef ⇨ self"
    );
}

#[test]
fn sibling_cap_policy_is_fixed_and_message_classes_uncapped() {
    // Message classes always render (None = uncapped); chattier machinery is capped.
    assert_eq!(sibling_cap(Class::UserMessage), None);
    assert_eq!(sibling_cap(Class::AgentMessage), None);
    assert_eq!(sibling_cap(Class::CommInbox), None);
    assert_eq!(sibling_cap(Class::AgentThinking), Some(2));
    assert_eq!(sibling_cap(Class::AgentThinkingNarration), Some(1));
    assert_eq!(sibling_cap(Class::UserUnsent), None);
    assert_eq!(sibling_cap(Class::AgentToolUse), Some(3));
    assert_eq!(sibling_cap(Class::AgentToolResult), Some(3));
    assert_eq!(sibling_cap(Class::CommandStdout), Some(2));
}

/// C-33: a compaction summary written by a `/rewind` summarize names its direction in the
/// label zone (display-only, like `[narration summary]`); an ordinary compaction summary
/// renders the bare leaf path exactly as before.
#[test]
fn a_summarize_summary_names_its_direction_in_the_label_zone() {
    let mk = |compaction: Option<Box<CompactionHit>>| Hit {
        class: Some(Class::CompactionSummary),
        labels: vec!["harness.compaction.summary"],
        excerpt: String::new(),
        body: None,
        timestamp_utc: None,
        tool_name: None,
        model: None,
        attachment_type: None,
        version: None,
        is_error: None,
        denial_kind: None,
        direction: None,
        tool_use_id: None,
        pair: None,
        line: 9,
        uuid: None,
        raw: None,
        image_ids: Vec::new(),
        from_sidecar: false,
        queue_operation: None,
        queue_reason: None,
        task_ids: Vec::new(),
        orphan_kind: None,
        delivery: None,
        resume_paired: None,
        scheduled_at: None,
        survival: crate::model::Survival::Live,
        rewound_branch: false,
        replay_copy_of: None,
        compaction,
        truncated: false,
    };
    let tagged = mk(Some(Box::new(CompactionHit {
        metadata: None,
        mode: Some(crate::model::SummarizeMode::UpToHere),
        direction: Some("up_to".into()),
    })));
    assert_eq!(
        render_label(&tagged),
        "harness.compaction.summary [summarize up_to]"
    );
    // An ordinary compaction: a mode, but no direction to name.
    let plain = mk(Some(Box::new(CompactionHit {
        metadata: None,
        mode: Some(crate::model::SummarizeMode::Compact),
        direction: None,
    })));
    assert_eq!(render_label(&plain), "harness.compaction.summary");
    assert_eq!(render_label(&mk(None)), "harness.compaction.summary");
    // An unmodeled direction still renders its own verbatim word rather than nothing.
    let odd = mk(Some(Box::new(CompactionHit {
        metadata: None,
        mode: None,
        direction: Some("sideways".into()),
    })));
    assert_eq!(
        render_label(&odd),
        "harness.compaction.summary [summarize sideways]"
    );
}

/// C-33 PERF: the boundary -> mode pairing is built only when the active selection can
/// surface a compaction leaf, so a `-t user`/`-t agent.*` scan never walks the records for a
/// feature it cannot show. The gate is a named predicate precisely so this is pinned.
#[test]
fn the_compaction_pairing_is_skipped_when_no_compaction_leaf_is_selectable() {
    use crate::cli::LabelFilter;
    let none: Vec<String> = Vec::new();
    // No `-t` at all: every leaf is selectable, so the pairing is built.
    assert!(wants_compaction_pairing(&LabelFilter::all()));
    assert!(wants_compaction_pairing(&LabelFilter::new(&none, &none)));
    for reaching in [
        "harness",
        "harness.compaction",
        "harness.compaction.boundary",
        "harness.compaction.summary",
    ] {
        let inc = vec![reaching.to_string()];
        assert!(
            wants_compaction_pairing(&LabelFilter::new(&inc, &none)),
            "{reaching} reaches a compaction leaf"
        );
    }
    for missing in ["user", "user.unsent", "agent", "agent.tool", "harness.meta"] {
        let inc = vec![missing.to_string()];
        assert!(
            !wants_compaction_pairing(&LabelFilter::new(&inc, &none)),
            "{missing} reaches neither compaction leaf, so the index must be skipped"
        );
    }
    // `-T` takes the leaves back out: an excluded pair is also a skipped index.
    let inc = vec!["harness".to_string()];
    let exc = vec!["harness.compaction".to_string()];
    assert!(!wants_compaction_pairing(&LabelFilter::new(&inc, &exc)));
}