From 80148ef7e42b1889d32437a8ff849d66d266625f Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Sun, 22 Nov 2026 09:42:06 +0000 Subject: [PATCH] Share a lazy source index across cache hits --- docs/roadmap/remediation.md | 5 +++ internal/framework/pipeline/runner.go | 6 +++ internal/framework/pipeline/runner_cache.go | 16 +++++-- .../framework/pipeline/runner_cache_test.go | 42 +++++++++++++++++++ 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/docs/roadmap/remediation.md b/docs/roadmap/remediation.md index 68f1ab7c..8d2df138 100644 --- a/docs/roadmap/remediation.md +++ b/docs/roadmap/remediation.md @@ -378,6 +378,11 @@ wall-clock thresholds or production data. ## Finding 7: Share A Lazy Source Index Across Artifact Cache Decoders +**Status: Complete.** Run-scoped decoders share a concurrency-safe lazy immutable +index, acquired only when a decoded hit has evidence to check. Concurrent-hit +runner coverage and the pipeline race suite pass; cache modes and usability +checks remain unchanged. + Priority: P2. Relevant stages: 11, 21–22, and 27–28. ### Evidence And Complexity diff --git a/internal/framework/pipeline/runner.go b/internal/framework/pipeline/runner.go index 2c548ca1..8683055d 100644 --- a/internal/framework/pipeline/runner.go +++ b/internal/framework/pipeline/runner.go @@ -10,6 +10,7 @@ import ( "path" "path/filepath" "strings" + "sync" "time" "gitea.maximumdirect.net/eric/notarius/internal/core/source" @@ -70,6 +71,8 @@ type RunInput struct { stepID string references map[referenceTargetKey]contracts.ReferenceSet referenceReuseEligibility map[referenceTargetKey]bool + // Shared by operation decoders, but built only when a cache hit needs it. + cacheSourceIndex func() source.DocumentIndex } type RunOutput struct { @@ -182,6 +185,9 @@ func (r *Runner) Run(ctx context.Context, input RunInput) (output RunOutput, err if err := source.ValidateDocument(doc); err != nil { return failOutput(output), fmt.Errorf("validate source document: %w", err) } + input.cacheSourceIndex = sync.OnceValue(func() source.DocumentIndex { + return source.NewDocumentIndex(doc) + }) if err := writeDebugTimed(debugRecorder, "source/output.json", debugTimedEnvelope{ Stage: "source", diff --git a/internal/framework/pipeline/runner_cache.go b/internal/framework/pipeline/runner_cache.go index fc2b38f3..fdbe0623 100644 --- a/internal/framework/pipeline/runner_cache.go +++ b/internal/framework/pipeline/runner_cache.go @@ -3,6 +3,7 @@ package pipeline import ( "context" "fmt" + "sync" "gitea.maximumdirect.net/eric/notarius/internal/core/source" "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" @@ -12,7 +13,11 @@ func artifactProducerCache(input RunInput, typed *preparedTypedLane, class contr if input.Cache == nil || class != contracts.ExecutionClassLLMBacked { return nil } - index := source.NewDocumentIndex(doc) + sourceIndex := input.cacheSourceIndex + if sourceIndex == nil { + // Standalone operation construction can also defer indexing until decode. + sourceIndex = sync.OnceValue(func() source.DocumentIndex { return source.NewDocumentIndex(doc) }) + } return &contracts.ProducerCache[any]{ Store: input.Cache, Encode: typed.codec.encode, @@ -26,9 +31,12 @@ func artifactProducerCache(input RunInput, typed *preparedTypedLane, class contr if err != nil { return nil, err } - for _, ref := range refs { - if err := index.ValidateRef(ref); err != nil { - return nil, fmt.Errorf("cached artifact source reference: %w", err) + if len(refs) > 0 { + index := sourceIndex() + for _, ref := range refs { + if err := index.ValidateRef(ref); err != nil { + return nil, fmt.Errorf("cached artifact source reference: %w", err) + } } } } diff --git a/internal/framework/pipeline/runner_cache_test.go b/internal/framework/pipeline/runner_cache_test.go index 8dc4b9d7..bb5b7926 100644 --- a/internal/framework/pipeline/runner_cache_test.go +++ b/internal/framework/pipeline/runner_cache_test.go @@ -6,6 +6,7 @@ import ( "errors" "reflect" "strings" + "sync/atomic" "testing" "gitea.maximumdirect.net/eric/notarius/internal/core/source" @@ -13,6 +14,47 @@ import ( "go.maximumdirect.net/promptkit" ) +func TestRunnerChecksCurrentSourceOnConcurrentCacheHits(t *testing.T) { + _, cache := filesystemProducerCache(t) + var calls atomic.Int32 + engine := nativeProducerEngine(t, producerClientFunc(func(context.Context, promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) { + calls.Add(1) + return &promptkit.GenerateResponse{Content: `{"value":1}`}, nil + })) + const chunkCount = 4 + prepared := preparedConcurrentPipeline(t, chunkCount) + for i := range prepared.Steps[0].lanes { + lane := &prepared.Steps[0].lanes[i] + if lane.resolved.ArtifactKind != "test/notes" { + continue + } + lane.resolved.ExtractExecutionClass = contracts.ExecutionClassLLMBacked + lane.typed.cacheEvidence = func(any) ([]source.SourceRef, error) { + return []source.SourceRef{{SourceID: "source", StartUnitID: 1, EndUnitID: 1}}, nil + } + lane.typed.extract = func(ctx context.Context, _ any, request contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) { + initial := nativeProducerInitial() + initial.Inputs["source"] = promptkit.Inline(string(request.Chunk.Content)) + value, outcome, err := RunProducer(ctx, engine, request.Operation, initial, + func(context.Context, *promptkit.RunResult) (*any, []string, error) { + var value any = codecNotes{Items: []string{"accepted"}} + return &value, nil, nil + }, nil, nil) + return erasedTypedResult{Value: value, Outcome: outcome}, err + } + } + for run := 0; run < 2; run++ { + // The filesystem store is concurrency-safe; its counting test wrapper is not. + output, err := New().Run(context.Background(), RunInput{Prepared: prepared, Cache: cache.inner, RawInput: []byte("input"), Workers: chunkCount}) + if err != nil || len(output.normalizeOutputs) != 2 { + t.Fatalf("run %d: outputs=%d error=%v", run, len(output.normalizeOutputs), err) + } + } + if calls.Load() != chunkCount { + t.Fatalf("cache hits executed model work: calls=%d", calls.Load()) + } +} + func TestRunnerReusesAcceptedPredecessorAfterDownstreamFailure(t *testing.T) { _, store := filesystemProducerCache(t) probe := newRunnerProbe()