Merge lane-retry: split chat/model/run files by concern (pure move)

This commit is contained in:
2026-10-17 21:09:27 +01:00
16 changed files with 1880 additions and 1790 deletions
-671
View File
@@ -6,217 +6,10 @@ import (
"database/sql"
"encoding/json"
"fmt"
"strings"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
"git.a-wan.net/alan/pipelines/internal/summarise"
)
func (s *Store) initRuns() error {
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS pipeline_runs (
id INTEGER PRIMARY KEY, pipeline_id INTEGER NOT NULL, version INTEGER NOT NULL,
request_key TEXT NOT NULL UNIQUE, workspace TEXT NOT NULL, allow_tools INTEGER NOT NULL,
title TEXT NOT NULL DEFAULT '', title_source TEXT NOT NULL DEFAULT '', title_model TEXT NOT NULL DEFAULT '',
model TEXT NOT NULL DEFAULT '', stage_models TEXT NOT NULL DEFAULT '{}',
write_paths TEXT NOT NULL, input_json TEXT NOT NULL, output_json TEXT NOT NULL DEFAULT '{}',
status TEXT NOT NULL DEFAULT 'queued', outcome TEXT NOT NULL DEFAULT '', current_stage TEXT NOT NULL DEFAULT '',
diagnostic TEXT NOT NULL DEFAULT '', created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, finished_at TEXT,
FOREIGN KEY(pipeline_id,version) REFERENCES pipeline_versions(pipeline_id,version));
CREATE TABLE IF NOT EXISTS run_steps (
run_id INTEGER NOT NULL REFERENCES pipeline_runs(id), sequence INTEGER NOT NULL, stage_key TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'running', outcome TEXT NOT NULL DEFAULT '', target TEXT NOT NULL DEFAULT '',
input_json TEXT NOT NULL, output_json TEXT NOT NULL DEFAULT '{}', diagnostic TEXT NOT NULL DEFAULT '',
provider TEXT NOT NULL DEFAULT '', model TEXT NOT NULL DEFAULT '',
session_id TEXT NOT NULL DEFAULT '', agent_active INTEGER NOT NULL DEFAULT 0, transcript TEXT NOT NULL DEFAULT '',
started_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, finished_at TEXT, PRIMARY KEY(run_id,sequence));
CREATE INDEX IF NOT EXISTS run_latest ON pipeline_runs(id DESC);
CREATE INDEX IF NOT EXISTS run_steps_session ON run_steps(session_id);
CREATE TABLE IF NOT EXISTS step_notifications (
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, receipts_json TEXT NOT NULL,
PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence));
CREATE TABLE IF NOT EXISTS step_file_changes (
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, changes_json TEXT NOT NULL,
PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence));
CREATE TABLE IF NOT EXISTS step_control (
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, evidence_json TEXT NOT NULL,
PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence));
CREATE TABLE IF NOT EXISTS step_artifacts (
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, artifacts_json TEXT NOT NULL,
PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence));
CREATE TABLE IF NOT EXISTS step_verdicts (
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, verdict_json TEXT NOT NULL,
PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence));
CREATE TABLE IF NOT EXISTS step_session_exports (
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, export_json TEXT NOT NULL,
PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence));
CREATE TABLE IF NOT EXISTS step_usage (
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, usage_json TEXT NOT NULL,
PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence));
CREATE TABLE IF NOT EXISTS run_session_owners (
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, backend TEXT NOT NULL, reference TEXT NOT NULL DEFAULT '',
PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence));
CREATE TABLE IF NOT EXISTS run_retries (
run_id INTEGER PRIMARY KEY REFERENCES pipeline_runs(id), original_run_id INTEGER NOT NULL REFERENCES pipeline_runs(id));`)
if err != nil {
return err
}
if err := s.migrateRunTitles(); err != nil {
return err
}
return s.migrateRunModels()
}
// migrateRunModels adds the launch-time model-override columns to a database
// created before per-run overrides existed. Each column is added only when
// missing, so an upgrade is idempotent across repeated Open calls and existing
// rows keep their defaults (no model override, no recorded step model).
func (s *Store) migrateRunModels() error {
for _, column := range [][3]string{
{"pipeline_runs", "model", "TEXT NOT NULL DEFAULT ''"},
{"pipeline_runs", "stage_models", "TEXT NOT NULL DEFAULT '{}'"},
{"run_steps", "provider", "TEXT NOT NULL DEFAULT ''"},
{"run_steps", "model", "TEXT NOT NULL DEFAULT ''"},
} {
if err := s.addColumnIfMissing(column[0], column[1], column[2]); err != nil {
return err
}
}
return nil
}
// migrateRunTitles adds the title columns to a database created before run
// titles existed. Adding a column with a default leaves every existing row valid
// and untouched, so old runs still list.
func (s *Store) migrateRunTitles() error {
for _, column := range [][2]string{
{"title", "TEXT NOT NULL DEFAULT ''"},
{"title_source", "TEXT NOT NULL DEFAULT ''"},
{"title_model", "TEXT NOT NULL DEFAULT ''"},
} {
if err := s.addColumnIfMissing("pipeline_runs", column[0], column[1]); err != nil {
return err
}
}
return nil
}
// addColumnIfMissing adds a column only when the table predates it, making an
// upgrade idempotent across repeated Open calls without rewriting history.
func (s *Store) addColumnIfMissing(table, column, definition string) error {
rows, err := s.db.Query("PRAGMA table_info(" + table + ")")
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var cid, notnull, pk int
var name, ctype string
var dflt sql.NullString
if err := rows.Scan(&cid, &name, &ctype, &notnull, &dflt, &pk); err != nil {
return err
}
if name == column {
return nil
}
}
if err := rows.Err(); err != nil {
return err
}
_, err = s.db.Exec("ALTER TABLE " + table + " ADD COLUMN " + column + " " + definition)
return err
}
// RunOverrides are the launch-time model overrides recorded for a run:
// Model applies to every model stage and StageModels overrides individual model
// stages by key (per-stage wins over whole-run). They are recorded so the run
// stays reproducible without touching the pinned pipeline version.
type RunOverrides struct {
Model string
StageModels map[string]string
}
// CreateRun inserts a run and derives its title from the prompt. It is the entry
// point for callers with no generated title; the derived fallback is always
// non-empty and never calls a model.
func (s *Store) CreateRun(ctx context.Context, d *protocol.PipelineDetail, key, workspace string, tools bool, paths []string, input map[string]any, retryOf ...int64) (int64, bool, error) {
return s.CreateRunTitled(ctx, d, key, workspace, tools, paths, input, RunOverrides{}, summarise.Title{}, retryOf...)
}
// CreateRunTitled inserts a run with the caller's title and launch-time model
// overrides. A non-empty title (a model generation) is stored verbatim; an empty
// one falls back to the prompt-derived title, so a run never stores an empty
// title. The overrides are compared in the request-key idempotency check, so a
// replay with different overrides is refused as a different run.
func (s *Store) CreateRunTitled(ctx context.Context, d *protocol.PipelineDetail, key, workspace string, tools bool, paths []string, input map[string]any, overrides RunOverrides, title summarise.Title, retryOf ...int64) (int64, bool, error) {
var parent int64
if len(retryOf) > 0 {
parent = retryOf[0]
}
if parent < 0 {
return 0, false, fmt.Errorf("invalid retry parent")
}
title = resolveRunTitle(title, input)
encoded, err := json.Marshal(input)
if err != nil {
return 0, false, err
}
writes, err := json.Marshal(paths)
if err != nil {
return 0, false, err
}
stageModels := overrides.StageModels
if stageModels == nil {
stageModels = map[string]string{}
}
encodedModels, err := json.Marshal(stageModels)
if err != nil {
return 0, false, err
}
s.mu.Lock()
defer s.mu.Unlock()
var id, pid int64
var version int
var oldInput, oldWorkspace, oldPaths, oldModel, oldStageModels string
var oldTools bool
err = s.db.QueryRowContext(ctx, "SELECT id,pipeline_id,version,input_json,workspace,write_paths,allow_tools,model,stage_models FROM pipeline_runs WHERE request_key=?", key).Scan(&id, &pid, &version, &oldInput, &oldWorkspace, &oldPaths, &oldTools, &oldModel, &oldStageModels)
if err == nil {
var oldParent int64
if err := s.db.QueryRowContext(ctx, "SELECT COALESCE((SELECT original_run_id FROM run_retries WHERE run_id=?),0)", id).Scan(&oldParent); err != nil {
return 0, false, err
}
if pid != d.ID || version != d.Version || oldInput != string(encoded) || oldWorkspace != workspace || oldPaths != string(writes) || oldTools != tools || oldParent != parent || oldModel != overrides.Model || oldStageModels != string(encodedModels) {
return 0, false, fmt.Errorf("request key already used for a different run")
}
return id, false, nil
}
if err != sql.ErrNoRows {
return 0, false, err
}
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return 0, false, err
}
defer tx.Rollback()
result, err := tx.ExecContext(ctx, "INSERT INTO pipeline_runs(pipeline_id,version,request_key,workspace,allow_tools,write_paths,input_json,title,title_source,title_model,model,stage_models) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", d.ID, d.Version, key, workspace, tools, string(writes), string(encoded), title.Text, title.Source, title.Model, overrides.Model, string(encodedModels))
if err != nil {
return 0, false, err
}
id, err = result.LastInsertId()
if err != nil {
return 0, false, err
}
if parent > 0 {
if _, err := tx.ExecContext(ctx, "INSERT INTO run_retries(run_id,original_run_id) VALUES(?,?)", id, parent); err != nil {
return 0, false, err
}
}
if err := tx.Commit(); err != nil {
return 0, false, err
}
return id, true, nil
}
// runFrom is the shared pipeline_runs/pipelines join every run-row read builds
// on, so the run projections cannot drift.
const runFrom = `FROM pipeline_runs r JOIN pipelines p ON p.id=r.pipeline_id`
@@ -225,36 +18,6 @@ const runFrom = `FROM pipeline_runs r JOIN pipelines p ON p.id=r.pipeline_id`
// and searchRuns instead of copied per query.
const runStamps = `strftime('%Y-%m-%dT%H:%M:%SZ',r.created_at),COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ',r.finished_at),'')`
// resolveRunTitle guarantees a stored run title is never empty: a title supplied
// by the caller (a model generation) is kept verbatim, and an absent one is
// derived from the prompt's first line. The fallback is server-owned and never
// calls a model, so it can never block or fail run creation.
func resolveRunTitle(title summarise.Title, input map[string]any) summarise.Title {
if strings.TrimSpace(title.Text) != "" {
return title
}
prompt, _ := input["prompt"].(string)
return summarise.Title{Text: summarise.DeriveTitle(prompt), Source: summarise.SourceFallback}
}
// SetRunTitle replaces a run's operator-facing title and its provenance without
// touching the run's input, steps or execution. It is how an operator renames a
// run after the fact.
func (s *Store) SetRunTitle(ctx context.Context, id int64, title summarise.Title) error {
result, err := s.db.ExecContext(ctx, "UPDATE pipeline_runs SET title=?,title_source=?,title_model=? WHERE id=?", title.Text, title.Source, title.Model, id)
if err != nil {
return err
}
n, err := result.RowsAffected()
if err != nil {
return err
}
if n != 1 {
return fmt.Errorf("run not found")
}
return nil
}
const runSelect = `SELECT r.id,r.pipeline_id,p.name,r.version,v.content_hash,r.status,r.outcome,r.current_stage,
` + runStamps + `,r.diagnostic,
CASE WHEN r.status='succeeded' THEN r.output_json ELSE COALESCE((SELECT output_json FROM run_steps WHERE run_id=r.id AND status='completed' ORDER BY sequence DESC LIMIT 1),'{}') END,
@@ -443,437 +206,3 @@ strftime('%Y-%m-%dT%H:%M:%SZ',started_at),COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ'
}
return r, nil
}
func (s *Store) SaveNotifications(ctx context.Context, id int64, sequence int, receipts []protocol.NotificationReceipt) error {
data, err := json.Marshal(receipts)
if err != nil {
return err
}
_, err = s.db.ExecContext(ctx, `INSERT INTO step_notifications(run_id,sequence,receipts_json) VALUES(?,?,?) ON CONFLICT(run_id,sequence) DO UPDATE SET receipts_json=excluded.receipts_json`, id, sequence, string(data))
return err
}
func affected(result sql.Result, err error) error {
if err != nil {
return err
}
n, err := result.RowsAffected()
if err != nil {
return err
}
if n != 1 {
return fmt.Errorf("invalid run state transition")
}
return nil
}
func (s *Store) BeginStep(ctx context.Context, id int64, sequence int, key string, input map[string]any) error {
encoded, err := json.Marshal(input)
if err != nil {
return err
}
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
if err := affected(tx.ExecContext(ctx, "UPDATE pipeline_runs SET status='running',current_stage=? WHERE id=? AND status IN ('queued','running')", key, id)); err != nil {
return err
}
if _, err := tx.ExecContext(ctx, "INSERT INTO run_steps(run_id,sequence,stage_key,input_json) VALUES (?,?,?,?)", id, sequence, key, string(encoded)); err != nil {
return err
}
return tx.Commit()
}
func (s *Store) RunSession(ctx context.Context, id int64, sequence int, session string, active bool, metadata ...string) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
if err = affected(tx.ExecContext(ctx, "UPDATE run_steps SET session_id=?,agent_active=? WHERE run_id=? AND sequence=? AND status='running'", session, active, id, sequence)); err != nil {
return err
}
if len(metadata) > 0 {
backend := metadata[0]
if backend == "" {
backend = "v2"
}
reference := ""
if len(metadata) > 1 {
reference = metadata[1]
}
if _, err = tx.ExecContext(ctx, "INSERT INTO run_session_owners(run_id,sequence,backend,reference) VALUES(?,?,?,?) ON CONFLICT(run_id,sequence) DO UPDATE SET backend=excluded.backend,reference=excluded.reference", id, sequence, backend, reference); err != nil {
return err
}
}
return tx.Commit()
}
type SessionOwner struct {
ID string
Backend string
Directory string
Reference string
// RunID is the owning run, used to tell an orphaned session (no active run)
// from one owned by a run that is active right now.
RunID int64
// Active is true while the owning step records the session as agent-active.
Active bool
}
func (s *Store) RunSessionOwner(ctx context.Context, id int64, sequence int) (SessionOwner, error) {
var owner SessionOwner
err := s.db.QueryRowContext(ctx, `SELECT s.session_id,COALESCE(o.backend,'v2'),r.workspace,COALESCE(o.reference,'') FROM run_steps s JOIN pipeline_runs r ON r.id=s.run_id LEFT JOIN run_session_owners o ON o.run_id=s.run_id AND o.sequence=s.sequence WHERE s.run_id=? AND s.sequence=?`, id, sequence).Scan(&owner.ID, &owner.Backend, &owner.Directory, &owner.Reference)
return owner, err
}
// sessionOwnerScanLimit bounds the ownership scan so a long-lived database with
// thousands of historical sessions cannot make one chat_sessions request scan
// them all. Rows are read newest-run first, so the bound only ever drops the
// oldest owners, never the sessions a client can still be writing.
const sessionOwnerScanLimit = 1024
// ActiveSessionOwners reports every currently active agent session, deduped by
// session id (newest owning run wins).
func (s *Store) ActiveSessionOwners(ctx context.Context) ([]SessionOwner, error) {
return s.sessionOwners(ctx, true)
}
// OwnedSessions reports every session the server ever recorded in run_steps,
// deduped by session id (newest owning run wins) and including inactive ones, so
// chat_sessions can be restricted to sessions this server created.
func (s *Store) OwnedSessions(ctx context.Context) ([]SessionOwner, error) {
return s.sessionOwners(ctx, false)
}
// sessionOwners is the single ownership query both ActiveSessionOwners and
// OwnedSessions run, so the SQL is not duplicated. It reads one bounded,
// newest-run-first window and drops repeated session ids in favour of the most
// recent owning run's directory and active flag.
func (s *Store) sessionOwners(ctx context.Context, activeOnly bool) ([]SessionOwner, error) {
query := `SELECT s.session_id,COALESCE(o.backend,'v2'),r.workspace,COALESCE(o.reference,''),s.run_id,s.agent_active
FROM run_steps s JOIN pipeline_runs r ON r.id=s.run_id LEFT JOIN run_session_owners o ON o.run_id=s.run_id AND o.sequence=s.sequence WHERE s.session_id<>''`
if activeOnly {
query += " AND s.agent_active=1"
}
query += " ORDER BY s.run_id DESC LIMIT ?"
rows, err := s.db.QueryContext(ctx, query, sessionOwnerScanLimit)
if err != nil {
return nil, err
}
defer rows.Close()
seen := map[string]struct{}{}
var owners []SessionOwner
for rows.Next() {
var owner SessionOwner
var active int
if err = rows.Scan(&owner.ID, &owner.Backend, &owner.Directory, &owner.Reference, &owner.RunID, &active); err != nil {
return nil, err
}
owner.Active = active != 0
if _, ok := seen[owner.ID]; ok {
continue
}
seen[owner.ID] = struct{}{}
owners = append(owners, owner)
}
return owners, rows.Err()
}
// stageArtifactsEvidence is the persisted per-step artifact list plus its
// bounded-scan counters; it is kept in a separate table like the other
// step-scoped evidence so run_steps needs no schema migration on old databases.
type stageArtifactsEvidence struct {
Files []protocol.StageArtifact `json:"files"`
Skipped int `json:"skipped,omitempty"`
Truncated int `json:"truncated,omitempty"`
}
// stageVerdictEvidence is the generated one-line verdict reason plus its
// provenance. Like the other step-scoped evidence it lives in its own table, so
// run_steps needs no schema migration and an old database gains the empty table
// on Open with no backfill. Only a model-generated line is ever stored; the
// read-time fallback is always derived, so existing runs still show a verdict.
type stageVerdictEvidence struct {
Reason string `json:"reason"`
Source string `json:"source"`
}
func (s *Store) FinishStep(ctx context.Context, id int64, step protocol.RunStep) error {
output, err := json.Marshal(step.Output)
if err != nil {
return err
}
// Bound the stored transcript without corrupting structured JSON. A large
// transcript becomes a valid bounded preview envelope preserving stage_limits
// and failure/format diagnostics; the exact Output is saved separately below.
step.Transcript = stage.BoundTranscript(step.Transcript, step.SessionID)
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
if err := affected(tx.ExecContext(ctx, `UPDATE run_steps SET status=?,outcome=?,target=?,output_json=?,diagnostic=?,transcript=?,provider=?,model=?,finished_at=CURRENT_TIMESTAMP
WHERE run_id=? AND sequence=? AND status='running'`, step.Status, step.Outcome, step.Target, string(output), step.Diagnostic, step.Transcript, step.Provider, step.Model, id, step.Sequence)); err != nil {
return err
}
if step.FileChanges != nil {
data, err := json.Marshal(step.FileChanges)
if err != nil {
return err
}
if _, err = tx.ExecContext(ctx, "INSERT INTO step_file_changes(run_id,sequence,changes_json) VALUES(?,?,?)", id, step.Sequence, string(data)); err != nil {
return err
}
}
if step.Control != nil {
data, err := json.Marshal(step.Control)
if err != nil {
return err
}
if _, err := tx.ExecContext(ctx, "INSERT INTO step_control(run_id,sequence,evidence_json) VALUES(?,?,?)", id, step.Sequence, string(data)); err != nil {
return err
}
}
if len(step.Artifacts) > 0 || step.ArtifactsSkipped > 0 || step.ArtifactsTruncated > 0 {
data, err := json.Marshal(stageArtifactsEvidence{Files: step.Artifacts, Skipped: step.ArtifactsSkipped, Truncated: step.ArtifactsTruncated})
if err != nil {
return err
}
if _, err := tx.ExecContext(ctx, "INSERT INTO step_artifacts(run_id,sequence,artifacts_json) VALUES(?,?,?)", id, step.Sequence, string(data)); err != nil {
return err
}
}
// Persist only a generated model line and its provenance; the read-time
// fallback is always derived in GetRun, so old runs stay intact.
if step.Verdict != nil && strings.TrimSpace(step.Verdict.Reason) != "" {
data, err := json.Marshal(stageVerdictEvidence{Reason: step.Verdict.Reason, Source: step.Verdict.ReasonSource})
if err != nil {
return err
}
if _, err := tx.ExecContext(ctx, "INSERT INTO step_verdicts(run_id,sequence,verdict_json) VALUES(?,?,?) ON CONFLICT(run_id,sequence) DO UPDATE SET verdict_json=excluded.verdict_json", id, step.Sequence, string(data)); err != nil {
return err
}
}
if step.SessionExport != nil {
data, err := json.Marshal(step.SessionExport)
if err != nil {
return err
}
if _, err := tx.ExecContext(ctx, "INSERT INTO step_session_exports(run_id,sequence,export_json) VALUES(?,?,?)", id, step.Sequence, string(data)); err != nil {
return err
}
}
// The reported usage the stage already measured for budget accounting is
// persisted as step-scoped evidence in the same transaction, mirroring the
// other step evidence. It is written only when the stage actually reported
// usage, so a historical or non-model step stays absent rather than zero.
if step.Usage != nil {
data, err := json.Marshal(step.Usage)
if err != nil {
return err
}
if _, err := tx.ExecContext(ctx, "INSERT INTO step_usage(run_id,sequence,usage_json) VALUES(?,?,?)", id, step.Sequence, string(data)); err != nil {
return err
}
}
return tx.Commit()
}
// MarkWaiting persists a human stage's pending state in one transaction: the
// step becomes waiting with self-contained evidence (its operator instructions
// and ordered options) and the run reports the same key it is waiting on. It
// leaves finished_at NULL for both and adds no goroutine, timer or new column.
func (s *Store) MarkWaiting(ctx context.Context, id int64, sequence int, key string, evidence map[string]any) error {
data, err := json.Marshal(evidence)
if err != nil {
return err
}
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
if err := affected(tx.ExecContext(ctx, `UPDATE run_steps SET status=?,output_json=? WHERE run_id=? AND sequence=? AND status='running'`,
protocol.StepStatusWaiting, string(data), id, sequence)); err != nil {
return err
}
if err := affected(tx.ExecContext(ctx, `UPDATE pipeline_runs SET status=?,current_stage=? WHERE id=? AND status IN ('queued','running')`,
protocol.RunStatusWaiting, key, id)); err != nil {
return err
}
return tx.Commit()
}
// ResolveDecision records an operator's choice for a waiting human stage and
// routes the run along that option's edge, all in one short transaction. It
// reads the pinned graph and the pending step from persisted rows, so it is
// correct after a restart and needs no live goroutine. Validation is idempotent:
// a repeated decision for an already-decided visit with the same option returns
// applied=false and no error, an unknown option is refused with the valid list,
// and a stage that is not waiting is refused with its current state.
func (s *Store) ResolveDecision(ctx context.Context, id int64, stageKey, option, feedback string) (protocol.RunStep, bool, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return protocol.RunStep{}, false, err
}
defer tx.Rollback()
var status, currentStage, graphJSON string
if err := tx.QueryRowContext(ctx, `SELECT r.status,r.current_stage,v.graph_json FROM pipeline_runs r
JOIN pipeline_versions v ON v.pipeline_id=r.pipeline_id AND v.version=r.version WHERE r.id=?`, id).Scan(&status, &currentStage, &graphJSON); err != nil {
if err == sql.ErrNoRows {
return protocol.RunStep{}, false, fmt.Errorf("run not found")
}
return protocol.RunStep{}, false, err
}
if stageKey == "" {
stageKey = currentStage
// Prefer the actual waiting step's stage; current_stage is normally the
// waiting stage, but this keeps the decision correct if it ever drifts.
var waitingKey string
if err := tx.QueryRowContext(ctx, `SELECT stage_key FROM run_steps WHERE run_id=? AND status=? ORDER BY sequence DESC LIMIT 1`, id, protocol.StepStatusWaiting).Scan(&waitingKey); err == nil {
stageKey = waitingKey
} else if err != sql.ErrNoRows {
return protocol.RunStep{}, false, err
}
}
var graph protocol.Graph
if err := json.Unmarshal([]byte(graphJSON), &graph); err != nil {
return protocol.RunStep{}, false, err
}
var edges map[string]string
found := false
for _, declared := range graph.Stages {
if declared.Key == stageKey {
edges = declared.Edges
found = true
break
}
}
if !found {
return protocol.RunStep{}, false, fmt.Errorf("stage %q is not part of this run", stageKey)
}
options := stage.HumanOptions(edges)
valid := false
for _, candidate := range options {
if candidate == option {
valid = true
break
}
}
if !valid {
return protocol.RunStep{}, false, fmt.Errorf("unknown option %q for stage %s; valid options: %s", option, stageKey, strings.Join(options, ", "))
}
var sequence int
var stepStatus, stepOutcome string
err = tx.QueryRowContext(ctx, `SELECT sequence,status,outcome FROM run_steps WHERE run_id=? AND stage_key=? ORDER BY sequence DESC LIMIT 1`, id, stageKey).Scan(&sequence, &stepStatus, &stepOutcome)
if err == sql.ErrNoRows {
return protocol.RunStep{}, false, fmt.Errorf("stage %s is not waiting", stageKey)
}
if err != nil {
return protocol.RunStep{}, false, err
}
if stepStatus != protocol.StepStatusWaiting {
if stepStatus == "completed" && stepOutcome == option {
return protocol.RunStep{Sequence: sequence, Stage: stageKey, Status: "completed", Outcome: stepOutcome, Target: edges[stepOutcome]}, false, nil
}
return protocol.RunStep{}, false, fmt.Errorf("stage %s is not waiting (state %s)", stageKey, stepStatus)
}
if status != protocol.RunStatusWaiting && status != protocol.RunStatusRunning {
return protocol.RunStep{}, false, fmt.Errorf("run %d is not waiting (state %s)", id, status)
}
target := edges[option]
// The operator's choice and feedback are the step's evidence. The feedback
// stays in the output only - never the step diagnostic - so a deliberate,
// successful decision is not presented as a stop or error; the derived
// verdict reads the feedback as the step's reason.
output := map[string]any{protocol.DecisionOptionKey: option, protocol.DecisionFeedbackKey: feedback}
encoded, err := json.Marshal(output)
if err != nil {
return protocol.RunStep{}, false, err
}
if err := affected(tx.ExecContext(ctx, `UPDATE run_steps SET status='completed',outcome=?,target=?,output_json=?,finished_at=CURRENT_TIMESTAMP
WHERE run_id=? AND sequence=? AND status=?`, option, target, string(encoded), id, sequence, protocol.StepStatusWaiting)); err != nil {
return protocol.RunStep{}, false, err
}
if err := affected(tx.ExecContext(ctx, `UPDATE pipeline_runs SET status='running' WHERE id=? AND status IN (?,?)`, id, protocol.RunStatusWaiting, protocol.RunStatusRunning)); err != nil {
return protocol.RunStep{}, false, err
}
if err := tx.Commit(); err != nil {
return protocol.RunStep{}, false, err
}
return protocol.RunStep{Sequence: sequence, Stage: stageKey, Status: "completed", Outcome: option, Target: target, Output: output}, true, nil
}
// CancelWaiting cancels a run that is waiting on a person without a live
// goroutine. It is a single transaction that marks the run canceled and its
// waiting step canceled; a run that is not waiting is reported, not silently
// ignored.
func (s *Store) CancelWaiting(ctx context.Context, id int64) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
res, err := tx.ExecContext(ctx, `UPDATE pipeline_runs SET status='canceled',finished_at=CURRENT_TIMESTAMP WHERE id=? AND status=?`, id, protocol.RunStatusWaiting)
if err != nil {
return err
}
n, err := res.RowsAffected()
if err != nil {
return err
}
if n != 1 {
return fmt.Errorf("run is not active or waiting")
}
if _, err := tx.ExecContext(ctx, `UPDATE run_steps SET status='canceled',diagnostic='Canceled while waiting for a decision',finished_at=CURRENT_TIMESTAMP WHERE run_id=? AND status=?`, id, protocol.StepStatusWaiting); err != nil {
return err
}
return tx.Commit()
}
func (s *Store) FinishRun(ctx context.Context, id int64, status, outcome, diagnostic string, output map[string]any) error {
data, err := json.Marshal(output)
if err != nil {
return err
}
return affected(s.db.ExecContext(ctx, "UPDATE pipeline_runs SET status=?,outcome=?,diagnostic=?,output_json=?,finished_at=CURRENT_TIMESTAMP WHERE id=? AND status IN ('queued','running')", status, outcome, diagnostic, string(data), id))
}
// ActiveAgentSessionsForRun returns the distinct active agent session ids that
// belong to one run. It is scoped to run_steps.run_id so concurrent runs never
// observe each other's sessions: run_steps is keyed by (run_id, sequence), so
// this is one indexed lookup, not a scan.
func (s *Store) ActiveAgentSessionsForRun(ctx context.Context, runID int64) ([]string, error) {
rows, err := s.db.QueryContext(ctx, "SELECT DISTINCT session_id FROM run_steps WHERE run_id=? AND agent_active=1 AND session_id<>''", runID)
if err != nil {
return nil, err
}
defer rows.Close()
ids := []string{}
for rows.Next() {
var id string
if err := rows.Scan(&id); err != nil {
return nil, err
}
ids = append(ids, id)
}
return ids, rows.Err()
}
func (s *Store) RecoverRuns(ctx context.Context) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
if _, err := tx.ExecContext(ctx, "UPDATE run_steps SET status='interrupted',agent_active=0,diagnostic='Server stopped; stage was not resumed',finished_at=CURRENT_TIMESTAMP WHERE (status='running' OR agent_active=1) AND status<>?", protocol.StepStatusWaiting); err != nil {
return err
}
if _, err := tx.ExecContext(ctx, "UPDATE pipeline_runs SET status='interrupted',diagnostic='Server stopped; run was not resumed',finished_at=CURRENT_TIMESTAMP WHERE status IN ('queued','running') AND status<>?", protocol.RunStatusWaiting); err != nil {
return err
}
return tx.Commit()
}
+132
View File
@@ -0,0 +1,132 @@
package pipeline
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"strings"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/summarise"
)
// RunOverrides are the launch-time model overrides recorded for a run:
// Model applies to every model stage and StageModels overrides individual model
// stages by key (per-stage wins over whole-run). They are recorded so the run
// stays reproducible without touching the pinned pipeline version.
type RunOverrides struct {
Model string
StageModels map[string]string
}
// CreateRun inserts a run and derives its title from the prompt. It is the entry
// point for callers with no generated title; the derived fallback is always
// non-empty and never calls a model.
func (s *Store) CreateRun(ctx context.Context, d *protocol.PipelineDetail, key, workspace string, tools bool, paths []string, input map[string]any, retryOf ...int64) (int64, bool, error) {
return s.CreateRunTitled(ctx, d, key, workspace, tools, paths, input, RunOverrides{}, summarise.Title{}, retryOf...)
}
// CreateRunTitled inserts a run with the caller's title and launch-time model
// overrides. A non-empty title (a model generation) is stored verbatim; an empty
// one falls back to the prompt-derived title, so a run never stores an empty
// title. The overrides are compared in the request-key idempotency check, so a
// replay with different overrides is refused as a different run.
func (s *Store) CreateRunTitled(ctx context.Context, d *protocol.PipelineDetail, key, workspace string, tools bool, paths []string, input map[string]any, overrides RunOverrides, title summarise.Title, retryOf ...int64) (int64, bool, error) {
var parent int64
if len(retryOf) > 0 {
parent = retryOf[0]
}
if parent < 0 {
return 0, false, fmt.Errorf("invalid retry parent")
}
title = resolveRunTitle(title, input)
encoded, err := json.Marshal(input)
if err != nil {
return 0, false, err
}
writes, err := json.Marshal(paths)
if err != nil {
return 0, false, err
}
stageModels := overrides.StageModels
if stageModels == nil {
stageModels = map[string]string{}
}
encodedModels, err := json.Marshal(stageModels)
if err != nil {
return 0, false, err
}
s.mu.Lock()
defer s.mu.Unlock()
var id, pid int64
var version int
var oldInput, oldWorkspace, oldPaths, oldModel, oldStageModels string
var oldTools bool
err = s.db.QueryRowContext(ctx, "SELECT id,pipeline_id,version,input_json,workspace,write_paths,allow_tools,model,stage_models FROM pipeline_runs WHERE request_key=?", key).Scan(&id, &pid, &version, &oldInput, &oldWorkspace, &oldPaths, &oldTools, &oldModel, &oldStageModels)
if err == nil {
var oldParent int64
if err := s.db.QueryRowContext(ctx, "SELECT COALESCE((SELECT original_run_id FROM run_retries WHERE run_id=?),0)", id).Scan(&oldParent); err != nil {
return 0, false, err
}
if pid != d.ID || version != d.Version || oldInput != string(encoded) || oldWorkspace != workspace || oldPaths != string(writes) || oldTools != tools || oldParent != parent || oldModel != overrides.Model || oldStageModels != string(encodedModels) {
return 0, false, fmt.Errorf("request key already used for a different run")
}
return id, false, nil
}
if err != sql.ErrNoRows {
return 0, false, err
}
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return 0, false, err
}
defer tx.Rollback()
result, err := tx.ExecContext(ctx, "INSERT INTO pipeline_runs(pipeline_id,version,request_key,workspace,allow_tools,write_paths,input_json,title,title_source,title_model,model,stage_models) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", d.ID, d.Version, key, workspace, tools, string(writes), string(encoded), title.Text, title.Source, title.Model, overrides.Model, string(encodedModels))
if err != nil {
return 0, false, err
}
id, err = result.LastInsertId()
if err != nil {
return 0, false, err
}
if parent > 0 {
if _, err := tx.ExecContext(ctx, "INSERT INTO run_retries(run_id,original_run_id) VALUES(?,?)", id, parent); err != nil {
return 0, false, err
}
}
if err := tx.Commit(); err != nil {
return 0, false, err
}
return id, true, nil
}
// resolveRunTitle guarantees a stored run title is never empty: a title supplied
// by the caller (a model generation) is kept verbatim, and an absent one is
// derived from the prompt's first line. The fallback is server-owned and never
// calls a model, so it can never block or fail run creation.
func resolveRunTitle(title summarise.Title, input map[string]any) summarise.Title {
if strings.TrimSpace(title.Text) != "" {
return title
}
prompt, _ := input["prompt"].(string)
return summarise.Title{Text: summarise.DeriveTitle(prompt), Source: summarise.SourceFallback}
}
// SetRunTitle replaces a run's operator-facing title and its provenance without
// touching the run's input, steps or execution. It is how an operator renames a
// run after the fact.
func (s *Store) SetRunTitle(ctx context.Context, id int64, title summarise.Title) error {
result, err := s.db.ExecContext(ctx, "UPDATE pipeline_runs SET title=?,title_source=?,title_model=? WHERE id=?", title.Text, title.Source, title.Model, id)
if err != nil {
return err
}
n, err := result.RowsAffected()
if err != nil {
return err
}
if n != 1 {
return fmt.Errorf("run not found")
}
return nil
}
+314
View File
@@ -0,0 +1,314 @@
package pipeline
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"strings"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
)
func (s *Store) SaveNotifications(ctx context.Context, id int64, sequence int, receipts []protocol.NotificationReceipt) error {
data, err := json.Marshal(receipts)
if err != nil {
return err
}
_, err = s.db.ExecContext(ctx, `INSERT INTO step_notifications(run_id,sequence,receipts_json) VALUES(?,?,?) ON CONFLICT(run_id,sequence) DO UPDATE SET receipts_json=excluded.receipts_json`, id, sequence, string(data))
return err
}
func affected(result sql.Result, err error) error {
if err != nil {
return err
}
n, err := result.RowsAffected()
if err != nil {
return err
}
if n != 1 {
return fmt.Errorf("invalid run state transition")
}
return nil
}
func (s *Store) BeginStep(ctx context.Context, id int64, sequence int, key string, input map[string]any) error {
encoded, err := json.Marshal(input)
if err != nil {
return err
}
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
if err := affected(tx.ExecContext(ctx, "UPDATE pipeline_runs SET status='running',current_stage=? WHERE id=? AND status IN ('queued','running')", key, id)); err != nil {
return err
}
if _, err := tx.ExecContext(ctx, "INSERT INTO run_steps(run_id,sequence,stage_key,input_json) VALUES (?,?,?,?)", id, sequence, key, string(encoded)); err != nil {
return err
}
return tx.Commit()
}
// stageArtifactsEvidence is the persisted per-step artifact list plus its
// bounded-scan counters; it is kept in a separate table like the other
// step-scoped evidence so run_steps needs no schema migration on old databases.
type stageArtifactsEvidence struct {
Files []protocol.StageArtifact `json:"files"`
Skipped int `json:"skipped,omitempty"`
Truncated int `json:"truncated,omitempty"`
}
// stageVerdictEvidence is the generated one-line verdict reason plus its
// provenance. Like the other step-scoped evidence it lives in its own table, so
// run_steps needs no schema migration and an old database gains the empty table
// on Open with no backfill. Only a model-generated line is ever stored; the
// read-time fallback is always derived, so existing runs still show a verdict.
type stageVerdictEvidence struct {
Reason string `json:"reason"`
Source string `json:"source"`
}
func (s *Store) FinishStep(ctx context.Context, id int64, step protocol.RunStep) error {
output, err := json.Marshal(step.Output)
if err != nil {
return err
}
// Bound the stored transcript without corrupting structured JSON. A large
// transcript becomes a valid bounded preview envelope preserving stage_limits
// and failure/format diagnostics; the exact Output is saved separately below.
step.Transcript = stage.BoundTranscript(step.Transcript, step.SessionID)
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
if err := affected(tx.ExecContext(ctx, `UPDATE run_steps SET status=?,outcome=?,target=?,output_json=?,diagnostic=?,transcript=?,provider=?,model=?,finished_at=CURRENT_TIMESTAMP
WHERE run_id=? AND sequence=? AND status='running'`, step.Status, step.Outcome, step.Target, string(output), step.Diagnostic, step.Transcript, step.Provider, step.Model, id, step.Sequence)); err != nil {
return err
}
if step.FileChanges != nil {
data, err := json.Marshal(step.FileChanges)
if err != nil {
return err
}
if _, err = tx.ExecContext(ctx, "INSERT INTO step_file_changes(run_id,sequence,changes_json) VALUES(?,?,?)", id, step.Sequence, string(data)); err != nil {
return err
}
}
if step.Control != nil {
data, err := json.Marshal(step.Control)
if err != nil {
return err
}
if _, err := tx.ExecContext(ctx, "INSERT INTO step_control(run_id,sequence,evidence_json) VALUES(?,?,?)", id, step.Sequence, string(data)); err != nil {
return err
}
}
if len(step.Artifacts) > 0 || step.ArtifactsSkipped > 0 || step.ArtifactsTruncated > 0 {
data, err := json.Marshal(stageArtifactsEvidence{Files: step.Artifacts, Skipped: step.ArtifactsSkipped, Truncated: step.ArtifactsTruncated})
if err != nil {
return err
}
if _, err := tx.ExecContext(ctx, "INSERT INTO step_artifacts(run_id,sequence,artifacts_json) VALUES(?,?,?)", id, step.Sequence, string(data)); err != nil {
return err
}
}
// Persist only a generated model line and its provenance; the read-time
// fallback is always derived in GetRun, so old runs stay intact.
if step.Verdict != nil && strings.TrimSpace(step.Verdict.Reason) != "" {
data, err := json.Marshal(stageVerdictEvidence{Reason: step.Verdict.Reason, Source: step.Verdict.ReasonSource})
if err != nil {
return err
}
if _, err := tx.ExecContext(ctx, "INSERT INTO step_verdicts(run_id,sequence,verdict_json) VALUES(?,?,?) ON CONFLICT(run_id,sequence) DO UPDATE SET verdict_json=excluded.verdict_json", id, step.Sequence, string(data)); err != nil {
return err
}
}
if step.SessionExport != nil {
data, err := json.Marshal(step.SessionExport)
if err != nil {
return err
}
if _, err := tx.ExecContext(ctx, "INSERT INTO step_session_exports(run_id,sequence,export_json) VALUES(?,?,?)", id, step.Sequence, string(data)); err != nil {
return err
}
}
// The reported usage the stage already measured for budget accounting is
// persisted as step-scoped evidence in the same transaction, mirroring the
// other step evidence. It is written only when the stage actually reported
// usage, so a historical or non-model step stays absent rather than zero.
if step.Usage != nil {
data, err := json.Marshal(step.Usage)
if err != nil {
return err
}
if _, err := tx.ExecContext(ctx, "INSERT INTO step_usage(run_id,sequence,usage_json) VALUES(?,?,?)", id, step.Sequence, string(data)); err != nil {
return err
}
}
return tx.Commit()
}
// MarkWaiting persists a human stage's pending state in one transaction: the
// step becomes waiting with self-contained evidence (its operator instructions
// and ordered options) and the run reports the same key it is waiting on. It
// leaves finished_at NULL for both and adds no goroutine, timer or new column.
func (s *Store) MarkWaiting(ctx context.Context, id int64, sequence int, key string, evidence map[string]any) error {
data, err := json.Marshal(evidence)
if err != nil {
return err
}
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
if err := affected(tx.ExecContext(ctx, `UPDATE run_steps SET status=?,output_json=? WHERE run_id=? AND sequence=? AND status='running'`,
protocol.StepStatusWaiting, string(data), id, sequence)); err != nil {
return err
}
if err := affected(tx.ExecContext(ctx, `UPDATE pipeline_runs SET status=?,current_stage=? WHERE id=? AND status IN ('queued','running')`,
protocol.RunStatusWaiting, key, id)); err != nil {
return err
}
return tx.Commit()
}
// ResolveDecision records an operator's choice for a waiting human stage and
// routes the run along that option's edge, all in one short transaction. It
// reads the pinned graph and the pending step from persisted rows, so it is
// correct after a restart and needs no live goroutine. Validation is idempotent:
// a repeated decision for an already-decided visit with the same option returns
// applied=false and no error, an unknown option is refused with the valid list,
// and a stage that is not waiting is refused with its current state.
func (s *Store) ResolveDecision(ctx context.Context, id int64, stageKey, option, feedback string) (protocol.RunStep, bool, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return protocol.RunStep{}, false, err
}
defer tx.Rollback()
var status, currentStage, graphJSON string
if err := tx.QueryRowContext(ctx, `SELECT r.status,r.current_stage,v.graph_json FROM pipeline_runs r
JOIN pipeline_versions v ON v.pipeline_id=r.pipeline_id AND v.version=r.version WHERE r.id=?`, id).Scan(&status, &currentStage, &graphJSON); err != nil {
if err == sql.ErrNoRows {
return protocol.RunStep{}, false, fmt.Errorf("run not found")
}
return protocol.RunStep{}, false, err
}
if stageKey == "" {
stageKey = currentStage
// Prefer the actual waiting step's stage; current_stage is normally the
// waiting stage, but this keeps the decision correct if it ever drifts.
var waitingKey string
if err := tx.QueryRowContext(ctx, `SELECT stage_key FROM run_steps WHERE run_id=? AND status=? ORDER BY sequence DESC LIMIT 1`, id, protocol.StepStatusWaiting).Scan(&waitingKey); err == nil {
stageKey = waitingKey
} else if err != sql.ErrNoRows {
return protocol.RunStep{}, false, err
}
}
var graph protocol.Graph
if err := json.Unmarshal([]byte(graphJSON), &graph); err != nil {
return protocol.RunStep{}, false, err
}
var edges map[string]string
found := false
for _, declared := range graph.Stages {
if declared.Key == stageKey {
edges = declared.Edges
found = true
break
}
}
if !found {
return protocol.RunStep{}, false, fmt.Errorf("stage %q is not part of this run", stageKey)
}
options := stage.HumanOptions(edges)
valid := false
for _, candidate := range options {
if candidate == option {
valid = true
break
}
}
if !valid {
return protocol.RunStep{}, false, fmt.Errorf("unknown option %q for stage %s; valid options: %s", option, stageKey, strings.Join(options, ", "))
}
var sequence int
var stepStatus, stepOutcome string
err = tx.QueryRowContext(ctx, `SELECT sequence,status,outcome FROM run_steps WHERE run_id=? AND stage_key=? ORDER BY sequence DESC LIMIT 1`, id, stageKey).Scan(&sequence, &stepStatus, &stepOutcome)
if err == sql.ErrNoRows {
return protocol.RunStep{}, false, fmt.Errorf("stage %s is not waiting", stageKey)
}
if err != nil {
return protocol.RunStep{}, false, err
}
if stepStatus != protocol.StepStatusWaiting {
if stepStatus == "completed" && stepOutcome == option {
return protocol.RunStep{Sequence: sequence, Stage: stageKey, Status: "completed", Outcome: stepOutcome, Target: edges[stepOutcome]}, false, nil
}
return protocol.RunStep{}, false, fmt.Errorf("stage %s is not waiting (state %s)", stageKey, stepStatus)
}
if status != protocol.RunStatusWaiting && status != protocol.RunStatusRunning {
return protocol.RunStep{}, false, fmt.Errorf("run %d is not waiting (state %s)", id, status)
}
target := edges[option]
// The operator's choice and feedback are the step's evidence. The feedback
// stays in the output only - never the step diagnostic - so a deliberate,
// successful decision is not presented as a stop or error; the derived
// verdict reads the feedback as the step's reason.
output := map[string]any{protocol.DecisionOptionKey: option, protocol.DecisionFeedbackKey: feedback}
encoded, err := json.Marshal(output)
if err != nil {
return protocol.RunStep{}, false, err
}
if err := affected(tx.ExecContext(ctx, `UPDATE run_steps SET status='completed',outcome=?,target=?,output_json=?,finished_at=CURRENT_TIMESTAMP
WHERE run_id=? AND sequence=? AND status=?`, option, target, string(encoded), id, sequence, protocol.StepStatusWaiting)); err != nil {
return protocol.RunStep{}, false, err
}
if err := affected(tx.ExecContext(ctx, `UPDATE pipeline_runs SET status='running' WHERE id=? AND status IN (?,?)`, id, protocol.RunStatusWaiting, protocol.RunStatusRunning)); err != nil {
return protocol.RunStep{}, false, err
}
if err := tx.Commit(); err != nil {
return protocol.RunStep{}, false, err
}
return protocol.RunStep{Sequence: sequence, Stage: stageKey, Status: "completed", Outcome: option, Target: target, Output: output}, true, nil
}
// CancelWaiting cancels a run that is waiting on a person without a live
// goroutine. It is a single transaction that marks the run canceled and its
// waiting step canceled; a run that is not waiting is reported, not silently
// ignored.
func (s *Store) CancelWaiting(ctx context.Context, id int64) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
res, err := tx.ExecContext(ctx, `UPDATE pipeline_runs SET status='canceled',finished_at=CURRENT_TIMESTAMP WHERE id=? AND status=?`, id, protocol.RunStatusWaiting)
if err != nil {
return err
}
n, err := res.RowsAffected()
if err != nil {
return err
}
if n != 1 {
return fmt.Errorf("run is not active or waiting")
}
if _, err := tx.ExecContext(ctx, `UPDATE run_steps SET status='canceled',diagnostic='Canceled while waiting for a decision',finished_at=CURRENT_TIMESTAMP WHERE run_id=? AND status=?`, id, protocol.StepStatusWaiting); err != nil {
return err
}
return tx.Commit()
}
func (s *Store) FinishRun(ctx context.Context, id int64, status, outcome, diagnostic string, output map[string]any) error {
data, err := json.Marshal(output)
if err != nil {
return err
}
return affected(s.db.ExecContext(ctx, "UPDATE pipeline_runs SET status=?,outcome=?,diagnostic=?,output_json=?,finished_at=CURRENT_TIMESTAMP WHERE id=? AND status IN ('queued','running')", status, outcome, diagnostic, string(data), id))
}
+137
View File
@@ -0,0 +1,137 @@
package pipeline
import (
"context"
"database/sql"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func (s *Store) initRuns() error {
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS pipeline_runs (
id INTEGER PRIMARY KEY, pipeline_id INTEGER NOT NULL, version INTEGER NOT NULL,
request_key TEXT NOT NULL UNIQUE, workspace TEXT NOT NULL, allow_tools INTEGER NOT NULL,
title TEXT NOT NULL DEFAULT '', title_source TEXT NOT NULL DEFAULT '', title_model TEXT NOT NULL DEFAULT '',
model TEXT NOT NULL DEFAULT '', stage_models TEXT NOT NULL DEFAULT '{}',
write_paths TEXT NOT NULL, input_json TEXT NOT NULL, output_json TEXT NOT NULL DEFAULT '{}',
status TEXT NOT NULL DEFAULT 'queued', outcome TEXT NOT NULL DEFAULT '', current_stage TEXT NOT NULL DEFAULT '',
diagnostic TEXT NOT NULL DEFAULT '', created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, finished_at TEXT,
FOREIGN KEY(pipeline_id,version) REFERENCES pipeline_versions(pipeline_id,version));
CREATE TABLE IF NOT EXISTS run_steps (
run_id INTEGER NOT NULL REFERENCES pipeline_runs(id), sequence INTEGER NOT NULL, stage_key TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'running', outcome TEXT NOT NULL DEFAULT '', target TEXT NOT NULL DEFAULT '',
input_json TEXT NOT NULL, output_json TEXT NOT NULL DEFAULT '{}', diagnostic TEXT NOT NULL DEFAULT '',
provider TEXT NOT NULL DEFAULT '', model TEXT NOT NULL DEFAULT '',
session_id TEXT NOT NULL DEFAULT '', agent_active INTEGER NOT NULL DEFAULT 0, transcript TEXT NOT NULL DEFAULT '',
started_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, finished_at TEXT, PRIMARY KEY(run_id,sequence));
CREATE INDEX IF NOT EXISTS run_latest ON pipeline_runs(id DESC);
CREATE INDEX IF NOT EXISTS run_steps_session ON run_steps(session_id);
CREATE TABLE IF NOT EXISTS step_notifications (
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, receipts_json TEXT NOT NULL,
PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence));
CREATE TABLE IF NOT EXISTS step_file_changes (
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, changes_json TEXT NOT NULL,
PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence));
CREATE TABLE IF NOT EXISTS step_control (
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, evidence_json TEXT NOT NULL,
PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence));
CREATE TABLE IF NOT EXISTS step_artifacts (
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, artifacts_json TEXT NOT NULL,
PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence));
CREATE TABLE IF NOT EXISTS step_verdicts (
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, verdict_json TEXT NOT NULL,
PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence));
CREATE TABLE IF NOT EXISTS step_session_exports (
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, export_json TEXT NOT NULL,
PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence));
CREATE TABLE IF NOT EXISTS step_usage (
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, usage_json TEXT NOT NULL,
PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence));
CREATE TABLE IF NOT EXISTS run_session_owners (
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, backend TEXT NOT NULL, reference TEXT NOT NULL DEFAULT '',
PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence));
CREATE TABLE IF NOT EXISTS run_retries (
run_id INTEGER PRIMARY KEY REFERENCES pipeline_runs(id), original_run_id INTEGER NOT NULL REFERENCES pipeline_runs(id));`)
if err != nil {
return err
}
if err := s.migrateRunTitles(); err != nil {
return err
}
return s.migrateRunModels()
}
// migrateRunModels adds the launch-time model-override columns to a database
// created before per-run overrides existed. Each column is added only when
// missing, so an upgrade is idempotent across repeated Open calls and existing
// rows keep their defaults (no model override, no recorded step model).
func (s *Store) migrateRunModels() error {
for _, column := range [][3]string{
{"pipeline_runs", "model", "TEXT NOT NULL DEFAULT ''"},
{"pipeline_runs", "stage_models", "TEXT NOT NULL DEFAULT '{}'"},
{"run_steps", "provider", "TEXT NOT NULL DEFAULT ''"},
{"run_steps", "model", "TEXT NOT NULL DEFAULT ''"},
} {
if err := s.addColumnIfMissing(column[0], column[1], column[2]); err != nil {
return err
}
}
return nil
}
// migrateRunTitles adds the title columns to a database created before run
// titles existed. Adding a column with a default leaves every existing row valid
// and untouched, so old runs still list.
func (s *Store) migrateRunTitles() error {
for _, column := range [][2]string{
{"title", "TEXT NOT NULL DEFAULT ''"},
{"title_source", "TEXT NOT NULL DEFAULT ''"},
{"title_model", "TEXT NOT NULL DEFAULT ''"},
} {
if err := s.addColumnIfMissing("pipeline_runs", column[0], column[1]); err != nil {
return err
}
}
return nil
}
// addColumnIfMissing adds a column only when the table predates it, making an
// upgrade idempotent across repeated Open calls without rewriting history.
func (s *Store) addColumnIfMissing(table, column, definition string) error {
rows, err := s.db.Query("PRAGMA table_info(" + table + ")")
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var cid, notnull, pk int
var name, ctype string
var dflt sql.NullString
if err := rows.Scan(&cid, &name, &ctype, &notnull, &dflt, &pk); err != nil {
return err
}
if name == column {
return nil
}
}
if err := rows.Err(); err != nil {
return err
}
_, err = s.db.Exec("ALTER TABLE " + table + " ADD COLUMN " + column + " " + definition)
return err
}
func (s *Store) RecoverRuns(ctx context.Context) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
if _, err := tx.ExecContext(ctx, "UPDATE run_steps SET status='interrupted',agent_active=0,diagnostic='Server stopped; stage was not resumed',finished_at=CURRENT_TIMESTAMP WHERE (status='running' OR agent_active=1) AND status<>?", protocol.StepStatusWaiting); err != nil {
return err
}
if _, err := tx.ExecContext(ctx, "UPDATE pipeline_runs SET status='interrupted',diagnostic='Server stopped; run was not resumed',finished_at=CURRENT_TIMESTAMP WHERE status IN ('queued','running') AND status<>?", protocol.RunStatusWaiting); err != nil {
return err
}
return tx.Commit()
}
+122
View File
@@ -0,0 +1,122 @@
package pipeline
import (
"context"
)
func (s *Store) RunSession(ctx context.Context, id int64, sequence int, session string, active bool, metadata ...string) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
if err = affected(tx.ExecContext(ctx, "UPDATE run_steps SET session_id=?,agent_active=? WHERE run_id=? AND sequence=? AND status='running'", session, active, id, sequence)); err != nil {
return err
}
if len(metadata) > 0 {
backend := metadata[0]
if backend == "" {
backend = "v2"
}
reference := ""
if len(metadata) > 1 {
reference = metadata[1]
}
if _, err = tx.ExecContext(ctx, "INSERT INTO run_session_owners(run_id,sequence,backend,reference) VALUES(?,?,?,?) ON CONFLICT(run_id,sequence) DO UPDATE SET backend=excluded.backend,reference=excluded.reference", id, sequence, backend, reference); err != nil {
return err
}
}
return tx.Commit()
}
type SessionOwner struct {
ID string
Backend string
Directory string
Reference string
// RunID is the owning run, used to tell an orphaned session (no active run)
// from one owned by a run that is active right now.
RunID int64
// Active is true while the owning step records the session as agent-active.
Active bool
}
func (s *Store) RunSessionOwner(ctx context.Context, id int64, sequence int) (SessionOwner, error) {
var owner SessionOwner
err := s.db.QueryRowContext(ctx, `SELECT s.session_id,COALESCE(o.backend,'v2'),r.workspace,COALESCE(o.reference,'') FROM run_steps s JOIN pipeline_runs r ON r.id=s.run_id LEFT JOIN run_session_owners o ON o.run_id=s.run_id AND o.sequence=s.sequence WHERE s.run_id=? AND s.sequence=?`, id, sequence).Scan(&owner.ID, &owner.Backend, &owner.Directory, &owner.Reference)
return owner, err
}
// sessionOwnerScanLimit bounds the ownership scan so a long-lived database with
// thousands of historical sessions cannot make one chat_sessions request scan
// them all. Rows are read newest-run first, so the bound only ever drops the
// oldest owners, never the sessions a client can still be writing.
const sessionOwnerScanLimit = 1024
// ActiveSessionOwners reports every currently active agent session, deduped by
// session id (newest owning run wins).
func (s *Store) ActiveSessionOwners(ctx context.Context) ([]SessionOwner, error) {
return s.sessionOwners(ctx, true)
}
// OwnedSessions reports every session the server ever recorded in run_steps,
// deduped by session id (newest owning run wins) and including inactive ones, so
// chat_sessions can be restricted to sessions this server created.
func (s *Store) OwnedSessions(ctx context.Context) ([]SessionOwner, error) {
return s.sessionOwners(ctx, false)
}
// sessionOwners is the single ownership query both ActiveSessionOwners and
// OwnedSessions run, so the SQL is not duplicated. It reads one bounded,
// newest-run-first window and drops repeated session ids in favour of the most
// recent owning run's directory and active flag.
func (s *Store) sessionOwners(ctx context.Context, activeOnly bool) ([]SessionOwner, error) {
query := `SELECT s.session_id,COALESCE(o.backend,'v2'),r.workspace,COALESCE(o.reference,''),s.run_id,s.agent_active
FROM run_steps s JOIN pipeline_runs r ON r.id=s.run_id LEFT JOIN run_session_owners o ON o.run_id=s.run_id AND o.sequence=s.sequence WHERE s.session_id<>''`
if activeOnly {
query += " AND s.agent_active=1"
}
query += " ORDER BY s.run_id DESC LIMIT ?"
rows, err := s.db.QueryContext(ctx, query, sessionOwnerScanLimit)
if err != nil {
return nil, err
}
defer rows.Close()
seen := map[string]struct{}{}
var owners []SessionOwner
for rows.Next() {
var owner SessionOwner
var active int
if err = rows.Scan(&owner.ID, &owner.Backend, &owner.Directory, &owner.Reference, &owner.RunID, &active); err != nil {
return nil, err
}
owner.Active = active != 0
if _, ok := seen[owner.ID]; ok {
continue
}
seen[owner.ID] = struct{}{}
owners = append(owners, owner)
}
return owners, rows.Err()
}
// ActiveAgentSessionsForRun returns the distinct active agent session ids that
// belong to one run. It is scoped to run_steps.run_id so concurrent runs never
// observe each other's sessions: run_steps is keyed by (run_id, sequence), so
// this is one indexed lookup, not a scan.
func (s *Store) ActiveAgentSessionsForRun(ctx context.Context, runID int64) ([]string, error) {
rows, err := s.db.QueryContext(ctx, "SELECT DISTINCT session_id FROM run_steps WHERE run_id=? AND agent_active=1 AND session_id<>''", runID)
if err != nil {
return nil, err
}
defer rows.Close()
ids := []string{}
for rows.Next() {
var id string
if err := rows.Scan(&id); err != nil {
return nil, err
}
ids = append(ids, id)
}
return ids, rows.Err()
}
-890
View File
@@ -4,15 +4,7 @@ import (
"context"
"crypto/rand"
"encoding/hex"
"errors"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
@@ -54,32 +46,6 @@ type operatorRegistry interface {
OperatorDirectories(ctx context.Context, limit int) ([]string, error)
}
// chatPermissionPollInterval is the fixed interval between permission polls: one
// GET per poll through the shared Connection bounds, never a busy loop. The first
// poll runs immediately so a request pending when the prompt is sent is surfaced
// without waiting a full interval.
const chatPermissionPollInterval = 2 * time.Second
// chatPermissionRetainedLimit bounds the dedup set of already-pushed permission
// request ids one session retains, so a long turn cannot grow it without limit.
const chatPermissionRetainedLimit = 256
// chatRetainedLimit bounds the state one session may retain so a stalled reader
// or a runaway turn cannot grow memory without limit. It matches the OpenCode
// response bound (1 MiB).
const chatRetainedLimit = opencode.MaxResponse
// chatMaxTurns bounds how many sessions retain turn state. A long-lived server
// cannot accumulate state for every session ever prompted, so the
// least-recently-updated turn is evicted once the cap is exceeded.
const chatMaxTurns = 128
// operatorDirectoryLimit bounds the chat_directories choices: the operator home
// plus recent operator-session directories, capped at this small fixed number so
// a long history is never returned. It matches the posture of the other small
// fixed list bounds.
const operatorDirectoryLimit = 8
// chatClientToken returns an unguessable per-connection identity. It attributes
// a session claim so two clients on the same server cannot silently write the
// same conversation; a fresh token per connection means a reconnected client is
@@ -92,367 +58,6 @@ func chatClientToken() string {
return "conn-" + hex.EncodeToString(b[:])
}
// chatHub retains the accumulated turn state per session, independently of any
// single connection, so a reconnecting client can re-sync what it missed. State
// is keyed by session and then by message id (a session holds many turns), and
// is capped at chatRetainedLimit.
type chatHub struct {
mu sync.Mutex
turns map[string]*chatTurn
now func() time.Time
// claims maps a session to the client currently writing it so a second
// client is rejected; claimOrder bounds the map like chatMaxTurns.
claims map[string]string
claimOrder []string
}
// chatPart is the retained form of one part. Text and reasoning deltas append to
// text (amortized O(1)); a tool frame replaces part outright.
type chatPart struct {
part protocol.ChatPart
text []byte
}
// chatMessage is one retained assistant turn. Its parts keep the exact ids the
// supporting chat_event frames carried, so a resume preserves attribution.
type chatMessage struct {
id string
order []string
parts map[string]*chatPart
}
type chatTurn struct {
sessionID string
order []string
messages map[string]*chatMessage
step *protocol.ChatStep
usage *protocol.ChatUsage
size int
overflow bool
updated time.Time
// permissions holds the request ids already pushed to the client so a
// repeated poll cannot push the same pending request twice; permissionOrder
// bounds it to chatPermissionRetainedLimit.
permissions map[string]struct{}
permissionOrder []string
}
func newChatHub() *chatHub {
return &chatHub{turns: map[string]*chatTurn{}, now: time.Now}
}
func (h *chatHub) clock() time.Time {
if h.now != nil {
return h.now()
}
return time.Now()
}
// apply folds one pushed frame into the retained state. Text and reasoning
// deltas append to their message's part; a tool frame upserts the full part
// (accounting for its whole size) so the latest lifecycle status, input,
// content, metadata, error and time are retained under the frame's message id.
// Step and usage frames update the session's latest progress.
func (h *chatHub) apply(frame protocol.ChatEventFrame) {
if frame.SessionID == "" {
return
}
h.mu.Lock()
defer h.mu.Unlock()
turn := h.turnLocked(frame.SessionID)
switch frame.Kind {
case protocol.ChatEventStep:
turn.step = frame.Step
return
case protocol.ChatEventUsage:
turn.usage = frame.Usage
return
}
if frame.MessageID == "" {
return
}
message := turn.messages[frame.MessageID]
if message == nil {
message = &chatMessage{id: frame.MessageID, parts: map[string]*chatPart{}}
turn.order = append(turn.order, frame.MessageID)
turn.messages[frame.MessageID] = message
}
switch frame.Kind {
case protocol.ChatEventText, protocol.ChatEventReasoning:
id := ""
if frame.Part != nil {
id = frame.Part.ID
}
if id == "" || frame.Delta == "" {
return
}
if !turn.retain(len(frame.Delta)) {
return
}
part, ok := message.parts[id]
if !ok {
part = &chatPart{part: protocol.ChatPart{Type: frame.Kind, ID: id}}
message.order = append(message.order, id)
message.parts[id] = part
}
part.text = append(part.text, frame.Delta...)
case protocol.ChatEventTool:
if frame.Part == nil || frame.Part.ID == "" || turn.overflow {
return
}
// The tool part is retained whole, so size the complete part (input,
// content, metadata, error and time) and charge the difference against
// the cap on every upsert, not just the first-seen name.
incoming := chatPartBytes(frame.Part)
part, ok := message.parts[frame.Part.ID]
if !ok {
if !turn.retain(incoming) {
return
}
part = &chatPart{}
message.order = append(message.order, frame.Part.ID)
message.parts[frame.Part.ID] = part
} else if previous := chatPartBytes(&part.part); incoming > previous {
if !turn.retain(incoming - previous) {
return
}
} else {
turn.shrink(previous - incoming)
}
part.part = *frame.Part
}
}
// chatPartBytes estimates the retained size of a part so the per-session cap
// accounts for its whole state instead of only the first-seen name. It walks the
// fields rather than re-marshalling, so the cost matches the bytes it stores.
func chatPartBytes(part *protocol.ChatPart) int {
if part == nil {
return 0
}
n := len(part.Type) + len(part.ID) + len(part.Name)
if part.State == nil {
return n
}
n += len(part.State.Status) + len(part.State.Input)
for _, content := range part.State.Content {
n += len(content.Type) + len(content.Text) + len(content.URI) + len(content.Mime) + len(content.Name)
}
if part.State.Error != nil {
n += len(part.State.Error.Type) + len(part.State.Error.Message)
}
for key, value := range part.State.Metadata {
n += len(key)
if text, ok := value.(string); ok {
n += len(text)
} else {
n += 8
}
}
return n
}
// evictLocked drops the least-recently-updated turn when the session cap is
// reached, keeping retained memory bounded across session churn.
func (h *chatHub) evictLocked() {
if len(h.turns) < chatMaxTurns {
return
}
oldestID := ""
var oldest time.Time
for id, turn := range h.turns {
if oldestID == "" || turn.updated.Before(oldest) {
oldestID, oldest = id, turn.updated
}
}
if oldestID != "" {
delete(h.turns, oldestID)
}
}
// claim reserves sessionID for clientID. It reports true when the session is
// free or already held by the same client (so a client may re-prompt its own
// session) and false when another client holds it. The claim map is bounded by
// chatMaxTurns with oldest-eviction so a stuck client cannot grow it without
// limit.
func (h *chatHub) claim(sessionID, clientID string) bool {
if sessionID == "" || clientID == "" {
return false
}
h.mu.Lock()
defer h.mu.Unlock()
if h.claims == nil {
h.claims = map[string]string{}
}
if holder, ok := h.claims[sessionID]; ok {
return holder == clientID
}
h.evictClaimsLocked()
h.claims[sessionID] = clientID
h.claimOrder = append(h.claimOrder, sessionID)
return true
}
// release drops the same client's claim on sessionID; a claim held by a
// different client is left untouched so a stale release cannot free a live one.
func (h *chatHub) release(sessionID, clientID string) {
h.mu.Lock()
defer h.mu.Unlock()
if holder, ok := h.claims[sessionID]; !ok || holder != clientID {
return
}
delete(h.claims, sessionID)
for i, id := range h.claimOrder {
if id == sessionID {
h.claimOrder = append(h.claimOrder[:i], h.claimOrder[i+1:]...)
break
}
}
}
// busyForOther reports whether another client currently holds sessionID.
func (h *chatHub) busyForOther(sessionID, clientID string) bool {
h.mu.Lock()
defer h.mu.Unlock()
holder, ok := h.claims[sessionID]
return ok && holder != clientID
}
// evictClaimsLocked drops the oldest claim when the cap is reached, keeping the
// claim map bounded like the retained turns. Callers must hold h.mu.
func (h *chatHub) evictClaimsLocked() {
if len(h.claims) < chatMaxTurns {
return
}
for len(h.claimOrder) > 0 {
oldest := h.claimOrder[0]
h.claimOrder = h.claimOrder[1:]
if _, ok := h.claims[oldest]; ok {
delete(h.claims, oldest)
return
}
}
}
// turnLocked returns the session's retained turn, creating it (and evicting the
// least-recently-updated turn first when the cap is reached) and refreshing its
// update time. It is the shared turn-initialization path for apply and
// seenPermission, so the two cannot drift. Callers must hold h.mu.
func (h *chatHub) turnLocked(sessionID string) *chatTurn {
turn := h.turns[sessionID]
if turn == nil {
h.evictLocked()
turn = &chatTurn{sessionID: sessionID, messages: map[string]*chatMessage{}}
h.turns[sessionID] = turn
}
turn.updated = h.clock()
return turn
}
// retain records n more bytes and reports whether the turn is still within the
// bound. The first overflow records one diagnostic and then drops further state
// instead of growing without limit.
func (t *chatTurn) retain(n int) bool {
if t.overflow {
return false
}
if t.size+n > chatRetainedLimit {
t.overflow = true
log.Printf("chat session %s retained state exceeds %d bytes; dropping further state", t.sessionID, chatRetainedLimit)
return false
}
t.size += n
return true
}
// shrink releases n bytes when a tool upsert replaces a larger retained part.
func (t *chatTurn) shrink(n int) {
t.size -= n
if t.size < 0 {
t.size = 0
}
}
// snapshot returns the accumulated session as one assistant message per
// retained message id, with the same stable part ids, or nil when nothing is
// retained. It is the resume source: a reconnecting client re-syncs the whole
// session once instead of replaying deltas, and each turn keeps its own id.
func (h *chatHub) snapshot(sessionID string) []protocol.ChatMessage {
h.mu.Lock()
defer h.mu.Unlock()
turn := h.turns[sessionID]
if turn == nil {
return nil
}
turn.updated = h.clock()
var messages []protocol.ChatMessage
for _, id := range turn.order {
message := turn.messages[id]
if message == nil || len(message.order) == 0 {
continue
}
out := protocol.ChatMessage{ID: id, SessionID: sessionID, Role: protocol.ChatRoleAssistant}
for _, partID := range message.order {
if part, ok := message.parts[partID]; ok {
piece := part.part
piece.Text = string(part.text)
out.Parts = append(out.Parts, piece)
}
}
messages = append(messages, out)
}
return messages
}
// seenPermission records requestID as pushed for the session and reports whether
// it is newly seen (true) or a duplicate (false). The retained ids are bounded:
// the oldest is dropped once the session exceeds chatPermissionRetainedLimit, so
// the same pending request is pushed at most once while the state stays bounded.
func (h *chatHub) seenPermission(sessionID, requestID string) bool {
if sessionID == "" || requestID == "" {
return false
}
h.mu.Lock()
defer h.mu.Unlock()
turn := h.turnLocked(sessionID)
if turn.permissions == nil {
turn.permissions = map[string]struct{}{}
}
if _, ok := turn.permissions[requestID]; ok {
return false
}
turn.permissions[requestID] = struct{}{}
turn.permissionOrder = append(turn.permissionOrder, requestID)
if len(turn.permissionOrder) > chatPermissionRetainedLimit {
oldest := turn.permissionOrder[0]
turn.permissionOrder = turn.permissionOrder[1:]
delete(turn.permissions, oldest)
}
return true
}
// forgetPermission releases a request id reserved by seenPermission when its
// frame could not be written, so a later poll re-pushes it instead of the id
// staying "seen" while the phone never received the request.
func (h *chatHub) forgetPermission(sessionID, requestID string) {
if sessionID == "" || requestID == "" {
return
}
h.mu.Lock()
defer h.mu.Unlock()
turn := h.turns[sessionID]
if turn == nil {
return
}
delete(turn.permissions, requestID)
for i, id := range turn.permissionOrder {
if id == requestID {
turn.permissionOrder = append(turn.permissionOrder[:i], turn.permissionOrder[i+1:]...)
break
}
}
}
// handleChat dispatches one chat_* request. It returns true when the request was
// a chat type and has been fully answered (the caller must not fall through to
// the generic response). Frames are pushed after the ack, each through the
@@ -487,498 +92,3 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker
}
return false
}
// handleChatSessions answers chat_sessions with the operator's own sessions,
// marking any another client currently holds as Busy. The registry is the single
// source of truth for which sessions are listed and for their directory, tag,
// created/recency, so a pipeline stage session (recorded in run_steps, never in
// the operator registry) is always excluded and a session with no recorded tag
// is never listed. FAIL-CLOSED: when no registry is configured the result is
// empty, never the whole endpoint list. broker.Sessions is called at most once
// per request, and the registry once.
func handleChatSessions(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, registry operatorRegistry, clientID string, write func(protocol.Message) error) {
if broker == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
return
}
response := protocol.Message{Type: protocol.ChatSessions, ID: request.ID}
// No registry (no store and no injection): list nothing rather than the whole
// OpenCode machine.
if registry == nil {
_ = write(response)
return
}
operatorSessions, err := registry.OperatorSessions(ctx)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
// OperatorSessions is already most-recently-used first with a deterministic
// tie-break; keep that order and only include a registry session the broker
// still reports, so a session the operator deleted upstream is not listed.
registered := make(map[string]struct{}, len(operatorSessions))
for _, session := range operatorSessions {
if session.ID != "" {
registered[session.ID] = struct{}{}
}
}
sessions, err := broker.Sessions(ctx)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
present := make(map[string]protocol.ChatSession, len(sessions))
for _, session := range sessions {
if _, ok := registered[session.ID]; ok {
present[session.ID] = session
}
}
for _, operator := range operatorSessions {
session, ok := present[operator.ID]
if !ok {
continue
}
if operator.Title != "" {
session.Title = operator.Title
}
session.Directory = operator.Directory
session.Tag = operator.Tag
session.CreatedAt = operator.CreatedAt
session.UpdatedAt = operator.UpdatedAt
session.Busy = hub.busyForOther(operator.ID, clientID)
response.ChatSessions = append(response.ChatSessions, session)
}
_ = write(response)
}
// handleChatCreate creates one operator session in a resolved directory and
// records it in the operator registry: the OpenCode session id, the exact
// resolved directory, the title and the operator tag. A blank or omitted
// directory defaults to the operator's home, never the server's working
// directory. A registry write failure answers an error naming the created id, so
// an orphaned session is visible rather than silently reported as success.
func handleChatCreate(ctx context.Context, request protocol.Message, broker chatBroker, registry operatorRegistry, write func(protocol.Message) error) {
if broker == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
return
}
if registry == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "operator session registry is not configured on this server"})
return
}
title := ""
directory := ""
if request.ChatCreate != nil {
title = strings.TrimSpace(request.ChatCreate.Title)
directory = strings.TrimSpace(request.ChatCreate.Directory)
}
if title == "" {
title = "New chat"
}
resolved, err := resolveChatDirectory(directory)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
session, err := broker.Create(ctx, title, resolved)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
if err := registry.RecordOperatorSession(ctx, session.ID, resolved, title, protocol.ChatSessionTagOperator); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + session.ID + " was created but could not be recorded: " + err.Error()})
return
}
session.Directory = resolved
session.Tag = protocol.ChatSessionTagOperator
response := protocol.Message{Type: protocol.ChatCreate, ID: request.ID, ChatSession: &session}
// The same registry row is echoed on the list field so a client can splice it
// into chat_sessions without a re-list.
response.ChatSessions = []protocol.ChatSession{session}
_ = write(response)
}
// handleChatAdopt records a session that already exists on the endpoint in the
// operator registry, using the endpoint's own directory and title verbatim, so
// it appears in chat_sessions and can be continued. It refuses a nil broker, a
// nil registry, a nil payload and an invalid/blank id before any broker call; an
// id the endpoint does not report is an error naming the id and records nothing;
// an endpoint record with an empty directory is refused rather than substituting
// the server's working directory or the operator home. The response echoes the
// adopted session and its registry row like chat_create.
func handleChatAdopt(ctx context.Context, request protocol.Message, broker chatBroker, registry operatorRegistry, write func(protocol.Message) error) {
if broker == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
return
}
if registry == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "operator session registry is not configured on this server"})
return
}
if request.ChatAdopt == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a chat_adopt session id is required"})
return
}
id := strings.TrimSpace(request.ChatAdopt.SessionID)
if !opencode.ValidSessionID(id) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
return
}
session, found, err := broker.Session(ctx, id)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
if !found {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + id + " was not found on the OpenCode endpoint"})
return
}
directory := strings.TrimSpace(session.Directory)
if directory == "" {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + id + " has no directory on the OpenCode endpoint"})
return
}
if err := registry.RecordOperatorSession(ctx, session.ID, directory, session.Title, protocol.ChatSessionTagOperator); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + session.ID + " was found but could not be recorded: " + err.Error()})
return
}
session.Directory = directory
session.Tag = protocol.ChatSessionTagOperator
response := protocol.Message{Type: protocol.ChatAdopt, ID: request.ID, ChatSession: &session}
// The same registry row is echoed on the list field so a client can splice it
// into chat_sessions without a re-list.
response.ChatSessions = []protocol.ChatSession{session}
_ = write(response)
}
// handleChatDirectories answers chat_directories with the operator's home first,
// then the directories of previous operator sessions most-recently-used first,
// deduplicated against the home directory and capped at operatorDirectoryLimit.
func handleChatDirectories(ctx context.Context, request protocol.Message, registry operatorRegistry, write func(protocol.Message) error) {
if registry == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "operator session registry is not configured on this server"})
return
}
home, err := OperatorHomeDirectory()
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
directories := []string{home}
seen := map[string]struct{}{home: {}}
recent, err := registry.OperatorDirectories(ctx, operatorDirectoryLimit)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
for _, directory := range recent {
if len(directories) >= operatorDirectoryLimit {
break
}
if directory == "" {
continue
}
if _, ok := seen[directory]; ok {
continue
}
seen[directory] = struct{}{}
directories = append(directories, directory)
}
_ = write(protocol.Message{Type: protocol.ChatDirectories, ID: request.ID, ChatDirectories: directories})
}
// resolveChatDirectory resolves a chat_create directory to an absolute path: an
// omitted/empty value, "~" or a "~/..." value resolves to the operator's home
// (os.UserHomeDir) and never the server's working directory; any other value
// must be an absolute, existing directory, so a relative path cannot silently
// create a session relative to wherever the server was started.
func resolveChatDirectory(directory string) (string, error) {
directory = strings.TrimSpace(directory)
if directory == "" || directory == "~" || strings.HasPrefix(directory, "~/") {
return OperatorHomeDirectory()
}
if !filepath.IsAbs(directory) {
return "", errors.New("chat directory must be absolute or start with ~")
}
info, err := os.Stat(directory)
if err != nil {
return "", errors.New("chat directory " + directory + " is not available: " + err.Error())
}
if !info.IsDir() {
return "", errors.New("chat directory " + directory + " is not a directory")
}
return directory, nil
}
// OperatorHomeDirectory returns the operator's home directory, the default chat
// location. It is resolved through the standard library so it never falls back
// to the process working directory.
func OperatorHomeDirectory() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", errors.New("resolve operator home directory: " + err.Error())
}
if home == "" {
return "", errors.New("operator home directory is empty")
}
return home, nil
}
func handleChatPrompt(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, registry operatorRegistry, clientID string, write func(protocol.Message) error) {
if broker == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
return
}
prompt := request.ChatPrompt
if prompt == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a chat prompt payload is required"})
return
}
if !opencode.ValidSessionID(prompt.SessionID) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
return
}
if strings.TrimSpace(prompt.Text) == "" {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a nonempty chat prompt is required"})
return
}
// Reject an oversized prompt before any OpenCode call; the bound matches the
// OpenCode request limit.
if len(prompt.Text) > opencode.MaxRequest {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat prompt exceeds 64 KiB"})
return
}
// Claim the session before subscribing or prompting: a second client must be
// rejected here, before any broker call, so two clients cannot silently write
// the same conversation. The same client may re-prompt its own session.
sessionID := prompt.SessionID
if !hub.claim(sessionID, clientID) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " is busy with another client"})
return
}
// Subscribe to the OpenCode firehose before posting the prompt: the stream
// has no replay cursor (research 2.4), so a delta emitted between the POST
// and the subscription would otherwise be lost. Frames that arrive before the
// ack is written are buffered and flushed in order after it.
streamCtx, streamCancel := context.WithCancel(ctx)
attached := make(chan struct{})
var attachOnce sync.Once
streamResult := make(chan error, 1)
var bufferMu sync.Mutex
buffering := true
var buffered []protocol.ChatEventFrame
go func() {
streamResult <- broker.Stream(streamCtx, sessionID, func() {
attachOnce.Do(func() { close(attached) })
}, func(frame protocol.ChatEventFrame) error {
bufferMu.Lock()
if buffering {
buffered = append(buffered, frame)
bufferMu.Unlock()
return nil
}
bufferMu.Unlock()
hub.apply(frame)
return write(protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame})
})
}()
select {
case <-attached:
case err := <-streamResult:
streamCancel()
hub.release(sessionID, clientID)
if err != nil && ctx.Err() == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat stream: " + err.Error()})
}
return
case <-ctx.Done():
streamCancel()
hub.release(sessionID, clientID)
return
}
if err := broker.Prompt(ctx, sessionID, prompt.Text); err != nil {
streamCancel()
hub.release(sessionID, clientID)
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
// Recency is one UPDATE: it moves the operator session to the front of
// chat_sessions. A session id that is not an operator session updates no row
// and is not an error, so pipeline stage prompts are unaffected.
if registry != nil {
if err := registry.TouchOperatorSession(ctx, sessionID); err != nil {
log.Printf("touch operator session %s: %v", sessionID, err)
}
}
// Acknowledge the accepted prompt with the echoed request id, then stream the
// reply. The prompt is sent exactly once and never re-sent on resume.
if err := write(protocol.Message{Type: protocol.ChatPrompt, ID: request.ID}); err != nil {
streamCancel()
hub.release(sessionID, clientID)
return
}
// Flush frames buffered while the prompt was accepted, then resume live
// delivery. Holding bufferMu preserves order: the stream goroutine either
// buffered before this flush or waits and writes after it.
bufferMu.Lock()
buffering = false
pending := buffered
buffered = nil
for _, frame := range pending {
hub.apply(frame)
if err := write(protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame}); err != nil {
bufferMu.Unlock()
streamCancel()
hub.release(sessionID, clientID)
return
}
}
bufferMu.Unlock()
go drainChatStream(ctx, streamResult, streamCancel, hub, sessionID, clientID, write)
// Poll for pending approval requests alongside the event stream, sharing
// streamCtx so the poller stops when the stream ends or the client
// disconnects. The first poll runs immediately.
go pollChatPermissions(streamCtx, broker, hub, sessionID, write)
}
// pollChatPermissions polls the broker's pending-approval list on a fixed
// interval, sharing the chat subscription's context so it stops on disconnect or
// when the turn's stream ends. The hub dedups the request ids, so each pending
// request is pushed at most once; a poll failure is surfaced once through the
// same write closure instead of staying silent. A backend that does not expose
// the approval surface (opencode.ErrPermissionUnsupported) stops the poller
// quietly, so the documented limitation is not reported as a per-prompt error.
func pollChatPermissions(ctx context.Context, broker chatBroker, hub *chatHub, sessionID string, write func(protocol.Message) error) {
ticker := time.NewTicker(chatPermissionPollInterval)
defer ticker.Stop()
reported := false
for {
if err := pushChatPermissions(ctx, broker, hub, sessionID, write); err != nil {
if ctx.Err() != nil {
return
}
if errors.Is(err, opencode.ErrPermissionUnsupported) {
log.Printf("chat permission poll session=%s: %v", sessionID, err)
return
}
if reported {
log.Printf("chat permission poll session=%s: %v", sessionID, err)
} else {
reported = true
_ = write(protocol.Message{Type: protocol.Error, Error: "chat permission poll: " + err.Error()})
}
}
select {
case <-ctx.Done():
return
case <-ticker.C:
}
}
}
// pushChatPermissions performs one poll and writes a chat_permission frame for
// every newly seen request of the session, through the same connection-scoped
// write closure as the chat stream. It never re-pushes a request id, and releases
// the reservation on a write failure so the frame is not silently dropped.
func pushChatPermissions(ctx context.Context, broker chatBroker, hub *chatHub, sessionID string, write func(protocol.Message) error) error {
pending, err := broker.Permissions(ctx, sessionID)
if err != nil {
return err
}
for _, request := range pending {
if request.SessionID != sessionID || request.RequestID == "" {
continue
}
if !hub.seenPermission(sessionID, request.RequestID) {
continue
}
frame := request
if err := write(protocol.Message{Type: protocol.ChatPermission, ChatPermission: &frame}); err != nil {
hub.forgetPermission(sessionID, request.RequestID)
return err
}
}
return nil
}
// drainChatStream reports the terminal error of a chat subscription and releases
// its context and the session claim once the stream has ended, so a later client
// is no longer blocked and nothing leaks.
func drainChatStream(ctx context.Context, result <-chan error, cancel context.CancelFunc, hub *chatHub, sessionID, clientID string, write func(protocol.Message) error) {
defer cancel()
defer hub.release(sessionID, clientID)
if err := <-result; err != nil && ctx.Err() == nil {
log.Printf("chat stream session=%s: %v", sessionID, err)
_ = write(protocol.Message{Type: protocol.Error, Error: "chat stream: " + err.Error()})
}
}
func handleChatMessages(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, write func(protocol.Message) error) {
query := request.ChatQuery
if query == nil || !opencode.ValidSessionID(query.SessionID) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
return
}
response := protocol.Message{Type: protocol.ChatMessages, ID: request.ID}
// Resume from retained server state first: a reconnecting client gets the
// accumulated turns with the same stable ids without re-reading OpenCode.
if messages := hub.snapshot(query.SessionID); len(messages) > 0 {
response.ChatMessages = messages
_ = write(response)
return
}
if broker == nil {
_ = write(response)
return
}
messages, err := broker.Messages(ctx, query.SessionID, query.Limit)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
response.ChatMessages = messages
_ = write(response)
}
func handleChatSimple(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, write func(protocol.Message) error) {
if broker == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
return
}
if request.Type == protocol.ChatInterrupt {
query := request.ChatQuery
if query == nil || !opencode.ValidSessionID(query.SessionID) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
return
}
if err := broker.Interrupt(ctx, query.SessionID); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
_ = write(protocol.Message{Type: protocol.ChatInterrupt, ID: request.ID})
return
}
reply := request.ChatReply
if reply == nil || !opencode.ValidSessionID(reply.SessionID) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
return
}
if strings.TrimSpace(reply.RequestID) == "" {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a permission request id is required"})
return
}
switch reply.Decision {
case protocol.ChatDecisionOnce, protocol.ChatDecisionAlways, protocol.ChatDecisionReject:
default:
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid permission decision is required"})
return
}
if err := broker.Reply(ctx, *reply); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
_ = write(protocol.Message{Type: protocol.ChatReply, ID: request.ID})
}
+65
View File
@@ -0,0 +1,65 @@
package server
// claim reserves sessionID for clientID. It reports true when the session is
// free or already held by the same client (so a client may re-prompt its own
// session) and false when another client holds it. The claim map is bounded by
// chatMaxTurns with oldest-eviction so a stuck client cannot grow it without
// limit.
func (h *chatHub) claim(sessionID, clientID string) bool {
if sessionID == "" || clientID == "" {
return false
}
h.mu.Lock()
defer h.mu.Unlock()
if h.claims == nil {
h.claims = map[string]string{}
}
if holder, ok := h.claims[sessionID]; ok {
return holder == clientID
}
h.evictClaimsLocked()
h.claims[sessionID] = clientID
h.claimOrder = append(h.claimOrder, sessionID)
return true
}
// release drops the same client's claim on sessionID; a claim held by a
// different client is left untouched so a stale release cannot free a live one.
func (h *chatHub) release(sessionID, clientID string) {
h.mu.Lock()
defer h.mu.Unlock()
if holder, ok := h.claims[sessionID]; !ok || holder != clientID {
return
}
delete(h.claims, sessionID)
for i, id := range h.claimOrder {
if id == sessionID {
h.claimOrder = append(h.claimOrder[:i], h.claimOrder[i+1:]...)
break
}
}
}
// busyForOther reports whether another client currently holds sessionID.
func (h *chatHub) busyForOther(sessionID, clientID string) bool {
h.mu.Lock()
defer h.mu.Unlock()
holder, ok := h.claims[sessionID]
return ok && holder != clientID
}
// evictClaimsLocked drops the oldest claim when the cap is reached, keeping the
// claim map bounded like the retained turns. Callers must hold h.mu.
func (h *chatHub) evictClaimsLocked() {
if len(h.claims) < chatMaxTurns {
return
}
for len(h.claimOrder) > 0 {
oldest := h.claimOrder[0]
h.claimOrder = h.claimOrder[1:]
if _, ok := h.claims[oldest]; ok {
delete(h.claims, oldest)
return
}
}
}
+76
View File
@@ -0,0 +1,76 @@
package server
import (
"context"
"strings"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func handleChatMessages(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, write func(protocol.Message) error) {
query := request.ChatQuery
if query == nil || !opencode.ValidSessionID(query.SessionID) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
return
}
response := protocol.Message{Type: protocol.ChatMessages, ID: request.ID}
// Resume from retained server state first: a reconnecting client gets the
// accumulated turns with the same stable ids without re-reading OpenCode.
if messages := hub.snapshot(query.SessionID); len(messages) > 0 {
response.ChatMessages = messages
_ = write(response)
return
}
if broker == nil {
_ = write(response)
return
}
messages, err := broker.Messages(ctx, query.SessionID, query.Limit)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
response.ChatMessages = messages
_ = write(response)
}
func handleChatSimple(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, write func(protocol.Message) error) {
if broker == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
return
}
if request.Type == protocol.ChatInterrupt {
query := request.ChatQuery
if query == nil || !opencode.ValidSessionID(query.SessionID) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
return
}
if err := broker.Interrupt(ctx, query.SessionID); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
_ = write(protocol.Message{Type: protocol.ChatInterrupt, ID: request.ID})
return
}
reply := request.ChatReply
if reply == nil || !opencode.ValidSessionID(reply.SessionID) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
return
}
if strings.TrimSpace(reply.RequestID) == "" {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a permission request id is required"})
return
}
switch reply.Decision {
case protocol.ChatDecisionOnce, protocol.ChatDecisionAlways, protocol.ChatDecisionReject:
default:
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid permission decision is required"})
return
}
if err := broker.Reply(ctx, *reply); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
_ = write(protocol.Message{Type: protocol.ChatReply, ID: request.ID})
}
+268
View File
@@ -0,0 +1,268 @@
package server
import (
"log"
"sync"
"time"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// chatRetainedLimit bounds the state one session may retain so a stalled reader
// or a runaway turn cannot grow memory without limit. It matches the OpenCode
// response bound (1 MiB).
const chatRetainedLimit = opencode.MaxResponse
// chatMaxTurns bounds how many sessions retain turn state. A long-lived server
// cannot accumulate state for every session ever prompted, so the
// least-recently-updated turn is evicted once the cap is exceeded.
const chatMaxTurns = 128
// chatHub retains the accumulated turn state per session, independently of any
// single connection, so a reconnecting client can re-sync what it missed. State
// is keyed by session and then by message id (a session holds many turns), and
// is capped at chatRetainedLimit.
type chatHub struct {
mu sync.Mutex
turns map[string]*chatTurn
now func() time.Time
// claims maps a session to the client currently writing it so a second
// client is rejected; claimOrder bounds the map like chatMaxTurns.
claims map[string]string
claimOrder []string
}
// chatPart is the retained form of one part. Text and reasoning deltas append to
// text (amortized O(1)); a tool frame replaces part outright.
type chatPart struct {
part protocol.ChatPart
text []byte
}
// chatMessage is one retained assistant turn. Its parts keep the exact ids the
// supporting chat_event frames carried, so a resume preserves attribution.
type chatMessage struct {
id string
order []string
parts map[string]*chatPart
}
type chatTurn struct {
sessionID string
order []string
messages map[string]*chatMessage
step *protocol.ChatStep
usage *protocol.ChatUsage
size int
overflow bool
updated time.Time
// permissions holds the request ids already pushed to the client so a
// repeated poll cannot push the same pending request twice; permissionOrder
// bounds it to chatPermissionRetainedLimit.
permissions map[string]struct{}
permissionOrder []string
}
func newChatHub() *chatHub {
return &chatHub{turns: map[string]*chatTurn{}, now: time.Now}
}
func (h *chatHub) clock() time.Time {
if h.now != nil {
return h.now()
}
return time.Now()
}
// apply folds one pushed frame into the retained state. Text and reasoning
// deltas append to their message's part; a tool frame upserts the full part
// (accounting for its whole size) so the latest lifecycle status, input,
// content, metadata, error and time are retained under the frame's message id.
// Step and usage frames update the session's latest progress.
func (h *chatHub) apply(frame protocol.ChatEventFrame) {
if frame.SessionID == "" {
return
}
h.mu.Lock()
defer h.mu.Unlock()
turn := h.turnLocked(frame.SessionID)
switch frame.Kind {
case protocol.ChatEventStep:
turn.step = frame.Step
return
case protocol.ChatEventUsage:
turn.usage = frame.Usage
return
}
if frame.MessageID == "" {
return
}
message := turn.messages[frame.MessageID]
if message == nil {
message = &chatMessage{id: frame.MessageID, parts: map[string]*chatPart{}}
turn.order = append(turn.order, frame.MessageID)
turn.messages[frame.MessageID] = message
}
switch frame.Kind {
case protocol.ChatEventText, protocol.ChatEventReasoning:
id := ""
if frame.Part != nil {
id = frame.Part.ID
}
if id == "" || frame.Delta == "" {
return
}
if !turn.retain(len(frame.Delta)) {
return
}
part, ok := message.parts[id]
if !ok {
part = &chatPart{part: protocol.ChatPart{Type: frame.Kind, ID: id}}
message.order = append(message.order, id)
message.parts[id] = part
}
part.text = append(part.text, frame.Delta...)
case protocol.ChatEventTool:
if frame.Part == nil || frame.Part.ID == "" || turn.overflow {
return
}
// The tool part is retained whole, so size the complete part (input,
// content, metadata, error and time) and charge the difference against
// the cap on every upsert, not just the first-seen name.
incoming := chatPartBytes(frame.Part)
part, ok := message.parts[frame.Part.ID]
if !ok {
if !turn.retain(incoming) {
return
}
part = &chatPart{}
message.order = append(message.order, frame.Part.ID)
message.parts[frame.Part.ID] = part
} else if previous := chatPartBytes(&part.part); incoming > previous {
if !turn.retain(incoming - previous) {
return
}
} else {
turn.shrink(previous - incoming)
}
part.part = *frame.Part
}
}
// chatPartBytes estimates the retained size of a part so the per-session cap
// accounts for its whole state instead of only the first-seen name. It walks the
// fields rather than re-marshalling, so the cost matches the bytes it stores.
func chatPartBytes(part *protocol.ChatPart) int {
if part == nil {
return 0
}
n := len(part.Type) + len(part.ID) + len(part.Name)
if part.State == nil {
return n
}
n += len(part.State.Status) + len(part.State.Input)
for _, content := range part.State.Content {
n += len(content.Type) + len(content.Text) + len(content.URI) + len(content.Mime) + len(content.Name)
}
if part.State.Error != nil {
n += len(part.State.Error.Type) + len(part.State.Error.Message)
}
for key, value := range part.State.Metadata {
n += len(key)
if text, ok := value.(string); ok {
n += len(text)
} else {
n += 8
}
}
return n
}
// evictLocked drops the least-recently-updated turn when the session cap is
// reached, keeping retained memory bounded across session churn.
func (h *chatHub) evictLocked() {
if len(h.turns) < chatMaxTurns {
return
}
oldestID := ""
var oldest time.Time
for id, turn := range h.turns {
if oldestID == "" || turn.updated.Before(oldest) {
oldestID, oldest = id, turn.updated
}
}
if oldestID != "" {
delete(h.turns, oldestID)
}
}
// turnLocked returns the session's retained turn, creating it (and evicting the
// least-recently-updated turn first when the cap is reached) and refreshing its
// update time. It is the shared turn-initialization path for apply and
// seenPermission, so the two cannot drift. Callers must hold h.mu.
func (h *chatHub) turnLocked(sessionID string) *chatTurn {
turn := h.turns[sessionID]
if turn == nil {
h.evictLocked()
turn = &chatTurn{sessionID: sessionID, messages: map[string]*chatMessage{}}
h.turns[sessionID] = turn
}
turn.updated = h.clock()
return turn
}
// retain records n more bytes and reports whether the turn is still within the
// bound. The first overflow records one diagnostic and then drops further state
// instead of growing without limit.
func (t *chatTurn) retain(n int) bool {
if t.overflow {
return false
}
if t.size+n > chatRetainedLimit {
t.overflow = true
log.Printf("chat session %s retained state exceeds %d bytes; dropping further state", t.sessionID, chatRetainedLimit)
return false
}
t.size += n
return true
}
// shrink releases n bytes when a tool upsert replaces a larger retained part.
func (t *chatTurn) shrink(n int) {
t.size -= n
if t.size < 0 {
t.size = 0
}
}
// snapshot returns the accumulated session as one assistant message per
// retained message id, with the same stable part ids, or nil when nothing is
// retained. It is the resume source: a reconnecting client re-syncs the whole
// session once instead of replaying deltas, and each turn keeps its own id.
func (h *chatHub) snapshot(sessionID string) []protocol.ChatMessage {
h.mu.Lock()
defer h.mu.Unlock()
turn := h.turns[sessionID]
if turn == nil {
return nil
}
turn.updated = h.clock()
var messages []protocol.ChatMessage
for _, id := range turn.order {
message := turn.messages[id]
if message == nil || len(message.order) == 0 {
continue
}
out := protocol.ChatMessage{ID: id, SessionID: sessionID, Role: protocol.ChatRoleAssistant}
for _, partID := range message.order {
if part, ok := message.parts[partID]; ok {
piece := part.part
piece.Text = string(part.text)
out.Parts = append(out.Parts, piece)
}
}
messages = append(messages, out)
}
return messages
}
+54
View File
@@ -0,0 +1,54 @@
package server
// chatPermissionRetainedLimit bounds the dedup set of already-pushed permission
// request ids one session retains, so a long turn cannot grow it without limit.
const chatPermissionRetainedLimit = 256
// seenPermission records requestID as pushed for the session and reports whether
// it is newly seen (true) or a duplicate (false). The retained ids are bounded:
// the oldest is dropped once the session exceeds chatPermissionRetainedLimit, so
// the same pending request is pushed at most once while the state stays bounded.
func (h *chatHub) seenPermission(sessionID, requestID string) bool {
if sessionID == "" || requestID == "" {
return false
}
h.mu.Lock()
defer h.mu.Unlock()
turn := h.turnLocked(sessionID)
if turn.permissions == nil {
turn.permissions = map[string]struct{}{}
}
if _, ok := turn.permissions[requestID]; ok {
return false
}
turn.permissions[requestID] = struct{}{}
turn.permissionOrder = append(turn.permissionOrder, requestID)
if len(turn.permissionOrder) > chatPermissionRetainedLimit {
oldest := turn.permissionOrder[0]
turn.permissionOrder = turn.permissionOrder[1:]
delete(turn.permissions, oldest)
}
return true
}
// forgetPermission releases a request id reserved by seenPermission when its
// frame could not be written, so a later poll re-pushes it instead of the id
// staying "seen" while the phone never received the request.
func (h *chatHub) forgetPermission(sessionID, requestID string) {
if sessionID == "" || requestID == "" {
return
}
h.mu.Lock()
defer h.mu.Unlock()
turn := h.turns[sessionID]
if turn == nil {
return
}
delete(turn.permissions, requestID)
for i, id := range turn.permissionOrder {
if id == requestID {
turn.permissionOrder = append(turn.permissionOrder[:i], turn.permissionOrder[i+1:]...)
break
}
}
}
+254
View File
@@ -0,0 +1,254 @@
package server
import (
"context"
"errors"
"os"
"path/filepath"
"strings"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// operatorDirectoryLimit bounds the chat_directories choices: the operator home
// plus recent operator-session directories, capped at this small fixed number so
// a long history is never returned. It matches the posture of the other small
// fixed list bounds.
const operatorDirectoryLimit = 8
// handleChatSessions answers chat_sessions with the operator's own sessions,
// marking any another client currently holds as Busy. The registry is the single
// source of truth for which sessions are listed and for their directory, tag,
// created/recency, so a pipeline stage session (recorded in run_steps, never in
// the operator registry) is always excluded and a session with no recorded tag
// is never listed. FAIL-CLOSED: when no registry is configured the result is
// empty, never the whole endpoint list. broker.Sessions is called at most once
// per request, and the registry once.
func handleChatSessions(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, registry operatorRegistry, clientID string, write func(protocol.Message) error) {
if broker == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
return
}
response := protocol.Message{Type: protocol.ChatSessions, ID: request.ID}
// No registry (no store and no injection): list nothing rather than the whole
// OpenCode machine.
if registry == nil {
_ = write(response)
return
}
operatorSessions, err := registry.OperatorSessions(ctx)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
// OperatorSessions is already most-recently-used first with a deterministic
// tie-break; keep that order and only include a registry session the broker
// still reports, so a session the operator deleted upstream is not listed.
registered := make(map[string]struct{}, len(operatorSessions))
for _, session := range operatorSessions {
if session.ID != "" {
registered[session.ID] = struct{}{}
}
}
sessions, err := broker.Sessions(ctx)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
present := make(map[string]protocol.ChatSession, len(sessions))
for _, session := range sessions {
if _, ok := registered[session.ID]; ok {
present[session.ID] = session
}
}
for _, operator := range operatorSessions {
session, ok := present[operator.ID]
if !ok {
continue
}
if operator.Title != "" {
session.Title = operator.Title
}
session.Directory = operator.Directory
session.Tag = operator.Tag
session.CreatedAt = operator.CreatedAt
session.UpdatedAt = operator.UpdatedAt
session.Busy = hub.busyForOther(operator.ID, clientID)
response.ChatSessions = append(response.ChatSessions, session)
}
_ = write(response)
}
// handleChatCreate creates one operator session in a resolved directory and
// records it in the operator registry: the OpenCode session id, the exact
// resolved directory, the title and the operator tag. A blank or omitted
// directory defaults to the operator's home, never the server's working
// directory. A registry write failure answers an error naming the created id, so
// an orphaned session is visible rather than silently reported as success.
func handleChatCreate(ctx context.Context, request protocol.Message, broker chatBroker, registry operatorRegistry, write func(protocol.Message) error) {
if broker == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
return
}
if registry == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "operator session registry is not configured on this server"})
return
}
title := ""
directory := ""
if request.ChatCreate != nil {
title = strings.TrimSpace(request.ChatCreate.Title)
directory = strings.TrimSpace(request.ChatCreate.Directory)
}
if title == "" {
title = "New chat"
}
resolved, err := resolveChatDirectory(directory)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
session, err := broker.Create(ctx, title, resolved)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
if err := registry.RecordOperatorSession(ctx, session.ID, resolved, title, protocol.ChatSessionTagOperator); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + session.ID + " was created but could not be recorded: " + err.Error()})
return
}
session.Directory = resolved
session.Tag = protocol.ChatSessionTagOperator
response := protocol.Message{Type: protocol.ChatCreate, ID: request.ID, ChatSession: &session}
// The same registry row is echoed on the list field so a client can splice it
// into chat_sessions without a re-list.
response.ChatSessions = []protocol.ChatSession{session}
_ = write(response)
}
// handleChatAdopt records a session that already exists on the endpoint in the
// operator registry, using the endpoint's own directory and title verbatim, so
// it appears in chat_sessions and can be continued. It refuses a nil broker, a
// nil registry, a nil payload and an invalid/blank id before any broker call; an
// id the endpoint does not report is an error naming the id and records nothing;
// an endpoint record with an empty directory is refused rather than substituting
// the server's working directory or the operator home. The response echoes the
// adopted session and its registry row like chat_create.
func handleChatAdopt(ctx context.Context, request protocol.Message, broker chatBroker, registry operatorRegistry, write func(protocol.Message) error) {
if broker == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
return
}
if registry == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "operator session registry is not configured on this server"})
return
}
if request.ChatAdopt == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a chat_adopt session id is required"})
return
}
id := strings.TrimSpace(request.ChatAdopt.SessionID)
if !opencode.ValidSessionID(id) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
return
}
session, found, err := broker.Session(ctx, id)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
if !found {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + id + " was not found on the OpenCode endpoint"})
return
}
directory := strings.TrimSpace(session.Directory)
if directory == "" {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + id + " has no directory on the OpenCode endpoint"})
return
}
if err := registry.RecordOperatorSession(ctx, session.ID, directory, session.Title, protocol.ChatSessionTagOperator); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + session.ID + " was found but could not be recorded: " + err.Error()})
return
}
session.Directory = directory
session.Tag = protocol.ChatSessionTagOperator
response := protocol.Message{Type: protocol.ChatAdopt, ID: request.ID, ChatSession: &session}
// The same registry row is echoed on the list field so a client can splice it
// into chat_sessions without a re-list.
response.ChatSessions = []protocol.ChatSession{session}
_ = write(response)
}
// handleChatDirectories answers chat_directories with the operator's home first,
// then the directories of previous operator sessions most-recently-used first,
// deduplicated against the home directory and capped at operatorDirectoryLimit.
func handleChatDirectories(ctx context.Context, request protocol.Message, registry operatorRegistry, write func(protocol.Message) error) {
if registry == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "operator session registry is not configured on this server"})
return
}
home, err := OperatorHomeDirectory()
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
directories := []string{home}
seen := map[string]struct{}{home: {}}
recent, err := registry.OperatorDirectories(ctx, operatorDirectoryLimit)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
for _, directory := range recent {
if len(directories) >= operatorDirectoryLimit {
break
}
if directory == "" {
continue
}
if _, ok := seen[directory]; ok {
continue
}
seen[directory] = struct{}{}
directories = append(directories, directory)
}
_ = write(protocol.Message{Type: protocol.ChatDirectories, ID: request.ID, ChatDirectories: directories})
}
// resolveChatDirectory resolves a chat_create directory to an absolute path: an
// omitted/empty value, "~" or a "~/..." value resolves to the operator's home
// (os.UserHomeDir) and never the server's working directory; any other value
// must be an absolute, existing directory, so a relative path cannot silently
// create a session relative to wherever the server was started.
func resolveChatDirectory(directory string) (string, error) {
directory = strings.TrimSpace(directory)
if directory == "" || directory == "~" || strings.HasPrefix(directory, "~/") {
return OperatorHomeDirectory()
}
if !filepath.IsAbs(directory) {
return "", errors.New("chat directory must be absolute or start with ~")
}
info, err := os.Stat(directory)
if err != nil {
return "", errors.New("chat directory " + directory + " is not available: " + err.Error())
}
if !info.IsDir() {
return "", errors.New("chat directory " + directory + " is not a directory")
}
return directory, nil
}
// OperatorHomeDirectory returns the operator's home directory, the default chat
// location. It is resolved through the standard library so it never falls back
// to the process working directory.
func OperatorHomeDirectory() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", errors.New("resolve operator home directory: " + err.Error())
}
if home == "" {
return "", errors.New("operator home directory is empty")
}
return home, nil
}
+211
View File
@@ -0,0 +1,211 @@
package server
import (
"context"
"errors"
"log"
"strings"
"sync"
"time"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// chatPermissionPollInterval is the fixed interval between permission polls: one
// GET per poll through the shared Connection bounds, never a busy loop. The first
// poll runs immediately so a request pending when the prompt is sent is surfaced
// without waiting a full interval.
const chatPermissionPollInterval = 2 * time.Second
func handleChatPrompt(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, registry operatorRegistry, clientID string, write func(protocol.Message) error) {
if broker == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
return
}
prompt := request.ChatPrompt
if prompt == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a chat prompt payload is required"})
return
}
if !opencode.ValidSessionID(prompt.SessionID) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
return
}
if strings.TrimSpace(prompt.Text) == "" {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a nonempty chat prompt is required"})
return
}
// Reject an oversized prompt before any OpenCode call; the bound matches the
// OpenCode request limit.
if len(prompt.Text) > opencode.MaxRequest {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat prompt exceeds 64 KiB"})
return
}
// Claim the session before subscribing or prompting: a second client must be
// rejected here, before any broker call, so two clients cannot silently write
// the same conversation. The same client may re-prompt its own session.
sessionID := prompt.SessionID
if !hub.claim(sessionID, clientID) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " is busy with another client"})
return
}
// Subscribe to the OpenCode firehose before posting the prompt: the stream
// has no replay cursor (research 2.4), so a delta emitted between the POST
// and the subscription would otherwise be lost. Frames that arrive before the
// ack is written are buffered and flushed in order after it.
streamCtx, streamCancel := context.WithCancel(ctx)
attached := make(chan struct{})
var attachOnce sync.Once
streamResult := make(chan error, 1)
var bufferMu sync.Mutex
buffering := true
var buffered []protocol.ChatEventFrame
go func() {
streamResult <- broker.Stream(streamCtx, sessionID, func() {
attachOnce.Do(func() { close(attached) })
}, func(frame protocol.ChatEventFrame) error {
bufferMu.Lock()
if buffering {
buffered = append(buffered, frame)
bufferMu.Unlock()
return nil
}
bufferMu.Unlock()
hub.apply(frame)
return write(protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame})
})
}()
select {
case <-attached:
case err := <-streamResult:
streamCancel()
hub.release(sessionID, clientID)
if err != nil && ctx.Err() == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat stream: " + err.Error()})
}
return
case <-ctx.Done():
streamCancel()
hub.release(sessionID, clientID)
return
}
if err := broker.Prompt(ctx, sessionID, prompt.Text); err != nil {
streamCancel()
hub.release(sessionID, clientID)
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
// Recency is one UPDATE: it moves the operator session to the front of
// chat_sessions. A session id that is not an operator session updates no row
// and is not an error, so pipeline stage prompts are unaffected.
if registry != nil {
if err := registry.TouchOperatorSession(ctx, sessionID); err != nil {
log.Printf("touch operator session %s: %v", sessionID, err)
}
}
// Acknowledge the accepted prompt with the echoed request id, then stream the
// reply. The prompt is sent exactly once and never re-sent on resume.
if err := write(protocol.Message{Type: protocol.ChatPrompt, ID: request.ID}); err != nil {
streamCancel()
hub.release(sessionID, clientID)
return
}
// Flush frames buffered while the prompt was accepted, then resume live
// delivery. Holding bufferMu preserves order: the stream goroutine either
// buffered before this flush or waits and writes after it.
bufferMu.Lock()
buffering = false
pending := buffered
buffered = nil
for _, frame := range pending {
hub.apply(frame)
if err := write(protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame}); err != nil {
bufferMu.Unlock()
streamCancel()
hub.release(sessionID, clientID)
return
}
}
bufferMu.Unlock()
go drainChatStream(ctx, streamResult, streamCancel, hub, sessionID, clientID, write)
// Poll for pending approval requests alongside the event stream, sharing
// streamCtx so the poller stops when the stream ends or the client
// disconnects. The first poll runs immediately.
go pollChatPermissions(streamCtx, broker, hub, sessionID, write)
}
// pollChatPermissions polls the broker's pending-approval list on a fixed
// interval, sharing the chat subscription's context so it stops on disconnect or
// when the turn's stream ends. The hub dedups the request ids, so each pending
// request is pushed at most once; a poll failure is surfaced once through the
// same write closure instead of staying silent. A backend that does not expose
// the approval surface (opencode.ErrPermissionUnsupported) stops the poller
// quietly, so the documented limitation is not reported as a per-prompt error.
func pollChatPermissions(ctx context.Context, broker chatBroker, hub *chatHub, sessionID string, write func(protocol.Message) error) {
ticker := time.NewTicker(chatPermissionPollInterval)
defer ticker.Stop()
reported := false
for {
if err := pushChatPermissions(ctx, broker, hub, sessionID, write); err != nil {
if ctx.Err() != nil {
return
}
if errors.Is(err, opencode.ErrPermissionUnsupported) {
log.Printf("chat permission poll session=%s: %v", sessionID, err)
return
}
if reported {
log.Printf("chat permission poll session=%s: %v", sessionID, err)
} else {
reported = true
_ = write(protocol.Message{Type: protocol.Error, Error: "chat permission poll: " + err.Error()})
}
}
select {
case <-ctx.Done():
return
case <-ticker.C:
}
}
}
// pushChatPermissions performs one poll and writes a chat_permission frame for
// every newly seen request of the session, through the same connection-scoped
// write closure as the chat stream. It never re-pushes a request id, and releases
// the reservation on a write failure so the frame is not silently dropped.
func pushChatPermissions(ctx context.Context, broker chatBroker, hub *chatHub, sessionID string, write func(protocol.Message) error) error {
pending, err := broker.Permissions(ctx, sessionID)
if err != nil {
return err
}
for _, request := range pending {
if request.SessionID != sessionID || request.RequestID == "" {
continue
}
if !hub.seenPermission(sessionID, request.RequestID) {
continue
}
frame := request
if err := write(protocol.Message{Type: protocol.ChatPermission, ChatPermission: &frame}); err != nil {
hub.forgetPermission(sessionID, request.RequestID)
return err
}
}
return nil
}
// drainChatStream reports the terminal error of a chat subscription and releases
// its context and the session claim once the stream has ended, so a later client
// is no longer blocked and nothing leaks.
func drainChatStream(ctx context.Context, result <-chan error, cancel context.CancelFunc, hub *chatHub, sessionID, clientID string, write func(protocol.Message) error) {
defer cancel()
defer hub.release(sessionID, clientID)
if err := <-result; err != nil && ctx.Err() == nil {
log.Printf("chat stream session=%s: %v", sessionID, err)
_ = write(protocol.Message{Type: protocol.Error, Error: "chat stream: " + err.Error()})
}
}
-229
View File
@@ -2,9 +2,7 @@ package stage
import (
"context"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
@@ -14,105 +12,6 @@ const ModelType = "model"
const ModelID = "deepseek-v4.1-flash"
// Retained model-stage failure categories. A strict final-result validation
// failure is safe and never replays admitted tool work; a harness transport,
// readback or wait failure is a different operator action. Labels are fixed and
// sanitized: no provider or model response body is copied into them.
const (
finalResultValidation = "FINAL RESULT VALIDATION"
harnessTransport = "HARNESS TRANSPORT"
)
// finalResultGuidance is a fixed sanitized sentence appended only to validation
// diagnostics. It states the retained side effects and the cost of a full retry.
const finalResultGuidance = "completed edits and tool side effects are retained and not undone; a full retry repeats work"
// harnessTransportFailure is the single classifier shared by both transport
// sentinels and the typed OpenCode error: a request, poll or readback failure
// that reports as the fixed HARNESS TRANSPORT category. A bare context deadline
// is classified separately so a genuine stage overrun is never relabeled as a
// transport failure.
func harnessTransportFailure(err error) bool {
var openCode *openCodeError
return errors.Is(err, ErrHarnessUnreachable) || errors.Is(err, ErrHarnessTransport) || errors.As(err, &openCode)
}
// harnessReachFailure reports whether err is a pre-admission harness reach
// failure. It is distinct from a post-admission ErrHarnessTransport failure: no
// session or prompt was admitted, so the retry replays no work.
func harnessReachFailure(err error) bool {
return errors.Is(err, ErrHarnessUnreachable)
}
// admittedWorker is implemented by a client that can admit tool work before its
// final result is read, so the retry loop can refuse to replay it without a
// per-client type switch.
type admittedWorker interface{ workAdmitted() bool }
// workAdmitted reports whether the client admitted any prompt or tool call, so
// the retry loop may retry a transport failure on any stage only when it is
// false, and only a read-only stage otherwise.
func (c *AgentClient) workAdmitted() bool { return c.WorkStarted }
func (c *PiClient) workAdmitted() bool { return c.WorkStarted }
// modelFailureCategory classifies a stage failure using error identity only, so
// the label stays sanitized and bounded. Validation is checked first: a
// structured-output failure is never reported as a transport failure and vice versa.
func modelFailureCategory(err error) string {
switch {
case errors.Is(err, ErrInvalidModelResult):
return finalResultValidation
case harnessTransportFailure(err), errors.Is(err, context.DeadlineExceeded):
return harnessTransport
default:
return ""
}
}
// wrapModelStageFailure preserves the underlying detail via %w while adding the
// stage key and, where known, the failure category. Budget/cleanup and other
// unclassified failures keep their previous single-prefix form.
func wrapModelStageFailure(key string, err error) error {
if err == nil {
return nil
}
switch modelFailureCategory(err) {
case finalResultValidation:
return fmt.Errorf("model stage %s: %s: %w; %s", key, finalResultValidation, err, finalResultGuidance)
case harnessTransport:
return fmt.Errorf("model stage %s: %s: %w", key, harnessTransport, err)
default:
return fmt.Errorf("model stage %s: %w", key, err)
}
}
// replayablePostAdmission reports whether a post-admission error can be retried
// from the start without risking a repeated tool mutation: a stage context
// deadline or a harness transport failure. A structured-output validation
// failure is classified separately as ErrInvalidModelResult.
func replayablePostAdmission(err error) bool {
return errors.Is(err, context.DeadlineExceeded) || errors.Is(err, ErrHarnessTransport)
}
// toleratedModelResultRetry reports whether a malformed final result may be
// re-emitted. It requires an explicit read-only stage, remaining attempts,
// live work, available budget and a plain validation error; fail-closed
// accounting and cleanup errors still stop immediately.
func toleratedModelResultRetry(ctx context.Context, def Definition, config ModelConfig, attempt int, resultErr error) bool {
return errors.Is(resultErr, ErrInvalidModelResult) &&
!errors.Is(resultErr, ErrBudgetExceeded) &&
!errors.Is(resultErr, ErrBudgetAccounting) &&
!errors.Is(resultErr, ErrStageCleanup) &&
safeToReplay(def) && attempt < config.Retries && ctx.Err() == nil && config.Budget != nil && config.Budget.Check() == nil
}
var providerID = regexp.MustCompile(`^[A-Za-z0-9_.-]+$`)
var modelID = regexp.MustCompile(`^[A-Za-z0-9_./:-]+$`)
func ValidModel(provider, model string) bool {
return len(provider) <= 200 && len(model) <= 300 && providerID.MatchString(provider) && modelID.MatchString(model)
}
// ModelConfig and ModelSpec group the settings/contracts that belong specifically
// to an LLM stage. Other stage types need not have a prompt, provider, or model.
type ModelConfig struct {
@@ -220,131 +119,3 @@ func (ModelStage) Spec(def Definition) (ModelSpec, error) {
Config: ModelConfig{Harness: backend, Provider: def.Configs["provider"], Model: def.Configs["model"], Retries: retries, Timeout: timeout, BudgetUSD: usd, BudgetTokens: tokens},
}, nil
}
func (m ModelStage) Run(ctx context.Context, def Definition, input Values) (Result, error) {
if err := ctx.Err(); err != nil {
return Result{}, err
}
spec, err := m.Spec(def)
if err != nil {
return Result{}, err
}
spec.Config.ResultSchema = resultSchema(def.Edges)
prompt, err := modelPrompt(def, spec, input)
if err != nil {
return Result{}, err
}
spec.Config.Budget = budgetFromContext(ctx)
if spec.Config.Budget == nil {
spec.Config.Budget = NewBudget(spec.Config.BudgetUSD, spec.Config.BudgetTokens)
}
client := m.Client
if client == nil {
if spec.Config.Harness == "pi" {
return Result{}, fmt.Errorf("Pi stages require an audited Pi client")
}
tools, _, _ := ToolNames(def)
if len(tools) > 0 {
return Result{}, fmt.Errorf("tool-enabled stages require an audited agent client")
}
client = &OpenCodeClient{}
}
if spec.Config.Budget.Enabled() {
capable, ok := client.(interface{ BudgetCapable() bool })
if !ok || !capable.BudgetCapable() {
return Result{}, fmt.Errorf("%w: generator cannot enforce usage limits", ErrBudgetAccounting)
}
}
// Replaying admitted tool work can repeat mutations. Only a stage whose
// explicit tools cannot mutate the run directory (safeToReplay) may
// automatically retry work that started after admission.
if agent, ok := client.(*AgentClient); ok {
if tools, explicit, _ := ToolNames(def); explicit {
agent.Tools = tools
}
agent.Policy, _ = StagePermissions(def)
}
for attempt := 0; attempt <= spec.Config.Retries; attempt++ {
if pi, ok := client.(*PiClient); ok {
pi.Policy, _ = StagePermissions(def)
}
if err := ctx.Err(); err != nil {
return Result{}, err
}
if err := spec.Config.Budget.Check(); err != nil {
return Result{}, err
}
spec.Config.Budget.Attempt()
attemptCtx, cancel := context.WithTimeout(ctx, spec.Config.Timeout)
text, generateErr := client.Generate(attemptCtx, spec.Config, prompt)
if generateErr == nil && attemptCtx.Err() != nil {
generateErr = attemptCtx.Err()
}
replayPostAdmission := false
if generateErr == nil {
var result Result
var resultErr error
if resultFormat(text) == "line" {
writeRawOutputFile(def.ArtifactsDir, text)
result, _, resultErr = decodeLineResult(text, def.Edges)
} else {
result, resultErr = decodeModelResult(stageResultText(text), def.Edges)
if resultErr != nil {
result, resultErr = repairModelResult(attemptCtx, client, spec.Config, text, def.Edges, resultErr)
}
}
if attemptCtx.Err() != nil {
resultErr = errors.Join(resultErr, attemptCtx.Err())
}
cancel()
if resultErr == nil {
return result, nil
}
// A malformed final result mutates nothing on an explicit read-only
// stage, so it may be re-emitted within the retry budget. A mutating
// or legacy-bundle stage retains its admitted work and returns now.
if toleratedModelResultRetry(ctx, def, spec.Config, attempt, resultErr) {
err = resultErr
} else {
return result, wrapModelStageFailure(def.Key, resultErr)
}
} else {
err = generateErr
if attemptCtx.Err() != nil && !errors.Is(err, ErrBudgetExceeded) && !errors.Is(err, ErrBudgetAccounting) {
err = errors.Join(err, attemptCtx.Err())
}
cancel()
if ctx.Err() != nil {
return Result{}, ctx.Err()
}
if errors.Is(err, ErrBudgetExceeded) || errors.Is(err, ErrBudgetAccounting) || errors.Is(err, ErrStageCleanup) {
break
}
replayPostAdmission = safeToReplay(def) && replayablePostAdmission(err)
// A reach failure observed before any work was admitted replays
// nothing, so it is retryable within the existing budget for any
// stage. After admission, only an explicit read-only stage may
// retry; admitted tool work is never replayed.
workStarted := false
if worker, ok := client.(admittedWorker); ok {
workStarted = worker.workAdmitted()
}
if workStarted && !replayPostAdmission {
break
}
preAdmissionReach := !workStarted && harnessReachFailure(err)
if attempt == spec.Config.Retries || (!retryableModelError(err) && !errors.Is(err, context.DeadlineExceeded) && !replayPostAdmission && !preAdmissionReach) {
break
}
}
delay := time.Duration(1<<min(attempt, 4)) * 100 * time.Millisecond
timer := time.NewTimer(delay)
select {
case <-ctx.Done():
timer.Stop()
return Result{}, ctx.Err()
case <-timer.C:
}
}
return Result{}, wrapModelStageFailure(def.Key, err)
}
+99
View File
@@ -0,0 +1,99 @@
package stage
import (
"context"
"errors"
"fmt"
)
// Retained model-stage failure categories. A strict final-result validation
// failure is safe and never replays admitted tool work; a harness transport,
// readback or wait failure is a different operator action. Labels are fixed and
// sanitized: no provider or model response body is copied into them.
const (
finalResultValidation = "FINAL RESULT VALIDATION"
harnessTransport = "HARNESS TRANSPORT"
)
// finalResultGuidance is a fixed sanitized sentence appended only to validation
// diagnostics. It states the retained side effects and the cost of a full retry.
const finalResultGuidance = "completed edits and tool side effects are retained and not undone; a full retry repeats work"
// harnessTransportFailure is the single classifier shared by both transport
// sentinels and the typed OpenCode error: a request, poll or readback failure
// that reports as the fixed HARNESS TRANSPORT category. A bare context deadline
// is classified separately so a genuine stage overrun is never relabeled as a
// transport failure.
func harnessTransportFailure(err error) bool {
var openCode *openCodeError
return errors.Is(err, ErrHarnessUnreachable) || errors.Is(err, ErrHarnessTransport) || errors.As(err, &openCode)
}
// harnessReachFailure reports whether err is a pre-admission harness reach
// failure. It is distinct from a post-admission ErrHarnessTransport failure: no
// session or prompt was admitted, so the retry replays no work.
func harnessReachFailure(err error) bool {
return errors.Is(err, ErrHarnessUnreachable)
}
// admittedWorker is implemented by a client that can admit tool work before its
// final result is read, so the retry loop can refuse to replay it without a
// per-client type switch.
type admittedWorker interface{ workAdmitted() bool }
// workAdmitted reports whether the client admitted any prompt or tool call, so
// the retry loop may retry a transport failure on any stage only when it is
// false, and only a read-only stage otherwise.
func (c *AgentClient) workAdmitted() bool { return c.WorkStarted }
func (c *PiClient) workAdmitted() bool { return c.WorkStarted }
// modelFailureCategory classifies a stage failure using error identity only, so
// the label stays sanitized and bounded. Validation is checked first: a
// structured-output failure is never reported as a transport failure and vice versa.
func modelFailureCategory(err error) string {
switch {
case errors.Is(err, ErrInvalidModelResult):
return finalResultValidation
case harnessTransportFailure(err), errors.Is(err, context.DeadlineExceeded):
return harnessTransport
default:
return ""
}
}
// wrapModelStageFailure preserves the underlying detail via %w while adding the
// stage key and, where known, the failure category. Budget/cleanup and other
// unclassified failures keep their previous single-prefix form.
func wrapModelStageFailure(key string, err error) error {
if err == nil {
return nil
}
switch modelFailureCategory(err) {
case finalResultValidation:
return fmt.Errorf("model stage %s: %s: %w; %s", key, finalResultValidation, err, finalResultGuidance)
case harnessTransport:
return fmt.Errorf("model stage %s: %s: %w", key, harnessTransport, err)
default:
return fmt.Errorf("model stage %s: %w", key, err)
}
}
// replayablePostAdmission reports whether a post-admission error can be retried
// from the start without risking a repeated tool mutation: a stage context
// deadline or a harness transport failure. A structured-output validation
// failure is classified separately as ErrInvalidModelResult.
func replayablePostAdmission(err error) bool {
return errors.Is(err, context.DeadlineExceeded) || errors.Is(err, ErrHarnessTransport)
}
// toleratedModelResultRetry reports whether a malformed final result may be
// re-emitted. It requires an explicit read-only stage, remaining attempts,
// live work, available budget and a plain validation error; fail-closed
// accounting and cleanup errors still stop immediately.
func toleratedModelResultRetry(ctx context.Context, def Definition, config ModelConfig, attempt int, resultErr error) bool {
return errors.Is(resultErr, ErrInvalidModelResult) &&
!errors.Is(resultErr, ErrBudgetExceeded) &&
!errors.Is(resultErr, ErrBudgetAccounting) &&
!errors.Is(resultErr, ErrStageCleanup) &&
safeToReplay(def) && attempt < config.Retries && ctx.Err() == nil && config.Budget != nil && config.Budget.Check() == nil
}
+136
View File
@@ -0,0 +1,136 @@
package stage
import (
"context"
"errors"
"fmt"
"time"
)
func (m ModelStage) Run(ctx context.Context, def Definition, input Values) (Result, error) {
if err := ctx.Err(); err != nil {
return Result{}, err
}
spec, err := m.Spec(def)
if err != nil {
return Result{}, err
}
spec.Config.ResultSchema = resultSchema(def.Edges)
prompt, err := modelPrompt(def, spec, input)
if err != nil {
return Result{}, err
}
spec.Config.Budget = budgetFromContext(ctx)
if spec.Config.Budget == nil {
spec.Config.Budget = NewBudget(spec.Config.BudgetUSD, spec.Config.BudgetTokens)
}
client := m.Client
if client == nil {
if spec.Config.Harness == "pi" {
return Result{}, fmt.Errorf("Pi stages require an audited Pi client")
}
tools, _, _ := ToolNames(def)
if len(tools) > 0 {
return Result{}, fmt.Errorf("tool-enabled stages require an audited agent client")
}
client = &OpenCodeClient{}
}
if spec.Config.Budget.Enabled() {
capable, ok := client.(interface{ BudgetCapable() bool })
if !ok || !capable.BudgetCapable() {
return Result{}, fmt.Errorf("%w: generator cannot enforce usage limits", ErrBudgetAccounting)
}
}
// Replaying admitted tool work can repeat mutations. Only a stage whose
// explicit tools cannot mutate the run directory (safeToReplay) may
// automatically retry work that started after admission.
if agent, ok := client.(*AgentClient); ok {
if tools, explicit, _ := ToolNames(def); explicit {
agent.Tools = tools
}
agent.Policy, _ = StagePermissions(def)
}
for attempt := 0; attempt <= spec.Config.Retries; attempt++ {
if pi, ok := client.(*PiClient); ok {
pi.Policy, _ = StagePermissions(def)
}
if err := ctx.Err(); err != nil {
return Result{}, err
}
if err := spec.Config.Budget.Check(); err != nil {
return Result{}, err
}
spec.Config.Budget.Attempt()
attemptCtx, cancel := context.WithTimeout(ctx, spec.Config.Timeout)
text, generateErr := client.Generate(attemptCtx, spec.Config, prompt)
if generateErr == nil && attemptCtx.Err() != nil {
generateErr = attemptCtx.Err()
}
replayPostAdmission := false
if generateErr == nil {
var result Result
var resultErr error
if resultFormat(text) == "line" {
writeRawOutputFile(def.ArtifactsDir, text)
result, _, resultErr = decodeLineResult(text, def.Edges)
} else {
result, resultErr = decodeModelResult(stageResultText(text), def.Edges)
if resultErr != nil {
result, resultErr = repairModelResult(attemptCtx, client, spec.Config, text, def.Edges, resultErr)
}
}
if attemptCtx.Err() != nil {
resultErr = errors.Join(resultErr, attemptCtx.Err())
}
cancel()
if resultErr == nil {
return result, nil
}
// A malformed final result mutates nothing on an explicit read-only
// stage, so it may be re-emitted within the retry budget. A mutating
// or legacy-bundle stage retains its admitted work and returns now.
if toleratedModelResultRetry(ctx, def, spec.Config, attempt, resultErr) {
err = resultErr
} else {
return result, wrapModelStageFailure(def.Key, resultErr)
}
} else {
err = generateErr
if attemptCtx.Err() != nil && !errors.Is(err, ErrBudgetExceeded) && !errors.Is(err, ErrBudgetAccounting) {
err = errors.Join(err, attemptCtx.Err())
}
cancel()
if ctx.Err() != nil {
return Result{}, ctx.Err()
}
if errors.Is(err, ErrBudgetExceeded) || errors.Is(err, ErrBudgetAccounting) || errors.Is(err, ErrStageCleanup) {
break
}
replayPostAdmission = safeToReplay(def) && replayablePostAdmission(err)
// A reach failure observed before any work was admitted replays
// nothing, so it is retryable within the existing budget for any
// stage. After admission, only an explicit read-only stage may
// retry; admitted tool work is never replayed.
workStarted := false
if worker, ok := client.(admittedWorker); ok {
workStarted = worker.workAdmitted()
}
if workStarted && !replayPostAdmission {
break
}
preAdmissionReach := !workStarted && harnessReachFailure(err)
if attempt == spec.Config.Retries || (!retryableModelError(err) && !errors.Is(err, context.DeadlineExceeded) && !replayPostAdmission && !preAdmissionReach) {
break
}
}
delay := time.Duration(1<<min(attempt, 4)) * 100 * time.Millisecond
timer := time.NewTimer(delay)
select {
case <-ctx.Done():
timer.Stop()
return Result{}, ctx.Err()
case <-timer.C:
}
}
return Result{}, wrapModelStageFailure(def.Key, err)
}
+12
View File
@@ -0,0 +1,12 @@
package stage
import (
"regexp"
)
var providerID = regexp.MustCompile(`^[A-Za-z0-9_.-]+$`)
var modelID = regexp.MustCompile(`^[A-Za-z0-9_./:-]+$`)
func ValidModel(provider, model string) bool {
return len(provider) <= 200 && len(model) <= 300 && providerID.MatchString(provider) && modelID.MatchString(model)
}