Skip to content

fix(batch): pick fresh merge rows inside the commit transaction - #25263

Merged
Git-on-my-level merged 5 commits into
mainfrom
lane/tx-fresh-gate
Nov 14, 2026
Merged

Git-on-my-level merged 5 commits into
mainfrom
lane/tx-fresh-gate

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

What

Select fresh-first rows with two transactional LIMIT 1 queries: earliest pending fresh (F1) and earliest ready pending fresh (FR), including terminal states in F1. Commit FR when it is F1 or its own ready age reaches the head wait. An FR behind F1 without a ready clock leaves the decision to the existing probe. Reuse the existing commit body.

Why

The outside-transaction 32-row probe can miss ready rows behind an unready page or pass an earlier row that settles or is redriven before the transaction.

Risk and cost

Gate attempts add up to two transactional queries on the fresh path. When the gate decides, they replace the outside-transaction 32-row probe and per-skipped-row rechecks; an undetermined gate or missing index still uses that probe. Transactional query contention may increase. Privacy fencing, late marking, chain writes and commit metrics use the existing body.

Known limits

A row that becomes ready after the transaction's snapshot can commit on a later drain when it is first, has a ready time (including the provider settle fallback) old enough to pass the head wait, or is picked by the outside probe. It is marked as a late capture if behind the watermark. This PR does not change owner discovery or release held provider backlog. No production behavior is verified by these local tests.

The migration runner wraps each migration in a transaction, and earlier migrations also use ordinary CREATE INDEX. These indexes are not online-safe for a Postgres job table under write traffic. This checkout selects Firestore in utils/batch_pipeline/serving.py via shadow_from_env(FirestoreStore(...)); BATCH_JOBSTORE_MODE defaults to firestore and supports only a staging Postgres shadow (database/jobstore/dual_store.py). Postgres is not selected as the production primary by this code; the live production configuration is not determined without a network check. Another open PR that adds a migration must take the next number after this one.

fresh_probe_full_no_ready is not emitted on the decided gate path; its count falling to zero is not proof by itself. Production proof is ready fresh rows committing behind more than 32 unready rows (local test: test_ready_fresh_beyond_probe_page_obeys_its_own_wait). No production proof was collected.

Deploy

Declare merge_fresh_ready indexes for prod and dev; F1 uses the existing merge_fresh index. Apply 0006_transactional_fresh_gate.sql before enabling traffic on a Postgres job store. FailedPrecondition restores the probe for 300 seconds with fresh_tx_gate_unavailable and record_fallback. BATCH_PIPELINE_MERGE_TX_FRESH_GATE_ENABLED=false restores the old probe. Index deployment, push and PR creation belong to the lane owner.

Review round 1: fixed missing-clock FR probe fallback; added between-transaction settle/redrive interleavings and query/result-write transaction identity spies for memory, simulator and the Firestore/Postgres conformance matrix; delegated both queries in _OwnWrites to clear pyright; handled transient gate query errors without backoff while preserving commit-write errors; documented the omitted probe-full log and conditional later-drain behavior; renamed migration 0007 and documented index locking and store selection; corrected settle-clock wording in the earlier note and merge comments.

Review round 2 (on b20fec7, FIX, all three fixed): with the head wait off the gate no longer lets a ready fresh row pass an unready earlier one (main requires a positive wait); an Aborted from a gate query is re-raised to the transaction retry or the caller instead of being handled as a gate retry; the transaction-identity spies now cover both selection queries.

CI round on 6b481fe: one failure, tests/unit/test_lifecycle_state_table_writers.py (backend unit shard 5/8): the gate builds a skipped-row note that reads the first row's state, and that payload site was not declared in tests/support/lifecycle_writer_inventory.py. Declared here (test inventory only; no product code change). The pre-push suite does not run that file.

Tests

Test file (one pytest process per file) Result
tests/unit/test_batch_pipeline_merge_fresh_first.py 59 passed
tests/unit/test_batch_pipeline_merge_head_wait.py 177 passed
tests/unit/test_batch_pipeline_merge_head_wait_sim.py 3 passed
tests/unit/test_batch_pipeline_merge_tx_fresh_gate.py 24 passed
tests/unit/test_batch_provider_merge_membership.py 46 passed
tests/unit/test_batch_provider_stt.py 158 passed
tests/unit/test_batch_pipeline_pump.py 36 passed
tests/unit/test_batch_pipeline_serving.py 60 passed
tests/unit/test_flag_budget.py 24 passed
tests/unit/test_batch_pipeline_scheduler.py 62 passed
tests/unit/test_jobstore_protocol_parity.py 80 passed
tests/unit/test_jobstore_migrations.py 7 passed
tests/unit/test_jobstore_postgres_locking.py 10 passed
tests/unit/test_firestore_index_config.py 12 passed
tests/unit/test_firestore_query_contract.py 50 passed
tests/unit/test_pipeline_sim.py 21 passed
tests/unit/test_pipeline_sim_scenarios.py 11 passed
tests/unit/test_pipeline_sim_policy_invariants.py 5 passed
tests/unit/test_batch_pipeline_recovered_release.py 13 passed
tests/integration/test_store_conformance.py 77 passed; 166 skipped
tests/unit/test_firestore_outside_query_contract.py 31 passed
tests/unit/test_firestore_caller_witnesses.py 40 passed
tests/unit/test_firestore_runtime_witnesses.py 25 passed
tests/unit/test_firestore_index_rules.py 96 passed
tests/unit/test_firestore_field_indexes.py 41 passed
tests/unit/test_firestore_query_shapes.py 579 passed

Review-round reruns, one pytest process per file: round 1 fix, all 26 listed files passed except test_jobstore_migrations.py (6 passed, 1 failed) while the migration was numbered 0007; back at 0006 it passes 7 of 7. Round 2 fix: test_batch_pipeline_merge_tx_fresh_gate.py 24 passed, test_batch_pipeline_merge_fresh_first.py 59, test_batch_pipeline_merge_head_wait.py 177, tests/integration/test_store_conformance.py 77 passed and 166 skipped (no local Firestore or Postgres).

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

🤖 Generated with Claude Code

Git-on-my-level and others added 4 commits November 14, 2026 11:16
Use two bounded transactional fresh queries with a default-on kill switch and the existing probe fallback during index unavailability. Add store parity, index declarations, regression coverage and the change note.

Failure-Class: FC-backlog-release-shares-lane-with-fresh-work
Migrations must be contiguous on main; the owner cursor PR stacks on this
one and takes 0007.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…n is re-raised

Review of b20fec7: with wait <= 0 the gate let a ready fresh row pass an
unready earlier one; an Aborted from a gate query was handled as a gate
retry when contention retries are off; the identity spies covered only
one of the two selection queries.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…le inventory

CI on 6b481fe failed test_lifecycle_state_table_writers: the gate's
skipped note reads the first row's state. Inventory only.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@Git-on-my-level
Git-on-my-level merged commit f1975d2 into main Nov 14, 2026
81 of 83 checks passed
@Git-on-my-level
Git-on-my-level deleted the lane/tx-fresh-gate branch November 14, 2026 05:55
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