Skip to content

πŸ™Œ OpenHands: AI-Driven Development

11.8 KB
Raw Blame
  • Electron desktop app (npm run desktop for dev / npm run build:desktop for the binary) starts the same stack as dev-with-automation.mjs but inside an Electron BrowserWindow. Two gotchas live here:

    1. Boot race vs. agent-server cold start: dev-with-automation.mjs is a fire-and-forget launcher β€” main() previously returned as soon as waitForService saw the ingress proxy respond on /api/health, but ingress responds immediately while the agent-server behind it can still be downloading via uvx (first run pulls ~50 MB of Python + the SDK from PyPI, easily 30–90s). electron/main.mjs used to load the URL as soon as the proxy responded, so the React app booted, called /server_info, and got the "Request timeout" popup. Fix: main() now accepts an agentServerReadyTimeoutMs option and returns { config, agentServerReady }; electron/main.mjs runs a two-stage wait β€” Stage 1 (waitForUrl) confirms the ingress proxy is up, Stage 2 (waitForAgentServer) hits ${ingress}/server_info and only accepts 200 (or 401, which proves the proxy reached a real agent-server) before the BrowserWindow loads. Don't shorten the agent-server timeout below ~3 min β€” uvx cold start on slow connections genuinely takes that long.

    2. First-run feedback loop: dev-with-automation.mjs::setServiceLogListener(cb) exposes a workspace-wide hook that fires cb(name, line, level) for every line of every child-process stdout/stderr/exit. electron/main.mjs::handleServiceLog filters for uvx install lines (Downloading..., Resolved N packages..., etc.) plus agent-server boot markers and forwards them to loading.html via setLoadingStatus() β†’ window.__setLoadingStatus(). The hook is best-effort and swallows listener errors β€” a buggy embedder must not be able to take down the dev stack.

  • Electron desktop packaging β€” electron-builder.config.mjs uses directories.app: "electron" so electron/package.json is the app manifest. Even though electron/package.json has zero dependencies, app-builder-lib's collectNodeModulesWithLogging walks UP from the app dir looking for the first npm workspace that resolves modules. The next dir in line is the project root, where npm list --json reports the full hoisted tree (342 dirs, ~600 MB of Vite/React/Monaco/HeroUI), and electron-builder copies all of it into Resources/app/node_modules/. The walk is hardcoded in app-builder-lib/out/util/appFileCopier.js::collectNodeModulesWithLogging β€” there is no config knob to disable it. Creating an empty electron/node_modules/ does NOT help because the collector falls through to project root when it sees zero deps. The fix is the afterPack hook (stripBundledNodeModules in electron-builder.config.mjs): after electron-builder copies everything, the hook rm -rfs Resources/app/node_modules/ (handling macOS .app bundle layout and Linux/Windows flat resources/ layout), then copies back the dependency closure of RUNTIME_PACKAGES: sirv for static-server.mjs, httpxy for proxy-utils.mjs/ingress.mjs, ws for tunnel-client.mjs (250 KB together), and @openhands/extensions (~6 MB), which no script imports: the launcher points the agent-server's EXTENSIONS_PATH at it so public skills load from disk. Effect: resources/app/ drops from ~598 MB to ~7 MB before the extensions package; total linux-unpacked/ from ~1 GB to ~365 MB (the rest is Electron + Chromium + the bundled uv binary). If a spawned backend script gains a new bare npm import, add the package to RUNTIME_PACKAGES β€” otherwise that service crashes with ERR_MODULE_NOT_FOUND only in the installed app. A package a backend only reads from disk fails quietly instead: without @openhands/extensions the launcher sets no EXTENSIONS_PATH and the agent-server falls back to cloning the extensions repo. Testing trap: an app launched from dist-electron/ inside the repo resolves bare specifiers against the repo's own node_modules (Node ESM resolution walks up from the script file), so a missing runtime package is invisible there β€” verify packaged builds from a copy outside the repo tree (e.g. /Applications). Don't add real deps to electron/package.json β€” any real dep would survive the strip and would also have to be hand-installed inside electron/ since the project root is npm-hoisted. build:desktop:universal and --linux/--win/--mac variants all run the same hook.

  • Electron desktop universal (multi-arch) builds β€” ELECTRON_ARCH=universal npm run build:desktop:universal produces one fat arm64+x64 DMG. The bundled uv/node runtimes are per-arch: the download scripts (arch resolution in scripts/download-arch-utils.mjs::resolveDownloadArches(); any ELECTRON_ARCH value other than unset or universal fails fast) populate resources/{bin,node}-{arm64,x64}/ in universal mode (legacy flat resources/{bin,node}/ otherwise), mac.extraResources ships all four dirs into BOTH packaging passes, and mac.x64ArchFiles whitelists them through the @electron/universal merge β€” the merger requires both pass trees to carry the same Mach-O file list and rejects byte-identical thin Mach-O files unless whitelisted, so bundling only the pass's own arch (e.g. via the ${arch} macro) fails the merge. electron/lib/bundled-runtime-dir.mjs::resolveBundledRuntimeDir() (used by injectBundledUv/injectBundledNode) prefers <Resources>/{bin,node}-<process.arch> β€” under a universal Electron binary process.arch reports the executing slice (arm64 native, x64 under Rosetta) β€” and falls back to the flat dirs. Release CI (desktop-macos.yml) builds universal; PR/manual smoke builds run a two-leg matrix β€” native arm64 on macos-latest and native Intel x64 on macos-15-intel (GitHub's last Intel macOS runner, available until Aug 2027) β€” so the per-arch download + packaging path is exercised on the runner's own CPU. On macos-15-intel (which reports process.arch === "x64"), npm run build:desktop resolves the Intel runtimes into the legacy flat dirs, so that leg also guards the single-arch x64 fallback path.

  • Electron desktop app name in dev (macOS) β€” npm run desktop shows the app as "Electron" in the Dock unless scripts/brand-dev-electron.mjs (wired as the predesktop hook) has run. There are three independent name sources and they must all be set; getting one wrong looks like the fix silently not working. (1) app.name β€” Electron-internal, drives the menu bar, About panel and app.getPath("userData"). It comes from productName in electron/package.json, read by Electron's default_app in dev and lib/browser/init when packaged. Note default_app only reads <arg>/package.json, so npm run desktop must point electron at the electron/ directory β€” electron electron/main.mjs makes it probe electron/main.mjs/package.json, miss, and leave app.name at the host bundle default. (2) CFBundleDisplayName / CFBundleName in the running bundle's Info.plist β€” what lsappinfo and NSRunningApplication.localizedName report. (3) The .app directory name β€” this is what the Dock tooltip actually shows. macOS prefers the bundle's filesystem name over the plist keys; /Applications/DBeaver.app displays as "DBeaver" despite CFBundleName = "DBeaver Community". So patching only the plist is NOT enough β€” the script also renames node_modules/electron/dist/Electron.app β†’ <productName>.app and rewrites node_modules/electron/path.txt to match (getElectronPath() in node_modules/electron/index.js joins path.txt onto dist/ and silently re-downloads Electron 100 MB if it doesn't resolve, so the two must move together). CFBundleExecutable is deliberately left as Electron β€” /Applications/Antigravity.app ships that exact value and still displays correctly, so it only affects ps/Activity Monitor. Editing the plist does not break code signing: Electron's dist is ad-hoc linker-signed (Info.plist=not bound, Sealed Resources=none), so the signature covers only the Mach-O. npm run build:desktop is unaffected by the rename β€” electron-builder packages from `/Library/Caches/electron/electron-v*.zip, never from node_modules/electron/dist. The packaged app never had the problem: electron-builder emits .app` with matching plist keys. Already-running instances keep the name they launched with, so quit and relaunch when verifying.

  • Electron desktop node / npm / npx PATH bridging β€” when the packaged .app is launched from Finder/Spotlight on macOS, the OS gives it a minimal PATH (/usr/bin:/bin). Homebrew, nvm, asdf installs of Node.js are invisible to spawned subprocesses. Two breakages flow from that: (1) backend launcher scripts that do spawn("node", ...) can't find Node; (2) most stdio MCP marketplace entries (Slack, GitHub, Figma, etc.) use command: "npx", and when the agent-server tries to spawn them the missing npx makes the spawn fail with ENOENT β€” the SDK reports it as an error_kind: "connection" MCP test failure, which the install modal renders as MCP$TEST_ERROR_CONNECTION ("Could not reach the server. Check the URL and server type."), a misleading error since no URL is involved. First fix attempt β€” DOES NOT WORK for stdio MCPs: wrap node/npm/npx with thin shell scripts that run Electron with ELECTRON_RUN_AS_NODE=1 against the package's CLI JS. That bridges the ENOENT but stdio JSON-RPC servers spawned through the wrapper exit with McpError: Connection closed before completing the MCP handshake β€” Electron-as-Node has subtly different stdin/stdout pipe semantics from a vanilla node binary when used as a stdio child of a windowed process. Working fix: bundle the real Node.js distribution. scripts/download-node.mjs downloads the official node-v<ver>-<platform>-<arch> tarball from https://nodejs.org/dist/v<ver>/ into resources/node/ (gitignored), prunes include/, share/, docs, and node_modules/corepack to keep the size down (~130 MB on Linux x64, dominated by the Node binary itself). Default pin: NODE_BUNDLE_VERSION = "24.21.0" (aligned with the volta pin in package.json and the engines.node >=24 requirement); override with NODE_VERSION=. electron-builder.config.mjs ships resources/node/ as an extraResource β†’ <Resources>/node/. electron/main.mjs::injectBundledNode() prepends the platform-appropriate bin dir to PATH (POSIX: <Resources>/node/bin; Windows: <Resources>/node/) so subsequent spawns of node/npm/npx resolve to real binaries with full stdio fidelity. It also chmod +x's the binaries on POSIX because electron-builder doesn't always preserve the bit. injectBundledNode() is a no-op when !app.isPackaged (dev npm run desktop uses the developer's system node). build:desktop and build:desktop:universal both run download-node.mjs after download-uv.mjs. If the bundled dir is missing at runtime, injectBundledNode() logs a loud [desktop] warning instead of silently leaving PATH bare. extraResources will not copy the distribution's root node_modules: app-builder-lib/src/util/filter.ts::createFilter returns false for any entry whose path relative to the copy root is exactly node_modules, before the filter patterns are consulted, so no filter value can opt back in. The Windows Node zip puts npm at <root>/node_modules/npm and hits this exactly; POSIX tarballs put it at <root>/lib/node_modules/npm and are unaffected β€” which is why this only broke Windows. Shipped result: a working node.exe beside npm.cmd/npx.cmd shims pointing at a missing node_modules\npm\bin\npx-cli.js, so every npx -y <pkg> spawn dies with MODULE_NOT_FOUND and shadows the user's own npm, since the dir is PREPENDED to PATH. The afterPack hook (restoreBundledNodeNpm) copies that directory into the packed output and then hard-fails the build if npm-cli.js still isn't there, mirroring the check download-node.mjs already runs on the source tree.