| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045 |
- /**
- * CodeGraph
- *
- * A local-first code intelligence system that builds a semantic
- * knowledge graph from any codebase.
- */
- import * as path from 'path';
- import {
- Node,
- Edge,
- FileRecord,
- ExtractionResult,
- Subgraph,
- TraversalOptions,
- SearchOptions,
- SearchResult,
- Context,
- GraphStats,
- TaskInput,
- TaskContext,
- BuildContextOptions,
- FindRelevantContextOptions,
- } from './types';
- import { DatabaseConnection, getDatabasePath } from './db';
- import { QueryBuilder } from './db/queries';
- import {
- isInitialized,
- createDirectory,
- removeDirectory,
- validateDirectory,
- } from './directory';
- import {
- ExtractionOrchestrator,
- IndexProgress,
- IndexResult,
- SyncResult,
- extractFromSource,
- initGrammars,
- } from './extraction';
- import {
- ReferenceResolver,
- createResolver,
- ResolutionResult,
- } from './resolution';
- import { GraphTraverser, GraphQueryManager } from './graph';
- import { ContextBuilder, createContextBuilder } from './context';
- import { Mutex, FileLock } from './utils';
- import { FileWatcher, WatchOptions, PendingFile, LockUnavailableError } from './sync';
- // Re-export types for consumers
- export * from './types';
- // Storage building blocks for embedded/SDK consumers that drive the graph
- // directly (open a DB, run prepared queries) rather than through the CodeGraph
- // facade. Exposed from the package entry so they no longer require deep imports
- // into dist/ (issue #354).
- export { getDatabasePath, DatabaseConnection } from './db';
- export { QueryBuilder } from './db/queries';
- export {
- getCodeGraphDir,
- isInitialized,
- findNearestCodeGraphRoot,
- CODEGRAPH_DIR,
- } from './directory';
- export { IndexProgress, IndexResult, SyncResult } from './extraction';
- export { detectLanguage, isLanguageSupported, isGrammarLoaded, getSupportedLanguages, initGrammars, loadGrammarsForLanguages, loadAllGrammars } from './extraction';
- export { ResolutionResult } from './resolution';
- export {
- CodeGraphError,
- FileError,
- ParseError,
- DatabaseError,
- SearchError,
- VectorError,
- ConfigError,
- Logger,
- setLogger,
- getLogger,
- silentLogger,
- defaultLogger,
- } from './errors';
- export { Mutex, FileLock, processInBatches, debounce, throttle, MemoryMonitor } from './utils';
- export { FileWatcher, WatchOptions, PendingFile, LockUnavailableError } from './sync';
- export { MCPServer } from './mcp';
- /**
- * Options for initializing a new CodeGraph project
- */
- export interface InitOptions {
- /** Whether to run initial indexing after init */
- index?: boolean;
- /** Progress callback for indexing */
- onProgress?: (progress: IndexProgress) => void;
- }
- /**
- * Options for opening an existing CodeGraph project
- */
- export interface OpenOptions {
- /** Whether to run sync if files have changed */
- sync?: boolean;
- /** Whether to run in read-only mode */
- readOnly?: boolean;
- }
- /**
- * Options for indexing
- */
- export interface IndexOptions {
- /** Progress callback */
- onProgress?: (progress: IndexProgress) => void;
- /** Abort signal for cancellation */
- signal?: AbortSignal;
- /** Enable verbose logging (worker lifecycle, memory, timeouts) */
- verbose?: boolean;
- }
- /**
- * Main CodeGraph class
- *
- * Provides the primary interface for interacting with the code knowledge graph.
- */
- export class CodeGraph {
- private db: DatabaseConnection;
- private queries: QueryBuilder;
- private projectRoot: string;
- private orchestrator: ExtractionOrchestrator;
- private resolver: ReferenceResolver;
- private graphManager: GraphQueryManager;
- private traverser: GraphTraverser;
- private contextBuilder: ContextBuilder;
- // Mutex for preventing concurrent indexing operations (in-process)
- private indexMutex = new Mutex();
- // File lock for preventing concurrent writes across processes (CLI, MCP, git hooks)
- private fileLock: FileLock;
- // File watcher for auto-sync on file changes
- private watcher: FileWatcher | null = null;
- private constructor(
- db: DatabaseConnection,
- queries: QueryBuilder,
- projectRoot: string
- ) {
- this.db = db;
- this.queries = queries;
- this.projectRoot = projectRoot;
- this.fileLock = new FileLock(
- path.join(projectRoot, '.codegraph', 'codegraph.lock')
- );
- this.orchestrator = new ExtractionOrchestrator(projectRoot, queries);
- this.resolver = createResolver(projectRoot, queries);
- this.graphManager = new GraphQueryManager(queries);
- this.traverser = new GraphTraverser(queries);
- this.contextBuilder = createContextBuilder(
- projectRoot,
- queries,
- this.traverser
- );
- }
- // ===========================================================================
- // Lifecycle Methods
- // ===========================================================================
- /**
- * Initialize a new CodeGraph project
- *
- * Creates the .CodeGraph directory, database, and configuration.
- *
- * @param projectRoot - Path to the project root directory
- * @param options - Initialization options
- * @returns A new CodeGraph instance
- */
- static async init(projectRoot: string, options: InitOptions = {}): Promise<CodeGraph> {
- await initGrammars();
- const resolvedRoot = path.resolve(projectRoot);
- // Check if already initialized
- if (isInitialized(resolvedRoot)) {
- throw new Error(`CodeGraph already initialized in ${resolvedRoot}`);
- }
- // Create directory structure
- createDirectory(resolvedRoot);
- // Initialize database
- const dbPath = getDatabasePath(resolvedRoot);
- const db = DatabaseConnection.initialize(dbPath);
- const queries = new QueryBuilder(db.getDb());
- const instance = new CodeGraph(db, queries, resolvedRoot);
- // Run initial indexing if requested
- if (options.index) {
- await instance.indexAll({ onProgress: options.onProgress });
- }
- return instance;
- }
- /**
- * Initialize synchronously (without indexing)
- */
- static initSync(projectRoot: string): CodeGraph {
- const resolvedRoot = path.resolve(projectRoot);
- // Check if already initialized
- if (isInitialized(resolvedRoot)) {
- throw new Error(`CodeGraph already initialized in ${resolvedRoot}`);
- }
- // Create directory structure
- createDirectory(resolvedRoot);
- // Initialize database
- const dbPath = getDatabasePath(resolvedRoot);
- const db = DatabaseConnection.initialize(dbPath);
- const queries = new QueryBuilder(db.getDb());
- return new CodeGraph(db, queries, resolvedRoot);
- }
- /**
- * Open an existing CodeGraph project
- *
- * @param projectRoot - Path to the project root directory
- * @param options - Open options
- * @returns A CodeGraph instance
- */
- static async open(projectRoot: string, options: OpenOptions = {}): Promise<CodeGraph> {
- await initGrammars();
- const resolvedRoot = path.resolve(projectRoot);
- // Check if initialized
- if (!isInitialized(resolvedRoot)) {
- throw new Error(`CodeGraph not initialized in ${resolvedRoot}. Run init() first.`);
- }
- // Validate directory structure
- const validation = validateDirectory(resolvedRoot);
- if (!validation.valid) {
- throw new Error(`Invalid CodeGraph directory: ${validation.errors.join(', ')}`);
- }
- // Open database
- const dbPath = getDatabasePath(resolvedRoot);
- const db = DatabaseConnection.open(dbPath);
- const queries = new QueryBuilder(db.getDb());
- const instance = new CodeGraph(db, queries, resolvedRoot);
- // Sync if requested
- if (options.sync) {
- await instance.sync();
- }
- return instance;
- }
- /**
- * Open synchronously (without sync)
- */
- static openSync(projectRoot: string): CodeGraph {
- const resolvedRoot = path.resolve(projectRoot);
- // Check if initialized
- if (!isInitialized(resolvedRoot)) {
- throw new Error(`CodeGraph not initialized in ${resolvedRoot}. Run init() first.`);
- }
- // Validate directory structure
- const validation = validateDirectory(resolvedRoot);
- if (!validation.valid) {
- throw new Error(`Invalid CodeGraph directory: ${validation.errors.join(', ')}`);
- }
- // Open database
- const dbPath = getDatabasePath(resolvedRoot);
- const db = DatabaseConnection.open(dbPath);
- const queries = new QueryBuilder(db.getDb());
- return new CodeGraph(db, queries, resolvedRoot);
- }
- /**
- * Check if a directory has been initialized as a CodeGraph project
- */
- static isInitialized(projectRoot: string): boolean {
- return isInitialized(path.resolve(projectRoot));
- }
- /**
- * Close the CodeGraph instance and release resources
- */
- close(): void {
- this.unwatch();
- // Release file lock if held
- this.fileLock.release();
- this.db.close();
- }
- /**
- * Get the project root directory
- */
- getProjectRoot(): string {
- return this.projectRoot;
- }
- // ===========================================================================
- // Indexing
- // ===========================================================================
- /**
- * Index all files in the project
- *
- * Uses a mutex to prevent concurrent indexing operations.
- */
- async indexAll(options: IndexOptions = {}): Promise<IndexResult> {
- return this.indexMutex.withLock(async () => {
- try {
- this.fileLock.acquire();
- } catch {
- return { success: false, filesIndexed: 0, filesSkipped: 0, filesErrored: 0, nodesCreated: 0, edgesCreated: 0, errors: [{ message: 'Could not acquire file lock - another process may be indexing', severity: 'error' as const }], durationMs: 0 };
- }
- try {
- const before = this.queries.getNodeAndEdgeCount();
- const result = await this.orchestrator.indexAll(options.onProgress, options.signal, options.verbose);
- // Re-detect frameworks now that the index is populated. The resolver
- // is constructed with createResolver() before any files exist, so
- // framework resolvers whose detect() consults the indexed file list
- // (e.g. UIKit/SwiftUI scanning for imports, swift-objc-bridge looking
- // for both Swift and ObjC files) all return false on that initial pass
- // and silently drop themselves. Re-initializing here gives them a
- // chance to see the actual project before resolution runs.
- if (result.success && result.filesIndexed > 0) {
- this.resolver.initialize();
- // Cross-file finalization (e.g. NestJS RouterModule prefixes). Runs
- // before resolution so updated names show up in subsequent reads.
- this.resolver.runPostExtract();
- }
- // Resolve references to create call/import/extends edges
- if (result.success && result.filesIndexed > 0) {
- // Get count without loading all refs into memory
- const unresolvedCount = this.queries.getUnresolvedReferencesCount();
- options.onProgress?.({
- phase: 'resolving',
- current: 0,
- total: unresolvedCount,
- });
- await this.resolveReferencesBatched((current, total) => {
- options.onProgress?.({
- phase: 'resolving',
- current,
- total,
- });
- });
- }
- // Refresh planner stats + checkpoint the WAL after bulk writes.
- // Cheap and non-blocking; never load-bearing for correctness.
- if (result.success && result.filesIndexed > 0) {
- this.db.runMaintenance();
- }
- // The orchestrator only sees extraction-phase counts; resolution and
- // synthesizer edges (often >50% of the graph on JVM repos) come later.
- // Recompute against the DB so the CLI summary reports the true totals.
- if (result.success && result.filesIndexed > 0) {
- const after = this.queries.getNodeAndEdgeCount();
- result.nodesCreated = after.nodes - before.nodes;
- result.edgesCreated = after.edges - before.edges;
- }
- return result;
- } finally {
- this.fileLock.release();
- }
- });
- }
- /**
- * Index specific files
- *
- * Uses a mutex to prevent concurrent indexing operations.
- */
- async indexFiles(filePaths: string[]): Promise<IndexResult> {
- return this.indexMutex.withLock(async () => {
- try {
- this.fileLock.acquire();
- } catch {
- return { success: false, filesIndexed: 0, filesSkipped: 0, filesErrored: 0, nodesCreated: 0, edgesCreated: 0, errors: [{ message: 'Could not acquire file lock - another process may be indexing', severity: 'error' as const }], durationMs: 0 };
- }
- try {
- return this.orchestrator.indexFiles(filePaths);
- } finally {
- this.fileLock.release();
- }
- });
- }
- /**
- * Sync with current file state (incremental update)
- *
- * Uses a mutex to prevent concurrent indexing operations.
- */
- async sync(options: IndexOptions = {}): Promise<SyncResult> {
- return this.indexMutex.withLock(async () => {
- try {
- this.fileLock.acquire();
- } catch {
- return { filesChecked: 0, filesAdded: 0, filesModified: 0, filesRemoved: 0, nodesUpdated: 0, durationMs: 0 };
- }
- try {
- const result = await this.orchestrator.sync(options.onProgress);
- // Cross-file finalization (e.g. NestJS RouterModule prefixes). Run on
- // every sync that touched files so edits to `app.module.ts` propagate
- // to controllers in unchanged files. The pass is idempotent and cheap
- // (regex over *.module.ts only).
- if (result.filesAdded > 0 || result.filesModified > 0) {
- this.resolver.runPostExtract();
- }
- // Resolve references if files were updated
- if (result.filesAdded > 0 || result.filesModified > 0) {
- if (result.changedFilePaths) {
- // Scope resolution to changed files (git fast path — bounded set)
- const unresolvedRefs = this.queries.getUnresolvedReferencesByFiles(result.changedFilePaths);
- options.onProgress?.({
- phase: 'resolving',
- current: 0,
- total: unresolvedRefs.length,
- });
- this.resolver.resolveAndPersist(unresolvedRefs, (current, total) => {
- options.onProgress?.({
- phase: 'resolving',
- current,
- total,
- });
- });
- } else {
- // No git info — use batched resolution to avoid OOM
- const unresolvedCount = this.queries.getUnresolvedReferencesCount();
- options.onProgress?.({
- phase: 'resolving',
- current: 0,
- total: unresolvedCount,
- });
- await this.resolveReferencesBatched((current, total) => {
- options.onProgress?.({
- phase: 'resolving',
- current,
- total,
- });
- });
- }
- }
- // Refresh planner stats + checkpoint the WAL after bulk writes.
- if (result.filesAdded > 0 || result.filesModified > 0 || result.filesRemoved > 0) {
- this.db.runMaintenance();
- }
- return result;
- } finally {
- this.fileLock.release();
- }
- });
- }
- /**
- * Check if an indexing operation is currently in progress
- */
- isIndexing(): boolean {
- return this.indexMutex.isLocked();
- }
- // ===========================================================================
- // File Watching
- // ===========================================================================
- /**
- * Start watching for file changes and auto-syncing.
- *
- * Uses native OS file events (FSEvents on macOS, inotify on Linux 19+,
- * ReadDirectoryChangesW on Windows) with debouncing to avoid thrashing.
- *
- * @param options - Watch options (debounce delay, callbacks)
- * @returns true if watching started successfully
- */
- watch(options: WatchOptions = {}): boolean {
- if (this.watcher?.isActive()) return true;
- this.watcher = new FileWatcher(
- this.projectRoot,
- async () => {
- const result = await this.sync();
- // sync() returns this exact zero-shape iff it failed to acquire the
- // file lock (a real empty sync always has filesChecked > 0 because
- // scanDirectory ran). Surface that to the watcher as a typed error
- // so it keeps pendingFiles + reschedules instead of clearing them
- // (#449).
- if (result.filesChecked === 0 && result.durationMs === 0) {
- throw new LockUnavailableError();
- }
- const filesChanged = result.filesAdded + result.filesModified + result.filesRemoved;
- return { filesChanged, durationMs: result.durationMs };
- },
- options
- );
- return this.watcher.start();
- }
- /**
- * Stop watching for file changes.
- */
- unwatch(): void {
- if (this.watcher) {
- this.watcher.stop();
- this.watcher = null;
- }
- }
- /**
- * Check if the file watcher is active.
- */
- isWatching(): boolean {
- return this.watcher?.isActive() ?? false;
- }
- /**
- * Files seen by the file watcher since the last successful sync —
- * the per-file "stale" signal MCP tools attach to responses so an agent
- * can fall back to {@link Read} for just the affected file without
- * waiting for a debounced sync to complete (issue #403).
- *
- * Returns an empty list when the watcher isn't active, or no events have
- * arrived. Each entry includes `firstSeenMs` and `lastSeenMs` (wall-clock
- * `Date.now()` values) so callers can render "edited Nms ago", plus an
- * `indexing` flag indicating whether the in-flight sync (if any) will
- * absorb that file.
- */
- getPendingFiles(): PendingFile[] {
- return this.watcher?.getPendingFiles() ?? [];
- }
- /**
- * Resolves once the file watcher has finished its initial chokidar scan.
- * Useful for tests that need a deterministic boundary before asserting on
- * `getPendingFiles()`. Resolves immediately when no watcher is active.
- */
- waitUntilWatcherReady(timeoutMs?: number): Promise<void> {
- return this.watcher ? this.watcher.waitUntilReady(timeoutMs) : Promise.resolve();
- }
- /**
- * Get files that have changed since last index
- */
- getChangedFiles(): { added: string[]; modified: string[]; removed: string[] } {
- return this.orchestrator.getChangedFiles();
- }
- /**
- * Extract nodes and edges from source code (without storing)
- */
- extractFromSource(filePath: string, source: string): ExtractionResult {
- return extractFromSource(filePath, source);
- }
- // ===========================================================================
- // Reference Resolution
- // ===========================================================================
- /**
- * Resolve unresolved references and create edges
- *
- * This method takes unresolved references from extraction and attempts
- * to resolve them using multiple strategies:
- * - Framework-specific patterns (React, Express, Laravel)
- * - Import-based resolution
- * - Name-based symbol matching
- */
- resolveReferences(onProgress?: (current: number, total: number) => void): ResolutionResult {
- // Get all unresolved references from the database
- const unresolvedRefs = this.queries.getUnresolvedReferences();
- return this.resolver.resolveAndPersist(unresolvedRefs, onProgress);
- }
- /**
- * Resolve references in batches to keep memory bounded on large codebases.
- * Processes chunks of unresolved refs, persisting results after each batch.
- */
- async resolveReferencesBatched(onProgress?: (current: number, total: number) => void): Promise<ResolutionResult> {
- return this.resolver.resolveAndPersistBatched(onProgress);
- }
- /**
- * Get detected frameworks in the project
- */
- getDetectedFrameworks(): string[] {
- return this.resolver.getDetectedFrameworks();
- }
- /**
- * Re-initialize the resolver (useful after adding new files)
- */
- reinitializeResolver(): void {
- this.resolver.initialize();
- }
- // ===========================================================================
- // Graph Statistics
- // ===========================================================================
- /**
- * Get statistics about the knowledge graph
- */
- getStats(): GraphStats {
- const stats = this.queries.getStats();
- stats.dbSizeBytes = this.db.getSize();
- return stats;
- }
- /**
- * Active SQLite backend for this project's connection (`node-sqlite` — Node's
- * built-in real-SQLite module). Surfaced via `codegraph status` and the
- * `codegraph_status` MCP tool alongside the effective journal mode.
- */
- getBackend(): import('./db').SqliteBackend {
- return this.db.getBackend();
- }
- /**
- * The journal mode actually in effect ('wal', 'delete', …). 'wal' means
- * readers never block on a concurrent writer; anything else means they can,
- * which is the precondition for the "database is locked" failures in issue
- * #238. Surfaced via `codegraph status` and the `codegraph_status` MCP tool.
- */
- getJournalMode(): string {
- return this.db.getJournalMode();
- }
- // ===========================================================================
- // Node Operations
- // ===========================================================================
- /**
- * Get a node by ID
- */
- getNode(id: string): Node | null {
- return this.queries.getNodeById(id);
- }
- /**
- * Get all nodes in a file
- */
- getNodesInFile(filePath: string): Node[] {
- return this.queries.getNodesByFile(filePath);
- }
- /**
- * Get all nodes of a specific kind
- */
- getNodesByKind(kind: Node['kind']): Node[] {
- return this.queries.getNodesByKind(kind);
- }
- /**
- * Search nodes by text
- */
- searchNodes(query: string, options?: SearchOptions): SearchResult[] {
- return this.queries.searchNodes(query, options);
- }
- /**
- * Find the project's "primary route file" — the file with the densest
- * concentration of framework-emitted `route` nodes (≥3 routes, ≥30%
- * of all non-test routes). Used to inline the routing config in
- * `codegraph_explore` responses on small realworld template repos
- * (rails-realworld, laravel-realworld, drupal-admintoolbar, …) where
- * Glob+Read of `routes.rb`/`urls.py`/etc. otherwise beats codegraph.
- */
- getTopRouteFile(): { filePath: string; routeCount: number; totalRoutes: number } | null {
- return this.queries.getTopRouteFile();
- }
- /**
- * Build a URL → handler routing manifest from the index. Each entry
- * pairs a route node (URL + method) with its handler function/method
- * via the `references` edge that framework resolvers emit. Returns
- * null when fewer than 3 valid (non-test) routes exist.
- */
- getRoutingManifest(limit?: number): {
- entries: Array<{ url: string; handler: string; handlerFile: string; handlerLine: number; handlerKind: string }>;
- topHandlerFile: string | null;
- topHandlerFileCount: number;
- totalRoutes: number;
- } | null {
- return this.queries.getRoutingManifest(limit);
- }
- // ===========================================================================
- // Edge Operations
- // ===========================================================================
- /**
- * Get outgoing edges from a node
- */
- getOutgoingEdges(nodeId: string): Edge[] {
- return this.queries.getOutgoingEdges(nodeId);
- }
- /**
- * Get incoming edges to a node
- */
- getIncomingEdges(nodeId: string): Edge[] {
- return this.queries.getIncomingEdges(nodeId);
- }
- // ===========================================================================
- // File Operations
- // ===========================================================================
- /**
- * Get a file record by path
- */
- getFile(filePath: string): FileRecord | null {
- return this.queries.getFileByPath(filePath);
- }
- /**
- * Get all tracked files
- */
- getFiles(): FileRecord[] {
- return this.queries.getAllFiles();
- }
- // ===========================================================================
- // Graph Query Methods
- // ===========================================================================
- /**
- * Get the context for a node (ancestors, children, references)
- *
- * Returns comprehensive context about a node including its containment
- * hierarchy, children, incoming/outgoing references, type information,
- * and relevant imports.
- *
- * @param nodeId - ID of the focal node
- * @returns Context object with all related information
- */
- getContext(nodeId: string): Context {
- return this.graphManager.getContext(nodeId);
- }
- /**
- * Traverse the graph from a starting node
- *
- * Uses breadth-first search by default. Supports filtering by edge types,
- * node types, and traversal direction.
- *
- * @param startId - Starting node ID
- * @param options - Traversal options
- * @returns Subgraph containing traversed nodes and edges
- */
- traverse(startId: string, options?: TraversalOptions): Subgraph {
- return this.traverser.traverseBFS(startId, options);
- }
- /**
- * Get the call graph for a function
- *
- * Returns both callers (functions that call this function) and
- * callees (functions called by this function) up to the specified depth.
- *
- * @param nodeId - ID of the function/method node
- * @param depth - Maximum depth in each direction (default: 2)
- * @returns Subgraph containing the call graph
- */
- getCallGraph(nodeId: string, depth: number = 2): Subgraph {
- return this.traverser.getCallGraph(nodeId, depth);
- }
- /**
- * Get the type hierarchy for a class/interface
- *
- * Returns both ancestors (types this extends/implements) and
- * descendants (types that extend/implement this).
- *
- * @param nodeId - ID of the class/interface node
- * @returns Subgraph containing the type hierarchy
- */
- getTypeHierarchy(nodeId: string): Subgraph {
- return this.traverser.getTypeHierarchy(nodeId);
- }
- /**
- * Find all usages of a symbol
- *
- * Returns all nodes that reference the specified symbol through
- * any edge type (calls, references, type_of, etc.).
- *
- * @param nodeId - ID of the symbol node
- * @returns Array of nodes and edges that reference this symbol
- */
- findUsages(nodeId: string): Array<{ node: Node; edge: Edge }> {
- return this.traverser.findUsages(nodeId);
- }
- /**
- * Get callers of a function/method
- *
- * @param nodeId - ID of the function/method node
- * @param maxDepth - Maximum depth to traverse (default: 1)
- * @returns Array of nodes that call this function
- */
- getCallers(nodeId: string, maxDepth: number = 1): Array<{ node: Node; edge: Edge }> {
- return this.traverser.getCallers(nodeId, maxDepth);
- }
- /**
- * Get callees of a function/method
- *
- * @param nodeId - ID of the function/method node
- * @param maxDepth - Maximum depth to traverse (default: 1)
- * @returns Array of nodes called by this function
- */
- getCallees(nodeId: string, maxDepth: number = 1): Array<{ node: Node; edge: Edge }> {
- return this.traverser.getCallees(nodeId, maxDepth);
- }
- /**
- * Calculate the impact radius of a node
- *
- * Returns all nodes that could be affected by changes to this node.
- *
- * @param nodeId - ID of the node
- * @param maxDepth - Maximum depth to traverse (default: 3)
- * @returns Subgraph containing potentially impacted nodes
- */
- getImpactRadius(nodeId: string, maxDepth: number = 3): Subgraph {
- return this.traverser.getImpactRadius(nodeId, maxDepth);
- }
- /**
- * Find the shortest path between two nodes
- *
- * @param fromId - Starting node ID
- * @param toId - Target node ID
- * @param edgeKinds - Edge types to consider (all if empty)
- * @returns Array of nodes and edges forming the path, or null if no path exists
- */
- findPath(
- fromId: string,
- toId: string,
- edgeKinds?: Edge['kind'][]
- ): Array<{ node: Node; edge: Edge | null }> | null {
- return this.traverser.findPath(fromId, toId, edgeKinds);
- }
- /**
- * Get ancestors of a node in the containment hierarchy
- *
- * @param nodeId - ID of the node
- * @returns Array of ancestor nodes from immediate parent to root
- */
- getAncestors(nodeId: string): Node[] {
- return this.traverser.getAncestors(nodeId);
- }
- /**
- * Get immediate children of a node
- *
- * @param nodeId - ID of the node
- * @returns Array of child nodes
- */
- getChildren(nodeId: string): Node[] {
- return this.traverser.getChildren(nodeId);
- }
- /**
- * Get dependencies of a file
- *
- * @param filePath - Path to the file
- * @returns Array of file paths this file depends on
- */
- getFileDependencies(filePath: string): string[] {
- return this.graphManager.getFileDependencies(filePath);
- }
- /**
- * Get dependents of a file
- *
- * @param filePath - Path to the file
- * @returns Array of file paths that depend on this file
- */
- getFileDependents(filePath: string): string[] {
- return this.graphManager.getFileDependents(filePath);
- }
- /**
- * Find circular dependencies in the codebase
- *
- * @returns Array of cycles, each cycle is an array of file paths
- */
- findCircularDependencies(): string[][] {
- return this.graphManager.findCircularDependencies();
- }
- /**
- * Find dead code (unreferenced symbols)
- *
- * @param kinds - Node kinds to check (default: functions, methods, classes)
- * @returns Array of unreferenced nodes
- */
- findDeadCode(kinds?: Node['kind'][]): Node[] {
- return this.graphManager.findDeadCode(kinds);
- }
- /**
- * Get complexity metrics for a node
- *
- * @param nodeId - ID of the node
- * @returns Object containing various complexity metrics
- */
- getNodeMetrics(nodeId: string): {
- incomingEdgeCount: number;
- outgoingEdgeCount: number;
- callCount: number;
- callerCount: number;
- childCount: number;
- depth: number;
- } {
- return this.graphManager.getNodeMetrics(nodeId);
- }
- // ===========================================================================
- // Context Building
- // ===========================================================================
- /**
- * Get the source code for a node
- *
- * Reads the file and extracts the code between startLine and endLine.
- *
- * @param nodeId - ID of the node
- * @returns Code string or null if not found
- */
- async getCode(nodeId: string): Promise<string | null> {
- return this.contextBuilder.getCode(nodeId);
- }
- /**
- * Find relevant subgraph for a query
- *
- * Combines semantic search with graph traversal to find the most
- * relevant nodes and their relationships for a given query.
- *
- * @param query - Natural language query describing the task
- * @param options - Search and traversal options
- * @returns Subgraph of relevant nodes and edges
- */
- async findRelevantContext(
- query: string,
- options?: FindRelevantContextOptions
- ): Promise<Subgraph> {
- return this.contextBuilder.findRelevantContext(query, options);
- }
- /**
- * Build context for a task
- *
- * Creates comprehensive context by:
- * 1. Running FTS search to find entry points
- * 2. Expanding the graph around entry points
- * 3. Extracting code blocks for key nodes
- * 4. Formatting output for Claude
- *
- * @param input - Task description (string or {title, description})
- * @param options - Build options (maxNodes, includeCode, format, etc.)
- * @returns TaskContext object or formatted string (markdown/JSON)
- */
- async buildContext(
- input: TaskInput,
- options?: BuildContextOptions
- ): Promise<TaskContext | string> {
- return this.contextBuilder.buildContext(input, options);
- }
- // ===========================================================================
- // Database Management
- // ===========================================================================
- /**
- * Optimize the database (vacuum and analyze)
- */
- optimize(): void {
- this.db.optimize();
- }
- /**
- * Clear all data from the graph
- */
- clear(): void {
- this.queries.clear();
- }
- /**
- * Alias for close() for backwards compatibility.
- * @deprecated Use close() instead
- */
- destroy(): void {
- this.close();
- }
- /**
- * Completely remove CodeGraph from the project.
- * This closes the database and deletes the .CodeGraph directory.
- *
- * WARNING: This permanently deletes all CodeGraph data for the project.
- */
- uninitialize(): void {
- this.close();
- removeDirectory(this.projectRoot);
- }
- }
- // Default export
- export default CodeGraph;
|