Move chunk planning into core operation execution
This commit is contained in:
@@ -39,27 +39,27 @@ func (c *Chunker) ReferenceSlots() []contracts.ReferenceSlot {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Chunker) Plan(ctx context.Context, req contracts.ChunkRequest) (contracts.ChunkPlanResult, error) {
|
||||
func (c *Chunker) PrepareChunk(ctx context.Context, req contracts.ChunkInput) (contracts.PreparedOperation[source.ChunkPlan], error) {
|
||||
if c == nil {
|
||||
return contracts.ChunkPlanResult{}, chunkerErrorf("chunker must not be nil")
|
||||
return nil, chunkerErrorf("chunker must not be nil")
|
||||
}
|
||||
if c.options.MaxUnits <= 0 || c.options.OverlapUnits < 0 || c.options.OverlapUnits >= c.options.MaxUnits {
|
||||
return contracts.ChunkPlanResult{}, chunkerErrorf("chunker options must be initialized by construction")
|
||||
return nil, chunkerErrorf("chunker options must be initialized by construction")
|
||||
}
|
||||
if ctx == nil {
|
||||
return contracts.ChunkPlanResult{}, chunkerErrorf("context must not be nil")
|
||||
return nil, chunkerErrorf("context must not be nil")
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return contracts.ChunkPlanResult{}, chunkerErrorf("context error before chunking: %w", err)
|
||||
return nil, chunkerErrorf("context error before chunking: %w", err)
|
||||
}
|
||||
if req.Source == nil {
|
||||
return contracts.ChunkPlanResult{}, chunkerErrorf("source must not be nil")
|
||||
return nil, chunkerErrorf("source must not be nil")
|
||||
}
|
||||
if len(req.Source.Units) == 0 {
|
||||
return contracts.ChunkPlanResult{}, chunkerErrorf("source units must not be empty")
|
||||
return nil, chunkerErrorf("source units must not be empty")
|
||||
}
|
||||
if err := source.ValidateDocument(req.Source); err != nil {
|
||||
return contracts.ChunkPlanResult{}, chunkerErrorf("validate source document: %w", err)
|
||||
return nil, chunkerErrorf("validate source document: %w", err)
|
||||
}
|
||||
|
||||
step := c.options.MaxUnits - c.options.OverlapUnits
|
||||
@@ -78,7 +78,8 @@ func (c *Chunker) Plan(ctx context.Context, req contracts.ChunkRequest) (contrac
|
||||
}
|
||||
}
|
||||
|
||||
return contracts.ChunkPlanResult{Plan: source.ChunkPlan{SourceDigest: req.Source.Digest, Ranges: ranges}}, nil
|
||||
plan := source.ChunkPlan{SourceDigest: req.Source.Digest, Ranges: ranges}
|
||||
return contracts.DeterministicOperation[source.ChunkPlan]{Evaluation: contracts.CandidateEvaluation[source.ChunkPlan]{Result: contracts.EvaluationView[source.ChunkPlan]{Candidate: &plan}}}, nil
|
||||
}
|
||||
|
||||
func ModuleSpec() pipeline.ModuleSpec {
|
||||
|
||||
@@ -49,47 +49,47 @@ func TestModuleSpecAndRegister(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("BuildWithRequest(%q) error = %v, want nil", Key, err)
|
||||
}
|
||||
result, err := configured.Plan(context.Background(), contracts.ChunkRequest{Source: testSource(2)})
|
||||
if err != nil || len(result.Plan.Ranges) != 2 {
|
||||
result, err := runChunk(context.Background(), configured, contracts.ChunkInput{Source: testSource(2)})
|
||||
if err != nil || len(result.Value.Ranges) != 2 {
|
||||
t.Fatalf("constructed chunker result = %#v, %v; want two ranges", result, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanUsesDefaultsForSingleRange(t *testing.T) {
|
||||
result, err := newChunker(t, nil).Plan(context.Background(), contracts.ChunkRequest{Source: testSource(3)})
|
||||
result, err := runChunk(context.Background(), newChunker(t, nil), contracts.ChunkInput{Source: testSource(3)})
|
||||
if err != nil {
|
||||
t.Fatalf("Plan() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
if result.Plan.SourceDigest != "sha256:source" || !reflect.DeepEqual(result.Plan.Ranges, []source.ChunkRange{{StartUnitID: 1, EndUnitID: 3}}) {
|
||||
t.Fatalf("Plan = %#v, want source digest and one complete range", result.Plan)
|
||||
if result.Value.SourceDigest != "sha256:source" || !reflect.DeepEqual(result.Value.Ranges, []source.ChunkRange{{StartUnitID: 1, EndUnitID: 3}}) {
|
||||
t.Fatalf("Plan = %#v, want source digest and one complete range", result.Value)
|
||||
}
|
||||
if len(result.Plan.Annotations) != 0 || len(result.Plan.Ranges[0].Annotations) != 0 {
|
||||
t.Fatalf("Plan annotations = %#v / %#v, want none", result.Plan.Annotations, result.Plan.Ranges[0].Annotations)
|
||||
if len(result.Value.Annotations) != 0 || len(result.Value.Ranges[0].Annotations) != 0 {
|
||||
t.Fatalf("Plan annotations = %#v / %#v, want none", result.Value.Annotations, result.Value.Ranges[0].Annotations)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanExactBoundaries(t *testing.T) {
|
||||
result, err := newChunker(t, map[string]any{"max_units": 2}).Plan(context.Background(), contracts.ChunkRequest{Source: testSource(6)})
|
||||
result, err := runChunk(context.Background(), newChunker(t, map[string]any{"max_units": 2}), contracts.ChunkInput{Source: testSource(6)})
|
||||
if err != nil {
|
||||
t.Fatalf("Plan() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
want := []source.ChunkRange{{StartUnitID: 1, EndUnitID: 2}, {StartUnitID: 3, EndUnitID: 4}, {StartUnitID: 5, EndUnitID: 6}}
|
||||
if !reflect.DeepEqual(result.Plan.Ranges, want) {
|
||||
t.Fatalf("ranges = %#v, want %#v", result.Plan.Ranges, want)
|
||||
if !reflect.DeepEqual(result.Value.Ranges, want) {
|
||||
t.Fatalf("ranges = %#v, want %#v", result.Value.Ranges, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanOverlap(t *testing.T) {
|
||||
result, err := newChunker(t, map[string]any{"max_units": 3, "overlap_units": 1}).Plan(context.Background(), contracts.ChunkRequest{Source: testSource(7)})
|
||||
result, err := runChunk(context.Background(), newChunker(t, map[string]any{"max_units": 3, "overlap_units": 1}), contracts.ChunkInput{Source: testSource(7)})
|
||||
if err != nil {
|
||||
t.Fatalf("Plan() error = %v, want nil", err)
|
||||
}
|
||||
|
||||
want := []source.ChunkRange{{StartUnitID: 1, EndUnitID: 3}, {StartUnitID: 3, EndUnitID: 5}, {StartUnitID: 5, EndUnitID: 7}}
|
||||
if !reflect.DeepEqual(result.Plan.Ranges, want) {
|
||||
t.Fatalf("ranges = %#v, want %#v", result.Plan.Ranges, want)
|
||||
if !reflect.DeepEqual(result.Value.Ranges, want) {
|
||||
t.Fatalf("ranges = %#v, want %#v", result.Value.Ranges, want)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,7 +151,7 @@ func TestPlanRejectsEmptySource(t *testing.T) {
|
||||
doc := testSource(1)
|
||||
doc.Units = nil
|
||||
|
||||
_, err := newChunker(t, nil).Plan(context.Background(), contracts.ChunkRequest{Source: doc})
|
||||
_, err := runChunk(context.Background(), newChunker(t, nil), contracts.ChunkInput{Source: doc})
|
||||
if err == nil {
|
||||
t.Fatal("Plan() error = nil, want empty source error")
|
||||
}
|
||||
@@ -163,20 +163,20 @@ func TestPlanRejectsEmptySource(t *testing.T) {
|
||||
func TestPlanDoesNotRetainSourceUnits(t *testing.T) {
|
||||
doc := testSource(2)
|
||||
|
||||
result, err := newChunker(t, map[string]any{"max_units": 1}).Plan(context.Background(), contracts.ChunkRequest{Source: doc})
|
||||
result, err := runChunk(context.Background(), newChunker(t, map[string]any{"max_units": 1}), contracts.ChunkInput{Source: doc})
|
||||
if err != nil {
|
||||
t.Fatalf("Plan() error = %v, want nil", err)
|
||||
}
|
||||
if len(result.Plan.Ranges) != 2 {
|
||||
t.Fatalf("len(Ranges) = %d, want 2", len(result.Plan.Ranges))
|
||||
if len(result.Value.Ranges) != 2 {
|
||||
t.Fatalf("len(Ranges) = %d, want 2", len(result.Value.Ranges))
|
||||
}
|
||||
|
||||
doc.Units[0].ID = 99
|
||||
doc.Units[0].Ref.SourceID = "changed"
|
||||
doc.Units[0].Metadata["speaker"] = "changed"
|
||||
|
||||
if result.Plan.Ranges[0].StartUnitID != 1 || result.Plan.Ranges[0].EndUnitID != 1 {
|
||||
t.Fatalf("first range changed after source mutation: %#v", result.Plan.Ranges[0])
|
||||
if result.Value.Ranges[0].StartUnitID != 1 || result.Value.Ranges[0].EndUnitID != 1 {
|
||||
t.Fatalf("first range changed after source mutation: %#v", result.Value.Ranges[0])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,6 +189,14 @@ func newChunker(t *testing.T, rawOptions map[string]any) *Chunker {
|
||||
return New(options)
|
||||
}
|
||||
|
||||
func runChunk(ctx context.Context, chunker contracts.Chunker, input contracts.ChunkInput) (pipeline.OperationResult[source.ChunkPlan], error) {
|
||||
return pipeline.ExecuteOperation(ctx, pipeline.ExecutionContext[source.ChunkPlan]{Assess: func(context.Context, source.ChunkPlan) (contracts.CandidateAssessment, error) {
|
||||
return contracts.CandidateAssessment{}, nil
|
||||
}}, func(ctx context.Context) (contracts.PreparedOperation[source.ChunkPlan], error) {
|
||||
return chunker.PrepareChunk(ctx, input)
|
||||
})
|
||||
}
|
||||
|
||||
func testSource(count int) *source.SourceDocument {
|
||||
units := make([]source.SourceUnit, 0, count)
|
||||
for i := 1; i <= count; i++ {
|
||||
|
||||
Reference in New Issue
Block a user