fix(pipeline): let a re-saved visit keep the newest evidence
This commit is contained in:
@@ -39,18 +39,20 @@ func TestFinishStepAcceptsAnIdenticalEvidenceReplay(t *testing.T) {
|
|||||||
if err = s.FinishStep(ctx, id, step); err != nil {
|
if err = s.FinishStep(ctx, id, step); err != nil {
|
||||||
t.Fatalf("first evidence write: %v", err)
|
t.Fatalf("first evidence write: %v", err)
|
||||||
}
|
}
|
||||||
|
step.Status = "running"
|
||||||
if err = s.FinishStep(ctx, id, step); err != nil {
|
if err = s.FinishStep(ctx, id, step); err != nil {
|
||||||
t.Fatalf("identical replay must be accepted, got %v", err)
|
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"}}}
|
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 {
|
step.Status = "running"
|
||||||
t.Fatal("different evidence must not overwrite a finished record")
|
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)
|
run, err := s.GetRun(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if got := run.Steps[0].FileChanges; got == nil || len(got.Files) != 1 || got.Files[0].Kind != "added" {
|
if got := run.Steps[0].FileChanges; got == nil || len(got.Files) != 1 || got.Files[0].Kind != "modified" {
|
||||||
t.Fatalf("recorded evidence changed: %+v", got)
|
t.Fatalf("latest evidence not stored: %+v", got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,35 +34,17 @@ func affected(result sql.Result, err error) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeStepEvidence stores one visit's evidence row without ever rewriting a
|
// writeStepEvidence stores one visit's evidence row, replacing the same visit's
|
||||||
// different record. A repeated finish of the same visit with byte-identical
|
// previous record. The runner persists a visit more than once: an export gate
|
||||||
// evidence is a harmless replay (the runner may persist after an ambiguous
|
// re-saves it after the session export adds files, and a settlement may follow a
|
||||||
// commit) and is accepted; a different record is still refused, so finished
|
// cleanup save. Those later writes describe the same visit more completely, and
|
||||||
// evidence stays immutable. Before this, any replay hit the table's primary key
|
// failing them aborted whole runs on a raw primary-key error. Per run the writes
|
||||||
// and failed the whole run with a raw constraint error.
|
// 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 {
|
func writeStepEvidence(ctx context.Context, tx *sql.Tx, table, column string, id int64, sequence int, data string) error {
|
||||||
result, err := tx.ExecContext(ctx,
|
_, err := tx.ExecContext(ctx,
|
||||||
"INSERT INTO "+table+"(run_id,sequence,"+column+") VALUES(?,?,?) ON CONFLICT(run_id,sequence) DO NOTHING",
|
"INSERT INTO "+table+"(run_id,sequence,"+column+") VALUES(?,?,?) ON CONFLICT(run_id,sequence) DO UPDATE SET "+column+"=excluded."+column,
|
||||||
id, sequence, data)
|
id, sequence, data)
|
||||||
if err != nil {
|
return err
|
||||||
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 {
|
func (s *Store) BeginStep(ctx context.Context, id int64, sequence int, key string, input map[string]any) error {
|
||||||
|
|||||||
Reference in New Issue
Block a user