Preserve diagnostic aggregation failures during finalization
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user