| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "strings" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 12 | "amadan.net/oficina/equipo/internal/equipo/pause" |
| 13 | "amadan.net/oficina/equipo/internal/wire" |
| 14 | ) |
| 15 | |
| 16 | // Why work stopped (spec 2026-11-24-paused-work-design.md, Option 2). A |
| 17 | // task whose work waits shows, on its report card and its page, the |
| 18 | // stepper's current stop in the pause's tone, a lead line saying what |
| 19 | // stopped and when it carries on, what happens next, and the one most |
| 20 | // useful action. Indigo (paused) carries on by itself; amber (warning) |
| 21 | // only the person can move; red (negative) failed. |
| 22 | |
| 23 | // pauseLine is the lead and its action. |
| 24 | type pauseLine struct { |
| 25 | // Tone is paused, warning or negative; Icon the lead's mark; Stop the |
| 26 | // stepper's current stop (Paused, Needs you, Stopped). |
| 27 | Tone, Icon, Stop string |
| 28 | // Strong is the lead's bold part, Rest the remainder; Next what |
| 29 | // happens next. |
| 30 | Strong, Rest, Next string |
| 31 | // Actions are the creator's (the first is the one most useful); Note |
| 32 | // the small line under them. |
| 33 | Actions []pauseAction |
| 34 | Note string |
| 35 | } |
| 36 | |
| 37 | // pauseAction is a form post (Run now, Try again) or a link. |
| 38 | type pauseAction struct { |
| 39 | Label, Post, Href, Kind string |
| 40 | Step string |
| 41 | Revision int64 |
| 42 | // Offer is what the button shows (wire.RunNow.Offer): the tap is |
| 43 | // refused if Run now would now do anything else (review R1). |
| 44 | Offer string |
| 45 | Primary bool |
| 46 | } |
| 47 | |
| 48 | // tooOld are stop reasons that wait for the runner to be updated. |
| 49 | var tooOld = map[string]bool{"runner_outdated": true, "runner_too_old": true, "copy_update_required": true, "research_unsupported": true} |
| 50 | |
| 51 | const ( |
| 52 | pauseConnections = "/conversation/settings/connections" |
| 53 | pauseMachines = "/conversation/settings/machines" |
| 54 | ) |
| 55 | |
| 56 | // pauseOf is task t's pause as viewer sees it at now in loc, nil while |
| 57 | // nothing waits. Only the task's creator is offered its actions. |
| 58 | func pauseOf(t wire.Task, viewer string, now time.Time, loc *time.Location) *pauseLine { |
| 59 | if wire.FinalStage(t.Stage) || t.Moving || t.Stopping || t.CopyHold || t.CopyCanSkip || triageLine(t.Triage) != "" { |
| 60 | // Placement, Copy's hold card and the platform's own triage keep |
| 61 | // their words. |
| 62 | return nil |
| 63 | } |
| 64 | if t.NeedsYou != "" && t.NeedsYou != wire.NeedsRetry && t.NeedsYou != wire.NeedsHeld { |
| 65 | // Review, rounds, a conflict, the branch moving, direction, the |
| 66 | // Mac's sandbox, a removed machine: Needs you, as before. |
| 67 | return nil |
| 68 | } |
| 69 | s := lastStep(t) |
| 70 | if s == nil { |
| 71 | return nil |
| 72 | } |
| 73 | mine := t.Creator == viewer |
| 74 | if s.State == wire.StepFailed { |
| 75 | return stoppedPause(t, *s, mine, now, loc) |
| 76 | } |
| 77 | if wire.StepTerminal(s.State) { |
| 78 | return nil |
| 79 | } |
| 80 | if s.Paused != nil && s.Paused.Offline { |
| 81 | p := &pauseLine{Tone: "paused", Icon: "monitor-off", Stop: copyText("work.stage.paused")} |
| 82 | // Without the machine's name there is no lead: the stop and what |
| 83 | // happens next still say it is paused, not working. |
| 84 | if s.RunnerLabel != "" { |
| 85 | reason := fmt.Sprintf(copyText("work.pause.reason.offline"), s.RunnerLabel) |
| 86 | if s.Paused.Since != nil { |
| 87 | p.Strong, p.Rest = pause.Lead(copyText, "work.pause.since", clockOrDay(now, *s.Paused.Since, loc), reason) |
| 88 | } else { |
| 89 | p.Strong, p.Rest = pause.Lead(copyText, "work.pause.moment", reason) |
| 90 | } |
| 91 | } |
| 92 | if s.RunnerLabel != "" { |
| 93 | p.Next = fmt.Sprintf(copyText("work.pause.next.online"), s.RunnerLabel) |
| 94 | } |
| 95 | if mine { |
| 96 | p.Actions = []pauseAction{{Label: copyText("work.pause.check_machine"), Href: pauseMachines, Kind: "machine"}} |
| 97 | } |
| 98 | return p |
| 99 | } |
| 100 | if s.Hold == nil { |
| 101 | return nil |
| 102 | } |
| 103 | return heldPause(t, *s, mine, now, loc) |
| 104 | } |
| 105 | |
| 106 | // heldPause is the pause of a step the runner holds. |
| 107 | func heldPause(t wire.Task, s wire.Step, mine bool, now time.Time, loc *time.Location) *pauseLine { |
| 108 | h := s.Hold |
| 109 | name := chat.ProviderName(h.Provider) |
| 110 | if h.Provider == "" { |
| 111 | name = "" |
| 112 | } |
| 113 | var offer *wire.RunNowOffer |
| 114 | var used *int |
| 115 | var held, due *time.Time |
| 116 | if s.Paused != nil { |
| 117 | offer, used, held = s.Paused.RunNow, s.Paused.UsedPercent, s.Paused.Since |
| 118 | } |
| 119 | if t.Priority != nil { |
| 120 | due = t.Priority.By |
| 121 | } |
| 122 | p := &pauseLine{Tone: "paused", Icon: "clock", Stop: copyText("work.stage.paused")} |
| 123 | until := func(at *time.Time, reason string) { |
| 124 | switch { |
| 125 | case at != nil && at.After(now): |
| 126 | p.Strong, p.Rest = pause.Lead(copyText, "work.pause.until", pause.When(copyText, now, *at, loc), pause.In(copyText, at.Sub(now)), reason) |
| 127 | case held != nil: |
| 128 | p.Strong, p.Rest = pause.Lead(copyText, "work.pause.since", clockOrDay(now, *held, loc), reason) |
| 129 | default: |
| 130 | p.Strong, p.Rest = pause.Lead(copyText, "work.pause.moment", reason) |
| 131 | } |
| 132 | } |
| 133 | switch { |
| 134 | case h.NeedsPerson == wire.HoldNeedsNoConnection || h.Reason == wire.HoldNoConnection: |
| 135 | p.Tone, p.Icon, p.Stop = "warning", "plug", copyText("work.stage.needs_you") |
| 136 | p.Strong, p.Rest = pause.Lead(copyText, "work.pause.lead.no_connection") |
| 137 | p.Next = copyText("work.pause.next.choose") |
| 138 | if mine { |
| 139 | p.Actions = runNow(t, s, offer, p, true) |
| 140 | p.Actions = append(p.Actions, pauseAction{Label: copyText("work.pause.connect"), Href: pauseConnections + "/add", Kind: "connect"}) |
| 141 | } |
| 142 | case h.Reason == wire.HoldSpendUnknown: |
| 143 | p.Tone, p.Icon, p.Stop = "warning", "coins", copyText("work.stage.needs_you") |
| 144 | p.Strong, p.Rest = pause.Lead(copyText, "work.pause.lead.spend_unknown", name) |
| 145 | p.Next = fmt.Sprintf(copyText("work.pause.next.spend_unknown"), name, name) |
| 146 | if mine { |
| 147 | p.Actions = []pauseAction{{Label: copyText("work.hold.resume"), Href: pauseConnections, Kind: "resume", Primary: true}} |
| 148 | } |
| 149 | case h.NeedsPerson != "": |
| 150 | // Equipo's recommendation, as the held card words it, with how |
| 151 | // long until the reset. |
| 152 | p.Tone, p.Icon, p.Stop = "warning", "hand", copyText("work.stage.needs_you") |
| 153 | words := chat.HeldWords(copyText, name, h, due, now, func(at time.Time) string { return pause.WhenIn(copyText, now, at, loc) }, func(at time.Time) string { return at.In(loc).Format("Monday 2 Jan") }) |
| 154 | p.Strong, p.Rest = splitLead(words) |
| 155 | p.Next = copyText("work.pause.next.choose") |
| 156 | switch { |
| 157 | case !mine: |
| 158 | case h.Reason == wire.HoldBudget: |
| 159 | p.Actions = []pauseAction{{Label: copyText("work.hold.raise_budget"), Href: pauseConnections, Kind: "raise-budget", Primary: true}} |
| 160 | default: |
| 161 | p.Actions = runNow(t, s, offer, p, true) |
| 162 | } |
| 163 | case h.Reason == wire.HoldBudget: |
| 164 | p.Icon = "wallet" |
| 165 | until(h.WakeAt, fmt.Sprintf(copyText("work.pause.reason.budget"), name)) |
| 166 | p.Next = copyText("work.pause.next.budget") |
| 167 | if mine { |
| 168 | p.Actions = []pauseAction{{Label: copyText("work.hold.raise_budget"), Href: pauseConnections, Kind: "raise-budget"}} |
| 169 | } |
| 170 | case h.Reason == wire.HoldBusy: |
| 171 | p.Icon = "hourglass" |
| 172 | p.Strong, p.Rest = pause.Lead(copyText, "work.pause.moment", fmt.Sprintf(copyText("work.pause.reason.busy"), name)) |
| 173 | p.Next = copyText("work.pause.next.busy") |
| 174 | default: |
| 175 | reason := fmt.Sprintf(copyText("work.pause.reason.headroom_out"), name) |
| 176 | if used != nil && *used < 100 { |
| 177 | reason = fmt.Sprintf(copyText("work.pause.reason.headroom_saving"), name, *used) |
| 178 | } |
| 179 | at := h.ResetsAt |
| 180 | if at == nil { |
| 181 | at = h.WakeAt |
| 182 | } |
| 183 | until(at, reason) |
| 184 | if at != nil && at.After(now) { |
| 185 | p.Next = copyText("work.pause.next.by_itself") |
| 186 | } |
| 187 | if mine { |
| 188 | p.Actions = runNow(t, s, offer, p, false) |
| 189 | } |
| 190 | } |
| 191 | return p |
| 192 | } |
| 193 | |
| 194 | // runNow is Run now as the instance predicts it (decision 2): with the |
| 195 | // connection it would use, and the note beneath; none when it predicts |
| 196 | // nothing would start. |
| 197 | func runNow(t wire.Task, s wire.Step, o *wire.RunNowOffer, p *pauseLine, primary bool) []pauseAction { |
| 198 | if o == nil || s.Hold == nil { |
| 199 | return nil |
| 200 | } |
| 201 | a := pauseAction{Post: "/conversation/tasks/" + t.ID + "/run-now", Kind: "run-now", Step: s.ID, Revision: s.Hold.StepRevision, Primary: primary} |
| 202 | a.Offer = o.Connection |
| 203 | switch { |
| 204 | case o.Plain: |
| 205 | // An older runner cannot be held to one connection: no promise, |
| 206 | // and said when a connection that bills per use could take it. |
| 207 | a.Label, a.Offer = copyText("work.hold.run_now"), wire.RunNowPlain |
| 208 | if o.MayBill { |
| 209 | p.Note = copyText("work.pause.run_now_may_bill") |
| 210 | } |
| 211 | case o.Billing == wire.BillingMetered: |
| 212 | a.Label, p.Note = fmt.Sprintf(copyText("work.pause.run_now_with"), o.Name), fmt.Sprintf(copyText("work.pause.run_now_metered"), o.Name) |
| 213 | case o.Same: |
| 214 | a.Label = fmt.Sprintf(copyText("work.pause.run_now_on"), o.Name) |
| 215 | if o.LeftPercent > 0 { |
| 216 | p.Note = fmt.Sprintf(copyText("work.pause.run_now_last"), o.LeftPercent) |
| 217 | } |
| 218 | default: |
| 219 | a.Label, p.Note = fmt.Sprintf(copyText("work.pause.run_now_with"), o.Name), fmt.Sprintf(copyText("work.pause.run_now_room"), o.Name) |
| 220 | } |
| 221 | return []pauseAction{a} |
| 222 | } |
| 223 | |
| 224 | // stoppedPause is a failed step's lead: waiting for a runner update |
| 225 | // (amber), or stopped (red) with Try again. |
| 226 | func stoppedPause(t wire.Task, s wire.Step, mine bool, now time.Time, loc *time.Location) *pauseLine { |
| 227 | if s.Reason == "machine_removed" && !s.Terminated { |
| 228 | // Settled by Recover, as before. |
| 229 | return nil |
| 230 | } |
| 231 | if tooOld[s.Reason] && s.RunnerLabel != "" { |
| 232 | p := &pauseLine{Tone: "warning", Icon: "circle-arrow-up", Stop: copyText("work.stage.needs_you")} |
| 233 | p.Strong, p.Rest = pause.Lead(copyText, "work.pause.lead.too_old", s.RunnerLabel) |
| 234 | p.Next = copyText("work.pause.next.update") |
| 235 | if mine { |
| 236 | p.Actions = []pauseAction{{Label: copyText("work.pause.update_runner"), Href: pauseMachines, Kind: "update", Primary: true}} |
| 237 | } |
| 238 | return p |
| 239 | } |
| 240 | reason := copyText("work.reason." + stopReason(t, s)) |
| 241 | if reason == "" { |
| 242 | reason = copyText("work.reason.other") |
| 243 | } |
| 244 | at := now |
| 245 | if s.FinishedAt != nil { |
| 246 | at = *s.FinishedAt |
| 247 | } |
| 248 | p := &pauseLine{Tone: "negative", Icon: "circle-alert", Stop: copyText("work.stage.stopped")} |
| 249 | p.Strong, p.Rest = pause.Lead(copyText, "work.pause.lead.stopped", clockOrDay(now, at, loc), reason) |
| 250 | p.Next = copyText("work.pause.next.try_again") |
| 251 | if mine && s.Terminated { |
| 252 | p.Actions = []pauseAction{{Label: copyText("work.pause.try_again"), Post: "/conversation/tasks/" + t.ID + "/retry", Kind: "retry", Primary: true}} |
| 253 | } |
| 254 | return p |
| 255 | } |
| 256 | |
| 257 | // splitLead splits words already worded at their first colon, kept bold. |
| 258 | func splitLead(words string) (string, string) { |
| 259 | if i := strings.Index(words, ":"); i >= 0 { |
| 260 | return words[:i+1], words[i+1:] |
| 261 | } |
| 262 | return "", words |
| 263 | } |
| 264 | |
| 265 | // clockOrDay is a time the lead says it began or stopped: the clock today, |
| 266 | // the day and the clock before. |
| 267 | func clockOrDay(now, at time.Time, loc *time.Location) string { |
| 268 | n, a := now.In(loc), at.In(loc) |
| 269 | if n.Year() == a.Year() && n.YearDay() == a.YearDay() { |
| 270 | return a.Format("15:04") |
| 271 | } |
| 272 | return a.Format("Mon 15:04") |
| 273 | } |
| 274 | |
| 275 | // pausedTrack puts the pause on the stepper's current stop: its label |
| 276 | // (the stage kept for screen readers in Was), its tone, and still. |
| 277 | func pausedTrack(track []trackStop, p *pauseLine) { |
| 278 | if p == nil { |
| 279 | return |
| 280 | } |
| 281 | for i := range track { |
| 282 | if track[i].State != "current" { |
| 283 | continue |
| 284 | } |
| 285 | track[i].Was = track[i].Label |
| 286 | track[i].Label, track[i].Tone, track[i].Working, track[i].Live = p.Stop, p.Tone, false, false |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // applyPause puts the task's pause on its line as viewer sees it: the |
| 291 | // stepper's stop, still; the lead as its stage line, which the summary |
| 292 | // and the Tasks rows say. |
| 293 | func (v *taskLine) applyPause(viewer string, now time.Time, loc *time.Location) { |
| 294 | v.Pause = pauseOf(v.Task, viewer, now, loc) |
| 295 | if v.Pause == nil { |
| 296 | return |
| 297 | } |
| 298 | pausedTrack(v.Track, v.Pause) |
| 299 | v.Live = false |
| 300 | if lead := v.Pause.Strong + v.Pause.Rest; lead != "" { |
| 301 | v.StageLine, v.StageTag = lead, nil |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | // settingsTimeZone is the page's quiet post of the browser's zone |
| 306 | // (zone.js, once a session or when it changes): the person's zone for |
| 307 | // every time Equipo words for them. |
| 308 | func (a *application) settingsTimeZone(w http.ResponseWriter, r *http.Request) { |
| 309 | a.chatActionTo(w, r, "/conversation/settings", func(ctx context.Context, address string) error { |
| 310 | _, err := a.store.Chat().SetZone(ctx, address, r.PostFormValue("time_zone")) |
| 311 | return err |
| 312 | }) |
| 313 | } |
| 314 | |
| 315 | // defaultZone is the zone of a line drawn where the person's is not to |
| 316 | // hand (Europe/Dublin). |
| 317 | func defaultZone() *time.Location { |
| 318 | loc, _ := chat.LoadZone(chat.DefaultZone) |
| 319 | return loc |
| 320 | } |
| 321 | |
| 322 | // pauseReading adds to a pause for headroom how old the reading it waits |
| 323 | // on is (connectors spec rev 3.2 §6): "It carries on by itself then. · |
| 324 | // last reported 2 h ago". |
| 325 | func pauseReading(v *taskLine, conns []wire.Connection, now time.Time) { |
| 326 | if v.Pause == nil || v.Pause.Next == "" || v.Step == nil || v.Step.Hold == nil || v.Step.Hold.Reason != wire.HoldHeadroom || v.Step.Hold.NeedsPerson != "" { |
| 327 | return |
| 328 | } |
| 329 | h := v.Step.Hold |
| 330 | for _, c := range conns { |
| 331 | if c.LocalID == h.Connection && c.Provider == h.Provider { |
| 332 | if age := readingAge(c, now); age != "" { |
| 333 | v.Pause.Next = fmt.Sprintf(copyText("work.hold.reading"), v.Pause.Next, age) |
| 334 | } |
| 335 | return |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | // ErrAlreadyRunning: Run now was already asked for this hold (S2); the |
| 341 | // page says so rather than answering as if it were new. |
| 342 | var ErrAlreadyRunning = errors.New("run now is already on its way") |
| 343 | |