-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincremental.go
More file actions
282 lines (261 loc) · 8.63 KB
/
Copy pathincremental.go
File metadata and controls
282 lines (261 loc) · 8.63 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package repomap
import (
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
"slices"
"time"
)
// incrementalThreshold is the max fraction of total files that can change before
// we give up and do a full rebuild. Past this, the bookkeeping (rank re-seed,
// importer-count re-scan) stops being cheaper than parsing everything.
const incrementalThreshold = 0.30
// LoadCacheIncremental attempts a fast-path rebuild. Returns (true, changedRel)
// when the cache is valid for incremental use and the caller should merge
// `changedRel` (relative paths of added+modified files; deletions handled via
// side channel). Returns (false, nil) for any of:
// - cache missing / corrupt / wrong version / wrong root
// - cache was written for a non-git root (GitRoot=false)
// - `git rev-parse HEAD` fails or returns empty
// - diff between LastSHA and HEAD fails (e.g., SHA pruned by rebase)
// - change set exceeds incrementalThreshold of total files
//
// On (true, changedRel) the Map has been hydrated with the cached state and
// its mtimes map is populated. Deleted paths have already been removed from
// m.ranked. The caller is expected to re-parse changedRel, splice the results
// back in, re-rank, re-budget, and SaveCache.
func (m *Map) LoadCacheIncremental(ctx context.Context, cacheDir string) (bool, []string) {
path := cachePath(cacheDir, m.root)
data, err := os.ReadFile(path)
if err != nil {
return false, nil
}
var entry diskCache
if err := json.Unmarshal(data, &entry); err != nil {
return false, nil
}
if !m.cacheEntryValid(&entry) {
return false, nil
}
if !entry.GitRoot || entry.LastSHA == "" {
return false, nil
}
if !isInsideGitRepo(m.root) {
return false, nil
}
headSHA, err := gitHeadSHA(ctx, m.root)
if err != nil || headSHA == "" {
return false, nil
}
// Fast path: HEAD hasn't moved AND no worktree/untracked changes. Whole
// cache is authoritative.
if headSHA == entry.LastSHA {
added, modified, deleted, diffErr := gitChangedFiles(ctx, m.root, entry.LastSHA)
if diffErr != nil {
return false, nil
}
if len(added) == 0 && len(modified) == 0 && len(deleted) == 0 {
m.hydrateFromCache(entry)
return true, nil
}
// HEAD unchanged but worktree dirty — treat those as the change set.
return m.prepareIncremental(entry, added, modified, deleted)
}
added, modified, deleted, err := gitChangedFiles(ctx, m.root, entry.LastSHA)
if err != nil {
return false, nil
}
return m.prepareIncremental(entry, added, modified, deleted)
}
// prepareIncremental applies the eligibility threshold and, if the change set
// is small enough, hydrates m from the cache with deletions already pruned.
// Returns (true, changedRelPaths) where changedRel = added ∪ modified.
func (m *Map) prepareIncremental(entry diskCache, added, modified, deleted []string) (bool, []string) {
total := len(entry.Ranked)
changeCount := len(added) + len(modified) + len(deleted)
if total == 0 {
return false, nil
}
if float64(changeCount)/float64(total) > incrementalThreshold {
return false, nil
}
m.hydrateFromCache(entry)
m.dropPaths(slices.Concat(deleted, modified))
changed := make([]string, 0, len(added)+len(modified))
changed = append(changed, added...)
changed = append(changed, modified...)
changed = dedupePaths(changed)
return true, changed
}
// hydrateFromCache populates the Map from the deserialized disk entry.
// Must be called with m.mu NOT held.
func (m *Map) hydrateFromCache(entry diskCache) {
m.mu.Lock()
m.ranked = entry.Ranked
m.builtAt = entry.BuiltAt
m.mtimes = entry.Mtimes
m.contentHashes = entry.ContentHashes // nil for old caches → mtime-only fallback
m.scanFP = entry.ScanFP
m.coverage = entry.Coverage
m.semanticCallers = entry.SemanticCallers
m.goDiagnostics = entry.GoDiagnostics
// One source of truth for derived-output invalidation: reset() clears every
// format (including ones added later), then the two cache-persisted strings
// are restored.
m.outputs.reset()
m.outputs.compact = &entry.Output
m.outputs.lines = &entry.OutputLines
m.mu.Unlock()
}
// applyIncremental re-parses only the changed paths, merges them into the
// already-hydrated m.ranked, re-detects implementations over the full merged
// set, re-ranks, and saves the cache. Returns an error if re-parsing fails
// entirely; caller must fall through to a full rebuild.
func (m *Map) applyIncremental(ctx context.Context, changedRel []string) error {
for _, path := range changedRel {
ext := filepath.Ext(path)
base := filepath.Base(path)
if ext == ".go" || base == "go.mod" || base == "go.sum" || base == "go.work" {
return errors.New("Go semantic input changed; full analysis required")
}
}
if len(changedRel) == 0 {
// Nothing to re-parse — cache is authoritative. Still refresh builtAt
// and save so LastSHA advances if HEAD moved without touching tracked
// files (rare but possible). No fingerprint change needed — hydrated
// ScanFP already matches the tree as it stands.
m.mu.Lock()
m.builtAt = time.Now()
m.mu.Unlock()
if m.cacheDir != "" {
_ = m.SaveCacheContext(ctx, m.cacheDir)
}
return nil
}
// Build FileInfo list for changed paths that still exist and have a
// recognised language. Silently skip unknown extensions / missing files —
// deletions are already applied to m.ranked by LoadCacheIncremental.
infos := make([]FileInfo, 0, len(changedRel))
for _, rel := range changedRel {
abs := m.absPath(rel)
info, err := os.Stat(abs)
if err != nil {
continue // deleted or unreadable — drop silently
}
if info.IsDir() {
continue
}
if tooBig(abs, m.config.MaxFileSize) || isBuildArtifact(rel) || inSkipDir(rel) {
continue
}
lang := LanguageFor(filepath.Ext(rel))
if lang == "" {
continue
}
infos = append(infos, FileInfo{Path: rel, Language: lang})
}
var parsed []*FileSymbols
var newMtimes map[string]time.Time
var newHashes map[string]string
if len(infos) > 0 {
var err error
parsed, newMtimes, newHashes, _, err = m.parseFiles(ctx, infos)
if err != nil {
return err
}
}
// Build a set of paths being replaced so we can skip them from cached ranked.
relNew := make(map[string]struct{}, len(parsed))
for _, fs := range parsed {
if fs != nil {
relNew[fs.Path] = struct{}{}
}
}
m.mu.Lock()
// Carry forward existing RankedFiles (modified paths were already dropped
// from m.ranked by LoadCacheIncremental.dropPaths; this is defensive).
existing := make([]*FileSymbols, 0, len(m.ranked)+len(parsed))
for _, rf := range m.ranked {
if rf.FileSymbols == nil {
continue
}
if _, re := relNew[rf.Path]; re {
continue // replaced by freshly parsed version (defensive)
}
existing = append(existing, rf.FileSymbols)
}
for _, fs := range parsed {
if fs != nil {
existing = append(existing, fs)
}
}
// Refresh mtimes and hashes for newly parsed files.
if m.mtimes == nil {
m.mtimes = make(map[string]time.Time, len(existing))
}
for path, t := range newMtimes {
m.mtimes[path] = t
}
if len(newHashes) > 0 {
if m.contentHashes == nil {
m.contentHashes = make(map[string]string, len(newHashes))
}
for path, h := range newHashes {
m.contentHashes[path] = h
}
}
m.mu.Unlock()
// Go inputs force a full rebuild above, so cached semantic implementation
// relationships remain authoritative during a non-Go incremental merge.
ranked := RankFiles(existing)
ranked = m.applyRankPasses(ranked)
// The file set may have changed; refresh the fingerprint from a real scan
// so Stale() compares against exactly what a rebuild would discover.
// On scan failure keep the old fingerprint — a later false-stale just
// triggers a full rebuild, which self-heals.
newFP := ""
if files, scanErr := scanFilesLimited(ctx, m.root, m.blocklist, m.config.MaxFileSize); scanErr == nil {
newFP = scanFingerprint(files)
}
m.mu.Lock()
m.ranked = ranked
m.builtAt = time.Now()
m.coverage = parseCoverageFromRanked(ranked, m.tsAvailable, m.ctagsAvailable)
if newFP != "" {
m.scanFP = newFP
}
m.outputs.reset()
m.mu.Unlock()
if m.cacheDir != "" {
_ = m.SaveCacheContext(ctx, m.cacheDir)
}
return nil
}
// dropPaths removes entries with matching FileSymbols.Path (relative) from
// m.ranked and m.mtimes. Caller has NOT yet re-ranked — this is pre-merge
// cleanup.
func (m *Map) dropPaths(relPaths []string) {
if len(relPaths) == 0 {
return
}
drop := make(map[string]struct{}, len(relPaths))
for _, p := range relPaths {
drop[p] = struct{}{}
}
m.mu.Lock()
defer m.mu.Unlock()
kept := m.ranked[:0]
for _, rf := range m.ranked {
if rf.FileSymbols == nil {
continue
}
if _, remove := drop[rf.Path]; remove {
delete(m.mtimes, joinAbs(m.root, rf.Path))
continue
}
kept = append(kept, rf)
}
m.ranked = kept
}