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

The forge runs from one bun build bundle (no runtime readdirSync of its own source), measured with and without --bytecode #16911

Merged opened this

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

Merged

The minified bundle now boots from bun build --minify, answers /healthz and /, and the forge no longer reads its own source directories at boot. The bundle's memory budget is lowered. The source's budget could not be, and the bundle isn't doing the same work as the source yet (details below).

Notes

opened this

The minified bundle now boots from bun build --minify, answers /healthz and /, and the forge no longer reads its own source directories at boot. The bundle's memory budget is lowered. The source's budget could not be, and the bundle isn't doing the same work as the source yet (details below). I ran the tests that cover this change and tsc, and they pass. The forge's full check hasn't run yet.

What changed

  • Build-time lists. scripts/own-source.ts writes src/generated/{migrations,marks,duties,screens}.ts as static imports (duties through lazy require). The migrations (core/schema.ts), route marks (http/route_marks.ts), duties (maintain/registry.ts) and screen palette entries (tui/screens/entries.ts) now boot from those lists instead of readdirSync.
  • Staleness check. test/own-source.test.ts fails when a list no longer matches its directory. AGENTS.md says to run bun scripts/own-source.ts after adding a migration, mark, duty or screen.
  • Moved to helpers. readMigrations and loadDuties now live in test/helpers/read-source-dirs.ts, because tests were their only callers and the dead-code rule would flag them in src/. checkMigrations and rosterOf in src/ hold the shared checks. entryFiles moved to entry_mark.ts and dutyFiles to duty_files.ts.
  • Bundle fixes.
    • Several import.meta.main blocks used top-level await, which a bundle can't require. I wrapped them in async functions in supervisor, release launch/boot, snapshot restore/drill_child, runner/main and chaos/run. Behaviour is the same.
    • kernel/data_home.ts now tolerates a checkout that isn't on disk, which a compiled executable hits.
  • Bundle test and docs. test/bundle-boot.test.ts builds with --minify, boots it from a directory with no src/, and checks /healthz and /. The how-to is docs/features/bundle.md.
  • forge.json is unchanged, so the bundle is an option and source is still the default.

Measurements (Linux, three runs each, bun scripts/boot-measure.ts)

cold startwarm startidle RSS
source5.6 s5.2 / 5.1 s292 MiB
bundle (--minify)2.2 s2.0 / 2.1 s119 MiB
bundle --bytecode (compiled exe)1.7 s1.9 / 2.4 s124 MiB

Not done or not comparable

  • --bytecode can't make a main.js. Without --compile it requires --format=cjs, and main.ts awaits at the top level. The bytecode row is a compiled executable (--compile --bytecode --format esm).
  • The bundle does less than the source.
    • The page workers (pages_worker.ts and other new URL(...) workers) and the docs search index fail with a log line and degrade. Assets built from source are untested in the bundle.
    • Its 119 MiB is therefore not like for like with source. Putting those into the bundle is follow-up work.
  • Budgets. The source budget stays at 400 MiB, since forge.json still runs source and it measures 281 to 292 MiB. I added a 180 MiB budget for the bundle in test/boot-idle-memory.test.ts instead of lowering the source one.
  • Gate and design lint. bun scripts/gate.ts --explain is clean except a duplicate-scan timeout. design-lint wasn't run because nothing under src/web/ changed.

Try it

bun build --minify --target bun --outdir out src/main.ts
bun out/main.js   # with the usual FORGE_DATA and PORT; GET /healthz and /
bun test test/own-source.test.ts test/bundle-boot.test.ts

Closes #16853

6 earlier comments
commented

The minified bundle now boots from bun build --minify, answers /healthz and /, and the forge no longer reads its own source directories at boot. The bundle's memory budget is lowered. The source's budget could not be, and the bundle isn't doing the same work as the source yet (details below). I ran the tests that cover this change and tsc, and they pass. The forge's full check hasn't run yet.

What changed

  • Build-time lists. scripts/own-source.ts writes src/generated/{migrations,marks,duties,screens}.ts as static imports (duties through lazy require). The migrations (core/schema.ts), route marks (http/route_marks.ts), duties (maintain/registry.ts) and screen palette entries (tui/screens/entries.ts) now boot from those lists instead of readdirSync.
  • Staleness check. test/own-source.test.ts fails when a list no longer matches its directory. AGENTS.md says to run bun scripts/own-source.ts after adding a migration, mark, duty or screen.
  • Moved to helpers. readMigrations and loadDuties now live in test/helpers/read-source-dirs.ts, because tests were their only callers and the dead-code rule would flag them in src/. checkMigrations and rosterOf in src/ hold the shared checks. entryFiles moved to entry_mark.ts and dutyFiles to duty_files.ts.
  • Bundle fixes.
    • Several import.meta.main blocks used top-level await, which a bundle can't require. I wrapped them in async functions in supervisor, release launch/boot, snapshot restore/drill_child, runner/main and chaos/run. Behaviour is the same.
    • kernel/data_home.ts now tolerates a checkout that isn't on disk, which a compiled executable hits.
  • Bundle test and docs. test/bundle-boot.test.ts builds with --minify, boots it from a directory with no src/, and checks /healthz and /. The how-to is docs/features/bundle.md.
  • forge.json is unchanged, so the bundle is an option and source is still the default.

Measurements (Linux, three runs each, bun scripts/boot-measure.ts)

cold startwarm startidle RSS
source5.6 s5.2 / 5.1 s292 MiB
bundle (--minify)2.2 s2.0 / 2.1 s119 MiB
bundle --bytecode (compiled exe)1.7 s1.9 / 2.4 s124 MiB

Not done or not comparable

  • --bytecode can't make a main.js. Without --compile it requires --format=cjs, and main.ts awaits at the top level. The bytecode row is a compiled executable (--compile --bytecode --format esm).
  • The bundle does less than the source.
    • The page workers (pages_worker.ts and other new URL(...) workers) and the docs search index fail with a log line and degrade. Assets built from source are untested in the bundle.
    • Its 119 MiB is therefore not like for like with source. Putting those into the bundle is follow-up work.
  • Budgets. The source budget stays at 400 MiB, since forge.json still runs source and it measures 281 to 292 MiB. I added a 180 MiB budget for the bundle in test/boot-idle-memory.test.ts instead of lowering the source one.
  • Gate and design lint. bun scripts/gate.ts --explain is clean except a duplicate-scan timeout. design-lint wasn't run because nothing under src/web/ changed.

Try it

bun build --minify --target bun --outdir out src/main.ts
bun out/main.js   # with the usual FORGE_DATA and PORT; GET /healthz and /
bun test test/own-source.test.ts test/bundle-boot.test.ts
commented

This push fixes the nine failing tests from the check; the changed tests pass locally and tsc is clean. I did not rerun the full suite, which the forge runs on this push.

  • Stale generated lists: scripts/own-source.ts wrote its comments on single long lines. The forge's rewrap had already wrapped the committed src/generated/*.ts files at 100 columns, so the files never matched what the generator wrote. The generator now emits the wrapped form, which fixes test/own-source.test.ts and test/duty-roster.test.ts. The migration and mark list tests, the migration-pinning tests and the pages-budget setup error were failing because of that stale list. They pass now without further changes.
  • test/launch-no-tla.test.ts: src/generated/duties.ts has to use a literal relative require(). A duty reaches the runner, which imports the roster, so a static import would be a cycle. bun build also only bundles a literal path. I added that one file to an allow-list in the test, with the reason beside it. The test was too broad for this file, not wrong in general. Duties were already loaded with require() before this change.
  • test/worker-database-opens.test.ts: src/generated/screens.ts now imports every screen statically, so the engine Worker reaches src/web/render/sqlite.ts. That file opens one in-memory read-only database through Database.deserialize, on the serving thread behind a request. No Worker calls it. I added it to the REACHED list with that reason. This one is also a test edit, because the old list could not know about the new import path.

Not done: I did not change how duties are loaded. I did not touch the bundle itself or its budget tests.

Try it: bun test test/own-source.test.ts test/duty-roster.test.ts test/launch-no-tla.test.ts test/worker-database-opens.test.ts

🤖 Generated with Claude Code

commented

The stale generated lists were the cause of the new check failure, and I've pushed the fix; I haven't seen the forge's rerun yet. The second failure was test/calm-banners.test.ts. I never reproduced it, but it passes now in the targeted run, along with the other failing tests. tsc --noEmit is clean.

Cause. Main had moved on. The check ran this branch against a newer main, which added a duty file and a migration, so the committed lists in src/generated/duties.ts and src/generated/migrations.ts no longer matched the directories. The same stale lists were behind the duty-roster, migration, pages-budget and egress-banner failures.

Fix. I merged origin/main (clean, no conflicts) and regenerated the lists with bun scripts/own-source.ts.

Not done. I made no code changes beyond that regeneration.

Try it:

bun scripts/own-source.ts --check
bun test test/own-source.test.ts test/duty-roster.test.ts

If main adds more duty files or migrations before this lands, the same lists go stale again. Re-run bun scripts/own-source.ts and commit the result.

🤖 Generated with Claude Code

commented

The failing check is addressed by naming test/bundle-boot.test.ts in SLOW in scripts/gate.ts, with issue #16853 and the reason. I didn't make the test itself faster, and I haven't seen the forge's check run on this push.

The test bundles src/main.ts with bun build --minify and boots the one file with no source beside it. That took 2.57 s against the 2 s per-test budget. The reason in the entry says it runs about 2.6 s a test under full-suite load.

Locally, bun test test/gate passes (93 tests), bun x tsc --noEmit is clean, and bun scripts/gate.ts --fix changed nothing. The gate only noted four files with comment lines over 100 columns, which the nightly rewrap handles. I did not run test/bundle-boot.test.ts itself.

The change touches no page, action, API route or MCP tool, so the both-doors rule and the design lint don't apply. I added no test and no new exports, so the as never and dead-code checks have nothing new to flag.

To try it, run bun test test/bundle-boot.test.ts.

commented

This update tightens the bundle-boot test and defers box 4 (lowering the memory budget). tsc was clean and test/bundle-boot.test.ts passes. I did not run the idle-memory test again after rewording its comment.

Box 4 (memory budget): deferred, not met.

  • IDLE_RSS_BUDGET stays at 400 MiB, because forge.json still runs from source and source idles at about 281–292 MiB. A single constant lowered to the bundle's 118 MiB would fail the source boot.
  • BUNDLE_IDLE_RSS_BUDGET (180 MiB) stays beside it so the bundle is still held to a cap. I reworded its comment to say the two are not like for like, because the bundle leaves out the page workers and the docs search index.
  • Lowering the single constant waits until forge.json runs the bundle.

Box 2 (/ answers with real content): now asserted.

  • test/bundle-boot.test.ts previously only checked that / returned a status below 400.
  • It now fetches / and requires status 200, a text/html content type, a <title>, and a body over 500 characters. It still checks the nosniff header.

No route, input, output or dependency changed, and the README is untouched.

How to try it: run bun test test/bundle-boot.test.ts. It builds the bundle, boots it on empty data, and checks /healthz and /.

commented

I merged main into the branch and pushed it.

The only conflict was in test/boot-idle-memory.test.ts, in the budget constants. main had lowered the source's IDLE_RSS_BUDGET from 400 to 360 MiB. My branch had added BUNDLE_IDLE_RSS_BUDGET (180 MiB) beside the old 400. I kept both: the source stays at main's 360 MiB, and the bundle keeps its separate 180 MiB cap.

tsc --noEmit is clean. test/boot-idle-memory.test.ts and test/bundle-boot.test.ts pass (3 tests). The source forge idled at 268 MiB against 360, and the bundle at 111 MiB against 180. I did not run the full suite. The forge's check runs it on this push.

The box-4 deferral still stands. IDLE_RSS_BUDGET is still a separate constant, because forge.json still runs the source.

commented

Left #16853 open: 1 unticked item remain (Cold start, warm start and idle RSS are measured for source, bundle and bundle --bytecode; numbers go in the pull, and the memory budget constant is lowered to what the cheapest measured.).

Sign in to join this conversation.

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

Keyboard shortcuts

Docs