tapirus 1.0.4

Embedded multi-model database with SQL, vector search and local AI memory
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
//! Portable requests for persisted document memory and hybrid retrieval.

use super::{MemoryConfig, MemoryFusion, MemoryRecallResult, MemorySearchResponse, MemoryStats};
use crate::{Connection, Error, Result};
use serde::{Deserialize, Serialize};

/// A document with an optional embedding supplied by the application's model.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct MemoryDocument {
    /// Original text returned as a source citation in search results.
    pub content: String,
    /// Model-generated embedding; omission enables lexical retrieval only.
    pub embedding: Option<Vec<f32>>,
    /// Retention importance between zero and one.
    pub importance: f32,
    /// Application-defined source or category tags.
    pub tags: Vec<String>,
    /// Optional namespace filter, independent of authentication.
    pub namespace: Option<String>,
    /// Optional conversation/session identifier.
    pub session_id: Option<String>,
    /// Idempotency key scoped by namespace and session.
    pub external_id: Option<String>,
    /// Application source version.
    pub source_version: Option<String>,
    /// Embedding model identity; absent selects the legacy model space.
    pub embedding_model: Option<String>,
    /// Absolute expiry in Unix seconds. Mutually exclusive with ttl_seconds.
    pub expires_at: Option<u64>,
    /// Expiry relative to insertion/update time.
    pub ttl_seconds: Option<u64>,
    /// Conditional update revision; zero means the document must not exist.
    pub expected_revision: Option<u64>,
}

impl Default for MemoryDocument {
    fn default() -> Self {
        Self {
            content: String::new(),
            embedding: None,
            importance: 0.5,
            tags: Vec::new(),
            namespace: None,
            session_id: None,
            external_id: None,
            source_version: None,
            embedding_model: None,
            expires_at: None,
            ttl_seconds: None,
            expected_revision: None,
        }
    }
}

/// Search text, supplied embeddings, or both, with explicit source filters.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct MemorySearch {
    /// BM25 query text. At least query or embedding is required.
    pub query: Option<String>,
    /// An embedding from the same model/dimension as stored documents.
    pub embedding: Option<Vec<f32>>,
    /// Maximum results, from zero to 10,000.
    pub limit: usize,
    /// Optional namespace restriction.
    pub namespace: Option<String>,
    /// Optional session restriction.
    pub session_id: Option<String>,
    /// Match at least one requested tag when this list is nonempty.
    pub tags: Vec<String>,
    /// Embedding model identity; vector searches never mix model spaces.
    pub embedding_model: Option<String>,
    /// Top candidates per channel before final ranking.
    pub candidate_limit: usize,
    /// Rank fusion or bounded weighted scoring.
    pub fusion: MemoryFusion,
    /// RRF smoothing constant.
    pub rrf_k: f32,
    /// Semantic contribution weight.
    pub vector_weight: f32,
    /// Keyword contribution weight.
    pub lexical_weight: f32,
    /// Recency contribution weight.
    pub recency_weight: f32,
    /// Importance contribution weight.
    pub importance_weight: f32,
    /// Recency half-life in seconds; zero disables decay.
    pub half_life_seconds: u64,
    /// Minimum final ranking score, from zero to one.
    pub min_score: f32,
    /// Deliberate exact vector scan for evaluation/small corpora.
    pub exact: bool,
}

impl Default for MemorySearch {
    fn default() -> Self {
        Self {
            query: None,
            embedding: None,
            limit: 5,
            namespace: None,
            session_id: None,
            tags: Vec::new(),
            embedding_model: None,
            candidate_limit: 128,
            fusion: MemoryFusion::Rrf,
            rrf_k: 60.0,
            vector_weight: 0.45,
            lexical_weight: 0.35,
            recency_weight: 0.10,
            importance_weight: 0.10,
            half_life_seconds: 30 * 86400,
            min_score: 0.0,
            exact: false,
        }
    }
}

impl Connection {
    /// Generate embeddings before an atomic upsert. Provider failures leave sources unchanged.
    /// Applications should pin the provider model ID to its actual model revision.
    pub fn upsert_embedded_memories(
        &self,
        provider: &impl super::EmbeddingProvider,
        documents: &[MemoryDocument],
    ) -> Result<Vec<u64>> {
        let config = self.memory.read().configuration().clone();
        if documents.is_empty() {
            return Ok(Vec::new());
        }
        if documents.len() > config.max_batch_documents
            || provider.vector_dimensions() > config.max_vector_dimensions
        {
            return Err(Error::ConstraintViolation(
                "Embedding batch exceeds configured limits".into(),
            ));
        }
        let texts: Vec<&str> = documents.iter().map(|doc| doc.content.as_str()).collect();
        let vectors = provider.embed_documents(&texts)?;
        super::embedder::validate_embeddings(
            &vectors,
            documents.len(),
            provider.vector_dimensions(),
        )?;
        let model = provider.model_id();
        let documents: Vec<_> = documents
            .iter()
            .zip(vectors)
            .map(|(doc, vector)| {
                let mut doc = doc.clone();
                doc.embedding = Some(vector);
                doc.embedding_model = Some(model.clone());
                doc
            })
            .collect();
        self.upsert_memory_batch(&documents)
    }
    /// Portable JSON gateway shared by language bindings. Public SDKs expose
    /// named methods; operation names here are an internal transport contract.
    pub fn memory_request_json(&self, operation: &str, payload: &str) -> Result<String> {
        fn decode<T: serde::de::DeserializeOwned>(payload: &str) -> Result<T> {
            serde_json::from_str(payload).map_err(|e| Error::Serialization(e.to_string()))
        }
        fn encode(value: impl Serialize) -> Result<String> {
            serde_json::to_string(&value).map_err(|e| Error::Serialization(e.to_string()))
        }
        match operation {
            "store" => encode(self.store_memory(&decode(payload)?)?),
            "search" => encode(self.search_memories(&decode(payload)?)?),
            "forget" => encode(self.memory_forget_checked(decode(payload)?)?),
            "upsert" => encode(self.upsert_memory(&decode(payload)?)?),
            "store_batch" => {
                encode(self.store_memory_batch(&decode::<Vec<MemoryDocument>>(payload)?)?)
            }
            "upsert_batch" => {
                encode(self.upsert_memory_batch(&decode::<Vec<MemoryDocument>>(payload)?)?)
            }
            "search_detailed" => encode(self.search_memories_detailed(&decode(payload)?)?),
            "configure" => {
                self.configure_memory(decode(payload)?)?;
                encode(self.memory_stats())
            }
            "stats" => encode(self.memory_stats()),
            "storage_health" => encode(self.storage_health()),
            "prepare" => encode(self.prepare_memory_indexes()?),
            "purge_expired" => encode(self.purge_expired_memories()?),
            "delete" => encode(self.delete_memories(&decode(payload)?)?),
            _ => Err(Error::ConstraintViolation(
                "Unknown memory operation".into(),
            )),
        }
    }
    /// Persist document text and optional external embeddings in one atomic operation.
    /// Participates in the active SQL transaction, including savepoints.
    pub fn store_memory(&self, document: &MemoryDocument) -> Result<u64> {
        self.write_memory_batch(std::slice::from_ref(document), false, unix_time())
            .map(|ids| ids[0])
    }

    /// Insert or replace a source using its external ID within namespace/session.
    pub fn upsert_memory(&self, document: &MemoryDocument) -> Result<u64> {
        self.write_memory_batch(std::slice::from_ref(document), true, unix_time())
            .map(|ids| ids[0])
    }

    /// Insert sources in one atomic statement, joining any active SQL transaction.
    pub fn store_memory_batch(&self, documents: &[MemoryDocument]) -> Result<Vec<u64>> {
        self.write_memory_batch(documents, false, unix_time())
    }

    /// Idempotent batch source replacement in one atomic statement.
    pub fn upsert_memory_batch(&self, documents: &[MemoryDocument]) -> Result<Vec<u64>> {
        self.write_memory_batch(documents, true, unix_time())
    }

    fn write_memory_batch(
        &self,
        documents: &[MemoryDocument],
        upsert: bool,
        now: u64,
    ) -> Result<Vec<u64>> {
        if documents.is_empty() {
            return Ok(Vec::new());
        }
        if documents.len() > self.memory.read().configuration().max_batch_documents {
            return Err(Error::ConstraintViolation(
                "Memory batch exceeds max_batch_documents".into(),
            ));
        }
        self.mutate_memory(|memory, executor, pager| {
            let mut ids = Vec::with_capacity(documents.len());
            for document in documents {
                let id = memory.write_document(document, upsert, now)?;
                executor
                    .persist_memory_entry(pager, memory.get_memory(id).expect("stored source"))?;
                ids.push(id);
            }
            Ok(ids)
        })
    }

    /// Configure resource limits for this open connection and its cloned handles.
    /// Reapply on reopen, or specify Config::memory when opening the database.
    pub fn configure_memory(&self, config: MemoryConfig) -> Result<()> {
        self.memory.write().configure(config)
    }

    /// Report estimated source/index admission usage, not measured process RSS.
    pub fn memory_stats(&self) -> MemoryStats {
        self.memory.read().stats()
    }

    /// Materialize pending derived vector indexes before latency-sensitive queries.
    pub fn prepare_memory_indexes(&self) -> Result<MemoryStats> {
        self.memory.write().prepare_indexes()
    }

    /// Physically purge sources whose absolute expiry has passed.
    pub fn purge_expired_memories(&self) -> Result<usize> {
        self.purge_expired_memories_at(unix_time())
    }

    /// Purge expiry using an explicit Unix timestamp for deterministic maintenance.
    pub fn purge_expired_memories_at(&self, now: u64) -> Result<usize> {
        self.mutate_memory(|memory, executor, pager| {
            let ids: Vec<_> = memory
                .all_entries()
                .values()
                .filter(|entry| entry.expires_at.is_some_and(|expiry| expiry <= now))
                .map(|entry| entry.id)
                .collect();
            let (removed, updated) = memory.remove_ids(&ids);
            for id in &removed {
                executor.delete_memory_entry(pager, *id)?;
            }
            for id in updated {
                executor.persist_memory_entry(
                    pager,
                    memory.get_memory(id).expect("remaining associated source"),
                )?;
            }
            Ok(removed.len())
        })
    }

    /// Delete sources matching explicit application filters in one transaction.
    /// Empty scopes are rejected unless all=true is deliberately supplied.
    pub fn delete_memories(&self, scope: &MemoryDeleteScope) -> Result<usize> {
        scope.validate()?;
        self.mutate_memory(|memory, executor, pager| {
            let ids: Vec<_> = memory
                .all_entries()
                .values()
                .filter(|entry| scope.matches(entry))
                .map(|entry| entry.id)
                .collect();
            let (removed, updated) = memory.remove_ids(&ids);
            for id in &removed {
                executor.delete_memory_entry(pager, *id)?;
            }
            for id in updated {
                executor.persist_memory_entry(
                    pager,
                    memory.get_memory(id).expect("remaining associated source"),
                )?;
            }
            Ok(removed.len())
        })
    }

    /// Return source text/IDs and separate lexical, cosine and recency scores.
    /// No embedding model or cloud service is invoked by this method.
    pub fn search_memories(&self, search: &MemorySearch) -> Result<Vec<MemoryRecallResult>> {
        Ok(self.search_memories_detailed(search)?.results)
    }

    /// Indexed retrieval with candidate counts and ranking diagnostics.
    pub fn search_memories_detailed(&self, search: &MemorySearch) -> Result<MemorySearchResponse> {
        self.search_memories_at(search, unix_time())
    }

    /// Indexed retrieval evaluated at an explicit Unix timestamp.
    pub fn search_memories_at(
        &self,
        search: &MemorySearch,
        now: u64,
    ) -> Result<MemorySearchResponse> {
        self.pager.read().ensure_io_ready()?;
        self.memory.write().search_indexed(search, now)
    }
}

pub(crate) fn unix_time() -> u64 {
    web_time::SystemTime::now()
        .duration_since(web_time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}

impl MemorySearch {
    pub(crate) fn validate(&self, config: &MemoryConfig) -> Result<()> {
        let reject = |text: &str| Error::ConstraintViolation(text.into());
        if self.query.as_ref().is_none_or(|q| q.trim().is_empty()) && self.embedding.is_none() {
            return Err(reject("Supply query text or an embedding"));
        }
        if self.query.as_ref().is_some_and(|q| q.len() > 65536) {
            return Err(reject("Memory query exceeds 65536 bytes"));
        }
        if self.limit > self.candidate_limit
            || self.candidate_limit == 0
            || self.candidate_limit > config.max_candidates
        {
            return Err(reject(
                "Require limit <= candidate_limit <= configured max_candidates",
            ));
        }
        if self.embedding.as_ref().is_some_and(|v| {
            v.is_empty()
                || v.len() > config.max_vector_dimensions
                || v.iter().any(|x| !x.is_finite())
        }) {
            return Err(reject("Invalid query embedding"));
        }
        let weights = [
            self.vector_weight,
            self.lexical_weight,
            self.recency_weight,
            self.importance_weight,
        ];
        if weights
            .iter()
            .any(|w| !w.is_finite() || !(0.0..=1_000_000.0).contains(w))
            || weights.iter().sum::<f32>() <= 0.0
            || (if self.query.is_some() {
                self.lexical_weight
            } else {
                0.0
            }) + (if self.embedding.is_some() {
                self.vector_weight
            } else {
                0.0
            }) <= 0.0
        {
            return Err(reject(
                "Memory ranking requires finite nonnegative weights and a positive query-channel weight",
            ));
        }
        if !self.rrf_k.is_finite()
            || !(1.0..=1_000_000.0).contains(&self.rrf_k)
            || !self.min_score.is_finite()
            || !(0.0..=1.0).contains(&self.min_score)
        {
            return Err(reject("Invalid RRF constant or minimum score"));
        }
        Ok(())
    }
}

/// Explicit selectors for atomic source deletion; filters are not authorization.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct MemoryDeleteScope {
    /// Namespace, compared case insensitively.
    pub namespace: Option<String>,
    /// Exact session ID.
    pub session_id: Option<String>,
    /// Exact external source ID.
    pub external_id: Option<String>,
    /// Exact application source revision.
    pub source_version: Option<String>,
    /// Delete versions other than this one, with another inclusive selector.
    pub except_source_version: Option<String>,
    /// Match at least one case-insensitive tag.
    pub tags: Vec<String>,
    /// Deliberately permit deleting every source.
    pub all: bool,
}
impl MemoryDeleteScope {
    fn validate(&self) -> Result<()> {
        if !self.all
            && self.namespace.is_none()
            && self.session_id.is_none()
            && self.external_id.is_none()
            && self.source_version.is_none()
            && self.tags.is_empty()
        {
            return Err(Error::ConstraintViolation(
                "Deletion requires explicit source filters or all=true".into(),
            ));
        }
        Ok(())
    }
    fn matches(&self, entry: &super::MemoryEntry) -> bool {
        self.namespace.as_ref().is_none_or(|ns| {
            entry
                .namespace
                .as_ref()
                .is_some_and(|value| value.to_lowercase() == ns.to_lowercase())
        }) && self
            .session_id
            .as_ref()
            .is_none_or(|id| entry.session_id.as_ref() == Some(id))
            && self
                .external_id
                .as_ref()
                .is_none_or(|id| entry.external_id.as_ref() == Some(id))
            && self
                .source_version
                .as_ref()
                .is_none_or(|version| entry.source_version.as_ref() == Some(version))
            && self
                .except_source_version
                .as_ref()
                .is_none_or(|version| entry.source_version.as_ref() != Some(version))
            && (self.tags.is_empty()
                || self.tags.iter().any(|tag| {
                    entry
                        .tags
                        .iter()
                        .any(|value| value.to_lowercase() == tag.to_lowercase())
                }))
    }
}