feat(workflow): resume a parked run from a chosen step
Each finished stage records the run's step results; a retry may name a resume point, and the next attempt skips the steps before it with their results loaded. A run with a repository resumes only on its pushed branch. The canvas offers Resume/Re-run from here on parked runs.
This commit is contained in:
37 files changed
+606
-88
No files matched your search
@@ -96,6 +96,20 @@ func ParseDefinition(src string, registry StepRegistry) (YAMLDefinition, error)
|
||||
return definition, nil
|
||||
}
|
||||
|
||||
// StageNames returns the names a definition's steps run under, in order. It
|
||||
// reads a definition already pinned, so it decodes without validating.
|
||||
func StageNames(src string) ([]string, error) {
|
||||
var definition YAMLDefinition
|
||||
if err := yaml.Unmarshal([]byte(src), &definition); err != nil {
|
||||
return nil, fmt.Errorf("parse workflow YAML: %w", err)
|
||||
}
|
||||
names := make([]string, len(definition.Steps))
|
||||
for i, step := range definition.Steps {
|
||||
names[i] = step.StageName()
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
// ParseAndCompile validates and compiles one definition in a single operation.
|
||||
func ParseAndCompile(src string, registry StepRegistry) (Workflow, error) {
|
||||
definition, err := ParseDefinition(src, registry)
|
||||
|
||||
@@ -33,7 +33,8 @@ steps:
|
||||
type: gate.repository
|
||||
settings:
|
||||
expect: test-failure
|
||||
- type: repo.commit
|
||||
- id: commit-repro
|
||||
type: repo.commit
|
||||
settings:
|
||||
message: "test: failing repro"
|
||||
reference: Refs
|
||||
@@ -53,7 +54,8 @@ steps:
|
||||
</analysis>
|
||||
|
||||
The full quality gate must pass. Make the smallest fix that makes the repro tests pass without changing them. Call finish with status "passed" and a summary for the PR reviewer: root cause, the fix, and verification.
|
||||
- type: repo.commit
|
||||
- id: commit-fix
|
||||
type: repo.commit
|
||||
settings:
|
||||
message: "fix: {{ task.title }} (archie)"
|
||||
reference: Fixes
|
||||
|
||||
@@ -171,7 +171,7 @@ func compileStep(step StepRecord, registry StepRegistry) (Stage, error) {
|
||||
return Stage{}, fmt.Errorf("build workflow step %q: %w", step.Type, err)
|
||||
}
|
||||
// Settings are built per run, with every reference resolved.
|
||||
return wrapStep(step, step.Type, func(ctx context.Context, tc *TaskContext) error {
|
||||
return wrapStep(step, func(ctx context.Context, tc *TaskContext) error {
|
||||
stage, err := factory(renderSettings(step.Settings, tc))
|
||||
if err != nil {
|
||||
return fmt.Errorf("step %q settings: %w", step.Type, err)
|
||||
@@ -180,12 +180,22 @@ func compileStep(step StepRecord, registry StepRegistry) (Stage, error) {
|
||||
}), nil
|
||||
}
|
||||
|
||||
// StageName is the name a step runs and is recorded under: its id, else its
|
||||
// type, else "parallel".
|
||||
func (step StepRecord) StageName() string {
|
||||
switch {
|
||||
case step.ID != "":
|
||||
return step.ID
|
||||
case step.Parallel != nil:
|
||||
return "parallel"
|
||||
}
|
||||
return step.Type
|
||||
}
|
||||
|
||||
// wrapStep applies the controls every step shares around its body: when,
|
||||
// retry, and recording its result under its id.
|
||||
func wrapStep(step StepRecord, name string, body func(context.Context, *TaskContext) error) Stage {
|
||||
if step.ID != "" {
|
||||
name = step.ID
|
||||
}
|
||||
func wrapStep(step StepRecord, body func(context.Context, *TaskContext) error) Stage {
|
||||
name := step.StageName()
|
||||
// ParseDefinition has already refused a condition or backoff that does
|
||||
// not parse.
|
||||
when, _ := parseCondition(step.When)
|
||||
@@ -241,7 +251,7 @@ func compileParallel(step StepRecord, registry StepRegistry) (Stage, error) {
|
||||
branches[i] = append(branches[i], stage)
|
||||
}
|
||||
}
|
||||
return wrapStep(step, "parallel", func(ctx context.Context, tc *TaskContext) error {
|
||||
return wrapStep(step, func(ctx context.Context, tc *TaskContext) error {
|
||||
clones := make([]*TaskContext, len(branches))
|
||||
errs := make([]error, len(branches))
|
||||
var wg sync.WaitGroup
|
||||
|
||||
@@ -60,6 +60,17 @@ type StepFinish struct {
|
||||
// TokensUsed is the usage the step itself accounted. Stages record none;
|
||||
// the agent runtime records its own calls' usage.
|
||||
TokensUsed int64
|
||||
// Results is the run's step results as JSON after a stage the run moves
|
||||
// past; nil otherwise. A resumed attempt starts from them.
|
||||
Results []byte
|
||||
}
|
||||
|
||||
// Resume is where a retried attempt starts: at stage From, with the results
|
||||
// recorded after stage After, the one before it. After is empty when From is
|
||||
// the first stage. An empty From runs the workflow from its first stage.
|
||||
type Resume struct {
|
||||
From string
|
||||
After string
|
||||
}
|
||||
|
||||
// StepExecution is one recorded step: ListSteps' read, the dashboard run
|
||||
|
||||
@@ -64,6 +64,11 @@ type Task struct {
|
||||
// RetryMode is the worktree mode for the next dispatch; empty means
|
||||
// RefreshOntoBase.
|
||||
RetryMode string `json:"retry_mode"`
|
||||
// ResumeFrom is the stage this attempt starts at; empty runs every stage.
|
||||
// ResumeResults are the step results (JSON, by step id) the attempt starts
|
||||
// with, so steps it skips still answer their references.
|
||||
ResumeFrom string `json:"resume_from,omitempty"`
|
||||
ResumeResults json.RawMessage `json:"resume_results,omitempty"`
|
||||
// WatchCommentID is the poll backstop's high-water mark over forge
|
||||
// review-comment IDs for this task (pr-review-remediation.md decision
|
||||
// 2's persisted per-task cursor; the comment-watch design it was
|
||||
|
||||
@@ -3,10 +3,12 @@ package workflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"maps"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
@@ -301,7 +303,12 @@ func Run(ctx context.Context, wf Workflow, tc *TaskContext) {
|
||||
tc.runInterface, tc.runInterfaceSet = wf.Interface, true
|
||||
log := tc.Log.With("workflow", wf.Name, "repo", tc.Repo.FullName(), "issue", t.IssueNumber)
|
||||
|
||||
for _, stage := range wf.Stages {
|
||||
stages, err := tc.resumeStages(wf.Stages)
|
||||
if err != nil {
|
||||
park(ctx, tc, err.Error())
|
||||
return
|
||||
}
|
||||
for _, stage := range stages {
|
||||
tc.Stage = stage.Name
|
||||
if err := tc.Store.Update(ctx, t); err != nil {
|
||||
park(ctx, tc, fmt.Sprintf("persist task before stage %s: %v", stage.Name, err))
|
||||
@@ -359,7 +366,7 @@ func Run(ctx context.Context, wf Workflow, tc *TaskContext) {
|
||||
// failed recording write parks the execution, like a failed start.
|
||||
finishEvent, recordErr := tc.Store.FinishStep(ctx, StepFinish{
|
||||
StepID: stepID, ExecutionID: t.ID,
|
||||
From: taskstate.StepRunning, To: finishTo, Detail: finishDetail,
|
||||
From: taskstate.StepRunning, To: finishTo, Detail: finishDetail, Results: tc.resultsAfter(stage, err),
|
||||
})
|
||||
if recordErr != nil {
|
||||
stageLog.Error("stage finish could not be recorded", "stage", stage.Name, "err", recordErr)
|
||||
@@ -377,6 +384,38 @@ func Run(ctx context.Context, wf Workflow, tc *TaskContext) {
|
||||
finish(ctx, tc, log)
|
||||
}
|
||||
|
||||
// resultsAfter is what a stage records for a later attempt resumed after it:
|
||||
// the results so far when the run moves past the stage, else nothing.
|
||||
func (tc *TaskContext) resultsAfter(stage Stage, err error) []byte {
|
||||
if err != nil && !stage.ContinueOnFailure {
|
||||
return nil
|
||||
}
|
||||
// StepResult holds strings and decoded JSON values, which always marshal.
|
||||
results, _ := json.Marshal(tc.stepResults)
|
||||
return results
|
||||
}
|
||||
|
||||
// resumeStages returns the stages this attempt runs: all of them, or those
|
||||
// from the task's resume point on, with the results the stages before it
|
||||
// recorded loaded so their references still resolve.
|
||||
func (tc *TaskContext) resumeStages(stages []Stage) ([]Stage, error) {
|
||||
from := tc.Task.ResumeFrom
|
||||
if from == "" {
|
||||
return stages, nil
|
||||
}
|
||||
at := slices.IndexFunc(stages, func(s Stage) bool { return s.Name == from })
|
||||
if at < 0 {
|
||||
return nil, fmt.Errorf("resume point %q is not a step of this workflow", from)
|
||||
}
|
||||
if len(tc.Task.ResumeResults) > 0 {
|
||||
if err := json.Unmarshal(tc.Task.ResumeResults, &tc.stepResults); err != nil {
|
||||
return nil, fmt.Errorf("resume results: %w", err)
|
||||
}
|
||||
}
|
||||
tc.Log.Info("resuming", "from", from, "skipped", at)
|
||||
return stages[at:], nil
|
||||
}
|
||||
|
||||
// endsRun applies a recorded stage's result: a failure parks unless the stage
|
||||
// continues on failure, and an outcome finishes the run.
|
||||
func endsRun(ctx context.Context, tc *TaskContext, stage Stage, err error, log, stageLog *slog.Logger) bool {
|
||||
|
||||
Reference in new issue
Block a user