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