Files
archie/internal/domain/workflow/baseline_ledger.go
T
sam 67c9b7a00a fix(workflow): publish a baseline repair as a PR before the task continues (#1844)
Dependents now wait for the baseline gate to pass on the base branch
instead of a specific branch merging.
2026-11-09 14:40:58 +11:00

96 lines
2.7 KiB
Go

package workflow
import (
"context"
"fmt"
"os/exec"
"regexp"
"strings"
"time"
)
// BaselineClaim answers a task's request to repair a red baseline.
type BaselineClaim struct {
// Lead is true when the caller owns the repair and must run it.
Lead bool
// Task is the task that owns the repair when Lead is false.
Task int64
// Done is true once that task has finished; Failure is its failure, empty
// when it repaired the baseline in a pull request.
Done bool
Failure string
}
// BaselineLedger makes one task repair a red baseline per repository revision
// and command. Every task runs in its own container, so the ledger lives with
// the daemon.
type BaselineLedger interface {
ClaimBaselineFix(ctx context.Context, key string) (BaselineClaim, error)
FinishBaselineFix(ctx context.Context, key, failure string) error
}
const defaultBaselinePoll = 15 * time.Second
// repairBaselineOnce runs the repair when this task leads it, or reports the
// verdict of the task that does. Without a ledger every task repairs its own.
func repairBaselineOnce(ctx context.Context, tc *TaskContext, argv []string, out []byte) error {
if tc.Baseline == nil {
return runBaselineFix(ctx, tc, argv, out)
}
revision, err := exec.CommandContext(ctx, "git", "-C", tc.Dir, "rev-parse", "HEAD").Output()
if err != nil {
return fmt.Errorf("resolve baseline revision: %w", err)
}
key := strings.TrimSpace(string(revision)) + " " + strings.Join(argv, " ")
poll := tc.baselinePoll
if poll <= 0 {
poll = defaultBaselinePoll
}
for {
claim, err := tc.Baseline.ClaimBaselineFix(ctx, key)
if err != nil {
return fmt.Errorf("claim baseline fix: %w", err)
}
switch {
case claim.Lead:
fixErr := runBaselineFix(ctx, tc, argv, out)
failure := ""
if fixErr != nil {
failure = clip(fixErr.Error(), baselineParkOutputBytes)
}
if err := tc.Baseline.FinishBaselineFix(context.WithoutCancel(ctx), key, failure); err != nil {
tc.Log.WarnContext(ctx, "release baseline fix claim", "err", err)
}
return fixErr
case !claim.Done:
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(poll):
}
default:
return baselineSharedError(argv, string(out), claim)
}
}
}
// gateProgressLine matches the package-download chatter `go` prints before the
// real failure.
var gateProgressLine = regexp.MustCompile(`^go: (downloading|finding|extracting) `)
// trimGateProgress drops download progress from gate output so the failing
// command's own lines lead.
func trimGateProgress(out string) string {
if !strings.Contains(out, "go: ") {
return out
}
lines := strings.Split(out, "\n")
kept := lines[:0]
for _, line := range lines {
if !gateProgressLine.MatchString(line) {
kept = append(kept, line)
}
}
return strings.Join(kept, "\n")
}