Merge main (findings ledger, loop continuation) into usage-limit branch
Keeps the findings ledger, extra visits and on_exhausted=decide alongside the run budget, failure classes, quota parking and error-loop backoff. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+158
-29
@@ -16,6 +16,7 @@ import (
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/claude"
|
||||
"git.a-wan.net/alan/pipelines/internal/filediff"
|
||||
"git.a-wan.net/alan/pipelines/internal/findings"
|
||||
"git.a-wan.net/alan/pipelines/internal/harness"
|
||||
"git.a-wan.net/alan/pipelines/internal/opencode"
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
@@ -448,7 +449,7 @@ func (r *runner) startWithSchedule(request protocol.Message, origin *pipeline.Sc
|
||||
}
|
||||
}
|
||||
if request.RetrySource > 0 && request.RetryMode != "restart" {
|
||||
checkpoint, checkpointErr := r.retrySourceCheckpointSelected(request.RetrySource, d, request.RetryStage, request.RetryVisit, request.RetryInput)
|
||||
checkpoint, checkpointErr := r.retrySourceCheckpointSelected(request.RetrySource, d, request.RetryStage, request.RetryVisit, request.RetryInput, request.RetryExtraVisits)
|
||||
if checkpointErr != nil {
|
||||
return nil, checkpointErr
|
||||
}
|
||||
@@ -863,10 +864,10 @@ func retryStageContractEqual(left, right protocol.Stage) bool {
|
||||
}
|
||||
|
||||
func (r *runner) retrySourceCheckpoint(sourceID int64, pinned *protocol.PipelineDetail) (retryCheckpoint, error) {
|
||||
return r.retrySourceCheckpointSelected(sourceID, pinned, "", 0, nil)
|
||||
return r.retrySourceCheckpointSelected(sourceID, pinned, "", 0, nil, 0)
|
||||
}
|
||||
|
||||
func (r *runner) retrySourceCheckpointSelected(sourceID int64, pinned *protocol.PipelineDetail, selectedStage string, selectedVisit int, override map[string]any) (retryCheckpoint, error) {
|
||||
func (r *runner) retrySourceCheckpointSelected(sourceID int64, pinned *protocol.PipelineDetail, selectedStage string, selectedVisit int, override map[string]any, extraVisits int) (retryCheckpoint, error) {
|
||||
source, err := r.store.GetRun(r.ctx, sourceID)
|
||||
if err != nil {
|
||||
return retryCheckpoint{}, err
|
||||
@@ -882,6 +883,13 @@ 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 extraVisits > 0 && selectedStage == "" && !interrupted {
|
||||
// More visits for an exhausted loop: continue the loop where the cap
|
||||
// stopped it instead of re-running the visit that hit it.
|
||||
if next, ok := exhaustedContinuation(failed); ok {
|
||||
failed = next
|
||||
}
|
||||
}
|
||||
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
|
||||
@@ -951,6 +959,9 @@ func (r *runner) retrySourceCheckpointSelected(sourceID int64, pinned *protocol.
|
||||
// stage's memory bindings must be resolved from the original prefix before
|
||||
// this override is applied at invocation time.
|
||||
handoff = map[string]any{"input": failed.Input, "override": override, "sequence": failed.Sequence, "stage": failed.Stage, "visit": failed.Sequence}
|
||||
if extraVisits > 0 {
|
||||
handoff["extra_visits"] = extraVisits
|
||||
}
|
||||
prefix, prefixRecords, prefixErr := r.retryPrefix(sourceID, source, failed.Sequence)
|
||||
if prefixErr != nil {
|
||||
return retryCheckpoint{}, prefixErr
|
||||
@@ -1076,6 +1087,20 @@ func (r *runner) retry(request protocol.Message) (*protocol.RunDetail, error) {
|
||||
if mode != "resume" && mode != "restart" {
|
||||
return nil, fmt.Errorf("retry mode must be resume or restart")
|
||||
}
|
||||
if request.RetryExtraVisits < 0 || request.RetryExtraVisits > maxExtraVisits {
|
||||
return nil, fmt.Errorf("extra visits must be from 1 to %d", maxExtraVisits)
|
||||
}
|
||||
if request.RetryExtraVisits > 0 && (mode != "resume" || request.RetryStage != "" || request.RetryVisit > 0) {
|
||||
return nil, fmt.Errorf("extra visits continue the exhausted loop; they cannot be combined with --mode=restart or a selected stage visit")
|
||||
}
|
||||
if request.RetryExtraStage != "" {
|
||||
if request.RetryExtraVisits == 0 {
|
||||
return nil, fmt.Errorf("an extra-visits stage needs extra visits")
|
||||
}
|
||||
if !graphHasStage(original.Graph, request.RetryExtraStage) {
|
||||
return nil, fmt.Errorf("stage %q is not part of run %d", request.RetryExtraStage, original.ID)
|
||||
}
|
||||
}
|
||||
version := original.Version
|
||||
if request.RetryVersion > 0 {
|
||||
version = request.RetryVersion
|
||||
@@ -1085,7 +1110,7 @@ func (r *runner) retry(request protocol.Message) (*protocol.RunDetail, error) {
|
||||
return nil, err
|
||||
}
|
||||
if mode == "resume" {
|
||||
if _, err := r.retrySourceCheckpointSelected(original.ID, d, request.RetryStage, request.RetryVisit, request.RetryInput); err != nil {
|
||||
if _, err := r.retrySourceCheckpointSelected(original.ID, d, request.RetryStage, request.RetryVisit, request.RetryInput, request.RetryExtraVisits); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@@ -1098,7 +1123,7 @@ func (r *runner) retry(request protocol.Message) (*protocol.RunDetail, error) {
|
||||
if budgetUSD == 0 && original.Budget != nil && original.Budget.Source == "run" {
|
||||
budgetUSD = original.Budget.LimitUSD
|
||||
}
|
||||
return r.start(protocol.Message{PipelineID: original.PipelineID, Version: version, RequestKey: request.RequestKey, RetryMode: mode, RetrySource: original.ID, RetryStage: request.RetryStage, RetryVisit: request.RetryVisit, RetryInput: request.RetryInput, Workspace: original.Workspace, Input: original.Input, AllowTools: original.AllowTools, WritePaths: original.WritePaths, Model: original.Model, StageModels: original.StageModels, NotifyMode: notifyMode, OutputFormat: original.OutputFormat, QuotaPolicy: request.QuotaPolicy, BudgetUSD: budgetUSD}, original.ID)
|
||||
return r.start(protocol.Message{PipelineID: original.PipelineID, Version: version, RequestKey: request.RequestKey, RetryMode: mode, RetrySource: original.ID, RetryStage: request.RetryStage, RetryVisit: request.RetryVisit, RetryInput: request.RetryInput, RetryExtraVisits: request.RetryExtraVisits, RetryExtraStage: request.RetryExtraStage, Workspace: original.Workspace, Input: original.Input, AllowTools: original.AllowTools, WritePaths: original.WritePaths, Model: original.Model, StageModels: original.StageModels, NotifyMode: notifyMode, OutputFormat: original.OutputFormat, QuotaPolicy: request.QuotaPolicy, BudgetUSD: budgetUSD}, original.ID)
|
||||
}
|
||||
|
||||
func (r *runner) cancel(id int64) error {
|
||||
@@ -1179,6 +1204,8 @@ func (r *runner) decide(request protocol.Message) (*protocol.RunDetail, error) {
|
||||
return nil, err
|
||||
}
|
||||
if applied {
|
||||
// "continue" on an exhausted loop grants the visit it needs.
|
||||
r.grantForDecision(r.ctx, detail, step)
|
||||
// Re-seed the identity so the resumed frames (this one and the resumed
|
||||
// loop's) carry it even after a restart, when no creation was observed.
|
||||
r.rememberRunIdentity(detail.ID, identityOfRunSummary(detail.RunSummary))
|
||||
@@ -1220,7 +1247,7 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail,
|
||||
r.finishRunState(id, d.Name, workspace, "failed", "error", err.Error(), 0, 0, "", nil)
|
||||
return
|
||||
}
|
||||
checkpoint, err := r.retrySourceCheckpointSelected(request.RetrySource, d, request.RetryStage, request.RetryVisit, request.RetryInput)
|
||||
checkpoint, err := r.retrySourceCheckpointSelected(request.RetrySource, d, request.RetryStage, request.RetryVisit, request.RetryInput, request.RetryExtraVisits)
|
||||
if err != nil {
|
||||
r.finishRunState(id, d.Name, workspace, "failed", "error", err.Error(), 0, 0, "", nil)
|
||||
return
|
||||
@@ -1251,6 +1278,30 @@ func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail,
|
||||
r.finishRunState(id, d.Name, workspace, "failed", "error", err.Error(), 0, 0, "", nil)
|
||||
return
|
||||
}
|
||||
// The ledger as it stood before the retried visit, the source's grants
|
||||
// plus any new ones, and the failed visit's partial work.
|
||||
state.findings = findings.Before(source.Findings, checkpoint.failed.Sequence)
|
||||
if len(state.findings) > 0 {
|
||||
r.persistFindings(id, state.findings)
|
||||
}
|
||||
for key, extra := range source.VisitGrants {
|
||||
state.grant([]string{key}, extra)
|
||||
}
|
||||
if request.RetryExtraVisits > 0 {
|
||||
stages := []string{request.RetryExtraStage}
|
||||
if request.RetryExtraStage == "" {
|
||||
stages = exhaustedStages(d.Graph, state, state.key)
|
||||
}
|
||||
state.grant(stages, request.RetryExtraVisits)
|
||||
}
|
||||
if len(state.grants) > 0 {
|
||||
r.persistGrants(id, state.grants)
|
||||
}
|
||||
for _, step := range source.Steps {
|
||||
if step.Sequence == checkpoint.failed.Sequence && step.Stage == checkpoint.failed.Stage && step.Status != "completed" {
|
||||
state.carryFrom = r.turnPaths(source.ID, step.Sequence, step.Stage).TurnDir
|
||||
}
|
||||
}
|
||||
r.runLoop(ctx, id, d.Graph, d.Name, workspace, request, state)
|
||||
return
|
||||
}
|
||||
@@ -1412,6 +1463,15 @@ type runState struct {
|
||||
// failure, so the same one twice in a row stops the loop.
|
||||
errorLoops map[string]int
|
||||
lastHarness map[string]string
|
||||
// findings is the run's findings ledger; grants are extra visits per stage
|
||||
// (loop_state.go). sessions is each stage's latest harness session, which
|
||||
// a resume_session stage continues on its next visit.
|
||||
findings []protocol.Finding
|
||||
grants map[string]int
|
||||
sessions map[string]string
|
||||
// carryFrom is the failed source visit's turn directory whose private/
|
||||
// and output.md the retried visit receives; prepareTurn consumes it.
|
||||
carryFrom string
|
||||
}
|
||||
|
||||
func newRunState(key string, input stage.Values) *runState {
|
||||
@@ -1429,7 +1489,24 @@ func rebuildRunState(detail *protocol.RunDetail, decided protocol.RunStep) *runS
|
||||
}
|
||||
publicSeen := map[string]bool{}
|
||||
state.priorBudgets = map[string]stage.BudgetSnapshot{}
|
||||
state.findings = append([]protocol.Finding(nil), detail.Findings...)
|
||||
for key, extra := range detail.VisitGrants {
|
||||
state.grant([]string{key}, extra)
|
||||
}
|
||||
for _, step := range detail.Steps {
|
||||
if isExhaustionStep(step) {
|
||||
// A runner-created exhaustion decision is not a stage visit.
|
||||
if step.Sequence >= state.nextSequence {
|
||||
state.nextSequence = step.Sequence + 1
|
||||
}
|
||||
continue
|
||||
}
|
||||
if step.SessionID != "" {
|
||||
if state.sessions == nil {
|
||||
state.sessions = map[string]string{}
|
||||
}
|
||||
state.sessions[step.Stage] = step.SessionID
|
||||
}
|
||||
// The runtime stamps this cumulative snapshot on every executed model
|
||||
// visit. Reusing it preserves per-stage limits across export recovery.
|
||||
var audit struct {
|
||||
@@ -1487,6 +1564,9 @@ func rebuildRetryState(detail *protocol.RunDetail, failed protocol.RunStep, hand
|
||||
if step.Sequence >= failed.Sequence {
|
||||
break
|
||||
}
|
||||
if isExhaustionStep(step) {
|
||||
continue
|
||||
}
|
||||
state.visits[step.Stage]++
|
||||
added := publicArtifacts(step.Artifacts, publicSeen)
|
||||
if step.Status == "completed" {
|
||||
@@ -1561,7 +1641,7 @@ func (r *runner) retryPrefix(sourceID int64, source *protocol.RunDetail, failedS
|
||||
}
|
||||
publicBySequence := retryPublicBySequence(source, failedSequence)
|
||||
for _, step := range source.Steps {
|
||||
if step.Sequence >= failedSequence || step.Status != "completed" {
|
||||
if step.Sequence >= failedSequence || step.Status != "completed" || isExhaustionStep(step) {
|
||||
continue
|
||||
}
|
||||
prefix[step.Stage] = step.Output
|
||||
@@ -2057,7 +2137,7 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
|
||||
effectiveModels = resolved
|
||||
}
|
||||
runBudget := r.runBudget(ctx, id)
|
||||
for sequence := state.nextSequence; sequence <= 30; sequence++ {
|
||||
for sequence := state.nextSequence; sequence <= state.stepLimit(graph); sequence++ {
|
||||
if ctx.Err() != nil {
|
||||
canceled()
|
||||
return
|
||||
@@ -2094,23 +2174,29 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
|
||||
}
|
||||
}
|
||||
isHuman := stage.EffectiveType(def.Type) == stage.HumanType
|
||||
maxVisits := 5
|
||||
if selected.Limits != nil {
|
||||
maxVisits = selected.Limits.MaxVisits
|
||||
}
|
||||
// Both caps include the extra visits granted to the stage.
|
||||
maxVisits, hardVisits := state.visitCaps(selected)
|
||||
// Validate the next visit without consuming it. An already-exhausted
|
||||
// stage must not begin work it can never execute.
|
||||
if !isHuman {
|
||||
if state.visits[key]+1 > 5 {
|
||||
state.failureClass = protocol.FailureLoopExhausted
|
||||
if !isHuman && (state.visits[key]+1 > hardVisits || state.visits[key]+1 > maxVisits) {
|
||||
if decidesOnExhaustion(selected) {
|
||||
// on_exhausted=decide: ask the operator instead of failing.
|
||||
if err := r.parkExhausted(ctx, id, name, workspace, state, sequence, selected, state.visits[key], min(maxVisits, hardVisits), key); err != nil {
|
||||
if ctx.Err() != nil {
|
||||
canceled()
|
||||
return
|
||||
}
|
||||
finish("failed", "error", err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
state.failureClass = protocol.FailureLoopExhausted
|
||||
if state.visits[key]+1 > hardVisits {
|
||||
finish("failed", "error", "stage visit budget exceeded")
|
||||
return
|
||||
}
|
||||
if state.visits[key]+1 > maxVisits {
|
||||
state.failureClass = protocol.FailureLoopExhausted
|
||||
finish("failed", "error", fmt.Sprintf("stage %s visit limit reached (%d)", key, maxVisits))
|
||||
return
|
||||
}
|
||||
finish("failed", "error", fmt.Sprintf("stage %s visit limit reached (%d)", key, maxVisits))
|
||||
return
|
||||
}
|
||||
// adopted is the visit a previous server process began and left running
|
||||
// across its restart: its step row, turn files and session already exist.
|
||||
@@ -2215,6 +2301,34 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
|
||||
r.deliverRunNotificationBatch(time.Now(), 1)
|
||||
return
|
||||
}
|
||||
if stage.EffectiveType(def.Type) == stage.FindingsType {
|
||||
// A findings gate routes mechanically on the run's ledger: no
|
||||
// model, no capture, no turn files.
|
||||
step := findingsGateStep(def, state, sequence)
|
||||
step.Control = &protocol.ControlEvidence{Visit: state.visits[key], MaxVisits: maxVisits, Memory: reads}
|
||||
park := applyLimitOn(&step, selected, state.visits[key], maxVisits)
|
||||
status, outcome, diagnostic, terminal, finishErr := r.finishStage(id, name, workspace, selected, state, step)
|
||||
if finishErr != nil {
|
||||
finish("failed", "error", finishErr.Error())
|
||||
return
|
||||
}
|
||||
state.recordTurn(step, nil)
|
||||
state.completed++
|
||||
r.emitRun(protocol.RunEventFrame{RunID: id, Workspace: workspace, Status: protocol.RunStatusRunning, CurrentStage: key, CompletedSteps: state.completed,
|
||||
Step: &protocol.RunStepDelta{Sequence: step.Sequence, Stage: step.Stage, Status: step.Status, Outcome: step.Outcome, Target: step.Target, Diagnostic: step.Diagnostic}}, false)
|
||||
if terminal {
|
||||
finish(status, outcome, diagnostic)
|
||||
return
|
||||
}
|
||||
if park {
|
||||
if err := r.parkExhausted(ctx, id, name, workspace, state, state.nextSequence, selected, state.visits[key], maxVisits, step.Target); err != nil {
|
||||
finish("failed", "error", err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
state.key = step.Target
|
||||
continue
|
||||
}
|
||||
// Server skip fallback: a stage that declares a `skipped` route is
|
||||
// short-circuited only when the run's evidence is complete and empty
|
||||
// (autoSkipEligible, ui_surface.go): every visit recorded a complete
|
||||
@@ -2390,6 +2504,9 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
|
||||
if adopted != nil {
|
||||
// Follow the CLI the previous process left (see run_reattach.go).
|
||||
claudeClient.Reference = adopted.owner.Reference
|
||||
} else if def.Configs["resume_session"] == "true" && state.visits[key] > 1 {
|
||||
// A loop visit continues the stage's previous session.
|
||||
claudeClient.ResumeSessionID = state.sessions[key]
|
||||
}
|
||||
claudeClient.OnOwnership = func(session, reference string, active bool) error {
|
||||
c, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
@@ -2642,18 +2759,13 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
|
||||
if step.Control.OutcomeRepairs > 0 {
|
||||
log.Printf("run %d stage %s #%d: outcome repair follow-up sent %d time(s)", id, key, sequence, step.Control.OutcomeRepairs)
|
||||
}
|
||||
if err == nil && selected.Limits != nil && state.visits[key] >= maxVisits {
|
||||
for _, outcome := range selected.Limits.On {
|
||||
if outcome == step.Outcome {
|
||||
step.Control.LimitReached = true
|
||||
step.Control.ModelTarget = step.Target
|
||||
step.Target = protocol.FailureEnd
|
||||
step.Diagnostic = fmt.Sprintf("Pipelines stopped %s after %d visits on outcome %s", key, maxVisits, step.Outcome)
|
||||
}
|
||||
}
|
||||
parkExhausted := false
|
||||
if err == nil {
|
||||
parkExhausted = applyLimitOn(&step, selected, state.visits[key], maxVisits)
|
||||
}
|
||||
if panicked {
|
||||
step.Target = protocol.FailureEnd
|
||||
parkExhausted = false
|
||||
}
|
||||
switch {
|
||||
case step.Control.LimitReached:
|
||||
@@ -2734,6 +2846,9 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
|
||||
if artifactsDir != "" {
|
||||
state.artifactDirs = append(state.artifactDirs, strconv.Itoa(sequence))
|
||||
}
|
||||
if stage.EffectiveType(def.Type) == stage.ModelType {
|
||||
r.recordFindings(id, state, step)
|
||||
}
|
||||
} else if quota.rerun {
|
||||
// The visit runs again on the switched account with its own
|
||||
// input, told that the limit interrupted it; it does not count.
|
||||
@@ -2768,10 +2883,24 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
|
||||
canceled()
|
||||
return
|
||||
}
|
||||
if stageSession != "" {
|
||||
if state.sessions == nil {
|
||||
state.sessions = map[string]string{}
|
||||
}
|
||||
state.sessions[key] = stageSession
|
||||
}
|
||||
if terminal {
|
||||
finish(status, outcome, diagnostic)
|
||||
return
|
||||
}
|
||||
if parkExhausted {
|
||||
// on_exhausted=decide: the last allowed visit asked for another
|
||||
// round; the operator decides instead of the run failing.
|
||||
if err := r.parkExhausted(ctx, id, name, workspace, state, state.nextSequence, selected, state.visits[key], maxVisits, step.Target); err != nil {
|
||||
finish("failed", "error", err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
r.checkRunBudget(id, name, runBudget)
|
||||
if step.Status == "completed" {
|
||||
delete(state.errorLoops, key)
|
||||
|
||||
Reference in New Issue
Block a user