Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

perf(cli): cut harnessd's memory — smaller young generation, Latin-1 bundle

0xken1516469660b270
diff --git a/cli/build-bundle.mjs b/cli/build-bundle.mjs
index 8af495155..066cdea7e 100644
--- a/cli/build-bundle.mjs
+++ b/cli/build-bundle.mjs
@@ -11,11 +11,12 @@
* `require` via the banner so ws's try/catch fallback works without them.
*/
import * as esbuild from 'esbuild'
-import { appendFileSync, readFileSync, copyFileSync, rmSync } from 'fs'
+import { appendFileSync, readFileSync, copyFileSync, rmSync, writeFileSync } from 'fs'
import { readDshRegistry } from './scripts/lib/dshRegistry.mjs'
import { readBuiltinBundle, readHarnessMonitorBundle, readModelManagerBundle } from './scripts/lib/modelManagerBundle.mjs'
import { readProcessImageBundle } from './scripts/lib/processImageBundle.mjs'
import { leanBlock } from './scripts/lib/leanBlock.mjs'
+import { asciiOnly } from './scripts/lib/asciiOnly.mjs'
import { fileURLToPath } from 'node:url'
import { basename } from 'node:path'
const modelManagerBundle = JSON.stringify(readModelManagerBundle(fileURLToPath(new URL('../store/agents/autonomous-grid', import.meta.url))))
@@ -57,7 +58,7 @@ const options = {
// receives, and the published bundle IS that copy (upload-cli.sh ships `cli.js` and `notify.mjs`,
// nothing else). `legalComments: 'eof'` below appends the dependencies' own notices; this is ours.
banner: {
- js: `/*! harness v${version} — Copyright (c) 2026 Autonomous, Inc. — MIT (https://github.com/autonomous-ai/openharness) */\n`
+ js: `/*! harness v${version} - Copyright (c) 2026 Autonomous, Inc. - MIT (https://github.com/autonomous-ai/openharness) */\n`
+ 'import{createRequire as ___cr}from"module";const require=___cr(import.meta.url);',
},
sourcemap: false,
@@ -95,11 +96,11 @@ const leanCore = await esbuild.build({
write: false,
logLevel: 'warning',
})
-const leanFiles = Object.fromEntries(lean.outputFiles.map((file) => [basename(file.path), file.contents]))
+const leanFiles = Object.fromEntries(lean.outputFiles.map((file) => [basename(file.path), asciiOnly(file.text)]))
for (const file of leanCore.outputFiles) {
const name = basename(file.path)
if (name in leanFiles) throw new Error(`the core's lean file ${name} has the name of one of the master's and the services'`)
- leanFiles[name] = file.contents
+ leanFiles[name] = asciiOnly(file.text)
}
await esbuild.build({
@@ -110,6 +111,7 @@ await esbuild.build({
entryPoints: ['src/entry.ts'],
outfile: `${outDir}/cli.js`,
})
+writeFileSync(`${outDir}/cli.js`, asciiOnly(readFileSync(`${outDir}/cli.js`, 'utf8')))
appendFileSync(`${outDir}/cli.js`, leanBlock(leanFiles))
copyFileSync('hook/notify.mjs', `${outDir}/notify.mjs`)
diff --git a/cli/scripts/lib/asciiOnly.mjs b/cli/scripts/lib/asciiOnly.mjs
new file mode 100644
index 000000000..2f430dedd
--- /dev/null
+++ b/cli/scripts/lib/asciiOnly.mjs
@@ -0,0 +1,18 @@
+/**
+ * V8 keeps a script's whole source in memory, one byte a character only while every character is
+ * Latin-1: one above U+00FF and the whole file takes two. esbuild escapes strings and plain templates
+ * but not regular expressions, tagged templates or the banner, and the `✳ ❯ │ ⌘` the pane parsers
+ * match on made the 5.0 MB cli.js a 9.5 MB string in the core's heap (measured 2026-10-07).
+ *
+ * `\uXXXX` means the same character in a regular expression (an emoji's two escapes are one code
+ * point under `u`, two code units without it, as the literal was) and in a comment. In a tagged
+ * template it changes the raw text; the only ones that carry such characters build RegExp sources
+ * (lib/askQuestion.ts) or a script whose one such character is in a regular expression
+ * (lib/codexStartupRetry.ts), where the escape and the character match alike. Checked 2026-10-07:
+ * all 133 regular expressions in the bundle matched the same as their escaped form on random input.
+ */
+export function asciiOnly(code) {
+ // An escaped character (`\✳`) would become a backslash followed by the text `✳`.
+ if (/\\[^\x00-\xff]/.test(code)) throw new Error('a backslash precedes a character above U+00FF')
+ return code.replace(/[^\x00-\xff]/g, (unit) => `\\u${unit.charCodeAt(0).toString(16).padStart(4, '0')}`)
+}
diff --git a/cli/src/asciiOnly.spec.ts b/cli/src/asciiOnly.spec.ts
new file mode 100644
index 000000000..0eb2a2796
--- /dev/null
+++ b/cli/src/asciiOnly.spec.ts
@@ -0,0 +1,28 @@
+import { describe, expect, it } from 'vitest'
+
+// The bundle's escaping (scripts/lib/asciiOnly.mjs, build-bundle.mjs): what it rewrites must mean what it did.
+// @ts-expect-error — plain ESM with no declaration file
+const { asciiOnly } = await import('../scripts/lib/asciiOnly.mjs') as { asciiOnly: (code: string) => string }
+
+describe('asciiOnly', () => {
+ it('leaves Latin-1, which V8 still stores a byte a character, and escapes the rest', () => {
+ expect(asciiOnly('ß ø © a')).toBe('ß ø © a')
+ expect(asciiOnly('/^[✳❯]/')).toBe('/^[\\u2733\\u276f]/')
+ expect(/[^\x00-\xff]/.test(asciiOnly('— 🎉 │'))).toBe(false)
+ })
+
+ it('keeps what a regular expression matches, with and without u', () => {
+ for (const [source, flags, input] of [
+ ['^[✳✱]+\\s', '', '✳✱ x'], ['^(?:🎉\\s*)?Update', '', '🎉 Update'], ['^(?:🎉\\s*)?Update', 'u', '🎉 Update'],
+ ['[🎉]', 'u', 'a🎉'], ['[\\p{L}\'’-]+', 'u', 'it’s'], ['(?<=…)\\s*\\d', '', '… 5'],
+ ]) {
+ const escaped = new RegExp(asciiOnly(source), flags)
+ expect(escaped.exec(input)).toEqual(new RegExp(source, flags).exec(input))
+ expect(escaped.exec(input)).not.toBeNull()
+ }
+ })
+
+ it('refuses a backslash before such a character, which escaping would change', () => {
+ expect(() => asciiOnly('/\\✳/')).toThrow(/backslash/)
+ })
+})
diff --git a/cli/src/harnessd/master.spec.ts b/cli/src/harnessd/master.spec.ts
index 6f538e6f4..d8aca1559 100644
--- a/cli/src/harnessd/master.spec.ts
+++ b/cli/src/harnessd/master.spec.ts
@@ -53,8 +53,13 @@ describe('the core\'s flags', () => {
})
it('gives the core exactly the heap limit its budget is a share of, or V8\'s own when that is 0', () => {
- expect(coreExecArgv(['--import', 'tsx', '--max-old-space-size=256'], 512)).toEqual(['--import', 'tsx', '--max-old-space-size=512'])
- expect(coreExecArgv(['--max_old_space_size=256'], 0)).toEqual([])
+ expect(coreExecArgv(['--import', 'tsx', '--max-old-space-size=256'], 512))
+ .toEqual(['--import', 'tsx', '--max-semi-space-size=4', '--max-old-space-size=512'])
+ expect(coreExecArgv(['--max_old_space_size=256'], 0)).toEqual(['--max-semi-space-size=4'])
+ })
+
+ it('keeps a young-generation size the master was given', () => {
+ expect(coreExecArgv(['--max_semi_space_size=8'], 512)).toEqual(['--max_semi_space_size=8', '--max-old-space-size=512'])
})
})
@@ -414,7 +419,7 @@ describe('runMaster', { timeout: 60_000 }, () => {
exit: (code) => exits.push(code), onSignal: (signal, listener) => signals.set(signal, listener),
})
await until('the core to be ready', () => readStatusFile(statusFile, process.pid)?.state === 'running')
- expect(JSON.parse(readFileSync(flags, 'utf8'))).toEqual(['--max-old-space-size=300'])
+ expect(JSON.parse(readFileSync(flags, 'utf8'))).toEqual(['--max-semi-space-size=4', '--max-old-space-size=300'])
signals.get('SIGTERM')!()
await until('the master to finish', () => exits.length > 0)
expect(readStatusFile(statusFile, process.pid)).toMatchObject({ state: 'stopped', lastExitReason: 'stopped' })
@@ -475,11 +480,11 @@ describe('runMaster', { timeout: 60_000 }, () => {
await until('the crashed service to be restarted', () => lines().filter((line) => line.role === 'service:search').length === 2)
// The light services share one process, the edge host, which runs each of those named.
await until('the edge host to start', () => lines().some((line) => line.role === 'service:usage,monitor'))
- expect(lines().find((line) => line.role === 'service:usage,monitor')).toMatchObject({ name: 'edge', flags: ['--max-old-space-size=384'] })
+ expect(lines().find((line) => line.role === 'service:usage,monitor')).toMatchObject({ name: 'edge', flags: ['--max-semi-space-size=4', '--max-old-space-size=384'] })
const core = lines().find((line) => line.role === '__run')!
const services = lines().filter((line) => line.role === 'service:search')
expect(services.map((service) => service.restarts)).toEqual(['0', '1'])
- expect(services[0]).toMatchObject({ name: 'search', flags: ['--max-old-space-size=1024'] })
+ expect(services[0]).toMatchObject({ name: 'search', flags: ['--max-semi-space-size=4', '--max-old-space-size=1024'] })
expect(services[0].token).toMatch(/^[0-9a-f]{48}$/)
expect(core.token).toBe(services[0].token)
// The core is told exactly what runs out here, in a form a core from before the list reads too.
diff --git a/cli/src/harnessd/master.ts b/cli/src/harnessd/master.ts
index 7e045efee..4d2f2e491 100644
--- a/cli/src/harnessd/master.ts
+++ b/cli/src/harnessd/master.ts
@@ -132,6 +132,10 @@ export function supervisorOptions(env: NodeJS.ProcessEnv, execArgv: readonly str
/** The flags the core runs with: the master's own, with the heap limit its budget is a share of. */
export function coreExecArgv(execArgv: readonly string[], heapLimitMiB: number): string[] {
const rest = execArgv.filter((flag) => !/^--max[-_]old[-_]space[-_]size=/.test(flag))
+ // Node lets the young generation grow to 16 MiB per semi-space and keeps it: the core sat at 32 MB
+ // of new space holding 2.6 MB, 13 hours in (measured 2026-10-07). 4 MiB returns ~24 MB a process;
+ // scavenges run more often, each as cheap, since what survives one is that same small set.
+ if (!rest.some((flag) => /^--max[-_]semi[-_]space[-_]size=/.test(flag))) rest.push('--max-semi-space-size=4')
return heapLimitMiB > 0 ? [...rest, `--max-old-space-size=${heapLimitMiB}`] : rest
}