Share a source index across each pipeline run
This commit is contained in:
@@ -61,6 +61,11 @@ capture, or terminal mode. The runner binds those privately; direct callers can
|
|||||||
supply a [core execution context](../../internal/framework/pipeline/operation.go)
|
supply a [core execution context](../../internal/framework/pipeline/operation.go)
|
||||||
and preparation function to the same executor.
|
and preparation function to the same executor.
|
||||||
|
|
||||||
|
Extraction receives an optional read-only index of the complete source document.
|
||||||
|
The runner shares one index across the run's extraction jobs and cache decoders;
|
||||||
|
standalone callers may omit it or supply an index for their source. It is not a
|
||||||
|
chunk index, and request-local mutable data remains separately owned.
|
||||||
|
|
||||||
- Return a deterministic operation with one candidate when no model is needed.
|
- Return a deterministic operation with one candidate when no model is needed.
|
||||||
Core assesses it once; there is no native request or synthetic cache key.
|
Core assesses it once; there is no native request or synthetic cache key.
|
||||||
- Return a native operation containing the selected `promptkit.RunRequest`,
|
- Return a native operation containing the selected `promptkit.RunRequest`,
|
||||||
|
|||||||
@@ -254,6 +254,13 @@ the algorithm to establish reduced work; no wall-clock gate.
|
|||||||
|
|
||||||
## Stage 6 — Supply One Run-Owned Source Index
|
## Stage 6 — Supply One Run-Owned Source Index
|
||||||
|
|
||||||
|
**Completed.** The runner builds one whole-source index after validation, shares
|
||||||
|
it with extraction jobs and cache decoders, and retains lazy local cache indexing
|
||||||
|
for standalone construction. The D&D ordering constructor accepts an optional
|
||||||
|
index. Tests cover jobs across steps, separate same-ID layouts, cache decoding
|
||||||
|
and omitted-index use. The specified package tests pass. Extractor migration and
|
||||||
|
the final CAT-005 disposition remain in the following stages.
|
||||||
|
|
||||||
**Coverage:** CAT-005 framework. **Prerequisites:** none.
|
**Coverage:** CAT-005 framework. **Prerequisites:** none.
|
||||||
|
|
||||||
Add `ExtractionInput.SourceIndex`, runner initialization, extraction passing,
|
Add `ExtractionInput.SourceIndex`, runner initialization, extraction passing,
|
||||||
|
|||||||
@@ -112,11 +112,13 @@ type PreparationLimit[T any] struct {
|
|||||||
func (PreparationLimit[T]) preparedOperation() {}
|
func (PreparationLimit[T]) preparedOperation() {}
|
||||||
func (PreparationLimit[T]) operationType(T) {}
|
func (PreparationLimit[T]) operationType(T) {}
|
||||||
|
|
||||||
// ExtractionInput contains domain inputs only. Its source, chunk, references,
|
// ExtractionInput contains domain inputs only. SourceIndex optionally borrows a
|
||||||
// and metadata follow the existing detached request boundaries; a preparation
|
// read-only index of the complete Source, not just the current chunk. Callers
|
||||||
// that retains mutable data must own the data it may modify.
|
// providing an index must ensure it describes Source. A preparation that retains
|
||||||
|
// mutable request data must own the data it may modify.
|
||||||
type ExtractionInput struct {
|
type ExtractionInput struct {
|
||||||
Source *source.SourceDocument
|
Source *source.SourceDocument
|
||||||
|
SourceIndex *source.DocumentIndex
|
||||||
Chunk *source.Chunk
|
Chunk *source.Chunk
|
||||||
AmbientContext map[string]any
|
AmbientContext map[string]any
|
||||||
SourceInput LLMInputMaterial
|
SourceInput LLMInputMaterial
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ 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"
|
||||||
@@ -66,8 +65,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.
|
// Shared read-only by extraction jobs and cache decoders for this run.
|
||||||
cacheSourceIndex func() source.DocumentIndex
|
sourceIndex *source.DocumentIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
type RunOutput struct {
|
type RunOutput struct {
|
||||||
@@ -168,9 +167,8 @@ 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 {
|
index := source.NewDocumentIndex(doc)
|
||||||
return source.NewDocumentIndex(doc)
|
input.sourceIndex = &index
|
||||||
})
|
|
||||||
|
|
||||||
if err := writeDebugTimed(debugRecorder, "source/output.json", debugTimedEnvelope{
|
if err := writeDebugTimed(debugRecorder, "source/output.json", debugTimedEnvelope{
|
||||||
Stage: "source",
|
Stage: "source",
|
||||||
|
|||||||
@@ -13,10 +13,13 @@ 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
|
||||||
}
|
}
|
||||||
sourceIndex := input.cacheSourceIndex
|
sourceIndex := func() *source.DocumentIndex { return input.sourceIndex }
|
||||||
if sourceIndex == nil {
|
if input.sourceIndex == nil {
|
||||||
// Standalone operation construction can also defer indexing until decode.
|
// Standalone operation construction can also defer indexing until decode.
|
||||||
sourceIndex = sync.OnceValue(func() source.DocumentIndex { return source.NewDocumentIndex(doc) })
|
sourceIndex = sync.OnceValue(func() *source.DocumentIndex {
|
||||||
|
index := source.NewDocumentIndex(doc)
|
||||||
|
return &index
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return &contracts.ProducerCache[any]{
|
return &contracts.ProducerCache[any]{
|
||||||
Store: input.Cache,
|
Store: input.Cache,
|
||||||
|
|||||||
@@ -197,6 +197,35 @@ func TestTypedCacheDecodeUsesCodecAndCurrentSourceWithoutSemanticAssessment(t *t
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTypedCacheDecodeUsesCurrentSourceLayout(t *testing.T) {
|
||||||
|
_, store := filesystemProducerCache(t)
|
||||||
|
registry := NewArtifactCodecRegistry()
|
||||||
|
mustRegisterArtifactCodec(t, registry, notesCodec())
|
||||||
|
entry, _, err := registry.entry("test/notes")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
typed := &preparedTypedLane{codec: entry, cacheEvidence: func(any) ([]source.SourceRef, error) {
|
||||||
|
return []source.SourceRef{{SourceID: "source", StartUnitID: 1, EndUnitID: 2}}, nil
|
||||||
|
}}
|
||||||
|
for _, reversed := range []bool{false, true} {
|
||||||
|
doc := typedTestDocumentWithUnits(2)
|
||||||
|
if reversed {
|
||||||
|
doc.Units[0], doc.Units[1] = doc.Units[1], doc.Units[0]
|
||||||
|
}
|
||||||
|
index := source.NewDocumentIndex(doc)
|
||||||
|
for _, supplied := range []*source.DocumentIndex{nil, &index} {
|
||||||
|
var warnings []contracts.ProducerDiagnostic
|
||||||
|
cache := artifactProducerCache(RunInput{Cache: store, sourceIndex: supplied}, typed, contracts.ExecutionClassLLMBacked,
|
||||||
|
contracts.DiagnosticOrigin{}, doc, nil, &warnings)
|
||||||
|
_, err := cache.Decode([]byte(`{"items":[]}`))
|
||||||
|
if (err != nil) != reversed {
|
||||||
|
t.Fatalf("reversed=%t supplied=%t: decode error = %v", reversed, supplied != nil, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestProducerCacheCancellationDuringHitCannotDeliverAcceptance(t *testing.T) {
|
func TestProducerCacheCancellationDuringHitCannotDeliverAcceptance(t *testing.T) {
|
||||||
_, store := filesystemProducerCache(t)
|
_, store := filesystemProducerCache(t)
|
||||||
engine := nativeProducerEngine(t, producerClientFunc(func(context.Context, promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
engine := nativeProducerEngine(t, producerClientFunc(func(context.Context, promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) {
|
||||||
|
|||||||
@@ -298,7 +298,7 @@ func (r *Runner) runExtractJob(ctx context.Context, input RunInput, doc *source.
|
|||||||
operation := ExecutionContext[any]{Engine: typed.engine, Controls: lane.ExtractControls, Execution: execution, ReuseEligible: !input.reuseIneligible && referenceTargetReuseEligible(input, lane.ExtractReferences), Assess: assess}
|
operation := ExecutionContext[any]{Engine: typed.engine, Controls: lane.ExtractControls, Execution: execution, ReuseEligible: !input.reuseIneligible && referenceTargetReuseEligible(input, lane.ExtractReferences), Assess: assess}
|
||||||
var cacheDiagnostics []contracts.ProducerDiagnostic
|
var cacheDiagnostics []contracts.ProducerDiagnostic
|
||||||
operation.Cache = artifactProducerCache(input, typed, lane.ExtractExecutionClass, contracts.DiagnosticOrigin{Stage: contracts.DiagnosticOriginStageExtract, StepID: input.stepID, LaneID: lane.ID, ModuleKey: lane.Extract.Module}, doc, &chunk, &cacheDiagnostics)
|
operation.Cache = artifactProducerCache(input, typed, lane.ExtractExecutionClass, contracts.DiagnosticOrigin{Stage: contracts.DiagnosticOriginStageExtract, StepID: input.stepID, LaneID: lane.ID, ModuleKey: lane.Extract.Module}, doc, &chunk, &cacheDiagnostics)
|
||||||
extracted, err := typed.extract(ctx, typed.extractor, contracts.ExtractionInput{Source: doc, Chunk: &chunk, SourceInput: target.sourceInput.Clone(), SessionID: sessionID, References: CloneReferenceSet(references), LLMProfile: lane.Extract.LLMProfile, Metadata: metadata}, operation)
|
extracted, err := typed.extract(ctx, typed.extractor, contracts.ExtractionInput{Source: doc, SourceIndex: input.sourceIndex, Chunk: &chunk, SourceInput: target.sourceInput.Clone(), SessionID: sessionID, References: CloneReferenceSet(references), LLMProfile: lane.Extract.LLMProfile, Metadata: metadata}, operation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
result.err = fmt.Errorf("extract lane %q chunk %q: %w", lane.ID, chunk.ID, err)
|
result.err = fmt.Errorf("extract lane %q chunk %q: %w", lane.ID, chunk.ID, err)
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -2,8 +2,10 @@ package pipeline
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -40,3 +42,50 @@ func TestRunnerUsesProvidedSessionWithoutSourceFallback(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRunnerSharesWholeSourceIndexAcrossJobsAndSteps(t *testing.T) {
|
||||||
|
prepared := preparedOrderedPipeline(t, 3, orderedLaneSpec{"first", "notes"}, orderedLaneSpec{"second", "notes"})
|
||||||
|
indexes := make(chan *source.DocumentIndex, 6)
|
||||||
|
for step := range prepared.Steps {
|
||||||
|
lane := &prepared.Steps[step].lanes[0]
|
||||||
|
original := lane.typed.extract
|
||||||
|
lane.typed.extract = func(ctx context.Context, impl any, request contracts.ExtractionInput, execution ExecutionContext[any]) (erasedTypedResult, error) {
|
||||||
|
if request.SourceIndex == nil {
|
||||||
|
return erasedTypedResult{}, fmt.Errorf("missing source index")
|
||||||
|
}
|
||||||
|
for position, unit := range request.Source.Units {
|
||||||
|
if got, ok := request.SourceIndex.Position(unit.ID); !ok || got != position {
|
||||||
|
return erasedTypedResult{}, fmt.Errorf("source index position for %d = %d, %t, want %d", unit.ID, got, ok, position)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
indexes <- request.SourceIndex
|
||||||
|
return original(ctx, impl, request, execution)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var previous *source.DocumentIndex
|
||||||
|
for run := 0; run < 2; run++ {
|
||||||
|
doc := typedTestDocumentWithUnits(3)
|
||||||
|
if run == 1 {
|
||||||
|
doc.Units[0], doc.Units[2] = doc.Units[2], doc.Units[0]
|
||||||
|
doc.Digest, _ = source.DigestDocument(doc)
|
||||||
|
}
|
||||||
|
prepared.input.(*typedTestInput).doc = doc
|
||||||
|
prepared.chunker = &typedTestChunker{key: "typed/chunk", plan: typedTestPlan(doc)}
|
||||||
|
if _, err := New().Run(context.Background(), RunInput{Prepared: prepared, RawInput: []byte("input"), Workers: 3}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(indexes) != 6 {
|
||||||
|
t.Fatalf("observed %d extraction jobs, want 6", len(indexes))
|
||||||
|
}
|
||||||
|
first := <-indexes
|
||||||
|
for i := 1; i < 6; i++ {
|
||||||
|
if got := <-indexes; got != first {
|
||||||
|
t.Fatal("jobs did not share the run source index")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if first == previous {
|
||||||
|
t.Fatal("separate runs shared a source index")
|
||||||
|
}
|
||||||
|
previous = first
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,6 +18,15 @@ func NewSourceRefOrder(doc *source.SourceDocument) SourceRefOrder {
|
|||||||
return NewSourceRefOrderFromIndex(source.NewDocumentIndex(doc))
|
return NewSourceRefOrderFromIndex(source.NewDocumentIndex(doc))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewSourceRefOrderForSource uses a borrowed whole-source index when provided.
|
||||||
|
// The caller must supply an index for doc; otherwise one is built locally.
|
||||||
|
func NewSourceRefOrderForSource(doc *source.SourceDocument, index *source.DocumentIndex) SourceRefOrder {
|
||||||
|
if index != nil {
|
||||||
|
return NewSourceRefOrderFromIndex(*index)
|
||||||
|
}
|
||||||
|
return NewSourceRefOrder(doc)
|
||||||
|
}
|
||||||
|
|
||||||
// NewSourceRefOrderFromIndex captures source identity and unit positions from
|
// NewSourceRefOrderFromIndex captures source identity and unit positions from
|
||||||
// one document index for source-reference comparison and canonicalization.
|
// one document index for source-reference comparison and canonicalization.
|
||||||
func NewSourceRefOrderFromIndex(index source.DocumentIndex) SourceRefOrder {
|
func NewSourceRefOrderFromIndex(index source.DocumentIndex) SourceRefOrder {
|
||||||
|
|||||||
@@ -34,6 +34,18 @@ func TestSourceRefOrderUsesDocumentOrderAndLiteralFallbacks(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSourceRefOrderForSourceUsesOptionalWholeSourceIndex(t *testing.T) {
|
||||||
|
doc := unitRefSourceDocument(30, 10, 20)
|
||||||
|
index := source.NewDocumentIndex(doc)
|
||||||
|
refs := []source.SourceRef{{SourceID: doc.ID, StartUnitID: 20, EndUnitID: 20}, {SourceID: doc.ID, StartUnitID: 30, EndUnitID: 30}}
|
||||||
|
want := []source.SourceRef{refs[1], refs[0]}
|
||||||
|
for _, supplied := range []*source.DocumentIndex{nil, &index} {
|
||||||
|
if got := NewSourceRefOrderForSource(doc, supplied).Canonicalize(refs); !reflect.DeepEqual(got, want) {
|
||||||
|
t.Fatalf("Canonicalize() = %#v, want %#v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSourceRefOrderCanonicalizePreservesRepresentationAndOwnership(t *testing.T) {
|
func TestSourceRefOrderCanonicalizePreservesRepresentationAndOwnership(t *testing.T) {
|
||||||
order := NewSourceRefOrder(unitRefSourceDocument(3, 1, 2))
|
order := NewSourceRefOrder(unitRefSourceDocument(3, 1, 2))
|
||||||
if got := order.Canonicalize(nil); got != nil {
|
if got := order.Canonicalize(nil); got != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user