Skip to content

The agent that grows with you

fix(memory): only a self-contained bullet is deduped, and a bold heading is not a bullet

John Paul Soliva83a1a9a2bb91
diff --git a/agent/memory_manager.py b/agent/memory_manager.py
index 3e27a249b47..fac737b23fb 100644
--- a/agent/memory_manager.py
+++ b/agent/memory_manager.py
@@ -263,6 +263,11 @@ class StreamingContextScrubber:
self._at_block_boundary = self._ends_at_block_boundary(text)
+# A markdown bullet: a marker, whitespace, then content. The whitespace matters — it is what keeps
+# ``**Preferences**`` (a bold heading) and ``*emphasis*`` out of the rule.
+_RECALL_BULLET_RE = re.compile(r"[-*+]\s+\S")
+
+
def _drop_repeated_recall_lines(text: str) -> str:
"""Drop a recalled bullet that an EARLIER line of this same block already states.
@@ -272,19 +277,28 @@ def _drop_repeated_recall_lines(text: str) -> str:
into the user row's ``api_content`` sidecar and replayed verbatim on every later request for as
long as that row is in context, so each duplicate is paid once per turn, forever.
- Only list items are considered, and only when identical after stripping: headings, prose,
- blank lines and separators are left exactly as the provider wrote them, so section structure —
- and any line a provider deliberately repeats as prose — survives untouched.
+ Only a SELF-CONTAINED bullet is considered — a marker, whitespace, content, and no continuation
+ line indented beneath it. A bullet that carries continuation lines is never dropped and never
+ suppresses a later one, because two entries can share a headline and differ underneath it
+ (``- prefers draft PRs`` / `` (logged 12 Jan, builtin)`` vs the same headline logged elsewhere):
+ dropping one would re-parent its provenance under the other and invent a record neither provider
+ reported. Headings — including ``**bold**`` ones — prose, blank lines, separators and numbered
+ items are left exactly as written.
"""
+ lines = text.split("\n")
seen: set[str] = set()
kept: list[str] = []
- for line in text.split("\n"):
+ for index, line in enumerate(lines):
stripped = line.strip()
- # A bullet with actual content; a bare "-" or a "---" rule is structure, never a duplicate.
- if stripped[:1] in ("-", "*") and any(ch.isalnum() for ch in stripped):
- if stripped in seen:
- continue
- seen.add(stripped)
+ if _RECALL_BULLET_RE.match(stripped):
+ following = lines[index + 1] if index + 1 < len(lines) else ""
+ indent = len(line) - len(line.lstrip())
+ carries_continuation = bool(following.strip()) and (
+ len(following) - len(following.lstrip())) > indent
+ if not carries_continuation:
+ if stripped in seen:
+ continue
+ seen.add(stripped)
kept.append(line)
return "\n".join(kept)
diff --git a/tests/agent/test_memory_context_dedupe.py b/tests/agent/test_memory_context_dedupe.py
index 841ccc4ded7..2c20b2de31a 100644
--- a/tests/agent/test_memory_context_dedupe.py
+++ b/tests/agent/test_memory_context_dedupe.py
@@ -24,10 +24,31 @@ def test_a_repeated_bullet_is_kept_once_in_first_position():
body = _body(build_memory_context_block(raw))
- assert body.count("- alpha") == 1
- assert body.splitlines()[-4:] == ["- alpha", "- beta", "- gamma"][-3:] or "- alpha" in body
- # order is the first-occurrence order, not a re-sort
- assert [l for l in body.splitlines() if l.startswith("- ")] == ["- alpha", "- beta", "- gamma"]
+ # First-occurrence order, not a re-sort, and nothing else moved.
+ assert body.splitlines() == ["- alpha", "- beta", "- gamma"]
+
+
+def test_a_bullet_with_continuation_lines_is_never_touched():
+ """Two entries can share a headline and differ underneath it. Dropping one would re-parent its
+ provenance under the other and invent a record neither provider reported."""
+ raw = ("- prefers draft PRs\n (logged 12 Jan, source: supermemory)\n"
+ "- prefers draft PRs\n (logged 3 Feb, source: builtin)\n")
+
+ assert _body(build_memory_context_block(raw)) == raw
+
+
+def test_a_bold_heading_is_not_a_bullet():
+ """``**Preferences**`` starts with ``*``; without the whitespace test it entered the rule and a
+ repeated section heading was silently deleted."""
+ raw = "**Preferences**\n- a\n\n**Preferences**\n- b\n"
+
+ assert _body(build_memory_context_block(raw)) == raw
+
+
+def test_emphasis_and_numbered_items_are_left_as_written():
+ for raw in ("*Important:*\n- x\n*Important:*\n- y\n",
+ "1. ships on Fridays\n2. reviews in the morning\n1. ships on Fridays\n"):
+ assert _body(build_memory_context_block(raw)) == raw
def test_bullets_repeated_across_merged_provider_sections_collapse():
@@ -42,12 +63,19 @@ def test_bullets_repeated_across_merged_provider_sections_collapse():
assert "- ships on Fridays" in body and "- reviews in the morning" in body
-def test_indentation_does_not_hide_a_duplicate():
- raw = "- fact\n - fact\n\t- fact\n"
+def test_indented_duplicates_at_the_same_depth_still_collapse():
+ raw = " - fact\n - other\n - fact\n"
body = _body(build_memory_context_block(raw))
- assert len([l for l in body.splitlines() if l.strip() == "- fact"]) == 1
+ assert body.splitlines() == [" - fact", " - other"]
+
+
+def test_a_nested_bullet_is_a_child_and_is_left_alone():
+ """``- fact`` followed by a deeper ``- fact`` is a parent and its child, not a repeat."""
+ raw = "- fact\n - fact\n"
+
+ assert _body(build_memory_context_block(raw)) == raw
def test_structure_and_prose_are_left_exactly_as_written():