Skip to content

SCA-441: map account-gate delete contention to 409 - #12707

Merged
Git-on-my-level merged 6 commits into
mainfrom
agent/memory-delete-gate
Sep 4, 2026
Merged

Git-on-my-level merged 6 commits into
mainfrom
agent/memory-delete-gate

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Sep 4, 2026 •

Copy link
Copy Markdown
Member

What changed and why

Live DELETE /v3/memories/{id} was 500-ing because two concurrent deletes for one account contend on the exclusive destructive_operation_gate. The router only mapped ValueError → 404; DestructiveOperationInProgress escaped as 500. The Windows bulk cleaner made this worse with CONCURRENCY = 4 single deletes (three of every four in-flight requests lose the gate, then retry 5xx).

Smallest fix that closes it: keep the 6h exclusive gate (do not weaken account deletion / legal holds), map contention to 409 + Retry-After: 2 + detail: account_gate_busy on the single, batch, and cascade-conversation delete routes, and send Windows bulk cleanup through DELETE /v3/memories/batch in chunks of ≤100 so one request holds the gate once.

Closes SCA-441

Product invariants affected

  • INV-MEM-3
  • INV-MEM-4
  • INV-MEM-1

HTTP mapping only: delete still goes through MemoryService / the existing exclusive destructive_operation_gate. No new intake path, no extra tier, no bypass of canonical delete. Conversation cascade retract still uses the same memory-delete owner; this PR only maps DestructiveOperationInProgress to 409 instead of 500.

Line-Count-Exception: backend/routers/conversations.py | 2203 -> 2210 | Map DestructiveOperationInProgress on cascade retract to 409; extracting a new owner is out of scope.

How it was verified

Backend (worktree venv):

cd backend && .venv/bin/python -m pytest -q \
  tests/unit/test_account_gate_busy_http.py \
  tests/unit/test_memories_batch_delete.py \
  tests/unit/test_legal_holds.py \
  tests/unit/test_delete_conversation_cascade_retraction_fence.py

25 + 14 passed (legal_holds file 25; combined targeted run 14 including the new 409 tests). Concurrent gate test: one destructive_operation_gate(uid) succeeds, the other raises DestructiveOperationInProgress; external_write_fence still blocks while the live gate is held. GATE_STALE_AFTER_SECONDS remains 6h.

Windows:

cd desktop/windows && ./node_modules/.bin/vitest run src/main/memoryCleanup/bulkDelete.test.ts

11 passed. ./node_modules/.bin/tsc --noEmit -p tsconfig.node.json --composite false exited 0.

Did not serve the live FastAPI process against a real Firestore gate in this run (no prod/dev GCP writes). The router tests drive the production handlers with a service that raises the typed exception.

Tests

Red proof (unfixed tree: only ValueError was caught on delete):

  • test_delete_memory_maps_gate_busy_to_409_not_500: pytest.raises(HTTPException) failed because DestructiveOperationInProgress: another destructive operation owns the account gate escaped uncaught.
  • test_batch_delete_maps_gate_busy_to_409_not_500: same uncaught exception from delete_batch.
  • classifyStatus(409) expected 'retry', received 'fail'.
  • test_concurrent_single_deletes_one_succeeds_other_raises_typed_exception is the reproduction and stays: two concurrent destructive_operation_gate(uid) contexts, one winner, typed exception on the loser.

Green after: 409 + Retry-After: 2 + account_gate_busy on single and batch delete; conversation retract source-maps the same helper; Windows bulk path issues DELETE /v3/memories/batch (100+1 for 101 ids) and retries 409 using the header.

Failure class (fixes)

Failure-Class: none

First instance of this HTTP mapping. The gate itself is unchanged (still exclusive, still 6h stale). Existing FC-destructive-gate-keyed-on-proxy-for-intent is a different contract (empty replacement set vs destroy).

Part B (POST /v3/memories 503) — findings, not this PR

PR #12524 already resolves an identical-content resubmission after the account head has moved (_existing_identical_add_row compares content.strip() only). Windows POST /v3/memories does not send an id; the server derives it from uid + source_id ("unknown" when omitted) + stripped content. Tags, category, and source are outside that identity, so a focus/insight dual-write with different text cannot share an id. Remaining 503s are likely (1) two in-flight identical POSTs racing before the winner's snapshot is readable, so _existing_identical_add_row returns None and create_memory still wraps every exception as 503, or (2) an invalid_patch reason other than add patch new_memory_id already exists. A red-proof fix would be a bounded snapshot re-read on that specific reason, plus not mapping unrelated apply failures to 503. Not done here.

Follow-ups not in this PR

  • macOS: bulk delete uses deleteAllMemories; single deleteMemory is not fanned out. Insight TaskGroup is mark-read, not delete. No change.
  • Flutter: deleteMemoryServer is per-row; bulk uses DELETE /v3/memories. No fan-out. No change.
  • MCP / developer delete routes still let DestructiveOperationInProgress escape as 500.
  • Renderer AdvancedTab / deleteMemoriesPaced still treat 409 as a hard fail (serial, so they do not self-collide; they can still lose to another in-flight gated op).
  • DELETE /v3/memories (delete-all) is ungated in the router the same way.

Do not merge; coordinator reviews.

@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 10 files

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

Re-trigger cubic

Git-on-my-level and others added 3 commits September 3, 2026 22:55
Concurrent single-memory deletes for one uid collided on the exclusive
destructive-operation gate and escaped as 500. Return 409 + Retry-After
instead, and send Windows bulk cleanup through DELETE /v3/memories/batch.

Co-authored-by: Cursor <[email protected]>
…umps

Conversation-router unit tests stub database/utils.other at load, so the
gate-busy mapping must not import those modules at router import time.
PINNED_CONVERSATION_DUMPS now includes the daily-summary stats model_dump
sites already on main.

Co-authored-by: Cursor <[email protected]>
Pinning the daily-summary dump sites puts this file in the changed set;
its red-proof scans exceed 0.30s CPU under saturated pre-push fanout.

Co-authored-by: Cursor <[email protected]>
@Git-on-my-level
Git-on-my-level force-pushed the agent/memory-delete-gate branch from 7a845e1 to 1ed990a Compare September 4, 2026 04:11
Git-on-my-level and others added 2 commits September 4, 2026 13:00
…isection

- Wire deleteAppIndexMemories to window.omi.memoriesBulkDelete so the
  shipped delete flow actually uses the batch route + 409 retry
  behavior instead of per-ID deletes (thread 1).
- Drop baseURL from the renderer-supplied IPC args; the main process
  derives it from VITE_OMI_API_BASE like ipc/auth.ts / ipc/byok.ts, so a
  compromised renderer cannot redirect bearer-token requests (thread 2).
- Bisect a 404 batch chunk instead of fanning the whole chunk out through
  the 60/hour single-delete limiter; only genuinely stale ids reach the
  single-delete route (thread 3).
- Rewrite the cascade 409 static test as a structural regex tying the
  RuntimeError/name-check catch to account_gate_busy_http_exception(),
  matching the 503 sibling test, so mapping regressions fail the guard
  (thread 4).

Verification:
- desktop/windows: vitest run src/main/memoryCleanup/bulkDelete.test.ts
  (12 passed, incl. 2 new bisection tests); npm run typecheck (0 errors);
  full vitest suite 5562 passed, 1 pre-existing Modal.test.tsx failure
  (fails at HEAD too, unrelated pointerdown assertion).
- backend: pytest test_delete_conversation_cascade_retraction_fence.py
  + test_account_gate_busy_http.py (6 passed),
  test_memories_batch_delete.py + test_legal_holds.py (31 passed).

Co-authored-by: Cursor <[email protected]>
# Conflicts:
#	backend/tests/fast_unit_duration_allowlist.txt
Git-on-my-level added a commit that referenced this pull request Sep 4, 2026
…#12729)

Two exact-set pins fail on main, so any PR whose test selection reaches them
goes red for reasons unrelated to its diff. #12707 is failing on this today.

Both share a cause: the change that broke each one did not touch a file that
selects the test that pins it, so the selector never ran it on the PR that
introduced it.

25a7d76 added two model_dump() calls in external_integrations.py. The
trust-boundary scanner keys on the call, not the receiver's type, so both are
reported as conversation serialization sites. Both dump a
DailySummaryDayStatsPayload, so neither can carry client_processing; pinned with
that reasoning beside the entries.

44d4888 deleted two onboarding views the task-intelligence manifest still
lists as desktop_manual owner paths. Removing them uncovers a second failure the
first was masking: ContextReminderCoordinator writes and updates action items and
is registered nowhere.

That one is a judgement. It is registered as desktop_manual / direct_command, not
as a proactive writer. The directory says ProactiveAssistants, which is why the
code was read: the create runs inside a user-invoked reminder flow on the user's
own text, its failure strings ask the user to try again, and updateActionItem is
reached from markDone, a button. Nothing fires on a timer or on observed context
alone. Under INV-TASK-2 that is a direct command. If context reminders are meant
to propose rather than write, the code is what should change, not this entry.

Co-authored-by: Claude Opus 5 <[email protected]>
@Git-on-my-level
Git-on-my-level merged commit 8e1a61a into main Sep 4, 2026
40 checks passed
@Git-on-my-level
Git-on-my-level deleted the agent/memory-delete-gate branch September 4, 2026 09:32
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