Computer use CLI for AI agents
Rust accessibilityaccessibility-apiai-agentsautomationclicomputer-usecomputer-use-agentcomputer-use-agentsdesktop-automationmacosmcprust
feat: tell npm users when the node launcher slows each call (#328)
Lahfir068df3edae86
diff --git a/.gitignore b/.gitignoreindex 09ce4b41e..63785af7d 100644--- a/.gitignore+++ b/.gitignore@@ -88,3 +88,4 @@ probes/windows/22-capture-clipboard/bin/# them, so they stay untracked. The per-area probe directories# probes/windows/NN-*/captures/ commit their twins beside the capture.probes/*/captures/**/*.normalized+npm/bin/.launcher-notice-*diff --git a/npm/bin/agent-desktop b/npm/bin/agent-desktopindex 27f4befb8..4cf2bdfe8 100755--- a/npm/bin/agent-desktop+++ b/npm/bin/agent-desktop@@ -1,7 +1,7 @@#!/usr/bin/env nodeconst { spawn } = require('child_process');-const { existsSync, accessSync, chmodSync, lstatSync, constants } = require('fs');+const { existsSync, accessSync, chmodSync, lstatSync, writeFileSync, constants } = require('fs');const { isAbsolute, join } = require('path');const { platform, arch } = require('os');const { LINUX_HELPER_NAME, MACOS_HELPER_NAME, releasedKeys, resolve } = require('../lib/platform');@@ -15,6 +15,22 @@ function getBinaryName() {return entry && entry.released ? entry.binaryName : null;}+const LAUNCHER_NOTICE = [+ 'agent-desktop: this command runs through the Node launcher because npm did not run the install script, which adds about 125 ms per call.',+ 'Allow it and reinstall to link the native binary: npm install -g agent-desktop --allow-scripts=agent-desktop (npm 11), or npm install-scripts approve agent-desktop (npm 12).',+].join('\n');++// Reaching this launcher means postinstall never linked the native binary.+// The marker is the only state: if it cannot be written, stay silent.+function noteLauncherOnce() {+ try {+ const { version } = require('../package.json');+ if (!/^[0-9A-Za-z.+-]+$/.test(version)) return;+ writeFileSync(join(binDir, `.launcher-notice-${version}`), '', { flag: 'wx' });+ process.stderr.write(`${LAUNCHER_NOTICE}\n`);+ } catch {}+}+function main() {const binaryName = getBinaryName();@@ -97,6 +113,8 @@ function main() {}}+ noteLauncherOnce();+const child = spawn(binaryPath, process.argv.slice(2), {stdio: 'inherit',env: helperPath ? { ...process.env, [MACOS_HELPER_PATH_ENV]: helperPath } : process.env,diff --git a/npm/scripts/postinstall.js b/npm/scripts/postinstall.jsindex 8d4b82f51..505c1b860 100644--- a/npm/scripts/postinstall.js+++ b/npm/scripts/postinstall.js@@ -183,9 +183,15 @@ function isNodeWrapper(path) {}}+const LINKED_NOTICE = 'Native binary linked: each call runs without the Node launcher.';+function linkBinEntry(directory, binaryName, osPlatform = platform()) {if (!isInstalledPackage(directory)) return false;- if (osPlatform === 'win32') return require('../lib/windows-bin')(directory, binaryName);+ if (osPlatform === 'win32') {+ const linked = require('../lib/windows-bin')(directory, binaryName);+ if (linked) log(LINKED_NOTICE);+ return linked;+ }const entryPath = join(directory, BIN_ENTRY_NAME);const binaryPath = join(directory, binaryName);const temporary = `${entryPath}.link-${process.pid}`;@@ -198,7 +204,7 @@ function linkBinEntry(directory, binaryName, osPlatform = platform()) {rmSync(temporary, { force: true });linkSync(binaryPath, temporary);renameSync(temporary, entryPath);- log('Linked bin entry to native binary');+ log(LINKED_NOTICE);return true;} catch (err) {if (isNodeWrapper(entryPath)) {diff --git a/tests/npm/postinstall.test.js b/tests/npm/postinstall.test.jsindex 32211b27a..fe7b76cba 100644--- a/tests/npm/postinstall.test.js+++ b/tests/npm/postinstall.test.js@@ -430,7 +430,7 @@ test('bin entry becomes a hard link to the native binary and stays executable',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.match(warnings, /Native binary linked: each call runs without the Node launcher/);assert.deepEqual(readdirSync(dir).filter((name) => name.includes('.link-')), []);});@@ -535,7 +535,7 @@ test('postinstall links the bin entry of an installed package to the native binatry {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/);+ assert.match(stderr, /Native binary linked: each call runs without the Node launcher/);} finally {delete process.env.AGENT_DESKTOP_BINARY_PATH;}diff --git a/tests/npm/wrapper.test.js b/tests/npm/wrapper.test.jsindex bc4860516..a1e150a2a 100644--- a/tests/npm/wrapper.test.js+++ b/tests/npm/wrapper.test.js@@ -46,6 +46,7 @@ afterEach(() => {// Installing now also writes a digest sidecar. Leaving one behind puts an// unexpected file in npm/bin, which `npm pack` would include and the package// contract gate rejects - so the sweep has to match what installing creates.+ try { unlinkSync(join(__dirname, '..', '..', 'npm', 'bin', '.launcher-notice-' + require('../../npm/package.json').version)); } catch {}for (const name of ['agent-desktop-win32-x64.exe', 'agent-desktop-win32-arm64.exe']) {const installed = join(__dirname, '..', '..', 'npm', 'bin', name);try { unlinkSync(installed); } catch {}@@ -143,3 +144,72 @@ test('AGENT_DESKTOP_BINARY_PATH installs on windows without any helper present',try { unlinkSync(`${installedBinary}.sha256`); } catch {}}});++function noticeWrapper() {+ const root = temporaryDirectory();+ const binDir = join(root, 'bin');+ mkdirSync(binDir);+ cpSync(wrapperScriptPath, join(binDir, 'agent-desktop'));+ cpSync(join(__dirname, '../../npm/lib'), join(root, 'lib'), { recursive: true });+ writeFileSync(join(root, 'package.json'), JSON.stringify({ version: '9.8.7' }));+ const entry = resolve('linux', 'x64');+ writeFileSync(+ join(binDir, entry.binaryName),+ '#!/usr/bin/env node\nconsole.log(JSON.stringify(process.argv.slice(2)));process.exit(3);\n',+ { mode: 0o755 },+ );+ chmodSync(join(binDir, entry.binaryName), 0o755);+ return { binDir, script: join(binDir, 'agent-desktop') };+}++async function runLinux(script) {+ const stub = writeOsStub('linux', 'x64');+ const { spawn } = require('node:child_process');+ try {+ return await new Promise((settle) => {+ let stdout = '';+ let stderr = '';+ const child = spawn(process.execPath, ['-r', stub, script, 'a', '--b'], {+ stdio: ['ignore', 'pipe', 'pipe'],+ env: { ...process.env, ...osStubEnv('linux', 'x64') },+ });+ child.stdout.on('data', (c) => { stdout += c; });+ child.stderr.on('data', (c) => { stderr += c; });+ child.on('close', (code) => settle({ code, stdout, stderr }));+ });+ } finally {+ removeOsStub(stub);+ }+}++test('launcher notice prints once per version and leaves stdout and exit code alone', {+ skip: process.platform === 'win32',+}, async () => {+ const { binDir, script } = noticeWrapper();+ const first = await runLinux(script);+ assert.equal(first.code, 3);+ assert.equal(first.stdout, '["a","--b"]\n');+ assert.match(first.stderr, /runs through the Node launcher/);+ assert.match(first.stderr, /--allow-scripts=agent-desktop/);+ assert.match(first.stderr, /npm install-scripts approve agent-desktop/);+ assert.equal(statSync(join(binDir, '.launcher-notice-9.8.7')).isFile(), true);+ const second = await runLinux(script);+ assert.equal(second.code, 3);+ assert.equal(second.stdout, first.stdout);+ assert.doesNotMatch(second.stderr, /Node launcher/);+});++test('launcher notice stays silent when the marker cannot be written', {+ skip: process.platform === 'win32' || (process.getuid && process.getuid() === 0),+}, async () => {+ const { binDir, script } = noticeWrapper();+ chmodSync(binDir, 0o555);+ try {+ const result = await runLinux(script);+ assert.equal(result.code, 3);+ assert.equal(result.stdout, '["a","--b"]\n');+ assert.doesNotMatch(result.stderr, /Node launcher/);+ } finally {+ chmodSync(binDir, 0o755);+ }+});