Trim relocated daemon-host copy and prune on every launch

The relocated daemon host copied the full win-unpacked runtime (~435MB). A
windowless run-as-node host loads none of the top-level GPU/graphics DLLs
(swiftshader, vulkan, d3d, dxcompiler, ffmpeg) and none of node-pty's .pdb
debug symbols or other-arch prebuilds — verified against the live daemon's
loaded-module list. Stop copying them: ~435MB -> ~260MB, exe and data blobs
untouched so per-file signatures are preserved.

Also run the old-version host-dir prune on every launch instead of only on a
fresh daemon spawn. Surviving daemons make spawns rare, so a spawn-only sweep
let superseded copies accumulate; current + live-daemon-pinned versions stay.
This commit is contained in:
Jinwoo Hong
2026-07-07 14:08:53 -04:00
parent 5ffaeb129c
commit 0cc8d5d055
3 changed files with 72 additions and 38 deletions
+36 -14
View File
@@ -61,8 +61,13 @@ function buildInstallFixture(root: string): void {
const nativeDir = join(root, 'resources', 'node_modules', 'node-pty', 'build', 'Release')
mkdirSync(nativeDir, { recursive: true })
writeFileSync(join(nativeDir, 'conpty.node'), 'native')
writeFileSync(join(nativeDir, 'conpty.pdb'), 'debug-symbols')
mkdirSync(join(nativeDir, 'conpty'), { recursive: true })
writeFileSync(join(nativeDir, 'conpty', 'conpty.dll'), 'conpty-dll')
// Other-arch prebuild the x64 host never loads — must be filtered from the copy.
const arm64Dir = join(root, 'resources', 'node_modules', 'node-pty', 'prebuilds', 'win32-arm64')
mkdirSync(arm64Dir, { recursive: true })
writeFileSync(join(arm64Dir, 'pty.node'), 'arm64-prebuild')
}
beforeEach(() => {
@@ -91,18 +96,15 @@ afterEach(() => {
})
describe('buildDaemonHostManifest', () => {
it('mirrors the win-unpacked layout: root exe/blobs/dlls, resources tree verbatim', () => {
it('mirrors the win-unpacked layout: exe + data blobs + resources tree, no GPU DLLs', () => {
const appDir = 'C:\\app'
const ops = buildDaemonHostManifest(
{
appDir,
execPath: 'C:\\app\\Orca.exe',
resourcesPath: 'C:\\app\\resources',
entrySourcePath: 'C:\\app\\resources\\app.asar.unpacked\\out\\main\\daemon-entry.js',
entryRelPath: 'resources/app.asar.unpacked/out/main/daemon-entry.js'
},
['ffmpeg.dll', 'libEGL.dll']
)
const ops = buildDaemonHostManifest({
appDir,
execPath: 'C:\\app\\Orca.exe',
resourcesPath: 'C:\\app\\resources',
entrySourcePath: 'C:\\app\\resources\\app.asar.unpacked\\out\\main\\daemon-entry.js',
entryRelPath: 'resources/app.asar.unpacked/out/main/daemon-entry.js'
})
const byDest = new Map(ops.map((op) => [op.destRel, op]))
// The host exe is renamed to a distinct image name (NOT the source basename)
// so the NSIS updater's name-based `taskkill /IM Orca.exe` can't kill it.
@@ -110,13 +112,20 @@ describe('buildDaemonHostManifest', () => {
expect(byDest.has('Orca.exe')).toBe(false)
const exeOp = ops.find((op) => op.sourcePath === 'C:\\app\\Orca.exe')
expect(exeOp?.destRel).not.toBe('Orca.exe')
// V8/ICU data blobs are read by the Electron bootstrap and kept.
expect(byDest.has('icudtl.dat')).toBe(true)
expect(byDest.has('ffmpeg.dll')).toBe(true)
expect(byDest.has('libEGL.dll')).toBe(true)
// GPU/graphics DLLs are never loaded by the windowless host, so not copied.
expect(byDest.has('ffmpeg.dll')).toBe(false)
expect(byDest.has('libEGL.dll')).toBe(false)
// Daemon bundle + node-pty mirrored at their real resources-relative paths.
expect(byDest.get('resources/app.asar.unpacked/out/main/daemon-entry.js')?.kind).toBe('file')
expect(byDest.get('resources/app.asar.unpacked/out/main/chunks')?.kind).toBe('dir')
expect(byDest.get('resources/node_modules/node-pty')?.kind).toBe('dir')
// node-pty is copied with a filter dropping .pdb + other-arch prebuilds.
const nodePtyOp = byDest.get('resources/node_modules/node-pty')
expect(nodePtyOp?.kind).toBe('dir')
expect(nodePtyOp?.filter?.('node-pty/build/Release/conpty.node')).toBe(true)
expect(nodePtyOp?.filter?.('node-pty/build/Release/conpty.pdb')).toBe(false)
expect(nodePtyOp?.filter?.('node-pty/prebuilds/win32-arm64/pty.node')).toBe(false)
})
})
@@ -140,6 +149,19 @@ describe('materializeRelocatedDaemonHost', () => {
expect(
existsSync(join(dest, 'resources', 'app.asar.unpacked', 'out', 'main', 'chunks', 'a.js'))
).toBe(true)
// Trim: GPU DLLs, .pdb debug symbols, and other-arch prebuilds are excluded.
expect(existsSync(join(dest, 'ffmpeg.dll'))).toBe(false)
expect(existsSync(join(dest, 'libEGL.dll'))).toBe(false)
expect(
existsSync(
join(dest, 'resources', 'node_modules', 'node-pty', 'build', 'Release', 'conpty.pdb')
)
).toBe(false)
expect(
existsSync(
join(dest, 'resources', 'node_modules', 'node-pty', 'prebuilds', 'win32-arm64', 'pty.node')
)
).toBe(false)
// Marker records the version + entry rel path, written into the published dir.
const marker = JSON.parse(readFileSync(join(dest, '.materialized.json'), 'utf8'))
expect(marker.version).toBe('9.9.9')
+32 -22
View File
@@ -63,6 +63,8 @@ type CopyOp = {
kind: 'file' | 'dir'
/** When true, a missing source is skipped rather than failing the copy. */
optional?: boolean
/** Per-source-path predicate for dir copies: return false to skip a path. */
filter?: (sourcePath: string) => boolean
}
type DaemonHostSources = {
@@ -123,12 +125,13 @@ function collectDaemonHostSources(): DaemonHostSources | null {
}
}
function listTopLevelDlls(appDir: string): string[] {
try {
return readdirSync(appDir).filter((name) => name.toLowerCase().endsWith('.dll'))
} catch {
return []
}
// node-pty ships debug symbols (.pdb) and prebuilds for other CPU arches; the
// run-as-node daemon loads neither (verified against the live daemon's loaded
// module list), so they are filtered out of the copy — the bulk of node-pty's
// on-disk size — leaving only the arch-native binaries the host actually loads.
function isRuntimeNodePtyPath(sourcePath: string): boolean {
const p = sourcePath.toLowerCase()
return !p.endsWith('.pdb') && !p.includes('win32-arm64')
}
/**
@@ -137,26 +140,21 @@ function listTopLevelDlls(appDir: string): string[] {
* identically to the packaged app. Pure over its inputs so tests can assert the
* layout without a real build.
*/
export function buildDaemonHostManifest(
sources: DaemonHostSources,
topLevelDlls: string[]
): CopyOp[] {
export function buildDaemonHostManifest(sources: DaemonHostSources): CopyOp[] {
const { appDir, execPath, resourcesPath, entrySourcePath, entryRelPath } = sources
const ops: CopyOp[] = []
// Electron host binary + runtime data blobs at the dest root. The exe is
// Electron host binary + V8/ICU data blobs at the dest root. The exe is
// renamed to a distinct image name so the NSIS updater's name-based
// `taskkill /IM Orca.exe` can't match it; the blobs/DLLs beside it are read by
// the Electron bootstrap by fixed names, so they keep their original names.
// `taskkill /IM Orca.exe` can't match it; the blobs beside it are read by the
// Electron bootstrap by fixed name. Top-level DLLs are deliberately NOT copied
// — they are all GPU/graphics/media (swiftshader, vulkan, d3d, dxcompiler,
// ffmpeg) that a windowless run-as-node host never loads (verified empirically
// against the live daemon's module list), so copying them only wastes ~48MB.
ops.push({ sourcePath: execPath, destRel: DAEMON_HOST_EXE_NAME, kind: 'file' })
for (const name of RUNTIME_DATA_FILES) {
ops.push({ sourcePath: join(appDir, name), destRel: name, kind: 'file', optional: true })
}
// All top-level runtime DLLs: exe size dominates the copy, so carry them all
// rather than guess which the run-as-node host loads.
for (const name of topLevelDlls) {
ops.push({ sourcePath: join(appDir, name), destRel: name, kind: 'file' })
}
// Daemon bundle: entry + its sibling chunks/ + the unpacked out/package.json
// (CJS/ESM loader resolution), mirrored verbatim.
@@ -178,9 +176,15 @@ export function buildDaemonHostManifest(
// node-pty package tree (native conpty.node + conpty/ runtime dir). It is a
// sibling of app.asar.unpacked; require('node-pty') resolves it by walking up
// from the mirrored daemon-entry dir to resources/node_modules.
// from the mirrored daemon-entry dir to resources/node_modules. Filtered to
// drop .pdb debug symbols and other-arch prebuilds the host never loads.
const nodePtyDir = join(resourcesPath, 'node_modules', 'node-pty')
ops.push({ sourcePath: nodePtyDir, destRel: toPosixRelative(appDir, nodePtyDir), kind: 'dir' })
ops.push({
sourcePath: nodePtyDir,
destRel: toPosixRelative(appDir, nodePtyDir),
kind: 'dir',
filter: isRuntimeNodePtyPath
})
return ops
}
@@ -195,8 +199,14 @@ function executeManifest(ops: CopyOp[], stagingRoot: string): void {
}
const dest = destPath(stagingRoot, op.destRel)
mkdirSync(dirname(dest), { recursive: true })
const { filter } = op
// Dereference symlinks so the copy holds no link back into the install dir.
cpSync(op.sourcePath, dest, { recursive: op.kind === 'dir', dereference: true, force: true })
cpSync(op.sourcePath, dest, {
recursive: op.kind === 'dir',
dereference: true,
force: true,
...(filter ? { filter: (src: string) => filter(src) } : {})
})
}
}
@@ -269,7 +279,7 @@ export function materializeRelocatedDaemonHost(): RelocatedDaemonHost | null {
try {
mkdirSync(root, { recursive: true })
rmSync(staging, { recursive: true, force: true })
executeManifest(buildDaemonHostManifest(sources, listTopLevelDlls(sources.appDir)), staging)
executeManifest(buildDaemonHostManifest(sources), staging)
// Marker written LAST: an interrupted copy leaves a marker-less staging dir
// that the next launch discards, never a dest the cheap check trusts.
const marker: MaterializeMarker = {
+4 -2
View File
@@ -288,8 +288,6 @@ function createOutOfProcessLauncher(runtimeDir: string): DaemonLauncher {
// so the one-time copy stays off the first-paint path and is skipped on
// launches that adopt a live daemon. Fail-open: null → in-dir host, below.
const relocatedHost = materializeRelocatedDaemonHost()
// Reclaim old version host dirs no surviving daemon still pins (best-effort).
pruneOldDaemonHosts(collectPinnedDaemonVersions(runtimeDir))
// Fork the relocated entry when available; otherwise the install-dir entry.
const forkEntryPath = relocatedHost ? relocatedHost.entryPath : entryPath
const child = fork(
@@ -434,6 +432,10 @@ export async function initDaemonPtyProvider(signal?: AbortSignal): Promise<void>
// throws, a stale spawner would prevent shutdownDaemon() from cleaning up
// correctly on retry.
const info = await newSpawner.ensureRunning()
// Reclaim superseded daemon-host copies on EVERY launch, not just on a fresh
// spawn: surviving daemons make spawns rare, so a spawn-only sweep would let
// old-version copies accumulate. Current + live-daemon-pinned versions stay.
pruneOldDaemonHosts(collectPinnedDaemonVersions(runtimeDir))
const launchMode = newSpawner.getHandle()?.mode
logDaemonMilestone('daemon-current-ready')
if (signal?.aborted) {