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

20.8 KB
Raw Blame

import { Database } from 'bun:sqlite';
import path from 'path';
import {
  existsSync,
  writeFileSync,
  readFileSync,
  renameSync,
  unlinkSync,
  readdirSync,
  realpathSync
} from 'fs';
import { execSync } from 'child_process';
import { SettingsDefaultsManager } from '../shared/SettingsDefaultsManager.js';
import { formatTime, groupByDate, parseJsonArray } from '../shared/timeline-formatting.js';
import { epochMs } from '../shared/epoch-ms.js';
import { isDirectChild, normalizePath } from '../shared/path-utils.js';
import { logger } from '../utils/logger.js';
import { getProjectContext } from '../utils/project-name.js';
import { replaceTaggedContent } from '../utils/claude-md-utils.js';
import { findContextBlockRange } from '../utils/context-injection.js';
import { toBmpSafe } from '../utils/bmp-safe.js';
import { ensureDirectoryExists, resolveSymlinkWriteTarget } from '../shared/atomic-json.js';
import { paths } from '../shared/paths.js';
import { streamRows } from '../services/sqlite/stream-rows.js';
import { parseContextCountValue } from '../shared/context-count.js';

const DB_PATH = paths.database();
const SETTINGS_PATH = paths.settings();

interface ObservationRow {
  id: number;
  title: string | null;
  subtitle: string | null;
  narrative: string | null;
  facts: string | null;
  type: string;
  created_at: string;
  created_at_epoch: number;
  files_modified: string | null;
  files_read: string | null;
  project: string;
  discovery_tokens: number | null;
}

// BMP-only markers (issue #2787): astral emoji can be split into a lone
// surrogate by a Claude Code context truncation and brick the session.
const TYPE_ICONS: Record<string, string> = {
  'bugfix': '●',
  'feature': '◆',
  'refactor': '↻',
  'change': '✓',
  'discovery': '○',
  'decision': '⚖',
  'session': '◎',
  'prompt': '”'
};

function getTypeIcon(type: string): string {
  return TYPE_ICONS[type] || '•';
}

function estimateTokens(obs: ObservationRow): number {
  const size = (obs.title?.length || 0) +
    (obs.subtitle?.length || 0) +
    (obs.narrative?.length || 0) +
    (obs.facts?.length || 0);
  return Math.ceil(size / 4);
}

function getTrackedFolders(workingDir: string): Set<string> {
  const folders = new Set<string>();

  let output: string;
  try {
    output = execSync('git ls-files -z', {
      cwd: workingDir,
      encoding: 'utf-8',
      maxBuffer: 50 * 1024 * 1024
    });
  } catch (error) {
    const errorMessage = error instanceof Error ? error.message : String(error);
    logger.warn('CLAUDE_MD', 'git ls-files failed, falling back to directory walk', { error: errorMessage });
    walkDirectoriesWithIgnore(workingDir, folders);
    return folders;
  }

  const files = output.split('\0').filter(f => f);

  for (const file of files) {
    const absPath = path.join(workingDir, file);
    let dir = path.dirname(absPath);

    while (dir.length > workingDir.length && dir.startsWith(workingDir)) {
      folders.add(dir);
      dir = path.dirname(dir);
    }
  }

  return folders;
}

function walkDirectoriesWithIgnore(dir: string, folders: Set<string>, depth: number = 0): void {
  if (depth > 10) return;

  const ignorePatterns = [
    'node_modules', '.git', '.next', 'dist', 'build', '.cache',
    '__pycache__', '.venv', 'venv', '.idea', '.vscode', 'coverage',
    '.claude-mem', '.open-next', '.turbo'
  ];

  try {
    const entries = readdirSync(dir, { withFileTypes: true });
    for (const entry of entries) {
      if (!entry.isDirectory()) continue;
      if (ignorePatterns.includes(entry.name)) continue;
      if (entry.name.startsWith('.') && entry.name !== '.claude') continue;

      const fullPath = path.join(dir, entry.name);
      folders.add(fullPath);
      walkDirectoriesWithIgnore(fullPath, folders, depth + 1);
    }
  } catch {
    // Ignore permission errors
  }
}

const ABSOLUTE_PATH = /^([A-Za-z]:)?[\\/]/;

function comparablePath(p: string): string {
  const normalized = normalizePath(p);
  // Windows drive letters and paths are case-insensitive.
  return /^[A-Za-z]:/.test(normalized) ? normalized.toLowerCase() : normalized;
}

function realComparablePath(p: string): string | null {
  try {
    return comparablePath(realpathSync(p));
  } catch {
    return null;
  }
}

/** realpath of the deepest existing ancestor, with the missing tail re-appended. */
function realComparablePathOfAncestor(p: string): string | null {
  const normalized = normalizePath(p);
  const missing: string[] = [];
  let current = normalized;
  while (current && current !== path.posix.dirname(current)) {
    const real = realComparablePath(current);
    if (real !== null) return [real, ...missing.reverse()].join('/');
    missing.push(path.posix.basename(current));
    current = path.posix.dirname(current);
  }
  return null;
}

/**
 * Tool evidence is usually absolute (Read/Edit/Write inputs). An absolute file
 * is a child of the folder when it sits in `<root>/<relativeFolder>` for some
 * checkout root: this one, or another checkout that shares the project key
 * (a moved clone, a git-remote project name). It is not a child when that
 * root lies inside this checkout: /repo/packages/widget/src/x.ts belongs to
 * packages/widget/src, not to /repo/src. Relative evidence keeps the
 * project-relative check.
 */
export function makeFolderChildMatcher(relativeFolder: string, workingDir: string): (file: string) => boolean {
  const relativeForm = comparablePath(relativeFolder);
  const rootForms = new Set([comparablePath(workingDir)]);
  const realRoot = realComparablePath(workingDir);
  if (realRoot) rootForms.add(realRoot);
  const insideThisCheckout = (dir: string) => [...rootForms].some(root => dir.startsWith(root + '/'));
  return (file: string) => {
    if (!ABSOLUTE_PATH.test(file)) return isDirectChild(file, relativeFolder);
    const fileDir = comparablePath(path.posix.dirname(normalizePath(file)));
    // comparablePath lowercases drive-qualified (Windows) paths; fold the folder the same way.
    const folderForm = /^[a-z]:/.test(fileDir) ? relativeForm.toLowerCase() : relativeForm;
    if (!fileDir.endsWith('/' + folderForm)) return false;
    const root = fileDir.slice(0, -(folderForm.length + 1));
    if (rootForms.has(root)) return true;
    const realFileRoot = realComparablePathOfAncestor(root);
    if (realFileRoot !== null && rootForms.has(realFileRoot)) return true;
    return !insideThisCheckout(root) && !(realFileRoot !== null && insideThisCheckout(realFileRoot));
  };
}

/**
 * Bucket key of a normalized path. A direct child's parent directory ends in
 * its folder's last segment, so the key never skips a file the matcher accepts.
 */
function lastSegmentKey(normalized: string): string {
  return path.posix.basename(normalized).toLowerCase();
}

interface FolderObservations {
  isFolderChild: (file: string) => boolean;
  /**
   * The folder's segments followed by a separator, once with `/` and once
   * with a backslash, ASCII-lowercased. A candidate file must contain one of them:
   * the same check the old per-folder `LIKE '%src/auth/%'` prefilter made, so
   * relative `auth/login.ts` never reaches `src/auth` through the matcher's
   * suffix rule.
   */
  pathNeedles: [string, string];
  observations: ObservationRow[];
}

/** SQLite's LIKE folds ASCII letters only; fold the same way. */
function asciiLowerCase(value: string): string {
  return value.replace(/[A-Z]/g, letter => letter.toLowerCase());
}

function folderPathNeedles(relativeFolder: string): [string, string] {
  const segments = relativeFolder.split(/[\\/]+/).filter(Boolean);
  return [
    asciiLowerCase(segments.join('/') + '/'),
    asciiLowerCase(segments.join('\\') + '\\'),
  ];
}

/**
 * The newest `limit` direct-child observations of each folder, in folder
 * order, from one scan of the project's rows. Rows are bucketed by the parent
 * directory's last segment, and the folder matcher does the exact check.
 */
function findObservationsByFolders(
  db: Database,
  relativeFolders: string[],
  workingDir: string,
  project: string,
  limit: number,
): FolderObservations[] {
  const folders: FolderObservations[] = [];
  const bucketed = new Map<string, FolderObservations[]>();
  for (const relativeFolder of relativeFolders) {
    const entry: FolderObservations = {
      isFolderChild: makeFolderChildMatcher(relativeFolder, workingDir),
      pathNeedles: folderPathNeedles(relativeFolder),
      observations: [],
    };
    folders.push(entry);
    const key = lastSegmentKey(normalizePath(relativeFolder));
    if (!bucketed.has(key)) bucketed.set(key, []);
    bucketed.get(key)!.push(entry);
  }
  if (limit <= 0 || folders.length === 0) return folders;

  const statement = db.prepare(`
    SELECT o.*
    FROM observations o
    WHERE o.project COLLATE NOCASE = ?
    ORDER BY o.created_at_epoch DESC
  `);
  // Folders whose page is not full yet. The scan stops when it reaches 0, as
  // the old per-folder query stopped at its own limit.
  let unfilledFolders = folders.length;
  try {
    for (const obs of streamRows(statement, project) as Iterable<ObservationRow>) {
      for (const file of [...parseJsonArray(obs.files_modified), ...parseJsonArray(obs.files_read)]) {
        const candidates = bucketed.get(lastSegmentKey(path.posix.dirname(normalizePath(file))));
        if (!candidates) continue;
        const comparableFile = asciiLowerCase(file);
        for (const entry of candidates) {
          // Rows arrive one at a time, so a row already added is the last one.
          if (entry.observations.at(-1) === obs || entry.observations.length === limit) continue;
          if (!entry.pathNeedles.some(needle => comparableFile.includes(needle)) || !entry.isFolderChild(file)) continue;
          entry.observations.push(obs);
          if (entry.observations.length === limit) unfilledFolders--;
        }
      }
      if (unfilledFolders === 0) break;
    }
  } finally {
    statement.finalize();
  }
  return folders;
}

// The heading uses the same predicate that selected the row, so an observation
// that also touched a same-named folder elsewhere is filed under this folder's file.
function extractRelevantFile(obs: ObservationRow, isFolderChild: (file: string) => boolean): string {
  for (const [column, filesJson] of [['files_modified', obs.files_modified], ['files_read', obs.files_read]] as const) {
    if (!filesJson) continue;
    try {
      const files = JSON.parse(filesJson);
      if (Array.isArray(files)) {
        const file = files.find((entry): entry is string => typeof entry === 'string' && isFolderChild(entry));
        if (file) return path.posix.basename(normalizePath(file));
      }
    } catch (error) {
      logger.warn('CLAUDE_MD', `Failed to parse ${column} JSON`, { error: error instanceof Error ? error.message : String(error) });
    }
  }

  return 'General';
}

function formatObservationsForClaudeMd(observations: ObservationRow[], isFolderChild: (file: string) => boolean): string {
  const lines: string[] = [];
  lines.push('# Recent Activity');
  lines.push('');
  lines.push('<!-- This section is auto-generated by claude-mem. Edit content outside the tags. -->');
  lines.push('');

  if (observations.length === 0) {
    lines.push('*No recent activity*');
    return lines.join('\n');
  }

  // Rows arrive newest-first (ORDER BY created_at_epoch DESC); keep days in the same order.
  const byDate = groupByDate(observations, obs => obs.created_at, { order: 'desc' });

  for (const [day, dayObs] of byDate) {
    lines.push(`### ${day}`);
    lines.push('');

    const byFile = new Map<string, ObservationRow[]>();
    for (const obs of dayObs) {
      const file = extractRelevantFile(obs, isFolderChild);
      if (!byFile.has(file)) byFile.set(file, []);
      byFile.get(file)!.push(obs);
    }

    for (const [file, fileObs] of byFile) {
      lines.push(`**${file}**`);
      lines.push('| ID | Time | T | Title | Read |');
      lines.push('|----|------|---|-------|------|');

      let lastTime = '';
      for (const obs of fileObs) {
        const time = formatTime(epochMs(obs.created_at_epoch));
        const timeDisplay = time === lastTime ? '"' : time;
        lastTime = time;

        const icon = getTypeIcon(obs.type);
        const title = obs.title || 'Untitled';
        const tokens = estimateTokens(obs);

        lines.push(`| #${obs.id} | ${timeDisplay} | ${icon} | ${title} | ~${tokens} |`);
      }

      lines.push('');
    }
  }

  return lines.join('\n').trim();
}

function writeClaudeMdToFolder(folderPath: string, newContent: string): void {
  const resolvedPath = path.resolve(folderPath);

  if (resolvedPath.includes('/.git/') || resolvedPath.includes('\\.git\\') || resolvedPath.endsWith('/.git') || resolvedPath.endsWith('\\.git')) return;

  const claudeMdPath = path.join(folderPath, 'CLAUDE.md');

  if (!existsSync(folderPath)) {
    throw new Error(`Folder does not exist: ${folderPath}`);
  }

  let existingContent = '';
  if (existsSync(claudeMdPath)) {
    existingContent = readFileSync(claudeMdPath, 'utf-8');
  }

  // #2787: an observation title can carry an emoji, and the block must stay
  // BMP-only, as the worker's folder writer keeps it.
  const finalContent = replaceTaggedContent(existingContent, toBmpSafe(newContent));

  // A symlinked CLAUDE.md stays a symlink: write onto its final target.
  const writeTarget = resolveSymlinkWriteTarget(claudeMdPath);
  ensureDirectoryExists(path.dirname(writeTarget));
  const tempFile = `${writeTarget}.tmp`;
  writeFileSync(tempFile, finalContent);
  renameSync(tempFile, writeTarget);
}

function regenerateFolder(
  absoluteFolder: string,
  relativeFolder: string,
  folder: FolderObservations,
  dryRun: boolean,
  workingDir: string
): { success: boolean; observationCount: number; error?: string } {
  if (!existsSync(absoluteFolder)) {
    return { success: false, observationCount: 0, error: 'Folder no longer exists' };
  }

  const resolvedFolder = path.resolve(absoluteFolder);
  const resolvedWorkingDir = path.resolve(workingDir);
  if (!resolvedFolder.startsWith(resolvedWorkingDir + path.sep)) {
    return { success: false, observationCount: 0, error: 'Path escapes project root' };
  }

  if (folder.observations.length === 0) {
    return { success: false, observationCount: 0, error: 'No observations for folder' };
  }

  const { observations, isFolderChild } = folder;
  if (dryRun) {
    return { success: true, observationCount: observations.length };
  }

  try {
    const formatted = formatObservationsForClaudeMd(observations, isFolderChild);
    writeClaudeMdToFolder(absoluteFolder, formatted);
    return { success: true, observationCount: observations.length };
  } catch (error) {
    const errorMessage = error instanceof Error ? error.message : String(error);
    logger.warn('CLAUDE_MD', 'Failed to regenerate folder', { folder: relativeFolder, error: errorMessage });
    return { success: false, observationCount: 0, error: errorMessage };
  }
}

function processAllFoldersForGeneration(
  trackedFolders: Set<string>,
  workingDir: string,
  project: string,
  dryRun: boolean,
  observationLimit: number
): number {
  const foldersArray = Array.from(trackedFolders).sort();
  const relativeFolders = foldersArray.map(folder => path.relative(workingDir, folder));

  const db = new Database(DB_PATH, { readonly: true, create: false });
  const observationsByFolder = findObservationsByFolders(db, relativeFolders, path.resolve(workingDir), project, observationLimit);
  db.close();

  let successCount = 0;
  let skipCount = 0;
  let errorCount = 0;

  for (const [i, absoluteFolder] of foldersArray.entries()) {
    const relativeFolder = relativeFolders[i];

    const result = regenerateFolder(
      absoluteFolder,
      relativeFolder,
      observationsByFolder[i],
      dryRun,
      workingDir
    );

    if (result.success) {
      logger.debug('CLAUDE_MD', `Processed folder: ${relativeFolder}`, {
        observationCount: result.observationCount
      });
      successCount++;
    } else if (result.error?.includes('No observations')) {
      skipCount++;
    } else {
      logger.warn('CLAUDE_MD', `Error processing folder: ${relativeFolder}`, {
        error: result.error
      });
      errorCount++;
    }
  }

  logger.info('CLAUDE_MD', 'CLAUDE.md generation complete', {
    totalFolders: foldersArray.length,
    withObservations: successCount,
    noObservations: skipCount,
    errors: errorCount,
    dryRun
  });

  return errorCount > 0 ? 1 : 0;
}

export async function generateClaudeMd(dryRun: boolean): Promise<number> {
  const workingDir = process.cwd();
  const settings = SettingsDefaultsManager.loadFromFile(SETTINGS_PATH);
  const observationLimit = parseContextCountValue(settings.CLAUDE_MEM_CONTEXT_OBSERVATIONS) ?? 50;

  logger.info('CLAUDE_MD', 'Starting CLAUDE.md generation', {
    workingDir,
    dryRun,
    observationLimit
  });

  const project = getProjectContext(workingDir).primary;
  const trackedFolders = getTrackedFolders(workingDir);

  if (trackedFolders.size === 0) {
    logger.info('CLAUDE_MD', 'No folders found in project');
    return 0;
  }

  logger.info('CLAUDE_MD', `Found ${trackedFolders.size} folders in project`);

  if (!existsSync(DB_PATH)) {
    logger.info('CLAUDE_MD', 'Database not found, no observations to process');
    return 0;
  }

  try {
    return processAllFoldersForGeneration(trackedFolders, workingDir, project, dryRun, observationLimit);
  } catch (error) {
    const errorMessage = error instanceof Error ? error.message : String(error);
    logger.error('CLAUDE_MD', 'Fatal error during CLAUDE.md generation', {
      error: errorMessage
    });
    return 1;
  }
}

function processFilesForCleanup(
  filesToProcess: string[],
  workingDir: string,
  dryRun: boolean
): number {
  let deletedCount = 0;
  let cleanedCount = 0;
  let errorCount = 0;

  for (const file of filesToProcess) {
    const relativePath = path.relative(workingDir, file);

    try {
      const result = cleanSingleFile(file, relativePath, dryRun);
      if (result === 'deleted') deletedCount++;
      else cleanedCount++;
    } catch (error) {
      const errorMessage = error instanceof Error ? error.message : String(error);
      logger.warn('CLAUDE_MD', `Error processing ${relativePath}`, { error: errorMessage });
      errorCount++;
    }
  }

  logger.info('CLAUDE_MD', 'CLAUDE.md cleanup complete', {
    deleted: deletedCount,
    cleaned: cleanedCount,
    errors: errorCount,
    dryRun
  });

  return errorCount > 0 ? 1 : 0;
}

export function cleanSingleFile(file: string, relativePath: string, dryRun: boolean): 'deleted' | 'cleaned' {
  let remaining = readFileSync(file, 'utf-8');
  for (let block = findContextBlockRange(remaining); block; block = findContextBlockRange(remaining)) {
    remaining = remaining.slice(0, block.start) + remaining.slice(block.end);
  }
  // Drop whole blank lines a leading managed block leaves behind, but keep the
  // first authored line's indentation (an indented Markdown code block).
  const stripped = remaining.replace(/^(?:[ \t]*\r?\n)+/, '').trimEnd();

  if (stripped === '') {
    if (!dryRun) {
      unlinkSync(file);
    }
    logger.debug('CLAUDE_MD', `${dryRun ? '[DRY-RUN] Would delete' : 'Deleted'} (empty): ${relativePath}`);
    return 'deleted';
  } else {
    if (!dryRun) {
      writeFileSync(file, stripped);
    }
    logger.debug('CLAUDE_MD', `${dryRun ? '[DRY-RUN] Would clean' : 'Cleaned'}: ${relativePath}`);
    return 'cleaned';
  }
}

export async function cleanClaudeMd(dryRun: boolean): Promise<number> {
  const workingDir = process.cwd();

  logger.info('CLAUDE_MD', 'Starting CLAUDE.md cleanup', {
    workingDir,
    dryRun
  });

  const filesToProcess: string[] = [];

  function walkForClaudeMd(dir: string): void {
    const ignorePatterns = [
      'node_modules', '.git', '.next', 'dist', 'build', '.cache',
      '__pycache__', '.venv', 'venv', '.idea', '.vscode', 'coverage',
      '.claude-mem', '.open-next', '.turbo'
    ];

    try {
      const entries = readdirSync(dir, { withFileTypes: true });
      for (const entry of entries) {
        const fullPath = path.join(dir, entry.name);

        if (entry.isDirectory()) {
          if (!ignorePatterns.includes(entry.name)) {
            walkForClaudeMd(fullPath);
          }
        } else if (entry.name === 'CLAUDE.md') {
          try {
            const content = readFileSync(fullPath, 'utf-8');
            if (findContextBlockRange(content)) {
              filesToProcess.push(fullPath);
            }
          } catch {
            // Skip files we can't read
          }
        }
      }
    } catch {
      // Ignore permission errors
    }
  }

  walkForClaudeMd(workingDir);

  if (filesToProcess.length === 0) {
    logger.info('CLAUDE_MD', 'No CLAUDE.md files with auto-generated content found');
    return 0;
  }

  logger.info('CLAUDE_MD', `Found ${filesToProcess.length} CLAUDE.md files with auto-generated content`);

  try {
    return processFilesForCleanup(filesToProcess, workingDir, dryRun);
  } catch (error) {
    const errorMessage = error instanceof Error ? error.message : String(error);
    logger.error('CLAUDE_MD', 'Fatal error during CLAUDE.md cleanup', {
      error: errorMessage
    });
    return 1;
  }
}