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

14.8 KB
Raw Blame
import { basename, join } from 'path';
import { existsSync, mkdirSync, renameSync, statSync, statfsSync, unlinkSync } from 'fs';
import { DB_PATH, paths } from '../../shared/paths.js';
import { logger } from '../../utils/logger.js';
import { runSqliteFileJob } from './sqlite-file-job.js';
import { listSnapshotNames, readPendingRestore } from './snapshot-files.js';
import { removePartialSnapshots } from './snapshot-staging.js';

/** Settings keys BackupManager reads (subset of SettingsDefaults). */
export interface BackupSettingKeys {
  CLAUDE_MEM_BACKUP_INTERVAL_HOURS?: string;
  CLAUDE_MEM_BACKUP_RETAIN_COUNT?: string;
}

export interface BackupManagerOptions {
  /** Live database to snapshot (defaults to the shared claude-mem.db). */
  dbPath?: string;
  /** Snapshot target directory (defaults to paths.backups()). */
  backupsDir?: string;
  /** Directory probed for free disk space (defaults to the snapshot destination). */
  preflightDir?: string;
  /** Delay before the first scheduled run after start(). */
  initialDelayMs?: number;
  /** Injectable clock (tests). */
  now?: () => number;
}

export interface BackupSnapshotResult {
  path: string;
  bytes: number;
  createdAt: number;
}

export interface BackupRunResult extends BackupSnapshotResult {
  /** The snapshot is complete, but pruning old snapshots failed. */
  retentionError?: string;
}

export interface BackupStatus {
  configured: boolean;
  lastSnapshotAt: number | null;
  lastSnapshotBytes: number | null;
  snapshotCount: number;
  lastError: string | null;
  nextRunAt: number | null;
}

/**
 * The first scheduled snapshot waits this long after worker start, so it
 * never competes with boot work (migrations, backfills, the spool drain) for
 * the database.
 */
const DEFAULT_INITIAL_DELAY_MS = 5 * 60_000;

// Node and Bun turn delays above the signed 32-bit limit into 1 ms.
const MAX_TIMER_DELAY_MS = 2_147_483_647;

/**
 * The 1-hour floor the settings write path enforces (SettingsRoutes): every
 * snapshot is a full VACUUM INTO copy of the database, and the floor caps
 * that I/O at 24 full copies a day. settings.json is hand-editable, so the
 * read side keeps the floor too: an out-of-range interval must not turn the
 * schedule into back-to-back full-database copies. There is deliberately no
 * ceiling here — a hand-edited monthly cadence is a supported scenario
 * (backup-long-cadence), and armTimer chains timer-sized steps for it.
 */
const BACKUP_INTERVAL_FLOOR_HOURS = 1;

/**
 * Automatic local SQLite snapshots with retention.
 *
 * Snapshot engine extracted from the one-time v12.4.3 cleanup backup
 * (CleanupV12_4_3.ts): disk pre-flight, then `VACUUM INTO`, in a child
 * process so the worker keeps serving requests during a multi-GB snapshot.
 * Unlike that one-time backup there is no raw-copy fallback: copying a live
 * db and its WAL separately can pair two different states. Every snapshot ends as one standalone,
 * quick_check-verified file in rollback-journal mode, so a snapshot never
 * depends on a sidecar. The schedule loop is SyncClient's self-rescheduling
 * unref'd setTimeout chain (the worker has no setInterval of its own) with a
 * single-flight guard, so a slow snapshot can never stack a second cycle.
 *
 * Construction is gated by DatabaseManager on CLAUDE_MEM_BACKUP_ENABLED, so
 * an instance existing means backups are configured.
 */
export class BackupManager {
  private readonly dbPath: string;
  private readonly backupsDir: string;
  private readonly preflightDir: string;
  private readonly intervalMs: number;
  private readonly retainCount: number;
  private readonly initialDelayMs: number;
  private readonly now: () => number;

  private timer: ReturnType<typeof setTimeout> | null = null;
  private started = false;
  private stopped = false;
  /** Single-flight: true while a snapshot cycle is in progress. */
  private running = false;
  private readonly snapshots = new Map<AbortController, Promise<BackupSnapshotResult>>();

  private lastError: string | null = null;
  private nextRunAt: number | null = null;

  constructor(settings: BackupSettingKeys, options: BackupManagerOptions = {}) {
    const rawIntervalHours = settings.CLAUDE_MEM_BACKUP_INTERVAL_HOURS ?? '';
    const intervalHours = rawIntervalHours === '' ? NaN : Number.parseFloat(rawIntervalHours);
    // Fractional hours stay legal down to the floor (the write path accepts 1.5 =
    // 90 minutes); below it the value is clamped, not rejected, because the
    // clamped schedule is the closest safe version of what was asked for.
    if (Number.isFinite(intervalHours) && intervalHours > 0 && intervalHours < BACKUP_INTERVAL_FLOOR_HOURS) {
      logger.warn('BACKUP', `CLAUDE_MEM_BACKUP_INTERVAL_HOURS below ${BACKUP_INTERVAL_FLOOR_HOURS} clamped to the write path's floor`, {
        value: rawIntervalHours,
        clampedToHours: BACKUP_INTERVAL_FLOOR_HOURS,
      });
    }
    const intervalMs = Number.isFinite(intervalHours) && intervalHours > 0
      ? Math.max(intervalHours, BACKUP_INTERVAL_FLOOR_HOURS) * 3_600_000
      : 24 * 3_600_000;
    this.intervalMs = intervalMs;
    const retain = Number.parseInt(settings.CLAUDE_MEM_BACKUP_RETAIN_COUNT ?? '', 10);
    // No ceiling on the read side: applyRetention deletes every snapshot past
    // retainCount, so clamping a hand-set count would delete backups the user
    // chose to keep.
    this.retainCount = Number.isFinite(retain) && retain > 0 ? retain : 7;
    this.dbPath = options.dbPath ?? DB_PATH;
    this.backupsDir = options.backupsDir ?? paths.backups();
    this.preflightDir = options.preflightDir ?? this.backupsDir;
    this.initialDelayMs = options.initialDelayMs ?? DEFAULT_INITIAL_DELAY_MS;
    this.now = options.now ?? Date.now;
  }

  /** Arm the cadence loop; the first run lands initialDelayMs after start. */
  start(): void {
    if (this.started || this.stopped) return;
    this.started = true;
    this.schedule(this.initialDelayMs);
    logger.info('BACKUP', 'Backup schedule started', {
      backupsDir: this.backupsDir,
      intervalMs: this.intervalMs,
      retainCount: this.retainCount,
    });
  }

  async stop(): Promise<void> {
    this.stopped = true;
    if (this.timer) {
      clearTimeout(this.timer);
      this.timer = null;
    }
    this.nextRunAt = null;
    for (const controller of this.snapshots.keys()) {
      controller.abort(new Error('Backup manager stopped'));
    }
    // A restart must not leave a child still writing a partial that the new
    // manager can remove as crash debris. Wait for exit and partial cleanup.
    await Promise.allSettled(this.snapshots.values());
  }

  /**
   * Snapshot facts come from the files on disk, so they survive the worker
   * restarts that enabling backups and restoring both go through.
   */
  status(): BackupStatus {
    const names = listSnapshotNames(this.backupsDir);
    const newest = names[0] ? statSync(join(this.backupsDir, names[0])) : null;
    return {
      configured: true,
      lastSnapshotAt: newest ? newest.mtimeMs : null,
      lastSnapshotBytes: newest ? newest.size : null,
      snapshotCount: names.length,
      lastError: this.lastError,
      nextRunAt: this.nextRunAt,
    };
  }

  /**
   * One consistent, standalone snapshot of the live DB. Disk pre-flight, then
   * (in a child process, see sqlite-file-job.ts) `VACUUM INTO` on a readonly
   * connection, made standalone and quick_check-verified. Throws when no
   * verified snapshot could be produced, and leaves no partial file behind.
   */
  createSnapshot(): Promise<BackupSnapshotResult> {
    if (this.stopped) return Promise.reject(new Error('Backup manager stopped'));
    const controller = new AbortController();
    const task = this.createOwnedSnapshot(controller.signal);
    this.snapshots.set(controller, task);
    const release = () => { this.snapshots.delete(controller); };
    void task.then(release, release);
    return task;
  }

  private async createOwnedSnapshot(signal: AbortSignal): Promise<BackupSnapshotResult> {
    // Committed pages can still live only in the WAL. Counting just the main
    // file substantially underestimates a snapshot until the next checkpoint.
    let sourceBytes = statSync(this.dbPath).size;
    try {
      sourceBytes += statSync(`${this.dbPath}-wal`).size;
    } catch (error: unknown) {
      // A non-WAL database, or a concurrent checkpoint, has no sidecar.
      if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error;
    }
    mkdirSync(this.backupsDir, { recursive: true });
    removePartialSnapshots(this.backupsDir);
    this.assertDiskSpace(sourceBytes);
    const createdAt = this.now();
    const ts = new Date(createdAt).toISOString().replace(/[:.]/g, '-');
    const backupPath = join(this.backupsDir, `claude-mem-${ts}.db`);

    // Written under a .partial name (outside SNAPSHOT_FILE_PATTERN) and
    // renamed only once verified, so status, list, retention and restore
    // never see an unfinished snapshot.
    const partialPath = `${backupPath}.partial`;
    try {
      await runSqliteFileJob({ kind: 'snapshot', source: this.dbPath, target: partialPath }, signal);
      signal.throwIfAborted();
      renameSync(partialPath, backupPath);
    } catch (err: unknown) {
      const error = err instanceof Error ? err : new Error(String(err));
      for (const leftover of [partialPath, `${partialPath}-wal`, `${partialPath}-shm`, `${partialPath}-journal`]) {
        if (existsSync(leftover)) unlinkSync(leftover);
      }
      logger.error('BACKUP', 'Snapshot failed; no snapshot was kept', { backupPath }, error);
      throw error;
    }

    const bytes = statSync(backupPath).size;
    logger.info('BACKUP', 'Snapshot created', { backupPath, sourceBytes, bytes });
    this.lastError = null;
    return { path: backupPath, bytes, createdAt };
  }

  /**
   * Keep the retainCount most-recent snapshots; delete the rest. Sanitized
   * ISO filenames sort lexicographically, so name order is timestamp order.
   * The snapshot just completed takes the first slot even if the clock moved
   * backward; a successful run must not prune the data it has just saved.
   */
  async applyRetention(completedSnapshot?: string): Promise<void> {
    const names = listSnapshotNames(this.backupsDir);
    const completedIndex = completedSnapshot ? names.indexOf(basename(completedSnapshot)) : -1;
    if (completedIndex > 0) names.unshift(...names.splice(completedIndex, 1));
    const pending = readPendingRestore()?.snapshot;
    const excess = names.slice(this.retainCount).filter(name => name !== pending);
    for (const name of excess) {
      const snapshotPath = join(this.backupsDir, name);
      unlinkSync(snapshotPath);
      for (const sidecar of [`${snapshotPath}-wal`, `${snapshotPath}-shm`]) {
        if (existsSync(sidecar)) unlinkSync(sidecar);
      }
    }
    if (excess.length > 0) {
      logger.info('BACKUP', 'Retention pruned old snapshots', {
        deleted: excess.length,
        retained: names.length - excess.length,
      });
    }
  }

  /**
   * One snapshot + retention cycle on demand (POST /api/backup/run).
   * Single-flight: returns `null` without doing anything when a cycle is
   * already in progress (the route maps that to 409). A snapshot failure is
   * thrown. A later retention failure accompanies the completed snapshot.
   */
  async runNow(): Promise<BackupRunResult | null> {
    if (this.running) return null;
    this.running = true;
    try {
      const result = await this.createSnapshot();
      try {
        await this.applyRetention(result.path);
      } catch (error) {
        const normalized = error instanceof Error ? error : new Error(String(error));
        this.lastError = normalized.message;
        logger.error('BACKUP', 'Snapshot saved but retention failed', { backupPath: result.path }, normalized);
        return { ...result, retentionError: normalized.message };
      }
      return result;
    } catch (error) {
      const normalized = error instanceof Error ? error : new Error(String(error));
      this.lastError = normalized.message;
      throw normalized;
    } finally {
      this.running = false;
    }
  }

  /**
   * Cadence-loop wrapper around runNow(): failures are logged, never thrown,
   * so the loop survives a bad cycle and tries again next interval.
   */
  async runOnce(): Promise<void> {
    try {
      await this.runNow();
    } catch (error) {
      logger.error('BACKUP', 'Snapshot cycle failed', {}, error instanceof Error ? error : new Error(String(error)));
    }
  }

  // Loop internals (shape copied from SyncClient.schedule()/tick())

  private schedule(delayMs: number): void {
    if (this.stopped) return;
    if (this.timer) clearTimeout(this.timer);
    const dueAt = this.now() + delayMs;
    this.nextRunAt = dueAt;
    this.armTimer(dueAt);
  }

  private armTimer(dueAt: number): void {
    const delayMs = Math.max(0, dueAt - this.now());
    const wait = Math.min(delayMs, MAX_TIMER_DELAY_MS);
    const timer = setTimeout(() => {
      this.timer = null;
      if (delayMs > wait) {
        this.armTimer(dueAt);
        return;
      }
      void this.tick();
    }, wait);
    (timer as { unref?: () => void }).unref?.(); // never hold the process open
    this.timer = timer;
  }

  private async tick(): Promise<void> {
    if (this.stopped) return;
    await this.runOnce();
    if (this.stopped) return;
    this.schedule(this.intervalMs);
  }

  /**
   * Disk pre-flight copied from CleanupV12_4_3: refuse to snapshot when free
   * destination space is credibly below 1.2x the DB plus WAL size + 100 MiB
   * (VACUUM INTO writes a
   * full copy, plus headroom so a snapshot never fills the disk the live DB
   * needs). statfs failures and non-credible readings (Bun <= 1.3.14
   * darwin-x64 returns a misaligned struct with bsize = 0,
   * https://github.com/oven-sh/bun/issues/31133) skip the gate rather than
   * block: a real out-of-space condition still surfaces from the write.
   */
  private assertDiskSpace(sourceBytes: number): void {
    const required = Math.ceil(sourceBytes * 1.2) + 100 * 1024 * 1024;

    let fsStats: ReturnType<typeof statfsSync> | undefined;
    try {
      fsStats = statfsSync(this.preflightDir);
    } catch (err: unknown) {
      logger.warn('BACKUP', 'statfsSync failed; proceeding without disk-space pre-flight', {}, err instanceof Error ? err : new Error(String(err)));
    }
    if (!fsStats) return;

    const bsize = Number(fsStats.bsize);
    const bavail = Number(fsStats.bavail);
    if (!Number.isFinite(bsize) || !Number.isFinite(bavail) || bsize <= 0) {
      logger.warn('BACKUP', 'statfsSync returned non-credible values; proceeding without disk-space pre-flight', {
        bsize,
        bavail,
        runtime: typeof Bun !== 'undefined' ? `bun ${Bun.version}` : 'node',
        platform: `${process.platform}-${process.arch}`,
      });
      return;
    }

    const free = bavail * bsize;
    if (free < required) {
      throw new Error(`Insufficient disk for backup snapshot (free=${free}, required=${required})`);
    }
  }

}