Skip to content

fix(memory): keep a different fact that shares most of a memory's words - #6902

Merged
kovtcharov-amd merged 5 commits into
mainfrom
kalin/fix-4732-memory-dedup-distinct-facts
Nov 7, 2026
Merged

kovtcharov-amd merged 5 commits into
mainfrom
kalin/fix-4732-memory-dedup-distinct-facts

Conversation

@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

Telling GAIA "My mother-in-law's name is Miriam." erased "My mother's name is Elena.", because memory treated any two facts sharing 80% of their words as one and overwrote the old one. Now only a real repeat merges: a restatement, or a refinement like "... Elena" → "... Elena Petrova". A fact that changes, inserts or reorders a word — a different relative, a negation, a new number — is kept as its own memory, so dedup can no longer lose a fact.

A changed value ("React 18" → "React 19") is now a second entry rather than an in-place overwrite; extraction's UPDATE and consolidation already supersede the old one with a trail.

Test plan

  • python -m pytest tests/unit/test_memory_dedup_distinct_facts.py tests/unit/test_memory_store.py tests/unit/test_memory_mixin.py tests/integration/test_memory_integration.py
  • In gaia chat, say "My mother's name is Elena", then "My mother-in-law's name is Miriam"; gaia memory (or the Memory page) lists both
  • Say "My mother's name is Elena Petrova"; the Elena entry updates instead of duplicating
🔍 Technical details
  • _merged_duplicate runs after the existing overlap pre-filter: filler-stripped token sequences must be equal or one a prefix of the other; the fuller content is kept. Numbers keep inner punctuation so "10" is not a prefix of "10:30".
  • Adversarial cases cover both orders of each near-miss pair (in-law, sibling swap, negation, version, reorder, day, city, name) plus longer-number "refinements".
  • Four existing tests used a value swap as their "duplicate"; they now use a refinement. test_recall_limit_clamped_to_20 only passed because its 25 facts had collapsed into one row — it now checks the 100 cap the recall tool documents.
  • Regression hunt: memory, skill, knowledge, recall and dedup unit tests plus the flagship's tests — 5507 passed, none newly failing.

Closes #4732

Storing "My mother-in-law's name is Miriam." silently overwrote "My
mother's name is Elena.": dedup merged any two facts with 80% word
overlap and replaced the old content in place.

Dedup now merges only a true duplicate: one fact's words, in order and
ignoring filler like "the", "is" and "my", must start the other's. A
restatement or a refinement ("... Elena" -> "... Elena Petrova") still
merges and keeps the fuller wording. A swapped, inserted or reordered
word, including a negation or a changed number, is stored as its own
entry, where extraction's UPDATE and consolidation can supersede it
with a trail.

Tests that relied on a value swap merging in place now use a real
refinement; the recall clamp test was vacuous because its 25 facts had
collapsed into one, and now checks the documented 100 cap.

Closes #4732
@kovtcharov-amd kovtcharov-amd self-assigned this Nov 7, 2026
@github-actions github-actions Bot added documentation Documentation changes tests Test changes agents labels Nov 7, 2026
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Verdict: Request changes

This fixes a real data-loss bug: telling GAIA a second fact that only looked like an old one ("mother-in-law" vs "mother", "React 18" vs "19", "is" vs "is not") used to overwrite the first. Now only true repeats and refinements merge. The rule is tight, the tests try both orders of every near-miss pair, and the live API run shows the pairs kept apart. One thing blocks the merge:

  • The SDK memory guide and two other places still describe the old rule. They say a fact with 80% word overlap replaces the old one "because the newer fact is assumed to be more current." Readers of the published SDK page will expect the overwrite this PR removes. Update them in this PR.

One smaller issue, not blocking: when a shorter restatement merges into a longer memory, the agent now keeps the longer text but stores the meaning-search vector for the shorter one. Recall by meaning can then miss the extra words, and Rebuild Embeddings won't repair it.

Real-world evidence

The evidence stage ran the Agent UI memory API live on a fresh, empty backend (POST /api/memory/knowledge, HTTP 200 on every write). Before the change (parent dad0ed13f), all three near-miss pairs collapsed into one entry:

"My mother's name is Elena." then "My mother-in-law's name is Miriam.": same_id=True; kept="My mother-in-law's name is Miriam."
'Project uses React 18 with webpack bundler' then 'Project uses React 19 with webpack bundler': same_id=True; kept='Project uses React 19 with webpack bundler'
'User is vegetarian at home' then 'User is not vegetarian at home': same_id=True; kept='User is not vegetarian at home'

After the change, each pair was stored as two entries. My sister's name is Ana → … Ana Lopez → … Ana all merged into one row that kept "My sister's name is Ana Lopez". User prefers dark mode / The user prefers dark mode. merged. Standup meeting is at 10 / … 10:30 stayed apart. The spot checks passed: list, stats and edit (PUT) all returned HTTP 200 with the expected rows. The Memory dashboard screenshot is pending the strix-halo lane, since this runner has no inference. The evidence supports the fix. The verdict rests only on the doc drift above.

🔍 Technical details

🟡 Stale dedup claims outside the doc this PR edited

The old "newer content replaces" rule is still stated in:

  • docs/sdk/sdks/memory.mdx:602: "the existing entry is updated with the newer content. The newer fact is assumed to be more current." This is the published SDK page.
  • docs/spec/agent-memory-architecture.md:2150: "replaces content with the newer version". This is the same spec the PR updated at L405, so the spec now contradicts itself.
  • src/gaia/agents/base/memory_store.py:16: the module docstring "(>80% word overlap in same category+context)".
**Deduplication:** A new entry merges into an existing one in the same `category` + `context` + `entity` scope only when it is a true duplicate: after >80% word overlap (Szymkiewicz-Simpson), one entry's words, in order and ignoring filler like "the" or "my", must start the other's. The fuller wording is kept. A swapped, inserted or reordered word ("18" → "19", "is" → "is not") is stored as a separate entry, and the extraction pipeline's UPDATE supersedes the old one.

🟡 Embedding can describe different text than the merged row (non-blocking)

_merged_duplicate can now return the existing content when the new text is a shorter prefix. store() saves that and sets embedding = NULL, but callers then embed the text they passed in, not what was stored:

  • memory.py:5583 (remember): fact[:MAX_CONTENT_LENGTH]
  • memory.py:2764 / :2807 (extraction add/update): op["content"]
  • memory.py:5022 (tool-error memory)

The row ends up with a non-NULL vector for "My sister's name is Ana" while its content is "… Ana Lopez". get_items_without_embeddings() never picks it up again, so Rebuild Embeddings won't fix it. Before this PR, stored content always equalled the input, so this mismatch is new. The UI router already does it right: _embed_row reads the stored row. The simplest fix is to have _embed_and_index (or these callers) embed store.get_item(kid)["content"]. Add a test: store a long fact, remember its prefix, then assert that the vector matches the long content.

Checked, no issue

  • Sibling callers that relied on overwrite: the extraction UPDATE path at memory.py:2801 already handles new_id != old_id by superseding, and with this PR that branch fires more often, as intended. The system-context refresh at memory.py:1411 already deletes before re-collecting.
  • Failed candidates now continue instead of returning, so a later candidate in scope can still match.
  • The tests changed from "value swap" to "refinement" still exercise the dedup/embedding-clear paths they were written for. test_recall_limit_clamped_to_100 now really tests the clamp, where before it passed only because its rows had collapsed.

Strengths

  • The fix is narrow: it keeps the cheap overlap pre-filter and adds an ordered-prefix check, and number tokens keep their inner punctuation so 10 isn't treated as a prefix of 10:30.
  • Adversarial tests run both orders of each pair, so the "keep the fuller one" branch is covered in both directions.

The SDK reference, the spec's summary line and the module docstring
still said 80% word overlap replaces the old fact. Only a restatement
or refinement merges now; a changed value is a new row that
consolidation retires.
When a shorter restatement merges into a fuller memory, the row keeps the
fuller wording, but callers embedded the text they passed in. The row
then had a vector for different text, and since it was no longer NULL,
rebuilding embeddings never repaired it. _embed_and_index now embeds the
stored content.
@kovtcharov-amd

Copy link
Copy Markdown
Collaborator Author

Both points are fixed in 56a334e. The docs now describe the new rule, and a merged memory is embedded with the text it actually stores.

  • Docs: the SDK memory guide, the spec's summary line and the memory_store module docstring now say that only restatements and refinements merge, and that a changed value is a new entry that consolidation retires. I grepped docs/ and src/ for other 80% or overwrite claims and found none left.
  • Embedding mismatch: when a shorter restatement merges into a fuller memory, the vector now describes the fuller text that is stored. A new test remembers the shorter fact and checks which text gets embedded.
🔍 Technical details
  • _embed_and_index reads get_item(knowledge_id)["content"] before embedding, so the fix covers every caller (remember, extraction ADD/UPDATE, tool-error memories, summaries) instead of patching each one.
  • New test: test_merged_restatement_is_embedded_with_the_stored_text in tests/unit/test_memory_mixin.py. It fails on the previous head and passes now.
  • Merged current main. The memory, skill and flagship suites gave 5599 passed and 2 failed. The two failures (test_unload_removes_the_body_from_the_system_prompt, test_a_skill_that_cannot_load_does_not_stop_its_tool) pass alone on both this branch and main, so they are flaky under parallel runs.

…-dedup-distinct-facts

# Conflicts:
#	docs/sdk/sdks/memory.mdx
#	docs/spec/agent-memory-architecture.md
#	src/gaia/agents/base/memory_store.py
@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Nov 7, 2026
Merged via the queue into main with commit 3bd72e1 Nov 7, 2026
41 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the kalin/fix-4732-memory-dedup-distinct-facts branch November 7, 2026 10:08
bniladridas pushed a commit to bniladridas/gaia that referenced this pull request Nov 12, 2026
… by id (amd#7212)

Asking GAIA to remember a sensitive fact ("I am allergic to mango")
saved it twice: once marked sensitive by the remember tool, and again a
few seconds later, unmarked, by the turn's own extraction. Forgetting it
by id then removed only the first copy, so the Memory page still listed
it and a new chat still answered with it. Now the second save merges
into the first and stays sensitive, and forgetting by id removes every
copy.

Closes amd#7175

## Test plan
- [x] `python -m pytest tests/unit/test_memory_sensitive_copy.py`: runs
the real remember tool, then extraction's save, then forget by id, then
recall. 11 of 12 fail without the fix, 12 pass with it
- [x] Memory unit and integration suites, including the distinct-facts
tests from amd#6924 and amd#6902: 2225 passed, 6 skipped
- [ ] Replay in the app on the next RC (the agent ships as a downloaded
binary): remember a sensitive fact, check Memory shows one entry, forget
it, ask in a new chat
- [ ] Eval categories to watch on the next RC: `memory`, `gaia_memory`,
`context_retention`

<details>
<summary>🔍 Technical details</summary>

- Duplicate detection compared only rows with the same entity tag. The
tool tags the fact and extraction does not, so the copies never met. No
tag, a bare label (`user`, `health`) and a `user:` tag now count as the
user; any other `type:name` tag (`person:`, `app:`, `project:`) is its
own subject.
- A merged row's source still changes from `tool` to `llm_extract`, as
it already did when the tags matched, so the Memory page labels it
"Picked up in a chat".
- Forgetting, by id or by words and from the tool or the Memory page,
also erases copies with the same words bar filler about the same
subject; if any copy is sensitive, all are recorded as stems only.
</details>

---------

Co-authored-by: Kalin Ovtcharov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents documentation Documentation changes tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(memory): dedup overwrites a different fact that shares most of its words

1 participant