fix: use ~/.agent-browser for socket files instead of TMPDIR (#180)

* fix: use ~/.agent-browser for socket files instead of TMPDIR

This fixes issue #163 where different TMPDIR values (common with
tmux/screen/VSCode/IntelliJ) caused the CLI and daemon to use
different socket paths.

Socket directory priority:
1. AGENT_BROWSER_SOCKET_DIR (explicit override)
2. $XDG_RUNTIME_DIR/agent-browser (Linux standard)
3. ~/.agent-browser (fallback, like Docker Desktop)

Both CLI (Rust) and daemon (Node.js) now use the same logic.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix: session list now looks in correct socket directory

- Make get_socket_dir() public in connection.rs
- Update session list to use get_socket_dir() instead of temp_dir()
- Update pid file pattern from agent-browser-{session}.pid to {session}.pid
- Add tmpdir fallback to daemon.ts when homedir is unavailable

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* test: add unit tests for socket directory resolution

Add comprehensive tests for get_socket_dir/getSocketDir to verify:
- AGENT_BROWSER_SOCKET_DIR takes priority
- Empty strings are ignored (fixes Rust/TypeScript consistency)
- XDG_RUNTIME_DIR fallback works correctly
- Home directory fallback when env vars unset

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
mmhiyoko
2026-01-22 01:15:25 -06:00
committed by GitHub
co-authored by Claude Opus 4.5
parent cb37630ccf
commit 946d236d9f
6 changed files with 401 additions and 30 deletions
+36 -5
View File
@@ -47,6 +47,31 @@ function getPortForSession(session: string): number {
return 49152 + (Math.abs(hash) % 16383);
}
/**
* Get the base directory for socket/pid files.
* Priority: AGENT_BROWSER_SOCKET_DIR > XDG_RUNTIME_DIR > ~/.agent-browser > tmpdir
*/
export function getSocketDir(): string {
// 1. Explicit override
if (process.env.AGENT_BROWSER_SOCKET_DIR) {
return process.env.AGENT_BROWSER_SOCKET_DIR;
}
// 2. XDG_RUNTIME_DIR (Linux standard)
if (process.env.XDG_RUNTIME_DIR) {
return path.join(process.env.XDG_RUNTIME_DIR, 'agent-browser');
}
// 3. Home directory fallback (like Docker Desktop's ~/.docker/run/)
const homeDir = os.homedir();
if (homeDir) {
return path.join(homeDir, '.agent-browser');
}
// 4. Last resort: temp dir
return path.join(os.tmpdir(), 'agent-browser');
}
/**
* Get the socket path for the current session (Unix) or port (Windows)
*/
@@ -55,7 +80,7 @@ export function getSocketPath(session?: string): string {
if (isWindows) {
return String(getPortForSession(sess));
}
return path.join(os.tmpdir(), `agent-browser-${sess}.sock`);
return path.join(getSocketDir(), `${sess}.sock`);
}
/**
@@ -63,7 +88,7 @@ export function getSocketPath(session?: string): string {
*/
export function getPortFile(session?: string): string {
const sess = session ?? currentSession;
return path.join(os.tmpdir(), `agent-browser-${sess}.port`);
return path.join(getSocketDir(), `${sess}.port`);
}
/**
@@ -71,7 +96,7 @@ export function getPortFile(session?: string): string {
*/
export function getPidFile(session?: string): string {
const sess = session ?? currentSession;
return path.join(os.tmpdir(), `agent-browser-${sess}.pid`);
return path.join(getSocketDir(), `${sess}.pid`);
}
/**
@@ -104,7 +129,7 @@ export function getConnectionInfo(
if (isWindows) {
return { type: 'tcp', port: getPortForSession(sess) };
}
return { type: 'unix', path: path.join(os.tmpdir(), `agent-browser-${sess}.sock`) };
return { type: 'unix', path: path.join(getSocketDir(), `${sess}.sock`) };
}
/**
@@ -133,7 +158,7 @@ export function cleanupSocket(session?: string): void {
*/
export function getStreamPortFile(session?: string): string {
const sess = session ?? currentSession;
return path.join(os.tmpdir(), `agent-browser-${sess}.stream`);
return path.join(getSocketDir(), `${sess}.stream`);
}
/**
@@ -141,6 +166,12 @@ export function getStreamPortFile(session?: string): string {
* @param options.streamPort Port for WebSocket stream server (0 to disable)
*/
export async function startDaemon(options?: { streamPort?: number }): Promise<void> {
// Ensure socket directory exists
const socketDir = getSocketDir();
if (!fs.existsSync(socketDir)) {
fs.mkdirSync(socketDir, { recursive: true });
}
// Clean up any stale socket
cleanupSocket();