Repository navigation
fix(storage): stop four statements that blocked the control plane for minutes a day - #1406
Conversation
… 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
|
Reviewed head What I checked, beyond the description:
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: Enqueued with |
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.
recordRetryFailureno longer scanstask_tools(src/tasks/retries.ts,src/agent/workspace.ts,src/agent/engine.ts). When the runner answers aworkspace_finalizewith an error,collectArtifactnow throwsFinalizeFailed(stillcandidate_unavailable/waiting_environment), which carries the id the call recorded the operation under. The engine passes that id torecordRetryFailure, which reads the row by primary key. Before, it searched the task's latest failed finalize withtask_id=? … ORDER BY rowid DESC, andtask_toolshas no task index (6.9 M rows: up to 306 s per call).task_metricscheckpoint every 30 s, only while a clock runs (src/agent/engine.ts). Readers (withTaskMetrics, the statistics query) already addnow - active_since, so the checkpoint only bounds what a restart loses (start()drops running intervals). It ran on every one-second tick before.src/github/rate-limits.ts).recordreads 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 (remaining0 ↔ non-zero, whichblockedUntilreads), the bucket's kind/account/installation/owner changed,remainingmoved by 1% of the quota, or the stored report is 30 s old.refused()still records itslimited_untilevery time.dispatch_events_object(src/review/bot-pr-wake.ts). Addingobject_type IN ('pr','issue')alone was not enough: withoutANALYZEstatistics SQLite still pickeddispatch_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_attakes 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 typeprorissue(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
Storeschema on SQLite 3.50.4 (Node 22.19.0), noANALYZE, warm cache. Production runs these on an 87 GBcontrol.sqlitewith a cold cache and IO pressure, so the absolute times there are far larger; what changes is how many rows each statement touches.task_toolsrowsSCAN task_tools, 154.6 msSEARCH task_tools USING INDEX sqlite_autoindex_task_tools_1 (id=?), 0.031 mstask_metricscheckpoint writes, two minutes with a running taskSEARCH dispatch_events USING INDEX dispatch_events_repository (repository_id=?), 8.8 ms, reads all 5,000 rowsSEARCH 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 rowsFor item 4,
object_type IN ('pr','issue')with the oldORDER BY created_atstill planneddispatch_events_repository (repository_id=?); with statistics both old and new statements already usedispatch_events_object.Behaviour notes
candidate_unavailablethat no finalize reported (conflict resolution insrc/tasks/request.ts) now records nooperation. Before, it cited whatever failed finalize the task had last, possibly from an earlier attempt.docs/features/deployment-and-operations.mdsays so.start()already drops the interval since the last checkpoint.src/roles/recovery.tsreads a finalize receipt with the sametask_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 realWorkspace.artifactagainst a manager that fails the finalize throwsFinalizeFailednaming the recorded row; the retry record cites that operation (not an earlier attempt's), a failure without a finalize cites none, and the onlytask_toolsread plans asSEARCH … (id=?).test/st/src/metrics/metrics.test.mjs: with mockedDate, 40 one-second ticks with nothing running write nothing, 120 ticks with a running task write 4 times,runtime_ms/active_sincematch the last checkpoint andwithTaskMetricsstays 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:botPrWakeRevisionFactsreturns the comments on the PR and its root Issue, and the statement it runs plans throughdispatch_events_objectwith all three columns, while the replaced statement reads the repository's events.Verification
Head
f4ed067c(rebased onorigin/maina6c824ca), Node 22.19.0:npm ci→ exit 0;(cd runner && npm ci)→ exit 0npm 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.BOT_TEST_JOBS=3 node test/framework/test-sets.mjs …) → exit 0, all passed.runner-transportrerun with a shortTMPDIR(this worktree's path makes its Unix sockets exceed 108 bytes,EADDRINUSE) → exit 0, 4/4.171bd21fa 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-harnesspassed 68/68 alone on both this branch and2f2195a6.retry-reports,github-rate-limits,metrics,history-lookup-plan) passed in the full run.Fixes #1401