| 1 | package team |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "encoding/json" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestJobRequestKey(t *testing.T) { |
| 11 | e := newEnv(t) |
| 12 | spec := softwareSpec(SoftwareSteps(false)) |
| 13 | spec.RequestKey = "request-1" |
| 14 | first, scope := e.createJob(spec) |
| 15 | again, _ := e.createJob(spec) |
| 16 | if first.ID != again.ID { |
| 17 | t.Fatal("retry duplicated job") |
| 18 | } |
| 19 | spec.Target.Ref = "other" |
| 20 | _, err := e.store.CreateJob(e.ctx, spec) |
| 21 | wantCode(t, err, CodeConflict) |
| 22 | spec.Target.Ref = first.Target.Ref |
| 23 | spec.Requester = "other" |
| 24 | other, _ := e.createJob(spec) |
| 25 | if other.ID == first.ID { |
| 26 | t.Fatal("key crossed requester") |
| 27 | } |
| 28 | spec.Requester = first.Requester |
| 29 | spec.Steps[0].DependsOn = []string{} |
| 30 | again, _ = e.createJob(spec) |
| 31 | if again.ID != first.ID { |
| 32 | t.Fatal("nil/empty dependencies not canonical") |
| 33 | } |
| 34 | events, err := e.store.Events(e.ctx, scope, 0) |
| 35 | must(t, err) |
| 36 | if len(events) != 1 { |
| 37 | t.Fatal("retry appended events") |
| 38 | } |
| 39 | must(t, e.store.Close()) |
| 40 | e.open() |
| 41 | again, _ = e.createJob(spec) |
| 42 | if again.ID != first.ID { |
| 43 | t.Fatal("reopen lost request key") |
| 44 | } |
| 45 | } |
| 46 | func TestBugFreezeEvidence(t *testing.T) { |
| 47 | e := newEnv(t) |
| 48 | spec := softwareSpec(SoftwareSteps(false)) |
| 49 | spec.Evidence.RequireBugFreeze = true |
| 50 | _, scope := e.createJob(spec) |
| 51 | h := e.claim(scope, "reproduce", "runner", "fresh") |
| 52 | if err := e.store.Complete(e.ctx, scope, h, AttemptResult{Outcome: OutcomeSuccess}); err == nil { |
| 53 | t.Fatal("reproduce completed without frozen base failure") |
| 54 | } |
| 55 | if err := e.store.FreezeBugEvidence(e.ctx, scope, h, FixtureSymptom, CheckDefinitionHash, "missing"); err == nil { |
| 56 | t.Fatal("unregistered reproduction frozen") |
| 57 | } |
| 58 | repro, err := e.store.Register(e.ctx, scope, h, reproSpec(h)) |
| 59 | must(t, err) |
| 60 | stale := h |
| 61 | stale.Revision-- |
| 62 | if err := e.store.FreezeBugEvidence(e.ctx, scope, stale, FixtureSymptom, CheckDefinitionHash, repro.Hash); err == nil { |
| 63 | t.Fatal("stale revision frozen") |
| 64 | } |
| 65 | must(t, e.store.FreezeBugEvidence(e.ctx, scope, h, FixtureSymptom, CheckDefinitionHash, repro.Hash)) |
| 66 | changed := reproSpec(h) |
| 67 | changed.Payload = []byte("different") |
| 68 | if _, err := e.store.Register(e.ctx, scope, h, changed); err == nil { |
| 69 | t.Fatal("frozen reproduction changed") |
| 70 | } |
| 71 | base := CheckResult{SubjectHash: spec.Target.BaseRevision, ReproductionHash: repro.Hash, CheckDefinitionHash: CheckDefinitionHash, Outcome: CheckFail, TestsRun: 1, TestsFailed: 1, FailureMessage: FixtureSymptom} |
| 72 | raw, _ := json.Marshal(base) |
| 73 | a := ArtifactSpec{Role: RoleBaseResult, MediaType: "application/json", Payload: raw, Provenance: Provenance{Producer: "executor", Provider: "local", Context: "check"}} |
| 74 | if _, err := e.store.Register(e.ctx, scope, h, a); err == nil { |
| 75 | t.Fatal("unattested base accepted") |
| 76 | } |
| 77 | e.store.executorEvidence = fakeBugExecutor |
| 78 | _, err = e.store.Register(e.ctx, scope, h, a) |
| 79 | must(t, err) |
| 80 | must(t, e.store.Complete(e.ctx, scope, h, AttemptResult{Outcome: OutcomeSuccess})) |
| 81 | must(t, e.store.Contain(e.ctx, scope, h.Attempt, "supervisor")) |
| 82 | h = e.claim(scope, "implement", "runner", "fresh-implement") |
| 83 | if err := e.store.FreezeBugEvidence(e.ctx, scope, h, "changed", CheckDefinitionHash, repro.Hash); err == nil { |
| 84 | t.Fatal("implement froze evidence") |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func TestBugFreezeGuards(t *testing.T) { |
| 89 | for _, guard := range []string{"revision", "capability", "definition", "attempt", "fence", "candidate", "symptom", "frozen_symptom", "frozen_hash", "base_hash", "base_subject", "base_symptom", "base_outcome", "base_counts", "executor"} { |
| 90 | t.Run(guard, func(t *testing.T) { |
| 91 | e := newEnv(t) |
| 92 | spec := softwareSpec(SoftwareSteps(false)) |
| 93 | spec.Evidence.RequireBugFreeze = true |
| 94 | _, scope := e.createJob(spec) |
| 95 | h := e.claim(scope, "reproduce", "runner", "workspace") |
| 96 | repro, err := e.store.Register(e.ctx, scope, h, reproSpec(h)) |
| 97 | must(t, err) |
| 98 | symptom, def, hash := FixtureSymptom, CheckDefinitionHash, repro.Hash |
| 99 | switch guard { |
| 100 | case "revision": |
| 101 | h.Revision++ |
| 102 | case "capability": |
| 103 | _, err = e.store.db.Exec("UPDATE steps SET capability='implement' WHERE key='reproduce'") |
| 104 | must(t, err) |
| 105 | case "definition": |
| 106 | def = "wrong" |
| 107 | case "attempt": |
| 108 | _, err = e.store.db.Exec("UPDATE artifacts SET attempt_id=999") |
| 109 | must(t, err) |
| 110 | case "fence": |
| 111 | _, err = e.store.db.Exec("UPDATE artifacts SET fence=fence+1") |
| 112 | must(t, err) |
| 113 | case "candidate": |
| 114 | _, err = e.store.db.Exec("INSERT INTO artifacts(job_id,step_id,attempt_id,fence,role,hash,media_type,size,provenance,payload,created_at) SELECT job_id,step_id,attempt_id,fence,'candidate',hash,media_type,size,provenance,payload,created_at FROM artifacts") |
| 115 | must(t, err) |
| 116 | case "symptom": |
| 117 | symptom = "" |
| 118 | } |
| 119 | if guard == "revision" || guard == "capability" || guard == "definition" || guard == "attempt" || guard == "fence" || guard == "candidate" || guard == "symptom" { |
| 120 | if err := e.store.FreezeBugEvidence(e.ctx, scope, h, symptom, def, hash); err == nil { |
| 121 | t.Fatal("freeze guard bypassed", guard) |
| 122 | } |
| 123 | return |
| 124 | } |
| 125 | must(t, e.store.FreezeBugEvidence(e.ctx, scope, h, symptom, def, hash)) |
| 126 | h.Revision = e.job(scope).Revision |
| 127 | if guard == "frozen_symptom" || guard == "frozen_hash" { |
| 128 | if guard == "frozen_symptom" { |
| 129 | symptom = "different" |
| 130 | } else { |
| 131 | hash = "different" |
| 132 | } |
| 133 | if err := e.store.FreezeBugEvidence(e.ctx, scope, h, symptom, def, hash); err == nil { |
| 134 | t.Fatal("frozen evidence changed", guard) |
| 135 | } |
| 136 | return |
| 137 | } |
| 138 | must(t, e.store.FreezeBugEvidence(e.ctx, scope, h, symptom, def, hash)) |
| 139 | base := CheckResult{SubjectHash: spec.Target.BaseRevision, ReproductionHash: repro.Hash, CheckDefinitionHash: def, Outcome: CheckFail, TestsRun: 1, TestsFailed: 1, FailureMessage: symptom} |
| 140 | e.store.executorEvidence = fakeBugExecutor |
| 141 | raw, _ := json.Marshal(base) |
| 142 | a := ArtifactSpec{Role: RoleBaseResult, MediaType: "application/json", Payload: raw, Provenance: Provenance{Producer: "executor", Provider: "local", Context: "check"}} |
| 143 | if guard == "executor" { |
| 144 | e.store.executorEvidence = func(context.Context, *sql.Tx, Scope, Handle, ArtifactSpec) (Provenance, error) { |
| 145 | return Provenance{Producer: "executor", Provider: "local", Context: "check"}, ErrEvidence |
| 146 | } |
| 147 | if _, err := e.store.Register(e.ctx, scope, h, a); err == nil { |
| 148 | t.Fatal("executor refusal ignored") |
| 149 | } |
| 150 | return |
| 151 | } |
| 152 | _, err = e.store.Register(e.ctx, scope, h, a) |
| 153 | must(t, err) |
| 154 | // Corrupt persisted evidence to independently exercise completion validation. |
| 155 | switch guard { |
| 156 | case "base_hash": |
| 157 | base.ReproductionHash = "wrong" |
| 158 | case "base_subject": |
| 159 | base.SubjectHash = "wrong" |
| 160 | case "base_symptom": |
| 161 | base.FailureMessage = "wrong" |
| 162 | case "base_outcome": |
| 163 | base.Outcome = CheckPass |
| 164 | case "base_counts": |
| 165 | base.TestsFailed = 0 |
| 166 | } |
| 167 | raw, _ = json.Marshal(base) |
| 168 | _, err = e.store.db.Exec("UPDATE artifacts SET payload=? WHERE role='base_result'", raw) |
| 169 | must(t, err) |
| 170 | if err := e.store.Complete(e.ctx, scope, h, AttemptResult{Outcome: OutcomeSuccess}); err == nil { |
| 171 | t.Fatal("base failure guard bypassed", guard) |
| 172 | } |
| 173 | }) |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | func fakeBugExecutor(context.Context, *sql.Tx, Scope, Handle, ArtifactSpec) (Provenance, error) { |
| 178 | return Provenance{Producer: "trusted-executor", Provider: "local", Context: "check-run"}, nil |
| 179 | } |
| 180 | |
| 181 | func TestJobRequestKeyScopesAndPayload(t *testing.T) { |
| 182 | for _, change := range []string{"project", "target", "grant", "policy", "evidence", "steps", "parent"} { |
| 183 | t.Run(change, func(t *testing.T) { |
| 184 | e := newEnv(t) |
| 185 | spec := softwareSpec(SoftwareSteps(false)) |
| 186 | spec.RequestKey = "key" |
| 187 | e.createJob(spec) |
| 188 | switch change { |
| 189 | case "project": |
| 190 | spec.Project = 1 |
| 191 | case "target": |
| 192 | spec.Target.Ref = "other" |
| 193 | case "grant": |
| 194 | e.auth.Issue("other", Grant{Account: acctA, Operations: map[Operation]bool{OpJobContinue: true}}) |
| 195 | spec.GrantRef = "other" |
| 196 | case "policy": |
| 197 | spec.Policy.Job.Calls-- |
| 198 | case "evidence": |
| 199 | spec.Evidence.ExpectedSymptom = "changed" |
| 200 | case "steps": |
| 201 | spec.Steps[0].Version++ |
| 202 | case "parent": |
| 203 | spec.Parent = 1 |
| 204 | } |
| 205 | _, err := e.store.CreateJob(e.ctx, spec) |
| 206 | wantCode(t, err, CodeConflict) |
| 207 | }) |
| 208 | } |
| 209 | t.Run("account", func(t *testing.T) { |
| 210 | e := newEnv(t) |
| 211 | spec := softwareSpec(SoftwareSteps(false)) |
| 212 | spec.RequestKey = "key" |
| 213 | first, _ := e.createJob(spec) |
| 214 | e.auth.Issue(grant, Grant{Account: acctB, Operations: map[Operation]bool{OpJobContinue: true}}) |
| 215 | spec.Account = acctB |
| 216 | other, _ := e.createJob(spec) |
| 217 | if first.ID == other.ID { |
| 218 | t.Fatal("key crossed account") |
| 219 | } |
| 220 | }) |
| 221 | t.Run("empty legacy key", func(t *testing.T) { |
| 222 | e := newEnv(t) |
| 223 | spec := softwareSpec(SoftwareSteps(false)) |
| 224 | a, _ := e.createJob(spec) |
| 225 | b, _ := e.createJob(spec) |
| 226 | if a.ID == b.ID { |
| 227 | t.Fatal("empty key deduplicated legacy job") |
| 228 | } |
| 229 | }) |
| 230 | t.Run("concurrent", func(t *testing.T) { |
| 231 | e := newEnv(t) |
| 232 | spec := softwareSpec(SoftwareSteps(false)) |
| 233 | spec.RequestKey = "key" |
| 234 | type result struct { |
| 235 | j Job |
| 236 | err error |
| 237 | } |
| 238 | results := make(chan result, 6) |
| 239 | for i := 0; i < 6; i++ { |
| 240 | go func() { j, err := e.store.CreateJob(e.ctx, spec); results <- result{j, err} }() |
| 241 | } |
| 242 | var id JobID |
| 243 | for i := 0; i < 6; i++ { |
| 244 | r := <-results |
| 245 | must(t, r.err) |
| 246 | if id != 0 && id != r.j.ID { |
| 247 | t.Fatal("concurrent job duplicated") |
| 248 | } |
| 249 | id = r.j.ID |
| 250 | } |
| 251 | }) |
| 252 | } |
| 253 | func TestBugExecutorProvenance(t *testing.T) { |
| 254 | g := newGitEnv(t) |
| 255 | j := g.job(g.scope) |
| 256 | j.Evidence.RequireBugFreeze = true |
| 257 | raw, _ := json.Marshal(j.Evidence) |
| 258 | _, err := g.store.db.Exec("UPDATE jobs SET evidence=? WHERE id=?", string(raw), j.ID) |
| 259 | must(t, err) |
| 260 | h := g.claimStep("reproduce") |
| 261 | repro, err := g.register(h, RoleReproduction, FixtureReproduction) |
| 262 | must(t, err) |
| 263 | must(t, g.store.FreezeBugEvidence(g.ctx, g.scope, h, FixtureSymptom, CheckDefinitionHash, repro.Hash)) |
| 264 | base := CheckResult{SubjectHash: j.Target.BaseRevision, ReproductionHash: repro.Hash, CheckDefinitionHash: CheckDefinitionHash, Outcome: CheckFail, TestsRun: 1, TestsFailed: 1, FailureMessage: FixtureSymptom} |
| 265 | raw, _ = json.Marshal(base) |
| 266 | g.store.executorEvidence = func(context.Context, *sql.Tx, Scope, Handle, ArtifactSpec) (Provenance, error) { |
| 267 | return Provenance{}, nil |
| 268 | } |
| 269 | if _, err := g.register(h, RoleBaseResult, raw); err == nil { |
| 270 | t.Fatal("invalid executor provenance accepted") |
| 271 | } |
| 272 | g.store.executorEvidence = fakeBugExecutor |
| 273 | a, err := g.register(h, RoleBaseResult, raw) |
| 274 | must(t, err) |
| 275 | if a.Provenance.Producer != "trusted-executor" || !a.Provenance.Isolation.RunnerTainted || a.Provenance.Isolation.Status != "unconfined" { |
| 276 | t.Fatal("executor identity or exposure lost", a.Provenance) |
| 277 | } |
| 278 | g.finish(h) |
| 279 | if _, err := g.store.Claim(g.ctx, g.scope, "implement", "runner", "ws-reproduce"); err == nil { |
| 280 | t.Fatal("workspace reused") |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | func TestBugPublishIsRunnerOnly(t *testing.T) { |
| 285 | e := newEnv(t) |
| 286 | spec := softwareSpec([]StepSpec{{Key: "publish", Capability: CapPublish, Version: 1}}) |
| 287 | spec.Evidence.RequireBugFreeze = true |
| 288 | _, scope := e.createJob(spec) |
| 289 | h := e.claim(scope, "publish", "runner", "publication") |
| 290 | _, err := e.store.Intend(e.ctx, scope, h, IntentSpec{Key: "agent-publish", Operation: OpProviderCall, Payload: []byte(`{"task":"implement","prompt":"publish"}`), Reserve: Usage{InputTokens: 1, OutputTokens: 1}}) |
| 291 | if err == nil { |
| 292 | t.Fatal("publish allowed an agent turn") |
| 293 | } |
| 294 | if _, err := e.store.CreateJob(e.ctx, softwareSpec([]StepSpec{{Key: "merge", Capability: Capability("merge"), Version: 1}})); err == nil { |
| 295 | t.Fatal("core accepted merge execution") |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | func TestBugCandidateRequiresFreeze(t *testing.T) { |
| 300 | g := newGitEnv(t) |
| 301 | j := g.job(g.scope) |
| 302 | j.Evidence.RequireBugFreeze = true |
| 303 | raw, _ := json.Marshal(j.Evidence) |
| 304 | _, err := g.store.db.Exec("UPDATE jobs SET evidence=? WHERE id=?", string(raw), j.ID) |
| 305 | must(t, err) |
| 306 | h := g.claimStep("implement") |
| 307 | raw, _ = json.Marshal(g.candidate) |
| 308 | if _, err := g.register(h, RoleCandidate, raw); err == nil { |
| 309 | t.Fatal("candidate accepted before freeze") |
| 310 | } |
| 311 | } |
| 312 | |