From 1f8bb6d025f772a8e5cd039d50433ec9f3bdaf3a Mon Sep 17 00:00:00 2001 From: lambertpw Date: Sun, 22 Nov 2026 16:24:30 -0500 Subject: [PATCH] fix(core): exclude the whole cache root from artifact discovery, not one app's subdirectory With the cache inside the project (-c ./cache), each application writes its analysis_cache.json beneath identity-v2-/. New() replaced the cache directory with that per-application subdirectory, and the repository layer was given only the subdirectory. So each run excluded its own cache and nothing else: run B recorded run A's identity-v2-/analysis_cache.json as a project artifact. With --artifact-text its full source, another application's analysis, was written into B's analysis.json. The Analyzer now keeps the cache root as the user gave it, and the repository layer excludes all of it. Every application's subdirectory is covered, including ones written by applications not yet analyzed. The cache root is the tool's own location, so nothing under it is project source. A root equal to the project directory is still not excluded (the walk never skips its own root). Test: TestAnalyze_CacheRootInsideProjectIsNotAnArtifact runs two applications against one in-project cache and fails on the old code (appB records cache/identity-v2-/analysis_cache.json as an artifact). Co-Authored-By: Claude Haiku 5.5 --- internal/core/analyzer.go | 11 ++++-- internal/core/cache_leak_test.go | 58 ++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 internal/core/cache_leak_test.go diff --git a/internal/core/analyzer.go b/internal/core/analyzer.go index b9dbf34..cef7c05 100644 --- a/internal/core/analyzer.go +++ b/internal/core/analyzer.go @@ -44,6 +44,10 @@ import ( // Analyzer is the top-level analysis driver. Construct with New() and call Analyze(). type Analyzer struct { opts options.AnalysisOptions + // cacheRoot is the cache directory as the user gave it, before the + // per-application identity-v2- suffix. The repository layer excludes all + // of it, so no application's cache is recorded as a project artifact. + cacheRoot string // l3 is the Phase 5 side table (nil below L3). It stays off // schema.GoApplication: schema must not depend on dataflow, and the // cached v1 model has no L3 facts. @@ -85,11 +89,14 @@ func resolveIdentity(opts options.AnalysisOptions) (v2.Identity, error) { // New creates an Analyzer with a cache isolated by canonical application identity. func New(opts options.AnalysisOptions) *Analyzer { + // The root is kept before the per-application suffix: it is the tool's own cache + // location, and every application's subdirectory lives beneath it. + cacheRoot := opts.CacheDir if identity, err := resolveIdentity(opts); err == nil && opts.CacheDir != "" { key := sha256.Sum256([]byte(identity.ApplicationID)) opts.CacheDir = filepath.Join(opts.CacheDir, fmt.Sprintf("identity-v2-%x", key)) } - return &Analyzer{opts: opts} + return &Analyzer{opts: opts, cacheRoot: cacheRoot} } // Analyze runs the full analysis pipeline and returns a GoApplication. @@ -103,7 +110,7 @@ func (a *Analyzer) Analyze() (*schema.GoApplication, error) { facts, err := repository.Facts(a.opts.InputPath, app, repository.Options{ CaptureText: a.opts.ArtifactText, OutputDir: a.opts.OutputDir, - CacheDir: a.opts.CacheDir, + CacheDir: a.cacheRoot, }) if err != nil { return nil, fmt.Errorf("repository layer: %w", err) diff --git a/internal/core/cache_leak_test.go b/internal/core/cache_leak_test.go new file mode 100644 index 0000000..548f388 --- /dev/null +++ b/internal/core/cache_leak_test.go @@ -0,0 +1,58 @@ +package core_test + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "github.com/codellm-devkit/codeanalyzer-go/internal/core" + "github.com/codellm-devkit/codeanalyzer-go/internal/options" +) + +// The cache directory lives inside the project (-c ./cache). Each application +// writes its own identity-v2-/analysis_cache.json beneath it. Before the +// fix, the repository layer excluded only the current application's subdirectory, +// so run B recorded run A's cache as an artifact, with its full source under +// --artifact-text. Those bytes are another application's analysis. +// +// Practical shape of the project: +// +// project/ +// go.mod main.go +// cache/ +// identity-v2-/analysis_cache.json <- written by run A +// identity-v2-/analysis_cache.json <- written by run B +func TestAnalyze_CacheRootInsideProjectIsNotAnArtifact(t *testing.T) { + dir := t.TempDir() + files := map[string]string{ + "go.mod": "module example.com/cl\n\ngo 1.22\n", + "main.go": "package main\n\nfunc main() {}\n", + } + for rel, content := range files { + if err := os.WriteFile(filepath.Join(dir, rel), []byte(content), 0o644); err != nil { + t.Fatal(err) + } + } + cache := filepath.Join(dir, "cache") + + // Run A, then run B: B is the run whose output must not carry A's cache. + for _, app := range []string{"appA", "appB"} { + opts := options.AnalysisOptions{ + InputPath: dir, AppName: app, Level: options.LevelSymbolTable, + SkipTests: true, CacheDir: cache, OutputDir: t.TempDir(), ArtifactText: true, + } + got, err := core.New(opts).Analyze() + if err != nil { + t.Fatalf("%s: %v", app, err) + } + for path := range got.Repository.Artifacts { + if strings.HasPrefix(path, "cache/") || strings.Contains(path, "analysis_cache.json") { + t.Errorf("%s recorded cache file %q as a project artifact", app, path) + } + } + if _, ok := got.Repository.Artifacts["go.mod"]; !ok { + t.Errorf("%s: go.mod missing from artifacts; the project itself must still be discovered", app) + } + } +}