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

Method wireLayers

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

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