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

Method sync

src/index.ts:1147–1974  ·  view source on GitHub ↗
(options: IndexOptions = {})

Source from the content-addressed store, hash-verified

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

Callers 15

openMethod · 0.95
watchMethod · 0.95
syncEditFunction · 0.45
convergesFunction · 0.45
syncedFunction · 0.45
syncEditFunction · 0.45

Calls 15

getIndexStateMethod · 0.95
isFreshlyRecreatedMethod · 0.95
pythonFilePathsMethod · 0.95
stampPhpWebRootsMethod · 0.95
synthesisHoldMsMethod · 0.95
armDeferredSynthesisMethod · 0.95

Tested by 15

syncEditFunction · 0.36
convergesFunction · 0.36
syncedFunction · 0.36
syncEditFunction · 0.36
reindexFunction · 0.36
convergeAfterFunction · 0.36
syncEditFunction · 0.36
indexThenSyncFunction · 0.36
syncsFunction · 0.36
syncEditFunction · 0.36
editAndSyncFunction · 0.36
syncEditFunction · 0.36