Files
pipelines/internal/server/chat_shell_dismiss.go
T
alan 03123c23fd Dismiss and restore operator shell commands per chat
chat_shell_dismiss hides finished runs from every readback and pushes a
dismissal frame to every watcher; chat_shell_restore undoes it within a
30 s window, after which the command, output and error are purged and only
a tombstone remains, reported by chat_shell_runs for clients that missed
the event.
2026-11-09 21:22:07 +00:00

108 lines
3.6 KiB
Go

package server
import (
"context"
"errors"
"fmt"
"time"
"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, since int64) ([]protocol.ChatShellRunRecord, error)
PurgeDismissedChatShellRuns(ctx context.Context, before int64) (int64, error)
DismissedChatShellRuns(ctx context.Context, sessionID string) ([]string, 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 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.
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})
}
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.
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)
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 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)
}
})
}
func (r *chatShellRunner) publishFrame(frame protocol.ChatShellRunEventFrame) {
if r.publish != nil {
r.publish(frame.SessionID, frame)
}
}