fix(pipeline): let a re-saved visit keep the newest evidence

This commit is contained in:
2026-10-22 00:25:59 +01:00
parent 1e578985e0
commit fac055da6e
2 changed files with 15 additions and 31 deletions
+6 -4
View File
@@ -39,18 +39,20 @@ func TestFinishStepAcceptsAnIdenticalEvidenceReplay(t *testing.T) {
if err = s.FinishStep(ctx, id, step); err != nil {
t.Fatalf("first evidence write: %v", err)
}
step.Status = "running"
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")
step.Status = "running"
if err = s.FinishStep(ctx, id, step); err != nil {
t.Fatalf("a later settlement of the same visit must persist: %v", err)
}
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)
if got := run.Steps[0].FileChanges; got == nil || len(got.Files) != 1 || got.Files[0].Kind != "modified" {
t.Fatalf("latest evidence not stored: %+v", got)
}
}
+9 -27
View File
@@ -34,35 +34,17 @@ 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.
// writeStepEvidence stores one visit's evidence row, replacing the same visit's
// previous record. The runner persists a visit more than once: an export gate
// re-saves it after the session export adds files, and a settlement may follow a
// cleanup save. Those later writes describe the same visit more completely, and
// failing them aborted whole runs on a raw primary-key error. Per run the writes
// are serialized, so the last record is the newest known evidence.
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",
_, err := tx.ExecContext(ctx,
"INSERT INTO "+table+"(run_id,sequence,"+column+") VALUES(?,?,?) ON CONFLICT(run_id,sequence) DO UPDATE SET "+column+"=excluded."+column,
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
return err
}
func (s *Store) BeginStep(ctx context.Context, id int64, sequence int, key string, input map[string]any) error {