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

Class CodeGraph

src/index.ts:193–3477  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected