Skip to content

fix(memory): restore exclusive claim for stale-vector cleanup - #24886

Merged
Git-on-my-level merged 6 commits into
mainfrom
lane/pinecone4-fence-revert
Nov 8, 2026
Merged

Git-on-my-level merged 6 commits into
mainfrom
lane/pinecone4-fence-revert

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Requested by David · project thread

Summary
Restores the exclusive account-gate claim for stale-vector cleanup in backend/database/vector_db.py, reverting the non-claiming fence from bfb566f. Stale cleanup on read paths again holds destructive_operation_gate through the Pinecone delete, retries a failed gate finish, and propagates provider errors after exiting the claim.

Trade-off: stale cleanup can again contend with user deletes for the account gate.

Tests
Adds four hermetic cases to backend/tests/unit/test_vector_search_service.py:

  • claim is held through the provider delete
  • busy claim refuses before the provider call
  • provider failure exits the claim and preserves the error
  • hydration default path claims exclusive admission

Focused run: 4 passed (pytest -k on those names, via omi-heavy). Full file, preflight and CI not yet run.

Note: the Luna BLOCK on bfb566f still stands; this PR should not merge until that review clears.

Failure-Class: new

🤖 Generated with Claude Code

Revert bfb566f. Stale-index cleanup on read paths again claims the exclusive
account gate (destructive_operation_gate) around the provider delete, instead
of only fencing provider writes. A failing gate finish is retried, and a
provider failure exits the claim and still propagates.

Folds b53a72e's exception guard back into the restored composite fail-closed
guard. Owner-index mutation tracking and the observed projection-version
filter are preserved.

Tradeoff: stale cleanup can again compete with user deletes for the account
gate.
Adds four hermetic cases for the restored exclusive claim: claim held through
the provider delete, busy claim refusing before the provider call, provider
failure exiting the claim and preserving the error, and the hydration default
cleanup path claiming exclusive admission.
@Git-on-my-level Git-on-my-level self-assigned this Nov 7, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-08T03:25:32.134737Z f17377b New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 2 files

You’re at about 95% of the monthly reviewed-line limit. You may want to disable incremental reviews to conserve quota. Reviews will continue until that limit is exceeded. If you need help avoiding interruptions, please contact [email protected].

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread backend/database/vector_db.py Resolved
After a successful provider delete, a displaced destructive gate token raised
DestructiveGateTakeover at exit. The broad except swallowed it, retried a
finish with the displaced token, and returned a success count. Re-raise the
takeover on its own without retrying. Other exit failures keep their
existing post-delete handling.

Adds a focused test that the takeover propagates and no count is returned.
Adds the failure-class definition for a destructive operation that reports
success after a same-kind peer displaces its account-gate ownership at exit.
Declared by the stale-vector cleanup gate fix in this change.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 87078447bb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +681 to +682
except Exception:
logger.warning('canonical memory vector delete gate finalization retry failed')

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Propagate gate takeover detected during finalization retry

If a cleanup outlives the six-hour gate expiry and a same-kind peer takes ownership, the initial finish can fail with a transient authority error while the retry detects DestructiveGateTakeover. This broad handler swallows that takeover, so delete_canonical_memory_vectors_by_ids returns a successful count because provider_deleted is already true. The explicit takeover handler in the caller cannot catch exceptions swallowed inside this helper. Re-raise DestructiveGateTakeover here and cover the initial-finish-error/retry-takeover sequence so hydration does not report cleanup success after ownership has been lost.

Useful? React with 👍 / 👎.

The retry finish after a failed gate exit caught every exception, so a
DestructiveGateTakeover from the retry was swallowed and the delete count was
returned. Re-raise the takeover from the retry helper, with a guard for the
import fallback. Other retry errors keep their existing handling.

Adds a test for an ordinary exit failure followed by a takeover on the retry.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f17377bdda

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +686 to +687
except Exception:
logger.warning('canonical memory vector delete gate finalization retry failed')

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Instrument the failed gate-finalization fallback

If both gate-finalization attempts fail after the provider delete succeeds, this handler logs a warning and allows the caller to return a successful count, even though the account gate can remain running and block subsequent mutations until its six-hour expiry. This new degraded continuation needs utils.observability.fallback.record_fallback with a degraded outcome so the unresolved authority state is visible through the shared fallback monitoring rather than only a log message, as required by the backend guide.

AGENTS.md reference: backend/AGENTS.md:L322-L322

Useful? React with 👍 / 👎.

@Git-on-my-level
Git-on-my-level merged commit 32c68e6 into main Nov 8, 2026
59 checks passed
@Git-on-my-level
Git-on-my-level deleted the lane/pinecone4-fence-revert branch November 8, 2026 03:41
kodjima33 added a commit that referenced this pull request Nov 11, 2026
…wn deletion gate (#24988)

## Problem (P0, privacy)

`DELETE /v1/conversations/{id}?cascade=true` answers `409
account_gate_busy` for everyone. Prod (Cloud Run `backend`), 2026-11-09
09:00 to 11:35 UTC: **371 of 374** such DELETEs returned 409, **0
returned 204** (the other 3: 2×504, 1×401), across 31 client IPs and 186
distinct conversations. Memory deletes look the same: `DELETE
/v3/memories*` since 06:00 UTC, **1487 of 1491** returned 409.

The route turns the gate exceptions into a 409 without logging, so these
requests leave no traceback. The evidence below comes from Firestore and
the code path:

- `legal_hold_deletion_gates` (one doc per uid, read-only query):
`explicit_memory_deletion` gates ending in `failed` (acquired, then the
body raised) go from **0 on 2026-10-26** to **49 on 10-27** and
80–170/day since. Today: 80 failed vs 12 completed. Only 7 gates are
`running`, so stale gates across accounts are not the cause.
- 272 of the 371 conversation 409s return in under 0.2 s. These are
retries hitting a gate held by the same account. About 99 run longer:
those acquired the gate and failed inside the body.

## Root cause: the request blocks on its own gate

1. `delete_conversation` takes `destructive_operation_gate(uid,
kind='explicit_memory_deletion')` for the whole cascade body. Introduced
by **#23783** (David Zhang, commits fe4499a + 1bba122, merged
**2026-10-27 06:38 UTC**). The analyst found the last normal success at
2026-10-27 16:57 UTC.
2. Inside the body, `delete_conversation_and_frame_evidence` →
`delete_conversation_with_sync_sources` →
`conversations_db.delete_conversation` starts with
`speaker_auto_enroll.retract_conversation` (#23625, 2026-10-26), which
runs `external_write_fence(uid, transaction=...)`.
`delete_transcript_chunk_vectors` is fenced the same way without a
transaction (#22835, 2026-10-20).
3. `external_write_fence` reads `legal_hold_deletion_gates/{uid}`, sees
the request's **own** running gate, and raises
`DestructiveOperationInProgress: external data write blocked by
destructive operation`. The `_EXTERNAL_TRANSACTION` reuse only covers
writes nested inside another transactional fence, not writes under a
gate the request owns.

Effect: the tombstone is written and memories and action items are
retracted, but the conversation doc, its vectors and its audio stay. The
client retries and gets 409 every time. One Windows client retried 144
times.

## Fix

`external_write_fence` now lets a write through only when the running
gate is the one **this context** acquired: `_ACTIVE_GATE == (uid,
gate.kind, gate.token)` and `gate.uid == uid`. Unchanged:
- other requests (they have no `_ACTIVE_GATE`), other accounts, and a
holder displaced by a stale takeover (its token no longer matches) are
still blocked;
- account deletion (`account_deletions/{uid}`) still blocks, including
the gate owner;
- a pass through the owner's own gate sees a running gate, so `reusable`
stays false and the result is never cached for other writers;
- canonical intake (`assert_no_destructive_operation_transaction`) stays
strict even inside the owner's context.
`test_ws_j_delete_privacy::test_delete_all_final_rescan_tombstones_concurrent_write`
still proves an inline intake write is refused.

## Tests

- New:
`test_legal_holds.py::test_gate_owner_nested_writes_are_not_blocked_by_their_own_gate[False|True]`,
`…exemption_never_reaches_other_requests_or_accounts`,
`…exemption_ends_after_peer_takeover_and_never_skips_account_deletion`.
- New, route level with the real gate on a fake Firestore:
`test_conversation_delete_wire_contract.py::test_cascade_delete_nested_fenced_writes_do_not_hit_their_own_gate`.
Before the fix it fails with `409 != 204`. The existing wire tests
replaced the gate with `nullcontext()`, which is why they never caught
this.
- Without the fix, the 3 new reproduction tests fail. With it, all 83
unit test files that reference the gate or fences pass (3128 tests).

## Not proven

- Which nested call blocks **memory** deletes (`MemoryService.delete` /
`delete_batch` under `_legal_hold_gated_deletion`). The fix covers any
nested `external_write_fence` in that path. If a memory-path self-block
goes through `assert_no_destructive_operation_transaction` instead, this
PR does not fix it. Check memory DELETE 409 rates after deploy.
- Conversations left half-deleted between 2026-10-27 and the deploy
(tombstoned but still stored) need a cleanup pass. Not in this PR.

## Scope after review
This fixes cascade **conversation** deletes only. Memory deletes
(`routers/memories.py`) never reach a nested `external_write_fence`;
their 409s are tracked separately (likely #24886, which made stale
memory-vector cleanup on read paths take the exclusive account gate).
About 272 of the 371 conversation 409s sampled today fail before the
delete body runs (gate acquisition), which this PR does not change;
check the 204 rate after deploy.

Added after review:
`test_gate_owner_pass_is_never_cached_for_other_writers` (privacy_ttl60
on): the owner passes its own fence, a writer in a fresh context still
raises and re-reads live authority every time. It fails if the owner
pass is made reusable (`legal_holds.py:534`).

## Product invariants affected

none

Failure-Class: FC-destructive-gate-keyed-on-proxy-for-intent

UI-Evidence: none

🤖 Generated with [Claude Code](https://claude.com/claude-code)


<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/BasedHardware/omi/pull/24988?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="View guided diff"
src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>
<a
href="https://www.cubic.dev/action/auto-fix/pr/BasedHardware/omi/24988?returnTo=https%3A%2F%2Fgithub.com%2FBasedHardware%2Fomi%2Fpull%2F24988&source=description"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"><img
alt="Turn on auto-fix"
src="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant