(opts: MCPEngineOptions = {})
| 108 | private queryPool: QueryPool | null = null; |
| 109 | |
| 110 | constructor(opts: MCPEngineOptions = {}) { |
| 111 | this.opts = { readOnly: opts.readOnly ?? false, watch: opts.watch ?? true, queryPool: opts.queryPool ?? false, queryPoolDefaultMax: opts.queryPoolDefaultMax }; |
| 112 | this.toolHandler = new ToolHandler(null); |
| 113 | this.toolHandler.setProjectLifecycle({ |
| 114 | open: (root, open) => { |
| 115 | // Explicit projects and read-only fallbacks also hold SQLite handles. |
| 116 | // Fence them before opening, just like the default daemon project. |
| 117 | assertNoRebuild(root); |
| 118 | if (this.opts.readOnly) return loadCodeGraph().openSync(root, { readOnly: true }); |
| 119 | if (!this.opts.watch) return open(); |
| 120 | const lease = acquireProject(root, open, this.watchOptions()); |
| 121 | this.explicitProjects.set(lease.cg, lease); |
| 122 | return lease.cg; |
| 123 | }, |
| 124 | activate: (cg) => this.explicitProjects.get(cg)?.ready() ?? Promise.resolve(), |
| 125 | release: (cg) => this.releaseExplicitProject(cg), |
| 126 | }); |
| 127 | // A tool call found the default project's database replaced on disk (a |
| 128 | // `codegraph index` rebuild) and reopened it (#1902). Reconcile the new |
| 129 | // file with the usual catch-up — `sync()` serializes on the index mutex, |
| 130 | // so it never overlaps an in-flight watcher sync. Only when this engine is |
| 131 | // watching, i.e. it is the project's writer: a read-only engine (writer |
| 132 | // lock held elsewhere, watching disabled) must not start writing. |
| 133 | this.toolHandler.setOnDatabaseReopened((cg) => { |
| 134 | if (cg === this.cg && cg.isWatching()) this.catchUpSync(true); |
| 135 | }); |
| 136 | if (opts.writerLockRoot && !this.opts.readOnly) { |
| 137 | assertNoRebuild(opts.writerLockRoot); |
| 138 | const writer = tryAcquireWriterLock(opts.writerLockRoot, 'fallback'); |
| 139 | if (writer.kind === 'taken') { |
| 140 | throw new Error(writerLockHeldMessage(writer.existing, writer.pidPath)); |
| 141 | } |
| 142 | this.writerLockRoot = opts.writerLockRoot; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Start the worker-thread query pool after resolving the default project |
nothing calls this directly
no test coverage detected