144 lines
5.3 KiB
Go
144 lines
5.3 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()
|
|
}
|
|
|
|
// ChatLiveSessions reads recovery membership without consuming it. A failed
|
|
// reconnect or a second crash must not erase the only record of live work.
|
|
func (s *Store) ChatLiveSessions(ctx context.Context) ([]ChatLiveSession, error) {
|
|
rows, err := s.db.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
|
|
}
|
|
defer rows.Close()
|
|
var out []ChatLiveSession
|
|
for rows.Next() {
|
|
var v ChatLiveSession
|
|
var at int64
|
|
if err := rows.Scan(&v.SessionID, &v.Backend, &v.Turn, &v.NativeTurnID, &v.Attention, &at); err != nil {
|
|
return nil, err
|
|
}
|
|
v.RecordedAt = time.UnixMilli(at)
|
|
out = append(out, v)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// RememberChatLiveSession journals live work as it is observed, not only at a
|
|
// graceful shutdown. Attention remains sticky until an authoritative reconnect
|
|
// confirms idle: evicting a bounded replay cache is not evidence tasks ended.
|
|
func (s *Store) RememberChatLiveSession(ctx context.Context, v ChatLiveSession) error {
|
|
if v.RecordedAt.IsZero() {
|
|
v.RecordedAt = time.Now()
|
|
}
|
|
if !v.Turn && !v.Attention {
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
if _, err := tx.ExecContext(ctx, `UPDATE chat_live_sessions SET turn=0,native_turn_id='' WHERE session_id=?`, v.SessionID); err != nil {
|
|
return err
|
|
}
|
|
if _, err := tx.ExecContext(ctx, `DELETE FROM chat_live_sessions WHERE session_id=? AND turn=0 AND attention=0`, v.SessionID); err != nil {
|
|
return err
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
_, err := s.db.ExecContext(ctx, `INSERT INTO chat_live_sessions(session_id,backend,turn,native_turn_id,attention,recorded_at) VALUES(?,?,?,?,?,?) ON CONFLICT(session_id) DO UPDATE SET backend=excluded.backend,turn=excluded.turn,native_turn_id=CASE WHEN excluded.turn=0 THEN '' WHEN excluded.native_turn_id!='' THEN excluded.native_turn_id ELSE chat_live_sessions.native_turn_id END,attention=MAX(chat_live_sessions.attention,excluded.attention),recorded_at=excluded.recorded_at`, v.SessionID, v.Backend, v.Turn, v.NativeTurnID, v.Attention, v.RecordedAt.UnixMilli())
|
|
return err
|
|
}
|
|
|
|
// ForgetChatLiveSession is used only after authoritative idle or explicit close.
|
|
func (s *Store) ForgetChatLiveSession(ctx context.Context, id string) error {
|
|
_, err := s.db.ExecContext(ctx, `DELETE FROM chat_live_sessions WHERE session_id=?`, id)
|
|
return err
|
|
}
|