Skip to content

Commit 41f986f

Browse files
Fix Copilot review issues
- store.go: Init header before append, validate version in Load - builder.go: Use cpID.Path() for checkpoint root directory - list.go/show.go/index_cmd.go: Use paths.WorktreeRoot() for repo root - index_cmd.go: Implement actual rebuild logic - search.go: Fix Use string to match Args requirement Entire-Checkpoint: 3e555fd3bee4
1 parent d8c6de2 commit 41f986f

6 files changed

Lines changed: 63 additions & 11 deletions

File tree

cmd/entire/cli/prompts/index/builder.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8-
"path/filepath"
98
"strconv"
109
"strings"
1110
"time"
@@ -163,9 +162,7 @@ func walkCheckpointShards(repo *git.Repository, treeHash plumbing.Hash, fn func(
163162
}
164163

165164
func (b *Builder) loadCheckpoint(cpID id.CheckpointID) ([]Entry, error) {
166-
shard := cpID.String()[:2]
167-
rest := cpID.String()[2:]
168-
cpDir := filepath.Join(shard, rest, "0")
165+
cpDir := cpID.Path()
169166

170167
ref, err := b.repo.Reference(plumbing.NewBranchReferenceName(paths.MetadataBranchName), true)
171168
if err != nil {
@@ -211,7 +208,7 @@ func (b *Builder) loadCheckpoint(cpID id.CheckpointID) ([]Entry, error) {
211208

212209
entries := make([]Entry, 0)
213210
for i := range metadata.Sessions {
214-
sessionDir := filepath.Join(cpDir, strconv.Itoa(i))
211+
sessionDir := strconv.Itoa(i)
215212
sessionTree, err := cpTree.Tree(sessionDir)
216213
if err != nil {
217214
continue

cmd/entire/cli/prompts/index/store.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ func (s *Store) Load(_ context.Context) ([]Entry, error) {
8181
if err := json.Unmarshal([]byte(line), &header); err != nil {
8282
return nil, fmt.Errorf("%w: header: %w", ErrIndexCorrupt, err)
8383
}
84+
if header.Version <= 0 {
85+
return nil, fmt.Errorf("%w: header: invalid version %d", ErrIndexCorrupt, header.Version)
86+
}
87+
if header.Version > CurrentIndexVersion {
88+
return nil, ErrIndexVersionNewer
89+
}
8490
} else {
8591
var entry Entry
8692
if err := json.Unmarshal([]byte(line), &entry); err != nil {
@@ -138,6 +144,13 @@ func (s *Store) AppendEntries(entries []Entry) error {
138144
}
139145

140146
func (s *Store) appendEntriesLine(entries []Entry) error {
147+
fi, err := os.Stat(s.indexPath)
148+
if err != nil || fi.Size() == 0 {
149+
if err := s.InitIndex(); err != nil {
150+
return fmt.Errorf("initializing index: %w", err)
151+
}
152+
}
153+
141154
f, err := os.OpenFile(s.indexPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600)
142155
if err != nil {
143156
return fmt.Errorf("opening index for append: %w", err)

cmd/entire/cli/prompts/index_cmd.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package prompts
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78

9+
"github.com/entireio/cli/cmd/entire/cli/paths"
810
"github.com/entireio/cli/cmd/entire/cli/prompts/index"
11+
"github.com/entireio/cli/cmd/entire/cli/strategy"
912
"github.com/spf13/cobra"
1013
)
1114

@@ -40,14 +43,39 @@ Examples:
4043
func runIndex(ctx context.Context, w io.Writer, ew io.Writer, rebuild, status, verify bool) error {
4144
_ = ew
4245

46+
repoRoot, err := paths.WorktreeRoot(ctx)
47+
if err != nil {
48+
return errors.New("not a git repository")
49+
}
50+
4351
if rebuild {
44-
fmt.Fprintln(w, "Rebuilding index...")
45-
fmt.Fprintln(w, "(Use 'entire prompts search' to trigger automatic rebuild if index is missing)")
52+
repo, err := strategy.OpenRepository(ctx)
53+
if err != nil {
54+
return fmt.Errorf("opening repository: %w", err)
55+
}
56+
57+
store := index.NewStore(repoRoot)
58+
builder := index.NewBuilder(repo, store)
59+
60+
fmt.Fprintln(w, "Rebuilding prompt index from checkpoints...")
61+
62+
progressFn := func(done, total int) {
63+
if total > 0 {
64+
fmt.Fprintf(w, "\r %d / %d checkpoints", done, total)
65+
}
66+
}
67+
68+
if err := builder.Build(ctx, w, progressFn); err != nil {
69+
return fmt.Errorf("building index: %w", err)
70+
}
71+
72+
fmt.Fprintln(w, "")
73+
fmt.Fprintln(w, "Index rebuild complete.")
4674
return nil
4775
}
4876

4977
if status {
50-
store := index.NewStore("")
78+
store := index.NewStore(repoRoot)
5179
stats, err := store.Stats(ctx)
5280
if err != nil {
5381
return fmt.Errorf("getting stats: %w", err)
@@ -70,6 +98,7 @@ func runIndex(ctx context.Context, w io.Writer, ew io.Writer, rebuild, status, v
7098

7199
if verify {
72100
fmt.Fprintln(w, "Verifying index entries...")
101+
fmt.Fprintln(w, "(Use 'entire prompts search' to trigger automatic rebuild if index is missing)")
73102
return nil
74103
}
75104

cmd/entire/cli/prompts/list.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"strings"
99

10+
"github.com/entireio/cli/cmd/entire/cli/paths"
1011
"github.com/entireio/cli/cmd/entire/cli/prompts/index"
1112
"github.com/spf13/cobra"
1213
)
@@ -32,7 +33,12 @@ Examples:
3233
}
3334

3435
func runList(ctx context.Context, w io.Writer, _ io.Writer, limit int) error {
35-
store := index.NewStore("")
36+
repoRoot, err := paths.WorktreeRoot(ctx)
37+
if err != nil {
38+
return errors.New("not a git repository")
39+
}
40+
41+
store := index.NewStore(repoRoot)
3642

3743
if !store.Exists() {
3844
fmt.Fprintln(w, "No prompt index found. Run 'entire prompts index --rebuild' first.")

cmd/entire/cli/prompts/search.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func newSearchCmd() *cobra.Command {
2727
)
2828

2929
cmd := &cobra.Command{
30-
Use: "search [query]",
30+
Use: "search <query>",
3131
Short: "Search prompts from checkpoint history",
3232
Long: `Search prompts from your checkpoint history by keywords.
3333

cmd/entire/cli/prompts/show.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package prompts
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78

9+
"github.com/entireio/cli/cmd/entire/cli/paths"
810
"github.com/entireio/cli/cmd/entire/cli/prompts/index"
911
"github.com/spf13/cobra"
1012
)
@@ -28,7 +30,12 @@ Examples:
2830
}
2931

3032
func runShow(ctx context.Context, w io.Writer, cpIDPrefix string) error {
31-
store := index.NewStore("")
33+
repoRoot, err := paths.WorktreeRoot(ctx)
34+
if err != nil {
35+
return errors.New("not a git repository")
36+
}
37+
38+
store := index.NewStore(repoRoot)
3239
entries, err := store.Load(ctx)
3340
if err != nil {
3441
return fmt.Errorf("loading index: %w", err)

0 commit comments

Comments
 (0)