Repository navigation
perf: reduce Pinecone search scans with verified owner partitions - #24315
Merged
Merged
Conversation
There was a problem hiding this comment.
All reported issues were addressed across 33 files
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
Git-on-my-level
added a commit
that referenced
this pull request
Nov 2, 2026
…tition copies (#24400) ## Why The Pinecone owner-partition operator (#24315) ends each qualification by querying the owner's old namespace and new bucket with a few of the owner's own vectors, and it required the two top-10 ID lists to be identical in order. Pinecone scores the same record slightly differently in different namespaces, so near-tied neighbours swap and the operator refuses copies that are complete and exactly verified. Measured on `memories-backend-dev` with a loopback Firestore emulator and synthetic owners (dense random 3072-d vectors), after a complete copy whose payload digests all matched: | records | probes with exact order | same ID set | same-ID score delta | smallest adjacent gap | |---|---|---|---|---| | 3 | qualified | | | | | 120 | 10/12 | 12/12 | 1.8-3.8e-4 | 2.8e-5 | | 230 | 8/12 | 11/12 (one swap at the rank-10 cutoff) | 1.8-4.0e-4 | 4e-6 | The odds of a near-tie in a top-10 rise with an owner's vector count, so the check blocks exactly the heavy owners whose queries cost the most. Production shows the same signature: the 11-02 rollout journal recorded 29 `bounded_search_convergence` mismatches, and those 29 keys are exactly the routes it left in `qualification_failed`. ## What changes - `search_parity()` replaces the exact list comparison. IDs present in both lists must score within 1e-3 (2.5x the largest drift measured). An ID may appear in only one list solely at the cutoff, tied with the other list's lowest score. Two IDs may rank in opposite order only when their scores are within 1e-3. Anything else still raises `MigrationNotReady`. `query_checks` records whether the order was exact. - The exact payload, inventory, freshness and revision checks are unchanged; they already prove every record byte-for-byte. This check only bounds ranking drift. - The operator CLI keeps the reason text of `MigrationNotReady` / `PartitionConflict` in receipts and owner journals. These are fixed strings from our own code, never provider text, and without them a failed batch can't be diagnosed. - The test provider fake now scores by dot product (it returned a constant 0.9, which made every order a tie) with per-namespace score jitter. Only `scripts/migrate_pinecone_partitions.py` and `scripts/qualify_pinecone_partitions.py` import this module. Serving code and every deployed runtime are unchanged, so no deploy is needed. ## Verification - `backend/test.sh` with `BACKEND_UNIT_TEST_FILE_LIST` = `test_pinecone_partition_migration.py`: 52 passed, including the new near-tie, drift and `search_parity` cases. The existing reversed-order case still fails closed. - `test_migrate_pinecone_partitions.py`: 23 passed. `test_qualification_script_direct_execution_reaches_safety_gate` timed out locally: the script reaches the expected gate error, but takes about 10s of wall time on a host at load 60 (about 1s CPU). That script is untouched here. - Dev qualification of this exact commit (6882b36) against `memories-backend-dev` with a loopback Firestore emulator, all steps passed (2026-11-02 14:59Z): `qualify_pinecone_partitions.py` (7 families x 3 migrate/update/rollback cycles with a shared-bucket owner, cleanup verified); the operator CLI end to end on two colliding synthetic owners with 2-230 dense vectors per family (inventory, budget stop then `--resume`, apply, serving write after cutover, rollback, re-apply, idempotent rerun, cleanup verified); and the opt-in action-item user-path test (run with a deterministic local embedding in place of OpenAI, because this lane holds no OpenAI key; the router, Firestore, owner routing, migration and Pinecone calls were real). Before this change the same CLI run refused 6 of 7 families with `Search result order differs`. Product invariants: none (operator-only module). Failure-Class: none Rationale: this is a parity-check tolerance in an operator tool, not a recurrence of a catalogued serving failure class. The nearest entries (FC-split-mutation-authority, FC-test-double-accepts-payload-store-rejects) do not describe it: the fake's constant score did hide the defect, but it accepted too little, not a payload production rejects. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
kodjima33
added a commit
that referenced
this pull request
Nov 14, 2026
…checks (#25298) ## Summary Account-deletion wipes on Cloud Run `backend-sync` (queue `account-deletion`) have returned HTTP 500 since 2026-11-09. Three accounts are stuck in a permanent retry loop. Firebase Auth is already removed for them, but their conversations, memories, vectors and storage are still there 1 to 5 days after the wipe came due. This PR fixes the two causes, both in the Pinecone owner adapter. ## Blast radius (prod, read-only: Cloud Logging + Firestore `account_deletions`) - **3 accounts stuck.** All 3 non-grace wipes in flight right now are in this loop (`failed` 1, `retrying` 1, `running` 1). None can finish without this fix. - A: due 2026-11-09 00:11Z, failing since 00:12Z, 524 attempts. Cause 1 (memory vectors). - B: due 2026-11-09 18:47Z, 417 attempts. Cause 1 (conversation vectors) since 18:47Z. Cause 2 (ns3) since 2026-11-10 17:40Z. - C: grace wipe requested 2026-10-30, came due 2026-11-13, failing since 23:33Z, 56 attempts. Cause 1 (conversation + memory vectors). - 4 more accounts each hit one transient failure (GCS DELETE, Firestore contention, shutdown drain) and completed within 15 minutes. They are unrelated to this PR. - Other wipes keep completing: 22 `Account Deletion Wipe Completed` events in the last 3 days. Totals: 1,200 completed, 70 in grace (`pending_deletion`), 6 cancelled. - Wipe 500s per day: 11-09 77, 11-10 156, 11-11 168, 11-12 129, 11-13 137, 11-14 125 (through 13:00Z). The first 500 of this wave was at 2026-11-09 01:26Z. There were none from 11-01 to 11-08. The handler also logged 116 `final attempt failed`, and each one was re-enqueued by the reconciler. - A separate 500 wave ran 2026-10-20 to 10-28. It predates the introducing PR and stopped by itself. ## Root cause Both causes came in with 0c3047b, "perf: route Pinecone searches through verified owner partitions" (#24315, author Nik Shevchenko, merged 2026-11-01). That commit moved every account-wipe vector delete from `index.delete(ids=…)` to `owner_index(…).delete(…)`. 1. **413 on the ownership pre-check.** `OwnerIndex._mutate` sends all ids of a delete chunk to a single `index.fetch` (`vector_db` chunks at 1,000 ids) before deleting them. Pinecone `fetch` is a `GET` with every id in the query string. I probed the prod index read-only with synthetic, non-existent ids of production length: 800 ids returned OK and 1,000 ids returned **HTTP 413**. So any account with more than about 800 conversations or memories can never finish purging. - Logs: `delete_account purge conversation vectors failed: (413)`. - Logs: `memory_vectors only deleted 339/1339`. The first 1,000-id chunk fails and the 339-id remainder succeeds. 2. **The purge's own lost delete blocks it forever.** On 2026-11-10 17:40Z, a connection reset hit account B's `delete_all_copies` on ns3. The admitted full-owner **delete** was correctly marked `uncertain`. Its route now shows `uncertain_count=1`, and its operations journal holds exactly one `uncertain` op of kind `delete`. `verify_empty_all_copies` refuses while any op is uncertain, and only an operator can reconcile that marker. So every retry since then has failed with `Account purge must wait for outstanding vector work`. ## Fix - `OwnerIndex._mutate` now runs the owner check in URL-bounded batches (`_fetch_batches`, 16 KB query budget, about 228 ids per fetch). The check and the delete behave exactly as before. Only the provider request size changes. - `verify_empty_all_copies` still refuses while there is pending work, an active copy, or any uncertain op other than `delete`. A late delete cannot bring data back. The proof of emptiness is unchanged: an LSN-fenced query of both copies must return no matches. The new read-only transaction `state.uncertain_mutations_are_deletes` re-reads the route at the verified revision and checks the kinds in the operations journal (single-field equality, bounded by `uncertain_count + 1`). It runs only when `uncertain_count > 0`. - What still holds: - Migration and rollback still refuse on any uncertain op. - Uncertain upserts and updates still block the purge (new test). - Legal holds are untouched (`acquire_destructive_operation` still runs first). - Retries stay idempotent: the full-owner delete and the barrier repeat safely (the test repeats the purge). ## Tests - `test_account_wipe_sized_delete_fetches_owner_check_in_url_bounded_batches` uses a fake index that returns 413 when the query string is over 60 KB, then deletes 1,000 production-length ids. On origin/main it fails with `PineconeApiException: (413)`. With the fix it passes, all fetch batches stay inside the budget, and every id is checked in both copies. - `test_account_purge_completes_after_its_own_delete_outcome_was_lost` replays account B: a connection reset on the purge delete, then a retry and a repeat. On origin/main it fails with `PartitionBusy: Account purge must wait for outstanding vector work`. With the fix it passes, and migration is still blocked. - `test_account_purge_still_waits_for_an_uncertain_write[upsert|update]` passes before and after. It guards the remaining fence. - The existing `test_purge_attempts_known_erasures_despite_uncertainty_but_never_claims_done` (uncertain op of `unknown` kind) still blocks. - I ran the local suites: `test_pinecone_partition_index.py` and `test_pinecone_partition_state.py` (147 passed), and `test_firestore_query_shapes.py` (583 passed). I also ran each selected unit file that touches vectors or account deletion (252 files: 251 passed. `test_empty_capture_recovery.py` fails locally only because native libopus is missing on this machine, which has nothing to do with this change). Not run: a live wipe. Prod is read-only for this change, and a dev account with more than 1,000 vectors was not available. Once this is deployed, the three stuck accounts should complete on their next reconciler retry. Account B's ns3 uncertain op stays in the journal for operator reconciliation, and nothing reads it again after the wipe. ## Product invariants affected none ## Failure class (fixes) Failure-Class: new 🤖 Generated with [Claude Code](https://claude.com/claude-code)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed and why
Pinecone searches currently scan shared feature namespaces even when filtered to one user. Route each owner's reads and writes through a Firestore authority so a verified migration can move that owner into one of 256 smaller buckets per feature, preserving UID filters and canonical hydration.
The operator copies and reconciles all seven vector families, verifies exact payloads and provider indexing freshness, then switches the route with a revision check. Unknown writes block migration; account deletion removes both copies; rollback recopies the current primary before switching. Production apply requires reviewed writer-fleet and test-account evidence. This PR does not itself migrate production users or prove dollar savings.
Product invariants affected
How it was verified
make preflight: all 35 selected repository checks passed, including Firestore query guards and all 18 runtime image source closures.RUN_LIVE_PINECONE_USER_PATH=1 PYTHONPATH=backend backend/.venv/bin/python backend/tests/integration/test_pinecone_partition_user_path.py: real action-item API, Firestore emulator, OpenAI embeddings and Pinecone dev index. 30 HTTP responses; three create/search/update/delete cycles; two owners sharing a bucket; foreign-owner denial; exact provider payload checks and verified cleanup. Auth is overridden only for synthetic identities; this is not a production or mobile-UI test.PYTHONPATH=backend backend/.venv/bin/python backend/scripts/qualify_pinecone_partitions.py <private-proof.json>: three forward/update/rollback cycles for each of seven families, deliberate bucket collisions and 14 all-copy cleanup checks against Pinecone dev. Operator CLI separately passed 21 forward/rollback/forward cutovers for an empty synthetic owner. A separate real-provider bulk test copied and rolled back 125 realistic 3072-dimensional records, verified payloads and cleanup, and reduced the largest upload from a 6.6 MB unsplit page to 1.78 MB batches.Review fixes also require a configured provider before acknowledging account erasure, expand route-discovered owners across every selected family, and verify cleanup after injected test setup failure.
After refreshing main, corrected its flag-registry count from 214 to 215 to match the entries already present. The registry consistency and migration suites pass with the same CI timing guard.
The real-provider, bulk and HTTP proofs were repeated on
30eeb11e71; all captured implementation hashes remained unchanged throughout each run. Production fleet deployment, test-account pilot and all-user migration remain rollout steps. Stored legacy copies continue incurring storage cost until a separately verified retention cleanup.Tests
test_pinecone_partition_state.py,test_pinecone_partition_index.py,test_pinecone_partition_migration.pyandtest_migrate_pinecone_partitions.pycover stale receipts, concurrent writes, ambiguous provider outcomes, safe recovery, multi-tenant isolation, >10,000-record qualification, rollback, deletion races and bounded journal-owned resume, and byte-bounded 3072-dimensional copy batches. The opt-in integration test exercises the actual public action-item handlers and provider mutations.UI evidence
UI-Evidence: none -- backend routing and operator tooling only.
Failure class
Failure-Class: none