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

Class CodeGraph

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected