Let runs carry an operator title shown in the CLI and TUI

Add a bounded, never-empty title to every run. `pipelines run --title "..."`
sets it explicitly and skips model generation; otherwise the server generates
it from the prompt through the shared summariser and falls back to the prompt's
first line, recording title_source (model/fallback/explicit) and title_model.

- protocol/store: RunSummary.Title plus Label(), a set_run_title message, title
  columns with an idempotent migration, CreateRunTitled, and SetRunTitle renames.
- summarise: Title/GenerateTitle/DeriveTitle/ExplicitTitle bounded to TitleCap.
- runner: generate the title outside the admission lock and only for a request
  that starts a run; a replay reuses the stored title and an explicit title
  skips the model entirely.
- CLI: `--title` on run and a `title <run-id> "<title>"` rename command; human
  `runs` and `inspect` show the title beside the pipeline name and version.
- TUI: the run list row, run detail header, Home run cards and last-run line,
  and the pipeline Results line show the title via Label().

Tests cover storage/migration/fallback, explicit-title and rename, the
end-to-end CLI flow, and title rendering; CLI and operations docs updated.
This commit is contained in:
2026-10-16 13:35:43 +01:00
parent 0e6fbcc062
commit f3e0593f4d
23 changed files with 916 additions and 74 deletions
+4
View File
@@ -344,6 +344,10 @@ Retries restart at the entry stage with fresh OpenCode sessions, never the previ
conversation or intermediate stage output. Pipelines does not automatically delete
generation, agent, or formatting-repair sessions, including on failure/cancellation.
Primary session IDs are retained in run steps (`pipelines inspect <run-id> --json`).
Every run also records a short, single-line title: `pipelines run ... --title "…"`
sets one explicitly, otherwise it is generated from the prompt. `pipelines runs`
and `pipelines inspect` show it beside the pipeline name and version, and
`pipelines title <run-id> "…"` renames it afterwards.
On Home, **r** opens global run history; **/** fuzzy-searches it. Inside a pipeline,
**r** / **:runs** opens only that pipeline's history.
Quit the TUI with **:quit**; **q** does not exit. **Ctrl+C** remains an interrupt.
+5 -1
View File
@@ -16,6 +16,7 @@ import (
"git.a-wan.net/alan/pipelines/internal/paths"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/server"
"git.a-wan.net/alan/pipelines/internal/summarise"
)
func main() {
@@ -37,7 +38,10 @@ func main() {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
artifactsRoot := filepath.Join(filepath.Dir(*db), "artifacts")
options := server.Options{ArtifactsRoot: artifactsRoot}
// Each new run gets a bounded one-line title from the configured default
// harness; the summariser falls back to the prompt's first line, so title
// generation never blocks or fails run creation.
options := server.Options{ArtifactsRoot: artifactsRoot, RunTitles: summarise.GenerateTitle}
// Chat brokers OpenCode over the owned endpoint. Streaming needs a
// URL-bearing, SSE-capable backend. Detect may select "v2" (the CLI
// advertises `api`), but Open never starts a serve process for v2 and so
+14 -1
View File
@@ -198,8 +198,9 @@ Submitting activates included schedules, so review them before publishing.
| Browse templates/source | `templates --json`, `template KEY --source` |
| Create or edit a definition | `new`, `edit`, `drafts` |
| Validate and publish | `validate FILE`, `submit FILE` |
| Start a run | `run NAME PROMPT --yes --json` |
| Start a run | `run NAME PROMPT [--title TITLE] --yes --json` |
| Run history/inspection | `runs --json`, `inspect ID --json` |
| Rename a run | `title ID "TITLE" --json` |
| Cancel | `cancel ID --json` |
| Connection and phone settings | `connection --json`, `ping --json` |
| Server/ntfy status popup | `status --json` |
@@ -224,6 +225,18 @@ transient client logs are presentation, not separate server capabilities.
## Scripted runs
Every run records a short, single-line `title` saying what it was for. `pipelines
run --title "..."` sets it explicitly; otherwise the server generates it from the
prompt with the configured default harness. It is never empty: when generation is
unavailable or times out, the server derives the prompt's first line, and
`title_source` (`model`, `fallback`, or `explicit` for an operator-set title) plus
`title_model` record how it was produced so a diagnosis can tell them apart.
`pipelines runs` shows the title beside the pipeline name and version, and
`pipelines inspect` returns the title, name and version, so two runs of one
pipeline are distinguishable. Rename after the fact with `pipelines title
<run-id> "<title>"`; the run's input, steps and execution are unchanged. The
stored prompt is unchanged, and listing/renaming never calls a model.
On either the runs list or run inspector, press `P` (or use `:pipeline`) to open
the selected run's exact pipeline version. Navigation does not cancel the run.
+11 -1
View File
@@ -70,7 +70,17 @@ delete databases, sessions, volumes or credentials to repair a workflow.
Use the authoritative `CLI_PREFIX` from the prompt:
- `CLI_PREFIX runs --json`: run history (optional `--pipeline NAME` scope).
- `CLI_PREFIX runs --json`: run history (optional `--pipeline NAME` scope). Each run
carries a bounded, single-line `title` saying what it was for, set explicitly with
`CLI_PREFIX run ... --title "..."` or generated from its prompt by the shared
summariser on the configured default harness. It is never empty: when generation is
unavailable or times out the server derives the prompt's first line, and
`title_source` (`model`, `fallback`, or `explicit`) plus `title_model` record how it
was produced, so a diagnosis can tell them apart. Human `runs` and `inspect` show the
title beside the pipeline name and version, so two runs of one pipeline are
distinguishable; `CLI_PREFIX title RUN_ID "..."` renames a run after the fact
without changing its input or steps. The stored prompt is unchanged, and
listing/renaming never calls a model.
- `CLI_PREFIX inspect RUN_ID --json`: selected run, visits and retained evidence.
Exact stage outputs are authoritative; a transcript over the 32 KiB retained
bound is a valid, credential-redacted preview envelope (with preserved stage
+32 -11
View File
@@ -33,14 +33,14 @@ type App struct {
In io.Reader
}
type options struct {
harness string
url, name, prompt, template, workspace, paths, key, opencode, model string
output string
conflict string
all bool
json, source, noOpen, yes, allow, createOnly, force, wait bool
version, expected int
timeout, waitTimeout time.Duration
harness string
url, name, prompt, title, template, workspace, paths, key, opencode, model string
output string
conflict string
all bool
json, source, noOpen, yes, allow, createOnly, force, wait bool
version, expected int
timeout, waitTimeout time.Duration
}
func (a App) flags(command string) (*flag.FlagSet, *options) {
@@ -89,6 +89,7 @@ func (a App) flags(command string) (*flag.FlagSet, *options) {
case "run":
f.StringVar(&o.name, "pipeline", "", "Exact registered pipeline name or ID")
f.StringVar(&o.prompt, "prompt", "", "Task prompt; omitted opens a multiline composer")
f.StringVar(&o.title, "title", "", "Explicit run title; default is derived from the prompt")
f.StringVar(&o.workspace, "workspace", "", "Run directory (default: current directory)")
f.BoolVar(&o.yes, "yes", false, "Confirm execution without an interactive prompt")
f.BoolVar(&o.allow, "allow-tools", false, "Compatibility flag; stage-declared tools are enabled automatically")
@@ -658,7 +659,7 @@ func (a App) Run(ctx context.Context, args []string) error {
}
case "runs":
for _, run := range r.Runs {
fmt.Fprintf(a.Out, "%d\t%s\tv%d\t%s\n", run.ID, run.Name, run.Version, run.Status)
fmt.Fprintf(a.Out, "%d\t%s\t%s\tv%d\t%s\n", run.ID, run.Label(), run.Name, run.Version, run.Status)
}
case "ping":
fmt.Fprintln(a.Out, "Connected ·", r.Time)
@@ -777,6 +778,26 @@ func (a App) Run(ctx context.Context, args []string) error {
return a.finishRun(ctx, r, o, call)
}
return a.output(r.RunDetail)
case "title":
if len(pos) != 2 {
return fmt.Errorf("title requires a run ID and a title")
}
id, err := strconv.ParseInt(pos[0], 10, 64)
if err != nil || id <= 0 {
return fmt.Errorf("run ID must be positive")
}
r, err := call(protocol.Message{Type: protocol.SetRunTitle, RunID: id, Title: pos[1]})
if err != nil {
return err
}
if r.RunDetail == nil {
return fmt.Errorf("server did not return run detail")
}
if o.json {
return a.output(r.RunDetail)
}
_, err = fmt.Fprintf(a.Out, "Renamed run #%d to %s\n", r.RunDetail.ID, r.RunDetail.Label())
return err
case "rpc":
path, err := one()
if err != nil {
@@ -995,7 +1016,7 @@ func (a App) run(ctx context.Context, pos []string, o *options, call callFunc, r
if o.wait || o.key != "" || o.version > 0 {
return fmt.Errorf("--wait, --request-key and --version require --json")
}
_, err := tea.NewProgram(tui.NewRun(ctx, o.url, tui.RunOptions{Pipeline: o.name, Prompt: o.prompt, Workspace: workspace, Yes: o.yes, AllowTools: o.allow, WritePaths: paths}), tea.WithContext(ctx), tea.WithAltScreen()).Run()
_, err := tea.NewProgram(tui.NewRun(ctx, o.url, tui.RunOptions{Pipeline: o.name, Prompt: o.prompt, Title: o.title, Workspace: workspace, Yes: o.yes, AllowTools: o.allow, WritePaths: paths}), tea.WithContext(ctx), tea.WithAltScreen()).Run()
return err
}
if o.name == "" || strings.TrimSpace(o.prompt) == "" {
@@ -1016,7 +1037,7 @@ func (a App) run(ctx context.Context, pos []string, o *options, call callFunc, r
if key == "" {
key = fmt.Sprintf("cli-%d-%d", os.Getpid(), time.Now().UnixNano())
}
r, err := call(protocol.Message{Type: protocol.RunPipeline, PipelineID: p.ID, Version: version, RequestKey: key, Workspace: workspace, Input: map[string]any{"prompt": o.prompt}, AllowTools: o.allow, WritePaths: paths})
r, err := call(protocol.Message{Type: protocol.RunPipeline, PipelineID: p.ID, Version: version, RequestKey: key, Workspace: workspace, Title: o.title, Input: map[string]any{"prompt": o.prompt}, AllowTools: o.allow, WritePaths: paths})
if err != nil {
return err
}
+4 -2
View File
@@ -16,11 +16,12 @@ var usage = map[string]string{
"import": "import [FILE.zip] [--all] [--on-conflict ask|keep|both|import] [--yes] [--json]",
"templates": "templates [--json]",
"template": "template <key> [--source | --json]",
"run": "run [name|id] [prompt] [--json [--wait [--wait-timeout DURATION]]]",
"run": "run [name|id] [prompt] [--title TITLE] [--json [--wait [--wait-timeout DURATION]]]",
"runs": "runs [--pipeline NAME] [--json]",
"inspect": "inspect <run-id> [--json]",
"cancel": "cancel <run-id> [--json]",
"retry": "retry <run-id> [--json [--wait [--wait-timeout DURATION]]]",
"title": "title <run-id> \"<title>\" [--json]",
"debug": "debug <run-id> [--json | --no-open]",
"connection": "connection [--json]",
"ping": "ping [--json]",
@@ -40,8 +41,9 @@ var notes = map[string]string{
"edit": "Omit the name to open fuzzy selection. Original immutable source is copied to a private draft. The configured harness edits that draft, never the live definition.",
"validate": "Uses the same server parser as submission. Does not persist or activate schedules. Structural validity does not prove runtime/tool availability.",
"submit": "Confirm publication at the prompt, or use --yes for explicit automation; schedules activate on acceptance. Managed drafts provide the name and base-version guard. --force never bypasses validation.",
"run": "Runs in your current directory after action confirmation (--yes skips it). Select a pipeline and compose a multiline task with Ctrl+S. Stage tools are enabled automatically; no file list or typed RUN confirmation. Cancellation does not undo edits. In JSON mode --wait polls until terminal; --wait-timeout bounds only the client wait after admission and is independent of per-request --timeout. A reached wait limit leaves the run active: inspect read-only with `inspect <run-id> --json`, or reattach with --wait reusing the original --request-key (never an automatic replay or cancel).",
"run": "Runs in your current directory after action confirmation (--yes skips it). Select a pipeline and compose a multiline task with Ctrl+S. Stage tools are enabled automatically; no file list or typed RUN confirmation. Cancellation does not undo edits. --title sets an explicit run title; without it the title is derived from the prompt. In JSON mode --wait polls until terminal; --wait-timeout bounds only the client wait after admission and is independent of per-request --timeout. A reached wait limit leaves the run active: inspect read-only with `inspect <run-id> --json`, or reattach with --wait reusing the original --request-key (never an automatic replay or cancel).",
"retry": "Start a new attempt of a failed run with the same initial input, directory, pipeline version, and edit scope. Original history is retained. Already-completed side effects are not undone. --wait/--wait-timeout behave as for run: a reached wait limit leaves the new run active; inspect read-only with `inspect <run-id> --json`, or reattach with --wait and the original --request-key instead of starting a duplicate retry.",
"title": "Rename a run's operator-facing title after the fact without touching its input, steps or execution. The title is collapsed to a single bounded line and recorded as explicitly set. `pipelines runs` and `pipelines inspect` then show it.",
"debug": "Open a fresh configured harness conversation to investigate a run using inspect/show/runs CLI queries. --harness opencode|pi|nvim chooses another configured harness or read-only nvim packet. Does not resume implementation or retry. --json returns the prompt without launching anything.",
"rpc": "Accepts a JSON protocol envelope; '-' reads stdin. Every TUI server operation is available here too. Mutating commands are not dry runs.",
}
+1
View File
@@ -27,6 +27,7 @@ EXECUTE
inspect <run-id> Inspect stages, outputs, errors and status
cancel <run-id> Cancel a run; changes are NOT rolled back
retry <run-id> Restart a failed run with its original input and directory
title <run-id> "<title>" Rename a run after the fact; input/steps unchanged
debug <run-id> Investigate a run with a configured harness or nvim
draft-delete <draft.md> Delete a selected retained draft and its metadata
connection Server and public phone connection (no secrets)
+93 -4
View File
@@ -6,15 +6,18 @@ 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 '',
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,
@@ -44,10 +47,65 @@ run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, backend TEXT NOT NULL, refer
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
}
return s.migrateRunTitles()
}
// 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
}
// 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, summarise.Title{}, retryOf...)
}
// CreateRunTitled inserts a run with the caller's title. 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.
func (s *Store) CreateRunTitled(ctx context.Context, d *protocol.PipelineDetail, key, workspace string, tools bool, paths []string, input map[string]any, title summarise.Title, retryOf ...int64) (int64, bool, error) {
var parent int64
if len(retryOf) > 0 {
parent = retryOf[0]
@@ -55,6 +113,7 @@ func (s *Store) CreateRun(ctx context.Context, d *protocol.PipelineDetail, key,
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
@@ -88,7 +147,7 @@ func (s *Store) CreateRun(ctx context.Context, d *protocol.PipelineDetail, key,
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) VALUES (?,?,?,?,?,?,?)", d.ID, d.Version, key, workspace, tools, string(writes), string(encoded))
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) VALUES (?,?,?,?,?,?,?,?,?,?)", d.ID, d.Version, key, workspace, tools, string(writes), string(encoded), title.Text, title.Source, title.Model)
if err != nil {
return 0, false, err
}
@@ -107,15 +166,45 @@ func (s *Store) CreateRun(ctx context.Context, d *protocol.PipelineDetail, key,
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
}
const runSelect = `SELECT r.id,r.pipeline_id,p.name,r.version,v.content_hash,r.status,r.outcome,r.current_stage,
strftime('%Y-%m-%dT%H:%M:%SZ',r.created_at),COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ',r.finished_at),''),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,
(SELECT COUNT(*) FROM run_steps WHERE run_id=r.id AND status='completed')
(SELECT COUNT(*) FROM run_steps WHERE run_id=r.id AND status='completed'),r.workspace,r.title
FROM pipeline_runs r JOIN pipelines p ON p.id=r.pipeline_id JOIN pipeline_versions v ON v.pipeline_id=r.pipeline_id AND v.version=r.version`
func scanRun(row interface{ Scan(...any) error }, r *protocol.RunSummary) error {
var output string
if err := row.Scan(&r.ID, &r.PipelineID, &r.Name, &r.Version, &r.ContentHash, &r.Status, &r.Outcome, &r.CurrentStage, &r.CreatedAt, &r.FinishedAt, &r.Diagnostic, &output, &r.CompletedSteps); err != nil {
if err := row.Scan(&r.ID, &r.PipelineID, &r.Name, &r.Version, &r.ContentHash, &r.Status, &r.Outcome, &r.CurrentStage, &r.CreatedAt, &r.FinishedAt, &r.Diagnostic, &output, &r.CompletedSteps, &r.Workspace, &r.Title); err != nil {
return err
}
var values map[string]any
@@ -179,7 +268,7 @@ func (s *Store) GetRun(ctx context.Context, id int64) (*protocol.RunDetail, erro
if err := json.Unmarshal([]byte(graphJSON), &r.Graph); err != nil {
return nil, err
}
if err := tx.QueryRowContext(ctx, "SELECT workspace,allow_tools,write_paths,input_json,output_json,COALESCE((SELECT original_run_id FROM run_retries WHERE run_id=pipeline_runs.id),0) FROM pipeline_runs WHERE id=?", id).Scan(&r.Workspace, &r.AllowTools, &paths, &input, &output, &r.RetryOf); err != nil {
if err := tx.QueryRowContext(ctx, "SELECT workspace,allow_tools,write_paths,input_json,output_json,COALESCE((SELECT original_run_id FROM run_retries WHERE run_id=pipeline_runs.id),0),title_source,title_model FROM pipeline_runs WHERE id=?", id).Scan(&r.Workspace, &r.AllowTools, &paths, &input, &output, &r.RetryOf, &r.TitleSource, &r.TitleModel); err != nil {
return nil, err
}
if err := decodeValues(input, &r.Input); err != nil {
+123
View File
@@ -5,7 +5,10 @@ import (
"fmt"
"path/filepath"
"reflect"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/summarise"
)
func TestRunListIncludesHistoryBeyondFiftyForSearchAndPipelineScope(t *testing.T) {
@@ -37,6 +40,126 @@ func TestRunListIncludesHistoryBeyondFiftyForSearchAndPipelineScope(t *testing.T
}
}
// TestRunTitleStorageMigrationAndFallback proves the three title contracts: an
// explicit title is kept verbatim, an absent one is derived bounded and
// non-empty, and runs created before the title columns survive the migration and
// still list.
func TestRunTitleStorageMigrationAndFallback(t *testing.T) {
ctx := context.Background()
path := filepath.Join(t.TempDir(), "titles.db")
store, err := Open(path)
if err != nil {
t.Fatal(err)
}
if _, err := store.Submit(ctx, "titled", Template([]string{"one"}, true)); err != nil {
t.Fatal(err)
}
items, _ := store.List(ctx)
definition, _ := store.Get(ctx, items[0].ID, 1)
explicit, _, err := store.CreateRunTitled(ctx, definition, "explicit", "workspace", false, nil,
map[string]any{"prompt": "a prompt that must not become the title"},
summarise.Title{Text: "Fix the flaky login test", Source: summarise.SourceModel, Model: "openai/gpt"})
if err != nil {
t.Fatal(err)
}
got, err := store.GetRun(ctx, explicit)
if err != nil {
t.Fatal(err)
}
if got.Title != "Fix the flaky login test" || got.TitleSource != summarise.SourceModel || got.TitleModel != "openai/gpt" {
t.Fatalf("explicit title not kept verbatim with its provenance: %+v", got)
}
derived, _, err := store.CreateRun(ctx, definition, "derived", "workspace", false, nil,
map[string]any{"prompt": strings.Repeat("é", 500) + "\nsecond line is ignored"})
if err != nil {
t.Fatal(err)
}
got, err = store.GetRun(ctx, derived)
if err != nil {
t.Fatal(err)
}
if got.Title == "" || got.TitleSource != summarise.SourceFallback || got.TitleModel != "" {
t.Fatalf("derived title = %+v, want a non-empty fallback with no model", got)
}
if len([]rune(got.Title)) > summarise.TitleCap {
t.Fatalf("derived title has %d runes, want <= %d", len([]rune(got.Title)), summarise.TitleCap)
}
// Simulate a database created before run titles by removing the columns, then
// reopen: existing rows must survive the re-add migration and still list.
for _, column := range []string{"title", "title_source", "title_model"} {
if _, err := store.db.ExecContext(ctx, "ALTER TABLE pipeline_runs DROP COLUMN "+column); err != nil {
t.Fatal(err)
}
}
if err := store.Close(); err != nil {
t.Fatal(err)
}
store, err = Open(path)
if err != nil {
t.Fatal(err)
}
defer store.Close()
runs, err := store.ListRuns(ctx)
if err != nil {
t.Fatal("legacy runs did not survive the title migration", err)
}
ids := map[int64]bool{}
for _, run := range runs {
ids[run.ID] = true
}
if !ids[explicit] || !ids[derived] {
t.Fatalf("migrated run list lost history: %+v", runs)
}
migrated, err := store.GetRun(ctx, explicit)
if err != nil || migrated.Title != "" || migrated.TitleSource != "" || migrated.TitleModel != "" {
t.Fatalf("migrated row should keep a valid empty title: %+v %v", migrated, err)
}
}
// TestSetRunTitleRenamesWithoutTouchingInput proves an after-the-fact rename
// replaces the stored title and its provenance and leaves the run's input,
// status and steps untouched; an unknown run is reported, not silently ignored.
func TestSetRunTitleRenamesWithoutTouchingInput(t *testing.T) {
ctx := context.Background()
store, err := Open(filepath.Join(t.TempDir(), "rename.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
if _, err := store.Submit(ctx, "rename", Template([]string{"one"}, true)); err != nil {
t.Fatal(err)
}
items, _ := store.List(ctx)
definition, _ := store.Get(ctx, items[0].ID, 1)
id, _, err := store.CreateRun(ctx, definition, "rename-key", "workspace", false, nil, map[string]any{"prompt": "original derived prompt"})
if err != nil {
t.Fatal(err)
}
before, err := store.GetRun(ctx, id)
if err != nil {
t.Fatal(err)
}
if err := store.SetRunTitle(ctx, id, summarise.Title{Text: "Investigate the regression", Source: summarise.SourceExplicit}); err != nil {
t.Fatal(err)
}
after, err := store.GetRun(ctx, id)
if err != nil {
t.Fatal(err)
}
if after.Title != "Investigate the regression" || after.TitleSource != summarise.SourceExplicit || after.TitleModel != "" {
t.Fatalf("rename = %+v, want the explicit title and provenance", after)
}
if after.Status != before.Status || !reflect.DeepEqual(after.Input, before.Input) || len(after.Steps) != len(before.Steps) {
t.Fatalf("rename changed run state: before %+v after %+v", before, after)
}
if err := store.SetRunTitle(ctx, id+999, summarise.Title{Text: "ghost", Source: summarise.SourceExplicit}); err == nil {
t.Fatal("renaming an unknown run succeeded")
}
}
func TestRunRecoveryAndVersionPinning(t *testing.T) {
ctx := context.Background()
path := filepath.Join(t.TempDir(), "audit.db")
+1
View File
@@ -121,6 +121,7 @@ type Message struct {
Error string `json:"error,omitempty"`
Name string `json:"name,omitempty"`
Source string `json:"source,omitempty"`
Title string `json:"title,omitempty"`
Version int `json:"version,omitempty"`
Pipelines []PipelineSummary `json:"pipelines,omitempty"`
PipelineID int64 `json:"pipeline_id,omitempty"`
+27 -3
View File
@@ -1,5 +1,7 @@
package protocol
import "strings"
const (
RunPipeline = "run_pipeline"
RunStarted = "run_started"
@@ -9,6 +11,7 @@ const (
Run = "run"
CancelRun = "cancel_run"
RetryRun = "retry_run"
SetRunTitle = "set_run_title"
GetRunEvidence = "get_run_evidence"
RunEvidence = "run_evidence"
)
@@ -23,15 +26,20 @@ type SessionEvidence struct {
}
type RunSummary struct {
ID int64 `json:"id"`
PipelineID int64 `json:"pipeline_id"`
Name string `json:"name"`
ID int64 `json:"id"`
PipelineID int64 `json:"pipeline_id"`
Name string `json:"name"`
// Title is the operator-facing, bounded single-line description of what the
// run is for, generated from its prompt. It is never empty for a new run;
// historical rows predating the title column may omit it.
Title string `json:"title,omitempty"`
Version int `json:"version"`
ContentHash string `json:"content_hash"`
Status string `json:"status"`
Executing bool `json:"executing"`
Outcome string `json:"outcome"`
CurrentStage string `json:"current_stage"`
Workspace string `json:"workspace,omitempty"`
CreatedAt string `json:"created_at"`
FinishedAt string `json:"finished_at"`
Diagnostic string `json:"diagnostic"`
@@ -39,6 +47,16 @@ type RunSummary struct {
CompletedSteps int `json:"completed_steps"`
}
// Label is the operator-facing name of a run: the generated Title when the
// server stored one, otherwise the pipeline Name (historical rows predate
// titles). It is the single definition shared by the CLI and TUI.
func (r RunSummary) Label() string {
if title := strings.TrimSpace(r.Title); title != "" {
return title
}
return r.Name
}
type RunStep struct {
Control *ControlEvidence `json:"control,omitempty"`
FileChanges *StageFileChanges `json:"file_changes,omitempty"`
@@ -126,4 +144,10 @@ type RunDetail struct {
Input map[string]any `json:"input"`
Output map[string]any `json:"output"`
Steps []RunStep `json:"steps"`
// TitleSource is "model" when Title came from the shared summariser or
// "fallback" when it was derived from the prompt; TitleModel names the model
// that produced a model title. They are retained so a diagnosis can tell the
// two apart and see which model was used.
TitleSource string `json:"title_source,omitempty"`
TitleModel string `json:"title_model,omitempty"`
}
+102
View File
@@ -11,6 +11,7 @@ import (
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/runinspect"
"git.a-wan.net/alan/pipelines/internal/stage"
"git.a-wan.net/alan/pipelines/internal/summarise"
"net/http/httptest"
"os"
"path/filepath"
@@ -143,3 +144,104 @@ func TestCLIHeadlessRunInspectionCancellationAndRPC(t *testing.T) {
t.Fatal("wait lost terminal state", output)
}
}
// TestCLIRunTitlesExplicitDerivedAndRename is the end-to-end title check: it
// drives the real CLI against the real websocket server and store, starts one
// run with --title and one without, and verifies the title is visible in the
// human `runs` list, in `inspect`, and after a `title` rename. Stage execution is
// stubbed because the environment must not call a model, so the without-flag run
// exercises the server-side prompt fallback rather than the shared summariser.
func TestCLIRunTitlesExplicitDerivedAndRename(t *testing.T) {
t.Setenv("PIPELINES_HARNESS_CONFIG", filepath.Join(t.TempDir(), "harnesses.json"))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
store, err := pipeline.Open(filepath.Join(t.TempDir(), "cli-titles.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
if _, err := store.Submit(ctx, "workflow", pipeline.Template([]string{"worker"}, true)); err != nil {
t.Fatal(err)
}
handler := Handler(ctx, store)
r := handler.(*runHandler).runner
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
return stage.Result{Outcome: "success", Output: stage.Values{"summary": "done"}}, nil
}
http := httptest.NewServer(handler)
defer http.Close()
defer func() { cancel(); WaitRuns(handler) }()
url := "ws" + strings.TrimPrefix(http.URL, "http") + "/connect"
run := func(args ...string) (string, error) {
var out bytes.Buffer
app := cli.App{Out: &out, Err: &out, In: strings.NewReader("")}
err := app.Run(ctx, append([]string{"--url", url}, args...))
return out.String(), err
}
output, err := run("run", "workflow", "first prompt line\nmore detail", "--title", "Operator chosen title", "--yes", "--json", "--wait")
if err != nil {
t.Fatal(err)
}
var started protocol.Message
if err := json.Unmarshal([]byte(output), &started); err != nil {
t.Fatal(err)
}
explicitID := started.RunDetail.ID
if started.RunDetail.Title != "Operator chosen title" || started.RunDetail.TitleSource != summarise.SourceExplicit {
t.Fatalf("explicit --title = %+v", started.RunDetail)
}
output, err = run("run", "workflow", "Derived title from prompt\nsecond line", "--yes", "--json", "--wait")
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal([]byte(output), &started); err != nil {
t.Fatal(err)
}
if started.RunDetail.Title != "Derived title from prompt" || started.RunDetail.TitleSource != summarise.SourceFallback {
t.Fatalf("derived title = %+v", started.RunDetail)
}
output, err = run("runs")
if err != nil {
t.Fatal(err)
}
t.Logf("pipelines runs:\n%s", output)
for _, want := range []string{
fmt.Sprintf("%d\t%s\t%s\tv1\t%s\n", explicitID, "Operator chosen title", "workflow", "succeeded"),
fmt.Sprintf("%d\t%s\t%s\tv1\t%s\n", started.RunDetail.ID, "Derived title from prompt", "workflow", "succeeded"),
} {
if !strings.Contains(output, want) {
t.Fatalf("runs output missing %q:\n%s", want, output)
}
}
output, err = run("inspect", fmt.Sprint(explicitID), "--json")
if err != nil {
t.Fatal(err)
}
var detail protocol.RunDetail
if err := json.Unmarshal([]byte(output), &detail); err != nil {
t.Fatal(err)
}
if detail.Title != "Operator chosen title" || detail.Name != "workflow" || detail.Version != 1 {
t.Fatalf("inspect omitted title/name/version: %+v", detail)
}
output, err = run("title", fmt.Sprint(explicitID), "Renamed after the fact")
if err != nil {
t.Fatal(err)
}
t.Logf("pipelines title: %s", strings.TrimSpace(output))
output, err = run("inspect", fmt.Sprint(explicitID), "--json")
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal([]byte(output), &detail); err != nil {
t.Fatal(err)
}
if detail.Title != "Renamed after the fact" || detail.TitleSource != summarise.SourceExplicit {
t.Fatalf("rename not visible in inspect: %+v", detail)
}
}
+76 -18
View File
@@ -18,6 +18,7 @@ import (
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
"git.a-wan.net/alan/pipelines/internal/summarise"
)
// activeRun records one in-flight run's cancellation seam and the resolved
@@ -35,8 +36,12 @@ type runner struct {
mu sync.Mutex
active map[int64]activeRun
execute func(context.Context, stage.Definition, stage.Values) (stage.Result, error)
wg sync.WaitGroup
schedulerWG sync.WaitGroup
// title generates the operator-facing run title from the run prompt. When nil
// the store derives a fallback from the prompt with no model call, so tests
// and unconfigured servers stay deterministic.
title func(context.Context, string) summarise.Title
wg sync.WaitGroup
schedulerWG sync.WaitGroup
}
func newRunner(ctx context.Context, s *pipeline.Store) (*runner, error) {
@@ -158,13 +163,19 @@ func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.Ru
}
}
}
// Decide admission and idempotency under the lock, but generate the title
// only for a request that will actually start a run: a rejected or replayed
// request must not pay for a bounded model generation, and a live one must
// not hold the lock (stalling listings and cancellation) while a model is
// called. An unavailable or slow model falls back to the prompt's first line.
r.mu.Lock()
defer r.mu.Unlock()
if r.ctx.Err() != nil {
r.mu.Unlock()
return nil, r.ctx.Err()
}
owners, err := r.store.ActiveSessionOwners(r.ctx)
if err != nil {
r.mu.Unlock()
return nil, err
}
// Refuse only an orphaned session: one whose owning run is not active now.
@@ -172,28 +183,49 @@ func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.Ru
// workspace) must not block new work.
for _, owner := range owners {
if _, ok := r.active[owner.RunID]; !ok {
r.mu.Unlock()
return nil, fmt.Errorf("an agent may still be active; restart recovery must interrupt it before new work")
}
}
// Idempotent repeats are accepted even while the original run is active.
if len(r.active) > 0 {
existing, err := r.store.RunByKey(r.ctx, request.RequestKey)
if err != nil {
// Idempotent repeats are accepted even while the original run is active, and
// a replay reuses the stored title instead of generating a new one.
existing, err := r.store.RunByKey(r.ctx, request.RequestKey)
if err != nil {
r.mu.Unlock()
return nil, err
}
if existing == 0 {
if err := r.checkWorkspaceFree(workspace); err != nil {
r.mu.Unlock()
return nil, err
}
if existing == 0 {
// The workspace is the only scope a run reserves: refuse a second
// run only when an active run is already changing this exact
// resolved directory. One active run per workspace keeps this scan
// finding at most one match, so the message is deterministic.
for id, entry := range r.active {
if entry.workspace == workspace {
return nil, fmt.Errorf("run %d is already active in workspace %s; wait for it to finish or start this run in a different workspace (for example another checkout of the repository)", id, workspace)
}
}
}
r.mu.Unlock()
var runTitle summarise.Title
if existing == 0 {
// An explicit operator title skips the model call; otherwise the
// configured generator names the run, falling back to the prompt's
// first line when it is unavailable or slow.
if explicit, ok := summarise.ExplicitTitle(request.Title); ok {
runTitle = explicit
} else if r.title != nil {
prompt, _ := request.Input["prompt"].(string)
runTitle = r.title(r.ctx, prompt)
}
}
id, created, err := r.store.CreateRun(r.ctx, d, request.RequestKey, workspace, request.AllowTools, request.WritePaths, request.Input, retryOf...)
r.mu.Lock()
defer r.mu.Unlock()
// Another run may have reserved this workspace while the title was
// generated, so re-check before reserving it; a race on the request key is
// handled by the store's idempotent insert.
if existing == 0 {
if err := r.checkWorkspaceFree(workspace); err != nil {
return nil, err
}
}
id, created, err := r.store.CreateRunTitled(r.ctx, d, request.RequestKey, workspace, request.AllowTools, request.WritePaths, request.Input, runTitle, retryOf...)
if err != nil {
return nil, err
}
@@ -215,6 +247,19 @@ func (r *runner) start(request protocol.Message, retryOf ...int64) (*protocol.Ru
return detail, err
}
// checkWorkspaceFree refuses a run when an active run is already changing the
// same resolved workspace. The workspace is the only scope a run reserves, and
// one active run per workspace keeps this scan finding at most one match, so the
// message is deterministic. Callers must hold r.mu.
func (r *runner) checkWorkspaceFree(workspace string) error {
for id, entry := range r.active {
if entry.workspace == workspace {
return fmt.Errorf("run %d is already active in workspace %s; wait for it to finish or start this run in a different workspace (for example another checkout of the repository)", id, workspace)
}
}
return nil
}
func (r *runner) getRun(id int64) (*protocol.RunDetail, error) {
r.mu.Lock()
defer r.mu.Unlock()
@@ -279,6 +324,19 @@ func (r *runner) cancel(id int64) error {
return nil
}
// setTitle renames a run: it replaces the stored operator-facing title without
// touching the run's input, steps or execution, then returns the updated detail.
func (r *runner) setTitle(id int64, title string) (*protocol.RunDetail, error) {
explicit, ok := summarise.ExplicitTitle(title)
if !ok {
return nil, fmt.Errorf("a title with usable text is required")
}
if err := r.store.SetRunTitle(r.ctx, id, explicit); err != nil {
return nil, err
}
return r.getRun(id)
}
func (r *runner) run(ctx context.Context, id int64, d *protocol.PipelineDetail, workspace string, request protocol.Message) {
input := stage.Values(request.Input)
key := d.Graph.Stages[0].Key
+186
View File
@@ -11,8 +11,194 @@ import (
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/stage"
"git.a-wan.net/alan/pipelines/internal/summarise"
)
// TestRunTitleGeneratedPersistedAndFallback proves the insert path stores a
// generated title with its provenance in list and detail responses, and falls
// back to a derived bounded title when no generator is wired, without failing
// run creation.
func TestRunTitleGeneratedPersistedAndFallback(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
store, err := pipeline.Open(filepath.Join(t.TempDir(), "titles.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
if _, err := store.Submit(ctx, "titled", pipeline.Template([]string{"one"}, true)); err != nil {
t.Fatal(err)
}
items, _ := store.List(ctx)
r, err := newRunner(ctx, store)
if err != nil {
t.Fatal(err)
}
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
return stage.Result{Outcome: "success", Output: stage.Values{}}, nil
}
r.title = func(context.Context, string) summarise.Title {
return summarise.Title{Text: "Fix the flaky login test", Source: summarise.SourceModel, Model: "openai/gpt"}
}
run, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "model-title", Workspace: t.TempDir(), Input: map[string]any{"prompt": "prompt body"}})
if err != nil {
t.Fatal(err)
}
r.wg.Wait()
detail, err := store.GetRun(ctx, run.ID)
if err != nil {
t.Fatal(err)
}
if detail.Title != "Fix the flaky login test" || detail.TitleSource != summarise.SourceModel || detail.TitleModel != "openai/gpt" {
t.Fatalf("model title not stored with provenance: %+v", detail)
}
list, err := store.ListRuns(ctx)
if err != nil || len(list) != 1 || list[0].Title != "Fix the flaky login test" {
t.Fatalf("run list omitted the title: %v %v", list, err)
}
// No generator: the fallback is derived from the prompt's first line and run
// creation still succeeds.
r.title = nil
fallback, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "fallback-title", Workspace: t.TempDir(), Input: map[string]any{"prompt": "Fix the widget\nand more detail"}})
if err != nil {
t.Fatal(err)
}
r.wg.Wait()
detail, err = store.GetRun(ctx, fallback.ID)
if err != nil {
t.Fatal(err)
}
if detail.Title != "Fix the widget" || detail.TitleSource != summarise.SourceFallback || detail.TitleModel != "" {
t.Fatalf("fallback title = %+v, want the prompt's first line", detail)
}
}
// TestRunTitleGeneratorSkippedForReplayAndRejection proves the bounded
// generation runs only for a request that starts a run: an idempotent replay and
// a request refused for an already-reserved workspace must not pay for it.
func TestRunTitleGeneratorSkippedForReplayAndRejection(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
store, err := pipeline.Open(filepath.Join(t.TempDir(), "skip.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
if _, err := store.Submit(ctx, "skip", pipeline.Template([]string{"one"}, true)); err != nil {
t.Fatal(err)
}
items, _ := store.List(ctx)
r, err := newRunner(ctx, store)
if err != nil {
t.Fatal(err)
}
release := make(chan struct{})
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
<-release
return stage.Result{Outcome: "success", Output: stage.Values{}}, nil
}
calls := 0
r.title = func(context.Context, string) summarise.Title {
calls++
return summarise.Title{Text: "Generated", Source: summarise.SourceModel}
}
workspace := t.TempDir()
first, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "skip-key", Workspace: workspace, Input: map[string]any{"prompt": "first"}})
if err != nil {
t.Fatal(err)
}
if calls != 1 {
t.Fatalf("first start generated %d titles, want 1", calls)
}
// A replay while the first run is active reuses the stored title.
replay, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "skip-key", Workspace: workspace, Input: map[string]any{"prompt": "first"}})
if err != nil || replay.ID != first.ID {
t.Fatalf("replay = %+v, %v; want run %d", replay, err, first.ID)
}
// A new key in the already-reserved workspace is refused.
if _, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "skip-other", Workspace: workspace, Input: map[string]any{"prompt": "second"}}); err == nil {
t.Fatal("workspace conflict was not refused")
}
if calls != 1 {
t.Fatalf("replay/rejection generated %d titles, want 1", calls)
}
close(release)
r.wg.Wait()
}
func TestHandlerWiresRunTitleGenerator(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
store, err := pipeline.Open(filepath.Join(t.TempDir(), "wire.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
configured := HandlerWithOptions(ctx, Options{RunTitles: summarise.GenerateTitle}, store).(*runHandler)
bare := HandlerWithOptions(ctx, Options{}, store).(*runHandler)
defer func() { cancel(); WaitRuns(configured); WaitRuns(bare) }()
if configured.runner.title == nil {
t.Fatal("configured RunTitles was not applied to the runner")
}
if bare.runner.title != nil {
t.Fatal("unconfigured RunTitles must keep deterministic fallback title generation")
}
}
// TestRunnerExplicitTitleSkipsGeneratorAndRenames proves an explicit --title is
// stored verbatim and skips the model generator, and that setTitle later
// replaces it with an explicit provenance without touching the run's input.
func TestRunnerExplicitTitleSkipsGeneratorAndRenames(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
store, err := pipeline.Open(filepath.Join(t.TempDir(), "explicit.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
if _, err := store.Submit(ctx, "explicit", pipeline.Template([]string{"one"}, true)); err != nil {
t.Fatal(err)
}
items, _ := store.List(ctx)
r, err := newRunner(ctx, store)
if err != nil {
t.Fatal(err)
}
r.execute = func(context.Context, stage.Definition, stage.Values) (stage.Result, error) {
return stage.Result{Outcome: "success", Output: stage.Values{}}, nil
}
generated := 0
r.title = func(context.Context, string) summarise.Title {
generated++
return summarise.Title{Text: "Generated", Source: summarise.SourceModel}
}
run, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "explicit-key", Workspace: t.TempDir(), Title: "Operator chosen title", Input: map[string]any{"prompt": "prompt body"}})
if err != nil {
t.Fatal(err)
}
r.wg.Wait()
if generated != 0 {
t.Fatalf("explicit title still called the generator %d times", generated)
}
if run.Title != "Operator chosen title" || run.TitleSource != summarise.SourceExplicit || run.TitleModel != "" {
t.Fatalf("explicit title = %+v", run)
}
renamed, err := r.setTitle(run.ID, " Renamed after the fact ")
if err != nil {
t.Fatal(err)
}
if renamed.Title != "Renamed after the fact" || renamed.TitleSource != summarise.SourceExplicit || renamed.Input["prompt"] != "prompt body" {
t.Fatalf("rename = %+v", renamed)
}
if _, err := r.setTitle(run.ID, " "); err == nil {
t.Fatal("blank rename was accepted")
}
if _, err := r.setTitle(run.ID+999, "ghost"); err == nil {
t.Fatal("renaming an unknown run was accepted")
}
}
func TestRunnerRoutingPersistenceAndIdempotency(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
+12 -1
View File
@@ -15,6 +15,7 @@ import (
"git.a-wan.net/alan/pipelines/internal/buildinfo"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/summarise"
)
// Options configures the HTTP handler.
@@ -31,6 +32,10 @@ type Options struct {
// it from the first store; when neither is available chat_sessions returns an
// empty list rather than the endpoint's whole session list.
OwnedSessions func(context.Context) ([]pipeline.SessionOwner, error)
// RunTitles generates the operator-facing title stored with each new run.
// When nil, run creation stores the prompt-derived fallback without a model
// call, which keeps tests and unconfigured servers deterministic.
RunTitles func(context.Context, string) summarise.Title
}
// Handler serves the persistent connection with default options. Canceling ctx
@@ -53,6 +58,9 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
if runErr == nil && options.ArtifactsRoot != "" {
runs.artifactsRoot = options.ArtifactsRoot
}
if runErr == nil && options.RunTitles != nil {
runs.title = options.RunTitles
}
if runErr == nil {
runs.schedulerWG.Add(1)
go runs.scheduleLoop()
@@ -192,7 +200,7 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
} else {
response.Pipelines = items
}
} else if runs != nil && (request.Type == protocol.RunPipeline || request.Type == protocol.RetryRun || request.Type == protocol.ListRuns || request.Type == protocol.GetRun || request.Type == protocol.CancelRun || request.Type == protocol.GetRunEvidence) {
} else if runs != nil && (request.Type == protocol.RunPipeline || request.Type == protocol.RetryRun || request.Type == protocol.ListRuns || request.Type == protocol.GetRun || request.Type == protocol.CancelRun || request.Type == protocol.SetRunTitle || request.Type == protocol.GetRunEvidence) {
var err error
switch request.Type {
case protocol.GetRunEvidence:
@@ -204,6 +212,9 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
case protocol.RetryRun:
response.Type = protocol.RunStarted
response.RunDetail, err = runs.retry(request)
case protocol.SetRunTitle:
response.Type = protocol.Run
response.RunDetail, err = runs.setTitle(request.RunID, request.Title)
case protocol.ListRuns:
response.Type = protocol.Runs
response.Runs, err = runs.listRuns()
+116 -22
View File
@@ -9,10 +9,14 @@
// the NoSummary sentinel instead of failing, stalling or delaying a stage or a
// run. It never runs tools, never writes files and never propagates an error.
//
// Obvious future call sites (not wired here): protocol.OutputPreview in
// internal/protocol/result.go and the notification summary in
// internal/server/notify.go. Changing a user-visible title/verdict later also
// requires updating the relevant internal/author/skill/references/*.md.
// Title/GenerateTitle build on Summarise to give every run a bounded, never
// empty operator-facing title: the model line when it succeeds and the prompt's
// first line otherwise. The server wires GenerateTitle at run creation; the
// store persists it with its source/model provenance. Obvious future call sites
// (not wired here): protocol.OutputPreview in internal/protocol/result.go and the
// notification summary in internal/server/notify.go. Changing a user-visible
// title/verdict later also requires updating the relevant
// internal/author/skill/references/*.md.
package summarise
import (
@@ -42,6 +46,31 @@ const InputCap = 8 << 10
// are trimmed, never streamed onward.
const OutputCap = 200
// TitleCap bounds an operator-facing run title (in runes) whether it came from
// the model or the fallback, so a stored title stays a short single line.
const TitleCap = 80
// FallbackTitle is the last-resort title used when a prompt has no usable line,
// so a stored title is never empty.
const FallbackTitle = "Untitled run"
// Title provenance recorded beside a stored run title so a diagnosis can tell a
// model title from the derived fallback.
const (
SourceModel = "model"
SourceFallback = "fallback"
SourceExplicit = "explicit"
)
// Title is a bounded, single-line operator-facing run title plus how it was
// produced: SourceModel with the generating Model, SourceFallback, or
// SourceExplicit when the operator typed it (--title or a rename).
type Title struct {
Text string
Source string
Model string
}
// DefaultTimeout is the hard per-call budget: one generation must finish within
// a few seconds even when the caller passes a longer or absent deadline.
const DefaultTimeout = 5 * time.Second
@@ -59,6 +88,10 @@ const quoteCutset = "\"'`\u2018\u2019\u201c\u201d"
// punctCutset is the set of trailing punctuation stripped from a summary line.
const punctCutset = ".!?,;:\u2026\u3002\uff01\uff1f\uff0c\uff1b\uff1a"
// markCutset is the set of leading markdown markers (headings, quotes, list and
// emphasis tokens) stripped from a cleaned line so a title carries no markdown.
const markCutset = "#>*_`-+"
// Selection is the resolved harness configuration the summariser generates with.
// Harness, Executable and Model come from the configured default harness profile
// (its interactive Model, never its PipelineModel); none of them are constants.
@@ -105,20 +138,28 @@ type Summariser struct {
// records at most one bounded OperationEvidence when a call fails and Operations
// is configured. It never propagates an error and never runs tools.
func (s Summariser) Summarise(ctx context.Context, source string) string {
line, _ := s.summarise(ctx, source)
return line
}
// summarise is the single implementation behind Summarise and Title. It returns
// the bounded line (or NoSummary) and the resolved selection, so a caller can
// record which model produced it without resolving the harness twice.
func (s Summariser) summarise(ctx context.Context, source string) (string, Selection) {
selection, err := s.resolve()
if err != nil {
s.log(Selection{}, true, err)
return NoSummary
return NoSummary, Selection{}
}
if strings.TrimSpace(source) == "" {
s.log(selection, false, nil)
return NoSummary
return NoSummary, selection
}
generator, err := s.generator(selection)
if err != nil {
s.record(selection, err)
s.log(selection, true, err)
return NoSummary
return NoSummary, selection
}
timeout := s.Timeout
if timeout <= 0 {
@@ -141,17 +182,62 @@ func (s Summariser) Summarise(ctx context.Context, source string) string {
if err != nil {
s.record(selection, err)
s.log(selection, true, err)
return NoSummary
return NoSummary, selection
}
line := oneLine(text)
if line == "" {
err = fmt.Errorf("model returned no usable one-line summary")
s.record(selection, err)
s.log(selection, true, err)
return NoSummary
return NoSummary, selection
}
s.log(selection, false, nil)
return line
return line, selection
}
// Title returns a bounded, single-line operator-facing title for a run created
// from prompt. It asks the shared summariser for one bounded line and, when that
// fails or times out, falls back to the prompt's first usable line. It never
// returns an empty title, never propagates an error and never exceeds the
// summariser's hard timeout, so run creation always starts with a usable title.
func (s Summariser) Title(ctx context.Context, prompt string) Title {
line, selection := s.summarise(ctx, prompt)
if line != NoSummary {
if text := capRunes(line, TitleCap); text != "" {
return Title{Text: text, Source: SourceModel, Model: selection.Model}
}
}
return Title{Text: DeriveTitle(prompt), Source: SourceFallback}
}
// GenerateTitle is the package entry point that derives a run title from the
// configured default harness, so a caller need not construct a Summariser. It is
// bounded and never blocks past the summariser's hard timeout.
func GenerateTitle(ctx context.Context, prompt string) Title { return Summariser{}.Title(ctx, prompt) }
// DeriveTitle is the bounded fallback title for a run prompt: its first usable
// line, cleaned the same way a model reply is and capped to TitleCap runes. It is
// never empty.
func DeriveTitle(prompt string) string {
for _, raw := range strings.Split(prompt, "\n") {
if line := cleanLine(raw); line != "" {
return capRunes(line, TitleCap)
}
}
return FallbackTitle
}
// ExplicitTitle normalises an operator-supplied run title (--title or a rename)
// to a bounded single line and reports whether it carried usable text. Unlike a
// derived title it preserves the operator's wording and punctuation, only
// collapsing whitespace and controlling characters; it is never generated or
// model-cleaned.
func ExplicitTitle(raw string) (Title, bool) {
text := capRunes(protocol.NormalizeText(raw), TitleCap)
if text == "" {
return Title{}, false
}
return Title{Text: text, Source: SourceExplicit}, true
}
func (s Summariser) resolve() (Selection, error) {
@@ -208,23 +294,31 @@ func truncateSource(source string) string {
// punctuation, and caps the result to OutputCap runes.
func oneLine(text string) string {
for _, raw := range strings.Split(text, "\n") {
line := strings.TrimSpace(raw)
if line == "" || isFenceLine(line) {
continue
if line := cleanLine(raw); line != "" {
return capRunes(line, OutputCap)
}
line = strings.TrimSpace(protocol.NormalizeText(line))
line = strings.Trim(line, quoteCutset)
line = strings.TrimSpace(line)
line = strings.TrimRight(line, punctCutset)
line = strings.TrimSpace(line)
if line == "" {
continue
}
return capRunes(line, OutputCap)
}
return ""
}
// cleanLine reduces one raw line to plain single-line text, or "" when it carries
// no usable content (empty, whitespace or a bare code fence). It collapses
// whitespace/control characters, strips surrounding quotes, leading markdown
// markers and trailing punctuation. It is the shared primitive behind a summary
// line and a derived run title.
func cleanLine(raw string) string {
line := strings.TrimSpace(raw)
if line == "" || isFenceLine(line) {
return ""
}
line = strings.TrimSpace(protocol.NormalizeText(line))
line = strings.TrimSpace(strings.TrimLeft(line, markCutset))
line = strings.Trim(line, quoteCutset)
line = strings.TrimSpace(line)
line = strings.TrimRight(line, punctCutset)
return strings.TrimSpace(line)
}
// isFenceLine reports whether a trimmed line is a markdown code-fence delimiter
// (an optional language token after a run of backticks/tildes, but no content).
// "```Summary```" is not a fence line: it carries content and is cleaned instead.
+81
View File
@@ -267,6 +267,87 @@ func TestSummariseDiagnostics(t *testing.T) {
}
}
func TestTitleUsesModelLineAndRecordsSource(t *testing.T) {
s := Summariser{
Selection: selection(),
Generator: generatorFunc(func(context.Context, stage.ModelConfig, string) (string, error) {
return `## "Fix the flaky login test."`, nil
}),
}
got := s.Title(context.Background(), "prompt text")
if got.Text != "Fix the flaky login test" {
t.Fatalf("got %q, want the cleaned model title", got.Text)
}
if got.Source != SourceModel || got.Model != "p/open" {
t.Fatalf("title provenance = %+v, want model p/open", got)
}
}
func TestTitleFallsBackToBoundedPromptLine(t *testing.T) {
long := strings.Repeat("é", TitleCap*2)
s := Summariser{
Selection: selection(),
Generator: generatorFunc(func(context.Context, stage.ModelConfig, string) (string, error) {
return "", errors.New("transport down")
}),
}
for _, prompt := range []string{"First line\nthen ignored", " \n Fix the widget \nmore", "- bullet task here\nrest", long} {
got := s.Title(context.Background(), prompt)
if got.Text == "" || got.Text == NoSummary || got.Text == FallbackTitle {
t.Fatalf("prompt %q produced unusable title %+v", prompt, got)
}
if got.Source != SourceFallback || got.Model != "" {
t.Fatalf("prompt %q provenance = %+v, want fallback with no model", prompt, got)
}
if len([]rune(got.Text)) > TitleCap {
t.Fatalf("prompt %q title has %d runes, want <= %d", prompt, len([]rune(got.Text)), TitleCap)
}
if strings.ContainsAny(got.Text, "\r\n") {
t.Fatalf("prompt %q title %q is not a single line", prompt, got.Text)
}
}
if got := s.Title(context.Background(), "First line\nthen ignored"); got.Text != "First line" {
t.Fatalf("fallback title = %q, want the prompt's first line", got.Text)
}
if got := s.Title(context.Background(), "- bullet task here\nrest"); got.Text != "bullet task here" {
t.Fatalf("fallback stripped markdown = %q, want the leading marker removed", got.Text)
}
}
func TestTitleNeverEmptyForBlankPrompt(t *testing.T) {
s := Summariser{
Selection: selection(),
Generator: generatorFunc(func(context.Context, stage.ModelConfig, string) (string, error) {
return "", nil
}),
}
got := s.Title(context.Background(), " \n\t")
if got.Text != FallbackTitle || got.Source != SourceFallback {
t.Fatalf("blank prompt title = %+v, want %q fallback", got, FallbackTitle)
}
}
func TestExplicitTitleNormalisesAndBounds(t *testing.T) {
got, ok := ExplicitTitle(" Fix the\nflaky login test ")
if !ok || got.Text != "Fix the flaky login test" || got.Source != SourceExplicit || got.Model != "" {
t.Fatalf("explicit title = %+v, %t; want collapsed explicit text", got, ok)
}
// The operator's punctuation and wording are preserved, unlike a cleaned
// model line.
if got, ok := ExplicitTitle("Ship v2.0!"); !ok || got.Text != "Ship v2.0!" {
t.Fatalf("explicit title altered the operator's text: %+v %t", got, ok)
}
long := strings.Repeat("é", TitleCap*2)
if got, ok := ExplicitTitle(long); !ok || len([]rune(got.Text)) != TitleCap {
t.Fatalf("explicit title not capped to %d runes: %d", TitleCap, len([]rune(got.Text)))
}
for _, blank := range []string{"", " ", "\n\t", "\x00\x01"} {
if got, ok := ExplicitTitle(blank); ok {
t.Fatalf("blank explicit title %q accepted as %+v", blank, got)
}
}
}
func TestSummariseSuccessLogsAndRecordsNoFailure(t *testing.T) {
var logs []string
operations := &stage.OperationLog{}
+3 -3
View File
@@ -10,9 +10,9 @@ import (
)
type RunOptions struct {
Pipeline, Prompt, Workspace string
Yes, AllowTools bool
WritePaths []string
Pipeline, Prompt, Title, Workspace string
Yes, AllowTools bool
WritePaths []string
}
func NewRun(ctx context.Context, url string, options RunOptions) Model {
+2 -2
View File
@@ -98,7 +98,7 @@ func (m Model) homeLines() []string {
limit = 2
}
for _, r := range m.runs[:min(limit, len(m.runs))] {
runs = append(runs, bar(fmt.Sprintf("#%d %s", r.ID, r.Name), m.progress(r.Status), max(1, panelWidth-4), false))
runs = append(runs, bar(fmt.Sprintf("#%d %s", r.ID, r.Label()), m.progress(r.Status), max(1, panelWidth-4), false))
runs = append(runs, previewRows(resultLabel(r), max(1, panelWidth-4), 3)...)
runs = append(runs, lipgloss.NewStyle().Foreground(themeMuted).Render(fmt.Sprintf("v%d · %d completed visits · %s", r.Version, r.CompletedSteps, runDuration(r.CreatedAt, r.FinishedAt))))
if r.CurrentStage != "" && (r.Status == "running" || r.Status == "queued") {
@@ -123,7 +123,7 @@ func (m Model) homeLines() []string {
lines = append(lines,
lipgloss.NewStyle().Bold(true).Render(p.Name), lipgloss.NewStyle().Foreground(themeMuted).Render(fmt.Sprintf("v%d · %d stages", p.Version, p.Stages)))
if r := m.latestPipelineRun(p.ID, 0); r != nil {
lines = append(lines, fmt.Sprintf("Last run #%d · v%d · %s", r.ID, r.Version, m.progress(r.Status)))
lines = append(lines, fmt.Sprintf("Last run #%d · %s · v%d · %s", r.ID, r.Label(), r.Version, m.progress(r.Status)))
lines = append(lines, previewRows(resultLabel(*r), max(1, pipelineWidth-4), 2)...)
} else {
lines = append(lines, "Not run yet · no recorded results")
+1 -1
View File
@@ -48,7 +48,7 @@ func (m Model) pipelineResultLines() []string {
if r == nil {
return []string{"Results · This version has not run yet. No accomplishments recorded."}
}
lines := []string{fmt.Sprintf("Results · run #%d · %s · %d completed visits · %s", r.ID, ansi.Strip(m.progress(r.Status)), r.CompletedSteps, runDuration(r.CreatedAt, r.FinishedAt)), resultLabel(*r)}
lines := []string{fmt.Sprintf("Results · run #%d · %s · %s · %d completed visits · %s", r.ID, r.Label(), ansi.Strip(m.progress(r.Status)), r.CompletedSteps, runDuration(r.CreatedAt, r.FinishedAt)), resultLabel(*r)}
if r.Status != "succeeded" && r.ResultPreview != "" && r.Diagnostic != "" {
lines = append(lines, "Partial output: "+ansi.Strip(r.ResultPreview))
}
+3 -3
View File
@@ -126,7 +126,7 @@ func (m Model) visibleRuns() []protocol.RunSummary {
}
labels := make([]string, len(runs))
for i, run := range runs {
labels[i] = fmt.Sprintf("#%d %s v%d %s %s %s %s", run.ID, run.Name, run.Version, run.Status, run.Outcome, run.CurrentStage, run.Diagnostic)
labels[i] = fmt.Sprintf("#%d %s v%d %s %s %s %s", run.ID, run.Label(), run.Version, run.Status, run.Outcome, run.CurrentStage, run.Diagnostic)
}
matched := make([]protocol.RunSummary, 0)
for _, match := range fuzzy.Find(query, labels) {
@@ -589,7 +589,7 @@ func (m Model) runsDetailsView(height int) string {
start, end := selectionWindow(len(runs), m.runIndex, max(1, (height-4-len(rows))/2))
for i := start; i < end; i++ {
r := runs[i]
text := fmt.Sprintf("#%d %s · v%d · %s · %s", r.ID, r.Name, r.Version, m.progress(r.Status), dateLabel(r.FinishedAt))
text := fmt.Sprintf("#%d %s · v%d · %s · %s", r.ID, r.Label(), r.Version, m.progress(r.Status), dateLabel(r.FinishedAt))
rows = append(rows, menuOption(text, i == m.runIndex))
rows = append(rows, " "+ansi.Truncate(resultLabel(r), max(1, m.width-6), "…"))
}
@@ -612,7 +612,7 @@ func (m Model) runsDetailsView(height int) string {
return panel(m.width, height, []string{bar(title, fmt.Sprint(len(runs)), max(1, m.width-4), true)}, rows, footer, true)
}
r := m.runDetail
header := bar(fmt.Sprintf("#%d %s · v%d", r.ID, r.Name, r.Version), m.progress(r.Status)+" · "+runDuration(r.CreatedAt, r.FinishedAt), m.width, false)
header := bar(fmt.Sprintf("#%d %s · v%d", r.ID, r.Label(), r.Version), m.progress(r.Status)+" · "+runDuration(r.CreatedAt, r.FinishedAt), m.width, false)
if height <= 6 {
return fitBlock([]string{header, runHighlight(m.runSelectionLabel(), m.width), m.runActions() + " · Tab pane · Esc back"}, m.width, height)
}
+1 -1
View File
@@ -147,7 +147,7 @@ func (m *Model) startSelectedRun() tea.Cmd {
m.runCollapsed = nil
m.runDetail = nil
m.runTop = 0
request := protocol.Message{Type: protocol.RunPipeline, PipelineID: m.detail.ID, Version: m.detail.Version, RequestKey: fmt.Sprintf("tui-%d", time.Now().UnixNano()), Input: initialRunInput(m.runTask), AllowTools: m.runHasMutationTools(), WritePaths: m.runPaths, Workspace: m.runWorkspace}
request := protocol.Message{Type: protocol.RunPipeline, PipelineID: m.detail.ID, Version: m.detail.Version, RequestKey: fmt.Sprintf("tui-%d", time.Now().UnixNano()), Title: m.cliOptions.Title, Input: initialRunInput(m.runTask), AllowTools: m.runHasMutationTools(), WritePaths: m.runPaths, Workspace: m.runWorkspace}
updated, cmd := m.runRequest(request)
*m = updated.(Model)
return cmd
+18
View File
@@ -43,6 +43,24 @@ func TestRunFormAndInspection(t *testing.T) {
}
}
func TestRunTitleIsShownToOperator(t *testing.T) {
titled := protocol.RunSummary{ID: 1, Name: "bug fix", Version: 2, Title: "Fix the flaky login test", Status: "succeeded"}
if got := titled.Label(); got != "Fix the flaky login test" {
t.Fatalf("Label = %q, want the generated title", got)
}
if got := (protocol.RunSummary{Name: "bug fix"}).Label(); got != "bug fix" {
t.Fatalf("Label without a title = %q, want the pipeline name fallback", got)
}
m := New(context.Background(), "")
m.width = 100
m.height = 28
m.runOpen = true
m.runDetail = &protocol.RunDetail{RunSummary: titled}
if view := ansi.Strip(m.runsDetailsView(24)); !strings.Contains(view, "Fix the flaky login test") {
t.Fatalf("run detail header omitted the title:\n%s", view)
}
}
func runInspectionModel() Model {
m := New(context.Background(), "")
m.width = 100