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
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
//! Safety gate for content filtering and PII redaction.
//!
//! The safety gate inspects captured content before it enters the
//! ingestion pipeline, detecting and optionally redacting sensitive
//! information such as credit card numbers, SSNs, and custom patterns.

use crate::config::SafetyConfig;

/// Decision made by the safety gate about a piece of content.
#[derive(Debug, Clone, PartialEq)]
pub enum SafetyDecision {
    /// Content is safe to store as-is.
    Allow,
    /// Content is safe after redaction; the redacted version is provided.
    AllowRedacted(String),
    /// Content must not be stored.
    Deny {
        /// Reason for denial.
        reason: String,
    },
}

/// Safety gate that checks content for sensitive information.
pub struct SafetyGate {
    config: SafetyConfig,
}

impl SafetyGate {
    /// Create a new safety gate with the given configuration.
    pub fn new(config: SafetyConfig) -> Self {
        Self { config }
    }

    /// Check content and return a safety decision.
    ///
    /// If PII is detected and redaction is enabled, the content is
    /// returned in redacted form. If custom patterns match and no
    /// redaction is possible, the content is denied.
    pub fn check(&self, content: &str) -> SafetyDecision {
        let mut redacted = content.to_string();
        let mut was_redacted = false;

        // Credit card redaction
        if self.config.credit_card_redaction {
            let (new_text, found) = redact_credit_cards(&redacted);
            if found {
                redacted = new_text;
                was_redacted = true;
            }
        }

        // SSN redaction
        if self.config.ssn_redaction {
            let (new_text, found) = redact_ssns(&redacted);
            if found {
                redacted = new_text;
                was_redacted = true;
            }
        }

        // PII detection (email addresses)
        if self.config.pii_detection {
            let (new_text, found) = redact_emails(&redacted);
            if found {
                redacted = new_text;
                was_redacted = true;
            }
        }

        // Custom patterns: deny if found (custom patterns indicate content
        // that should not be stored at all)
        for pattern in &self.config.custom_patterns {
            if content.contains(pattern.as_str()) {
                return SafetyDecision::Deny {
                    reason: format!("Custom pattern matched: {}", pattern),
                };
            }
        }

        if was_redacted {
            SafetyDecision::AllowRedacted(redacted)
        } else {
            SafetyDecision::Allow
        }
    }

    /// Redact all detected sensitive content and return the cleaned string.
    pub fn redact(&self, content: &str) -> String {
        match self.check(content) {
            SafetyDecision::Allow => content.to_string(),
            SafetyDecision::AllowRedacted(redacted) => redacted,
            SafetyDecision::Deny { .. } => "[REDACTED]".to_string(),
        }
    }
}

/// Detect and redact sequences of 13-16 digits that look like credit card numbers.
///
/// This uses a simple pattern: sequences of digits (with optional spaces or dashes)
/// totaling 13-16 digits are replaced with [CC_REDACTED].
fn redact_credit_cards(text: &str) -> (String, bool) {
    let mut result = String::with_capacity(text.len());
    let chars: Vec<char> = text.chars().collect();
    let mut i = 0;
    let mut found = false;

    while i < chars.len() {
        // Check if we are at the start of a digit sequence
        if chars[i].is_ascii_digit() {
            let start = i;
            let mut digit_count = 0;

            // Consume digits, spaces, and dashes
            while i < chars.len()
                && (chars[i].is_ascii_digit() || chars[i] == ' ' || chars[i] == '-')
            {
                if chars[i].is_ascii_digit() {
                    digit_count += 1;
                }
                i += 1;
            }

            if (13..=16).contains(&digit_count) {
                result.push_str("[CC_REDACTED]");
                found = true;
            } else {
                // Not a credit card, keep original text
                for c in &chars[start..i] {
                    result.push(*c);
                }
            }
        } else {
            result.push(chars[i]);
            i += 1;
        }
    }

    (result, found)
}

/// Detect and redact SSN patterns (XXX-XX-XXXX).
fn redact_ssns(text: &str) -> (String, bool) {
    let mut result = String::new();
    let chars: Vec<char> = text.chars().collect();
    let mut found = false;
    let mut i = 0;

    while i < chars.len() {
        // Check for SSN pattern: 3 digits, dash, 2 digits, dash, 4 digits
        if i + 10 < chars.len() && is_ssn_at(&chars, i) {
            result.push_str("[SSN_REDACTED]");
            found = true;
            i += 11; // Skip the SSN (XXX-XX-XXXX = 11 chars)
        } else {
            result.push(chars[i]);
            i += 1;
        }
    }

    (result, found)
}

/// Check if an SSN pattern exists at the given position.
fn is_ssn_at(chars: &[char], pos: usize) -> bool {
    if pos + 10 >= chars.len() {
        return false;
    }
    // XXX-XX-XXXX
    chars[pos].is_ascii_digit()
        && chars[pos + 1].is_ascii_digit()
        && chars[pos + 2].is_ascii_digit()
        && chars[pos + 3] == '-'
        && chars[pos + 4].is_ascii_digit()
        && chars[pos + 5].is_ascii_digit()
        && chars[pos + 6] == '-'
        && chars[pos + 7].is_ascii_digit()
        && chars[pos + 8].is_ascii_digit()
        && chars[pos + 9].is_ascii_digit()
        && chars[pos + 10].is_ascii_digit()
}

/// Detect and redact email addresses while preserving surrounding whitespace.
///
/// Scans character-by-character for `@` signs, then expands outward to find
/// the full `local@domain.tld` span and replaces it in-place, keeping all
/// surrounding whitespace (tabs, newlines, multi-space runs) intact.
fn redact_emails(text: &str) -> (String, bool) {
    let chars: Vec<char> = text.chars().collect();
    let len = chars.len();
    let mut result = String::with_capacity(text.len());
    let mut found = false;
    let mut i = 0;

    while i < len {
        if chars[i] == '@' {
            // Try to identify an email around this '@'.
            // Scan backwards for the local part.
            let mut local_start = i;
            while local_start > 0 && is_email_local_char(chars[local_start - 1]) {
                local_start -= 1;
            }

            // Scan forwards for the domain part.
            let mut domain_end = i + 1;
            let mut has_dot = false;
            while domain_end < len && is_email_domain_char(chars[domain_end]) {
                if chars[domain_end] == '.' {
                    has_dot = true;
                }
                domain_end += 1;
            }
            // Trim trailing dots/hyphens from domain (not valid at end).
            while domain_end > i + 1
                && (chars[domain_end - 1] == '.' || chars[domain_end - 1] == '-')
            {
                if chars[domain_end - 1] == '.' {
                    // Re-check if we still have a dot in the trimmed domain.
                    has_dot = chars[i + 1..domain_end - 1].contains(&'.');
                }
                domain_end -= 1;
            }

            let local_len = i - local_start;
            let domain_len = domain_end - (i + 1);

            if local_len > 0 && domain_len >= 3 && has_dot {
                // Valid email: replace the span [local_start..domain_end]
                // We need to remove any characters already pushed for the local part.
                // They were pushed in the normal flow below, so truncate them.
                let already_pushed = i - local_start;
                let new_len = result.len() - already_pushed;
                result.truncate(new_len);
                result.push_str("[EMAIL_REDACTED]");
                found = true;
                i = domain_end;
            } else {
                // Not a valid email, keep the '@' as-is.
                result.push(chars[i]);
                i += 1;
            }
        } else {
            result.push(chars[i]);
            i += 1;
        }
    }

    (result, found)
}

/// Characters valid in the local part of an email address.
fn is_email_local_char(c: char) -> bool {
    c.is_ascii_alphanumeric() || c == '.' || c == '+' || c == '-' || c == '_'
}

/// Characters valid in the domain part of an email address.
fn is_email_domain_char(c: char) -> bool {
    c.is_ascii_alphanumeric() || c == '.' || c == '-'
}

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

    // ---------------------------------------------------------------
    // Email redaction whitespace preservation tests
    // ---------------------------------------------------------------

    #[test]
    fn test_email_redaction_preserves_tabs() {
        let (result, found) = redact_emails("contact\tuser@example.com\there");
        assert!(found);
        assert_eq!(result, "contact\t[EMAIL_REDACTED]\there");
    }

    #[test]
    fn test_email_redaction_preserves_newlines() {
        let (result, found) = redact_emails("contact\nuser@example.com\nhere");
        assert!(found);
        assert_eq!(result, "contact\n[EMAIL_REDACTED]\nhere");
    }

    #[test]
    fn test_email_redaction_preserves_multi_spaces() {
        let (result, found) = redact_emails("contact   user@example.com   here");
        assert!(found);
        assert_eq!(result, "contact   [EMAIL_REDACTED]   here");
    }

    #[test]
    fn test_email_redaction_preserves_mixed_whitespace() {
        let (result, found) = redact_emails("contact\t  user@example.com\n  here");
        assert!(found);
        assert_eq!(result, "contact\t  [EMAIL_REDACTED]\n  here");
    }

    #[test]
    fn test_email_redaction_basic() {
        let (result, found) = redact_emails("email user@example.com here");
        assert!(found);
        assert_eq!(result, "email [EMAIL_REDACTED] here");
    }

    #[test]
    fn test_email_redaction_no_email() {
        let (result, found) = redact_emails("no email here");
        assert!(!found);
        assert_eq!(result, "no email here");
    }

    #[test]
    fn test_email_redaction_multiple_emails() {
        let (result, found) = redact_emails("a@b.com and c@d.org");
        assert!(found);
        assert_eq!(result, "[EMAIL_REDACTED] and [EMAIL_REDACTED]");
    }

    #[test]
    fn test_email_redaction_at_start() {
        let (result, found) = redact_emails("user@example.com is the contact");
        assert!(found);
        assert_eq!(result, "[EMAIL_REDACTED] is the contact");
    }

    #[test]
    fn test_email_redaction_at_end() {
        let (result, found) = redact_emails("contact: user@example.com");
        assert!(found);
        assert_eq!(result, "contact: [EMAIL_REDACTED]");
    }

    // ---------------------------------------------------------------
    // Safety gate integration tests for consistency
    // ---------------------------------------------------------------

    #[test]
    fn test_safety_gate_email_preserves_whitespace() {
        let config = SafetyConfig::default();
        let gate = SafetyGate::new(config);
        let decision = gate.check("contact\tuser@example.com\nhere");
        match decision {
            SafetyDecision::AllowRedacted(redacted) => {
                assert_eq!(redacted, "contact\t[EMAIL_REDACTED]\nhere");
            }
            other => panic!("Expected AllowRedacted, got {:?}", other),
        }
    }

    // ---------------------------------------------------------------
    // Routing consistency tests (WASM vs native)
    // ---------------------------------------------------------------

    #[test]
    fn test_wasm_routing_matches_native_temporal() {
        use crate::search::router::QueryRouter;
        use crate::search::router::QueryRoute;
        use crate::wasm::helpers::route_query;

        let router = QueryRouter::new();
        let queries = [
            "what did I see yesterday",
            "show me last week",
            "results from today",
        ];
        for q in &queries {
            assert_eq!(
                router.route(q),
                QueryRoute::Temporal,
                "Native router failed for: {}", q
            );
            assert_eq!(
                route_query(q),
                "Temporal",
                "WASM router failed for: {}", q
            );
        }
    }

    #[test]
    fn test_wasm_routing_matches_native_graph() {
        use crate::search::router::QueryRouter;
        use crate::search::router::QueryRoute;
        use crate::wasm::helpers::route_query;

        let router = QueryRouter::new();
        let queries = [
            "documents related to authentication",
            "things connected to the API module",
        ];
        for q in &queries {
            assert_eq!(
                router.route(q),
                QueryRoute::Graph,
                "Native router failed for: {}", q
            );
            assert_eq!(
                route_query(q),
                "Graph",
                "WASM router failed for: {}", q
            );
        }
    }

    #[test]
    fn test_wasm_routing_matches_native_keyword_short() {
        use crate::search::router::QueryRouter;
        use crate::search::router::QueryRoute;
        use crate::wasm::helpers::route_query;

        let router = QueryRouter::new();
        let queries = [
            "hello",
            "rust programming",
        ];
        for q in &queries {
            assert_eq!(
                router.route(q),
                QueryRoute::Keyword,
                "Native router failed for: {}", q
            );
            assert_eq!(
                route_query(q),
                "Keyword",
                "WASM router failed for: {}", q
            );
        }
    }

    #[test]
    fn test_wasm_routing_matches_native_keyword_quoted() {
        use crate::search::router::QueryRouter;
        use crate::search::router::QueryRoute;
        use crate::wasm::helpers::route_query;

        let router = QueryRouter::new();
        let q = "\"exact phrase search\"";
        assert_eq!(router.route(q), QueryRoute::Keyword);
        assert_eq!(route_query(q), "Keyword");
    }

    #[test]
    fn test_wasm_routing_matches_native_hybrid() {
        use crate::search::router::QueryRouter;
        use crate::search::router::QueryRoute;
        use crate::wasm::helpers::route_query;

        let router = QueryRouter::new();
        let queries = [
            "how to implement authentication in Rust",
            "explain how embeddings work",
            "something about machine learning",
        ];
        for q in &queries {
            assert_eq!(
                router.route(q),
                QueryRoute::Hybrid,
                "Native router failed for: {}", q
            );
            assert_eq!(
                route_query(q),
                "Hybrid",
                "WASM router failed for: {}", q
            );
        }
    }

    // ---------------------------------------------------------------
    // Safety consistency tests (WASM vs native)
    // ---------------------------------------------------------------

    #[test]
    fn test_wasm_safety_matches_native_cc() {
        use crate::wasm::helpers::safety_classify;

        // Native: CC -> AllowRedacted; WASM should return "redact"
        let config = SafetyConfig::default();
        let gate = SafetyGate::new(config);
        let content = "pay with 4111-1111-1111-1111";
        assert!(matches!(gate.check(content), SafetyDecision::AllowRedacted(_)));
        assert_eq!(safety_classify(content), "redact");
    }

    #[test]
    fn test_wasm_safety_matches_native_ssn() {
        use crate::wasm::helpers::safety_classify;

        let config = SafetyConfig::default();
        let gate = SafetyGate::new(config);
        let content = "my ssn 123-45-6789";
        assert!(matches!(gate.check(content), SafetyDecision::AllowRedacted(_)));
        assert_eq!(safety_classify(content), "redact");
    }

    #[test]
    fn test_wasm_safety_matches_native_email() {
        use crate::wasm::helpers::safety_classify;

        let config = SafetyConfig::default();
        let gate = SafetyGate::new(config);
        let content = "email user@example.com here";
        assert!(matches!(gate.check(content), SafetyDecision::AllowRedacted(_)));
        assert_eq!(safety_classify(content), "redact");
    }

    #[test]
    fn test_wasm_safety_matches_native_custom_deny() {
        use crate::wasm::helpers::safety_classify;

        // Native: custom_patterns -> Deny; WASM: sensitive keywords -> "deny"
        let config = SafetyConfig {
            custom_patterns: vec!["password".to_string()],
            ..Default::default()
        };
        let gate = SafetyGate::new(config);
        let content = "my password is foo";
        assert!(matches!(gate.check(content), SafetyDecision::Deny { .. }));
        assert_eq!(safety_classify(content), "deny");
    }

    #[test]
    fn test_wasm_safety_matches_native_allow() {
        use crate::wasm::helpers::safety_classify;

        let config = SafetyConfig::default();
        let gate = SafetyGate::new(config);
        let content = "the weather is nice";
        assert_eq!(gate.check(content), SafetyDecision::Allow);
        assert_eq!(safety_classify(content), "allow");
    }

    // ---------------------------------------------------------------
    // MMR tests
    // ---------------------------------------------------------------

    #[test]
    fn test_mmr_produces_different_order_than_cosine() {
        use crate::search::mmr::MmrReranker;

        let mmr = MmrReranker::new(0.3);
        let query = vec![1.0, 0.0, 0.0, 0.0];
        let results = vec![
            ("a".to_string(), 0.95, vec![1.0, 0.0, 0.0, 0.0]),
            ("b".to_string(), 0.90, vec![0.99, 0.01, 0.0, 0.0]),
            ("c".to_string(), 0.60, vec![0.0, 1.0, 0.0, 0.0]),
        ];

        let ranked = mmr.rerank(&query, &results, 3);
        assert_eq!(ranked.len(), 3);

        // Pure cosine order: a, b, c
        // MMR with diversity: a, c, b (c is diverse, b is near-duplicate of a)
        assert_eq!(ranked[0].0, "a");
        assert_eq!(ranked[1].0, "c", "MMR should promote diverse result");
        assert_eq!(ranked[2].0, "b");
    }
}