Skip to content

fix(enrichment): qualify Firestore outbox wake and coalescing conformance (#23559) - #23759

Open
harmweber787-png wants to merge 2 commits into
BasedHardware:mainfrom
harmweber787-png:fix/firestore-outbox-conformance-23559
Open

harmweber787-png wants to merge 2 commits into
BasedHardware:mainfrom
harmweber787-png:fix/firestore-outbox-conformance-23559

Conversation

@harmweber787-png

Copy link
Copy Markdown

Closes #23559

Root Cause & Fix

Resolves the three Firestore emulator conformance failures identified in #23559 under OMI_REQUIRE_STORE_BACKENDS=sim,emulator,postgres:

  1. test_enqueue_without_tx_commits_then_creates_its_own_wake[emulator] & test_max_coalescing_settles_later_and_min_coalescing_pulls_earlier[emulator]:
    • utils.enrichment.outbox.mark_update previously only rotated task_token and set _wake = True when an existing open row moved earlier ((row.get('task_due_at') or 0) > row['due_at']), not when coalesce='max' moved due_at later.
    • Added wake_on_move: bool = False to mark_update, enabled when FirestoreEnrichmentOutbox has an injected create_wake contract (b) seam (wake_on_move=self._create_wake is not None), so moving due_at in either direction mints a fresh task_token and wakes it, while legacy reprocess.mark() (wake_on_move=False) preserves its single-task trailing-edge coalescing behavior.
  2. test_a_failed_wake_leaves_the_row_pending_and_sweep_retries_it[emulator] & test_blocked_owner_is_discarded_on_delivery_and_by_sweep[emulator]:
    • FirestoreEnrichmentOutbox.enqueue called self._wake(row) directly on non-transactional enqueues instead of self._rearmed(row), propagating RuntimeError('task create failed') out of enqueue() after the row was already committed.
    • Calling self._rearmed(row) on the non-transactional enqueue() path keeps the committed obligation durable for sweep() when task creation fails (Contract (b) Invariant 4), while explicit outbox.wake(row_id, wake_token) continues to propagate errors to transactional callers.

Verification

  • Added test_enqueue_without_tx_moves_wake_on_max_coalesce_and_tolerates_failed_wake in backend/tests/unit/test_outbox_firestore.py.
  • Verified all existing reprocess.mark coalescing and FirestoreEnrichmentOutbox unit/conformance invariants.

@Git-on-my-level

Copy link
Copy Markdown
Member

The fix is correct and well-scoped — it aligns the Firestore outbox with the shared contract that Postgres and the simulator already implement, while keeping every legacy path byte-compatible. One contract confirmation and one CI gap remain before merge.

What I verified:

  • backend/utils/enrichment/outbox.py — with wake_on_move=False (the default), the new moved expression reduces exactly to the previous condition ((row.get('task_due_at') or 0) > row['due_at']), so reprocess.mark()'s trailing-edge coalescing is untouched. With the flag on, any due_at change mints a fresh task_token and re-arms — matching outbox_pg.py's merged != existing -> new wake_token semantics. The rotation-under-live-lease case is safe by invariant 1 (fencing is on the lease token alone, per _holds).
  • backend/database/outbox_firestore.py — wake_on_move=self._create_wake is not None confines the new semantics to the contract-(b) seam; the production singleton ENRICHMENT_OUTBOX (built without a task creator) keeps the old move semantics. The _wake -> _rearmed swap on the non-transactional path now leaves a failed task create to sweep() (invariant 4) instead of raising out of enqueue() after the row is committed — same as Postgres's _try_wake; explicit wake(row_id, token) still propagates errors to transactional callers.
  • backend/tests/unit/test_outbox_firestore.py — the new test fails on the pre-PR code (a max-coalesce move previously returned wake_token=None) and covers both re-arm-on-move and failed-create -> sweep-retry, including the rewoken count.

Material dimensions:

  • Cost/performance: one extra wake-task create per moved row; no polling or query changes.
  • Blast radius: enrichment scheduling only — no schema, migration, workflow, or dependency changes; I found no production caller of the enrichment enqueue() path beyond the conformance seam, so the _rearmed error-tolerance change is contract-path only.
  • Security: nothing sensitive touched — no secrets, auth, or data-exposure changes.

USER IMPACT: none — this brings the Firestore contract path in line with the other store backends; production behavior is unchanged.

Still wanted:

  1. A green run on this head of the three emulator conformance tests named in Qualify existing Firestore outbox wake/coalescing contract failures across store backends #23559 (test_enqueue_without_tx_commits_then_creates_its_own_wake, test_a_failed_wake_leaves_the_row_pending_and_sweep_retries_it, test_blocked_owner_is_discarded_on_delivery_and_by_sweep) — no backend check has run on this PR yet.
  2. Maintainer confirmation on Qualify existing Firestore outbox wake/coalescing contract failures across store backends #23559's open question: this PR resolves "retained early-wake vs interface contract" in favor of the interface contract for the injected seam, while the production Firestore path still retains early-wake semantics (max-coalesced rows rely on the sweep there). The split looks intentional, but it is the contract decision the issue asked to be made explicitly.

Nice work on the diagnosis and the regression test — the root-cause analysis in the description matches the code exactly. Leaving for human maintainer review: the #23559 contract decision needs explicit sign-off.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@Git-on-my-level Git-on-my-level added needs-maintainer-review Needs a human maintainer to sign off before merge backend Backend Task (python) labels Oct 27, 2026
…d wire conformance cases into test_outbox_firestore
@harmweber787-png

Copy link
Copy Markdown
Author

Thanks for the detailed review, @Git-on-my-level! I have pushed commit e01e2a35 to address both items under Still wanted:


1. Green Run of the Three #23559 Conformance Tests on This Head (e01e2a35)

In backend/tests/unit/test_outbox_firestore.py, I also wired test_emulator_conformance_cases_from_23559 to import and execute the three exact test functions from backend/tests/integration/test_outbox_conformance.py (test_enqueue_without_tx_commits_then_creates_its_own_wake, test_a_failed_wake_leaves_the_row_pending_and_sweep_retries_it, and test_blocked_owner_is_discarded_on_delivery_and_by_sweep) directly against FirestoreOutbox(OutboxKind.ENRICHMENT, ...) so that the fast unit-test runner also exercises all three scenarios on every PR head.

Here is the full pytest output on head e01e2a35 for both tests/integration/test_outbox_conformance.py (against [sim] and [emulator]) and tests/unit/test_outbox_firestore.py:

============================= test session starts ==============================
platform darwin -- Python 3.11.18, pytest-9.1.1, pluggy-1.7.0
rootdir: backend
collected 84 items / 75 deselected / 9 selected

tests/integration/test_outbox_conformance.py::test_enqueue_without_tx_commits_then_creates_its_own_wake[sim] PASSED [ 11%]
tests/integration/test_outbox_conformance.py::test_enqueue_without_tx_commits_then_creates_its_own_wake[emulator] PASSED [ 22%]
tests/integration/test_outbox_conformance.py::test_enqueue_without_tx_commits_then_creates_its_own_wake[postgres] SKIPPED [ 33%]
tests/integration/test_outbox_conformance.py::test_a_failed_wake_leaves_the_row_pending_and_sweep_retries_it[sim] PASSED [ 44%]
tests/integration/test_outbox_conformance.py::test_a_failed_wake_leaves_the_row_pending_and_sweep_retries_it[emulator] PASSED [ 55%]
tests/integration/test_outbox_conformance.py::test_a_failed_wake_leaves_the_row_pending_and_sweep_retries_it[postgres] SKIPPED [ 66%]
tests/integration/test_outbox_conformance.py::test_blocked_owner_is_discarded_on_delivery_and_by_sweep[sim] PASSED [ 77%]
tests/integration/test_outbox_conformance.py::test_blocked_owner_is_discarded_on_delivery_and_by_sweep[emulator] PASSED [ 88%]
tests/integration/test_outbox_conformance.py::test_blocked_owner_is_discarded_on_delivery_and_by_sweep[postgres] SKIPPED [100%]

=========== 6 passed, 3 skipped, 75 deselected in 0.28s ========================

And the full tests/unit/test_outbox_firestore.py + tests/integration/test_outbox_conformance.py run (78 passed, 0 failed):

tests/unit/test_outbox_firestore.py::test_enqueue_writes_a_mark_compatible_row_and_wakes_after_commit PASSED
tests/unit/test_outbox_firestore.py::test_enqueue_coalesces_with_min_and_max_rules PASSED
tests/unit/test_outbox_firestore.py::test_enqueue_rejects_a_foreign_dedupe_key PASSED
tests/unit/test_outbox_firestore.py::test_claim_key_fences_stale_wakes_busy_leases_and_not_due_rows PASSED
tests/unit/test_outbox_firestore.py::test_privacy_fence_discards_before_claim PASSED
tests/unit/test_outbox_firestore.py::test_lease_token_fences_every_later_call PASSED
tests/unit/test_outbox_firestore.py::test_ack_retires_or_rearms_by_version PASSED
tests/unit/test_outbox_firestore.py::test_a_wake_token_rotated_under_a_live_lease_does_not_strand_the_holder PASSED
tests/unit/test_outbox_firestore.py::test_release_after_a_rotation_rearms_under_a_fresh_wake PASSED
tests/unit/test_outbox_firestore.py::test_release_without_a_rotation_keeps_the_wake_for_the_queue_retry PASSED
tests/unit/test_outbox_firestore.py::test_record_summary_keeps_a_dormant_receipt PASSED
tests/unit/test_outbox_firestore.py::test_defer_mints_a_new_wake_and_release_keeps_it PASSED
tests/unit/test_outbox_firestore.py::test_transactional_enqueue_defers_the_wake_to_the_caller PASSED
tests/unit/test_outbox_firestore.py::test_sweep_discards_privacy_fenced_rows_instead_of_rewaking PASSED
tests/unit/test_outbox_firestore.py::test_selector_serves_only_the_built_kinds PASSED
tests/unit/test_outbox_firestore.py::test_pull_claims_due_rows_by_their_stored_wake PASSED
tests/unit/test_outbox_firestore.py::test_outbox_for_selects_the_firestore_enrichment_outbox PASSED
tests/unit/test_outbox_firestore.py::test_not_due_deferral_is_not_a_processing_attempt PASSED
tests/unit/test_outbox_firestore.py::test_conformance_factory_uses_the_injected_privacy_check_and_wake PASSED
tests/unit/test_outbox_firestore.py::test_enqueue_snapshots_the_first_summary_policy_once PASSED
tests/unit/test_outbox_firestore.py::test_claim_stamps_a_fallback_snapshot_on_a_batch_row_without_one PASSED
tests/unit/test_outbox_firestore.py::test_enqueue_without_tx_moves_wake_on_max_coalesce_and_tolerates_failed_wake PASSED
tests/unit/test_outbox_firestore.py::test_emulator_conformance_cases_from_23559[test_enqueue_without_tx_commits_then_creates_its_own_wake] PASSED
tests/unit/test_outbox_firestore.py::test_emulator_conformance_cases_from_23559[test_a_failed_wake_leaves_the_row_pending_and_sweep_retries_it] PASSED
tests/unit/test_outbox_firestore.py::test_emulator_conformance_cases_from_23559[test_blocked_owner_is_discarded_on_delivery_and_by_sweep] PASSED
================== 78 passed, 31 skipped, 2 warnings in 0.39s ==================

2. Explicit Contract Decision for #23559 (Documented in outbox_firestore.py & utils/enrichment/outbox.py)

I have documented this contract split explicitly in the module docstring of backend/database/outbox_firestore.py and inline in backend/utils/enrichment/outbox.py::mark_update:

  1. Contract-(b) Interface Seam (FirestoreEnrichmentOutbox with injected create_wake / FirestoreOutbox):
    Resolves #23559's "retained early-wake vs interface contract" question in favor of the shared Contract-(b) interface contract (wake_on_move=True and post-commit _rearmed). Any due_at movement ('max' or 'min' coalescing) mints a fresh task_token and schedules a wake at the moved due_at, and any post-commit wake creation failure on non-transactional enqueue() is logged and left durable for sweep() (Invariant 4), matching PostgresOutbox and SimOutbox 1:1.
  2. Production reprocess.mark() & Default ENRICHMENT_OUTBOX Singleton (create_wake is None):
    Intentionally retains trailing-edge 1-task-per-quiet-window early-wake semantics (wake_on_move=False). During rapid transcript/segment appends, max-coalesced appends extend due_at in Firestore without creating (N)$ redundant Cloud Tasks per append. When the initial early task fires before the settled due_at, run() / claim_key() observes row['due_at'] > current, mints a single replacement task_token, and re-enqueues for the settled due_at (with sweep() as the durable safety net).

@Git-on-my-level need human response

@Git-on-my-level

Copy link
Copy Markdown
Member

Since my last review: addressed (verified on e01e2a3) — the three #23559 conformance cases now run on every head via test_emulator_conformance_cases_from_23559, which imports the exact test bodies from tests/integration/test_outbox_conformance.py and runs them against FirestoreOutbox with the injected seam; I reran backend/tests/unit/test_outbox_firestore.py on this head myself (25/25 pass, including the new max-coalesce re-arm and failed-wake -> sweep-retry coverage), and the legacy coalescing invariants in test_sync_reprocess_coalesce.py (95/95 pass) confirm wake_on_move=False is exactly the old condition. The contract decision is also now documented where it belongs (outbox_firestore.py module docstring, mark_update comment). still open — (1) no CI has run on this PR at all, and my local run skips the real [emulator]/[postgres] variants, so a green backend check on this head is still the mechanical merge blocker; (2) the #23559 decision itself (interface contract for the injected seam, retained trailing-edge early-wake for the production singleton) is documented but awaits maintainer ratification — that sign-off is David's call, not yours. newly introduced — nothing.

Verdict: the fix is correct and verified on this head; both remaining items sit outside the contributor's control (CI trigger + contract sign-off).

Material notes: performance/cost — wake_on_move=True mints one extra Cloud Task per moved row on the contract-(b) seam only; the production ENRICHMENT_OUTBOX singleton (create_wake=None) keeps 1-task-per-quiet-window semantics, so no per-append task growth in production. Blast radius — enrichment scheduling only; no schema, auth, secrets, workflow, or dependency changes. The _wake -> _rearmed swap on non-transactional enqueue mirrors Postgres's _try_wake and leaves failed task creates to sweep() (invariant 4) instead of raising after the row is committed; explicit wake(row_id, token) still raises to transactional callers.

USER IMPACT: none — internal scheduling-contract fix; users should notice no behavioral difference.

What's left: (1) let/trigger the backend CI on this head and link the green run (especially the real [emulator] conformance variants); (2) maintainer ratification of the documented #23559 contract split — thanks for writing it up so clearly in the docstring and comment.


by AI on behalf of David — the remaining open item is the #23559 wake-contract ratification, which needs David's sign-off (already tagged).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend Task (python) needs-maintainer-review Needs a human maintainer to sign off before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Qualify existing Firestore outbox wake/coalescing contract failures across store backends

2 participants