From a88141ef4ae0ef22881218a88a029a18d8384940 Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Tue, 17 Nov 2026 03:55:29 +0000 Subject: [PATCH] Use consistent reasons for typed failures --- docs/roadmap/remediation.md | 4 ++++ internal/framework/processreport/failures.go | 4 ++-- .../framework/processreport/report_test.go | 23 +++++++++++++++++++ .../framework/structuredoutput/failure.go | 12 +++++++--- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/docs/roadmap/remediation.md b/docs/roadmap/remediation.md index b870530..bc9af1f 100644 --- a/docs/roadmap/remediation.md +++ b/docs/roadmap/remediation.md @@ -110,6 +110,10 @@ report, and that the peer's available phase, reason, and origin remain present. ### R3 — Use the Same Missing-Reason Fallback in Run Reports (P3) +**Complete.** The operation and run report use the same typed reason fallback. +The regression covers a missing reason with a wrapped deadline, retained +operation origin and cause, and distinct parent cancellation. + **Affected plan:** Stage 12, item 2, and Stage 17, item 1. **Evidence:** [Operation projection](../../internal/framework/structuredoutput/failure.go#L42) diff --git a/internal/framework/processreport/failures.go b/internal/framework/processreport/failures.go index 1873070..f8e479b 100644 --- a/internal/framework/processreport/failures.go +++ b/internal/framework/processreport/failures.go @@ -24,8 +24,8 @@ func processingFailure(cause error, phase string) RunFailure { var model *structuredoutput.Failure var infeasible *chunking.InfeasibleTargetError switch { - case errors.As(cause, &model) && model.Reason != "": - failure.Reason = model.Reason + case errors.As(cause, &model): + failure.Reason = model.EffectiveReason() case errors.Is(cause, context.Canceled), errors.Is(cause, context.DeadlineExceeded): failure.Reason = outcomes.ReasonCanceled case errors.As(cause, &infeasible): diff --git a/internal/framework/processreport/report_test.go b/internal/framework/processreport/report_test.go index f704b35..982edec 100644 --- a/internal/framework/processreport/report_test.go +++ b/internal/framework/processreport/report_test.go @@ -14,9 +14,32 @@ import ( "gitea.maximumdirect.net/eric/audita/v3/internal/framework/contracts" "gitea.maximumdirect.net/eric/audita/v3/internal/framework/outcomes" "gitea.maximumdirect.net/eric/audita/v3/internal/framework/runner" + "gitea.maximumdirect.net/eric/audita/v3/internal/framework/structuredoutput" "gopkg.in/yaml.v3" ) +func TestRunReportUsesTypedFailureReasonFallback(t *testing.T) { + cause := &structuredoutput.Failure{Class: outcomes.FailureFatal, Cause: context.DeadlineExceeded} + ref := outcomes.Reference{Kind: outcomes.OperationValidation, ValidatorName: "review", SectionIndex: 2} + failure := &runner.ExecutionFailure{Phase: "validation", Origin: &ref, HasSection: true, Cause: cause} + var operation outcomes.Operation + structuredoutput.RecordFailure(&operation, failure) + snapshot := Build(BuildInput{ProcessingError: failure}) + if operation.FailureClass != outcomes.FailureFatal || operation.FailureReason != outcomes.ReasonInternalFailure || + !errors.Is(operation.Cause, context.DeadlineExceeded) { + t.Fatalf("operation lost typed failure facts: %+v", operation) + } + record := snapshot.Errors[0] + if record.Reason != string(operation.FailureReason) || record.Phase != failure.Phase || + record.Validator != ref.ValidatorName || !reflect.DeepEqual(record.ChunkIndex, intPtr(ref.SectionIndex)) { + t.Fatalf("report lost typed failure facts: %+v", record) + } + canceled := Build(BuildInput{ProcessingError: context.Canceled}).Errors[0] + if canceled.Reason != string(outcomes.ReasonCanceled) { + t.Fatalf("parent cancellation reason = %q", canceled.Reason) + } +} + func TestBuildPartialUnitsAndCombinedRecovery(t *testing.T) { started := time.Date(2026, 9, 24, 12, 0, 0, 0, time.UTC) completed := started.Add(time.Minute) diff --git a/internal/framework/structuredoutput/failure.go b/internal/framework/structuredoutput/failure.go index edd0219..239aaeb 100644 --- a/internal/framework/structuredoutput/failure.go +++ b/internal/framework/structuredoutput/failure.go @@ -35,6 +35,14 @@ const ( func (f *Failure) Error() string { return f.Message } func (f *Failure) Unwrap() error { return f.Cause } +// EffectiveReason supplies the report reason even when a caller omitted it. +func (f *Failure) EffectiveReason() outcomes.ReasonCode { + if f.Reason != "" { + return f.Reason + } + return outcomes.ReasonInternalFailure +} + func IsRecoverable(err error) bool { var failure *Failure return errors.As(err, &failure) && failure.Class == FailureRecoverable @@ -49,9 +57,7 @@ func RecordFailure(operation *outcomes.Operation, err error) { var failure *Failure if errors.As(err, &failure) { operation.FailureClass = failure.Class - if failure.Reason != "" { - operation.FailureReason = failure.Reason - } + operation.FailureReason = failure.EffectiveReason() } }