diff --git a/src/cli.test.ts b/src/cli.test.ts index 46d7e45..fb4e8f5 100644 --- a/src/cli.test.ts +++ b/src/cli.test.ts @@ -44,6 +44,8 @@ describe("CLI version & help handling", () => { expect(result.status).toBe(0); expect(result.stdout).toContain("seal-home CLI"); expect(result.stdout).toContain("Usage:"); + expect(result.stdout).toContain("seal-home mcp"); + expect(result.stdout).toContain("seal-home probe []"); expect(result.stdout).toContain("seal-home users search "); expect(result.stdout).toContain("seal-home users delegation "); expect(result.stdout).toContain("seal-home webhook get|find "); diff --git a/src/cli.ts b/src/cli.ts index 4f28195..b5f56da 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,19 +1,18 @@ #!/usr/bin/env bun -import { dirname } from "node:path"; import { fileURLToPath } from "node:url"; import { runCli } from "./cli/runner.js"; +import { isCompiledBinary, resolveAppRoot, resolveDefaultLauncher } from "./core/runtime.js"; const entryFile = fileURLToPath(import.meta.url); -const appRoot = dirname(dirname(entryFile)); +const isCompiled = isCompiledBinary(); +const appRoot = resolveAppRoot(entryFile, isCompiled); +const launcher = resolveDefaultLauncher(entryFile, isCompiled); const result = await runCli(Bun.argv.slice(2), { appRoot, entryFile, - launcher: { - command: process.execPath, - args: [entryFile] - } + launcher }); if (result.stdout) process.stdout.write(result.stdout); diff --git a/src/cli/runner.ts b/src/cli/runner.ts index 3574e7f..dc4cb27 100755 --- a/src/cli/runner.ts +++ b/src/cli/runner.ts @@ -24,6 +24,8 @@ import { clearCorpTokenCache } from "../core/auth/token-store.js"; import { getHoseProvisionalAuthLink } from "../core/auth/hose.js"; import { VERSION } from "../version.js"; import { runServiceCommand, serviceStatus, type ServiceConfig } from "./service.js"; +import { startServer } from "../server.js"; +import { runProbe } from "../probe.js"; const DEFAULT_APP_ROOT = dirname(dirname(dirname(fileURLToPath(import.meta.url)))); const STATE_DIR = join(homedir(), ".config", "seal-home"); @@ -50,7 +52,10 @@ const KNOWN_AREAS = new Set([ "context", "rules", "approval-runs", - "simulation" + "simulation", + "mcp", + "server", + "probe" ]); type ParsedArgs = { @@ -198,6 +203,17 @@ async function dispatch(argv: string[], runtime: CliRuntime) { return; } + if (area === "mcp" || area === "server") { + await startServer(); + return; + } + + if (area === "probe") { + const corpId = maybeName ?? stringOption(args.options.corp) ?? (action && action !== "--corp" ? action : undefined); + await runProbe({ corpId }); + return; + } + if (area === "service") { const result = await runServiceCommand(action, { appRoot: runtime.appRoot, @@ -1561,6 +1577,8 @@ function printHelp() { Usage: seal-home version + seal-home mcp + seal-home probe [] seal-home config paths seal-home config get seal-home config mode [--corp ] diff --git a/src/cli/service.ts b/src/cli/service.ts index 3e96349..79591a3 100644 --- a/src/cli/service.ts +++ b/src/cli/service.ts @@ -133,7 +133,11 @@ function isSealHomeProcess(pid: number, appRoot: string) { try { const command = execFileSync("ps", ["-p", String(pid), "-o", "command="], { encoding: "utf8" }); - return command.includes(appRoot) && command.includes("service") && command.includes("run"); + const matchesTarget = + command.includes(appRoot) || + command.includes("seal-home") || + command.includes("src/cli.ts"); + return matchesTarget && command.includes("service") && command.includes("run"); } catch { return false; } diff --git a/src/core/runtime.test.ts b/src/core/runtime.test.ts new file mode 100644 index 0000000..84a6b0d --- /dev/null +++ b/src/core/runtime.test.ts @@ -0,0 +1,33 @@ +import { describe, expect, test } from "bun:test"; +import { isCompiledBinary, resolveAppRoot, resolveDefaultLauncher } from "./runtime.js"; + +describe("runtime detection and launcher", () => { + test("isCompiledBinary detects bun runtime executable vs standalone binary", () => { + expect(isCompiledBinary("/Users/user/.bun/bin/bun", "file:///Users/user/code/src/cli.ts")).toBe(false); + expect(isCompiledBinary("C:\\Users\\user\\.bun\\bin\\bun.exe", "file:///C:/code/src/cli.ts")).toBe(false); + expect(isCompiledBinary("/usr/local/bin/seal-home", "file:///Users/user/code/src/cli.ts")).toBe(true); + expect(isCompiledBinary("/Users/user/.bun/bin/bun", "file:///$bunfs/root/src/cli.ts")).toBe(true); + }); + + test("resolveAppRoot returns cwd for compiled binary and project root for source", () => { + const sourceRoot = resolveAppRoot("/Users/user/code/src/cli.ts", false); + expect(sourceRoot).toBe("/Users/user/code"); + + const binaryRoot = resolveAppRoot(undefined, true); + expect(binaryRoot).toBe(process.cwd()); + }); + + test("resolveDefaultLauncher configures args based on compiled mode", () => { + const sourceLauncher = resolveDefaultLauncher("/Users/user/code/src/cli.ts", false, "/path/to/bun"); + expect(sourceLauncher).toEqual({ + command: "/path/to/bun", + args: ["/Users/user/code/src/cli.ts"] + }); + + const binaryLauncher = resolveDefaultLauncher("/ignored", true, "/usr/local/bin/seal-home"); + expect(binaryLauncher).toEqual({ + command: "/usr/local/bin/seal-home", + args: [] + }); + }); +}); diff --git a/src/core/runtime.ts b/src/core/runtime.ts new file mode 100644 index 0000000..9e2ae93 --- /dev/null +++ b/src/core/runtime.ts @@ -0,0 +1,40 @@ +import { dirname } from "node:path"; +import { fileURLToPath } from "node:url"; + +export function isCompiledBinary(execPath = process.execPath, metaUrl = import.meta.url): boolean { + if (metaUrl.includes("/$bunfs/") || metaUrl.includes("\\$bunfs\\")) { + return true; + } + const lower = execPath.toLowerCase().replace(/\\/g, "/"); + return !lower.endsWith("/bun") && !lower.endsWith("/bun.exe"); +} + +export function resolveAppRoot(entryFile?: string, isCompiled = isCompiledBinary()): string { + if (isCompiled) { + return process.cwd(); + } + const file = entryFile || fileURLToPath(import.meta.url); + return dirname(dirname(file)); +} + +export type LauncherConfig = { + command: string; + args?: string[]; +}; + +export function resolveDefaultLauncher( + entryFile: string, + isCompiled = isCompiledBinary(), + execPath = process.execPath +): LauncherConfig { + if (isCompiled) { + return { + command: execPath, + args: [] + }; + } + return { + command: execPath, + args: [entryFile] + }; +} diff --git a/src/probe.ts b/src/probe.ts index 3fd3c35..f8b7505 100644 --- a/src/probe.ts +++ b/src/probe.ts @@ -4,9 +4,7 @@ import { createSealClient } from "./core/http/factory.js"; import { resolveLiveSealEnterpriseConfig } from "./domains/seal/source.js"; import { getApprovalContext } from "./domains/seal/api.js"; -async function main() { - const args = Bun.argv.slice(2); - const corpId = args[0] === "--corp" ? args[1] : args[0]; +export async function runProbe(options: { corpId?: string } = {}) { const corps = loadCorpConfigs(); const globalConfig = loadGlobalConfig(); const corp = resolveCorpConfig( @@ -14,7 +12,7 @@ async function main() { { stateful: globalConfig.stateful, defaultCorpId: globalConfig.defaultCorpId, - corpId + corpId: options.corpId }, getEnterprisesDirCandidates() ); @@ -43,7 +41,11 @@ async function main() { }, null, 2)); } -main().catch((error) => { - console.error(error instanceof Error ? error.message : error); - process.exit(1); -}); +if (import.meta.main) { + const args = process.argv.slice(2); + const corpId = args[0] === "--corp" ? args[1] : args[0]; + runProbe({ corpId }).catch((error) => { + console.error(error instanceof Error ? error.message : error); + process.exit(1); + }); +} diff --git a/src/server.ts b/src/server.ts index a4bd260..907b7b2 100644 --- a/src/server.ts +++ b/src/server.ts @@ -199,9 +199,10 @@ async function runEmbeddedCli() { process.exitCode = result.exitCode; } -const isMainModule = process.argv[1] - ? resolve(process.argv[1]) === resolve(serverEntryFile) - : false; +const isMainModule = Boolean( + (import.meta as any).main || + (!import.meta.url.includes("$bunfs") && process.argv[1] && resolve(process.argv[1]) === resolve(serverEntryFile)) +); if (isMainModule) { if (process.argv[2] === "--seal-home-cli") {