Record run input directory and retry failed runs with original inputs

This commit is contained in:
2026-10-09 14:23:21 +01:00
parent 42600f6961
commit 6bd0e28ce6
19 changed files with 507 additions and 22 deletions
+72
View File
@@ -0,0 +1,72 @@
package pipeline
import (
"context"
"path/filepath"
"reflect"
"testing"
)
func TestRetryStorageMigrationPersistenceAndKeyConflicts(t *testing.T) {
ctx := context.Background()
path := filepath.Join(t.TempDir(), "legacy.db")
store, err := Open(path)
if err != nil {
t.Fatal(err)
}
if _, err := store.Submit(ctx, "workflow", Template([]string{"one"}, true)); err != nil {
t.Fatal(err)
}
items, _ := store.List(ctx)
d, _ := store.Get(ctx, items[0].ID, 1)
input := map[string]any{"prompt": "legacy input"}
id, _, err := store.CreateRun(ctx, d, "original", "/original", false, nil, input)
if err != nil {
t.Fatal(err)
}
if err := store.FinishRun(ctx, id, "failed", "error", "failed", nil); err != nil {
t.Fatal(err)
}
original, err := store.GetRun(ctx, id)
if err != nil {
t.Fatal(err)
}
// Simulate the schema before retry support, preserving its run history.
if _, err := store.db.ExecContext(ctx, "DROP TABLE run_retries"); err != nil {
t.Fatal(err)
}
if err := store.Close(); err != nil {
t.Fatal(err)
}
store, err = Open(path)
if err != nil {
t.Fatal(err)
}
after, err := store.GetRun(ctx, id)
if err != nil || !reflect.DeepEqual(original, after) {
t.Fatal("migration modified legacy history", after, err)
}
retryID, created, err := store.CreateRun(ctx, d, "retry", "/original", false, nil, input, id)
if err != nil || !created {
t.Fatal("retry creation failed", err)
}
if _, _, err := store.CreateRun(ctx, d, "retry", "/original", false, nil, input); err == nil {
t.Fatal("request key accepted with different provenance")
}
if err := store.Close(); err != nil {
t.Fatal(err)
}
store, err = Open(path)
if err != nil {
t.Fatal(err)
}
defer store.Close()
retry, err := store.GetRun(ctx, retryID)
if err != nil || retry.RetryOf != id || !reflect.DeepEqual(retry.Input, input) {
t.Fatal("retry metadata did not survive reopen", retry, err)
}
existing, created, err := store.CreateRun(ctx, d, "retry", "/original", false, nil, input, id)
if err != nil || created || existing != retryID {
t.Fatal("persisted retry lost idempotency", existing, created, err)
}
}
+35 -6
View File
@@ -30,11 +30,20 @@ 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 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));`)
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));`)
return err
}
func (s *Store) CreateRun(ctx context.Context, d *protocol.PipelineDetail, key, workspace string, tools bool, paths []string, input map[string]any) (int64, bool, error) {
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) {
var parent int64
if len(retryOf) > 0 {
parent = retryOf[0]
}
if parent < 0 {
return 0, false, fmt.Errorf("invalid retry parent")
}
encoded, err := json.Marshal(input)
if err != nil {
return 0, false, err
@@ -51,7 +60,11 @@ func (s *Store) CreateRun(ctx context.Context, d *protocol.PipelineDetail, key,
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)
if err == nil {
if pid != d.ID || version != d.Version || oldInput != string(encoded) || oldWorkspace != workspace || oldPaths != string(writes) || oldTools != tools {
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 {
return 0, false, fmt.Errorf("request key already used for a different run")
}
return id, false, nil
@@ -59,12 +72,28 @@ func (s *Store) CreateRun(ctx context.Context, d *protocol.PipelineDetail, key,
if err != sql.ErrNoRows {
return 0, false, err
}
result, err := s.db.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))
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return 0, false, err
}
defer tx.Rollback()
result, err := tx.ExecContext(ctx, "INSERT INTO pipeline_runs(pipeline_id,version,request_key,workspace,allow_tools,write_paths,input_json) VALUES (?,?,?,?,?,?,?)", d.ID, d.Version, key, workspace, tools, string(writes), string(encoded))
if err != nil {
return 0, false, err
}
id, err = result.LastInsertId()
return id, true, err
if err != nil {
return 0, false, err
}
if parent > 0 {
if _, err := tx.ExecContext(ctx, "INSERT INTO run_retries(run_id,original_run_id) VALUES(?,?)", id, parent); err != nil {
return 0, false, err
}
}
if err := tx.Commit(); err != nil {
return 0, false, err
}
return id, true, nil
}
const runSelect = `SELECT r.id,r.pipeline_id,p.name,r.version,v.content_hash,r.status,r.outcome,r.current_stage,
@@ -121,7 +150,7 @@ func (s *Store) GetRun(ctx context.Context, id int64) (*protocol.RunDetail, erro
return nil, err
}
var input, output, paths string
if err := tx.QueryRowContext(ctx, "SELECT workspace,allow_tools,write_paths,input_json,output_json FROM pipeline_runs WHERE id=?", id).Scan(&r.Workspace, &r.AllowTools, &paths, &input, &output); 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) FROM pipeline_runs WHERE id=?", id).Scan(&r.Workspace, &r.AllowTools, &paths, &input, &output, &r.RetryOf); err != nil {
return nil, err
}
if err := decodeValues(input, &r.Input); err != nil {