| 1 | // Package mergegate is Equipo's merge authority (spec |
| 2 | // docs/superpowers/specs/2026-11-11-equipo-merge-authority.md): one pure |
| 3 | // function decides whether Equipo may send a merge request on its own, |
| 4 | // must ask its person, or must do nothing. No store, clock or I/O: the |
| 5 | // instance builds the inputs inside its write transaction. No model's |
| 6 | // words can make it merge; a hold (the coordinator's) can only stop it. |
| 7 | package mergegate |
| 8 | |
| 9 | import ( |
| 10 | "path" |
| 11 | "slices" |
| 12 | "strings" |
| 13 | |
| 14 | "amadan.net/oficina/equipo/internal/wire" |
| 15 | ) |
| 16 | |
| 17 | // Decision classes, the housekeeping classes by name. |
| 18 | const ( |
| 19 | ClassAct = "act" |
| 20 | ClassAsk = "ask" |
| 21 | ClassNever = "never" |
| 22 | ) |
| 23 | |
| 24 | // Stances: Equipo's own call on an ask card. |
| 25 | const ( |
| 26 | StanceMerge = "merge" |
| 27 | StanceWait = "wait" |
| 28 | ) |
| 29 | |
| 30 | // Who decided an act. |
| 31 | const ( |
| 32 | DecidedPolicy = "policy" |
| 33 | DecidedPerson = "person" |
| 34 | ) |
| 35 | |
| 36 | // Policy modes and the answers to "does main ship to users?". |
| 37 | const ( |
| 38 | ModeRules = "rules" |
| 39 | ModeAsk = "ask" |
| 40 | ShipsUnknown = "unknown" |
| 41 | ShipsYes = "yes" |
| 42 | ShipsNo = "no" |
| 43 | ) |
| 44 | |
| 45 | // Policy bounds (spec §7.1). |
| 46 | const ( |
| 47 | DefaultMaxLines = 300 |
| 48 | DefaultMaxFiles = 15 |
| 49 | MaxLinesCeiling = 2000 |
| 50 | MaxFilesCeiling = 100 |
| 51 | MaxExtraGlobs = 20 |
| 52 | MaxGlobBytes = 200 |
| 53 | // MinProtocol certifies base-run checks and measured Review truncation. |
| 54 | MinProtocol = wire.ProtocolInjectionDefences |
| 55 | ) |
| 56 | |
| 57 | // Criteria, in the order Decide evaluates and reports them. |
| 58 | const ( |
| 59 | CritApproved = "approved_exact_sha" |
| 60 | CritEnvironment = "environment_reason" |
| 61 | CritProtocol = "runner_protocol" |
| 62 | CritFacts = "facts_present" |
| 63 | CritBaseChecks = "trusted_base_checks" |
| 64 | CritGates = "gate_files_unchanged" |
| 65 | CritReview = "review_diff_complete" |
| 66 | CritSmall = "small" |
| 67 | CritSensitive = "not_sensitive" |
| 68 | CritShips = "main_does_not_ship" |
| 69 | CritPolicy = "policy_allows" |
| 70 | CritHold = "no_hold" |
| 71 | CritRevert = "revert_exact" |
| 72 | CritEligible = "first_ready_checked" |
| 73 | CritSandbox = "checked_in_sandbox" |
| 74 | // CritPrimaryMade: the candidate was made on the primary. A Mac |
| 75 | // worker's Engineering cannot enforce immutable base gates, so its |
| 76 | // candidate merges only on a person's Merge. |
| 77 | CritPrimaryMade = "made_on_primary" |
| 78 | // CritChange: the task is a change. An exploration (mockups, |
| 79 | // prototypes, spikes) never merges, whoever asks. |
| 80 | CritChange = "is_a_change" |
| 81 | ) |
| 82 | |
| 83 | // environment are the reasons checks could not run that say nothing |
| 84 | // about the change: no Mac, or the runner that could run them offline. |
| 85 | var environment = []string{"needs_macos", "runner_offline"} |
| 86 | |
| 87 | // Policy is a project's merge policy. |
| 88 | type Policy struct { |
| 89 | Mode string |
| 90 | MaxLines int |
| 91 | MaxFiles int |
| 92 | MainShips string |
| 93 | ExtraSensitive []string |
| 94 | } |
| 95 | |
| 96 | // DefaultPolicy applies to a project without a row. |
| 97 | func DefaultPolicy() Policy { |
| 98 | return Policy{Mode: ModeRules, MaxLines: DefaultMaxLines, MaxFiles: DefaultMaxFiles, MainShips: ShipsUnknown} |
| 99 | } |
| 100 | |
| 101 | // ValidPolicy reports whether every field is inside its bounds. |
| 102 | func ValidPolicy(p Policy) bool { |
| 103 | if p.Mode != ModeRules && p.Mode != ModeAsk || !slices.Contains([]string{ShipsUnknown, ShipsYes, ShipsNo}, p.MainShips) { |
| 104 | return false |
| 105 | } |
| 106 | if p.MaxLines < 0 || p.MaxLines > MaxLinesCeiling || p.MaxFiles < 0 || p.MaxFiles > MaxFilesCeiling || len(p.ExtraSensitive) > MaxExtraGlobs { |
| 107 | return false |
| 108 | } |
| 109 | for _, g := range p.ExtraSensitive { |
| 110 | if !ValidGlob(g) { |
| 111 | return false |
| 112 | } |
| 113 | } |
| 114 | return true |
| 115 | } |
| 116 | |
| 117 | // ValidGlob reports whether g is a usable project glob: relative, clean, |
| 118 | // at most MaxGlobBytes, path.Match syntax per segment plus "**". |
| 119 | func ValidGlob(g string) bool { |
| 120 | if g == "" || len(g) > MaxGlobBytes || strings.HasPrefix(g, "/") || strings.ContainsAny(g, "\x00\\") { |
| 121 | return false |
| 122 | } |
| 123 | for _, seg := range strings.Split(g, "/") { |
| 124 | if seg == "" || seg == "." || seg == ".." { |
| 125 | return false |
| 126 | } |
| 127 | if _, err := path.Match(seg, ""); err != nil { |
| 128 | return false |
| 129 | } |
| 130 | } |
| 131 | return true |
| 132 | } |
| 133 | |
| 134 | // Tighter reports whether next allows nothing that old did not: what |
| 135 | // the coordinator may apply without a person's tap. |
| 136 | func Tighter(old, next Policy) bool { |
| 137 | if old.Mode == ModeAsk && next.Mode != ModeAsk || next.MaxLines > old.MaxLines || next.MaxFiles > old.MaxFiles { |
| 138 | return false |
| 139 | } |
| 140 | if next.MainShips != old.MainShips && next.MainShips != ShipsYes { |
| 141 | return false |
| 142 | } |
| 143 | for _, g := range old.ExtraSensitive { |
| 144 | if !slices.Contains(next.ExtraSensitive, g) { |
| 145 | return false |
| 146 | } |
| 147 | } |
| 148 | return true |
| 149 | } |
| 150 | |
| 151 | // Inputs are the facts of one decision, built by the instance. |
| 152 | type Inputs struct { |
| 153 | // Candidate is the newest pushed candidate, "" when the branch moved. |
| 154 | Candidate string |
| 155 | // Verdict, Checks and ChecksReason are the finished judgements of |
| 156 | // Candidate ("" while one runs). |
| 157 | Verdict, Checks, ChecksReason string |
| 158 | // OutsideSandbox: the checks ran with checks_sandbox none. |
| 159 | OutsideSandbox bool |
| 160 | // BaseChecks is accepted provenance for this candidate and its base. |
| 161 | BaseChecks bool |
| 162 | // AutoMergeCI is the current owner-granted project setting. |
| 163 | AutoMergeCI bool |
| 164 | AutoMergeSensitive bool |
| 165 | // Eligible: Candidate first earned Ready with checks passed (slice 4c's |
| 166 | // merge_eligible), or its checks passed on a Mac re-run later. |
| 167 | Eligible bool |
| 168 | // Rechecked: the checks that passed are a Mac re-run (mac-recheck) |
| 169 | // for a candidate that first reached Ready unchecked. Such a pass is |
| 170 | // judged like a merge without checks, less the environment rule: a |
| 171 | // routine change merges on its own, a sensitive, large or shipping one |
| 172 | // asks. |
| 173 | Rechecked bool |
| 174 | // Person: the task's creator, a current owner, chose Merge for this |
| 175 | // SHA. |
| 176 | Person bool |
| 177 | // Change is the review's measured change, nil when missing or refused. |
| 178 | Change *wire.ChangeFacts |
| 179 | // Protocol is the primary runner's protocol on the connection that |
| 180 | // will send (or is sending) the request. |
| 181 | Protocol int |
| 182 | Policy Policy |
| 183 | // Hold is the coordinator's hold on the task, "" when none. |
| 184 | Hold string |
| 185 | // Explore: the task is an exploration. Decide says never, first, |
| 186 | // whatever else holds (a person's Merge included). |
| 187 | Explore bool |
| 188 | // RevertSHA is set for a revert task: the merge commit it undoes. |
| 189 | RevertSHA string |
| 190 | // MacMade: an Engineering round of the task ran on a Mac worker. |
| 191 | MacMade bool |
| 192 | } |
| 193 | |
| 194 | // Criterion is one rule's outcome, for the card and the audit. |
| 195 | type Criterion struct { |
| 196 | Name string `json:"name"` |
| 197 | OK bool `json:"ok"` |
| 198 | Value string `json:"value,omitempty"` |
| 199 | } |
| 200 | |
| 201 | // Decision is the gate's answer. |
| 202 | type Decision struct { |
| 203 | Class string `json:"class"` |
| 204 | Stance string `json:"stance,omitempty"` |
| 205 | Checks bool `json:"checks"` |
| 206 | DecidedBy string `json:"decided_by,omitempty"` |
| 207 | Reasons []Criterion `json:"reasons"` |
| 208 | // Sensitive are the matches that failed not_sensitive. |
| 209 | Sensitive []Match `json:"sensitive,omitempty"` |
| 210 | } |
| 211 | |
| 212 | // Failing names the criteria that did not hold. |
| 213 | func (d Decision) Failing() []string { |
| 214 | var out []string |
| 215 | for _, c := range d.Reasons { |
| 216 | if !c.OK { |
| 217 | out = append(out, c.Name) |
| 218 | } |
| 219 | } |
| 220 | return out |
| 221 | } |
| 222 | |
| 223 | // Decide is the merge authority. Never: nothing to merge. A person's |
| 224 | // Merge acts on approved work. Checked work requires trusted base checks |
| 225 | // and complete change facts, subject to current policy and holds. |
| 226 | // Unchecked work acts only when every criterion holds. |
| 227 | func Decide(in Inputs) Decision { |
| 228 | if in.Explore { |
| 229 | return Decision{Class: ClassNever, Reasons: []Criterion{{Name: CritChange}}} |
| 230 | } |
| 231 | if in.Candidate == "" || in.Verdict != wire.VerdictApprove { |
| 232 | return Decision{Class: ClassNever, Reasons: []Criterion{{Name: CritApproved}}} |
| 233 | } |
| 234 | switch in.Checks { |
| 235 | case wire.ChecksPassed, wire.ChecksCannotRun, wire.ChecksNone: |
| 236 | default: |
| 237 | return Decision{Class: ClassNever, Reasons: []Criterion{{Name: CritApproved, OK: true}, {Name: CritEnvironment, Value: in.Checks}}} |
| 238 | } |
| 239 | approved := Criterion{Name: CritApproved, OK: true} |
| 240 | if in.Person { |
| 241 | return Decision{Class: ClassAct, Checks: in.Checks == wire.ChecksPassed, DecidedBy: DecidedPerson, Reasons: []Criterion{approved}} |
| 242 | } |
| 243 | validPolicy := ValidPolicy(in.Policy) |
| 244 | protocol := Criterion{Name: CritProtocol, OK: in.Protocol >= MinProtocol} |
| 245 | policy := Criterion{Name: CritPolicy, OK: validPolicy && in.Policy.Mode == ModeRules, Value: in.Policy.Mode} |
| 246 | hold := Criterion{Name: CritHold, OK: in.Hold == ""} |
| 247 | if in.Checks == wire.ChecksPassed && !in.Rechecked { |
| 248 | rs := []Criterion{approved, {Name: CritEligible, OK: in.Eligible}, {Name: CritSandbox, OK: !in.OutsideSandbox}, {Name: CritBaseChecks, OK: in.BaseChecks}, protocol} |
| 249 | rs, matches := changeCriteria(rs, in, validPolicy) |
| 250 | rs = append(rs, policy, hold) |
| 251 | return decided(macMade(rs, in), true, matches, []string{CritEligible, CritProtocol, CritPolicy}) |
| 252 | } |
| 253 | var rs []Criterion |
| 254 | if in.Checks == wire.ChecksPassed { |
| 255 | rs = []Criterion{approved, {Name: CritEligible, OK: in.Eligible}, {Name: CritSandbox, OK: !in.OutsideSandbox}, {Name: CritBaseChecks, OK: in.BaseChecks}, protocol} |
| 256 | } else { |
| 257 | rs = []Criterion{approved, {Name: CritEnvironment, OK: in.Checks == wire.ChecksCannotRun && slices.Contains(environment, in.ChecksReason), Value: in.ChecksReason}, protocol} |
| 258 | } |
| 259 | rs, matches := changeCriteria(rs, in, validPolicy) |
| 260 | rs = append(rs, policy, hold) |
| 261 | if in.RevertSHA != "" { |
| 262 | c := in.Change |
| 263 | ok := c != nil && c.Complete && c.Revert != nil && c.Revert.SHA == in.RevertSHA && c.Revert.OnTop && c.Revert.Exact |
| 264 | rs = append(rs, Criterion{Name: CritRevert, OK: ok}) |
| 265 | } |
| 266 | return decided(macMade(rs, in), in.Checks == wire.ChecksPassed, matches, []string{CritSmall, CritPolicy}) |
| 267 | } |
| 268 | |
| 269 | // macMade adds, for a Mac-made candidate, the rule it fails: only a |
| 270 | // person's Merge takes it, whatever setting or policy says (no waiver). |
| 271 | func macMade(rs []Criterion, in Inputs) []Criterion { |
| 272 | if !in.MacMade { |
| 273 | return rs |
| 274 | } |
| 275 | return append(rs, Criterion{Name: CritPrimaryMade}) |
| 276 | } |
| 277 | |
| 278 | // changeCriteria adds what the change itself must be to merge on its own: |
| 279 | // measured gate edits and Review completeness; unchecked work also has |
| 280 | // the size, sensitive-path and release restrictions. |
| 281 | func changeCriteria(rs []Criterion, in Inputs, validPolicy bool) ([]Criterion, []Match) { |
| 282 | c := in.Change |
| 283 | facts := Criterion{Name: CritFacts, OK: c != nil && c.Complete} |
| 284 | rs = append(rs, facts) |
| 285 | waived := in.AutoMergeCI && in.Checks == wire.ChecksPassed && in.BaseChecks && !in.OutsideSandbox |
| 286 | gatesChanged := c != nil && c.GateFilesChanged |
| 287 | if c != nil { |
| 288 | for _, entry := range c.Entries { |
| 289 | for _, name := range []string{entry.Old, entry.New} { |
| 290 | if name == ".equipo/gates.json" || name == ".amadan/ci" || name == ".amadan/ci.d" || strings.HasPrefix(name, ".amadan/ci.d/") || slices.Contains([]string{"Makefile", "makefile", "GNUmakefile"}, path.Base(name)) || path.Ext(name) == ".mk" { |
| 291 | gatesChanged = true |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | if facts.OK { |
| 297 | rs = append(rs, Criterion{Name: CritGates, OK: !gatesChanged || waived}, Criterion{Name: CritReview, OK: !c.ReviewTruncated || waived}) |
| 298 | } |
| 299 | if in.Checks == wire.ChecksPassed && !in.Rechecked { |
| 300 | var matches []Match |
| 301 | ok := false |
| 302 | if facts.OK { |
| 303 | matches = Sensitive(c.Entries, in.Policy.ExtraSensitive) |
| 304 | ok = len(matches) == 0 && !c.Special && !c.Signing |
| 305 | } |
| 306 | waiver := in.AutoMergeSensitive && in.BaseChecks && !in.OutsideSandbox |
| 307 | return append(rs, Criterion{Name: CritSensitive, OK: ok || waiver}), matches |
| 308 | } |
| 309 | var matches []Match |
| 310 | if facts.OK { |
| 311 | small := validPolicy && c.Lines <= in.Policy.MaxLines && c.Files <= in.Policy.MaxFiles && c.Binary == 0 |
| 312 | matches = Sensitive(c.Entries, in.Policy.ExtraSensitive) |
| 313 | rs = append(rs, Criterion{Name: CritSmall, OK: small}, Criterion{Name: CritSensitive, OK: len(matches) == 0 && !c.Special && !c.Signing}) |
| 314 | } else { |
| 315 | rs = append(rs, Criterion{Name: CritSmall}, Criterion{Name: CritSensitive}) |
| 316 | } |
| 317 | return append(rs, Criterion{Name: CritShips, OK: validPolicy && in.Policy.MainShips == ShipsNo, Value: in.Policy.MainShips}), matches |
| 318 | } |
| 319 | |
| 320 | // decided is act when every criterion holds, else ask; the stance is |
| 321 | // merge only when every failing criterion is one of soft. |
| 322 | func decided(rs []Criterion, checks bool, matches []Match, soft []string) Decision { |
| 323 | d := Decision{Class: ClassAct, Checks: checks, DecidedBy: DecidedPolicy, Reasons: rs, Sensitive: matches} |
| 324 | stance := StanceMerge |
| 325 | for _, c := range rs { |
| 326 | if c.OK { |
| 327 | continue |
| 328 | } |
| 329 | d.Class, d.DecidedBy = ClassAsk, "" |
| 330 | if !slices.Contains(soft, c.Name) { |
| 331 | stance = StanceWait |
| 332 | } |
| 333 | } |
| 334 | if d.Class == ClassAsk { |
| 335 | d.Stance = stance |
| 336 | } |
| 337 | return d |
| 338 | } |
| 339 | |