| 1140 | } |
| 1141 | |
| 1142 | async sync(options: IndexOptions = {}): Promise<SyncResult> { |
| 1143 | const { |
| 1144 | ESM_IMPORT_LANGUAGES, |
| 1145 | aliasMayName, |
| 1146 | changedModuleConfigs, |
| 1147 | changedRoutes, |
| 1148 | createImportReader, |
| 1149 | hasSynthesisPattern, |
| 1150 | isJsxChildCandidate, |
| 1151 | moduleConfigLanguages, |
| 1152 | parseManifestStamp, |
| 1153 | parseModuleConfigSnapshot, |
| 1154 | reopenNameMatchedImports, |
| 1155 | reopenReferencesThroughAddedFiles, |
| 1156 | reopenReferencesThroughBarrels, |
| 1157 | reopenReferencesThroughChangedDefaults, |
| 1158 | reopenReferencesThroughDeclaredPackages, |
| 1159 | reopenReferencesThroughObjectMembers, |
| 1160 | reopenReferencesThroughRetargetedModules, |
| 1161 | trustedManifestList, |
| 1162 | } = engine(); |
| 1163 | return this.indexMutex.withLock(async () => { |
| 1164 | const startedAt = Date.now(); |
| 1165 | try { |
| 1166 | this.fileLock.acquire(); |
| 1167 | } catch (err) { |
| 1168 | throw new LockUnavailableError( |
| 1169 | `Sync could not acquire the file lock; retry when the index is available. ${err instanceof Error ? err.message : String(err)}` |
| 1170 | ); |
| 1171 | } |
| 1172 | // A full rebuild in another process (`codegraph index` → recreate) |
| 1173 | // unlinks the database and creates a new file at the same path. A |
| 1174 | // long-lived instance — the MCP daemon's watcher — would otherwise keep |
| 1175 | // "syncing" into the dead inode, and nothing it wrote there is visible |
| 1176 | // to anyone (#1902). Follow the path before writing (one stat), and |
| 1177 | // widen a scoped sync to a full one: whatever the old handle absorbed |
| 1178 | // since the rebuild is gone, so the new file has to be reconciled whole. |
| 1179 | // If the reopen fails (the rebuild is mid-way), report lock contention |
| 1180 | // so the watcher keeps its pending files and retries. |
| 1181 | try { |
| 1182 | this.reopenReplacedDatabase(); |
| 1183 | } catch (err) { |
| 1184 | this.fileLock.release(); |
| 1185 | throw new LockUnavailableError( |
| 1186 | `Sync could not open the rebuilt index yet; retry when the rebuild finishes. ${err instanceof Error ? err.message : String(err)}` |
| 1187 | ); |
| 1188 | } |
| 1189 | if (this.pendingFullReconcile) { |
| 1190 | // `codegraph index` recreates the file, THEN takes the write lock in |
| 1191 | // indexAll. A sync landing in that gap would otherwise run a full |
| 1192 | // reconcile of the empty file and hold the lock the rebuild is about |
| 1193 | // to ask for. A fresh file with no index_state yet is that rebuild: |
| 1194 | // step aside (lock contention, the watcher retries) and reconcile in |
| 1195 | // full once it is done. Bounded, so a rebuild that died before |
| 1196 | // indexing does not park the watcher forever. |
| 1197 | if (this.getIndexState() === null && this.isFreshlyRecreated()) { |
| 1198 | this.fileLock.release(); |
| 1199 | throw new LockUnavailableError('A rebuild of this index is in progress; retry when it finishes.'); |