Route spell extraction through core preparation with remaining extractors pending
This commit is contained in:
@@ -53,20 +53,18 @@ func RegisterExtractorBuilder[T any](registry *ExtractorRegistry, spec ModuleSpe
|
||||
if _, ok := registry.specs[normalized.Key]; ok {
|
||||
return fmt.Errorf("extractor %q is already registered", normalized.Key)
|
||||
}
|
||||
entry := typedExtractorEntry{spec: cloneModuleSpec(normalized), valueType: reflect.TypeFor[T](), validateOptions: validateOptions, builder: func(request BuildRequest) (any, error) { return builder(cloneBuildRequest(request)) }, extract: func(ctx context.Context, implementation any, request contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
entry := typedExtractorEntry{spec: cloneModuleSpec(normalized), valueType: reflect.TypeFor[T](), validateOptions: validateOptions, builder: func(request BuildRequest) (any, error) { return builder(cloneBuildRequest(request)) }, extract: func(ctx context.Context, implementation any, request contracts.ExtractionInput, execution ExecutionContext[any]) (erasedTypedResult, error) {
|
||||
extractor, ok := implementation.(contracts.Extractor[T])
|
||||
if !ok {
|
||||
return erasedTypedResult{}, fmt.Errorf("extractor %q has incompatible implementation %T", normalized.Key, implementation)
|
||||
}
|
||||
result, err := extractor.Extract(ctx, contracts.TypedExtractionRequest[T]{
|
||||
Source: request.Source, Chunk: request.Chunk, AmbientContext: request.AmbientContext,
|
||||
SourceInput: request.SourceInput, SessionID: request.SessionID, References: request.References,
|
||||
LLMProfile: request.LLMProfile, Metadata: request.Metadata, Operation: specializeProducerOperation[T](request.Operation),
|
||||
result, err := ExecuteOperation(ctx, specializeExecutionContext[T](execution), func(ctx context.Context) (contracts.PreparedOperation[T], error) {
|
||||
return extractor.PrepareExtraction(ctx, request)
|
||||
})
|
||||
if err != nil {
|
||||
return erasedTypedResult{}, err
|
||||
}
|
||||
return erasedTypedResult{Value: result.Value, Diagnostics: contracts.CloneProducerDiagnostics(result.Diagnostics), Outcome: result.Outcome, Fallback: eraseProducerFallback(result.Fallback)}, nil
|
||||
return erasedTypedResult{Value: result.Value, Diagnostics: contracts.CloneProducerDiagnostics(result.Diagnostics), Outcome: result.Outcome, Assessment: result.Assessment}, nil
|
||||
}}
|
||||
if registry.typedEntries == nil {
|
||||
registry.typedEntries = map[string]typedExtractorEntry{}
|
||||
|
||||
@@ -194,10 +194,10 @@ func TestRunnerHandsOffAcceptedNormalizedOutputBeforeConsumerLanes(t *testing.T)
|
||||
input, _, _ := handoffFixture(t, codecNotes{Items: []string{"first"}})
|
||||
prepared := input.Prepared
|
||||
received := make(map[ModuleStage]contracts.ReferenceSet)
|
||||
prepared.Steps[0].lanes[0].typed.extract = func(context.Context, any, contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
prepared.Steps[0].lanes[0].typed.extract = func(context.Context, any, contracts.ExtractionInput, ExecutionContext[any]) (erasedTypedResult, error) {
|
||||
return erasedTypedResult{Value: codecNotes{Items: []string{"first"}}}, nil
|
||||
}
|
||||
prepared.Steps[1].lanes[0].typed.extract = func(_ context.Context, _ any, request contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
prepared.Steps[1].lanes[0].typed.extract = func(_ context.Context, _ any, request contracts.ExtractionInput, _ ExecutionContext[any]) (erasedTypedResult, error) {
|
||||
received[StageExtract] = CloneReferenceSet(request.References)
|
||||
return erasedTypedResult{Value: codecScore{Value: 3}}, nil
|
||||
}
|
||||
|
||||
@@ -189,9 +189,11 @@ func TestPrepareDeliversTargetReferencesAsIndependentBuildInputs(t *testing.T) {
|
||||
t.Fatalf("prepared extract options = %#v, want original nested value", got)
|
||||
}
|
||||
|
||||
_, err = prepared.Steps[0].lanes[0].typed.extract(context.Background(), prepared.Steps[0].lanes[0].typed.extractor, contracts.TypedExtractionRequest[any]{
|
||||
_, err = prepared.Steps[0].lanes[0].typed.extract(context.Background(), prepared.Steps[0].lanes[0].typed.extractor, contracts.ExtractionInput{
|
||||
References: CloneReferenceSet(resolved.Steps[0].ArtifactLanes[0].ExtractReferences.ReferenceSet),
|
||||
})
|
||||
}, ExecutionContext[any]{Assess: func(context.Context, any) (contracts.CandidateAssessment, error) {
|
||||
return contracts.CandidateAssessment{}, nil
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatalf("prepared extractor operation error = %v, want nil", err)
|
||||
}
|
||||
@@ -536,21 +538,22 @@ func constructionRegistriesWithHooks(t *testing.T, built *[]string, failure *con
|
||||
type constructionInput struct {
|
||||
key string
|
||||
requests []contracts.ParseRequest
|
||||
extractRequests []contracts.TypedExtractionRequest[codecNotes]
|
||||
extractRequests []contracts.ExtractionInput
|
||||
}
|
||||
|
||||
type constructionExtractor struct {
|
||||
key string
|
||||
requests *[]contracts.TypedExtractionRequest[codecNotes]
|
||||
requests *[]contracts.ExtractionInput
|
||||
}
|
||||
|
||||
func (extractor *constructionExtractor) Key() string { return extractor.key }
|
||||
func (*constructionExtractor) ReferenceSlots() []contracts.ReferenceSlot { return nil }
|
||||
func (extractor *constructionExtractor) Extract(_ context.Context, request contracts.TypedExtractionRequest[codecNotes]) (contracts.TypedExtractionResult[codecNotes], error) {
|
||||
func (extractor *constructionExtractor) PrepareExtraction(_ context.Context, request contracts.ExtractionInput) (contracts.PreparedOperation[codecNotes], error) {
|
||||
if extractor.requests != nil {
|
||||
*extractor.requests = append(*extractor.requests, request)
|
||||
}
|
||||
return contracts.TypedExtractionResult[codecNotes]{}, nil
|
||||
value := codecNotes{}
|
||||
return contracts.DeterministicOperation[codecNotes]{Evaluation: contracts.CandidateEvaluation[codecNotes]{Result: contracts.EvaluationView[codecNotes]{Candidate: &value}}}, nil
|
||||
}
|
||||
|
||||
func constructionReferenceSet(slotName, content string) contracts.ReferenceSet {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
// PreparedPipeline owns the constructed, run-local implementation set for one
|
||||
@@ -51,6 +52,7 @@ type preparedLaneExecutor struct {
|
||||
}
|
||||
|
||||
type preparedTypedLane struct {
|
||||
engine *promptkit.Engine
|
||||
extractor any
|
||||
merger any
|
||||
normalizer any
|
||||
@@ -223,7 +225,7 @@ func prepareLane(pipeline ResolvedPipeline, lane ResolvedArtifactLane, registrie
|
||||
if codecErr != nil {
|
||||
return preparedLaneExecutor{}, constructionError(pipeline.ID, lane.ID, StageExtract, lane.Extract.Module, "", codecErr)
|
||||
}
|
||||
executor.typed = &preparedTypedLane{extractor: module, extract: extractEntry.extract, codec: codec}
|
||||
executor.typed = &preparedTypedLane{extractor: module, extract: extractEntry.extract, codec: codec, engine: deps.Engine}
|
||||
if registry := registries.ArtifactEvidence; registry != nil {
|
||||
if evidence, ok := registry.entries[lane.ArtifactKind]; ok {
|
||||
// Projectors retain their own exact-type check. A mismatch is an
|
||||
|
||||
@@ -43,7 +43,7 @@ func TestRunnerSharesWorkerLimitBetweenExtractionAndReadyContinuation(t *testing
|
||||
mergeStarted := make(chan struct{}, 1)
|
||||
for laneIndex := range prepared.Steps[0].lanes {
|
||||
lane := laneIndex
|
||||
installExtractOperation(prepared, lane, func(ctx context.Context, request contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
installExtractOperation(prepared, lane, func(ctx context.Context, request contracts.ExtractionInput) (erasedTypedResult, error) {
|
||||
defer jobs.enter()()
|
||||
if lane == 0 {
|
||||
firstLaneExtracts.Add(1)
|
||||
@@ -121,26 +121,28 @@ func installNativeExtraction(prepared *PreparedPipeline, engine *promptkit.Engin
|
||||
for laneIndex := range prepared.Steps[0].lanes {
|
||||
lane := laneIndex
|
||||
prepared.Steps[0].lanes[lane].resolved.ExtractExecutionClass = contracts.ExecutionClassLLMBacked
|
||||
installExtractOperation(prepared, lane, func(ctx context.Context, request contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
prepared.Steps[0].lanes[lane].typed.extract = func(ctx context.Context, _ any, request contracts.ExtractionInput, execution ExecutionContext[any]) (erasedTypedResult, error) {
|
||||
if jobs != nil {
|
||||
defer jobs.enter()()
|
||||
}
|
||||
if started != nil {
|
||||
started <- struct{}{}
|
||||
}
|
||||
value, outcome, err := RunProducer(ctx, engine, request.Operation, nativeProducerInitial(),
|
||||
func(ctx context.Context, response *promptkit.RunResult) (*any, []string, error) {
|
||||
execution.Engine = engine
|
||||
result, err := ExecuteOperation(ctx, execution, func(context.Context) (contracts.PreparedOperation[any], error) {
|
||||
return contracts.NativeOperation[any]{Initial: nativeProducerInitial(), Interpret: func(ctx context.Context, response *promptkit.RunResult) (contracts.Evaluation[any], error) {
|
||||
_, _, err := interpretProducerNumber(ctx, response)
|
||||
value := typedValueForLane(lane, request.Chunk.Index)
|
||||
return &value, nil, err
|
||||
}, producerCorrection, func(context.Context, *any) (any, error) {
|
||||
return contracts.CandidateEvaluation[any]{Result: contracts.EvaluationView[any]{Candidate: &value}}, err
|
||||
}, Correction: producerCorrection, Fallback: func(context.Context, contracts.Evaluation[any]) (any, error) {
|
||||
if lane == 0 {
|
||||
return codecNotes{Items: []string{}}, nil
|
||||
}
|
||||
return codecScore{}, nil
|
||||
})
|
||||
return erasedTypedResult{Value: value, Outcome: outcome}, err
|
||||
})
|
||||
}}, nil
|
||||
})
|
||||
return erasedTypedResult{Value: result.Value, Outcome: result.Outcome, Assessment: result.Assessment}, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,15 +32,17 @@ func TestRunnerChecksCurrentSourceOnConcurrentCacheHits(t *testing.T) {
|
||||
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) {
|
||||
lane.typed.extract = func(ctx context.Context, _ any, request contracts.ExtractionInput, execution ExecutionContext[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) {
|
||||
execution.Engine = engine
|
||||
result, err := ExecuteOperation(ctx, execution, func(context.Context) (contracts.PreparedOperation[any], error) {
|
||||
return contracts.NativeOperation[any]{Initial: initial, Interpret: func(context.Context, *promptkit.RunResult) (contracts.Evaluation[any], error) {
|
||||
var value any = codecNotes{Items: []string{"accepted"}}
|
||||
return &value, nil, nil
|
||||
}, nil, nil)
|
||||
return erasedTypedResult{Value: value, Outcome: outcome}, err
|
||||
return contracts.CandidateEvaluation[any]{Result: contracts.EvaluationView[any]{Candidate: &value}}, nil
|
||||
}}, nil
|
||||
})
|
||||
return erasedTypedResult{Value: result.Value, Outcome: result.Outcome, Assessment: result.Assessment}, err
|
||||
}
|
||||
}
|
||||
for run := 0; run < 2; run++ {
|
||||
@@ -76,11 +78,11 @@ func TestRunnerReusesAcceptedPredecessorAfterDownstreamFailure(t *testing.T) {
|
||||
failure := errors.New("downstream application failure")
|
||||
first := true
|
||||
consumerOperation := prepared.Steps[1].lanes[0].typed.extract
|
||||
prepared.Steps[1].lanes[0].typed.extract = func(ctx context.Context, implementation any, request contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
prepared.Steps[1].lanes[0].typed.extract = func(ctx context.Context, implementation any, request contracts.ExtractionInput, execution ExecutionContext[any]) (erasedTypedResult, error) {
|
||||
if first {
|
||||
return erasedTypedResult{}, failure
|
||||
}
|
||||
return consumerOperation(ctx, implementation, request)
|
||||
return consumerOperation(ctx, implementation, request, execution)
|
||||
}
|
||||
var observations []contracts.CacheObservation
|
||||
input := RunInput{Prepared: prepared, RawInput: []byte("input"), Workers: 1, Cache: store,
|
||||
|
||||
@@ -108,8 +108,12 @@ func configureCandidateOperation(prepared *PreparedPipeline, target ModuleStage,
|
||||
lane := &prepared.Steps[0].lanes[0]
|
||||
switch target {
|
||||
case StageExtract:
|
||||
lane.typed.extract = func(context.Context, any, contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
return erasedTypedResult{Value: value}, nil
|
||||
lane.typed.extract = func(ctx context.Context, _ any, _ contracts.ExtractionInput, execution ExecutionContext[any]) (erasedTypedResult, error) {
|
||||
var candidate any = value
|
||||
result, err := ExecuteOperation(ctx, execution, func(context.Context) (contracts.PreparedOperation[any], error) {
|
||||
return contracts.DeterministicOperation[any]{Evaluation: contracts.CandidateEvaluation[any]{Result: contracts.EvaluationView[any]{Candidate: &candidate}}}, nil
|
||||
})
|
||||
return erasedTypedResult{Value: result.Value, Outcome: result.Outcome, Assessment: result.Assessment}, err
|
||||
}
|
||||
lane.typed.merge = func(context.Context, any, contracts.TypedMergeRequest[any]) (erasedTypedResult, error) {
|
||||
return erasedTypedResult{Value: codecNotes{Items: []string{"merged-other"}}}, nil
|
||||
|
||||
@@ -201,7 +201,7 @@ func TestRunnerFailsGeneratedHandoffBeforeConsumerExtraction(t *testing.T) {
|
||||
}}
|
||||
prepared.resolved.Steps[0].ArtifactLanes[0].ExtractReferences = CloneReferenceTarget(lane.ExtractReferences)
|
||||
var extractCalls atomic.Int32
|
||||
preparedLane.typed.extract = func(context.Context, any, contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
preparedLane.typed.extract = func(context.Context, any, contracts.ExtractionInput, ExecutionContext[any]) (erasedTypedResult, error) {
|
||||
extractCalls.Add(1)
|
||||
return erasedTypedResult{Value: codecNotes{}}, nil
|
||||
}
|
||||
@@ -232,7 +232,7 @@ func TestRunnerExecutesOrderedStepsWithHardBarriers(t *testing.T) {
|
||||
}
|
||||
first := &prepared.Steps[0].lanes[0]
|
||||
second := &prepared.Steps[1].lanes[0]
|
||||
first.typed.extract = func(context.Context, any, contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
first.typed.extract = func(context.Context, any, contracts.ExtractionInput, ExecutionContext[any]) (erasedTypedResult, error) {
|
||||
return erasedTypedResult{Value: codecNotes{Items: []string{"first"}}}, nil
|
||||
}
|
||||
originalFirstNormalize := first.typed.normalize
|
||||
@@ -243,7 +243,7 @@ func TestRunnerExecutesOrderedStepsWithHardBarriers(t *testing.T) {
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
second.typed.extract = func(context.Context, any, contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
second.typed.extract = func(context.Context, any, contracts.ExtractionInput, ExecutionContext[any]) (erasedTypedResult, error) {
|
||||
record("second-extract-started")
|
||||
return erasedTypedResult{Value: codecScore{Value: 2}}, nil
|
||||
}
|
||||
@@ -272,14 +272,14 @@ func TestRunnerStopsLaterOrderedStepsAfterFailure(t *testing.T) {
|
||||
first := &prepared.Steps[0].lanes[0]
|
||||
second := &prepared.Steps[1].lanes[0]
|
||||
third := &prepared.Steps[2].lanes[0]
|
||||
first.typed.extract = func(context.Context, any, contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
first.typed.extract = func(context.Context, any, contracts.ExtractionInput, ExecutionContext[any]) (erasedTypedResult, error) {
|
||||
return erasedTypedResult{Value: codecNotes{Items: []string{"first"}}}, nil
|
||||
}
|
||||
second.typed.extract = func(context.Context, any, contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
second.typed.extract = func(context.Context, any, contracts.ExtractionInput, ExecutionContext[any]) (erasedTypedResult, error) {
|
||||
return erasedTypedResult{}, errors.New("ordered step extraction failed")
|
||||
}
|
||||
var thirdCalls atomic.Int32
|
||||
third.typed.extract = func(context.Context, any, contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
third.typed.extract = func(context.Context, any, contracts.ExtractionInput, ExecutionContext[any]) (erasedTypedResult, error) {
|
||||
thirdCalls.Add(1)
|
||||
return erasedTypedResult{Value: codecNotes{Items: []string{"later"}}}, nil
|
||||
}
|
||||
@@ -301,8 +301,8 @@ func TestRunnerStopsLaterOrderedStepsAfterFailure(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func installExtractOperation(prepared *PreparedPipeline, laneIndex int, operation func(context.Context, contracts.TypedExtractionRequest[any]) (erasedTypedResult, error)) {
|
||||
prepared.Steps[0].lanes[laneIndex].typed.extract = func(ctx context.Context, _ any, request contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
func installExtractOperation(prepared *PreparedPipeline, laneIndex int, operation func(context.Context, contracts.ExtractionInput) (erasedTypedResult, error)) {
|
||||
prepared.Steps[0].lanes[laneIndex].typed.extract = func(ctx context.Context, _ any, request contracts.ExtractionInput, execution ExecutionContext[any]) (erasedTypedResult, error) {
|
||||
return operation(ctx, request)
|
||||
}
|
||||
}
|
||||
@@ -325,7 +325,7 @@ func TestRunnerBoundsExtractJobsAndStabilizesReverseCompletion(t *testing.T) {
|
||||
var maximum atomic.Int32
|
||||
for laneIndex := range prepared.Steps[0].lanes {
|
||||
lane := laneIndex
|
||||
installExtractOperation(prepared, lane, func(ctx context.Context, request contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
installExtractOperation(prepared, lane, func(ctx context.Context, request contracts.ExtractionInput) (erasedTypedResult, error) {
|
||||
jobIndex := request.Chunk.Index*2 + lane
|
||||
current := active.Add(1)
|
||||
defer active.Add(-1)
|
||||
@@ -406,7 +406,7 @@ func TestRunnerBoundsConcurrentLaneContinuations(t *testing.T) {
|
||||
extractor: base.typed.extractor,
|
||||
merger: base.typed.merger,
|
||||
normalizer: base.typed.normalizer,
|
||||
extract: func(_ context.Context, _ any, request contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
extract: func(_ context.Context, _ any, request contracts.ExtractionInput, _ ExecutionContext[any]) (erasedTypedResult, error) {
|
||||
return erasedTypedResult{Value: codecNotes{Items: []string{request.Chunk.ID}}}, nil
|
||||
},
|
||||
merge: func(_ context.Context, _ any, _ contracts.TypedMergeRequest[any]) (erasedTypedResult, error) {
|
||||
@@ -450,7 +450,7 @@ func TestRunnerSelectsFrameworkErrorByStableLaneOrder(t *testing.T) {
|
||||
release := make(chan struct{})
|
||||
for laneIndex := range prepared.Steps[0].lanes {
|
||||
lane := laneIndex
|
||||
installExtractOperation(prepared, lane, func(context.Context, contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
installExtractOperation(prepared, lane, func(context.Context, contracts.ExtractionInput) (erasedTypedResult, error) {
|
||||
ready <- struct{}{}
|
||||
<-release
|
||||
return erasedTypedResult{}, fmt.Errorf("lane-%d failure", lane)
|
||||
@@ -473,7 +473,7 @@ func TestRunnerSelectsMergeErrorBeforeEarlierLaneNormalizeError(t *testing.T) {
|
||||
prepared := preparedConcurrentPipeline(t, 1)
|
||||
for laneIndex := range prepared.Steps[0].lanes {
|
||||
lane := laneIndex
|
||||
installExtractOperation(prepared, lane, func(_ context.Context, request contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
installExtractOperation(prepared, lane, func(_ context.Context, request contracts.ExtractionInput) (erasedTypedResult, error) {
|
||||
return erasedTypedResult{Value: typedValueForLane(lane, request.Chunk.Index)}, nil
|
||||
})
|
||||
}
|
||||
@@ -507,7 +507,7 @@ func TestRunnerReturnsParentCancellationAndStopsQueuedExtracts(t *testing.T) {
|
||||
started := make(chan struct{}, 8)
|
||||
for laneIndex := range prepared.Steps[0].lanes {
|
||||
lane := laneIndex
|
||||
installExtractOperation(prepared, lane, func(ctx context.Context, request contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
installExtractOperation(prepared, lane, func(ctx context.Context, request contracts.ExtractionInput) (erasedTypedResult, error) {
|
||||
started <- struct{}{}
|
||||
<-ctx.Done()
|
||||
return erasedTypedResult{Value: typedValueForLane(lane, request.Chunk.Index)}, ctx.Err()
|
||||
|
||||
@@ -294,34 +294,23 @@ func (r *Runner) runExtractJob(ctx context.Context, input RunInput, doc *source.
|
||||
result.err = err
|
||||
return result
|
||||
}
|
||||
var assessment contracts.CandidateAssessment
|
||||
assess := typedCandidateAssessor[any](typed.codec, target, state.prepared.extractValidators)
|
||||
operation := contracts.ProducerOperation[any]{Controls: lane.ExtractControls, Execution: execution, ReuseEligible: !input.reuseIneligible && referenceTargetReuseEligible(input, lane.ExtractReferences),
|
||||
Assess: func(ctx context.Context, value any) (contracts.CandidateAssessment, error) {
|
||||
var err error
|
||||
assessment, err = assess(ctx, value)
|
||||
return assessment, err
|
||||
},
|
||||
}
|
||||
operation := ExecutionContext[any]{Engine: typed.engine, Controls: lane.ExtractControls, Execution: execution, ReuseEligible: !input.reuseIneligible && referenceTargetReuseEligible(input, lane.ExtractReferences), Assess: assess}
|
||||
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)
|
||||
extracted, err := typed.extract(ctx, typed.extractor, contracts.TypedExtractionRequest[any]{Operation: operation, Source: doc, Chunk: &chunk, SourceInput: target.sourceInput.Clone(), SessionID: sessionID, References: CloneReferenceSet(references), LLMProfile: lane.Extract.LLMProfile, Metadata: metadata})
|
||||
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)
|
||||
if err != nil {
|
||||
result.err = fmt.Errorf("extract lane %q chunk %q: %w", lane.ID, chunk.ID, err)
|
||||
return result
|
||||
}
|
||||
value, outcome, err := completeProducerOperation(ctx, lane.ExtractExecutionClass, operation, extracted.Value, extracted.Outcome, extracted.Fallback)
|
||||
if err != nil {
|
||||
result.err = fmt.Errorf("accept extract lane %q chunk %q: %w", lane.ID, chunk.ID, err)
|
||||
return result
|
||||
}
|
||||
value, outcome := extracted.Value, extracted.Outcome
|
||||
stored, err := serializeArtifact(typed.codec, value, false)
|
||||
if err != nil {
|
||||
result.err = fmt.Errorf("encode extract lane %q chunk %q: %w", lane.ID, chunk.ID, err)
|
||||
return result
|
||||
}
|
||||
capturedArtifact, result.outcome = stored, outcome
|
||||
result.diagnostics, err = operationDiagnosticGroups(extracted.Diagnostics, cacheDiagnostics, assessment, outcome, contracts.DiagnosticOrigin{Stage: contracts.DiagnosticOriginStageExtract, StepID: input.stepID, LaneID: lane.ID, ModuleKey: lane.Extract.Module}, &chunk)
|
||||
result.diagnostics, err = operationDiagnosticGroups(extracted.Diagnostics, cacheDiagnostics, extracted.Assessment, outcome, contracts.DiagnosticOrigin{Stage: contracts.DiagnosticOriginStageExtract, StepID: input.stepID, LaneID: lane.ID, ModuleKey: lane.Extract.Module}, &chunk)
|
||||
if err != nil {
|
||||
result.err = err
|
||||
return result
|
||||
|
||||
@@ -35,7 +35,7 @@ func TestRunnerPassesIndependentAnnotationScopesToExtractors(t *testing.T) {
|
||||
chunker.plan.Annotations = source.ChunkAnnotations{"same": json.RawMessage(`{"plan":2}`)}
|
||||
|
||||
var gotRange, gotPlan string
|
||||
installExtractOperation(prepared, 0, func(_ context.Context, request contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
installExtractOperation(prepared, 0, func(_ context.Context, request contracts.ExtractionInput) (erasedTypedResult, error) {
|
||||
gotRange = string(request.Chunk.Annotations["same"])
|
||||
gotPlan = string(request.Chunk.PlanAnnotations["same"])
|
||||
request.Chunk.Annotations["same"][0] = '['
|
||||
@@ -73,7 +73,7 @@ func TestRunnerPassesIndependentConcreteMetadataToValidatorsAndExtractors(t *tes
|
||||
chunk: validator,
|
||||
}}
|
||||
var extractorSeen string
|
||||
installExtractOperation(prepared, 0, func(_ context.Context, request contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
installExtractOperation(prepared, 0, func(_ context.Context, request contracts.ExtractionInput) (erasedTypedResult, error) {
|
||||
nested := request.Chunk.Units[0].Metadata["typed"].(pipelineTypedMetadata)
|
||||
extractorSeen = string(nested["raw"].(json.RawMessage))
|
||||
nested["raw"].(json.RawMessage)[0] = '['
|
||||
|
||||
@@ -31,7 +31,6 @@ func (p *runnerProbe) record(key string, eligible bool) {
|
||||
type runnerNotesProducer struct {
|
||||
name string
|
||||
probe *runnerProbe
|
||||
engine *promptkit.Engine
|
||||
native bool
|
||||
references bool
|
||||
failure error
|
||||
@@ -49,28 +48,32 @@ func (p *runnerNotesProducer) fallback(ctx context.Context, role string) (codecN
|
||||
p.probe.record(p.name+"/"+role+"/fallback", false)
|
||||
return codecNotes{Items: []string{}}, ctx.Err()
|
||||
}
|
||||
func (p *runnerNotesProducer) Extract(ctx context.Context, request contracts.TypedExtractionRequest[codecNotes]) (contracts.TypedExtractionResult[codecNotes], error) {
|
||||
p.probe.record(p.name+"/extract", request.Operation.ReuseEligible)
|
||||
func (p *runnerNotesProducer) PrepareExtraction(ctx context.Context, request contracts.ExtractionInput) (contracts.PreparedOperation[codecNotes], error) {
|
||||
if p.cancel != nil {
|
||||
p.cancel()
|
||||
}
|
||||
if p.failure != nil {
|
||||
return contracts.TypedExtractionResult[codecNotes]{}, p.failure
|
||||
return nil, p.failure
|
||||
}
|
||||
if p.native {
|
||||
initial := nativeProducerInitial()
|
||||
initial.ProfileID, initial.SessionID = request.LLMProfile, request.SessionID
|
||||
value, outcome, err := RunProducer(ctx, p.engine, request.Operation, initial,
|
||||
func(ctx context.Context, result *promptkit.RunResult) (*codecNotes, []string, error) {
|
||||
return contracts.NativeOperation[codecNotes]{Initial: initial,
|
||||
Interpret: func(ctx context.Context, result *promptkit.RunResult) (contracts.Evaluation[codecNotes], error) {
|
||||
number, _, err := interpretProducerNumber(ctx, result)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
return &codecNotes{Items: []string{fmt.Sprint(*number)}}, nil, nil
|
||||
}, producerCorrection, func(ctx context.Context, _ *codecNotes) (codecNotes, error) { return p.fallback(ctx, "extract") })
|
||||
return contracts.TypedExtractionResult[codecNotes]{Value: value, Outcome: outcome}, err
|
||||
value := codecNotes{Items: []string{fmt.Sprint(*number)}}
|
||||
return contracts.CandidateEvaluation[codecNotes]{Result: contracts.EvaluationView[codecNotes]{Candidate: &value}}, nil
|
||||
}, Correction: producerCorrection, Fallback: func(ctx context.Context, _ contracts.Evaluation[codecNotes]) (codecNotes, error) {
|
||||
return p.fallback(ctx, "extract")
|
||||
}}, nil
|
||||
}
|
||||
return contracts.TypedExtractionResult[codecNotes]{Value: codecNotes{Items: []string{p.name}}, Fallback: func(ctx context.Context) (codecNotes, error) { return p.fallback(ctx, "extract") }}, nil
|
||||
value := codecNotes{Items: []string{p.name}}
|
||||
return contracts.DeterministicOperation[codecNotes]{Evaluation: contracts.CandidateEvaluation[codecNotes]{Result: contracts.EvaluationView[codecNotes]{Candidate: &value}}, Fallback: func(ctx context.Context, _ contracts.Evaluation[codecNotes]) (codecNotes, error) {
|
||||
return p.fallback(ctx, "extract")
|
||||
}}, nil
|
||||
}
|
||||
func (p *runnerNotesProducer) Merge(ctx context.Context, request contracts.TypedMergeRequest[codecNotes]) (contracts.TypedMergeResult[codecNotes], error) {
|
||||
p.probe.record(p.name+"/merge", request.Operation.ReuseEligible)
|
||||
@@ -101,13 +104,19 @@ func registerRunnerNotes(t *testing.T, catalog ModuleCatalog, producer runnerNot
|
||||
}
|
||||
extractSpec := spec(StageExtract)
|
||||
extractSpec.ExecutionClass = class
|
||||
if err := RegisterExtractorBuilder(catalog.Extractors, extractSpec, rejectUnconfiguredOptions, func(request BuildRequest) (contracts.Extractor[codecNotes], error) {
|
||||
if err := RegisterExtractorBuilder(catalog.Extractors, extractSpec, rejectUnconfiguredOptions, func(BuildRequest) (contracts.Extractor[codecNotes], error) {
|
||||
copy := producer
|
||||
copy.engine = request.Dependencies.Engine
|
||||
return ©, nil
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entry := catalog.Extractors.typedEntries[extractSpec.Key]
|
||||
extract := entry.extract
|
||||
entry.extract = func(ctx context.Context, module any, input contracts.ExtractionInput, execution ExecutionContext[any]) (erasedTypedResult, error) {
|
||||
producer.probe.record(producer.name+"/extract", execution.ReuseEligible)
|
||||
return extract(ctx, module, input, execution)
|
||||
}
|
||||
catalog.Extractors.typedEntries[extractSpec.Key] = entry
|
||||
if err := RegisterMerger(catalog.Mergers, spec(StageMerge), func() (contracts.Merger[codecNotes], error) { copy := producer; return ©, nil }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ func TestRunnerUsesProvidedSessionWithoutSourceFallback(t *testing.T) {
|
||||
prepared := preparedConcurrentPipeline(t, 1)
|
||||
var session string
|
||||
calls := 0
|
||||
installExtractOperation(prepared, 0, func(_ context.Context, request contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
installExtractOperation(prepared, 0, func(_ context.Context, request contracts.ExtractionInput) (erasedTypedResult, error) {
|
||||
calls++
|
||||
session = request.SessionID
|
||||
return erasedTypedResult{Value: typedValueForLane(0, request.Chunk.Index)}, nil
|
||||
|
||||
@@ -27,6 +27,7 @@ type erasedTypedResult struct {
|
||||
Value any
|
||||
Diagnostics []contracts.ProducerDiagnostic
|
||||
Outcome contracts.ProducerOutcome
|
||||
Assessment contracts.CandidateAssessment
|
||||
}
|
||||
|
||||
type typedValidationTarget struct {
|
||||
@@ -69,7 +70,7 @@ func exactTypedValue[T any](operation string, value any) (T, error) {
|
||||
return typed, nil
|
||||
}
|
||||
|
||||
type typedExtractOperation func(context.Context, any, contracts.TypedExtractionRequest[any]) (erasedTypedResult, error)
|
||||
type typedExtractOperation func(context.Context, any, contracts.ExtractionInput, ExecutionContext[any]) (erasedTypedResult, error)
|
||||
type typedMergeOperation func(context.Context, any, contracts.TypedMergeRequest[any]) (erasedTypedResult, error)
|
||||
type typedNormalizeOperation func(context.Context, any, contracts.TypedNormalizeRequest[any]) (erasedTypedResult, error)
|
||||
type typedValidateOperation func(context.Context, any, typedValidationTarget) (contracts.ValidationResult, error)
|
||||
@@ -100,3 +101,8 @@ func specializeProducerOperation[T any](operation contracts.ProducerOperation[an
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func specializeExecutionContext[T any](execution ExecutionContext[any]) ExecutionContext[T] {
|
||||
operation := specializeProducerOperation[T](contracts.ProducerOperation[any]{Controls: execution.Controls, Execution: execution.Execution, ReuseEligible: execution.ReuseEligible, Assess: execution.Assess, Cache: execution.Cache})
|
||||
return ExecutionContext[T]{Engine: execution.Engine, Controls: operation.Controls, Execution: operation.Execution, ReuseEligible: operation.ReuseEligible, Assess: operation.Assess, Cache: operation.Cache}
|
||||
}
|
||||
|
||||
@@ -29,10 +29,9 @@ func (p *collaborationProducer) assess(ctx context.Context, value collaborationV
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *collaborationProducer) Extract(ctx context.Context, request contracts.TypedExtractionRequest[collaborationValue]) (contracts.TypedExtractionResult[collaborationValue], error) {
|
||||
func (p *collaborationProducer) PrepareExtraction(context.Context, contracts.ExtractionInput) (contracts.PreparedOperation[collaborationValue], error) {
|
||||
value := collaborationValue{"candidate"}
|
||||
err := p.assess(ctx, value, request.Operation)
|
||||
return contracts.TypedExtractionResult[collaborationValue]{Value: value, Outcome: p.outcome}, err
|
||||
return contracts.DeterministicOperation[collaborationValue]{Evaluation: contracts.CandidateEvaluation[collaborationValue]{Result: contracts.EvaluationView[collaborationValue]{Candidate: &value}}}, nil
|
||||
}
|
||||
func (p *collaborationProducer) Merge(ctx context.Context, request contracts.TypedMergeRequest[collaborationValue]) (contracts.TypedMergeResult[collaborationValue], error) {
|
||||
value := request.ExtractOutputs[0].Value
|
||||
@@ -59,7 +58,7 @@ func (v collaborationValidator) Validate(_ context.Context, request contracts.Ty
|
||||
}
|
||||
|
||||
func TestTypedRegistryCarriesOperationAssessmentAndOutcome(t *testing.T) {
|
||||
for _, stage := range []ModuleStage{StageExtract, StageMerge, StageNormalize} {
|
||||
for _, stage := range []ModuleStage{StageMerge, StageNormalize} {
|
||||
t.Run(string(stage), func(t *testing.T) {
|
||||
expected := collaborationValue{"candidate"}
|
||||
calls := 0
|
||||
@@ -112,7 +111,7 @@ func registerCollaborationProducer(t *testing.T, stage ModuleStage, impl *collab
|
||||
}
|
||||
entry, _ := registry.typedEntry(spec.Key)
|
||||
return func(ctx context.Context, op contracts.ProducerOperation[any], _ any) (erasedTypedResult, error) {
|
||||
return entry.extract(ctx, impl, contracts.TypedExtractionRequest[any]{Operation: op})
|
||||
return entry.extract(ctx, impl, contracts.ExtractionInput{}, ExecutionContext[any]{Controls: op.Controls, Execution: op.Execution, ReuseEligible: op.ReuseEligible, Assess: op.Assess, Cache: op.Cache})
|
||||
}
|
||||
case StageMerge:
|
||||
registry := NewMergerRegistry()
|
||||
@@ -135,6 +134,25 @@ func registerCollaborationProducer(t *testing.T, stage ModuleStage, impl *collab
|
||||
}
|
||||
}
|
||||
|
||||
func TestTypedExtractionRegistryAssessesPreparedExactType(t *testing.T) {
|
||||
impl := &collaborationProducer{}
|
||||
invoke := registerCollaborationProducer(t, StageExtract, impl)
|
||||
assessments := 0
|
||||
result, err := invoke(context.Background(), contracts.ProducerOperation[any]{ReuseEligible: true, Assess: func(_ context.Context, value any) (contracts.CandidateAssessment, error) {
|
||||
assessments++
|
||||
if reflect.TypeOf(value) != reflect.TypeFor[collaborationValue]() || value != (collaborationValue{"candidate"}) {
|
||||
t.Fatalf("candidate = %#v", value)
|
||||
}
|
||||
return contracts.CandidateAssessment{}, nil
|
||||
}}, nil)
|
||||
if err != nil || result.Value != (collaborationValue{"candidate"}) || result.Outcome.Disposition != contracts.ProducerAccepted || assessments != 1 {
|
||||
t.Fatalf("result=%#v err=%v assessments=%d", result, err, assessments)
|
||||
}
|
||||
if impl.operation.Assess != nil {
|
||||
t.Fatal("framework assessor leaked into preparation")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTypedRegistryRejectsIncompatibleOperationValues(t *testing.T) {
|
||||
for _, stage := range []ModuleStage{StageMerge, StageNormalize} {
|
||||
invoke := registerCollaborationProducer(t, stage, &collaborationProducer{})
|
||||
|
||||
@@ -15,8 +15,9 @@ type typedTestExtractor[T any] struct{ key string }
|
||||
|
||||
func (e typedTestExtractor[T]) Key() string { return e.key }
|
||||
func (typedTestExtractor[T]) ReferenceSlots() []contracts.ReferenceSlot { return nil }
|
||||
func (typedTestExtractor[T]) Extract(context.Context, contracts.TypedExtractionRequest[T]) (contracts.TypedExtractionResult[T], error) {
|
||||
return contracts.TypedExtractionResult[T]{}, nil
|
||||
func (typedTestExtractor[T]) PrepareExtraction(context.Context, contracts.ExtractionInput) (contracts.PreparedOperation[T], error) {
|
||||
var value T
|
||||
return contracts.DeterministicOperation[T]{Evaluation: contracts.CandidateEvaluation[T]{Result: contracts.EvaluationView[T]{Candidate: &value}}}, nil
|
||||
}
|
||||
|
||||
type typedTestMerger[T any] struct{ key string }
|
||||
|
||||
Reference in New Issue
Block a user