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

Class CodeGraph

src/index.ts:191–3364  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected