diff --git a/internal/pipeline/chat_shell_dismiss.go b/internal/pipeline/chat_shell_dismiss.go index d329320d..ff265a46 100644 --- a/internal/pipeline/chat_shell_dismiss.go +++ b/internal/pipeline/chat_shell_dismiss.go @@ -8,10 +8,10 @@ import ( ) // 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. +// 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"}, @@ -48,9 +48,9 @@ WHERE session_id = ? AND id = ? AND status != ? AND dismissed_at = 0`, at, sessi } // 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) { +// 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 @@ -59,7 +59,7 @@ func (s *Store) RestoreChatShellRuns(ctx context.Context, sessionID string, ids 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) +WHERE session_id = ? AND id = ? AND dismissed_at > 0 AND purged = 0`, sessionID, id) if err != nil { return nil, err } @@ -74,8 +74,19 @@ WHERE session_id = ? AND id = ? AND dismissed_at > 0 AND dismissed_at >= ? AND p for _, id := range restored { args = append(args, id) } - rows, err := s.db.QueryContext(ctx, chatShellRunSelect+` WHERE session_id = ? AND id IN (?`+ + 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 } @@ -90,34 +101,3 @@ WHERE session_id = ? AND id = ? AND dismissed_at > 0 AND dismissed_at >= ? AND p } 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() -} diff --git a/internal/pipeline/chat_shell_dismiss_test.go b/internal/pipeline/chat_shell_dismiss_test.go index 009ca864..9160ad70 100644 --- a/internal/pipeline/chat_shell_dismiss_test.go +++ b/internal/pipeline/chat_shell_dismiss_test.go @@ -9,7 +9,19 @@ import ( "git.a-wan.net/alan/pipelines/internal/protocol" ) -func TestChatShellRunsDismissRestoreAndPurge(t *testing.T) { +func chatShellIDs(t *testing.T, runs []protocol.ChatShellRunRecord, err error) string { + t.Helper() + if err != nil { + t.Fatal(err) + } + var out []string + for _, run := range runs { + out = append(out, run.ID) + } + return fmt.Sprint(out) +} + +func TestChatShellRunsDismissKeepsOutputAndRestores(t *testing.T) { ctx := context.Background() store, err := Open(filepath.Join(t.TempDir(), "shell.db")) if err != nil { @@ -18,8 +30,10 @@ func TestChatShellRunsDismissRestoreAndPurge(t *testing.T) { defer store.Close() save := func(id, session, status string, started int64) { t.Helper() + code := 3 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"} + ExitCode: &code, StartedAt: started, EndedAt: started + 5, DurationMS: 5, Output: "out " + id, OutputBytes: 6, + Text: "out " + id, Error: "err"} if err := store.SaveChatShellRun(ctx, run, 0); err != nil { t.Fatal(err) } @@ -28,17 +42,10 @@ func TestChatShellRunsDismissRestoreAndPurge(t *testing.T) { 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 { + listed := 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) + return chatShellIDs(t, runs, err) } // Running, foreign and unknown runs are skipped. @@ -46,57 +53,93 @@ func TestChatShellRunsDismissRestoreAndPurge(t *testing.T) { 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]" { + if got := listed("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) + + // The dismissed run keeps its command, status, times and output. + held, err := store.DismissedChatShellRuns(ctx, "ses_1") + if err != nil || len(held) != 1 { + t.Fatalf("dismissed runs = %+v, %v", held, err) + } + if run := held[0]; run.ID != "run_a" || !run.Dismissed || run.DismissedAt != 1000 || run.Purged || + run.Command != "echo run_a" || run.Output != "out run_a" || run.Text != "out run_a" || + run.ExitCode == nil || *run.ExitCode != 3 || run.DurationMS != 5 || run.EndedAt != 6 { + t.Fatalf("dismissed run = %+v", run) + } + if run, found, err := store.ChatShellRun(ctx, "ses_1", "run_a"); err != nil || !found || !run.Dismissed || run.Output != "out run_a" { + t.Fatalf("reading a dismissed run = %+v, %v, %v", run, found, err) } - // 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" { + // Restore has no time limit and brings the whole record back. + restored, err := store.RestoreChatShellRuns(ctx, "ses_1", []string{"run_a", "run_b"}) + if err != nil || len(restored) != 1 || restored[0].ID != "run_a" || restored[0].Dismissed || restored[0].Output != "out run_a" { t.Fatalf("restored = %+v, %v", restored, err) } - if got := ids("ses_1"); got != "[run_a run_b run_live]" { + if got := listed("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 runs, err := store.DismissedChatShellRuns(ctx, "ses_1"); chatShellIDs(t, runs, err) != "[]" { + t.Fatalf("dismissed after restore = %+v", runs) } - 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]" { + if got := listed("ses_2"); got != "[run_other]" { t.Fatalf("another session changed: %s", got) } } + +func TestChatShellRunsListOlderPurgedTombstones(t *testing.T) { + ctx := context.Background() + store, err := Open(filepath.Join(t.TempDir(), "shell.db")) + if err != nil { + t.Fatal(err) + } + defer store.Close() + run := protocol.ChatShellRunRecord{ID: "run_old", SessionID: "ses_1", Directory: "/w", + Status: protocol.ChatShellRunExited, StartedAt: 10, EndedAt: 20, DurationMS: 10} + if err := store.SaveChatShellRun(ctx, run, 0); err != nil { + t.Fatal(err) + } + // An older server purged this dismissed row after its undo window. + if _, err := store.db.ExecContext(ctx, `UPDATE chat_shell_runs SET dismissed_at = 15, purged = 1 WHERE id = 'run_old'`); err != nil { + t.Fatal(err) + } + held, err := store.DismissedChatShellRuns(ctx, "ses_1") + if err != nil || len(held) != 1 || !held[0].Purged || !held[0].Dismissed || held[0].Command != "" || held[0].Output != "" { + t.Fatalf("tombstone = %+v, %v", held, err) + } + if restored, err := store.RestoreChatShellRuns(ctx, "ses_1", []string{"run_old"}); err != nil || len(restored) != 0 { + t.Fatalf("a purged run was restored: %+v, %v", restored, err) + } +} + +func TestChatShellRunsDismissedCountTowardHistory(t *testing.T) { + ctx := context.Background() + store, err := Open(filepath.Join(t.TempDir(), "shell.db")) + if err != nil { + t.Fatal(err) + } + defer store.Close() + for i := range 4 { + run := protocol.ChatShellRunRecord{ID: fmt.Sprintf("run_%d", i), SessionID: "ses_1", Command: "true", Directory: "/w", + Status: protocol.ChatShellRunExited, StartedAt: int64(i + 1)} + if err := store.SaveChatShellRun(ctx, run, 3); err != nil { + t.Fatal(err) + } + if i == 1 { + if _, err := store.DismissChatShellRuns(ctx, "ses_1", []string{"run_0", "run_1"}, 100); err != nil { + t.Fatal(err) + } + } + } + dismissed, err := store.DismissedChatShellRuns(ctx, "ses_1") + if got := chatShellIDs(t, dismissed, err); got != "[run_1]" { + t.Fatalf("dismissed kept = %s", got) + } + kept, err := store.ChatShellRuns(ctx, "ses_1") + if got := chatShellIDs(t, kept, err); got != "[run_2 run_3]" { + t.Fatalf("runs kept = %s", got) + } +} diff --git a/internal/pipeline/chat_shell_runs.go b/internal/pipeline/chat_shell_runs.go index cdabddc0..0de80c53 100644 --- a/internal/pipeline/chat_shell_runs.go +++ b/internal/pipeline/chat_shell_runs.go @@ -84,26 +84,13 @@ cols = excluded.cols, rows = excluded.rows, output_text = excluded.output_text`, // 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 = ? AND dismissed_at = 0 ORDER BY started_at, id`, sessionID) - 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() + return s.queryChatShellRuns(ctx, ` WHERE session_id = ? AND dismissed_at = 0 ORDER BY started_at, id`, sessionID) } -// ChatShellRun returns one run of sessionID; found is false when that session -// has no undismissed run with this id. +// ChatShellRun returns one run of sessionID, dismissed or not (a dismissed +// one is flagged); found is false when that session has no 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 = ? AND dismissed_at = 0`, sessionID, id)) + run, err := scanChatShellRun(s.db.QueryRowContext(ctx, chatShellRunSelect+` WHERE session_id = ? AND id = ?`, sessionID, id)) if errors.Is(err, sql.ErrNoRows) { return protocol.ChatShellRunRecord{}, false, nil } @@ -123,14 +110,14 @@ duration_ms = CASE WHEN ? > started_at THEN ? - started_at ELSE 0 END WHERE stat return result.RowsAffected() } -const chatShellRunSelect = `SELECT id, session_id, command, directory, status, exit_code, signal, error, started_at, ended_at, duration_ms, output, output_start, output_bytes, terminal, cols, rows, output_text FROM chat_shell_runs` +const chatShellRunSelect = `SELECT id, session_id, command, directory, status, exit_code, signal, error, started_at, ended_at, duration_ms, output, output_start, output_bytes, terminal, cols, rows, output_text, dismissed_at, purged FROM chat_shell_runs` func scanChatShellRun(row chatQueueScanner) (protocol.ChatShellRunRecord, error) { var run protocol.ChatShellRunRecord var exit sql.NullInt64 err := row.Scan(&run.ID, &run.SessionID, &run.Command, &run.Directory, &run.Status, &exit, &run.Signal, &run.Error, &run.StartedAt, &run.EndedAt, &run.DurationMS, &run.Output, &run.OutputStart, &run.OutputBytes, - &run.Terminal, &run.Cols, &run.Rows, &run.Text) + &run.Terminal, &run.Cols, &run.Rows, &run.Text, &run.DismissedAt, &run.Purged) if err != nil { return protocol.ChatShellRunRecord{}, err } @@ -139,5 +126,6 @@ func scanChatShellRun(row chatQueueScanner) (protocol.ChatShellRunRecord, error) run.ExitCode = &code } run.Truncated = run.OutputStart > 0 + run.Dismissed = run.DismissedAt > 0 return run, nil } diff --git a/internal/protocol/chat_shell_dismiss.go b/internal/protocol/chat_shell_dismiss.go index da3db3eb..042cb181 100644 --- a/internal/protocol/chat_shell_dismiss.go +++ b/internal/protocol/chat_shell_dismiss.go @@ -1,26 +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. +// a dismissed run leaves the transcript (chat_shell_runs' ChatShellRuns) and +// every watcher receives a chat_shell_run_event with Dismissed set. A running +// command cannot be dismissed. A dismissed run is kept, command, status, times +// and output included, within the same history and output bounds as any other +// run: chat_shell_runs lists it in ChatShellDismissedRuns (flagged Dismissed) +// so clients can show it collapsed, chat_shell_run_output still reads it, and +// chat_shell_restore puts it back at any time. Runs dismissed by an older +// server had their command and output purged; they are listed with Purged set +// and cannot be restored. 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 undoes a dismissal; the reply carries the restored runs + // (with their list output tail) in ChatShellRuns, and every watcher + // receives their lifecycle frames. There is no time limit; a purged run + // cannot be restored. 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 ) diff --git a/internal/protocol/chat_shell_run.go b/internal/protocol/chat_shell_run.go index be1d9d35..90ae515d 100644 --- a/internal/protocol/chat_shell_run.go +++ b/internal/protocol/chat_shell_run.go @@ -114,6 +114,12 @@ type ChatShellRunRecord struct { // carriage-return redraws applied): what Copy output copies and Send to // agent shares. Only chat_shell_run_output carries it. Text string `json:"text,omitempty"` + // Dismissed is set on a run the operator dismissed (chat_shell_dismiss.go) + // and DismissedAt is when, in epoch milliseconds. Purged is set when an + // older server deleted its command and output: only the tombstone is left. + Dismissed bool `json:"dismissed,omitempty"` + DismissedAt int64 `json:"dismissed_at,omitempty"` + Purged bool `json:"purged,omitempty"` } // ChatShellRunEventFrame is one live change to a run. A lifecycle frame sets diff --git a/internal/protocol/message.go b/internal/protocol/message.go index 2394c6c2..a7f28b65 100644 --- a/internal/protocol/message.go +++ b/internal/protocol/message.go @@ -281,8 +281,11 @@ type Message struct { 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"` + // and chat_shell_runs' dismissed ids; chat_shell_dismiss.go), and + // ChatShellDismissedRuns carries chat_shell_runs' dismissed runs, oldest + // first, with their list output tail. + ChatShellDismissed []string `json:"chat_shell_dismissed,omitempty"` + ChatShellDismissedRuns []ChatShellRunRecord `json:"chat_shell_dismissed_runs,omitempty"` // Machine terminals (terminal.go): the request body, one terminal, the // machine's terminals and the pushed change. TerminalRequest *TerminalRequest `json:"terminal_request,omitempty"` diff --git a/internal/server/chat_shell_dismiss.go b/internal/server/chat_shell_dismiss.go index b98aea94..052fe4e8 100644 --- a/internal/server/chat_shell_dismiss.go +++ b/internal/server/chat_shell_dismiss.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "time" "git.a-wan.net/alan/pipelines/internal/protocol" ) @@ -12,9 +11,8 @@ import ( // 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) + RestoreChatShellRuns(ctx context.Context, sessionID string, ids []string) ([]protocol.ChatShellRunRecord, error) + DismissedChatShellRuns(ctx context.Context, sessionID string) ([]protocol.ChatShellRunRecord, error) } func validChatShellRunIDs(ids []string) error { @@ -27,9 +25,9 @@ func validChatShellRunIDs(ids []string) error { 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. +// Dismiss takes the session's finished runs among ids out of the transcript +// for every watcher and returns the ids it dismissed. Their record and output +// are kept: the readback lists them as dismissed and Restore brings them back. func (r *chatShellRunner) Dismiss(ctx context.Context, sessionID string, ids []string) ([]string, error) { if err := validChatShellRunIDs(ids); err != nil { return nil, err @@ -52,20 +50,17 @@ func (r *chatShellRunner) Dismiss(ctx context.Context, sessionID string, ids []s 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. +// Restore undoes the dismissal of the session's runs among ids, however long +// ago they were dismissed, and returns them with their list output tail. A +// purged run (an older server's tombstone) cannot be restored. 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) + runs, err := r.store.RestoreChatShellRuns(ctx, sessionID, ids) if err != nil { return nil, err } @@ -80,24 +75,20 @@ func (r *chatShellRunner) Restore(ctx context.Context, sessionID string, ids []s 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) - } - }) +// Dismissed is the session's dismissed runs, oldest first, each with at most +// protocol.ChatShellRunListOutput bytes of its output tail, and their ids. +func (r *chatShellRunner) Dismissed(ctx context.Context, sessionID string) ([]protocol.ChatShellRunRecord, []string, error) { + runs, err := r.store.DismissedChatShellRuns(ctx, sessionID) + if err != nil { + return nil, nil, err + } + ids := make([]string, len(runs)) + for i, run := range runs { + runs[i] = chatShellRecordTail(run, protocol.ChatShellRunListOutput) + runs[i].Text = "" + ids[i] = run.ID + } + return runs, ids, nil } func (r *chatShellRunner) publishFrame(frame protocol.ChatShellRunEventFrame) { diff --git a/internal/server/chat_shell_dismiss_test.go b/internal/server/chat_shell_dismiss_test.go index 28acfd4d..6a3d1b16 100644 --- a/internal/server/chat_shell_dismiss_test.go +++ b/internal/server/chat_shell_dismiss_test.go @@ -13,9 +13,9 @@ 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) { +func TestChatShellDismissKeepsTheRunForEveryWatcherAndRestores(t *testing.T) { ctx, conns, _ := shellChat(t) - done := runShell(t, ctx, conns[0], "r1", "echo kept-output") + done := runShell(t, ctx, conns[0], "r1", "echo kept-output; exit 2") for _, conn := range conns { finishedShell(t, ctx, conn, done.ID) } @@ -44,21 +44,32 @@ func TestChatShellDismissRemovesForEveryWatcherAndRestores(t *testing.T) { } } - // The readback leaves it out and reports its tombstone; its output is gone. + // The readback lists it apart, flagged, with its command, status and output. 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) } + if len(list.ChatShellDismissedRuns) != 1 { + t.Fatalf("dismissed runs = %+v", list.ChatShellDismissedRuns) + } + if run := list.ChatShellDismissedRuns[0]; run.ID != done.ID || !run.Dismissed || run.DismissedAt == 0 || run.Purged || + run.Command != "echo kept-output; exit 2" || run.ExitCode == nil || *run.ExitCode != 2 || + run.Output != "kept-output\r\n" || run.Text != "" { + t.Fatalf("dismissed run = %+v", run) + } + // Its whole output stays readable (the history's output view, Copy output). 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 { + if got := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "out" }); got.Type != protocol.ChatShellRunOutput || + got.ChatShellRun == nil || !got.ChatShellRun.Dismissed || got.ChatShellRun.Text != "kept-output\n" { t.Fatalf("output of a dismissed run = %+v", got) } - // Undo brings the run back, output included, for every watcher. + // Restore 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" { + if restored.Type != protocol.ChatShellRestore || len(restored.ChatShellRuns) != 1 || restored.ChatShellRuns[0].Output != "kept-output\r\n" || + restored.ChatShellRuns[0].Dismissed { t.Fatalf("restore reply = %+v", restored) } for i, conn := range conns { @@ -77,13 +88,13 @@ func TestChatShellDismissRemovesForEveryWatcherAndRestores(t *testing.T) { finishedShell(t, ctx, conns[0], live.ID) } -func TestChatShellDismissPurgesAfterTheUndoWindow(t *testing.T) { +func TestChatShellDismissedRunIsKeptAcrossRestartsAndRestoresLater(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) + run, err := runner.Start(ctx, "ses_1", t.TempDir(), "echo kept", 0, 0) if err != nil { t.Fatal(err) } @@ -94,21 +105,21 @@ func TestChatShellDismissPurgesAfterTheUndoWindow(t *testing.T) { 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. + // A day later, after a restart, the run is still whole and restorable. + now = now.Add(24 * time.Hour) 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") + held, ids, err := runner.Dismissed(ctx, "ses_1") + if err != nil || len(held) != 1 || fmt.Sprint(ids) != fmt.Sprint([]string{run.ID}) || held[0].Command != "echo kept" || + held[0].Output == "" || held[0].Purged { + t.Fatalf("dismissed = %+v, %v, %v", held, ids, err) } - 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) + if read, err := runner.Read(ctx, "ses_1", run.ID); err != nil || read.Text != "kept\n" || !read.Dismissed { + t.Fatalf("read = %+v, %v", read, err) + } + if restored, err := runner.Restore(ctx, "ses_1", []string{run.ID}); err != nil || len(restored) != 1 || restored[0].Output == "" { + t.Fatalf("restore a day later = %+v, %v", restored, err) + } + if held, _, _ := runner.Dismissed(ctx, "ses_1"); len(held) != 0 { + t.Fatalf("still dismissed: %+v", held) } } diff --git a/internal/server/chat_shell_run_handlers.go b/internal/server/chat_shell_run_handlers.go index b6098cbd..6abab624 100644 --- a/internal/server/chat_shell_run_handlers.go +++ b/internal/server/chat_shell_run_handlers.go @@ -70,12 +70,13 @@ func handleChatShellRunRequest(ctx context.Context, request protocol.Message, br fail(err) return } - dismissed, err := runner.Dismissed(ctx, sessionID) + dismissedRuns, 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}) + _ = write(protocol.Message{Type: protocol.ChatShellRuns, ID: request.ID, ChatShellRuns: runs, + ChatShellDismissed: dismissed, ChatShellDismissedRuns: dismissedRuns}) case protocol.ChatShellDismiss: dismissed, err := runner.Dismiss(ctx, sessionID, body.RunIDs) if err != nil { diff --git a/internal/server/chat_shell_runner.go b/internal/server/chat_shell_runner.go index 17a15e5f..1de83da0 100644 --- a/internal/server/chat_shell_runner.go +++ b/internal/server/chat_shell_runner.go @@ -49,8 +49,6 @@ 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 @@ -77,7 +75,6 @@ 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{}, } } @@ -93,14 +90,12 @@ 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). It also -// purges dismissals whose undo window passed while no server was running. +// their processes died with it (or are no longer owned by anyone). 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