claude-hippo 0.2.0

Claude Code に海馬を足す MCP サーバ。特異性が高い瞬間だけを長期記憶化する surprise-aware memory store. Pure Rust、SHODH-compatible schema、Apache-2.0/MIT dual-licensed.
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
//! Storage layer — SQLite + sqlite-vec, mcp-memory-service-rs と schema 互換。
//!
//! - 11 列 schema を verbatim コピー → 同じ DB ファイルを両者で読み書き可能
//! - surprise_score / surprise_components は `metadata` JSON 列に格納する
//!   (専用列を増やすと competitor の `CREATE TABLE IF NOT EXISTS` で
//!   自動マイグレーションが走らないため、JSON に閉じ込める)
//! - soft-delete only (`deleted_at REAL`)、物理削除は API 層で禁止
//! - tag は comma-separated 文字列、case-sensitive、whitespace-stripped GLOB

use crate::{HippoError, Result, EMBEDDING_DIM};
use rusqlite::{params, Connection, OptionalExtension};
use std::path::Path;
use zerocopy::AsBytes;

/// mcp-memory-service-rs と verbatim 一致する schema。
/// DB ファイルレベルで両者が drop-in swap 可能。
pub const SCHEMA_SQL: &str = r#"
CREATE TABLE IF NOT EXISTS memories (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    content_hash    TEXT UNIQUE NOT NULL,
    content         TEXT NOT NULL,
    tags            TEXT,
    memory_type     TEXT,
    metadata        TEXT,
    created_at      REAL,
    updated_at      REAL,
    created_at_iso  TEXT,
    updated_at_iso  TEXT,
    deleted_at      REAL DEFAULT NULL
);

CREATE INDEX IF NOT EXISTS idx_content_hash ON memories(content_hash);
CREATE INDEX IF NOT EXISTS idx_created_at  ON memories(created_at);
CREATE INDEX IF NOT EXISTS idx_memory_type ON memories(memory_type);
CREATE INDEX IF NOT EXISTS idx_deleted_at  ON memories(deleted_at);

CREATE TABLE IF NOT EXISTS metadata (
    key   TEXT PRIMARY KEY,
    value TEXT NOT NULL
);

CREATE VIRTUAL TABLE IF NOT EXISTS memory_embeddings USING vec0(
    content_embedding FLOAT[384] distance_metric=cosine
);
"#;

const PRAGMAS_SQL: &str = r#"
PRAGMA journal_mode = WAL;
PRAGMA busy_timeout = 5000;
PRAGMA synchronous = NORMAL;
PRAGMA cache_size = 10000;
PRAGMA temp_store = MEMORY;
"#;

/// sqlite-vec extension をすべての connection で auto-load する。
/// `Connection::open` を呼ぶ前に **一度だけ** 呼び出すこと。
pub fn register_sqlite_vec() {
    use std::sync::Once;
    static ONCE: Once = Once::new();
    ONCE.call_once(|| {
        // SAFETY: sqlite_vec::sqlite3_vec_init is the canonical extension entry
        // point with the exact ABI sqlite3 expects.
        unsafe {
            #[allow(clippy::missing_transmute_annotations)]
            let f: unsafe extern "C" fn(
                *mut rusqlite::ffi::sqlite3,
                *mut *mut std::os::raw::c_char,
                *const rusqlite::ffi::sqlite3_api_routines,
            ) -> std::os::raw::c_int =
                std::mem::transmute(sqlite_vec::sqlite3_vec_init as *const ());
            rusqlite::ffi::sqlite3_auto_extension(Some(f));
        }
    });
}

/// Memory record. `metadata` は free-form JSON、surprise score は
/// `_hippo.surprise` namespace で詰める。
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
pub struct MemoryRow {
    pub id: Option<i64>,
    pub content_hash: String,
    pub content: String,
    pub tags: Vec<String>,
    pub memory_type: Option<String>,
    /// JSON object, free-form. claude-hippo は `_hippo` キー namespace を予約。
    pub metadata: serde_json::Value,
    pub created_at: f64,
    pub updated_at: f64,
    pub created_at_iso: String,
    pub updated_at_iso: String,
    /// soft-delete tombstone (None = alive)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deleted_at: Option<f64>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TagMatch {
    Any,
    All,
}

impl TagMatch {
    pub fn parse(s: &str) -> Self {
        if s.eq_ignore_ascii_case("all") {
            Self::All
        } else {
            Self::Any
        }
    }
}

pub struct Storage {
    conn: Connection,
}

impl Storage {
    /// DB を開いて schema を適用する。`register_sqlite_vec()` が事前に
    /// 呼ばれていることが必要。
    pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
        let conn = Connection::open(path)?;
        conn.execute_batch(PRAGMAS_SQL)?;
        conn.execute_batch(SCHEMA_SQL)
            .map_err(|e| HippoError::Schema(format!("apply schema: {e}")))?;
        Ok(Self { conn })
    }

    /// in-memory DB (tests).
    pub fn open_in_memory() -> Result<Self> {
        let conn = Connection::open_in_memory()?;
        conn.execute_batch(PRAGMAS_SQL)?;
        conn.execute_batch(SCHEMA_SQL)
            .map_err(|e| HippoError::Schema(format!("apply schema: {e}")))?;
        Ok(Self { conn })
    }

    pub fn conn(&self) -> &Connection {
        &self.conn
    }

    pub fn vec_version(&self) -> Result<String> {
        Ok(self
            .conn
            .query_row("SELECT vec_version()", [], |r| r.get(0))?)
    }

    pub fn count_alive(&self) -> Result<i64> {
        Ok(self.conn.query_row(
            "SELECT COUNT(*) FROM memories WHERE deleted_at IS NULL",
            [],
            |r| r.get(0),
        )?)
    }

    pub fn count_total(&self) -> Result<i64> {
        Ok(self
            .conn
            .query_row("SELECT COUNT(*) FROM memories", [], |r| r.get(0))?)
    }

    /// Insert a memory + its embedding. content_hash UNIQUE collision → no-op
    /// で `Ok((existing_id, true))` を返す (duplicate=true)。
    /// embedding が None の場合は memory 行のみ insert (テスト用)。
    pub fn insert(&mut self, row: &MemoryRow, embedding: Option<&[f32]>) -> Result<(i64, bool)> {
        if let Some(emb) = embedding {
            if emb.len() != EMBEDDING_DIM {
                return Err(HippoError::Embedding(format!(
                    "embedding dim mismatch: expected {EMBEDDING_DIM}, got {}",
                    emb.len()
                )));
            }
        }

        let tx = self.conn.transaction()?;

        // Dedup check.
        let existing: Option<i64> = tx
            .query_row(
                "SELECT id FROM memories WHERE content_hash = ?1",
                params![row.content_hash],
                |r| r.get(0),
            )
            .optional()?;
        if let Some(id) = existing {
            return Ok((id, true));
        }

        let tags_str = encode_tags(&row.tags);
        let metadata_str = serde_json::to_string(&row.metadata)?;

        tx.execute(
            "INSERT INTO memories (
                content_hash, content, tags, memory_type, metadata,
                created_at, updated_at, created_at_iso, updated_at_iso
             ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
            params![
                row.content_hash,
                row.content,
                tags_str,
                row.memory_type,
                metadata_str,
                row.created_at,
                row.updated_at,
                row.created_at_iso,
                row.updated_at_iso,
            ],
        )?;
        let id = tx.last_insert_rowid();

        if let Some(emb) = embedding {
            tx.execute(
                "INSERT INTO memory_embeddings (rowid, content_embedding) VALUES (?1, ?2)",
                params![id, emb.as_bytes()],
            )?;
        }

        tx.commit()?;
        Ok((id, false))
    }

    /// content_hash で 1 件取得。
    pub fn get_by_hash(&self, hash: &str) -> Result<Option<MemoryRow>> {
        let row = self
            .conn
            .query_row(
                "SELECT id, content_hash, content, tags, memory_type, metadata,
                        created_at, updated_at, created_at_iso, updated_at_iso, deleted_at
                 FROM memories WHERE content_hash = ?1",
                params![hash],
                row_from_sql,
            )
            .optional()?;
        Ok(row)
    }

    /// id で 1 件取得。
    pub fn get_by_id(&self, id: i64) -> Result<Option<MemoryRow>> {
        let row = self
            .conn
            .query_row(
                "SELECT id, content_hash, content, tags, memory_type, metadata,
                        created_at, updated_at, created_at_iso, updated_at_iso, deleted_at
                 FROM memories WHERE id = ?1",
                params![id],
                row_from_sql,
            )
            .optional()?;
        Ok(row)
    }

    /// 直近 n 件 (alive) を created_at DESC で。
    pub fn list_recent(&self, n: i64) -> Result<Vec<MemoryRow>> {
        let mut stmt = self.conn.prepare(
            "SELECT id, content_hash, content, tags, memory_type, metadata,
                    created_at, updated_at, created_at_iso, updated_at_iso, deleted_at
             FROM memories
             WHERE deleted_at IS NULL
             ORDER BY created_at DESC
             LIMIT ?1",
        )?;
        let rows = stmt
            .query_map(params![n], row_from_sql)?
            .collect::<std::result::Result<Vec<_>, _>>()?;
        Ok(rows)
    }

    /// tag GLOB 検索。mcp-memory-service-rs / Python upstream と完全同一の
    /// pattern: `(',' || REPLACE(tags, ' ', '') || ',') GLOB '*,<tag>,*'`
    pub fn search_by_tag(
        &self,
        tags: &[String],
        match_mode: TagMatch,
        memory_type: Option<&str>,
        limit: i64,
    ) -> Result<Vec<MemoryRow>> {
        if tags.is_empty() {
            return Ok(Vec::new());
        }

        let mut where_parts: Vec<String> = Vec::new();
        let mut params_vec: Vec<rusqlite::types::Value> = Vec::new();

        where_parts.push("deleted_at IS NULL".into());

        let op = match match_mode {
            TagMatch::Any => "OR",
            TagMatch::All => "AND",
        };

        let tag_clauses: Vec<String> = (0..tags.len())
            .map(|_| "(',' || REPLACE(IFNULL(tags, ''), ' ', '') || ',') GLOB ?".to_string())
            .collect();
        where_parts.push(format!("({})", tag_clauses.join(&format!(" {op} "))));
        for t in tags {
            params_vec.push(rusqlite::types::Value::from(format!(
                "*,{},*",
                t.replace(' ', "")
            )));
        }

        if let Some(mt) = memory_type {
            where_parts.push("memory_type = ?".into());
            params_vec.push(rusqlite::types::Value::from(mt.to_string()));
        }

        let sql = format!(
            "SELECT id, content_hash, content, tags, memory_type, metadata,
                    created_at, updated_at, created_at_iso, updated_at_iso, deleted_at
             FROM memories
             WHERE {}
             ORDER BY created_at DESC
             LIMIT {}",
            where_parts.join(" AND "),
            limit.max(0)
        );

        let mut stmt = self.conn.prepare(&sql)?;
        let params_refs: Vec<&dyn rusqlite::ToSql> = params_vec
            .iter()
            .map(|v| v as &dyn rusqlite::ToSql)
            .collect();
        let rows = stmt
            .query_map(params_refs.as_slice(), row_from_sql)?
            .collect::<std::result::Result<Vec<_>, _>>()?;
        Ok(rows)
    }

    /// vector KNN search. embedding は L2 normalized 必須。
    /// 戻り値の `f32` は cosine distance (sqlite-vec 規約)、
    /// 0.0 が完全一致、2.0 が完全反対。
    pub fn knn(&self, query_embedding: &[f32], k: usize) -> Result<Vec<(i64, f32)>> {
        if query_embedding.len() != EMBEDDING_DIM {
            return Err(HippoError::Embedding(format!(
                "query embedding dim mismatch: expected {EMBEDDING_DIM}, got {}",
                query_embedding.len()
            )));
        }
        // soft-delete tombstone がある時だけ over-sample (mcp-memory-service-rs 流儀)。
        let has_tombstones: bool = self.conn.query_row(
            "SELECT EXISTS(SELECT 1 FROM memories WHERE deleted_at IS NOT NULL LIMIT 1)",
            [],
            |r| r.get(0),
        )?;
        let oversample = if has_tombstones { k * 3 } else { k };

        let mut stmt = self.conn.prepare(
            "SELECT rowid, distance FROM memory_embeddings
             WHERE content_embedding MATCH ?1 AND k = ?2
             ORDER BY distance",
        )?;
        let rows: Vec<(i64, f32)> = stmt
            .query_map(
                params![query_embedding.as_bytes(), oversample as i64],
                |r| Ok((r.get::<_, i64>(0)?, r.get::<_, f32>(1)?)),
            )?
            .collect::<std::result::Result<Vec<_>, _>>()?;

        // tombstone を弾いて k 件まで切り詰める。
        let mut alive: Vec<(i64, f32)> = Vec::with_capacity(k);
        if rows.is_empty() {
            return Ok(alive);
        }
        // 一括で alive id を引いてから filter。
        let ids: Vec<i64> = rows.iter().map(|(id, _)| *id).collect();
        let placeholders = (0..ids.len()).map(|_| "?").collect::<Vec<_>>().join(",");
        let alive_sql =
            format!("SELECT id FROM memories WHERE id IN ({placeholders}) AND deleted_at IS NULL");
        let mut stmt2 = self.conn.prepare(&alive_sql)?;
        let id_params: Vec<&dyn rusqlite::ToSql> =
            ids.iter().map(|i| i as &dyn rusqlite::ToSql).collect();
        let alive_set: std::collections::HashSet<i64> = stmt2
            .query_map(id_params.as_slice(), |r| r.get::<_, i64>(0))?
            .collect::<std::result::Result<_, _>>()?;
        for (id, dist) in rows {
            if alive_set.contains(&id) {
                alive.push((id, dist));
                if alive.len() >= k {
                    break;
                }
            }
        }
        Ok(alive)
    }

    /// soft-delete by content_hash。返り値は実際に削除された件数。
    pub fn soft_delete_by_hash(&mut self, hash: &str) -> Result<usize> {
        let now = unix_now();
        let n = self.conn.execute(
            "UPDATE memories SET deleted_at = ?1
             WHERE content_hash = ?2 AND deleted_at IS NULL",
            params![now, hash],
        )?;
        Ok(n)
    }

    /// **Tests / eval harness only.** Backdate a memory's `created_at` and
    /// `updated_at` to simulate cross-session recall scenarios. Production
    /// memories should never be backdated; this helper exists so the v0.2
    /// evaluation suite can stress the forgetting-curve interaction with
    /// surprise rerank without waiting 30 days of wall-clock.
    pub fn debug_set_created_at(&self, id: i64, ts: f64) -> Result<usize> {
        Ok(self.conn.execute(
            "UPDATE memories SET created_at = ?1, updated_at = ?1 WHERE id = ?2",
            params![ts, id],
        )?)
    }

    /// soft-delete by id。
    pub fn soft_delete_by_id(&mut self, id: i64) -> Result<usize> {
        let now = unix_now();
        let n = self.conn.execute(
            "UPDATE memories SET deleted_at = ?1
             WHERE id = ?2 AND deleted_at IS NULL",
            params![now, id],
        )?;
        Ok(n)
    }
}

fn row_from_sql(r: &rusqlite::Row<'_>) -> rusqlite::Result<MemoryRow> {
    let metadata_str: Option<String> = r.get("metadata")?;
    let metadata = match metadata_str {
        Some(s) if !s.is_empty() => serde_json::from_str(&s).unwrap_or(serde_json::Value::Null),
        _ => serde_json::Value::Object(Default::default()),
    };
    let tags_str: Option<String> = r.get("tags")?;
    Ok(MemoryRow {
        id: Some(r.get("id")?),
        content_hash: r.get("content_hash")?,
        content: r.get("content")?,
        tags: decode_tags(tags_str.as_deref()),
        memory_type: r.get("memory_type")?,
        metadata,
        created_at: r.get::<_, Option<f64>>("created_at")?.unwrap_or(0.0),
        updated_at: r.get::<_, Option<f64>>("updated_at")?.unwrap_or(0.0),
        created_at_iso: r
            .get::<_, Option<String>>("created_at_iso")?
            .unwrap_or_default(),
        updated_at_iso: r
            .get::<_, Option<String>>("updated_at_iso")?
            .unwrap_or_default(),
        deleted_at: r.get("deleted_at")?,
    })
}

/// 新しい MemoryRow を作る。timestamps は now。content_hash は SHA256(content)。
pub fn new_memory_row(
    content: String,
    tags: Vec<String>,
    memory_type: Option<String>,
    metadata: serde_json::Value,
) -> MemoryRow {
    let now = unix_now();
    let now_iso = chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true);
    let hash = content_hash(&content);
    MemoryRow {
        id: None,
        content_hash: hash,
        content,
        tags,
        memory_type,
        metadata,
        created_at: now,
        updated_at: now,
        created_at_iso: now_iso.clone(),
        updated_at_iso: now_iso,
        deleted_at: None,
    }
}

fn unix_now() -> f64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs_f64())
        .unwrap_or(0.0)
}

/// SHA-256 hex (lowercase) — mcp-memory-service-rs と同じ content_hash 規約。
pub fn content_hash(content: &str) -> String {
    use sha2::{Digest, Sha256};
    let mut h = Sha256::new();
    h.update(content.as_bytes());
    hex::encode(h.finalize())
}

/// tags を「カンマ区切り文字列、空白保持、case 保持」で encode する。
/// (mcp-memory-service-rs の Python 上流挙動と同一: 検索時に空白は strip
/// するが、ストレージは生のまま保持)
fn encode_tags(tags: &[String]) -> Option<String> {
    if tags.is_empty() {
        None
    } else {
        Some(tags.join(","))
    }
}

fn decode_tags(s: Option<&str>) -> Vec<String> {
    match s {
        Some(s) if !s.is_empty() => s.split(',').map(|t| t.to_string()).collect(),
        _ => Vec::new(),
    }
}

/// Surprise score を MemoryRow.metadata に attach する helper。
/// metadata が non-object なら object に置き換える。
pub fn attach_surprise(
    metadata: &mut serde_json::Value,
    score: f32,
    components: &crate::surprise::SurpriseComponents,
) {
    if !metadata.is_object() {
        *metadata = serde_json::Value::Object(Default::default());
    }
    let map = metadata.as_object_mut().expect("ensured object above");
    let hippo = map
        .entry("_hippo")
        .or_insert_with(|| serde_json::Value::Object(Default::default()));
    if !hippo.is_object() {
        *hippo = serde_json::Value::Object(Default::default());
    }
    let hm = hippo.as_object_mut().expect("ensured object above");
    hm.insert(
        "surprise".into(),
        serde_json::json!({
            "score": score,
            "components": components,
            "version": crate::VERSION,
        }),
    );
}

/// metadata から surprise score を読み出す。なければ None。
pub fn read_surprise(metadata: &serde_json::Value) -> Option<f32> {
    metadata
        .get("_hippo")?
        .get("surprise")?
        .get("score")?
        .as_f64()
        .map(|f| f as f32)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn store() -> Storage {
        register_sqlite_vec();
        Storage::open_in_memory().expect("open in-memory")
    }

    fn dummy_emb(seed: f32) -> Vec<f32> {
        let mut v = vec![0.0_f32; EMBEDDING_DIM];
        v[0] = seed;
        // L2 normalize
        let norm: f32 = v.iter().map(|x| x * x).sum::<f32>().sqrt().max(1e-8);
        v.iter_mut().for_each(|x| *x /= norm);
        v
    }

    #[test]
    fn open_applies_schema() {
        let s = store();
        assert_eq!(s.count_alive().unwrap(), 0);
        assert_eq!(s.count_total().unwrap(), 0);
        // vec_version available
        let v = s.vec_version().unwrap();
        assert!(!v.is_empty(), "vec_version: {v}");
    }

    #[test]
    fn insert_and_get_roundtrip() {
        let mut s = store();
        let row = new_memory_row(
            "JWT 24h expiry".into(),
            vec!["auth".into(), "security".into()],
            Some("Decision".into()),
            serde_json::json!({}),
        );
        let (id, dup) = s.insert(&row, Some(&dummy_emb(1.0))).unwrap();
        assert!(!dup);
        assert!(id > 0);
        let fetched = s.get_by_hash(&row.content_hash).unwrap().unwrap();
        assert_eq!(fetched.content, row.content);
        assert_eq!(fetched.tags, vec!["auth", "security"]);
    }

    #[test]
    fn dedup_returns_existing() {
        let mut s = store();
        let row = new_memory_row("same content".into(), vec![], None, serde_json::json!({}));
        let (id1, dup1) = s.insert(&row, Some(&dummy_emb(1.0))).unwrap();
        let (id2, dup2) = s.insert(&row, Some(&dummy_emb(2.0))).unwrap();
        assert!(!dup1);
        assert!(dup2);
        assert_eq!(id1, id2);
    }

    #[test]
    fn soft_delete_filters_alive() {
        let mut s = store();
        let r1 = new_memory_row("first".into(), vec![], None, serde_json::json!({}));
        let r2 = new_memory_row("second".into(), vec![], None, serde_json::json!({}));
        s.insert(&r1, Some(&dummy_emb(1.0))).unwrap();
        s.insert(&r2, Some(&dummy_emb(2.0))).unwrap();
        assert_eq!(s.count_alive().unwrap(), 2);
        s.soft_delete_by_hash(&r1.content_hash).unwrap();
        assert_eq!(s.count_alive().unwrap(), 1);
        assert_eq!(s.count_total().unwrap(), 2);
    }

    #[test]
    fn list_recent_orders_by_created_at_desc() {
        let mut s = store();
        let r1 = new_memory_row("oldest".into(), vec![], None, serde_json::json!({}));
        s.insert(&r1, Some(&dummy_emb(1.0))).unwrap();
        std::thread::sleep(std::time::Duration::from_millis(10));
        let r2 = new_memory_row("newer".into(), vec![], None, serde_json::json!({}));
        s.insert(&r2, Some(&dummy_emb(2.0))).unwrap();
        let rows = s.list_recent(10).unwrap();
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0].content, "newer");
        assert_eq!(rows[1].content, "oldest");
    }

    #[test]
    fn tag_glob_any_matches_either() {
        let mut s = store();
        let r1 = new_memory_row(
            "auth note".into(),
            vec!["auth".into()],
            None,
            serde_json::json!({}),
        );
        let r2 = new_memory_row(
            "db note".into(),
            vec!["db".into()],
            None,
            serde_json::json!({}),
        );
        s.insert(&r1, Some(&dummy_emb(1.0))).unwrap();
        s.insert(&r2, Some(&dummy_emb(2.0))).unwrap();
        let hits = s
            .search_by_tag(&["auth".into(), "db".into()], TagMatch::Any, None, 10)
            .unwrap();
        assert_eq!(hits.len(), 2);
    }

    #[test]
    fn tag_glob_all_requires_both() {
        let mut s = store();
        let r1 = new_memory_row(
            "both".into(),
            vec!["auth".into(), "security".into()],
            None,
            serde_json::json!({}),
        );
        let r2 = new_memory_row(
            "one".into(),
            vec!["auth".into()],
            None,
            serde_json::json!({}),
        );
        s.insert(&r1, Some(&dummy_emb(1.0))).unwrap();
        s.insert(&r2, Some(&dummy_emb(2.0))).unwrap();
        let hits = s
            .search_by_tag(&["auth".into(), "security".into()], TagMatch::All, None, 10)
            .unwrap();
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].content, "both");
    }

    #[test]
    fn knn_returns_alive_ranked() {
        let mut s = store();
        let r1 = new_memory_row("a".into(), vec![], None, serde_json::json!({}));
        let r2 = new_memory_row("b".into(), vec![], None, serde_json::json!({}));
        s.insert(&r1, Some(&dummy_emb(1.0))).unwrap();
        s.insert(&r2, Some(&dummy_emb(-1.0))).unwrap();
        let hits = s.knn(&dummy_emb(1.0), 2).unwrap();
        assert_eq!(hits.len(), 2);
        assert!(hits[0].1 < hits[1].1, "closer match must come first");
    }

    #[test]
    fn knn_skips_soft_deleted() {
        let mut s = store();
        let r1 = new_memory_row("deleted".into(), vec![], None, serde_json::json!({}));
        let r2 = new_memory_row("alive".into(), vec![], None, serde_json::json!({}));
        s.insert(&r1, Some(&dummy_emb(1.0))).unwrap();
        s.insert(&r2, Some(&dummy_emb(0.99))).unwrap();
        s.soft_delete_by_hash(&r1.content_hash).unwrap();
        let hits = s.knn(&dummy_emb(1.0), 5).unwrap();
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].0, 2);
    }

    #[test]
    fn surprise_attach_round_trip() {
        let mut meta = serde_json::json!({"user_field": 1});
        let comps = crate::surprise::SurpriseComponents {
            embedding_outlier: 0.5,
            engagement: 0.3,
            explicit: 0.2,
            prediction_loss: None,
        };
        attach_surprise(&mut meta, 0.42, &comps);
        let s = read_surprise(&meta).unwrap();
        assert!((s - 0.42).abs() < 1e-6);
        // user_field 保存されている
        assert_eq!(meta["user_field"], serde_json::json!(1));
    }
}