Repository navigation
fix(storage): read role checkouts without walking each task's history - #1404
zhaozhaozz merged 1 commit into
Conversation
The storage refresh (every five minutes) and the hourly workspace sweep read each unfinished task's role checkouts with task_id=? AND type='role_candidate'. No index covers task_id with type, so every history row of the task was read to compare its type: in production 0.1-8 s per task, 65.9 s for the 65 unfinished tasks holding directories, in one synchronous block every five minutes. A task's newest checkout per role is now read once through the indexes there are (its runs from task_history_run, their candidates from task_history_run_type, its rows without a run), then only the rows added since, a rowid range at the end of the table. History ids only grow and a role_candidate row is never rewritten, so the result is the same as reading the whole history. The first refresh or sweep after a start reads the tasks one at a time, yielding between them, and the refresh yields between projects. No index is added: building one on task_history (13.2M rows) at start would read the whole table and sort it through the 512 MB /tmp of the control plane. Fixes agent-CI-Infra#1398
|
Reviewed head The cached newest I compared the cached result against an independent full-history read (newest row per role, Required checks (unit, e2e, the four image builds, CodeQL) pass on this head. No blocking findings — enqueued for merge. |
| const nowMs = Date.now(); | ||
| // The role checkouts first, a task at a time, then one project at a time, yielding between them (#1398): a | ||
| // project's records take a few hundred ms in production. | ||
| await primeRoleCandidates(this.store); |
There was a problem hiding this comment.
F1 (medium, suggestion): the cold role-checkout read this call is meant to spread has already happened above it, in one synchronous block.
This await primeRoleCandidates(this.store) is the only place the refresh warms the cache, but this.computer(row) runs for every computer in the loop just above (src/app/storage.ts:130) before this line, and it calls workspaceClasses() (:198), which calls retention() → unfinishedWorkspacesState() → roleCandidates(store, ids) with every unfinished task that owns a listed directory of that project. this.classified (:57) is empty on a fresh process, so that is exactly the cold read: per project, one SELECT DISTINCT run_id … IN (all that project's unfinished ids) plus the run/run_id IS NULL candidate reads, run synchronously for all projects in the same loop. When this line is then reached, every id is already in cache.tasks, so the for loop inside primeRoleCandidates neither reads nor yields.
Evidence: tracing a real StorageReport.refresh() (6 unfinished tasks in one project, manager scan carrying result.tasks) shows the only role read is a single batch of 6 task ids inside computer(), and no per-task (IN (1)) read afterwards; calling workspaceClasses() alone shows the same single batch of 6. So the one-task-at-a-time priming that this PR describes as the fix (“StorageReport.refresh() and WorkspaceCleanup.sweep() call it before their loops, so the first pass after a start does not do it all in one block”) does not happen on the refresh path — the sweep path is fine because sweep() primes before its project loop. With the PR's own production figures (first-read path 5.3 s / 10.4 s for 65 tasks) the first refresh after each start/deploy still blocks the event loop for seconds, which is what #1398 asks to remove, and the added docs/design/storage-and-knowledge.md sentence (“启动后首次存储页刷新或清理逐任务预热并让出事件循环”) does not describe this path.
Suggested fix: move the prime call above the manager.computers loop (e.g. right after the manager read, before const computers = …), so the cold read is spread per task before workspaceClasses() asks for the same tasks; then add a StorageReport.refresh() test that the first role read is per task when the scan carries tasks — the two new tests only drive primeRoleCandidates() and unfinishedTaskWorkspaces() directly, so neither covers this ordering. If the ordering is intentionally left as is, the PR description and that docs sentence should be corrected instead, because the first refresh after a start still blocks for the full cold read.
Review of PR #1404 (
|
Fixes #1398
Problem
The storage refresh (every five minutes) and the hourly workspace sweep decide which directories an unfinished task can still use. Part of that is each role's newest checkout, read per task with:
No index covers
task_idtogether withtype, so SQLite seekstask_history_run (task_id=?)and reads every history row of the task to compare its type, then sorts. Long-lived tasks have tens of thousands of rows, so this is one random page read per row.Measured read-only on production (v0.4.13, 2026-11-26, in a separate
node:sqliteprocess inside the bot container):[stall]lines seen at five-minute intervals.unfinishedTaskWorkspacesacross all 7 projects took 0.84 s (at most 0.5 s for one project).Change
src/projects/workspace-cleanup.ts: the newestrole_candidateper role of each task now comes from a per-store cache:SELECT DISTINCT run_id … WHERE task_id IN (…) AND run_id IS NOT NULL(coveringtask_history_run);INDEXED BY task_history_run_type (run_id=? AND type=?);INDEXED BY task_history_run (task_id=? AND run_id IS NULL).WHERE id>? AND type='role_candidate'. This is exact for two reasons.task_history.idisAUTOINCREMENT, so it only grows. Androle_candidaterows are never rewritten: the payload migration only movesoperationandruntime_logbodies, and history is deleted only together with its task.primeRoleCandidates(store): once per store, reads the unfinished tasks holding directories one task at a time, withsetImmediatebetween tasks.StorageReport.refresh()andWorkspaceCleanup.sweep()call it before their loops, so the first pass after a start does not do it all in one block.src/app/storage.ts: the refresh also yields between projects.Why no new index
Adding
task_history(task_id,type,id), or a partial index onrole_candidate, would build on start over 13.2M rows in an 87 GB database.some≈ 77%).SQLITE_TMPDIRis the control plane's 512 MB/tmptmpfs. A deploy could fail on start withdatabase or disk is full.Equivalence and evidence
Production (read-only): the per-task query and the new index reads returned the same 270 rows for the 65 tasks. The first-read path took 5.3 s and 10.4 s in two runs, under heavy I/O. In the new code that time is spread one task at a time after each start; later refreshes read only the table end.
Local benchmark: production-shaped database (3.0M history rows; 65 unfinished tasks with 514k rows, 10.3k runs and 19k rows without a run). One storage-refresh pass, with the page cache dropped (
POSIX_FADV_DONTNEED) before each run, two runs each:2f2195a6The storage-page summaries from both versions are identical (same digest).
Tests:
test/ut/src/storage/history-lookup-plan.test.mjspins the plans withEXPLAIN QUERY PLAN: coveringtask_history_run,task_history_run_type (run_id=? AND type=?),task_history_run (task_id=? AND run_id=?)andINTEGER PRIMARY KEY (rowid>?). It also checks that no statement seekstask_history_run (task_id=?)alone or scans the table. Against main's module this test fails on the oldtask_history_run (task_id=?) | USE TEMP B-TREE FOR ORDER BYplan.test/ut/src/projects/workspace-cleanup.test.mjsadds two tests:Verification
Head
e125b736, Node v22:npm ci→ 0(cd runner && npm ci)→ 0npm run check→ 0npm test→ 1: 347 of 352 sets passed (2842/2852 tests). The five failed sets fail for reasons outside this change, and each passes when run alone withnode --test:runner-transport:EADDRINUSE, because the deep worktree path pushes the socket path past 108 bytes; passes 4/4 with a shortTMPDIR.distributed-nodes(5/5),service-step-timing(6/6),pi-mailbox(4/4),codex-harness(68/68): timeouts and duration assertions under parallel load.