From e64325b64eb0918b4f82fa8fc6d88ca5dceae63e Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Sat, 28 Nov 2026 04:14:35 +0000 Subject: [PATCH] Probe pipeline output and cache destinations --- docs/roadmap/implementation.md | 2 + internal/cli/destination_probe.go | 53 +++++++++++++++++++ internal/cli/destination_probe_test.go | 70 ++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 internal/cli/destination_probe.go create mode 100644 internal/cli/destination_probe_test.go diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 7db2282f..a1affef0 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -267,6 +267,8 @@ does not include filesystem destinations or command/report wiring. ## Stage 5: Implement Destination Access Probes +**Status:** Complete. + **Goal:** Check only invocation-relevant filesystem access, without cache effects. **Inspect:** Existing output/file helpers, cache-mode resolution, configured roots, diff --git a/internal/cli/destination_probe.go b/internal/cli/destination_probe.go new file mode 100644 index 00000000..af9bb67e --- /dev/null +++ b/internal/cli/destination_probe.go @@ -0,0 +1,53 @@ +package cli + +import ( + "errors" + "fmt" + "os" + + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" + "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" +) + +// probeDestination checks practical write access without publishing an artifact. +func probeDestination(root string, mode os.FileMode) error { + if err := os.MkdirAll(root, mode); err != nil { + return fmt.Errorf("create destination %q: %w", root, err) + } + file, err := os.CreateTemp(root, ".notarius-probe-*") + if err != nil { + return fmt.Errorf("create access probe in %q: %w", root, err) + } + _, writeErr := file.Write([]byte("probe")) + closeErr := file.Close() + removeErr := os.Remove(file.Name()) + if err := errors.Join(writeErr, closeErr, removeErr); err != nil { + return fmt.Errorf("access probe in %q: %w", root, err) + } + return nil +} + +func pipelineUsesCache(resolved pipeline.ResolvedPipeline) bool { + uses := func(class contracts.ExecutionClass, controls contracts.ProducerControls) bool { + return class == contracts.ExecutionClassLLMBacked && controls.Cache != contracts.CacheBypass + } + if uses(resolved.ChunkExecutionClass, resolved.ChunkControls) { + return true + } + for _, step := range resolved.Steps { + for _, lane := range step.ArtifactLanes { + if uses(lane.ExtractExecutionClass, lane.ExtractControls) || uses(lane.MergeExecutionClass, lane.MergeControls) || uses(lane.NormalizeExecutionClass, lane.NormalizeControls) { + return true + } + } + } + return false +} + +func probePipelineDestinations(outputRoot, cacheRoot string, resolved pipeline.ResolvedPipeline) (outputErr, cacheErr error) { + outputErr = probeDestination(outputRoot, 0o755) + if outputErr == nil && pipelineUsesCache(resolved) { + cacheErr = probeDestination(cacheRoot, 0o777) + } + return outputErr, cacheErr +} diff --git a/internal/cli/destination_probe_test.go b/internal/cli/destination_probe_test.go new file mode 100644 index 00000000..489e088a --- /dev/null +++ b/internal/cli/destination_probe_test.go @@ -0,0 +1,70 @@ +package cli + +import ( + "os" + "path/filepath" + "testing" + + "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" + "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" +) + +func TestDestinationProbesPreserveCacheAndCleanUp(t *testing.T) { + root := t.TempDir() + output, cache := filepath.Join(root, "output"), filepath.Join(root, "cache") + if err := os.Mkdir(cache, 0o755); err != nil { + t.Fatal(err) + } + entry := filepath.Join(cache, "accepted.json") + if err := os.WriteFile(entry, []byte(`{"accepted":true}`), 0o600); err != nil { + t.Fatal(err) + } + resolved := pipeline.ResolvedPipeline{ChunkExecutionClass: contracts.ExecutionClassLLMBacked} + for _, mode := range []contracts.CacheMode{contracts.CacheAuto, contracts.CacheRefresh} { + resolved.ChunkControls.Cache = mode + if outputErr, cacheErr := probePipelineDestinations(output, cache, resolved); outputErr != nil || cacheErr != nil { + t.Fatalf("%s: output %v, cache %v", mode, outputErr, cacheErr) + } + content, err := os.ReadFile(entry) + if err != nil || string(content) != `{"accepted":true}` { + t.Fatalf("cache entry = %q, %v", content, err) + } + entries, err := os.ReadDir(cache) + if err != nil || len(entries) != 1 { + t.Fatalf("cache directory = %v, %v", entries, err) + } + entries, err = os.ReadDir(output) + if err != nil || len(entries) != 0 { + t.Fatalf("output directory = %v, %v", entries, err) + } + } +} + +func TestDestinationProbesDistinguishRequiredAndOptionalAccess(t *testing.T) { + root := t.TempDir() + file := filepath.Join(root, "file") + if err := os.WriteFile(file, []byte("x"), 0o600); err != nil { + t.Fatal(err) + } + bad := filepath.Join(file, "child") + resolved := pipeline.ResolvedPipeline{ChunkExecutionClass: contracts.ExecutionClassLLMBacked, ChunkControls: contracts.ProducerControls{Cache: contracts.CacheAuto}} + if outputErr, cacheErr := probePipelineDestinations(bad, bad, resolved); outputErr == nil || cacheErr != nil { + t.Fatalf("output=%v cache=%v", outputErr, cacheErr) + } + if outputErr, cacheErr := probePipelineDestinations(filepath.Join(root, "output"), bad, resolved); outputErr != nil || cacheErr == nil { + t.Fatalf("output=%v cache=%v", outputErr, cacheErr) + } + resolved.ChunkControls.Cache = contracts.CacheBypass + if outputErr, cacheErr := probePipelineDestinations(filepath.Join(root, "output"), bad, resolved); outputErr != nil || cacheErr != nil { + t.Fatalf("bypass: output=%v cache=%v", outputErr, cacheErr) + } + resolved.ChunkControls.Cache = contracts.CacheAuto + resolved.ChunkExecutionClass = contracts.ExecutionClassDeterministic + if outputErr, cacheErr := probePipelineDestinations(filepath.Join(root, "output"), bad, resolved); outputErr != nil || cacheErr != nil { + t.Fatalf("deterministic: output=%v cache=%v", outputErr, cacheErr) + } + resolved.Steps = []pipeline.ResolvedPipelineStep{{ArtifactLanes: []pipeline.ResolvedArtifactLane{{NormalizeExecutionClass: contracts.ExecutionClassLLMBacked, NormalizeControls: contracts.ProducerControls{Cache: contracts.CacheRefresh}}}}} + if outputErr, cacheErr := probePipelineDestinations(filepath.Join(root, "output"), bad, resolved); outputErr != nil || cacheErr == nil { + t.Fatalf("selected model lane: output=%v cache=%v", outputErr, cacheErr) + } +}