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:
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package protocol
|
||||
|
||||
// Dismissing operator shell commands. Dismissal is per chat, for every client:
|
||||
// a dismissed run leaves every chat_shell_runs readback and every watcher
|
||||
// receives a chat_shell_run_event with Dismissed set. A running command cannot
|
||||
// be dismissed. For ChatShellRunUndoWindow after a dismissal the run can be
|
||||
// restored (chat_shell_restore); after that the server purges its command,
|
||||
// output and error, keeping only a tombstone whose id chat_shell_runs reports
|
||||
// in ChatShellDismissed so a client that missed the event drops the card.
|
||||
const (
|
||||
// ChatShellDismiss dismisses the finished runs named in
|
||||
// ChatShellRunRequest.RunIDs; the reply lists those it dismissed in
|
||||
// ChatShellDismissed (running, unknown and already dismissed runs are
|
||||
// skipped).
|
||||
ChatShellDismiss = "chat_shell_dismiss"
|
||||
// ChatShellRestore undoes a dismissal still inside its undo window; the
|
||||
// reply carries the restored runs (with their list output tail) in
|
||||
// ChatShellRuns, and every watcher receives their lifecycle frames.
|
||||
ChatShellRestore = "chat_shell_restore"
|
||||
// ChatShellRunUndoWindow is how long, in milliseconds, a dismissed run is
|
||||
// kept restorable before its output is purged. It is longer than the
|
||||
// clients' Undo notice so a late Undo still succeeds.
|
||||
ChatShellRunUndoWindow = 30_000
|
||||
// ChatShellDismissLimit bounds the run ids of one request.
|
||||
ChatShellDismissLimit = 200
|
||||
)
|
||||
@@ -76,6 +76,8 @@ type ChatShellRunRequest struct {
|
||||
Rows int `json:"rows,omitempty"`
|
||||
Data string `json:"data,omitempty"`
|
||||
After int64 `json:"after,omitempty"`
|
||||
// RunIDs names the runs of chat_shell_dismiss and chat_shell_restore.
|
||||
RunIDs []string `json:"run_ids,omitempty"`
|
||||
}
|
||||
|
||||
// ChatShellRunRecord is one operator command and what it produced. Output is
|
||||
@@ -116,7 +118,7 @@ type ChatShellRunRecord struct {
|
||||
|
||||
// ChatShellRunEventFrame is one live change to a run. A lifecycle frame sets
|
||||
// Run (without output); an output frame sets Output covering absolute bytes
|
||||
// [Offset, End). A client whose own copy ends before Offset missed output and
|
||||
// [Offset, End); a dismissal frame sets Dismissed (the card goes away). A client whose own copy ends before Offset missed output and
|
||||
// shows that gap rather than joining unrelated text.
|
||||
type ChatShellRunEventFrame struct {
|
||||
SessionID string `json:"session_id"`
|
||||
@@ -125,4 +127,5 @@ type ChatShellRunEventFrame struct {
|
||||
Output string `json:"output,omitempty"`
|
||||
Offset int64 `json:"offset,omitempty"`
|
||||
End int64 `json:"end,omitempty"`
|
||||
Dismissed bool `json:"dismissed,omitempty"`
|
||||
}
|
||||
|
||||
@@ -280,6 +280,9 @@ type Message struct {
|
||||
ChatShellRun *ChatShellRunRecord `json:"chat_shell_run,omitempty"`
|
||||
ChatShellRuns []ChatShellRunRecord `json:"chat_shell_runs,omitempty"`
|
||||
ChatShellRunEvent *ChatShellRunEventFrame `json:"chat_shell_run_event,omitempty"`
|
||||
// ChatShellDismissed lists dismissed run ids (chat_shell_dismiss's reply
|
||||
// and chat_shell_runs' tombstones; chat_shell_dismiss.go).
|
||||
ChatShellDismissed []string `json:"chat_shell_dismissed,omitempty"`
|
||||
// ListDirectory is the list_directory body and DirectoryListing its reply:
|
||||
// the home-restricted folder picker listing (see directories.go).
|
||||
ListDirectory *DirectoryListRequest `json:"list_directory,omitempty"`
|
||||
|
||||
@@ -259,7 +259,7 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker
|
||||
handleChatShellOutput(ctx, request, broker, registry, write)
|
||||
return true
|
||||
case protocol.ChatShellRun, protocol.ChatShellStop, protocol.ChatShellRuns, protocol.ChatShellRunOutput, protocol.ChatShellShare,
|
||||
protocol.ChatShellInput, protocol.ChatShellResize:
|
||||
protocol.ChatShellInput, protocol.ChatShellResize, protocol.ChatShellDismiss, protocol.ChatShellRestore:
|
||||
handleChatShellRunRequest(ctx, request, broker, hub, registry, clientID, write)
|
||||
return true
|
||||
case protocol.ChatAttachmentImage:
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// chatShellDismissStore persists dismissals; *pipeline.Store implements it.
|
||||
type chatShellDismissStore interface {
|
||||
DismissChatShellRuns(ctx context.Context, sessionID string, ids []string, at int64) ([]string, error)
|
||||
RestoreChatShellRuns(ctx context.Context, sessionID string, ids []string, since int64) ([]protocol.ChatShellRunRecord, error)
|
||||
PurgeDismissedChatShellRuns(ctx context.Context, before int64) (int64, error)
|
||||
DismissedChatShellRuns(ctx context.Context, sessionID string) ([]string, error)
|
||||
}
|
||||
|
||||
func validChatShellRunIDs(ids []string) error {
|
||||
if len(ids) == 0 {
|
||||
return errors.New("name at least one command")
|
||||
}
|
||||
if len(ids) > protocol.ChatShellDismissLimit {
|
||||
return fmt.Errorf("name at most %d commands", protocol.ChatShellDismissLimit)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Dismiss removes the session's finished runs among ids for every watcher and
|
||||
// returns the ids it dismissed. Their output is purged once the undo window
|
||||
// has passed.
|
||||
func (r *chatShellRunner) Dismiss(ctx context.Context, sessionID string, ids []string) ([]string, error) {
|
||||
if err := validChatShellRunIDs(ids); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
finished := make([]string, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
// The store refuses a run recorded as running; a process that just
|
||||
// exited may not have saved its final record yet, so ask it too.
|
||||
if r.activeProcess(sessionID, id) == nil {
|
||||
finished = append(finished, id)
|
||||
}
|
||||
}
|
||||
if len(finished) == 0 {
|
||||
return nil, errors.New("stop the command and wait for it to finish before dismissing it")
|
||||
}
|
||||
dismissed, err := r.store.DismissChatShellRuns(ctx, sessionID, finished, r.now().UnixMilli())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, id := range dismissed {
|
||||
r.publishFrame(protocol.ChatShellRunEventFrame{SessionID: sessionID, RunID: id, Dismissed: true})
|
||||
}
|
||||
if len(dismissed) > 0 {
|
||||
r.schedulePurge()
|
||||
}
|
||||
return dismissed, nil
|
||||
}
|
||||
|
||||
// Restore undoes the dismissal of the session's runs among ids that are still
|
||||
// inside the undo window and returns them with their list output tail.
|
||||
func (r *chatShellRunner) Restore(ctx context.Context, sessionID string, ids []string) ([]protocol.ChatShellRunRecord, error) {
|
||||
if err := validChatShellRunIDs(ids); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
since := r.now().Add(-r.undo).UnixMilli()
|
||||
runs, err := r.store.RestoreChatShellRuns(ctx, sessionID, ids, since)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(runs) == 0 {
|
||||
return nil, errors.New("these commands can no longer be restored")
|
||||
}
|
||||
for i, run := range runs {
|
||||
r.publishRun(run)
|
||||
runs[i] = chatShellRecordTail(run, protocol.ChatShellRunListOutput)
|
||||
runs[i].Text = ""
|
||||
}
|
||||
return runs, nil
|
||||
}
|
||||
|
||||
// Dismissed is the session's dismissed run ids (their tombstones).
|
||||
func (r *chatShellRunner) Dismissed(ctx context.Context, sessionID string) ([]string, error) {
|
||||
return r.store.DismissedChatShellRuns(ctx, sessionID)
|
||||
}
|
||||
|
||||
// purgeDismissed deletes the output of runs whose undo window has passed.
|
||||
func (r *chatShellRunner) purgeDismissed(ctx context.Context) {
|
||||
_, _ = r.store.PurgeDismissedChatShellRuns(ctx, r.now().Add(-r.undo).UnixMilli())
|
||||
}
|
||||
|
||||
// schedulePurge purges once the window of a dismissal made now has passed.
|
||||
// A server stopping before then purges on its next start (recover).
|
||||
func (r *chatShellRunner) schedulePurge() {
|
||||
time.AfterFunc(r.undo+time.Second, func() {
|
||||
if r.base.Err() == nil {
|
||||
r.purgeDismissed(r.base)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (r *chatShellRunner) publishFrame(frame protocol.ChatShellRunEventFrame) {
|
||||
if r.publish != nil {
|
||||
r.publish(frame.SessionID, frame)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
func dismissRequest(kind, id string, runIDs ...string) protocol.Message {
|
||||
return protocol.Message{Type: kind, ID: id, ChatShellRunRequest: &protocol.ChatShellRunRequest{SessionID: "ses_1", RunIDs: runIDs}}
|
||||
}
|
||||
|
||||
func TestChatShellDismissRemovesForEveryWatcherAndRestores(t *testing.T) {
|
||||
ctx, conns, _ := shellChat(t)
|
||||
done := runShell(t, ctx, conns[0], "r1", "echo kept-output")
|
||||
for _, conn := range conns {
|
||||
finishedShell(t, ctx, conn, done.ID)
|
||||
}
|
||||
live := runShell(t, ctx, conns[0], "r2", "sleep 30")
|
||||
|
||||
// A running command alone is refused; with a finished one it is skipped.
|
||||
writeChat(t, ctx, conns[0], dismissRequest(protocol.ChatShellDismiss, "running", live.ID))
|
||||
if reply := readShellFrame(t, ctx, conns[0], func(m protocol.Message) bool { return m.ID == "running" }); reply.Type != protocol.Error {
|
||||
t.Fatalf("dismissing a running command = %+v", reply)
|
||||
}
|
||||
writeChat(t, ctx, conns[0], dismissRequest(protocol.ChatShellDismiss, "empty"))
|
||||
if reply := readShellFrame(t, ctx, conns[0], func(m protocol.Message) bool { return m.ID == "empty" }); reply.Type != protocol.Error {
|
||||
t.Fatalf("dismissing nothing = %+v", reply)
|
||||
}
|
||||
writeChat(t, ctx, conns[0], dismissRequest(protocol.ChatShellDismiss, "d", done.ID, live.ID))
|
||||
reply := readShellFrame(t, ctx, conns[0], func(m protocol.Message) bool { return m.ID == "d" })
|
||||
if reply.Type != protocol.ChatShellDismiss || fmt.Sprint(reply.ChatShellDismissed) != fmt.Sprint([]string{done.ID}) {
|
||||
t.Fatalf("dismiss reply = %+v", reply)
|
||||
}
|
||||
isDismissal := func(m protocol.Message) bool {
|
||||
return m.Type == protocol.ChatShellRunEvent && m.ChatShellRunEvent != nil && m.ChatShellRunEvent.Dismissed
|
||||
}
|
||||
for i, conn := range conns {
|
||||
if frame := readShellFrame(t, ctx, conn, isDismissal).ChatShellRunEvent; frame.RunID != done.ID || frame.SessionID != "ses_1" {
|
||||
t.Fatalf("dismissal frame %d = %+v", i, frame)
|
||||
}
|
||||
}
|
||||
|
||||
// The readback leaves it out and reports its tombstone; its output is gone.
|
||||
writeChat(t, ctx, conns[1], protocol.Message{Type: protocol.ChatShellRuns, ID: "list", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}})
|
||||
list := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "list" })
|
||||
if len(list.ChatShellRuns) != 1 || list.ChatShellRuns[0].ID != live.ID || fmt.Sprint(list.ChatShellDismissed) != fmt.Sprint([]string{done.ID}) {
|
||||
t.Fatalf("readback = %+v", list)
|
||||
}
|
||||
writeChat(t, ctx, conns[1], shellRequest(protocol.ChatShellRunOutput, "out", done.ID, ""))
|
||||
if got := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "out" }); got.Type != protocol.Error {
|
||||
t.Fatalf("output of a dismissed run = %+v", got)
|
||||
}
|
||||
|
||||
// Undo brings the run back, output included, for every watcher.
|
||||
writeChat(t, ctx, conns[1], dismissRequest(protocol.ChatShellRestore, "undo", done.ID))
|
||||
restored := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "undo" })
|
||||
if restored.Type != protocol.ChatShellRestore || len(restored.ChatShellRuns) != 1 || restored.ChatShellRuns[0].Output != "kept-output\r\n" {
|
||||
t.Fatalf("restore reply = %+v", restored)
|
||||
}
|
||||
for i, conn := range conns {
|
||||
frame := readShellFrame(t, ctx, conn, func(m protocol.Message) bool {
|
||||
return m.Type == protocol.ChatShellRunEvent && m.ChatShellRunEvent != nil && m.ChatShellRunEvent.RunID == done.ID && m.ChatShellRunEvent.Run != nil
|
||||
}).ChatShellRunEvent
|
||||
if frame.Run.Status != protocol.ChatShellRunExited {
|
||||
t.Fatalf("restore frame %d = %+v", i, frame)
|
||||
}
|
||||
}
|
||||
writeChat(t, ctx, conns[0], dismissRequest(protocol.ChatShellRestore, "undo-again", done.ID))
|
||||
if got := readShellFrame(t, ctx, conns[0], func(m protocol.Message) bool { return m.ID == "undo-again" }); got.Type != protocol.Error {
|
||||
t.Fatalf("restoring an undismissed run = %+v", got)
|
||||
}
|
||||
writeChat(t, ctx, conns[0], shellRequest(protocol.ChatShellStop, "stop", live.ID, ""))
|
||||
finishedShell(t, ctx, conns[0], live.ID)
|
||||
}
|
||||
|
||||
func TestChatShellDismissPurgesAfterTheUndoWindow(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
runner, frames := testShellRunner(t, ctx)
|
||||
now := time.UnixMilli(1_000_000)
|
||||
runner.now = func() time.Time { return now }
|
||||
run, err := runner.Start(ctx, "ses_1", t.TempDir(), "echo gone", 0, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runner.Wait()
|
||||
if frames.final(run.ID) == nil {
|
||||
t.Fatal("the run never finished")
|
||||
}
|
||||
if _, err := runner.Dismiss(ctx, "ses_1", []string{run.ID}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now = now.Add(runner.undo - time.Millisecond)
|
||||
runner.purgeDismissed(ctx)
|
||||
if restored, err := runner.Restore(ctx, "ses_1", []string{run.ID}); err != nil || len(restored) != 1 || restored[0].Output == "" {
|
||||
t.Fatalf("restore inside the window = %+v, %v", restored, err)
|
||||
}
|
||||
if _, err := runner.Dismiss(ctx, "ses_1", []string{run.ID}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now = now.Add(runner.undo + time.Millisecond)
|
||||
// A restart purges what expired while no server ran.
|
||||
runner.recover(ctx)
|
||||
if _, err := runner.Restore(ctx, "ses_1", []string{run.ID}); err == nil {
|
||||
t.Fatal("a run was restored after the undo window")
|
||||
}
|
||||
if tombs, err := runner.Dismissed(ctx, "ses_1"); err != nil || fmt.Sprint(tombs) != fmt.Sprint([]string{run.ID}) {
|
||||
t.Fatalf("tombstones = %v, %v", tombs, err)
|
||||
}
|
||||
}
|
||||
@@ -70,7 +70,26 @@ func handleChatShellRunRequest(ctx context.Context, request protocol.Message, br
|
||||
fail(err)
|
||||
return
|
||||
}
|
||||
_ = write(protocol.Message{Type: protocol.ChatShellRuns, ID: request.ID, ChatShellRuns: runs})
|
||||
dismissed, err := runner.Dismissed(ctx, sessionID)
|
||||
if err != nil {
|
||||
fail(err)
|
||||
return
|
||||
}
|
||||
_ = write(protocol.Message{Type: protocol.ChatShellRuns, ID: request.ID, ChatShellRuns: runs, ChatShellDismissed: dismissed})
|
||||
case protocol.ChatShellDismiss:
|
||||
dismissed, err := runner.Dismiss(ctx, sessionID, body.RunIDs)
|
||||
if err != nil {
|
||||
fail(err)
|
||||
return
|
||||
}
|
||||
_ = write(protocol.Message{Type: protocol.ChatShellDismiss, ID: request.ID, ChatShellDismissed: dismissed})
|
||||
case protocol.ChatShellRestore:
|
||||
runs, err := runner.Restore(ctx, sessionID, body.RunIDs)
|
||||
if err != nil {
|
||||
fail(err)
|
||||
return
|
||||
}
|
||||
_ = write(protocol.Message{Type: protocol.ChatShellRestore, ID: request.ID, ChatShellRuns: runs})
|
||||
case protocol.ChatShellInput:
|
||||
if err := runner.Input(sessionID, body.RunID, body.Data); err != nil {
|
||||
fail(err)
|
||||
|
||||
@@ -23,6 +23,7 @@ type chatShellRunStore interface {
|
||||
ChatShellRuns(ctx context.Context, sessionID string) ([]protocol.ChatShellRunRecord, error)
|
||||
ChatShellRun(ctx context.Context, sessionID, id string) (protocol.ChatShellRunRecord, bool, error)
|
||||
InterruptChatShellRuns(ctx context.Context, endedAt int64, reason string) (int64, error)
|
||||
chatShellDismissStore
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -48,6 +49,8 @@ type chatShellRunner struct {
|
||||
grace time.Duration
|
||||
flush time.Duration
|
||||
persist time.Duration
|
||||
// undo is how long a dismissed run stays restorable before its purge.
|
||||
undo time.Duration
|
||||
|
||||
mu sync.Mutex
|
||||
active map[string]*chatShellProcess
|
||||
@@ -74,6 +77,7 @@ func newChatShellRunner(base context.Context, store chatShellRunStore, publish f
|
||||
return &chatShellRunner{
|
||||
base: base, store: store, publish: publish, now: time.Now, shell: chatShellProgram,
|
||||
grace: 3 * time.Second, flush: 100 * time.Millisecond, persist: 2 * time.Second,
|
||||
undo: protocol.ChatShellRunUndoWindow * time.Millisecond,
|
||||
active: map[string]*chatShellProcess{},
|
||||
}
|
||||
}
|
||||
@@ -89,12 +93,14 @@ func chatShellProgram() string {
|
||||
}
|
||||
|
||||
// recover marks runs a previous server process left running as interrupted:
|
||||
// their processes died with it (or are no longer owned by anyone).
|
||||
// their processes died with it (or are no longer owned by anyone). It also
|
||||
// purges dismissals whose undo window passed while no server was running.
|
||||
func (r *chatShellRunner) recover(ctx context.Context) {
|
||||
if r.store == nil {
|
||||
return
|
||||
}
|
||||
_, _ = r.store.InterruptChatShellRuns(ctx, r.now().UnixMilli(), chatShellInterruptedReason)
|
||||
r.purgeDismissed(ctx)
|
||||
}
|
||||
|
||||
// resolveChatShellDirectory requires the chat's recorded directory to still be
|
||||
|
||||
Reference in New Issue
Block a user