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