Files
pipelines/internal/server/run_reattach.go
T
alan 2ea7eb9db0 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>
2026-12-03 00:42:25 +00:00

285 lines
11 KiB
Go

package server
import (
"context"
"errors"
"fmt"
"log"
"time"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
)
// Runs survive a server restart. With detach enabled (the installed server),
// a stopping server cancels each run's context with stage.ErrServerShutdown:
// stage clients leave their admitted harness session running and owned, and the
// loop returns without settling anything. The next process plans, before it
// serves requests, how to continue every run it finds in flight:
//
// - a running visit with a live OpenCode V2 session is re-adopted: the loop
// re-enters that visit, attaches to the session, waits for it to settle (or
// reads the result it reached while the server was down) and continues the
// graph as if it had watched it;
// - a visit begun but never handed to a harness is marked interrupted and the
// same stage starts again (nothing was admitted, so nothing replays);
// - a run between visits continues with its next stage;
// - a session whose harness process is gone, or that belongs to a harness the
// server owned as a child (Pi, OpenCode V1), falls back to the previous
// behaviour: the run is interrupted, with the reason in its diagnostic.
//
// Only an operator cancel (cancel_run) stops a stage session.
type reattachKind int
const (
reattachAdopt reattachKind = iota
reattachRestart
reattachBetween
)
// reattachPlan is one run start-up recovery kept for reattachRuns.
type reattachPlan struct {
runID int64
kind reattachKind
// step is the running visit (adopt/restart) or the last completed one.
step protocol.RunStep
owner pipeline.SessionOwner
}
// adoptedVisit is the visit a runLoop re-adopts instead of starting.
type adoptedVisit struct {
sequence int
owner pipeline.SessionOwner
}
// stageServiceAlive reports whether a retained stage session's harness service
// is still running. It is a seam for tests.
var stageServiceAlive = func(ctx context.Context, owner pipeline.SessionOwner) error {
return opencode.ReferenceAlive(ctx, owner.Reference)
}
// interruptRecoveredSession stops a previously active session that start-up
// recovery does not re-adopt. It is a seam for tests.
var interruptRecoveredSession = func(ctx context.Context, owner pipeline.SessionOwner) error {
if owner.Backend == "pi" {
return stage.InterruptPi(ctx, owner.Reference)
}
if owner.Backend == stage.ClaudeBackend {
return stage.InterruptClaudeStage(ctx, owner.Reference)
}
return (stage.OpenCodeClient{Backend: owner.Backend, Directory: owner.Directory, Reference: owner.Reference}).InterruptAndConfirm(ctx, owner.ID)
}
// runContext derives one run's execution context. With detach, a server
// shutdown cancels it with stage.ErrServerShutdown as the cause, while the
// returned cancel (an operator cancel or the run's own end) uses the ordinary
// context.Canceled, so stage clients can tell the two apart.
func (r *runner) runContext() (context.Context, context.CancelFunc) {
if !r.detach {
return context.WithCancel(r.ctx)
}
ctx, cancel := context.WithCancelCause(context.WithoutCancel(r.ctx))
stop := context.AfterFunc(r.ctx, func() { cancel(stage.ErrServerShutdown) })
return ctx, func() { stop(); cancel(context.Canceled) }
}
// planReattach decides, per in-flight run, whether start-up continues it.
// Runs it does not plan keep today's recovery (interrupted); reasons carries the
// explanation for the ones that could not be reattached.
// gone lists runs whose session's service is not running: there is nothing
// left to interrupt, so recovery only clears their ownership.
func planReattach(ctx context.Context, s *pipeline.Store) ([]reattachPlan, map[int64]string, map[int64]bool, error) {
reasons, gone := map[int64]string{}, map[int64]bool{}
rows, err := s.ListRuns(ctx)
if err != nil {
return nil, nil, nil, err
}
var plans []reattachPlan
for _, row := range rows {
if row.Status != protocol.RunStatusRunning || row.CancelRequested || row.CleanupUnconfirmed {
continue
}
detail, err := s.GetRun(ctx, row.ID)
if err != nil {
return nil, nil, nil, err
}
if len(detail.Steps) == 0 {
continue
}
last := detail.Steps[0]
for _, step := range detail.Steps {
if step.Sequence > last.Sequence {
last = step
}
}
switch last.Status {
case protocol.StepStatusRunning:
if last.SessionID == "" {
plans = append(plans, reattachPlan{runID: row.ID, kind: reattachRestart, step: last})
continue
}
owner, err := s.RunSessionOwner(ctx, row.ID, last.Sequence)
if err != nil {
return nil, nil, nil, err
}
owner.RunID = row.ID
if owner.Backend != "v2" {
name := "OpenCode V1"
if owner.Backend == "pi" {
name = "Pi"
}
if owner.Backend == stage.ClaudeBackend {
name = "Claude Code"
}
reasons[row.ID] = fmt.Sprintf("%s stage sessions belong to the stopped server process, so stage %s could not be reattached", name, last.Stage)
continue
}
probe, cancel := context.WithTimeout(ctx, 5*time.Second)
aliveErr := stageServiceAlive(probe, owner)
cancel()
if aliveErr != nil {
gone[row.ID] = errors.Is(aliveErr, opencode.ErrServiceNotRunning)
reasons[row.ID] = fmt.Sprintf("the OpenCode service running stage %s was gone (%v), so its session could not be reattached", last.Stage, aliveErr)
continue
}
plans = append(plans, reattachPlan{runID: row.ID, kind: reattachAdopt, step: last, owner: owner})
case "completed":
plans = append(plans, reattachPlan{runID: row.ID, kind: reattachBetween, step: last})
}
}
return plans, reasons, gone, nil
}
// reattachRuns continues every run start-up recovery kept. It is called once,
// after the runner's collaborators are wired and before requests are served.
func (r *runner) reattachRuns() {
plans := r.reattach
r.reattach = nil
for _, plan := range plans {
r.reattachRun(plan)
}
}
func (r *runner) reattachRun(plan reattachPlan) {
id := plan.runID
ctx, cancel := context.WithTimeout(r.ctx, 10*time.Second)
defer cancel()
if plan.kind == reattachRestart {
if err := r.store.RestartStep(ctx, id, plan.step.Sequence, "Server restarted before this stage handed work to a harness; the stage was started again"); err != nil {
log.Printf("run %d: restart stage %s after server restart: %v", id, plan.step.Stage, err)
}
}
detail, err := r.readForReattach(ctx, id)
if err != nil {
log.Printf("run %d: read for reattach: %v; marking it interrupted", id, err)
r.abandonReattach(plan, err)
return
}
r.rememberRunIdentity(id, identityOfRunSummary(detail.RunSummary))
workspace := detail.Workspace
request := protocol.Message{WritePaths: detail.WritePaths, AllowTools: detail.AllowTools, Model: detail.Model, StageModels: detail.StageModels}
r.mu.Lock()
defer r.mu.Unlock()
switch plan.kind {
case reattachBetween:
log.Printf("run %d: continuing after stage %s following a server restart", id, plan.step.Stage)
r.launch(id, workspace, func(ctx context.Context) { r.resume(ctx, id, detail, workspace, plan.step) })
default:
state := rebuildAdoptState(detail, plan)
if plan.kind == reattachAdopt {
log.Printf("run %d: reattached stage %s #%d session %s after a server restart", id, plan.step.Stage, plan.step.Sequence, plan.owner.ID)
} else {
log.Printf("run %d: starting stage %s again after a server restart", id, plan.step.Stage)
}
r.launch(id, workspace, func(ctx context.Context) {
r.runLoop(ctx, id, detail.Graph, detail.Name, workspace, request, state)
})
}
}
// 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.
func rebuildAdoptState(detail *protocol.RunDetail, plan reattachPlan) *runState {
decided := protocol.RunStep{Target: plan.step.Stage, Output: detail.Input}
previous := 0
for _, step := range detail.Steps {
if step.Sequence < plan.step.Sequence && step.Sequence > previous {
previous = step.Sequence
decided = step
decided.Target = plan.step.Stage
}
}
state := rebuildRunState(detail, decided)
state.key = plan.step.Stage
// The interrupted or adopted visit was counted; the loop counts it again.
if state.visits[plan.step.Stage] > 0 {
state.visits[plan.step.Stage]--
}
if plan.kind == reattachAdopt {
state.nextSequence = plan.step.Sequence
state.adopt = &adoptedVisit{sequence: plan.step.Sequence, owner: plan.owner}
}
return state
}