Skip to content

fix(dream): harden gate retries and minute headroom - #22835

Merged
Git-on-my-level merged 2 commits into
mainfrom
lane/dream-gate-hardening
Oct 20, 2026
Merged

Git-on-my-level merged 2 commits into
mainfrom
lane/dream-gate-hardening

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes four bugs that the 2026-10-18 post-merge review found in the Dream gate-retry (#21966) and minute-ceiling (#21933) work. I re-checked each against origin/main on 2026-10-20 before fixing it.

  • F1 — a timeout releases the lease while an apply is still running. Before this change, a TimeoutError during a gate-retried executor apply was rewritten to dream_gate_contention. That released the owner lease and the capacity slot, but the executor thread could still commit. A new pass could then be admitted on top of it. Now only a timeout during the retry sleep is rewritten (gate_retry_sleeping). A timeout during the apply or during the retry-allowed read stays a TimeoutError, which keeps both fences.
  • F2 — vectors are deleted, then the retry reports applied. sync_transcript_chunks deleted chunk vectors with no fence before the fenced upsert ran, so a held gate could delete vectors it then couldn't rewrite. The gate retry then short-circuited to 'applied' from the committed journal. Now delete_transcript_chunk_vectors is fenced (@_account_external_data_write, the same fence as the upsert). The journal short-circuit also re-runs the projection for transcript overlays before returning 'applied'. A non-gate repair failure (embed or upsert) is logged, not raised: the change is already committed, so the repair is best-effort post-commit index maintenance.
  • F3 — a person-merge retry fails after the first attempt created graph nodes. The first attempt created a person: knowledge node and fenced it as None, so the retry failed as invalid_evidence. Now the retry accepts that node in place of its None fence only if it exactly matches the node this flow creates for the current person record. Any other difference still fails closed.
  • F4 — the minute hold isn't a hard ceiling. The pre-call hold discounted the previous turn's cache hit, so a cache miss pushed usage past the shared minute quota. Now the hold assumes no cache hit; settlement still posts the actual price-weighted burndown. The largest possible turn holds about 1.21M tokens, under the 2M default. A full minute still pauses rather than discards.

Not in scope: F5, which became moot when #22209 removed the Jev shadow; F6, which is informational; and F7, where the report found no defect.

Review

Grok (grok-4.7-high, read-only) reviewed this and returned SHIP-WITH-NITS. I fixed its two nits in the second commit:

  • repair failure on the retry path no longer fails the pass;
  • the sleep flag is cleared on any exception from the backoff sleep except a pass timeout.

A third nit I accepted as it stands: if a person is renamed during the backoff window, the retried merge fails closed. Nothing is wrongly merged, and the leftover node stays undo-safe.

Grok checked that each new regression test would fail on origin/main.

Verification

  • backend/test.sh with BACKEND_UNIT_TEST_FILE_LIST, under nice -n 19 taskpolicy -b with 2 workers, on the final head:
    • test_dream_minute_headroom.py: 6 passed
    • test_dream_review_round_one.py: 19 passed
    • test_dream_cleanup_edits.py: 45 passed
    • test_dream_harness_runtime.py: 22 passed
  • Before the review fixes, Sol's run also covered the transcript overlay (13), vector search service (23) and conversation relevance (25) tests. All passed.
  • These are unit-level results only. The live check comes after deploy: Dream passes for the allowlisted owner on backend-sync (utils.dream_metrics:Dream status=) keep completing.

Product invariants affected

none

Failure class (fixes)

Failure-Class: FC-post-commit-index-maintenance-terminal

🤖 Generated with Claude Code

View guided diff Turn on auto-fix

Git-on-my-level and others added 2 commits October 20, 2026 16:17
Retain owner and capacity fences when a retried executor apply times out. Fence transcript chunk deletion and rebuild committed corrections on gate retry. Accept only exact canonical person bootstraps when retrying a merge. Reserve uncached minute burndown before every model call.
A non-gate embed or upsert failure while rebuilding transcript chunks on a
gate retry is logged instead of failing the pass. The gate-retry sleep flag
is cleared on any non-timeout exception from the backoff sleep.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-20T10:54:55.164203Z aa3fa80 PR opened
ℹ️ 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.

@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: aa3fa80a13

ℹ️ 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 +1330 to 1331
@_account_external_data_write
def delete_transcript_chunk_vectors(uid: str, conversation_id: str) -> None:

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 Fence conversation deletion before removing its Firestore data

When Pinecone is configured and another same-account destructive-operation gate is active, this decorator raises DestructiveOperationInProgress before the delete function runs. The user-facing delete flow has already durably removed the conversation at backend/routers/conversations.py:1396 before calling this function at line 1399, and it does not catch this exception, so the request returns a 500 after deleting the primary record while leaving its transcript vectors behind. The gate check needs to occur before the durable conversation deletion (or the entire delete must participate in the gate) rather than being introduced only at this final cleanup step.

Useful? React with 👍 / 👎.

@Git-on-my-level
Git-on-my-level merged commit 5098bbc into main Oct 20, 2026
63 checks passed
@Git-on-my-level
Git-on-my-level deleted the lane/dream-gate-hardening branch October 20, 2026 11:06
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