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

Class CodeGraph

src/index.ts:144–2374  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

142 * Provides the primary interface for interacting with the code knowledge graph.
143 */
144export class CodeGraph {
145 private db: DatabaseConnection;
146 private queries: QueryBuilder;
147 private projectRoot: string;
148 // Assigned via wireLayers() from the constructor (and again on reopen) — the
149 // `!` tells TS these are definitely set even though the assignment is one
150 // method call away from the constructor body.
151 private orchestrator!: ExtractionOrchestrator;
152 private resolver!: ReferenceResolver;
153 private graphManager!: GraphQueryManager;
154 private traverser!: GraphTraverser;
155 private contextBuilder!: ContextBuilder;
156
157 // Mutex for preventing concurrent indexing operations (in-process)
158 private indexMutex = new Mutex();
159
160 // File lock for preventing concurrent writes across processes (CLI, MCP, git hooks)
161 private fileLock: FileLock;
162
163 // File watcher for auto-sync on file changes
164 private watcher: FileWatcher | null = null;
165
166 private constructor(
167 db: DatabaseConnection,
168 queries: QueryBuilder,
169 projectRoot: string
170 ) {
171 this.db = db;
172 this.queries = queries;
173 this.projectRoot = projectRoot;
174 this.fileLock = new FileLock(
175 path.join(getCodeGraphDir(projectRoot), 'codegraph.lock')
176 );
177 this.wireLayers();
178 }
179
180 /**
181 * (Re)build the query/extraction/graph layers over the current `this.queries`
182 * (which wraps `this.db`). Factored out of the constructor so `reopenIfReplaced`
183 * can rebuild them against a fresh connection without duplicating the wiring.
184 * The path-based `fileLock` is independent of the DB handle, so it stays put.
185 */
186 private wireLayers(): void {
187 // Down-weight the project name as a query term in search ranking — it names
188 // the whole repo, not a symbol, so it has no discriminative value (#720).
189 try {
190 this.queries.setProjectNameTokens(deriveProjectNameTokens(this.projectRoot));
191 } catch {
192 // Best-effort: ranking still works without it.
193 }
194 // Down-weight the peripheral trees the project named in `codegraph.json`
195 // `deprioritize` — indexed and findable, but never outranking real code
196 // (#982). Ranking-only, so a bad pattern costs relevance, never recall.
197 //
198 // Read LAZILY, not once here: `wireLayers` runs from the constructor and
199 // from `reopenIfReplaced`, so a matcher built here would freeze at whatever
200 // the config said when the project opened. The MCP server caches one
201 // CodeGraph per root for its whole lifetime, so editing `codegraph.json`

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected