Share the source index with validators

This commit is contained in:
2026-11-26 05:12:19 +00:00
parent 60e732bbe8
commit d66cfe3986
43 changed files with 191 additions and 68 deletions

View File

@@ -45,20 +45,70 @@ 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)
expected := 6 + len(prepared.chunkValidators.validators)
for _, step := range prepared.Steps {
lane := step.lanes[0]
expected += 3*len(lane.extractValidators.validators) + len(lane.mergeValidators.validators) + len(lane.normalizeValidators.validators)
}
indexes := make(chan *source.DocumentIndex, expected)
observe := func(doc *source.SourceDocument, index *source.DocumentIndex) error {
if index == nil {
return fmt.Errorf("missing source index")
}
for position, unit := range doc.Units {
if got, ok := index.Position(unit.ID); !ok || got != position {
return fmt.Errorf("source index position for %d = %d, %t, want %d", unit.ID, got, ok, position)
}
}
select {
case indexes <- index:
default:
return fmt.Errorf("unexpected extra index observation")
}
return nil
}
wrapValidators := func(chain *preparedValidatorChain) {
for i := range chain.validators {
validator := &chain.validators[i]
switch validator.resolved.Target {
case ValidatorTargetTyped:
original := validator.typedValidate
validator.typedValidate = func(ctx context.Context, impl any, target typedValidationTarget) (contracts.ValidationResult, error) {
if err := observe(target.source, target.sourceIndex); err != nil {
return contracts.ValidationResult{}, err
}
return original(ctx, impl, target)
}
case ValidatorTargetSerialized:
original := validator.serialized
validator.serialized = serializedValidationFunc(func(ctx context.Context, req contracts.SerializedValidationRequest) (contracts.ValidationResult, error) {
if err := observe(req.Source, req.SourceIndex); err != nil {
return contracts.ValidationResult{}, err
}
return original.Validate(ctx, req)
})
case ValidatorTargetChunk:
original := validator.chunk
validator.chunk = chunkAssessmentValidator(func(ctx context.Context, req contracts.ChunkValidationRequest) (contracts.ValidationResult, error) {
if err := observe(req.Source, req.SourceIndex); err != nil {
return contracts.ValidationResult{}, err
}
return original.Validate(ctx, req)
})
}
}
}
wrapValidators(&prepared.chunkValidators)
for step := range prepared.Steps {
lane := &prepared.Steps[step].lanes[0]
wrapValidators(&lane.extractValidators)
wrapValidators(&lane.mergeValidators)
wrapValidators(&lane.normalizeValidators)
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")
if err := observe(request.Source, request.SourceIndex); err != nil {
return erasedTypedResult{}, err
}
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)
}
}
@@ -74,11 +124,11 @@ func TestRunnerSharesWholeSourceIndexAcrossJobsAndSteps(t *testing.T) {
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))
if len(indexes) != expected {
t.Fatalf("observed %d extraction/validation calls, want %d", len(indexes), expected)
}
first := <-indexes
for i := 1; i < 6; i++ {
for i := 1; i < expected; i++ {
if got := <-indexes; got != first {
t.Fatal("jobs did not share the run source index")
}