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

Method retryInitializeSync

src/mcp/engine.ts:238–286  ·  view source on GitHub ↗

* Synchronous last-resort init used by the per-session retry loop when the * background `ensureInitialized` already finished (or failed) and we need * to pick up a project that appeared *after* the engine started.

(searchFrom: string)

Source from the content-addressed store, hash-verified

236 * to pick up a project that appeared *after* the engine started.
237 */
238 retryInitializeSync(searchFrom: string): void {
239 if (this.closed) return;
240 if (this.toolHandler.hasDefaultCodeGraph()) return;
241 this.toolHandler.setDefaultProjectHint(searchFrom);
242 // Same resolution `doInitialize` used: up-walk, then the bounded workspace
243 // down-scan (#1606) — this retry is exactly the path that picks up a
244 // project (root or child) `codegraph init`'d after the server started. The
245 // down-scan is throttled so the persistent no-default state doesn't pay a
246 // directory walk on every tool call; the up-walk always runs.
247 const scanDue = Date.now() - this.lastRetrySubScanAt >= RETRY_SUBSCAN_TTL_MS;
248 const res = resolveServerRoot(searchFrom, { subprojectScan: scanDue });
249 if (scanDue) {
250 this.lastRetrySubScanAt = Date.now();
251 if (!res.root) this.toolHandler.setKnownSubprojects(res.candidates, searchFrom);
252 }
253 // An up-walk alone can't clear a refusal the throttled down-scan found.
254 if (scanDue || res.refusal) this.noteRootRefusal(res.refusal);
255 const resolvedRoot = res.root;
256 if (!resolvedRoot) return;
257 if (res.viaSubScan) this.logSubprojectAdoption(searchFrom, resolvedRoot);
258 try {
259 // Close any previously failed instance to avoid leaking resources.
260 if (this.cg) {
261 try { this.cg.close(); } catch { /* ignore */ }
262 this.cg = null;
263 }
264 assertNoRebuild(resolvedRoot);
265 // Indexes an interrupted bulk load dropped are rebuilt off this thread
266 // (#1887); the catch-up and tool calls wait for them.
267 this.cg = loadCodeGraph().openSync(resolvedRoot, { readOnly: this.opts.readOnly, deferIndexRecovery: true });
268 this.projectPath = resolvedRoot;
269 this.toolHandler.setDefaultCodeGraph(this.cg);
270 this.startWatching();
271 this.catchUpSync();
272 // The pool's workers open the project themselves: start them on a
273 // rebuilt one, not racing the rebuild for its write lock.
274 const opened = this.cg;
275 if (opened?.hasPendingIndexRecovery()) {
276 void opened.recoverIndexes().catch(() => { /* reported by the tool call waiting on it */ }).then(() => {
277 if (this.cg === opened) this.maybeStartPool(resolvedRoot);
278 });
279 } else {
280 this.maybeStartPool(resolvedRoot);
281 }
282 } catch (err) {
283 // Still failing — caller will try again on the next tool call.
284 this.toolHandler.setDefaultOpenFailure(reportableOpenFailure(err));
285 }
286 }
287
288 /**
289 * Close everything. Used on graceful daemon shutdown (SIGTERM/idle timeout)

Calls 15

noteRootRefusalMethod · 0.95
logSubprojectAdoptionMethod · 0.95
startWatchingMethod · 0.95
catchUpSyncMethod · 0.95
maybeStartPoolMethod · 0.95
resolveServerRootFunction · 0.90
assertNoRebuildFunction · 0.90
reportableOpenFailureFunction · 0.85
setDefaultProjectHintMethod · 0.80
setKnownSubprojectsMethod · 0.80
setDefaultCodeGraphMethod · 0.80

Tested by

no test coverage detected