Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

9.8 KB
Raw Blame
import { styleText } from 'node:util';
import { readFlatSettings } from '../utils/settings.js';
import {
  runServerRestartCommand,
  runServerStartCommand,
  runServerStatusCommand,
  runServerStopCommand,
  runServerWorkerStartCommand,
  runRestartCommand,
  runServerApiKeyCommand,
  runStartCommand,
  runStatusCommand,
  runStopCommand,
} from './runtime.js';

function printServerUsage(): void {
  console.error(`Usage: ${styleText('bold', 'npx claude-mem server <command>')}`);
  console.error('Commands: start, stop, restart, status, api-key create|list|revoke, keys rotate, worker start, jobs status|failed|retry|cancel');
}

function runWorkerLifecycleCommand(command: string): boolean {
  switch (command) {
    case 'start':
      runStartCommand();
      return true;
    case 'stop':
      runStopCommand();
      return true;
    case 'restart':
      runRestartCommand();
      return true;
    case 'status':
      runStatusCommand();
      return true;
    default:
      return false;
  }
}

function runServerLifecycleCommand(command: string): boolean {
  switch (command) {
    case 'start':
      runServerStartCommand();
      return true;
    case 'stop':
      runServerStopCommand();
      return true;
    case 'restart':
      runServerRestartCommand();
      return true;
    case 'status':
      runServerStatusCommand();
      return true;
    default:
      return false;
  }
}

export async function runServerCommand(argv: string[] = []): Promise<void> {
  const subCommand = argv[0]?.toLowerCase();

  if (!subCommand) {
    printServerUsage();
    process.exit(1);
  }

  if (runServerLifecycleCommand(subCommand)) {
    return;
  }

  if (subCommand === 'api-key') {
    const apiKeyCommand = argv[1]?.toLowerCase();
    if (apiKeyCommand === 'create' || apiKeyCommand === 'list' || apiKeyCommand === 'revoke') {
      runServerApiKeyCommand(argv.slice(1));
      return;
    }
    console.error(styleText('red', `Unknown server api-key subcommand: ${apiKeyCommand ?? '(none)'}`));
    console.error('Usage: npx claude-mem server api-key create|list|revoke');
    process.exit(1);
  }

  if (subCommand === 'worker') {
    const workerCommand = argv[1]?.toLowerCase();
    if (workerCommand === 'start') {
      runServerWorkerStartCommand();
      return;
    }
    console.error(styleText('red', `Unknown server worker subcommand: ${workerCommand ?? '(none)'}`));
    console.error('Usage: npx claude-mem server worker start');
    process.exit(1);
  }

  if (subCommand === 'keys') {
    const keysCommand = argv[1]?.toLowerCase();
    if (keysCommand === 'rotate') {
      await runServerKeysRotateCommand();
      return;
    }
    console.error(styleText('red', `Unknown server keys subcommand: ${keysCommand ?? '(none)'}`));
    console.error('Usage: npx claude-mem server keys rotate');
    process.exit(1);
  }

  if (subCommand === 'jobs') {
    // Phase 12 — operator queue console. Uses Postgres (canonical) +
    // BullMQ (transport) directly. See src/npx-cli/commands/server-jobs.ts.
    const { runServerJobsCommand } = await import('./server-jobs.js');
    await runServerJobsCommand(argv.slice(1));
    return;
  }

  console.error(styleText('red', `Unknown server command: ${subCommand}`));
  printServerUsage();
  process.exit(1);
}

async function runServerKeysRotateCommand(): Promise<void> {
  if (!process.env.CLAUDE_MEM_SERVER_DATABASE_URL) {
    console.error(styleText('red', 'Cannot rotate server API key: CLAUDE_MEM_SERVER_DATABASE_URL is not set.'));
    console.error('Configure Postgres first, then re-run this command.');
    process.exit(1);
  }
  const { rotateServerApiKey, revokeServerApiKey, persistServerSettings, readServerKeyRotationState } = await import(
    '../../services/hooks/server-bootstrap.js'
  );
  const { USER_SETTINGS_PATH: settingsPath } = await import('../../shared/paths.js');

  let state = readServerKeyRotationState(null);
  try {
    state = readServerKeyRotationState(readFlatSettings(settingsPath));
  } catch {
    // ignore — we'll just generate a new key without revoking the old one
  }
  const { pendingRevocationKeyId, currentApiKey, currentProjectId } = state;

  // A prior rotation may have saved the current credential while its old-key
  // revocation failed (its retry marker is still in settings.json). Resolve
  // that pending revocation in place; creating a third key would leave the
  // saved current key active but untracked.
  if (pendingRevocationKeyId) {
    if (!currentApiKey || !currentProjectId) {
      console.error(styleText('red', 'Cannot resume rotation: the pending retry marker has no current credential.'));
      console.error('Repair or restore settings.json before rotating again.');
      process.exit(1);
    }
    try {
      await revokeServerApiKey(pendingRevocationKeyId);
    } catch {
      console.error(styleText('red', 'Cannot finish the pending rotation: the previous API key could not be revoked.'));
      console.error('The current API key remains active and is still recorded in settings.json. Retry after confirming Postgres is available.');
      process.exit(1);
    }
    if (!persistServerSettings(settingsPath, { apiKey: currentApiKey, projectId: currentProjectId })) {
      console.error(styleText('red', 'The previous API key was revoked, but the retry marker could not be removed from settings.json.'));
      console.error('Repair settings.json before rotating again.');
      process.exit(1);
    }
    console.log(JSON.stringify({
      rotated: true,
      retryResolved: true,
      settingsPath,
    }, null, 2));
    return;
  }

  // Ordinary rotation: the current key is revoked only after its replacement
  // is saved (beforeRevoke below), and is recorded as the retry marker until then.
  let previousApiKeyId: string | null = null;
  if (currentApiKey) {
    try {
      previousApiKeyId = await lookupApiKeyIdByPlaintext(currentApiKey);
    } catch {
      console.error(styleText('red', 'Cannot rotate: the current API key could not be looked up in Postgres.'));
      console.error('No replacement key was created. Confirm Postgres is available and retry.');
      process.exit(1);
    }
  }

  let result: Awaited<ReturnType<typeof rotateServerApiKey>>;
  let settingsPersisted = false;
  try {
    result = await rotateServerApiKey({
      previousApiKeyId,
      beforeRevoke: next => {
        if (!persistServerSettings(settingsPath, {
          apiKey: next.rawKey,
          projectId: next.projectId,
          previousApiKeyId,
        })) {
          throw new Error('settings.json could not be updated');
        }
        settingsPersisted = true;
      },
    });
  } catch {
    if (settingsPersisted) {
      console.error(styleText('red', 'A new API key was saved, but revoking the previous key failed.'));
      console.error('Retry the rotation after confirming Postgres is available.');
    } else {
      console.error(styleText('red', 'Cannot rotate: settings.json was not updated, so the existing API key remains active.'));
      console.error('Repair or restore the file, then re-run this command.');
    }
    process.exit(1);
  }
  let cleanupPersisted = false;
  try {
    cleanupPersisted = persistServerSettings(settingsPath, {
      apiKey: result.rawKey,
      projectId: result.projectId,
    });
  } catch {
    cleanupPersisted = false;
  }
  if (!cleanupPersisted) {
    console.error(styleText('red', 'The new API key is active, but its cleanup marker could not be removed from settings.json.'));
    console.error('Repair settings.json and retry; the existing pending marker will be resolved without minting another key.');
    process.exit(1);
  }
  console.log(JSON.stringify({
    rotated: true,
    apiKeyId: result.apiKeyId,
    teamId: result.teamId,
    projectId: result.projectId,
    settingsPath,
  }, null, 2));
}

async function lookupApiKeyIdByPlaintext(rawKey: string): Promise<string | null> {
  const { createPostgresPool } = await import('../../storage/postgres/pool.js');
  const { parsePostgresConfig } = await import('../../storage/postgres/config.js');
  const { apiKeyPrefix, verifyRawKeyAgainstStoredHashAsync, hashApiKeyLegacySha256 } = await import(
    '../../server/auth/api-key-hash.js'
  );
  const { ensureApiKeySchema } = await import('../../storage/postgres/schema.js');
  const config = parsePostgresConfig({ requireDatabaseUrl: true });
  if (!config) return null;
  const pool = createPostgresPool(config);
  try {
    // The standalone key commands run before any server process has bootstrapped
    // the schema; apply the key_prefix upgrade first so these queries never fail
    // against an existing database.
    await ensureApiKeySchema(pool);
    // Salted rows: narrow by prefix, verify each candidate. (scrypt is
    // non-deterministic, so a direct key_hash equality lookup cannot work.)
    const prefix = apiKeyPrefix(rawKey);
    const candidates = await pool.query<{ id: string; key_hash: string }>(
      'SELECT id, key_hash FROM api_keys WHERE key_prefix = $1',
      [prefix],
    );
    for (const row of candidates.rows) {
      if (await verifyRawKeyAgainstStoredHashAsync(rawKey, row.key_hash)) return row.id;
    }
    // Legacy unsalted SHA-256 rows never got a prefix column populated yet;
    // fall back to deterministic lookup so they stay revocable/rotatable.
    const legacy = await pool.query<{ id: string }>(
      'SELECT id FROM api_keys WHERE key_hash = $1 LIMIT 1',
      [hashApiKeyLegacySha256(rawKey)],
    );
    return legacy.rows[0]?.id ?? null;
  } finally {
    await pool.end().catch(() => undefined);
  }
}

export function runWorkerAliasCommand(argv: string[] = []): void {
  const subCommand = argv[0]?.toLowerCase();

  if (!subCommand || !runWorkerLifecycleCommand(subCommand)) {
    console.error(styleText('red', `Unknown worker command: ${subCommand ?? '(none)'}`));
    console.error('Usage: npx claude-mem worker start|stop|restart|status');
    process.exit(1);
  }
}