Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.
feat: introduce mercury up command to automate service installation and daemon startup, and add mercury restart functionality.
Salman Qureshia109f12c3060
diff --git a/README.md b/README.mdindex d4cf9306..96108101 100644--- a/README.md+++ b/README.md@@ -52,27 +52,31 @@ Every AI agent can read files, run commands, and fetch URLs. Most do it silently## Daemon Mode-Run Mercury in the background so Telegram and scheduled tasks keep working after you close the terminal.+**One command to make Mercury persistent:**```bash-# Start in background-mercury start -d+mercury up+```++This installs the system service (if not installed), starts the background daemon, and ensures Mercury is running. Use this as your go-to command.-# Check status-mercury status+If Mercury is already running, `mercury up` just confirms it and shows the PID.-# View logs-mercury logs+### Other daemon commands-# Stop background process-mercury stop+```bash+mercury restart # Restart the background process+mercury stop # Stop the background process+mercury start -d # Start in background (without service install)+mercury logs # View recent daemon logs+mercury status # Show if daemon is running```Daemon mode includes built-in crash recovery — if the process crashes, it restarts automatically with exponential backoff (up to 10 restarts per minute).### System Service (auto-start on boot)-Install Mercury as a system service so it starts automatically:+`mercury up` installs this automatically. You can also manage it directly:```bashmercury service install@@ -95,9 +99,11 @@ In daemon mode, Telegram becomes your primary channel — CLI is log-only since| Command | Description ||---------|-------------|+| `mercury up` | **Recommended.** Install service + start daemon + ensure running || `mercury` | Start the agent (same as `mercury start`) || `mercury start` | Start in foreground || `mercury start -d` | Start in background (daemon mode) |+| `mercury restart` | Restart the background process || `mercury stop` | Stop a background process || `mercury logs` | View recent daemon logs || `mercury doctor` | Reconfigure (Enter to keep current values) |diff --git a/docs/docs.html b/docs/docs.htmlindex d612c4f6..1ca7eadb 100644--- a/docs/docs.html+++ b/docs/docs.html@@ -291,6 +291,18 @@</div><h2 id="starting">Starting Mercury</h2>++ <div class="callout" style="border-left-color: var(--cyan); background: rgba(0, 212, 255, 0.06);">+ <div class="callout-title">Recommended: mercury up</div>+ <p>The easiest way to run Mercury persistently. Installs the system service if needed, starts the daemon, and+ ensures it's running.</p>+ </div>+ <div class="terminal-inline">+ <code>mercury up</code>+ </div>+ <p>If Mercury is already running, <code>mercury up</code> just confirms it and shows the PID. If the service isn't+ installed yet, it installs it first, then starts the daemon.</p>+<h3>Foreground (interactive)</h3><div class="terminal-inline"><code>mercury start</code>@@ -324,6 +336,10 @@</tr></thead><tbody>+ <tr>+ <td>mercury up</td>+ <td><strong>Recommended.</strong> Install service + start daemon + ensure running</td>+ </tr><tr><td>mercury</td><td>Start the agent (same as <code>mercury start</code>)</td>@@ -336,6 +352,10 @@<td>mercury start -d</td><td>Start in background (daemon mode)</td></tr>+ <tr>+ <td>mercury restart</td>+ <td>Restart a background Mercury process</td>+ </tr><tr><td>mercury stop</td><td>Stop a background Mercury process</td>@@ -440,6 +460,13 @@<h2 id="daemon">Daemon Mode</h2><p>Mercury can run in the background so Telegram and scheduled tasks keep working after you close the terminal.</p>+ <h3>The easy way: mercury up</h3>+ <div class="terminal-inline">+ <code>mercury up</code>+ </div>+ <p>This one command does everything: installs the system service if needed, starts the background daemon, and+ confirms Mercury is running. If it's already running, it just shows you the PID.</p>+<h3>Start in background</h3><div class="terminal-inline"><code>mercury start -d</code>@@ -454,6 +481,13 @@<p>Sends <code>SIGTERM</code> (<code>process.kill</code> on Windows) to the background process and removes the PIDfile.</p>+ <h3>Restart</h3>+ <div class="terminal-inline">+ <code>mercury restart</code>+ </div>+ <p>Stops the running daemon (if any) and starts a fresh one. Useful when something isn't working right and you need a+ clean restart.</p>+<h3>View logs</h3><div class="terminal-inline"><code>mercury logs</code>diff --git a/docs/index.html b/docs/index.htmlindex c85edfc6..56f38e94 100644--- a/docs/index.html+++ b/docs/index.html@@ -403,12 +403,11 @@<span class="terminal-title">terminal</span></div><div class="terminal-body" style="min-height: auto; padding: 16px 24px;">- <p style="margin:0;color:var(--gray);">$ <span style="color:var(--cyan);">mercury start -d</span></p>+ <p style="margin:0;color:var(--gray);">$ <span style="color:var(--cyan);">mercury up</span></p>+ <p style="margin:0;color:var(--cyan);">Installing Mercury as a system service...</p>+ <p style="margin:0;color:var(--green);">Mercury service installed (LaunchAgent)</p>+ <p style="margin:0;color:var(--cyan);">Starting Mercury in background...</p><p style="margin:0;color:var(--white);">Mercury started in background (PID: 48291)</p>- <p style="margin:0;color:var(--gray-dim);">Logs: ~/.mercury/daemon.log</p>- <p style="margin:0;color:var(--gray-dim);">Use `mercury stop` to stop.</p>- <p style="margin:8px 0 0;color:var(--gray);">$ <span style="color:var(--cyan);">mercury service install</span></p>- <p style="margin:0;color:var(--green);">Mercury service installed (macOS LaunchAgent)</p><p style="margin:0;color:var(--gray-dim);">Auto-starts on login. Auto-restarts on crash.</p></div></div>@@ -416,10 +415,10 @@<div class="cap-group" data-reveal><h3>Commands</h3><ul>- <li><code>mercury start -d</code> — Background daemon</li>+ <li><code>mercury up</code> — Install service + start daemon</li>+ <li><code>mercury start</code> — Foreground mode</li>+ <li><code>mercury restart</code> — Restart background process</li><li><code>mercury stop</code> — Stop background process</li>- <li><code>mercury logs</code> — View daemon logs</li>- <li><code>mercury service install</code> — Auto-start on boot</li><li><code>mercury doctor</code> — Reconfigure (Enter keeps values)</li><li><code>/help</code> <code>/status</code> <code>/budget</code> — In-chat commands</li></ul>diff --git a/src/cli/daemon.ts b/src/cli/daemon.tsindex 633b53bd..aab30c52 100644--- a/src/cli/daemon.ts+++ b/src/cli/daemon.ts@@ -115,6 +115,30 @@ export function stopDaemon(): void {console.log('');}+export function restartDaemon(): void {+ const status = getDaemonStatus();++ if (status.running && status.pid) {+ console.log(chalk.yellow(` Stopping Mercury (PID: ${status.pid})...`));+ try {+ if (process.platform === 'win32') {+ process.kill(status.pid);+ } else {+ process.kill(status.pid, 'SIGTERM');+ }+ } catch {+ // process may have already exited+ }+ try { unlinkSync(pidPath()); } catch {}+ console.log(chalk.green(' Mercury stopped.'));+ } else if (status.pid) {+ try { unlinkSync(pidPath()); } catch {}+ }++ console.log(chalk.yellow(' Starting Mercury...'));+ startBackground();+}+export function showLogs(): void {const logFile = logPath();if (!existsSync(logFile)) {diff --git a/src/cli/service.ts b/src/cli/service.tsindex 9be040ee..78a57916 100644--- a/src/cli/service.ts+++ b/src/cli/service.ts@@ -8,6 +8,25 @@ import { logger } from '../utils/logger.js';const SERVICE_NAME = 'mercury';const SERVICE_DESC = 'Mercury — Soul-Driven AI Agent';+const WIN_TASK_NAME = 'MercuryAgent';++export function isServiceInstalled(): boolean {+ const platform = process.platform;++ if (platform === 'darwin') {+ return existsSync(join(homedir(), 'Library', 'LaunchAgents', 'com.cosmicstack.mercury.plist'));+ } else if (platform === 'linux') {+ return existsSync(join(homedir(), '.config', 'systemd', 'user', 'mercury.service'));+ } else if (platform === 'win32') {+ try {+ execSync(`schtasks /query /tn "${WIN_TASK_NAME}"`, { stdio: 'pipe', shell: 'cmd.exe' });+ return true;+ } catch {+ return false;+ }+ }+ return false;+}function getNodeBinPath(): string {return process.execPath;@@ -288,8 +307,6 @@ function showLinuxStatus(): void {console.log('');}-const WIN_TASK_NAME = 'MercuryAgent';-function installWindows(): void {const nodeBin = getNodeBinPath();const scriptPath = getDistPath();diff --git a/src/index.ts b/src/index.tsindex 858143d6..43c000c2 100644--- a/src/index.ts+++ b/src/index.ts@@ -16,8 +16,8 @@ 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 } from './cli/daemon.js';-import { installService, uninstallService, showServiceStatus } from './cli/service.js';+import { startBackground, stopDaemon, showLogs, getDaemonStatus, restartDaemon } from './cli/daemon.js';+import { installService, uninstallService, showServiceStatus, isServiceInstalled } from './cli/service.js';import { runWithWatchdog } from './cli/watchdog.js';function hr() {@@ -348,6 +348,41 @@ programstopDaemon();});+program+ .command('restart')+ .description('Restart a background Mercury process')+ .action(() => {+ restartDaemon();+ });++program+ .command('up')+ .description('Ensure Mercury is running persistently — installs service if needed, starts daemon')+ .action(async () => {+ if (!isSetupComplete()) {+ await configure();+ }++ const daemon = getDaemonStatus();++ if (daemon.running && daemon.pid) {+ console.log('');+ console.log(chalk.green(` Mercury is already running (PID: ${daemon.pid})`));+ console.log(chalk.dim(` Logs: ${daemon.logPath}`));+ console.log('');+ return;+ }++ if (!isServiceInstalled()) {+ console.log('');+ console.log(chalk.cyan(' Installing Mercury as a system service...'));+ installService();+ }++ console.log(chalk.cyan(' Starting Mercury in background...'));+ startBackground();+ });+program.command('logs').description('Show recent daemon logs')diff --git a/src/utils/manual.ts b/src/utils/manual.tsindex 371c8eb1..d7d328db 100644--- a/src/utils/manual.ts+++ b/src/utils/manual.ts@@ -48,10 +48,12 @@ export function getManual(): string {sections.push('');const commands = [+ ['mercury up', 'Start persistently (install service + daemon)'],['mercury', 'Start the agent (same as mercury start)'],['mercury start', 'Start the agent in foreground'],['mercury start -d', 'Start in background (daemon mode)'],- ['mercury stop', 'Stop a background Mercury process'],+ ['mercury restart', 'Restart a background process'],+ ['mercury stop', 'Stop a background process'],['mercury logs', 'Show recent daemon logs'],['mercury doctor', 'Reconfigure settings (Enter keeps current)'],['mercury setup', 'Re-run the setup wizard'],