Merge lane-go: keep the lane's reviewed work
Combines lane-go's typed omni search, launch-time model overrides, TUI artifacts view, run-state websocket push, and the "reflect the change where it belongs" committer rule with the Templates gallery, chat/omni-search and other reviewed work already on main. The 12 files both sides touched keep both intents composed rather than dropping either side: the omni search keeps the typed search contract (CLI `pipelines search` and the TUI popup with type badges and marked snippets) alongside the run-list quick filter; cli.md and ui-ux-review.md document both the search rework and the artifacts view; orchestration.md, harnesses.md and operations.md keep the model-override and reflect-where-it-belongs guidance; and the protocol/CLI/server/TUI code keeps the new search, run-event, override and artifacts paths alongside the existing run, title and help surfaces. Checks before committing: - go build ./... -- pass - go vet ./... -- pass - go test -count=1 ./internal/... ./cmd/... -- all pass Doc check before committing: nothing needs updating. The lane's own docs and references (docs/cli.md, docs/ui-ux-review.md, and the orchestration, harnesses and operations skill references) already carry the new features. format.md and the packaged templates already describe the line-oriented result format and the human decision stage from the earlier lane-go merge. The root README already describes the omni search popup. Model overrides, the search command, run events and the artifacts view are runtime/CLI/TUI surfaces that cli.md documents; no template, README or topic-doc edit is required by this merge.
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/stage"
|
||||
)
|
||||
|
||||
// ModelOverride is a provider/model pair chosen for a run.
|
||||
type ModelOverride struct {
|
||||
Provider string
|
||||
Model string
|
||||
}
|
||||
|
||||
// String renders the pair as provider/model.
|
||||
func (m ModelOverride) String() string { return m.Provider + "/" + m.Model }
|
||||
|
||||
// ParseModelOverride parses a provider/model value and validates both sides with
|
||||
// stage.ValidModel. It is the one parser shared by launch-time resolution and the
|
||||
// CLI's pre-flight form check, so the two cannot drift.
|
||||
func ParseModelOverride(value string) (ModelOverride, error) {
|
||||
provider, model, ok := strings.Cut(strings.TrimSpace(value), "/")
|
||||
provider, model = strings.TrimSpace(provider), strings.TrimSpace(model)
|
||||
if !ok || !stage.ValidModel(provider, model) {
|
||||
return ModelOverride{}, fmt.Errorf("model override %q must be provider/model", value)
|
||||
}
|
||||
return ModelOverride{Provider: provider, Model: model}, nil
|
||||
}
|
||||
|
||||
// EffectiveModels resolves the provider/model each model stage will run with for
|
||||
// one run. Precedence: a per-stage override beats the whole-run override, which
|
||||
// beats the model pinned in the pipeline version. It is pure and validates the
|
||||
// overrides against the pinned graph: an unknown stage key fails naming the valid
|
||||
// keys, a value that is not a provider/model fails, and a per-stage key for a
|
||||
// non-model stage fails because that stage could never honour it. A whole-run
|
||||
// override on a graph with no model stage is accepted and inert. The helper is
|
||||
// shared by the server (authoritative, against the pinned version) and the CLI
|
||||
// (pre-flight form check), so the precedence rule lives once.
|
||||
func EffectiveModels(graph protocol.Graph, model string, stageModels map[string]string) (map[string]ModelOverride, error) {
|
||||
keys := make([]string, 0, len(graph.Stages))
|
||||
byKey := make(map[string]protocol.Stage, len(graph.Stages))
|
||||
for _, s := range graph.Stages {
|
||||
keys = append(keys, s.Key)
|
||||
byKey[s.Key] = s
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
var whole *ModelOverride
|
||||
if strings.TrimSpace(model) != "" {
|
||||
parsed, err := ParseModelOverride(model)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
whole = &parsed
|
||||
}
|
||||
|
||||
perStage := make(map[string]ModelOverride, len(stageModels))
|
||||
overrideKeys := make([]string, 0, len(stageModels))
|
||||
for key := range stageModels {
|
||||
overrideKeys = append(overrideKeys, key)
|
||||
}
|
||||
sort.Strings(overrideKeys)
|
||||
for _, key := range overrideKeys {
|
||||
declared, exists := byKey[key]
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("unknown stage key %q; valid keys: %s", key, strings.Join(keys, ", "))
|
||||
}
|
||||
if stage.EffectiveType(declared.Type) != stage.ModelType {
|
||||
return nil, fmt.Errorf("stage %q cannot honour a model override; it is not a model stage", key)
|
||||
}
|
||||
parsed, err := ParseModelOverride(stageModels[key])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("stage %q: %w", key, err)
|
||||
}
|
||||
perStage[key] = parsed
|
||||
}
|
||||
|
||||
effective := make(map[string]ModelOverride, len(graph.Stages))
|
||||
for _, s := range graph.Stages {
|
||||
if stage.EffectiveType(s.Type) != stage.ModelType {
|
||||
continue
|
||||
}
|
||||
if override, ok := perStage[s.Key]; ok {
|
||||
effective[s.Key] = override
|
||||
continue
|
||||
}
|
||||
if whole != nil {
|
||||
effective[s.Key] = *whole
|
||||
continue
|
||||
}
|
||||
effective[s.Key] = ModelOverride{Provider: s.Configs["provider"], Model: s.Configs["model"]}
|
||||
}
|
||||
return effective, nil
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
func overrideGraph() protocol.Graph {
|
||||
return protocol.Graph{Stages: []protocol.Stage{
|
||||
{Key: "plan", Type: "model", Configs: map[string]string{"provider": "pinned", "model": "pinned-model"}},
|
||||
{Key: "note", Type: "ntfy", Configs: map[string]string{}},
|
||||
{Key: "review", Type: "model", Configs: map[string]string{"provider": "pinned2", "model": "pinned2-model"}},
|
||||
}}
|
||||
}
|
||||
|
||||
// TestEffectiveModelsPrecedence proves the one precedence rule: a per-stage
|
||||
// override beats the whole-run override, which beats the model pinned in the
|
||||
// pipeline version, and with no override the pinned pair is returned unchanged.
|
||||
func TestEffectiveModelsPrecedence(t *testing.T) {
|
||||
graph := overrideGraph()
|
||||
cases := []struct {
|
||||
name string
|
||||
model string
|
||||
stageModels map[string]string
|
||||
want map[string]ModelOverride
|
||||
}{
|
||||
{
|
||||
name: "no override keeps the pinned pair",
|
||||
want: map[string]ModelOverride{
|
||||
"plan": {Provider: "pinned", Model: "pinned-model"},
|
||||
"review": {Provider: "pinned2", Model: "pinned2-model"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "whole-run beats the pinned pair",
|
||||
model: "acme/whole",
|
||||
want: map[string]ModelOverride{
|
||||
"plan": {Provider: "acme", Model: "whole"},
|
||||
"review": {Provider: "acme", Model: "whole"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "per-stage beats whole-run and leaves the other pinned",
|
||||
model: "acme/whole",
|
||||
stageModels: map[string]string{"plan": "acme/stage"},
|
||||
want: map[string]ModelOverride{
|
||||
"plan": {Provider: "acme", Model: "stage"},
|
||||
"review": {Provider: "acme", Model: "whole"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "per-stage alone leaves the other pinned",
|
||||
stageModels: map[string]string{"plan": "acme/stage"},
|
||||
want: map[string]ModelOverride{
|
||||
"plan": {Provider: "acme", Model: "stage"},
|
||||
"review": {Provider: "pinned2", Model: "pinned2-model"},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got, err := EffectiveModels(graph, tc.model, tc.stageModels)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != len(tc.want) {
|
||||
t.Fatalf("effective = %+v, want %+v", got, tc.want)
|
||||
}
|
||||
for key, want := range tc.want {
|
||||
if got[key] != want {
|
||||
t.Fatalf("stage %s = %+v, want %+v", key, got[key], want)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestEffectiveModelsRejectsBadInput proves an unknown stage key fails naming the
|
||||
// valid keys, a malformed provider/model fails, and a per-stage key for a
|
||||
// non-model stage is refused as one that can never honour it.
|
||||
func TestEffectiveModelsRejectsBadInput(t *testing.T) {
|
||||
graph := overrideGraph()
|
||||
if _, err := EffectiveModels(graph, "", map[string]string{"nope": "acme/x"}); err == nil || !strings.Contains(err.Error(), "unknown stage key") || !strings.Contains(err.Error(), "note, plan, review") {
|
||||
t.Fatalf("unknown key error = %v, want the valid keys named", err)
|
||||
}
|
||||
for _, value := range []string{"not-a-pair", "/model", "provider/", "bad provider/model", "provider/bad model"} {
|
||||
if _, err := EffectiveModels(graph, value, nil); err == nil {
|
||||
t.Fatalf("whole-run value %q was accepted", value)
|
||||
}
|
||||
if _, err := EffectiveModels(graph, "", map[string]string{"plan": value}); err == nil {
|
||||
t.Fatalf("per-stage value %q was accepted", value)
|
||||
}
|
||||
}
|
||||
if _, err := EffectiveModels(graph, "", map[string]string{"note": "acme/x"}); err == nil || !strings.Contains(err.Error(), "cannot honour") {
|
||||
t.Fatalf("non-model stage override error = %v, want a cannot-honour refusal", err)
|
||||
}
|
||||
// A whole-run override on a graph with no model stage is accepted and inert.
|
||||
inert := protocol.Graph{Stages: []protocol.Stage{{Key: "note", Type: "ntfy", Configs: map[string]string{}}}}
|
||||
got, err := EffectiveModels(inert, "acme/whole", nil)
|
||||
if err != nil || len(got) != 0 {
|
||||
t.Fatalf("whole-run override on a no-model graph = %+v, %v; want inert", got, err)
|
||||
}
|
||||
}
|
||||
+64
-17
@@ -18,6 +18,7 @@ func (s *Store) initRuns() error {
|
||||
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,
|
||||
@@ -26,6 +27,7 @@ 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);
|
||||
@@ -53,7 +55,28 @@ run_id INTEGER PRIMARY KEY REFERENCES pipeline_runs(id), original_run_id INTEGER
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.migrateRunTitles()
|
||||
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
|
||||
@@ -98,17 +121,28 @@ func (s *Store) addColumnIfMissing(table, column, definition string) error {
|
||||
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, summarise.Title{}, retryOf...)
|
||||
return s.CreateRunTitled(ctx, d, key, workspace, tools, paths, input, RunOverrides{}, 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) {
|
||||
// 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]
|
||||
@@ -125,19 +159,27 @@ func (s *Store) CreateRunTitled(ctx context.Context, d *protocol.PipelineDetail,
|
||||
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 string
|
||||
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 FROM pipeline_runs WHERE request_key=?", key).Scan(&id, &pid, &version, &oldInput, &oldWorkspace, &oldPaths, &oldTools)
|
||||
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 {
|
||||
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
|
||||
@@ -150,7 +192,7 @@ func (s *Store) CreateRunTitled(ctx context.Context, d *protocol.PipelineDetail,
|
||||
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) VALUES (?,?,?,?,?,?,?,?,?,?)", d.ID, d.Version, key, workspace, tools, string(writes), string(encoded), title.Text, title.Source, title.Model)
|
||||
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
|
||||
}
|
||||
@@ -210,14 +252,19 @@ func (s *Store) SetRunTitle(ctx context.Context, id int64, title summarise.Title
|
||||
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,
|
||||
(SELECT COUNT(*) FROM run_steps WHERE run_id=r.id AND status='completed'),r.workspace,r.title
|
||||
(SELECT COUNT(*) FROM run_steps WHERE run_id=r.id AND status='completed'),r.workspace,r.title,r.model,r.stage_models
|
||||
` + runFrom + ` 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, &r.Workspace, &r.Title); err != nil {
|
||||
var output, stageModels 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, &r.Workspace, &r.Title, &r.Model, &stageModels); err != nil {
|
||||
return err
|
||||
}
|
||||
if stageModels != "" && stageModels != "{}" {
|
||||
if err := json.Unmarshal([]byte(stageModels), &r.StageModels); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
var values map[string]any
|
||||
if err := decodeValues(output, &values); err != nil {
|
||||
return err
|
||||
@@ -291,7 +338,7 @@ func (s *Store) GetRun(ctx context.Context, id int64) (*protocol.RunDetail, erro
|
||||
if err := json.Unmarshal([]byte(paths), &r.WritePaths); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := tx.QueryContext(ctx, `SELECT sequence,stage_key,status,outcome,target,input_json,output_json,diagnostic,session_id,agent_active,transcript,
|
||||
rows, err := tx.QueryContext(ctx, `SELECT sequence,stage_key,status,outcome,target,input_json,output_json,diagnostic,session_id,agent_active,transcript,provider,model,
|
||||
COALESCE((SELECT receipts_json FROM step_notifications n WHERE n.run_id=run_steps.run_id AND n.sequence=run_steps.sequence),'[]'),
|
||||
COALESCE((SELECT changes_json FROM step_file_changes f WHERE f.run_id=run_steps.run_id AND f.sequence=run_steps.sequence),'null'),
|
||||
COALESCE((SELECT artifacts_json FROM step_artifacts a WHERE a.run_id=run_steps.run_id AND a.sequence=run_steps.sequence),'null'),
|
||||
@@ -305,7 +352,7 @@ strftime('%Y-%m-%dT%H:%M:%SZ',started_at),COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ'
|
||||
var step protocol.RunStep
|
||||
var notifications string
|
||||
var fileChanges, artifacts, storedVerdict string
|
||||
if err := rows.Scan(&step.Sequence, &step.Stage, &step.Status, &step.Outcome, &step.Target, &input, &output, &step.Diagnostic, &step.SessionID, &step.AgentActive, &step.Transcript, ¬ifications, &fileChanges, &artifacts, &storedVerdict, &step.StartedAt, &step.FinishedAt); err != nil {
|
||||
if err := rows.Scan(&step.Sequence, &step.Stage, &step.Status, &step.Outcome, &step.Target, &input, &output, &step.Diagnostic, &step.SessionID, &step.AgentActive, &step.Transcript, &step.Provider, &step.Model, ¬ifications, &fileChanges, &artifacts, &storedVerdict, &step.StartedAt, &step.FinishedAt); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
@@ -545,8 +592,8 @@ func (s *Store) FinishStep(ctx context.Context, id int64, step protocol.RunStep)
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if err := affected(tx.ExecContext(ctx, `UPDATE run_steps SET status=?,outcome=?,target=?,output_json=?,diagnostic=?,transcript=?,finished_at=CURRENT_TIMESTAMP
|
||||
WHERE run_id=? AND sequence=? AND status='running'`, step.Status, step.Outcome, step.Target, string(output), step.Diagnostic, step.Transcript, id, step.Sequence)); err != nil {
|
||||
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 {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/summarise"
|
||||
)
|
||||
|
||||
@@ -58,7 +59,7 @@ func TestRunTitleStorageMigrationAndFallback(t *testing.T) {
|
||||
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"},
|
||||
map[string]any{"prompt": "a prompt that must not become the title"}, RunOverrides{},
|
||||
summarise.Title{Text: "Fix the flaky login test", Source: summarise.SourceModel, Model: "openai/gpt"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -160,6 +161,116 @@ func TestSetRunTitleRenamesWithoutTouchingInput(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunModelOverrideRoundTripAndRequestKey proves the launch-time model
|
||||
// overrides round-trip through the run record and the per-step provider/model,
|
||||
// and that a request-key replay with different overrides is refused as a
|
||||
// different run.
|
||||
func TestRunModelOverrideRoundTripAndRequestKey(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store, err := Open(filepath.Join(t.TempDir(), "override.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
if _, err := store.Submit(ctx, "override", Template([]string{"one"}, true)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, _ := store.List(ctx)
|
||||
definition, _ := store.Get(ctx, items[0].ID, 1)
|
||||
overrides := RunOverrides{Model: "acme/whole", StageModels: map[string]string{"one": "acme/strong"}}
|
||||
id, created, err := store.CreateRunTitled(ctx, definition, "override-key", "workspace", false, nil,
|
||||
map[string]any{"prompt": "task"}, overrides, summarise.Title{})
|
||||
if err != nil || !created {
|
||||
t.Fatalf("create override run = %d %v %v", id, created, err)
|
||||
}
|
||||
if err := store.BeginStep(ctx, id, 1, "one", map[string]any{"prompt": "task"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.FinishStep(ctx, id, protocol.RunStep{Sequence: 1, Stage: "one", Status: "completed", Outcome: "success", Output: map[string]any{}, Provider: "acme", Model: "strong"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := store.GetRun(ctx, id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Model != "acme/whole" || got.StageModels["one"] != "acme/strong" {
|
||||
t.Fatalf("run record did not echo overrides: %+v", got)
|
||||
}
|
||||
if len(got.Steps) != 1 || got.Steps[0].Provider != "acme" || got.Steps[0].Model != "strong" {
|
||||
t.Fatalf("step did not record the effective model: %+v", got.Steps)
|
||||
}
|
||||
list, err := store.ListRuns(ctx)
|
||||
if err != nil || len(list) != 1 || list[0].Model != "acme/whole" || list[0].StageModels["one"] != "acme/strong" {
|
||||
t.Fatalf("run list omitted the overrides: %+v %v", list, err)
|
||||
}
|
||||
// An idempotent replay with the same overrides returns the same run.
|
||||
again, created, err := store.CreateRunTitled(ctx, definition, "override-key", "workspace", false, nil,
|
||||
map[string]any{"prompt": "task"}, overrides, summarise.Title{})
|
||||
if err != nil || created || again != id {
|
||||
t.Fatalf("same-override replay = %d %v %v, want the stored run", again, created, err)
|
||||
}
|
||||
// A different override under the same key is a different run.
|
||||
if _, _, err := store.CreateRunTitled(ctx, definition, "override-key", "workspace", false, nil,
|
||||
map[string]any{"prompt": "task"}, RunOverrides{StageModels: map[string]string{"one": "acme/other"}}, summarise.Title{}); err == nil || !strings.Contains(err.Error(), "request key already used") {
|
||||
t.Fatalf("differing-override replay was not refused: %v", err)
|
||||
}
|
||||
if _, _, err := store.CreateRunTitled(ctx, definition, "override-key", "workspace", false, nil,
|
||||
map[string]any{"prompt": "task"}, RunOverrides{}, summarise.Title{}); err == nil || !strings.Contains(err.Error(), "request key already used") {
|
||||
t.Fatalf("no-override replay was not refused: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunModelOverrideMigrationKeepsHistory proves a database created before the
|
||||
// override columns existed gains them idempotently on Open and still lists and
|
||||
// reads its historical rows with empty overrides.
|
||||
func TestRunModelOverrideMigrationKeepsHistory(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
path := filepath.Join(t.TempDir(), "override-migration.db")
|
||||
store, err := Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := store.Submit(ctx, "legacy", 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, "legacy-key", "workspace", false, nil, map[string]any{"prompt": "task"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Simulate a pre-override database by dropping the columns, then reopen.
|
||||
for _, column := range []string{"model", "stage_models"} {
|
||||
if _, err := store.db.ExecContext(ctx, "ALTER TABLE pipeline_runs DROP COLUMN "+column); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for _, column := range []string{"provider", "model"} {
|
||||
if _, err := store.db.ExecContext(ctx, "ALTER TABLE run_steps 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 || len(runs) != 1 {
|
||||
t.Fatalf("legacy run did not survive the override migration: %+v %v", runs, err)
|
||||
}
|
||||
got, err := store.GetRun(ctx, id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Model != "" || len(got.StageModels) != 0 {
|
||||
t.Fatalf("migrated row should keep empty overrides: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunRecoveryAndVersionPinning(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
path := filepath.Join(t.TempDir(), "audit.db")
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
@@ -484,6 +483,8 @@ ORDER BY s.run_id DESC,s.sequence DESC LIMIT ?`, protocol.SearchMaxScanSteps+1)
|
||||
continue
|
||||
}
|
||||
var values map[string]any
|
||||
// A malformed retained row cannot match anything, so it is skipped
|
||||
// rather than failing the whole query and hiding every other hit.
|
||||
if err := decodeValues(field.raw, &values); err != nil {
|
||||
continue
|
||||
}
|
||||
@@ -536,6 +537,8 @@ ORDER BY a.run_id DESC,a.sequence DESC LIMIT ?`, pattern, searchRowBudget+1)
|
||||
return nil, err
|
||||
}
|
||||
var evidence stageArtifactsEvidence
|
||||
// A malformed retained row cannot match anything, so it is skipped
|
||||
// rather than failing the whole query and hiding every other hit.
|
||||
if err := json.Unmarshal([]byte(artifacts), &evidence); err != nil {
|
||||
continue
|
||||
}
|
||||
@@ -592,7 +595,7 @@ func findMatch(value any, text string) (string, bool) {
|
||||
func searchSnippet(text, query string) string {
|
||||
normalized := protocol.NormalizeText(text)
|
||||
runes := []rune(normalized)
|
||||
index := matchRunes(runes, []rune(query))
|
||||
index := protocol.RuneIndexFold(runes, []rune(query))
|
||||
if index < 0 {
|
||||
return truncateRunes(normalized, 80)
|
||||
}
|
||||
@@ -615,26 +618,6 @@ func searchSnippet(text, query string) string {
|
||||
return snippet
|
||||
}
|
||||
|
||||
// matchRunes returns the rune index of query in text, case-insensitively.
|
||||
func matchRunes(text, query []rune) int {
|
||||
if len(query) == 0 || len(query) > len(text) {
|
||||
return -1
|
||||
}
|
||||
for i := 0; i+len(query) <= len(text); i++ {
|
||||
match := true
|
||||
for j := range query {
|
||||
if unicode.ToLower(text[i+j]) != unicode.ToLower(query[j]) {
|
||||
match = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if match {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func truncateRunes(text string, limit int) string {
|
||||
runes := []rune(text)
|
||||
if len(runes) <= limit {
|
||||
|
||||
Reference in New Issue
Block a user