fix: resolve disableAutoRebuild and fork() initialization issues

- Fix disableAutoRebuild being ignored when indexes are empty on startup
- Add second check after needsRebuild to enable lazy loading properly
- Auto-create initial commit in fork() if none exists, eliminating manual setup
- Resolves Workshop team's 30-minute startup delays with large datasets (5.9M relationships)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
David Snelling 2025-11-04 07:42:06 -08:00
parent 7a399085c3
commit 12839b5ac9

View file

@ -2159,7 +2159,20 @@ export class Brainy<T = any> implements BrainyInterface<T> {
const refManager = (this.storage as any).refManager
const currentBranch = (this.storage as any).currentBranch || 'main'
// Step 1: Copy storage ref (COW layer - instant!)
// Step 1: Ensure initial commit exists (required for fork)
const currentRef = await refManager.getRef(currentBranch)
if (!currentRef) {
// Auto-create initial commit if none exists
await this.commit(`Initial commit on ${currentBranch}`, {
author: options?.author || 'Brainy',
timestamp: Date.now()
})
if (!this.config.silent) {
console.log(`📝 Auto-created initial commit on ${currentBranch} (required for fork)`)
}
}
// Step 2: Copy storage ref (COW layer - instant!)
await refManager.copyRef(currentBranch, branchName)
// Step 2: Create new Brainy instance pointing to fork branch
@ -4348,14 +4361,22 @@ export class Brainy<T = any> implements BrainyInterface<T> {
const needsRebuild =
metadataStats.totalEntries === 0 ||
hnswIndexSize === 0 ||
graphIndexSize === 0 ||
this.config.disableAutoRebuild === false // Explicitly enabled
graphIndexSize === 0
if (!needsRebuild) {
// All indexes already populated, no rebuild needed
return
}
// BUG FIX: If disableAutoRebuild === true, skip rebuild even if indexes are empty
// Indexes will load lazily on first query
if (this.config.disableAutoRebuild === true) {
if (!this.config.silent) {
console.log('⚡ Indexes empty but auto-rebuild disabled - using lazy loading')
}
return
}
// Small dataset: Rebuild all indexes for best performance
if (totalCount < AUTO_REBUILD_THRESHOLD || this.config.disableAutoRebuild === false) {
if (!this.config.silent) {