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