Release extraction intermediates after their last consumers
This commit is contained in:
@@ -80,6 +80,10 @@ func (r *Runner) runLanes(parent context.Context, input RunInput, step PreparedP
|
||||
groups = append(groups, state.completedGroups[index]...)
|
||||
}
|
||||
completedOutputs[lane].diagnosticGroups = append(groups, completedOutputs[lane].diagnosticGroups...)
|
||||
// All dispatched jobs have drained; incomplete values cannot be consumed.
|
||||
state.completedGroups = nil
|
||||
state.results = nil
|
||||
state.values = nil
|
||||
}
|
||||
mergeCompletedLanes(&output, completedOutputs)
|
||||
if err := selectRunError(parent, runErrors); err != nil {
|
||||
@@ -263,6 +267,9 @@ func (e *laneEngine) handleExtractResult(result extractJobResult) {
|
||||
}
|
||||
|
||||
func (e *laneEngine) handleCompletion(completion laneCompletion) {
|
||||
// The worker has returned, so merge, normalization, and capture no longer
|
||||
// borrow this lane's extraction values even if a sibling is still running.
|
||||
e.states[completion.index].values = nil
|
||||
e.completedOutputs[completion.index] = completion.output
|
||||
if completion.err != nil {
|
||||
e.runErrors = append(e.runErrors, classifyLaneError(completion.index, len(e.chunks), completion.err))
|
||||
@@ -342,6 +349,8 @@ func finalizeLaneExtract(state *laneExtractState) {
|
||||
state.fallbackCount++
|
||||
}
|
||||
}
|
||||
// Ordered values now own the needed references; the reorder map is redundant.
|
||||
state.results = nil
|
||||
}
|
||||
|
||||
func (r *Runner) continueLane(ctx context.Context, input RunInput, doc *source.SourceDocument, sourceInput contracts.LLMInputMaterial, sessionID string, chunks []source.Chunk, state *laneExtractState) (laneOutput, error) {
|
||||
|
||||
77
internal/framework/pipeline/runner_lifetime_test.go
Normal file
77
internal/framework/pipeline/runner_lifetime_test.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
)
|
||||
|
||||
func TestLaneReleasesExtractValuesWhileSiblingRuns(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
prepared := preparedConcurrentPipeline(t, 1)
|
||||
doc := typedTestDocumentWithUnits(1)
|
||||
_, chunks, err := validateAndMaterializeChunkPlan(doc, typedTestPlan(doc))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
input := RunInput{Prepared: prepared, Workers: 2, pipeline: prepared.resolved}
|
||||
states, err := initializeLaneStates(input, prepared.Steps[0], chunks)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
release := make(chan struct{})
|
||||
installExtractOperation(prepared, 1, func(ctx context.Context, request contracts.ExtractionInput) (erasedTypedResult, error) {
|
||||
select {
|
||||
case <-release:
|
||||
case <-ctx.Done():
|
||||
return erasedTypedResult{}, ctx.Err()
|
||||
}
|
||||
return erasedTypedResult{Value: typedValueForLane(1, request.Chunk.Index)}, nil
|
||||
})
|
||||
completed := make(chan struct{})
|
||||
original := prepared.Steps[0].lanes[0].typed.normalize
|
||||
prepared.Steps[0].lanes[0].typed.normalize = func(ctx context.Context, implementation any, request contracts.NormalizeInput[any], execution ExecutionContext[any]) (erasedTypedResult, error) {
|
||||
result, err := original(ctx, implementation, request, execution)
|
||||
close(completed)
|
||||
return result, err
|
||||
}
|
||||
type result struct {
|
||||
outputs []laneOutput
|
||||
errors []orderedRunError
|
||||
}
|
||||
done := make(chan result, 1)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go func() {
|
||||
outputs, failures := New().runLaneEngine(ctx, laneEngineConfig{input: input, doc: doc, chunks: chunks, states: states})
|
||||
done <- result{outputs, failures}
|
||||
}()
|
||||
<-completed
|
||||
synctest.Wait()
|
||||
if states[0].results != nil || states[0].values != nil {
|
||||
t.Fatal("completed lane retains extraction intermediates")
|
||||
}
|
||||
close(release)
|
||||
got := <-done
|
||||
if len(got.errors) != 0 {
|
||||
t.Fatalf("lane errors = %v", got.errors)
|
||||
}
|
||||
if len(got.outputs[0].normalizeOutputs) != 1 || len(got.outputs[1].normalizeOutputs) != 1 {
|
||||
t.Fatalf("normalized outputs were retired early: %#v", got.outputs)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestFinalizeLaneExtractReleasesReorderMapWithoutMutatingArtifacts(t *testing.T) {
|
||||
value := erasedExtractArtifact{Value: codecNotes{Items: []string{"kept"}}}
|
||||
state := &laneExtractState{results: map[int]extractJobResult{0: {value: value}}}
|
||||
finalizeLaneExtract(state)
|
||||
if state.results != nil || len(state.values) != 1 {
|
||||
t.Fatalf("finalized state = %#v", state)
|
||||
}
|
||||
if got := state.values[0].Value.(codecNotes).Items[0]; got != "kept" {
|
||||
t.Fatalf("artifact value = %q", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user