Preserve capture failures when a run is canceled
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
@@ -23,3 +24,27 @@ func TestRunErrorSelectionPreservesOperationalDeadlines(t *testing.T) {
|
||||
t.Fatal(got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunErrorSelectionRetainsOnlyCaptureBearingJobsOnCancellation(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
firstCause, extraCause, laterCause := errors.New("first capture"), errors.New("extra capture"), errors.New("later capture")
|
||||
first := &contracts.CaptureError{Cause: firstCause}
|
||||
later := &contracts.CaptureError{Cause: laterCause}
|
||||
ordinary := errors.New("ordinary sibling failure")
|
||||
got := selectRunError(ctx, []orderedRunError{
|
||||
{stage: 2, lane: 0, err: later},
|
||||
{stage: 0, lane: -1, err: ordinary},
|
||||
{stage: 0, lane: 0, err: errors.Join(first, &contracts.CaptureError{Cause: extraCause})},
|
||||
{stage: 0, lane: -2, err: context.Canceled},
|
||||
})
|
||||
for _, cause := range []error{context.Canceled, firstCause, extraCause, laterCause} {
|
||||
if !errors.Is(got, cause) {
|
||||
t.Fatalf("selected error %v lost cause %v", got, cause)
|
||||
}
|
||||
}
|
||||
var captured *contracts.CaptureError
|
||||
if !errors.As(got, &captured) || captured != first || errors.Is(got, ordinary) {
|
||||
t.Fatalf("selected error %v changed canonical capture order or retained ordinary siblings", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
)
|
||||
|
||||
func TestRunnerCancellationRetainsDrainedCaptureFailure(t *testing.T) {
|
||||
for _, stage := range []string{"extract", "merge", "normalize"} {
|
||||
t.Run(stage, func(t *testing.T) {
|
||||
prepared := preparedConcurrentPipeline(t, 1)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
cause := errors.New("operation outcome capture failed")
|
||||
writes := 0
|
||||
writer := &nativeEvidence{fail: func(name string) error {
|
||||
if strings.Contains(name, "/"+stage+"/") && strings.HasSuffix(name, "/outcome.json") {
|
||||
writes++
|
||||
cancel()
|
||||
return cause
|
||||
}
|
||||
return nil
|
||||
}}
|
||||
output, err := New().Run(ctx, RunInput{Prepared: prepared, RawInput: []byte("input"), Workers: 1,
|
||||
Execution: contracts.ExecutionOptions{Capture: writer}})
|
||||
var captured *contracts.CaptureError
|
||||
if !errors.Is(err, context.Canceled) || !errors.Is(err, cause) || !errors.As(err, &captured) {
|
||||
t.Fatalf("Run() error = %v, want cancellation and capture cause/type", err)
|
||||
}
|
||||
if writes != 1 || len(output.OutputFiles) != 0 || output.FallbackCount != 0 {
|
||||
t.Fatalf("writes=%d files=%d fallbacks=%d", writes, len(output.OutputFiles), output.FallbackCount)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -401,7 +402,27 @@ func classifyLaneError(lane, sentinel int, err error) orderedRunError {
|
||||
|
||||
func selectRunError(parent context.Context, values []orderedRunError) error {
|
||||
if err := parent.Err(); err != nil {
|
||||
return err
|
||||
// Cancellation stays primary, but drained jobs can also have failed
|
||||
// required capture. Retain those scoped errors, not ordinary siblings.
|
||||
var captureFailures []orderedRunError
|
||||
for _, value := range values {
|
||||
var captured *contracts.CaptureError
|
||||
if errors.As(value.err, &captured) {
|
||||
captureFailures = append(captureFailures, value)
|
||||
}
|
||||
}
|
||||
if len(captureFailures) == 0 {
|
||||
return err
|
||||
}
|
||||
sort.SliceStable(captureFailures, func(i, j int) bool {
|
||||
left, right := captureFailures[i], captureFailures[j]
|
||||
return left.stage < right.stage || left.stage == right.stage && (left.lane < right.lane || left.lane == right.lane && left.chunk < right.chunk)
|
||||
})
|
||||
causes := []error{err}
|
||||
for _, failure := range captureFailures {
|
||||
causes = append(causes, failure.err)
|
||||
}
|
||||
return errors.Join(causes...)
|
||||
}
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user