Skip to content

fix(tracking): page the pending-task read so large runs aren't cut short - #716

Merged
gkhngyk merged 1 commit into
mainfrom
fix/714-drain-pagination
Aug 13, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/714-drain-pagination

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Summary

The drain loop read a brand's pending tasks in one un-paginated request, and PostgREST silently caps those at 1000 rows. The largest brand submits ~2130 tasks, so every poll reported exactly 1000 pending — wrong in two directions at once:

  • First poll: processed = 2130 − 1000 = 1130, so the loop decided delivery had started and began the stall clock against a run that had received nothing.
  • Every later poll: the number never moved, so the loop decided nothing was progressing.

Fifteen minutes of that plus the tasks crossing the 30-minute ghost age exited the run 06:45:43 — four seconds past the threshold — with its first result arriving 06:49:52. Three consecutive nights the ledger row was deleted and the dashboard and pulse stayed frozen on an older window while ~1300–1800 results landed on time.

Only the one brand over 1000 tasks was ever affected (next largest: 840), which is why it read as brand-specific rather than as a query bug. Verified against this project's REST endpoint: an un-paginated select returns exactly 1000 rows.

Two smaller holes are closed alongside it, each able to cause the same premature exit on its own:

  • "Delivery started" now requires a result to exist, not merely a pending row to disappear. The callback handler also deletes rows for FAILED tasks, for a COMPLETED task with no response body, and when the brand lookup fails — and ~800 of that day's tasks produced no result at all, so a shrinking pending set was never proof that anything had arrived. Costs one indexed existence check per poll, and only until delivery starts.
  • The ghost age is configurable (CLORO_GHOST_TASK_AGE_MIN, default 30 — unchanged behaviour). Its premise is that a task older than the threshold can no longer be in flight, and delivery latency undercuts that today: one brand's first result arrived at 34 minutes, another's at 29.7, seconds under the threshold.

Related issue

Closes #715

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

The failure is timing-dependent, so it is covered two ways.

Unit tests (yarn test from server/, 199 passing, 5 new) pin the paging behaviour: a 2130-row set — what the brand actually submits — comes back whole; an 840-row set costs one request; a page that comes back exactly full triggers one more; a failed page returns an error rather than a partial set, since a half-read pending set is exactly the "progress that never happened" this change exists to prevent.

Replaying the real numbers from the 13th through both versions of the loop — 2130 tasks, first result at 34.0 min, delivery ending at 57 min, 1310 results, 11 ghosts:

old new
exit ghosts @ 30.3 min drained @ 57.0 min
first delivery detected at 0.0 min (false) 34.3 min (true: 34.0)
results at exit 0 / 1310 1310 / 1310
run stamped no — row deleted yes

In production after the next nightly cycle: the brand should get a tracking_runs row again, the log should carry cloro delivery started with a waitedMs near the real delivery latency rather than near zero, and the drain should finish (drained) rather than (ghosts).

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

PostgREST silently caps an un-paginated select at 1000 rows. The drain loop
read a brand's pending tasks in one request, so the largest brand — which
submits ~2130 tasks — saw a pending count frozen at exactly 1000 on every
poll.

That produced two false readings at once. On the first poll it looked like
1130 tasks had already finished, which set "delivery started" and began the
stall clock against a run that had received nothing. On every poll after it
the number never moved, which read as "nothing is progressing". Fifteen
minutes of that, plus the tasks crossing the 30-minute ghost age, exited the
run four seconds after the threshold — four minutes before its first result
arrived. Three consecutive nights the brand's ledger row was deleted and its
dashboard and pulse stayed frozen on an older window.

It only ever bit the one brand above 1000 tasks (next largest: 840), which
is why it looked brand-specific rather than like a query bug.

Two smaller holes closed alongside it, both able to cause the same premature
exit on their own:

- "Delivery started" now requires a result to actually exist, not merely a
  pending row to disappear. The callback handler also deletes rows for FAILED
  tasks, for a COMPLETED task with no response body, and when the brand
  lookup fails; ~800 of today's tasks produced no result at all, so a
  shrinking pending set was never proof that anything had arrived.
- The ghost age is now configurable (CLORO_GHOST_TASK_AGE_MIN, default 30 —
  unchanged behaviour). Its premise is that a task older than the threshold
  can no longer be in flight, and current delivery latency undercuts that:
  one brand's first result arrived at 34 minutes, another's at 29.7.

Replaying today's numbers through both versions: the old drain exits at 30.3
minutes with 0 of 1310 results and the run is discarded; the new one exits
drained at 57.0 minutes with all 1310, having detected first delivery at
34.3 minutes against a true 34.0.
@gkhngyk gkhngyk self-assigned this Aug 13, 2026
@gkhngyk
gkhngyk merged commit 7161ed7 into main Aug 13, 2026
5 checks passed
@gkhngyk
gkhngyk deleted the fix/714-drain-pagination branch August 18, 2026 18:29
gkhngyk added a commit that referenced this pull request Aug 19, 2026
…row cap (#740)

The skip added in #739 read one row per *citation*, not per answer: 200
answers matched 1,591 rows in production, and PostgREST silently caps an
un-paginated select at 1,000. Everything past that first page looked unstored,
was reprocessed, and had its writes discarded as duplicates — so a repair run
reported thousands of citations written while the table did not move at all.
The same trap as #714 and #716, and this file now says so.

The lookup is paginated. A short page is the end-of-results signal, which is
why it reads in exactly PostgREST's own page size.

The reporting that hid it is fixed too. persistCitationRows counted the rows it
attempted rather than the rows that landed, so duplicates counted as writes.
The upsert now returns what it inserted, and the function separates three
outcomes that were previously one number:

  n     rows inserted
  0     nothing new — every row was already there, which is what --force
        produces on every answer it revisits
  null  nothing stored, retry

Conflating 0 with failure would have made --force report every answer as
broken; conflating failure with 0 is what let the original 228 lost answers
look like a healthy run.

Refs #732
@gkhngyk gkhngyk mentioned this pull request Aug 24, 2026
7 of 12 tasks
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.

bug: drain loop reads only the first 1000 pending tasks, discarding large runs

1 participant