brokk-sessionwiki 0.30.1

Find, search, and read every AI coding session you've ever had - across Claude Code, Codex, Gemini CLI, OpenCode, Cline, and more.
Documentation
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
//! Golden-file tests for the session adapters.
//!
//! These guard the most fragile part of the project: parsing three different
//! JSONL/JSON schemas that drift between tool versions. Each fixture under
//! `tests/fixtures/<tool>/` is a small, realistic session including the edge
//! cases the parsers are supposed to handle (boilerplate dropping, subagent
//! detection, malformed lines, multi-block content, skipped roles).
//!
//! When a tool changes its format, a fixture from the new version plus an
//! assertion here is the cleanest possible bug report and regression test.

use sessionwiki::adapters;
use sessionwiki::model::Session;
use std::path::PathBuf;

fn fixture(rel: &str) -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests/fixtures")
        .join(rel)
}

fn parse(tool: &str, rel: &str) -> Session {
    let adapter = adapters::by_name(tool).expect("adapter exists");
    adapter.parse(&fixture(rel)).expect("parse succeeds")
}

fn roles(s: &Session) -> Vec<&'static str> {
    s.messages.iter().map(|m| m.role.label()).collect()
}

#[test]
fn claude_main_session() {
    let s = parse(
        "claude-code",
        "claude-code/proj-a/0a000000-0000-4000-8000-000000000001.jsonl",
    );

    // Title comes from the summary line, not the first user message.
    assert_eq!(s.title, "Fix CORS preflight failing on /auth routes");
    assert_eq!(s.tool, "claude-code");
    assert_eq!(s.project, "/home/dev/proj-a");
    assert!(!s.subagent);

    // A malformed line and the isMeta:true boilerplate user turn are dropped;
    // tool_use (Bash, two Edits, a Write) and tool_result all become Tool
    // messages.
    assert_eq!(
        roles(&s),
        [
            "user",
            "assistant",
            "tool",
            "tool",
            "tool",
            "tool",
            "tool",
            "assistant"
        ]
    );

    // The dropped boilerplate must not leak into any message.
    assert!(s
        .messages
        .iter()
        .all(|m| !m.text.contains("harness boilerplate")));

    // First user prompt and final assistant message survive intact.
    assert!(s.messages[0].text.contains("Preflight requests"));
    assert!(s.messages.iter().any(|m| m.text.contains("14 tests pass")));

    // Provenance: Edit/Write file_path is extracted, the repeated edit to
    // mod.rs is de-duplicated, and the read-only Bash call contributes nothing.
    assert_eq!(
        s.touched,
        [
            "/home/dev/proj-a/src/middleware/mod.rs",
            "/home/dev/proj-a/tests/cors_preflight.rs"
        ]
    );

    assert_eq!(
        s.started.map(|t| t.to_rfc3339()),
        Some("2026-06-09T14:01:00+00:00".to_string())
    );
}

#[test]
fn claude_subagent_is_flagged() {
    let s = parse(
        "claude-code",
        "claude-code/proj-a/0a000000-0000-4000-8000-000000000001/subagents/agent-deadbeef.jsonl",
    );
    assert!(s.subagent, "files under /subagents/ must be flagged");
    assert_eq!(roles(&s), ["user", "assistant"]);
    assert_eq!(s.title, "Search the codebase for all CORS references.");
}

#[test]
fn claude_ai_title_precedes_first_user_message() {
    let s = parse(
        "claude-code",
        "claude-code/proj-a/0a000000-0000-4000-8000-000000000002.jsonl",
    );

    assert_eq!(s.title, "Repair synthetic schema drift");
    assert_eq!(roles(&s), ["user"]);
}

#[test]
fn codex_session_with_schema_variants() {
    let s = parse(
        "codex",
        "codex/rollout-2026-06-11T13-00-00-019eb9b2-1466-7e93-8b85-5b596295e96b.jsonl",
    );

    assert_eq!(s.tool, "codex");
    assert_eq!(s.project, "/home/dev/api-server");
    assert_eq!(s.title, "Write property-based tests for the rate limiter.");

    // environment_context boilerplate dropped; both function_calls (the shell
    // test run and the apply_patch) kept as Tool; function_call_output and
    // reasoning dropped; both response_item and event_msg message shapes parse.
    assert_eq!(
        roles(&s),
        ["user", "tool", "tool", "assistant", "user", "assistant"]
    );
    assert!(s
        .messages
        .iter()
        .all(|m| !m.text.contains("environment_context")));
    assert!(s.messages.iter().all(|m| !m.text.contains("cases passed"))); // function_call_output excluded
    assert!(s.messages.iter().any(|m| m.text.contains("2.1M ops/sec")));

    // Provenance: apply_patch file headers (Update File / Add File) are pulled
    // from the call arguments even though the patch is double-JSON-escaped.
    assert_eq!(
        s.touched,
        ["src/rate_limiter.rs", "tests/rate_limiter_props.rs"]
    );
}

#[test]
fn codex_deduplicates_user_message_schema_variants() {
    let s = parse(
        "codex",
        "codex/rollout-2026-07-13T07-05-40-duplicate-user.jsonl",
    );

    assert_eq!(roles(&s), ["user"]);
    assert_eq!(s.messages[0].text, "Explain the synthetic parser failure.");
}

#[test]
fn codex_drops_current_agents_instructions_boilerplate() {
    let s = parse(
        "codex",
        "codex/rollout-2026-07-13T16-06-13-agents-instructions.jsonl",
    );

    assert_eq!(s.title, "Fix the synthetic parser.");
    assert_eq!(roles(&s), ["user"]);
    assert!(s
        .messages
        .iter()
        .all(|message| !message.text.starts_with("# AGENTS.md instructions")));
}

#[test]
fn codex_keeps_genuinely_repeated_prompts() {
    // Each prompt arrives in BOTH shapes (event_msg + response_item); the
    // dedup must cancel per PAIR, so a user who sends "continue" twice still
    // indexes two user messages - not one.
    let s = parse(
        "codex",
        "codex/rollout-2026-07-13T08-00-00-repeated-prompt.jsonl",
    );

    assert_eq!(roles(&s), ["user", "assistant", "user"]);
    assert_eq!(s.messages[0].text, "continue");
    assert_eq!(s.messages[2].text, "continue");
}

#[test]
fn codex_parses_legacy_bare_messages() {
    let s = parse(
        "codex",
        "codex/rollout-2025-08-25T00-04-27-bare-message.jsonl",
    );

    assert_eq!(roles(&s), ["user", "assistant"]);
    assert_eq!(s.title, "Parse the legacy synthetic rollout.");
}

#[test]
fn codex_indexes_custom_tool_calls_and_their_patched_paths() {
    let s = parse(
        "codex",
        "codex/rollout-2026-07-13T16-05-05-custom-tool-call.jsonl",
    );

    assert_eq!(roles(&s), ["user", "tool"]);
    assert!(s.messages[1].text.starts_with("exec "));
    assert!(s.messages[1].text.contains("src/synthetic.rs"));
    assert_eq!(s.touched, ["src/synthetic.rs"]);
}

#[test]
fn gemini_session() {
    let s = parse(
        "gemini",
        "gemini/myproject/chats/session-2026-06-08T10-00-abcd1234.json",
    );

    assert_eq!(s.tool, "gemini");
    assert_eq!(s.project, "myproject");
    // String content and array-of-blocks content both parse; unknown role
    // ("system") is skipped.
    assert_eq!(roles(&s), ["user", "assistant"]);
    assert!(s.messages[0].text.contains("iframe"));
    assert!(s.messages[1].text.contains("1급 스코프")); // CJK survives intact
    assert_eq!(
        s.ended.map(|t| t.to_rfc3339()),
        Some("2026-06-08T10:00:30+00:00".to_string())
    );
}

#[test]
fn opencode_multi_file_session() {
    // OpenCode splits a session across session/message/part JSON files; the
    // adapter is handed the session file and joins the rest from the store.
    let s = parse("opencode", "opencode/storage/session/proj1/ses_aaa.json");

    assert_eq!(s.tool, "opencode");
    assert_eq!(s.title, "Add retry to the HTTP client"); // from session.title
    assert_eq!(s.project, "/home/dev/myapp"); // session.directory
    assert!(!s.subagent);

    // reasoning parts are dropped; text parts and the edit tool become messages
    // in id order; the malformed part file is skipped without panicking; the
    // patch part contributes no message (only provenance).
    assert_eq!(roles(&s), ["user", "assistant", "tool"]);
    assert!(s.messages[0]
        .text
        .contains("retry with exponential backoff"));
    assert!(s
        .messages
        .iter()
        .any(|m| m.text.contains("3-attempt retry")));
    assert!(s
        .messages
        .iter()
        .all(|m| !m.text.contains("thinking about"))); // reasoning dropped

    // Provenance: edit tool's state.input.filePath + the patch's files list.
    assert_eq!(
        s.touched,
        [
            "/home/dev/myapp/src/http/client.ts",
            "/home/dev/myapp/src/util.ts"
        ]
    );

    // epoch-millis timestamp is parsed (not an RFC3339 string).
    assert_eq!(
        s.started,
        chrono::DateTime::from_timestamp_millis(1718630400000)
    );
}

#[test]
fn cline_xml_and_native_tool_edits() {
    // Cline keeps each task as api_conversation_history.json (the Anthropic
    // Messages array) plus ui_messages.json (timestamps + the title).
    let s = parse(
        "cline",
        "cline/tasks/1749477660000/api_conversation_history.json",
    );

    assert_eq!(s.tool, "cline");
    assert_eq!(s.title, "Add a hello function to src/main.py"); // ui_messages say:"task"
    assert_eq!(s.project, "/home/dev/app"); // from <environment_details>
    assert!(!s.subagent);

    // The <task>/<environment_details> wrappers and the tool-result feedback
    // turn ("[... ] Result:") are stripped; XML and native tool calls both
    // become Tool messages.
    assert_eq!(
        roles(&s),
        [
            "user",
            "assistant",
            "tool",
            "assistant",
            "tool",
            "assistant"
        ]
    );
    assert_eq!(s.messages[0].text, "Add a hello function to src/main.py");
    assert!(s
        .messages
        .iter()
        .all(|m| !m.text.contains("environment_details")));
    assert!(s.messages.iter().all(|m| !m.text.contains("] Result:")));
    assert!(s
        .messages
        .iter()
        .all(|m| !m.text.contains("successfully saved")));

    // Provenance from BOTH the XML <write_to_file><path> and the native
    // tool_use input.path.
    assert_eq!(s.touched, ["src/main.py", "tests/test_main.py"]);

    assert_eq!(
        s.started,
        chrono::DateTime::from_timestamp_millis(1749477660000)
    );
    assert_eq!(
        s.ended,
        chrono::DateTime::from_timestamp_millis(1749477700000)
    );
}

#[test]
fn gajae_jsonl_session() {
    // gajae-code (and upstream Pi) store one JSONL transcript per session: a
    // header line then message lines, with toolCall/arguments content blocks.
    let s = parse(
        "gajae-code",
        "gajae-code/--home--dev--proj--/2025-12-09T00-53-29-825Z_ffae836b-9420-4060-ac13-7745215f90ff.jsonl",
    );

    assert_eq!(s.tool, "gajae-code");
    assert_eq!(s.title, "refactor the parser"); // header title
    assert_eq!(s.project, "/home/dev/proj"); // header cwd
    assert!(!s.subagent);

    // thinking blocks dropped; toolResult -> tool; the malformed line is skipped
    // without panicking.
    assert_eq!(
        roles(&s),
        [
            "user",
            "assistant",
            "tool",
            "tool",
            "assistant",
            "tool",
            "assistant",
            "tool",
            "assistant"
        ]
    );
    assert!(s
        .messages
        .iter()
        .all(|m| !m.text.contains("considering the structure")));

    // Provenance: write (arguments.path) + ast_edit (arguments.paths[]) with the
    // repeat de-duplicated; read is excluded.
    assert_eq!(s.touched, ["src/parse.ts", "src/helper.ts"]);

    // started = header RFC3339; ended = last entry's RFC3339.
    assert_eq!(
        s.started.map(|t| t.to_rfc3339()),
        Some("2025-12-09T00:53:29.825+00:00".to_string())
    );
    assert_eq!(
        s.ended.map(|t| t.to_rfc3339()),
        Some("2025-12-09T00:53:40+00:00".to_string())
    );
}

#[test]
fn continue_session_with_tool_edit() {
    let s = parse(
        "continue",
        "continue/sessions/5f1d2c9a-8b34-4e21-9d6e-7a0c1b2e3f44.json",
    );

    assert_eq!(s.tool, "continue");
    assert_eq!(s.title, "Add retry to the HTTP client");
    assert_eq!(s.project, "/home/alex/projects/acme-api");
    assert!(!s.subagent);

    assert_eq!(roles(&s), ["user", "assistant", "tool", "tool"]);

    // Provenance from toolCallStates[].parsedArgs.filepath.
    assert_eq!(s.touched, ["src/http.ts"]);

    // The session file has no timestamps; started comes from sessions.json's
    // dateCreated (epoch-ms as a string); there is no reliable ended.
    assert_eq!(
        s.started,
        chrono::DateTime::from_timestamp_millis(1718600000000)
    );
    assert_eq!(s.ended, None);
}

#[test]
fn gptme_session() {
    let s = parse(
        "gptme",
        "gptme/fix-cors-bug-20260608-abcd/conversation.jsonl",
    );

    assert_eq!(s.tool, "gptme");
    // Project label is the session directory name (the slug), not a cwd.
    assert_eq!(s.project, "fix-cors-bug-20260608-abcd");

    // Pinned system-prompt line and bare system role are both dropped;
    // the malformed JSON line is silently skipped.
    assert_eq!(roles(&s), ["user", "assistant", "user", "assistant"]);

    // Naive timestamp fallback (no UTC offset) is parsed and assumed UTC.
    assert_eq!(
        s.started,
        chrono::DateTime::parse_from_rfc3339("2026-06-08T10:00:01Z")
            .ok()
            .map(|t| t.with_timezone(&chrono::Utc))
    );

    // Title comes from first user message.
    assert!(s.messages[0].text.contains("CORS preflight"));
}

#[test]
fn gptme_malformed_lines_do_not_panic() {
    // A fixture with a bad line must not panic — it is silently skipped.
    let adapter = adapters::by_name("gptme").unwrap();
    let result = adapter.parse(&fixture(
        "gptme/fix-cors-bug-20260608-abcd/conversation.jsonl",
    ));
    assert!(result.is_ok());
}

#[test]
fn missing_file_errors_without_panicking() {
    let adapter = adapters::by_name("codex").unwrap();
    assert!(adapter
        .parse(&fixture("codex/does-not-exist.jsonl"))
        .is_err());
}

#[test]
fn aider_golden() {
    // aider is a shared store: drive parse_key with `<path>\u{1f}<run-index>`.
    let path = fixture("aider/myrepo/.aider.chat.history.md");
    let adapter = adapters::by_name("aider").unwrap();
    let s = adapter
        .parse_key(&format!("{}\u{1f}0", path.to_string_lossy()))
        .unwrap();
    assert_eq!(s.tool, "aider");
    assert_eq!(s.title, "add a retry to the webhook");
    let roles: Vec<&str> = s.messages.iter().map(|m| m.role.label()).collect();
    assert_eq!(roles, vec!["user", "assistant", "tool"]);
    assert_eq!(s.touched, vec!["src/webhook.py", "src/retry.py"]); // dry-run ignored
    assert!(s.started.is_some());
    assert!(s.messages.iter().all(|m| m.ts.is_none()));
    // a bad run index errors, never panics
    assert!(adapter
        .parse_key(&format!("{}\u{1f}99", path.to_string_lossy()))
        .is_err());
}

#[test]
fn every_adapter_is_addressable_by_name() {
    for tool in [
        "claude-code",
        "codex",
        "gemini",
        "opencode",
        "cline",
        "roo-code",
        "kilo-code",
        "gajae-code",
        "continue",
        "gptme",
        "aider",
    ] {
        assert!(adapters::by_name(tool).is_some(), "{tool} should resolve");
    }
    assert!(adapters::by_name("nonexistent").is_none());
}

#[test]
fn prodex_consult_task_becomes_a_two_message_session() {
    let s = parse(
        "prodex",
        "prodex/repo-a/.bridge/tasks/task_20260707_090000_gpt-pro-consult.json",
    );
    assert_eq!(s.tool, "prodex");
    // sha256("task_20260707_090000_gpt-pro-consult")[..12] - the uniform
    // short-id shape every tool gets in the index.
    assert_eq!(s.id, "567f374d7a70");
    // The question is the title - prodex auto-titles every consult
    // "GPT Pro consult", which would make a list of them indistinguishable.
    assert!(s.title.starts_with("Should the retry loop"), "{}", s.title);
    assert_eq!(roles(&s), ["user", "assistant"]);
    assert!(s.messages[0].text.contains("exponential backoff"), "prompt");
    // The full pro-consult artifact wins over the truncated summary.
    assert!(
        s.messages[1].text.contains("thundering herds"),
        "artifact text used as the answer: {}",
        s.messages[1].text
    );
    assert!(s.started.is_some(), "claimed_at parsed");
    // Project = the repo that owns the .bridge.
    assert!(s.project.ends_with("repo-a"), "{}", s.project);
}

#[test]
fn prodex_pending_task_is_a_one_message_session() {
    let s = parse(
        "prodex",
        "prodex/repo-a/.bridge/tasks/task_20260707_100000_open-question.json",
    );
    assert_eq!(roles(&s), ["user"]);
    assert!(s.started.is_some(), "falls back to the id timestamp");
}

#[test]
fn prodex_thread_url_comes_from_the_bridge_session() {
    let task = fixture("prodex/repo-a/.bridge/tasks/task_20260707_090000_gpt-pro-consult.json");
    let url = sessionwiki::adapters::prodex_thread_url(&task);
    assert_eq!(
        url.as_deref(),
        Some("https://chatgpt.com/c/aaaa1111-2222-3333-4444-555566667777")
    );
    // A non-chatgpt URL in a tampered file is never surfaced.
    let dir = tempfile::tempdir().unwrap();
    let tasks = dir.path().join(".bridge/tasks");
    let sessions = dir.path().join(".bridge/sessions");
    std::fs::create_dir_all(&tasks).unwrap();
    std::fs::create_dir_all(&sessions).unwrap();
    let t = tasks.join("task_x.json");
    std::fs::write(&t, "{}").unwrap();
    std::fs::write(
        sessions.join("s.json"),
        r#"{"thread":"file:///etc/passwd"}"#,
    )
    .unwrap();
    assert_eq!(sessionwiki::adapters::prodex_thread_url(&t), None);
}

// The crash path the durable ledger exists for: the answer artifact was
// written but the result JSON was not. The answer must still be indexed.
#[test]
fn prodex_artifact_without_result_is_still_the_answer() {
    let s = parse(
        "prodex",
        "prodex/repo-a/.bridge/tasks/task_20260707_110000_crash-path.json",
    );
    assert_eq!(roles(&s), ["user", "assistant"]);
    assert!(
        s.messages[1].text.contains("logical shards"),
        "{}",
        s.messages[1].text
    );
}

// AUDIT-HIGH: a crafted/corrupt task id with a multibyte char at a slice
// boundary must not panic (an uncaught parse panic aborts the WHOLE indexer -
// one bad .bridge file would brick every list/search/scan).
#[test]
fn prodex_multibyte_task_id_never_panics() {
    let dir = tempfile::tempdir().unwrap();
    let tasks = dir.path().join(".bridge/tasks");
    std::fs::create_dir_all(&tasks).unwrap();
    let p = tasks.join("bad.json");
    std::fs::write(
        &p,
        r#"{"id":"task_12345é7_123456","title":"t","prompt":"p"}"#,
    )
    .unwrap();
    let adapter = sessionwiki::adapters::by_name("prodex").unwrap();
    let s = adapter.parse(&p).expect("parse must not panic");
    assert!(
        s.started.is_none(),
        "unparseable stamp -> None, not a crash"
    );
}