-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
138 lines (123 loc) · 3.48 KB
/
cache.go
File metadata and controls
138 lines (123 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package repomap
import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
)
// outputCache lazily computes and caches formatted output strings.
type outputCache struct {
compact *string // enriched default (m.String())
verbose *string
detail *string
lines *string
xml *string
orientation *string // lean compact / orientation mode (m.StringCompact())
}
func (c *outputCache) get(ptr **string, fn func() string) string {
if *ptr == nil {
s := fn()
*ptr = &s
}
return **ptr
}
func (c *outputCache) reset() {
c.compact = nil
c.verbose = nil
c.detail = nil
c.lines = nil
c.xml = nil
c.orientation = nil
}
// diskCache is the on-disk format for a cached repomap build.
type diskCache struct {
Version int `json:"version"`
Root string `json:"root"`
BuiltAt time.Time `json:"built_at"`
Mtimes map[string]time.Time `json:"mtimes"`
Output string `json:"output"`
OutputLines string `json:"output_lines"`
Ranked []RankedFile `json:"ranked"`
LastSHA string `json:"last_sha,omitempty"` // HEAD sha at write time; "" when not a git repo
GitRoot bool `json:"git_root,omitempty"` // true if root was inside a git repo at write time
}
const cacheVersion = 6
// SaveCache writes the current map state to disk.
func (m *Map) SaveCache(cacheDir string) error {
m.mu.Lock()
if m.builtAt.IsZero() || len(m.ranked) == 0 {
m.mu.Unlock()
return nil
}
// Compute lazy strings if not yet cached, so they are persisted.
compact := m.outputs.get(&m.outputs.compact, func() string {
return FormatMap(m.ranked, m.config.MaxTokens, false, false)
})
lines := m.outputs.get(&m.outputs.lines, func() string {
return FormatLines(m.ranked, m.config.MaxTokensNoCtx, m.root)
})
entry := diskCache{
Version: cacheVersion,
Root: m.root,
BuiltAt: m.builtAt,
Mtimes: m.mtimes,
Output: compact,
OutputLines: lines,
Ranked: m.ranked,
}
if isInsideGitRepo(m.root) {
entry.GitRoot = true
if sha, err := gitHeadSHA(context.Background(), m.root); err == nil {
entry.LastSHA = sha
}
}
m.mu.Unlock()
data, err := json.Marshal(&entry)
if err != nil {
return fmt.Errorf("marshal cache: %w", err)
}
path := cachePath(cacheDir, m.root)
if err := atomicWriteFile(path, data, 0o644); err != nil {
return fmt.Errorf("write cache: %w", err)
}
return nil
}
// LoadCache loads a previously saved map from disk. Returns false if
// the cache is missing, corrupt, or for a different version.
func (m *Map) LoadCache(cacheDir string) bool {
path := cachePath(cacheDir, m.root)
data, err := os.ReadFile(path)
if err != nil {
return false
}
var entry diskCache
if err := json.Unmarshal(data, &entry); err != nil {
return false
}
if entry.Version != cacheVersion || entry.Root != m.root {
return false
}
m.mu.Lock()
m.ranked = entry.Ranked
m.outputs.compact = &entry.Output
m.outputs.lines = &entry.OutputLines
m.builtAt = entry.BuiltAt
m.mtimes = entry.Mtimes
m.mu.Unlock()
return true
}
// Dirty marks the map as needing a rebuild by zeroing builtAt,
// bypassing the stale debounce. Use after code-changing tool calls.
func (m *Map) Dirty() {
m.mu.Lock()
m.builtAt = time.Time{}
m.mu.Unlock()
}
func cachePath(cacheDir, root string) string {
h := sha256.Sum256([]byte(root))
name := fmt.Sprintf("repomap-%x.json", h[:8])
return filepath.Join(cacheDir, name)
}