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.
This commit is contained in:
2026-11-09 23:29:25 +00:00
parent 371d29224c
commit f2520a09b8
10 changed files with 204 additions and 186 deletions
+33 -22
View File
@@ -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)
}
}