Copied to Clipboard
saveCodebaseSnapshot
Below is the code snippet picked from the snapshot.ts
public saveCodebaseSnapshot(): void {
console.log('[SNAPSHOT-DEBUG] Saving codebase snapshot to:', this.snapshotFilePath);
const locked = this.acquireLock();
if (!locked) {
console.warn('[SNAPSHOT-DEBUG] Failed to acquire lock, saving without lock');
}
try {
// Ensure directory exists
const snapshotDir = path.dirname(this.snapshotFilePath);
if (!fs.existsSync(snapshotDir)) {
fs.mkdirSync(snapshotDir, { recursive: true });
console.log('[SNAPSHOT-DEBUG] Created snapshot directory:', snapshotDir);
}
...
I mean, snapshot is written to your local file system. Claude-Context uses file system to keep track of codebase indexing, is what I figured looking at some of the files and the way file-system was used.
Since we now understand what led to finding this acquireLock function, letβs take a look at this function, acquireLock.
acquireLock function explained.
acquireLock is defined in claude-context/packages/mcp/snapshot.ts at L555 as shown below:
private acquireLock(maxRetries = 5, retryInterval = 100): boolean {
const lockPath = this.snapshotFilePath + '.lock';
for (let i = 0; i < maxRetries; i++) {
try {
fs.mkdirSync(lockPath);
return true;
} catch {
// Check for stale lock (> 10 seconds old)
try {
const stat = fs.statSync(lockPath);
if (Date.now() - stat.mtimeMs > 10000) {
fs.rmdirSync(lockPath);
continue; // retry after removing stale lock
}
} catch { /* lock was removed by another process */ }
// Busy wait and retry
const waitUntil = Date.now() + retryInterval;
while (Date.now() < waitUntil) { /* busy wait */ }
}
}
return false;
}
It creates a lock file using the snapshotFilePath with .lock extension and then returns true. mkdirsync synchronously creates a directory. Returns undefined, or if recursive is true, the first directory path created. This is the synchronous version of mkdir.
About me:
Hey, my name is ramunarasinga. Email: ramunarasinga@gmail.com
Tired of AI slop?
I spent 3+ years studying OSS codebases and wrote 350+ articles on what makes them production-grade. I built
Get started for free β thinkthroo.com
References:
zilliztech/claude-context/packages/mcp/src/snapshot.ts#L555.
zilliztech/claude-context/packages/mcp/src/snapshot.ts#L603.