MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / CodeGraph

Class CodeGraph

src/index.ts:192–3467  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

190}
191
192export class CodeGraph {
193 private db: DatabaseConnection;
194 private queries: QueryBuilder;
195 private projectRoot: string;
196 // Assigned via wireLayers() from the constructor (and again on reopen) — the
197 // `!` tells TS these are definitely set even though the assignment is one
198 // method call away from the constructor body.
199 private graphManager!: GraphQueryManager;
200 private traverser!: GraphTraverser;
201 private contextBuilder!: ContextBuilder;
202
203 // Mutex for preventing concurrent indexing operations (in-process)
204 private indexMutex = new Mutex();
205
206 // File lock for preventing concurrent writes across processes (CLI, MCP, git hooks)
207 private fileLock: FileLock;
208 // Only recreate() hands us an acquired lock, for the first indexAll() call.
209 private hasRebuildLock: boolean;
210
211 // File watcher for auto-sync on file changes
212 private watcher: FileWatcher | null = null;
213
214 // When this instance last rebuilt the synthesized edges, and the timer that
215 // runs a synthesis a sync put off under CODEGRAPH_SYNTH_MIN_INTERVAL_MS
216 // (#2911). Per instance: the daemon's watcher is what syncs on every edit.
217 private lastSynthesisAt = 0;
218 private deferredSynthesisTimer: NodeJS.Timeout | null = null;
219
220 private constructor(
221 db: DatabaseConnection,
222 queries: QueryBuilder,
223 projectRoot: string,
224 rebuildLock?: FileLock
225 ) {
226 this.db = db;
227 this.queries = queries;
228 this.projectRoot = projectRoot;
229 this.fileLock = rebuildLock ?? new FileLock(
230 path.join(getCodeGraphDir(projectRoot), 'codegraph.lock')
231 );
232 this.hasRebuildLock = rebuildLock !== undefined;
233 this.wireLayers();
234 }
235
236 /**
237 * (Re)build the query/extraction/graph layers over the current `this.queries`
238 * (which wraps `this.db`). Factored out of the constructor so `reopenIfReplaced`
239 * can rebuild them against a fresh connection without duplicating the wiring.
240 * The path-based `fileLock` is independent of the DB handle, so it stays put.
241 */
242 private wireLayers(): void {
243 // Down-weight the project name as a query term in search ranking — it names
244 // the whole repo, not a symbol, so it has no discriminative value (#720).
245 try {
246 this.queries.setProjectNameTokens(deriveProjectNameTokens(this.projectRoot));
247 } catch {
248 // Best-effort: ranking still works without it.
249 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected