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:
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user