Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.
feat: implement auto-daemonization during initial setup to run Mercury in the background
Salman Qureshi4bcf960f66da
diff --git a/src/cli/daemon.ts b/src/cli/daemon.tsindex aab30c52..7476f57e 100644--- a/src/cli/daemon.ts+++ b/src/cli/daemon.ts@@ -149,4 +149,44 @@ export function showLogs(): void {const content = readFileSync(logFile, 'utf-8');const lines = content.split('\n').slice(-100);console.log(lines.join('\n'));+}++export function tryAutoDaemonize(): boolean {+ try {+ const status = getDaemonStatus();+ if (status.running && status.pid) {+ return true;+ }++ if (status.pid && !status.running) {+ try { unlinkSync(pidPath()); } catch {}+ }++ const home = getMercuryHome();+ if (!existsSync(home)) {+ mkdirSync(home, { recursive: true });+ }++ const logFile = logPath();+ const isWin = process.platform === 'win32';+ const outFd = openSync(logFile, 'a');++ const child = spawn(process.execPath, [process.argv[1], 'start', '--daemon'], {+ detached: true,+ stdio: ['ignore', outFd, outFd],+ env: { ...process.env },+ windowsHide: isWin,+ });++ child.unref();++ if (!child.pid) {+ return false;+ }++ writeFileSync(pidPath(), String(child.pid));+ return true;+ } catch {+ return false;+ }}\ No newline at end of filediff --git a/src/index.ts b/src/index.tsindex 43c000c2..2fd408c4 100644--- a/src/index.ts+++ b/src/index.ts@@ -16,7 +16,7 @@ import { TokenBudget } from './utils/tokens.js';import { CapabilityRegistry } from './capabilities/registry.js';import { SkillLoader } from './skills/loader.js';import { getManual } from './utils/manual.js';-import { startBackground, stopDaemon, showLogs, getDaemonStatus, restartDaemon } from './cli/daemon.js';+import { startBackground, stopDaemon, showLogs, getDaemonStatus, restartDaemon, tryAutoDaemonize } from './cli/daemon.js';import { installService, uninstallService, showServiceStatus, isServiceInstalled } from './cli/service.js';import { runWithWatchdog } from './cli/watchdog.js';@@ -180,11 +180,39 @@ async function configure(existingConfig?: MercuryConfig): Promise<void> {console.log(chalk.green(` ✓ Permissions seeded in ${home}/permissions.yaml`));console.log(chalk.green(` ✓ Skills directory ready in ${home}/skills/`));console.log('');- console.log(chalk.cyan(` ${config.identity.name} is ready. Run \`mercury start\` to begin.`));+ console.log(chalk.cyan(` ${config.identity.name} is ready. Run \`mercury start\` to chat.`));console.log(chalk.dim(' mercury.cosmicstack.org'));console.log('');}+function autoDaemonize(): void {+ const daemon = getDaemonStatus();+ if (daemon.running) {+ return;+ }++ console.log(chalk.dim(' Setting up background mode...'));++ try {+ if (!isServiceInstalled()) {+ installService();+ }+ } catch {+ console.log(chalk.dim(' Service install skipped (can run `mercury service install` later).'));+ }++ const ok = tryAutoDaemonize();+ if (ok) {+ const status = getDaemonStatus();+ console.log(chalk.green(` ✓ Mercury is running in background (PID: ${status.pid})`));+ console.log(chalk.green(' ✓ Auto-starts on login. Auto-restarts on crash.'));+ console.log(chalk.dim(' Use `mercury stop` to stop. `mercury restart` to restart.'));+ } else {+ console.log(chalk.dim(' Background mode not available. Run `mercury up` to set it up.'));+ }+ console.log('');+}+async function runAgent(isDaemon: boolean = false): Promise<void> {let config = loadConfig();config = ensureCreatorField(config);@@ -312,6 +340,7 @@ program.action(async () => {if (!isSetupComplete()) {await configure();+ autoDaemonize();return;}await runAgent();