Dismiss and restore operator shell commands per chat

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.
This commit is contained in:
2026-11-09 21:22:07 +00:00
parent 8f06f12d49
commit 03123c23fd
11 changed files with 513 additions and 9 deletions
+123
View File
@@ -0,0 +1,123 @@
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()
}
@@ -0,0 +1,102 @@
package pipeline
import (
"context"
"fmt"
"path/filepath"
"testing"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func TestChatShellRunsDismissRestoreAndPurge(t *testing.T) {
ctx := context.Background()
store, err := Open(filepath.Join(t.TempDir(), "shell.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
save := func(id, session, status string, started int64) {
t.Helper()
run := protocol.ChatShellRunRecord{ID: id, SessionID: session, Command: "echo " + id, Directory: "/w", Status: status,
StartedAt: started, Output: "out " + id, OutputBytes: 6, Text: "out " + id, Error: "err"}
if err := store.SaveChatShellRun(ctx, run, 0); err != nil {
t.Fatal(err)
}
}
save("run_a", "ses_1", protocol.ChatShellRunExited, 1)
save("run_b", "ses_1", protocol.ChatShellRunFailed, 2)
save("run_live", "ses_1", protocol.ChatShellRunRunning, 3)
save("run_other", "ses_2", protocol.ChatShellRunExited, 4)
ids := func(session string) string {
t.Helper()
runs, err := store.ChatShellRuns(ctx, session)
if err != nil {
t.Fatal(err)
}
var out []string
for _, run := range runs {
out = append(out, run.ID)
}
return fmt.Sprint(out)
}
// Running, foreign and unknown runs are skipped.
dismissed, err := store.DismissChatShellRuns(ctx, "ses_1", []string{"run_a", "run_live", "run_other", "nope"}, 1000)
if err != nil || fmt.Sprint(dismissed) != "[run_a]" {
t.Fatalf("dismissed = %v, %v", dismissed, err)
}
if got := ids("ses_1"); got != "[run_b run_live]" {
t.Fatalf("readback after dismiss = %s", got)
}
if _, found, _ := store.ChatShellRun(ctx, "ses_1", "run_a"); found {
t.Fatal("a dismissed run is still readable")
}
if again, _ := store.DismissChatShellRuns(ctx, "ses_1", []string{"run_a"}, 1001); len(again) != 0 {
t.Fatalf("dismissed twice: %v", again)
}
if tombs, _ := store.DismissedChatShellRuns(ctx, "ses_1"); fmt.Sprint(tombs) != "[run_a]" {
t.Fatalf("tombstones = %v", tombs)
}
// Undo inside the window restores the whole record.
restored, err := store.RestoreChatShellRuns(ctx, "ses_1", []string{"run_a", "run_b"}, 900)
if err != nil || len(restored) != 1 || restored[0].ID != "run_a" || restored[0].Output != "out run_a" || restored[0].Text != "out run_a" {
t.Fatalf("restored = %+v, %v", restored, err)
}
if got := ids("ses_1"); got != "[run_a run_b run_live]" {
t.Fatalf("readback after restore = %s", got)
}
// Past the window: no restore, and the purge keeps only a tombstone.
if _, err := store.DismissChatShellRuns(ctx, "ses_1", []string{"run_a", "run_b"}, 2000); err != nil {
t.Fatal(err)
}
if late, err := store.RestoreChatShellRuns(ctx, "ses_1", []string{"run_a"}, 2001); err != nil || len(late) != 0 {
t.Fatalf("late restore = %+v, %v", late, err)
}
if n, err := store.PurgeDismissedChatShellRuns(ctx, 2000); err != nil || n != 0 {
t.Fatalf("purged inside the window: %d, %v", n, err)
}
if n, err := store.PurgeDismissedChatShellRuns(ctx, 2001); err != nil || n != 2 {
t.Fatalf("purge = %d, %v", n, err)
}
if restoredPurged, _ := store.RestoreChatShellRuns(ctx, "ses_1", []string{"run_a"}, 0); len(restoredPurged) != 0 {
t.Fatalf("a purged run was restored: %+v", restoredPurged)
}
var command, output, text, errText string
var bytes int64
if err := store.db.QueryRowContext(ctx, `SELECT command, output, output_text, error, output_bytes FROM chat_shell_runs WHERE id = 'run_a'`).
Scan(&command, &output, &text, &errText, &bytes); err != nil {
t.Fatal(err)
}
if command != "" || output != "" || text != "" || errText != "" || bytes != 0 {
t.Fatalf("purged row kept %q %q %q %q %d", command, output, text, errText, bytes)
}
if tombs, _ := store.DismissedChatShellRuns(ctx, "ses_1"); fmt.Sprint(tombs) != "[run_a run_b]" {
t.Fatalf("tombstones = %v", tombs)
}
if got := ids("ses_2"); got != "[run_other]" {
t.Fatalf("another session changed: %s", got)
}
}
+6 -5
View File
@@ -43,7 +43,7 @@ CREATE INDEX IF NOT EXISTS chat_shell_runs_session ON chat_shell_runs(session_id
return err
}
}
return nil
return s.initChatShellDismissals()
}
// SaveChatShellRun inserts or replaces one run. Inserting a new run prunes the
@@ -81,9 +81,10 @@ cols = excluded.cols, rows = excluded.rows, output_text = excluded.output_text`,
return tx.Commit()
}
// ChatShellRuns lists one session's runs, oldest first.
// ChatShellRuns lists one session's runs, oldest first, leaving out dismissed
// ones.
func (s *Store) ChatShellRuns(ctx context.Context, sessionID string) ([]protocol.ChatShellRunRecord, error) {
rows, err := s.db.QueryContext(ctx, chatShellRunSelect+` WHERE session_id = ? ORDER BY started_at, id`, sessionID)
rows, err := s.db.QueryContext(ctx, chatShellRunSelect+` WHERE session_id = ? AND dismissed_at = 0 ORDER BY started_at, id`, sessionID)
if err != nil {
return nil, err
}
@@ -100,9 +101,9 @@ func (s *Store) ChatShellRuns(ctx context.Context, sessionID string) ([]protocol
}
// ChatShellRun returns one run of sessionID; found is false when that session
// has no run with this id.
// has no undismissed run with this id.
func (s *Store) ChatShellRun(ctx context.Context, sessionID, id string) (protocol.ChatShellRunRecord, bool, error) {
run, err := scanChatShellRun(s.db.QueryRowContext(ctx, chatShellRunSelect+` WHERE session_id = ? AND id = ?`, sessionID, id))
run, err := scanChatShellRun(s.db.QueryRowContext(ctx, chatShellRunSelect+` WHERE session_id = ? AND id = ? AND dismissed_at = 0`, sessionID, id))
if errors.Is(err, sql.ErrNoRows) {
return protocol.ChatShellRunRecord{}, false, nil
}