03123c23fd
chat_shell_dismiss hides finished runs from every readback and pushes a dismissal frame to every watcher; chat_shell_restore undoes it within a 30 s window, after which the command, output and error are purged and only a tombstone remains, reported by chat_shell_runs for clients that missed the event.
124 lines
4.1 KiB
Go
124 lines
4.1 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// initChatShellDismissals adds the dismissal columns of chat_shell_runs. A
|
|
// dismissed run (dismissed_at > 0) is left out of every readback; once its
|
|
// undo window has passed it is purged: its command, output and error are
|
|
// deleted and only a tombstone (id, session, status, times) remains, which
|
|
// tells a client that missed the dismissal to drop its card.
|
|
func (s *Store) initChatShellDismissals() error {
|
|
for _, column := range [][2]string{
|
|
{"dismissed_at", "INTEGER NOT NULL DEFAULT 0"},
|
|
{"purged", "INTEGER NOT NULL DEFAULT 0"},
|
|
} {
|
|
if err := s.addColumnIfMissing("chat_shell_runs", column[0], column[1]); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DismissChatShellRuns marks the finished runs of sessionID among ids
|
|
// dismissed at epoch millisecond at and returns the ids it dismissed. Running,
|
|
// unknown and already dismissed runs are skipped.
|
|
func (s *Store) DismissChatShellRuns(ctx context.Context, sessionID string, ids []string, at int64) ([]string, error) {
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer tx.Rollback()
|
|
var dismissed []string
|
|
for _, id := range ids {
|
|
result, err := tx.ExecContext(ctx, `UPDATE chat_shell_runs SET dismissed_at = ?
|
|
WHERE session_id = ? AND id = ? AND status != ? AND dismissed_at = 0`, at, sessionID, id, protocol.ChatShellRunRunning)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if n, _ := result.RowsAffected(); n > 0 {
|
|
dismissed = append(dismissed, id)
|
|
}
|
|
}
|
|
return dismissed, tx.Commit()
|
|
}
|
|
|
|
// RestoreChatShellRuns undoes the dismissal of the runs of sessionID among ids
|
|
// that were dismissed at or after since and not yet purged, and returns them,
|
|
// oldest first.
|
|
func (s *Store) RestoreChatShellRuns(ctx context.Context, sessionID string, ids []string, since int64) ([]protocol.ChatShellRunRecord, error) {
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer tx.Rollback()
|
|
var restored []string
|
|
for _, id := range ids {
|
|
result, err := tx.ExecContext(ctx, `UPDATE chat_shell_runs SET dismissed_at = 0
|
|
WHERE session_id = ? AND id = ? AND dismissed_at > 0 AND dismissed_at >= ? AND purged = 0`, sessionID, id, since)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if n, _ := result.RowsAffected(); n > 0 {
|
|
restored = append(restored, id)
|
|
}
|
|
}
|
|
if err := tx.Commit(); err != nil || len(restored) == 0 {
|
|
return nil, err
|
|
}
|
|
args := []any{sessionID}
|
|
for _, id := range restored {
|
|
args = append(args, id)
|
|
}
|
|
rows, err := s.db.QueryContext(ctx, chatShellRunSelect+` WHERE session_id = ? AND id IN (?`+
|
|
strings.Repeat(", ?", len(restored)-1)+`) ORDER BY started_at, id`, args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var runs []protocol.ChatShellRunRecord
|
|
for rows.Next() {
|
|
run, err := scanChatShellRun(rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
runs = append(runs, run)
|
|
}
|
|
return runs, rows.Err()
|
|
}
|
|
|
|
// PurgeDismissedChatShellRuns deletes the command, output and error of every
|
|
// run dismissed before epoch millisecond before, leaving its tombstone.
|
|
func (s *Store) PurgeDismissedChatShellRuns(ctx context.Context, before int64) (int64, error) {
|
|
result, err := s.db.ExecContext(ctx, `UPDATE chat_shell_runs SET command = '', output = '', output_text = '', error = '',
|
|
output_start = 0, output_bytes = 0, purged = 1 WHERE dismissed_at > 0 AND dismissed_at < ? AND purged = 0`, before)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected()
|
|
}
|
|
|
|
// DismissedChatShellRuns lists the ids of sessionID's dismissed runs, oldest
|
|
// first: the tombstones a readback reports so clients drop those cards.
|
|
func (s *Store) DismissedChatShellRuns(ctx context.Context, sessionID string) ([]string, error) {
|
|
rows, err := s.db.QueryContext(ctx, `SELECT id FROM chat_shell_runs WHERE session_id = ? AND dismissed_at > 0
|
|
ORDER BY started_at, id`, sessionID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var ids []string
|
|
for rows.Next() {
|
|
var id string
|
|
if err := rows.Scan(&id); err != nil {
|
|
return nil, err
|
|
}
|
|
ids = append(ids, id)
|
|
}
|
|
return ids, rows.Err()
|
|
}
|