ENG-13800: workflow workers report how many runs each table has pending, by state

AD
11h ago
+869-3

Stacked on #8114 (it adds this gauge to the reflex_workflow meter set up there).

An orchestrator asking "how much work is pending per table" had nothing to read. With reflex-otel instrumented, workers now report reflex.workflow.run.count, an observable gauge ({run}) with reflex.workflow.table and reflex.workflow.run.state. The states partition each table's unfinished runs, each run counted in the first that fits:

  • running: leased (claimed_until not past)
  • due: the engine's own claimable predicate (table-wide, not narrowed to this worker's lanes)
  • waiting: on an event, possibly until a deadline
  • scheduled: wakes later
  • parked: no wake time, i.e. waiting for its children

Summing over run.state gives every unfinished run. Every worker of a table reports the whole table, so across instances combine with max (or take any one), never sum. The README and the instrument description say so. Within a process the counts are keyed by table, so two workers there report one series.

The query (engine/backlog.py): one SELECT count(*) FILTER (...) per table, WHERE next_step IS NOT NULL OR waiting_for IS NOT NULL. It reads every unfinished run, so the cost grows with those. Where most runs are finished, Postgres uses the two indexes: 0.3 ms for 1k unfinished among 300k finished. Where many are unfinished it scans the table: about 40 ms with 300k of each. The README says this. The plan test covers only the first case.

When it runs: only while telemetry is on, as a task beside the worker loop (never inline, so a slow or locked count holds up no claim), started by a pass, which has just queried the database, so an idle database is never woken for it. Once per max_idle_interval, or RESAMPLE_AFTER (10 s) after the last count when a step started or finished since. That is at most workers Ɨ tables counts per 10 s under steady work. Never two at once. Bounded to 5 s across tables, starting from a different table each time; a table not reached, or that fails, drops out of the gauge until counted again. drain cancels a running count and drops the worker's own tables from the gauge. The gauge callback runs on the exporter thread and only returns the last count (copy-on-write dict).

Tests

  • test_otel.py: gauge reports the last count per table/state, forgetting a table, disabled path a no-op, disable clears it.
  • test_runner.py (Postgres): exact counts per state over hand-written rows in every state plus finished ones, summing to the unfinished count. Also: the no-seq-scan plan with few unfinished among 5000 finished; the background sample reaching the gauge; once per idle interval or RESAMPLE_AFTER after a step, never two at once, waking the loop when a stale count ends; rotation; fresh after a burst; a table that fails or overruns dropping out; a count that never ends holding up no claim and being cancelled by drain.

uv run pytest tests/units/reflex_workflow tests/units/reflex_otel with REFLEX_TEST_POSTGRES: 1306 passed, 2 skipped. Pre-commit clean on the changed files.

Linear: ENG-13800

šŸ¤– Generated with Claude Code