codeburn/src/cli.ts
Giovannini Barbosa 720824e619 fix(cli): default NODE_ENV to production so Ink's React build stops leaking user-timing entries.
React, loaded by Ink, picks its development build unless NODE_ENV is
exactly "production". Since React 19.2 that build records a
performance.measure() entry on every render (its DevTools "Scheduler"
and "Components" tracks), and Node keeps user-timing marks and measures
in a process-wide buffer that nothing ever trims. The interactive
dashboard re-renders on every progress tick and refresh, so the buffer
grows by roughly 700 entries a minute; after a few days the heap hits
V8's 4 GB default and the process aborts with "Ineffective mark-compacts
near heap limit ... JavaScript heap out of memory".

The launcher (dist/cli.js, the entry every client spawns) now defaults
NODE_ENV to "production" before importing main.js, which selects the
build that records nothing; an explicit NODE_ENV still wins. For that
explicit case, a small guard drops the user-timing buffer every 30 s for
the lifetime of each Ink UI (dashboard, sessions, context, compare).
codeburn records no marks or measures of its own, so nothing is lost.

Tests spawn the real launcher against a stub main.js to pin the default,
and drive a ticking Ink app in a child Node process to show the
development build's backlog, the production build's absence of one, and
the guard keeping it bounded.
2026-09-07 10:27:46 -03:00

24 lines
1 KiB
JavaScript

#!/usr/bin/env node
// This launcher must stay parseable by Node 18. Do NOT add static imports.
const [major, minor] = process.versions.node.split('.').map(Number)
if (major < 22 || (major === 22 && minor < 13)) {
process.stderr.write(
`codeburn requires Node.js >= 22.13.0 (current: ${process.version})\n` +
'Upgrade at https://nodejs.org/\n',
)
process.exit(1)
}
// React (loaded by Ink) picks its development build unless NODE_ENV is exactly
// "production". That build records a performance.measure() entry on every
// render into Node's user-timing buffer, which nothing ever trims, so an
// interactive dashboard left open for days grows until V8 aborts with
// "JavaScript heap out of memory". Default to the production build; an
// explicit NODE_ENV in the shell still wins. This must run before main.js is
// imported because React reads NODE_ENV at module load.
if (!process.env.NODE_ENV) process.env.NODE_ENV = 'production'
import('./main.js').catch((err) => {
process.stderr.write(String(err?.message ?? err) + '\n')
process.exit(1)
})