* 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)
| 262 | * to pick up a project that appeared *after* the engine started. |
| 263 | */ |
| 264 | retryInitializeSync(searchFrom: string): void { |
| 265 | if (this.closed) return; |
| 266 | if (this.toolHandler.hasDefaultCodeGraph()) return; |
| 267 | this.toolHandler.setDefaultProjectHint(searchFrom); |
| 268 | // Same resolution `doInitialize` used: up-walk, then the bounded workspace |
| 269 | // down-scan (#1606) — this retry is exactly the path that picks up a |
| 270 | // project (root or child) `codegraph init`'d after the server started. The |
| 271 | // down-scan is throttled so the persistent no-default state doesn't pay a |
| 272 | // directory walk on every tool call; the up-walk always runs. |
| 273 | const scanDue = Date.now() - this.lastRetrySubScanAt >= RETRY_SUBSCAN_TTL_MS; |
| 274 | const res = resolveServerRoot(searchFrom, { subprojectScan: scanDue }); |
| 275 | if (scanDue) { |
| 276 | this.lastRetrySubScanAt = Date.now(); |
| 277 | if (!res.root) this.toolHandler.setKnownSubprojects(res.candidates, searchFrom); |
| 278 | } |
| 279 | // An up-walk alone can't clear a refusal the throttled down-scan found. |
| 280 | if (scanDue || res.refusal) this.noteRootRefusal(res.refusal); |
| 281 | const resolvedRoot = res.root; |
| 282 | if (!resolvedRoot) return; |
| 283 | if (res.viaSubScan) this.logSubprojectAdoption(searchFrom, resolvedRoot); |
| 284 | // Opening a project, and the catch-up after it, is work (#3250). |
| 285 | this.workBegins(); |
| 286 | try { |
| 287 | // Close any previously failed instance to avoid leaking resources. |
| 288 | if (this.cg) { |
| 289 | try { this.cg.close(); } catch { /* ignore */ } |
| 290 | this.cg = null; |
| 291 | } |
| 292 | assertNoRebuild(resolvedRoot); |
| 293 | // Indexes an interrupted bulk load dropped are rebuilt off this thread |
| 294 | // (#1887); the catch-up and tool calls wait for them. |
| 295 | this.cg = loadCodeGraph().openSync(resolvedRoot, { readOnly: this.opts.readOnly, deferIndexRecovery: true }); |
| 296 | this.projectPath = resolvedRoot; |
| 297 | this.toolHandler.setDefaultCodeGraph(this.cg); |
| 298 | this.startWatching(); |
| 299 | this.catchUpSync(); |
| 300 | // The pool's workers open the project themselves: start them on a |
| 301 | // rebuilt one, not racing the rebuild for its write lock. |
| 302 | const opened = this.cg; |
| 303 | if (opened?.hasPendingIndexRecovery()) { |
| 304 | void opened.recoverIndexes().catch(() => { /* reported by the tool call waiting on it */ }).then(() => { |
| 305 | if (this.cg === opened) this.maybeStartPool(resolvedRoot); |
| 306 | }); |
| 307 | } else { |
| 308 | this.maybeStartPool(resolvedRoot); |
| 309 | } |
| 310 | } catch (err) { |
| 311 | // Still failing — caller will try again on the next tool call. |
| 312 | this.toolHandler.setDefaultOpenFailure(reportableOpenFailure(err)); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Close everything. Used on graceful daemon shutdown (SIGTERM/idle timeout) |
no test coverage detected