Skip to content

fix(memory): preserve symbol-distinct facts during deduplication - #6924

Merged
kovtcharov-amd merged 6 commits into
amd:mainfrom
huong8373tt-beep:fix/6918-memory-symbol-dedup
Nov 7, 2026
Merged

kovtcharov-amd merged 6 commits into
amd:mainfrom
huong8373tt-beep:fix/6918-memory-symbol-dedup

Conversation

@huong8373tt-beep

@huong8373tt-beep huong8373tt-beep commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • preserve signed numeric values and language suffixes such as -5, +1, C++, and C# when comparing memories
  • keep ordinary hyphenated prose deduplication unchanged
  • add regression coverage for both deduplication and forgotten-memory matching

Fixes #6918

Validation

  • python -m pytest tests/unit/test_memory_store.py tests/unit/test_memory_forget_sticks.py -q — 426 passed
  • uvx black --check — passed
  • python -m compileall — passed
  • git diff --check — passed

All worktree and validation work was performed on D:.

Disclosure and authorization

Slai Agent Runtime performed the investigation, implementation, and test execution. The account owner authorized this contribution and is responsible for its content.

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Verdict: Request changes

This PR fixes a real bug: memory dedup merged facts that differ only by a sign or a language suffix, such as -5 and 5 or C++ and C. That part works. But the new tokenizer also starts treating dates, phone numbers and ranges as single tokens, and that breaks "forget" for them: if a user forgets a date or phone number and the extractor later writes it in a different format, the memory comes back.

  • Forgotten dates and phone numbers can come back. Before this PR, a forgotten 1990-05-14 still matched a re-extracted 1990/05/14, and 555-1234 matched 555 1234, so the forget stuck. Now neither pair matches and the fact is stored again. The fix needs only two changes: keep a leading +/- attached to a number, and keep +/# on a word. Digits joined by -./:, should stay split as before. That also makes the PR match its own claim that ordinary text is unchanged.

Real-world evidence

N/A: no evidence bundle was produced for this run. This is an internal memory-matching change with no CLI, API or UI surface to exercise here, so the verdict rests on static review. I also ran the old and new tokenizers side by side on edge-case strings (described below). The test suite could not run on this runner because its dependencies aren't installed. The PR reports 426 passing.

🔍 Technical details

🟡 Number tokens now span -./:,, so forget-matching misses format variants (src/gaia/agents/base/memory_store.py:292-299)

The number alternatives [0-9](?:[0-9.,:/-]*[0-9])? pull separators into the token. I compared _same_memory on the base and PR tokenizers:

forgotten re-extracted base PR
User's birthday is 1990-05-14 User's birthday is 1990/05/14 same different
User's phone is 555-1234 User's phone number is 555 1234 same different

content_stems also drives _lifted_from_tools / _copied_from_material in memory.py:524,550. A user saying "10 to 12" no longer shares any token with a memory reading 10-12. Only the sign and the suffix are needed for #6918:

_MEMORY_TOKEN_RE = re.compile(
    r"""
    (?<![a-z0-9_])[+-][0-9]+
    | [a-z][a-z0-9]*[+#]+(?![a-z0-9_])
    | [a-z0-9']+
    """,
    re.IGNORECASE | re.VERBOSE,
)

With this, -5/+1/C++/C# stay distinct, and 555-1234 splits into 555 and 1234 as on main. +1-555-1234 becomes +1, 555, 1234, and mother-in-law is unchanged. Please add a forget-sticks case with a reformatted date or phone number next to the new ones in test_memory_forget_sticks.py.

🟢 Gate regex duplicated inline (memory_store.py:380-382): the same pattern literal appears twice and isn't compiled. It also lacks the (?![a-z0-9_]) lookahead that _MEMORY_TOKEN_RE has, so a+b switches the pair to the new tokenizer, which keeps apostrophes. It's harmless today, but hoist it into a module-level compiled constant so the two can't drift.

Strengths

  • Regression tests cover both dedup paths (store() overlap and forgotten-memory matching), and include a guard that hyphenated prose still merges.
  • The new tokenizer only applies to _word_overlap when a sign or suffix is present, which limits the impact on existing dedup.

@huong8373tt-beep

Copy link
Copy Markdown
Contributor Author

Addressed the review findings in 3717c4bae.

  • Numeric separators remain split, preserving format-insensitive matching for dates, phone numbers, and ranges.
  • A leading +/- stays attached to a number.
  • C++ / C# remain distinct from C.
  • The symbol-detection regex is now compiled once and reused by the overlap gate.
  • Added regression cases for reformatted dates, phone numbers, and ranges.

Validation on D: 429 passed; Black, compileall, and git diff --check pass.

@huong8373tt-beep

Copy link
Copy Markdown
Contributor Author

Addressed the cross-platform CI failures in 4554b2d40.

The tokenizer correctly keeps signs and language suffixes meaningful, while _word_overlap now rejects a match when the same plain base appears with conflicting symbols (-5 vs 5, +1 vs 1, C++/C# vs C). This fixes the store/dedup path that still merged those values on Linux/macOS/Windows. Date, phone, and range separators remain format-insensitive.

Validation on D: 429 targeted tests passed with external pytest plugins disabled; Black, compileall, and git diff --check pass.

@huong8373tt-beep

Copy link
Copy Markdown
Contributor Author

Addressed the remaining Code Quality failure in 14255a4bb by applying the repository's Black formatting to memory_store.py. This is formatting-only (no dedup behavior change).

Local checks on the D: worktree: uvx black --check, python -m compileall, and git diff --check all pass. The targeted pytest command was blocked before collection by an unrelated local third-party pytest-plugin dependency mismatch (langsmith/pydantic/typing_extensions), so I am leaving functional re-validation to the newly triggered CI rather than claiming it passed locally.

… match

The fact/preference cross-category dedup compared memories with a
punctuation-stripping tokenizer, so "User knows C++" and "User knows C"
(or -5 and 5) still merged even after the overlap gate kept them apart.
@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

Thanks for this fix, @huong8373tt-beep, and for working through the review rounds. I pushed a small change so C++/C and -5/5 now really stay separate memories. That was the last piece the new test was catching on all three OSes. I also merged the latest main into the branch.

🔍 Technical details
  • Root cause: fact is one of the interchangeable categories, so after the same-category check _find_similar_locked runs a second pass that merges any entry with the same words. That pass used _same_words, which drops all punctuation. "User knows C++" and "User knows C" both became user knows c and merged, even though your _word_overlap change already kept them apart.
  • Fix: _same_words now tokenizes with your _MEMORY_SYMBOL_RE ahead of [a-z0-9]+, so signed numbers and +/# suffixes survive. Dates, phone numbers and ranges still split the same way, because the sign alternative needs a non-alphanumeric character before it.
  • Validation: all -k memory unit tests pass on Windows after the merge (2521 passed, 71 skipped), including your new dedup and forget-sticks cases. black, isort and flake8 are clean.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Nov 7, 2026
Merged via the queue into amd:main with commit 10031ca Nov 7, 2026
40 checks passed
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 tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory dedup merges facts that differ only by a symbol

2 participants