Files
pipelines/internal/pipeline/chat_shell_dismiss.go
T
alan f2520a09b8 Keep dismissed shell commands and restore them without a time limit
A dismissed run keeps its command, status, times and output within the
usual history bounds. chat_shell_runs lists dismissed runs apart
(chat_shell_dismissed_runs), chat_shell_run_output still reads them, and
the purge timer and startup purge are gone. Rows an older server purged
are listed with purged set and cannot be restored.
2026-11-09 23:29:25 +00:00

104 lines
3.5 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) leaves the transcript readback but keeps
// its command, output and error, so the operator can still read and restore
// it. purged marks a row an older server emptied after its undo window: only
// a tombstone (id, session, status, times) remains of it.
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 are dismissed and not purged, however long ago they were dismissed,
// and returns them, oldest first.
func (s *Store) RestoreChatShellRuns(ctx context.Context, sessionID string, ids []string) ([]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 purged = 0`, sessionID, id)
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)
}
return s.queryChatShellRuns(ctx, ` WHERE session_id = ? AND id IN (?`+
strings.Repeat(", ?", len(restored)-1)+`) ORDER BY started_at, id`, args...)
}
// DismissedChatShellRuns lists sessionID's dismissed runs, oldest first, with
// their whole retained output. A purged one (an older server's tombstone) has
// no command or output.
func (s *Store) DismissedChatShellRuns(ctx context.Context, sessionID string) ([]protocol.ChatShellRunRecord, error) {
return s.queryChatShellRuns(ctx, ` WHERE session_id = ? AND dismissed_at > 0 ORDER BY started_at, id`, sessionID)
}
func (s *Store) queryChatShellRuns(ctx context.Context, where string, args ...any) ([]protocol.ChatShellRunRecord, error) {
rows, err := s.db.QueryContext(ctx, chatShellRunSelect+where, 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()
}