index.ts 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /**
  2. * CodeGraph
  3. *
  4. * A local-first code intelligence system that builds a semantic
  5. * knowledge graph from any codebase.
  6. */
  7. import * as path from 'path';
  8. import {
  9. Node,
  10. Edge,
  11. FileRecord,
  12. ExtractionResult,
  13. Subgraph,
  14. TraversalOptions,
  15. SearchOptions,
  16. SearchResult,
  17. Context,
  18. GraphStats,
  19. TaskInput,
  20. TaskContext,
  21. BuildContextOptions,
  22. FindRelevantContextOptions,
  23. } from './types';
  24. import { DatabaseConnection, getDatabasePath } from './db';
  25. import { QueryBuilder } from './db/queries';
  26. import {
  27. isInitialized,
  28. createDirectory,
  29. removeDirectory,
  30. validateDirectory,
  31. } from './directory';
  32. import {
  33. ExtractionOrchestrator,
  34. IndexProgress,
  35. IndexResult,
  36. SyncResult,
  37. extractFromSource,
  38. initGrammars,
  39. } from './extraction';
  40. import {
  41. ReferenceResolver,
  42. createResolver,
  43. ResolutionResult,
  44. } from './resolution';
  45. import { GraphTraverser, GraphQueryManager } from './graph';
  46. import { ContextBuilder, createContextBuilder } from './context';
  47. import { Mutex, FileLock } from './utils';
  48. import { FileWatcher, WatchOptions, PendingFile, LockUnavailableError } from './sync';
  49. // Re-export types for consumers
  50. export * from './types';
  51. // Storage building blocks for embedded/SDK consumers that drive the graph
  52. // directly (open a DB, run prepared queries) rather than through the CodeGraph
  53. // facade. Exposed from the package entry so they no longer require deep imports
  54. // into dist/ (issue #354).
  55. export { getDatabasePath, DatabaseConnection } from './db';
  56. export { QueryBuilder } from './db/queries';
  57. export {
  58. getCodeGraphDir,
  59. isInitialized,
  60. findNearestCodeGraphRoot,
  61. CODEGRAPH_DIR,
  62. } from './directory';
  63. export { IndexProgress, IndexResult, SyncResult } from './extraction';
  64. export { detectLanguage, isLanguageSupported, isGrammarLoaded, getSupportedLanguages, initGrammars, loadGrammarsForLanguages, loadAllGrammars } from './extraction';
  65. export { ResolutionResult } from './resolution';
  66. export {
  67. CodeGraphError,
  68. FileError,
  69. ParseError,
  70. DatabaseError,
  71. SearchError,
  72. VectorError,
  73. ConfigError,
  74. Logger,
  75. setLogger,
  76. getLogger,
  77. silentLogger,
  78. defaultLogger,
  79. } from './errors';
  80. export { Mutex, FileLock, processInBatches, debounce, throttle, MemoryMonitor } from './utils';
  81. export { FileWatcher, WatchOptions, PendingFile, LockUnavailableError } from './sync';
  82. export { MCPServer } from './mcp';
  83. /**
  84. * Options for initializing a new CodeGraph project
  85. */
  86. export interface InitOptions {
  87. /** Whether to run initial indexing after init */
  88. index?: boolean;
  89. /** Progress callback for indexing */
  90. onProgress?: (progress: IndexProgress) => void;
  91. }
  92. /**
  93. * Options for opening an existing CodeGraph project
  94. */
  95. export interface OpenOptions {
  96. /** Whether to run sync if files have changed */
  97. sync?: boolean;
  98. /** Whether to run in read-only mode */
  99. readOnly?: boolean;
  100. }
  101. /**
  102. * Options for indexing
  103. */
  104. export interface IndexOptions {
  105. /** Progress callback */
  106. onProgress?: (progress: IndexProgress) => void;
  107. /** Abort signal for cancellation */
  108. signal?: AbortSignal;
  109. /** Enable verbose logging (worker lifecycle, memory, timeouts) */
  110. verbose?: boolean;
  111. }
  112. /**
  113. * Main CodeGraph class
  114. *
  115. * Provides the primary interface for interacting with the code knowledge graph.
  116. */
  117. export class CodeGraph {
  118. private db: DatabaseConnection;
  119. private queries: QueryBuilder;
  120. private projectRoot: string;
  121. private orchestrator: ExtractionOrchestrator;
  122. private resolver: ReferenceResolver;
  123. private graphManager: GraphQueryManager;
  124. private traverser: GraphTraverser;
  125. private contextBuilder: ContextBuilder;
  126. // Mutex for preventing concurrent indexing operations (in-process)
  127. private indexMutex = new Mutex();
  128. // File lock for preventing concurrent writes across processes (CLI, MCP, git hooks)
  129. private fileLock: FileLock;
  130. // File watcher for auto-sync on file changes
  131. private watcher: FileWatcher | null = null;
  132. private constructor(
  133. db: DatabaseConnection,
  134. queries: QueryBuilder,
  135. projectRoot: string
  136. ) {
  137. this.db = db;
  138. this.queries = queries;
  139. this.projectRoot = projectRoot;
  140. this.fileLock = new FileLock(
  141. path.join(projectRoot, '.codegraph', 'codegraph.lock')
  142. );
  143. this.orchestrator = new ExtractionOrchestrator(projectRoot, queries);
  144. this.resolver = createResolver(projectRoot, queries);
  145. this.graphManager = new GraphQueryManager(queries);
  146. this.traverser = new GraphTraverser(queries);
  147. this.contextBuilder = createContextBuilder(
  148. projectRoot,
  149. queries,
  150. this.traverser
  151. );
  152. }
  153. // ===========================================================================
  154. // Lifecycle Methods
  155. // ===========================================================================
  156. /**
  157. * Initialize a new CodeGraph project
  158. *
  159. * Creates the .CodeGraph directory, database, and configuration.
  160. *
  161. * @param projectRoot - Path to the project root directory
  162. * @param options - Initialization options
  163. * @returns A new CodeGraph instance
  164. */
  165. static async init(projectRoot: string, options: InitOptions = {}): Promise<CodeGraph> {
  166. await initGrammars();
  167. const resolvedRoot = path.resolve(projectRoot);
  168. // Check if already initialized
  169. if (isInitialized(resolvedRoot)) {
  170. throw new Error(`CodeGraph already initialized in ${resolvedRoot}`);
  171. }
  172. // Create directory structure
  173. createDirectory(resolvedRoot);
  174. // Initialize database
  175. const dbPath = getDatabasePath(resolvedRoot);
  176. const db = DatabaseConnection.initialize(dbPath);
  177. const queries = new QueryBuilder(db.getDb());
  178. const instance = new CodeGraph(db, queries, resolvedRoot);
  179. // Run initial indexing if requested
  180. if (options.index) {
  181. await instance.indexAll({ onProgress: options.onProgress });
  182. }
  183. return instance;
  184. }
  185. /**
  186. * Initialize synchronously (without indexing)
  187. */
  188. static initSync(projectRoot: string): CodeGraph {
  189. const resolvedRoot = path.resolve(projectRoot);
  190. // Check if already initialized
  191. if (isInitialized(resolvedRoot)) {
  192. throw new Error(`CodeGraph already initialized in ${resolvedRoot}`);
  193. }
  194. // Create directory structure
  195. createDirectory(resolvedRoot);
  196. // Initialize database
  197. const dbPath = getDatabasePath(resolvedRoot);
  198. const db = DatabaseConnection.initialize(dbPath);
  199. const queries = new QueryBuilder(db.getDb());
  200. return new CodeGraph(db, queries, resolvedRoot);
  201. }
  202. /**
  203. * Open an existing CodeGraph project
  204. *
  205. * @param projectRoot - Path to the project root directory
  206. * @param options - Open options
  207. * @returns A CodeGraph instance
  208. */
  209. static async open(projectRoot: string, options: OpenOptions = {}): Promise<CodeGraph> {
  210. await initGrammars();
  211. const resolvedRoot = path.resolve(projectRoot);
  212. // Check if initialized
  213. if (!isInitialized(resolvedRoot)) {
  214. throw new Error(`CodeGraph not initialized in ${resolvedRoot}. Run init() first.`);
  215. }
  216. // Validate directory structure
  217. const validation = validateDirectory(resolvedRoot);
  218. if (!validation.valid) {
  219. throw new Error(`Invalid CodeGraph directory: ${validation.errors.join(', ')}`);
  220. }
  221. // Open database
  222. const dbPath = getDatabasePath(resolvedRoot);
  223. const db = DatabaseConnection.open(dbPath);
  224. const queries = new QueryBuilder(db.getDb());
  225. const instance = new CodeGraph(db, queries, resolvedRoot);
  226. // Sync if requested
  227. if (options.sync) {
  228. await instance.sync();
  229. }
  230. return instance;
  231. }
  232. /**
  233. * Open synchronously (without sync)
  234. */
  235. static openSync(projectRoot: string): CodeGraph {
  236. const resolvedRoot = path.resolve(projectRoot);
  237. // Check if initialized
  238. if (!isInitialized(resolvedRoot)) {
  239. throw new Error(`CodeGraph not initialized in ${resolvedRoot}. Run init() first.`);
  240. }
  241. // Validate directory structure
  242. const validation = validateDirectory(resolvedRoot);
  243. if (!validation.valid) {
  244. throw new Error(`Invalid CodeGraph directory: ${validation.errors.join(', ')}`);
  245. }
  246. // Open database
  247. const dbPath = getDatabasePath(resolvedRoot);
  248. const db = DatabaseConnection.open(dbPath);
  249. const queries = new QueryBuilder(db.getDb());
  250. return new CodeGraph(db, queries, resolvedRoot);
  251. }
  252. /**
  253. * Check if a directory has been initialized as a CodeGraph project
  254. */
  255. static isInitialized(projectRoot: string): boolean {
  256. return isInitialized(path.resolve(projectRoot));
  257. }
  258. /**
  259. * Close the CodeGraph instance and release resources
  260. */
  261. close(): void {
  262. this.unwatch();
  263. // Release file lock if held
  264. this.fileLock.release();
  265. this.db.close();
  266. }
  267. /**
  268. * Get the project root directory
  269. */
  270. getProjectRoot(): string {
  271. return this.projectRoot;
  272. }
  273. // ===========================================================================
  274. // Indexing
  275. // ===========================================================================
  276. /**
  277. * Index all files in the project
  278. *
  279. * Uses a mutex to prevent concurrent indexing operations.
  280. */
  281. async indexAll(options: IndexOptions = {}): Promise<IndexResult> {
  282. return this.indexMutex.withLock(async () => {
  283. try {
  284. this.fileLock.acquire();
  285. } catch {
  286. 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 };
  287. }
  288. try {
  289. const before = this.queries.getNodeAndEdgeCount();
  290. const result = await this.orchestrator.indexAll(options.onProgress, options.signal, options.verbose);
  291. // Re-detect frameworks now that the index is populated. The resolver
  292. // is constructed with createResolver() before any files exist, so
  293. // framework resolvers whose detect() consults the indexed file list
  294. // (e.g. UIKit/SwiftUI scanning for imports, swift-objc-bridge looking
  295. // for both Swift and ObjC files) all return false on that initial pass
  296. // and silently drop themselves. Re-initializing here gives them a
  297. // chance to see the actual project before resolution runs.
  298. if (result.success && result.filesIndexed > 0) {
  299. this.resolver.initialize();
  300. // Cross-file finalization (e.g. NestJS RouterModule prefixes). Runs
  301. // before resolution so updated names show up in subsequent reads.
  302. this.resolver.runPostExtract();
  303. }
  304. // Resolve references to create call/import/extends edges
  305. if (result.success && result.filesIndexed > 0) {
  306. // Get count without loading all refs into memory
  307. const unresolvedCount = this.queries.getUnresolvedReferencesCount();
  308. options.onProgress?.({
  309. phase: 'resolving',
  310. current: 0,
  311. total: unresolvedCount,
  312. });
  313. await this.resolveReferencesBatched((current, total) => {
  314. options.onProgress?.({
  315. phase: 'resolving',
  316. current,
  317. total,
  318. });
  319. });
  320. }
  321. // Refresh planner stats + checkpoint the WAL after bulk writes.
  322. // Cheap and non-blocking; never load-bearing for correctness.
  323. if (result.success && result.filesIndexed > 0) {
  324. this.db.runMaintenance();
  325. }
  326. // The orchestrator only sees extraction-phase counts; resolution and
  327. // synthesizer edges (often >50% of the graph on JVM repos) come later.
  328. // Recompute against the DB so the CLI summary reports the true totals.
  329. if (result.success && result.filesIndexed > 0) {
  330. const after = this.queries.getNodeAndEdgeCount();
  331. result.nodesCreated = after.nodes - before.nodes;
  332. result.edgesCreated = after.edges - before.edges;
  333. }
  334. return result;
  335. } finally {
  336. this.fileLock.release();
  337. }
  338. });
  339. }
  340. /**
  341. * Index specific files
  342. *
  343. * Uses a mutex to prevent concurrent indexing operations.
  344. */
  345. async indexFiles(filePaths: string[]): Promise<IndexResult> {
  346. return this.indexMutex.withLock(async () => {
  347. try {
  348. this.fileLock.acquire();
  349. } catch {
  350. 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 };
  351. }
  352. try {
  353. return this.orchestrator.indexFiles(filePaths);
  354. } finally {
  355. this.fileLock.release();
  356. }
  357. });
  358. }
  359. /**
  360. * Sync with current file state (incremental update)
  361. *
  362. * Uses a mutex to prevent concurrent indexing operations.
  363. */
  364. async sync(options: IndexOptions = {}): Promise<SyncResult> {
  365. return this.indexMutex.withLock(async () => {
  366. try {
  367. this.fileLock.acquire();
  368. } catch {
  369. return { filesChecked: 0, filesAdded: 0, filesModified: 0, filesRemoved: 0, nodesUpdated: 0, durationMs: 0 };
  370. }
  371. try {
  372. const result = await this.orchestrator.sync(options.onProgress);
  373. // Cross-file finalization (e.g. NestJS RouterModule prefixes). Run on
  374. // every sync that touched files so edits to `app.module.ts` propagate
  375. // to controllers in unchanged files. The pass is idempotent and cheap
  376. // (regex over *.module.ts only).
  377. if (result.filesAdded > 0 || result.filesModified > 0) {
  378. this.resolver.runPostExtract();
  379. }
  380. // Resolve references if files were updated
  381. if (result.filesAdded > 0 || result.filesModified > 0) {
  382. if (result.changedFilePaths) {
  383. // Scope resolution to changed files (git fast path — bounded set)
  384. const unresolvedRefs = this.queries.getUnresolvedReferencesByFiles(result.changedFilePaths);
  385. options.onProgress?.({
  386. phase: 'resolving',
  387. current: 0,
  388. total: unresolvedRefs.length,
  389. });
  390. this.resolver.resolveAndPersist(unresolvedRefs, (current, total) => {
  391. options.onProgress?.({
  392. phase: 'resolving',
  393. current,
  394. total,
  395. });
  396. });
  397. } else {
  398. // No git info — use batched resolution to avoid OOM
  399. const unresolvedCount = this.queries.getUnresolvedReferencesCount();
  400. options.onProgress?.({
  401. phase: 'resolving',
  402. current: 0,
  403. total: unresolvedCount,
  404. });
  405. await this.resolveReferencesBatched((current, total) => {
  406. options.onProgress?.({
  407. phase: 'resolving',
  408. current,
  409. total,
  410. });
  411. });
  412. }
  413. }
  414. // Refresh planner stats + checkpoint the WAL after bulk writes.
  415. if (result.filesAdded > 0 || result.filesModified > 0 || result.filesRemoved > 0) {
  416. this.db.runMaintenance();
  417. }
  418. return result;
  419. } finally {
  420. this.fileLock.release();
  421. }
  422. });
  423. }
  424. /**
  425. * Check if an indexing operation is currently in progress
  426. */
  427. isIndexing(): boolean {
  428. return this.indexMutex.isLocked();
  429. }
  430. // ===========================================================================
  431. // File Watching
  432. // ===========================================================================
  433. /**
  434. * Start watching for file changes and auto-syncing.
  435. *
  436. * Uses native OS file events (FSEvents on macOS, inotify on Linux 19+,
  437. * ReadDirectoryChangesW on Windows) with debouncing to avoid thrashing.
  438. *
  439. * @param options - Watch options (debounce delay, callbacks)
  440. * @returns true if watching started successfully
  441. */
  442. watch(options: WatchOptions = {}): boolean {
  443. if (this.watcher?.isActive()) return true;
  444. this.watcher = new FileWatcher(
  445. this.projectRoot,
  446. async () => {
  447. const result = await this.sync();
  448. // sync() returns this exact zero-shape iff it failed to acquire the
  449. // file lock (a real empty sync always has filesChecked > 0 because
  450. // scanDirectory ran). Surface that to the watcher as a typed error
  451. // so it keeps pendingFiles + reschedules instead of clearing them
  452. // (#449).
  453. if (result.filesChecked === 0 && result.durationMs === 0) {
  454. throw new LockUnavailableError();
  455. }
  456. const filesChanged = result.filesAdded + result.filesModified + result.filesRemoved;
  457. return { filesChanged, durationMs: result.durationMs };
  458. },
  459. options
  460. );
  461. return this.watcher.start();
  462. }
  463. /**
  464. * Stop watching for file changes.
  465. */
  466. unwatch(): void {
  467. if (this.watcher) {
  468. this.watcher.stop();
  469. this.watcher = null;
  470. }
  471. }
  472. /**
  473. * Check if the file watcher is active.
  474. */
  475. isWatching(): boolean {
  476. return this.watcher?.isActive() ?? false;
  477. }
  478. /**
  479. * Files seen by the file watcher since the last successful sync —
  480. * the per-file "stale" signal MCP tools attach to responses so an agent
  481. * can fall back to {@link Read} for just the affected file without
  482. * waiting for a debounced sync to complete (issue #403).
  483. *
  484. * Returns an empty list when the watcher isn't active, or no events have
  485. * arrived. Each entry includes `firstSeenMs` and `lastSeenMs` (wall-clock
  486. * `Date.now()` values) so callers can render "edited Nms ago", plus an
  487. * `indexing` flag indicating whether the in-flight sync (if any) will
  488. * absorb that file.
  489. */
  490. getPendingFiles(): PendingFile[] {
  491. return this.watcher?.getPendingFiles() ?? [];
  492. }
  493. /**
  494. * Resolves once the file watcher has finished its initial chokidar scan.
  495. * Useful for tests that need a deterministic boundary before asserting on
  496. * `getPendingFiles()`. Resolves immediately when no watcher is active.
  497. */
  498. waitUntilWatcherReady(timeoutMs?: number): Promise<void> {
  499. return this.watcher ? this.watcher.waitUntilReady(timeoutMs) : Promise.resolve();
  500. }
  501. /**
  502. * Get files that have changed since last index
  503. */
  504. getChangedFiles(): { added: string[]; modified: string[]; removed: string[] } {
  505. return this.orchestrator.getChangedFiles();
  506. }
  507. /**
  508. * Extract nodes and edges from source code (without storing)
  509. */
  510. extractFromSource(filePath: string, source: string): ExtractionResult {
  511. return extractFromSource(filePath, source);
  512. }
  513. // ===========================================================================
  514. // Reference Resolution
  515. // ===========================================================================
  516. /**
  517. * Resolve unresolved references and create edges
  518. *
  519. * This method takes unresolved references from extraction and attempts
  520. * to resolve them using multiple strategies:
  521. * - Framework-specific patterns (React, Express, Laravel)
  522. * - Import-based resolution
  523. * - Name-based symbol matching
  524. */
  525. resolveReferences(onProgress?: (current: number, total: number) => void): ResolutionResult {
  526. // Get all unresolved references from the database
  527. const unresolvedRefs = this.queries.getUnresolvedReferences();
  528. return this.resolver.resolveAndPersist(unresolvedRefs, onProgress);
  529. }
  530. /**
  531. * Resolve references in batches to keep memory bounded on large codebases.
  532. * Processes chunks of unresolved refs, persisting results after each batch.
  533. */
  534. async resolveReferencesBatched(onProgress?: (current: number, total: number) => void): Promise<ResolutionResult> {
  535. return this.resolver.resolveAndPersistBatched(onProgress);
  536. }
  537. /**
  538. * Get detected frameworks in the project
  539. */
  540. getDetectedFrameworks(): string[] {
  541. return this.resolver.getDetectedFrameworks();
  542. }
  543. /**
  544. * Re-initialize the resolver (useful after adding new files)
  545. */
  546. reinitializeResolver(): void {
  547. this.resolver.initialize();
  548. }
  549. // ===========================================================================
  550. // Graph Statistics
  551. // ===========================================================================
  552. /**
  553. * Get statistics about the knowledge graph
  554. */
  555. getStats(): GraphStats {
  556. const stats = this.queries.getStats();
  557. stats.dbSizeBytes = this.db.getSize();
  558. return stats;
  559. }
  560. /**
  561. * Active SQLite backend for this project's connection (`node-sqlite` — Node's
  562. * built-in real-SQLite module). Surfaced via `codegraph status` and the
  563. * `codegraph_status` MCP tool alongside the effective journal mode.
  564. */
  565. getBackend(): import('./db').SqliteBackend {
  566. return this.db.getBackend();
  567. }
  568. /**
  569. * The journal mode actually in effect ('wal', 'delete', …). 'wal' means
  570. * readers never block on a concurrent writer; anything else means they can,
  571. * which is the precondition for the "database is locked" failures in issue
  572. * #238. Surfaced via `codegraph status` and the `codegraph_status` MCP tool.
  573. */
  574. getJournalMode(): string {
  575. return this.db.getJournalMode();
  576. }
  577. // ===========================================================================
  578. // Node Operations
  579. // ===========================================================================
  580. /**
  581. * Get a node by ID
  582. */
  583. getNode(id: string): Node | null {
  584. return this.queries.getNodeById(id);
  585. }
  586. /**
  587. * Get all nodes in a file
  588. */
  589. getNodesInFile(filePath: string): Node[] {
  590. return this.queries.getNodesByFile(filePath);
  591. }
  592. /**
  593. * Get all nodes of a specific kind
  594. */
  595. getNodesByKind(kind: Node['kind']): Node[] {
  596. return this.queries.getNodesByKind(kind);
  597. }
  598. /**
  599. * Search nodes by text
  600. */
  601. searchNodes(query: string, options?: SearchOptions): SearchResult[] {
  602. return this.queries.searchNodes(query, options);
  603. }
  604. /**
  605. * Find the project's "primary route file" — the file with the densest
  606. * concentration of framework-emitted `route` nodes (≥3 routes, ≥30%
  607. * of all non-test routes). Used to inline the routing config in
  608. * `codegraph_explore` responses on small realworld template repos
  609. * (rails-realworld, laravel-realworld, drupal-admintoolbar, …) where
  610. * Glob+Read of `routes.rb`/`urls.py`/etc. otherwise beats codegraph.
  611. */
  612. getTopRouteFile(): { filePath: string; routeCount: number; totalRoutes: number } | null {
  613. return this.queries.getTopRouteFile();
  614. }
  615. /**
  616. * Build a URL → handler routing manifest from the index. Each entry
  617. * pairs a route node (URL + method) with its handler function/method
  618. * via the `references` edge that framework resolvers emit. Returns
  619. * null when fewer than 3 valid (non-test) routes exist.
  620. */
  621. getRoutingManifest(limit?: number): {
  622. entries: Array<{ url: string; handler: string; handlerFile: string; handlerLine: number; handlerKind: string }>;
  623. topHandlerFile: string | null;
  624. topHandlerFileCount: number;
  625. totalRoutes: number;
  626. } | null {
  627. return this.queries.getRoutingManifest(limit);
  628. }
  629. // ===========================================================================
  630. // Edge Operations
  631. // ===========================================================================
  632. /**
  633. * Get outgoing edges from a node
  634. */
  635. getOutgoingEdges(nodeId: string): Edge[] {
  636. return this.queries.getOutgoingEdges(nodeId);
  637. }
  638. /**
  639. * Get incoming edges to a node
  640. */
  641. getIncomingEdges(nodeId: string): Edge[] {
  642. return this.queries.getIncomingEdges(nodeId);
  643. }
  644. // ===========================================================================
  645. // File Operations
  646. // ===========================================================================
  647. /**
  648. * Get a file record by path
  649. */
  650. getFile(filePath: string): FileRecord | null {
  651. return this.queries.getFileByPath(filePath);
  652. }
  653. /**
  654. * Get all tracked files
  655. */
  656. getFiles(): FileRecord[] {
  657. return this.queries.getAllFiles();
  658. }
  659. // ===========================================================================
  660. // Graph Query Methods
  661. // ===========================================================================
  662. /**
  663. * Get the context for a node (ancestors, children, references)
  664. *
  665. * Returns comprehensive context about a node including its containment
  666. * hierarchy, children, incoming/outgoing references, type information,
  667. * and relevant imports.
  668. *
  669. * @param nodeId - ID of the focal node
  670. * @returns Context object with all related information
  671. */
  672. getContext(nodeId: string): Context {
  673. return this.graphManager.getContext(nodeId);
  674. }
  675. /**
  676. * Traverse the graph from a starting node
  677. *
  678. * Uses breadth-first search by default. Supports filtering by edge types,
  679. * node types, and traversal direction.
  680. *
  681. * @param startId - Starting node ID
  682. * @param options - Traversal options
  683. * @returns Subgraph containing traversed nodes and edges
  684. */
  685. traverse(startId: string, options?: TraversalOptions): Subgraph {
  686. return this.traverser.traverseBFS(startId, options);
  687. }
  688. /**
  689. * Get the call graph for a function
  690. *
  691. * Returns both callers (functions that call this function) and
  692. * callees (functions called by this function) up to the specified depth.
  693. *
  694. * @param nodeId - ID of the function/method node
  695. * @param depth - Maximum depth in each direction (default: 2)
  696. * @returns Subgraph containing the call graph
  697. */
  698. getCallGraph(nodeId: string, depth: number = 2): Subgraph {
  699. return this.traverser.getCallGraph(nodeId, depth);
  700. }
  701. /**
  702. * Get the type hierarchy for a class/interface
  703. *
  704. * Returns both ancestors (types this extends/implements) and
  705. * descendants (types that extend/implement this).
  706. *
  707. * @param nodeId - ID of the class/interface node
  708. * @returns Subgraph containing the type hierarchy
  709. */
  710. getTypeHierarchy(nodeId: string): Subgraph {
  711. return this.traverser.getTypeHierarchy(nodeId);
  712. }
  713. /**
  714. * Find all usages of a symbol
  715. *
  716. * Returns all nodes that reference the specified symbol through
  717. * any edge type (calls, references, type_of, etc.).
  718. *
  719. * @param nodeId - ID of the symbol node
  720. * @returns Array of nodes and edges that reference this symbol
  721. */
  722. findUsages(nodeId: string): Array<{ node: Node; edge: Edge }> {
  723. return this.traverser.findUsages(nodeId);
  724. }
  725. /**
  726. * Get callers of a function/method
  727. *
  728. * @param nodeId - ID of the function/method node
  729. * @param maxDepth - Maximum depth to traverse (default: 1)
  730. * @returns Array of nodes that call this function
  731. */
  732. getCallers(nodeId: string, maxDepth: number = 1): Array<{ node: Node; edge: Edge }> {
  733. return this.traverser.getCallers(nodeId, maxDepth);
  734. }
  735. /**
  736. * Get callees of a function/method
  737. *
  738. * @param nodeId - ID of the function/method node
  739. * @param maxDepth - Maximum depth to traverse (default: 1)
  740. * @returns Array of nodes called by this function
  741. */
  742. getCallees(nodeId: string, maxDepth: number = 1): Array<{ node: Node; edge: Edge }> {
  743. return this.traverser.getCallees(nodeId, maxDepth);
  744. }
  745. /**
  746. * Calculate the impact radius of a node
  747. *
  748. * Returns all nodes that could be affected by changes to this node.
  749. *
  750. * @param nodeId - ID of the node
  751. * @param maxDepth - Maximum depth to traverse (default: 3)
  752. * @returns Subgraph containing potentially impacted nodes
  753. */
  754. getImpactRadius(nodeId: string, maxDepth: number = 3): Subgraph {
  755. return this.traverser.getImpactRadius(nodeId, maxDepth);
  756. }
  757. /**
  758. * Find the shortest path between two nodes
  759. *
  760. * @param fromId - Starting node ID
  761. * @param toId - Target node ID
  762. * @param edgeKinds - Edge types to consider (all if empty)
  763. * @returns Array of nodes and edges forming the path, or null if no path exists
  764. */
  765. findPath(
  766. fromId: string,
  767. toId: string,
  768. edgeKinds?: Edge['kind'][]
  769. ): Array<{ node: Node; edge: Edge | null }> | null {
  770. return this.traverser.findPath(fromId, toId, edgeKinds);
  771. }
  772. /**
  773. * Get ancestors of a node in the containment hierarchy
  774. *
  775. * @param nodeId - ID of the node
  776. * @returns Array of ancestor nodes from immediate parent to root
  777. */
  778. getAncestors(nodeId: string): Node[] {
  779. return this.traverser.getAncestors(nodeId);
  780. }
  781. /**
  782. * Get immediate children of a node
  783. *
  784. * @param nodeId - ID of the node
  785. * @returns Array of child nodes
  786. */
  787. getChildren(nodeId: string): Node[] {
  788. return this.traverser.getChildren(nodeId);
  789. }
  790. /**
  791. * Get dependencies of a file
  792. *
  793. * @param filePath - Path to the file
  794. * @returns Array of file paths this file depends on
  795. */
  796. getFileDependencies(filePath: string): string[] {
  797. return this.graphManager.getFileDependencies(filePath);
  798. }
  799. /**
  800. * Get dependents of a file
  801. *
  802. * @param filePath - Path to the file
  803. * @returns Array of file paths that depend on this file
  804. */
  805. getFileDependents(filePath: string): string[] {
  806. return this.graphManager.getFileDependents(filePath);
  807. }
  808. /**
  809. * Find circular dependencies in the codebase
  810. *
  811. * @returns Array of cycles, each cycle is an array of file paths
  812. */
  813. findCircularDependencies(): string[][] {
  814. return this.graphManager.findCircularDependencies();
  815. }
  816. /**
  817. * Find dead code (unreferenced symbols)
  818. *
  819. * @param kinds - Node kinds to check (default: functions, methods, classes)
  820. * @returns Array of unreferenced nodes
  821. */
  822. findDeadCode(kinds?: Node['kind'][]): Node[] {
  823. return this.graphManager.findDeadCode(kinds);
  824. }
  825. /**
  826. * Get complexity metrics for a node
  827. *
  828. * @param nodeId - ID of the node
  829. * @returns Object containing various complexity metrics
  830. */
  831. getNodeMetrics(nodeId: string): {
  832. incomingEdgeCount: number;
  833. outgoingEdgeCount: number;
  834. callCount: number;
  835. callerCount: number;
  836. childCount: number;
  837. depth: number;
  838. } {
  839. return this.graphManager.getNodeMetrics(nodeId);
  840. }
  841. // ===========================================================================
  842. // Context Building
  843. // ===========================================================================
  844. /**
  845. * Get the source code for a node
  846. *
  847. * Reads the file and extracts the code between startLine and endLine.
  848. *
  849. * @param nodeId - ID of the node
  850. * @returns Code string or null if not found
  851. */
  852. async getCode(nodeId: string): Promise<string | null> {
  853. return this.contextBuilder.getCode(nodeId);
  854. }
  855. /**
  856. * Find relevant subgraph for a query
  857. *
  858. * Combines semantic search with graph traversal to find the most
  859. * relevant nodes and their relationships for a given query.
  860. *
  861. * @param query - Natural language query describing the task
  862. * @param options - Search and traversal options
  863. * @returns Subgraph of relevant nodes and edges
  864. */
  865. async findRelevantContext(
  866. query: string,
  867. options?: FindRelevantContextOptions
  868. ): Promise<Subgraph> {
  869. return this.contextBuilder.findRelevantContext(query, options);
  870. }
  871. /**
  872. * Build context for a task
  873. *
  874. * Creates comprehensive context by:
  875. * 1. Running FTS search to find entry points
  876. * 2. Expanding the graph around entry points
  877. * 3. Extracting code blocks for key nodes
  878. * 4. Formatting output for Claude
  879. *
  880. * @param input - Task description (string or {title, description})
  881. * @param options - Build options (maxNodes, includeCode, format, etc.)
  882. * @returns TaskContext object or formatted string (markdown/JSON)
  883. */
  884. async buildContext(
  885. input: TaskInput,
  886. options?: BuildContextOptions
  887. ): Promise<TaskContext | string> {
  888. return this.contextBuilder.buildContext(input, options);
  889. }
  890. // ===========================================================================
  891. // Database Management
  892. // ===========================================================================
  893. /**
  894. * Optimize the database (vacuum and analyze)
  895. */
  896. optimize(): void {
  897. this.db.optimize();
  898. }
  899. /**
  900. * Clear all data from the graph
  901. */
  902. clear(): void {
  903. this.queries.clear();
  904. }
  905. /**
  906. * Alias for close() for backwards compatibility.
  907. * @deprecated Use close() instead
  908. */
  909. destroy(): void {
  910. this.close();
  911. }
  912. /**
  913. * Completely remove CodeGraph from the project.
  914. * This closes the database and deletes the .CodeGraph directory.
  915. *
  916. * WARNING: This permanently deletes all CodeGraph data for the project.
  917. */
  918. uninitialize(): void {
  919. this.close();
  920. removeDirectory(this.projectRoot);
  921. }
  922. }
  923. // Default export
  924. export default CodeGraph;