From 350dd838d66cf15f85bd4af9e71359ba30ccead6 Mon Sep 17 00:00:00 2001 From: Sami Fouad Date: Wed, 14 Oct 2026 17:21:49 -0600 Subject: [PATCH] Multipass VMs on Apple silicon: linux-arm64 build, Local Network errors, relative programs - Publish @gildforge/cli-linux-arm64 (bun-linux-arm64, pinned node-pty linux-arm64 prebuild) so the arm64 VM has its own gild; no server optionalDependency for it (none is published). - Retry multipass exec on "No route to host", then fail naming the macOS Local Network setting; the readiness check only treats a missing marker as not set up. Wait for the agent to start in the VM before reporting a detached session ready. - Run a program inside the working directory by its relative path in the VM. --- .github/workflows/release.yml | 4 +- CHANGELOG.md | 6 ++ PUBLISHING.md | 7 +- scripts/node-pty.json | 1 + scripts/pack.mjs | 11 +- src/isolation/multipass.test.ts | 87 +++++++++++++++ src/isolation/multipass.ts | 180 ++++++++++++++++++++++++++------ 7 files changed, 262 insertions(+), 34 deletions(-) create mode 100644 src/isolation/multipass.test.ts diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1ff1af4..f96e545 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -100,8 +100,8 @@ jobs: cd "out/$TAG" for f in gild-*; do shasum -a 256 "$f" | cut -d' ' -f1 > "$f.sha"; done jq -n --arg version "$VERSION" --arg tag "$TAG" \ - --arg arm64 "$(cat gild-darwin-arm64.sha)" --arg x64 "$(cat gild-darwin-x64.sha)" --arg linux "$(cat gild-linux-x64.sha)" \ - '{version: $version, tag: $tag, artifacts: {"gild-darwin-arm64": $arm64, "gild-darwin-x64": $x64, "gild-linux-x64": $linux}}' > manifest.json + --arg arm64 "$(cat gild-darwin-arm64.sha)" --arg x64 "$(cat gild-darwin-x64.sha)" --arg linux "$(cat gild-linux-x64.sha)" --arg linuxarm64 "$(cat gild-linux-arm64.sha)" \ + '{version: $version, tag: $tag, artifacts: {"gild-darwin-arm64": $arm64, "gild-darwin-x64": $x64, "gild-linux-x64": $linux, "gild-linux-arm64": $linuxarm64}}' > manifest.json rm -f *.sha jq -n --arg version "$VERSION" --arg tag "$TAG" '{version: $version, tag: $tag}' > latest.json cp latest.json "../latest.json" diff --git a/CHANGELOG.md b/CHANGELOG.md index 090225e..05aedca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 0.6.13 + +- `gild spawn --vm` works on Apple silicon: gild publishes a linux-arm64 build (`@gildforge/cli-linux-arm64`, with its node-pty prebuild), so the agent inside the arm64 Multipass VM has its own `gild` for git credentials and pull requests. Before, every `gild` call in that VM failed with "no build for linux-arm64". +- Multipass VM: a connection refused with "No route to host" is retried briefly, then reported with the fix (on macOS: allow Multipass in System Settings → Privacy & Security → Local Network) instead of being mistaken for an unfinished VM setup. `gild spawn --detach --vm` returns once the agent has started in the VM, so closing the terminal or SSH session right after no longer kills the session before it starts. +- `gild spawn --vm ./agent.sh` runs the script from the copied working directory; a program inside the working directory was looked up by name on the VM's PATH and never started. + ## 0.6.12 - `gild setup` sets up your own gild in your Cloudflare account from a terminal (the same steps as gild desktop): it asks for the API token (not saved), the account, your name and email, and makes your gild key the owner. Running it again upgrades in place. diff --git a/PUBLISHING.md b/PUBLISHING.md index 877ddf9..a7aa6bf 100644 --- a/PUBLISHING.md +++ b/PUBLISHING.md @@ -1,9 +1,14 @@ # Publishing to npm -Names: `gildforge` (umbrella CLI) + `@gildforge/cli-{darwin-arm64,darwin-x64,linux-x64}` +Names: `gildforge` (umbrella CLI) + `@gildforge/cli-{darwin-arm64,darwin-x64,linux-x64,linux-arm64}` (binaries). The `@gildforge/server[-*]` packages publish from gildforge/server, not here. +A new platform package (`@gildforge/cli-linux-arm64` from 0.6.13) must exist on +npm and have the trusted publisher (gildforge/cli, `release.yml`, environment +`release`) set before the Release workflow can publish it: publish its first +version by hand, then add the trusted publisher in its npm settings. + ```sh npm login diff --git a/scripts/node-pty.json b/scripts/node-pty.json index 6d3896d..310416b 100644 --- a/scripts/node-pty.json +++ b/scripts/node-pty.json @@ -14,6 +14,7 @@ "prebuilds/darwin-arm64/spawn-helper": "08bc83a084651d095645010d34783f94b9af443614747e953e66d6578027e999", "prebuilds/darwin-x64/pty.node": "811f4a357e260fc0e520c250a3de5990483761d82afd635effbdf4b793b8a0e9", "prebuilds/darwin-x64/spawn-helper": "a3bed36ae3ed83b2ac2fce475bd578c51f5b536041f0b2dfe116987f565e9758", + "prebuilds/linux-arm64/pty.node": "5a3b8930d62535edabec2eac79fe8eca70a178d93fbb76b1ae875dd60ee8f825", "prebuilds/linux-x64/pty.node": "123792d8f22f36d519b21dfb6f4b716f9b18b9e579085ea2840d0773d88d35fd" } } diff --git a/scripts/pack.mjs b/scripts/pack.mjs index 0c9371a..aaf5225 100644 --- a/scripts/pack.mjs +++ b/scripts/pack.mjs @@ -3,6 +3,7 @@ // packages/gild-darwin-arm64 — the compiled binary per platform // packages/gild-darwin-x64 // packages/gild-linux-x64 +// packages/gild-linux-arm64 — the CLI inside an arm64 Linux VM (Multipass on Apple silicon) // No postinstall anywhere: npm's optionalDependencies + os/cpu fields pick the // right package, and the launcher just execs the binary it finds. import { @@ -26,6 +27,14 @@ const TARGETS = [ }, { name: 'cli-darwin-x64', bun: 'bun-darwin-x64', os: 'darwin', cpu: 'x64' }, { name: 'cli-linux-x64', bun: 'bun-linux-x64', os: 'linux', cpu: 'x64' }, + // No gild-server build for linux-arm64 yet: no server optionalDependency. + { + name: 'cli-linux-arm64', + bun: 'bun-linux-arm64', + os: 'linux', + cpu: 'arm64', + server: false, + }, ] rmSync(join(ROOT, 'packages'), { recursive: true, force: true }) @@ -136,7 +145,7 @@ writeFileSync( TARGETS.map((t) => [`@gildforge/${t.name}`, pkg.version]), ), ...Object.fromEntries( - TARGETS.map((t) => [ + TARGETS.filter((t) => t.server !== false).map((t) => [ `@gildforge/server-${t.name.replace('cli-', '')}`, '*', ]), diff --git a/src/isolation/multipass.test.ts b/src/isolation/multipass.test.ts new file mode 100644 index 0000000..ff18403 --- /dev/null +++ b/src/isolation/multipass.test.ts @@ -0,0 +1,87 @@ +import { describe, expect, test } from 'bun:test' +import { join } from 'node:path' +import { + guestProgram, + noRoute, + noRouteError, + readiness, + retryNoRoute, +} from './multipass' + +const NO_ROUTE = + "exec failed: ssh connection failed: 'Failed to connect: No route to host'\n" + +describe('guestProgram', () => { + const cwd = '/Users/me/work' + test('a script inside the working directory runs by its relative path', () => { + expect(guestProgram(join(cwd, 'agent.sh'), cwd)).toBe('./agent.sh') + expect(guestProgram(join(cwd, 'bin', 'run'), cwd)).toBe('./bin/run') + }) + test('a program from PATH runs by name from the VM PATH', () => { + expect(guestProgram('/opt/homebrew/bin/claude', cwd)).toBe('claude') + expect(guestProgram('/bin/sh', cwd)).toBe('sh') + }) + test('a sibling directory with the same prefix is not inside', () => { + expect(guestProgram('/Users/me/work2/agent.sh', cwd)).toBe('agent.sh') + expect(guestProgram('/Users/me/agent.sh', cwd)).toBe('agent.sh') + }) +}) + +describe('No route to host', () => { + test('is recognised in Multipass output', () => { + expect(noRoute(NO_ROUTE)).toBe(true) + expect(noRoute('exec failed: instance "gild" is not running')).toBe(false) + }) + test('on macOS the error names the Local Network setting', () => { + const message = noRouteError('darwin').message + expect(message).toContain('No route to host') + expect(message).toContain( + 'System Settings → Privacy & Security → Local Network', + ) + }) + test('is retried, then returned when it persists', () => { + let calls = 0 + const slept: number[] = [] + const r = retryNoRoute( + () => (calls++, { status: 2, stdout: '', stderr: NO_ROUTE }), + { tries: 3, delayMs: 7, sleep: (ms) => slept.push(ms) }, + ) + expect(calls).toBe(3) + expect(slept).toEqual([7, 7]) + expect(r.status).toBe(2) + }) + test('a retry that connects returns its result', () => { + const results = [ + { status: 2, stdout: '', stderr: NO_ROUTE }, + { status: 0, stdout: 'ready\n', stderr: '' }, + ] + let calls = 0 + const r = retryNoRoute(() => results[calls++], { sleep: () => {} }) + expect(calls).toBe(2) + expect(r.stdout).toBe('ready\n') + }) + test('other failures are not retried', () => { + let calls = 0 + retryNoRoute(() => (calls++, { status: 1, stdout: '', stderr: 'nope' }), { + sleep: () => {}, + }) + expect(calls).toBe(1) + }) +}) + +describe('readiness', () => { + test('only the marker answer decides set up or not', () => { + expect(readiness({ status: 0, stdout: 'ready\n', stderr: '' })).toBe(true) + expect(readiness({ status: 0, stdout: 'missing\n', stderr: '' })).toBe( + false, + ) + }) + test('an unreachable VM is an error, not "not set up yet"', () => { + expect(() => + readiness({ status: 2, stdout: '', stderr: NO_ROUTE }), + ).toThrow('No route to host') + expect(() => + readiness({ status: 2, stdout: '', stderr: 'exec failed: boom' }), + ).toThrow('checking the gild VM: exec failed: boom') + }) +}) diff --git a/src/isolation/multipass.ts b/src/isolation/multipass.ts index 73f2e1b..5a8e34e 100644 --- a/src/isolation/multipass.ts +++ b/src/isolation/multipass.ts @@ -9,7 +9,7 @@ import { spawn, spawnSync } from 'node:child_process' import { readFileSync } from 'node:fs' import { readFile } from 'node:fs/promises' -import { basename, join } from 'node:path' +import { basename, isAbsolute, join, relative, sep } from 'node:path' import type { VmChild } from '../spawn-vm' export const MULTIPASS_VM = 'gild' @@ -56,25 +56,89 @@ function multipassPath() { return r.status === 0 ? r.stdout.split(/\r?\n/)[0].trim() : 'multipass' } +/** Multipass could not open its SSH connection into the VM. On macOS that is + * Local Network privacy refusing the connection, typically once the terminal + * or SSH session that started gild has gone; on any host, briefly, a VM whose + * network is still coming up. */ +export const noRoute = (output: string) => /no route to host/i.test(output) + +export function noRouteError(platform: NodeJS.Platform = process.platform) { + return new Error( + platform === 'darwin' + ? 'Multipass cannot reach the gild VM (No route to host). macOS Local Network privacy is blocking it: allow Multipass in System Settings → Privacy & Security → Local Network, and keep the terminal or SSH session that starts gild open until the agent has started' + : 'Multipass cannot reach the gild VM (No route to host). Check `multipass list` and the host firewall, then run this again', + ) +} + +/** Runs `attempt` again while it fails with "No route to host", up to + * `tries` times, waiting `delayMs` between tries. */ +export function retryNoRoute( + attempt: () => T & { stdout?: string; stderr?: string }, + { tries = 5, delayMs = 1000, sleep = sleepSync } = {}, +): T { + for (let i = 1; ; i++) { + const r = attempt() + if (r.status === 0 || !noRoute(`${r.stderr ?? ''}${r.stdout ?? ''}`)) + return r + if (i >= tries) return r + sleep(delayMs) + } +} + +function sleepSync(ms: number) { + Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms) +} + function mp(args: string[], input?: string) { - const r = spawnSync('multipass', args, { - encoding: 'utf8', - input, - maxBuffer: 64 << 20, + return retryNoRoute(() => { + const r = spawnSync('multipass', args, { + encoding: 'utf8', + input, + maxBuffer: 64 << 20, + }) + if (r.error) throw r.error + return r }) - if (r.error) throw r.error - return r } function must(args: string[], what: string, input?: string) { const r = mp(args, input) - if (r.status !== 0) + if (r.status !== 0) { + const output = `${r.stderr}${r.stdout}` + if (noRoute(output)) throw noRouteError() throw new Error( `${what}: ${(r.stderr || r.stdout).trim() || `multipass exited ${r.status}`}`, ) + } return r.stdout } +/** Whether the VM finished its one-time setup. Only a missing marker file + * means "not set up"; a VM that cannot be reached is an error. */ +export function readiness(r: { + status: number | null + stdout: string + stderr: string +}): boolean { + const answer = r.stdout.trim() + if (r.status === 0 && answer === 'ready') return true + if (r.status === 0 && answer === 'missing') return false + if (noRoute(`${r.stderr}${r.stdout}`)) throw noRouteError() + throw new Error( + `checking the gild VM: ${(r.stderr || r.stdout).trim() || `multipass exited ${r.status}`}`, + ) +} + +/** The program as the VM runs it: a program inside the working directory by + * its path relative to the copy (`./agent.sh`), anything else by name, from + * the VM's own PATH. */ +export function guestProgram(binary: string, cwd: string) { + const inside = relative(cwd, binary) + if (inside && !inside.startsWith('..') && !isAbsolute(inside)) + return `./${inside.split(sep).join('/')}` + return basename(binary) +} + /** Why Multipass can't be used here, or null. */ export function multipassMissing(): string | null { const r = spawnSync('multipass', ['version'], { encoding: 'utf8' }) @@ -122,6 +186,7 @@ function step(label: string, args: string[], log: (line: string) => void) { return resolve() } if (tty) process.stderr.write(`\r\x1b[2K ✗ ${label} ${seconds()}\n`) + if (noRoute(out)) return reject(noRouteError()) const why = out .replace(/\x1b\[[0-9;]*[A-Za-z]/g, '') .trim() @@ -138,15 +203,24 @@ export async function ensureVm(log: (line: string) => void = () => {}) { if (missing) throw new Error(missing) const info = mp(['info', MULTIPASS_VM, '--format', 'json']) const fresh = info.status !== 0 - const ready = - !fresh && mp(['exec', MULTIPASS_VM, '--', 'test', '-f', READY]).status === 0 const state = fresh ? null : JSON.parse(info.stdout).info?.[MULTIPASS_VM]?.state - if (!fresh && ready && state === 'Running') return - if (fresh || !ready) + const isReady = () => + readiness( + mp([ + 'exec', + MULTIPASS_VM, + '--', + 'sh', + '-c', + `test -f ${READY} && echo ready || echo missing`, + ]), + ) + const setup = () => log('setting up the gild VM (first time only, about two minutes)') - if (fresh) + if (fresh) { + setup() await step( 'Download Ubuntu 24.04 and create the VM', [ @@ -165,14 +239,49 @@ export async function ensureVm(log: (line: string) => void = () => {}) { ], log, ) - else if (state !== 'Running') + } else if (state !== 'Running') await step('Start the gild VM', ['start', MULTIPASS_VM], log) - if (!ready) - await step( - 'Install Node, git, Claude Code and gild', - ['exec', MULTIPASS_VM, '--', 'sh', '-c', SETUP], - log, - ) + if (isReady()) return + if (!fresh) setup() + await step( + 'Install Node, git, Claude Code and gild', + ['exec', MULTIPASS_VM, '--', 'sh', '-c', SETUP], + log, + ) +} + +/** Resolves when the guest wrote the session's pid file (the program is + * starting), when the multipass process exits, or after `timeoutMs`. */ +function started( + child: import('node:child_process').ChildProcess, + pidFile: string, + timeoutMs = 20000, +) { + return new Promise((resolve) => { + let done = false + const finish = () => { + if (done) return + done = true + clearTimeout(timer) + child.off('exit', finish) + resolve() + } + const timer = setTimeout(finish, timeoutMs) + child.once('exit', finish) + const poll = () => { + if (done) return + const check = spawn( + multipassPath(), + ['exec', MULTIPASS_VM, '--', 'test', '-s', pidFile], + { stdio: 'ignore' }, + ) + check.on('error', finish) + check.on('exit', (code) => + code === 0 ? finish() : setTimeout(poll, 250), + ) + } + poll() + }) } const quote = (s: string) => `'${s.replace(/'/g, `'\\''`)}'` @@ -223,14 +332,17 @@ export async function startMultipassChild(opts: { ) opts.log?.(`copying ${opts.cwd} into the VM`) // A tar stream: sockets and other special files are skipped, not fatal. - const copy = spawnSync( - 'sh', - [ - '-c', - `COPYFILE_DISABLE=1 tar -C ${quote(opts.cwd)} -cf - . 2>/dev/null | ${quote(multipassPath())} exec ${MULTIPASS_VM} -- sh -c ${quote(`mkdir -p ${quote(ws)} && tar -C ${quote(ws)} -xf - 2>/dev/null`)}`, - ], - { encoding: 'utf8', maxBuffer: 16 << 20 }, + const copy = retryNoRoute(() => + spawnSync( + 'sh', + [ + '-c', + `COPYFILE_DISABLE=1 tar -C ${quote(opts.cwd)} -cf - . 2>/dev/null | ${quote(multipassPath())} exec ${MULTIPASS_VM} -- sh -c ${quote(`mkdir -p ${quote(ws)} && tar -C ${quote(ws)} -xf - 2>/dev/null`)}`, + ], + { encoding: 'utf8', maxBuffer: 16 << 20 }, + ), ) + if (noRoute(copy.stderr ?? '')) throw noRouteError() if ( copy.status !== 0 || mp(['exec', MULTIPASS_VM, '--', 'test', '-d', ws]).status !== 0 @@ -239,8 +351,8 @@ export async function startMultipassChild(opts: { `copying the working directory into the VM: ${copy.stderr.trim() || 'tar failed'}`, ) - const program = basename(opts.binary) - if (program === 'claude') { + const program = guestProgram(opts.binary, opts.cwd) + if (basename(opts.binary) === 'claude') { // Claude Code signs in with a token made once by `gild vm login claude`. const token = process.env.CLAUDE_CODE_OAUTH_TOKEN ?? @@ -331,6 +443,10 @@ o=$(git remote get-url origin 2>/dev/null | sed -E 's#^(https?://[^/]+).*#\\1#') stdio: ['pipe', 'pipe', 'pipe'], }, ) + // Return once the program runs in the VM: `gild spawn --detach` reports the + // session ready only then, so the session that started it is still there + // while Multipass connects (macOS Local Network privacy refuses it after). + await started(p, `${dir}/pid`) const hangUp = () => mp([ 'exec', @@ -348,7 +464,11 @@ o=$(git remote get-url origin 2>/dev/null | sed -E 's#^(https?://[^/]+).*#\\1#') resize: () => {}, onData: (cb) => { p.stdout.on('data', (d: Buffer) => cb(d)) - p.stderr.on('data', (d: Buffer) => cb(d)) + p.stderr.on('data', (d: Buffer) => { + cb(d) + if (noRoute(d.toString())) + cb(Buffer.from(`\r\ngild: ${noRouteError().message}\r\n`)) + }) }, onExit: (cb) => void p.on('exit', (code, signal) =>