Honor cancellation before transcript delivery

This commit is contained in:
2026-11-10 20:28:41 +00:00
parent 56d39fca80
commit bbac312b32
4 changed files with 99 additions and 8 deletions

View File

@@ -1,12 +1,15 @@
package cli
import (
"context"
"errors"
"fmt"
"time"
"gitea.maximumdirect.net/eric/audita/v3/internal/core/diagnostics"
"gitea.maximumdirect.net/eric/audita/v3/internal/core/redaction"
"gitea.maximumdirect.net/eric/audita/v3/internal/core/reporting"
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/outcomes"
commandkit "go.maximumdirect.net/commandkit"
)
@@ -21,15 +24,12 @@ func finishProcess(inv commandkit.Invocation, input processInput, loaded *proces
completed := time.Now().UTC()
snapshot.RunID, snapshot.StartedAt, snapshot.CompletedAt = prepared.RunID, prepared.Started, &completed
snapshot.TranscriptPath, snapshot.OutputPath = input.Transcript, input.Output.Value
if primary == nil {
primary = inv.Context.Err()
}
snapshot.Status = reporting.StatusCompleted
if primary != nil {
snapshot.Status = reporting.StatusFailed
snapshot = failedOutputSnapshot(snapshot, nil)
if *snapshot.Summary.RunErrors == 0 {
snapshot.Errors = append(snapshot.Errors, reporting.ErrorRecord{Scope: "run", Reason: "processing_failure", Message: "Processing failed"})
snapshot = failedOutputSnapshot(snapshot, nil)
}
} else {
snapshot.Status = reporting.StatusCompleted
snapshot = failedProcessingSnapshot(snapshot, primary, inv.Context.Err())
}
reports, err := renderReportBundle(snapshot, secrets...)
if err != nil {
@@ -45,6 +45,11 @@ func finishProcess(inv commandkit.Invocation, input processInput, loaded *proces
}
return result
}
if canceled := inv.Context.Err(); primary == nil && canceled != nil {
primary = canceled
snapshot = failedProcessingSnapshot(snapshot, canceled, canceled)
reports = failed(nil)
}
result := prepared.Prepared.Output.Finish(primary, transcript, reports, failed)
if result != nil {
return commandkit.Failure(redact(result)).WithCause(result)
@@ -60,6 +65,22 @@ func finishProcess(inv commandkit.Invocation, input processInput, loaded *proces
return commandkit.Success()
}
func failedProcessingSnapshot(snapshot reporting.Snapshot, primary, contextErr error) reporting.Snapshot {
for _, record := range snapshot.Errors {
if record.Scope == "run" {
return failedOutputSnapshot(snapshot, nil)
}
}
record := reporting.ErrorRecord{Scope: "run", Reason: "processing_failure", Message: "Processing failed"}
if errors.Is(primary, context.Canceled) || (contextErr != nil && errors.Is(primary, contextErr)) {
record.Phase = "processing"
record.Reason = string(outcomes.ReasonCanceled)
record.Message = "Processing was canceled."
}
snapshot.Errors = append(snapshot.Errors, record)
return failedOutputSnapshot(snapshot, nil)
}
// failedOutputSnapshot derives the count from the same run records sent to each report.
func failedOutputSnapshot(snapshot reporting.Snapshot, failures []diagnostics.FileFailure) reporting.Snapshot {
snapshot.Status = reporting.StatusFailed