feat: require Git-bound checks and independent model families
9 files changed,
+961
−53
.rastrillo/budgets.txt+1 −1internal/equipo/team/actions.go+49 −4internal/equipo/team/evidence.go+96 −31internal/equipo/team/git_evidence.go+272 −0internal/equipo/team/git_evidence_test.go+517 −0internal/equipo/team/model.go+19 −15internal/equipo/team/policy.go+3 −0internal/equipo/team/shared.go+1 −1internal/equipo/team/store.go+3 −1
CI failed — run details
diff --git a/.rastrillo/budgets.txt b/.rastrillo/budgets.txt| index 854391ed..9bb98b50 100644 |
| --- a/.rastrillo/budgets.txt |
| +++ b/.rastrillo/budgets.txt |
| @@ -9,7 +9,7 @@ |
| # time <dir> <duration> <reason> |
| # perf <METHOD> <path> <duration>|skip <reason> |
| # boot <duration> <reason> |
| -size internal/equipo/team 7000 - Preserve the plan-mandated core layout; Task 2 adds quarantine release, recovery charges, grant fencing and conservative clock accounting to the copied transactional core. |
| +size internal/equipo/team 7600 - Preserve the plan-mandated core layout; Tasks 2-4 add recovery, subscription accounting, Git candidate identity, repository checks and observed isolation provenance to the copied transactional core. |
| perf GET /* skip Unknown private paths render 404; this fallback contains no screens. |
| time internal/equipo/team 15s Copied core crash, recovery and concurrency tests take about 7.7s idle and 13.6s on the loaded shared machine. |
| time internal/equipotest 15s Real NoProof sign-in ceremonies wait 3s each; the package takes about 9.3s. |
diff --git a/internal/equipo/team/actions.go b/internal/equipo/team/actions.go| index f778eb32..c6102425 100644 |
| --- a/internal/equipo/team/actions.go |
| +++ b/internal/equipo/team/actions.go |
| @@ -171,6 +171,25 @@ func (s *Store) Intend(ctx context.Context, scope Scope, h Handle, in IntentSpec |
| return fail(CodeScope, "capability %s cannot perform %s", hd.step.Capability, in.Operation) |
| } |
| if in.Operation == OpForgePublish { |
| + if hd.job.Policy.Version == 2 { |
| + observed, err := s.observedProvenance(ctx, tx, scope, h) |
| + if err != nil { |
| + return err |
| + } |
| + var p publication |
| + if err := strictJSON(payload, &p); err != nil { |
| + return fail(CodeInvalid, "publication_invalid") |
| + } |
| + p.Isolation = observed.Isolation |
| + raw, err := json.Marshal(p) |
| + if err != nil { |
| + return err |
| + } |
| + payload, hash, err = canonical(raw) |
| + if err != nil { |
| + return err |
| + } |
| + } |
| if err := checkPublication(ctx, tx, hd.job, payload); err != nil { |
| return err |
| } |
| @@ -225,9 +244,11 @@ func (s *Store) Intend(ctx context.Context, scope Scope, h Handle, in IntentSpec |
| // publication is the forge.publish_branch payload. |
| type publication struct { |
| - Repository string `json:"repository"` |
| - Ref string `json:"ref"` |
| - Head string `json:"head"` |
| + CandidateHash string `json:"candidate_hash,omitempty"` |
| + Isolation IsolationSnapshot `json:"isolation,omitzero"` |
| + Repository string `json:"repository"` |
| + Ref string `json:"ref"` |
| + Head string `json:"head"` |
| } |
| // checkPublication refuses a publication whose repository, ref or head is |
| @@ -251,9 +272,18 @@ func checkPublication(ctx context.Context, tx *sql.Tx, j jobRow, payload []byte) |
| if err != nil { |
| return err |
| } |
| - if cand == nil || cand.Hash != p.Head { |
| + if !publicationMatches(p, cand, j) { |
| return fail(CodeEvidence, "publication head %q is not the latest candidate", p.Head) |
| } |
| + if j.Policy.Version == 2 { |
| + rep, err := evaluateSoftwareFix(ctx, tx, j, true) |
| + if err != nil { |
| + return err |
| + } |
| + if !rep.Satisfied || !isolationContains(p.Isolation, rep.Isolation) { |
| + return fail(CodeEvidence, "publication_evidence_invalid") |
| + } |
| + } |
| return nil |
| } |
| @@ -330,6 +360,21 @@ func (s *Store) Admit(ctx context.Context, scope Scope, id ActionID) (Dispatch, |
| if err != nil { |
| return err |
| } |
| + if a.Operation == OpForgePublish { |
| + if err := checkPublication(ctx, tx, j, a.Payload); err != nil { |
| + return err |
| + } |
| + if j.Policy.Version == 2 { |
| + observed, err := s.observedProvenance(ctx, tx, scope, Handle{Attempt: att.ID, Fence: a.Fence, Runner: att.Runner}) |
| + if err != nil { |
| + return err |
| + } |
| + var p publication |
| + if strictJSON(a.Payload, &p) != nil || !isolationContains(p.Isolation, observed.Isolation) { |
| + return fail(CodeEvidence, "publication_isolation_changed") |
| + } |
| + } |
| + } |
| if j.Policy.Version == 2 { |
| if err := unresolvedTurn(ctx, tx, j.ID, a.ID); err != nil { |
| return err |
diff --git a/internal/equipo/team/evidence.go b/internal/equipo/team/evidence.go| index ee7a2e46..3e09e3a3 100644 |
| --- a/internal/equipo/team/evidence.go |
| +++ b/internal/equipo/team/evidence.go |
| @@ -72,9 +72,6 @@ func (s *Store) Register(ctx context.Context, scope Scope, h Handle, spec Artifa |
| if spec.MediaType == "" { |
| return Artifact{}, fail(CodeInvalid, "artifact needs a media type") |
| } |
| - if err := spec.Provenance.validate(); err != nil { |
| - return Artifact{}, err |
| - } |
| hash := hashBytes(spec.Payload) |
| var out Artifact |
| err := s.tx(ctx, func(tx *sql.Tx) error { |
| @@ -89,6 +86,23 @@ func (s *Store) Register(ctx context.Context, scope Scope, h Handle, spec Artifa |
| if !roleAllowed(hd.step.Capability, spec.Role) { |
| return fail(CodeScope, "capability %s may not register %s", hd.step.Capability, spec.Role) |
| } |
| + if hd.job.Policy.Version == 2 { |
| + spec.Provenance, err = s.observedProvenance(ctx, tx, scope, h) |
| + if err != nil { |
| + return err |
| + } |
| + if spec.Role == RoleCandidate { |
| + if !gitCandidateFields(spec.Payload) { |
| + return fail(CodeEvidence, "git_candidate_invalid") |
| + } |
| + spec.Payload, hash, err = canonical(spec.Payload) |
| + if err != nil { |
| + return fail(CodeEvidence, "git_candidate_invalid") |
| + } |
| + } |
| + } else if err := spec.Provenance.validate(); err != nil { |
| + return err |
| + } |
| if err := validateArtifact(ctx, tx, hd.job, spec, hash); err != nil { |
| return err |
| } |
| @@ -165,7 +179,7 @@ func validateArtifact(ctx context.Context, tx *sql.Tx, j jobRow, spec ArtifactSp |
| if subj == nil || subj.Hash != rv.SubjectHash { |
| return fail(CodeEvidence, "review of %q, latest %s is %q", short(rv.SubjectHash), subjectRole, short(hashOf(subj))) |
| } |
| - if !independent(subj.Provenance, spec.Provenance, j.Policy.RequireDistinctReviewProvider) { |
| + if !independentForPolicy(subj.Provenance, spec.Provenance, j.Policy) { |
| return fail(CodeEvidence, "review is not independent of the %s it reviews", subjectRole) |
| } |
| case RoleReconciliation: |
| @@ -176,7 +190,21 @@ func validateArtifact(ctx context.Context, tx *sql.Tx, j jobRow, spec ArtifactSp |
| if rc.Discrepancies == nil { |
| return fail(CodeEvidence, "reconciliation without an explicit discrepancies list") |
| } |
| - case RoleCandidate, RoleSourceRecord: |
| + case RoleRepositoryChecks: |
| + if j.Policy.Version != 2 { |
| + return fail(CodeEvidence, "repository_checks_require_v2") |
| + } |
| + cand, err := latestAny(ctx, tx, j.ID, RoleCandidate) |
| + if err != nil { |
| + return err |
| + } |
| + return validateRepositoryChecks(spec.Payload, cand, j) |
| + case RoleCandidate: |
| + if j.Policy.Version == 2 { |
| + _, err := gitCandidate(&Artifact{MediaType: spec.MediaType, Payload: spec.Payload, Hash: hash}, j) |
| + return err |
| + } |
| + case RoleSourceRecord: |
| } |
| return nil |
| } |
| @@ -216,19 +244,21 @@ func short(h string) string { |
| // EvidenceReport is Conclude's finding: which artifacts and publication |
| // satisfied the predicate, or the first reason it is not satisfied. |
| type EvidenceReport struct { |
| - Predicate Predicate |
| - Satisfied bool |
| - Reason string |
| - Reproduction string // hash |
| - BaseResult string |
| - Candidate string |
| - CandidateResult string |
| - Review string |
| - Reviewer Provenance |
| - Publication ActionID |
| - PublishReceipt string |
| - Sources []string |
| - Reconciliation string |
| + RepositoryChecks string |
| + Isolation IsolationSnapshot |
| + Predicate Predicate |
| + Satisfied bool |
| + Reason string |
| + Reproduction string // hash |
| + BaseResult string |
| + Candidate string |
| + CandidateResult string |
| + Review string |
| + Reviewer Provenance |
| + Publication ActionID |
| + PublishReceipt string |
| + Sources []string |
| + Reconciliation string |
| } |
| // Conclude evaluates the job's success predicate against stored evidence |
| @@ -261,14 +291,16 @@ func (s *Store) Conclude(ctx context.Context, scope Scope, actor string) (Eviden |
| if children != 0 { |
| return fail(CodeState, "job %d has %d unfinished children", j.ID, children) |
| } |
| - switch j.Evidence.Predicate { |
| - case PredicateSoftwareFix: |
| - rep, err = evaluateSoftwareFix(ctx, tx, j) |
| - case PredicateAttestedReconciliation: |
| - rep, err = evaluateReconciliation(ctx, tx, j) |
| - default: |
| - return fail(CodeUnknownKind, "predicate %q", j.Evidence.Predicate) |
| + if j.Policy.Version == 2 { |
| + var unfinished int |
| + if err := tx.QueryRowContext(ctx, `SELECT COUNT(*) FROM steps WHERE job_id=? AND state IN ('pending','running')`, int64(j.ID)).Scan(&unfinished); err != nil { |
| + return err |
| + } |
| + if unfinished != 0 { |
| + return fail(CodeState, "workflow_unfinished") |
| + } |
| } |
| + rep, err = evaluateEvidence(ctx, tx, j, false) |
| if err != nil { |
| return err |
| } |
| @@ -296,7 +328,7 @@ func (s *Store) Conclude(ctx context.Context, scope Scope, actor string) (Eviden |
| return rep, nil |
| } |
| -func evaluateSoftwareFix(ctx context.Context, tx *sql.Tx, j jobRow) (EvidenceReport, error) { |
| +func evaluateSoftwareFix(ctx context.Context, tx *sql.Tx, j jobRow, ready bool) (EvidenceReport, error) { |
| rep := EvidenceReport{Predicate: PredicateSoftwareFix} |
| miss := func(why string) (EvidenceReport, error) { |
| rep.Reason = why |
| @@ -324,7 +356,7 @@ func evaluateSoftwareFix(ctx context.Context, tx *sql.Tx, j jobRow) (EvidenceRep |
| if br.ReproductionHash != repro.Hash || br.CheckDefinitionHash != j.Evidence.CheckDefinitionHash || br.SubjectHash != j.Target.BaseRevision { |
| return miss("base result does not name the frozen reproduction, trusted check definition and base revision") |
| } |
| - if br.Outcome != CheckFail || br.TestsRun == 0 || br.TestsFailed == 0 { |
| + if br.Outcome != CheckFail || br.TestsRun <= 0 || br.TestsFailed <= 0 || br.TestsFailed > br.TestsRun { |
| return miss("base result is " + string(br.Outcome) + " with " + itoa(br.TestsRun) + " run and " + itoa(br.TestsFailed) + " failed; expected a failing test") |
| } |
| if !strings.Contains(br.FailureMessage, j.Evidence.ExpectedSymptom) { |
| @@ -339,6 +371,22 @@ func evaluateSoftwareFix(ctx context.Context, tx *sql.Tx, j jobRow) (EvidenceRep |
| return miss("no candidate") |
| } |
| rep.Candidate = cand.Hash |
| + if j.Policy.Version == 2 { |
| + latest, err := latestAny(ctx, tx, j.ID, RoleCandidate) |
| + if err != nil { |
| + return rep, err |
| + } |
| + if latest == nil || latest.ID != cand.ID { |
| + return miss("candidate_changed") |
| + } |
| + if _, err := gitCandidate(cand, j); err != nil { |
| + return miss("git_candidate_invalid") |
| + } |
| + rep.Isolation, err = lineageIsolation(ctx, tx, j.ID, cand.Provenance.Isolation) |
| + if err != nil { |
| + return rep, err |
| + } |
| + } |
| review, err := latestArtifact(ctx, tx, j.ID, RoleReview) |
| if err != nil { |
| return rep, err |
| @@ -353,7 +401,7 @@ func evaluateSoftwareFix(ctx context.Context, tx *sql.Tx, j jobRow) (EvidenceRep |
| if rv.SubjectHash != cand.Hash || review.ID < cand.ID { |
| return miss("review is for another candidate; the candidate changed") |
| } |
| - if !independent(cand.Provenance, review.Provenance, j.Policy.RequireDistinctReviewProvider) { |
| + if !independentForPolicy(cand.Provenance, review.Provenance, j.Policy) { |
| return miss("review is not independent of the candidate") |
| } |
| if rv.Verdict != VerdictApprove { |
| @@ -377,10 +425,27 @@ func evaluateSoftwareFix(ctx context.Context, tx *sql.Tx, j jobRow) (EvidenceRep |
| if vr.SubjectHash != cand.Hash || cr.ID < cand.ID { |
| return miss("candidate result is for another candidate; the candidate changed") |
| } |
| - if vr.Outcome != CheckPass || vr.TestsRun == 0 || vr.TestsFailed != 0 { |
| + if vr.Outcome != CheckPass || vr.TestsRun <= 0 || vr.TestsFailed != 0 { |
| return miss("candidate result is " + string(vr.Outcome) + " with " + itoa(vr.TestsRun) + " run and " + itoa(vr.TestsFailed) + " failed; expected a pass") |
| } |
| rep.CandidateResult = cr.Hash |
| + if j.Policy.Version == 2 { |
| + checks, err := latestArtifact(ctx, tx, j.ID, RoleRepositoryChecks) |
| + if err != nil { |
| + return rep, err |
| + } |
| + if checks == nil || checks.ID < cand.ID { |
| + return miss("repository_checks_missing") |
| + } |
| + if err := validateRepositoryChecks(checks.Payload, cand, j); err != nil { |
| + return miss("repository_checks_failed") |
| + } |
| + rep.RepositoryChecks = checks.Hash |
| + } |
| + if ready { |
| + rep.Satisfied = true |
| + return rep, nil |
| + } |
| rows, err := tx.QueryContext(ctx, `SELECT `+actionColumns+` FROM actions WHERE job_id=? AND operation=? AND state='succeeded' AND step_id IN (SELECT id FROM steps WHERE job_id=? AND capability=?) ORDER BY id DESC`, int64(j.ID), string(OpForgePublish), int64(j.ID), string(CapPublish)) |
| if err != nil { |
| return rep, fail(CodeInvalid, "publications: %v", err) |
| @@ -395,7 +460,7 @@ func evaluateSoftwareFix(ctx context.Context, tx *sql.Tx, j jobRow) (EvidenceRep |
| if err := strictJSON(a.Payload, &p); err != nil { |
| continue |
| } |
| - if p.Head == cand.Hash && p.Repository == j.Target.Repository && p.Ref == j.Target.Ref && a.Receipt != nil { |
| + if publicationMatches(p, cand, j) && a.Receipt != nil && (j.Policy.Version == 1 || isolationContains(p.Isolation, rep.Isolation)) { |
| rep.Publication, rep.PublishReceipt = a.ID, a.Receipt.ID |
| break |
| } |
| @@ -464,7 +529,7 @@ func evaluateReconciliation(ctx context.Context, tx *sql.Tx, j jobRow) (Evidence |
| if rv.SubjectHash != rec.Hash || review.ID < rec.ID { |
| return miss("review is for another reconciliation") |
| } |
| - if !independent(rec.Provenance, review.Provenance, j.Policy.RequireDistinctReviewProvider) { |
| + if !independentForPolicy(rec.Provenance, review.Provenance, j.Policy) { |
| return miss("review is not independent of the reconciliation") |
| } |
| if rv.Verdict != VerdictApprove { |
diff --git a/internal/equipo/team/git_evidence.go b/internal/equipo/team/git_evidence.go| new file mode 100644 |
| index 00000000..b30ac0c3 |
| --- /dev/null |
| +++ b/internal/equipo/team/git_evidence.go |
| @@ -0,0 +1,272 @@ |
| +package team |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "database/sql" |
| + "encoding/json" |
| + "slices" |
| +) |
| + |
| +const GitCandidateMediaType = "application/vnd.equipo.git-candidate+json" |
| + |
| +type GitCandidate struct { |
| + Repository string `json:"repository"` |
| + BaseCommit string `json:"base_commit"` |
| + Commit string `json:"commit"` |
| + Tree string `json:"tree"` |
| +} |
| + |
| +type RepositoryCheckResult struct { |
| + CandidateHash string `json:"candidate_hash"` |
| + Commit string `json:"commit"` |
| + CheckDefinitionHash string `json:"check_definition_hash"` |
| + BaselineTestSetHash string `json:"baseline_test_set_hash"` |
| + ObservedTestSetHash string `json:"observed_test_set_hash"` |
| + Outcome CheckOutcome `json:"outcome"` |
| + ExitCode int `json:"exit_code"` |
| + TestsRun int `json:"tests_run"` |
| + TestsFailed int `json:"tests_failed"` |
| + LogHash string `json:"log_hash"` |
| +} |
| + |
| +type IsolationTuple struct { |
| + Provider string `json:"provider"` |
| + ProviderVersion string `json:"provider_version"` |
| + PaseoVersion string `json:"paseo_version"` |
| + IsolationConfigHash string `json:"isolation_config_hash"` |
| +} |
| + |
| +type IsolationSnapshot struct { |
| + Status string `json:"status"` |
| + RunnerTainted bool `json:"runner_tainted"` |
| + AcknowledgementIDs []string `json:"acknowledgement_ids"` |
| + ObservedTuples []IsolationTuple `json:"observed_tuples"` |
| +} |
| + |
| +// ProvenanceResolver reads the authorized assignment and observed session using |
| +// the supplied transaction. It must not trust tool payloads or call Store methods. |
| +// A missing resolver denies v2 artifact registration and publication. |
| +type ProvenanceResolver func(context.Context, *sql.Tx, Scope, Handle) (Provenance, error) |
| + |
| +func lowerHex(s string, size int) bool { |
| + if len(s) != size { |
| + return false |
| + } |
| + for _, c := range s { |
| + if !(c >= '0' && c <= '9' || c >= 'a' && c <= 'f') { |
| + return false |
| + } |
| + } |
| + return true |
| +} |
| +func gitObject(s string) bool { return lowerHex(s, 40) || lowerHex(s, 64) } |
| +func validGitCandidate(c GitCandidate, target TargetBinding) bool { |
| + return c.Repository == target.Repository && c.BaseCommit == target.BaseRevision && gitObject(c.BaseCommit) && gitObject(c.Commit) && gitObject(c.Tree) && len(c.BaseCommit) == len(c.Commit) && len(c.Tree) == len(c.Commit) |
| +} |
| +func gitCandidate(a *Artifact, j jobRow) (GitCandidate, error) { |
| + var c GitCandidate |
| + if a == nil || a.MediaType != GitCandidateMediaType || !gitCandidateFields(a.Payload) || strictJSON(a.Payload, &c) != nil || !validGitCandidate(c, j.Target) { |
| + return c, fail(CodeEvidence, "git_candidate_invalid") |
| + } |
| + payload, hash, err := canonical(a.Payload) |
| + if err != nil || hash != a.Hash || string(payload) != string(a.Payload) { |
| + return c, fail(CodeEvidence, "git_candidate_identity_invalid") |
| + } |
| + return c, nil |
| +} |
| +func (p IsolationSnapshot) valid() bool { |
| + if p.Status != "confined" && p.Status != "unconfined" { |
| + return false |
| + } |
| + if len(p.ObservedTuples) == 0 { |
| + return false |
| + } |
| + if (p.Status == "unconfined" || p.RunnerTainted) && len(p.AcknowledgementIDs) == 0 { |
| + return false |
| + } |
| + if p.Status == "unconfined" && !p.RunnerTainted { |
| + return false |
| + } |
| + for _, id := range p.AcknowledgementIDs { |
| + if id == "" { |
| + return false |
| + } |
| + } |
| + for _, v := range p.ObservedTuples { |
| + if v.Provider == "" || v.ProviderVersion == "" || v.PaseoVersion == "" || !lowerHex(v.IsolationConfigHash, 64) { |
| + return false |
| + } |
| + } |
| + return true |
| +} |
| +func mergeIsolation(a, b IsolationSnapshot) IsolationSnapshot { |
| + // Status belongs to this connection; historical runner exposure is separate. |
| + a.RunnerTainted = a.RunnerTainted || b.RunnerTainted || b.Status == "unconfined" |
| + a.AcknowledgementIDs = slices.Clone(a.AcknowledgementIDs) |
| + a.ObservedTuples = slices.Clone(a.ObservedTuples) |
| + for _, id := range b.AcknowledgementIDs { |
| + if !slices.Contains(a.AcknowledgementIDs, id) { |
| + a.AcknowledgementIDs = append(a.AcknowledgementIDs, id) |
| + } |
| + } |
| + for _, v := range b.ObservedTuples { |
| + if !slices.Contains(a.ObservedTuples, v) { |
| + a.ObservedTuples = append(a.ObservedTuples, v) |
| + } |
| + } |
| + slices.Sort(a.AcknowledgementIDs) |
| + slices.SortFunc(a.ObservedTuples, func(a, b IsolationTuple) int { |
| + x, _ := json.Marshal(a) |
| + y, _ := json.Marshal(b) |
| + return slices.Compare(x, y) |
| + }) |
| + return a |
| +} |
| +func lineageIsolation(ctx context.Context, tx *sql.Tx, job JobID, current IsolationSnapshot) (IsolationSnapshot, error) { |
| + rows, err := tx.QueryContext(ctx, `SELECT provenance FROM artifacts WHERE job_id=? ORDER BY id`, int64(job)) |
| + if err != nil { |
| + return current, err |
| + } |
| + defer rows.Close() |
| + for rows.Next() { |
| + var raw string |
| + var p Provenance |
| + if err := rows.Scan(&raw); err != nil { |
| + return current, err |
| + } |
| + if strictJSON([]byte(raw), &p) != nil || p.validate() != nil || !p.Isolation.valid() { |
| + return current, fail(CodeEvidence, "isolation_provenance_missing") |
| + } |
| + current = mergeIsolation(current, p.Isolation) |
| + } |
| + return current, rows.Err() |
| +} |
| +func (s *Store) observedProvenance(ctx context.Context, tx *sql.Tx, scope Scope, h Handle) (Provenance, error) { |
| + if s.provenance == nil { |
| + return Provenance{}, fail(CodeEvidence, "observed_provenance_missing") |
| + } |
| + p, err := s.provenance(ctx, tx, scope, h) |
| + if err != nil { |
| + return p, err |
| + } |
| + if p.validate() != nil || !p.Isolation.valid() { |
| + return p, fail(CodeEvidence, "observed_provenance_invalid") |
| + } |
| + p.Isolation, err = lineageIsolation(ctx, tx, scope.Job, p.Isolation) |
| + return p, err |
| +} |
| +func independentForPolicy(a, b Provenance, p Policy) bool { |
| + if p.Version == 1 { |
| + return independent(a, b, p.RequireDistinctReviewProvider) |
| + } |
| + // The trusted resolver supplies canonical known families, or an empty/unknown |
| + // family when observation cannot identify one. Provider profiles are not families. |
| + known := func(s string) bool { return s != "" && s != "unknown" && s != "unspecified" } |
| + return independent(a, b, false) && known(a.ModelFamily) && known(b.ModelFamily) && a.ModelFamily != b.ModelFamily |
| +} |
| +func validateRepositoryChecks(payload []byte, cand *Artifact, j jobRow) error { |
| + c, err := gitCandidate(cand, j) |
| + if err != nil { |
| + return err |
| + } |
| + var r RepositoryCheckResult |
| + if strictJSON(payload, &r) != nil { |
| + return fail(CodeEvidence, "repository_checks_invalid") |
| + } |
| + if r.CandidateHash != cand.Hash || r.Commit != c.Commit || r.CheckDefinitionHash != j.Evidence.CheckDefinitionHash || !lowerHex(r.CheckDefinitionHash, 64) || !lowerHex(r.LogHash, 64) || !lowerHex(r.BaselineTestSetHash, 64) || r.BaselineTestSetHash != j.Evidence.BaselineTestSetHash || !lowerHex(r.ObservedTestSetHash, 64) || r.Outcome != CheckPass || r.ExitCode != 0 || r.TestsRun <= 0 || r.TestsFailed != 0 { |
| + return fail(CodeEvidence, "repository_checks_failed") |
| + } |
| + return nil |
| +} |
| + |
| +// Evidence evaluates stored evidence without changing job state. |
| +func (s *Store) Evidence(ctx context.Context, scope Scope) (EvidenceReport, error) { |
| + return s.evidence(ctx, scope, false) |
| +} |
| + |
| +// ReadyToPublish excludes only the publication receipt from the predicate. |
| +func (s *Store) ReadyToPublish(ctx context.Context, scope Scope) (EvidenceReport, error) { |
| + return s.evidence(ctx, scope, true) |
| +} |
| +func (s *Store) evidence(ctx context.Context, scope Scope, ready bool) (EvidenceReport, error) { |
| + var rep EvidenceReport |
| + err := s.read(ctx, func(tx *sql.Tx) error { |
| + j, err := loadJob(ctx, tx, scope) |
| + if err != nil { |
| + return err |
| + } |
| + rep, err = evaluateEvidence(ctx, tx, j, ready) |
| + return err |
| + }) |
| + return rep, err |
| +} |
| +func evaluateEvidence(ctx context.Context, tx *sql.Tx, j jobRow, ready bool) (EvidenceReport, error) { |
| + switch j.Evidence.Predicate { |
| + case PredicateSoftwareFix: |
| + return evaluateSoftwareFix(ctx, tx, j, ready) |
| + case PredicateAttestedReconciliation: |
| + return evaluateReconciliation(ctx, tx, j) |
| + default: |
| + return EvidenceReport{}, fail(CodeUnknownKind, "predicate %q", j.Evidence.Predicate) |
| + } |
| +} |
| +func publicationMatches(p publication, cand *Artifact, j jobRow) bool { |
| + if cand == nil || p.Repository != j.Target.Repository || p.Ref != j.Target.Ref || p.Ref == j.Target.DefaultRef { |
| + return false |
| + } |
| + if j.Policy.Version == 1 { |
| + return p.Head == cand.Hash |
| + } |
| + c, err := gitCandidate(cand, j) |
| + return err == nil && p.Head == c.Commit && p.CandidateHash == cand.Hash && p.Isolation.valid() |
| +} |
| + |
| +func isolationContains(have, need IsolationSnapshot) bool { |
| + if !have.valid() || !need.valid() || need.RunnerTainted && !have.RunnerTainted { |
| + return false |
| + } |
| + for _, id := range need.AcknowledgementIDs { |
| + if !slices.Contains(have.AcknowledgementIDs, id) { |
| + return false |
| + } |
| + } |
| + for _, v := range need.ObservedTuples { |
| + if !slices.Contains(have.ObservedTuples, v) { |
| + return false |
| + } |
| + } |
| + return true |
| +} |
| + |
| +// Reject aliases and duplicate keys before canonicalization can erase them. |
| +func gitCandidateFields(raw []byte) bool { |
| + dec := json.NewDecoder(bytes.NewReader(raw)) |
| + token, err := dec.Token() |
| + if err != nil || token != json.Delim('{') { |
| + return false |
| + } |
| + seen := map[string]bool{} |
| + for dec.More() { |
| + token, err := dec.Token() |
| + if err != nil { |
| + return false |
| + } |
| + key, ok := token.(string) |
| + if !ok || seen[key] { |
| + return false |
| + } |
| + switch key { |
| + case "repository", "base_commit", "commit", "tree": |
| + default: |
| + return false |
| + } |
| + seen[key] = true |
| + var value string |
| + if dec.Decode(&value) != nil { |
| + return false |
| + } |
| + } |
| + token, err = dec.Token() |
| + return err == nil && token == json.Delim('}') && len(seen) == 4 && jsonEnd(dec) == nil |
| +} |
diff --git a/internal/equipo/team/git_evidence_test.go b/internal/equipo/team/git_evidence_test.go| new file mode 100644 |
| index 00000000..6eb5f38a |
| --- /dev/null |
| +++ b/internal/equipo/team/git_evidence_test.go |
| @@ -0,0 +1,517 @@ |
| +package team |
| + |
| +import ( |
| + "context" |
| + "database/sql" |
| + "encoding/json" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +type gitEnv struct { |
| + *env |
| + scope Scope |
| + candidate GitCandidate |
| + artifact Artifact |
| + observed Provenance |
| +} |
| + |
| +func newGitEnv(t *testing.T) *gitEnv { return newGitEnvObject(t, 40) } |
| + |
| +func newGitEnvObject(t *testing.T, objectLength int) *gitEnv { |
| + e := newEnv(t) |
| + g := &gitEnv{env: e} |
| + g.observed = Provenance{Producer: "builder", Provider: "provider-a", Context: "session-a", ModelFamily: "gpt", Isolation: IsolationSnapshot{Status: "unconfined", RunnerTainted: true, AcknowledgementIDs: []string{"ack-1"}, ObservedTuples: []IsolationTuple{{Provider: "provider-a", ProviderVersion: "1", PaseoVersion: "0.14.0", IsolationConfigHash: hashBytes([]byte("config"))}}}} |
| + must(t, e.store.Close()) |
| + opts := e.options() |
| + opts.Provenance = func(context.Context, *sql.Tx, Scope, Handle) (Provenance, error) { return g.observed, nil } |
| + var err error |
| + e.store, err = Open(e.ctx, e.path, opts) |
| + must(t, err) |
| + _, err = e.store.Resume(e.ctx) |
| + must(t, err) |
| + steps := SoftwareSteps(false) |
| + for i := range steps { |
| + steps[i].DependsOn = nil |
| + } |
| + steps = append(steps, StepSpec{Key: "rework", Capability: CapImplement, Version: 1}) |
| + spec := softwareSpec(steps) |
| + spec.Policy = runnerPolicy(t, "subscription") |
| + spec.Target.BaseRevision = strings.Repeat("a", objectLength) |
| + spec.Evidence.BaselineTestSetHash = hashBytes([]byte("tests")) |
| + _, g.scope = e.createJob(spec) |
| + g.candidate = GitCandidate{Repository: spec.Target.Repository, BaseCommit: spec.Target.BaseRevision, Commit: strings.Repeat("b", objectLength), Tree: strings.Repeat("c", objectLength)} |
| + return g |
| +} |
| +func (g *gitEnv) claimStep(step string) Handle { return g.claim(g.scope, step, "runner", "ws-"+step) } |
| +func (g *gitEnv) finish(h Handle) { |
| + must(g.t, g.store.Complete(g.ctx, g.scope, h, AttemptResult{Outcome: OutcomeSuccess})) |
| + must(g.t, g.store.Contain(g.ctx, g.scope, h.Attempt, "supervisor")) |
| +} |
| +func (g *gitEnv) register(h Handle, role Role, payload []byte) (Artifact, error) { |
| + media := "application/json" |
| + if role == RoleCandidate { |
| + media = GitCandidateMediaType |
| + } |
| + return g.store.Register(g.ctx, g.scope, h, ArtifactSpec{Role: role, MediaType: media, Payload: payload, Provenance: Provenance{Producer: "spoof", Provider: "spoof", Context: "spoof", ModelFamily: "spoof"}}) |
| +} |
| +func (g *gitEnv) build() { |
| + h := g.claimStep("reproduce") |
| + repro, err := g.register(h, RoleReproduction, FixtureReproduction) |
| + must(g.t, err) |
| + _, err = g.register(h, RoleBaseResult, mustJSON(CheckResult{ReproductionHash: repro.Hash, SubjectHash: g.candidate.BaseCommit, CheckDefinitionHash: CheckDefinitionHash, Outcome: CheckFail, TestsRun: 3, TestsFailed: 1, FailureMessage: FixtureSymptom})) |
| + must(g.t, err) |
| + g.finish(h) |
| + h = g.claimStep("implement") |
| + g.artifact, err = g.register(h, RoleCandidate, mustJSON(g.candidate)) |
| + must(g.t, err) |
| + g.finish(h) |
| +} |
| +func (g *gitEnv) review() { |
| + g.observed.Producer = "reviewer" |
| + g.observed.Context = "session-b" |
| + g.observed.Provider = "provider-b" |
| + g.observed.ModelFamily = "claude" |
| + h := g.claimStep("review") |
| + _, err := g.register(h, RoleReview, mustJSON(Review{SubjectHash: g.artifact.Hash, Verdict: VerdictApprove})) |
| + must(g.t, err) |
| + g.finish(h) |
| +} |
| +func (g *gitEnv) checks() RepositoryCheckResult { |
| + return RepositoryCheckResult{CandidateHash: g.artifact.Hash, Commit: g.candidate.Commit, CheckDefinitionHash: CheckDefinitionHash, BaselineTestSetHash: hashBytes([]byte("tests")), ObservedTestSetHash: hashBytes([]byte("tests")), Outcome: CheckPass, TestsRun: 3, LogHash: hashBytes([]byte("log"))} |
| +} |
| +func (g *gitEnv) verify(withChecks bool) { |
| + h := g.claimStep("verify") |
| + _, err := g.register(h, RoleCandidateResult, mustJSON(CheckResult{ReproductionHash: hashBytes(FixtureReproduction), SubjectHash: g.artifact.Hash, CheckDefinitionHash: CheckDefinitionHash, Outcome: CheckPass, TestsRun: 3})) |
| + must(g.t, err) |
| + if withChecks { |
| + _, err = g.register(h, RoleRepositoryChecks, mustJSON(g.checks())) |
| + must(g.t, err) |
| + } |
| + g.finish(h) |
| +} |
| +func (g *gitEnv) publication() publication { |
| + return publication{Repository: g.candidate.Repository, Ref: "team/fix-sum", Head: g.candidate.Commit, CandidateHash: g.artifact.Hash} |
| +} |
| +func TestGitCandidateIdentity(t *testing.T) { |
| + g := newGitEnv(t) |
| + g.build() |
| + payload, hash, err := canonical(mustJSON(g.candidate)) |
| + must(t, err) |
| + if g.artifact.Hash != hash || string(g.artifact.Payload) != string(payload) || g.artifact.Hash == g.candidate.Commit { |
| + t.Fatal("candidate digest is not canonical artifact identity") |
| + } |
| + if g.artifact.Provenance.Producer != "builder" || g.artifact.Provenance.ModelFamily != "gpt" { |
| + t.Fatal("submitted provenance replaced observed session") |
| + } |
| + h := g.claimStep("rework") |
| + for _, change := range []func(*GitCandidate){func(c *GitCandidate) { c.Commit = strings.Repeat("B", 40) }, func(c *GitCandidate) { c.Commit = strings.Repeat("b", 41) }, func(c *GitCandidate) { c.Tree = "sha1:" + c.Tree }, func(c *GitCandidate) { c.BaseCommit = strings.Repeat("d", 40) }, func(c *GitCandidate) { c.Repository = "other" }, func(c *GitCandidate) { c.Tree = strings.Repeat("e", 64) }} { |
| + c := g.candidate |
| + change(&c) |
| + _, err := g.register(h, RoleCandidate, mustJSON(c)) |
| + wantCode(t, err, CodeEvidence) |
| + } |
| + _, err = g.store.Register(g.ctx, g.scope, h, ArtifactSpec{Role: RoleCandidate, MediaType: "text/plain", Payload: mustJSON(g.candidate)}) |
| + wantCode(t, err, CodeEvidence) |
| + // Canonical JSON, rather than submitted whitespace or field order, defines identity. |
| + raw := []byte(`{ "tree":"` + g.candidate.Tree + `", "commit":"` + g.candidate.Commit + `", "base_commit":"` + g.candidate.BaseCommit + `", "repository":"` + g.candidate.Repository + `" }`) |
| + a, err := g.register(h, RoleCandidate, raw) |
| + must(t, err) |
| + if a.Hash != g.artifact.Hash { |
| + t.Fatal("noncanonical candidate digest") |
| + } |
| + g.finish(h) |
| + // All Git object IDs may also use SHA-256. |
| + // Test the parser independently because a job's base is immutable. |
| + c := GitCandidate{Repository: "repo", BaseCommit: strings.Repeat("a", 64), Commit: strings.Repeat("b", 64), Tree: strings.Repeat("c", 64)} |
| + if !validGitCandidate(c, TargetBinding{Repository: "repo", BaseRevision: c.BaseCommit}) { |
| + t.Fatal("SHA-256 Git objects refused") |
| + } |
| +} |
| +func TestBugNeedsRepositoryChecks(t *testing.T) { |
| + t.Run("missing", func(t *testing.T) { |
| + g := newGitEnv(t) |
| + g.build() |
| + g.review() |
| + g.verify(false) |
| + rep, err := g.store.ReadyToPublish(g.ctx, g.scope) |
| + must(t, err) |
| + if rep.Satisfied || !strings.Contains(rep.Reason, "repository") { |
| + t.Fatal("missing repository checks passed", rep) |
| + } |
| + if g.job(g.scope).State != JobRunning { |
| + t.Fatal("readiness terminated job") |
| + } |
| + g.finish(g.claimStep("rework")) |
| + g.finish(g.claimStep("publish")) |
| + _, err = g.store.Conclude(g.ctx, g.scope, "test") |
| + wantCode(t, err, CodeEvidence) |
| + if g.job(g.scope).State != JobFailed { |
| + t.Fatal("missing checks concluded successfully") |
| + } |
| + }) |
| + for _, name := range []string{"compile", "skip", "no_tests", "zero", "exit", "failed", "definition", "malformed", "malformed observed", "baseline", "commit", "candidate"} { |
| + t.Run(name, func(t *testing.T) { |
| + g := newGitEnv(t) |
| + g.build() |
| + h := g.claimStep("verify") |
| + c := g.checks() |
| + switch name { |
| + case "compile": |
| + c.Outcome = CheckCompileError |
| + case "skip": |
| + c.Outcome = CheckSkipped |
| + case "no_tests": |
| + c.Outcome = CheckNoTests |
| + case "zero": |
| + c.TestsRun = 0 |
| + case "exit": |
| + c.ExitCode = 1 |
| + case "failed": |
| + c.TestsFailed = 1 |
| + case "definition": |
| + c.CheckDefinitionHash = hashBytes([]byte("wrong")) |
| + case "malformed": |
| + c.LogHash = "ABC" |
| + case "malformed observed": |
| + c.ObservedTestSetHash = "not-a-digest" |
| + case "baseline": |
| + c.BaselineTestSetHash = hashBytes([]byte("wrong")) |
| + case "commit": |
| + c.Commit = strings.Repeat("d", 40) |
| + case "candidate": |
| + c.CandidateHash = hashBytes([]byte("wrong")) |
| + } |
| + _, err := g.register(h, RoleRepositoryChecks, mustJSON(c)) |
| + wantCode(t, err, CodeEvidence) |
| + }) |
| + } |
| + t.Run("success and publication", func(t *testing.T) { |
| + g := newGitEnv(t) |
| + g.build() |
| + g.review() |
| + g.verify(true) |
| + rep, err := g.store.ReadyToPublish(g.ctx, g.scope) |
| + must(t, err) |
| + if !rep.Satisfied || rep.RepositoryChecks == "" || !rep.Isolation.RunnerTainted { |
| + t.Fatal("valid readiness rejected", rep) |
| + } |
| + rep, err = g.store.Evidence(g.ctx, g.scope) |
| + must(t, err) |
| + if rep.Satisfied { |
| + t.Fatal("receipt omitted from evidence") |
| + } |
| + _, err = g.store.Conclude(g.ctx, g.scope, "test") |
| + wantCode(t, err, CodeState) |
| + // Finish the unused rework step without creating another candidate. |
| + g.finish(g.claimStep("rework")) |
| + h := g.claimStep("publish") |
| + for _, change := range []func(*publication){func(p *publication) { p.Repository = "other" }, func(p *publication) { p.Ref = "main" }, func(p *publication) { p.Head = p.CandidateHash }, func(p *publication) { p.CandidateHash = p.Head }} { |
| + p := g.publication() |
| + change(&p) |
| + _, err = g.store.Intend(g.ctx, g.scope, h, IntentSpec{Key: "bad", Operation: OpForgePublish, Payload: mustJSON(p)}) |
| + if err == nil { |
| + t.Fatal("invalid publication accepted") |
| + } |
| + } |
| + id, err := g.store.Intend(g.ctx, g.scope, h, IntentSpec{Key: "publish", Operation: OpForgePublish, Payload: mustJSON(g.publication())}) |
| + must(t, err) |
| + action, err := g.store.GetAction(g.ctx, g.scope, id) |
| + must(t, err) |
| + var p publication |
| + must(t, json.Unmarshal(action.Payload, &p)) |
| + if !p.Isolation.RunnerTainted { |
| + t.Fatal("publication stripped isolation") |
| + } |
| + _, err = g.store.Dispatch(g.ctx, g.scope, id) |
| + must(t, err) |
| + must(t, g.store.Complete(g.ctx, g.scope, h, AttemptResult{Outcome: OutcomeSuccess})) |
| + _, err = g.store.Conclude(g.ctx, g.scope, "test") |
| + wantCode(t, err, CodeState) |
| + must(t, g.store.Contain(g.ctx, g.scope, h.Attempt, "supervisor")) |
| + rep, err = g.store.Conclude(g.ctx, g.scope, "test") |
| + must(t, err) |
| + if !rep.Satisfied || rep.Publication != id { |
| + t.Fatal("publication identity mismatch", rep) |
| + } |
| + }) |
| +} |
| +func TestReviewDifferentFamily(t *testing.T) { |
| + for _, name := range []string{"same across providers", "unknown", "empty", "same producer", "same session", "different"} { |
| + t.Run(name, func(t *testing.T) { |
| + g := newGitEnv(t) |
| + g.build() |
| + h := g.claimStep("review") |
| + g.observed.Provider = "other-provider" |
| + g.observed.Producer = "reviewer" |
| + g.observed.Context = "review-session" |
| + g.observed.ModelFamily = "claude" |
| + switch name { |
| + case "same across providers": |
| + g.observed.Model = "another-profile" |
| + g.observed.ModelFamily = "gpt" |
| + case "unknown": |
| + g.observed.ModelFamily = "unknown" |
| + case "empty": |
| + g.observed.ModelFamily = "" |
| + case "same producer": |
| + g.observed.Producer = "builder" |
| + case "same session": |
| + g.observed.Context = "session-a" |
| + } |
| + _, err := g.register(h, RoleReview, mustJSON(Review{SubjectHash: g.artifact.Hash, Verdict: VerdictApprove})) |
| + if name == "different" { |
| + must(t, err) |
| + } else { |
| + wantCode(t, err, CodeEvidence) |
| + } |
| + }) |
| + } |
| +} |
| +func TestChangedCandidateInvalidatesEvidence(t *testing.T) { |
| + g := newGitEnv(t) |
| + g.build() |
| + g.review() |
| + g.verify(true) |
| + hpub := g.claimStep("publish") |
| + id, err := g.store.Intend(g.ctx, g.scope, hpub, IntentSpec{Key: "publish", Operation: OpForgePublish, Payload: mustJSON(g.publication())}) |
| + must(t, err) |
| + h := g.claimStep("rework") |
| + g.candidate.Commit = strings.Repeat("d", 40) |
| + g.observed.Isolation.Status = "confined" |
| + g.observed.Isolation.RunnerTainted = false |
| + g.observed.Isolation.AcknowledgementIDs = nil |
| + a, err := g.register(h, RoleCandidate, mustJSON(g.candidate)) |
| + must(t, err) |
| + if !a.Provenance.Isolation.RunnerTainted || len(a.Provenance.Isolation.AcknowledgementIDs) != 1 { |
| + t.Fatal("candidate lineage erased isolation") |
| + } |
| + // A new candidate invalidates evidence even while its attempt is unfinished. |
| + rep, err := g.store.ReadyToPublish(g.ctx, g.scope) |
| + must(t, err) |
| + if rep.Satisfied { |
| + t.Fatal("new candidate retained old evidence") |
| + } |
| + g.finish(h) |
| + _, err = g.store.Admit(g.ctx, g.scope, id) |
| + wantCode(t, err, CodeEvidence) |
| + rep, err = g.store.ReadyToPublish(g.ctx, g.scope) |
| + must(t, err) |
| + if rep.Satisfied { |
| + t.Fatal("changed candidate retained review or checks") |
| + } |
| +} |
| + |
| +func TestEvidenceObservedIsolation(t *testing.T) { |
| + for _, name := range []string{"resolver", "status", "ack", "tuple", "lookup"} { |
| + t.Run(name, func(t *testing.T) { |
| + g := newGitEnv(t) |
| + h := g.claimStep("reproduce") |
| + switch name { |
| + case "resolver": |
| + g.store.provenance = nil |
| + case "status": |
| + g.observed.Isolation.Status = "" |
| + case "ack": |
| + g.observed.Isolation.AcknowledgementIDs = nil |
| + case "tuple": |
| + g.observed.Isolation.ObservedTuples = nil |
| + case "lookup": |
| + g.store.provenance = func(context.Context, *sql.Tx, Scope, Handle) (Provenance, error) { |
| + return Provenance{}, fail(CodeEvidence, "lookup failed") |
| + } |
| + } |
| + _, err := g.register(h, RoleReproduction, FixtureReproduction) |
| + wantCode(t, err, CodeEvidence) |
| + }) |
| + } |
| + g := newGitEnv(t) |
| + g.build() |
| + g.review() |
| + g.verify(true) |
| + arts, err := g.store.Artifacts(g.ctx, g.scope) |
| + must(t, err) |
| + for _, a := range arts { |
| + if !a.Provenance.Isolation.RunnerTainted { |
| + t.Fatal("evidence lost isolation", a.Role) |
| + } |
| + } |
| + before, err := g.store.Events(g.ctx, g.scope, 0) |
| + must(t, err) |
| + _, err = g.store.Evidence(g.ctx, g.scope) |
| + must(t, err) |
| + _, err = g.store.ReadyToPublish(g.ctx, g.scope) |
| + must(t, err) |
| + after, err := g.store.Events(g.ctx, g.scope, 0) |
| + must(t, err) |
| + if len(before) != len(after) || g.job(g.scope).State != JobRunning { |
| + t.Fatal("evidence read wrote state") |
| + } |
| + // Persisted historical exposure survives reopening without a live resolver. |
| + must(t, g.store.Close()) |
| + g.open() |
| + rep, err := g.store.ReadyToPublish(g.ctx, g.scope) |
| + must(t, err) |
| + if !rep.Satisfied || !rep.Isolation.RunnerTainted { |
| + t.Fatal("reopen erased evidence provenance", rep) |
| + } |
| + // A stripped historical snapshot fails both reads and subsequent registration. |
| + must(t, g.store.tx(g.ctx, func(tx *sql.Tx) error { |
| + p := g.artifact.Provenance |
| + p.Isolation = IsolationSnapshot{} |
| + _, err := tx.ExecContext(g.ctx, `UPDATE artifacts SET provenance=? WHERE id=?`, string(mustJSON(p)), int64(g.artifact.ID)) |
| + return err |
| + })) |
| + _, err = g.store.ReadyToPublish(g.ctx, g.scope) |
| + wantCode(t, err, CodeEvidence) |
| +} |
| +func TestEvidencePublicationAdmission(t *testing.T) { |
| + for _, name := range []string{"repository", "ref", "commit", "hash", "isolation", "new exposure"} { |
| + t.Run(name, func(t *testing.T) { |
| + g := newGitEnv(t) |
| + g.build() |
| + g.review() |
| + g.verify(true) |
| + h := g.claimStep("publish") |
| + id, err := g.store.Intend(g.ctx, g.scope, h, IntentSpec{Key: "publish", Operation: OpForgePublish, Payload: mustJSON(g.publication())}) |
| + must(t, err) |
| + a, err := g.store.GetAction(g.ctx, g.scope, id) |
| + must(t, err) |
| + var p publication |
| + must(t, strictJSON(a.Payload, &p)) |
| + switch name { |
| + case "repository": |
| + p.Repository = "other" |
| + case "ref": |
| + p.Ref = "main" |
| + case "commit": |
| + p.Head = strings.Repeat("d", 40) |
| + case "hash": |
| + p.CandidateHash = hashBytes([]byte("other")) |
| + case "isolation": |
| + p.Isolation = IsolationSnapshot{} |
| + case "new exposure": |
| + g.observed.Isolation.AcknowledgementIDs = append(g.observed.Isolation.AcknowledgementIDs, "ack-2") |
| + } |
| + must(t, g.store.tx(g.ctx, func(tx *sql.Tx) error { |
| + _, err := tx.ExecContext(g.ctx, `UPDATE actions SET payload=? WHERE id=?`, mustJSON(p), int64(id)) |
| + return err |
| + })) |
| + _, err = g.store.Admit(g.ctx, g.scope, id) |
| + if err == nil { |
| + t.Fatal("publication admission accepted changed " + name) |
| + } |
| + }) |
| + } |
| +} |
| +func TestEvidenceRepositoryChecksRevalidated(t *testing.T) { |
| + for _, role := range []Role{RoleReview, RoleRepositoryChecks, RoleCandidateResult} { |
| + t.Run(string(role), func(t *testing.T) { |
| + g := newGitEnv(t) |
| + g.build() |
| + g.review() |
| + g.verify(true) |
| + // Keep other evidence valid to exercise each independent evaluation guard. |
| + must(t, g.store.tx(g.ctx, func(tx *sql.Tx) error { |
| + var payload []byte |
| + switch role { |
| + case RoleReview: |
| + payload = mustJSON(Review{SubjectHash: hashBytes([]byte("old")), Verdict: VerdictApprove}) |
| + case RoleRepositoryChecks: |
| + r := g.checks() |
| + r.CandidateHash = hashBytes([]byte("old")) |
| + payload = mustJSON(r) |
| + case RoleCandidateResult: |
| + payload = mustJSON(CheckResult{SubjectHash: hashBytes([]byte("old")), ReproductionHash: hashBytes(FixtureReproduction), CheckDefinitionHash: CheckDefinitionHash, Outcome: CheckPass, TestsRun: 3}) |
| + } |
| + _, err := tx.ExecContext(g.ctx, `UPDATE artifacts SET payload=? WHERE job_id=? AND role=?`, payload, int64(g.scope.Job), string(role)) |
| + return err |
| + })) |
| + rep, err := g.store.ReadyToPublish(g.ctx, g.scope) |
| + must(t, err) |
| + if rep.Satisfied { |
| + t.Fatal("stale evidence passed", role) |
| + } |
| + }) |
| + } |
| + g := newGitEnv(t) |
| + g.build() |
| + h := g.claimStep("review") |
| + _, err := g.register(h, RoleRepositoryChecks, mustJSON(g.checks())) |
| + wantCode(t, err, CodeScope) |
| +} |
| +func TestReviewDifferentFamilySameProvider(t *testing.T) { |
| + g := newGitEnv(t) |
| + g.build() |
| + h := g.claimStep("review") |
| + g.observed.Producer = "reviewer" |
| + g.observed.Context = "other-session" |
| + g.observed.ModelFamily = "claude" |
| + _, err := g.register(h, RoleReview, mustJSON(Review{SubjectHash: g.artifact.Hash, Verdict: VerdictApprove})) |
| + must(t, err) |
| +} |
| +func TestBugNeedsMeasuredBaseline(t *testing.T) { |
| + g := newGitEnv(t) |
| + g.build() |
| + h := g.claimStep("verify") |
| + r := g.checks() |
| + // The executor verifies a superset of required tests; additional passing tests |
| + // change the observed set digest without changing the frozen baseline digest. |
| + r.ObservedTestSetHash = hashBytes([]byte("tests plus new tests")) |
| + r.TestsRun = 4 |
| + _, err := g.register(h, RoleRepositoryChecks, mustJSON(r)) |
| + must(t, err) |
| +} |
| + |
| +func TestGitCandidateStrictFields(t *testing.T) { |
| + g := newGitEnv(t) |
| + h := g.claimStep("implement") |
| + for _, payload := range []string{ |
| + `{"Repository":"example/shop","base_commit":"` + g.candidate.BaseCommit + `","commit":"` + g.candidate.Commit + `","tree":"` + g.candidate.Tree + `"}`, |
| + `{"repository":"wrong","repository":"example/shop","base_commit":"` + g.candidate.BaseCommit + `","commit":"` + g.candidate.Commit + `","tree":"` + g.candidate.Tree + `"}`, |
| + } { |
| + _, err := g.register(h, RoleCandidate, []byte(payload)) |
| + wantCode(t, err, CodeEvidence) |
| + } |
| +} |
| + |
| +func TestReviewDifferentFamilyPolicy(t *testing.T) { |
| + p := runnerPolicy(t, "subscription") |
| + p.RequireDistinctReviewFamily = false |
| + wantCode(t, p.Validate(), CodeInvalid) |
| +} |
| + |
| +func TestGitCandidateSHA256(t *testing.T) { |
| + g := newGitEnvObject(t, 64) |
| + g.build() |
| + g.review() |
| + g.verify(true) |
| + h := g.claimStep("publish") |
| + _, err := g.store.Intend(g.ctx, g.scope, h, IntentSpec{Key: "publish", Operation: OpForgePublish, Payload: mustJSON(g.publication())}) |
| + must(t, err) |
| +} |
| + |
| +func TestGitCandidateObjectLengths(t *testing.T) { |
| + for _, n := range []int{0, 39, 40, 41, 63, 64, 65, 128} { |
| + c := GitCandidate{Repository: "repo", BaseCommit: strings.Repeat("a", n), Commit: strings.Repeat("b", n), Tree: strings.Repeat("c", n)} |
| + got := validGitCandidate(c, TargetBinding{Repository: "repo", BaseRevision: c.BaseCommit}) |
| + if got != (n == 40 || n == 64) { |
| + t.Fatalf("Git object length %d accepted=%v", n, got) |
| + } |
| + } |
| +} |
| + |
| +func TestEvidenceStrippedProducer(t *testing.T) { |
| + g := newGitEnv(t) |
| + g.build() |
| + g.review() |
| + g.verify(true) |
| + must(t, g.store.tx(g.ctx, func(tx *sql.Tx) error { |
| + p := g.artifact.Provenance |
| + p.Producer = "" |
| + _, err := tx.ExecContext(g.ctx, `UPDATE artifacts SET provenance=? WHERE id=?`, string(mustJSON(p)), int64(g.artifact.ID)) |
| + return err |
| + })) |
| + _, err := g.store.ReadyToPublish(g.ctx, g.scope) |
| + wantCode(t, err, CodeEvidence) |
| +} |
diff --git a/internal/equipo/team/model.go b/internal/equipo/team/model.go| index c21ef6d6..f77e8009 100644 |
| --- a/internal/equipo/team/model.go |
| +++ b/internal/equipo/team/model.go |
| @@ -273,8 +273,9 @@ func (p Predicate) Valid() bool { |
| // EvidenceRequirement is the job's configured success predicate and its |
| // parameters. ExpectedSymptom must appear in the base failure message; |
| // CheckDefinitionHash pins the trusted check definition every result must |
| -// have run under. |
| +// have run under. BaselineTestSetHash pins the executor's required test set. |
| type EvidenceRequirement struct { |
| + BaselineTestSetHash string `json:"baseline_test_set_hash,omitempty"` |
| Predicate Predicate `json:"predicate"` |
| ExpectedSymptom string `json:"expected_symptom,omitempty"` |
| CheckDefinitionHash string `json:"check_definition_hash,omitempty"` |
| @@ -660,18 +661,19 @@ type Event struct { |
| type Role string |
| const ( |
| - RoleReproduction Role = "reproduction" // frozen reproduction, registered by reproduce before any candidate |
| - RoleBaseResult Role = "base_result" // CheckResult of the reproduction on the base |
| - RoleCandidate Role = "candidate" // candidate identity and content |
| - RoleCandidateResult Role = "candidate_result" // CheckResult of the reproduction on the candidate |
| - RoleReview Role = "review" // Review of an exact candidate or reconciliation |
| - RoleSourceRecord Role = "source_record" // business input |
| - RoleReconciliation Role = "reconciliation" // business output over source records |
| + RoleReproduction Role = "reproduction" // frozen reproduction, registered by reproduce before any candidate |
| + RoleBaseResult Role = "base_result" // CheckResult of the reproduction on the base |
| + RoleCandidate Role = "candidate" // candidate identity and content |
| + RoleCandidateResult Role = "candidate_result" // CheckResult of the reproduction on the candidate |
| + RoleRepositoryChecks Role = "repository_checks" |
| + RoleReview Role = "review" // Review of an exact candidate or reconciliation |
| + RoleSourceRecord Role = "source_record" // business input |
| + RoleReconciliation Role = "reconciliation" // business output over source records |
| ) |
| func (r Role) Valid() bool { |
| switch r { |
| - case RoleReproduction, RoleBaseResult, RoleCandidate, RoleCandidateResult, RoleReview, RoleSourceRecord, RoleReconciliation: |
| + case RoleReproduction, RoleBaseResult, RoleCandidate, RoleCandidateResult, RoleRepositoryChecks, RoleReview, RoleSourceRecord, RoleReconciliation: |
| return true |
| } |
| return false |
| @@ -687,7 +689,7 @@ func roleAllowed(c Capability, r Role) bool { |
| case CapReview: |
| return r == RoleReview |
| case CapVerify: |
| - return r == RoleCandidateResult || r == RoleBaseResult |
| + return r == RoleCandidateResult || r == RoleBaseResult || r == RoleRepositoryChecks |
| case CapCollect: |
| return r == RoleSourceRecord |
| case CapReconcile: |
| @@ -698,12 +700,14 @@ func roleAllowed(c Capability, r Role) bool { |
| // Provenance records who produced an artifact: the producer identity, the |
| // provider and model behind it and the context (conversation or session) |
| -// it came from. Independence of a review is decided on these three. |
| +// it came from. V2 also requires different observed model families. |
| type Provenance struct { |
| - Producer string `json:"producer"` |
| - Provider string `json:"provider"` |
| - Model string `json:"model,omitempty"` |
| - Context string `json:"context"` |
| + ModelFamily string `json:"model_family,omitempty"` |
| + Isolation IsolationSnapshot `json:"isolation,omitzero"` |
| + Producer string `json:"producer"` |
| + Provider string `json:"provider"` |
| + Model string `json:"model,omitempty"` |
| + Context string `json:"context"` |
| } |
| func (p Provenance) validate() error { |
diff --git a/internal/equipo/team/policy.go b/internal/equipo/team/policy.go| index af0ce60e..ea61358e 100644 |
| --- a/internal/equipo/team/policy.go |
| +++ b/internal/equipo/team/policy.go |
| @@ -87,6 +87,9 @@ func (p Policy) Validate() error { |
| if p.Version == 1 && (p.UsageMode != "" || p.RequireDistinctReviewFamily) { |
| return fail(CodeInvalid, "v2 fields in v1 policy") |
| } |
| + if p.Version == 2 && !p.RequireDistinctReviewFamily { |
| + return fail(CodeInvalid, "review_family_required") |
| + } |
| if p.Version == 2 && p.UsageMode != "metered" && p.UsageMode != "subscription" { |
| return fail(CodeUnknownKind, "usage mode %q", p.UsageMode) |
| } |
diff --git a/internal/equipo/team/shared.go b/internal/equipo/team/shared.go| index 97152b19..74dfcd61 100644 |
| --- a/internal/equipo/team/shared.go |
| +++ b/internal/equipo/team/shared.go |
| @@ -23,7 +23,7 @@ func OpenShared(ctx context.Context, writer *sql.DB, path string, o Options) (*S |
| if err != nil { |
| return nil, err |
| } |
| - s := &Store{db: writer, path: path, marker: o.Marker, clock: o.Clock, auth: o.Authority, adapters: o.Adapters, commitHook: o.commitHook} |
| + s := &Store{db: writer, path: path, marker: o.Marker, clock: o.Clock, auth: o.Authority, adapters: o.Adapters, provenance: o.Provenance, commitHook: o.commitHook} |
| if s.adapters == nil { |
| s.adapters = map[Operation]Adapter{} |
| } |
diff --git a/internal/equipo/team/store.go b/internal/equipo/team/store.go| index f67b542e..5f3af981 100644 |
| --- a/internal/equipo/team/store.go |
| +++ b/internal/equipo/team/store.go |
| @@ -38,6 +38,7 @@ type Adapter interface { |
| // the incarnation marker kept outside the database; it defaults to the |
| // database path plus ".incarnation". |
| type Options struct { |
| + Provenance ProvenanceResolver |
| Clock Clock |
| Authority Authority |
| Adapters map[Operation]Adapter |
| @@ -49,6 +50,7 @@ type Options struct { |
| // commit with its external witness. Open launches no goroutine or network |
| // connection. The database, witness and lock live in trusted local storage. |
| type Store struct { |
| + provenance ProvenanceResolver |
| db *sql.DB |
| ownsDB bool |
| closed bool |
| @@ -90,7 +92,7 @@ func Open(ctx context.Context, path string, o Options) (*Store, error) { |
| return nil, fail(CodeInvalid, "open: %v", err) |
| } |
| db.SetMaxOpenConns(4) |
| - s := &Store{db: db, ownsDB: true, path: path, marker: o.Marker, clock: o.Clock, auth: o.Authority, adapters: o.Adapters} |
| + s := &Store{db: db, ownsDB: true, path: path, marker: o.Marker, clock: o.Clock, auth: o.Authority, adapters: o.Adapters, provenance: o.Provenance} |
| if s.adapters == nil { |
| s.adapters = map[Operation]Adapter{} |
| } |