All files / kernel-cli/src/commands daemon.ts

40% Statements 48/120
39.13% Branches 36/92
42.85% Functions 6/14
41.02% Lines 48/117

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396                                        1x               2x     2x                   3x                                                                                                                                                                                                                                                 5x 5x   1x 1x                                 8x 7x 7x 2x     2x 2x     5x 5x 2x     2x 2x     3x   3x         3x         1x 1x 1x     2x           2x 1x     1x 1x   1x   1x   2x                                                                                                                                                                                     2x             2x 1x 1x     1x                                                           5x             5x 1x 1x       4x 1x   3x     4x 5x     4x      
import { deleteDaemonState } from '@metamask/kernel-node-runtime/daemon';
import { isCapData, prettifySmallcaps } from '@metamask/kernel-utils';
import type { JsonRpcFailure } from '@metamask/utils';
import { isJsonRpcFailure } from '@metamask/utils';
import { readFile, rm } from 'node:fs/promises';
import { homedir } from 'node:os';
import { join, resolve } from 'node:path';
import { pathToFileURL } from 'node:url';
 
import { getOcapHome } from '../ocap-home.ts';
import {
  isErrorWithCode,
  isProcessAlive,
  readPidFile,
  waitFor,
} from '../utils.ts';
import { pingDaemon, sendCommand } from './daemon-client.ts';
import { ensureDaemon } from './daemon-spawn.ts';
import { getRelayAddrPath, getRelayPidPath } from './relay.ts';
 
const home = homedir();
 
/**
 * Format a JSON-RPC error for stderr and set exit code 1.
 *
 * @param response - The failed JSON-RPC response.
 */
function writeRpcError(response: JsonRpcFailure): void {
  process.stderr.write(
    `Error: ${response.error.message} (code ${String(response.error.code)})\n`,
  );
  process.exitCode = 1;
}
 
/**
 * Replace the home directory prefix with `~` for display.
 *
 * @param path - An absolute path.
 * @returns The path with the home prefix replaced.
 */
function tildefy(path: string): string {
  return path.startsWith(home) ? `~${path.slice(home.length)}` : path;
}
 
/**
 * Check if a value looks like a cluster config (has bootstrap + vats).
 *
 * @param value - The value to check.
 * @returns True if the value has bootstrap and vats fields.
 */
function isClusterConfigLike(
  value: unknown,
): value is { vats: Record<string, { bundleSpec?: string }> } {
  return (
    typeof value === 'object' &&
    value !== null &&
    'bootstrap' in value &&
    'vats' in value &&
    typeof (value as { vats: unknown }).vats === 'object'
  );
}
 
/**
 * Resolve relative bundleSpec paths in a cluster config to file:// URLs.
 *
 * @param config - The cluster config object.
 * @param config.vats - The vat configurations with optional bundleSpec paths.
 * @returns The config with resolved bundleSpec URLs.
 */
function resolveBundleSpecs(config: {
  vats: Record<string, { bundleSpec?: string }>;
}): unknown {
  const resolvedVats: Record<string, unknown> = {};
  for (const [vatName, vatConfig] of Object.entries(config.vats)) {
    const spec = vatConfig.bundleSpec;
    if (spec && !spec.includes('://')) {
      resolvedVats[vatName] = {
        ...vatConfig,
        bundleSpec: pathToFileURL(resolve(spec)).href,
      };
    } else {
      resolvedVats[vatName] = vatConfig;
    }
  }
  return { ...config, vats: resolvedVats };
}
 
/**
 * Stop the daemon via a `shutdown` RPC call. Falls back to PID + SIGTERM if
 * the socket is unresponsive, and escalates to SIGKILL if SIGTERM is ignored.
 *
 * @param socketPath - The daemon socket path.
 * @returns True if the daemon was stopped (or was not running), false if it
 * failed to stop within the timeout.
 */
export async function stopDaemon(socketPath: string): Promise<boolean> {
  const pidPath = join(getOcapHome(), 'daemon.pid');
  const pid = await readPidFile(pidPath);
  const processAlive = pid !== undefined && isProcessAlive(pid);
  const socketResponsive = await pingDaemon(socketPath);
 
  if (!socketResponsive && !processAlive) {
    if (pid !== undefined) {
      await rm(pidPath, { force: true });
    }
    process.stderr.write('Daemon is not running.\n');
    return true;
  }
 
  process.stderr.write('Stopping daemon...\n');
 
  let stopped = false;
 
  // Strategy 1: Graceful socket-based shutdown.
  if (socketResponsive) {
    try {
      await sendCommand({ socketPath, method: 'shutdown', timeoutMs: 10_000 });
    } catch {
      // Socket became unresponsive.
    }
    stopped = await waitFor(async () => !(await pingDaemon(socketPath)), 5_000);
  }
 
  // Strategy 2: SIGTERM.
  if (!stopped && pid !== undefined) {
    try {
      process.kill(pid, 'SIGTERM');
    } catch {
      stopped = true;
    }
    if (!stopped) {
      stopped = await waitFor(() => !isProcessAlive(pid), 5_000);
    }
  }
 
  // Strategy 3: SIGKILL.
  if (!stopped && pid !== undefined) {
    try {
      process.kill(pid, 'SIGKILL');
    } catch {
      stopped = true;
    }
    if (!stopped) {
      stopped = await waitFor(() => !isProcessAlive(pid), 2_000);
    }
  }
 
  if (stopped) {
    await rm(pidPath, { force: true });
    process.stderr.write('Daemon stopped.\n');
  } else {
    process.stderr.write('Daemon did not stop within timeout.\n');
  }
  return stopped;
}
 
/**
 * Read the relay address from the relay address file.
 *
 * @returns The relay address, or undefined if the file is missing or empty.
 */
async function readRelayAddr(): Promise<string | undefined> {
  try {
    return (await readFile(getRelayAddrPath(), 'utf-8')).trim() || undefined;
  } catch (error: unknown) {
    Eif (isErrorWithCode(error, 'ENOENT')) {
      return undefined;
    }
    throw error;
  }
}
 
/**
 * Ensure the daemon is running and print its socket path.
 *
 * @param socketPath - The daemon socket path.
 * @param options - Additional options.
 * @param options.localRelay - Initialize remote comms with the local relay after starting.
 */
export async function handleDaemonStart(
  socketPath: string,
  { localRelay = false }: { localRelay?: boolean } = {},
): Promise<void> {
  if (localRelay) {
    const relayPid = await readPidFile(getRelayPidPath());
    if (relayPid === undefined || !isProcessAlive(relayPid)) {
      process.stderr.write(
        'Relay is not running. Start it with: ocap relay start\n',
      );
      process.exitCode = 1;
      return;
    }
 
    const relayAddr = await readRelayAddr();
    if (relayAddr === undefined) {
      process.stderr.write(
        'Relay address file not found. Restart the relay.\n',
      );
      process.exitCode = 1;
      return;
    }
 
    await ensureDaemon(socketPath);
 
    const statusResponse = await sendCommand({
      socketPath,
      method: 'getStatus',
      timeoutMs: 10_000,
    });
    if (
      !isJsonRpcFailure(statusResponse) &&
      (statusResponse.result as { remoteComms?: { state: string } }).remoteComms
        ?.state === 'connected'
    ) {
      process.stderr.write('Remote comms already initialized.\n');
      process.stderr.write(`Daemon running. Socket: ${tildefy(socketPath)}\n`);
      return;
    }
 
    const initResponse = await sendCommand({
      socketPath,
      method: 'initRemoteComms',
      params: { relays: [relayAddr] },
      timeoutMs: 30_000,
    });
    if (isJsonRpcFailure(initResponse)) {
      process.stderr.write(
        `Failed to initialize remote comms: ${initResponse.error.message}\n`,
      );
      process.exitCode = 1;
      return;
    }
    process.stderr.write('Remote comms initialized with local relay.\n');
  } else {
    await ensureDaemon(socketPath);
  }
  process.stderr.write(`Daemon running. Socket: ${tildefy(socketPath)}\n`);
}
 
/**
 * Stop the daemon (if running) and delete all state.
 *
 * @param socketPath - The daemon socket path.
 */
export async function handleDaemonBegone(socketPath: string): Promise<void> {
  const stopped = await stopDaemon(socketPath);
  if (!stopped) {
    process.stderr.write(
      'Refusing to delete state while the daemon is still running.\n',
    );
    process.exitCode = 1;
    return;
  }
  await deleteDaemonState({ ocapHome: getOcapHome(), socketPath });
  process.stderr.write('All daemon state deleted.\n');
}
 
/**
 * Send an RPC method call to the daemon.
 *
 * @param args - Positional arguments: [method, params-json].
 * @param socketPath - The daemon socket path.
 * @param options - Additional options.
 * @param options.timeoutMs - Read timeout in milliseconds.
 */
export async function handleDaemonExec(
  args: string[],
  socketPath: string,
  { timeoutMs }: { timeoutMs?: number | undefined } = {},
): Promise<void> {
  const method = args[0] ?? 'getStatus';
  const rawParams = args[1];
 
  // For launchSubcluster: resolve relative bundleSpec paths to file:// URLs.
  let params: Record<string, unknown> | undefined;
  if (rawParams !== undefined) {
    try {
      const parsed = JSON.parse(rawParams) as Record<string, unknown>;
      const { config } = parsed as { config?: unknown };
      if (method === 'launchSubcluster' && isClusterConfigLike(config)) {
        params = {
          ...parsed,
          config: resolveBundleSpecs(config),
        };
      } else {
        params = parsed;
      }
    } catch {
      process.stderr.write('Error: params-json must be valid JSON.\n');
      process.exitCode = 1;
      return;
    }
  }
 
  const response = await sendCommand({
    socketPath,
    method,
    params,
    ...(timeoutMs === undefined ? {} : { timeoutMs }),
  });
 
  if (isJsonRpcFailure(response)) {
    writeRpcError(response);
    return;
  }
 
  const isTTY = process.stdout.isTTY ?? false;
  if (isTTY) {
    process.stdout.write(`${JSON.stringify(response.result, null, 2)}\n`);
  } else {
    process.stdout.write(`${JSON.stringify(response.result)}\n`);
  }
}
 
/**
 * Redeem an OCAP URL via the daemon.
 *
 * @param url - The OCAP URL to redeem.
 * @param socketPath - The daemon socket path.
 * @param options - Additional options.
 * @param options.timeoutMs - Read timeout in milliseconds.
 */
export async function handleRedeemURL(
  url: string,
  socketPath: string,
  { timeoutMs }: { timeoutMs?: number | undefined } = {},
): Promise<void> {
  const response = await sendCommand({
    socketPath,
    method: 'redeemOcapURL',
    params: { url },
    ...(timeoutMs === undefined ? {} : { timeoutMs }),
  });
 
  if (isJsonRpcFailure(response)) {
    writeRpcError(response);
    return;
  }
 
  process.stdout.write(`${JSON.stringify(response.result)}\n`);
}
 
/**
 * Send a `queueMessage` RPC call to the daemon and print the result.
 * By default the CapData result is decoded into a human-readable form.
 *
 * @param options - The command options.
 * @param options.target - KRef of the target object.
 * @param options.method - Method name to invoke.
 * @param options.args - JSON-encoded array of arguments.
 * @param options.socketPath - The daemon socket path.
 * @param options.raw - If true, output raw CapData JSON.
 * @param options.timeoutMs - Read timeout in milliseconds.
 */
export async function handleDaemonQueueMessage({
  target,
  method,
  args,
  socketPath,
  raw = false,
  timeoutMs,
}: {
  target: string;
  method: string;
  args: unknown[];
  socketPath: string;
  raw?: boolean | undefined;
  timeoutMs?: number | undefined;
}): Promise<void> {
  const response = await sendCommand({
    socketPath,
    method: 'queueMessage',
    params: [target, method, args],
    ...(timeoutMs === undefined ? {} : { timeoutMs }),
  });
 
  if (isJsonRpcFailure(response)) {
    writeRpcError(response);
    return;
  }
 
  let output: unknown;
  if (raw || !isCapData(response.result)) {
    output = response.result;
  } else {
    output = prettifySmallcaps(response.result);
  }
 
  const isTTY = process.stdout.isTTY ?? false;
  Iif (isTTY) {
    process.stdout.write(`${JSON.stringify(output, null, 2)}\n`);
  } else {
    process.stdout.write(`${JSON.stringify(output)}\n`);
  }
}