Skip to content

fix(batch): hold the provider backlog; owner wakes commit fresh provider rows only - #25204

Merged
Git-on-my-level merged 6 commits into
mainfrom
lane/provider-wake-fresh-only
Nov 13, 2026
Merged

Git-on-my-level merged 6 commits into
mainfrom
lane/provider-wake-fresh-only

Conversation

@Git-on-my-level

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

Copy link
Copy Markdown
Member

What

Follow-up to #25187. While a new switch is off (the default), the ordered merge does not commit provider-route catch-up rows. An owner's fresh provider rows still commit, ahead of that held backlog, whenever the fresh-row query is available (see the known limits for when it is not).

Why

#25187 reached backend-integration on 2026-11-13 at 11:36Z. Measured in production (job and result metadata, request logs):

  • Active provider owners began draining their own already-transcribed backlog: about 230,000 rows an hour left result_ready (170,000 at catch-up depth 4, 125,000 at depth 1).
  • Projection takes owners from one FIFO queue with no class for fresh work. Of results committed 10–20 minutes earlier, 35% were still unprojected, and 45% of those captured under an hour before. It was about 0.2% before the deploy. That delay applied to every user, not only provider-route users.
  • The owner drains run in the serving process. backend-integration was at its instance ceiling, and completed enrichment and provider-STT requests fell by about half until the ceiling was raised from 35 to 70.
  • What the fix was meant to do did work: of provider units settled 3–8 minutes earlier, 53% were committed at 12:35Z (0% before, 3% while admission stamps were missing).

So the fault is the unmetered release of the backlog, not provider merge membership. This PR removes the release from the owner wake and leaves delivery of fresh units in place.

Change

BATCH_PIPELINE_PROVIDER_BACKLOG_RELEASE_ENABLED, off unless true. While off, in utils/batch_pipeline/merge.py:

  • A provider-route row that is not stamped fresh (fresh_capture absent, or false) is not committed. It stays result_ready with merge_pending. This holds whatever BATCH_PIPELINE_PROVIDER_MERGE_MEMBERSHIP_ENABLED is set to: on 2026-11-13 at 13:00Z, 78 minutes after that switch was set to false, 3,435 provider rows captured over a day earlier were still committed in a 2-minute window, 2,925 of them through the head wait, because rows stamped ready_at while it was on stay eligible afterwards. Unprojected results rose from 78,204 (12:12Z) to 117,076 (12:56Z).
  • The fresh-first probe still runs for that owner, so its fresh rows commit ahead of the held rows. It does not run while the fresh query is backing off after FailedPrecondition; fresh rows behind a held row then wait for the query to work again. The probe now also runs when the catch-up depth hold is switched off (depth 0), both when the held row heads the owner and when an unready provider backlog row does (there the head wait alone would pick a held row and commit nothing).
  • While the fresh query is backing off after FailedPrecondition, a held owner is not probed and its backlog stays held. Before review this path recursed without bound.
  • The hold writes no merge_holds marker and adds no owner to the in-memory re-offer set, including when the owner is at the depth cap and whether the held head is ready or not, so maintenance does not re-drain held owners every pass.
  • A recovered provider row that is not fresh is held as well: the recovery wake does not pass the backlog hold. GPU-route rows, recovered or not, are unchanged.

Details: backend/docs/listen_pusher_pipeline/changes/2026-11-13-provider-backlog-hold.md.

What is not delivered while the switch is off

  • The backlog transcribed since 5 Nov (about 1.59 million rows at 12:35Z).
  • New provider uploads that arrive more than 10 minutes after capture (FRESH_CAPTURE_LAG_SECONDS): 13% of 1,820 provider admissions sampled on 2026-11-13. These were not being delivered before today either.

Known limit: when an owner's oldest pending row is a GPU-route row that is not ready, a fresh provider row behind a held provider row waits for that head, because the fresh probe only runs behind a catch-up head. Provider owners are almost entirely single-route, and this is the behaviour GPU rows already have.

Known limit: a GPU-route catch-up row (an old upload) that sits behind a held provider row of the same owner waits with it, because the merge takes an owner's rows in capture order and cannot select past the held row. Fresh rows of either route still commit ahead of the held row through the fresh probe. The exception is while the fresh query is unavailable (FailedPrecondition, a missing index): the probe backs off, and a fresh row behind a ready held provider row waits until the query works again. It ends with the metered release.

Rollout and the default-off switch

This is a default-off gate, so here is its plan. It is not a per-user ramp; it guards one stock of rows.

  • Ships off. backend-integration then turns BATCH_PIPELINE_PROVIDER_MERGE_MEMBERSHIP_ENABLED back on (it was set to false after the measurements above).
  • The metric that must stay clean when that is turned on: share of results committed 10–20 minutes earlier that are still unprojected, under 2%; and completed enrichment requests per minute on backend-integration, flat.
  • The backlog is released by a metered job with its own rate setting, after the projection owner queue gives fresh owners their own class. Owner: the provider-STT drain owner (anx card:drain-stuck-provider-stt-backlog-for). Date: before 2026-12-05, when the oldest staged results reach the bucket's 30-day lifecycle. The switch is removed with that job.
  • Turning this switch to true is the old behaviour (release paced only by the depth hold) and should not be used without projection headroom.

Rollback of this PR's effect: BATCH_PIPELINE_PROVIDER_BACKLOG_RELEASE_ENABLED=true.

BATCH_PIPELINE_PROVIDER_MERGE_MEMBERSHIP_ENABLED=false now stops fresh delivery too (no new row is stamped fresh, and unstamped rows are held), without releasing anything.

Tuning disclosure

No value changes and no new knob. No new pipeline state: a held row keeps state=result_ready, merge_pending=true. Its exit is a commit, when the switch is on or the release job offers it, or the existing terminal paths.

Product invariants affected

none

Failure class (fixes)

Failure-Class: new

Proposed: FC-backlog-release-shares-lane-with-fresh-work. A fix that made stuck work eligible released all of it at once into stages (projection owner queue, serving CPU) that fresh work also depends on, with no class and no rate of its own.

Tests

cd backend
.venv/bin/python -m pytest tests/unit/test_batch_provider_merge_membership.py tests/unit/test_batch_provider_stt.py \
  tests/unit/test_batch_pipeline_merge_fresh_first.py tests/unit/test_batch_pipeline_merge_head_wait.py \
  tests/unit/test_batch_pipeline_recovered_release.py tests/unit/test_pipeline_characterization.py -q

New in tests/unit/test_batch_provider_merge_membership.py: by default only fresh rows commit and the backlog stays ready, with no hold marker and no re-offer, however far projection advances; opting in releases it under the depth hold; fresh rows pass a held backlog with the depth hold off; a provider row stamped not-fresh is held too; a ready backlog row does not pass an unready head; GPU catch-up rows are not affected. Added after review: the membership switch off still holds the backlog; a fresh row passes an unready backlog head at depth 0; an unavailable fresh query is asked once and the backlog stays held; a held head at the depth cap writes no marker. Each of the four fails without the fix.

tests/unit/test_flag_budget.py failed in CI on b1165bd (the new name was missing from the budget count and the helper-switch list); fixed.

The two provider golden traces (provider_stt_ja, provider_stt_unsupported) admit their uploads after the fresh lag, so their config now sets the switch on and the traces are unchanged: they keep pinning the commit and projection path.

Review

Independent review (Luna), round 1 on b1165bd: FIX, two blockers (no fresh probe at depth 0 behind an unready provider head; unbounded recursion when the fresh query is unavailable) and one should-fix (a depth marker written for a held head at the cap). All three are fixed with a test each. The reviewer found no privacy-fence bypass and confirmed the other commit routes use the guarded path.

Round 2 on bfa768a: FIX. Round-1 findings confirmed closed; no privacy-fence bypass. Two blockers and two should-fix:

  1. Blocker, fixed: a recovered non-fresh provider row passed the hold through the recovery wake. It is now held; test in tests/unit/test_batch_pipeline_recovered_release.py.
  2. Should-fix, fixed: an unready held provider head at the depth cap still wrote a marker. Test added.
  3. Blocker, left open as existing production state: the maintenance page (due('result_ready')) takes the 100 oldest result_ready rows with no cursor, so rows that never commit pin it. That page is already pinned: on 2026-11-13 at 13:35Z its 100 rows were all 9.1 to 10.1 days old (71 GPU-route, 29 provider). This PR adds more rows that stay result_ready, behind those. Rows that can commit are reached by their owner wake and the per-pump drains, as today. One class is not: a GPU-route catch-up row behind a held provider row of the same owner (the known limit above) is reached by its wake, commits nothing, and has no marker or page to bring it back; it waits for the release like the provider row in front of it. The fix is a cursor on that page plus the metered release.
  4. Should-fix, fixed after the head confirmation: a probe made only for the backlog hold now backs off on its own clock, so its failure no longer switches the depth hold off for the merge instance. Test added.

Head confirmation on 2e675fc: FIX. It confirmed 1 and 2 closed, no privacy-fence bypass, and no change for owners without provider rows. It found my account of items 3 and 4 inaccurate; both are corrected above (the GPU catch-up case is stated, item 4 is now fixed in code). It also raised custody: held results are GCS artifacts under the 30-day staging lifecycle, and this PR adds no enforcement of the release deadline. That is accurate and stays open by design: these rows have been undelivered since 2026-11-05 with or without this PR, releasing them unmetered is what delayed every user's results on 2026-11-13, and the metered release has an owner and a date (before 2026-12-05, see Rollout). If that job is late, the oldest held results become unprojectable.

Narrow confirmation on b1cbfad: item 4 confirmed closed, recursion terminates on every path, no new data-loss or privacy-fence risk, and the account of the two open items matches the code. Its verdict was FIX for one sentence in this description ("Fresh rows of either route are not affected"), which lacked the query-availability exception; the sentence is corrected under the known limits. No code change followed, so the head is unchanged.

Rulings by the rollout owner session (goal-owners rollout, R14 self-drive, 2026-11-13), both accepted for this PR:

  1. The maintenance page without a cursor is existing behaviour and not a blocker. The added cost, an old GPU-route upload waiting behind a held provider row of the same owner, is acceptable; the cursor goes in the metered-release PR.
  2. No enforced release deadline for held provider results is accepted on condition that the metered release has an owner and a date: the provider-STT drain owner, 2026-11-28, recorded on anx card:drain-stuck-provider-stt-backlog-for with what is lost if it slips.

Not tested here: production behaviour. The acceptance read after re-enabling is the share of fresh provider units settled 3–8 minutes earlier that are committed, and the two must-stay-clean metrics above.

🤖 Generated with Claude Code

Git-on-my-level and others added 6 commits November 13, 2026 19:44
…ixes

- hold applies whatever the membership switch says (rows it stamped kept
  committing through the head wait after it was turned off)
- fresh probe behind an unready provider backlog head at depth 0
- bounded fallback when the fresh query is unavailable
- no depth marker for a held backlog head at the cap
- flag budget count and helper-switch list

Failure-Class: new

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…ready held head

Round-2 review: a recovered non-fresh provider row passed the hold through the
recovery wake, and an unready held provider head at the depth cap still wrote a
depth marker.

Failure-Class: new

Co-Authored-By: Claude Fable 5.2 <[email protected]>
A probe made only for the backlog hold no longer switches the depth hold off
for the whole merge instance when the fresh query is unavailable.

Failure-Class: new

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@Git-on-my-level
Git-on-my-level merged commit f3902ec into main Nov 13, 2026
83 of 85 checks passed
@Git-on-my-level
Git-on-my-level deleted the lane/provider-wake-fresh-only branch November 13, 2026 14:21
Git-on-my-level added a commit that referenced this pull request Nov 13, 2026
…owners yield (#25213)

## What

Two changes so a fresh result is projected promptly while a projection
backlog drains:

1. Discovery: each assignment discovery tick also reads up to half a
page from the newest 120 seconds of commits, so a new result is usually
seen within its first two minutes however long the backlog (an estimate,
below; not a per-row guarantee).
2. Owner drain: newly requested owners get a turn before backlog owners.

## Why

On 2026-11-13 about 250,000 old results were committed in three hours
(the provider backlog release, stopped by #25204). Projection drains
owners from one FIFO queue, 8 at a time per process, and a turn runs up
to 120 seconds. Measured in production (result metadata and log counts
only):

- Sample of 20,000 pending results at 15:34Z: 74% captured over a day
earlier; 651 owners; the top 100 hold 82%.
- Of results committed 10–20 minutes earlier, 56% were unprojected
(about 0.2% before the incident); 26% of those committed 60–70 minutes
earlier.
- Half of the waiting under-1h rows (1,290 of 2,587) belong to 198
owners with no older rows of their own. They wait only for other owners'
turns.
- Projection is about 1,000 rows a minute against about 575 a minute of
normal inflow at that hour, so the backlog needs hours to drain. Fresh
results should not be late for all of it.

- Discovery pages pending results oldest first, 100 rows per 20-second
tick per process, and a GPU-route commit has no other wake (the worker
pump has no commit callback). With 154,000 pending rows, 35 processes
pass a given row about once in 15 minutes. That matches the measured
delay.
- Owner-drain turn lines, 5-minute sample at about 15:45Z: turns that
ran to the 120-second deadline were 51 of 1,140 turns but 58% of slot
time, and advanced about 0.43 rows a second each.

## Change

**Discovery** (`backend/utils/batch_pipeline/projection_discovery.py`,
assignment discovery only). Each tick also reads up to half a page from
the newest `BATCH_PIPELINE_PROJECTION_RECENT_DISCOVERY_SECONDS` (default
120) of commits.

- No cursor (the offer memory below is the only state): the read starts
at a random point in the window each tick and reads oldest first.
Replicas draw their start points independently, so they can land on
different rows (nothing forces them to); a restarted process behaves
like any other.
- A process offers a row from this read once. The entry is kept for two
windows, longer than the row stays in the window. A row still pending
after that is offered by the main cursor, as before.
- Rows read here count against the page, so the page holds no more rows;
the main cursor gets the rest (at least half). The tick makes one more
Firestore query than before (two instead of one for the pending page).
- A main cursor that has not reached the newest row for 30 minutes
(`MAIN_PASS_SECONDS`) starts again: one time in eight at the oldest
pending row, otherwise at a uniform random point between it and now.
This closes a gap review found: when rows arrive as fast as a cursor
reads, no cursor wraps, and rows before every process's start point are
never paged. The gap exists on `main` too, at twice the arrival rate.
- Same query and index as the main cursor, with a start point. A failed
recent query costs only that half page; rows read before a failed main
query are not recorded as offered, so they stay eligible for a later
recent read (which starts at a new random point and may not reach them
on the next tick).

Expected coverage, from the numbers above and not measured: with about
170–330 rows committed per 20-second tick and 35 processes each reading
50 from a random point of a window holding six ticks of rows, one tick
misses a given new row with probability about e^(-35×50/(6×rows per
tick)), 18–41% today. Over the six ticks a row spends in the window that
is about e^(-35×50/rows per tick), under 1% today and about 7% at twice
today's commit rate. It assumes uniform arrivals and independent draws.

**Owner drain** (`backend/utils/batch_pipeline/projection_drain.py`).
Per process; no new read, no new stored state, no index.

- A newly requested owner's first turn runs
`BATCH_PIPELINE_OWNER_DRAIN_FIRST_TURN_SECONDS` (default 15, about one
step) instead of 120.
- An owner whose productive turn ends with rows left is a backlog owner.
It keeps 120-second turns but waits in its own queue; newly requested
owners launch first.
- A backlog turn ends at its next step boundary (`stop=yield`) when a
newly requested owner is waiting and no slot is free: one backlog turn
per waiting owner, and never the last backlog turn running.
- If no backlog owner is running, one goes ahead after four rounds of
new owners, so the backlog cannot be starved.

Unchanged: order inside one owner (commit order under the assignment
fence and the owner claim), discovery, claim-aware admission, privacy
and live-target backoff. An owner's fresh row behind that same owner's
backlog still waits for it.

Details:
`backend/docs/listen_pusher_pipeline/changes/2026-11-13-projection-fresh-owner-turns.md`.

## Rollout

Ships on. Both are scheduling changes inside one process, each with an
off value: `BATCH_PIPELINE_OWNER_DRAIN_FIRST_TURN_SECONDS=0` restores
the single FIFO queue and 120-second turns;
`BATCH_PIPELINE_PROJECTION_RECENT_DISCOVERY_SECONDS=0` restores the
single discovery cursor and removes the 30-minute restart. Deploy:
`backend-integration` only (the surface that runs the projection owner
drain).

Must stay clean after deploy: projected rows per minute not lower than
before (the backlog keeps draining); enrichment 200s per minute flat.
Expected to improve: share of under-1h captures committed 10–20 minutes
earlier that are unprojected.

## Known limits

- The main cursor advances at as little as half its speed while recent
rows fill their half of the page, so a backlog row that depends on
discovery alone (not on its owner's running drain) is found later.
Owners already draining are not affected. The 30-minute restart is
random: one restart in eight begins at the oldest row, the others at a
uniform point, so every stretch of the log has a chance of being read in
each period. That is not a guarantee for any single row, and how long a
row can wait has not been measured.
- A row is offered by the recent read once per process run, so up to 35
times fleet-wide when fewer than 50 rows are pending in the window, plus
once for each process that restarts while the row is in the window. The
bound assumes the commit stamp is no more than one window ahead of the
process clock (entries are kept for two windows). Offers per tick stay
within the page size, as before; each offer reads the row's job and
result, and repeated owners are coalesced per process and by the owner
claim across processes.
- It does not help an owner whose fresh row sits behind that owner's own
backlog (about half of the waiting fresh rows today).
- A long chain's read-ahead is per turn, so a backlog turn that yields
early discards it; under constant arrival of new owners, backlog turns
get shorter and the backlog drains somewhat slower.
- Classification is per process and resets on restart: after a deploy
every backlog owner takes one 15-second first turn before it is known as
backlog.

## Tuning disclosure

- BATCH_PIPELINE_OWNER_DRAIN_FIRST_TURN_SECONDS new: 15 (0 = off, max
120)
- BACKLOG_OWED_TURNS new: 4
- BATCH_PIPELINE_PROJECTION_RECENT_DISCOVERY_SECONDS new: 120 (0 = off,
max 3600)
- MAIN_PASS_SECONDS new: 1800
- MAIN_RESTART_HEAD_SHARE new: 0.125
- offer memory new: two windows (240 s at the default)

No new pipeline state and nothing new stored. New process memory only:
in the owner drain, the backlog queue and two sets of owner ids; in
discovery, the ids and offer times of rows the recent read offered
(about 150 a minute at most; an entry stops suppressing after two
windows and is removed when the map next passes 4,096 entries, so older
entries can sit in memory until then) and the time the main pass began.

## Product invariants affected

none

## Failure class (fixes)

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

Second fix in this class: the first (#25204) stopped the unmetered
release; this one gives fresh work its own place in the projection stage
the release filled.

## Tests

```
cd backend
.venv/bin/python -m pytest tests/unit/test_batch_pipeline_projection_discovery.py tests/unit/test_batch_pipeline_owner_drain.py \
  tests/unit/test_batch_pipeline_sweep_owner_chain.py tests/unit/test_pipeline_sim_projection_drain.py tests/unit/test_flag_budget.py -q
```

New in `tests/unit/test_batch_pipeline_owner_drain.py`: a light owner
queued behind three heavy owners finishes before any heavy owner gets a
second turn, and every chain still projects whole and in order; new
owners launch before queued backlog owners and the backlog is still owed
a turn; a backlog turn yields only for a waiting new owner without a
free slot, one per waiting owner, never the last one; the off value; the
setting's bounds. The existing FIFO fairness test now pins the off
value.

New in `tests/unit/test_batch_pipeline_projection_discovery.py`: a new
commit is on the first page behind a 400-row backlog and the page is no
larger; the main cursor visits every backlog row while a full half page
of new commits arrives on every tick; a row that stays pending is
offered by the recent read once in 20 minutes of ticks, and once more by
a restarted process; rows already offered still count against the page
(query limits 50 and 50); each tick reads from a different point; the
off value keeps one cursor and no restart; a failed recent query costs
only its half; rows read before a failed main query are not recorded as
offered and are offered again when a later read reaches them (shown with
a fixed start point, striping on and off); a main cursor that has not
wrapped for 30 minutes restarts and one that wraps does not; one restart
in eight begins at the oldest row; setting bounds.


`tests/unit/test_batch_pipeline_sweep_owner_chain.py::test_pending_page_skips_receipts_and_follow_ups_still_run`
pinned one query for the pending page; it now pins two, with limits 50
and 100. That test was already failing on the first pushed heads of this
branch and neither the pre-push gate nor CI ran it.

## Review

Independent review (Luna). Round 1 on `cc4a583`: FIX, one should-fix (a
failed main query moved the recent cursor); the owner drain passed
checks A to G. Round 2 on `1a06b86`, against the contract (SCA-1960):
FIX. Blocker: with half a page, the main cursor may never wrap under
sustained arrivals, so rows before every process's start point could go
unread. Should-fix: a still-pending recent row could be re-offered up to
45 times per process per window. Note: one more query per tick. I also
found that the round-2 design would have left every process's recent
cursor at the window floor under today's load. The recent read was
rewritten (no cursor, one offer per row per process run) and the
30-minute restart added.

Round 3 on `4d722a7`: FIX. It confirmed the restart's gating, the page
and query counts, the off value, the failed-query paths and unchanged
order inside an owner. Blocker: a uniformly random restart point almost
never lands on the head of the log, so the oldest rows could still go
unread under unending arrivals. Should-fix: the once-per-process bound
assumed aligned clocks and no restarts; several statements in this
description were stale or too strong. Response: one restart in eight now
begins at the oldest row; offer entries are kept for two windows; the
description is corrected. The rollout owner ruled the 1-in-8 head
restart acceptable as probabilistic coverage (SCA-1960); a cursor that
guarantees every row is read is assigned to the metered-release PR.

Narrow confirmation on `b30e6be`: the code matches the ruling (head
restart, gating, two-window offer memory in filter and prune, nothing
recorded on a failed main query), tests prove their names, no security,
privacy, custody or data-loss risk. Its verdict was FIX for two
sentences in this description: the retry after a failed main query was
stated as next-tick, and the memory statement ignored that pruning only
runs above 4,096 entries. Both are corrected above; no code change
followed, so the head is unchanged. A description-only pass on the same
head confirmed both and asked for one more wording change (replicas can,
not do, land on different rows), made above. The reviewer proposed
always restarting at the oldest row; that was not taken because every
process would then re-read the same first rows each period and nothing
past them.

Not tested here: production behaviour, the coverage estimate above, and
the effect on the backlog's drain rate.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
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