Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(pty): detect cross-arch spawn-helper clobber + always rebuild after release
enes84217d62654e
diff --git a/package.json b/package.jsonindex abec8c858..113fd6bab 100644--- a/package.json+++ b/package.json@@ -18,7 +18,7 @@"make-icon": "node scripts/make-icon.mjs","make-social": "node scripts/make-social.mjs","dist": "npm run make-icon && electron-vite build && electron-builder --mac --arm64 -c.mac.identity=null -c.mac.notarize=false",- "release": "npm run make-icon && electron-vite build && electron-builder --mac --arm64 --x64 --publish never && npm run rebuild",+ "release": "npm run make-icon && electron-vite build && sh -c 'electron-builder --mac --arm64 --x64 --publish never; s=$?; npm run rebuild; exit $s'","postinstall": "electron-rebuild -f -w node-pty"},"build": {diff --git a/src/main/macho-arch.test.ts b/src/main/macho-arch.test.tsnew file mode 100644index 000000000..a9f333d45--- /dev/null+++ b/src/main/macho-arch.test.ts@@ -0,0 +1,38 @@+import { describe, it, expect } from 'vitest'+import { machOArch, archMismatch } from './macho-arch'++// On-disk little-endian Mach-O 64 header: magic cf fa ed fe, then cputype (LE int32).+function header(cpuTypeLE: number[]): Buffer {+ return Buffer.from([0xcf, 0xfa, 0xed, 0xfe, ...cpuTypeLE])+}++describe('machOArch', () => {+ it('identifies arm64 (cputype 0x0100000c)', () => {+ expect(machOArch(header([0x0c, 0x00, 0x00, 0x01]))).toBe('arm64')+ })+ it('identifies x86_64 (cputype 0x01000007)', () => {+ expect(machOArch(header([0x07, 0x00, 0x00, 0x01]))).toBe('x86_64')+ })+ it('identifies a fat/universal binary (cafebabe big-endian)', () => {+ expect(machOArch(Buffer.from([0xca, 0xfe, 0xba, 0xbe, 0, 0, 0, 2]))).toBe('universal')+ })+ it('returns unknown for short or non-Mach-O input', () => {+ expect(machOArch(Buffer.from([1, 2, 3]))).toBe('unknown')+ expect(machOArch(Buffer.from('#!/bin/sh\n'))).toBe('unknown')+ })+})++describe('archMismatch', () => {+ it('flags an x86_64 helper in an arm64 process (the release-clobber incident)', () => {+ expect(archMismatch('x86_64', 'arm64')).toBe(true)+ })+ it('flags an arm64 helper in an x64 process', () => {+ expect(archMismatch('arm64', 'x64')).toBe(true)+ })+ it('accepts matching arch and never flags universal/unknown', () => {+ expect(archMismatch('arm64', 'arm64')).toBe(false)+ expect(archMismatch('x86_64', 'x64')).toBe(false)+ expect(archMismatch('universal', 'arm64')).toBe(false)+ expect(archMismatch('unknown', 'arm64')).toBe(false)+ })+})diff --git a/src/main/macho-arch.ts b/src/main/macho-arch.tsnew file mode 100644index 000000000..f58403971--- /dev/null+++ b/src/main/macho-arch.ts@@ -0,0 +1,32 @@+// Tiny Mach-O header reader: enough to tell which CPU architecture a binary was built for.+// Used to diagnose the recurring field failure where a cross-arch `electron-builder --x64` run+// clobbers node-pty's `spawn-helper` in node_modules with an x86_64 build, after which every+// pty spawn on an arm64 app fails with an opaque "posix_spawnp failed.".++const MH_MAGIC_64 = 0xfeedfacf // little-endian on-disk bytes: cf fa ed fe+const FAT_MAGIC = 0xcafebabe // universal (fat) binary, big-endian header+const CPU_TYPE_X86_64 = 0x01000007+const CPU_TYPE_ARM64 = 0x0100000c++export type MachOArch = 'arm64' | 'x86_64' | 'universal' | 'unknown'++/** Architecture of a Mach-O binary from its first 8 bytes. 'universal' = fat binary (any arch). */+export function machOArch(header: Buffer): MachOArch {+ if (header.length < 8) return 'unknown'+ if (header.readUInt32BE(0) === FAT_MAGIC) return 'universal'+ if (header.readUInt32LE(0) !== MH_MAGIC_64) return 'unknown'+ const cpuType = header.readUInt32LE(4)+ if (cpuType === CPU_TYPE_ARM64) return 'arm64'+ if (cpuType === CPU_TYPE_X86_64) return 'x86_64'+ return 'unknown'+}++/**+ * True when a binary of `arch` cannot run natively in this process's architecture — i.e. the+ * spawn-helper was clobbered by a cross-arch build. Universal/unknown never flag (fail open).+ */+export function archMismatch(arch: MachOArch, processArch: NodeJS.Architecture): boolean {+ if (arch === 'arm64') return processArch !== 'arm64'+ if (arch === 'x86_64') return processArch !== 'x64'+ return false+}diff --git a/src/main/pty-manager.ts b/src/main/pty-manager.tsindex 1bb5cd861..d3350a60f 100644--- a/src/main/pty-manager.ts+++ b/src/main/pty-manager.ts@@ -22,6 +22,7 @@ import {} from './remote-ssh/control-master'import { TMUX_SOCKET, sessionName } from './tmux-naming'import { releasePty, type ReleasablePty } from './pty-release'+import { machOArch, archMismatch } from './macho-arch'import { writeScrollback, readScrollback, deleteScrollback } from './scrollback-store'import { claudeConfigDirFor } from './claude-accounts'import { AUTH_ENV_STRIP, accountTmuxEnvArgs, remoteAccountConfigDirAbs } from './claude-accounts-core'@@ -84,6 +85,39 @@ function findTmux(): string | null {/** Resolve an absolute ssh path (GUI apps don't inherit the shell PATH). */let cachedSsh: string | null | undefined+/**+ * macOS-only diagnostic for the recurring release-clobber incident: `electron-builder --x64`+ * rebuilds node-pty IN PLACE in node_modules, leaving an x86_64 spawn-helper that an arm64 app+ * cannot posix_spawn — every terminal then fails with an opaque "posix_spawnp failed.". Returns+ * the precise message (with the `npm run rebuild` remedy) when the helper's arch mismatches this+ * process, else null. Fail-open: any read/parse problem returns null (diagnostics only).+ */+function spawnHelperArchMismatch(): string | null {+ if (os.platform() !== 'darwin') return null+ try {+ const helper = path.join(+ path.dirname(require.resolve('node-pty/package.json')),+ 'build',+ 'Release',+ 'spawn-helper'+ )+ const fd = fs.openSync(helper, 'r')+ const buf = Buffer.alloc(8)+ fs.readSync(fd, buf, 0, 8, 0)+ fs.closeSync(fd)+ const arch = machOArch(buf)+ if (archMismatch(arch, process.arch)) {+ return (+ `node-pty's spawn-helper is ${arch} but this app is ${process.arch} — a cross-arch ` ++ `release build clobbered node_modules. Fix: run \`npm run rebuild\`, then restart the app.`+ )+ }+ } catch {+ /* diagnostics only — never mask the real spawn error */+ }+ return null+}+function findSsh(): string | null {if (cachedSsh !== undefined) return cachedSshtry {@@ -652,19 +686,19 @@ export class PtyManager {env})} catch (err) {- // node-pty surfaces the underlying failure as a bare "posix_spawnp failed." with no errno,- // so we can't tell EMFILE/EAGAIN/ENOMEM apart here. In practice the dominant cause is THIS- // process running out of file descriptors (macOS gives GUI apps a soft limit of only 256;- // a field incident showed 253 fds used while the system PTY cap sat at 149/511). Re-throw- // with the resolved program + cwd + live-PTY count so the renderer surfaces an actionable- // message, and the caller (create) rejects cleanly rather than leaving a blank node.+ // node-pty surfaces the underlying failure as a bare "posix_spawnp failed." with no errno.+ // The recurring field cause is a cross-arch `electron-builder --x64` run clobbering+ // node-pty's spawn-helper in node_modules (arm64 app can't exec an x86_64 helper), so+ // check the helper's Mach-O arch first and name the exact remedy when it mismatches.const openPtys = this.sessions.sizeconst reason = err instanceof Error ? err.message : String(err)+ const archNote = spawnHelperArchMismatch()throw new Error(`Failed to spawn terminal (${reason}). Program: ${file}, cwd: ${cwd}, live PTYs: ${openPtys}. ` +- `The app process is likely out of file descriptors — restart the app to reclaim them ` +- `(tmux sessions survive an app restart). If it recurs, reduce the number of open ` +- `terminals in the active project.`+ (archNote ??+ `If this persists, restart the app (tmux sessions survive a restart) or run ` ++ `\`npm run rebuild\` in the repo — a release build may have rebuilt node-pty ` ++ `for the wrong architecture.`))}