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

Function makeFallbackEngine

src/mcp/index.ts:126–164  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

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

Callers 1

Calls 8

assertNoRebuildFunction · 0.90
decodeLockInfoFunction · 0.90
getDaemonPidPathFunction · 0.90
isProcessAliveFunction · 0.90
canProbeDaemonIdentityFunction · 0.90
readWriterLockFunction · 0.90
installChangedReasonFunction · 0.90
readOnlyFallbackFunction · 0.85

Tested by

no test coverage detected