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
TypeScript aiai-agentsai-memoryanthropicartificial-intelligencechromadbclaudeclaude-agent-sdkclaude-agentsclaude-codeclaude-code-pluginclaude-skillsembeddingslong-term-memorymem0memory-engineopenmemoryragsqlitesupermemory
// SPDX-License-Identifier: Apache-2.0
//
// Phase 7 — Local API key bootstrap for the server runtime.
//
// When the operator selects `runtime: "server"` during install (or via
// the `claude-mem server keys rotate` command), we provision a local hook
// API key against the local Postgres so hooks can authenticate to /v1/*.
//
// Bootstrapping flow:
// 1. Connect to Postgres (CLAUDE_MEM_SERVER_DATABASE_URL).
// 2. Find or create a "local-hook" team and project so the api_key has
// proper tenant scope.
// 3. Generate a `cmem_<random>` key, hash with SHA-256, insert into
// `api_keys` with the scopes the /v1 routes actually require:
// memories:read and memories:write.
// 4. Persist the plaintext key to ~/.claude-mem/settings.json under
// `CLAUDE_MEM_SERVER_API_KEY` (the new canonical key after the
// server-beta → server rename). Reads in `runtime-selector.ts`
// dual-accept the legacy `CLAUDE_MEM_SERVER_BETA_*` keys, but writes
// from this bootstrapper use the new canonical names going forward.
// Then chmod that file to 0600 so only the owner can read it.
//
// The plaintext key is NEVER written into the generated bundle and never
// logged.
import { randomBytes } from 'crypto';
import { apiKeyPrefix, hashApiKey as hashApiKeyStorage } from '../../server/auth/api-key-hash.js';
import { logger } from '../../utils/logger.js';
import { updateSettingsDocument } from '../../shared/settings-document.js';
import { createPostgresPool, withPostgresTransaction, type PostgresPool } from '../../storage/postgres/pool.js';
import type { PostgresQueryable } from '../../storage/postgres/utils.js';
import { parsePostgresConfig } from '../../storage/postgres/config.js';
import { PostgresAuthRepository } from '../../storage/postgres/auth.js';
import { ensureApiKeySchema } from '../../storage/postgres/schema.js';
import { PostgresProjectsRepository } from '../../storage/postgres/projects.js';
import { PostgresTeamsRepository } from '../../storage/postgres/teams.js';
const LOCAL_HOOK_TEAM_NAME = 'local-hook-team';
const LOCAL_HOOK_PROJECT_NAME = 'local-hook-project';
const LOCAL_HOOK_ACTOR_ID = 'system:local-hook-bootstrap';
// The /v1 routes gate reads on `memories:read` and writes on `memories:write`
// (see ServerV1PostgresRoutes.ts), and the auth middleware matches scopes by
// exact membership with no alias or expansion. A bootstrapped hook key must
// therefore carry both, or every request it authenticates is refused with
// `403 Forbidden — Invalid API key or insufficient scope`. This mirrors
// DEFAULT_LOCAL_API_KEY_SCOPES (the SQLite path's equivalent, #2428).
export const HOOK_API_KEY_SCOPES: readonly string[] = Object.freeze([
'memories:read',
'memories:write',
]);
export interface BootstrapResult {
rawKey: string;
apiKeyId: string;
teamId: string;
projectId: string;
}
export interface BootstrapDependencies {
pool?: PostgresPool;
// For tests: skip pool.end() because the caller owns lifecycle.
closePool?: boolean;
}
export async function bootstrapServerApiKey(
deps: BootstrapDependencies = {},
): Promise<BootstrapResult> {
const closePool = deps.closePool ?? deps.pool === undefined;
const pool = deps.pool ?? buildPoolFromEnv();
try {
const rawKey = createRawApiKey();
const keyHash = hashApiKey(rawKey);
const keyPrefix = apiKeyPrefix(rawKey);
return await withPostgresTransaction(pool, async client => {
// Keep the schema/key locks until the linked audit is written. Releasing
// them between inserts lets concurrent bootstrap DDL lock api_keys and
// then wait for audit_log while the audit's FK check waits for api_keys.
// Failure also rolls back tenant rows and the otherwise unreported key.
await ensureApiKeySchema(client);
const teamId = await findOrCreateTeam(client);
const projectId = await findOrCreateProject(client, teamId);
const repo = new PostgresAuthRepository(client);
const created = await repo.createApiKey({
keyHash,
keyPrefix,
teamId,
projectId,
actorId: LOCAL_HOOK_ACTOR_ID,
scopes: [...HOOK_API_KEY_SCOPES],
});
await repo.createAuditLog({
teamId,
projectId,
actorId: LOCAL_HOOK_ACTOR_ID,
apiKeyId: created.id,
action: 'api_key.create',
resourceType: 'api_key',
resourceId: created.id,
details: { source: 'server-bootstrap' },
});
return { rawKey, apiKeyId: created.id, teamId, projectId };
});
} finally {
if (closePool) {
await pool.end().catch(() => undefined);
}
}
}
export async function revokeServerApiKey(
apiKeyId: string,
pool?: PostgresPool,
): Promise<void> {
const closePool = pool === undefined;
const activePool = pool ?? buildPoolFromEnv();
try {
await activePool.query(
`UPDATE api_keys SET revoked_at = now() WHERE id = $1 AND revoked_at IS NULL`,
[apiKeyId],
);
} finally {
if (closePool) {
await activePool.end().catch(() => undefined);
}
}
}
export interface RotateOptions {
previousApiKeyId?: string | null;
pool?: PostgresPool;
beforeRevoke?: (result: BootstrapResult) => void | Promise<void>;
}
export async function rotateServerApiKey(options: RotateOptions = {}): Promise<BootstrapResult> {
const closePool = options.pool === undefined;
const pool = options.pool ?? buildPoolFromEnv();
try {
const result = await bootstrapServerApiKey({ pool, closePool: false });
let beforeRevokeCompleted = options.beforeRevoke === undefined;
try {
if (options.beforeRevoke) {
await options.beforeRevoke(result);
beforeRevokeCompleted = true;
}
if (options.previousApiKeyId) {
await pool.query(
`UPDATE api_keys SET revoked_at = now() WHERE id = $1 AND revoked_at IS NULL`,
[options.previousApiKeyId],
);
}
} catch (error) {
if (!beforeRevokeCompleted) {
await revokeServerApiKey(result.apiKeyId, pool).catch(() => undefined);
}
throw error;
}
return result;
} finally {
if (closePool) {
await pool.end().catch(() => undefined);
}
}
}
export function persistServerSettings(
settingsPath: string,
values: { apiKey: string; projectId: string; serverBaseUrl?: string; previousApiKeyId?: string | null },
): boolean {
const updates: Record<string, unknown> = {
CLAUDE_MEM_SERVER_API_KEY: values.apiKey,
CLAUDE_MEM_SERVER_PROJECT_ID: values.projectId,
};
if (values.serverBaseUrl) updates.CLAUDE_MEM_SERVER_URL = values.serverBaseUrl;
if (values.previousApiKeyId) updates.CLAUDE_MEM_SERVER_PREVIOUS_API_KEY_ID = values.previousApiKeyId;
const result = updateSettingsDocument(settingsPath, updates, {}, target => {
if (!values.previousApiKeyId) delete target.CLAUDE_MEM_SERVER_PREVIOUS_API_KEY_ID;
});
if (result.status === 'refused') {
logger.warn('HOOK', 'Could not persist server settings; leaving existing credentials unchanged.', { settingsPath }, result.error instanceof Error ? result.error : undefined);
return false;
}
// The boundary writes settings.json owner-only from the first byte, so the
// API key hooks read on every invocation is never readable by other users.
return true;
}
export interface ServerKeyRotationState {
/** Set only by an unfinished rotation (persistServerSettings' retry marker). */
pendingRevocationKeyId: string | null;
currentApiKey: string | null;
currentProjectId: string | null;
}
/**
* What `server keys rotate` finds in settings.json. Only an explicit
* CLAUDE_MEM_SERVER_PREVIOUS_API_KEY_ID means an earlier rotation saved its new
* key but could not revoke the old one; that run finishes the revocation and
* mints nothing. The current key's own id is never a pending revocation: an
* ordinary rotation revokes it only after its replacement is saved.
*/
export function readServerKeyRotationState(flat: Record<string, unknown> | null | undefined): ServerKeyRotationState {
const nonEmpty = (value: unknown): string | null =>
typeof value === 'string' && value.length > 0 ? value : null;
// The first non-blank, trimmed candidate: the same pick runtime-selector
// makes for the key hooks authenticate with. Settings seeded from defaults
// hold the canonical key as "", which must not hide a legacy key.
const firstNonBlank = (...values: unknown[]): string | null => {
for (const value of values) {
if (typeof value === 'string' && value.trim().length > 0) return value.trim();
}
return null;
};
return {
pendingRevocationKeyId: nonEmpty(flat?.CLAUDE_MEM_SERVER_PREVIOUS_API_KEY_ID),
// The canonical key first, then the pre-rename CLAUDE_MEM_SERVER_BETA_* name.
currentApiKey: firstNonBlank(flat?.CLAUDE_MEM_SERVER_API_KEY, flat?.CLAUDE_MEM_SERVER_BETA_API_KEY),
currentProjectId: firstNonBlank(flat?.CLAUDE_MEM_SERVER_PROJECT_ID, flat?.CLAUDE_MEM_SERVER_BETA_PROJECT_ID),
};
}
export function createRawApiKey(): string {
return `cmem_${randomBytes(32).toString('base64url')}`;
}
export function hashApiKey(rawKey: string): string {
return hashApiKeyStorage(rawKey);
}
async function findOrCreateTeam(pool: PostgresQueryable): Promise<string> {
const existing = await pool.query<{ id: string }>(
`SELECT id FROM teams WHERE name = $1 LIMIT 1`,
[LOCAL_HOOK_TEAM_NAME],
);
if (existing.rows[0]) {
return existing.rows[0].id;
}
const repo = new PostgresTeamsRepository(pool);
const team = await repo.create({ name: LOCAL_HOOK_TEAM_NAME, metadata: { source: 'local-hook-bootstrap' } });
return team.id;
}
async function findOrCreateProject(pool: PostgresQueryable, teamId: string): Promise<string> {
const existing = await pool.query<{ id: string }>(
`SELECT id FROM projects WHERE team_id = $1 AND name = $2 LIMIT 1`,
[teamId, LOCAL_HOOK_PROJECT_NAME],
);
if (existing.rows[0]) {
return existing.rows[0].id;
}
const repo = new PostgresProjectsRepository(pool);
const project = await repo.create({
teamId,
name: LOCAL_HOOK_PROJECT_NAME,
metadata: { source: 'local-hook-bootstrap' },
});
return project.id;
}
function buildPoolFromEnv(): PostgresPool {
const config = parsePostgresConfig({ requireDatabaseUrl: true });
if (!config) {
throw new Error(
'Cannot bootstrap server API key: CLAUDE_MEM_SERVER_DATABASE_URL is not set.',
);
}
return createPostgresPool(config);
}