Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

feat(cli): `mercury code [dir]` — launch the Mercury Code TUI from the terminal

Salman Qureshi11c173c08393
diff --git a/src/index-code-boot.test.ts b/src/index-code-boot.test.ts
new file mode 100644
index 00000000..76fbb1b2
--- /dev/null
+++ b/src/index-code-boot.test.ts
@@ -0,0 +1,31 @@
+import { describe, expect, it } from 'vitest';
+import { readFileSync } from 'node:fs';
+import { fileURLToPath } from 'node:url';
+import { dirname, join } from 'node:path';
+
+const src = readFileSync(join(dirname(fileURLToPath(import.meta.url)), 'index.ts'), 'utf8');
+
+/**
+ * `mercury code [dir]` contract: a dedicated CLI command that boots the TUI
+ * straight into Mercury Code in the given (default: current) directory with a
+ * FRESH session — the terminal equivalent of typing /code after launch.
+ */
+describe('mercury code launcher', () => {
+ it('registers a dedicated code command with a fresh-session boot', () => {
+ expect(src).toContain(".command('code')");
+ // Fresh session: create + bind to the CLI current binding, and surface it.
+ expect(src).toContain('sessions.create()');
+ expect(src).toContain("sessions.bind(fresh.id, 'cli', 'current')");
+ expect(src).toContain('bootCli.setCurrentSession(activeSession)');
+ // Boots into Mercury Code with the AUTO programming flow, mirroring /code.
+ expect(src).toContain('bootCli.enterMercuryCode(bootDir, pkgVersion)');
+ expect(src).toContain('agent.programmingMode.setAuto()');
+ });
+
+ it('passes the boot flag only to the foreground TUI process', () => {
+ // The flag is set by the command action and consumed in runAgent's
+ // !isDaemon path — the daemon child must never enter Mercury Code.
+ expect(src).toContain('process.env.MERCURY_BOOT_CODE');
+ expect(src).toMatch(/if \(!isDaemon\) \{[\s\S]*?MERCURY_BOOT_CODE/s);
+ });
+});
\ No newline at end of file
diff --git a/src/index.ts b/src/index.ts
index 7a2df3d4..6a22245e 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,6 +1,6 @@
-import { readFileSync, writeFileSync, existsSync } from 'node:fs';
+import { readFileSync, writeFileSync, existsSync, statSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
-import { dirname, join } from 'node:path';
+import { dirname, join, resolve as pathResolve } from 'node:path';
import { Command } from 'commander';
import readline from 'node:readline';
import chalk from 'chalk';
@@ -2443,6 +2443,25 @@ async function runAgent(isDaemon: boolean = false): Promise<void> {
try { agent.cancelActiveWork('Mercury Code was exited from the TUI.'); } catch { /* best effort */ }
process.exit(0);
});
+ // `mercury code [dir]`: boot straight into the Mercury Code TUI in the
+ // requested (default: current) directory with a FRESH session. The flag
+ // is set by the `code` command action and consumed here, in the
+ // foreground process only (the daemon child ignores it).
+ if (process.env.MERCURY_BOOT_CODE) {
+ const bootDir = process.env.MERCURY_BOOT_CODE === '1' ? process.cwd() : process.env.MERCURY_BOOT_CODE;
+ delete process.env.MERCURY_BOOT_CODE;
+ const fresh = sessions.create();
+ sessions.bind(fresh.id, 'cli', 'current');
+ const activeSession = sessions.get(fresh.id);
+ if (activeSession) bootCli.setCurrentSession(activeSession);
+ agent.programmingMode.setAuto();
+ agent.programmingMode.setProjectContext(bootDir);
+ const entered = bootCli.enterMercuryCode(bootDir, pkgVersion);
+ bootCli.setProgrammingStatus(agent.programmingMode.getState(), agent.programmingMode.getProjectContext());
+ await bootCli.send(entered.ok
+ ? 'Mercury Code active (AUTO). Describe the change — I will plan and build in one flow, confirming with you only before large or consequential changes.'
+ : `Mercury Code could not start: ${entered.message}`);
+ }
} else {
await channels.startAll();
}
@@ -4504,6 +4523,37 @@ serviceCmd
console.log('');
});
+program
+ .command('code')
+ .description('Open the Mercury Code TUI in a directory (default: current) with a fresh session')
+ .argument('[dir]', 'Directory to work in (defaults to the current one)')
+ .action(async (dirArg?: string) => {
+ const target = dirArg ? pathResolve(dirArg) : process.cwd();
+ if (!existsSync(target) || !statSync(target).isDirectory()) {
+ console.log(chalk.red(` ✗ Not a directory: ${target}`));
+ console.log('');
+ process.exit(1);
+ }
+ // Consumed by runAgent() in the foreground process (the daemon child
+ // ignores it — only the TUI boots into Mercury Code).
+ process.env.MERCURY_BOOT_CODE = dirArg ? target : '1';
+ if (!isSetupComplete()) {
+ await configure();
+ delete process.env.MERCURY_BOOT_CODE;
+ autoDaemonize();
+ return;
+ }
+ const foreground = getForegroundRuntimeStatus();
+ if (foreground.running && foreground.pid) {
+ console.log(chalk.cyan(` ⚿ Mercury is already running (PID: ${foreground.pid}) — \`mercury code\` needs its own runtime.`));
+ console.log(chalk.dim(' Stop it first: mercury stop (or use /code inside the running TUI).'));
+ console.log('');
+ return;
+ }
+ autoDaemonize();
+ await runAgent();
+ });
+
program
.command('uninstall')
.description('Fully remove Mercury from this machine (runtime, service, npm/binary artifacts)')
diff --git a/src/utils/manual.ts b/src/utils/manual.ts
index 56761ec6..eea00979 100644
--- a/src/utils/manual.ts
+++ b/src/utils/manual.ts
@@ -93,6 +93,8 @@ export function getManual(): string {
['mercury service install', 'Install as system service (auto-start)'],
['mercury service uninstall', 'Uninstall system service'],
['mercury service status', 'Show system service status'],
+ ['mercury code', 'Open the Mercury Code TUI here (fresh session)'],
+ ['mercury code <dir>', 'Open Mercury Code in <dir>'],
['mercury uninstall', 'Fully remove Mercury (runtime, service, npm/binary artifacts)'],
['mercury uninstall --purge-data', 'Also delete all Mercury data (~/.mercury)'],
['mercury --verbose', 'Start with debug logging on stderr'],