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

3.6 KB
Raw Blame

import { Database } from 'bun:sqlite';
import { projectReadKeys, projectScopeSql } from '../project-read-keys.js';
import type { ObservationRecord } from '../../../types/database.js';
import { DEFAULT_PLATFORM_SOURCE, normalizePlatformSource } from '../../../shared/platform-source.js';
import { logger } from '../../../utils/logger.js';

export function getObservationsByFilePath(
  db: Database,
  filePath: string | string[],
  options?: { projects?: string[]; limit?: number; platformSource?: string }
): ObservationRecord[] {
  const rawLimit = options?.limit;
  const limit = Number.isInteger(rawLimit) && (rawLimit as number) > 0
    ? Math.min(rawLimit as number, 100)
    : 15;

  // #2691 — PreToolUse:Read and PostToolUse can disagree on the stored path
  // form (absolute vs project-root-relative vs cwd-relative). Accept multiple
  // candidate path forms and match observations whose files_read/files_modified
  // contain ANY of them, so context injection keyed on path is consistent
  // across the two events. De-duplicate to keep the IN() clause minimal.
  // A Windows tool input is stored as written (`C:\proj\src\a.ts`) while
  // callers send forward-slashed forms, so each candidate also matches its
  // backslashed spelling, as folder lookups already do.
  const paths = (Array.isArray(filePath) ? filePath : [filePath])
    .filter(p => typeof p === 'string' && p.length > 0);
  // An absolute POSIX filename also determines how its relative candidates read.
  const literalBackslashes = paths.some(p => p.startsWith('/') && !p.startsWith('//') && p.includes('\\'));
  const candidatePaths = Array.from(
    new Set(paths
      .flatMap(p => {
        const forward = (literalBackslashes ? p : p.replaceAll('\\', '/')).replace(/^(\.\/)+/, '');
        const forms = [p, forward];
        // Read resolves away ./, but captured tool evidence keeps it verbatim.
        // Only relative paths have this equivalent current-directory spelling.
        if (forward && !/^(\/|[A-Za-z]:|~\/)/.test(forward)) forms.push(`./${forward}`);
        return literalBackslashes ? forms : forms.flatMap(form => [form, form.replaceAll('/', '\\')]);
      }))
  );
  if (candidatePaths.length === 0) {
    logger.debug('DB', 'Skipping observation file lookup with no candidate paths');
    return [];
  }

  const pathPlaceholders = candidatePaths.map(() => '?').join(',');
  // The path is bound once: observation_files indexes both files_read and files_modified.
  const params: (string | number)[] = [...candidatePaths];

  let projectClause = '';
  if (options?.projects?.length) {
    const projects = projectReadKeys(db, options.projects);
    const scope = projectScopeSql('o', projects, { includeMerged: true });
    projectClause = `AND ${scope.sql}`;
    params.push(...scope.params);
  }

  let platformClause = '';
  if (options?.platformSource) {
    platformClause = `AND COALESCE(NULLIF(s.platform_source, ''), '${DEFAULT_PLATFORM_SOURCE}') = ?`;
    params.push(normalizePlatformSource(options.platformSource));
  }

  params.push(limit);

  // observation_files holds one row per path an observation reads or modifies,
  // so this is an index seek on the primary key instead of json_each over every
  // observation. The LEFT JOIN to sdk_sessions stays only for the platform filter.
  const stmt = db.prepare(`
    SELECT DISTINCT o.*
    FROM observation_files f
    JOIN observations o ON o.id = f.observation_id
    LEFT JOIN sdk_sessions s ON s.memory_session_id = o.memory_session_id
    WHERE f.path IN (${pathPlaceholders})
    ${projectClause}
    ${platformClause}
    ORDER BY o.created_at_epoch DESC, o.id DESC
    LIMIT ?
  `);

  return stmt.all(...params) as ObservationRecord[];
}