Refresh reports when delivery failures accumulate

This commit is contained in:
2026-11-17 03:52:48 +00:00
parent f99fd993b4
commit 0c2a2164ec
4 changed files with 116 additions and 35 deletions

View File

@@ -314,31 +314,31 @@ func (s *OutputSession) publishReports(ctx context.Context, reports ReportBundle
result = append(result, FileFailure{Phase: OutputCleanup, Kind: "output cleanup", Err: err})
}
}()
primary = includeCancellation(ctx, primary)
phase := OutputReportWrite
if action == "report initialization" {
phase = OutputInitialization
}
failed := false
var failedWritten [3]bool
write := func(i int, payload []byte) {
writeCtx := ctx
kind := action
if failed {
writeCtx = context.WithoutCancel(ctx)
kind = "failed " + action
}
if err := s.replace(writeCtx, s.reportDestinations[i], payload); err != nil {
failures = append(failures, FileFailure{Path: s.reports[i], Phase: phase, Kind: kind, Err: err})
} else if failed {
failedWritten[i] = true
}
type reportState struct {
failed, canceled bool
failureCount int
}
renderFailed := func() bool {
if action == "report initialization" {
observed := func() reportState {
canceled := ctx.Err() != nil
return reportState{primary != nil || canceled || len(failures) > 0, canceled, len(failures)}
}
var rendered reportState
var written [3]reportState
var attemptedFailed [3]bool
refresh := func() bool {
state := observed()
if state == rendered {
return true
}
if phase == OutputInitialization && !rendered.failed {
if err := s.Close(); err != nil {
failures = append(failures, FileFailure{Path: s.transcript, Phase: OutputCleanup, Kind: "output cleanup", Err: err})
}
state = observed()
}
if render == nil {
return false
@@ -349,28 +349,38 @@ func (s *OutputSession) publishReports(ctx context.Context, reports ReportBundle
failures = append(failures, FileFailure{Phase: phase, Kind: "failed report rendering", Err: err})
return false
}
failed = true
rendered = state
return true
}
knownFailure := primary != nil || len(failures) != 0
if knownFailure && !renderFailed() {
return failures
write := func(i int) {
writeCtx, kind := ctx, action
if rendered.failed {
writeCtx, kind = context.WithoutCancel(ctx), "failed "+action
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})
} else {
written[i] = rendered
}
}
for i := range s.reports {
if !failed && (len(failures) != 0 || ctx.Err() != nil) && !renderFailed() {
if !refresh() {
return failures
}
write(i, bundleParts(reports)[i])
write(i)
}
if !knownFailure && (failed || len(failures) != 0 || ctx.Err() != nil) {
if !failed && !renderFailed() {
// Refresh stale publications once and give any report that failed before
// the switch to failed status an attempt with the failed bundle.
for i := range s.reports {
state := observed()
if !state.failed || written[i] == state || (attemptedFailed[i] && !written[i].failed) {
continue
}
if !refresh() {
return failures
}
for i, payload := range bundleParts(reports) {
if !failedWritten[i] {
write(i, payload)
}
}
write(i)
}
return failures
}