Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ delete process.env.CLAUDECODE;

var fs = require("fs");
var path = require("path");
var { loadConfig, saveConfig, socketPath, generateSlug, syncClayrc, removeFromClayrc, writeCrashInfo, readCrashInfo, clearCrashInfo, isPidAlive, clearStaleConfig, REAL_HOME } = require("./config");
var { loadConfig, saveConfig, socketPath, ensureConfigDir, generateSlug, syncClayrc, removeFromClayrc, writeCrashInfo, readCrashInfo, clearCrashInfo, isPidAlive, clearStaleConfig, REAL_HOME } = require("./config");
var { createIPCServer } = require("./ipc");
var { createServer, generateAuthToken } = require("./server");
var { checkAclSupport, grantProjectAccess, revokeProjectAccess, provisionAllUsers, provisionLinuxUser, grantAllUsersAccess, deactivateLinuxUser, ensureProjectsDir } = require("./os-users");
Expand Down Expand Up @@ -1150,6 +1150,9 @@ if (existingConfig && existingConfig.pid && existingConfig.pid !== process.pid)
clearStaleConfig();
}
}
// Ensure config dirs exist before binding the socket. The daemon may start
// without a prior CLI invocation (systemd, supervisor), so it must be self-sufficient.
ensureConfigDir();
var ipc = createIPCServer(socketPath(), function (msg) {
console.log("[daemon] IPC:", msg.cmd);
switch (msg.cmd) {
Expand Down
12 changes: 12 additions & 0 deletions lib/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ var fs = require("fs");
* handler(msg) should return a response object (or a Promise of one).
*/
function createIPCServer(sockPath, handler) {
// Ensure the socket's parent directory exists (e.g. ~/.clagentic/console/).
// The daemon may start without a prior CLI invocation, so it must be self-sufficient.
if (process.platform !== "win32") {
try { fs.mkdirSync(require("path").dirname(sockPath), { recursive: true }); } catch (e) {}
}

// Remove stale socket file (not needed for Windows named pipes)
if (process.platform !== "win32") {
try { fs.unlinkSync(sockPath); } catch (e) {}
Expand Down Expand Up @@ -53,6 +59,12 @@ function createIPCServer(sockPath, handler) {
console.log("[ipc] Socket in use, removing stale socket and retrying...");
try { fs.unlinkSync(sockPath); } catch (e) {}
server.listen(sockPath);
} else if (err.code === "ENOENT" && !retried) {
// Parent directory missing — create it and retry once.
retried = true;
console.log("[ipc] Socket directory missing, creating and retrying...");
try { fs.mkdirSync(require("path").dirname(sockPath), { recursive: true }); } catch (e) {}
server.listen(sockPath);
} else {
console.error("[ipc] Failed to bind socket:", err.message);
process.exit(1);
Expand Down
Loading