Share a lazy source index across cache hits
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user