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

Method wireLayers

src/index.ts:242–319  ·  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

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