f2520a09b8
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.
99 lines
3.4 KiB
Go
99 lines
3.4 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"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) ([]protocol.ChatShellRunRecord, error)
|
|
DismissedChatShellRuns(ctx context.Context, sessionID string) ([]protocol.ChatShellRunRecord, 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 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
|
|
}
|
|
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})
|
|
}
|
|
return dismissed, nil
|
|
}
|
|
|
|
// 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
|
|
}
|
|
runs, err := r.store.RestoreChatShellRuns(ctx, sessionID, ids)
|
|
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 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) {
|
|
if r.publish != nil {
|
|
r.publish(frame.SessionID, frame)
|
|
}
|
|
}
|