@cosmicstack/mercury-agent 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -7491,21 +7491,20 @@ function isProcessRunning(pid) {
7491
7491
  function getDaemonStatus() {
7492
7492
  const pid = readPid();
7493
7493
  if (!pid) return { running: false, pid: null, logPath: logPath() };
7494
- return { running: isProcessRunning(pid), pid, logPath: logPath() };
7495
- }
7496
- function startBackground() {
7497
- const status = getDaemonStatus();
7498
- if (status.running && status.pid) {
7499
- console.log(chalk5.yellow(` Mercury is already running (PID: ${status.pid})`));
7500
- console.log(chalk5.dim(` Use \`mercury stop\` to stop it first.`));
7501
- console.log("");
7502
- process2.exit(1);
7503
- }
7504
- if (status.pid && !status.running) {
7494
+ const running = isProcessRunning(pid);
7495
+ if (!running) {
7505
7496
  try {
7506
7497
  unlinkSync4(pidPath());
7507
7498
  } catch {
7508
7499
  }
7500
+ return { running: false, pid: null, logPath: logPath() };
7501
+ }
7502
+ return { running, pid, logPath: logPath() };
7503
+ }
7504
+ function ensureDaemonRunning() {
7505
+ const status = getDaemonStatus();
7506
+ if (status.running && status.pid) {
7507
+ return { pid: status.pid, fresh: false };
7509
7508
  }
7510
7509
  const home = getMercuryHome();
7511
7510
  if (!existsSync17(home)) {
@@ -7521,13 +7520,25 @@ function startBackground() {
7521
7520
  windowsHide: isWin
7522
7521
  });
7523
7522
  child.unref();
7523
+ if (!child.pid) {
7524
+ throw new Error("Failed to spawn daemon process");
7525
+ }
7524
7526
  writeFileSync12(pidPath(), String(child.pid));
7525
- console.log("");
7526
- console.log(chalk5.green(` Mercury started in background (PID: ${child.pid})`));
7527
- console.log(chalk5.dim(` Logs: ${logFile}`));
7528
- console.log(chalk5.dim(` Use \`mercury stop\` to stop.`));
7529
- console.log(chalk5.dim(` Use \`mercury logs\` to view logs.`));
7530
- console.log("");
7527
+ return { pid: child.pid, fresh: true };
7528
+ }
7529
+ function startBackground() {
7530
+ try {
7531
+ const result = ensureDaemonRunning();
7532
+ console.log("");
7533
+ console.log(chalk5.green(` Mercury started in background (PID: ${result.pid})`));
7534
+ console.log(chalk5.dim(` Logs: ${logPath()}`));
7535
+ console.log(chalk5.dim(` Use \`mercury stop\` to stop.`));
7536
+ console.log(chalk5.dim(` Use \`mercury logs\` to view logs.`));
7537
+ console.log("");
7538
+ } catch (err) {
7539
+ console.log(chalk5.red(` Failed to start: ${err.message}`));
7540
+ process2.exit(1);
7541
+ }
7531
7542
  }
7532
7543
  function stopDaemon() {
7533
7544
  const status = getDaemonStatus();
@@ -7578,11 +7589,6 @@ function restartDaemon() {
7578
7589
  } catch {
7579
7590
  }
7580
7591
  console.log(chalk5.green(" Mercury stopped."));
7581
- } else if (status.pid) {
7582
- try {
7583
- unlinkSync4(pidPath());
7584
- } catch {
7585
- }
7586
7592
  }
7587
7593
  console.log(chalk5.yellow(" Starting Mercury..."));
7588
7594
  startBackground();
@@ -7595,40 +7601,13 @@ function showLogs() {
7595
7601
  return;
7596
7602
  }
7597
7603
  const content = readFileSync11(logFile, "utf-8");
7598
- const lines = content.split("\n").slice(-100);
7604
+ const lines = content.split(/\r?\n/).slice(-100);
7599
7605
  console.log(lines.join("\n"));
7600
7606
  }
7601
7607
  function tryAutoDaemonize() {
7602
7608
  try {
7603
- const status = getDaemonStatus();
7604
- if (status.running && status.pid) {
7605
- return true;
7606
- }
7607
- if (status.pid && !status.running) {
7608
- try {
7609
- unlinkSync4(pidPath());
7610
- } catch {
7611
- }
7612
- }
7613
- const home = getMercuryHome();
7614
- if (!existsSync17(home)) {
7615
- mkdirSync10(home, { recursive: true });
7616
- }
7617
- const logFile = logPath();
7618
- const isWin = process2.platform === "win32";
7619
- const outFd = openSync(logFile, "a");
7620
- const child = spawn(process2.execPath, [process2.argv[1], "start", "--daemon"], {
7621
- detached: true,
7622
- stdio: ["ignore", outFd, outFd],
7623
- env: { ...process2.env },
7624
- windowsHide: isWin
7625
- });
7626
- child.unref();
7627
- if (!child.pid) {
7628
- return false;
7629
- }
7630
- writeFileSync12(pidPath(), String(child.pid));
7631
- return true;
7609
+ const result = ensureDaemonRunning();
7610
+ return result.pid > 0;
7632
7611
  } catch {
7633
7612
  return false;
7634
7613
  }
@@ -8798,7 +8777,7 @@ async function configure(existingConfig) {
8798
8777
  }
8799
8778
  function autoDaemonize() {
8800
8779
  const daemon = getDaemonStatus();
8801
- if (daemon.running) {
8780
+ if (daemon.running && daemon.pid) {
8802
8781
  return;
8803
8782
  }
8804
8783
  console.log(chalk7.dim(" Setting up background mode..."));
@@ -8816,7 +8795,7 @@ function autoDaemonize() {
8816
8795
  console.log(chalk7.green(" \u2713 Auto-starts on login. Auto-restarts on crash."));
8817
8796
  console.log(chalk7.dim(" Use `mercury stop` to stop. `mercury restart` to restart."));
8818
8797
  } else {
8819
- console.log(chalk7.dim(" Background mode not available. Run `mercury up` to set it up."));
8798
+ console.log(chalk7.yellow(" Background mode not available. Run `mercury start` to set it up."));
8820
8799
  }
8821
8800
  console.log("");
8822
8801
  }
@@ -9006,6 +8985,22 @@ async function runAgent(isDaemon = false) {
9006
8985
  };
9007
8986
  process.on("SIGINT", shutdown);
9008
8987
  process.on("SIGTERM", shutdown);
8988
+ if (!isDaemon && process.platform !== "win32") {
8989
+ process.on("SIGHUP", () => {
8990
+ logger.info("SIGHUP received \u2014 terminal closed. Daemonizing.");
8991
+ try {
8992
+ const result = tryAutoDaemonize();
8993
+ if (result) {
8994
+ logger.info(`Forked daemon. Foreground process exiting.`);
8995
+ } else {
8996
+ logger.warn("SIGHUP received but daemonization failed. Shutting down.");
8997
+ }
8998
+ } catch {
8999
+ logger.warn("SIGHUP received but daemonization failed. Shutting down.");
9000
+ }
9001
+ process.exit(0);
9002
+ });
9003
+ }
9009
9004
  }
9010
9005
  var program = new Command();
9011
9006
  program.name("mercury").description("Mercury \u2014 Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access.").version(pkgVersion).option("-v, --verbose", "Show debug logs").action(async () => {
@@ -9014,22 +9009,24 @@ program.name("mercury").description("Mercury \u2014 Soul-driven AI agent with pe
9014
9009
  autoDaemonize();
9015
9010
  return;
9016
9011
  }
9012
+ autoDaemonize();
9017
9013
  await runAgent();
9018
9014
  });
9019
- program.command("start").description("Start Mercury agent").option("-v, --verbose", "Show debug logs").option("-d, --detached", "Run in background (daemon mode)").option("--daemon", "Internal flag for daemon child process").action(async (opts) => {
9015
+ program.command("start").description("Start Mercury \u2014 runs as a daemon by default, use --foreground to attach to terminal").option("-v, --verbose", "Show debug logs").option("-f, --foreground", "Run in foreground (attached to terminal)").option("-d, --detached", "Run in background (daemon mode) \u2014 same as default").option("--daemon", "Internal flag for daemon child process").action(async (opts) => {
9020
9016
  if (opts.daemon) {
9021
9017
  await runWithWatchdog(() => runAgent(true));
9022
9018
  return;
9023
9019
  }
9024
- if (opts.detached) {
9025
- startBackground();
9026
- return;
9027
- }
9028
9020
  if (!isSetupComplete()) {
9029
9021
  await configure();
9022
+ autoDaemonize();
9030
9023
  return;
9031
9024
  }
9032
- await runAgent();
9025
+ if (opts.foreground) {
9026
+ await runAgent();
9027
+ return;
9028
+ }
9029
+ startBackground();
9033
9030
  });
9034
9031
  program.command("stop").description("Stop a background Mercury process").action(() => {
9035
9032
  stopDaemon();
@@ -9037,9 +9034,11 @@ program.command("stop").description("Stop a background Mercury process").action(
9037
9034
  program.command("restart").description("Restart a background Mercury process").action(() => {
9038
9035
  restartDaemon();
9039
9036
  });
9040
- program.command("up").description("Ensure Mercury is running persistently \u2014 installs service if needed, starts daemon").action(async () => {
9037
+ program.command("up").description("Start Mercury as a persistent daemon (same as `mercury start`)").action(async () => {
9041
9038
  if (!isSetupComplete()) {
9042
9039
  await configure();
9040
+ autoDaemonize();
9041
+ return;
9043
9042
  }
9044
9043
  const daemon = getDaemonStatus();
9045
9044
  if (daemon.running && daemon.pid) {
@@ -9054,7 +9053,6 @@ program.command("up").description("Ensure Mercury is running persistently \u2014
9054
9053
  console.log(chalk7.cyan(" Installing Mercury as a system service..."));
9055
9054
  installService();
9056
9055
  }
9057
- console.log(chalk7.cyan(" Starting Mercury in background..."));
9058
9056
  startBackground();
9059
9057
  });
9060
9058
  program.command("logs").description("Show recent daemon logs").action(() => {