diff --git a/docs/roadmap/remediation.md b/docs/roadmap/remediation.md index 07b771d..5edb5ab 100644 --- a/docs/roadmap/remediation.md +++ b/docs/roadmap/remediation.md @@ -53,6 +53,10 @@ One best-effort pass updates earlier reports with stale evidence. Diagnostics tests cover both initially failed runs and failures that arise during publication of a completed run. +The follow-up fix counts repeated write failures for one report as one +delivery failure and retains both causes. The regression makes both writes to +`recoveries.yml` fail and verifies that the surviving reports agree on the count. + **Affected plan:** Stage 4, item 3, and Stage 17, item 1. **Evidence:** [Report publication](../../internal/core/diagnostics/output_session.go#L311) diff --git a/internal/core/diagnostics/output_session.go b/internal/core/diagnostics/output_session.go index abafb0e..2210f14 100644 --- a/internal/core/diagnostics/output_session.go +++ b/internal/core/diagnostics/output_session.go @@ -329,6 +329,7 @@ func (s *OutputSession) publishReports(ctx context.Context, reports ReportBundle var rendered reportState var written [3]reportState var attemptedFailed [3]bool + failureIndexes := [3]int{-1, -1, -1} refresh := func() bool { state := observed() if state == rendered { @@ -359,7 +360,14 @@ func (s *OutputSession) publishReports(ctx context.Context, reports ReportBundle attemptedFailed[i] = true } if err := s.replace(writeCtx, s.reportDestinations[i], bundleParts(reports)[i]); err != nil { - failures = append(failures, FileFailure{Path: s.reports[i], Phase: phase, Kind: kind, Err: err}) + // A retry adds a cause to the same delivery failure. Reports count + // the failed destination, so the rendered facts stay current. + if previous := failureIndexes[i]; previous >= 0 { + failures[previous].Err = errors.Join(failures[previous].Err, err) + } else { + failureIndexes[i] = len(failures) + failures = append(failures, FileFailure{Path: s.reports[i], Phase: phase, Kind: kind, Err: err}) + } } else { written[i] = rendered } diff --git a/internal/core/diagnostics/output_session_test.go b/internal/core/diagnostics/output_session_test.go index 2fd8521..699f597 100644 --- a/internal/core/diagnostics/output_session_test.go +++ b/internal/core/diagnostics/output_session_test.go @@ -11,6 +11,7 @@ import ( "os/exec" "path/filepath" "slices" + "strconv" "strings" "testing" @@ -427,6 +428,40 @@ func TestFailedReportsIncludeLaterDeliveryFailures(t *testing.T) { } } +func TestRepeatedFailureOfOneReportCountsOnce(t *testing.T) { + dir := t.TempDir() + session, err := PrepareOutput(OutputSelection{ReportDir: dir, ReportingEnabled: true}) + if err != nil { + t.Fatal(err) + } + initial, retry := errors.New("initial write failed"), errors.New("retry failed") + replace := session.replace + attempts := 0 + session.replace = func(ctx context.Context, path string, payload []byte) error { + if filepath.Base(path) == RecoveriesFile { + attempts++ + if attempts == 1 { + return initial + } + return retry + } + return replace(ctx, path, payload) + } + err = session.Finish(context.Background(), nil, nil, func(_ error, failures []FileFailure) (ReportBundle, error) { + payload := []byte(strconv.Itoa(len(failures))) + return ReportBundle{Errors: payload, Recoveries: payload, Markdown: payload}, nil + }) + if !errors.Is(err, initial) || !errors.Is(err, retry) || attempts != 2 { + t.Fatalf("writes = %d, error = %v", attempts, err) + } + for _, name := range []string{ErrorsFile, MarkdownFile} { + got, readErr := os.ReadFile(filepath.Join(dir, name)) + if readErr != nil || string(got) != "1" { + t.Errorf("%s recorded %q failures, error = %v", name, got, readErr) + } + } +} + type writerFunc func([]byte) (int, error) func (write writerFunc) Write(payload []byte) (int, error) { return write(payload) }