Skip to content

fix(memory): keep one copy of a sensitive fact, and forget every copy by id - #7212

Merged
kovtcharov-amd merged 3 commits into
amd:mainfrom
kovtcharov:kalin/rc18-forget-sensitive-duplicate
Nov 12, 2026
Merged

kovtcharov-amd merged 3 commits into
amd:mainfrom
kovtcharov:kalin/rc18-forget-sensitive-duplicate

Conversation

@kovtcharov

@kovtcharov kovtcharov commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

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 #7175

Test plan

🔍 Technical details
  • 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.

… by id

Before: "Remember that I am allergic to mango" saved a sensitive memory, and
seconds later the turn's extraction saved the same fact again, not sensitive.
Forgetting it by id erased only the first copy, so the Memory page still
listed it and a new chat still answered "you're allergic to mango". After:
the second save merges into the first and stays sensitive, and forgetting by
id erases every copy of the same words.

- Dedup treated the remember tool's entity tag ("user", "health") and
  extraction's missing tag as different subjects. Only a person: tag now
  names someone else; distinct facts that share most words still stay apart.
- forget(knowledge_id) now also forgets current rows with the same words
  about the same subject, as forgetting by words already did.

Refs amd/gaia finding F9
@github-actions github-actions Bot added documentation Documentation changes tests Test changes agents labels Nov 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions

This fixes a real privacy bug: a sensitive fact like "I am allergic to mango" was saved twice, once marked sensitive and once not, and forgetting it by id left the second copy behind. Now the second save merges into the first and stays sensitive, and the forget tool removes every copy. The tests drive the real tools against a real store, and the fix holds up.

Two things are worth fixing before or soon after merge:

  • The new "same subject" rule is broader than intended. It treats any tag except person: as being about the user. GAIA's own extraction prompt also tags facts with app: and project: labels. So an untagged fact can now merge into, or be erased along with, an identical fact filed under a specific app or project. Treat every type:name tag except the user's own as a separate subject.
  • Deleting from the Memory page still removes only one copy. The fix covers the chat forget tool but not the Memory page's delete button. Duplicates saved before this release stay on users' disks, and deleting one there still leaves the other for a new chat to recall. That is the symptom in Memory: a forgotten sensitive fact is still remembered #7175.

Real-world evidence

N/A: the evidence harness produced no bundle for this PR. The change is in the memory store and agent tool layer, which ships inside the downloaded agent binary. The verdict rests on static review and the PR's unit and integration runs. The author lists an in-app replay on the next RC as still to do.

🔍 Technical details

🟡 Non-person typed tags are folded into the user's subject (src/gaia/agents/base/memory_store.py:475-486)
_same_subject_sql(None) matches entity IS NULL OR NOT LIKE 'person:%'. The extraction prompt (memory.py:827) documents type:name tags (person:sarah_chen, app:vscode, project:gaia), and the remember docstring (memory.py:5726) gives app:slack as an example. Here is what goes wrong:

  • Extraction stores the untagged fact "Uses dark mode". It now merges into an app:vscode row with the same words, and entity = COALESCE(?, entity) keeps app:vscode. The user's general preference is now filed under one app.
  • forget(id) on the untagged row: copies_of runs the same condition, so it also erases the app:* and project:* rows with identical words.

The rule is also asymmetric. An untagged lookup matches every non-person tag, while a tagged lookup matches only its own tag or NULL. Suggested scope: a tag is the user's own when it has no : or starts with user:, and any other typed tag is a distinct subject. That keeps the user, health and user:self cases your tests cover:

def _same_subject_sql(entity: Optional[str]) -> Tuple[str, tuple]:
    """SQL condition on ``k.entity`` for rows about the same subject as *entity*.

    A ``type:name`` tag (``person:sam``, ``app:slack``) names its own subject;
    a bare label ("user", "health") or ``user:...`` tags the user's own fact,
    so an untagged copy of it is the same memory.
    """
    users_own = "(k.entity IS NULL OR instr(k.entity, ':') = 0 OR lower(k.entity) LIKE 'user:%')"
    if entity is None:
        return users_own, ()
    if ":" in entity and not entity.lower().startswith("user:"):
        return "k.entity = ?", (entity,)
    return "(k.entity = ? OR k.entity IS NULL)", (entity,)

A test with entity="app:slack" alongside the existing person:Sam case would pin this.

🟡 Sibling path: Memory page delete still removes one copy (src/gaia/ui/routers/memory.py:505-520)
delete_knowledge calls store.forget/store.delete on the single id. The dedup fix stops new duplicates from forming, but rows duplicated before this release are still on disk, and this endpoint leaves the twin for recall to find. Reusing store.copies_of(knowledge_id) there, plus FAISS removal as the tool does, would close it. If that belongs in a separate PR, file it as Follow-up: Memory page delete leaves earlier duplicate copies of a fact.

🟢 Stale "entity" wording (nits)

  • memory_store.py:259: the comment still says the dedup scope is "category+context+entity".
  • memory_store.py:1515: same wording.
  • bootstrap.py:120: same wording.
  • memory_store.py:2404 (is_current): the docstring says store dedup ignores no entity at all, which is no longer quite true.
  • docs/spec/agent-memory-architecture.md:405 opens by saying "category + context + entity" and then explains subject scoping a few sentences later. Saying "subject" in the first clause would read consistently.

Strengths

  • The tests run the real remember tool, then extraction, then forget, then recall, against a real MemoryStore. The parametrization covers three tags and three ways the copy can be worded, and there are negative cases for "green mango" and person:Sam.
  • The two dedup queries collapse into one shared SQL condition, so store() and copies_of can't drift apart.
  • Copies are found before the forget runs and erased through forget() rather than delete(), so extraction won't learn them again.

… the Memory page

The first fix treated every tag but person: as the user's own, so an
untagged "Uses dark mode" could merge into an app:vscode fact, and forgetting
one could erase the other. Now only no tag, a bare label or a user: tag is
the user; any other type:name tag is its own subject. Deleting a fact on the
Memory page now also forgets its copies, as the forget tool does, so a
duplicate saved before this release no longer keeps the fact alive.

Refs amd/gaia finding F9
@kovtcharov

Copy link
Copy Markdown
Contributor Author

Both review points are in the new commit. A tag now counts as the user's own only when it is missing, a bare label, or user:; any other type:name tag (app:, project:, person:) is its own subject, with tests for app:slack. Deleting an entry on the Memory page now removes its copies too and returns every erased id. The stale wording and the three docs that describe it are updated.

…sitive copy's words out of the forgotten list

Forgetting by the user's words still left a copy behind when the two were
worded apart ("User is allergic to mango" and "Allergic to mango."):
the lookup counted them as two memories and erased neither. Copies are now
the same words bar filler on both paths, as the store's dedup already says.

Forgetting an unflagged copy of a sensitive fact also wrote the fact's
full text into the forgotten list, where it stayed on disk. When any copy
is sensitive, every copy is now recorded as word stems only. The tool and
the Memory page share one store call, forget_with_copies, for both.
@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

Two gaps closed in a new commit: forgetting by words now takes a reworded copy too, and forgetting an unflagged copy of a sensitive fact no longer leaves the fact's full text in the forgotten list.

🔍 Technical details
  • Words path: with "User is allergic to mango" (sensitive, user) and a pre-existing untagged "Allergic to mango.", forget("allergic to mango") returned the ambiguity error and erased nothing, because _forget_lookup compared copies by every word, filler included. It now uses the same filler-free words as copies_of (fact_words), and each match is forgotten with its copies.
  • Forgotten list: forget() stored stems only for a row flagged sensitive, so the unflagged twin wrote "Allergic to mango" in plain text. forget_with_copies() records every copy as sensitive when any one is. The tool and the Memory page both go through it.
  • Verified: FTS rows and embeddings go with the row, a later extraction of the same turn doesn't re-learn it (said_at guard), a concurrent extraction during the forget leaves nothing, "User is not allergic to mango" and Sam's copy are kept, a profile row with the same words is untouched.
  • New tests in test_memory_sensitive_copy.py (3 fail on the previous head). Memory unit, router and integration suites pass.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Nov 12, 2026
Merged via the queue into amd:main with commit 4836477 Nov 12, 2026
48 of 49 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the kalin/rc18-forget-sensitive-duplicate branch November 12, 2026 01:05
kovtcharov-amd added a commit that referenced this pull request Nov 12, 2026
Resolves the conflict with #7212 in MemoryStore.forget: the superseded-chain
walk keeps main's sensitive argument, and if any version in the chain is
sensitive every version is kept as stems only, so an unflagged old version
can't leave a sensitive fact's words in forgotten.

Adds tests for that, for a supersede cycle, and for forget_with_copies
taking a copy's older versions with it.
kovtcharov-amd added a commit to kovtcharov/gaia that referenced this pull request Nov 12, 2026
…md#7269)

When the user forgot a fact that had replaced an older version, only the
current version was erased. The older versions stayed on disk with their
full text. Recall and the Memory page hid them, but they were still
stored, which broke the promise that "forget" erases the fact. Now
forget erases every older version too, and records each one as forgotten
so it isn't learned again from old chats. A sensitive version is
recorded as word stems only.

Closes amd#7266

## Test plan

- [x] `python -m pytest tests/unit/test_memory_forget_superseded.py`
passes (2 of its 4 tests fail on `main`)
- [x] `python util/lint.py --all` passes
- [x] Memory/forget unit tests pass (`pytest tests/unit -k "memory or
forget"`: 2572 passed; 25 collection errors are modules this runner
lacks, `responses`/`matplotlib`)
- [ ] Tell GAIA a fact, then correct it so the newer memory supersedes
the old one. Forget it, then open `~/.gaia/memory.db` and confirm
neither version is in `knowledge`

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

- `MemoryStore.forget` gathers the target plus every row that reaches it
through `superseded_by`, using a recursive CTE. `UNION` rather than
`UNION ALL`, so a cycle (consolidation can link two rows either way)
terminates. Each row is checked against the privileged-category rule,
removed from `knowledge_fts` and `knowledge`, and written to `forgotten`
(stems if sensitive), all in one transaction.
- Forgetting an older version leaves the newer one untouched: the walk
only follows rows that were superseded.
- No vector-index change is needed. Superseded rows are dropped from
FAISS when they're superseded (`_faiss_remove(old_id)`), and the index
is rebuilt from active rows only.
- This may conflict textually with amd#7212, which also edits `forget`. The
two changes are complementary.
</details>

Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
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.

Memory: a forgotten sensitive fact is still remembered

2 participants