Share a lazy source index across cache hits
This commit is contained in:
@@ -378,6 +378,11 @@ wall-clock thresholds or production data.
|
|||||||
|
|
||||||
## Finding 7: Share A Lazy Source Index Across Artifact Cache Decoders
|
## 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.
|
Priority: P2. Relevant stages: 11, 21–22, and 27–28.
|
||||||
|
|
||||||
### Evidence And Complexity
|
### Evidence And Complexity
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||||
@@ -70,6 +71,8 @@ type RunInput struct {
|
|||||||
stepID string
|
stepID string
|
||||||
references map[referenceTargetKey]contracts.ReferenceSet
|
references map[referenceTargetKey]contracts.ReferenceSet
|
||||||
referenceReuseEligibility map[referenceTargetKey]bool
|
referenceReuseEligibility map[referenceTargetKey]bool
|
||||||
|
// Shared by operation decoders, but built only when a cache hit needs it.
|
||||||
|
cacheSourceIndex func() source.DocumentIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
type RunOutput struct {
|
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 {
|
if err := source.ValidateDocument(doc); err != nil {
|
||||||
return failOutput(output), fmt.Errorf("validate source document: %w", err)
|
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{
|
if err := writeDebugTimed(debugRecorder, "source/output.json", debugTimedEnvelope{
|
||||||
Stage: "source",
|
Stage: "source",
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package pipeline
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
"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 {
|
if input.Cache == nil || class != contracts.ExecutionClassLLMBacked {
|
||||||
return nil
|
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]{
|
return &contracts.ProducerCache[any]{
|
||||||
Store: input.Cache,
|
Store: input.Cache,
|
||||||
Encode: typed.codec.encode,
|
Encode: typed.codec.encode,
|
||||||
@@ -26,9 +31,12 @@ func artifactProducerCache(input RunInput, typed *preparedTypedLane, class contr
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, ref := range refs {
|
if len(refs) > 0 {
|
||||||
if err := index.ValidateRef(ref); err != nil {
|
index := sourceIndex()
|
||||||
return nil, fmt.Errorf("cached artifact source reference: %w", err)
|
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"
|
"errors"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||||
@@ -13,6 +14,47 @@ import (
|
|||||||
"go.maximumdirect.net/promptkit"
|
"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) {
|
func TestRunnerReusesAcceptedPredecessorAfterDownstreamFailure(t *testing.T) {
|
||||||
_, store := filesystemProducerCache(t)
|
_, store := filesystemProducerCache(t)
|
||||||
probe := newRunnerProbe()
|
probe := newRunnerProbe()
|
||||||
|
|||||||
Reference in New Issue
Block a user