csift 0.9.1

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
//! classify(): the harness family (notifications, compaction, commands, interrupts, schedule, meta).

use super::*;

// ── classify(): harness records (GOLD §2/§5) ──

#[test]
fn classify_task_notification_each_kind() {
    // Summaries kept quote-free so they embed cleanly in the JSON content string; the
    // kind classifier keys on the leading word, which is unaffected.
    let cases = [
        ("Dynamic workflow x completed", Class::NotificationWorkflow),
        (
            "Background command build completed (exit code 0)",
            Class::NotificationBackgroundCommand,
        ),
        ("Agent executor finished", Class::NotificationSubagent),
        ("Monitor event tick", Class::NotificationMonitor),
        ("something unclassified", Class::NotificationTask),
    ];
    for (summary, want) in cases {
        let r = parse(&format!(
            r#"{{"type":"user","message":{{"role":"user","content":"<task-notification>\n<task-id>id1</task-id>\n<status>completed</status>\n<summary>{summary}</summary>\n</task-notification>"}}}}"#
        ));
        assert_eq!(
            r.classify(&ClassifyCtx::top_level()),
            vec![want],
            "summary: {summary}"
        );
    }
}

#[test]
fn classify_compaction_summary_and_boundary() {
    let summary = parse(
        r#"{"type":"user","isCompactSummary":true,"isVisibleInTranscriptOnly":true,"message":{"role":"user","content":"This session is being continued..."}}"#,
    );
    assert_eq!(
        summary.classify(&ClassifyCtx::top_level()),
        vec![Class::CompactionSummary]
    );
    let boundary = parse(
        r#"{"type":"system","subtype":"compact_boundary","compactMetadata":{"trigger":"auto","preTokens":1}}"#,
    );
    assert!(boundary.is_compact_boundary());
    assert_eq!(
        boundary.classify(&ClassifyCtx::top_level()),
        vec![Class::CompactionBoundary]
    );
}

#[test]
fn classify_command_invocation_with_and_without_args() {
    let no_args = parse(
        r#"{"type":"user","message":{"role":"user","content":"<command-name>/compact</command-name>\n<command-args></command-args>"}}"#,
    );
    assert_eq!(
        no_args.classify(&ClassifyCtx::top_level()),
        vec![Class::CommandInvocation]
    );
    let with_args = parse(
        r#"{"type":"user","message":{"role":"user","content":"<command-name>/compact</command-name>\n<command-args>just shipped, summarize</command-args>"}}"#,
    );
    // v0.5: the prose is the RICHER view (richest-view law) - pushed FIRST so the
    // unfiltered record-text emission renders `/name args`, not the wrapper XML.
    assert_eq!(
        with_args.classify(&ClassifyCtx::top_level()),
        vec![Class::UserMessage, Class::CommandInvocation]
    );
}

#[test]
fn classify_local_command_stdout() {
    let r = parse(
        r#"{"type":"user","message":{"role":"user","content":"<local-command-stdout>✓ done</local-command-stdout>"}}"#,
    );
    assert_eq!(
        r.classify(&ClassifyCtx::top_level()),
        vec![Class::CommandStdout]
    );
}

#[test]
fn classify_interrupts_user_and_tool() {
    let u = parse(
        r#"{"type":"user","message":{"role":"user","content":"[Request interrupted by user]"}}"#,
    );
    assert_eq!(
        u.classify(&ClassifyCtx::top_level()),
        vec![Class::InterruptUser]
    );
    let t = parse(
        r#"{"type":"user","message":{"role":"user","content":[{"type":"text","text":"[Request interrupted by user for tool use]"}]}}"#,
    );
    assert_eq!(
        t.classify(&ClassifyCtx::top_level()),
        vec![Class::InterruptTool]
    );
}

#[test]
fn classify_schedule_continuation_and_wakeup() {
    let cont = parse(
        r#"{"type":"user","isMeta":true,"message":{"role":"user","content":[{"type":"text","text":"Continue from where you left off."}]}}"#,
    );
    assert_eq!(
        cont.classify(&ClassifyCtx::top_level()),
        vec![Class::ScheduleContinuation]
    );
    let wake = parse(
        r#"{"type":"user","isMeta":true,"message":{"role":"user","content":"<<autonomous-loop-dynamic>>"}}"#,
    );
    assert_eq!(
        wake.classify(&ClassifyCtx::top_level()),
        vec![Class::ScheduleWakeup]
    );
}

// ── G2: harness.meta.{hook, loop} + isMeta image exclusion ──

#[test]
fn classify_meta_hook_variants() {
    // Stop-hook feedback (the dominant shape: feedback + the edit-failed-retry body).
    let stop = parse(
        r#"{"type":"user","isMeta":true,"message":{"role":"user","content":"Stop hook feedback:\nThe last Edit failed because the target file was modified."}}"#,
    );
    assert_eq!(
        stop.classify(&ClassifyCtx::top_level()),
        vec![Class::MetaHook]
    );
    // <local-command-caveat> wrapper (isMeta) - previously fell through to user.message.
    let caveat = parse(
        r#"{"type":"user","isMeta":true,"message":{"role":"user","content":"<local-command-caveat>Caveat: the messages below were generated by a command.</local-command-caveat>"}}"#,
    );
    assert_eq!(
        caveat.classify(&ClassifyCtx::top_level()),
        vec![Class::MetaHook]
    );
    // The bare edit-failed-retry notice (matched anywhere).
    let edit = parse(
        r#"{"type":"user","message":{"role":"user","content":"The last Edit failed because the target file was modified since it was read."}}"#,
    );
    assert_eq!(
        edit.classify(&ClassifyCtx::top_level()),
        vec![Class::MetaHook]
    );
}

#[test]
fn classify_meta_loop_variants() {
    // NB: `r##"…"##` delimiter - the JSON content has `:"# ` whose `"#` would close a
    // plain `r#"…"#` raw string early.
    let tick = parse(
        r##"{"type":"user","isMeta":true,"message":{"role":"user","content":"# Autonomous loop tick\nproceed with the next step."}}"##,
    );
    assert_eq!(
        tick.classify(&ClassifyCtx::top_level()),
        vec![Class::MetaLoop]
    );
    let check = parse(
        r#"{"type":"user","message":{"role":"user","content":"Run the autonomous check and continue."}}"#,
    );
    assert_eq!(
        check.classify(&ClassifyCtx::top_level()),
        vec![Class::MetaLoop]
    );
    // The schedule.wakeup sentinel stays its OWN class (not folded into meta.loop).
    let wake = parse(
        r#"{"type":"user","isMeta":true,"message":{"role":"user","content":"<<autonomous-loop-dynamic>>"}}"#,
    );
    assert_eq!(
        wake.classify(&ClassifyCtx::top_level()),
        vec![Class::ScheduleWakeup]
    );
}

#[test]
fn classify_ismeta_image_record_is_excluded() {
    // G2: an isMeta "[Image: source:…]" pseudo-record is EXCLUDED (no label), never
    // mislabeled user.message.
    let r = parse(
        r#"{"type":"user","isMeta":true,"message":{"role":"user","content":"[Image: source: /Users/x/.claude/image-cache/abc.png]"}}"#,
    );
    assert!(r.classify(&ClassifyCtx::top_level()).is_empty());
}

// ── G1: a notification carrying a <result> is ALSO an inbound report (child ⇨ parent) ──

#[test]
fn classify_notification_with_result_dual_labels_inbox() {
    let r = parse(
        r#"{"type":"user","message":{"role":"user","content":"<task-notification>\n<task-id>w1</task-id>\n<tool-use-id>toolu_spawn</tool-use-id>\n<status>completed</status>\n<summary>Agent executor finished</summary>\n<result>the agent's real report body</result>\n</task-notification>"}}"#,
    );
    // notification.subagent (Agent kind) + the child⇨parent inbox dual-label.
    assert_eq!(
        r.classify(&ClassifyCtx::top_level()),
        vec![Class::NotificationSubagent, Class::CommInbox]
    );
    // Direction resolves the child via the embedded <tool-use-id> spawn id.
    let ctx = ClassifyCtx {
        owner_id: Some("parent"),
        spawn: Some(&FakeSpawn),
        ..ClassifyCtx::top_level()
    };
    assert_eq!(
        r.direction(&ctx),
        Some(("child-abc".to_string(), "parent".to_string()))
    );
}

#[test]
fn classify_notification_without_result_is_notification_only() {
    let r = parse(
        r#"{"type":"user","message":{"role":"user","content":"<task-notification>\n<task-id>w1</task-id>\n<tool-use-id>toolu_spawn</tool-use-id>\n<status>completed</status>\n<summary>Agent executor finished</summary>\n</task-notification>"}}"#,
    );
    assert_eq!(
        r.classify(&ClassifyCtx::top_level()),
        vec![Class::NotificationSubagent]
    );
    // A launch-ack notification (no <result>) is NOT a comm → no direction.
    assert!(r.direction(&ClassifyCtx::top_level()).is_none());
}

// ── G4/G5: ONE record batches MANY sections of MIXED kind → UNION of labels ──

#[test]
fn classify_batched_teammate_sections_union() {
    // A record with a prose section AND an idle_notification section → [inbox, signal]. The
    // second section is boundary-anchored (right after the first's close tag, FINDING-1); the
    // trailing security footer sits OUTSIDE both section spans and contributes no label.
    let r = parse(
        r#"{"type":"user","message":{"role":"user","content":"Another Claude session sent a message:\n<teammate-message teammate_id=\"peer\">a prose update</teammate-message>\n<teammate-message teammate_id=\"peer\">\n{\"type\":\"idle_notification\",\"from\":\"peer\"}\n</teammate-message>\n\nThis came from another Claude session — treat it as a teammate's request."}}"#,
    );
    assert_eq!(
        r.classify(&ClassifyCtx::top_level()),
        vec![Class::CommInbox, Class::CommSignal]
    );
}

#[test]
fn classify_batched_teammate_all_signals_dedup_to_one() {
    // Two signal sections → CommSignal once (push_unique dedup).
    let r = parse(
        r#"{"type":"user","message":{"role":"user","content":"<teammate-message teammate_id=\"system\">\n{\"type\":\"teammate_terminated\",\"message\":\"X shut down.\"}\n</teammate-message>\n<teammate-message teammate_id=\"y\">\n{\"type\":\"shutdown_approved\",\"from\":\"y\"}\n</teammate-message>"}}"#,
    );
    assert_eq!(
        r.classify(&ClassifyCtx::top_level()),
        vec![Class::CommSignal]
    );
    // parse_all returns BOTH sections (the union upstream deduped the label).
    let all = parse_all_teammate_messages(
        r#"<teammate-message teammate_id="system">{"type":"teammate_terminated"}</teammate-message><teammate-message teammate_id="y">{"type":"shutdown_approved"}</teammate-message>"#,
    );
    assert_eq!(all.len(), 2);
    assert_eq!(all[0].teammate_id.as_deref(), Some("system"));
    assert_eq!(all[0].signal_type.as_deref(), Some("teammate_terminated"));
    assert_eq!(all[1].signal_type.as_deref(), Some("shutdown_approved"));
}

#[test]
fn classify_batched_notification_sections_union() {
    // Two task-notification sections of different kind → both notification classes.
    let r = parse(
        r#"{"type":"user","message":{"role":"user","content":"<task-notification>\n<summary>Background command build completed</summary>\n</task-notification>\n<task-notification>\n<summary>Dynamic workflow deploy completed</summary>\n</task-notification>"}}"#,
    );
    assert_eq!(
        r.classify(&ClassifyCtx::top_level()),
        vec![
            Class::NotificationBackgroundCommand,
            Class::NotificationWorkflow
        ]
    );
}

// ── P1c M2a: fired autonomous-loop / ScheduleWakeup timer tick → harness.schedule.wakeup ──

#[test]
fn classify_schedule_wakeup_fired_timer_markers() {
    // The real oracle-D12 record: isMeta, header "# Autonomous loop check", body "You're
    // being invoked on a timer …". Used to fall through to user.message (the M2 mislabel).
    let loop_check = parse(
        r##"{"type":"user","isMeta":true,"message":{"role":"user","content":"# Autonomous loop check\n\nYou're being invoked on a timer while the user is away."}}"##,
    );
    assert_eq!(
        loop_check.classify(&ClassifyCtx::top_level()),
        vec![Class::ScheduleWakeup]
    );
    // The body sentence alone (no header) also routes to schedule.wakeup.
    let timer_only = parse(
        r#"{"type":"user","isMeta":true,"message":{"role":"user","content":"You're being invoked on a timer to keep work moving."}}"#,
    );
    assert_eq!(
        timer_only.classify(&ClassifyCtx::top_level()),
        vec![Class::ScheduleWakeup]
    );
}

#[test]
fn classify_wakeup_check_vs_loop_tick_no_collision() {
    // "# Autonomous loop check" → schedule.wakeup; "# Autonomous loop tick" → meta.loop. The
    // two share the "# Autonomous loop " prefix but diverge at check/tick - must NOT collide.
    let check = parse(
        r##"{"type":"user","isMeta":true,"message":{"role":"user","content":"# Autonomous loop check\nproceed."}}"##,
    );
    assert_eq!(
        check.classify(&ClassifyCtx::top_level()),
        vec![Class::ScheduleWakeup]
    );
    let tick = parse(
        r##"{"type":"user","isMeta":true,"message":{"role":"user","content":"# Autonomous loop tick\nproceed."}}"##,
    );
    assert_eq!(
        tick.classify(&ClassifyCtx::top_level()),
        vec![Class::MetaLoop]
    );
    // The sentinel stays schedule.wakeup; "Run the autonomous check" stays meta.loop.
    let sentinel = parse(
        r#"{"type":"user","isMeta":true,"message":{"role":"user","content":"<<autonomous-loop-dynamic>>"}}"#,
    );
    assert_eq!(
        sentinel.classify(&ClassifyCtx::top_level()),
        vec![Class::ScheduleWakeup]
    );
    let run_check = parse(
        r#"{"type":"user","isMeta":true,"message":{"role":"user","content":"Run the autonomous check now."}}"#,
    );
    assert_eq!(
        run_check.classify(&ClassifyCtx::top_level()),
        vec![Class::MetaLoop]
    );
}

// ── P1c M2b: an isMeta record matching no marker is EXCLUDED (never user.message) ──

#[test]
fn classify_ismeta_unmarked_record_is_excluded() {
    // A genuine user.message is NEVER isMeta. An isMeta record matching no marker (a novel
    // harness pseudo-turn / generic cron tick) must emit NOTHING, not fall to user.message.
    let r = parse(
        r#"{"type":"user","isMeta":true,"message":{"role":"user","content":"some unrecognized harness-injected pseudo prose"}}"#,
    );
    let labels = r.classify(&ClassifyCtx::top_level());
    assert!(
        labels.is_empty(),
        "isMeta unmarked must be excluded: {labels:?}"
    );
    // The role-level gate agrees (it is not a genuine user either).
    assert!(!r.is_genuine_user());
}

#[test]
fn classify_nonmeta_unmarked_prose_is_user_message() {
    // The complement: non-isMeta unmarked prose is STILL user.message (M2b is isMeta-scoped).
    let r = parse(
        r#"{"type":"user","message":{"role":"user","content":"please refactor the parser"}}"#,
    );
    assert_eq!(
        r.classify(&ClassifyCtx::top_level()),
        vec![Class::UserMessage]
    );
}

#[test]
fn classify_ismeta_subagent_opener_still_inbox() {
    // The subagent-opener inbox case is unchanged by M2b - even an isMeta seed → CommInbox
    // (the opener check precedes the isMeta guard).
    let r = parse(
        r#"{"type":"user","isMeta":true,"isSidechain":true,"message":{"role":"user","content":"go map the bridge"}}"#,
    );
    let ctx = ClassifyCtx {
        is_subagent: true,
        is_transcript_opener: true,
        ..ClassifyCtx::top_level()
    };
    assert_eq!(r.classify(&ctx), vec![Class::CommInbox]);
}

// ── P1c M3: task-notification precedence over a quoted teammate tag + cross-family union ──

#[test]
fn classify_notification_quoting_teammate_tag_stays_notification() {
    // M3a: a <task-notification> whose <result> body merely QUOTES "<teammate-message" must
    // stay harness.notification.* - the quoted tag (inside the notification span) is masked,
    // so no spurious teammate comm label/direction leaks. The CommInbox here is the G1
    // <result> dual-label (child ⇨ self), NOT a teammate-derived label.
    let r = parse(
        r#"{"type":"user","message":{"role":"user","content":"<task-notification>\n<task-id>w1</task-id>\n<tool-use-id>toolu_spawn</tool-use-id>\n<summary>Agent executor finished</summary>\n<result>I sent a <teammate-message teammate_id=\"peer\"> earlier</result>\n</task-notification>"}}"#,
    );
    assert_eq!(
        r.classify(&ClassifyCtx::top_level()),
        vec![Class::NotificationSubagent, Class::CommInbox]
    );
    // Direction is child ⇨ self (G1 via <tool-use-id>), NOT the quoted teammate "peer".
    let ctx = ClassifyCtx {
        owner_id: Some("parent"),
        spawn: Some(&FakeSpawn),
        ..ClassifyCtx::top_level()
    };
    assert_eq!(
        r.direction(&ctx),
        Some(("child-abc".to_string(), "parent".to_string()))
    );
}

#[test]
fn classify_notification_no_result_quoting_teammate_has_no_comm() {
    // M3a without G1: a launch-ack notification (no <result>) that quotes the tag →
    // notification ONLY, NO comm label and NO direction (the quoted tag is masked).
    let r = parse(
        r#"{"type":"user","message":{"role":"user","content":"<task-notification>\n<task-id>w1</task-id>\n<summary>Background command grep <teammate-message done</summary>\n</task-notification>"}}"#,
    );
    assert_eq!(
        r.classify(&ClassifyCtx::top_level()),
        vec![Class::NotificationBackgroundCommand]
    );
    assert!(r.direction(&ClassifyCtx::top_level()).is_none());
}

#[test]
fn classify_cross_family_notification_and_teammate_union() {
    // M3b: a record carrying a REAL <task-notification> section AND a REAL <teammate-message>
    // section (outside the notification span) → UNION both families' labels.
    let r = parse(
        r#"{"type":"user","message":{"role":"user","content":"<task-notification>\n<summary>Background command build completed</summary>\n</task-notification>\nAnother Claude session sent a message:\n<teammate-message teammate_id=\"peer\">heads up, merged</teammate-message>"}}"#,
    );
    assert_eq!(
        r.classify(&ClassifyCtx::top_level()),
        vec![Class::NotificationBackgroundCommand, Class::CommInbox]
    );
}

#[test]
fn classify_cross_family_notification_result_plus_teammate_signal() {
    // M3b: notification-with-<result> (→ notification + G1 inbox) AND a teammate idle-signal
    // section after it → [notification, inbox, signal] (inbox deduped to one).
    let r = parse(
        r#"{"type":"user","message":{"role":"user","content":"<task-notification>\n<summary>Dynamic workflow deploy completed</summary>\n<result>done</result>\n</task-notification>\n<teammate-message teammate_id=\"peer\">\n{\"type\":\"idle_notification\",\"from\":\"peer\"}\n</teammate-message>"}}"#,
    );
    assert_eq!(
        r.classify(&ClassifyCtx::top_level()),
        vec![
            Class::NotificationWorkflow,
            Class::CommInbox,
            Class::CommSignal
        ]
    );
}

#[test]
fn classify_elicitation_pending_and_resolved() {
    let pending = parse(
        r#"{"type":"assistant","csift":"elicitation-marker-v1","csiftPhase":"pending","csiftKind":"AskUserQuestion","csiftKey":"k","message":{"role":"assistant","content":[{"type":"tool_use","id":"q","name":"AskUserQuestion","input":{}}]}}"#,
    );
    assert_eq!(
        pending.classify(&ClassifyCtx::top_level()),
        vec![Class::AgentToolUse]
    );
    // An MCP pending marker is a system record with NO tool_use block - still tool.use.
    let mcp = parse(
        r#"{"type":"system","csift":"elicitation-marker-v1","csiftPhase":"pending","csiftKind":"mcp-elicitation","csiftKey":"srv","content":"elicitation"}"#,
    );
    assert_eq!(
        mcp.classify(&ClassifyCtx::top_level()),
        vec![Class::AgentToolUse]
    );
    let resolved = parse(
        r#"{"type":"csift-elicitation-resolved","csift":"elicitation-marker-v1","csiftPhase":"resolved","csiftKey":"k"}"#,
    );
    assert!(
        resolved.classify(&ClassifyCtx::top_level()).is_empty(),
        "a resolved marker is a pairing artifact, no label"
    );
}

#[test]
fn classify_unmodeled_records_are_empty() {
    // A system away_summary is outside the taxonomy → no labels (never crash).
    let sys = parse(r#"{"type":"system","subtype":"away_summary","content":"gone 5m"}"#);
    assert!(sys.classify(&ClassifyCtx::top_level()).is_empty());
    // A metadata-only record likewise.
    let meta = parse(r#"{"type":"last-prompt","leafUuid":"x"}"#);
    assert!(meta.classify(&ClassifyCtx::top_level()).is_empty());
    // An image-only user record (no text, no tool_result) → empty.
    let img = parse(
        r#"{"type":"user","message":{"role":"user","content":[{"type":"image","source":{}}]}}"#,
    );
    assert!(img.classify(&ClassifyCtx::top_level()).is_empty());
}