* Create an in-process fallback only when it cannot conflict with a live * legacy daemon. Plain-PID locks cannot prove daemon identity, but they still * prove that a process owns the legacy writer slot.
(root: string)
| 123 | * prove that a process owns the legacy writer slot. |
| 124 | */ |
| 125 | function makeFallbackEngine(root: string): MCPEngine { |
| 126 | assertNoRebuild(root); |
| 127 | let existing: ReturnType<typeof decodeLockInfo> = null; |
| 128 | try { |
| 129 | existing = decodeLockInfo(fs.readFileSync(getDaemonPidPath(root), 'utf8')); |
| 130 | } catch (err) { |
| 131 | const code = (err as NodeJS.ErrnoException).code; |
| 132 | if (code !== 'ENOENT') { |
| 133 | throw new Error(`The daemon lock could not be read (${code ?? 'unknown error'}); refusing an in-process fallback.`); |
| 134 | } |
| 135 | } |
| 136 | if ( |
| 137 | existing && |
| 138 | isProcessAlive(existing.pid) && |
| 139 | !canProbeDaemonIdentity(existing) |
| 140 | ) { |
| 141 | throw new Error( |
| 142 | `Cannot start an in-process fallback while live legacy daemon pid ${existing.pid} holds the project lock.` |
| 143 | ); |
| 144 | } |
| 145 | const writer = readWriterLock(root); |
| 146 | if (writer && writer.pid > 0 && isProcessAlive(writer.pid)) { |
| 147 | // Another process owns updates. A fallback may still serve read-only WAL |
| 148 | // queries without claiming a second writer or starting a watcher (#1963). |
| 149 | return readOnlyFallback(`writer lock held by PID ${writer.pid} (${writer.mode} mode)`); |
| 150 | } |
| 151 | if (existing && isProcessAlive(existing.pid)) { |
| 152 | return readOnlyFallback(`live daemon PID ${existing.pid} holds the project lock`); |
| 153 | } |
| 154 | // This session's own install was upgraded or removed underneath it (#2335): |
| 155 | // its code no longer loads whole, so as the project's writer it would only |
| 156 | // keep a daemon from the current install from starting. Its daemon exits |
| 157 | // for the same reason, which is how it got here. |
| 158 | if (CodeGraphPackageVersion !== '0.0.0-unknown' && |
| 159 | installChangedReason(CodeGraphPackageJsonPath, CodeGraphPackageVersion) !== null) { |
| 160 | return readOnlyFallback('this session\'s CodeGraph install was upgraded or removed; restart the session to use the current one'); |
| 161 | } |
| 162 | return new MCPEngine({ writerLockRoot: root, queryPool: true, queryPoolDefaultMax: DIRECT_QUERY_POOL_MAX }); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * How long a launcher waits for a freshly-spawned daemon to answer before |
no test coverage detected