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

Method wireLayers

src/index.ts:243–320  ·  view source on GitHub ↗

* (Re)build the query/extraction/graph layers over the current `this.queries` * (which wraps `this.db`). Factored out of the constructor so `reopenIfReplaced` * can rebuild them against a fresh connection without duplicating the wiring. * The path-based `fileLock` is independent of the DB h

()

Source from the content-addressed store, hash-verified

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 }
251 // Down-weight the peripheral trees the project named in `codegraph.json`
252 // `deprioritize` — indexed and findable, but never outranking real code
253 // (#982). Ranking-only, so a bad pattern costs relevance, never recall.
254 //
255 // Read LAZILY, not once here: `wireLayers` runs from the constructor and
256 // from `reopenIfReplaced`, so a matcher built here would freeze at whatever
257 // the config said when the project opened. The MCP server caches one
258 // CodeGraph per root for its whole lifetime, so editing `codegraph.json`
259 // would appear to do nothing until the process restarted — `exclude` and
260 // `include` do not behave that way. `loadDeprioritizePatterns` is
261 // mtime-cached, so this costs one `stat`; the compiled matcher is memoized
262 // on the pattern array's identity, which the cache keeps stable.
263 let cachedPatterns: string[] | undefined;
264 let cachedMatcher: ReturnType<typeof ignore> | undefined;
265 // The compiled matcher for the patterns `codegraph.json` holds right now,
266 // or null when there are none.
267 const currentMatcher = (): ReturnType<typeof ignore> | null => {
268 const patterns = loadDeprioritizePatterns(this.projectRoot);
269 if (patterns.length === 0) return null;
270 if (patterns !== cachedPatterns) {
271 cachedPatterns = patterns;
272 cachedMatcher = ignore().add(patterns);
273 }
274 return cachedMatcher!;
275 };
276 const matches = (matcher: ReturnType<typeof ignore> | null, filePath: string): boolean => {
277 if (!matcher) return false;
278 const rel = path.isAbsolute(filePath)
279 ? path.relative(this.projectRoot, filePath)
280 : filePath;
281 if (!rel || escapesRoot(rel)) return false;
282 return matcher.ignores(rel.split(path.sep).join('/'));
283 };
284 const deprioritized: DeprioritizedPathMatcher = (filePath: string): boolean => {
285 try {
286 return matches(currentMatcher(), filePath);
287 } catch {
288 // Ranking must never take the search down with it.
289 return false;
290 }
291 };
292 // One `stat` for a whole loop of candidates instead of one per candidate
293 // (#2620): a search reads the config once, then asks the frozen matcher.
294 deprioritized.snapshot = () => {
295 let matcher: ReturnType<typeof ignore> | null = null;
296 try { matcher = currentMatcher(); } catch { matcher = null; }
297 return (filePath: string): boolean => {
298 try {
299 return matches(matcher, filePath);
300 } catch {

Callers 2

constructorMethod · 0.95

Calls 5

deriveProjectNameTokensFunction · 0.90
createContextBuilderFunction · 0.90
setProjectNameTokensMethod · 0.80
matchesFunction · 0.50

Tested by

no test coverage detected