| 1 | package team |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "encoding/json" |
| 7 | "strings" |
| 8 | "unicode/utf8" |
| 9 | ) |
| 10 | |
| 11 | // ExecutorEvidenceValidator verifies a trusted executor attestation, using the |
| 12 | // enclosing transaction. A missing validator refuses bug check results. |
| 13 | type ExecutorEvidenceValidator func(context.Context, *sql.Tx, Scope, Handle, ArtifactSpec) (Provenance, error) |
| 14 | |
| 15 | func (s *Store) FreezeBugEvidence(ctx context.Context, scope Scope, h Handle, expectedSymptom, definitionHash, reproductionHash string) error { |
| 16 | if !utf8.ValidString(expectedSymptom) || len(expectedSymptom) < 1 || len(expectedSymptom) > 16384 { |
| 17 | return fail(CodeInvalid, "invalid_bug_symptom") |
| 18 | } |
| 19 | return s.tx(ctx, func(tx *sql.Tx) error { |
| 20 | if err := admissionOpen(ctx, tx); err != nil { |
| 21 | return err |
| 22 | } |
| 23 | hd, err := current(ctx, tx, scope, h, s.clock.Now()) |
| 24 | if err != nil { |
| 25 | return err |
| 26 | } |
| 27 | if hd.job.Revision != h.Revision { |
| 28 | return fail(CodeRevision, "bug_revision_changed") |
| 29 | } |
| 30 | if hd.step.Capability != CapReproduce || !hd.job.Evidence.RequireBugFreeze { |
| 31 | return fail(CodeScope, "bug_reproduce_required") |
| 32 | } |
| 33 | if definitionHash != hd.job.Evidence.CheckDefinitionHash { |
| 34 | return fail(CodeEvidence, "check_definition_changed") |
| 35 | } |
| 36 | cand, err := latestAny(ctx, tx, scope.Job, RoleCandidate) |
| 37 | if err != nil { |
| 38 | return err |
| 39 | } |
| 40 | if cand != nil { |
| 41 | return fail(CodeState, "candidate_exists") |
| 42 | } |
| 43 | repro, err := latestAny(ctx, tx, scope.Job, RoleReproduction) |
| 44 | if err != nil { |
| 45 | return err |
| 46 | } |
| 47 | if repro == nil || repro.Hash != reproductionHash || repro.Attempt != h.Attempt || repro.Fence != h.Fence { |
| 48 | return fail(CodeEvidence, "attempt_reproduction_required") |
| 49 | } |
| 50 | evidence := hd.job.Evidence |
| 51 | if evidence.FrozenReproductionHash != "" { |
| 52 | if evidence.FrozenReproductionHash != reproductionHash || evidence.ExpectedSymptom != expectedSymptom { |
| 53 | return fail(CodeConflict, "bug_evidence_already_frozen") |
| 54 | } |
| 55 | return nil |
| 56 | } |
| 57 | evidence.ExpectedSymptom = expectedSymptom |
| 58 | evidence.FrozenReproductionHash = reproductionHash |
| 59 | raw, err := json.Marshal(evidence) |
| 60 | if err != nil { |
| 61 | return err |
| 62 | } |
| 63 | result, err := tx.ExecContext(ctx, `UPDATE jobs SET evidence=?, revision=revision+1, updated_at=? WHERE id=? AND revision=?`, string(raw), ts(s.clock.Now()), scope.Job, h.Revision) |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | if n, _ := result.RowsAffected(); n != 1 { |
| 68 | return fail(CodeRevision, "bug_revision_changed") |
| 69 | } |
| 70 | return appendEvent(ctx, tx, scope, "bug.evidence_frozen", h.Runner, map[string]string{"reproduction": reproductionHash, "definition": definitionHash}, s.clock.Now()) |
| 71 | }) |
| 72 | } |
| 73 | func completedBugReproduction(ctx context.Context, tx *sql.Tx, hd holder) error { |
| 74 | e := hd.job.Evidence |
| 75 | if e.FrozenReproductionHash == "" { |
| 76 | return fail(CodeEvidence, "bug_evidence_not_frozen") |
| 77 | } |
| 78 | repro, err := latestAny(ctx, tx, hd.job.ID, RoleReproduction) |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | if repro == nil || repro.Hash != e.FrozenReproductionHash || repro.Attempt != hd.attempt.ID { |
| 83 | return fail(CodeEvidence, "attempt_reproduction_required") |
| 84 | } |
| 85 | base, err := latestAny(ctx, tx, hd.job.ID, RoleBaseResult) |
| 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | if base == nil || base.Attempt != hd.attempt.ID { |
| 90 | return fail(CodeEvidence, "attempt_base_failure_required") |
| 91 | } |
| 92 | var result CheckResult |
| 93 | if strictJSON(base.Payload, &result) != nil || result.ReproductionHash != e.FrozenReproductionHash || result.CheckDefinitionHash != e.CheckDefinitionHash || result.SubjectHash != hd.job.Target.BaseRevision || result.Outcome != CheckFail || result.TestsRun <= 0 || result.TestsFailed <= 0 || result.TestsFailed > result.TestsRun || !strings.Contains(result.FailureMessage, e.ExpectedSymptom) { |
| 94 | return fail(CodeEvidence, "frozen_base_failure_required") |
| 95 | } |
| 96 | return nil |
| 97 | } |
| 98 | |