aa8eddd686
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.
91 lines
2.8 KiB
Go
91 lines
2.8 KiB
Go
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()
|
|
}
|