mirror of
https://github.com/NeuralNomadsAI/CodeNomad.git
synced 2026-11-17 05:49:28 +00:00
## Summary
Electron already ships the Node runtime the backend needs. The Electron
package now launches the CodeNomad server through `process.execPath`
with `ELECTRON_RUN_AS_NODE=1` and keeps only npm (from the pinned
`.node-version` archive), dropping the redundant standalone `node.exe`
(~89 MiB on Windows). Tauri keeps its full bundled runtime unchanged.
## Changes
- **Launch**: `process-manager.ts` runs the backend with Electron's own
executable in run-as-Node mode; `managed-node.ts` is removed.
- **Resources**: `prepare-node-runtime.cjs` gains an npm-only layout
requested by `electron-app/scripts/prepare-resources.js`; Tauri keeps
the full Node + npm layout.
- **npm discovery**: `bundledNpm()` (`npm-runtime.ts`) finds npm in
Electron resources (Windows, Linux, macOS `.app/Contents/Resources`)
while preserving Node/Tauri layouts.
- **npm lifecycle scripts**: `@opencode/cli` runs `node
./postinstall.mjs`; installation (`shared-installation.ts`) and native
upgrades (`native-upgrade.ts`) get a private, scoped `node` command
pointing at the Electron executable, removed afterwards. The shared
OpenCode daemon never inherits `ELECTRON_RUN_AS_NODE`.
- **Checks/CI**: packaged-resource smoke, `verify-bundled-node`,
`build.js` and `build-and-upload.yml` separate the Electron (npm-only)
and Tauri contracts; `test-opencode-*-native.mjs` and the desktop setup
qualification can target the packaged Electron runtime without a system
Node on PATH.
- **Docs**: `dev-docs/RUNTIME_MAINTENANCE.md` (the Electron package
follows Electron's Node version), `dev-docs/OPENCODE_RUNTIME_SETUP.md`,
and `server.md` / `updates.md` help pages in all ten locales.
## Validation
- Electron 39.0.0 → Node 22.20.0; npm 11.19.0 (engines `^20.17.0 ||
>=22.9.0`).
- Packaged Windows ZIP and Linux (`linux-unpacked` + extracted DEB,
Ubuntu 24.04 VM): backend reaches ready, API answers 200, both backend
processes run as the app executable with `ELECTRON_RUN_AS_NODE=1`, no
Node in resources or on PATH, clean exit with no orphan and the
executable released.
- `test-opencode-install-native` (2.2.3), `test-opencode-upgrade-native`
(→ 2.2.5, isolated service PID kept) and
`test-opencode-legacy-replacement-native` (1.18.10 → 2.2.5) pass on
Windows and Linux with the packaged Electron runtime.
- Typechecks (server, Electron), resource tests 11/11, `opencode-update`
tests (Windows 66 pass / 1 POSIX skip; Linux 62 pass / 5 Windows skips),
help validation.
- The full Electron suite on Windows shows intermittent failures in
`process-stop.test.ts` and `native-service-start.test.ts` (PowerShell
capture timeout of 1.5 s); they reproduce identically on `dev`
(30527e58d) and the files are unchanged here.
## Size (same machine and options)
| | Before | After | Delta |
|---|---:|---:|---:|
| Windows `resources` | 344 274 218 B | 250 740 778 B | −89.20 MiB |
| Windows ZIP | 221 737 165 B | 188 286 779 B | −31.90 MiB |
| Linux `resources` | 377 016 175 B | 250 408 680 B | −120.74 MiB |
| Linux DEB | 138 393 810 B | 107 123 634 B | −29.82 MiB |
macOS was not exercised at runtime (path handling is covered by unit
tests; arm64/x64 builds run in CI).
90 lines
6 KiB
JavaScript
90 lines
6 KiB
JavaScript
// Standard npm --global install into a fresh synthetic user's prefix.
|
|
// No real user PATH/registry/profile changes and no shared daemon.
|
|
import assert from "node:assert/strict"
|
|
import { mkdtemp, mkdir, cp, copyFile, chmod, rm } from "node:fs/promises"
|
|
import path from "node:path"
|
|
import os from "node:os"
|
|
import { execFileSync, spawn } from "node:child_process"
|
|
import { once } from "node:events"
|
|
import { tsImport } from "tsx/esm/api"
|
|
import { fixtureRuntime } from "./opencode-fixture-runtime.mjs"
|
|
const { installSharedOpenCode, resolveDefaultInstallation, findPathOpenCode } = await tsImport("../packages/server/src/opencode-update/shared-installation.ts", import.meta.url)
|
|
const { registerUserPath } = await tsImport("../packages/server/src/opencode-update/user-path.ts", import.meta.url)
|
|
const { MINIMUM_OPENCODE_VERSION } = await tsImport("../packages/server/src/opencode/runtime-support.ts", import.meta.url)
|
|
const parent = path.join(os.tmpdir(), "opencode")
|
|
await mkdir(parent, { recursive: true })
|
|
const root = await mkdtemp(path.join(parent, "codenomad-install-native-"))
|
|
const runtime = path.join(root, "runtime")
|
|
await mkdir(runtime)
|
|
const selected = await fixtureRuntime(root)
|
|
const node = selected.electron ? selected.node : path.join(runtime, process.platform === "win32" ? "node.exe" : "bin/node")
|
|
if (!selected.electron) {
|
|
await mkdir(path.dirname(node), { recursive: true })
|
|
await copyFile(selected.node, node)
|
|
await chmod(node, 0o755)
|
|
const npmRoot = path.resolve(selected.npm, "../..")
|
|
await cp(npmRoot, path.join(runtime, process.platform === "win32" ? "node_modules/npm" : "lib/node_modules/npm"), { recursive: true })
|
|
}
|
|
const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !/^(PATH|OPENCODE_|XDG_|npm_)/i.test(key)))
|
|
// npm invokes the OS shell for package lifecycle scripts; provide that shell
|
|
// explicitly without admitting /usr/bin/node or any other system Node to PATH.
|
|
Object.assign(env, { HOME: root, USERPROFILE: root, LOCALAPPDATA: root, APPDATA: path.join(root, "AppData"),
|
|
ZDOTDIR: root, SHELL: process.platform === "darwin" ? "/bin/zsh" : "/bin/bash",
|
|
npm_config_cache: path.join(root, "npm-cache"), npm_config_userconfig: path.join(root, "npmrc"),
|
|
...selected.env, PATH: process.platform === "win32" ? `${process.env.SystemRoot}\\System32` : path.join(root, "tools") })
|
|
try {
|
|
let registered = false
|
|
const options = { home: root, node, ...(selected.electron ? { npm: selected.npm } : {}), env, registerPath: directory => registerUserPath(directory, {
|
|
home: root, env, registerWindowsPath: async () => { registered = true },
|
|
}) }
|
|
const binary = await installSharedOpenCode(MINIMUM_OPENCODE_VERSION, options)
|
|
assert.equal(resolveDefaultInstallation(options).source, "path")
|
|
assert.equal(resolveDefaultInstallation(options).path, findPathOpenCode(options))
|
|
if (process.platform === "win32") assert.equal(registered, true)
|
|
assert.match(execFileSync(binary, ["--version"], { encoding: "utf8", env }), new RegExp(MINIMUM_OPENCODE_VERSION.replaceAll(".", "\\.")))
|
|
const terminalVersion = process.platform === "win32"
|
|
? execFileSync(process.env.ComSpec || "C:\\Windows\\System32\\cmd.exe", ["/d", "/s", "/c", "opencode2 --version"], { cwd: root, encoding: "utf8", env })
|
|
: execFileSync("opencode2", ["--version"], { cwd: root, encoding: "utf8", env })
|
|
assert.match(terminalVersion, new RegExp(MINIMUM_OPENCODE_VERSION.replaceAll(".", "\\.")))
|
|
const commandDirectory = path.dirname(findPathOpenCode(options))
|
|
const removeCommands = () => Promise.all(["opencode", "opencode2"].flatMap(name =>
|
|
(process.platform === "win32" ? ["", ".cmd", ".ps1"] : [""]).map(extension =>
|
|
rm(path.join(commandDirectory, name + extension), { force: true }))))
|
|
await removeCommands()
|
|
await installSharedOpenCode(MINIMUM_OPENCODE_VERSION, options)
|
|
assert.ok(findPathOpenCode(options), "same-version npm install repairs missing terminal commands")
|
|
if (process.platform === "win32") {
|
|
// A private foreground server maps the real OpenCode image for Electron;
|
|
// standalone Node also supports the small mapped-executable fixture.
|
|
const backup = path.join(root, "original-opencode.exe")
|
|
if (!selected.electron) {
|
|
await copyFile(binary, backup)
|
|
await copyFile(node, binary)
|
|
}
|
|
const child = spawn(binary, selected.electron ? ["serve", "--hostname", "127.0.0.1", "--port", "0"]
|
|
: ["-e", "console.log('ready'); setInterval(() => {}, 1000)"], { env, windowsHide: true, stdio: ["ignore", "pipe", "pipe"] })
|
|
try {
|
|
await once(child.stdout, "data")
|
|
// Same-version repair still uses direct npm/preflight. Missing launchers
|
|
// force that write path instead of the already-satisfied no-op path.
|
|
await removeCommands()
|
|
let executed = false
|
|
await assert.rejects(installSharedOpenCode(MINIMUM_OPENCODE_VERSION, { ...options,
|
|
probe: async () => ({ valid: true, version: MINIMUM_OPENCODE_VERSION }),
|
|
execute: async () => { executed = true },
|
|
}), error => error.code === "installation_in_use")
|
|
assert.equal(executed, false, "npm must not retire the live package")
|
|
assert.equal(child.exitCode, null, "installation never stops a running executable")
|
|
} finally {
|
|
const closed = once(child, "close")
|
|
child.kill()
|
|
await closed
|
|
if (!selected.electron) await copyFile(backup, binary)
|
|
}
|
|
await installSharedOpenCode(MINIMUM_OPENCODE_VERSION, options)
|
|
assert.ok(findPathOpenCode(options), "repair succeeds after the mapped fixture executable exits")
|
|
assert.match(execFileSync(binary, ["--version"], { encoding: "utf8", env }), new RegExp(MINIMUM_OPENCODE_VERSION.replaceAll(".", "\\.")))
|
|
console.log("PASS: Windows live-executable repair deferred before npm; original installation retained")
|
|
}
|
|
console.log(`PASS: shared user npm installation with an isolated ${selected.electron ? "Electron" : "Node"} runtime, no system Node PATH; backend and terminal both resolve ${MINIMUM_OPENCODE_VERSION}`)
|
|
} finally { await rm(root, { recursive: true, force: true }) }
|