Skip to content

fix(storage): stop four statements that blocked the control plane for minutes a day - #1406

Merged
zhaozhaozz merged 1 commit into
agent-CI-Infra:mainfrom
zhaozhaozz:fix/issue-1401-small-stall-statements
Nov 26, 2026
Merged

zhaozhaozz merged 1 commit into
agent-CI-Infra:mainfrom
zhaozhaozz:fix/issue-1401-small-stall-statements

Conversation

@zhaozhaozz

Copy link
Copy Markdown
Collaborator

Summary

Four small statements blocked the control plane's thread for minutes a day in production (#1401: 1,181 s + 557 s + 370 s + 317 s over 24 h). Each one is fixed where it runs, without new indexes on large tables.

  1. recordRetryFailure no longer scans task_tools (src/tasks/retries.ts, src/agent/workspace.ts, src/agent/engine.ts). When the runner answers a workspace_finalize with an error, collectArtifact now throws FinalizeFailed (still candidate_unavailable / waiting_environment), which carries the id the call recorded the operation under. The engine passes that id to recordRetryFailure, which reads the row by primary key. Before, it searched the task's latest failed finalize with task_id=? … ORDER BY rowid DESC, and task_tools has no task index (6.9 M rows: up to 306 s per call).
  2. task_metrics checkpoint every 30 s, only while a clock runs (src/agent/engine.ts). Readers (withTaskMetrics, the statistics query) already add now - active_since, so the checkpoint only bounds what a restart loses (start() drops running intervals). It ran on every one-second tick before.
  3. GitHub rate-limit windows are written only when they change what readers act on (src/github/rate-limits.ts). record reads the stored row (a primary-key read, no commit) and skips the upsert unless the window reset or quota changed, the window ran out or came back (remaining 0 ↔ non-zero, which blockedUntil reads), the bucket's kind/account/installation/owner changed, remaining moved by 1% of the quota, or the stored report is 30 s old. refused() still records its limited_until every time.
  4. Bot PR wake human-comment facts read through dispatch_events_object (src/review/bot-pr-wake.ts). Adding object_type IN ('pr','issue') alone was not enough: without ANALYZE statistics SQLite still picked dispatch_events_repository (repository_id, created_at) because its order spares the sort, and read every event of the repository (~40 KB each). Sorting by +created_at takes that advantage away, so the three-column index wins and only the matching rows are sorted. The four event types are only recorded with object type pr or issue (review/queue.ts, review/development.ts, review/bot-pr-wake.ts), so the row set is unchanged.

Query plans and measurements

Measured with the repository's Store schema on SQLite 3.50.4 (Node 22.19.0), no ANALYZE, warm cache. Production runs these on an 87 GB control.sqlite with a cold cache and IO pressure, so the absolute times there are far larger; what changes is how many rows each statement touches.

Before After
1. failed finalize lookup, 1,000,001 task_tools rows SCAN task_tools, 154.6 ms SEARCH task_tools USING INDEX sqlite_autoindex_task_tools_1 (id=?), 0.031 ms
2. task_metrics checkpoint writes, two minutes with a running task 120 4 (none while nothing runs)
3. rate-limit upserts, 1 request/s for an hour 3,600 120
3. rate-limit upserts, 10 requests/s for five minutes 3,000 60
4. comment facts, 5,000 events of ~80 KB, 26 matching SEARCH dispatch_events USING INDEX dispatch_events_repository (repository_id=?), 8.8 ms, reads all 5,000 rows SEARCH dispatch_events USING INDEX dispatch_events_object (repository_id=? AND object_type=? AND object_number=?) | USE TEMP B-TREE FOR ORDER BY, 0.95 ms, reads 26 rows

For item 4, object_type IN ('pr','issue') with the old ORDER BY created_at still planned dispatch_events_repository (repository_id=?); with statistics both old and new statements already use dispatch_events_object.

Behaviour notes

  • A candidate_unavailable that no finalize reported (conflict resolution in src/tasks/request.ts) now records no operation. Before, it cited whatever failed finalize the task had last, possibly from an earlier attempt.
  • The console's rate-limit numbers can lag by at most 30 s or 1% of the quota; a used-up window, a reset and a quota change are recorded at once. docs/features/deployment-and-operations.md says so.
  • A restart loses at most 30 s of runtime per running task (was 1 s), as start() already drops the interval since the last checkpoint.
  • Not changed: src/roles/recovery.ts reads a finalize receipt with the same task_id=? pattern, but only on a narrow recovery path (a Dev run stopped before its first checkpoint with a preserved workspace) and it did not appear in the 24 h statistics.

Tests

  • test/st/src/tasks/retry-reports.test.mjs: a real Workspace.artifact against a manager that fails the finalize throws FinalizeFailed naming the recorded row; the retry record cites that operation (not an earlier attempt's), a failure without a finalize cites none, and the only task_tools read plans as SEARCH … (id=?).
  • test/st/src/metrics/metrics.test.mjs: with mocked Date, 40 one-second ticks with nothing running write nothing, 120 ticks with a running task write 4 times, runtime_ms/active_since match the last checkpoint and withTaskMetrics stays exact.
  • test/st/src/github/github-rate-limits.test.mjs: the write counts above, a used-up window written at once and blocking, a refused request still recording its wait, and a new window / quota / learned owner / 30 s-old report written.
  • test/ut/src/storage/history-lookup-plan.test.mjs: botPrWakeRevisionFacts returns the comments on the PR and its root Issue, and the statement it runs plans through dispatch_events_object with all three columns, while the replaced statement reads the repository's events.

Verification

Head f4ed067c (rebased on origin/main a6c824ca), Node 22.19.0:

  • npm ci → exit 0; (cd runner && npm ci) → exit 0
  • npm run check → exit 0 (runtime syntax checks passed, 854 files)
  • npm test → exit 1: 2,862 tests, 2,851 pass, 11 fail in 7 sets (git-cache, event-subscriptions, rule-loading, distributed-nodes, service-step-timing, runner-transport, codex-harness), with the host's 1-minute load around 28 from other work. Each failure is a wall-clock assertion or the local socket-path limit.
    • The six sets rerun alone (BOT_TEST_JOBS=3 node test/framework/test-sets.mjs …) → exit 0, all passed.
    • runner-transport rerun with a short TMPDIR (this worktree's path makes its Unix sockets exceed 108 bytes, EADDRINUSE) → exit 0, 4/4.
    • On the pre-rebase head 171bd21f a full run failed a different set of 6 (bot-pr-wake, concurrency, distributed-nodes, repository-skills-request, runner-transport, codex-harness); all passed alone. codex-harness passed 68/68 alone on both this branch and 2f2195a6.
  • The sets with the new tests (retry-reports, github-rate-limits, metrics, history-lookup-plan) passed in the full run.

Fixes #1401

… minutes a day

- A failed workspace_finalize now stops with FinalizeFailed, which names the
  operation it recorded; the retry record reads that task_tools row by its key
  instead of searching the task's rows in a table with no task index.
- The engine checkpoints running task clocks into task_metrics every 30 s and
  only while a clock runs, instead of committing on every one-second tick.
  Readers already add the running interval themselves.
- A GitHub response rewrites its rate-limit window only when the stored numbers
  would change what readers act on (new window or quota, used up or back,
  owner learned, remaining moved by 1% of the quota) or are 30 s old.
- The human-comment facts of a bot PR wake name object_type and sort with
  +created_at, so they read through dispatch_events_object instead of every
  event of the repository through dispatch_events_repository.

Fixes agent-CI-Infra#1401
@zhaozhaozz
zhaozhaozz added this pull request to the merge queue Nov 26, 2026
@zhaozhaozz

Copy link
Copy Markdown
Collaborator Author

Reviewed head f4ed067c699fd19adaecbe9409a1e020762ef88a independently (detached worktree on a clean rebase onto origin/main a6c824ca) and queued it for merge.

What I checked, beyond the description:

  • recordRetryFailure / FinalizeFailed: the stop carries exactly the operation id its own call() recorded (task.id:attempt:key), the lookup is by primary key and still constrained by task_id + state='completed' + a stored error, so a replayed or missing row cites nothing rather than the wrong one. The old task_id = ? scan is gone; EXPLAIN QUERY PLAN is SEARCH task_tools USING INDEX sqlite_autoindex_task_tools_1 (id=?).
  • task_metrics checkpoint: both readers (withTaskMetrics, the statistics query) already add now - active_since; no other consumer reads runtime_ms directly. The 30 s cadence bounds a restart's loss, and the guard still writes on the first tick for a running task.
  • Rate-limit writes: blockedUntil reads only remaining/reset_at/limited_until; the 0 ↔ non-zero transition, a new window/quota and refused() always write, so backoff stays correct while the displayed numbers lag at most 30 s or 1% of the quota, as docs/features/deployment-and-operations.md now says.
  • Wake facts query: every producer of the four event types records object_type 'pr'/'issue' (queue.ts, development.ts, bot-pr-wake.ts, filtered records), and the column has existed since the table was created, so the row set is unchanged. +created_at is a SQLite no-op that keeps text ordering (ISO timestamps stay chronological) while denying the planner the cheap index order; the plan test pins dispatch_events_object (repository_id=? AND object_type=? AND object_number=?) and fails without it.

I re-ran the four changed test sets locally (all pass); the required checks are green on this head (unit shards 1–2, e2e, the four image builds, CodeQL).

Non-blocking: src/agent/engine.ts threads failure.operation from instanceof FinalizeFailed, but no test drives a failed workspace_finalize through TaskEngine (the new retry test passes the id to recordRetryFailure directly). If that one line were dropped, citations would silently disappear again; an engine-level regression test would be worth adding later.

Enqueued with expectedHeadOid = reviewed head; queue entry state QUEUED.

Merged via the queue into agent-CI-Infra:main with commit 4a40741 Nov 26, 2026
12 checks passed
@zhaozhaozz
zhaozhaozz deleted the fix/issue-1401-small-stall-statements branch November 26, 2026 09:59
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.

Control plane: small statements that block the event loop for minutes a day (task_tools scan, task_metrics and rate-limit writes, dispatch facts)

1 participant