(searchFrom: string)
| 368 | } |
| 369 | |
| 370 | private async doInitialize(searchFrom: string): Promise<void> { |
| 371 | this.toolHandler.setDefaultProjectHint(searchFrom); |
| 372 | |
| 373 | // Up-walk first; when nothing is indexed at or above searchFrom, a bounded |
| 374 | // down-scan may adopt a SINGLE indexed sub-project as the default (#1606 — |
| 375 | // the workspace-container shape where only children are indexed). Zero or |
| 376 | // several candidates → no default project, but SAY so (#1607): the silent |
| 377 | // variant of this state read as "CodeGraph is broken" and was diagnosable |
| 378 | // only by knowing to look for a missing ~/.codegraph/daemons/ entry. |
| 379 | const res = resolveServerRoot(searchFrom); |
| 380 | const resolvedRoot = res.root; |
| 381 | this.noteRootRefusal(res.refusal); |
| 382 | if (!resolvedRoot) { |
| 383 | // Sessions may still discover a project later via roots/list, and the |
| 384 | // per-call retry re-resolves — this state is recoverable, hence stderr |
| 385 | // (not a failure) + candidates surfaced through the tool-call error. |
| 386 | this.projectPath = searchFrom; |
| 387 | this.toolHandler.setKnownSubprojects(res.candidates, searchFrom); |
| 388 | if (!res.refusal) { |
| 389 | process.stderr.write( |
| 390 | `[CodeGraph MCP] No .codegraph/ at or above ${searchFrom}: no default project, live sync disabled until an indexed project is accessed via projectPath.\n` |
| 391 | ); |
| 392 | } |
| 393 | if (res.candidates.length > 0) { |
| 394 | const rels = res.candidates.map((c) => path.relative(searchFrom, c) || '.'); |
| 395 | process.stderr.write( |
| 396 | `[CodeGraph MCP] Indexed sub-projects found: ${rels.join(', ')}. Pass \`projectPath\` per call, or launch with --path.\n` |
| 397 | ); |
| 398 | } |
| 399 | this.maybeStartPool(null); |
| 400 | return; |
| 401 | } |
| 402 | if (res.viaSubScan) this.logSubprojectAdoption(searchFrom, resolvedRoot); |
| 403 | |
| 404 | this.projectPath = resolvedRoot; |
| 405 | try { |
| 406 | assertNoRebuild(resolvedRoot); |
| 407 | const opened = await loadCodeGraph().open(resolvedRoot, { readOnly: this.opts.readOnly }); |
| 408 | if (this.closed) { opened.close(); return; } |
| 409 | this.cg = opened; |
| 410 | this.toolHandler.setDefaultCodeGraph(this.cg); |
| 411 | this.startWatching(); |
| 412 | this.catchUpSync(); |
| 413 | this.maybeStartPool(resolvedRoot); |
| 414 | } catch (err) { |
| 415 | const msg = err instanceof Error ? err.message : String(err); |
| 416 | process.stderr.write(`[CodeGraph MCP] Failed to open project at ${resolvedRoot}: ${msg}\n`); |
| 417 | // The agent otherwise hears only "no project loaded" (#995). |
| 418 | this.toolHandler.setDefaultOpenFailure(reportableOpenFailure(err)); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Record why the default project is refused (a sensitive folder, #2929's |
no test coverage detected