sessionwiki 0.34.0

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
//! Search semantics and tokenizer configuration against a disposable index.

use rusqlite::{params, Connection};
use sessionwiki::adapters::{Adapter, Discovered};
use sessionwiki::index;
use sessionwiki::model::{Message, Role, Session};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};

static LOCK: Mutex<()> = Mutex::new(());

fn fresh_index() -> (Connection, PathBuf) {
    let dir = std::env::temp_dir().join(format!("sessionwiki-test-search-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    std::env::set_var("SESSIONWIKI_DATA", &dir);
    let conn = index::open().unwrap();
    (conn, dir)
}

fn seed_message(conn: &Connection, id: &str, text: &str) {
    seed_role_message(conn, id, Role::User, text);
}

fn seed_role_message(conn: &Connection, id: &str, role: Role, text: &str) {
    conn.execute(
        "INSERT OR REPLACE INTO files
         (path, mtime, size, session_id, tool, project, title, started, ended, msg_count, kind)
         VALUES (?1, 0, 0, ?2, 'codex', '/search', ?2, '2026-06-10T10:00:00+00:00',
                 '2026-06-10T10:00:00+00:00', 1, 'main')",
        params![format!("/fake/{id}.jsonl"), id],
    )
    .unwrap();
    conn.execute(
        "INSERT INTO messages(session_id, role, text) VALUES (?1, ?2, ?3)",
        params![id, role.label(), text],
    )
    .unwrap();
    let message_id = conn.last_insert_rowid();
    conn.execute(
        "INSERT INTO msgs(rowid, text) VALUES (?1, ?2)",
        params![message_id, text],
    )
    .unwrap();
}

#[test]
fn role_filter_excludes_tool_only_matches_and_leaves_default_search_unchanged() {
    let _guard = LOCK.lock().unwrap();
    let (conn, _) = fresh_index();
    seed_role_message(
        &conn,
        "tool-only",
        Role::Tool,
        "unique needle from command output",
    );

    let hits = index::search(&conn, "unique needle", 10, None, None).unwrap();
    assert_eq!(hits.len(), 1);
    assert_eq!(hits[0].role, "tool");

    let conversation = [Role::User, Role::Assistant];
    assert!(
        index::search_with_roles(&conn, "unique needle", 10, None, None, Some(&conversation),)
            .unwrap()
            .is_empty()
    );
}

#[test]
fn role_filter_returns_the_best_allowed_message_for_each_session() {
    let _guard = LOCK.lock().unwrap();
    let (conn, _) = fresh_index();
    seed_role_message(&conn, "mixed-roles", Role::Tool, "beacon");
    seed_role_message(&conn, "mixed-roles", Role::Assistant, "assistant beacon");

    let unfiltered = index::search(&conn, "beacon", 10, None, None).unwrap();
    assert_eq!(unfiltered[0].role, "tool");

    let conversation = [Role::User, Role::Assistant];
    let hits =
        index::search_with_roles(&conn, "beacon", 10, None, None, Some(&conversation)).unwrap();
    assert_eq!(hits.len(), 1);
    assert_eq!(hits[0].row.session_id, "mixed-roles");
    assert_eq!(hits[0].role, "assistant");
    assert!(
        hits[0].snippet.contains("assistant"),
        "{:?}",
        hits[0].snippet
    );
}

#[test]
fn role_filter_runs_before_the_fts_candidate_cap() {
    let _guard = LOCK.lock().unwrap();
    let (mut conn, _) = fresh_index();
    let tx = conn.transaction().unwrap();
    let mut insert_file = tx
        .prepare_cached(
            "INSERT INTO files
             (path, mtime, size, session_id, tool, project, title, started, ended, msg_count, kind)
             VALUES (?1, 0, 0, ?2, 'codex', '/search', ?2, '2026-06-10T10:00:00+00:00',
                     '2026-06-10T10:00:00+00:00', 1, 'main')",
        )
        .unwrap();
    let mut insert_message = tx
        .prepare_cached(
            "INSERT INTO messages(session_id, role, text) VALUES (?1, 'tool', 'rankneedle')",
        )
        .unwrap();
    let mut insert_fts = tx
        .prepare_cached("INSERT INTO msgs(rowid, text) VALUES (?1, 'rankneedle')")
        .unwrap();
    for n in 0..4_001 {
        let id = format!("tool-overflow-{n:04}");
        insert_file
            .execute(params![format!("/fake/{id}.jsonl"), id])
            .unwrap();
        insert_message.execute(params![id]).unwrap();
        insert_fts.execute(params![tx.last_insert_rowid()]).unwrap();
    }
    drop(insert_fts);
    drop(insert_message);
    drop(insert_file);
    tx.commit().unwrap();

    let long_context = format!("{}rankneedle", "background ".repeat(500));
    seed_role_message(&conn, "assistant-after-cap", Role::Assistant, &long_context);

    let unfiltered = index::search(&conn, "rankneedle", 10, None, None).unwrap();
    assert!(unfiltered
        .iter()
        .all(|hit| hit.row.session_id != "assistant-after-cap"));

    let assistant = [Role::Assistant];
    let filtered =
        index::search_with_roles(&conn, "rankneedle", 10, None, None, Some(&assistant)).unwrap();
    assert_eq!(filtered.len(), 1);
    assert_eq!(filtered[0].row.session_id, "assistant-after-cap");
}

#[test]
fn role_filter_applies_to_the_short_term_like_path() {
    let _guard = LOCK.lock().unwrap();
    let (conn, _) = fresh_index();
    seed_role_message(
        &conn,
        "short-term",
        Role::Assistant,
        "xy in the assistant reply",
    );
    seed_role_message(&conn, "short-term", Role::Tool, "xy in newer tool output");

    let assistant = [Role::Assistant];
    let hits = index::search_with_roles(&conn, "xy", 10, None, None, Some(&assistant)).unwrap();
    assert_eq!(hits.len(), 1);
    assert_eq!(hits[0].role, "assistant");
    assert!(hits[0].snippet.contains("assistant reply"));
}

#[test]
fn cli_rejects_roles_the_index_does_not_store() {
    let output = std::process::Command::new(env!("CARGO_BIN_EXE_sessionwiki"))
        .args(["search", "needle", "--role", "system", "--no-sync"])
        .output()
        .unwrap();

    assert!(!output.status.success());
    assert!(
        String::from_utf8_lossy(&output.stderr).contains("role must be user, assistant, or tool"),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn cli_accepts_comma_separated_search_roles() {
    let data = tempfile::tempdir().unwrap();
    let output = std::process::Command::new(env!("CARGO_BIN_EXE_sessionwiki"))
        .args(["search", "needle", "--role", "user,assistant", "--no-sync"])
        .env("SESSIONWIKI_DATA", data.path())
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn new_index_defaults_to_trigram_and_creates_external_content_fts() {
    let _guard = LOCK.lock().unwrap();
    let (conn, _) = fresh_index();

    assert_eq!(index::tokenizer_spec(&conn).unwrap(), "trigram");
    let ddl: String = conn
        .query_row(
            "SELECT sql FROM sqlite_master WHERE name = 'msgs'",
            [],
            |row| row.get(0),
        )
        .unwrap();
    assert!(ddl.contains("content='messages'"), "{ddl}");
    assert!(ddl.contains("content_rowid='id'"), "{ddl}");
    assert!(ddl.contains("tokenize='trigram'"), "{ddl}");
    let version: i64 = conn
        .pragma_query_value(None, "user_version", |row| row.get(0))
        .unwrap();
    assert_eq!(version, index::SCHEMA_VERSION);
}

#[test]
fn opening_current_index_succeeds_while_another_connection_holds_write_lock() {
    let _guard = LOCK.lock().unwrap();
    let (conn, _) = fresh_index();
    seed_message(&conn, "writer-lock", "searchable result");

    let blocker = Connection::open(index::db_path().unwrap()).unwrap();
    blocker
        .busy_timeout(std::time::Duration::from_millis(5_000))
        .unwrap();
    blocker.execute_batch("BEGIN IMMEDIATE").unwrap();

    let open_and_search = (|| -> anyhow::Result<Vec<index::Hit>> {
        let reopened = index::open()?;
        index::search(&reopened, "searchable result", 10, None, None)
    })();
    blocker.execute_batch("ROLLBACK").unwrap();

    let hits = open_and_search.expect("current index open and search should not need a write lock");
    assert!(hits.iter().any(|hit| hit.row.session_id == "writer-lock"));
}

#[test]
fn unquoted_terms_match_when_separated_in_one_message() {
    let _guard = LOCK.lock().unwrap();
    let (conn, _) = fresh_index();
    seed_message(
        &conn,
        "separated",
        "alpha appears early; the matching word comes much later: beta.",
    );

    let hits = index::search(&conn, "alpha beta", 10, None, None).unwrap();
    assert_eq!(hits.len(), 1);
    assert_eq!(hits[0].row.session_id, "separated");
}

#[test]
fn trigram_phrases_preserve_whitespace_and_require_adjacency() {
    let _guard = LOCK.lock().unwrap();
    let (conn, _) = fresh_index();
    seed_message(&conn, "wrapped", "before alpha\n  beta after");
    seed_message(&conn, "adjacent", "before alpha beta after");
    seed_message(&conn, "apart", "before alpha elsewhere beta after");

    let hits = index::search(&conn, "\"alpha beta\"", 10, None, None).unwrap();
    let ids: Vec<_> = hits.iter().map(|hit| hit.row.session_id.as_str()).collect();
    assert_eq!(ids, ["adjacent"]);

    let hits = index::search(&conn, "\"alpha beta", 10, None, None).unwrap();
    let ids: Vec<_> = hits.iter().map(|hit| hit.row.session_id.as_str()).collect();
    assert_eq!(
        ids,
        ["adjacent"],
        "an unmatched quote opens a phrase to end"
    );

    let hits = index::search(&conn, "\"alpha\n  beta\"", 10, None, None).unwrap();
    let ids: Vec<_> = hits.iter().map(|hit| hit.row.session_id.as_str()).collect();
    assert_eq!(ids, ["wrapped"]);
}

#[test]
fn terms_in_different_messages_of_one_session_do_not_match() {
    let _guard = LOCK.lock().unwrap();
    let (conn, _) = fresh_index();
    seed_message(&conn, "split", "alpha is in this message");
    seed_message(&conn, "split", "beta is in another message");

    assert!(index::search(&conn, "alpha beta", 10, None, None)
        .unwrap()
        .is_empty());
}

#[test]
fn mixed_short_long_and_all_short_terms_are_anded() {
    let _guard = LOCK.lock().unwrap();
    let (conn, _) = fresh_index();
    seed_message(&conn, "mixed", "the DB reconnect can retry");
    let hits = index::search(&conn, "DB retry", 10, None, None).unwrap();
    assert_eq!(hits.len(), 1);
    assert_eq!(hits[0].row.session_id, "mixed");

    seed_message(&conn, "all-short", "use db with id");
    seed_message(&conn, "split-short", "db in one message");
    seed_message(&conn, "split-short", "id in another message");
    let hits = index::search(&conn, "db id", 10, None, None).unwrap();
    let ids: Vec<_> = hits.iter().map(|hit| hit.row.session_id.as_str()).collect();
    assert_eq!(ids, ["all-short"]);
}

#[test]
fn quotes_and_fts_operator_words_are_searched_as_text() {
    let _guard = LOCK.lock().unwrap();
    let (conn, _) = fresh_index();
    seed_message(
        &conn,
        "operators",
        "literal AND OR NEAR * marker; say \"hello\" here",
    );

    let hits = index::search(&conn, "AND \"OR\" NEAR *", 10, None, None).unwrap();
    assert_eq!(hits.len(), 1);
    assert_eq!(hits[0].row.session_id, "operators");

    let hits = index::search(&conn, "\"say \"\"hello\"\"\"", 10, None, None).unwrap();
    assert_eq!(hits.len(), 1, "escaped quotes remain searchable text");
    assert_eq!(hits[0].row.session_id, "operators");
}

#[test]
fn snippets_highlight_all_short_terms_on_the_like_path() {
    let _guard = LOCK.lock().unwrap();
    let (conn, _) = fresh_index();
    seed_message(
        &conn,
        "snippet",
        "first words db among records and id near the end",
    );

    let hits = index::search(&conn, "db id", 10, None, None).unwrap();
    assert_eq!(hits.len(), 1);
    assert!(hits[0].snippet.contains("\u{2}db\u{3}"));
    assert!(hits[0].snippet.contains("\u{2}id\u{3}"));
}

#[test]
fn fts_snippets_highlight_terms_and_exact_phrase_whitespace() {
    let _guard = LOCK.lock().unwrap();
    let (conn, _) = fresh_index();
    seed_message(&conn, "snippet-fts", "first alpha\n  beta");

    let hits = index::search(&conn, "first \"alpha\n  beta\"", 10, None, None).unwrap();
    assert_eq!(hits.len(), 1);
    assert!(hits[0].snippet.contains("\u{2}first\u{3}"));
    assert!(hits[0].snippet.contains("\u{2}alpha\n  beta\u{3}"));
}

struct SearchAdapter {
    source: PathBuf,
    parses: Arc<AtomicUsize>,
}

impl Adapter for SearchAdapter {
    fn name(&self) -> &'static str {
        "search-fixture"
    }

    fn root(&self) -> Option<PathBuf> {
        self.source.parent().map(Path::to_path_buf)
    }

    fn discover(&self) -> Discovered {
        vec![self.source.clone()].into()
    }

    fn parse(&self, path: &Path) -> anyhow::Result<Session> {
        self.parses.fetch_add(1, Ordering::SeqCst);
        Ok(Session {
            id: "reindexed-session".into(),
            tool: self.name(),
            path: path.into(),
            project: "/search".into(),
            started: None,
            ended: None,
            title: "reindex fixture".into(),
            subagent: false,
            messages: vec![Message {
                role: Role::User,
                text: std::fs::read_to_string(path)?,
                ts: None,
                tool: None,
            }],
            touched: vec![],
            edits: vec![],
        })
    }
}

fn adapter(source: PathBuf, parses: Arc<AtomicUsize>) -> Vec<Box<dyn Adapter>> {
    vec![Box::new(SearchAdapter { source, parses })]
}

#[test]
fn unicode61_persists_rebuilds_fts_without_parsing_and_matches_wrapped_phrase() {
    let _guard = LOCK.lock().unwrap();
    let (mut conn, dir) = fresh_index();
    let source = dir.join("session.txt");
    std::fs::write(&source, "before alpha\n  beta after").unwrap();
    let parses = Arc::new(AtomicUsize::new(0));
    let adapters = adapter(source.clone(), parses.clone());

    index::sync_with(&mut conn, &adapters, None).unwrap();
    let parse_count = parses.load(Ordering::SeqCst);
    assert_eq!(parse_count, 1);
    assert!(index::set_tokenizer_spec(&conn, "unicode61").unwrap());
    assert!(!index::set_tokenizer_spec(&conn, "unicode61").unwrap());
    assert_eq!(parses.load(Ordering::SeqCst), parse_count);

    let raw: String = conn
        .query_row(
            "SELECT text FROM messages WHERE session_id = 'reindexed-session'",
            [],
            |row| row.get(0),
        )
        .unwrap();
    assert_eq!(raw, "before alpha\n  beta after");
    drop(conn);

    let conn = index::open().unwrap();
    assert_eq!(index::tokenizer_spec(&conn).unwrap(), "unicode61");
    let hits = index::search(&conn, "\"alpha beta\"", 10, None, None).unwrap();
    assert_eq!(hits.len(), 1);
    assert_eq!(hits[0].row.session_id, "reindexed-session");
}

#[test]
fn unicode61_finds_short_words_through_fts_not_substring_like() {
    let _guard = LOCK.lock().unwrap();
    let (conn, _) = fresh_index();
    seed_message(&conn, "word", "the id is here");
    seed_message(&conn, "substring", "identity is here");
    index::set_tokenizer_spec(&conn, "unicode61").unwrap();

    let hits = index::search(&conn, "id", 10, None, None).unwrap();
    let ids: Vec<_> = hits.iter().map(|hit| hit.row.session_id.as_str()).collect();
    assert_eq!(ids, ["word"]);
}

#[test]
fn unicode61_tokenless_punctuation_term_returns_no_hits_without_sql_error() {
    let _guard = LOCK.lock().unwrap();
    let (conn, _) = fresh_index();
    seed_message(&conn, "punctuation", "ordinary searchable words");
    index::set_tokenizer_spec(&conn, "unicode61").unwrap();

    assert!(index::search(&conn, "***", 10, None, None)
        .unwrap()
        .is_empty());
}

#[test]
fn invalid_tokenizer_leaves_existing_index_and_setting_unchanged() {
    let _guard = LOCK.lock().unwrap();
    let (conn, _) = fresh_index();
    seed_message(&conn, "preserved", "searchable words remain");
    let ddl: String = conn
        .query_row(
            "SELECT sql FROM sqlite_master WHERE name = 'msgs'",
            [],
            |row| row.get(0),
        )
        .unwrap();

    assert!(index::set_tokenizer_spec(&conn, "unicode61 unknown_option").is_err());
    assert_eq!(index::tokenizer_spec(&conn).unwrap(), "trigram");
    let after: String = conn
        .query_row(
            "SELECT sql FROM sqlite_master WHERE name = 'msgs'",
            [],
            |row| row.get(0),
        )
        .unwrap();
    assert_eq!(after, ddl);
    assert_eq!(
        index::search(&conn, "searchable words", 10, None, None)
            .unwrap()
            .len(),
        1
    );
}

#[test]
fn tokenizer_spec_with_quotes_cannot_inject_sql() {
    let _guard = LOCK.lock().unwrap();
    let (conn, _) = fresh_index();
    seed_message(&conn, "preserved", "searchable words remain");

    let malicious = "trigram'); DROP TABLE messages; --";
    assert!(index::set_tokenizer_spec(&conn, malicious).is_err());
    assert_eq!(index::tokenizer_spec(&conn).unwrap(), "trigram");
    let messages: i64 = conn
        .query_row("SELECT count(*) FROM messages", [], |row| row.get(0))
        .unwrap();
    assert_eq!(messages, 1);

    let quoted = "unicode61 tokenchars '._-'";
    assert!(index::set_tokenizer_spec(&conn, quoted).unwrap());
    assert_eq!(index::tokenizer_spec(&conn).unwrap(), quoted);
    let messages: i64 = conn
        .query_row("SELECT count(*) FROM messages", [], |row| row.get(0))
        .unwrap();
    assert_eq!(messages, 1);
}

#[test]
fn cache_rebuild_uses_the_stored_tokenizer() {
    let _guard = LOCK.lock().unwrap();
    let (conn, _) = fresh_index();
    index::set_tokenizer_spec(&conn, "unicode61").unwrap();
    conn.pragma_update(None, "user_version", 0i64).unwrap();
    drop(conn);

    let conn = index::open().unwrap();
    assert_eq!(index::tokenizer_spec(&conn).unwrap(), "unicode61");
    let ddl: String = conn
        .query_row(
            "SELECT sql FROM sqlite_master WHERE name = 'msgs'",
            [],
            |row| row.get(0),
        )
        .unwrap();
    assert!(ddl.contains("tokenize='unicode61'"), "{ddl}");
}

#[test]
fn mcp_short_query_guard_depends_on_tokenizer() {
    let _guard = LOCK.lock().unwrap();
    let (conn, _) = fresh_index();
    assert!(index::mcp_search_guard(&conn, "xy").unwrap().is_some());
    assert!(index::mcp_search_guard(&conn, "xyz").unwrap().is_none());
    assert!(index::mcp_search_guard(&conn, "   ").unwrap().is_some());

    index::set_tokenizer_spec(&conn, "unicode61").unwrap();
    assert!(index::mcp_search_guard(&conn, "xy").unwrap().is_none());
    assert!(index::mcp_search_guard(&conn, "   ").unwrap().is_some());
}

#[test]
fn reindexing_a_session_removes_its_old_external_content_rows() {
    let _guard = LOCK.lock().unwrap();
    let (mut conn, dir) = fresh_index();
    let source = dir.join("session.txt");
    std::fs::write(&source, "old material phrase").unwrap();
    let adapters = adapter(source.clone(), Arc::new(AtomicUsize::new(0)));

    index::sync_with(&mut conn, &adapters, None).unwrap();
    assert_eq!(
        index::search(&conn, "old phrase", 10, None, None)
            .unwrap()
            .len(),
        1
    );
    std::fs::write(&source, "new material now").unwrap();
    index::sync_with(&mut conn, &adapters, None).unwrap();

    assert!(index::search(&conn, "old phrase", 10, None, None)
        .unwrap()
        .is_empty());
    assert_eq!(
        index::search(&conn, "new material", 10, None, None)
            .unwrap()
            .len(),
        1
    );
}