0f5a188c6a
Every agent stage now writes into a per-run/step artifacts directory under
the data dir (<data>/artifacts/<run>/<step>), passed to Pi as
PIPELINES_ARTIFACTS_DIR, exposed on the OpenCode client env seam, and stated
in the agent prompt boilerplate so agents save images, screenshots, reports
and logs there.
After each step the server scans that directory with a bounded, symlink- and
traversal-safe inventory (os.OpenRoot + Lstat + O_NOFOLLOW) recording name,
size_bytes, content type (extension then 512-byte sniff fallback) and URL,
enforcing max files, per-file bytes and total bytes while counting skipped
and truncated entries. A missing, unreadable or oversized directory yields
only a best-effort diagnostic and never changes the step outcome.
The list is carried on protocol.RunStep as Artifacts []StageArtifact plus
skipped/truncated counters (omitempty, backwards compatible), persisted in a
new step_artifacts table keyed (run_id, sequence) alongside the
notifications/file-changes tables and loaded by GetRun.
GET /artifacts/{run}/{step}/{name} on the existing handler serves the bytes
with http.ServeContent, so Content-Type, Content-Length, HEAD and Range
(206/416) come from the stdlib; unknown runs/steps, absolute paths,
traversal, symlink escapes and non-regular files are refused.
The TUI lists each selected step's artifacts (name + human size) in the
retained-evidence section and adds an artifacts summary in the style of the
file-changes summary. Adds docs/stage-artifacts.md linked from README and an
artifacts subsection to the packaged operations skill reference.
Tests cover the scan records and caps, missing/oversized directories,
HTTP HEAD/Range/traversal/symlink/unknown refusals and protocol round-trips.
239 lines
8.1 KiB
Go
239 lines
8.1 KiB
Go
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"
|
||
"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
|
||
}
|
||
return s, nil
|
||
}
|
||
|
||
func (s *Store) Close() error { return s.db.Close() }
|
||
|
||
// 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
|
||
}
|