Skip to content

fix(store): index the team loops and make the worker cancel poll one query (part of #447) - #507

Open
mblaauw wants to merge 1 commit into
mainfrom
fix/447-team-loop-indexes
Open

mblaauw wants to merge 1 commit into
mainfrom
fix/447-team-loop-indexes

Conversation

@mblaauw

@mblaauw mblaauw commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Part of #447 (finding 1 and the cancel poll; the worker-token throttle was fixed in #471). All acceptance criteria are now met; pruning archived runs, mentioned in the issue text, is not done.

Summary

The team background loops run every 2 seconds on every API replica and filtered
team_runs / team_queue_items by status = 'running' without a usable index, so
list_running_team_runs scanned the whole (never pruned) run history.

  • Migration 0065_team_loop_indexes adds partial indexes that hold only running
    rows: team_runs (id), team_queue_items (team_run_id), and team_queue_items
    (updated_at) for items not yet linked to a run. EXPLAIN on 200 000 runs: the
    running-runs query goes from a parallel seq scan (9.7 ms) to a bitmap scan on
    team_runs_running_idx (0.05 ms); the other two use the new indexes.
  • GET /worker/executions/{id}/cancel, polled every second per running
    execution, now answers with one query (org + bound worker token) instead of
    ensure_execution plus a flag read (four to five queries). Rows claimed before
    token binding still take the full check.

The worker_tokens last_used_at throttle (finding 2, first half) landed in #471.

Testing

  • cargo fmt --check, clippy --workspace --all-targets -D warnings: clean
  • cargo test --workspace with a database: 762 passed, 0 failed
  • New store test worker_cancel_poll_reads_only_the_bound_token_row; the worker-token binding API test checks the claiming worker's cancel poll
  • EXPLAIN on 200,000 team runs (20 running): list_running_team_runs Parallel Seq Scan 9.7 ms → Bitmap Index Scan 0.045 ms; list_team_runs_pending_outcome 0.97 → 0.083 ms (Index Only Scan); release_stale_team_queue_claims 0.12 → 0.022 ms
  • pnpm docs:check: passed

Behaviour change: a bound token that already claimed an execution reads its cancel flag even if its profile's agent scope changed after the claim (previously 403). Migration 0065.

🤖 Generated with Claude Code

…query (part of #447)

The team background loops run every 2 seconds on every API replica and filtered
team_runs / team_queue_items by status = 'running' without a usable index, so
list_running_team_runs scanned the whole (never pruned) run history.

- Migration 0065_team_loop_indexes adds partial indexes that hold only running
  rows: team_runs (id), team_queue_items (team_run_id), and team_queue_items
  (updated_at) for items not yet linked to a run. EXPLAIN on 200 000 runs: the
  running-runs query goes from a parallel seq scan (9.7 ms) to a bitmap scan on
  team_runs_running_idx (0.05 ms); the other two use the new indexes.
- GET /worker/executions/{id}/cancel, polled every second per running
  execution, now answers with one query (org + bound worker token) instead of
  ensure_execution plus a flag read (four to five queries). Rows claimed before
  token binding still take the full check.

The worker_tokens last_used_at throttle (finding 2, first half) landed in #471.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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