From df17999431cace442dfd10f3cc454524d2004088 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=B0=E5=B0=BC=E9=BE=9F?= <3098584968@qq.com> Date: Sun, 25 Oct 2026 22:03:39 +0800 Subject: [PATCH 1/3] fix: preserve text order in feedback update guard --- src/memos/mem_feedback/utils.py | 14 ++++++-------- tests/mem_feedback/test_utils.py | 7 +++++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/memos/mem_feedback/utils.py b/src/memos/mem_feedback/utils.py index 8e3b2f34c..fc528ac15 100644 --- a/src/memos/mem_feedback/utils.py +++ b/src/memos/mem_feedback/utils.py @@ -1,6 +1,8 @@ import json import re +from difflib import SequenceMatcher + from memos.memories.textual.item import TextualMemoryItem, TreeNodeTextualMemoryMetadata @@ -39,14 +41,10 @@ def should_keep_update(new_text: str, old_text: str) -> bool: old_len = estimate_tokens(old_text) def calculate_similarity(text1: str, text2: str) -> float: - set1 = set(text1) - set2 = set(text2) - if not set1 and not set2: - return 1.0 - - intersection = len(set1.intersection(set2)) - union = len(set1.union(set2)) - return intersection / union if union > 0 else 0.0 + # Compare character order as well as content. A character-set score + # treats a complete reordering of facts as unchanged, which can let a + # destructive feedback update pass the small-edit guard. + return SequenceMatcher(None, text1, text2).ratio() similarity = calculate_similarity(old_text, new_text) change_ratio = 1 - similarity diff --git a/tests/mem_feedback/test_utils.py b/tests/mem_feedback/test_utils.py index 109d83ff6..fed244452 100644 --- a/tests/mem_feedback/test_utils.py +++ b/tests/mem_feedback/test_utils.py @@ -33,6 +33,13 @@ def test_should_keep_update_rejects_large_rewrite(): assert not should_keep_update("zzzzzzzzzz", "aaaaaaaaaa") +def test_should_keep_update_rejects_reordered_text_with_same_characters(): + old_text = "alpha bravo charlie delta echo foxtrot golf hotel india juliet" + new_text = "juliet india hotel golf foxtrot echo delta charlie bravo alpha x" + + assert not should_keep_update(new_text, old_text) + + def test_general_split_into_chunks_groups_small_items(): chunks = general_split_into_chunks( [{"text": "short"}, {"text": "also short"}], From 10adb591b5f1a3e2fd6135b34795115629e71a52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=B0=E5=B0=BC=E9=BE=9F?= <3098584968@qq.com> Date: Mon, 26 Oct 2026 10:57:26 +0800 Subject: [PATCH 2/3] fix: detect reordered feedback facts --- src/memos/mem_feedback/utils.py | 13 ++++++++++++- tests/mem_feedback/test_utils.py | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/memos/mem_feedback/utils.py b/src/memos/mem_feedback/utils.py index fc528ac15..49f3244c1 100644 --- a/src/memos/mem_feedback/utils.py +++ b/src/memos/mem_feedback/utils.py @@ -44,7 +44,18 @@ def calculate_similarity(text1: str, text2: str) -> float: # Compare character order as well as content. A character-set score # treats a complete reordering of facts as unchanged, which can let a # destructive feedback update pass the small-edit guard. - return SequenceMatcher(None, text1, text2).ratio() + character_similarity = SequenceMatcher(None, text1, text2).ratio() + + # Word-level matching catches fact reordering that still shares many + # character subsequences. For text without whitespace (for example, + # Chinese), the character-level score remains the useful signal. + words1 = text1.split() + words2 = text2.split() + if len(words1) > 1 and len(words2) > 1: + word_similarity = SequenceMatcher(None, words1, words2).ratio() + return min(character_similarity, word_similarity) + + return character_similarity similarity = calculate_similarity(old_text, new_text) change_ratio = 1 - similarity diff --git a/tests/mem_feedback/test_utils.py b/tests/mem_feedback/test_utils.py index fed244452..67a487151 100644 --- a/tests/mem_feedback/test_utils.py +++ b/tests/mem_feedback/test_utils.py @@ -35,7 +35,7 @@ def test_should_keep_update_rejects_large_rewrite(): def test_should_keep_update_rejects_reordered_text_with_same_characters(): old_text = "alpha bravo charlie delta echo foxtrot golf hotel india juliet" - new_text = "juliet india hotel golf foxtrot echo delta charlie bravo alpha x" + new_text = "juliet india hotel golf foxtrot echo delta charlie bravo alpha" assert not should_keep_update(new_text, old_text) From ad3d1176161425794eec4785fa7f045441769e28 Mon Sep 17 00:00:00 2001 From: Wenqiang Wei <46308778+endxxxx@users.noreply.github.com> Date: Wed, 18 Nov 2026 14:47:00 +0800 Subject: [PATCH 3/3] fix: recognize small edits in repetitive memory text --- src/memos/mem_feedback/utils.py | 13 ++++--------- tests/mem_feedback/test_utils.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/memos/mem_feedback/utils.py b/src/memos/mem_feedback/utils.py index 49f3244c1..5d7c91fb2 100644 --- a/src/memos/mem_feedback/utils.py +++ b/src/memos/mem_feedback/utils.py @@ -30,13 +30,7 @@ def estimate_tokens(text: str) -> int: def should_keep_update(new_text: str, old_text: str) -> bool: - """ - Determine whether the update should be skipped - Rule: - 1. If the length of old_text is less than 50 and the modification ratio is less than 50% => returns True - 2. If the length of old_text is greater than or equal to 50 and the modification ratio is less than 15% => returns True - 3. Return False in other cases - """ + """Keep a non-identical update if its ordered edit ratio is below the length threshold.""" old_len = estimate_tokens(old_text) @@ -44,7 +38,8 @@ def calculate_similarity(text1: str, text2: str) -> float: # Compare character order as well as content. A character-set score # treats a complete reordering of facts as unchanged, which can let a # destructive feedback update pass the small-edit guard. - character_similarity = SequenceMatcher(None, text1, text2).ratio() + # Repeated characters and words are meaningful in memory text. + character_similarity = SequenceMatcher(None, text1, text2, autojunk=False).ratio() # Word-level matching catches fact reordering that still shares many # character subsequences. For text without whitespace (for example, @@ -52,7 +47,7 @@ def calculate_similarity(text1: str, text2: str) -> float: words1 = text1.split() words2 = text2.split() if len(words1) > 1 and len(words2) > 1: - word_similarity = SequenceMatcher(None, words1, words2).ratio() + word_similarity = SequenceMatcher(None, words1, words2, autojunk=False).ratio() return min(character_similarity, word_similarity) return character_similarity diff --git a/tests/mem_feedback/test_utils.py b/tests/mem_feedback/test_utils.py index 67a487151..bec9fbe1b 100644 --- a/tests/mem_feedback/test_utils.py +++ b/tests/mem_feedback/test_utils.py @@ -29,6 +29,18 @@ def test_should_keep_update_accepts_small_edit(): assert should_keep_update(new_text, old_text) +def test_should_keep_update_accepts_small_edit_in_repetitive_chinese(): + old_text = "春夏秋冬山川河流" * 70 + new_text = old_text[:10] + "海" + old_text[11:] + assert should_keep_update(new_text, old_text) + + +def test_should_keep_update_accepts_small_edit_in_repetitive_english(): + old_text = " ".join(["The customer prefers email notifications."] * 50) + new_text = old_text.replace("prefers", "uses", 1) + assert should_keep_update(new_text, old_text) + + def test_should_keep_update_rejects_large_rewrite(): assert not should_keep_update("zzzzzzzzzz", "aaaaaaaaaa")