| 1 | package team |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "encoding/json" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | type gitEnv struct { |
| 12 | *env |
| 13 | scope Scope |
| 14 | candidate GitCandidate |
| 15 | artifact Artifact |
| 16 | observed Provenance |
| 17 | } |
| 18 | |
| 19 | func newGitEnv(t *testing.T) *gitEnv { return newGitEnvObject(t, 40) } |
| 20 | |
| 21 | func newGitEnvObject(t *testing.T, objectLength int) *gitEnv { |
| 22 | return newGitEnvRepository(t, objectLength, "example/shop") |
| 23 | } |
| 24 | func newGitEnvRepository(t *testing.T, objectLength int, repository string) *gitEnv { |
| 25 | e := newEnv(t) |
| 26 | g := &gitEnv{env: e} |
| 27 | g.observed = Provenance{ConnectionID: "observed-connection", 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"))}}}} |
| 28 | must(t, e.store.Close()) |
| 29 | opts := e.options() |
| 30 | opts.Provenance = func(context.Context, *sql.Tx, Scope, Handle) (Provenance, error) { return g.observed, nil } |
| 31 | var err error |
| 32 | e.store, err = Open(e.ctx, e.path, opts) |
| 33 | must(t, err) |
| 34 | _, err = e.store.Resume(e.ctx) |
| 35 | must(t, err) |
| 36 | steps := SoftwareSteps(false) |
| 37 | for i := range steps { |
| 38 | steps[i].DependsOn = nil |
| 39 | } |
| 40 | steps = append(steps, StepSpec{Key: "rework", Capability: CapImplement, Version: 1}) |
| 41 | spec := softwareSpec(steps) |
| 42 | spec.Target.Repository = repository |
| 43 | spec.Policy = runnerPolicy(t, "subscription") |
| 44 | spec.Target.BaseRevision = strings.Repeat("a", objectLength) |
| 45 | spec.Evidence.BaselineTestSetHash = hashBytes([]byte("tests")) |
| 46 | _, g.scope = e.createJob(spec) |
| 47 | g.candidate = GitCandidate{Repository: spec.Target.Repository, BaseCommit: spec.Target.BaseRevision, Commit: strings.Repeat("b", objectLength), Tree: strings.Repeat("c", objectLength)} |
| 48 | return g |
| 49 | } |
| 50 | func (g *gitEnv) claimStep(step string) Handle { return g.claim(g.scope, step, "runner", "ws-"+step) } |
| 51 | func (g *gitEnv) finish(h Handle) { |
| 52 | must(g.t, g.store.Complete(g.ctx, g.scope, h, AttemptResult{Outcome: OutcomeSuccess})) |
| 53 | must(g.t, g.store.Contain(g.ctx, g.scope, h.Attempt, "supervisor")) |
| 54 | } |
| 55 | func (g *gitEnv) register(h Handle, role Role, payload []byte) (Artifact, error) { |
| 56 | media := "application/json" |
| 57 | if role == RoleCandidate { |
| 58 | media = GitCandidateMediaType |
| 59 | } |
| 60 | 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"}}) |
| 61 | } |
| 62 | func (g *gitEnv) build() { |
| 63 | h := g.claimStep("reproduce") |
| 64 | repro, err := g.register(h, RoleReproduction, FixtureReproduction) |
| 65 | must(g.t, err) |
| 66 | _, err = g.register(h, RoleBaseResult, mustJSON(CheckResult{ReproductionHash: repro.Hash, SubjectHash: g.candidate.BaseCommit, CheckDefinitionHash: CheckDefinitionHash, Outcome: CheckFail, TestsRun: 3, TestsFailed: 1, FailureMessage: FixtureSymptom})) |
| 67 | must(g.t, err) |
| 68 | g.finish(h) |
| 69 | h = g.claimStep("implement") |
| 70 | g.artifact, err = g.register(h, RoleCandidate, mustJSON(g.candidate)) |
| 71 | must(g.t, err) |
| 72 | g.finish(h) |
| 73 | } |
| 74 | func (g *gitEnv) review() { |
| 75 | g.observed.Producer = "reviewer" |
| 76 | g.observed.Context = "session-b" |
| 77 | g.observed.Provider = "provider-b" |
| 78 | g.observed.ModelFamily = "claude" |
| 79 | h := g.claimStep("review") |
| 80 | _, err := g.register(h, RoleReview, mustJSON(Review{SubjectHash: g.artifact.Hash, Verdict: VerdictApprove})) |
| 81 | must(g.t, err) |
| 82 | g.finish(h) |
| 83 | } |
| 84 | func (g *gitEnv) checks() RepositoryCheckResult { |
| 85 | 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"))} |
| 86 | } |
| 87 | func (g *gitEnv) verify(withChecks bool) { |
| 88 | h := g.claimStep("verify") |
| 89 | _, err := g.register(h, RoleCandidateResult, mustJSON(CheckResult{ReproductionHash: hashBytes(FixtureReproduction), SubjectHash: g.artifact.Hash, CheckDefinitionHash: CheckDefinitionHash, Outcome: CheckPass, TestsRun: 3})) |
| 90 | must(g.t, err) |
| 91 | if withChecks { |
| 92 | _, err = g.register(h, RoleRepositoryChecks, mustJSON(g.checks())) |
| 93 | must(g.t, err) |
| 94 | } |
| 95 | g.finish(h) |
| 96 | } |
| 97 | func (g *gitEnv) publication() publication { |
| 98 | return publication{Repository: g.candidate.Repository, Ref: "team/fix-sum", Head: g.candidate.Commit, CandidateHash: g.artifact.Hash} |
| 99 | } |
| 100 | func TestGitCandidateIdentity(t *testing.T) { |
| 101 | g := newGitEnv(t) |
| 102 | g.build() |
| 103 | payload, hash, err := canonical(mustJSON(g.candidate)) |
| 104 | must(t, err) |
| 105 | if g.artifact.Hash != hash || string(g.artifact.Payload) != string(payload) || g.artifact.Hash == g.candidate.Commit { |
| 106 | t.Fatal("candidate digest is not canonical artifact identity") |
| 107 | } |
| 108 | if g.artifact.Provenance.Producer != "builder" || g.artifact.Provenance.ModelFamily != "gpt" { |
| 109 | t.Fatal("submitted provenance replaced observed session") |
| 110 | } |
| 111 | h := g.claimStep("rework") |
| 112 | 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) }} { |
| 113 | c := g.candidate |
| 114 | change(&c) |
| 115 | _, err := g.register(h, RoleCandidate, mustJSON(c)) |
| 116 | wantCode(t, err, CodeEvidence) |
| 117 | } |
| 118 | _, err = g.store.Register(g.ctx, g.scope, h, ArtifactSpec{Role: RoleCandidate, MediaType: "text/plain", Payload: mustJSON(g.candidate)}) |
| 119 | wantCode(t, err, CodeEvidence) |
| 120 | // Canonical JSON, rather than submitted whitespace or field order, defines identity. |
| 121 | raw := []byte(`{ "tree":"` + g.candidate.Tree + `", "commit":"` + g.candidate.Commit + `", "base_commit":"` + g.candidate.BaseCommit + `", "repository":"` + g.candidate.Repository + `" }`) |
| 122 | a, err := g.register(h, RoleCandidate, raw) |
| 123 | must(t, err) |
| 124 | if a.Hash != g.artifact.Hash { |
| 125 | t.Fatal("noncanonical candidate digest") |
| 126 | } |
| 127 | g.finish(h) |
| 128 | // All Git object IDs may also use SHA-256. |
| 129 | // Test the parser independently because a job's base is immutable. |
| 130 | c := GitCandidate{Repository: "repo", BaseCommit: strings.Repeat("a", 64), Commit: strings.Repeat("b", 64), Tree: strings.Repeat("c", 64)} |
| 131 | if !validGitCandidate(c, TargetBinding{Repository: "repo", BaseRevision: c.BaseCommit}) { |
| 132 | t.Fatal("SHA-256 Git objects refused") |
| 133 | } |
| 134 | } |
| 135 | func TestBugNeedsRepositoryChecks(t *testing.T) { |
| 136 | t.Run("missing", func(t *testing.T) { |
| 137 | g := newGitEnv(t) |
| 138 | g.build() |
| 139 | g.review() |
| 140 | g.verify(false) |
| 141 | rep, err := g.store.ReadyToPublish(g.ctx, g.scope) |
| 142 | must(t, err) |
| 143 | if rep.Satisfied || !strings.Contains(rep.Reason, "repository") { |
| 144 | t.Fatal("missing repository checks passed", rep) |
| 145 | } |
| 146 | if g.job(g.scope).State != JobRunning { |
| 147 | t.Fatal("readiness terminated job") |
| 148 | } |
| 149 | g.finish(g.claimStep("rework")) |
| 150 | g.finish(g.claimStep("publish")) |
| 151 | _, err = g.store.Conclude(g.ctx, g.scope, "test") |
| 152 | wantCode(t, err, CodeEvidence) |
| 153 | if g.job(g.scope).State != JobFailed { |
| 154 | t.Fatal("missing checks concluded successfully") |
| 155 | } |
| 156 | }) |
| 157 | for _, name := range []string{"compile", "skip", "no_tests", "zero", "exit", "failed", "definition", "malformed", "malformed observed", "baseline", "commit", "candidate"} { |
| 158 | t.Run(name, func(t *testing.T) { |
| 159 | g := newGitEnv(t) |
| 160 | g.build() |
| 161 | h := g.claimStep("verify") |
| 162 | c := g.checks() |
| 163 | switch name { |
| 164 | case "compile": |
| 165 | c.Outcome = CheckCompileError |
| 166 | case "skip": |
| 167 | c.Outcome = CheckSkipped |
| 168 | case "no_tests": |
| 169 | c.Outcome = CheckNoTests |
| 170 | case "zero": |
| 171 | c.TestsRun = 0 |
| 172 | case "exit": |
| 173 | c.ExitCode = 1 |
| 174 | case "failed": |
| 175 | c.TestsFailed = 1 |
| 176 | case "definition": |
| 177 | c.CheckDefinitionHash = hashBytes([]byte("wrong")) |
| 178 | case "malformed": |
| 179 | c.LogHash = "ABC" |
| 180 | case "malformed observed": |
| 181 | c.ObservedTestSetHash = "not-a-digest" |
| 182 | case "baseline": |
| 183 | c.BaselineTestSetHash = hashBytes([]byte("wrong")) |
| 184 | case "commit": |
| 185 | c.Commit = strings.Repeat("d", 40) |
| 186 | case "candidate": |
| 187 | c.CandidateHash = hashBytes([]byte("wrong")) |
| 188 | } |
| 189 | _, err := g.register(h, RoleRepositoryChecks, mustJSON(c)) |
| 190 | wantCode(t, err, CodeEvidence) |
| 191 | }) |
| 192 | } |
| 193 | t.Run("success and publication", func(t *testing.T) { |
| 194 | g := newGitEnv(t) |
| 195 | g.build() |
| 196 | g.review() |
| 197 | g.verify(true) |
| 198 | rep, err := g.store.ReadyToPublish(g.ctx, g.scope) |
| 199 | must(t, err) |
| 200 | if !rep.Satisfied || rep.RepositoryChecks == "" || !rep.Isolation.RunnerTainted { |
| 201 | t.Fatal("valid readiness rejected", rep) |
| 202 | } |
| 203 | rep, err = g.store.Evidence(g.ctx, g.scope) |
| 204 | must(t, err) |
| 205 | if rep.Satisfied { |
| 206 | t.Fatal("receipt omitted from evidence") |
| 207 | } |
| 208 | _, err = g.store.Conclude(g.ctx, g.scope, "test") |
| 209 | wantCode(t, err, CodeState) |
| 210 | // Finish the unused rework step without creating another candidate. |
| 211 | g.finish(g.claimStep("rework")) |
| 212 | h := g.claimStep("publish") |
| 213 | 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 }} { |
| 214 | p := g.publication() |
| 215 | change(&p) |
| 216 | _, err = g.store.Intend(g.ctx, g.scope, h, IntentSpec{Key: "bad", Operation: OpForgePublish, Payload: mustJSON(p)}) |
| 217 | if err == nil { |
| 218 | t.Fatal("invalid publication accepted") |
| 219 | } |
| 220 | } |
| 221 | id, err := g.store.Intend(g.ctx, g.scope, h, IntentSpec{Key: "publish", Operation: OpForgePublish, Payload: mustJSON(g.publication())}) |
| 222 | must(t, err) |
| 223 | action, err := g.store.GetAction(g.ctx, g.scope, id) |
| 224 | must(t, err) |
| 225 | var p publication |
| 226 | must(t, json.Unmarshal(action.Payload, &p)) |
| 227 | if !p.Isolation.RunnerTainted { |
| 228 | t.Fatal("publication stripped isolation") |
| 229 | } |
| 230 | _, err = g.store.Dispatch(g.ctx, g.scope, id) |
| 231 | must(t, err) |
| 232 | must(t, g.store.Complete(g.ctx, g.scope, h, AttemptResult{Outcome: OutcomeSuccess})) |
| 233 | _, err = g.store.Conclude(g.ctx, g.scope, "test") |
| 234 | wantCode(t, err, CodeState) |
| 235 | must(t, g.store.Contain(g.ctx, g.scope, h.Attempt, "supervisor")) |
| 236 | rep, err = g.store.Conclude(g.ctx, g.scope, "test") |
| 237 | must(t, err) |
| 238 | if !rep.Satisfied || rep.Publication != id { |
| 239 | t.Fatal("publication identity mismatch", rep) |
| 240 | } |
| 241 | }) |
| 242 | } |
| 243 | func TestReviewDifferentFamily(t *testing.T) { |
| 244 | for _, name := range []string{"same across providers", "unknown", "empty", "same producer", "same session", "different"} { |
| 245 | t.Run(name, func(t *testing.T) { |
| 246 | g := newGitEnv(t) |
| 247 | g.build() |
| 248 | h := g.claimStep("review") |
| 249 | g.observed.Provider = "other-provider" |
| 250 | g.observed.Producer = "reviewer" |
| 251 | g.observed.Context = "review-session" |
| 252 | g.observed.ModelFamily = "claude" |
| 253 | switch name { |
| 254 | case "same across providers": |
| 255 | g.observed.Model = "another-profile" |
| 256 | g.observed.ModelFamily = "gpt" |
| 257 | case "unknown": |
| 258 | g.observed.ModelFamily = "unknown" |
| 259 | case "empty": |
| 260 | g.observed.ModelFamily = "" |
| 261 | case "same producer": |
| 262 | g.observed.Producer = "builder" |
| 263 | case "same session": |
| 264 | g.observed.Context = "session-a" |
| 265 | } |
| 266 | _, err := g.register(h, RoleReview, mustJSON(Review{SubjectHash: g.artifact.Hash, Verdict: VerdictApprove})) |
| 267 | if name == "different" { |
| 268 | must(t, err) |
| 269 | } else { |
| 270 | wantCode(t, err, CodeEvidence) |
| 271 | } |
| 272 | }) |
| 273 | } |
| 274 | } |
| 275 | func TestChangedCandidateInvalidatesEvidence(t *testing.T) { |
| 276 | g := newGitEnv(t) |
| 277 | g.build() |
| 278 | g.review() |
| 279 | g.verify(true) |
| 280 | hpub := g.claimStep("publish") |
| 281 | id, err := g.store.Intend(g.ctx, g.scope, hpub, IntentSpec{Key: "publish", Operation: OpForgePublish, Payload: mustJSON(g.publication())}) |
| 282 | must(t, err) |
| 283 | h := g.claimStep("rework") |
| 284 | g.candidate.Commit = strings.Repeat("d", 40) |
| 285 | g.observed.Isolation.Status = "confined" |
| 286 | g.observed.Isolation.RunnerTainted = false |
| 287 | g.observed.Isolation.AcknowledgementIDs = nil |
| 288 | a, err := g.register(h, RoleCandidate, mustJSON(g.candidate)) |
| 289 | must(t, err) |
| 290 | if !a.Provenance.Isolation.RunnerTainted || len(a.Provenance.Isolation.AcknowledgementIDs) != 1 { |
| 291 | t.Fatal("candidate lineage erased isolation") |
| 292 | } |
| 293 | // A new candidate invalidates evidence even while its attempt is unfinished. |
| 294 | rep, err := g.store.ReadyToPublish(g.ctx, g.scope) |
| 295 | must(t, err) |
| 296 | if rep.Satisfied { |
| 297 | t.Fatal("new candidate retained old evidence") |
| 298 | } |
| 299 | g.finish(h) |
| 300 | _, err = g.store.Admit(g.ctx, g.scope, id) |
| 301 | wantCode(t, err, CodeEvidence) |
| 302 | rep, err = g.store.ReadyToPublish(g.ctx, g.scope) |
| 303 | must(t, err) |
| 304 | if rep.Satisfied { |
| 305 | t.Fatal("changed candidate retained review or checks") |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | func TestEvidenceObservedIsolation(t *testing.T) { |
| 310 | for _, name := range []string{"resolver", "status", "ack", "tuple", "lookup"} { |
| 311 | t.Run(name, func(t *testing.T) { |
| 312 | g := newGitEnv(t) |
| 313 | h := g.claimStep("reproduce") |
| 314 | switch name { |
| 315 | case "resolver": |
| 316 | g.store.provenance = nil |
| 317 | case "status": |
| 318 | g.observed.Isolation.Status = "" |
| 319 | case "ack": |
| 320 | g.observed.Isolation.AcknowledgementIDs = nil |
| 321 | case "tuple": |
| 322 | g.observed.Isolation.ObservedTuples = nil |
| 323 | case "lookup": |
| 324 | g.store.provenance = func(context.Context, *sql.Tx, Scope, Handle) (Provenance, error) { |
| 325 | return Provenance{}, fail(CodeEvidence, "lookup failed") |
| 326 | } |
| 327 | } |
| 328 | _, err := g.register(h, RoleReproduction, FixtureReproduction) |
| 329 | wantCode(t, err, CodeEvidence) |
| 330 | }) |
| 331 | } |
| 332 | g := newGitEnv(t) |
| 333 | g.build() |
| 334 | g.review() |
| 335 | g.verify(true) |
| 336 | arts, err := g.store.Artifacts(g.ctx, g.scope) |
| 337 | must(t, err) |
| 338 | for _, a := range arts { |
| 339 | if !a.Provenance.Isolation.RunnerTainted { |
| 340 | t.Fatal("evidence lost isolation", a.Role) |
| 341 | } |
| 342 | } |
| 343 | before, err := g.store.Events(g.ctx, g.scope, 0) |
| 344 | must(t, err) |
| 345 | _, err = g.store.Evidence(g.ctx, g.scope) |
| 346 | must(t, err) |
| 347 | _, err = g.store.ReadyToPublish(g.ctx, g.scope) |
| 348 | must(t, err) |
| 349 | after, err := g.store.Events(g.ctx, g.scope, 0) |
| 350 | must(t, err) |
| 351 | if len(before) != len(after) || g.job(g.scope).State != JobRunning { |
| 352 | t.Fatal("evidence read wrote state") |
| 353 | } |
| 354 | // Persisted historical exposure survives reopening without a live resolver. |
| 355 | must(t, g.store.Close()) |
| 356 | g.open() |
| 357 | rep, err := g.store.ReadyToPublish(g.ctx, g.scope) |
| 358 | must(t, err) |
| 359 | if !rep.Satisfied || !rep.Isolation.RunnerTainted { |
| 360 | t.Fatal("reopen erased evidence provenance", rep) |
| 361 | } |
| 362 | // A stripped historical snapshot fails both reads and subsequent registration. |
| 363 | must(t, g.store.tx(g.ctx, func(tx *sql.Tx) error { |
| 364 | p := g.artifact.Provenance |
| 365 | p.Isolation = IsolationSnapshot{} |
| 366 | _, err := tx.ExecContext(g.ctx, `UPDATE artifacts SET provenance=? WHERE id=?`, string(mustJSON(p)), int64(g.artifact.ID)) |
| 367 | return err |
| 368 | })) |
| 369 | _, err = g.store.ReadyToPublish(g.ctx, g.scope) |
| 370 | wantCode(t, err, CodeEvidence) |
| 371 | } |
| 372 | func TestEvidencePublicationAdmission(t *testing.T) { |
| 373 | for _, name := range []string{"repository", "ref", "commit", "hash", "isolation", "new exposure"} { |
| 374 | t.Run(name, func(t *testing.T) { |
| 375 | g := newGitEnv(t) |
| 376 | g.build() |
| 377 | g.review() |
| 378 | g.verify(true) |
| 379 | h := g.claimStep("publish") |
| 380 | id, err := g.store.Intend(g.ctx, g.scope, h, IntentSpec{Key: "publish", Operation: OpForgePublish, Payload: mustJSON(g.publication())}) |
| 381 | must(t, err) |
| 382 | a, err := g.store.GetAction(g.ctx, g.scope, id) |
| 383 | must(t, err) |
| 384 | var p publication |
| 385 | must(t, strictJSON(a.Payload, &p)) |
| 386 | switch name { |
| 387 | case "repository": |
| 388 | p.Repository = "other" |
| 389 | case "ref": |
| 390 | p.Ref = "main" |
| 391 | case "commit": |
| 392 | p.Head = strings.Repeat("d", 40) |
| 393 | case "hash": |
| 394 | p.CandidateHash = hashBytes([]byte("other")) |
| 395 | case "isolation": |
| 396 | p.Isolation = IsolationSnapshot{} |
| 397 | case "new exposure": |
| 398 | g.observed.Isolation.AcknowledgementIDs = append(g.observed.Isolation.AcknowledgementIDs, "ack-2") |
| 399 | } |
| 400 | must(t, g.store.tx(g.ctx, func(tx *sql.Tx) error { |
| 401 | _, err := tx.ExecContext(g.ctx, `UPDATE actions SET payload=? WHERE id=?`, mustJSON(p), int64(id)) |
| 402 | return err |
| 403 | })) |
| 404 | _, err = g.store.Admit(g.ctx, g.scope, id) |
| 405 | if err == nil { |
| 406 | t.Fatal("publication admission accepted changed " + name) |
| 407 | } |
| 408 | }) |
| 409 | } |
| 410 | } |
| 411 | func TestEvidenceRepositoryChecksRevalidated(t *testing.T) { |
| 412 | for _, role := range []Role{RoleReview, RoleRepositoryChecks, RoleCandidateResult} { |
| 413 | t.Run(string(role), func(t *testing.T) { |
| 414 | g := newGitEnv(t) |
| 415 | g.build() |
| 416 | g.review() |
| 417 | g.verify(true) |
| 418 | // Keep other evidence valid to exercise each independent evaluation guard. |
| 419 | must(t, g.store.tx(g.ctx, func(tx *sql.Tx) error { |
| 420 | var payload []byte |
| 421 | switch role { |
| 422 | case RoleReview: |
| 423 | payload = mustJSON(Review{SubjectHash: hashBytes([]byte("old")), Verdict: VerdictApprove}) |
| 424 | case RoleRepositoryChecks: |
| 425 | r := g.checks() |
| 426 | r.CandidateHash = hashBytes([]byte("old")) |
| 427 | payload = mustJSON(r) |
| 428 | case RoleCandidateResult: |
| 429 | payload = mustJSON(CheckResult{SubjectHash: hashBytes([]byte("old")), ReproductionHash: hashBytes(FixtureReproduction), CheckDefinitionHash: CheckDefinitionHash, Outcome: CheckPass, TestsRun: 3}) |
| 430 | } |
| 431 | _, err := tx.ExecContext(g.ctx, `UPDATE artifacts SET payload=? WHERE job_id=? AND role=?`, payload, int64(g.scope.Job), string(role)) |
| 432 | return err |
| 433 | })) |
| 434 | rep, err := g.store.ReadyToPublish(g.ctx, g.scope) |
| 435 | must(t, err) |
| 436 | if rep.Satisfied { |
| 437 | t.Fatal("stale evidence passed", role) |
| 438 | } |
| 439 | }) |
| 440 | } |
| 441 | g := newGitEnv(t) |
| 442 | g.build() |
| 443 | h := g.claimStep("review") |
| 444 | _, err := g.register(h, RoleRepositoryChecks, mustJSON(g.checks())) |
| 445 | wantCode(t, err, CodeScope) |
| 446 | } |
| 447 | func TestReviewDifferentFamilySameProvider(t *testing.T) { |
| 448 | g := newGitEnv(t) |
| 449 | g.build() |
| 450 | h := g.claimStep("review") |
| 451 | g.observed.Producer = "reviewer" |
| 452 | g.observed.Context = "other-session" |
| 453 | g.observed.ModelFamily = "claude" |
| 454 | _, err := g.register(h, RoleReview, mustJSON(Review{SubjectHash: g.artifact.Hash, Verdict: VerdictApprove})) |
| 455 | must(t, err) |
| 456 | } |
| 457 | func TestBugNeedsMeasuredBaseline(t *testing.T) { |
| 458 | g := newGitEnv(t) |
| 459 | g.build() |
| 460 | h := g.claimStep("verify") |
| 461 | r := g.checks() |
| 462 | // The executor verifies a superset of required tests; additional passing tests |
| 463 | // change the observed set digest without changing the frozen baseline digest. |
| 464 | r.ObservedTestSetHash = hashBytes([]byte("tests plus new tests")) |
| 465 | r.TestsRun = 4 |
| 466 | _, err := g.register(h, RoleRepositoryChecks, mustJSON(r)) |
| 467 | must(t, err) |
| 468 | } |
| 469 | |
| 470 | func TestGitCandidateStrictFields(t *testing.T) { |
| 471 | g := newGitEnv(t) |
| 472 | h := g.claimStep("implement") |
| 473 | for _, payload := range []string{ |
| 474 | `{"Repository":"example/shop","base_commit":"` + g.candidate.BaseCommit + `","commit":"` + g.candidate.Commit + `","tree":"` + g.candidate.Tree + `"}`, |
| 475 | `{"repository":"wrong","repository":"example/shop","base_commit":"` + g.candidate.BaseCommit + `","commit":"` + g.candidate.Commit + `","tree":"` + g.candidate.Tree + `"}`, |
| 476 | } { |
| 477 | _, err := g.register(h, RoleCandidate, []byte(payload)) |
| 478 | wantCode(t, err, CodeEvidence) |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | func TestReviewDifferentFamilyPolicy(t *testing.T) { |
| 483 | p := runnerPolicy(t, "subscription") |
| 484 | p.RequireDistinctReviewFamily = false |
| 485 | wantCode(t, p.Validate(), CodeInvalid) |
| 486 | } |
| 487 | |
| 488 | func TestGitCandidateSHA256(t *testing.T) { |
| 489 | g := newGitEnvObject(t, 64) |
| 490 | g.build() |
| 491 | g.review() |
| 492 | g.verify(true) |
| 493 | h := g.claimStep("publish") |
| 494 | _, err := g.store.Intend(g.ctx, g.scope, h, IntentSpec{Key: "publish", Operation: OpForgePublish, Payload: mustJSON(g.publication())}) |
| 495 | must(t, err) |
| 496 | } |
| 497 | |
| 498 | func TestGitCandidateObjectLengths(t *testing.T) { |
| 499 | for _, n := range []int{0, 39, 40, 41, 63, 64, 65, 128} { |
| 500 | c := GitCandidate{Repository: "repo", BaseCommit: strings.Repeat("a", n), Commit: strings.Repeat("b", n), Tree: strings.Repeat("c", n)} |
| 501 | got := validGitCandidate(c, TargetBinding{Repository: "repo", BaseRevision: c.BaseCommit}) |
| 502 | if got != (n == 40 || n == 64) { |
| 503 | t.Fatalf("Git object length %d accepted=%v", n, got) |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | func TestEvidenceStrippedProducer(t *testing.T) { |
| 509 | g := newGitEnv(t) |
| 510 | g.build() |
| 511 | g.review() |
| 512 | g.verify(true) |
| 513 | must(t, g.store.tx(g.ctx, func(tx *sql.Tx) error { |
| 514 | p := g.artifact.Provenance |
| 515 | p.Producer = "" |
| 516 | _, err := tx.ExecContext(g.ctx, `UPDATE artifacts SET provenance=? WHERE id=?`, string(mustJSON(p)), int64(g.artifact.ID)) |
| 517 | return err |
| 518 | })) |
| 519 | _, err := g.store.ReadyToPublish(g.ctx, g.scope) |
| 520 | wantCode(t, err, CodeEvidence) |
| 521 | } |
| 522 | |