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

28.4 KB
Raw Blame
/**
 * Claude Code auto-memory ingest.
 *
 * Claude Code writes "auto memory" — markdown it distills for itself — to
 *   <CLAUDE_CONFIG_DIR>/projects/<encoded-cwd>/memory/MEMORY.md   (an index of links)
 *   <CLAUDE_CONFIG_DIR>/projects/<encoded-cwd>/memory/<topic>.md  (distilled prose)
 * where <encoded-cwd> is the repo's absolute path with every character outside
 * [a-zA-Z0-9] replaced by '-', and a name over 200 characters cut to 200 plus a
 * hash suffix (see `cwdToDashed`; CLAUDE_CONFIG_DIR defaults to ~/.claude).
 *
 * Unlike transcripts, memory is ALREADY distilled — each topic file is the same
 * KIND of artifact the observation generator produces. So memory-ingest does NOT
 * run the generation pipeline. It stores each file's prose DIRECTLY as an
 * observation (mechanical store-direct, zero model spend), reusing claude-mem's
 * existing `storeObservation` seam (content-hash dedup + Chroma sync).
 *
 * Reads stay inside Claude Code's projects directory: every source is resolved
 * (symlinks included) and must lie within it. A sibling transcript is read only
 * for its `cwd`, to key the project; transcript content is never ingested.
 *
 * This module is the DB-free half: enumerate + parse + count. The real store
 * path (`ingestMemorySource`) runs inside the worker where the SQLite store lives.
 */
import {
  closeSync,
  constants as fsConstants,
  existsSync,
  fstatSync,
  lstatSync,
  openSync,
  readFileSync,
  readdirSync,
  realpathSync,
  statSync,
} from 'fs';
import { open, type FileHandle } from 'node:fs/promises';
import { basename, dirname, isAbsolute, join, relative } from 'path';
import { StringDecoder } from 'node:string_decoder';
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import { CLAUDE_CONFIG_DIR } from '../../shared/paths.js';
import { expandHome } from '../../shared/expand-home.js';
import { cwdToDashed } from '../context/ObservationCompiler.js';
import { getProjectContext } from '../../utils/project-name.js';
import { retainTextHeadTail } from '../../shared/text-truncation.js';

const MD_EXT = '.md';
const JSONL_EXT = '.jsonl';
/** The link-only index Claude Code maintains; carries no knowledge of its own. */
const INDEX_FILE = 'MEMORY.md';
const MEMORY_SUBDIR = 'memory';
/** Read chunk size for finding the project's real cwd in a sibling transcript. */
const CWD_SNIFF_BYTES = 16_384;
/**
 * Claude Code's memory notes are short distilled prose. Anything larger is not
 * a note (a log or a dump dropped into memory/) and is skipped rather than
 * stored and synced as one observation.
 */
export const MAX_MEMORY_FILE_BYTES = 64 * 1024;
/**
 * With O_NOFOLLOW, open() itself refuses a symlink (ELOOP), so a file swapped
 * for a symlink after its lstat check is still never followed. Windows has no
 * O_NOFOLLOW; there the lstat check is the guard.
 */
const OPEN_NO_FOLLOW_FLAGS = fsConstants.O_RDONLY | (fsConstants.O_NOFOLLOW ?? 0);

/** Claude Code's projects directory: the only tree memory ingest reads. */
export function claudeProjectsDir(): string {
  return join(CLAUDE_CONFIG_DIR, 'projects');
}

/**
 * The `memory/` dir for a repo cwd, using Claude Code's path encoding (the same
 * `cwdToDashed` the prior-session transcript lookup uses). E.g.
 *   /home/u/code/mm/observability → -home-u-code-mm-observability/memory
 */
export function memoryDirForCwd(cwd: string): string {
  return join(claudeProjectsDir(), cwdToDashed(cwd), MEMORY_SUBDIR);
}

/** A source the caller asked for that cannot be read: reported as a bad request. */
export class MemorySourceError extends Error {
  constructor(message: string) {
    super(message);
    this.name = 'MemorySourceError';
  }
}

function errorMessage(error: unknown): string {
  return error instanceof Error ? error.message : String(error);
}

function isWithin(child: string, parent: string): boolean {
  const rel = relative(parent, child);
  return rel === '' || (!rel.startsWith('..') && !isAbsolute(rel));
}

/**
 * Resolve `source` (with `~` and symlinks expanded) and require it to be a
 * directory inside `root`. Throws MemorySourceError otherwise.
 */
function resolveSourceWithinRoot(source: string, root: string): { resolved: string; rootReal: string } {
  const expanded = expandHome(source);
  if (!existsSync(expanded)) {
    throw new MemorySourceError(`memory ingest source not found: ${expanded}`);
  }
  if (!statSync(expanded).isDirectory()) {
    throw new MemorySourceError(`memory ingest source is not a directory: ${expanded}`);
  }
  if (!existsSync(root)) {
    throw new MemorySourceError(`Claude Code projects directory not found: ${root}`);
  }
  const resolved = realpathSync(expanded);
  const rootReal = realpathSync(root);
  if (!isWithin(resolved, rootReal)) {
    throw new MemorySourceError(
      `memory ingest reads only Claude Code's projects directory (${root}); ${expanded} is outside it`,
    );
  }
  return { resolved, rootReal };
}

// ───────────────────────────── frontmatter ──────────────────────────────
//
// Topic files carry a small YAML frontmatter block, e.g.
//   ---
//   name: recent-work
//   description: "What was done in the most recent session"
//   metadata:
//     node_type: memory
//     type: project
//     originSessionId: 74e59070-...
//   ---
// We hand-roll a parser for exactly this shape rather than add a YAML dep.

export interface MemoryFrontmatter {
  name?: string;
  description?: string;
  /** metadata.type, or top-level type before native stamping — project | feedback | reference | user. */
  type?: string;
  /** metadata.originSessionId — the session that produced this memory. */
  originSessionId?: string;
  /** metadata.node_type — e.g. "memory". */
  nodeType?: string;
}

const YAML_ESCAPES: Readonly<Record<string, string>> = {
  '0': '\0', a: '\x07', b: '\b', t: '\t', '\t': '\t', n: '\n',
  v: '\v', f: '\f', r: '\r', e: '\x1b', ' ': ' ', '"': '"',
  '/': '/', '\\': '\\', N: '\u0085', _: '\u00a0', L: '\u2028', P: '\u2029',
};

/** Decode the single-line scalar styles used by these known frontmatter keys. */
function unquote(value: string): string {
  const v = value.trim();
  if (v.length < 2) return v;
  if (v.startsWith("'") && v.endsWith("'")) return v.slice(1, -1).replace(/''/g, "'");
  if (!v.startsWith('"') || !v.endsWith('"')) return v;

  // One pass: a decoded backslash must not start a second escape. Unknown or
  // malformed escapes stay literal so a bad metadata value cannot drop a note.
  return v.slice(1, -1).replace(/\\(x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|.)/g, (match, escape: string) => {
    if (escape.length === 1) return YAML_ESCAPES[escape] ?? match;
    const codePoint = Number.parseInt(escape.slice(1), 16);
    return codePoint <= 0x10ffff && !(codePoint >= 0xd800 && codePoint <= 0xdfff)
      ? String.fromCodePoint(codePoint)
      : match;
  });
}

/**
 * Split leading `--- ... ---` frontmatter from the body. Returns parsed (known)
 * keys and the remaining markdown body. Files without frontmatter return `{}`
 * and the original text as body, apart from a leading UTF-8 byte-order mark.
 */
export function parseMemoryFrontmatter(raw: string): { frontmatter: MemoryFrontmatter; body: string } {
  // A UTF-8 signature is encoding metadata, not part of the first Markdown
  // line. Remove it before recognizing frontmatter or deriving a heading.
  raw = raw.replace(/^\uFEFF/, '');
  const lines = raw.split('\n');
  // The opener must be a complete `---` line at column 0 (a trailing \r is
  // tolerated for CRLF files): a `----` horizontal rule or an indented `---`
  // is note text, not frontmatter. The close scan below likewise requires
  // an exact `---`.
  if (!/^---\r?$/.test(lines[0] ?? '')) return { frontmatter: {}, body: raw };

  // First line is the opening '---'; find the closing one.
  let close = -1;
  for (let i = 1; i < lines.length; i++) {
    if (/^---\r?$/.test(lines[i])) { close = i; break; }
  }
  if (close === -1) return { frontmatter: {}, body: raw };

  const fm: MemoryFrontmatter = {};
  let topLevelType: string | undefined;
  let inMetadata = false;
  let metadataIndent: number | undefined;
  for (let i = 1; i < close; i++) {
    const line = lines[i];
    const trimmed = line.trimStart();
    // YAML comments do not end a mapping or establish its child indentation.
    if (!trimmed || trimmed.startsWith('#')) continue;
    const indent = line.length - trimmed.length;
    const colon = line.indexOf(':');
    if (colon === -1) continue;
    const key = line.slice(0, colon).trim();
    const value = line.slice(colon + 1).trim();

    if (indent === 0) {
      inMetadata = key === 'metadata';
      metadataIndent = undefined;
      if (key === 'name') fm.name = unquote(value);
      else if (key === 'description') fm.description = unquote(value);
      else if (key === 'type') topLevelType = unquote(value);
      continue;
    }
    // Only direct children belong to this note's metadata. Unknown fields may
    // contain nested objects or literal examples with the same key names.
    if (inMetadata) {
      metadataIndent ??= indent;
      if (indent !== metadataIndent) continue;
      if (key === 'type') fm.type = unquote(value);
      else if (key === 'originSessionId') fm.originSessionId = unquote(value);
      else if (key === 'node_type') fm.nodeType = unquote(value);
    }
  }

  // Notes written outside Claude Code's Write/Edit stamper keep their type at the root.
  // Preserve the existing stamped value when a file contains both spellings.
  if (fm.type === undefined && topLevelType !== undefined) fm.type = topLevelType;
  const body = lines.slice(close + 1).join('\n').replace(/^\n+/, '');
  return { frontmatter: fm, body };
}

const memoryMarkdown = unified().use(remarkParse);

/** Derive a human title: frontmatter name → first H1 → first H2 → filename stem. */
export function deriveTitle(fileName: string, frontmatter: MemoryFrontmatter, body: string): string {
  if (frontmatter.name && frontmatter.name.trim()) return frontmatter.name.trim();
  let firstH2: string | undefined;
  // A shell comment or Markdown example inside a code block is not a note
  // heading. Use the existing Markdown parser for block context, but retain
  // the source spelling instead of reformatting valid headings.
  for (const node of memoryMarkdown.parse(body).children) {
    if (node.type !== 'heading' || node.position?.start.offset === undefined) continue;
    const line = body.slice(node.position.start.offset, node.position.end.offset).split('\n', 1)[0];
    const h1 = line.match(/^#\s+(.+)/);
    if (h1) return h1[1].trim();
    const h2 = line.match(/^##\s+(.+)/);
    if (h2 && firstH2 === undefined) firstH2 = h2[1].trim();
  }
  return firstH2 ?? basename(fileName, MD_EXT);
}

// ───────────────────────────── enumeration ──────────────────────────────

export interface MemoryFileRef {
  filePath: string;
  fileName: string;
  /** True for the MEMORY.md link index — excluded from ingest. */
  isIndex: boolean;
  bytes: number;
  /** File mtime in epoch ms — used to backdate the stored observation. */
  mtimeEpoch: number;
  title: string;
  frontmatter: MemoryFrontmatter;
  /** Markdown body with the frontmatter block stripped — the stored narrative. */
  body: string;
}

export interface MemoryDirRef {
  memoryDir: string;
  /** The `<encoded-cwd>` directory name (parent of memory/). */
  encodedName: string;
  /** Resolved absolute repo cwd (from a sibling transcript), if found. */
  cwd?: string;
  /** Project key — MUST match live capture's getProjectContext so recall merges. */
  project: string;
  /** Non-index topic files (the ingestable knowledge). */
  files: MemoryFileRef[];
  /** The MEMORY.md index, recorded for reporting but never ingested. */
  indexFile?: MemoryFileRef;
  /** Files (or the whole dir) that could not be read; reported as failed, never abort the run. */
  unreadable: Array<{ fileName: string; reason: string }>;
  /** Files deliberately not read: symlinks (never followed) and oversized files. */
  skipped: Array<{ fileName: string; reason: string }>;
}

export interface ScanOptions {
  /** Treat `source` as the projects root and sweep every `<encoded>/memory/`. */
  all?: boolean;
  /** The directory every source must stay inside. Defaults to claudeProjectsDir(). */
  root?: string;
}

/** Read the first JSONL line that carries a `cwd` from a sibling transcript. */
async function sniffCwd(projectDir: string): Promise<string | undefined> {
  let names: string[];
  try {
    names = readdirSync(projectDir).filter(n => n.endsWith(JSONL_EXT));
  } catch {
    return undefined;
  }
  for (const name of names) {
    const filePath = join(projectDir, name);
    let file: FileHandle | undefined;
    try {
      // A symlinked transcript could point anywhere; only real files are sniffed.
      if (!lstatSync(filePath).isFile()) continue;
      file = await open(filePath, OPEN_NO_FOLLOW_FLAGS);
      const buf = Buffer.alloc(CWD_SNIFF_BYTES);
      const decoder = new StringDecoder('utf8');
      let pending: string[] = [];
      for (;;) {
        // Yield between reads so an unresolved or unfinished transcript does
        // not block other requests in the worker. Do not truncate a valid
        // native record just because a pasted prompt is unusually long.
        const { bytesRead: read } = await file.read(buf, 0, buf.length, null);
        const chunk = read ? decoder.write(buf.subarray(0, read)) : decoder.end();
        // A native user record can exceed one chunk. Parse complete records,
        // stopping at the first cwd without loading the rest of the history.
        const lines = chunk.split('\n');
        const tail = read ? lines.pop()! : '';
        for (const line of lines) {
          const trimmed = (pending.join('') + line).trim();
          pending = [];
          if (!trimmed) continue;
          try {
            const cwd = (JSON.parse(trimmed) as { cwd?: unknown }).cwd;
            if (typeof cwd === 'string' && cwd.trim()) return cwd;
          } catch {
            // A malformed record or an incomplete final write has no cwd.
          }
        }
        if (!read) break;
        if (tail) pending.push(tail);
      }
    } catch {
      // Unreadable transcript — try the next sibling.
    } finally {
      await file?.close();
    }
  }
  return undefined;
}

/** Best-effort project key when no sibling transcript reveals the cwd. */
function fallbackProject(cwd: string | undefined, encodedName: string): string {
  if (cwd) {
    try {
      const primary = getProjectContext(cwd).primary;
      if (primary && primary.trim()) return primary;
    } catch {
      // getProjectContext may probe git; fall through to the basename.
    }
    const leaf = basename(cwd);
    if (leaf) return leaf;
  }
  // Last resort: the trailing segment of the encoded dir name.
  const segs = encodedName.replace(/^-+/, '').split('-').filter(Boolean);
  return segs.length ? segs[segs.length - 1] : encodedName;
}

async function readMemoryDir(memoryDir: string): Promise<MemoryDirRef> {
  const projectDir = dirname(memoryDir);
  const encodedName = basename(projectDir);
  const cwd = await sniffCwd(projectDir);
  const project = fallbackProject(cwd, encodedName);

  const ref: MemoryDirRef = { memoryDir, encodedName, cwd, project, files: [], unreadable: [], skipped: [] };

  for (const name of readdirSync(memoryDir)) {
    if (!name.endsWith(MD_EXT)) continue;
    const filePath = join(memoryDir, name);
    // One unreadable file is reported and skipped; it never aborts the run.
    let fd: number | undefined;
    try {
      // Symlinks are never followed: a symlinked note (notes.md ->
      // ~/.aws/credentials) would otherwise be read, stored and synced. lstat
      // names the skip; the O_NOFOLLOW open refuses a note swapped for a
      // symlink after this check, and the size and contents both come from
      // the file that open returned.
      const linkStat = lstatSync(filePath);
      if (linkStat.isSymbolicLink()) {
        ref.skipped.push({ fileName: name, reason: 'symlink (not followed)' });
        continue;
      }
      if (!linkStat.isFile()) continue;
      fd = openSync(filePath, OPEN_NO_FOLLOW_FLAGS);
      const stat = fstatSync(fd);
      if (!stat.isFile()) continue;
      if (stat.size > MAX_MEMORY_FILE_BYTES) {
        ref.skipped.push({ fileName: name, reason: `larger than ${MAX_MEMORY_FILE_BYTES} bytes` });
        continue;
      }

      const raw = readFileSync(fd, 'utf-8');
      const { frontmatter, body } = parseMemoryFrontmatter(raw);
      const isIndex = name === INDEX_FILE;
      const fileRef: MemoryFileRef = {
        filePath,
        fileName: name,
        isIndex,
        bytes: stat.size,
        mtimeEpoch: Math.round(stat.mtimeMs),
        title: deriveTitle(name, frontmatter, body),
        frontmatter,
        body,
      };
      if (isIndex) ref.indexFile = fileRef;
      else ref.files.push(fileRef);
    } catch (error: unknown) {
      ref.unreadable.push({ fileName: name, reason: errorMessage(error) });
    } finally {
      if (fd !== undefined) closeSync(fd);
    }
  }

  ref.files.sort((a, b) => a.fileName.localeCompare(b.fileName));
  return ref;
}

function hasMemorySubdir(dir: string): boolean {
  const sub = join(dir, MEMORY_SUBDIR);
  return existsSync(sub) && statSync(sub).isDirectory();
}

/**
 * Enumerate memory directories under `source`.
 *
 * `source` may be:
 *   - a `memory/` directory itself,
 *   - a `<encoded-cwd>` project directory that contains a `memory/` subdir,
 *   - (with `all: true`) the projects root, swept for every `<encoded>/memory/`.
 *
 * Every source must resolve inside `options.root` (Claude Code's projects dir
 * by default); a symlinked memory dir that leads outside it is skipped (sweep)
 * or refused (single target). Throws MemorySourceError for a source that is
 * missing, not a directory, outside the root, or unreadable.
 */
export async function scanMemorySource(source: string, options: ScanOptions = {}): Promise<MemoryDirRef[]> {
  const { resolved, rootReal } = resolveSourceWithinRoot(source, options.root ?? claudeProjectsDir());

  if (options.all) {
    const refs: MemoryDirRef[] = [];
    for (const name of readdirSync(resolved)) {
      const memoryDir = join(resolved, name, MEMORY_SUBDIR);
      try {
        if (!statSync(join(resolved, name)).isDirectory() || !hasMemorySubdir(join(resolved, name))) continue;
        if (!isWithin(realpathSync(memoryDir), rootReal)) continue;
      } catch {
        continue;
      }
      try {
        refs.push(await readMemoryDir(memoryDir));
      } catch (error: unknown) {
        refs.push({
          memoryDir,
          encodedName: name,
          project: name,
          files: [],
          unreadable: [{ fileName: MEMORY_SUBDIR, reason: errorMessage(error) }],
          skipped: [],
        });
      }
    }
    refs.sort((a, b) => a.encodedName.localeCompare(b.encodedName));
    return refs;
  }

  // Single target: the memory/ dir itself, its parent project dir, or a
  // directory of *.md with no memory/ subdir (treated as a memory dir).
  const memoryDir = basename(resolved) === MEMORY_SUBDIR || !hasMemorySubdir(resolved)
    ? resolved
    : join(resolved, MEMORY_SUBDIR);
  if (!isWithin(realpathSync(memoryDir), rootReal)) {
    throw new MemorySourceError(`memory ingest reads only Claude Code's projects directory; ${memoryDir} leads outside it`);
  }
  try {
    return [await readMemoryDir(memoryDir)];
  } catch (error: unknown) {
    throw new MemorySourceError(`memory ingest source cannot be read: ${errorMessage(error)}`);
  }
}

// ───────────────────────────── dry-run ──────────────────────────────

export interface MemoryDirCounts {
  encodedName: string;
  project: string;
  cwd?: string;
  cwdResolved: boolean;
  files: number;
  indexSkipped: boolean;
  bytes: number;
  /** Files that could not be read; a real ingest reports them as failed. */
  unreadable: number;
  /** Symlinked or oversized files, never read. */
  skipped: number;
}

export interface MemoryDryRunReport {
  source: string;
  all: boolean;
  dirs: MemoryDirCounts[];
  totals: {
    dirs: number;
    /** One observation per non-index file (the store count — NO Haiku). */
    files: number;
    bytes: number;
    cwdUnresolved: number;
    unreadable: number;
    skipped: number;
  };
}

/**
 * Enumerate + parse every memory dir under `source` and report what a real
 * ingest WOULD store. No DB, no Haiku — memory is stored mechanically, so the
 * file count IS the observation count (modulo content-hash dedup at store time).
 */
export async function dryRunMemorySource(source: string, options: ScanOptions = {}): Promise<MemoryDryRunReport> {
  const refs = await scanMemorySource(source, options);
  const dirs: MemoryDirCounts[] = refs.map(ref => ({
    encodedName: ref.encodedName,
    project: ref.project,
    cwd: ref.cwd,
    cwdResolved: !!ref.cwd,
    files: ref.files.length,
    indexSkipped: !!ref.indexFile,
    bytes: ref.files.reduce((n, f) => n + f.bytes, 0),
    unreadable: ref.unreadable.length,
    skipped: ref.skipped.length,
  }));

  return {
    source: expandHome(source),
    all: !!options.all,
    dirs,
    totals: {
      dirs: dirs.length,
      files: dirs.reduce((n, d) => n + d.files, 0),
      bytes: dirs.reduce((n, d) => n + d.bytes, 0),
      cwdUnresolved: dirs.filter(d => !d.cwdResolved).length,
      unreadable: dirs.reduce((n, d) => n + d.unreadable, 0),
      skipped: dirs.reduce((n, d) => n + d.skipped, 0),
    },
  };
}

/** Render a dry-run report as human-readable CLI lines. */
export function formatMemoryDryRunReport(report: MemoryDryRunReport): string {
  const lines: string[] = [];
  lines.push(`Memory dry-run (no Haiku, mechanical store) — source: ${report.source}`);
  lines.push(`Mode: ${report.all ? 'all projects' : 'single'}`);
  lines.push('');
  for (const d of report.dirs) {
    const cwdTag = d.cwdResolved ? d.project : `${d.project} (cwd UNRESOLVED)`;
    lines.push(
      `${cwdTag}: ${d.files} files → ${d.files} obs, ${(d.bytes / 1024).toFixed(1)} KB` +
        (d.indexSkipped ? ', index skipped' : '')
    );
  }
  lines.push('');
  const t = report.totals;
  lines.push(
    `TOTAL: ${t.dirs} memory dirs → ${t.files} files = ~${t.files} observations ` +
      `(${(t.bytes / 1024).toFixed(1)} KB prose)`
  );
  if (t.cwdUnresolved) {
    lines.push(
      `WARNING: ${t.cwdUnresolved} dir(s) had no sibling transcript to resolve cwd — ` +
        `project key is a best-effort guess and may not merge with live capture.`
    );
  }
  if (t.unreadable) {
    lines.push(`WARNING: ${t.unreadable} file(s) could not be read and would be reported as failed.`);
  }
  if (t.skipped) {
    lines.push(`NOTE: ${t.skipped} file(s) skipped: symlinks are never followed; files over ${MAX_MEMORY_FILE_BYTES} bytes are not notes.`);
  }
  lines.push('NOTE: mechanical store — memory prose is stored as-is, no model spend. Dedup by content_hash.');
  return lines.join('\n');
}

// ───────────────────────────── real ingest ──────────────────────────────
//
// The store path. Mechanical — NO Haiku. Each memory file becomes one
// observation whose `narrative` IS the file body. Runs inside the worker (the
// SQLite store lives there), driven by injected deps so it stays unit-testable
// without a live worker.

/** One observation to store, derived purely from a memory file (no model). */
export interface MemoryObservationToStore {
  project: string;
  type: string;
  title: string;
  subtitle: string;
  narrative: string;
  concepts: string[];
  /** Persisted JSON provenance (SessionStore.storeObservation writes `metadata`). */
  metadata: Record<string, unknown>;
  /** Backdate to the file's mtime so imported memory sorts chronologically. */
  createdAtEpoch: number;
  sourceFile: string;
  originSessionId?: string;
}

export interface MemoryIngestDeps {
  /**
   * Persist one observation mechanically (storeObservation + Chroma sync) and
   * report whether it was newly inserted or collapsed onto an existing row by
   * content_hash. Implemented by the worker route; faked in tests.
   */
  storeMemoryObservation(obs: MemoryObservationToStore): Promise<{ id: number; deduped: boolean }>;
}

export interface MemoryIngestOptions extends ScanOptions {
  /** Skip dirs whose cwd could not be resolved (avoids best-effort project keys). */
  requireCwd?: boolean;
}

export interface MemoryFileIngestResult {
  project: string;
  file: string;
  status: 'stored' | 'deduped' | 'skipped' | 'failed';
  observationId?: number;
  reason?: string;
}

export interface MemoryIngestReport {
  source: string;
  all: boolean;
  dirs: number;
  found: number;
  stored: number;
  deduped: number;
  skipped: number;
  failed: number;
  cwdUnresolvedDirs: number;
  files: MemoryFileIngestResult[];
}

/**
 * Map a memory file onto an observation. Pure (no I/O) → directly unit-testable.
 * Full provenance rides in `metadata` (persisted by SessionStore.storeObservation);
 * a couple of lightweight `concepts` tags make imported memory searchable.
 */
export function buildMemoryObservation(ref: MemoryDirRef, file: MemoryFileRef): MemoryObservationToStore {
  const memoryType = file.frontmatter.type ?? 'topic';
  const concepts = ['memory-import', `memory-type:${memoryType}`];
  const description = file.frontmatter.description?.trim();
  const subtitle = description && description.length
    ? retainTextHeadTail(description, 300, 0).head
    : `Imported memory (${memoryType})`;

  const metadata: Record<string, unknown> = {
    source: 'memory-import',
    file: file.fileName,
    memoryType,
    encodedDir: ref.encodedName,
  };
  if (file.frontmatter.originSessionId) metadata.originSessionId = file.frontmatter.originSessionId;
  if (ref.cwd) metadata.cwd = ref.cwd;

  return {
    project: ref.project,
    type: 'discovery',
    title: retainTextHeadTail(file.title, 200, 0).head,
    subtitle,
    narrative: file.body,
    concepts,
    metadata,
    createdAtEpoch: file.mtimeEpoch,
    sourceFile: file.fileName,
    originSessionId: file.frontmatter.originSessionId,
  };
}

/**
 * Store every memory file under `source` as an observation. Idempotent by
 * content_hash (re-runs dedupe). Orphaned memory (no sibling transcript to
 * resolve cwd) is ingested by default with a best-effort project key — that is
 * the whole point of memory-direct ingest; pass `requireCwd` to skip it instead.
 */
export async function ingestMemorySource(
  source: string,
  options: MemoryIngestOptions,
  deps: MemoryIngestDeps
): Promise<MemoryIngestReport> {
  const refs = await scanMemorySource(source, { all: options.all, root: options.root });

  const report: MemoryIngestReport = {
    source: expandHome(source),
    all: !!options.all,
    dirs: refs.length,
    found: 0,
    stored: 0,
    deduped: 0,
    skipped: 0,
    failed: 0,
    cwdUnresolvedDirs: refs.filter(r => !r.cwd).length,
    files: [],
  };

  for (const ref of refs) {
    for (const { fileName, reason } of ref.unreadable) {
      report.found++;
      report.failed++;
      report.files.push({ project: ref.project, file: fileName, status: 'failed', reason });
    }
    for (const { fileName, reason } of ref.skipped) {
      report.found++;
      report.skipped++;
      report.files.push({ project: ref.project, file: fileName, status: 'skipped', reason });
    }
    for (const file of ref.files) {
      report.found++;
      const result: MemoryFileIngestResult = { project: ref.project, file: file.fileName, status: 'stored' };

      if (options.requireCwd && !ref.cwd) {
        result.status = 'skipped';
        result.reason = 'cwd-unresolved';
        report.skipped++;
        report.files.push(result);
        continue;
      }
      if (!file.body.trim()) {
        result.status = 'skipped';
        result.reason = 'empty-body';
        report.skipped++;
        report.files.push(result);
        continue;
      }

      try {
        const { id, deduped } = await deps.storeMemoryObservation(buildMemoryObservation(ref, file));
        result.observationId = id;
        if (deduped) {
          result.status = 'deduped';
          report.deduped++;
        } else {
          report.stored++;
        }
      } catch (err) {
        result.status = 'failed';
        result.reason = err instanceof Error ? err.message : String(err);
        report.failed++;
      }
      report.files.push(result);
    }
  }
  return report;
}