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

server.ts imports page, studio, render and API modules on the first request to their prefix #17313

opened this

Merged 32h 48m ago, live in production

The boot's static module count drops from 4061 to 2320, 1741 fewer than before and well past the 700 asked for. The count is the static import closure of src/main.ts, not bun build --metafile. A bundled build inlines dynamic imports, so the metafile does not fall when a module moves to import().

Notes

opened this

The boot's static module count drops from 4061 to 2320, 1741 fewer than before and well past the 700 asked for. The count is the static import closure of src/main.ts, not bun build --metafile. A bundled build inlines dynamic imports, so the metafile does not fall when a module moves to import(). I did not run the metafile here.

Tests. bun x tsc --noEmit is clean. I ran about 1,000 tests that exercise what changed; all pass, including test/boot-lazy.test.ts, test/lazy-first-use.test.ts, test/mcp-size.test.ts and test/unused-exports.test.ts. The full suite was not run: scripts/fast.ts picked 3118 files, which it expects to take hours. No existing test was edited.

Route families

  • buildRouter and buildApiRouter in src/http/server.ts now register all 32 route modules under src/http/routes/, the JSON API, its siblings (feed, config, checks_recheck) and the /api and /api/v1/changelog pages from route lists in src/http/lazy_lists.ts. Each module is imported on the first request to its prefix.
  • test/boot-lazy.test.ts holds each list to the routes its module adds, in order. It also checks that no family module is in the boot's static closure.
  • It also holds a ceiling on the boot's module count: 2400, against 2320 now.
  • forges.ts stays as it was; its module is already lazy.

Leaks I cut so the count could drop

  • contract/openapi.ts is read with require on first use in cursor_lists.ts, named_refusal.ts, web/standards.ts and admin_gate.ts. The gate loads the contract only for /api/ routes and /metrics. A new test fails if the contract adds an admin route outside those.
  • originIcon moved to web/origin_icon.ts, so web/scope.ts no longer pulls in pages/origins.ts.
  • isPreviewEnv and the preview account lookups moved to spawn/preview_who.ts. preview-seed.ts re-exports them, so its importers are untouched, and the seed itself is imported only in a preview.
  • main.ts imports the home, plans and boards warm-ups and recordAskLedger with import(). They still run at boot as before.
  • The everyday-account refusal page is read when first needed.

I also updated AGENTS.md on where modules load and how a new route module is mounted.

Not done. About 58 modules in the page and studio families remain reachable through callers such as actions/* that import web/studio/asks.ts. Cutting them needs asks.ts's data half moved out of web/, which is a separate change. The previewAdmin and previewNewcomer near-duplicate the gate flagged was already in the code I moved.

To try it. On the preview, open /, /settings/tokens, /api/v1/changelog and /api; each answers as on main, and the first request to each prefix pays one import. To count the boot's modules, run bun test test/boot-lazy.test.ts.

🤖 Generated with Claude Code

Closes #17020

commented

The boot's static module count drops from 4061 to 2320, 1741 fewer than before and well past the 700 asked for. The count is the static import closure of src/main.ts, not bun build --metafile. A bundled build inlines dynamic imports, so the metafile does not fall when a module moves to import(). I did not run the metafile here.

Tests. bun x tsc --noEmit is clean. I ran about 1,000 tests that exercise what changed; all pass, including test/boot-lazy.test.ts, test/lazy-first-use.test.ts, test/mcp-size.test.ts and test/unused-exports.test.ts. The full suite was not run: scripts/fast.ts picked 3118 files, which it expects to take hours. No existing test was edited.

Route families

  • buildRouter and buildApiRouter in src/http/server.ts now register all 32 route modules under src/http/routes/, the JSON API, its siblings (feed, config, checks_recheck) and the /api and /api/v1/changelog pages from route lists in src/http/lazy_lists.ts. Each module is imported on the first request to its prefix.
  • test/boot-lazy.test.ts holds each list to the routes its module adds, in order. It also checks that no family module is in the boot's static closure.
  • It also holds a ceiling on the boot's module count: 2400, against 2320 now.
  • forges.ts stays as it was; its module is already lazy.

Leaks I cut so the count could drop

  • contract/openapi.ts is read with require on first use in cursor_lists.ts, named_refusal.ts, web/standards.ts and admin_gate.ts. The gate loads the contract only for /api/ routes and /metrics. A new test fails if the contract adds an admin route outside those.
  • originIcon moved to web/origin_icon.ts, so web/scope.ts no longer pulls in pages/origins.ts.
  • isPreviewEnv and the preview account lookups moved to spawn/preview_who.ts. preview-seed.ts re-exports them, so its importers are untouched, and the seed itself is imported only in a preview.
  • main.ts imports the home, plans and boards warm-ups and recordAskLedger with import(). They still run at boot as before.
  • The everyday-account refusal page is read when first needed.

I also updated AGENTS.md on where modules load and how a new route module is mounted.

Not done. About 58 modules in the page and studio families remain reachable through callers such as actions/* that import web/studio/asks.ts. Cutting them needs asks.ts's data half moved out of web/, which is a separate change. The previewAdmin and previewNewcomer near-duplicate the gate flagged was already in the code I moved.

To try it. On the preview, open /, /settings/tokens, /api/v1/changelog and /api; each answers as on main, and the first request to each prefix pays one import. To count the boot's modules, run bun test test/boot-lazy.test.ts.

🤖 Generated with Claude Code

commented

This push fixes the three failing tests from the check. The full suite has not been run here. I ran the failing tests, boot-lazy, admin-gate, guide, server-timing-sweep, unused-exports, and the openapi* tests, and all passed. tsc is clean. I stopped a wider scripts/fast.ts run because it named 3121 files and would have taken hours.

What changed

  • require() rule: the lazy loads from the earlier commit used require(), which Bun refuses on a module graph with a top-level await. They are now import().
    • A new src/http/contract_lazy.ts has loadContract(), contract() and the /metrics list. server.ts awaits it only for /api/ and /metrics routes and before the ?page= and ?fields= guards. namedRefusal awaits it too.
    • adminGate is now async so it can import() the everyday page.
    • llmsTxt imports contract/served.ts when it draws the page.
    • admin-gate.test.ts awaits the gate and loads the contract first. boot-lazy.test.ts takes the /metrics list from its new home.
  • Design audit: the tests read the route modules from the ./routes/… imports in server.ts. Those became import() calls, so the helper (route-modules.ts) found none. It now reads both forms. The /api page is registered through own.add, so design-audit.test.ts matches that too.
  • Route drift: test/helpers/registered_routes.ts matches own.add as well, which fixes the described-but-not-routed GET /api/v1/changelog.

I did not add tests for contract_lazy.ts. The existing rule, audit and drift tests are what would fail without it.

gate.ts --fix rewrapped comments in two files. I committed them, since the gate skips uncommitted files. It also flagged preview_who.ts as having two near-duplicate functions (previewAdmin and previewNewcomer). I left that for you to judge.

Sign in to join this conversation.

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

Keyboard shortcuts

Docs