-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincremental.go
More file actions
331 lines (304 loc) · 10.5 KB
/
Copy pathincremental.go
File metadata and controls
331 lines (304 loc) · 10.5 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package repomap
import (
"context"
"encoding/json"
"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
// cacheLoadPlan is the internal result used by Build. exactHit means the cache
// already represents the precise HEAD+worktree contents and must not be
// rewritten merely to refresh its timestamp.
type cacheLoadPlan struct {
changed []string
persistMetadata bool
exactHit bool
}
// LoadCacheIncremental preserves the historical public fast-path API. Build
// consumes cacheLoadPlan directly so it can distinguish exact cache hits from
// incremental merges that need cache metadata persisted.
func (m *Map) LoadCacheIncremental(ctx context.Context, cacheDir string) (bool, []string) {
plan, ok := m.cacheLoadPlan(ctx, cacheDir)
if !ok {
return false, nil
}
return true, plan.changed
}
// cacheLoadPlan attempts a fast-path rebuild. It returns false for any of:
// - cache missing / corrupt / wrong version / wrong root
// - cache was written for a non-git root (GitRoot=false)
// - Git status cannot produce a complete HEAD+worktree snapshot
// - diff between LastSHA and HEAD fails (e.g., SHA pruned by rebase)
// - change set exceeds incrementalThreshold of total files
//
// On success the Map has been hydrated with cached state and deleted paths have
// already been removed from m.ranked for non-exact plans.
func (m *Map) cacheLoadPlan(ctx context.Context, cacheDir string) (cacheLoadPlan, bool) {
path := cachePath(cacheDir, m.root)
data, err := os.ReadFile(path)
if err != nil {
return cacheLoadPlan{}, false
}
var entry diskCache
if err := json.Unmarshal(data, &entry); err != nil {
return cacheLoadPlan{}, false
}
if !m.cacheEntryValid(&entry) {
return cacheLoadPlan{}, false
}
if !entry.GitRoot || entry.LastSHA == "" || entry.WorktreeDigest == "" {
return cacheLoadPlan{}, false
}
snapshot, err := m.gitStatusSnapshot(ctx)
if err != nil {
return cacheLoadPlan{}, false
}
if snapshot.headSHA == entry.LastSHA && snapshot.digest == entry.WorktreeDigest {
m.hydrateFromCache(entry)
return cacheLoadPlan{exactHit: true}, true
}
// A dirty cache cannot safely be composed with a changed worktree or a new
// HEAD: its cached symbols came from an unknown overlay rather than LastSHA.
if entry.WorktreeDigest != cacheCleanWorktreeDigest() {
return cacheLoadPlan{}, false
}
added, modified, deleted, err := m.snapshotChangedFiles(snapshot)
if err != nil {
return cacheLoadPlan{}, false
}
if snapshot.headSHA != entry.LastSHA {
committedAdded, committedModified, committedDeleted, diffErr := gitCommittedChangedFiles(ctx, m.root, entry.LastSHA)
if diffErr != nil {
return cacheLoadPlan{}, false
}
committedAdded, committedModified, committedDeleted = m.filterCacheRelevantChanges(committedAdded, committedModified, committedDeleted)
added = append(added, committedAdded...)
modified = append(modified, committedModified...)
deleted = append(deleted, committedDeleted...)
}
ok, changed := m.prepareIncremental(entry, dedupePaths(added), dedupePaths(modified), dedupePaths(deleted))
if !ok {
return cacheLoadPlan{}, false
}
return cacheLoadPlan{changed: changed, persistMetadata: true}, true
}
func (m *Map) filterCacheRelevantChanges(added, modified, deleted []string) ([]string, []string, []string) {
filter := func(paths []string) []string {
out := make([]string, 0, len(paths))
for _, path := range paths {
if m.cacheRelevantPath(path) {
out = append(out, path)
}
}
return out
}
return filter(added), filter(modified), filter(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) {
if containsGoSemanticInput(added, modified, deleted) {
return false, nil
}
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
}
// containsGoSemanticInput reports changes that invalidate the whole-program Go
// semantic graph. It runs before cache hydration so deleted callers cannot
// survive in the cached semantic caller projection.
func containsGoSemanticInput(changes ...[]string) bool {
for _, paths := range changes {
for _, path := range paths {
base := filepath.Base(path)
if filepath.Ext(path) == ".go" || base == "go.mod" || base == "go.sum" || base == "go.work" {
return true
}
}
}
return false
}
// 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, persistMetadata bool) error {
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 persistMetadata && 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).
// RankFiles mutates symbol metadata, so detach the carried-forward file
// symbols before releasing the lock rather than mutating published state.
cachedRanked := cloneRanked(m.ranked)
existing := make([]*FileSymbols, 0, len(m.ranked)+len(parsed))
for _, rf := range cachedRanked {
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 persistMetadata && 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
}