Preserve diagnostic aggregation failures during finalization
This commit is contained in:
@@ -82,9 +82,12 @@ func finalizeDiagnostics(output *runState) error {
|
||||
aggregator := frameworkdiagnostics.Aggregator{}
|
||||
for _, group := range output.diagnosticGroups {
|
||||
if err := aggregator.Add(group); err != nil {
|
||||
output.Diagnostics = aggregator.Collection()
|
||||
output.diagnosticGroups = nil
|
||||
return err
|
||||
}
|
||||
}
|
||||
output.Diagnostics = aggregator.Collection()
|
||||
output.diagnosticGroups = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime"
|
||||
"path"
|
||||
@@ -98,6 +99,19 @@ func (r *Runner) Run(ctx context.Context, input RunInput) (RunOutput, error) {
|
||||
}
|
||||
|
||||
func (r *Runner) run(ctx context.Context, input RunInput) (output runState, err error) {
|
||||
diagnosticsFinalized := false
|
||||
finishDiagnostics := func() error {
|
||||
diagnosticsFinalized = true
|
||||
if finalErr := finalizeDiagnostics(&output); finalErr != nil {
|
||||
return fmt.Errorf("aggregate diagnostics: %w", finalErr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
defer func() {
|
||||
if !diagnosticsFinalized {
|
||||
err = errors.Join(err, finishDiagnostics())
|
||||
}
|
||||
}()
|
||||
if r == nil {
|
||||
return output, fmt.Errorf("runner must not be nil")
|
||||
}
|
||||
@@ -127,7 +141,7 @@ func (r *Runner) run(ctx context.Context, input RunInput) (output runState, err
|
||||
}
|
||||
inputDiagnostics, diagnosticErr := promoteProducerDiagnostics(input.Diagnostics, contracts.DiagnosticOrigin{Stage: contracts.DiagnosticOriginStageReferences}, nil)
|
||||
if diagnosticErr != nil {
|
||||
return failOutput(output), fmt.Errorf("promote input diagnostics: %w", diagnosticErr)
|
||||
return output, fmt.Errorf("promote input diagnostics: %w", diagnosticErr)
|
||||
}
|
||||
appendDiagnosticGroups(&output, inputDiagnostics)
|
||||
if debugRecorder.Enabled() {
|
||||
@@ -140,10 +154,10 @@ func (r *Runner) run(ctx context.Context, input RunInput) (output runState, err
|
||||
"run_id": input.RunID,
|
||||
},
|
||||
}); err != nil {
|
||||
return failOutput(output), fmt.Errorf("write debug run artifact: %w", err)
|
||||
return output, fmt.Errorf("write debug run artifact: %w", err)
|
||||
}
|
||||
if err := debugRecorder.WriteJSON("composition.json", captureComposition(input.pipeline)); err != nil {
|
||||
return failOutput(output), &contracts.CaptureError{Cause: err}
|
||||
return output, &contracts.CaptureError{Cause: err}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,28 +176,28 @@ func (r *Runner) run(ctx context.Context, input RunInput) (output runState, err
|
||||
Metadata: input.Metadata,
|
||||
},
|
||||
}); err != nil {
|
||||
return failOutput(output), fmt.Errorf("write source debug artifact: %w", err)
|
||||
return output, fmt.Errorf("write source debug artifact: %w", err)
|
||||
}
|
||||
}
|
||||
requestMetadata, err := cloneMetadata(input.Metadata)
|
||||
if err != nil {
|
||||
return failOutput(output), fmt.Errorf("clone input adapter metadata: %w", err)
|
||||
return output, fmt.Errorf("clone input adapter metadata: %w", err)
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return failOutput(output), err
|
||||
return output, err
|
||||
}
|
||||
doc, err = adapter.Parse(ctx, contracts.ParseRequest{
|
||||
SourceID: input.SourceID, Path: input.Path, Raw: input.RawInput,
|
||||
LLMProfile: input.pipeline.Input.LLMProfile, Metadata: requestMetadata,
|
||||
})
|
||||
if ctxErr := ctx.Err(); ctxErr != nil {
|
||||
return failOutput(output), ctxErr
|
||||
return output, ctxErr
|
||||
}
|
||||
if err != nil {
|
||||
return failOutput(output), fmt.Errorf("parse input with adapter %q: %w", adapter.Key(), err)
|
||||
return output, fmt.Errorf("parse input with adapter %q: %w", adapter.Key(), err)
|
||||
}
|
||||
if err := source.ValidateDocument(doc); err != nil {
|
||||
return failOutput(output), fmt.Errorf("validate source document: %w", err)
|
||||
return output, fmt.Errorf("validate source document: %w", err)
|
||||
}
|
||||
index := source.NewDocumentIndex(doc)
|
||||
input.sourceIndex = &index
|
||||
@@ -197,7 +211,7 @@ func (r *Runner) run(ctx context.Context, input RunInput) (output runState, err
|
||||
"document": debugSourceDocumentEnvelope(doc),
|
||||
},
|
||||
}); err != nil {
|
||||
return failOutput(output), fmt.Errorf("write source debug artifact: %w", err)
|
||||
return output, fmt.Errorf("write source debug artifact: %w", err)
|
||||
}
|
||||
}
|
||||
sourceInput := sourceInputMaterial(input.Path, input.RawInput)
|
||||
@@ -216,13 +230,13 @@ func (r *Runner) run(ctx context.Context, input RunInput) (output runState, err
|
||||
"metadata": input.Metadata,
|
||||
},
|
||||
}); err != nil {
|
||||
return failOutput(output), fmt.Errorf("write chunk debug artifact: %w", err)
|
||||
return output, fmt.Errorf("write chunk debug artifact: %w", err)
|
||||
}
|
||||
}
|
||||
chunkResult, err := r.runChunkPlan(ctx, input, doc, sourceInput, sessionID)
|
||||
appendDiagnosticGroups(&output, chunkResult.diagnostics)
|
||||
if err != nil {
|
||||
return failOutput(output), err
|
||||
return output, err
|
||||
}
|
||||
if debugRecorder.Enabled() {
|
||||
chunkDebugPayload := map[string]any{
|
||||
@@ -239,14 +253,14 @@ func (r *Runner) run(ctx context.Context, input RunInput) (output runState, err
|
||||
StartedAt: chunkStarted,
|
||||
Payload: chunkDebugPayload,
|
||||
}); err != nil {
|
||||
return failOutput(output), fmt.Errorf("write chunk debug artifact: %w", err)
|
||||
return output, fmt.Errorf("write chunk debug artifact: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
var acceptedChunkMap *contracts.SerializedArtifact
|
||||
if chunkResult.accepted {
|
||||
if chunkResult.plan == nil {
|
||||
return failOutput(output), fmt.Errorf("accepted chunk plan is missing")
|
||||
return output, fmt.Errorf("accepted chunk plan is missing")
|
||||
}
|
||||
chunkProfile := ""
|
||||
if input.pipeline.ChunkExecutionClass == contracts.ExecutionClassLLMBacked {
|
||||
@@ -264,7 +278,7 @@ func (r *Runner) run(ctx context.Context, input RunInput) (output runState, err
|
||||
},
|
||||
})
|
||||
if buildErr != nil {
|
||||
return failOutput(output), fmt.Errorf("serialize accepted chunk map: %w", buildErr)
|
||||
return output, fmt.Errorf("serialize accepted chunk map: %w", buildErr)
|
||||
}
|
||||
acceptedChunkMap = &artifact
|
||||
}
|
||||
@@ -275,28 +289,28 @@ func (r *Runner) run(ctx context.Context, input RunInput) (output runState, err
|
||||
output.FallbackCount++
|
||||
}
|
||||
if err := r.runPreparedSteps(ctx, input, doc, sourceInput, sessionID, chunkResult.chunks, &output); err != nil {
|
||||
return failOutput(output), err
|
||||
return output, err
|
||||
}
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return failOutput(output), err
|
||||
return output, err
|
||||
}
|
||||
|
||||
if err := finalizeDiagnostics(&output); err != nil {
|
||||
return failOutput(output), fmt.Errorf("aggregate diagnostics: %w", err)
|
||||
if err := finishDiagnostics(); err != nil {
|
||||
return output, err
|
||||
}
|
||||
|
||||
encoder := input.Prepared.output
|
||||
if err := ctx.Err(); err != nil {
|
||||
return failOutput(output), err
|
||||
return output, err
|
||||
}
|
||||
outputStarted := time.Now().UTC()
|
||||
evidenceArtifact, evidenceSummary, err := buildOutputEvidenceContext(input.Prepared, doc, output.normalizeOutputs, debugRecorder.Enabled())
|
||||
if err != nil {
|
||||
return failOutput(output), err
|
||||
return output, err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return failOutput(output), err
|
||||
return output, err
|
||||
}
|
||||
if evidenceSummary != nil {
|
||||
if err := writeDebugTimed(debugRecorder, "output/evidence-context.json", debugTimedEnvelope{
|
||||
@@ -305,10 +319,10 @@ func (r *Runner) run(ctx context.Context, input RunInput) (output runState, err
|
||||
StartedAt: outputStarted,
|
||||
Payload: *evidenceSummary,
|
||||
}); err != nil {
|
||||
return failOutput(output), fmt.Errorf("write evidence context debug artifact: %w", err)
|
||||
return output, fmt.Errorf("write evidence context debug artifact: %w", err)
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return failOutput(output), err
|
||||
return output, err
|
||||
}
|
||||
}
|
||||
if debugRecorder.Enabled() {
|
||||
@@ -325,11 +339,11 @@ func (r *Runner) run(ctx context.Context, input RunInput) (output runState, err
|
||||
StartedAt: outputStarted,
|
||||
Payload: outputDebugPayload,
|
||||
}); err != nil {
|
||||
return failOutput(output), fmt.Errorf("write output debug artifact: %w", err)
|
||||
return output, fmt.Errorf("write output debug artifact: %w", err)
|
||||
}
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return failOutput(output), err
|
||||
return output, err
|
||||
}
|
||||
encoded, err := encoder.Encode(ctx, contracts.OutputRequest{
|
||||
NormalizeOutputs: cloneSerializedOutputs(output.normalizeOutputs),
|
||||
@@ -337,14 +351,14 @@ func (r *Runner) run(ctx context.Context, input RunInput) (output runState, err
|
||||
EvidenceContext: contracts.CloneSerializedArtifactPointer(evidenceArtifact),
|
||||
})
|
||||
if ctxErr := ctx.Err(); ctxErr != nil {
|
||||
return failOutput(output), ctxErr
|
||||
return output, ctxErr
|
||||
}
|
||||
if err != nil {
|
||||
return failOutput(output), fmt.Errorf("encode output with encoder %q: %w", encoder.Key(), err)
|
||||
return output, fmt.Errorf("encode output with encoder %q: %w", encoder.Key(), err)
|
||||
}
|
||||
files, err := outputFilesFromResult(encoded)
|
||||
if err != nil {
|
||||
return failOutput(output), fmt.Errorf("validate output files from encoder %q: %w", encoder.Key(), err)
|
||||
return output, fmt.Errorf("validate output files from encoder %q: %w", encoder.Key(), err)
|
||||
}
|
||||
if debugRecorder.Enabled() {
|
||||
if err := writeDebugTimed(debugRecorder, "output/output.json", debugTimedEnvelope{
|
||||
@@ -355,11 +369,11 @@ func (r *Runner) run(ctx context.Context, input RunInput) (output runState, err
|
||||
"files": debugOutputFiles(files),
|
||||
},
|
||||
}); err != nil {
|
||||
return failOutput(output), fmt.Errorf("write output debug artifact: %w", err)
|
||||
return output, fmt.Errorf("write output debug artifact: %w", err)
|
||||
}
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return failOutput(output), err
|
||||
return output, err
|
||||
}
|
||||
output.OutputFiles = files
|
||||
|
||||
@@ -469,12 +483,6 @@ func startedTime(t time.Time) time.Time {
|
||||
return t.UTC()
|
||||
}
|
||||
|
||||
func failOutput(output runState) runState {
|
||||
// Attempt to preserve diagnostics accumulated before a later failure.
|
||||
_ = finalizeDiagnostics(&output)
|
||||
return output
|
||||
}
|
||||
|
||||
func outputFilesFromResult(result contracts.OutputResult) ([]contracts.OutputFile, error) {
|
||||
out := make([]contracts.OutputFile, 0, len(result.Files))
|
||||
for _, file := range result.Files {
|
||||
|
||||
90
internal/framework/pipeline/runner_finalization_test.go
Normal file
90
internal/framework/pipeline/runner_finalization_test.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"math"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
)
|
||||
|
||||
func overflowingInputDiagnostics() []contracts.ProducerDiagnostic {
|
||||
first := producerDiagnostic("first", "retained")
|
||||
first.OccurrenceCount = math.MaxInt
|
||||
first.OmittedSampleCount = math.MaxInt - 1
|
||||
second := producerDiagnostic("second", "overflow")
|
||||
second.ReasonCode = "another_reason"
|
||||
return []contracts.ProducerDiagnostic{first, second}
|
||||
}
|
||||
|
||||
func TestRunnerFinalizationPreservesPrefixAndExecutionFailure(t *testing.T) {
|
||||
prepared := preparedOrderedPipeline(t, 1, orderedLaneSpec{"notes", "notes"})
|
||||
failure := errors.New("extraction failed")
|
||||
installExtractOperation(prepared, 0, func(context.Context, contracts.ExtractionInput) (erasedTypedResult, error) {
|
||||
return erasedTypedResult{}, failure
|
||||
})
|
||||
output, err := New().Run(context.Background(), RunInput{Prepared: prepared, RawInput: []byte("input"), Diagnostics: overflowingInputDiagnostics()})
|
||||
if !errors.Is(err, failure) || !strings.Contains(err.Error(), "diagnostic occurrence count overflow") {
|
||||
t.Fatalf("Run() error = %v, want extraction failure and aggregation overflow", err)
|
||||
}
|
||||
if len(output.Diagnostics.Groups) != 1 || output.Diagnostics.Groups[0].OccurrenceCount != math.MaxInt {
|
||||
t.Fatalf("diagnostic prefix = %#v, want first group", output.Diagnostics.Groups)
|
||||
}
|
||||
if len(output.OutputFiles) != 0 {
|
||||
t.Fatal("failed run published output")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerFinalizationFailurePreventsEncoding(t *testing.T) {
|
||||
prepared := preparedOrderedPipeline(t, 1, orderedLaneSpec{"notes", "notes"})
|
||||
encoder := &countingOrderedOutput{}
|
||||
prepared.output = encoder
|
||||
output, err := New().Run(context.Background(), RunInput{Prepared: prepared, RawInput: []byte("input"), Diagnostics: overflowingInputDiagnostics()})
|
||||
if err == nil || !strings.Contains(err.Error(), "diagnostic occurrence count overflow") {
|
||||
t.Fatalf("Run() error = %v, want aggregation overflow", err)
|
||||
}
|
||||
if len(output.Diagnostics.Groups) != 1 || output.Diagnostics.Groups[0].OccurrenceCount != math.MaxInt {
|
||||
t.Fatalf("diagnostic prefix = %#v, want first group", output.Diagnostics.Groups)
|
||||
}
|
||||
if encoder.calls.Load() != 0 || len(output.OutputFiles) != 0 {
|
||||
t.Fatal("aggregation failure published output")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerFinalizationRetainsCancellation(t *testing.T) {
|
||||
prepared := preparedOrderedPipeline(t, 1, orderedLaneSpec{"notes", "notes"})
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
installExtractOperation(prepared, 0, func(context.Context, contracts.ExtractionInput) (erasedTypedResult, error) {
|
||||
cancel()
|
||||
return erasedTypedResult{}, context.Canceled
|
||||
})
|
||||
output, err := New().Run(ctx, RunInput{Prepared: prepared, RawInput: []byte("input"), Diagnostics: overflowingInputDiagnostics()})
|
||||
if !errors.Is(err, context.Canceled) || !strings.Contains(err.Error(), "diagnostic occurrence count overflow") {
|
||||
t.Fatalf("Run() error = %v, want cancellation and aggregation overflow", err)
|
||||
}
|
||||
if len(output.Diagnostics.Groups) != 1 {
|
||||
t.Fatalf("diagnostic prefix = %#v, want first group", output.Diagnostics.Groups)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerFinalizationRetainsCaptureFailure(t *testing.T) {
|
||||
prepared := preparedOrderedPipeline(t, 1, orderedLaneSpec{"notes", "notes"})
|
||||
cause := errors.New("capture failed")
|
||||
writer := &nativeEvidence{fail: func(name string) error {
|
||||
if name == "source/input.json" {
|
||||
return cause
|
||||
}
|
||||
return nil
|
||||
}}
|
||||
output, err := New().Run(context.Background(), RunInput{Prepared: prepared, RawInput: []byte("input"), Diagnostics: overflowingInputDiagnostics(), Execution: contracts.ExecutionOptions{Capture: writer}})
|
||||
var captureErr *contracts.CaptureError
|
||||
if !errors.Is(err, cause) || !errors.As(err, &captureErr) || !strings.Contains(err.Error(), "diagnostic occurrence count overflow") {
|
||||
t.Fatalf("Run() error = %v, want capture failure and aggregation overflow", err)
|
||||
}
|
||||
if len(output.Diagnostics.Groups) != 1 {
|
||||
t.Fatalf("diagnostic prefix = %#v, want first group", output.Diagnostics.Groups)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user