Retries: tell stages why the last attempt failed; resume interrupted runs

- turn.RenderPreviousAttempt: bounded "# Previous attempt" section (attempt
  number, failing stage, bounded diagnostic, partial-edits note).
- In-stage retries (stage/model_run.go) append it instead of resending an
  identical prompt; run retries (resume and restart) add it to the first
  model visit's input.md, numbering attempts along the retry chain.
- --input-file retry overrides render only the override keys in input.md,
  labelled "# Operator correction" (the step row keeps the merged input).
- Essential memory (memory.md) inlines a revisiting stage's own last handoff,
  bounded, besides the previous stage's handoff.
- Interrupted runs can be retried: resume reruns the interrupted visit, or
  starts the next stage after the last completed visit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 00:22:45 +00:00
parent accb9970b6
commit ae4526d87c
9 changed files with 458 additions and 29 deletions
+97 -11
View File
@@ -662,8 +662,9 @@ func (r *runner) retrySourceCheckpointSelected(sourceID int64, pinned *protocol.
if err != nil {
return retryCheckpoint{}, err
}
if source.Status != "failed" || source.FinishedAt == "" {
return retryCheckpoint{}, fmt.Errorf("retry source %d is not a terminal failed run", sourceID)
interrupted := source.Status == "interrupted"
if (source.Status != "failed" && !interrupted) || source.FinishedAt == "" {
return retryCheckpoint{}, fmt.Errorf("retry source %d is not a terminal failed or interrupted run", sourceID)
}
if source.PipelineID != pinned.ID {
return retryCheckpoint{}, fmt.Errorf("retry source %d belongs to a different pipeline", sourceID)
@@ -672,6 +673,28 @@ func (r *runner) retrySourceCheckpointSelected(sourceID int64, pinned *protocol.
return retryCheckpoint{}, fmt.Errorf("retry source %d has no failed stage checkpoint", sourceID)
}
failed := source.Steps[len(source.Steps)-1]
if interrupted && selectedStage == "" {
// A server stop interrupts a run mid-visit (the visit is recorded as
// interrupted, with the input it began with) or between visits (the
// last recorded visit completed and routed to its next stage). Either
// way the run resumes after its last completed step: the interrupted
// visit runs again, or the next stage starts with the handoff the
// completed visit produced.
switch failed.Status {
case "interrupted":
failed.Target = protocol.FailureEnd
case "completed":
if failed.Target == "" || failed.Target == protocol.SuccessEnd || failed.Target == protocol.FailureEnd {
return retryCheckpoint{}, fmt.Errorf("retry source %d has no unfinished stage to resume; use --mode=restart", sourceID)
}
failed = protocol.RunStep{Sequence: failed.Sequence + 1, Stage: failed.Target, Status: "interrupted", Target: protocol.FailureEnd, Input: failed.Output}
if failed.Input == nil {
failed.Input = map[string]any{}
}
default:
return retryCheckpoint{}, fmt.Errorf("retry source %d has no safe interrupted-stage checkpoint; use --mode=restart", sourceID)
}
}
if selectedStage != "" {
failed = protocol.RunStep{}
visits := 0
@@ -705,7 +728,7 @@ func (r *runner) retrySourceCheckpointSelected(sourceID int64, pinned *protocol.
return retryCheckpoint{}, fmt.Errorf("retry stage %q has no visit %d", selectedStage, selectedVisit)
}
}
if (failed.Status != "completed" && failed.Status != "failed") || (selectedStage == "" && failed.Target != protocol.FailureEnd) {
if (failed.Status != "completed" && failed.Status != "failed" && !(interrupted && failed.Status == "interrupted")) || (selectedStage == "" && failed.Target != protocol.FailureEnd) {
return retryCheckpoint{}, fmt.Errorf("retry source %d has no safe failed-stage checkpoint", sourceID)
}
var handoff map[string]any
@@ -813,8 +836,10 @@ func (r *runner) retry(request protocol.Message) (*protocol.RunDetail, error) {
if original.Executing {
return nil, fmt.Errorf("run %d is still executing; wait for it to stop completely before retrying", original.ID)
}
if original.Status != "failed" || original.FinishedAt == "" {
return nil, fmt.Errorf("only failed runs can be retried (run %d is %s)", original.ID, original.Status)
// An interrupted run (the server stopped mid-run) resumes after its last
// completed step, like a failed run resumes its failed stage.
if (original.Status != "failed" && original.Status != "interrupted") || original.FinishedAt == "" {
return nil, fmt.Errorf("only failed or interrupted runs can be retried (run %d is %s)", original.ID, original.Status)
}
if request.RequestKey == "" {
return nil, fmt.Errorf("retry requires an idempotency request key")
@@ -998,6 +1023,7 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail,
return
}
state := rebuildRetryState(source, checkpoint.failed, storedHandoff)
state.previousAttempt = r.previousAttemptSection(ctx, source, d.Graph, state.key)
if override, ok := storedHandoff["override"].(map[string]any); ok {
state.retryOverride = stage.Values(override)
}
@@ -1019,9 +1045,65 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail,
input := stage.Values(request.Input)
state := newRunState(d.Graph.Stages[0].Key, input)
state.memory["input"] = memoryValue{output: original}
if request.RetrySource > 0 {
// A restart from entry still tells its first model visit why the
// source run failed.
if source, err := r.store.GetRun(ctx, request.RetrySource); err == nil {
state.previousAttempt = r.previousAttemptSection(ctx, source, d.Graph, state.key)
}
}
r.runLoop(ctx, id, d.Graph, d.Name, workspace, request, state)
}
// maxRetryChain bounds the walk that numbers a retry attempt.
const maxRetryChain = 50
// previousAttemptSection renders the bounded "# Previous attempt" section for
// a retried run whose first visit is the model stage firstStage: the attempt
// number (counted along the retry chain), the stage the source run stopped in
// and its diagnostic. A first visit that is not a model stage gets nothing, so
// the note never lands on an unrelated later stage.
func (r *runner) previousAttemptSection(ctx context.Context, source *protocol.RunDetail, graph protocol.Graph, firstStage string) string {
if source == nil {
return ""
}
model := false
for _, declared := range graph.Stages {
if declared.Key == firstStage {
model = stage.EffectiveType(declared.Type) == stage.ModelType
}
}
if !model {
return ""
}
parentOf := func(d *protocol.RunDetail) int64 {
if d.RetryOf > 0 {
return d.RetryOf
}
return d.RetrySource
}
attempt := 2
for previous := parentOf(source); previous > 0 && attempt < maxRetryChain; attempt++ {
ancestor, err := r.store.GetRun(ctx, previous)
if err != nil || ancestor == nil {
break
}
previous = parentOf(ancestor)
}
failedStage, diagnostic := source.CurrentStage, ""
if n := len(source.Steps); n > 0 {
last := source.Steps[n-1]
failedStage, diagnostic = last.Stage, strings.TrimSpace(last.Diagnostic)
}
if diagnostic == "" {
diagnostic = strings.TrimSpace(source.Diagnostic)
}
if diagnostic == "" {
diagnostic = strings.TrimSpace(source.FailureReason)
}
return turn.RenderPreviousAttempt(turn.PreviousAttempt{Attempt: attempt, Stage: failedStage, Diagnostic: diagnostic, Run: source.ID})
}
// terminalFinish classifies a stage's resolved target. A terminal key yields the
// run's final status, outcome and diagnostic; an empty target means the stage
// named an undeclared outcome; any other value is a stage key and is not
@@ -1098,7 +1180,10 @@ type runState struct {
// retryOverride is applied once, after the selected stage's memory bindings
// have been resolved. It is intentionally not part of run input or memory.
retryOverride stage.Values
priorBudgets map[string]stage.BudgetSnapshot
// previousAttempt is the rendered "# Previous attempt" section a retried
// run's first model visit receives; prepareTurn consumes it.
previousAttempt string
priorBudgets map[string]stage.BudgetSnapshot
// adopt names the visit to re-adopt from a previous server process instead
// of starting it; it is consumed by the loop's first iteration.
adopt *adoptedVisit
@@ -1779,11 +1864,12 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
finish("failed", "error", copyErr.Error())
return
}
// Keep the merged values for this visit's input.md: the model reads
// files, not the persisted step row, so the override must be visible
// there. A selected-stage override belongs only to this invocation,
// not to a later visit reached through the stage graph.
retryInput = input
// The step row persists the merged input; input.md shows only the
// operator's override keys: the model reads files, not the step row,
// so the correction must be visible there, without re-printing the
// whole input. A selected-stage override belongs only to this
// invocation, not to a later visit reached through the stage graph.
retryInput = state.retryOverride
state.retryOverride = nil
}
if adopted == nil {