remem-ai 0.6.71

Local-first coding agent memory for Claude Code and OpenAI Codex
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
786
787
788
789
790
pub(crate) fn block_reason(
    claim_text: &str,
    source_preview: Option<&str>,
    source_kind: &str,
) -> Option<&'static str> {
    let blob = normalized_blob(claim_text, source_preview);
    if contains_secret_like_content(&blob, claim_text, source_preview) {
        return Some("secret_like_content");
    }
    if source_kind == "speculative_inference"
        || contains_non_retention_pattern(&blob, SPECULATIVE_PATTERNS)
    {
        return Some("speculative_or_roleplay_content");
    }
    if contains_temporary_or_one_off_content(&blob) {
        return Some("temporary_or_one_off_content");
    }
    if contains_general_knowledge_content(&blob) {
        return Some("general_knowledge_content");
    }
    if contains_illegal_or_harmful_content(&blob) {
        return Some("illegal_or_harmful_content");
    }
    if has_external_source_pattern(&blob) && !has_external_source_approval(&blob) {
        return Some("unapproved_external_source");
    }
    None
}

pub(crate) fn has_external_source_pattern(text: &str) -> bool {
    let text = text.to_ascii_lowercase();
    contains_non_retention_pattern(&text, EXTERNAL_SOURCE_PATTERNS)
        || contains_contextual_external_source_phrase(&text)
}

pub(crate) fn has_external_source_approval(text: &str) -> bool {
    contains_external_source_approval(&text.to_ascii_lowercase())
}

fn normalized_blob(claim_text: &str, source_preview: Option<&str>) -> String {
    let mut blob = claim_text.to_ascii_lowercase();
    if let Some(preview) = source_preview {
        blob.push('\n');
        blob.push_str(&preview.to_ascii_lowercase());
    }
    blob
}

fn contains_secret_like_content(
    blob: &str,
    claim_text: &str,
    source_preview: Option<&str>,
) -> bool {
    if redaction_changed_sensitive_content(claim_text) {
        return true;
    }
    if let Some(preview) = source_preview {
        if redaction_changed_sensitive_content(preview) {
            return true;
        }
    }
    contains_secret_key_token(blob)
        || contains_token_secret_phrase(blob)
        || contains_access_key_phrase(blob)
        || contains_payment_card_number(blob)
        || contains_non_retention_pattern(blob, SECRET_PATTERNS)
        || (blob.contains("[redacted")
            && contains_non_retention_pattern(blob, SECRET_REDACTION_CONTEXT))
}

fn redaction_changed_sensitive_content(text: &str) -> bool {
    let redacted = crate::adapter::common::redact_sensitive_text(text);
    redacted.contains("[REDACTED]")
        && normalized_redaction_compare(&redacted) != normalized_redaction_compare(text)
}

fn normalized_redaction_compare(text: &str) -> String {
    text.split_whitespace().collect::<Vec<_>>().join(" ")
}

fn contains_non_retention_pattern(haystack: &str, needles: &[&str]) -> bool {
    needles
        .iter()
        .any(|needle| contains_bounded_phrase(haystack, needle))
}

fn contains_bounded_phrase(haystack: &str, needle: &str) -> bool {
    haystack
        .match_indices(needle)
        .any(|(start, _)| bounded_phrase_at(haystack, needle, start))
}

fn contains_secret_key_token(text: &str) -> bool {
    text.split(|ch: char| ch.is_ascii_whitespace() || matches!(ch, ',' | ';' | '"' | '\'' | '`'))
        .map(|token| {
            token.trim_matches(|ch: char| !ch.is_ascii_alphanumeric() && ch != '-' && ch != '_')
        })
        .any(is_secret_key_token)
}

fn is_secret_key_token(token: &str) -> bool {
    let Some(rest) = token.strip_prefix("sk-") else {
        return false;
    };
    !rest.is_empty()
        && rest
            .chars()
            .all(|ch| ch.is_ascii_alphanumeric() || ch == '-' || ch == '_')
        && (rest.len() >= 20 || (rest.len() >= 10 && rest.chars().any(|ch| ch.is_ascii_digit())))
}

fn contains_token_secret_phrase(text: &str) -> bool {
    let tokens = lexical_tokens(text);
    tokens.iter().enumerate().any(|(index, token)| {
        if !matches!(token.as_str(), "secret" | "token") {
            return false;
        }
        match (tokens.get(index + 1), tokens.get(index + 2)) {
            (Some(next), _) if is_secret_value_token(next) => true,
            (Some(next), Some(value)) if next == "is" => is_secret_value_token(value),
            _ => false,
        }
    })
}

fn contains_access_key_phrase(text: &str) -> bool {
    let tokens = lexical_tokens(text);
    tokens.iter().enumerate().any(|(index, token)| {
        if token != "access" || tokens.get(index + 1).is_none_or(|next| next != "key") {
            return false;
        }
        tokens
            .iter()
            .skip(index + 2)
            .take(5)
            .filter(|candidate| {
                !matches!(
                    candidate.as_str(),
                    "id" | "is" | "key" | "secret" | "the" | "value"
                )
            })
            .any(|candidate| is_secret_value_token(candidate) || is_known_access_key_id(candidate))
    })
}

fn contains_payment_card_number(text: &str) -> bool {
    let tokens = lexical_tokens(text);
    let has_card_context = tokens.iter().any(|token| {
        matches!(
            token.as_str(),
            "amex" | "card" | "credit" | "discover" | "mastercard" | "payment" | "visa"
        )
    });
    has_card_context && contains_card_digit_sequence(text)
}

fn contains_card_digit_sequence(text: &str) -> bool {
    let mut digits = 0;
    let mut in_sequence = false;
    for ch in text.chars() {
        if ch.is_ascii_digit() {
            digits += 1;
            in_sequence = true;
            continue;
        }
        if in_sequence && matches!(ch, ' ' | '-') {
            continue;
        }
        if (13..=19).contains(&digits) {
            return true;
        }
        digits = 0;
        in_sequence = false;
    }
    (13..=19).contains(&digits)
}

fn is_known_access_key_id(token: &str) -> bool {
    matches!(token.get(..4), Some("akia" | "asia"))
        && token.len() >= 16
        && token.chars().all(|ch| ch.is_ascii_alphanumeric())
}

fn is_secret_value_token(token: &str) -> bool {
    token.len() >= 6
        && token
            .chars()
            .all(|ch| ch.is_ascii_alphanumeric() || ch == '-' || ch == '_')
        && (token.len() >= 16 || token.chars().any(|ch| ch.is_ascii_digit()))
}

fn contains_temporary_or_one_off_content(text: &str) -> bool {
    contains_non_retention_pattern(text, TEMPORARY_PATTERNS)
        || contains_today_meal_content(text)
        || contains_current_weather_content(text)
}

fn contains_today_meal_content(text: &str) -> bool {
    let tokens = lexical_tokens(text);
    let has_meal = tokens.iter().any(|token| {
        matches!(
            token.as_str(),
            "breakfast" | "dinner" | "lunch" | "meal" | "sushi"
        )
    });
    let has_current_time = tokens
        .iter()
        .any(|token| matches!(token.as_str(), "today" | "tonight" | "yesterday" | "now"));
    has_meal && has_current_time
}

fn contains_current_weather_content(text: &str) -> bool {
    let tokens = lexical_tokens(text);
    tokens.iter().any(|token| token == "weather")
        && tokens
            .iter()
            .any(|token| matches!(token.as_str(), "current" | "today" | "now"))
}

fn contains_general_knowledge_content(text: &str) -> bool {
    text.lines().any(|line| {
        let tokens = lexical_tokens(line);
        contains_non_retention_pattern(line, GENERAL_KNOWLEDGE_PATTERNS)
            && !has_retainable_user_preference_context(&tokens)
            && !has_project_context_reference(&tokens)
    }) || contains_project_independent_fact_shape(text)
}

fn contains_project_independent_fact_shape(text: &str) -> bool {
    text.lines().any(|line| {
        let tokens = lexical_tokens(line);
        let has_topic = tokens.iter().any(|token| {
            matches!(
                token.as_str(),
                "docker"
                    | "git"
                    | "http"
                    | "javascript"
                    | "linux"
                    | "postgres"
                    | "python"
                    | "rust"
                    | "sqlite"
            )
        });
        let has_factual_verb = tokens.iter().any(|token| {
            matches!(
                token.as_str(),
                "are" | "is" | "prevents" | "stores" | "supports" | "uses"
            )
        });
        has_topic
            && has_factual_verb
            && !has_retainable_user_preference_context(&tokens)
            && !has_project_context_reference(&tokens)
    })
}

fn has_project_context_reference(tokens: &[String]) -> bool {
    tokens.iter().any(|token| {
        matches!(
            token.as_str(),
            "codebase" | "project" | "repo" | "repository" | "workspace"
        )
    })
}

fn has_retainable_user_preference_context(tokens: &[String]) -> bool {
    has_user_context_reference(tokens)
        && tokens.iter().any(|token| {
            matches!(
                token.as_str(),
                "prefer" | "preferred" | "prefers" | "use" | "uses" | "work" | "works"
            )
        })
}

fn contains_illegal_or_harmful_content(text: &str) -> bool {
    contains_non_retention_pattern(text, ILLEGAL_OR_HARMFUL_PATTERNS)
        || (contains_bounded_phrase(text, "exfiltrate")
            && contains_non_retention_pattern(text, SECRET_REDACTION_CONTEXT))
}

fn contains_external_source_approval(text: &str) -> bool {
    [
        "please remember from file",
        "please remember from files",
        "please remember from readme",
        "please remember from the readme",
        "please remember from website",
        "please remember from web page",
        "please remember from browser page",
        "remember from file",
        "remember from files",
        "remember from readme",
        "remember from the readme",
        "remember from website",
        "remember from web page",
        "remember from browser page",
        "save from file",
        "save from files",
        "save from readme",
        "save from the readme",
        "save from website",
        "save from web page",
        "save from browser page",
    ]
    .iter()
    .any(|phrase| contains_non_negated_bounded_phrase(text, phrase))
}

fn contains_contextual_external_source_phrase(text: &str) -> bool {
    [
        "from a file",
        "from a readme",
        "from browser page",
        "from file",
        "from files",
        "from readme",
        "from the browser page",
        "from the web page",
        "from web page",
    ]
    .iter()
    .any(|phrase| {
        text.match_indices(phrase).any(|(start, _)| {
            bounded_phrase_at(text, phrase, start)
                && external_source_phrase_has_attribution_context(text, start, phrase.len())
        })
    })
}

fn external_source_phrase_has_attribution_context(
    text: &str,
    start: usize,
    phrase_len: usize,
) -> bool {
    let end = start + phrase_len;
    let before = text[..start].chars().rev().take(64).collect::<String>();
    let before = before.chars().rev().collect::<String>();
    let after = text[end..].chars().take(96).collect::<String>();
    let window = format!("{before}{after}");
    contains_non_retention_pattern(
        &window,
        &[
            "according to",
            "derived",
            "extracted",
            "inferred",
            "remember",
            "save",
            "source",
            "without explicit user approval",
            "without user approval",
        ],
    )
}

fn contains_non_negated_bounded_phrase(haystack: &str, needle: &str) -> bool {
    haystack.match_indices(needle).any(|(start, _)| {
        bounded_phrase_at(haystack, needle, start) && !is_negated_before(haystack, start)
    })
}

fn bounded_phrase_at(haystack: &str, needle: &str, start: usize) -> bool {
    let end = start + needle.len();
    let left_ok = !needle
        .chars()
        .next()
        .is_some_and(|ch| ch.is_ascii_alphanumeric())
        || start == 0
        || !haystack[..start]
            .chars()
            .next_back()
            .is_some_and(|ch| ch.is_ascii_alphanumeric());
    let right_ok = !needle
        .chars()
        .next_back()
        .is_some_and(|ch| ch.is_ascii_alphanumeric())
        || end >= haystack.len()
        || !haystack[end..]
            .chars()
            .next()
            .is_some_and(|ch| ch.is_ascii_alphanumeric());
    left_ok && right_ok
}

fn is_negated_before(text: &str, start: usize) -> bool {
    let tokens = lexical_tokens(&text[..start]);
    tokens
        .iter()
        .rev()
        .take(3)
        .any(|token| matches!(token.as_str(), "not" | "don't" | "dont" | "never" | "no"))
}

fn has_user_context_reference(tokens: &[String]) -> bool {
    tokens.iter().any(|token| {
        matches!(
            token.as_str(),
            "i" | "me" | "my" | "mine" | "our" | "ours" | "user" | "user's" | "we"
        )
    })
}

fn lexical_tokens(text: &str) -> Vec<String> {
    text.split(|ch: char| !(ch.is_ascii_alphanumeric() || ch == '-' || ch == '_' || ch == '\''))
        .filter(|token| !token.is_empty())
        .map(|token| token.to_ascii_lowercase())
        .collect()
}

const SECRET_PATTERNS: &[&str] = &[
    "api key is",
    "api key:",
    "api_key",
    "api token",
    "access token",
    "account number",
    "auth token",
    "bank account",
    "bearer ",
    "client secret",
    "credit card",
    "credential is",
    "driver license",
    "driver's license",
    "drivers license",
    "identity document",
    "password",
    "passport",
    "payment card",
    "private key",
    "routing number",
    "secret key",
    "social security number",
    "ssn",
];

const SECRET_REDACTION_CONTEXT: &[&str] = &[
    "api key",
    "api token",
    "authorization",
    "bearer",
    "credential",
    "credentials",
    "password",
    "private key",
    "secret",
    "token",
];

const SPECULATIVE_PATTERNS: &[&str] = &[
    "as a joke",
    "fictional",
    "guessed",
    "hypothetical",
    "if i were",
    "if the user were",
    "imagine",
    "may be",
    "might be",
    "pretend",
    "probably",
    "role-play",
    "roleplay",
    "sarcasm",
    "suppose",
];

const TEMPORARY_PATTERNS: &[&str] = &[
    "fatigue",
    "mood",
    "one-off",
    "right now",
    "user ate",
    "user feels tired",
    "user had lunch",
    "user is hungry",
    "user is tired",
    "user was tired",
];

const GENERAL_KNOWLEDGE_PATTERNS: &[&str] = &[
    " is a programming language",
    "general technical fact",
    "project-independent fact",
    "rust ownership prevents data races",
    "sqlite is",
    "the earth",
    "world knowledge",
];

const ILLEGAL_OR_HARMFUL_PATTERNS: &[&str] = &[
    "bypass authentication",
    "clearly false",
    "fabricated",
    "false claim",
    "harmful",
    "illegal",
    "malware",
    "phishing",
    "steal credentials",
];

const EXTERNAL_SOURCE_PATTERNS: &[&str] = &[
    "according to browser page",
    "according to readme",
    "according to the browser page",
    "according to the readme",
    "according to the web page",
    "according to web page",
    "browser page says",
    "derived from file",
    "external source",
    "file says",
    "files say",
    "from browser page,",
    "from the browser page,",
    "from the readme",
    "from the web page,",
    "from web page,",
    "readme says",
    "repository file says",
    "web page says",
    "website says",
    "without explicit user approval",
    "without user approval",
];

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

    #[test]
    fn secret_prefix_requires_key_shape() {
        assert_eq!(
            block_reason(
                "User prefers task-specific low-risk code reviews.",
                None,
                "explicit_user_statement"
            ),
            None
        );
        assert_eq!(
            block_reason(
                "User's API key is sk-testsecret123456.",
                None,
                "explicit_user_statement"
            ),
            Some("secret_like_content")
        );
        assert_eq!(
            block_reason(
                "User's GitHub secret is abc123.",
                None,
                "explicit_user_statement"
            ),
            Some("secret_like_content")
        );
    }

    #[test]
    fn blocklist_terms_need_sensitive_or_temporary_context() {
        assert_eq!(
            block_reason(
                "User prefers passwordless authentication.",
                None,
                "explicit_user_statement"
            ),
            None
        );
        assert_eq!(
            block_reason(
                "User maintains a weather app project.",
                None,
                "explicit_user_statement"
            ),
            None
        );
        assert_eq!(
            block_reason(
                "User tests with temporary directories.",
                None,
                "explicit_user_statement"
            ),
            None
        );
    }

    #[test]
    fn blocks_payment_cards_and_natural_language_tokens() {
        assert_eq!(
            block_reason(
                "User's Visa number is 4111111111111111.",
                None,
                "explicit_user_statement"
            ),
            Some("secret_like_content")
        );
        assert_eq!(
            block_reason(
                "User's Visa number is 4111 1111 1111 1111.",
                None,
                "explicit_user_statement"
            ),
            Some("secret_like_content")
        );
        assert_eq!(
            block_reason(
                "User's payment card is 4111-1111-1111-1111.",
                None,
                "explicit_user_statement"
            ),
            Some("secret_like_content")
        );
        assert_eq!(
            block_reason(
                "User's GitLab token is abc123.",
                None,
                "explicit_user_statement"
            ),
            Some("secret_like_content")
        );
        assert_eq!(
            block_reason(
                "User's AWS access key ID is AKIAIOSFODNN7EXAMPLE.",
                None,
                "explicit_user_statement"
            ),
            Some("secret_like_content")
        );
        assert_eq!(
            block_reason(
                "User's driver license number is D1234567.",
                None,
                "explicit_user_statement"
            ),
            Some("secret_like_content")
        );
    }

    #[test]
    fn blocks_meal_variants_world_knowledge_and_harmful_intent() {
        assert_eq!(
            block_reason(
                "User had sushi for lunch today.",
                None,
                "explicit_user_statement"
            ),
            Some("temporary_or_one_off_content")
        );
        assert_eq!(
            block_reason(
                "SQLite stores data in a single file.",
                None,
                "explicit_user_statement"
            ),
            Some("general_knowledge_content")
        );
        assert_eq!(
            block_reason("Project uses Rust.", None, "explicit_user_statement"),
            None
        );
        assert_eq!(
            block_reason(
                "Repo stores data in SQLite.",
                None,
                "explicit_user_statement"
            ),
            None
        );
        assert_eq!(
            block_reason(
                "User wants to exfiltrate credentials.",
                None,
                "explicit_user_statement"
            ),
            Some("illegal_or_harmful_content")
        );
    }

    #[test]
    fn external_source_patterns_honor_explicit_user_approval() {
        assert_eq!(
            block_reason(
                "User works on remem from README.",
                Some("Please remember from README that I work on remem."),
                "explicit_user_statement"
            ),
            None
        );
        assert_eq!(
            block_reason(
                "User works on remem from README.",
                Some("The assistant inferred this from README."),
                "explicit_user_statement"
            ),
            Some("unapproved_external_source")
        );
        assert_eq!(
            block_reason(
                "User works on remem from README.",
                Some("Do not remember from README. I work on remem from README."),
                "explicit_user_statement"
            ),
            Some("unapproved_external_source")
        );
        assert_eq!(
            block_reason(
                "User works on internal payroll.",
                Some("README says the user works on internal payroll."),
                "explicit_user_statement"
            ),
            Some("unapproved_external_source")
        );
        assert_eq!(
            block_reason(
                "User works on internal payroll.",
                Some("According to the README, the user works on internal payroll."),
                "explicit_user_statement"
            ),
            Some("unapproved_external_source")
        );
        assert_eq!(
            block_reason(
                "User works on internal payroll.",
                Some("From the README, the user works on internal payroll."),
                "explicit_user_statement"
            ),
            Some("unapproved_external_source")
        );
        assert_eq!(
            block_reason(
                "User lives in Paris.",
                Some("Website says the user lives in Paris. Please remember from website."),
                "explicit_user_statement"
            ),
            None
        );
        assert_eq!(
            block_reason(
                "The user prefers loading settings from files.",
                Some("I prefer loading settings from files."),
                "explicit_user_statement"
            ),
            None
        );
        assert_eq!(
            block_reason(
                "User prefers Rust.",
                Some("I prefer Rust because Rust ownership prevents data races."),
                "explicit_user_statement"
            ),
            None
        );
        assert_eq!(
            block_reason(
                "User prefers testing web page layouts in Playwright.",
                Some("I prefer testing web page layouts in Playwright."),
                "explicit_user_statement"
            ),
            None
        );
        assert_eq!(
            block_reason(
                "User prefers deriving selectors from web page text.",
                Some("I prefer deriving selectors from web page text."),
                "explicit_user_statement"
            ),
            None
        );
        assert_eq!(
            block_reason(
                "User lives in Paris.",
                Some("From the web page, the user lives in Paris."),
                "explicit_user_statement"
            ),
            Some("unapproved_external_source")
        );
        assert_eq!(
            block_reason(
                "User thinks SQLite is a single-file database.",
                Some("I think SQLite is a single-file database."),
                "explicit_user_statement"
            ),
            Some("general_knowledge_content")
        );
    }
}