Collect optional validation context only when it can fit

This commit is contained in:
2026-11-17 04:25:35 +00:00
parent 44c767f285
commit d26991f0bf
9 changed files with 128 additions and 56 deletions

View File

@@ -1,8 +1,8 @@
package validators
import (
"bytes"
"encoding/json"
"reflect"
"slices"
"strings"
"testing"
@@ -97,7 +97,7 @@ func TestComposedValidationBudgetMatchesSerializedPayload(t *testing.T) {
simple := chunking.NewSimpleTokenEstimator()
full := serializedTokenCount(t, items)
for budget := 1; budget <= full; budget++ {
got, rejected, err := prepareValidationBatches(items, budget, simple)
got, rejected, err := prepareValidationBatches(items, budget, simple, nil)
want, wantRejected := serializedBudgetOracle(t, items, budget)
var rejectedIndices []int
for _, decision := range rejected {
@@ -106,7 +106,9 @@ func TestComposedValidationBudgetMatchesSerializedPayload(t *testing.T) {
}
rejectedIndices = append(rejectedIndices, decision.ProposalIndex)
}
if err != nil || !reflect.DeepEqual(got, want) || !slices.Equal(rejectedIndices, wantRejected) {
gotJSON, gotErr := json.Marshal(got)
wantJSON, wantErr := json.Marshal(want)
if err != nil || gotErr != nil || wantErr != nil || !bytes.Equal(gotJSON, wantJSON) || !slices.Equal(rejectedIndices, wantRejected) {
t.Fatalf("budget %d: composed=%+v rejected=%+v err=%v; serialized=%+v rejected=%+v",
budget, got, rejected, err, want, wantRejected)
}
@@ -153,7 +155,7 @@ func TestValidationFitsRequiredDataBeforeScanningOptionalContext(t *testing.T) {
item.GlossaryEntries = append([]glossary.Entry{first, {Description: strings.Repeat("large body ", 500)}}, make([]glossary.Entry, 5000)...)
item.NeighboringSegments = []NeighboringSegment{{Text: "neighbor"}}
estimator := &countingEstimator{}
fitted, err := fitValidationContext(item, budget, estimator)
fitted, err := fitValidationContext(item, budget, estimator, nil)
if err != nil || len(fitted.item.GlossaryEntries) != 1 || fitted.item.GlossaryEntries[0].ID != first.ID ||
len(fitted.item.NeighboringSegments) != 0 || fitted.tokens != budget {
t.Fatalf("incorrect usable prefix: %+v (%v)", fitted, err)
@@ -162,8 +164,32 @@ func TestValidationFitsRequiredDataBeforeScanningOptionalContext(t *testing.T) {
t.Fatalf("measured beyond the first excess: %d measurements", estimator.measurements)
}
estimator.measurements = 0
fitted, err = fitValidationContext(item, 1, estimator)
fitted, err = fitValidationContext(item, 1, estimator, nil)
if err != nil || fitted.tokens <= 1 || estimator.measurements != 1 {
t.Fatalf("oversized required data: tokens=%d measurements=%d error=%v", fitted.tokens, estimator.measurements, err)
}
}
func TestValidationSkipsOptionalContextWithoutBudget(t *testing.T) {
item := LLMValidationItem{CorrectionIndex: 7, OriginalText: "teh", CorrectedText: "the"}
estimator := chunking.NewSimpleTokenEstimator()
required := serializedTokenCount(t, []LLMValidationItem{item})
loads := 0
optional := func(LLMValidationItem) ([]NeighboringSegment, []glossary.Entry) {
loads++
return nil, []glossary.Entry{{Name: "optional"}}
}
for _, budget := range []int{required - 1, required} {
batches, oversized, err := prepareValidationBatches([]LLMValidationItem{item}, budget, estimator, optional)
if err != nil || loads != 0 {
t.Fatalf("budget=%d loads=%d error=%v", budget, loads, err)
}
if budget < required {
if len(batches) != 0 || len(oversized) != 1 {
t.Fatalf("oversized result: batches=%+v rejected=%+v", batches, oversized)
}
} else if len(batches) != 1 || len(oversized) != 0 || batches[0].TokenCount != required {
t.Fatalf("exact fit: batches=%+v rejected=%+v", batches, oversized)
}
}
}