Files
pipelines/internal/pipeline/store.go
T
alan 1f75e4797e Broker chat from an operator's own OpenCode endpoint and adopt existing sessions
Add an opt-in way to broker chat from an operator-supplied OpenCode service
instead of the instance the server spawns for itself, and a way to adopt a
session that already exists on that endpoint so it appears in chat_sessions and
can be continued from the app.

- Endpoint selection: the `-opencode-endpoint` server flag (defaulting to
  `PIPELINES_OPENCODE_ENDPOINT`) opts in; precedence is explicit flag >
  explicit environment > the endpoint recorded on a previous run, with the
  spawned instance as the default. opencode.OpenEndpoint validates the
  reference once (http(s), host, no userinfo/query/fragment/path), detects the
  backend from its own /api/info (v2) or /global/health (v1) contract, and
  requires a streamable text/event-stream event path. It owns no process
  (Close is a no-op) and never calls startServe; every failure names the
  endpoint and disables chat rather than silently falling back. An
  authenticated endpoint takes an optional password only through the private
  file PIPELINES_OPENCODE_ENDPOINT_PASSWORD_FILE, with
  PIPELINES_OPENCODE_ENDPOINT_USERNAME overriding the detected default.
- Persistence: the validated choice is recorded in an additive single-row
  server_settings table so a restart with no flag/environment reuses it; an
  unreachable recorded endpoint disables chat with a clear message and no
  fallback.
- chat_adopt: a new protocol request (ChatAdoptRequest) with handleChatAdopt in
  internal/server/chat.go. It refuses a nil broker/registry/payload and a
  blank or foreign-shaped id before any endpoint call, reads the endpoint via
  the shared Chat.Session (which reuses Sessions/decodeChatSessions), records
  through the existing RecordOperatorSession with the endpoint's directory and
  title verbatim, and echoes the adopted session plus its registry row like
  chat_create. A missing id is refused naming the id and records nothing; an
  endpoint record with an empty directory is refused rather than substituting
  the server's working directory or home.
- CLI: `pipelines chat-adopt <session-id> [--json]` (usage, notes, help
  inventory, dispatch) with docs/cli.md updated under Chat sessions.
- Autostart preserves the endpoint variables (and the password-file reference,
  never an inline secret) via serviceEnvironmentKeys.

Checks before committing:
- Package location: the change is user-facing chat behaviour, so the packaged
  authoring skill reference internal/author/skill/references/operations.md is
  updated in the same change (Brokered OpenCode chat section) with a dated
  verification-evidence entry and the not-deployed state. No new reference file
  is added, so internal/author/install.go's managed inventory is unchanged.
- Packaged templates: this repository packages no template set for this
  behaviour and none references the chat endpoint, so no template needs
  updating.
- Verification: `go build ./...`, `go vet` on the touched packages, `gofmt -l`
  clean and `go test ./...` all pass. The unit tests cover the endpoint
  validation/precedence, the adopt happy path and refusals, the recorded
  endpoint round-trip, the protocol round-trip and the CLI dispatch. The live
  scratch-server check against the operator's real endpoint was not run in this
  change.

Nothing unrelated is staged; docs left stale (README and other shared documents)
are not edited here and are for a single folding writer.
2026-10-17 20:14:11 +01:00

300 lines
10 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
path string
mu sync.Mutex
}
// openDB opens one SQLite connection pool for a DSN. Every open path shares it
// so the connection count and pool settings cannot drift between the writable,
// read-only and maintenance stores.
func openDB(dsn string) (*sql.DB, error) {
db, err := sql.Open("sqlite", dsn)
if err != nil {
return nil, err
}
db.SetMaxOpenConns(1)
return db, nil
}
func Open(path string) (*Store, error) {
db, err := openDB(path)
if err != nil {
return nil, err
}
_, 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), path: 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
}
if err := s.initServerSettings(); 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 := openDB("file:" + path + "?mode=ro")
if err != nil {
return nil, err
}
if _, err := db.Exec("PRAGMA busy_timeout=5000"); err != nil {
db.Close()
return nil, err
}
return &Store{db: db, dir: filepath.Dir(path), path: path}, nil
}
// OpenMaintenance opens an existing database read-write for an explicit
// operator maintenance action such as retention apply. It never creates a file
// and never runs schema DDL or changes the journal mode, so it cannot silently
// create or migrate a live database. It uses an immediate transaction lock and
// a longer busy timeout, so a concurrent server write produces an honest busy
// failure instead of a hang. The deployed server is never started or changed.
func OpenMaintenance(path string) (*Store, error) {
if _, err := os.Stat(path); err != nil {
return nil, err
}
db, err := openDB("file:" + path + "?_txlock=immediate")
if err != nil {
return nil, err
}
if _, err := db.Exec("PRAGMA busy_timeout=30000; PRAGMA foreign_keys=ON"); err != nil {
db.Close()
return nil, err
}
return &Store{db: db, dir: filepath.Dir(path), path: 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
}