| 1 | // Package triage sorts what a task needs from its person into what Equipo |
| 2 | // does about it (spec 2026-11-11-equipo-needs-you-triage.md, thin v1). It |
| 3 | // is a pure function of facts the store reads: no store, clock or I/O. An |
| 4 | // action is one of three deterministic steps; anything else stays in |
| 5 | // Needs you, with a recommendation. |
| 6 | package triage |
| 7 | |
| 8 | import ( |
| 9 | "slices" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | ) |
| 14 | |
| 15 | // Actions. |
| 16 | const ( |
| 17 | ActNone = "" |
| 18 | ActRetry = "retry" |
| 19 | ActRebase = "rebase" |
| 20 | ActRecheck = "recheck" |
| 21 | // ActUnstick and ActKick are the stall sweep's (Stalled): take back an |
| 22 | // attempt that runs or waits far past its time, so the next one is |
| 23 | // placed; or move on a task left with nothing scheduled. |
| 24 | ActUnstick = "unstick" |
| 25 | ActKick = "kick" |
| 26 | ) |
| 27 | |
| 28 | // The stall sweep's thresholds. A step's own limit is 20 minutes (a |
| 29 | // runner may set up to 60) and a hosted checks step waits at most 60, so |
| 30 | // 90 minutes running is far past |
| 31 | // it; an attempt placed on an online runner that has not started in 30 |
| 32 | // minutes, or a task whose newest step ended 30 minutes ago with nothing |
| 33 | // scheduled and nothing for its person to decide, has stalled. |
| 34 | const ( |
| 35 | StallRunning = 90 * time.Minute |
| 36 | StallQueued = 30 * time.Minute |
| 37 | StallIdle = 30 * time.Minute |
| 38 | ) |
| 39 | |
| 40 | // Step is what the stall sweep knows of a task's newest step. |
| 41 | type Step struct { |
| 42 | Specialist, State string |
| 43 | Withdrawn, Waiting bool |
| 44 | Online, Worker bool |
| 45 | StartedAt, FinishedAt *time.Time |
| 46 | UpdatedAt time.Time |
| 47 | } |
| 48 | |
| 49 | // Stalled is the stall sweep's action for a task with nothing for its |
| 50 | // person to decide (needsYou empty), or none. A merge step is never |
| 51 | // touched; an attempt is taken back only from a runner that is online |
| 52 | // (an offline one is the offline rule's and the next sync's), and never |
| 53 | // from a worker (the offline rule and the Mac checks gate own those); a |
| 54 | // task Ready to merge or merging is merge authority's. A merge hold does |
| 55 | // not stop the sweep: it stops a merge, and the sweep never merges. |
| 56 | func Stalled(f Facts, s Step, needsYou string, now time.Time) string { |
| 57 | if f.Ended || f.CancelRequested || f.Folded || f.Merging || needsYou != "" || s.Specialist == wire.SpecialistMerge || s.State == "" || s.Worker && !wire.StepTerminal(s.State) { |
| 58 | return ActNone |
| 59 | } |
| 60 | switch s.State { |
| 61 | case wire.StepRunning: |
| 62 | if s.Online && !s.Withdrawn && s.StartedAt != nil && now.Sub(*s.StartedAt) >= StallRunning { |
| 63 | return ActUnstick |
| 64 | } |
| 65 | case wire.StepQueued: |
| 66 | if s.Online && !s.Withdrawn && !s.Waiting && s.StartedAt == nil && now.Sub(s.UpdatedAt) >= StallQueued { |
| 67 | return ActUnstick |
| 68 | } |
| 69 | case wire.StepDone: |
| 70 | if !f.Ready && s.FinishedAt != nil && now.Sub(*s.FinishedAt) >= StallIdle { |
| 71 | return ActKick |
| 72 | } |
| 73 | } |
| 74 | return ActNone |
| 75 | } |
| 76 | |
| 77 | // MaxRebases is how many automatic rebase rounds a task gets, beyond the |
| 78 | // first that a merge conflict always sends (one per candidate). |
| 79 | const MaxRebases = 2 |
| 80 | |
| 81 | // passing are the stops worth one automatic retry: the runner restarted, |
| 82 | // lost the step (it no longer held it at sync) or its lease, or the step |
| 83 | // ran out of time. |
| 84 | var passing = []string{"runner_restarted", "interrupted", "superseded", "step_timeout", "checks_timeout"} |
| 85 | |
| 86 | // retried are the specialists a retry may run again. A merge step never: |
| 87 | // a merge is only ever decided through merge authority. |
| 88 | var retried = []string{wire.SpecialistCopy, wire.SpecialistEngineering, wire.SpecialistReview, wire.SpecialistChecks} |
| 89 | |
| 90 | // staleChecks are checks a hosted CI run may now give a verdict for. |
| 91 | var staleChecks = []string{"needs_network", "needs_macos"} |
| 92 | |
| 93 | // Passing reports whether a step's stop is worth one automatic retry. |
| 94 | func Passing(reason string) bool { return slices.Contains(passing, reason) } |
| 95 | |
| 96 | // Facts are what the store knows of the task when it triages. |
| 97 | type Facts struct { |
| 98 | // NeedsYou is what the person must decide, as progress computes it. |
| 99 | NeedsYou string |
| 100 | // Never touched: ended or cancel-requested, folded into another task, |
| 101 | // or merging. Held: the coordinator holds its merge, which stops only |
| 102 | // the merge-side actions (a rebase, a re-check before the merge card); |
| 103 | // a held task's passing stop is still retried. |
| 104 | Ended, CancelRequested, Folded, Merging, Held bool |
| 105 | // The newest step: its specialist, failure reason, termination proof, |
| 106 | // and whether Equipo already retried it on its own (any attempt). |
| 107 | Specialist, Reason string |
| 108 | Terminated, Retried bool |
| 109 | // OnWorker: the step was placed on a worker (a Mac). Its retries are |
| 110 | // the person's and the Mac checks gate's (it waits for the Mac's |
| 111 | // sandbox), never Equipo's own. |
| 112 | OnWorker bool |
| 113 | // The judged checks of the candidate; Stale when they ended before the |
| 114 | // project's first hosted CI verdict; Rechecked when Equipo already ran |
| 115 | // them again on this candidate. |
| 116 | Checks, ChecksReason string |
| 117 | Stale, Rechecked bool |
| 118 | // Rebased: Equipo already rebased this candidate on its own; Rebases |
| 119 | // how often it did on this task. |
| 120 | Rebased bool |
| 121 | Rebases int |
| 122 | // Ready: the task is Ready to merge (merge authority decides). |
| 123 | Ready bool |
| 124 | } |
| 125 | |
| 126 | // Verdict is what Equipo does (Action), or, when the task stays with its |
| 127 | // person, what it recommends and why it did not act itself. |
| 128 | type Verdict struct { |
| 129 | Action, Recommend, Why string |
| 130 | // Waiting: the task waits on the platform for this reason (a step |
| 131 | // reason); Equipo does nothing and recommends nothing. |
| 132 | Waiting string |
| 133 | } |
| 134 | |
| 135 | // Classify sorts the task's need (spec §§2, 4). A merge hold stops only |
| 136 | // what leads to a merge: the person still decides a held task's merge |
| 137 | // conflict and its merge, but a step that stopped in passing is retried. |
| 138 | func Classify(f Facts) Verdict { |
| 139 | if f.Ended || f.CancelRequested || f.Folded || f.Merging { |
| 140 | return Verdict{} |
| 141 | } |
| 142 | if f.Held && (f.NeedsYou == wire.NeedsMergeConflict || f.NeedsYou == wire.NeedsMerge) { |
| 143 | return Verdict{} |
| 144 | } |
| 145 | switch f.NeedsYou { |
| 146 | case wire.NeedsRetry, wire.NeedsReviewBlocked: |
| 147 | if !slices.Contains(retried, f.Specialist) { |
| 148 | return Verdict{} |
| 149 | } |
| 150 | if !f.Terminated || !Passing(f.Reason) || f.OnWorker { |
| 151 | return Verdict{Recommend: wire.RecommendRetry} |
| 152 | } |
| 153 | if f.Retried { |
| 154 | return Verdict{Recommend: wire.RecommendRetry, Why: wire.TriageWhyTried} |
| 155 | } |
| 156 | return Verdict{Action: ActRetry} |
| 157 | case wire.NeedsMergeConflict: |
| 158 | if f.Rebased || f.Rebases >= MaxRebases { |
| 159 | return Verdict{Recommend: wire.RecommendRebase, Why: wire.TriageWhyTried} |
| 160 | } |
| 161 | return Verdict{Action: ActRebase} |
| 162 | case wire.NeedsMerge: |
| 163 | if f.Specialist == wire.SpecialistMerge && f.Terminated && f.Reason == wire.ReasonMergeUnbornDefault { |
| 164 | // The hub cannot make the first merge into an empty repository |
| 165 | // yet: nothing for Equipo or the person to do but wait for it. |
| 166 | return Verdict{Waiting: f.Reason} |
| 167 | } |
| 168 | if f.Checks == wire.ChecksCannotRun && f.Stale && !f.Rechecked && slices.Contains(staleChecks, f.ChecksReason) { |
| 169 | return Verdict{Action: ActRecheck} |
| 170 | } |
| 171 | case wire.NeedsFixRounds: |
| 172 | return Verdict{Recommend: wire.RecommendRound} |
| 173 | } |
| 174 | return Verdict{} |
| 175 | } |
| 176 | |