Files
pipelines/internal/pipeline/chat_shell_runs.go
T
alan e6bc57c901 Run operator shell commands in a chat's directory on the server
chat_shell_run starts $SHELL -lc in the chat session's directory in its own
process group, streams merged output to every watcher of the session, keeps a
1 MiB tail and persists each run; chat_shell_stop interrupts the group and
kills it after a grace period. chat_shell_share queues an ordinary prompt
with the command and a bounded output tail.
2026-11-09 15:55:14 +00:00

124 lines
4.8 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.
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);`)
return err
}
// 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)
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`,
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); 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.
func (s *Store) ChatShellRuns(ctx context.Context, sessionID string) ([]protocol.ChatShellRunRecord, error) {
rows, err := s.db.QueryContext(ctx, chatShellRunSelect+` WHERE session_id = ? ORDER BY started_at, id`, sessionID)
if err != nil {
return nil, err
}
defer rows.Close()
var runs []protocol.ChatShellRunRecord
for rows.Next() {
run, err := scanChatShellRun(rows)
if err != nil {
return nil, err
}
runs = append(runs, run)
}
return runs, rows.Err()
}
// ChatShellRun returns one run of sessionID; 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 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)
if err != nil {
return protocol.ChatShellRunRecord{}, err
}
if exit.Valid {
code := int(exit.Int64)
run.ExitCode = &code
}
run.Truncated = run.OutputStart > 0
return run, nil
}