Improve workflow UI, retain stage diffs, and add portable pipeline transfers
Make OpenCode authoring responsive and expand the managed skill references. Add conflict-aware import/export, real-provider reference workflows, and tracked V2 generation for reliable budget accounting.
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFileChangeMigrationAndAtomicImmutableFinish(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db := filepath.Join(t.TempDir(), "old.db")
|
||||
s, err := Open(db)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.Submit(ctx, "audit", Template([]string{"one"}, true)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, _ := s.List(ctx)
|
||||
def, _ := s.Get(ctx, items[0].ID, 1)
|
||||
id, _, err := s.CreateRun(ctx, def, "key", "workspace", false, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.BeginStep(ctx, id, 1, "one", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Simulate the pre-diff database while retaining its existing history.
|
||||
if _, err = s.db.Exec("DROP TABLE step_file_changes"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.Close()
|
||||
s, err = Open(db)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer s.Close()
|
||||
run, err := s.GetRun(ctx, id)
|
||||
if err != nil || run.Steps[0].FileChanges != nil {
|
||||
t.Fatal("historical step misleading", run, err)
|
||||
}
|
||||
c := &protocol.StageFileChanges{Status: "complete", Files: []protocol.FileChange{{Path: "file", Kind: "added", Patch: "+new\n"}}}
|
||||
step := protocol.RunStep{Sequence: 1, Status: "completed", Outcome: "success", Target: protocol.SuccessEnd, FileChanges: c}
|
||||
if err = s.FinishStep(ctx, id, step); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
step.FileChanges = &protocol.StageFileChanges{Status: "complete"}
|
||||
if err = s.FinishStep(ctx, id, step); err == nil {
|
||||
t.Fatal("finished evidence overwritten")
|
||||
}
|
||||
run, err = s.GetRun(ctx, id)
|
||||
if err != nil || !reflect.DeepEqual(run.Steps[0].FileChanges, c) {
|
||||
t.Fatal("diff lost/changed", run, err)
|
||||
}
|
||||
if err = s.BeginStep(ctx, id, 2, "one", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.db.Exec(`CREATE TRIGGER fail_diff BEFORE INSERT ON step_file_changes BEGIN SELECT RAISE(ABORT,'fixture'); END`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
step.Sequence = 2
|
||||
step.FileChanges = c
|
||||
if err = s.FinishStep(ctx, id, step); err == nil {
|
||||
t.Fatal("expected write failure")
|
||||
}
|
||||
run, err = s.GetRun(ctx, id)
|
||||
if err != nil || run.Steps[1].Status != "running" || run.Steps[1].FileChanges != nil {
|
||||
t.Fatal("partial finish committed", run, err)
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,9 @@ CREATE INDEX IF NOT EXISTS run_latest ON pipeline_runs(id DESC);
|
||||
CREATE TABLE IF NOT EXISTS step_notifications (
|
||||
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, receipts_json TEXT NOT NULL,
|
||||
PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence));
|
||||
CREATE TABLE IF NOT EXISTS step_file_changes (
|
||||
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, changes_json TEXT NOT NULL,
|
||||
PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence));
|
||||
CREATE TABLE IF NOT EXISTS run_session_owners (
|
||||
run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, backend TEXT NOT NULL, reference TEXT NOT NULL DEFAULT '',
|
||||
PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence));
|
||||
@@ -182,6 +185,7 @@ func (s *Store) GetRun(ctx context.Context, id int64) (*protocol.RunDetail, erro
|
||||
}
|
||||
rows, err := tx.QueryContext(ctx, `SELECT sequence,stage_key,status,outcome,target,input_json,output_json,diagnostic,session_id,agent_active,transcript,
|
||||
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'),
|
||||
strftime('%Y-%m-%dT%H:%M:%SZ',started_at),COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ',finished_at),'') FROM run_steps WHERE run_id=? ORDER BY sequence`, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -190,7 +194,8 @@ strftime('%Y-%m-%dT%H:%M:%SZ',started_at),COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ'
|
||||
for rows.Next() {
|
||||
var step protocol.RunStep
|
||||
var notifications 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, &step.StartedAt, &step.FinishedAt); err != nil {
|
||||
var fileChanges 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, &step.StartedAt, &step.FinishedAt); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
@@ -198,6 +203,10 @@ strftime('%Y-%m-%dT%H:%M:%SZ',started_at),COALESCE(strftime('%Y-%m-%dT%H:%M:%SZ'
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err := json.Unmarshal([]byte(fileChanges), &step.FileChanges); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err := decodeValues(input, &step.Input); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
@@ -320,8 +329,25 @@ func (s *Store) FinishStep(ctx context.Context, id int64, step protocol.RunStep)
|
||||
if len(step.Transcript) > 32<<10 {
|
||||
step.Transcript = step.Transcript[:32<<10] + "\n[audit preview truncated; inspect the retained OpenCode session for full evidence]"
|
||||
}
|
||||
return affected(s.db.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))
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if err := affected(tx.ExecContext(ctx, `UPDATE run_steps SET status=?,outcome=?,target=?,output_json=?,diagnostic=?,transcript=?,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 {
|
||||
return err
|
||||
}
|
||||
if step.FileChanges != nil {
|
||||
data, err := json.Marshal(step.FileChanges)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, "INSERT INTO step_file_changes(run_id,sequence,changes_json) VALUES(?,?,?)", id, step.Sequence, string(data)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (s *Store) FinishRun(ctx context.Context, id int64, status, outcome, diagnostic string, output map[string]any) error {
|
||||
|
||||
Reference in New Issue
Block a user