Skip to content

fix(backend): give conversation erasure its own task queue - #25299

Merged
kodjima33 merged 2 commits into
mainfrom
fix/conversation-erasure-queue
Nov 14, 2026
Merged

kodjima33 merged 2 commits into
mainfrom
fix/conversation-erasure-queue

Conversation

@kodjima33

@kodjima33 kodjima33 commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

What changed and why

#25124/#25197 put conversation batch purges and accepted-delete erasures on the account-deletion Cloud Tasks queue (prod: 4 concurrent dispatches). Their handler also ran on the same 4-worker cleanup_executor as account wipes. On 2026-11-14 a cleanup of 533 half-deleted conversations queued about 760 tasks there. The queue drained 2 to 3 tasks a minute. 32 real users' accepted deletes and 2 account deletions waited up to 40 minutes. The reconciler also redrove every obligation that had waited longer than REDRIVE_SECONDS (600 s), which added duplicate tasks to the same backlog.

  1. Own queue. A new CONVERSATION_ERASURE_TASKS_QUEUE (prod value conversation-erasure) is set on all 5 hosts that run the reconciler: backend-listen (manifest and chart), backend, backend-sync, backend-sync-backfill and backend-integration. Both kinds of erasure unit now dispatch there. Account wipes stay alone on account-deletion. The prod dispatch contract pins the literal.
  2. Own pool. /v1/conversations/deletion-batch-purges/run now runs on a new erasure_executor with 20 workers. Without this, wipes and units would still contend on cleanup's 4 threads inside each backend-sync instance.
  3. No duplicate redrive. Before the reconciler mints a new generation, it probes the previous generation's task by name, first in conversation-erasure and then in account-deletion. If that task is still waiting, delivering or retrying, it is left to run that same generation, and the obligation is re-checked after REDRIVE_SECONDS. Only an authoritative NotFound in both queues allows a redrive. If the probe fails (permission or transport error), the reconciler redrives as before and records the fallback (blind_redrive, once per pass), so no obligation is stranded.
  4. Deploy creates the queue. On every prod deploy, deploy-backend-stack creates or updates conversation-erasure before any traffic shift. It uses the same list-based step as batch-quarantine-recovery, and an inventory error fails the step. Staging gets staging-conversation-erasure in queue_map and create_queues.sh.

Rollout safety (no task lost)

  • Old revisions ignore the new env and keep dispatching to account-deletion. Their tasks still reach the same handler, and the new reconciler probes both queues for them.
  • If conversation-erasure does not exist at enqueue time (NotFound), the unit goes to account-deletion instead. This is recorded via record_fallback, conversation_erasure_queue -> account_deletion_queue. A rollback deploy therefore never strands work.
  • Generation fencing (begin_run and run_token) is unchanged. A late duplicate is still superseded and the lease still prevents overlap.
  • The ~760 tasks already on account-deletion are left alone. They drain there, and new passes stop adding duplicates on top of them.

Queue sizing

--max-concurrent-dispatches=20 --max-dispatches-per-second=5 --max-attempts=5 --min-backoff=5s --max-backoff=300s

  • Throughput. 4 slots drained 2 to 3 units a minute, which is roughly 80 to 120 s per slot including gate waits. The cleanup armed about 400 units an hour (6.7 a minute). 20 slots give about 12 to 15 a minute, so the arming peak drains with about 2x headroom.
  • Firestore. Units serialize per account on its gate. A purge unit touches at most 10 rows per 1.5 s, then yields 0.6 s. 20 concurrent units are at most about 100 row deletes a second, far below Firestore write limits. Accepted-delete cascades run the same work the foreground DELETE route already runs inline.
  • backend-sync. Container concurrency is 20 and max scale is 40 (read 2026-11-14), so 20 dispatches fit one instance. The 20-worker erasure pool means a delivered task never waits for a thread against its 1500 s dispatch deadline.
  • Rate cap. 5/s only bounds bursts. The reconciler enqueues at most about 100 units per 60 s pass.

Infra step

The deploy creates the queue. The new step Configure production conversation-erasure Cloud Tasks queue runs on the next prod backend deploy, before traffic moves. Nothing manual is needed. The equivalent command, for reference only:

gcloud tasks queues create conversation-erasure --location=us-central1 --project=based-hardware \
  --max-concurrent-dispatches=20 --max-dispatches-per-second=5 --max-attempts=5 \
  --min-backoff=5s --max-backoff=300s

No IAM change is needed. Read-only on 2026-11-14: backend-runtime@ and backend-listen-runtime@ already hold project-level roles/cloudtasks.enqueuer and roles/cloudtasks.viewer (the viewer role covers the tasks.get probe). omi-ci-deployer-prod@ holds roles/cloudtasks.queueAdmin. Neither queue has queue-level IAM bindings. Staging: run backend/deploy/staging/create_queues.sh --apply on the dev-operator tier.

Product invariants affected

  • INV-MEM-4: path-triggered by backend/main.py (a log condition only). Memory promotion is not touched.

How it was verified

UNVERIFIED in production. Nothing here is deployed, and I created and changed no live queue. Prod reads only: gcloud tasks queues describe/list/get-iam-policy, gcloud projects get-iam-policy, gcloud run services describe backend-sync.

Run locally in a Python 3.11.15 worktree venv:

  • Targeted suites: pytest tests/unit/test_conversation_erasure_queue_deploy.py test_conversation_delete_gate_wait.py test_conversation_delete_accept.py test_conversation_batch_purge_{resumable,authority,deadlines}.py test_executors.py: 146+ passed.
  • Deploy/manifest suites: test_backend_runtime_env_validator (271), test_render_backend_runtime_env (93), test_staging_overlay (21), test_cross_service_deploy_contract (30, 3 skipped), test_sync_cloud_tasks (209), test_cloud_run_live_caps (50), test_batch_pipeline_recovery_runtime_contract (8), tests/routers/test_users.py (327): all passed.
  • Firestore emulator: tests/integration/test_conversation_batch_purge_emulator.py 14 passed.
  • backend/scripts/pre-deploy-check.sh: 578 passed. scan_async_blockers, check_module_stub_pollution, scan_import_time_side_effects, verify_pusher_cohost_env_diff, check_agents_md_lean: clean.

Tests

  • test_generation_still_in_its_queue_is_not_redriven: a backlog older than the redrive delay leaves generation 1 queued, and the waiting task still purges it. This would have caught the duplicate backlog.
  • test_generation_gone_from_its_queue_is_redriven, test_unanswered_probe_redrives_rather_than_strand, test_one_unanswered_probe_per_pass, test_running_generation_is_skipped_without_a_probe, test_queued_hold_never_touches_a_newer_generation.
  • test_units_ride_the_conversation_erasure_queue_apart_from_account_wipes, test_task_probe_looks_in_the_erasure_queue_then_the_shared_one, test_missing_erasure_queue_keeps_units_on_the_shared_queue.
  • test_task_handler_runs_on_its_own_pool_not_the_account_wipe_pool.
  • test_conversation_erasure_queue_deploy.py: the deploy step runs before the traffic shift with the bounded flags and fails closed on inventory errors. Every prod dispatcher, the GKE chart and staging name the queue, and the prod contract rejects erasure units on the wipe queue.

UI evidence

UI-Evidence: none -- backend and deploy only

Failure class (fixes)

Failure-Class: FC-backlog-release-shares-lane-with-fresh-work

The redrive half also matches FC-queued-delivery-invalidated-by-redrive: queue delay past the retry interval made the dispatcher mint new generations for tasks that were still queued.

🤖 Generated with Claude Code

View guided diff Turn on auto-fix

kodjima33 and others added 2 commits November 14, 2026 08:28
Conversation batch purges and accepted-delete erasures dispatched onto the
account-deletion queue (4 dispatches) and ran on the same 4-worker cleanup
pool as account wipes. A 533-conversation cleanup queued ~760 of them; real
users' accepted deletes and account deletions waited up to 40 minutes, and
the reconciler redrove every obligation that waited past 10 minutes, adding
duplicate tasks to the same backlog.

- New CONVERSATION_ERASURE_TASKS_QUEUE (prod: conversation-erasure) on every
  dispatcher; the deploy creates the queue (20 concurrent, 5/s, 5 attempts)
  before traffic moves. Unset, or the queue missing at enqueue, keeps the
  shared queue (recorded fallback). Account wipes stay alone on
  account-deletion.
- The handler runs on its own 20-worker erasure pool, off cleanup.
- The reconciler probes the previous generation's task (new queue, then the
  shared one) and leaves a still-queued task to run instead of redriving it.
  An unanswered probe redrives as before (recorded fallback, once per pass).

Failure-Class: FC-backlog-release-shares-lane-with-fresh-work

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Failure-Class: FC-backlog-release-shares-lane-with-fresh-work

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kodjima33 kodjima33 added the mux-proposal Mobile UX improver (MUX) proposal; waits for Nik's ship label Nov 14, 2026
@kodjima33
kodjima33 merged commit 6f4db33 into main Nov 14, 2026
72 of 73 checks passed
@kodjima33
kodjima33 deleted the fix/conversation-erasure-queue branch November 14, 2026 15:11
kodjima33 added a commit that referenced this pull request Nov 15, 2026
…ds off instead of failing its task (#25393)

## Problem

On Cloud Run `backend-sync`, `POST
/v1/conversations/deletion-batch-purges/run` has answered HTTP 500 1,461
times since 2026-11-12T18:05Z. Per day: 53 on 11-12, 1,179 on 11-13, 85
on 11-14 and 144 on 11-15 up to about 11:07Z. The first 500 came an hour
after `backend-sync-83c5eaa` (2026-11-12T17:04Z), the first revision to
serve the route from #25124. The 11-13 peak was before #25299 (queue
switch) and #25298, so neither of those caused it.

The handler did log every failure, but only the exception class, and the
line is not attached to the request trace. Across the WARNING lines
(kept in full), 1,293 of 1,462 failures are
`DestructiveOperationInProgress`. The rest are scattered transient
Firestore errors (`DeadlineExceeded`, `ServiceUnavailable`, `ValueError`
commit contention, `ReadTimeout`).

## Root cause

A purge unit waits `UNIT_GATE_WAIT_SECONDS` (30 s) for the account's
destructive gate. A same-account multi-delete arms one obligation per
conversation, and the reconciler dispatches all of them at once. They
then queue on that one gate together with the account's accepted-delete
erasures and foreground deletes. A unit that waits past 30 s raises
`DestructiveOperationInProgress`. `run_job` treated that as a hard
failure: `last_outcome=failed`, a 600 s redrive, and a 500 that also
burns the queue's 5 retries. Most 500s therefore take about 30 s, the
wait budget. #25197 added the `busy` outcome for erasures
(`run_erasure_job`), but batch purges never got it.

## Fix

- `run_job` maps a unit that lost the gate to a same-kind peer
(`_lost_gate_to_peer`, the helper `run_erasure_job` already uses) to the
existing `busy` outcome. That means a 200, the lease released, and a
redrive in `BUSY_RETRY_SECONDS`.
- `busy` is safe because a unit acquires the gate before erasing
anything, so the losing unit erased nothing. The scan position and
segment records were already checkpointed under the run's ownership, and
the next generation resumes from them.
- Privacy invariants from #25124 are unchanged:
- An account wipe, retention or audio-deletion gate is never a peer. It
still fails the run (tested for each kind).
  - A legal hold still ends as `held`.
  - Every erasure is still fenced by its unit's authority.
  - Run and unit deadlines are unchanged.
- A stuck remote-erasure gate is still settled by `settle()` before each
unit, and is still reported by
`event=destructive_gate_remote_unresolved`.
- The handler's failure line now includes the obligation kind,
generation, exception type and `sanitize(str(e))`, so every 500 states
its reason.
- Re-pinned the `run_job` `BODY_DIGEST`. It adds no query: its only
stream is still `segment_records`, and the gate check is the existing
point read.

## Blast radius (read-only)

- 301 distinct obligations failed at least one attempt since 11-12, 202
of them with `DestructiveOperationInProgress`.
- None is stuck: all 301 are gone from `conversation_batch_purge_jobs`
and `conversation_erasure_jobs`, so they completed.
- Success lines look missing for 237 of them only because app stdout
INFO is kept at 10% by the `_Default` sink exclusion
`app-stdout-info-sample90`. 2xx request logs are also kept at 10%
(`cost-audit-cloud-run-success-sample`).
- Live right now: 19 batch-purge and 5 erasure obligations, all from one
account and all under 1 h old. 18 tasks are queued on
`conversation-erasure`.
- The roughly 300 403s a day on this path are the reconciler's
unauthenticated readiness probe
(`conversation_batch_purge_consumer_ready`), which is expected.

## Not in this PR

- `/v2/audio-merge-jobs/run` 500s on 11-15 (171 so far) have a different
cause. All of them are `stamped playback source could not be read`
(`utils/other/storage.py`, strict-source download failure), across 7
conversations, and each fails in under 1 s. They have nothing to do with
the gate.
- The reconciler still dispatches every due batch-purge obligation of
one account at once. A per-account spread like
`MAX_ERASURES_PER_ACCOUNT` would cut how many gate waiters there are.
With this fix those waiters hand off with `busy` instead of failing, so
the spread is a follow-up.

## Tests

-
`test_unit_losing_its_gate_wait_to_a_same_account_peer_hands_off_busy_and_resumes`
reproduces the prod failure. On `main` it raises
`DestructiveOperationInProgress: another destructive operation owns the
account gate`. With the fix it returns `busy`, erases nothing without
the gate, keeps progress, and the next generation purges.
-
`test_unit_blocked_by_another_destructive_kind_still_fails_the_run[account_deletion|retention_cleanup|audio_conversation_deletion]`.
- `test_task_handler_maps_run_outcomes_to_queue_retry` gains a `busy`
case (200). `test_task_handler_logs_why_a_run_failed` checks the new
failure line and that it is sanitized.
- Purge, gate and accept suites pass (111 tests):
`test_conversation_batch_purge_{resumable,deadlines,authority}.py`,
`test_conversation_delete_gate_wait.py`,
`test_conversation_delete_accept.py`. `test_firestore_query_shapes.py`
also passes. `make preflight` was run.

## Product invariants affected

none

## Failure class (fixes)

Failure-Class: FC-user-intent-queued-behind-account-exclusive-work

🤖 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/25393?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/25393?returnTo=https%3A%2F%2Fgithub.com%2FBasedHardware%2Fomi%2Fpull%2F25393&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

mux-proposal Mobile UX improver (MUX) proposal; waits for Nik's ship

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant