| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 6 | "amadan.net/oficina/equipo/internal/taskplan" |
| 7 | "errors" |
| 8 | "gorm.io/gorm" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | func TestPlanApprovalPrecedence(t *testing.T) { |
| 14 | f := newFixture(t) |
| 15 | p := proposed(t, f) |
| 16 | mode, err := f.Store.PlanApproval(ctx, owner, p.ProjectID) |
| 17 | must(t, err) |
| 18 | if mode != equipo.ApprovalImmediate { |
| 19 | t.Fatal("default is not immediate") |
| 20 | } |
| 21 | must(t, f.Store.SetProjectApproval(ctx, owner, p.ProjectID, equipo.ApprovalRequired, equipo.Mutation{RequestID: "required", Revision: 0})) |
| 22 | if err = f.Store.SetProjectApproval(ctx, owner, p.ProjectID, equipo.ApprovalImmediate, equipo.Mutation{RequestID: "stale", Revision: 0}); !errors.Is(err, equipo.ErrConflict) { |
| 23 | t.Fatal("two tabs lost update", err) |
| 24 | } |
| 25 | mode, err = f.Store.PlanApproval(ctx, owner, p.ProjectID) |
| 26 | must(t, err) |
| 27 | if mode != equipo.ApprovalRequired { |
| 28 | t.Fatal("project rule ignored") |
| 29 | } |
| 30 | immediate := equipo.ApprovalImmediate |
| 31 | must(t, f.Store.SetApprovalOverride(ctx, owner, p.ProjectID, &immediate, equipo.Mutation{RequestID: "override"})) |
| 32 | mode, err = f.Store.PlanApproval(ctx, owner, p.ProjectID) |
| 33 | must(t, err) |
| 34 | if mode != immediate { |
| 35 | t.Fatal("override ignored") |
| 36 | } |
| 37 | mode, err = f.Store.PlanApproval(ctx, peer, p.ProjectID) |
| 38 | must(t, err) |
| 39 | if mode != equipo.ApprovalRequired { |
| 40 | t.Fatal("override leaked") |
| 41 | } |
| 42 | must(t, f.Store.SetApprovalOverride(ctx, owner, p.ProjectID, nil, equipo.Mutation{RequestID: "inherit", Revision: 1})) |
| 43 | mode, err = f.Store.PlanApproval(ctx, owner, p.ProjectID) |
| 44 | must(t, err) |
| 45 | if mode != equipo.ApprovalRequired { |
| 46 | t.Fatal("inherit ignored") |
| 47 | } |
| 48 | if err = f.Store.CheckPlanDispatch(ctx, owner, p.TaskID); !errors.Is(err, equipo.ErrConflict) { |
| 49 | t.Fatal("required switch did not block dispatch", err) |
| 50 | } |
| 51 | p.Steps[1].Instructions = "new instructions" |
| 52 | edited, err := f.Store.EditPlan(ctx, owner, p, equipo.Mutation{RequestID: "edit", Revision: p.Revision}) |
| 53 | must(t, err) |
| 54 | if edited.Revision != 2 { |
| 55 | t.Fatal("instruction edit did not advance revision") |
| 56 | } |
| 57 | if _, err = f.Store.EditPlan(ctx, owner, p, equipo.Mutation{RequestID: "stale-edit", Revision: 1}); !errors.Is(err, equipo.ErrConflict) { |
| 58 | t.Fatal("stale edit accepted", err) |
| 59 | } |
| 60 | if f.count(t, "SELECT count(*) FROM plan_revisions WHERE task_id=?", p.TaskID) != 2 { |
| 61 | t.Fatal("history missing") |
| 62 | } |
| 63 | } |
| 64 | func TestPlanProjectCorrection(t *testing.T) { |
| 65 | f := newFixture(t) |
| 66 | p := proposed(t, f) |
| 67 | target := equipo.Project{Name: "other", Repository: "amadan.net/test/other", DefaultBranch: "main", Kind: equipo.ProjectRepository, Revision: 1, GrantVersion: 1} |
| 68 | must(t, f.DB.G.Create(&target).Error) |
| 69 | must(t, f.DB.G.Create(&equipo.DecisionOwner{ProjectID: target.ID, Decision: "merge", Address: owner.Address}).Error) |
| 70 | f.exec(t, "UPDATE tasks SET base_commit='old',approval_revision=1 WHERE id=?", p.TaskID) |
| 71 | f.exec(t, "UPDATE task_outboxes SET engineering='old',review='old',prepared='old',job_spec='old' WHERE task_id=?", p.TaskID) |
| 72 | got, err := f.Store.CorrectTaskProject(ctx, owner, p.TaskID, target.ID, equipo.Mutation{RequestID: "correct", Revision: 1}) |
| 73 | must(t, err) |
| 74 | if got.ProjectID != target.ID || got.ApprovalRevision != 0 || got.BaseCommit != "" { |
| 75 | t.Fatal("correction retained preparation", got) |
| 76 | } |
| 77 | var prepared, eng, rev string |
| 78 | must(t, f.DB.Writer().QueryRow("SELECT prepared,engineering,review FROM task_outboxes WHERE task_id=?", p.TaskID).Scan(&prepared, &eng, &rev)) |
| 79 | if prepared != "" || eng != "" || rev != "" { |
| 80 | t.Fatal("correction retained picks") |
| 81 | } |
| 82 | if _, err = f.Store.CorrectTaskProject(ctx, owner, p.TaskID, p.ProjectID, equipo.Mutation{RequestID: "stale", Revision: 1}); !errors.Is(err, equipo.ErrConflict) { |
| 83 | t.Fatal("stale correction accepted", err) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func (f *fixture) planCore(t *testing.T, p taskplan.Plan) (*equipo.Execution, equipo.Task, team.Scope) { |
| 88 | t.Helper() |
| 89 | var path string |
| 90 | must(t, f.DB.Writer().QueryRow("SELECT file FROM pragma_database_list WHERE name='main'").Scan(&path)) |
| 91 | x, err := f.Store.AttachExecution(ctx, path, team.Options{Clock: team.RealClock{}, Authority: equipo.NewAuthority(f.Store, nil)}) |
| 92 | must(t, err) |
| 93 | t.Cleanup(func() { must(t, x.Core.Close()) }) |
| 94 | _, err = x.Core.Resume(ctx) |
| 95 | must(t, err) |
| 96 | task, err := f.Store.Task(ctx, owner, p.TaskID) |
| 97 | must(t, err) |
| 98 | var project equipo.Project |
| 99 | must(t, f.DB.G.First(&project, p.ProjectID).Error) |
| 100 | task.BaseCommit = strings.Repeat("a", 40) |
| 101 | task.CheckDefinitionHash = strings.Repeat("b", 64) |
| 102 | task.BaselineTestSetHash = strings.Repeat("c", 64) |
| 103 | policy, err := team.RunnerPolicy("subscription", team.Limits{Calls: 24, InputTokens: 200000, OutputTokens: 40000}, team.Limits{Calls: 1, InputTokens: 32000, OutputTokens: 4000}, team.Rates{}) |
| 104 | must(t, err) |
| 105 | spec, err := equipo.BugJobSpec(task, project, policy, "fixture symptom") |
| 106 | must(t, err) |
| 107 | job, err := x.Core.CreateJob(ctx, spec) |
| 108 | must(t, err) |
| 109 | task.JobID = job.ID |
| 110 | task.State = "queued" |
| 111 | must(t, x.Write(ctx, func(tx *gorm.DB) error { |
| 112 | return tx.Model(&equipo.Task{}).Where("id=?", task.ID).Select("JobID", "State", "BaseCommit", "CheckDefinitionHash", "BaselineTestSetHash").Updates(&task).Error |
| 113 | })) |
| 114 | return x, task, team.Scope{Account: team.AccountID(task.TeamID), Job: task.JobID} |
| 115 | } |
| 116 | func TestPlanEditClaimRaceAtomic(t *testing.T) { |
| 117 | for _, order := range []string{"edit_first", "claim_first", "audit_failure"} { |
| 118 | t.Run(order, func(t *testing.T) { |
| 119 | f := newFixture(t) |
| 120 | p := proposed(t, f) |
| 121 | x, task, scope := f.planCore(t, p) |
| 122 | original, err := x.Core.GetJob(ctx, scope) |
| 123 | must(t, err) |
| 124 | audits := f.count(t, "SELECT count(*) FROM audit_events") |
| 125 | p.Steps[0].Instructions = "changed before start" |
| 126 | switch order { |
| 127 | case "claim_first": |
| 128 | _, err = x.Core.ClaimRevision(ctx, scope, original.Revision, "reproduce", "runner", "workspace") |
| 129 | must(t, err) |
| 130 | if _, err = f.Store.EditPlan(ctx, owner, p, equipo.Mutation{RequestID: "race", Revision: 1}); err == nil { |
| 131 | t.Fatal("claim lost started-step protection") |
| 132 | } |
| 133 | if f.count(t, "SELECT count(*) FROM plan_revisions WHERE task_id=?", task.ID) != 1 || f.count(t, "SELECT count(*) FROM audit_events") != audits { |
| 134 | t.Fatal("losing edit partially committed") |
| 135 | } |
| 136 | case "edit_first": |
| 137 | edited, err := f.Store.EditPlan(ctx, owner, p, equipo.Mutation{RequestID: "race", Revision: 1}) |
| 138 | must(t, err) |
| 139 | if _, err = x.Core.ClaimRevision(ctx, scope, original.Revision, "reproduce", "runner", "workspace"); !errors.Is(err, team.ErrRevision) { |
| 140 | t.Fatal("stale claim accepted", err) |
| 141 | } |
| 142 | job, err := x.Core.GetJob(ctx, scope) |
| 143 | must(t, err) |
| 144 | if job.Revision != original.Revision+1 || edited.Revision != 2 || f.count(t, "SELECT revision FROM conversation_cards WHERE resource_id=?", task.ID) != 2 || f.count(t, "SELECT count(*) FROM audit_events") != audits+1 { |
| 145 | t.Fatal("edit did not commit core/card/audit together") |
| 146 | } |
| 147 | case "audit_failure": |
| 148 | f.exec(t, "CREATE TRIGGER fail_plan_audit BEFORE INSERT ON audit_events WHEN NEW.kind='plan.edit' BEGIN SELECT RAISE(ABORT,'fixture failure'); END") |
| 149 | if _, err = f.Store.EditPlan(ctx, owner, p, equipo.Mutation{RequestID: "race", Revision: 1}); err == nil { |
| 150 | t.Fatal("audit failure ignored") |
| 151 | } |
| 152 | job, err := x.Core.GetJob(ctx, scope) |
| 153 | must(t, err) |
| 154 | if job.Revision != original.Revision || f.count(t, "SELECT count(*) FROM plan_revisions WHERE task_id=?", task.ID) != 1 || f.count(t, "SELECT revision FROM conversation_cards WHERE resource_id=?", task.ID) != 1 { |
| 155 | t.Fatal("audit failure left partial changes") |
| 156 | } |
| 157 | } |
| 158 | }) |
| 159 | } |
| 160 | } |
| 161 | func TestPlanProjectCorrectionStarted(t *testing.T) { |
| 162 | f := newFixture(t) |
| 163 | p := proposed(t, f) |
| 164 | x, task, scope := f.planCore(t, p) |
| 165 | _, err := x.Core.Claim(ctx, scope, "reproduce", "runner", "workspace") |
| 166 | must(t, err) |
| 167 | target := equipo.Project{Name: "replacement", Repository: "amadan.net/test/replacement", DefaultBranch: "main", Kind: equipo.ProjectRepository, Revision: 1, GrantVersion: 1} |
| 168 | must(t, f.DB.G.Create(&target).Error) |
| 169 | must(t, f.DB.G.Create(&equipo.DecisionOwner{ProjectID: target.ID, Decision: "merge", Address: owner.Address}).Error) |
| 170 | result, err := f.Store.CorrectTaskProject(ctx, owner, task.ID, target.ID, equipo.Mutation{RequestID: "correct-started", Revision: 1}) |
| 171 | must(t, err) |
| 172 | if result.ProjectID != task.ProjectID || result.JobID != task.JobID || result.BaseCommit != task.BaseCommit || result.ReplacementID == "" { |
| 173 | t.Fatal("started correction retargeted original", result) |
| 174 | } |
| 175 | replacement, err := f.Store.Task(ctx, owner, result.ReplacementID) |
| 176 | must(t, err) |
| 177 | if replacement.ReplacesID != task.ID || replacement.ProjectID != target.ID || replacement.JobID != 0 || replacement.ApprovalRevision != 0 || replacement.State != "proposed" { |
| 178 | t.Fatal("unsafe replacement", replacement) |
| 179 | } |
| 180 | job, err := x.Core.GetJob(ctx, scope) |
| 181 | must(t, err) |
| 182 | if job.State != team.JobCancelled { |
| 183 | t.Fatal("original job remains executable") |
| 184 | } |
| 185 | attempts, err := x.Core.Attempts(ctx, scope) |
| 186 | must(t, err) |
| 187 | if len(attempts) != 1 || attempts[0].State != team.AttemptStopping { |
| 188 | t.Fatal("started worker not asked to stop") |
| 189 | } |
| 190 | again, err := f.Store.CorrectTaskProject(ctx, owner, task.ID, target.ID, equipo.Mutation{RequestID: "correct-started", Revision: 1}) |
| 191 | must(t, err) |
| 192 | if again.ReplacementID != result.ReplacementID { |
| 193 | t.Fatal("duplicate replacement") |
| 194 | } |
| 195 | } |
| 196 | func TestPlanAuthorityReplay(t *testing.T) { |
| 197 | f := newFixture(t) |
| 198 | p := proposed(t, f) |
| 199 | p.Steps[0].Instructions = "edited" |
| 200 | m := equipo.Mutation{RequestID: "saved", Revision: 1} |
| 201 | saved, err := f.Store.EditPlan(ctx, owner, p, m) |
| 202 | must(t, err) |
| 203 | replay, err := f.Store.EditPlan(ctx, owner, p, m) |
| 204 | must(t, err) |
| 205 | if replay.Revision != saved.Revision { |
| 206 | t.Fatal("lost response retry changed result") |
| 207 | } |
| 208 | p.Steps[0].Instructions = "changed request" |
| 209 | if _, err = f.Store.EditPlan(ctx, owner, p, m); !errors.Is(err, equipo.ErrConflict) { |
| 210 | t.Fatal("changed replay accepted", err) |
| 211 | } |
| 212 | f.exec(t, "UPDATE members SET active=0 WHERE address=?", owner.Address) |
| 213 | p.Steps[0].Instructions = "edited" |
| 214 | if _, err = f.Store.EditPlan(ctx, owner, p, m); !errors.Is(err, equipo.ErrForbidden) { |
| 215 | t.Fatal("revoked replay accepted", err) |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | func TestPlanProjectCorrectionPreparationStarted(t *testing.T) { |
| 220 | f := newFixture(t) |
| 221 | p := proposed(t, f) |
| 222 | f.exec(t, "INSERT INTO runner_assignments(id,task_id,step_key,revision,kind,state,claim_request,person) VALUES('prep',?,'prepare',1,'prepare','claimed','claim',?)", p.TaskID, owner.Address) |
| 223 | target := equipo.Project{Name: "preparation replacement", Repository: "amadan.net/test/preparation", DefaultBranch: "main", Kind: equipo.ProjectRepository, Revision: 1, GrantVersion: 1} |
| 224 | must(t, f.DB.G.Create(&target).Error) |
| 225 | must(t, f.DB.G.Create(&equipo.DecisionOwner{ProjectID: target.ID, Decision: "merge", Address: owner.Address}).Error) |
| 226 | got, err := f.Store.CorrectTaskProject(ctx, owner, p.TaskID, target.ID, equipo.Mutation{RequestID: "correct-preparation", Revision: 1}) |
| 227 | must(t, err) |
| 228 | if got.ProjectID != p.ProjectID || got.ReplacementID == "" || got.State != "cancel_requested" { |
| 229 | t.Fatal("retargeted started preparation") |
| 230 | } |
| 231 | if f.count(t, "SELECT count(*) FROM runner_assignments WHERE id='prep' AND state='fenced'") != 1 { |
| 232 | t.Fatal("preparation not fenced") |
| 233 | } |
| 234 | if err = f.Store.CheckPlanDispatch(ctx, owner, got.ReplacementID); !errors.Is(err, equipo.ErrConflict) { |
| 235 | t.Fatal("replacement can auto-dispatch", err) |
| 236 | } |
| 237 | } |
| 238 | |