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:
2026-12-03 00:42:25 +00:00
parent ab6b2bb632
commit 2ea7eb9db0
7 changed files with 567 additions and 70 deletions
+6 -1
View File
@@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"strings"
@@ -20,6 +21,10 @@ func (s *Store) SaveNotifications(ctx context.Context, id int64, sequence int, r
return err
}
// ErrInvalidRunTransition reports a conditional run update that matched no row:
// the run is not in a state the transition applies to. Retrying cannot help.
var ErrInvalidRunTransition = errors.New("invalid run state transition")
func affected(result sql.Result, err error) error {
if err != nil {
return err
@@ -29,7 +34,7 @@ func affected(result sql.Result, err error) error {
return err
}
if n != 1 {
return fmt.Errorf("invalid run state transition")
return ErrInvalidRunTransition
}
return nil
}
+1
View File
@@ -26,6 +26,7 @@ session_id TEXT NOT NULL DEFAULT '', agent_active INTEGER NOT NULL DEFAULT 0, tr
started_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, finished_at TEXT, PRIMARY KEY(run_id,sequence));
CREATE INDEX IF NOT EXISTS run_latest ON pipeline_runs(id DESC);
CREATE INDEX IF NOT EXISTS run_steps_session ON run_steps(session_id);
CREATE INDEX IF NOT EXISTS run_steps_agent_active ON run_steps(run_id) WHERE agent_active<>0;
CREATE TABLE IF NOT EXISTS step_notifications (
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, receipts_json TEXT NOT NULL,
PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence));
+19
View File
@@ -143,3 +143,22 @@ func (s *Store) ActiveAgentSessionsForRun(ctx context.Context, runID int64) ([]s
}
return ids, rows.Err()
}
// RunsWithActiveAgents reports every run with a step whose agent is still
// active, in one query.
func (s *Store) RunsWithActiveAgents(ctx context.Context) (map[int64]bool, error) {
rows, err := s.db.QueryContext(ctx, "SELECT DISTINCT run_id FROM run_steps WHERE agent_active<>0")
if err != nil {
return nil, err
}
defer rows.Close()
runs := map[int64]bool{}
for rows.Next() {
var id int64
if err := rows.Scan(&id); err != nil {
return nil, err
}
runs[id] = true
}
return runs, rows.Err()
}