diff --git a/internal/pipeline/runs_evidence_test.go b/internal/pipeline/runs_evidence_test.go new file mode 100644 index 00000000..bc68e219 --- /dev/null +++ b/internal/pipeline/runs_evidence_test.go @@ -0,0 +1,56 @@ +package pipeline + +import ( + "context" + "path/filepath" + "testing" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// A run must never fail because the runner persisted the same settled visit +// twice: an ambiguous commit, a retried finish or a late settlement can repeat +// the write. Identical evidence is a replay; different evidence still fails. +func TestFinishStepAcceptsAnIdenticalEvidenceReplay(t *testing.T) { + ctx := context.Background() + s, err := Open(filepath.Join(t.TempDir(), "replay.db")) + if err != nil { + t.Fatal(err) + } + defer s.Close() + 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) + } + changes := &protocol.StageFileChanges{Status: "complete", Files: []protocol.FileChange{{Path: "a.txt", Kind: "added", Patch: "+a\n"}}} + step := protocol.RunStep{ + Sequence: 1, Status: "running", FileChanges: changes, + Artifacts: []protocol.StageArtifact{{Name: "out.txt", SizeBytes: 3, ContentType: "text/plain", URL: "/artifacts/1/1/out.txt"}}, + Control: &protocol.ControlEvidence{Visit: 1}, + } + if err = s.FinishStep(ctx, id, step); err != nil { + t.Fatalf("first evidence write: %v", err) + } + if err = s.FinishStep(ctx, id, step); err != nil { + t.Fatalf("identical replay must be accepted, got %v", err) + } + step.FileChanges = &protocol.StageFileChanges{Status: "complete", Files: []protocol.FileChange{{Path: "a.txt", Kind: "modified", Patch: "-a\n+b\n"}}} + if err = s.FinishStep(ctx, id, step); err == nil { + t.Fatal("different evidence must not overwrite a finished record") + } + run, err := s.GetRun(ctx, id) + if err != nil { + t.Fatal(err) + } + if got := run.Steps[0].FileChanges; got == nil || len(got.Files) != 1 || got.Files[0].Kind != "added" { + t.Fatalf("recorded evidence changed: %+v", got) + } +} diff --git a/internal/pipeline/runs_mutate.go b/internal/pipeline/runs_mutate.go index 204ee489..9a8aa551 100644 --- a/internal/pipeline/runs_mutate.go +++ b/internal/pipeline/runs_mutate.go @@ -34,6 +34,37 @@ func affected(result sql.Result, err error) error { return nil } +// writeStepEvidence stores one visit's evidence row without ever rewriting a +// different record. A repeated finish of the same visit with byte-identical +// evidence is a harmless replay (the runner may persist after an ambiguous +// commit) and is accepted; a different record is still refused, so finished +// evidence stays immutable. Before this, any replay hit the table's primary key +// and failed the whole run with a raw constraint error. +func writeStepEvidence(ctx context.Context, tx *sql.Tx, table, column string, id int64, sequence int, data string) error { + result, err := tx.ExecContext(ctx, + "INSERT INTO "+table+"(run_id,sequence,"+column+") VALUES(?,?,?) ON CONFLICT(run_id,sequence) DO NOTHING", + id, sequence, data) + if err != nil { + return err + } + n, err := result.RowsAffected() + if err != nil { + return err + } + if n == 1 { + return nil + } + var existing string + if err := tx.QueryRowContext(ctx, + "SELECT "+column+" FROM "+table+" WHERE run_id=? AND sequence=?", id, sequence).Scan(&existing); err != nil { + return err + } + if existing != data { + return fmt.Errorf("finished evidence overwritten") + } + return nil +} + func (s *Store) BeginStep(ctx context.Context, id int64, sequence int, key string, input map[string]any) error { encoded, err := json.Marshal(input) if err != nil { @@ -119,7 +150,7 @@ WHERE run_id=? AND sequence=? AND status='running'`, step.Status, step.Outcome, 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 { + if err = writeStepEvidence(ctx, tx, "step_file_changes", "changes_json", id, step.Sequence, string(data)); err != nil { return err } } @@ -128,7 +159,7 @@ WHERE run_id=? AND sequence=? AND status='running'`, step.Status, step.Outcome, if err != nil { return err } - if _, err := tx.ExecContext(ctx, "INSERT INTO step_control(run_id,sequence,evidence_json) VALUES(?,?,?)", id, step.Sequence, string(data)); err != nil { + if err := writeStepEvidence(ctx, tx, "step_control", "evidence_json", id, step.Sequence, string(data)); err != nil { return err } } @@ -137,7 +168,7 @@ WHERE run_id=? AND sequence=? AND status='running'`, step.Status, step.Outcome, if err != nil { return err } - if _, err := tx.ExecContext(ctx, "INSERT INTO step_artifacts(run_id,sequence,artifacts_json) VALUES(?,?,?)", id, step.Sequence, string(data)); err != nil { + if err := writeStepEvidence(ctx, tx, "step_artifacts", "artifacts_json", id, step.Sequence, string(data)); err != nil { return err } } @@ -157,7 +188,7 @@ WHERE run_id=? AND sequence=? AND status='running'`, step.Status, step.Outcome, if err != nil { return err } - if _, err := tx.ExecContext(ctx, "INSERT INTO step_session_exports(run_id,sequence,export_json) VALUES(?,?,?)", id, step.Sequence, string(data)); err != nil { + if err := writeStepEvidence(ctx, tx, "step_session_exports", "export_json", id, step.Sequence, string(data)); err != nil { return err } } @@ -181,7 +212,7 @@ WHERE run_id=? AND sequence=? AND status='running'`, step.Status, step.Outcome, if err != nil { return err } - if _, err := tx.ExecContext(ctx, "INSERT INTO step_usage(run_id,sequence,usage_json) VALUES(?,?,?)", id, step.Sequence, string(data)); err != nil { + if err := writeStepEvidence(ctx, tx, "step_usage", "usage_json", id, step.Sequence, string(data)); err != nil { return err } }