Runner: no I/O under the runner lock; retry run settlement
- Run worktree creation happens outside r.mu; the source tree is protected from storage maintenance meanwhile and the reservation is re-checked under the lock before the run is recorded (a refused tree is discarded). - getRun/listRuns read the store without r.mu; listRuns finds failed runs with an active cleanup agent in one query (partial index) instead of a detail read per failed run. - finishRunState writes the deliverable and walks the run size without the lock; FinishRun retries transient failures with backoff (a refused transition is final). - Launch teardown retries a failed read with backoff instead of keeping the reservation forever; a kept run that cannot be read at reattach is retried and then marked interrupted (stopping its adopted session first). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -172,9 +172,10 @@ func (r *runner) reattachRun(plan reattachPlan) {
|
||||
log.Printf("run %d: restart stage %s after server restart: %v", id, plan.step.Stage, err)
|
||||
}
|
||||
}
|
||||
detail, err := r.store.GetRun(ctx, id)
|
||||
detail, err := r.readForReattach(ctx, id)
|
||||
if err != nil {
|
||||
log.Printf("run %d: read for reattach: %v", id, err)
|
||||
log.Printf("run %d: read for reattach: %v; marking it interrupted", id, err)
|
||||
r.abandonReattach(plan, err)
|
||||
return
|
||||
}
|
||||
r.rememberRunIdentity(id, identityOfRunSummary(detail.RunSummary))
|
||||
@@ -199,6 +200,63 @@ func (r *runner) reattachRun(plan reattachPlan) {
|
||||
}
|
||||
}
|
||||
|
||||
// reattachReadBackoff bounds the retries of a reattach read; reattach runs
|
||||
// before requests are served, so it stays short.
|
||||
var reattachReadBackoff = []time.Duration{100 * time.Millisecond, 500 * time.Millisecond, 2 * time.Second}
|
||||
|
||||
// readForReattach reads a kept run, retrying a transient failure.
|
||||
func (r *runner) readForReattach(ctx context.Context, id int64) (*protocol.RunDetail, error) {
|
||||
for attempt := 0; ; attempt++ {
|
||||
detail, err := runSettleRead(r, ctx, id)
|
||||
if err == nil && detail != nil {
|
||||
return detail, nil
|
||||
}
|
||||
if err == nil {
|
||||
err = fmt.Errorf("run %d not found", id)
|
||||
}
|
||||
if attempt >= len(reattachReadBackoff) || ctx.Err() != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !r.sleepSettle(reattachReadBackoff[attempt]) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// abandonReattach settles a kept run that cannot be continued, instead of
|
||||
// leaving it "running" with nothing executing it: an adopted session is
|
||||
// stopped first, then the run is marked interrupted. If the session cannot be
|
||||
// stopped, the run keeps its workspace reserved as cleanup-unconfirmed, as
|
||||
// start-up recovery does.
|
||||
func (r *runner) abandonReattach(plan reattachPlan, cause error) {
|
||||
id := plan.runID
|
||||
if plan.kind == reattachAdopt {
|
||||
stop, cancel := context.WithTimeout(r.ctx, 20*time.Second)
|
||||
err := interruptRecoveredSession(stop, plan.owner)
|
||||
if err == nil {
|
||||
err = r.store.ConfirmRecoveredSession(stop, plan.owner)
|
||||
}
|
||||
cancel()
|
||||
if err != nil {
|
||||
log.Printf("run %d: stop session %s after a failed reattach: %v; workspace stays reserved", id, plan.owner.ID, err)
|
||||
mark, done := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
if markErr := r.store.MarkCleanupUnconfirmed(mark, id); markErr != nil {
|
||||
log.Printf("run %d: mark cleanup unconfirmed: %v", id, markErr)
|
||||
}
|
||||
done()
|
||||
r.mu.Lock()
|
||||
if _, ok := r.active[id]; !ok {
|
||||
r.active[id] = activeRun{cancel: func() {}, workspace: plan.owner.Directory}
|
||||
}
|
||||
r.mu.Unlock()
|
||||
return
|
||||
}
|
||||
}
|
||||
r.finishRunWithRetry(id, "interrupted", "", "Server restarted; the run could not be reattached: "+cause.Error(), stage.Values{})
|
||||
r.forgetAttention(id)
|
||||
r.forgetNotifyMode(id)
|
||||
}
|
||||
|
||||
// rebuildAdoptState restores the loop state at the planned visit: memory and
|
||||
// visit counts from every other persisted step, the handoff input from the
|
||||
// visit before it, and (for an adopted visit) the session to re-adopt.
|
||||
|
||||
Reference in New Issue
Block a user