Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/entire/cli/benchutil/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func benchBuildTree(entryCount int) func(*testing.B) {

b.ResetTimer()
for range b.N {
_, buildErr := checkpoint.BuildTreeFromEntries(freshRepo, entries)
_, buildErr := checkpoint.BuildTreeFromEntries(context.Background(), freshRepo, entries)
if buildErr != nil {
b.Fatalf("BuildTreeFromEntries: %v", buildErr)
}
Expand Down
13 changes: 7 additions & 6 deletions cmd/entire/cli/benchutil/parse_tree_bench_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package benchutil

import (
"context"
"fmt"
"testing"

Expand Down Expand Up @@ -73,7 +74,7 @@ func buildShardedMetadataTree(b *testing.B, repo *gogit.Repository, checkpointCo
}
}

hash, err := checkpoint.BuildTreeFromEntries(repo, entries)
hash, err := checkpoint.BuildTreeFromEntries(context.Background(), repo, entries)
if err != nil {
b.Fatalf("build tree: %v", err)
}
Expand All @@ -98,7 +99,7 @@ func buildFlatFileTree(b *testing.B, repo *gogit.Repository, fileCount int) plum
}
}

hash, err := checkpoint.BuildTreeFromEntries(repo, entries)
hash, err := checkpoint.BuildTreeFromEntries(context.Background(), repo, entries)
if err != nil {
b.Fatalf("build tree: %v", err)
}
Expand Down Expand Up @@ -138,7 +139,7 @@ func benchUpdateSubtreeTreeSurgery(priorCheckpoints int) func(*testing.B) {
Hash: newBlobs[i],
}
}
cpTreeHash, err := checkpoint.BuildTreeFromEntries(repo, cpEntries)
cpTreeHash, err := checkpoint.BuildTreeFromEntries(context.Background(), repo, cpEntries)
if err != nil {
b.Fatalf("build checkpoint tree: %v", err)
}
Expand Down Expand Up @@ -203,7 +204,7 @@ func benchUpdateSubtreeFlattenRebuild(priorCheckpoints int) func(*testing.B) {
}

// Rebuild entire tree
_, err = checkpoint.BuildTreeFromEntries(repo, entries)
_, err = checkpoint.BuildTreeFromEntries(context.Background(), repo, entries)
if err != nil {
b.Fatalf("BuildTreeFromEntries: %v", err)
}
Expand Down Expand Up @@ -255,7 +256,7 @@ func benchApplyTreeChangesTreeSurgery(fileCount, changeCount int) func(*testing.

b.ResetTimer()
for range b.N {
_, err := checkpoint.ApplyTreeChanges(repo, rootTree, changes)
_, err := checkpoint.ApplyTreeChanges(context.Background(), repo, rootTree, changes)
if err != nil {
b.Fatalf("ApplyTreeChanges: %v", err)
}
Expand Down Expand Up @@ -307,7 +308,7 @@ func benchApplyTreeChangesFlattenRebuild(fileCount, changeCount int) func(*testi
}

// Rebuild
_, err = checkpoint.BuildTreeFromEntries(repo, entries)
_, err = checkpoint.BuildTreeFromEntries(context.Background(), repo, entries)
if err != nil {
b.Fatalf("BuildTreeFromEntries: %v", err)
}
Expand Down
129 changes: 126 additions & 3 deletions cmd/entire/cli/checkpoint/checkpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ func TestUpdateSummary_NotFound(t *testing.T) {
store := NewGitStore(repo)

// Ensure sessions branch exists
err := store.ensureSessionsBranch()
err := store.ensureSessionsBranch(context.Background())
if err != nil {
t.Fatalf("ensureSessionsBranch() error = %v", err)
}
Expand Down Expand Up @@ -1534,7 +1534,7 @@ func TestReadCommitted_NonexistentCheckpoint(t *testing.T) {
store := NewGitStore(repo)

// Ensure sessions branch exists
err := store.ensureSessionsBranch()
err := store.ensureSessionsBranch(context.Background())
if err != nil {
t.Fatalf("ensureSessionsBranch() error = %v", err)
}
Expand All @@ -1557,7 +1557,7 @@ func TestReadSessionContent_NonexistentCheckpoint(t *testing.T) {
store := NewGitStore(repo)

// Ensure sessions branch exists
err := store.ensureSessionsBranch()
err := store.ensureSessionsBranch(context.Background())
if err != nil {
t.Fatalf("ensureSessionsBranch() error = %v", err)
}
Expand Down Expand Up @@ -1672,6 +1672,129 @@ func TestWriteTemporary_FirstCheckpoint_CapturesModifiedTrackedFiles(t *testing.
}
}

// TestWriteTemporary_PathNormalizationAndSkipping verifies that shadow branch writes
// normalize absolute in-repo paths back to repo-relative tree entries and skip invalid
// paths rather than encoding them into git trees.
func TestWriteTemporary_PathNormalizationAndSkipping(t *testing.T) {
tests := []struct {
name string
modifiedFiles func(repoRoot, mainFile string) []string
wantUpdated bool
}{
{
name: "absolute in repo path is normalized",
modifiedFiles: func(_, mainFile string) []string {
return []string{mainFile}
},
wantUpdated: true,
},
{
name: "absolute outside repo path is skipped",
modifiedFiles: func(_, _ string) []string {
return []string{"C:/Users/rober/Vaults/Flowsign/main.go"}
},
wantUpdated: false,
},
{
name: "empty segment path is skipped",
modifiedFiles: func(_, _ string) []string {
return []string{"dir//main.go"}
},
wantUpdated: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDir := t.TempDir()

repo, err := git.PlainInit(tempDir, false)
if err != nil {
t.Fatalf("failed to init git repo: %v", err)
}

worktree, err := repo.Worktree()
if err != nil {
t.Fatalf("failed to get worktree: %v", err)
}

mainFile := filepath.Join(tempDir, "main.go")
if err := os.WriteFile(mainFile, []byte("package main\n"), 0o644); err != nil {
t.Fatalf("failed to write main.go: %v", err)
}
if _, err := worktree.Add("main.go"); err != nil {
t.Fatalf("failed to add main.go: %v", err)
}
initialCommit, err := worktree.Commit("Initial commit", &git.CommitOptions{
Author: &object.Signature{Name: "Test", Email: "test@test.com"},
})
if err != nil {
t.Fatalf("failed to commit: %v", err)
}

updatedContent := "package main\n\nfunc main() {}\n"
if err := os.WriteFile(mainFile, []byte(updatedContent), 0o644); err != nil {
t.Fatalf("failed to update main.go: %v", err)
}

t.Chdir(tempDir)

metadataDir := filepath.Join(tempDir, ".entire", "metadata", "test-session")
if err := os.MkdirAll(metadataDir, 0o755); err != nil {
t.Fatalf("failed to create metadata dir: %v", err)
}
if err := os.WriteFile(filepath.Join(metadataDir, "full.jsonl"), []byte(`{"test": true}`), 0o644); err != nil {
t.Fatalf("failed to write transcript: %v", err)
}

store := NewGitStore(repo)
result, err := store.WriteTemporary(context.Background(), WriteTemporaryOptions{
SessionID: "test-session",
BaseCommit: initialCommit.String(),
ModifiedFiles: tt.modifiedFiles(tempDir, mainFile),
MetadataDir: ".entire/metadata/test-session",
MetadataDirAbs: metadataDir,
CommitMessage: "Checkpoint with path normalization",
AuthorName: "Test",
AuthorEmail: "test@test.com",
})
if err != nil {
t.Fatalf("WriteTemporary() error = %v", err)
}

commit, err := repo.CommitObject(result.CommitHash)
if err != nil {
t.Fatalf("failed to get commit object: %v", err)
}

tree, err := commit.Tree()
if err != nil {
t.Fatalf("failed to get tree: %v", err)
}

assertNoEmptyEntryNames(t, repo, commit.TreeHash, "")

file, err := tree.File("main.go")
if err != nil {
t.Fatalf("main.go not found in checkpoint tree: %v", err)
}

content, err := file.Contents()
if err != nil {
t.Fatalf("failed to read main.go content: %v", err)
}

wantContent := "package main\n"
if tt.wantUpdated {
wantContent = updatedContent
}
if content != wantContent {
t.Errorf("unexpected main.go content\ngot:\n%s\nwant:\n%s", content, wantContent)
}
})
}
}

// TestWriteTemporary_FirstCheckpoint_CapturesUntrackedFiles verifies that
// the first checkpoint captures untracked files that exist in the working directory.
func TestWriteTemporary_FirstCheckpoint_CapturesUntrackedFiles(t *testing.T) {
Expand Down
24 changes: 12 additions & 12 deletions cmd/entire/cli/checkpoint/committed.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (s *GitStore) WriteCommitted(ctx context.Context, opts WriteCommittedOption
}

// Ensure sessions branch exists
if err := s.ensureSessionsBranch(); err != nil {
if err := s.ensureSessionsBranch(ctx); err != nil {
return fmt.Errorf("failed to ensure sessions branch: %w", err)
}

Expand Down Expand Up @@ -98,7 +98,7 @@ func (s *GitStore) WriteCommitted(ctx context.Context, opts WriteCommittedOption
}

// Build checkpoint subtree and splice into root (O(depth) tree surgery)
newTreeHash, err := s.spliceCheckpointSubtree(rootTreeHash, opts.CheckpointID, basePath, entries)
newTreeHash, err := s.spliceCheckpointSubtree(ctx, rootTreeHash, opts.CheckpointID, basePath, entries)
if err != nil {
return err
}
Expand Down Expand Up @@ -151,7 +151,7 @@ func (s *GitStore) flattenCheckpointEntries(rootTreeHash plumbing.Hash, checkpoi
// at the correct shard location in the root tree using O(depth) tree surgery.
// basePath is like "a3/b2c4d5e6f7/" (with trailing slash).
// Returns the new root tree hash.
func (s *GitStore) spliceCheckpointSubtree(rootTreeHash plumbing.Hash, checkpointID id.CheckpointID, basePath string, entries map[string]object.TreeEntry) (plumbing.Hash, error) {
func (s *GitStore) spliceCheckpointSubtree(ctx context.Context, rootTreeHash plumbing.Hash, checkpointID id.CheckpointID, basePath string, entries map[string]object.TreeEntry) (plumbing.Hash, error) {
// Convert entries to relative paths (strip basePath prefix)
relEntries := make(map[string]object.TreeEntry, len(entries))
for path, entry := range entries {
Expand All @@ -163,7 +163,7 @@ func (s *GitStore) spliceCheckpointSubtree(rootTreeHash plumbing.Hash, checkpoin
}

// Build the checkpoint subtree from relative entries
checkpointTreeHash, err := BuildTreeFromEntries(s.repo, relEntries)
checkpointTreeHash, err := BuildTreeFromEntries(ctx, s.repo, relEntries)
if err != nil {
return plumbing.ZeroHash, fmt.Errorf("failed to build checkpoint subtree: %w", err)
}
Expand Down Expand Up @@ -461,7 +461,7 @@ func (s *GitStore) UpdateCheckpointSummary(ctx context.Context, checkpointID id.
return err //nolint:wrapcheck // Propagating context cancellation
}

if err := s.ensureSessionsBranch(); err != nil {
if err := s.ensureSessionsBranch(ctx); err != nil {
return fmt.Errorf("failed to ensure sessions branch: %w", err)
}

Expand Down Expand Up @@ -503,7 +503,7 @@ func (s *GitStore) UpdateCheckpointSummary(ctx context.Context, checkpointID id.
Hash: metadataHash,
}

newTreeHash, err := s.spliceCheckpointSubtree(rootTreeHash, checkpointID, basePath, entries)
newTreeHash, err := s.spliceCheckpointSubtree(ctx, rootTreeHash, checkpointID, basePath, entries)
if err != nil {
return err
}
Expand Down Expand Up @@ -1154,7 +1154,7 @@ func (s *GitStore) UpdateSummary(ctx context.Context, checkpointID id.Checkpoint
}

// Ensure sessions branch exists
if err := s.ensureSessionsBranch(); err != nil {
if err := s.ensureSessionsBranch(ctx); err != nil {
return fmt.Errorf("failed to ensure sessions branch: %w", err)
}

Expand Down Expand Up @@ -1217,7 +1217,7 @@ func (s *GitStore) UpdateSummary(ctx context.Context, checkpointID id.Checkpoint
}

// Build checkpoint subtree and splice into root (O(depth) tree surgery)
newTreeHash, err := s.spliceCheckpointSubtree(rootTreeHash, checkpointID, basePath, entries)
newTreeHash, err := s.spliceCheckpointSubtree(ctx, rootTreeHash, checkpointID, basePath, entries)
if err != nil {
return err
}
Expand Down Expand Up @@ -1252,7 +1252,7 @@ func (s *GitStore) UpdateCommitted(ctx context.Context, opts UpdateCommittedOpti
}

// Ensure sessions branch exists
if err := s.ensureSessionsBranch(); err != nil {
if err := s.ensureSessionsBranch(ctx); err != nil {
return fmt.Errorf("failed to ensure sessions branch: %w", err)
}

Expand Down Expand Up @@ -1336,7 +1336,7 @@ func (s *GitStore) UpdateCommitted(ctx context.Context, opts UpdateCommittedOpti
}

// Build checkpoint subtree and splice into root (O(depth) tree surgery)
newTreeHash, err := s.spliceCheckpointSubtree(rootTreeHash, opts.CheckpointID, basePath, entries)
newTreeHash, err := s.spliceCheckpointSubtree(ctx, rootTreeHash, opts.CheckpointID, basePath, entries)
if err != nil {
return err
}
Expand Down Expand Up @@ -1405,15 +1405,15 @@ func (s *GitStore) replaceTranscript(ctx context.Context, transcript []byte, age
}

// ensureSessionsBranch ensures the entire/checkpoints/v1 branch exists.
func (s *GitStore) ensureSessionsBranch() error {
func (s *GitStore) ensureSessionsBranch(ctx context.Context) error {
refName := plumbing.NewBranchReferenceName(paths.MetadataBranchName)
_, err := s.repo.Reference(refName, true)
if err == nil {
return nil // Branch exists
}

// Create orphan branch with empty tree
emptyTreeHash, err := BuildTreeFromEntries(s.repo, make(map[string]object.TreeEntry))
emptyTreeHash, err := BuildTreeFromEntries(ctx, s.repo, make(map[string]object.TreeEntry))
if err != nil {
return err
}
Expand Down
Loading
Loading