| 1 | package planning |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 5 | "amadan.net/oficina/equipo/internal/taskplan" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "gorm.io/gorm" |
| 9 | ) |
| 10 | |
| 11 | type Selection = runnerproto.Selection |
| 12 | |
| 13 | // SelectionPolicy uses transaction-bound application authority and eligibility. |
| 14 | type SelectionPolicy struct { |
| 15 | Select func(*gorm.DB, string, int64, string, string, string, taskplan.Step, bool, bool) (Selection, error) |
| 16 | Current func(*gorm.DB, Task, string, Selection) (Selection, error) |
| 17 | Subscription func(*gorm.DB, Selection) error |
| 18 | ChoiceRevision func(*gorm.DB, string, int64) (int64, error) |
| 19 | SpecialistRevision func(*gorm.DB, string) (int64, error) |
| 20 | Unavailable, Conflict, SecondFamily error |
| 21 | } |
| 22 | |
| 23 | func ExecutionSpecialist(st taskplan.Step) string { |
| 24 | if st.Specialist != "" { |
| 25 | return st.Specialist |
| 26 | } |
| 27 | if st.Action == "checks" || st.Action == "publish" { |
| 28 | return "review" |
| 29 | } |
| 30 | return "engineering" |
| 31 | } |
| 32 | |
| 33 | // Choose validates a proposal inside the same transaction as the saved choice. |
| 34 | func (s SelectionPolicy) Choose(tx *gorm.DB, t Task, p taskplan.Plan, step taskplan.Step, confirm bool) (Selection, error) { |
| 35 | excluded := "" |
| 36 | if step.Action == "review" { |
| 37 | families := map[string]bool{} |
| 38 | for _, st := range p.Steps { |
| 39 | if !taskplan.Candidate(st) { |
| 40 | continue |
| 41 | } |
| 42 | sel, started, err := ActualSelection(tx, t, st.Key) |
| 43 | if err != nil { |
| 44 | return Selection{}, err |
| 45 | } |
| 46 | if !started { |
| 47 | sel, err = s.ForStep(tx, t, st, "", "", false) |
| 48 | if err != nil { |
| 49 | return Selection{}, err |
| 50 | } |
| 51 | } |
| 52 | families[sel.Family] = true |
| 53 | } |
| 54 | if len(families) != 1 { |
| 55 | return Selection{}, s.SecondFamily |
| 56 | } |
| 57 | for f := range families { |
| 58 | excluded = f |
| 59 | } |
| 60 | } |
| 61 | if confirm { |
| 62 | sel, err := s.Select(tx, t.Requester, t.ProjectID, ExecutionSpecialist(step), excluded, "", step, false, true) |
| 63 | if err != nil { |
| 64 | return sel, err |
| 65 | } |
| 66 | sel.Reason = "connections.reason_confirmed" |
| 67 | return sel, nil |
| 68 | } |
| 69 | return s.ForStep(tx, t, step, excluded, "", false) |
| 70 | } |
| 71 | |
| 72 | func (s SelectionPolicy) ForStep(tx *gorm.DB, t Task, st taskplan.Step, excluded, runner string, availabilityOnly bool) (Selection, error) { |
| 73 | actual, started, err := ActualSelection(tx, t, st.Key) |
| 74 | if err != nil { |
| 75 | return Selection{}, err |
| 76 | } |
| 77 | if started { |
| 78 | return s.Current(tx, t, st.Key, actual) |
| 79 | } |
| 80 | choice, err := s.ChoiceRevision(tx, t.Requester, t.ProjectID) |
| 81 | if err != nil { |
| 82 | return Selection{}, err |
| 83 | } |
| 84 | var row SelectionRecord |
| 85 | err = tx.First(&row, "task_id=? AND step_key=?", t.ID, st.Key).Error |
| 86 | if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { |
| 87 | return Selection{}, err |
| 88 | } |
| 89 | var saved Selection |
| 90 | valid := err == nil && row.PlanRevision == t.PlanRevision |
| 91 | if valid { |
| 92 | if err = json.Unmarshal([]byte(row.Selection), &saved); err != nil { |
| 93 | return Selection{}, err |
| 94 | } |
| 95 | sp, e := s.SpecialistRevision(tx, ExecutionSpecialist(st)) |
| 96 | if e != nil { |
| 97 | return Selection{}, e |
| 98 | } |
| 99 | valid = saved.ChoiceRevision == choice && saved.SpecialistRevision == sp |
| 100 | if valid { |
| 101 | var c struct { |
| 102 | Revision int64 |
| 103 | Person string |
| 104 | } |
| 105 | e = tx.Table("connections").First(&c, "id=?", saved.ConnectionID).Error |
| 106 | if e != nil && !errors.Is(e, gorm.ErrRecordNotFound) { |
| 107 | return Selection{}, e |
| 108 | } |
| 109 | valid = e == nil && c.Revision == saved.Revision && c.Person == t.Requester |
| 110 | } |
| 111 | } |
| 112 | confirmed := valid && row.Confirmed |
| 113 | if valid { |
| 114 | st.ConnectionID = saved.ConnectionID |
| 115 | st.Model = saved.Model |
| 116 | } |
| 117 | // Availability checks for a future reviewer do not confer ask consent. |
| 118 | sel, err := s.Select(tx, t.Requester, t.ProjectID, ExecutionSpecialist(st), excluded, runner, st, false, confirmed || availabilityOnly) |
| 119 | if err != nil { |
| 120 | if excluded != "" && err == s.Unavailable { |
| 121 | return sel, s.SecondFamily |
| 122 | } |
| 123 | return sel, err |
| 124 | } |
| 125 | if valid { |
| 126 | sel.Reason = saved.Reason |
| 127 | |
| 128 | } |
| 129 | return sel, nil |
| 130 | } |
| 131 | func (s SelectionPolicy) Pair(tx *gorm.DB, t Task, p taskplan.Plan, approval bool) (Selection, Selection, error) { |
| 132 | var first taskplan.Step |
| 133 | for _, st := range p.Steps { |
| 134 | if st.Specialist != "" { |
| 135 | first = st |
| 136 | break |
| 137 | } |
| 138 | } |
| 139 | selected, err := s.ForStep(tx, t, first, "", "", approval) |
| 140 | if err != nil { |
| 141 | return selected, Selection{}, err |
| 142 | } |
| 143 | if err = s.Subscription(tx, selected); err != nil { |
| 144 | return selected, Selection{}, err |
| 145 | } |
| 146 | // Scope and research can begin with one family. Builders require a reviewer. |
| 147 | if !taskplan.Candidate(first) && first.Action != "reproduce" { |
| 148 | return selected, Selection{}, nil |
| 149 | } |
| 150 | review, err := s.ForStep(tx, t, taskplan.Step{Specialist: "review", Action: "review"}, selected.Family, selected.RunnerID, true) |
| 151 | if err == nil { |
| 152 | err = s.Subscription(tx, review) |
| 153 | } |
| 154 | return selected, review, err |
| 155 | } |
| 156 | func (s SelectionPolicy) Dispatch(tx *gorm.DB, t Task, st taskplan.Step, first Selection) (Selection, error) { |
| 157 | runnerID := first.RunnerID |
| 158 | var assignments []struct{ Brief, RunnerID string } |
| 159 | if err := tx.Table("runner_assignments").Where("task_id=? AND job_id=? AND claim_request<>''", t.ID, t.JobID).Order("id").Find(&assignments).Error; err != nil { |
| 160 | return Selection{}, err |
| 161 | } |
| 162 | families := map[string]bool{} |
| 163 | for _, a := range assignments { |
| 164 | var b runnerproto.RunnerBrief |
| 165 | if json.Unmarshal([]byte(a.Brief), &b) != nil { |
| 166 | return Selection{}, s.Unavailable |
| 167 | } |
| 168 | if a.RunnerID != runnerID { |
| 169 | return Selection{}, s.Conflict |
| 170 | } |
| 171 | if taskplan.Candidate(taskplan.Step{Action: b.Step.Action}) { |
| 172 | families[b.Selection.Family] = true |
| 173 | } |
| 174 | } |
| 175 | excluded := "" |
| 176 | if st.Action == "review" || st.Action == "checks" || st.Action == "publish" { |
| 177 | if len(families) != 1 { |
| 178 | return Selection{}, s.Unavailable |
| 179 | } |
| 180 | for f := range families { |
| 181 | excluded = f |
| 182 | } |
| 183 | } |
| 184 | sel, err := s.ForStep(tx, t, st, excluded, runnerID, st.Action == "checks" || st.Action == "publish") |
| 185 | if err != nil { |
| 186 | return sel, err |
| 187 | } |
| 188 | if taskplan.Candidate(st) || st.Action == "reproduce" { |
| 189 | families[sel.Family] = true |
| 190 | if len(families) > 1 { |
| 191 | return sel, s.Unavailable |
| 192 | } |
| 193 | if _, err = s.ForStep(tx, t, taskplan.Step{Specialist: "review", Action: "review"}, sel.Family, runnerID, true); err != nil { |
| 194 | return sel, err |
| 195 | } |
| 196 | } |
| 197 | return sel, s.Subscription(tx, sel) |
| 198 | } |
| 199 | |
| 200 | func ResolveStep(p taskplan.Plan, key string, invalid error) (taskplan.Step, error) { |
| 201 | if key == "prepare" { |
| 202 | for _, st := range p.Steps { |
| 203 | if st.Specialist != "" { |
| 204 | return st, nil |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | for _, st := range p.Steps { |
| 209 | if st.Key == key { |
| 210 | return st, nil |
| 211 | } |
| 212 | } |
| 213 | if key == "publish" && taskplan.Publication(p.Kind) { |
| 214 | return taskplan.Step{Key: key, Action: "publish"}, nil |
| 215 | } |
| 216 | return taskplan.Step{}, invalid |
| 217 | } |
| 218 | |