Files
pipelines/internal/pipeline/store.go
T
alan 18398dfa5b chat: give the operator their own sessions, separate from run sessions
chat_sessions used to list exactly the OpenCode sessions the pipelines own,
filtered through pipeline.SessionOwner (run_steps/run_session_owners), so an
operator-created chat was an accident of the run-ownership table and the
operator's working sessions were mixed with the internals of running pipelines.

Add a first-class operator session:

- internal/pipeline/operator_sessions.go: a dedicated operator_sessions table
  (session_id PK, directory, title, tag, created_at, last_used_at) with
  RecordOperatorSession (upsert preserving created_at), a one-statement
  TouchOperatorSession (no-op for a non-operator id), OperatorSessions bounded to
  128 rows most-recently-used first, and OperatorDirectories (bounded MRU,
  deduped). run_steps/run_session_owners and SessionOwner/OwnedSessions are
  untouched.
- internal/server/chat.go: chat_sessions returns operator sessions only - it
  reads the registry once, intersects it with one broker session-list call, fills
  directory/tag/created/updated from the registry, sets busy for a session held
  by another connection, keeps MRU order, and stays fail-closed (empty list, never
  the endpoint's whole list). A pipeline stage session and an untagged row can
  never be listed. chat_create resolves the directory (empty/'~'/'~/...' ->
  operator home; relative or missing rejected before any OpenCode call), calls
  broker.Create(title, resolved) and records the row with the operator tag.
  chat_directories returns home first then bounded (8) deduped recents. A
  successful prompt touches the operator session without affecting pipeline
  prompts.
- internal/opencode, internal/protocol, internal/server/websocket.go and
  message.go: unscoped session list, Create posts {"title":...,
  "location":{"directory":...}} to POST /api/session, additive protocol fields
  (omitempty) and the ChatDirectories request/response, plus the OperatorRegistry
  option defaulting to the first store. One OpenCode transport per server is
  unchanged.

Docs and references: docs/cli.md gains a "Chat sessions" section documenting the
operator-only fail-closed list, directory defaulting/rejection, chat_directories
(home first, deduped, bounded 8) and why a dedicated request was chosen.
internal/author/skill/references/operations.md's stale "Brokered OpenCode chat"
text (chat_create unbrokered, run_steps filtering, "listed the owned scratch
session" evidence) is rewritten to the operator-session registry, the fail-closed
filter, directory defaulting and the corrected live-test wording. No new
reference file, so internal/author/install.go's inventory is unchanged.

Checked for staleness elsewhere: README.md, the docs/ topic pages and the
packaged server/TUI templates carry no chat-session ownership claims, so none
needed updating.

Verified: gofmt -l clean; go vet and go build ./... clean; go test -count=1
./internal/pipeline/... ./internal/protocol/... ./internal/opencode/...
./internal/server/... and the full go test -count=1 ./... pass.
2026-10-17 11:14:25 +01:00

264 lines
8.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package pipeline
import (
"context"
"crypto/sha256"
"database/sql"
"encoding/json"
"fmt"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
"os"
"path/filepath"
"strings"
"sync"
"time"
_ "modernc.org/sqlite"
)
type Store struct {
db *sql.DB
dir string
mu sync.Mutex
}
func Open(path string) (*Store, error) {
db, err := sql.Open("sqlite", path)
if err != nil {
return nil, err
}
db.SetMaxOpenConns(1)
_, err = db.Exec(`PRAGMA foreign_keys=ON; PRAGMA journal_mode=WAL; PRAGMA busy_timeout=5000;
CREATE TABLE IF NOT EXISTS pipelines (id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE, created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP);
CREATE TABLE IF NOT EXISTS pipeline_versions (id INTEGER PRIMARY KEY, pipeline_id INTEGER NOT NULL REFERENCES pipelines(id), version INTEGER NOT NULL, source TEXT NOT NULL, content_hash TEXT NOT NULL, graph_json TEXT NOT NULL, created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, UNIQUE(pipeline_id, version));
CREATE TABLE IF NOT EXISTS submissions (id INTEGER PRIMARY KEY, name TEXT NOT NULL, source TEXT NOT NULL, diagnostic TEXT NOT NULL, version_id INTEGER REFERENCES pipeline_versions(id), created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP);`)
if err != nil {
db.Close()
return nil, err
}
s := &Store{db: db, dir: filepath.Dir(path)}
if err := s.initRuns(); err != nil {
db.Close()
return nil, err
}
if err := s.initSchedules(); err != nil {
db.Close()
return nil, err
}
if err := s.initOperatorSessions(); err != nil {
db.Close()
return nil, err
}
return s, nil
}
func (s *Store) Close() error { return s.db.Close() }
// OpenReadOnly opens an existing database without running schema DDL or
// changing journal mode, so a live database can be queried read-only while its
// server keeps writing. It fails when the file does not exist. It is used by
// tests that inspect a real database and is never part of server startup.
func OpenReadOnly(path string) (*Store, error) {
if _, err := os.Stat(path); err != nil {
return nil, err
}
db, err := sql.Open("sqlite", "file:"+path+"?mode=ro")
if err != nil {
return nil, err
}
db.SetMaxOpenConns(1)
if _, err := db.Exec("PRAGMA busy_timeout=5000"); err != nil {
db.Close()
return nil, err
}
return &Store{db: db, dir: filepath.Dir(path)}, nil
}
// Directory is the resolved directory holding the database, used to place
// adjacent server state such as stage artifacts.
func (s *Store) Directory() string { return s.dir }
func (s *Store) List(ctx context.Context) ([]protocol.PipelineSummary, error) {
rows, err := s.db.QueryContext(ctx, `SELECT p.id, p.name, v.version,
json_array_length(v.graph_json, '$.stages'),
strftime('%Y-%m-%dT%H:%M:%SZ', p.created_at), strftime('%Y-%m-%dT%H:%M:%SZ', v.created_at)
FROM pipelines p JOIN pipeline_versions v ON v.pipeline_id=p.id
WHERE v.version=(SELECT MAX(version) FROM pipeline_versions WHERE pipeline_id=p.id)
ORDER BY p.name, p.id`)
if err != nil {
return nil, err
}
defer rows.Close()
result := []protocol.PipelineSummary{}
for rows.Next() {
var item protocol.PipelineSummary
if err := rows.Scan(&item.ID, &item.Name, &item.Version, &item.Stages, &item.CreatedAt, &item.UpdatedAt); err != nil {
return nil, err
}
result = append(result, item)
}
return result, rows.Err()
}
// Get returns a consistent immutable version and the available version history.
// Version zero selects the latest accepted version.
func (s *Store) Get(ctx context.Context, id int64, version int) (*protocol.PipelineDetail, error) {
if id <= 0 || version < 0 {
return nil, fmt.Errorf("invalid pipeline ID or version")
}
tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, err
}
defer tx.Rollback()
detail := &protocol.PipelineDetail{}
err = tx.QueryRowContext(ctx, `SELECT p.id,p.name,strftime('%Y-%m-%dT%H:%M:%SZ',p.created_at),
v.version,strftime('%Y-%m-%dT%H:%M:%SZ',v.created_at)
FROM pipelines p JOIN pipeline_versions v ON v.pipeline_id=p.id
WHERE p.id=? ORDER BY v.version DESC LIMIT 1`, id).
Scan(&detail.ID, &detail.Name, &detail.CreatedAt, &detail.LatestVersion, &detail.UpdatedAt)
if err == sql.ErrNoRows {
return nil, fmt.Errorf("pipeline not found")
}
if err != nil {
return nil, err
}
if version == 0 {
version = detail.LatestVersion
}
var graph string
err = tx.QueryRowContext(ctx, `SELECT version,source,content_hash,graph_json,strftime('%Y-%m-%dT%H:%M:%SZ',created_at)
FROM pipeline_versions WHERE pipeline_id=? AND version=?`, id, version).
Scan(&detail.Version, &detail.Source, &detail.ContentHash, &graph, &detail.VersionCreatedAt)
if err == sql.ErrNoRows {
return nil, fmt.Errorf("pipeline version not found")
}
if err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(graph), &detail.Graph); err != nil {
return nil, err
}
// Normalize legacy terminal aliases for clients without rewriting history.
for i := range detail.Graph.Stages {
s := &detail.Graph.Stages[i]
s.Type = stage.EffectiveType(s.Type)
for outcome, target := range s.Edges {
s.Edges[outcome] = protocol.ResolveTarget(outcome, target)
}
}
detail.Stages = len(detail.Graph.Stages)
rows, err := tx.QueryContext(ctx, `SELECT version,json_array_length(graph_json,'$.stages'),strftime('%Y-%m-%dT%H:%M:%SZ',created_at)
FROM pipeline_versions WHERE pipeline_id=? ORDER BY version DESC`, id)
if err != nil {
return nil, err
}
detail.Versions = []protocol.VersionSummary{}
for rows.Next() {
var item protocol.VersionSummary
if err := rows.Scan(&item.Version, &item.Stages, &item.CreatedAt); err != nil {
rows.Close()
return nil, err
}
detail.Versions = append(detail.Versions, item)
}
err = rows.Err()
rows.Close()
if err != nil {
return nil, err
}
if err := tx.Commit(); err != nil {
return nil, err
}
return detail, nil
}
// Submit persists both accepted and rejected submissions. Equality is source-byte equality.
func (s *Store) Submit(ctx context.Context, name, source string, createOnly ...bool) (int, error) {
fresh := len(createOnly) > 0 && createOnly[0]
return s.SubmitChecked(ctx, name, source, fresh, 0)
}
// SubmitChecked guards draft edits against concurrent accepted versions atomically.
func (s *Store) SubmitChecked(ctx context.Context, name, source string, createOnly bool, expectedVersion int) (int, error) {
s.mu.Lock()
defer s.mu.Unlock()
name = strings.TrimSpace(name)
g, validation := Parse(source)
if expectedVersion < 0 {
validation = fmt.Errorf("expected version must be nonnegative")
}
if name == "" || len(name) > 200 || strings.ContainsAny(name, "\r\n\x00") {
validation = fmt.Errorf("pipeline name must be 1–200 bytes without line breaks or NUL")
}
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return 0, err
}
defer tx.Rollback()
var pid int64
var version int
var versionID any
hash := fmt.Sprintf("%x", sha256.Sum256([]byte(source)))
if validation == nil {
_, err = tx.ExecContext(ctx, "INSERT INTO pipelines(name) VALUES (?) ON CONFLICT(name) DO NOTHING", name)
if err != nil {
return 0, err
}
if err = tx.QueryRowContext(ctx, "SELECT id FROM pipelines WHERE name=?", name).Scan(&pid); err != nil {
return 0, err
}
var latestHash string
err = tx.QueryRowContext(ctx, "SELECT version, content_hash FROM pipeline_versions WHERE pipeline_id=? ORDER BY version DESC LIMIT 1", pid).Scan(&version, &latestHash)
if err != nil && err != sql.ErrNoRows {
return 0, err
}
if createOnly && version > 0 {
validation = fmt.Errorf("pipeline name already exists; choose a new name")
} else if expectedVersion > 0 && version != expectedVersion {
validation = fmt.Errorf("draft conflict: expected latest version %d, found %d; review changes before submitting", expectedVersion, version)
} else if hash == latestHash {
validation = fmt.Errorf("no changes")
} else {
version++
encoded, err := json.Marshal(g)
if err != nil {
return 0, err
}
result, err := tx.ExecContext(ctx, "INSERT INTO pipeline_versions(pipeline_id,version,source,content_hash,graph_json) VALUES (?,?,?,?,?)", pid, version, source, hash, string(encoded))
if err != nil {
return 0, err
}
id, err := result.LastInsertId()
if err != nil {
return 0, err
}
versionID = id
if g.Settings != nil {
activated := time.Now()
for slot, rule := range g.Settings.Schedules {
next, err := NextSchedule(rule, activated)
if err != nil {
return 0, err
}
if _, err := tx.ExecContext(ctx, `INSERT INTO schedule_state(pipeline_id,version,slot,next_due) VALUES(?,?,?,?)`, pid, version, slot, next.UTC().Format(time.RFC3339Nano)); err != nil {
return 0, err
}
}
}
}
}
diagnostic := ""
if validation != nil {
diagnostic = validation.Error()
}
if _, err := tx.ExecContext(ctx, "INSERT INTO submissions(name,source,diagnostic,version_id) VALUES (?,?,?,?)", name, source, diagnostic, versionID); err != nil {
return 0, err
}
if err := tx.Commit(); err != nil {
return 0, err
}
return version, validation
}