Skip to content
Merged
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
20 changes: 15 additions & 5 deletions lib/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,24 @@ var daemonVersion = require("../package.json").version;
var configFile = process.env.CLAGENTIC_CONFIG || process.env.CLAY_CONFIG || require("./config").configPath();
var config;

// Ensure dirs exist before any file access — daemon must be self-sufficient when
// launched directly by systemd/supervisor with no prior CLI run.
ensureConfigDir();

try {
config = JSON.parse(fs.readFileSync(configFile, "utf8"));
} catch (e) {
console.error("[daemon] Failed to read config:", e.message);
process.exit(1);
if (e.code === "ENOENT") {
// No config file yet (fresh install, systemd-only start). Bootstrap minimal defaults.
console.warn("[daemon] No config file found at " + configFile + " — bootstrapping defaults. Run clagentic-console to configure projects.");
var _isDev = !!(process.env.CLAGENTIC_DEV || process.env.CLAY_DEV);
config = { port: _isDev ? 2635 : 2633, projects: [], mode: "single", setupCompleted: false };
// Persist so subsequent reads succeed.
fs.writeFileSync(configFile, JSON.stringify(config, null, 2), { mode: 0o600 });
} else {
console.error("[daemon] Failed to read config:", e.message);
process.exit(1);
}
}
console.log("[daemon] v" + daemonVersion + " PID " + process.pid + " config " + configFile);

Expand Down Expand Up @@ -1150,9 +1163,6 @@ 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
Loading