Skip to content

fix(memories): mirror canonical deletes into the store the user still reads - #10451

Merged
kodjima33 merged 2 commits into
BasedHardware:mainfrom
aryanorastar:fix/10446-dual-write-delete-visibility
Jul 25, 2026
Merged

kodjima33 merged 2 commits into
BasedHardware:mainfrom
aryanorastar:fix/10446-dual-write-delete-visibility

Conversation

@aryanorastar

@aryanorastar aryanorastar commented Jul 24, 2026 •

Copy link
Copy Markdown
Collaborator

What

Deleted memories come back a few seconds later. Root-caused to a read/write decision asymmetry in the V3 memory rollout — not a client cache issue.

Refs #10446 (server-side half; see the note on symptom 1 below).

Root cause

The delete and list routes are gated on different rollout modes:

route gate canonical when
DELETE /v3/memories/{id}, /v3/memories/batch, /v3/memories (all) canonical_write_decision mode ∈ {write, read}
GET /v3/memories canonical_read_enabled mode == read only

So during the dual-write stage (MEMORY_MODE=write) — which every rollout passes through before read cutover, and #10394 has this one mid-flight — a delete tombstones the canonical record while the user's list keeps reading legacy, where the row is untouched. The client drops it optimistically, the next refresh re-reads legacy, and the memory reappears. That matches the reported ~10–15s timing.

Fix

Mirror the canonical delete into legacy until read cutover — deleting is symmetric with dual-write — across all three delete paths (single, batch, and delete-all). The mirror:

  • no-ops after cutover (canonical_read_enabled true ⇒ both sides canonical), so this is confined to the migration window;
  • is best-effort — canonical is authoritative and its delete has already committed, so a legacy cleanup failure must not fail a request the user already saw succeed;
  • is harmless for canonical-only rows: Firestore deletes of absent documents are no-ops.

Single/batch mirror the specific memory_ids; delete-all purges the legacy collection for the uid (_mirror_delete_all_into_legacy → _purge_legacy_memories), matching what the canonical delete-all already did to the canonical store.

⚠️ Please confirm the semantic

This changes deletion behavior during a live migration, so it deserves the rollout owner's eye rather than mine. I picked symmetric dual-delete (a delete applies to both stores while both are live). The alternative is routing deletes at the store the user reads. I'm happy to switch if you prefer the other semantic.

Note the delete-all mirror is a bulk wipe of the legacy collection for that uid — larger blast radius than single/batch, but it only mirrors what the canonical delete-all already committed, is confined to the write window, and no-ops after cutover. Called out so the rollout owner can weigh it explicitly.

Related: _force_canonical in the batch-delete tests previously left the read stage implicit, inheriting whatever the ambient rollout config produced. It now pins MEMORY_MODE=read explicitly, so those batch-vs-single authorization-parity cases keep asserting exactly what they always did — the contract is unchanged, only the stage is now stated.

Tests

backend/tests/unit/test_memories_batch_delete.py (23 passed, Python 3.11 .venv):

  • test_delete_during_dual_write_also_clears_the_store_the_user_reads — the single/batch regression. Fails on old code (Expected 'mock' to have been called once. Called 0 times.), passes with the fix.
  • test_delete_after_read_cutover_leaves_legacy_alone — the mirror must not fire post-cutover.
  • delete-all variants covering the same dual-write-clears / post-cutover-leaves-alone pair.

Not exercised live: I can't see prod's MEMORY_MODE or the reporter's account from here. If prod is at write, this is deterministic for every affected account rather than a flake — worth confirming when you triage.

Changes since first review

Folded in the delete-all mirror (previously split into #10454, now closed) so this PR closes the whole asymmetry in one place, per the review — single, batch, and delete-all all mirror during the write window.

Product invariants affected

  • INV-MEM-1 — no tier added, no new canonical collection, default access policy untouched; this only mirrors a delete into the pre-existing legacy collection during the migration window. Guard test green.

Failure-Class: none

No registered class covers "a mutation must land in the store the caller reads while a dual-write migration is in flight". Single instance, fixed at the route boundary that owns the decision.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@Git-on-my-level Git-on-my-level added needs-tests PR introduces logic that should be covered by tests security-review Touches auth, provider routing, secrets, or security-sensitive surfaces needs-maintainer-review Needs a human maintainer to sign off before merge labels Jul 24, 2026

@Git-on-my-level Git-on-my-level left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tracking this down — the symmetric dual-delete direction makes sense for the single and batch delete paths during the write-before-read rollout window.

I found one blocker before this can land: DELETE /v3/memories (delete-all) still only calls MemoryService.delete_all(uid) in the canonical-write branch and returns without clearing legacy. During MEMORY_MODE=write, GET /v3/memories still reads legacy, so a user who chooses “delete all memories” would see the same class of resurrection this PR fixes for single/batch deletes. Please mirror delete-all into the legacy store while canonical_read_enabled(...) is false as well, and add a regression test for that route.

Validation I ran locally with stripped env/secrets: pytest -q backend/tests/unit/test_memories_batch_delete.py using the existing backend venv from another checkout; the current touched test file passes (20 passed), but it does not cover the delete-all route.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@aryanorastar
aryanorastar force-pushed the fix/10446-dual-write-delete-visibility branch from 226a983 to a5bf83f Compare July 24, 2026 09:48
@aryanorastar

Copy link
Copy Markdown
Collaborator Author

Done — folded the delete-all mirror in (it was split into #10454, now closed), so this PR closes the whole asymmetry: single, batch, and delete-all all mirror into legacy during the write window via _mirror_delete_all_into_legacy → _purge_legacy_memories, no-op after read cutover. test_memories_batch_delete.py covers the delete-all dual-write-clears / post-cutover-leaves-alone pair — 23 passed.

Flagged the bulk-wipe blast radius of the delete-all mirror in the body for your call, since it's larger than single/batch.

(Also fixed a stale static count in test_memories_create.py — 11→12 rate-limited endpoints — that's red on main independently; separate labeled commit.)

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix(memories) mirror canonical deletes — approve only (security-review, CHANGES_REQUESTED)

aryanorastar and others added 2 commits July 25, 2026 09:17
… reads

DELETE /v3/memories/{id} and /v3/memories/batch branch on canonical_write_decision,
which enables canonical at MEMORY_MODE in {write, read}. GET /v3/memories branches on
canonical_read_enabled, which enables canonical only at MEMORY_MODE=read.

So during the dual-write stage (MEMORY_MODE=write) a delete tombstones the canonical
record while the user's list keeps reading legacy, where the row is untouched. The
client drops it optimistically and the next refresh re-reads legacy, so the memory
reappears seconds later (BasedHardware#10446).

Deleting is symmetric with dual-write: mirror the canonical delete into legacy until
read cutover. The mirror is best-effort — canonical is authoritative and already
committed, so a legacy cleanup failure must not fail a request the user saw succeed —
and it no-ops after cutover, where both sides are canonical.

_force_canonical now pins MEMORY_MODE=read explicitly rather than inheriting the
ambient rollout config, so the existing batch-vs-single authorization-parity cases
keep asserting exactly what they always did.

Refs BasedHardware#10446.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…r reads

Follow-up to the single/batch delete mirror: DELETE /v3/memories has the same
read/write asymmetry. At MEMORY_MODE=write the canonical wipe is invisible, because
GET /v3/memories still reads legacy — so "delete everything" leaves the user's entire
list intact on the next refresh (BasedHardware#10446).

Mirror the wipe into legacy until read cutover, under the same guard and best-effort
contract as the single/batch mirror. Also factors the legacy branch's collect-then-purge
into _purge_legacy_memories so both paths share one definition instead of duplicating
the pagination and vector cleanup.

Refs BasedHardware#10446.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@aryanorastar
aryanorastar force-pushed the fix/10446-dual-write-delete-visibility branch from a5bf83f to f75e7f2 Compare July 25, 2026 03:49
@Git-on-my-level Git-on-my-level removed the needs-tests PR introduces logic that should be covered by tests label Jul 25, 2026
@Git-on-my-level
Git-on-my-level dismissed their stale review July 25, 2026 08:50

Resolved on f75e7f2: delete-all now mirrors into legacy before read cutover and regression tests cover the route.

@Git-on-my-level

Copy link
Copy Markdown
Member

Thanks for the update — the previous delete-all blocker looks resolved on the current head.

What I verified:

  • Single and batch canonical deletes now mirror the specific ids into legacy only while canonical writes are on but canonical reads have not cut over.
  • DELETE /v3/memories now mirrors delete-all through _mirror_delete_all_into_legacy / _purge_legacy_memories in the same pre-read-cutover window.
  • Post-cutover cases leave legacy alone, and the legacy delete-all branch still uses the shared purge helper.
  • Targeted validation passed locally with a fresh backend venv and stripped test env: pytest -q tests/unit/test_memories_batch_delete.py → 23 passed, 9 warnings.

I removed needs-tests because the regression coverage is now present and CI is green. I’m intentionally not approving because this is still a memory-deletion/data-lifecycle change during rollout and the PR carries the right security-review / needs-maintainer-review gates. David or another maintainer should explicitly sign off on the delete-all blast-radius semantics before merge.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Real dual-write bug (#10446) — canonical delete invisible while reads still hit legacy. Scoped, best-effort mirror, tests cover both rollout stages.

@kodjima33
kodjima33 merged commit 5fa372e into BasedHardware:main Jul 25, 2026
35 checks passed
@aryanorastar

Copy link
Copy Markdown
Collaborator Author

Rebased onto current main. Green (0 failing), MERGEABLE. Single + batch + delete-all mirror are all folded in with tests. Ready for re-review.

kodjima33 pushed a commit that referenced this pull request Sep 20, 2026
…onfigured (#15097)

* fix(backend): fail privacy deletion open when the vector store is unconfigured

#10451 (5fa372e) made Pinecone-unavailable failure handling fail closed:
HistoricalMemoryAdapter.cleanup(required=True) raised 503 when
vector_db.index is None, and delete_canonical_memory_vectors returned False
→ RuntimeError → 503 on cascade retraction. Production never provisions
Pinecone on most releases (backend/deploy/runtime_env.yaml attaches the key
only to llm-gateway releases; desktop_backend_prod.yml explicitly strips it),
so every DELETE /v3/memories/{id} and every cascade
DELETE /v1/conversations/{id}?cascade=true deterministically 503'd — the
desktop app surfaces the error verbatim, and mobile reads the non-200 as a
failed delete, drops the tombstone, and the item resurfaces on refresh
(#10446 recurrence).

Deletion of the user's data is the privacy-preserving direction: when no
derived vector store is configured, there is nothing to purge, so cleanup
completes successfully; a configured store that errors still fails closed.

Tests cover the unconfigured-index deletion path, required historical
cleanup completing without a vector authority, and cascade deletion of a
conversation with attributed memories completing without a vector store.

Verified: offline harness — memory delete returned 503 before this change
and 200 afterwards.

Failure-Class: FC-privacy-delete-fails-on-unprovisioned-derived-store
Amp-Thread-ID: https://ampcode.com/threads/T-01a0b590-2123-744a-9e4a-16706295c2d3
Co-authored-by: Max Carter <[email protected]>
(cherry picked from commit c55e69a)

* chore: register failure class FC-privacy-delete-fails-on-unprovisioned-derived-store

Registers the definition for the privacy-delete failure class cited by the
two fixes in this branch.

Failure-Class: none
Amp-Thread-ID: https://ampcode.com/threads/T-01a0b590-2123-744a-9e4a-16706295c2d3
Co-authored-by: Max Carter <[email protected]>
(cherry picked from commit 12e47ac)

* chore: point the failure class at the guards this PR carries

The definition listed test_conversation_delete_wire_contract.py, which pins
the 204 delete contract for the released mobile client and ships with that
fix, not with this one. Name the two guards that are here instead: the
unconfigured-index canonical vector delete and required historical cleanup
without a vector authority, plus the module each one guards.

Co-Authored-By: Claude Opus 5 <[email protected]>

---------

Co-authored-by: Amp <[email protected]>
Co-authored-by: Max Carter <[email protected]>
Co-authored-by: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-maintainer-review Needs a human maintainer to sign off before merge security-review Touches auth, provider routing, secrets, or security-sensitive surfaces

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants