Files
pipelines/internal/pipeline/chat_shell_runs.go
T
alan f2520a09b8 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.
2026-11-09 23:29:25 +00:00

132 lines
5.5 KiB
Go

package pipeline
import (
"context"
"database/sql"
"errors"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// initChatShellRuns creates the additive table of operator shell commands. A
// run belongs to its chat session and is Pipelines' own record: it is never
// part of the harness transcript. Output holds only the retained tail (the raw
// terminal stream of a terminal run); output_text is its plain-text rendering.
// Typed input is never stored.
func (s *Store) initChatShellRuns() error {
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS chat_shell_runs (
id TEXT PRIMARY KEY,
session_id TEXT NOT NULL,
command TEXT NOT NULL,
directory TEXT NOT NULL,
status TEXT NOT NULL,
exit_code INTEGER,
signal TEXT NOT NULL DEFAULT '',
error TEXT NOT NULL DEFAULT '',
started_at INTEGER NOT NULL,
ended_at INTEGER NOT NULL DEFAULT 0,
duration_ms INTEGER NOT NULL DEFAULT 0,
output TEXT NOT NULL DEFAULT '',
output_start INTEGER NOT NULL DEFAULT 0,
output_bytes INTEGER NOT NULL DEFAULT 0);
CREATE INDEX IF NOT EXISTS chat_shell_runs_session ON chat_shell_runs(session_id, started_at);`)
if err != nil {
return err
}
for _, column := range [][2]string{
{"terminal", "INTEGER NOT NULL DEFAULT 0"},
{"cols", "INTEGER NOT NULL DEFAULT 0"},
{"rows", "INTEGER NOT NULL DEFAULT 0"},
{"output_text", "TEXT NOT NULL DEFAULT ''"},
} {
if err := s.addColumnIfMissing("chat_shell_runs", column[0], column[1]); err != nil {
return err
}
}
return s.initChatShellDismissals()
}
// SaveChatShellRun inserts or replaces one run. Inserting a new run prunes the
// session's oldest finished runs beyond keep (keep <= 0 keeps everything).
func (s *Store) SaveChatShellRun(ctx context.Context, run protocol.ChatShellRunRecord, keep int) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
var exit any
if run.ExitCode != nil {
exit = *run.ExitCode
}
if _, err := tx.ExecContext(ctx, `INSERT INTO chat_shell_runs
(id, session_id, command, directory, status, exit_code, signal, error, started_at, ended_at, duration_ms, output, output_start, output_bytes,
terminal, cols, rows, output_text)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO UPDATE SET status = excluded.status, exit_code = excluded.exit_code, signal = excluded.signal,
error = excluded.error, ended_at = excluded.ended_at, duration_ms = excluded.duration_ms, output = excluded.output,
output_start = excluded.output_start, output_bytes = excluded.output_bytes, terminal = excluded.terminal,
cols = excluded.cols, rows = excluded.rows, output_text = excluded.output_text`,
run.ID, run.SessionID, run.Command, run.Directory, run.Status, exit, run.Signal, run.Error,
run.StartedAt, run.EndedAt, run.DurationMS, run.Output, run.OutputStart, run.OutputBytes,
run.Terminal, run.Cols, run.Rows, run.Text); err != nil {
return err
}
if keep > 0 {
if _, err := tx.ExecContext(ctx, `DELETE FROM chat_shell_runs WHERE session_id = ? AND status != ? AND id NOT IN
(SELECT id FROM chat_shell_runs WHERE session_id = ? ORDER BY started_at DESC, id DESC LIMIT ?)`,
run.SessionID, protocol.ChatShellRunRunning, run.SessionID, keep); err != nil {
return err
}
}
return tx.Commit()
}
// ChatShellRuns lists one session's runs, oldest first, leaving out dismissed
// ones.
func (s *Store) ChatShellRuns(ctx context.Context, sessionID string) ([]protocol.ChatShellRunRecord, error) {
return s.queryChatShellRuns(ctx, ` WHERE session_id = ? AND dismissed_at = 0 ORDER BY started_at, id`, sessionID)
}
// ChatShellRun returns one run of sessionID, dismissed or not (a dismissed
// one is flagged); found is false when that session has no run with this id.
func (s *Store) ChatShellRun(ctx context.Context, sessionID, id string) (protocol.ChatShellRunRecord, bool, error) {
run, err := scanChatShellRun(s.db.QueryRowContext(ctx, chatShellRunSelect+` WHERE session_id = ? AND id = ?`, sessionID, id))
if errors.Is(err, sql.ErrNoRows) {
return protocol.ChatShellRunRecord{}, false, nil
}
return run, err == nil, err
}
// InterruptChatShellRuns marks every run still recorded as running as
// interrupted at endedAt. A starting server calls it: no process of a previous
// server is still owned by anyone.
func (s *Store) InterruptChatShellRuns(ctx context.Context, endedAt int64, reason string) (int64, error) {
result, err := s.db.ExecContext(ctx, `UPDATE chat_shell_runs SET status = ?, error = ?, ended_at = ?,
duration_ms = CASE WHEN ? > started_at THEN ? - started_at ELSE 0 END WHERE status = ?`,
protocol.ChatShellRunInterrupted, reason, endedAt, endedAt, endedAt, protocol.ChatShellRunRunning)
if err != nil {
return 0, err
}
return result.RowsAffected()
}
const chatShellRunSelect = `SELECT id, session_id, command, directory, status, exit_code, signal, error, started_at, ended_at, duration_ms, output, output_start, output_bytes, terminal, cols, rows, output_text, dismissed_at, purged FROM chat_shell_runs`
func scanChatShellRun(row chatQueueScanner) (protocol.ChatShellRunRecord, error) {
var run protocol.ChatShellRunRecord
var exit sql.NullInt64
err := row.Scan(&run.ID, &run.SessionID, &run.Command, &run.Directory, &run.Status, &exit, &run.Signal, &run.Error,
&run.StartedAt, &run.EndedAt, &run.DurationMS, &run.Output, &run.OutputStart, &run.OutputBytes,
&run.Terminal, &run.Cols, &run.Rows, &run.Text, &run.DismissedAt, &run.Purged)
if err != nil {
return protocol.ChatShellRunRecord{}, err
}
if exit.Valid {
code := int(exit.Int64)
run.ExitCode = &code
}
run.Truncated = run.OutputStart > 0
run.Dismissed = run.DismissedAt > 0
return run, nil
}