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

Nothing optional loads at boot: route modules and warm-ups (search, samplers, crew) start on first use #16908

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

This is a partial step on #16852. The /settings… and /forges… route modules and the search warm now load on first use, but the issue is not done. The samplers, the crew warm-ups and most other route families are untouched, and the idle-memory gain is only about 20 MiB.

Notes

opened this

This is a partial step on #16852. The /settings… and /forges… route modules and the search warm now load on first use, but the issue is not done. The samplers, the crew warm-ups and most other route families are untouched, and the idle-memory gain is only about 20 MiB. I did not run the whole suite; the forge's check runs it on this push.

What changed

  • Settings and forges routes: the /settings… pages and the /forges, /family, /pricing and /templates pages now import on their first request. src/http/lazy_lists.ts lists each family's routes, and src/http/server.ts registers them through the existing lazyRoutes hook (the one MCP uses). Matching, the admin gate and 405s behave as before.
  • Search warm: warmOnFirstSearch (in src/search/warm.ts and src/actions/search.ts) replaces the boot-time warmSearch. The warm starts on the turn after the first non-empty search and fires once per forge. The first search is answered as before, cold and budgeted.
  • Idle memory: a fresh forge settles at 255–265 MiB, down from about 281–285 MiB. IDLE_RSS_BUDGET in test/boot-idle-memory.test.ts goes from 400 to 360 MiB.
  • Tests: test/lazy-first-use.test.ts has four tests.
    • Each lazy list equals its module's own routes.
    • Boot's static imports don't reach those modules.
    • The first request to /settings/tokens, /pricing and /templates answers under 2 s, including the import.
    • The warm doesn't start when armed, doesn't start on an empty query, and starts on the first real search while that search still answers.
  • Refactor: staticClosure moved to test/helpers/static-closure.ts, now used by both test/boot-lazy.test.ts and the new test.
  • Docs: AGENTS.md gets a short note on the pattern.

What I chose not to do

  • Samplers: I did not make the 15 s machine sampler or the memory sampler start on first use. What they record is history a page reads later, so starting them late would change behaviour. AGENTS.md says so.
  • Pre-listener warm-ups: I left warmFlow, warmPlans and warmDisplays as they are. They run before the listener opens so that visitors don't wait.
  • Other route families: I did not touch api.ts or repo.ts. They are heavily shared, so each needs its own route list and an audit of what else imports it.
  • Crew warm-ups: I found no separate crew warm-up to move.
  • Search-warm trade-off: the first search now runs cold and the warm fills the grep cache for the next one, where before the boot filled it up front.

Checks

  • tsc is clean and gate.ts passes. The duplicate scan stopped at its 8 s limit and compared against part of the tree.
  • The 262 settings/forges/pricing/templates/usage tests and 98 openapi, page-budget, search, guide and dead-code tests pass.
  • The /forges route answers 404 on a plain test forge, so the first-request test uses /templates instead.

Try: open /settings/tokens, /pricing and /templates on the preview. Each answers as before, and the first search warms the grep cache.

🤖 Generated with Claude Code

Closes #16852

4 earlier comments
commented

This is a partial step on #16852. The /settings… and /forges… route modules and the search warm now load on first use, but the issue is not done. The samplers, the crew warm-ups and most other route families are untouched, and the idle-memory gain is only about 20 MiB. I did not run the whole suite; the forge's check runs it on this push.

What changed

  • Settings and forges routes: the /settings… pages and the /forges, /family, /pricing and /templates pages now import on their first request. src/http/lazy_lists.ts lists each family's routes, and src/http/server.ts registers them through the existing lazyRoutes hook (the one MCP uses). Matching, the admin gate and 405s behave as before.
  • Search warm: warmOnFirstSearch (in src/search/warm.ts and src/actions/search.ts) replaces the boot-time warmSearch. The warm starts on the turn after the first non-empty search and fires once per forge. The first search is answered as before, cold and budgeted.
  • Idle memory: a fresh forge settles at 255–265 MiB, down from about 281–285 MiB. IDLE_RSS_BUDGET in test/boot-idle-memory.test.ts goes from 400 to 360 MiB.
  • Tests: test/lazy-first-use.test.ts has four tests.
    • Each lazy list equals its module's own routes.
    • Boot's static imports don't reach those modules.
    • The first request to /settings/tokens, /pricing and /templates answers under 2 s, including the import.
    • The warm doesn't start when armed, doesn't start on an empty query, and starts on the first real search while that search still answers.
  • Refactor: staticClosure moved to test/helpers/static-closure.ts, now used by both test/boot-lazy.test.ts and the new test.
  • Docs: AGENTS.md gets a short note on the pattern.

What I chose not to do

  • Samplers: I did not make the 15 s machine sampler or the memory sampler start on first use. What they record is history a page reads later, so starting them late would change behaviour. AGENTS.md says so.
  • Pre-listener warm-ups: I left warmFlow, warmPlans and warmDisplays as they are. They run before the listener opens so that visitors don't wait.
  • Other route families: I did not touch api.ts or repo.ts. They are heavily shared, so each needs its own route list and an audit of what else imports it.
  • Crew warm-ups: I found no separate crew warm-up to move.
  • Search-warm trade-off: the first search now runs cold and the warm fills the grep cache for the next one, where before the boot filled it up front.

Checks

  • tsc is clean and gate.ts passes. The duplicate scan stopped at its 8 s limit and compared against part of the tree.
  • The 262 settings/forges/pricing/templates/usage tests and 98 openapi, page-budget, search, guide and dead-code tests pass.
  • The /forges route answers 404 on a plain test forge, so the first-request test uses /templates instead.

Try: open /settings/tokens, /pricing and /templates on the preview. Each answers as before, and the first search warms the grep cache.

🤖 Generated with Claude Code

commented

The settings and forges route modules and the search warm-up now load on first use instead of at boot. This push fixes the one failing check, the timer-naming test in test/timers-named.test.ts.

The deferred start in src/actions/search.ts used a bare setTimeout(first, 0). It is now wrapped as during("search-warm-start", first, undefined, app), like the other server-side timers. Behaviour is the same.

bun x tsc --noEmit is clean. test/timers-named.test.ts and the test/search*.test.ts files pass (104 pass, 2 skipped). I did not run the rest of the suite; the forge runs it on this push.

bun scripts/gate.ts --fix reports one comment line over 100 columns in src/actions/search.ts. It left it alone, because the nightly rewrap on main handles those.

commented

The job-slices check is fixed and pushed. test/job-slices.test.ts and test/timers-named.test.ts pass (11 tests) and bun x tsc --noEmit is clean. I did not run the rest of the suite; the forge runs it on this push.

The earlier fix wrapped the search warm-up's deferred start in during("search-warm-start", …). That test requires every during name to be timed or listed in UNTIMED with a reason, and the new name was in neither. I added it to UNTIMED in test/job-slices.test.ts. The reason given is that it is a one-shot zero-delay timer that only starts the warm-up. The warm-up's own work is covered by its own tests.

I picked "untimed" over timing it in the slice table because the timer does no work of its own.

commented

Settings and forges route modules, plus the search warm-up, now load on first use instead of at boot (#16852).

What changed

  • src/http/lazy_routes.ts and src/http/lazy_lists.ts register each lazy family's method and pattern at boot, ahead of its module. Matching, the admin gate, 405s and HEAD answer as before. The first request imports the module and hands over to its handler. A failed load is retried on the next request.
  • src/search/warm.ts and src/actions/search.ts wait for the first search before warming.
  • test/lazy-first-use.test.ts holds each lazy list to its module's own, checks the boot's static imports don't reach those modules, and checks the first request answers inside the page budget.
  • The failing check, "the marks name only registered routes and heads", broke because main added POST /settings/runners/:id/revoke and /resume to the settings module. The lazy list didn't have them. I merged main and added both to SETTINGS_ROUTES.

Verification

  • test/route-marks.test.ts, lazy-first-use, boot-lazy, boot-idle-memory, timers-named, web-import-cost and unused-exports pass.
  • tsc --noEmit is clean and the gate is quiet.
  • scripts/fast.ts named 2,940 files, so I ran the targeted set above instead of the full suite. The forge runs the full check on push.

Not done

  • No new behaviour or routes.
  • No door change. The list only mirrors routes the module already adds, and the commit says No door: with the reason.

To try Run bun test test/lazy-first-use.test.ts test/route-marks.test.ts. Then boot the app and request /settings. The first hit loads the module, and later hits are warm.

🤖 Generated with Claude Code

commented

Left #16852 open: 2 unticked items remain (Each optional warm-up started at boot (search, samplers, crew) is listed and starts on first use instead; a test per warm-up proves first use still answers.; Boot imports only what /healthz and / need; the other route modules are imported on the first request to their prefix.).

Sign in to join this conversation.

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

Keyboard shortcuts

Docs