aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/daemon/spawn.ts34
-rw-r--r--src/util/tool.ts7
2 files changed, 26 insertions, 15 deletions
diff --git a/src/daemon/spawn.ts b/src/daemon/spawn.ts
index d051fcc..165abcd 100644
--- a/src/daemon/spawn.ts
+++ b/src/daemon/spawn.ts
@@ -33,22 +33,36 @@ const cleanupStaleSocket = (): void => {
};
/**
+ * Whether the daemon endpoint could plausibly exist, judged without connecting.
+ *
+ * On POSIX a missing socket file means the daemon definitely isn't running, so
+ * callers can short-circuit. Windows named pipes are NOT filesystem entries —
+ * access() always fails on `\\.\pipe\…` even while the daemon is serving it — so
+ * on Windows the answer is always "maybe" and the caller must probe instead.
+ *
+ * Platform and path are parameters so the branch is testable off-platform.
+ */
+export const daemonSocketMayExist = async (
+ platform: NodeJS.Platform = process.platform,
+ socketPath: string = DAEMON_SOCKET_PATH,
+): Promise<boolean> => {
+ if (platform === "win32") return true;
+ try {
+ await access(socketPath);
+ return true;
+ } catch {
+ return false;
+ }
+};
+
+/**
* Check whether the daemon is running by probing its Unix socket.
* A file-existence check is unreliable — a crashed daemon leaves a stale socket.
* We connect, send a register, wait for the registered ack, then disconnect.
* On failure, clean up the stale socket so ensureDaemon can spawn a fresh one.
*/
export const isDaemonRunning = async (timeoutMs = 2_000): Promise<boolean> => {
- // On POSIX, a missing socket file means the daemon definitely isn't running,
- // so we can short-circuit. Windows named pipes are not filesystem entries —
- // access() always fails there — so skip the pre-check and probe directly.
- if (!isWindows) {
- try {
- await access(DAEMON_SOCKET_PATH);
- } catch {
- return false;
- }
- }
+ if (!(await daemonSocketMayExist())) return false;
return new Promise((resolve) => {
let settled = false;
diff --git a/src/util/tool.ts b/src/util/tool.ts
index e4bb5ba..76087bc 100644
--- a/src/util/tool.ts
+++ b/src/util/tool.ts
@@ -8,8 +8,7 @@ import type {
ToolRenderResultOptions,
} from "@mariozechner/pi-coding-agent";
import type { Component } from "@mariozechner/pi-tui";
-import { access } from "node:fs/promises";
-import { DAEMON_SOCKET_PATH } from "../daemon/protocol";
+import { daemonSocketMayExist } from "../daemon/spawn";
import type { BrowserClient } from "../client";
import type { Result } from "./result";
@@ -141,9 +140,7 @@ export const registerBrowserTool = (
// Check daemon socket exists before attempting browser control.
// If missing, the user hasn't run /browser-setup yet. Return a clear
// error so the agent knows to direct the user to initialize.
- try {
- await access(DAEMON_SOCKET_PATH);
- } catch {
+ if (!(await daemonSocketMayExist())) {
return toToolResult(
{
success: false,