fix: create proper initial commit instead of using tree hash for main branch

Fixed critical bug where COW storage initialization was creating the 'main'
branch with a tree hash (0000...0) instead of creating an actual commit object.
This caused getHistory() to fail with "Blob not found: 0000...0" on fresh
Brainy instances.

Changes:
- src/storage/baseStorage.ts: Create initial commit object during initialization
- tests/integration/empty-tree-bug.test.ts: Add regression tests

The 'main' branch now properly points to an initial commit with an empty tree,
allowing commit history to be traversed correctly.

🤖 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 13:34:51 -08:00
parent c1d4de4105
commit 5e602a03ca
2 changed files with 160 additions and 2 deletions

View file

@ -302,9 +302,23 @@ export abstract class BaseStorage extends BaseStorageAdapter {
// Check if main branch exists, create if not
const mainRef = await this.refManager.getRef('main')
if (!mainRef) {
// Create initial commit (empty tree)
// Create initial commit with empty tree
const emptyTreeHash = '0000000000000000000000000000000000000000000000000000000000000000'
await this.refManager.createBranch('main', emptyTreeHash, {
// Import CommitBuilder
const { CommitBuilder } = await import('./cow/CommitObject.js')
// Create initial commit object
const initialCommitHash = await CommitBuilder.create(this.blobStorage)
.tree(emptyTreeHash)
.parent(null)
.message('Initial commit')
.author('system')
.timestamp(Date.now())
.build()
// Create main branch pointing to initial commit
await this.refManager.createBranch('main', initialCommitHash, {
description: 'Initial branch',
author: 'system'
})