Runs: one uninterruptible stage session no longer disables the runner

Start-up recovery used to fail the whole runner when any previously active
stage session could not be confirmed stopped. That run alone is now marked
interrupted with a diagnostic naming the session and error. A Pi owner whose
process and process group are gone counts as stopped without its quiescence
receipt (it crashed), as the Claude stage path already did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 00:23:39 +00:00
parent 3e267d4863
commit e649eced10
4 changed files with 83 additions and 3 deletions
+34
View File
@@ -194,6 +194,40 @@ func TestStageRunWithHarnessGoneFallsBackWithReason(t *testing.T) {
} }
} }
// TestUninterruptibleStageSessionInterruptsOnlyItsRun: one stage session that
// start-up cannot confirm stopped (a crashed Pi owner, a missing Claude
// evidence directory) used to fail the whole runner, disabling every run.
// Now only its run is interrupted, naming why, and the runner starts.
func TestUninterruptibleStageSessionInterruptsOnlyItsRun(t *testing.T) {
seamRestartRecovery(t, nil)
interruptRecoveredSession = func(context.Context, pipeline.SessionOwner) error {
return fmt.Errorf("retained Pi owner: %w", stage.ErrStageCleanup)
}
f := stopMidStage(t, func(ctx context.Context, s *pipeline.Store, id int64) (stage.Result, error) {
if err := s.RunSession(context.Background(), id, 1, "ses_pi", true, "pi", "/nonexistent/pi-ref"); err != nil {
return stage.Result{}, err
}
<-ctx.Done()
return stage.Result{}, stage.ErrDetached
})
r, s := f.restart(t, func(context.Context, *pipeline.Store, stage.Definition) (stage.Result, error) {
t.Error("an interrupted run executed again")
return stage.Result{}, nil
})
r.reattachRuns()
r.wg.Wait()
d, err := s.GetRun(context.Background(), f.runID)
if err != nil {
t.Fatal(err)
}
if d.Status != "interrupted" || !strings.Contains(d.Diagnostic, "could not be confirmed stopped") || !strings.Contains(d.Diagnostic, "ses_pi") {
t.Fatalf("uninterruptible session's run lacks a clear reason: status=%s run=%q", d.Status, d.Diagnostic)
}
if owners, _ := s.ActiveSessionOwners(context.Background()); len(owners) != 0 {
t.Fatalf("the interrupted run's session still blocks recovery at the next start: %+v", owners)
}
}
// TestStageNotYetAdmittedStartsAgainAfterRestart: a visit the stopped server // TestStageNotYetAdmittedStartsAgainAfterRestart: a visit the stopped server
// had begun but not handed to any session is started again, nothing replayed. // had begun but not handed to any session is started again, nothing replayed.
func TestStageNotYetAdmittedStartsAgainAfterRestart(t *testing.T) { func TestStageNotYetAdmittedStartsAgainAfterRestart(t *testing.T) {
+9 -1
View File
@@ -152,7 +152,15 @@ func newRunnerReattaching(ctx context.Context, s *pipeline.Store, artifactsRoot
cancel() cancel()
} }
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot interrupt previously active agent %s: %w", owner.ID, err) // One session that cannot be confirmed stopped must not disable
// every run: interrupt only its run, saying why, and go on.
reason := fmt.Sprintf("its previous %s stage agent (session %s) could not be confirmed stopped (%v); check for a leftover agent process before reusing the workspace", owner.Backend, owner.ID, err)
if prior := reasons[owner.RunID]; prior != "" {
reason = prior + "; " + reason
}
reasons[owner.RunID] = reason
log.Printf("run %d: cannot interrupt previously active agent %s: %v; the run is marked interrupted", owner.RunID, owner.ID, err)
continue
} }
if err := s.ConfirmRecoveredSession(ctx, owner); err != nil { if err := s.ConfirmRecoveredSession(ctx, owner); err != nil {
return nil, err return nil, err
+12 -2
View File
@@ -608,7 +608,9 @@ func piResult(data []byte) (string, error) {
} }
// InterruptPi expires the retained lease. The explicit extension aborts tools and // InterruptPi expires the retained lease. The explicit extension aborts tools and
// marks quiescence before shutting down; without that receipt recovery fails closed. // marks quiescence before shutting down. An owner whose process and process
// group are both gone (a crash, a reboot) has nothing left to stop, so it
// counts as interrupted without that receipt; otherwise recovery fails closed.
func InterruptPi(ctx context.Context, reference string) error { func InterruptPi(ctx context.Context, reference string) error {
if !filepath.IsAbs(reference) { if !filepath.IsAbs(reference) {
return fmt.Errorf("invalid retained Pi reference") return fmt.Errorf("invalid retained Pi reference")
@@ -623,6 +625,12 @@ func InterruptPi(ctx context.Context, reference string) error {
if json.Unmarshal(data, &owner) != nil || owner.PID <= 1 { if json.Unmarshal(data, &owner) != nil || owner.PID <= 1 {
return errors.Join(fmt.Errorf("invalid retained Pi owner"), ErrStageCleanup) return errors.Join(fmt.Errorf("invalid retained Pi owner"), ErrStageCleanup)
} }
gone := func() bool {
return errors.Is(syscall.Kill(owner.PID, 0), syscall.ESRCH) && errors.Is(syscall.Kill(-owner.PID, 0), syscall.ESRCH)
}
if gone() {
return nil
}
old := time.Unix(0, 0) old := time.Unix(0, 0)
if err := os.Chtimes(filepath.Join(reference, "lease"), old, old); err != nil { if err := os.Chtimes(filepath.Join(reference, "lease"), old, old); err != nil {
return err return err
@@ -630,7 +638,9 @@ func InterruptPi(ctx context.Context, reference string) error {
ticker := time.NewTicker(100 * time.Millisecond) ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop() defer ticker.Stop()
for { for {
if data, err := os.ReadFile(filepath.Join(reference, "quiescent")); err == nil && string(data) == "quiescent" && errors.Is(syscall.Kill(owner.PID, 0), syscall.ESRCH) && errors.Is(syscall.Kill(-owner.PID, 0), syscall.ESRCH) { // The receipt is written just before a clean exit; a process that
// died without it is still gone.
if gone() {
return nil return nil
} }
select { select {
+28
View File
@@ -42,3 +42,31 @@ func TestPiReceiptRequiresProcessExit(t *testing.T) {
t.Fatal("confirmed process exit not accepted", err) t.Fatal("confirmed process exit not accepted", err)
} }
} }
// A Pi owner that crashed never wrote its quiescence receipt; with its process
// and process group gone there is nothing left to stop, so recovery must not
// wait out its timeout and fail.
func TestPiOwnerGoneWithoutReceiptCountsAsInterrupted(t *testing.T) {
cmd := exec.Command("sleep", "5")
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
pid := cmd.Process.Pid
if err := cmd.Process.Kill(); err != nil {
t.Fatal(err)
}
cmd.Wait()
dir := t.TempDir()
owner, _ := json.Marshal(map[string]any{"pid": pid})
for name, data := range map[string][]byte{"owner.json": owner, "lease": []byte("lease")} {
if err := os.WriteFile(filepath.Join(dir, name), data, 0600); err != nil {
t.Fatal(err)
}
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if err := InterruptPi(ctx, dir); err != nil {
t.Fatal("a crashed owner was not accepted as gone:", err)
}
}