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

Method openProjectRoot

src/mcp/tools.ts:2539–2583  ·  view source on GitHub ↗

* The open CodeGraph for an index root the up-walk resolved: the default * instance, a cached one, or a newly opened (and cached) one.

(resolvedRoot: string, canonicalRoot: string)

Source from the content-addressed store, hash-verified

2537 * instance, a cached one, or a newly opened (and cached) one.
2538 */
2539 private openProjectRoot(resolvedRoot: string, canonicalRoot: string): CodeGraph {
2540 // If the path resolves to the default project, reuse the already-open
2541 // default instance rather than opening a SECOND connection to the same DB.
2542 // A duplicate connection serializes reads against the watcher's auto-sync
2543 // writes; when WAL isn't in effect (e.g. a filesystem without shared-memory
2544 // support) that surfaces as intermittent
2545 // "database is locked" on concurrent tool calls. See issue #238. The
2546 // default instance is owned/closed by the server, so it's never cached.
2547 // Another spelling of the same root counts too (#1057).
2548 if (this.cg && isSameIndexRoot(this.cg.getProjectRoot(), resolvedRoot)) {
2549 return this.freshen(this.cg);
2550 }
2551
2552 // Cache the open DB connection by CANONICAL ROOT only — never by the input
2553 // path. One key per instance means closeAll() closes each exactly once, and
2554 // a changed resolution maps to a different entry instead of a stale hit.
2555 const cached = this.projectCache.get(canonicalRoot);
2556 if (cached) {
2557 // Refresh LRU position.
2558 this.projectCache.delete(canonicalRoot);
2559 this.projectCache.set(canonicalRoot, cached);
2560 this.projectUsedAt.set(canonicalRoot, Date.now());
2561 return this.freshen(cached);
2562 }
2563
2564 // Compare current identities on every cache miss: a previously seen alias
2565 // may have been retargeted or recreated since the last call (#1057).
2566 for (const [root, open] of this.projectCache) {
2567 if (isSameIndexRoot(root, resolvedRoot)) {
2568 this.projectCache.delete(root);
2569 this.projectCache.set(root, open);
2570 this.projectUsedAt.set(root, Date.now());
2571 return this.freshen(open);
2572 }
2573 }
2574
2575 // Indexes an interrupted bulk load dropped are rebuilt off this thread,
2576 // before the project is served (awaitIndexRecovery, #1887).
2577 const open = () => loadCodeGraph().openSync(canonicalRoot, { deferIndexRecovery: true });
2578 const cg = this.projectLifecycle?.open(canonicalRoot, open) ?? open();
2579 this.projectCache.set(canonicalRoot, cg);
2580 this.projectUsedAt.set(canonicalRoot, Date.now());
2581 this.trimProjects();
2582 return cg;
2583 }
2584
2585 /**
2586 * The nested git repository `projectPath` lives in when the index the

Callers 1

getCodeGraphMethod · 0.95

Calls 9

freshenMethod · 0.95
trimProjectsMethod · 0.95
isSameIndexRootFunction · 0.90
getProjectRootMethod · 0.65
getMethod · 0.65
deleteMethod · 0.65
openMethod · 0.65
openFunction · 0.50
setMethod · 0.45

Tested by

no test coverage detected