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

Method openProjectRoot

src/mcp/tools.ts:2581–2625  ·  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

2579 * instance, a cached one, or a newly opened (and cached) one.
2580 */
2581 private openProjectRoot(resolvedRoot: string, canonicalRoot: string): CodeGraph {
2582 // If the path resolves to the default project, reuse the already-open
2583 // default instance rather than opening a SECOND connection to the same DB.
2584 // A duplicate connection serializes reads against the watcher's auto-sync
2585 // writes; when WAL isn't in effect (e.g. a filesystem without shared-memory
2586 // support) that surfaces as intermittent
2587 // "database is locked" on concurrent tool calls. See issue #238. The
2588 // default instance is owned/closed by the server, so it's never cached.
2589 // Another spelling of the same root counts too (#1057).
2590 if (this.cg && isSameIndexRoot(this.cg.getProjectRoot(), resolvedRoot)) {
2591 return this.freshen(this.cg);
2592 }
2593
2594 // Cache the open DB connection by CANONICAL ROOT only — never by the input
2595 // path. One key per instance means closeAll() closes each exactly once, and
2596 // a changed resolution maps to a different entry instead of a stale hit.
2597 const cached = this.projectCache.get(canonicalRoot);
2598 if (cached) {
2599 // Refresh LRU position.
2600 this.projectCache.delete(canonicalRoot);
2601 this.projectCache.set(canonicalRoot, cached);
2602 this.projectUsedAt.set(canonicalRoot, Date.now());
2603 return this.freshen(cached);
2604 }
2605
2606 // Compare current identities on every cache miss: a previously seen alias
2607 // may have been retargeted or recreated since the last call (#1057).
2608 for (const [root, open] of this.projectCache) {
2609 if (isSameIndexRoot(root, resolvedRoot)) {
2610 this.projectCache.delete(root);
2611 this.projectCache.set(root, open);
2612 this.projectUsedAt.set(root, Date.now());
2613 return this.freshen(open);
2614 }
2615 }
2616
2617 // Indexes an interrupted bulk load dropped are rebuilt off this thread,
2618 // before the project is served (awaitIndexRecovery, #1887).
2619 const open = () => loadCodeGraph().openSync(canonicalRoot, { deferIndexRecovery: true });
2620 const cg = this.projectLifecycle?.open(canonicalRoot, open) ?? open();
2621 this.projectCache.set(canonicalRoot, cg);
2622 this.projectUsedAt.set(canonicalRoot, Date.now());
2623 this.trimProjects();
2624 return cg;
2625 }
2626
2627 /**
2628 * 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