Keep OpenCode/Codex chat and stage sessions running across server restarts
A stopping server no longer interrupts harness sessions. Runs are canceled with a shutdown cause, so stage clients leave admitted sessions running and owned; start-up recovery re-adopts live OpenCode V2 stage sessions (wait, read back the result, continue the graph), restarts unadmitted visits, continues runs between stages, and falls back to interrupted with a reason when the harness is gone. Chats with live work are recorded on shutdown and reattached on start; turns that ended during the downtime are folded in from history. The server-spawned V2 chat serve is now a detached owned service. PIPELINES_SHUTDOWN_SESSIONS=stop restores the old behaviour.
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ChatLiveSession is one chat a stopping server left live on its harness: a
|
||||
// turn in flight, an unanswered question or approval, or running background
|
||||
// tasks. The next server process reattaches each one (and reads back the final
|
||||
// state of a turn that ended while it was down).
|
||||
type ChatLiveSession struct {
|
||||
SessionID string
|
||||
Backend string
|
||||
// Turn is true when a turn was in flight; NativeTurnID names it when the
|
||||
// harness reported one, so a reply is announced once.
|
||||
Turn bool
|
||||
NativeTurnID string
|
||||
// Attention is true when a question/approval was pending or background
|
||||
// tasks were running.
|
||||
Attention bool
|
||||
RecordedAt time.Time
|
||||
}
|
||||
|
||||
func (s *Store) initChatLiveSessions() error {
|
||||
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS chat_live_sessions (
|
||||
session_id TEXT PRIMARY KEY,
|
||||
backend TEXT NOT NULL DEFAULT '',
|
||||
turn INTEGER NOT NULL DEFAULT 0,
|
||||
native_turn_id TEXT NOT NULL DEFAULT '',
|
||||
attention INTEGER NOT NULL DEFAULT 0,
|
||||
recorded_at INTEGER NOT NULL)`)
|
||||
return err
|
||||
}
|
||||
|
||||
// RecordChatLiveSessions replaces the recorded set with sessions.
|
||||
func (s *Store) RecordChatLiveSessions(ctx context.Context, sessions []ChatLiveSession) error {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if _, err := tx.ExecContext(ctx, `DELETE FROM chat_live_sessions`); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, session := range sessions {
|
||||
at := session.RecordedAt
|
||||
if at.IsZero() {
|
||||
at = time.Now()
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `INSERT OR REPLACE INTO chat_live_sessions(session_id,backend,turn,native_turn_id,attention,recorded_at) VALUES(?,?,?,?,?,?)`,
|
||||
session.SessionID, session.Backend, session.Turn, session.NativeTurnID, session.Attention, at.UnixMilli()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// TakeChatLiveSessions returns the recorded set and clears it, so each start
|
||||
// reattaches what the previous process recorded exactly once.
|
||||
func (s *Store) TakeChatLiveSessions(ctx context.Context) ([]ChatLiveSession, error) {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
rows, err := tx.QueryContext(ctx, `SELECT session_id,backend,turn,native_turn_id,attention,recorded_at FROM chat_live_sessions ORDER BY recorded_at, session_id`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var sessions []ChatLiveSession
|
||||
for rows.Next() {
|
||||
var session ChatLiveSession
|
||||
var at int64
|
||||
if err := rows.Scan(&session.SessionID, &session.Backend, &session.Turn, &session.NativeTurnID, &session.Attention, &at); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
session.RecordedAt = time.UnixMilli(at)
|
||||
sessions = append(sessions, session)
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `DELETE FROM chat_live_sessions`); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return sessions, tx.Commit()
|
||||
}
|
||||
@@ -190,18 +190,57 @@ func (s *Store) addColumnIfMissing(table, column, definition string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// RestartStep marks a visit that a stopped server had begun but had not yet
|
||||
// handed to any harness session as interrupted, so its run can begin the same
|
||||
// stage again under a new sequence. Nothing was admitted, so nothing replays.
|
||||
func (s *Store) RestartStep(ctx context.Context, id int64, sequence int, diagnostic string) error {
|
||||
return affected(s.db.ExecContext(ctx, "UPDATE run_steps SET status='interrupted',agent_active=0,diagnostic=?,finished_at=CURRENT_TIMESTAMP WHERE run_id=? AND sequence=? AND status='running' AND session_id=''", diagnostic, id, sequence))
|
||||
}
|
||||
|
||||
func (s *Store) RecoverRuns(ctx context.Context) error {
|
||||
return s.RecoverRunsKeeping(ctx, nil, nil)
|
||||
}
|
||||
|
||||
// RecoverRunsKeeping is RecoverRuns for a server that reattaches work across a
|
||||
// restart: runs in keep are left exactly as the previous process left them (the
|
||||
// runner continues them), and a run in reasons is interrupted with that reason
|
||||
// instead of the generic one, so the operator sees why it was not reattached.
|
||||
func (s *Store) RecoverRunsKeeping(ctx context.Context, keep map[int64]bool, reasons map[int64]string) error {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
kept, keptArgs := "", []any{}
|
||||
for id := range keep {
|
||||
if kept != "" {
|
||||
kept += ","
|
||||
}
|
||||
kept += "?"
|
||||
keptArgs = append(keptArgs, id)
|
||||
}
|
||||
stepFilter, runFilter := "", ""
|
||||
if kept != "" {
|
||||
stepFilter = " AND run_id NOT IN (" + kept + ")"
|
||||
runFilter = " AND id NOT IN (" + kept + ")"
|
||||
}
|
||||
// No replay: pending visits never began. A cleanup agent that was still
|
||||
// active is marked interrupted, and its run is interrupted below.
|
||||
if _, err := tx.ExecContext(ctx, "UPDATE run_steps SET status=CASE WHEN (SELECT cancel_requested FROM pipeline_runs WHERE id=run_steps.run_id)=1 THEN 'canceled' ELSE 'interrupted' END,agent_active=0,diagnostic='Server stopped; stage was not resumed',finished_at=CURRENT_TIMESTAMP WHERE (status='running' OR agent_active=1) AND status<>? AND run_id IN (SELECT id FROM pipeline_runs WHERE cleanup_unconfirmed=0)", protocol.StepStatusWaiting); err != nil {
|
||||
if _, err := tx.ExecContext(ctx, "UPDATE run_steps SET status=CASE WHEN (SELECT cancel_requested FROM pipeline_runs WHERE id=run_steps.run_id)=1 THEN 'canceled' ELSE 'interrupted' END,agent_active=0,diagnostic='Server stopped; stage was not resumed',finished_at=CURRENT_TIMESTAMP WHERE (status='running' OR agent_active=1) AND status<>? AND run_id IN (SELECT id FROM pipeline_runs WHERE cleanup_unconfirmed=0)"+stepFilter, append([]any{protocol.StepStatusWaiting}, keptArgs...)...); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, "UPDATE pipeline_runs SET status=CASE WHEN cancel_requested=1 THEN 'canceled' ELSE 'interrupted' END,diagnostic='Server stopped; owned sessions stopped; run was not resumed',finished_at=CURRENT_TIMESTAMP WHERE status IN ('queued','running') AND status<>? AND cleanup_unconfirmed=0", protocol.RunStatusWaiting); err != nil {
|
||||
for id, reason := range reasons {
|
||||
if keep[id] {
|
||||
continue
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, "UPDATE run_steps SET diagnostic=? WHERE run_id=? AND status='interrupted' AND sequence=(SELECT MAX(sequence) FROM run_steps WHERE run_id=?)", "Server restarted; "+reason, id, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, "UPDATE pipeline_runs SET status='interrupted',diagnostic=?,finished_at=CURRENT_TIMESTAMP WHERE id=? AND status IN ('queued','running') AND cancel_requested=0 AND cleanup_unconfirmed=0", "Server restarted; "+reason+"; run was not resumed", id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, "UPDATE pipeline_runs SET status=CASE WHEN cancel_requested=1 THEN 'canceled' ELSE 'interrupted' END,diagnostic='Server stopped; owned sessions stopped; run was not resumed',finished_at=CURRENT_TIMESTAMP WHERE status IN ('queued','running') AND status<>? AND cleanup_unconfirmed=0"+runFilter, append([]any{protocol.RunStatusWaiting}, keptArgs...)...); err != nil {
|
||||
return err
|
||||
}
|
||||
// An export checkpoint is a paused handoff, not an interrupted model call.
|
||||
|
||||
@@ -93,6 +93,10 @@ CREATE TABLE IF NOT EXISTS submissions (id INTEGER PRIMARY KEY, name TEXT NOT NU
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err := s.initChatLiveSessions(); err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err := s.initAppNotifications(); err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user