Skip to content

Computer use CLI for AI agents

fix(npm): link the bin entry to the native binary so the command starts without Node (#300)

Lahfir5f35032403cf
diff --git a/README.md b/README.md
index 221160b00..45c8eb5a8 100644
--- a/README.md
+++ b/README.md
@@ -30,6 +30,15 @@ npm install -g --allow-scripts=agent-desktop agent-desktop
npm config set allow-scripts=agent-desktop --location=user
```
+pnpm and bun block install scripts by default:
+
+```bash
+pnpm add -g --allow-build=agent-desktop agent-desktop
+bun add -g --trust agent-desktop
+```
+
+On macOS the script also links the package's `agent-desktop` command to the native binary, so the command starts without Node. On Windows the command runs through a small Node wrapper.
+
Release archives are also on the [releases page](https://github.com/lahfir/agent-desktop/releases). The checksums come from the same release as the archives, so they catch corruption but do not prove origin. Check origin with:
```bash
diff --git a/npm/bin/agent-desktop.js b/npm/bin/agent-desktop
similarity index 92%
rename from npm/bin/agent-desktop.js
rename to npm/bin/agent-desktop
index bd4031f1f..f84147753 100755
--- a/npm/bin/agent-desktop.js
+++ b/npm/bin/agent-desktop
@@ -35,17 +35,13 @@ function main() {
console.error('the package manager blocked the postinstall script (npm: allow-scripts=agent-desktop),');
console.error('or its postinstall download step failed.');
console.error('Try reinstalling:');
- console.error(' npm install -g agent-desktop');
+ console.error(' npm: npm install -g --allow-scripts=agent-desktop agent-desktop');
+ console.error(' pnpm: pnpm add -g --allow-build=agent-desktop agent-desktop');
+ console.error(' bun: bun add -g --trust agent-desktop');
console.error('');
console.error('Or download directly from:');
console.error(' https://github.com/lahfir/agent-desktop/releases');
- if (typeof process.versions.bun !== 'undefined') {
- console.error('');
- console.error('Bun detected — postinstall scripts require --trust:');
- console.error(' bun install -g --trust agent-desktop');
- }
-
process.exit(1);
}
diff --git a/npm/package.json b/npm/package.json
index 614c0c328..00c290b54 100644
--- a/npm/package.json
+++ b/npm/package.json
@@ -9,7 +9,7 @@
},
"homepage": "https://github.com/lahfir/agent-desktop",
"bin": {
- "agent-desktop": "bin/agent-desktop.js"
+ "agent-desktop": "bin/agent-desktop"
},
"scripts": {
"postinstall": "node scripts/postinstall.js"
diff --git a/npm/scripts/postinstall.js b/npm/scripts/postinstall.js
index f62247dcf..ced0a9b7e 100644
--- a/npm/scripts/postinstall.js
+++ b/npm/scripts/postinstall.js
@@ -3,6 +3,7 @@
const {
chmodSync,
existsSync,
+ linkSync,
lstatSync,
mkdirSync,
mkdtempSync,
@@ -10,11 +11,10 @@ const {
readdirSync,
renameSync,
rmSync,
- symlinkSync,
unlinkSync,
writeFileSync,
} = require('fs');
-const { dirname, isAbsolute, join } = require('path');
+const { basename, dirname, isAbsolute, join } = require('path');
const { platform, arch } = require('os');
const { execFileSync } = require('child_process');
const { createHash } = require('crypto');
@@ -185,36 +185,46 @@ function installArchive(tarballPath, binaryPath, helperPath, entry, trashCommand
}
}
-function fixGlobalInstallBin() {
- if (platform() === 'win32') return;
+const BIN_ENTRY_NAME = 'agent-desktop';
- let npmBinDir;
- try {
- const prefix = execFileSync('npm', ['prefix', '-g'], { encoding: 'utf8', timeout: 5000 }).trim();
- npmBinDir = join(prefix, 'bin');
- } catch {
- return;
- }
-
- const symlinkPath = join(npmBinDir, 'agent-desktop');
- const entry = resolve(platform(), arch());
- if (!entry) return;
-
- const binaryPath = join(binDir, entry.binaryName);
+function isInstalledPackage(directory) {
+ return basename(dirname(dirname(directory))) === 'node_modules';
+}
+function isNodeWrapper(path) {
try {
- const stat = lstatSync(symlinkPath);
- if (!stat.isSymbolicLink()) return;
+ return readFileSync(path).subarray(0, 2).toString() === '#!';
} catch {
- return;
+ return false;
}
+}
+function linkBinEntry(directory, binaryName, osPlatform = platform()) {
+ if (osPlatform === 'win32' || !isInstalledPackage(directory)) return false;
+ const entryPath = join(directory, BIN_ENTRY_NAME);
+ const binaryPath = join(directory, binaryName);
+ const temporary = `${entryPath}.link-${process.pid}`;
try {
- unlinkSync(symlinkPath);
- symlinkSync(binaryPath, symlinkPath);
- log('Optimized: symlink points to native binary (zero overhead)');
+ const binaryStat = lstatSync(binaryPath);
+ try {
+ const entryStat = lstatSync(entryPath);
+ if (entryStat.ino === binaryStat.ino && entryStat.dev === binaryStat.dev) return false;
+ } catch {}
+ rmSync(temporary, { force: true });
+ linkSync(binaryPath, temporary);
+ renameSync(temporary, entryPath);
+ log('Linked bin entry to native binary');
+ return true;
} catch (err) {
- log(`Could not optimize symlink: ${err.message}`);
+ if (isNodeWrapper(entryPath)) {
+ log(`Could not link bin entry, keeping the Node wrapper: ${err.message}`);
+ } else {
+ log(`Could not link bin entry to the installed native binary: ${err.message}`);
+ process.exitCode = 1;
+ }
+ return false;
+ } finally {
+ try { unlinkSync(temporary); } catch {}
}
}
@@ -292,7 +302,7 @@ function main() {
}
installExecutable(customPath, binaryPath);
log(`Using binary from AGENT_DESKTOP_BINARY_PATH: ${customPath}`);
- fixGlobalInstallBin();
+ linkBinEntry(binDir, binaryName);
promptSkillInstall();
} catch (err) {
log(`Failed to install from AGENT_DESKTOP_BINARY_PATH: ${err.message}`);
@@ -314,7 +324,7 @@ function main() {
? `Native executables ready: ${binaryName}, ${MACOS_HELPER_NAME}`
: `Native executable ready: ${binaryName}`,
);
- fixGlobalInstallBin();
+ linkBinEntry(binDir, binaryName);
promptSkillInstall();
return;
}
@@ -356,7 +366,7 @@ function main() {
return;
}
- fixGlobalInstallBin();
+ linkBinEntry(binDir, binaryName);
promptSkillInstall();
}
@@ -370,6 +380,7 @@ module.exports = {
cleanupStaging,
customHelperPath,
installArchive,
+ linkBinEntry,
matchesRecordedDigest,
printManualFallback,
promptSkillInstall,
diff --git a/scripts/check-npm-package.js b/scripts/check-npm-package.js
index ecebdc7f7..9de0f4b96 100644
--- a/scripts/check-npm-package.js
+++ b/scripts/check-npm-package.js
@@ -11,7 +11,7 @@ const pkg = require(join(npmDir, 'package.json'));
const releaseWorkflow = readFileSync(join(root, '.github/workflows/release.yml'), 'utf8');
const { PLATFORMS, tarballName } = require(join(npmDir, 'lib', 'platform.js'));
const expectedFiles = [
- 'bin/agent-desktop.js',
+ 'bin/agent-desktop',
'lib/platform.js',
'package.json',
'scripts/postinstall.js',
@@ -342,8 +342,8 @@ function selfTest() {
selfTest();
-if (pkg.bin?.['agent-desktop'] !== 'bin/agent-desktop.js') {
- throw new Error('npm bin path must be bin/agent-desktop.js');
+if (pkg.bin?.['agent-desktop'] !== 'bin/agent-desktop') {
+ throw new Error('npm bin path must be bin/agent-desktop');
}
if (pkg.repository?.url !== 'git+https://github.com/lahfir/agent-desktop.git') {
diff --git a/scripts/ci-npm-wrapper-smoke.sh b/scripts/ci-npm-wrapper-smoke.sh
index fb2521da1..6bacd6ea8 100755
--- a/scripts/ci-npm-wrapper-smoke.sh
+++ b/scripts/ci-npm-wrapper-smoke.sh
@@ -13,6 +13,9 @@ case "$(uname -s)-$(uname -m)" in
;;
esac
HELPER_NAME=agent-desktop-macos-helper
+WORK="$(mktemp -d)"
+trap 'rm -rf "$WORK"' EXIT
+TARBALL="$WORK/$(npm pack ./npm --pack-destination "$WORK" --silent)"
SOURCE=target/release/agent-desktop
HELPER_SOURCE=target/release/agent-desktop-macos-helper
SOURCE_SHA=$(shasum -a 256 "$SOURCE" | awk '{print $1}')
@@ -27,7 +30,7 @@ if [ "$SOURCE_SHA" != "$COPY_SHA" ] || [ "$HELPER_SOURCE_SHA" != "$HELPER_COPY_S
echo "NPM smoke executable copy failed immutable identity verification" >&2
exit 1
fi
-node npm/bin/agent-desktop.js version > /tmp/agent-desktop-version.json
+node npm/bin/agent-desktop version > /tmp/agent-desktop-version.json
node -e "
const out = require('fs').readFileSync('/tmp/agent-desktop-version.json', 'utf8');
const json = JSON.parse(out);
@@ -42,3 +45,18 @@ if [ "$SOURCE_SHA" != "$(shasum -a 256 "$SOURCE" | awk '{print $1}')" ] || \
echo "NPM smoke executable changed while it was executed" >&2
exit 1
fi
+
+PREFIX="$WORK/prefix"
+AGENT_DESKTOP_BINARY_PATH="$ROOT/$SOURCE" npm install -g "$TARBALL" --prefix "$PREFIX" --no-audit --no-fund
+INSTALLED_ENTRY="$PREFIX/lib/node_modules/agent-desktop/bin/agent-desktop"
+if [ "$(head -c 2 "$INSTALLED_ENTRY")" = '#!' ]; then
+ echo "Installed bin entry is still the Node wrapper, not the native binary" >&2
+ exit 1
+fi
+PATH="$PREFIX/bin:/usr/bin:/bin" agent-desktop version > "$WORK/installed-version.json"
+node -e "
+ const json = JSON.parse(require('fs').readFileSync(process.argv[1], 'utf8'));
+ if (json.ok !== true || typeof json.data?.version !== 'string') {
+ throw new Error('installed agent-desktop command did not return version JSON');
+ }
+" "$WORK/installed-version.json"
diff --git a/tests/npm/helpers.js b/tests/npm/helpers.js
index cedea141d..a21119596 100644
--- a/tests/npm/helpers.js
+++ b/tests/npm/helpers.js
@@ -4,7 +4,7 @@ const { tmpdir } = require('node:os');
const { join } = require('node:path');
const postinstallScriptPath = join(__dirname, '..', '..', 'npm', 'scripts', 'postinstall.js');
-const wrapperScriptPath = join(__dirname, '..', '..', 'npm', 'bin', 'agent-desktop.js');
+const wrapperScriptPath = join(__dirname, '..', '..', 'npm', 'bin', 'agent-desktop');
const OS_STUB_PLATFORM_ENV = 'AGENT_DESKTOP_TEST_OS_PLATFORM';
const OS_STUB_ARCH_ENV = 'AGENT_DESKTOP_TEST_OS_ARCH';
diff --git a/tests/npm/postinstall.test.js b/tests/npm/postinstall.test.js
index 04175ae2b..5971ced4f 100644
--- a/tests/npm/postinstall.test.js
+++ b/tests/npm/postinstall.test.js
@@ -3,9 +3,11 @@ const { execFileSync } = require('node:child_process');
const {
chmodSync,
existsSync,
+ lstatSync,
mkdirSync,
readFileSync,
readdirSync,
+ renameSync,
rmSync,
unlinkSync,
writeFileSync,
@@ -460,3 +462,137 @@ test('prompted skills are a subset of the skill packages that exist', () => {
'linux must not advertise a platform skill until one exists',
);
});
+
+function binWithWrapper(segments = ['node_modules', 'agent-desktop', 'bin']) {
+ const dir = join(temporaryDirectory(), ...segments);
+ mkdirSync(dir, { recursive: true });
+ writeFileSync(join(dir, 'agent-desktop'), '#!/usr/bin/env node\n', { mode: 0o755 });
+ writeFileSync(join(dir, 'agent-desktop-darwin-arm64'), 'native', { mode: 0o755 });
+ return dir;
+}
+
+test('bin entry becomes a hard link to the native binary and stays executable', {
+ skip: process.platform === 'win32',
+}, () => {
+ const dir = binWithWrapper();
+ const warnings = captureWarnings(() => {
+ assert.equal(postinstall.linkBinEntry(dir, 'agent-desktop-darwin-arm64', 'darwin'), true);
+ });
+ const entry = lstatSync(join(dir, 'agent-desktop'));
+ const native = lstatSync(join(dir, 'agent-desktop-darwin-arm64'));
+ assert.equal(entry.ino, native.ino);
+ assert.ok(entry.mode & 0o111, 'linked entry must be executable');
+ assert.equal(readFileSync(join(dir, 'agent-desktop'), 'utf8'), 'native');
+ assert.match(warnings, /Linked bin entry to native binary/);
+ assert.deepEqual(readdirSync(dir).filter((name) => name.includes('.link-')), []);
+});
+
+test('linking the bin entry twice is a no-op', {
+ skip: process.platform === 'win32',
+}, () => {
+ const dir = binWithWrapper();
+ postinstall.linkBinEntry(dir, 'agent-desktop-darwin-arm64', 'darwin');
+ const warnings = captureWarnings(() => {
+ assert.equal(postinstall.linkBinEntry(dir, 'agent-desktop-darwin-arm64', 'darwin'), false);
+ });
+ assert.equal(warnings, '');
+});
+
+test('bin entry is relinked after the native binary is replaced', {
+ skip: process.platform === 'win32',
+}, () => {
+ const dir = binWithWrapper();
+ postinstall.linkBinEntry(dir, 'agent-desktop-darwin-arm64', 'darwin');
+ const native = join(dir, 'agent-desktop-darwin-arm64');
+ writeFileSync(`${native}.next`, 'newer', { mode: 0o755 });
+ renameSync(`${native}.next`, native);
+ assert.equal(postinstall.linkBinEntry(dir, 'agent-desktop-darwin-arm64', 'darwin'), true);
+ assert.equal(readFileSync(join(dir, 'agent-desktop'), 'utf8'), 'newer');
+});
+
+test('bin entry is untouched outside node_modules', () => {
+ const dir = binWithWrapper(['checkout', 'bin']);
+ assert.equal(postinstall.linkBinEntry(dir, 'agent-desktop-darwin-arm64', 'darwin'), false);
+ assert.equal(readFileSync(join(dir, 'agent-desktop'), 'utf8'), '#!/usr/bin/env node\n');
+});
+
+test('bin entry is untouched on win32', () => {
+ const dir = binWithWrapper();
+ assert.equal(postinstall.linkBinEntry(dir, 'agent-desktop-darwin-arm64', 'win32'), false);
+ assert.equal(readFileSync(join(dir, 'agent-desktop'), 'utf8'), '#!/usr/bin/env node\n');
+});
+
+test('bin entry keeps the wrapper when the native binary is missing', () => {
+ const dir = join(temporaryDirectory(), 'node_modules', 'agent-desktop', 'bin');
+ mkdirSync(dir, { recursive: true });
+ writeFileSync(join(dir, 'agent-desktop'), '#!/usr/bin/env node\n', { mode: 0o755 });
+ const warnings = captureWarnings(() => {
+ assert.equal(postinstall.linkBinEntry(dir, 'agent-desktop-darwin-arm64', 'darwin'), false);
+ });
+ assert.equal(readFileSync(join(dir, 'agent-desktop'), 'utf8'), '#!/usr/bin/env node\n');
+ assert.match(warnings, /keeping the Node wrapper/);
+ assert.notEqual(process.exitCode, 1);
+});
+
+test('bin entry is untouched under an unrelated node_modules ancestor', () => {
+ const dir = binWithWrapper(['node_modules', 'sandbox', 'agent-desktop', 'npm', 'bin']);
+ assert.equal(postinstall.linkBinEntry(dir, 'agent-desktop-darwin-arm64', 'darwin'), false);
+ assert.equal(readFileSync(join(dir, 'agent-desktop'), 'utf8'), '#!/usr/bin/env node\n');
+});
+
+test('a leftover temporary link does not block linking', {
+ skip: process.platform === 'win32',
+}, () => {
+ const dir = binWithWrapper();
+ writeFileSync(join(dir, `agent-desktop.link-${process.pid}`), 'stale');
+ captureWarnings(() => {
+ assert.equal(postinstall.linkBinEntry(dir, 'agent-desktop-darwin-arm64', 'darwin'), true);
+ });
+ assert.equal(readFileSync(join(dir, 'agent-desktop'), 'utf8'), 'native');
+});
+
+test('a failed relink that leaves an older native entry fails the install', {
+ skip: process.platform === 'win32' || process.getuid?.() === 0,
+}, () => {
+ const dir = binWithWrapper();
+ captureWarnings(() => postinstall.linkBinEntry(dir, 'agent-desktop-darwin-arm64', 'darwin'));
+ const native = join(dir, 'agent-desktop-darwin-arm64');
+ writeFileSync(`${native}.next`, 'newer', { mode: 0o755 });
+ renameSync(`${native}.next`, native);
+ chmodSync(dir, 0o555);
+ try {
+ const warnings = captureWarnings(() => {
+ assert.equal(postinstall.linkBinEntry(dir, 'agent-desktop-darwin-arm64', 'darwin'), false);
+ });
+ assert.match(warnings, /Could not link bin entry to the installed native binary/);
+ assert.equal(process.exitCode, 1);
+ } finally {
+ process.exitCode = 0;
+ chmodSync(dir, 0o755);
+ }
+});
+
+test('postinstall links the bin entry of an installed package to the native binary', {
+ skip: process.platform === 'win32',
+}, async () => {
+ const root = join(temporaryDirectory(), 'node_modules', 'agent-desktop');
+ for (const part of ['package.json', 'bin/agent-desktop', 'lib/platform.js', 'scripts/postinstall.js']) {
+ mkdirSync(join(root, part, '..'), { recursive: true });
+ writeFileSync(join(root, part), readFileSync(join(__dirname, '..', '..', 'npm', part)), { mode: 0o755 });
+ }
+ const source = join(temporaryDirectory(), 'release');
+ mkdirSync(source);
+ writeFileSync(join(source, 'agent-desktop'), 'native', { mode: 0o755 });
+ writeFileSync(join(source, 'agent-desktop-macos-helper'), 'helper', { mode: 0o755 });
+ process.env.AGENT_DESKTOP_BINARY_PATH = join(source, 'agent-desktop');
+ try {
+ const { code, stderr } = await runScriptWithOsStub(join(root, 'scripts', 'postinstall.js'), 'darwin', 'arm64');
+ assert.equal(code, 0, stderr);
+ assert.match(stderr, /Linked bin entry to native binary/);
+ } finally {
+ delete process.env.AGENT_DESKTOP_BINARY_PATH;
+ }
+ const entry = lstatSync(join(root, 'bin', 'agent-desktop'));
+ assert.equal(entry.ino, lstatSync(join(root, 'bin', 'agent-desktop-darwin-arm64')).ino);
+ assert.equal(readFileSync(join(root, 'bin', 'agent-desktop-macos-helper'), 'utf8'), 'helper');
+});
diff --git a/tests/npm/wrapper.test.js b/tests/npm/wrapper.test.js
index ca0835be0..f3a9b5fdc 100644
--- a/tests/npm/wrapper.test.js
+++ b/tests/npm/wrapper.test.js
@@ -43,6 +43,8 @@ test('wrapper resolves the released win32-x64 mapping and names the missing-bina
assert.equal(code, 1);
assert.match(stderr, /Error: Native binary not found for win32-x64/);
assert.match(stderr, /--ignore-scripts/);
+ assert.match(stderr, /pnpm add -g --allow-build=agent-desktop agent-desktop/);
+ assert.match(stderr, /bun add -g --trust agent-desktop/);
});
test('wrapper resolves the released win32-arm64 mapping and exits non-zero when the binary is absent', async () => {