ospipe 0.1.0

OSpipe: RuVector-enhanced personal AI memory system integrating with Screenpipe
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
//! Pure helper functions used by the WASM bindings.
//!
//! These functions have no WASM dependencies and can be tested on any target.

/// Cosine similarity between two vectors.
///
/// Returns 0.0 when either vector has zero magnitude.
pub fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 {
    debug_assert_eq!(a.len(), b.len(), "vectors must be same length");

    let mut dot: f32 = 0.0;
    let mut mag_a: f32 = 0.0;
    let mut mag_b: f32 = 0.0;

    for i in 0..a.len() {
        dot += a[i] * b[i];
        mag_a += a[i] * a[i];
        mag_b += b[i] * b[i];
    }

    let denom = mag_a.sqrt() * mag_b.sqrt();
    if denom == 0.0 {
        0.0
    } else {
        dot / denom
    }
}

/// Produce a deterministic pseudo-embedding from text using a simple hash.
///
/// The algorithm:
/// 1. Hash each character position into a seed.
/// 2. Use the seed to generate a float in [-1, 1].
/// 3. L2-normalise the resulting vector.
///
/// This is NOT a real embedding model -- it is only useful for demos and
/// testing that the WASM plumbing works end-to-end.
pub fn hash_embed(text: &str, dimension: usize) -> Vec<f32> {
    let mut vec = vec![0.0f32; dimension];
    let bytes = text.as_bytes();

    for (i, slot) in vec.iter_mut().enumerate() {
        // Mix byte values into the slot.
        let mut h: u64 = 0xcbf29ce484222325; // FNV-1a offset basis
        for (j, &b) in bytes.iter().enumerate() {
            h ^= (b as u64).wrapping_add((i as u64).wrapping_mul(31)).wrapping_add(j as u64);
            h = h.wrapping_mul(0x100000001b3); // FNV-1a prime
        }
        // Map to [-1, 1].
        *slot = ((h as i64) as f64 / i64::MAX as f64) as f32;
    }

    // L2 normalise.
    let mag: f32 = vec.iter().map(|v| v * v).sum::<f32>().sqrt();
    if mag > 0.0 {
        for v in &mut vec {
            *v /= mag;
        }
    }

    vec
}

/// Check for credit-card-like patterns: 4 groups of 4 digits separated by
/// spaces or dashes (or no separator).
pub fn has_credit_card_pattern(content: &str) -> bool {
    // Strategy: scan for sequences of 16 digits (possibly with separators).
    let digits_only: String = content.chars().filter(|c| c.is_ascii_digit()).collect();

    // Quick check: must have at least 16 digits somewhere.
    if digits_only.len() < 16 {
        return false;
    }

    // Look for the formatted pattern: DDDD[-/ ]DDDD[-/ ]DDDD[-/ ]DDDD
    // We do a simple windowed scan on the original string.
    let chars: Vec<char> = content.chars().collect();
    let len = chars.len();
    let mut i = 0;

    while i < len {
        if let Some(end) = try_parse_cc_at(&chars, i) {
            // Verify the group doesn't continue with more digits (avoid
            // matching longer numeric strings that aren't cards).
            if end >= len || !chars[end].is_ascii_digit() {
                // Also make sure it didn't start as part of a longer number.
                if i == 0 || !chars[i - 1].is_ascii_digit() {
                    return true;
                }
            }
            i = end;
        } else {
            i += 1;
        }
    }

    false
}

/// Try to parse a credit-card-like pattern starting at position `start`.
/// Returns the index past the last consumed character on success.
fn try_parse_cc_at(chars: &[char], start: usize) -> Option<usize> {
    let mut pos = start;
    for group in 0..4 {
        // Expect 4 digits.
        for _ in 0..4 {
            if pos >= chars.len() || !chars[pos].is_ascii_digit() {
                return None;
            }
            pos += 1;
        }
        // After the first 3 groups, allow an optional separator.
        if group < 3
            && pos < chars.len() && (chars[pos] == '-' || chars[pos] == ' ') {
                pos += 1;
            }
    }
    Some(pos)
}

/// Check for SSN-like patterns: XXX-XX-XXXX
pub fn has_ssn_pattern(content: &str) -> bool {
    let chars: Vec<char> = content.chars().collect();
    let len = chars.len();

    // Pattern length: 3 + 1 + 2 + 1 + 4 = 11
    if len < 11 {
        return false;
    }

    for i in 0..=len - 11 {
        // Must not be preceded by a digit.
        if i > 0 && chars[i - 1].is_ascii_digit() {
            continue;
        }
        // Must not be followed by a digit.
        if i + 11 < len && chars[i + 11].is_ascii_digit() {
            continue;
        }

        if chars[i].is_ascii_digit()
            && chars[i + 1].is_ascii_digit()
            && chars[i + 2].is_ascii_digit()
            && chars[i + 3] == '-'
            && chars[i + 4].is_ascii_digit()
            && chars[i + 5].is_ascii_digit()
            && chars[i + 6] == '-'
            && chars[i + 7].is_ascii_digit()
            && chars[i + 8].is_ascii_digit()
            && chars[i + 9].is_ascii_digit()
            && chars[i + 10].is_ascii_digit()
        {
            return true;
        }
    }

    false
}

/// Simple safety classification for content.
///
/// Returns `"deny"`, `"redact"`, or `"allow"`.
///
/// Classification matches native `SafetyGate::check`:
/// - Credit card patterns -> "redact"
/// - SSN patterns -> "redact"
/// - Email patterns -> "redact"
/// - Custom sensitive keywords -> "deny"
pub fn safety_classify(content: &str) -> &'static str {
    // PII patterns are redacted (matching native SafetyGate behavior)
    if has_credit_card_pattern(content) {
        return "redact";
    }
    if has_ssn_pattern(content) {
        return "redact";
    }
    if has_email_pattern(content) {
        return "redact";
    }

    // Custom sensitive keywords are denied (matching native custom_patterns -> Deny)
    let lower = content.to_lowercase();
    let deny_keywords = [
        "password",
        "secret",
        "api_key",
        "api-key",
        "apikey",
        "token",
        "private_key",
        "private-key",
    ];
    for kw in &deny_keywords {
        if lower.contains(kw) {
            return "deny";
        }
    }

    "allow"
}

/// Check for email-like patterns: local@domain.tld
pub fn has_email_pattern(content: &str) -> bool {
    let chars: Vec<char> = content.chars().collect();
    let len = chars.len();

    for i in 0..len {
        if chars[i] == '@' {
            // Must have at least one local-part char before '@'
            if i == 0 || chars[i - 1].is_whitespace() {
                continue;
            }
            // Must have at least one domain char and a dot after '@'
            if i + 1 >= len || chars[i + 1].is_whitespace() {
                continue;
            }
            // Scan backwards to find start of local part
            let mut start = i;
            while start > 0 && is_email_char(chars[start - 1]) {
                start -= 1;
            }
            if start == i {
                continue;
            }
            // Scan forwards to find end of domain
            let mut end = i + 1;
            let mut has_dot = false;
            while end < len && is_domain_char(chars[end]) {
                if chars[end] == '.' {
                    has_dot = true;
                }
                end += 1;
            }
            if has_dot && end > i + 3 {
                return true;
            }
        }
    }
    false
}

fn is_email_char(c: char) -> bool {
    c.is_ascii_alphanumeric() || c == '.' || c == '+' || c == '-' || c == '_'
}

fn is_domain_char(c: char) -> bool {
    c.is_ascii_alphanumeric() || c == '.' || c == '-'
}

/// Route a query string to the optimal search backend.
///
/// Returns `"Temporal"`, `"Graph"`, `"Keyword"`, or `"Hybrid"`.
///
/// Routing heuristics (matching native `QueryRouter::route`):
/// - Temporal keywords ("yesterday", "last week", etc.) -> Temporal
/// - Graph keywords ("related to", "connected to", etc.) -> Graph
/// - Quoted exact phrases -> Keyword
/// - Short queries (1-2 words) -> Keyword
/// - Everything else -> Hybrid
pub fn route_query(query: &str) -> &'static str {
    let lower = query.to_lowercase();
    let word_count = lower.split_whitespace().count();

    // Temporal patterns (checked first, matching native router order)
    let temporal_keywords = [
        "yesterday",
        "last week",
        "last month",
        "today",
        "this morning",
        "this afternoon",
        "hours ago",
        "minutes ago",
        "days ago",
        "between",
        "before",
        "after",
    ];
    for kw in &temporal_keywords {
        if lower.contains(kw) {
            return "Temporal";
        }
    }

    // Graph patterns
    let graph_keywords = [
        "related to",
        "connected to",
        "linked with",
        "associated with",
        "relationship between",
    ];
    for kw in &graph_keywords {
        if lower.contains(kw) {
            return "Graph";
        }
    }

    // Exact phrase (quoted)
    if query.starts_with('"') && query.ends_with('"') {
        return "Keyword";
    }

    // Very short queries are better served by keyword
    if word_count <= 2 {
        return "Keyword";
    }

    // Default: hybrid combines the best of both
    "Hybrid"
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_cosine_similarity_identical() {
        let v = vec![1.0, 2.0, 3.0];
        let sim = cosine_similarity(&v, &v);
        assert!((sim - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_cosine_similarity_orthogonal() {
        let a = vec![1.0, 0.0];
        let b = vec![0.0, 1.0];
        let sim = cosine_similarity(&a, &b);
        assert!(sim.abs() < 1e-6);
    }

    #[test]
    fn test_cosine_similarity_opposite() {
        let a = vec![1.0, 0.0];
        let b = vec![-1.0, 0.0];
        let sim = cosine_similarity(&a, &b);
        assert!((sim + 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_cosine_similarity_zero_vector() {
        let a = vec![0.0, 0.0];
        let b = vec![1.0, 2.0];
        assert_eq!(cosine_similarity(&a, &b), 0.0);
    }

    #[test]
    fn test_hash_embed_deterministic() {
        let v1 = hash_embed("hello world", 128);
        let v2 = hash_embed("hello world", 128);
        assert_eq!(v1, v2);
    }

    #[test]
    fn test_hash_embed_normalized() {
        let v = hash_embed("test text", 64);
        let mag: f32 = v.iter().map(|x| x * x).sum::<f32>().sqrt();
        assert!((mag - 1.0).abs() < 1e-4, "magnitude should be ~1.0, got {mag}");
    }

    #[test]
    fn test_hash_embed_different_texts_differ() {
        let v1 = hash_embed("hello", 64);
        let v2 = hash_embed("world", 64);
        assert_ne!(v1, v2);
    }

    #[test]
    fn test_has_credit_card_pattern() {
        assert!(has_credit_card_pattern("my card is 1234 5678 9012 3456"));
        assert!(has_credit_card_pattern("cc: 1234-5678-9012-3456"));
        assert!(has_credit_card_pattern("number 1234567890123456 here"));
        assert!(!has_credit_card_pattern("short 123456"));
        assert!(!has_credit_card_pattern("no cards here"));
    }

    #[test]
    fn test_has_ssn_pattern() {
        assert!(has_ssn_pattern("ssn is 123-45-6789"));
        assert!(has_ssn_pattern("start 999-99-9999 end"));
        assert!(!has_ssn_pattern("not a ssn 12-345-6789"));
        assert!(!has_ssn_pattern("1234-56-7890")); // preceded by extra digit
        assert!(!has_ssn_pattern("no ssn here"));
    }

    #[test]
    fn test_safety_classify_redact_cc() {
        assert_eq!(safety_classify("pay with 4111-1111-1111-1111"), "redact");
    }

    #[test]
    fn test_safety_classify_redact_ssn() {
        assert_eq!(safety_classify("my ssn 123-45-6789"), "redact");
    }

    #[test]
    fn test_safety_classify_redact_email() {
        assert_eq!(safety_classify("contact user@example.com"), "redact");
    }

    #[test]
    fn test_safety_classify_deny_password() {
        assert_eq!(safety_classify("my password is foo"), "deny");
    }

    #[test]
    fn test_safety_classify_deny_api_key() {
        assert_eq!(safety_classify("api_key: sk-abc123"), "deny");
    }

    #[test]
    fn test_safety_classify_allow() {
        assert_eq!(safety_classify("the weather is nice"), "allow");
    }

    #[test]
    fn test_has_email_pattern() {
        assert!(has_email_pattern("contact user@example.com please"));
        assert!(has_email_pattern("email: alice@test.org"));
        assert!(!has_email_pattern("not an email"));
        assert!(!has_email_pattern("@ alone"));
        assert!(!has_email_pattern("no@d"));
    }

    #[test]
    fn test_route_query_temporal() {
        assert_eq!(route_query("what happened yesterday"), "Temporal");
        assert_eq!(route_query("show me events from last week"), "Temporal");
    }

    #[test]
    fn test_route_query_graph() {
        assert_eq!(route_query("documents related to authentication"), "Graph");
        assert_eq!(route_query("things connected to the API module"), "Graph");
    }

    #[test]
    fn test_route_query_keyword_quoted() {
        assert_eq!(route_query("\"exact phrase search\""), "Keyword");
    }

    #[test]
    fn test_route_query_keyword_short() {
        assert_eq!(route_query("rust programming"), "Keyword");
        assert_eq!(route_query("hello"), "Keyword");
    }

    #[test]
    fn test_route_query_hybrid() {
        assert_eq!(route_query("something about machine learning"), "Hybrid");
        assert_eq!(route_query("explain how embeddings work"), "Hybrid");
    }
}