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

Class CodeGraph

src/index.ts:193–3437  ·  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
210 // File watcher for auto-sync on file changes
211 private watcher: FileWatcher | null = null;
212
213 // When this instance last rebuilt the synthesized edges, and the timer that
214 // runs a synthesis a sync put off under CODEGRAPH_SYNTH_MIN_INTERVAL_MS
215 // (#2911). Per instance: the daemon's watcher is what syncs on every edit.
216 private lastSynthesisAt = 0;
217 private deferredSynthesisTimer: NodeJS.Timeout | null = null;
218
219 private constructor(
220 db: DatabaseConnection,
221 queries: QueryBuilder,
222 projectRoot: string
223 ) {
224 this.db = db;
225 this.queries = queries;
226 this.projectRoot = projectRoot;
227 this.fileLock = new FileLock(
228 path.join(getCodeGraphDir(projectRoot), 'codegraph.lock')
229 );
230 this.wireLayers();
231 }
232
233 /**
234 * (Re)build the query/extraction/graph layers over the current `this.queries`
235 * (which wraps `this.db`). Factored out of the constructor so `reopenIfReplaced`
236 * can rebuild them against a fresh connection without duplicating the wiring.
237 * The path-based `fileLock` is independent of the DB handle, so it stays put.
238 */
239 private wireLayers(): void {
240 // Down-weight the project name as a query term in search ranking — it names
241 // the whole repo, not a symbol, so it has no discriminative value (#720).
242 try {
243 this.queries.setProjectNameTokens(deriveProjectNameTokens(this.projectRoot));
244 } catch {
245 // Best-effort: ranking still works without it.
246 }
247 // Down-weight the peripheral trees the project named in `codegraph.json`
248 // `deprioritize` — indexed and findable, but never outranking real code
249 // (#982). Ranking-only, so a bad pattern costs relevance, never recall.
250 //

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected