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

Method flush

src/sync/watcher.ts:1080–1197  ·  view source on GitHub ↗

* Flush pending changes by running sync. * * pendingFiles is NOT cleared at the start of sync — entries are removed * only after sync commits successfully, and only for entries whose * lastSeenMs <= syncStartedMs. That way, a query that arrives mid-sync * still sees the affected files

()

Source from the content-addressed store, hash-verified

1078 * unindexed, and the rescheduled sync will absorb the same set next time.
1079 */
1080 private async flush(): Promise<void> {
1081 // If already syncing, the post-sync check will re-trigger
1082 if (this.syncing || this.stopped) return;
1083
1084 this.syncStartedMs = Date.now();
1085 this.syncing = true;
1086
1087 // Scoped fast path: when every pending change is a known file event, hand
1088 // the exact paths to sync and skip its O(repo) scan-diff. Anything the
1089 // events can't fully describe — a directory removal, an empty pending set
1090 // (retry paths), or an event storm past the ceiling — runs the full
1091 // scan-diff, which remains the ground truth.
1092 const scoped =
1093 !this.needsFullScan && this.pendingFiles.size > 0 && this.pendingFiles.size <= SCOPED_SYNC_MAX_PENDING
1094 ? [...this.pendingFiles.keys()]
1095 : undefined;
1096
1097 // Consume this request before yielding so a topology/scope change during
1098 // the full sync remains pending for the next reconcile.
1099 if (!scoped) this.needsFullScan = false;
1100 try {
1101 const result = await this.syncFn(scoped);
1102 if (this.recoveringFromLock && !scoped && !this.needsFullScan) {
1103 this.degradedReason = null;
1104 this.recoveringFromLock = false;
1105 }
1106 this.lockRetryCount = 0; // a clean sync clears any contention backoff
1107 this.syncFailureRetryCount = 0; // ...and any generic-failure backoff
1108 // Remove entries whose most recent event predates this sync — those
1109 // edits are now in the DB. Entries with lastSeenMs > syncStartedMs
1110 // arrived mid-sync; whether the in-flight sync captured them depends
1111 // on when sync read that file, so we keep them as pending and let
1112 // the follow-up sync handle them. We prefer false positives ("shown
1113 // stale, actually fresh" → at worst one extra Read) over false
1114 // negatives ("shown fresh, actually stale" → misleads the agent).
1115 for (const [filePath, info] of this.pendingFiles) {
1116 if (info.lastSeenMs <= this.syncStartedMs) {
1117 this.pendingFiles.delete(filePath);
1118 }
1119 }
1120 this.callHost('onSyncComplete', () => this.onSyncComplete?.(result));
1121 } catch (err) {
1122 if (!scoped) this.needsFullScan = true;
1123 if (err instanceof LockUnavailableError) {
1124 this.lockRetryCount += 1;
1125 // Lock-failure no-op (another writer holds the lock). pendingFiles
1126 // stays intact and the `finally` block reschedules with backoff. Keep
1127 // brief contention quiet (debug-only — a long external index would
1128 // otherwise spam stderr every cycle), but stop retrying forever: once a
1129 // writer holds the lock past the budget, degrade auto-sync explicitly.
1130 logDebug('Watch sync skipped: file lock unavailable', {
1131 pendingFiles: this.pendingFiles.size,
1132 retryCount: this.lockRetryCount,
1133 });
1134 if (this.lockRetryCount > MAX_LOCK_RETRIES) {
1135 this.degrade(
1136 'CodeGraph file lock held by another process past the retry budget; ' +
1137 'auto-sync disabled. Run `codegraph sync` once the other writer finishes ' +

Callers 3

scheduleSyncMethod · 0.95
scheduleRetrySyncMethod · 0.95
mainFunction · 0.45

Calls 7

callHostMethod · 0.95
degradeMethod · 0.95
scheduleRetrySyncMethod · 0.95
scheduleSyncMethod · 0.95
logDebugFunction · 0.90
logWarnFunction · 0.90
deleteMethod · 0.65

Tested by

no test coverage detected