-
Notifications
You must be signed in to change notification settings - Fork 802
fix: auto-start daemon on Windows to resolve 'Daemon failed to start' #362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0okay
wants to merge
2
commits into
vercel-labs:main
Choose a base branch
from
0okay:fix/windows-daemon-startup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+160
−9
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| /** | ||
| * Cross-platform CLI wrapper for agent-browser | ||
| * | ||
| * This wrapper enables npx support on Windows where shell scripts don't work. | ||
| * For global installs, postinstall.js patches the shims to invoke the native | ||
| * binary directly (zero overhead). | ||
| */ | ||
|
|
||
| import { spawn } from 'child_process'; | ||
| import { existsSync, accessSync, chmodSync, constants } from 'fs'; | ||
| import { dirname, join } from 'path'; | ||
| import { fileURLToPath } from 'url'; | ||
| import { platform, arch, homedir } from 'os'; | ||
|
|
||
| const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
|
|
||
| // [Fix] Auto-start daemon on Windows if missing | ||
| async function ensureDaemonRunning() { | ||
| if (platform() !== 'win32') return; | ||
|
|
||
| const agentDir = join(homedir(), '.agent-browser'); | ||
| const portFile = join(agentDir, 'default.port'); | ||
|
|
||
| if (existsSync(portFile)) return; | ||
|
|
||
| // console.log('[Auto-Fix] Starting agent-browser daemon...'); | ||
| // The daemon is located in ../dist/daemon.js relative to this script | ||
| const daemonScript = join(__dirname, '../dist/daemon.js'); | ||
|
|
||
| if (!existsSync(daemonScript)) { | ||
| // If dist doesn't exist (e.g. dev mode), try src (via tsx maybe?) | ||
| // but in production/installed package dist should exist. | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| const child = spawn(process.execPath, [daemonScript], { | ||
| detached: true, | ||
| stdio: 'ignore', | ||
| windowsHide: true | ||
| }); | ||
| child.unref(); | ||
|
|
||
| // Wait up to 3s for startup | ||
| const start = Date.now(); | ||
| while (Date.now() - start < 3000) { | ||
| if (existsSync(portFile)) break; | ||
| await new Promise(r => setTimeout(r, 100)); | ||
| } | ||
| } catch (e) { | ||
| // Ignore spawn errors, let the native binary try its own startup | ||
| } | ||
| } | ||
|
|
||
| // Map Node.js platform/arch to binary naming convention | ||
| function getBinaryName() { | ||
| const os = platform(); | ||
| const cpuArch = arch(); | ||
|
|
||
| let osKey; | ||
| switch (os) { | ||
| case 'darwin': | ||
| osKey = 'darwin'; | ||
| break; | ||
| case 'linux': | ||
| osKey = 'linux'; | ||
| break; | ||
| case 'win32': | ||
| osKey = 'win32'; | ||
| break; | ||
| default: | ||
| return null; | ||
| } | ||
|
|
||
| let archKey; | ||
| switch (cpuArch) { | ||
| case 'x64': | ||
| case 'x86_64': | ||
| archKey = 'x64'; | ||
| break; | ||
| case 'arm64': | ||
| case 'aarch64': | ||
| archKey = 'arm64'; | ||
| break; | ||
| default: | ||
| return null; | ||
| } | ||
|
|
||
| const ext = os === 'win32' ? '.exe' : ''; | ||
| return `agent-browser-${osKey}-${archKey}${ext}`; | ||
| } | ||
|
|
||
| async function main() { | ||
| await ensureDaemonRunning(); | ||
|
|
||
| const binaryName = getBinaryName(); | ||
|
|
||
| if (!binaryName) { | ||
| console.error(`Error: Unsupported platform: ${platform()}-${arch()}`); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const binaryPath = join(__dirname, binaryName); | ||
|
|
||
| // If native binary is missing, we could try to run the Node.js version directly? | ||
| // But for now let's stick to the original logic which expects the binary. | ||
|
|
||
| if (!existsSync(binaryPath)) { | ||
| console.error(`Error: No binary found for ${platform()}-${arch()}`); | ||
| console.error(`Expected: ${binaryPath}`); | ||
| console.error(''); | ||
| console.error('Run "npm run build:native" to build for your platform,'); | ||
| console.error('or reinstall the package to trigger the postinstall download.'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| // Ensure binary is executable (fixes EACCES on macOS/Linux when postinstall didn't run, | ||
| // e.g., when using bun which blocks lifecycle scripts by default) | ||
| if (platform() !== 'win32') { | ||
| try { | ||
| accessSync(binaryPath, constants.X_OK); | ||
| } catch { | ||
| // Binary exists but isn't executable - fix it | ||
| try { | ||
| chmodSync(binaryPath, 0o755); | ||
| } catch (chmodErr) { | ||
| console.error(`Error: Cannot make binary executable: ${chmodErr.message}`); | ||
| console.error('Try running: chmod +x ' + binaryPath); | ||
| process.exit(1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Spawn the native binary with inherited stdio | ||
| const child = spawn(binaryPath, process.argv.slice(2), { | ||
| stdio: 'inherit', | ||
| windowsHide: false, | ||
| }); | ||
|
|
||
| child.on('error', (err) => { | ||
| console.error(`Error executing binary: ${err.message}`); | ||
| process.exit(1); | ||
| }); | ||
|
|
||
| child.on('close', (code) => { | ||
| process.exit(code ?? 0); | ||
| }); | ||
| } | ||
|
|
||
| main(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Port file path mismatch between daemon writer and wrapper reader causes daemon detection to always fail on Windows