Public: anyone can read this; the crew can change it.

The maintain duty works in slices and yields to the serving thread #17840

opened this

Merged 4h 42m ago, live in production

Two maintenance duties now read their files in 20 ms slices and hand the loop back between them, instead of holding it for one long pass. Which step runs long. I couldn't read production's [loop] lines from the sandbox: /health and /system don't show them, and the perf logs are outside what I can reach.

Notes

opened this

Two maintenance duties now read their files in 20 ms slices and hand the loop back between them, instead of holding it for one long pass.

Which step runs long. I couldn't read production's [loop] lines from the sandbox: /health and /system don't show them, and the perf logs are outside what I can reach. So I timed each duty's file-reading step on this checkout. The worst is untested-sources: one synchronous pass that globs 5,105 src/ files, then reads and regex-matches all 5,096 test files (~37 MB). It took 290–333 ms here, and on a loaded production host that fits the 0.2–1.5 s in the issue. Next is dead-doc-paths: 816 docs read in one pass, about 45 ms. The other duties do database queries or run git without blocking.

What changed.

  • untestedSources (src/maintain/duties/untested_sources.ts) and deadDocPaths (src/maintain/duties/dead_doc_paths.ts) are now async. Before each file they ask the kernel's existing sliced() clock (src/kernel/breathe.ts) whether the current slice has used its 20 ms (SLICE_MS). When it has, they hand the loop back with a zero-delay timer, so waiting requests are answered first, and carry on with the next file.
  • Each takes an optional slice clock, which is how the tests drive it. Both duties' run await the result.
  • I reused sliced() / breathe rather than writing a new helper.

Choices.

  • "The rest carries to the next tick" means the next turn of the event loop, within the same duty run. It does not mean the next 60 s runner tick.
  • So a duty still finishes in one run and still counts as one run against its budget. Pausing, the timeout and never running the same duty twice at once all work as before.
  • Saving a cursor between runner ticks would have needed a table or state kept in memory across the process, and that could leave a duty unfinished. Slicing inside the run already keeps each hold under the cap.
  • runner.ts is unchanged; it already yields between duties.

Tests.

  • test/maint-untested.test.ts: a backlog of 249 test files is read in slices capped at 40 files each ([40,40,40,40,40,40] plus a last slice of 9), and the result is still complete.
  • test/maint-untested.test.ts: with a clock that is always past its slice, a timer set before the pass fires before the pass ends. That test fails if the pass never hands the loop back.
  • test/maintain-dead-doc-paths.test.ts: the same timer check for deadDocPaths.
  • I changed the existing tests that call these two functions only to await them, because the functions became async.

Checks. bun x tsc --noEmit and bun scripts/gate.ts --explain both pass (32 of 32 rules). The 181 test files named by scripts/fast.ts passed apart from one failure in the first run. A second run of the same set, filtered for failure lines, showed none, so I took that one as flaky; I didn't capture which test it was.

To try it: bun test test/maint-untested.test.ts test/maintain-dead-doc-paths.test.ts. On a running forge, the untested-sources and dead-doc-paths rows on /maintenance should report the same findings as before.

🤖 Generated with Claude Code

Closes #17823

commented

Two maintenance duties now read their files in 20 ms slices and hand the loop back between them, instead of holding it for one long pass.

Which step runs long. I couldn't read production's [loop] lines from the sandbox: /health and /system don't show them, and the perf logs are outside what I can reach. So I timed each duty's file-reading step on this checkout. The worst is untested-sources: one synchronous pass that globs 5,105 src/ files, then reads and regex-matches all 5,096 test files (~37 MB). It took 290–333 ms here, and on a loaded production host that fits the 0.2–1.5 s in the issue. Next is dead-doc-paths: 816 docs read in one pass, about 45 ms. The other duties do database queries or run git without blocking.

What changed.

  • untestedSources (src/maintain/duties/untested_sources.ts) and deadDocPaths (src/maintain/duties/dead_doc_paths.ts) are now async. Before each file they ask the kernel's existing sliced() clock (src/kernel/breathe.ts) whether the current slice has used its 20 ms (SLICE_MS). When it has, they hand the loop back with a zero-delay timer, so waiting requests are answered first, and carry on with the next file.
  • Each takes an optional slice clock, which is how the tests drive it. Both duties' run await the result.
  • I reused sliced() / breathe rather than writing a new helper.

Choices.

  • "The rest carries to the next tick" means the next turn of the event loop, within the same duty run. It does not mean the next 60 s runner tick.
  • So a duty still finishes in one run and still counts as one run against its budget. Pausing, the timeout and never running the same duty twice at once all work as before.
  • Saving a cursor between runner ticks would have needed a table or state kept in memory across the process, and that could leave a duty unfinished. Slicing inside the run already keeps each hold under the cap.
  • runner.ts is unchanged; it already yields between duties.

Tests.

  • test/maint-untested.test.ts: a backlog of 249 test files is read in slices capped at 40 files each ([40,40,40,40,40,40] plus a last slice of 9), and the result is still complete.
  • test/maint-untested.test.ts: with a clock that is always past its slice, a timer set before the pass fires before the pass ends. That test fails if the pass never hands the loop back.
  • test/maintain-dead-doc-paths.test.ts: the same timer check for deadDocPaths.
  • I changed the existing tests that call these two functions only to await them, because the functions became async.

Checks. bun x tsc --noEmit and bun scripts/gate.ts --explain both pass (32 of 32 rules). The 181 test files named by scripts/fast.ts passed apart from one failure in the first run. A second run of the same set, filtered for failure lines, showed none, so I took that one as flaky; I didn't capture which test it was.

To try it: bun test test/maint-untested.test.ts test/maintain-dead-doc-paths.test.ts. On a running forge, the untested-sources and dead-doc-paths rows on /maintenance should report the same findings as before.

🤖 Generated with Claude Code

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs