semantic-memory 0.5.1

Local-first hybrid semantic search (SQLite + FTS5 + usearch 2.25) with bitemporal truth and typed receipts
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
//! Session and message CRUD for conversation storage.

#[cfg(feature = "hnsw")]
use crate::db::{enqueue_pending_index_op, PendingIndexOpKind};
use crate::db::{parse_optional_json, parse_role, with_transaction};
use crate::error::MemoryError;
use crate::quantize::{self, Quantizer};
use crate::search;
use crate::types::{Message, Role, SearchResult, SearchSourceType, Session};
use crate::{as_str_slice, merge_trace_ctx, to_owned_string_vec, MemoryStore};
use rusqlite::{params, Connection};
use stack_ids::TraceCtx;

/// Create a new conversation session and return its UUID.
pub fn create_session(
    conn: &Connection,
    channel: &str,
    metadata: Option<&serde_json::Value>,
) -> Result<String, MemoryError> {
    let id = uuid::Uuid::new_v4().to_string();
    let metadata_str = metadata.map(|m| m.to_string());
    conn.execute(
        "INSERT INTO sessions (id, channel, metadata) VALUES (?1, ?2, ?3)",
        params![id, channel, metadata_str],
    )?;
    Ok(id)
}

/// Append a message to a session without search indexes.
#[allow(dead_code)]
pub fn add_message(
    conn: &Connection,
    session_id: &str,
    role: Role,
    content: &str,
    token_count: Option<u32>,
    metadata: Option<&serde_json::Value>,
) -> Result<i64, MemoryError> {
    let exists: bool = conn.query_row(
        "SELECT EXISTS(SELECT 1 FROM sessions WHERE id = ?1)",
        params![session_id],
        |row| row.get(0),
    )?;
    if !exists {
        return Err(MemoryError::SessionNotFound(session_id.to_string()));
    }

    let metadata_str = metadata.map(|m| m.to_string());
    with_transaction(conn, |tx| {
        tx.execute(
            "INSERT INTO messages (session_id, role, content, token_count, metadata)
             VALUES (?1, ?2, ?3, ?4, ?5)",
            params![
                session_id,
                role.as_str(),
                content,
                token_count,
                metadata_str
            ],
        )?;
        let msg_id = tx.last_insert_rowid();
        tx.execute(
            "UPDATE sessions SET updated_at = datetime('now') WHERE id = ?1",
            params![session_id],
        )?;
        Ok(msg_id)
    })
}

/// Get the most recent N messages from a session in chronological order.
pub fn get_recent_messages(
    conn: &Connection,
    session_id: &str,
    limit: usize,
) -> Result<Vec<Message>, MemoryError> {
    let mut stmt = conn.prepare(
        "SELECT id, session_id, role, content, token_count, created_at, metadata
         FROM messages
         WHERE session_id = ?1
         ORDER BY created_at DESC, id DESC
         LIMIT ?2",
    )?;

    let mut messages: Vec<Message> = stmt
        .query_map(params![session_id, limit as i64], |row| {
            Ok((
                row.get::<_, i64>(0)?,
                row.get::<_, String>(1)?,
                row.get::<_, String>(2)?,
                row.get::<_, String>(3)?,
                row.get::<_, Option<u32>>(4)?,
                row.get::<_, String>(5)?,
                row.get::<_, Option<String>>(6)?,
            ))
        })?
        .collect::<Result<Vec<_>, _>>()?
        .into_iter()
        .map(
            |(id, session_id, role_raw, content, token_count, created_at, metadata_raw)| {
                Ok(Message {
                    role: parse_role("messages", &id.to_string(), &role_raw)?,
                    metadata: parse_optional_json(
                        "messages",
                        &id.to_string(),
                        "metadata",
                        metadata_raw.as_deref(),
                    )?,
                    id,
                    session_id,
                    content,
                    token_count,
                    created_at,
                })
            },
        )
        .collect::<Result<Vec<_>, MemoryError>>()?;

    messages.reverse();
    Ok(messages)
}

/// Get messages from a session while staying under the token budget.
pub fn get_messages_within_budget(
    conn: &Connection,
    session_id: &str,
    max_tokens: u32,
) -> Result<Vec<Message>, MemoryError> {
    let mut stmt = conn.prepare(
        "SELECT id, session_id, role, content, token_count, created_at, metadata
         FROM messages
         WHERE session_id = ?1
         ORDER BY created_at DESC, id DESC",
    )?;

    let all_messages: Vec<Message> = stmt
        .query_map(params![session_id], |row| {
            Ok((
                row.get::<_, i64>(0)?,
                row.get::<_, String>(1)?,
                row.get::<_, String>(2)?,
                row.get::<_, String>(3)?,
                row.get::<_, Option<u32>>(4)?,
                row.get::<_, String>(5)?,
                row.get::<_, Option<String>>(6)?,
            ))
        })?
        .collect::<Result<Vec<_>, _>>()?
        .into_iter()
        .map(
            |(id, session_id, role_raw, content, token_count, created_at, metadata_raw)| {
                Ok(Message {
                    role: parse_role("messages", &id.to_string(), &role_raw)?,
                    metadata: parse_optional_json(
                        "messages",
                        &id.to_string(),
                        "metadata",
                        metadata_raw.as_deref(),
                    )?,
                    id,
                    session_id,
                    content,
                    token_count,
                    created_at,
                })
            },
        )
        .collect::<Result<Vec<_>, MemoryError>>()?;

    let mut collected = Vec::new();
    let mut total_tokens = 0u32;
    for msg in all_messages {
        let msg_tokens = msg
            .token_count
            .unwrap_or_else(|| (msg.content.len() / 4).max(1) as u32);
        let next_total = total_tokens.saturating_add(msg_tokens);
        if next_total > max_tokens && !collected.is_empty() {
            break;
        }
        total_tokens = next_total;
        collected.push(msg);
    }

    collected.reverse();
    Ok(collected)
}

/// Get the total token count for a session.
pub fn session_token_count(conn: &Connection, session_id: &str) -> Result<u64, MemoryError> {
    let count: i64 = conn.query_row(
        "SELECT COALESCE(SUM(token_count), 0) FROM messages WHERE session_id = ?1",
        params![session_id],
        |row| row.get(0),
    )?;
    if count < 0 {
        return Err(MemoryError::CorruptData {
            table: "messages",
            row_id: session_id.to_string(),
            detail: format!("negative token_count aggregate: {count}"),
        });
    }
    Ok(count as u64)
}

/// Append a message with embedding + q8 + FTS entries.
#[allow(clippy::too_many_arguments)]
pub fn add_message_with_embedding_q8(
    conn: &Connection,
    session_id: &str,
    role: Role,
    content: &str,
    token_count: Option<u32>,
    metadata: Option<&serde_json::Value>,
    embedding_bytes: &[u8],
    q8_bytes: Option<&[u8]>,
) -> Result<i64, MemoryError> {
    let exists: bool = conn.query_row(
        "SELECT EXISTS(SELECT 1 FROM sessions WHERE id = ?1)",
        params![session_id],
        |row| row.get(0),
    )?;
    if !exists {
        return Err(MemoryError::SessionNotFound(session_id.to_string()));
    }

    let metadata_str = metadata.map(|m| m.to_string());
    with_transaction(conn, |tx| {
        tx.execute(
            "INSERT INTO messages (session_id, role, content, token_count, metadata, embedding, embedding_q8)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
            params![
                session_id,
                role.as_str(),
                content,
                token_count,
                metadata_str,
                embedding_bytes,
                q8_bytes
            ],
        )?;
        let msg_id = tx.last_insert_rowid();

        tx.execute(
            "INSERT INTO messages_rowid_map (message_id) VALUES (?1)",
            params![msg_id],
        )?;
        let fts_rowid = tx.last_insert_rowid();
        tx.execute(
            "INSERT INTO messages_fts(rowid, content) VALUES (?1, ?2)",
            params![fts_rowid, content],
        )?;

        #[cfg(feature = "hnsw")]
        enqueue_pending_index_op(
            tx,
            &format!("msg:{}", msg_id),
            "message",
            PendingIndexOpKind::Upsert,
        )?;
        crate::db::invalidate_derived_vector_artifact(tx, &format!("msg:{msg_id}"))?;

        tx.execute(
            "UPDATE sessions SET updated_at = datetime('now') WHERE id = ?1",
            params![session_id],
        )?;

        Ok(msg_id)
    })
}

/// Backward-compatible wrapper for embedded messages without q8 input.
#[allow(dead_code, clippy::too_many_arguments)]
pub fn add_message_with_embedding(
    conn: &Connection,
    session_id: &str,
    role: Role,
    content: &str,
    token_count: Option<u32>,
    metadata: Option<&serde_json::Value>,
    embedding_bytes: &[u8],
) -> Result<i64, MemoryError> {
    add_message_with_embedding_q8(
        conn,
        session_id,
        role,
        content,
        token_count,
        metadata,
        embedding_bytes,
        None,
    )
}

/// Append a message with FTS indexing but no embedding.
pub fn add_message_with_fts(
    conn: &Connection,
    session_id: &str,
    role: Role,
    content: &str,
    token_count: Option<u32>,
    metadata: Option<&serde_json::Value>,
) -> Result<i64, MemoryError> {
    let exists: bool = conn.query_row(
        "SELECT EXISTS(SELECT 1 FROM sessions WHERE id = ?1)",
        params![session_id],
        |row| row.get(0),
    )?;
    if !exists {
        return Err(MemoryError::SessionNotFound(session_id.to_string()));
    }

    let metadata_str = metadata.map(|m| m.to_string());
    with_transaction(conn, |tx| {
        tx.execute(
            "INSERT INTO messages (session_id, role, content, token_count, metadata, embedding, embedding_q8)
             VALUES (?1, ?2, ?3, ?4, ?5, NULL, NULL)",
            params![session_id, role.as_str(), content, token_count, metadata_str],
        )?;
        let msg_id = tx.last_insert_rowid();

        tx.execute(
            "INSERT INTO messages_rowid_map (message_id) VALUES (?1)",
            params![msg_id],
        )?;
        let fts_rowid = tx.last_insert_rowid();
        tx.execute(
            "INSERT INTO messages_fts(rowid, content) VALUES (?1, ?2)",
            params![fts_rowid, content],
        )?;
        tx.execute(
            "UPDATE sessions SET updated_at = datetime('now') WHERE id = ?1",
            params![session_id],
        )?;

        Ok(msg_id)
    })
}

/// Delete a session and all its messages.
pub fn delete_session(conn: &Connection, session_id: &str) -> Result<(), MemoryError> {
    with_transaction(conn, |tx| {
        let fts_data: Vec<(i64, String, i64, bool)> = {
            let mut stmt = tx.prepare(
                "SELECT m.id, m.content, mm.rowid, m.embedding IS NOT NULL
                 FROM messages m
                 JOIN messages_rowid_map mm ON mm.message_id = m.id
                 WHERE m.session_id = ?1",
            )?;
            let rows = stmt.query_map(params![session_id], |row| {
                Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?))
            })?;
            rows.collect::<Result<Vec<_>, _>>()?
        };

        for (msg_id, content, fts_rowid, has_embedding) in &fts_data {
            tx.execute(
                "INSERT INTO messages_fts(messages_fts, rowid, content) VALUES('delete', ?1, ?2)",
                params![fts_rowid, content],
            )?;

            #[cfg(feature = "hnsw")]
            if *has_embedding {
                enqueue_pending_index_op(
                    tx,
                    &format!("msg:{}", msg_id),
                    "message",
                    PendingIndexOpKind::Delete,
                )?;
            }

            #[cfg(not(feature = "hnsw"))]
            {
                let _ = msg_id;
                let _ = has_embedding;
            }
            if *has_embedding {
                crate::db::invalidate_derived_vector_artifact(tx, &format!("msg:{msg_id}"))?;
            }
        }

        let affected = tx.execute("DELETE FROM sessions WHERE id = ?1", params![session_id])?;
        if affected == 0 {
            return Err(MemoryError::SessionNotFound(session_id.to_string()));
        }

        Ok(())
    })
}

/// List recent sessions with message counts.
pub fn list_sessions(
    conn: &Connection,
    limit: usize,
    offset: usize,
) -> Result<Vec<Session>, MemoryError> {
    let mut stmt = conn.prepare(
        "SELECT s.id, s.channel, s.created_at, s.updated_at, s.metadata,
                COUNT(m.id) AS message_count
         FROM sessions s
         LEFT JOIN messages m ON m.session_id = s.id
         GROUP BY s.id
         ORDER BY s.updated_at DESC
         LIMIT ?1 OFFSET ?2",
    )?;

    let sessions = stmt
        .query_map(params![limit as i64, offset as i64], |row| {
            Ok((
                row.get::<_, String>(0)?,
                row.get::<_, String>(1)?,
                row.get::<_, String>(2)?,
                row.get::<_, String>(3)?,
                row.get::<_, Option<String>>(4)?,
                row.get::<_, i64>(5)? as u32,
            ))
        })?
        .collect::<Result<Vec<_>, _>>()?
        .into_iter()
        .map(
            |(id, channel, created_at, updated_at, metadata_raw, message_count)| {
                Ok(Session {
                    metadata: parse_optional_json(
                        "sessions",
                        &id,
                        "metadata",
                        metadata_raw.as_deref(),
                    )?,
                    id,
                    channel,
                    created_at,
                    updated_at,
                    message_count,
                })
            },
        )
        .collect::<Result<Vec<_>, MemoryError>>()?;

    Ok(sessions)
}

/// Update a session channel.
pub fn rename_session(
    conn: &Connection,
    session_id: &str,
    new_channel: &str,
) -> Result<(), MemoryError> {
    let affected = conn.execute(
        "UPDATE sessions SET channel = ?1, updated_at = datetime('now') WHERE id = ?2",
        params![new_channel, session_id],
    )?;
    if affected == 0 {
        return Err(MemoryError::SessionNotFound(session_id.to_string()));
    }
    Ok(())
}

impl MemoryStore {
    /// Create a new conversation session. Returns the session ID (UUID v4).
    pub async fn create_session(&self, channel: &str) -> Result<String, MemoryError> {
        let channel = channel.to_string();
        self.with_write_conn(move |conn| create_session(conn, &channel, None))
            .await
    }

    /// Create a new conversation session with metadata.
    ///
    /// Metadata can be used to carry namespace tags and trace data for retention
    /// and deletion policy decisions.
    pub async fn create_session_with_metadata(
        &self,
        channel: &str,
        metadata: Option<serde_json::Value>,
    ) -> Result<String, MemoryError> {
        let channel = channel.to_string();
        self.with_write_conn(move |conn| create_session(conn, &channel, metadata.as_ref()))
            .await
    }

    /// Rename a session's channel (display name).
    pub async fn rename_session(
        &self,
        session_id: &str,
        new_channel: &str,
    ) -> Result<(), MemoryError> {
        let sid = session_id.to_string();
        let ch = new_channel.to_string();
        self.with_write_conn(move |conn| rename_session(conn, &sid, &ch))
            .await
    }

    /// List recent sessions, newest first.
    pub async fn list_sessions(
        &self,
        limit: usize,
        offset: usize,
    ) -> Result<Vec<Session>, MemoryError> {
        self.with_read_conn(move |conn| list_sessions(conn, limit, offset))
            .await
    }

    /// Delete a session and all its messages.
    ///
    /// Cleans up HNSW entries for embedded messages before CASCADE delete.
    pub async fn delete_session(&self, session_id: &str) -> Result<(), MemoryError> {
        let sid = session_id.to_string();
        self.with_write_conn(move |conn| delete_session(conn, &sid))
            .await?;

        #[cfg(feature = "hnsw")]
        self.sync_pending_hnsw_ops_best_effort("delete_session")
            .await;

        Ok(())
    }

    /// Append a message to a session. Returns the message's auto-increment ID.
    pub async fn add_message(
        &self,
        session_id: &str,
        role: Role,
        content: &str,
        token_count: Option<u32>,
        metadata: Option<serde_json::Value>,
    ) -> Result<i64, MemoryError> {
        self.add_message_with_trace(session_id, role, content, token_count, metadata, None)
            .await
    }

    /// Append a message to a session with optional trace metadata.
    pub async fn add_message_with_trace(
        &self,
        session_id: &str,
        role: Role,
        content: &str,
        token_count: Option<u32>,
        metadata: Option<serde_json::Value>,
        trace_ctx: Option<&TraceCtx>,
    ) -> Result<i64, MemoryError> {
        self.add_message_embedded_with_trace(
            session_id,
            role,
            content,
            token_count,
            metadata,
            trace_ctx,
        )
        .await
    }

    /// Append a message to a session with FTS indexing but no embedding.
    ///
    /// Fallback path when embedding fails: messages still appear in conversation
    /// history and are findable via BM25 search, just not via vector search.
    pub async fn add_message_fts(
        &self,
        session_id: &str,
        role: Role,
        content: &str,
        token_count: Option<u32>,
        metadata: Option<serde_json::Value>,
    ) -> Result<i64, MemoryError> {
        self.add_message_fts_with_trace(session_id, role, content, token_count, metadata, None)
            .await
    }

    /// Append a message with FTS indexing and optional trace metadata.
    pub async fn add_message_fts_with_trace(
        &self,
        session_id: &str,
        role: Role,
        content: &str,
        token_count: Option<u32>,
        metadata: Option<serde_json::Value>,
        trace_ctx: Option<&TraceCtx>,
    ) -> Result<i64, MemoryError> {
        self.validate_content("message.content", content)?;

        let effective_token_count =
            token_count.or_else(|| Some(self.inner.token_counter.count_tokens(content) as u32));
        let sid = session_id.to_string();
        let ct = content.to_string();
        let meta = merge_trace_ctx(metadata, trace_ctx);
        self.with_write_conn(move |conn| {
            add_message_with_fts(conn, &sid, role, &ct, effective_token_count, meta.as_ref())
        })
        .await
    }

    /// Get the most recent N messages from a session, in chronological order.
    pub async fn get_recent_messages(
        &self,
        session_id: &str,
        limit: usize,
    ) -> Result<Vec<Message>, MemoryError> {
        let sid = session_id.to_string();
        self.with_read_conn(move |conn| get_recent_messages(conn, &sid, limit))
            .await
    }

    /// Get messages from a session up to `max_tokens` total.
    pub async fn get_messages_within_budget(
        &self,
        session_id: &str,
        max_tokens: u32,
    ) -> Result<Vec<Message>, MemoryError> {
        let sid = session_id.to_string();
        self.with_read_conn(move |conn| get_messages_within_budget(conn, &sid, max_tokens))
            .await
    }

    /// Get total token count for a session.
    pub async fn session_token_count(&self, session_id: &str) -> Result<u64, MemoryError> {
        let sid = session_id.to_string();
        self.with_read_conn(move |conn| session_token_count(conn, &sid))
            .await
    }

    /// Append a message to a session with automatic embedding and FTS indexing.
    pub async fn add_message_embedded(
        &self,
        session_id: &str,
        role: Role,
        content: &str,
        token_count: Option<u32>,
        metadata: Option<serde_json::Value>,
    ) -> Result<i64, MemoryError> {
        self.add_message_embedded_with_trace(session_id, role, content, token_count, metadata, None)
            .await
    }

    /// Append an embedded message with optional trace metadata.
    pub async fn add_message_embedded_with_trace(
        &self,
        session_id: &str,
        role: Role,
        content: &str,
        token_count: Option<u32>,
        metadata: Option<serde_json::Value>,
        trace_ctx: Option<&TraceCtx>,
    ) -> Result<i64, MemoryError> {
        self.validate_content("message.content", content)?;

        let effective_token_count =
            token_count.or_else(|| Some(self.inner.token_counter.count_tokens(content) as u32));

        let embedding = self.embed_text_internal(content).await?;
        self.validate_embedding_dimensions(&embedding)?;
        let embedding_bytes = crate::db::embedding_to_bytes(&embedding);
        // INTENTIONAL: q8 quantization is an optional search optimization; missing q8 is non-fatal
        let q8_bytes = Quantizer::new(self.inner.config.embedding.dimensions)
            .quantize(&embedding)
            .map(|qv| quantize::pack_quantized(&qv))
            .ok();

        let sid = session_id.to_string();
        let ct = content.to_string();
        let meta = merge_trace_ctx(metadata, trace_ctx);
        let msg_id = self
            .with_write_conn(move |conn| {
                add_message_with_embedding_q8(
                    conn,
                    &sid,
                    role,
                    &ct,
                    effective_token_count,
                    meta.as_ref(),
                    &embedding_bytes,
                    q8_bytes.as_deref(),
                )
            })
            .await?;

        #[cfg(feature = "hnsw")]
        self.sync_pending_hnsw_ops_best_effort("add_message_embedded")
            .await;

        Ok(msg_id)
    }

    /// Hybrid search over conversation messages only.
    pub async fn search_conversations(
        &self,
        query: &str,
        top_k: Option<usize>,
        session_ids: Option<&[&str]>,
    ) -> Result<Vec<SearchResult>, MemoryError> {
        const MAX_TOP_K: usize = 1_000;
        let k = top_k
            .unwrap_or(self.inner.config.search.default_top_k)
            .min(MAX_TOP_K);

        let query_embedding = self.embed_text_internal(query).await?;

        #[cfg(feature = "hnsw")]
        let hnsw_hits = {
            let index = self
                .inner
                .hnsw_index
                .read()
                .unwrap_or_else(|e| e.into_inner())
                .clone();
            let candidates = self
                .inner
                .config
                .search
                .candidate_pool_size
                .max(k.saturating_mul(3))
                .min(MAX_TOP_K.saturating_mul(10));
            let query_embedding_for_hnsw = query_embedding.clone();
            match tokio::task::spawn_blocking(move || {
                index.search(&query_embedding_for_hnsw, candidates)
            })
            .await
            {
                Ok(Ok(hits)) => hits,
                Ok(Err(err)) => {
                    tracing::error!(
                        "HNSW conversation search failed, falling back to brute-force message search: {}",
                        err
                    );
                    Vec::new()
                }
                Err(err) => {
                    tracing::error!(
                        "HNSW conversation search task failed, falling back to brute-force message search: {}",
                        err
                    );
                    Vec::new()
                }
            }
        };

        let q = query.to_string();
        let config = self.inner.config.search.clone();
        let sids_owned = to_owned_string_vec(session_ids);

        #[cfg(feature = "hnsw")]
        let hnsw_hits_owned = hnsw_hits;

        self.with_read_conn(move |conn| {
            let sids_refs = as_str_slice(&sids_owned);
            let sids_slice: Option<&[&str]> = sids_refs.as_deref();
            #[cfg(feature = "hnsw")]
            {
                if hnsw_hits_owned.is_empty() {
                    search::hybrid_search(
                        conn,
                        &q,
                        &query_embedding,
                        &config,
                        k,
                        None,
                        Some(&[SearchSourceType::Messages]),
                        sids_slice,
                    )
                } else {
                    search::hybrid_search_with_hnsw(
                        conn,
                        &q,
                        &query_embedding,
                        &config,
                        k,
                        None,
                        Some(&[SearchSourceType::Messages]),
                        sids_slice,
                        &hnsw_hits_owned,
                    )
                }
            }
            #[cfg(not(feature = "hnsw"))]
            {
                search::hybrid_search(
                    conn,
                    &q,
                    &query_embedding,
                    &config,
                    k,
                    None,
                    Some(&[SearchSourceType::Messages]),
                    sids_slice,
                )
            }
        })
        .await
    }
}