| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "hash/fnv" |
| 6 | "net/url" |
| 7 | "regexp" |
| 8 | "slices" |
| 9 | "strings" |
| 10 | "time" |
| 11 | "unicode" |
| 12 | "unicode/utf8" |
| 13 | |
| 14 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 15 | "amadan.net/oficina/equipo/internal/wire" |
| 16 | ) |
| 17 | |
| 18 | // The inner screens' pieces: people with initials, project monograms, |
| 19 | // the six-stop stage track, badges, the line saying who is at work, the |
| 20 | // source line, the timeline, the one-line summary and the decisions only |
| 21 | // the task's creator answers. Everything here is derived from API v1 |
| 22 | // resources, so a native app can draw the same from the same data. |
| 23 | |
| 24 | // personLine is a team member as a screen shows them. |
| 25 | type personLine struct { |
| 26 | Address, Name, Initials string |
| 27 | Hue int |
| 28 | You bool |
| 29 | } |
| 30 | |
| 31 | func personOf(address, viewer string) personLine { |
| 32 | name, initials := chat.DisplayName(address) |
| 33 | return personLine{Address: address, Name: name, Initials: initials, Hue: hueOf(address), You: address == viewer && viewer != ""} |
| 34 | } |
| 35 | |
| 36 | // hueOf is a stable hue for a name, so a person or project keeps its |
| 37 | // colour everywhere: one of 24 steps of 15 degrees, each a class in |
| 38 | // equipo.css ([data-h]), since the page's policy refuses inline styles. |
| 39 | func hueOf(s string) int { |
| 40 | h := fnv.New32a() |
| 41 | h.Write([]byte(s)) |
| 42 | return int(h.Sum32()%24) * 15 |
| 43 | } |
| 44 | |
| 45 | // monoLine is a project's letter monogram with a stable hue, until |
| 46 | // projects have icons. |
| 47 | type monoLine struct { |
| 48 | InTitle bool |
| 49 | Letter, Title string |
| 50 | Hue int |
| 51 | } |
| 52 | |
| 53 | func monoOf(title, id string) monoLine { |
| 54 | r, _ := utf8.DecodeRuneInString(strings.TrimSpace(title)) |
| 55 | if r == utf8.RuneError { |
| 56 | r = '?' |
| 57 | } |
| 58 | return monoLine{Letter: string(unicode.ToUpper(r)), Title: title, Hue: hueOf(id), InTitle: unicode.IsSymbol(r)} |
| 59 | } |
| 60 | |
| 61 | // trackStages are the six stops of the stage track; merging sits on |
| 62 | // Ready to merge, live. |
| 63 | var trackStages = []string{wire.StageNoted, wire.StageInvestigated, wire.StageInProgress, wire.StageInReview, wire.StageReadyToMerge, wire.StageDone} |
| 64 | |
| 65 | // exploreStages are an exploration's stops: it is reviewed, previewed |
| 66 | // and decided, never merged. The last three are track stops of their own |
| 67 | // (labels work.track.*), not task stages. |
| 68 | var exploreStages = []string{wire.StageNoted, wire.StageInvestigated, wire.StageInProgress, "reviewed", "preview", "decided"} |
| 69 | |
| 70 | // pageTask is a task as the pages show it: an exploration without the |
| 71 | // merge steps it had before it became one (they never apply to it), and |
| 72 | // whether any were left out. |
| 73 | func pageTask(t wire.Task) (wire.Task, bool) { |
| 74 | if t.Kind != wire.KindExplore { |
| 75 | return t, false |
| 76 | } |
| 77 | steps := make([]wire.Step, 0, len(t.Steps)) |
| 78 | for _, s := range t.Steps { |
| 79 | if s.Specialist != wire.SpecialistMerge { |
| 80 | steps = append(steps, s) |
| 81 | } |
| 82 | } |
| 83 | dropped := len(steps) != len(t.Steps) |
| 84 | t.Steps = steps |
| 85 | return t, dropped |
| 86 | } |
| 87 | |
| 88 | // exploreIndex is an exploration's current stop: Reviewed while Review |
| 89 | // reads it, Preview once approved and waiting for its direction, Decided |
| 90 | // once built as a change, dropped or ended. |
| 91 | func exploreIndex(t wire.Task) int { |
| 92 | switch { |
| 93 | case wire.FinalStage(t.Stage): |
| 94 | return 5 |
| 95 | case t.NeedsYou == wire.NeedsDirection: |
| 96 | return 4 |
| 97 | case t.Stage == wire.StageInReview: |
| 98 | return 3 |
| 99 | } |
| 100 | return max(slices.Index(exploreStages, t.Stage), 0) |
| 101 | } |
| 102 | |
| 103 | // trackStop is one stop: done, current or todo, the current one with a |
| 104 | // tone and, while someone is at work, the living mark. Working is the |
| 105 | // current stop while a specialist's step is queued or running: its |
| 106 | // marker pulses. |
| 107 | type trackStop struct { |
| 108 | // Was is the stage's own name when Label says it is paused, stopped |
| 109 | // or needs the person (paused work): screen readers hear both. |
| 110 | Was string |
| 111 | Stage, Label, State, Tone string |
| 112 | Live, Working bool |
| 113 | At *time.Time |
| 114 | } |
| 115 | |
| 116 | // badgeLine is a checks or review badge; Title is its tooltip. |
| 117 | type badgeLine struct { |
| 118 | Kind, Tone, Icon, Text, Title string |
| 119 | Live bool |
| 120 | } |
| 121 | |
| 122 | // chipLine says who is at work on a task, or who it waits for. |
| 123 | type chipLine struct { |
| 124 | Text, Icon string |
| 125 | Live bool |
| 126 | Since *time.Time |
| 127 | } |
| 128 | |
| 129 | // menuItem is one quiet action behind ⋯, for the task's creator. |
| 130 | type menuItem struct { |
| 131 | Action, Label, Note, Value string |
| 132 | Danger bool |
| 133 | } |
| 134 | |
| 135 | // decisionView is a decision card: why, the question, and the one-tap |
| 136 | // answers. Card is the merge card it answers, if any. |
| 137 | type decisionView struct { |
| 138 | CopyContinue bool |
| 139 | Need, Tone, Why, Ask, Hint string |
| 140 | Card *wire.Card |
| 141 | Choices []decisionChoice |
| 142 | Retry, Cancel, Fold bool |
| 143 | Talk bool |
| 144 | // Round is Try another round (fix rounds used, or a conflict Equipo |
| 145 | // already rebased). Said, Failed and Recommend are needs-you triage's |
| 146 | // last word from Review and hosted CI, and Equipo's recommendation. |
| 147 | Round bool |
| 148 | Said, Failed, Recommend string |
| 149 | // Direction: an exploration's choice. Link and LinkLabel open its |
| 150 | // preview or its branch; Note shows Try again's note field. |
| 151 | Link, LinkLabel string |
| 152 | // LinkText goes before the link, on its own line under the question. |
| 153 | LinkText string |
| 154 | // AltLink and AltLabel: a second link (the branch, when previews are |
| 155 | // not paired). |
| 156 | AltLink, AltLabel string |
| 157 | Note bool |
| 158 | // Gallery is what the newest Design attempt kept, by option; with |
| 159 | // more than one, BuildNote: Build this needs the note too. |
| 160 | Gallery *designGallery |
| 161 | BuildNote bool |
| 162 | // Recover settles a step stopped when its machine was removed. |
| 163 | Recover bool |
| 164 | // Mac: checks wait for a Mac; the card links setting one up. |
| 165 | Mac bool |
| 166 | // AddClaude links adding Claude to the Mac Engineering waits for |
| 167 | // (AddClaudeLabel says so). |
| 168 | AddClaude, AddClaudeLabel string |
| 169 | // Unsandboxed offers the creator Run outside the sandbox, behind a |
| 170 | // confirm with its warning, for checks waiting for a Mac's sandbox. |
| 171 | // SandboxHeld: not offered, because that Mac holds Claude (D1); Ask |
| 172 | // says so. |
| 173 | Unsandboxed *unsandboxedOffer |
| 174 | SandboxHeld bool |
| 175 | } |
| 176 | |
| 177 | // unsandboxedOffer is Run outside the sandbox for one attempt of a |
| 178 | // checks step waiting for the Mac's sandbox, and the warning its confirm |
| 179 | // shows. |
| 180 | type unsandboxedOffer struct { |
| 181 | Step string |
| 182 | Attempt int64 |
| 183 | Warning string |
| 184 | } |
| 185 | |
| 186 | // waitsForMac: approved and ready, with checks that could not run |
| 187 | // because they need macOS. |
| 188 | func waitsForMac(t wire.Task) bool { |
| 189 | return t.Stage == wire.StageReadyToMerge && t.Checks == wire.ChecksCannotRun && t.ChecksReason == "needs_macos" && t.Verdict == wire.VerdictApprove |
| 190 | } |
| 191 | |
| 192 | type decisionChoice struct { |
| 193 | Value, Label string |
| 194 | Primary bool |
| 195 | } |
| 196 | |
| 197 | // workGroups are the Tasks page's groups, in order. |
| 198 | var workGroups = []string{"needs", "progress", "ready", "done"} |
| 199 | |
| 200 | // lastStep is the newest step that drives the task: previews never do. |
| 201 | func lastStep(t wire.Task) *wire.Step { |
| 202 | for i := len(t.Steps) - 1; i >= 0; i-- { |
| 203 | if t.Steps[i].Specialist != wire.SpecialistPreview && t.Steps[i].Specialist != wire.SpecialistResearch && t.Steps[i].Specialist != wire.SpecialistPublish { |
| 204 | s := t.Steps[i] |
| 205 | return &s |
| 206 | } |
| 207 | } |
| 208 | return nil |
| 209 | } |
| 210 | |
| 211 | // liveStep is the newest step when it runs. |
| 212 | func liveStep(t wire.Task) *wire.Step { |
| 213 | if s := lastStep(t); s != nil && s.State == wire.StepRunning { |
| 214 | return s |
| 215 | } |
| 216 | return nil |
| 217 | } |
| 218 | |
| 219 | // workingNow reports whether a specialist is at work on t: its newest |
| 220 | // step queued or running, the task open and not waiting on the person. |
| 221 | func workingNow(t wire.Task) bool { |
| 222 | if len(t.Steps) == 0 || wire.FinalStage(t.Stage) || t.NeedsYou != "" || t.CopyHold || t.CopyCanSkip { |
| 223 | return false |
| 224 | } |
| 225 | s := t.Steps[len(t.Steps)-1].State |
| 226 | return s == wire.StepQueued || s == wire.StepRunning |
| 227 | } |
| 228 | |
| 229 | // stoppedStep is the newest step when it failed on a task still open. |
| 230 | func stoppedStep(t wire.Task) *wire.Step { |
| 231 | if s := lastStep(t); s != nil && s.State == wire.StepFailed && !wire.FinalStage(t.Stage) { |
| 232 | return s |
| 233 | } |
| 234 | return nil |
| 235 | } |
| 236 | |
| 237 | // stopReason is the reason whose words say why s stopped: an Engineering |
| 238 | // step that changed nothing after an earlier step pushed a candidate |
| 239 | // made no further changes to it (no_further_changes, the instance's |
| 240 | // own words: never a runner's reason); otherwise s's own reason. |
| 241 | func stopReason(t wire.Task, s wire.Step) string { |
| 242 | if s.Reason == "no_changes" && candidateBefore(t, s.ID) { |
| 243 | return "no_further_changes" |
| 244 | } |
| 245 | return s.Reason |
| 246 | } |
| 247 | |
| 248 | // candidateBefore reports whether a step before the step id pushed a |
| 249 | // candidate: an Engineering round, or a Copy review applied to one. |
| 250 | func candidateBefore(t wire.Task, id string) bool { |
| 251 | for _, s := range t.Steps { |
| 252 | if s.ID == id { |
| 253 | return false |
| 254 | } |
| 255 | if s.Pushed && s.CandidateSHA != "" && (s.Specialist == wire.SpecialistEngineering || s.Specialist == wire.SpecialistCopy && s.State == wire.StepDone) { |
| 256 | return true |
| 257 | } |
| 258 | } |
| 259 | return false |
| 260 | } |
| 261 | |
| 262 | // needsViewer reports whether the task waits on viewer's own decision: |
| 263 | // a stopped step, a review without verdict, the fix-round cap, a branch |
| 264 | // that moved, a conflict, or an open merge card. |
| 265 | func needsViewer(t wire.Task, viewer string, card *wire.Card) bool { |
| 266 | if t.Creator != viewer || wire.FinalStage(t.Stage) { |
| 267 | return false |
| 268 | } |
| 269 | if t.CopyHold || t.CopyCanSkip { |
| 270 | return true |
| 271 | } |
| 272 | switch t.NeedsYou { |
| 273 | case wire.NeedsRetry, wire.NeedsReviewBlocked, wire.NeedsFixRounds, wire.NeedsBranchMoved, wire.NeedsMergeConflict, wire.NeedsMachineRemoved, wire.NeedsMacSandbox, wire.NeedsMacRunner, wire.NeedsMacEngineering, wire.NeedsMacConnection, wire.NeedsMacUpdate, wire.NeedsDirection: |
| 274 | return true |
| 275 | case wire.NeedsMerge, wire.NeedsHeld: |
| 276 | return card != nil |
| 277 | } |
| 278 | return false |
| 279 | } |
| 280 | |
| 281 | // needsSomeone is the same for any creator, for "Waiting for Nora". |
| 282 | func needsSomeone(t wire.Task) bool { |
| 283 | if t.CopyHold || t.CopyCanSkip { |
| 284 | return !wire.FinalStage(t.Stage) |
| 285 | } |
| 286 | switch t.NeedsYou { |
| 287 | case wire.NeedsRetry, wire.NeedsReviewBlocked, wire.NeedsFixRounds, wire.NeedsBranchMoved, wire.NeedsMergeConflict, wire.NeedsMachineRemoved, wire.NeedsMacSandbox, wire.NeedsMacRunner, wire.NeedsMacEngineering, wire.NeedsMacConnection, wire.NeedsMacUpdate, wire.NeedsDirection: |
| 288 | return !wire.FinalStage(t.Stage) |
| 289 | } |
| 290 | return false |
| 291 | } |
| 292 | |
| 293 | func groupOf(t wire.Task, viewer string, card *wire.Card) string { |
| 294 | if t.Moving || t.Stopping { |
| 295 | return "progress" |
| 296 | } |
| 297 | switch { |
| 298 | case wire.FinalStage(t.Stage): |
| 299 | return "done" |
| 300 | case needsViewer(t, viewer, card): |
| 301 | return "needs" |
| 302 | case t.Stage == wire.StageReadyToMerge || t.Stage == wire.StageMerging: |
| 303 | return "ready" |
| 304 | } |
| 305 | return "progress" |
| 306 | } |
| 307 | |
| 308 | // trackIndex is the current stop: merging sits on Ready to merge, a |
| 309 | // cancelled task where it got to. |
| 310 | func trackIndex(t wire.Task) int { |
| 311 | switch t.Stage { |
| 312 | case wire.StageMerging: |
| 313 | return 4 |
| 314 | case wire.StageCancelled: |
| 315 | i := 0 |
| 316 | if len(t.Findings) > 0 { |
| 317 | i = 1 |
| 318 | } |
| 319 | for _, s := range t.Steps { |
| 320 | switch s.Specialist { |
| 321 | case wire.SpecialistEngineering: |
| 322 | i = max(i, 2) |
| 323 | case wire.SpecialistChecks, wire.SpecialistReview: |
| 324 | i = max(i, 3) |
| 325 | } |
| 326 | } |
| 327 | return i |
| 328 | } |
| 329 | return max(slices.Index(trackStages, t.Stage), 0) |
| 330 | } |
| 331 | |
| 332 | func (v *taskLine) trackOf(t wire.Task) { |
| 333 | cur, stages := trackIndex(t), trackStages |
| 334 | if t.Kind == wire.KindExplore { |
| 335 | cur, stages = exploreIndex(t), exploreStages |
| 336 | } |
| 337 | at := stageTimes(t) |
| 338 | for i, s := range stages { |
| 339 | label := copyText("work.stage." + s) |
| 340 | if i >= 3 && t.Kind == wire.KindExplore { |
| 341 | label = copyText("work.track." + s) |
| 342 | } |
| 343 | n := trackStop{Stage: s, Label: label, State: "todo"} |
| 344 | switch { |
| 345 | case i < cur: |
| 346 | n.State = "done" |
| 347 | case i == cur: |
| 348 | n.State, n.Tone, n.Live, n.Working = "current", v.Tone, v.Live, workingNow(t) |
| 349 | switch { |
| 350 | case t.Stage == wire.StageMerging: |
| 351 | n.Label = copyText("work.stage.merging") |
| 352 | case t.Stage == wire.StageCancelled: |
| 353 | n.Label = copyText("work.stage.cancelled") |
| 354 | case v.Stopped != nil: |
| 355 | n.Label = copyText("work.track.stopped") |
| 356 | } |
| 357 | } |
| 358 | if n.State != "todo" { |
| 359 | n.At = at[s] |
| 360 | } |
| 361 | v.Track = append(v.Track, n) |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | // stageTimes is when each stage was reached, as far as the steps say. |
| 366 | func stageTimes(t wire.Task) map[string]*time.Time { |
| 367 | at := map[string]*time.Time{} |
| 368 | created := t.CreatedAt |
| 369 | at[wire.StageNoted] = &created |
| 370 | if len(t.Findings) > 0 { |
| 371 | f := t.Findings[0].At |
| 372 | at[wire.StageInvestigated] = &f |
| 373 | } |
| 374 | for _, s := range t.Steps { |
| 375 | switch s.Specialist { |
| 376 | case wire.SpecialistEngineering: |
| 377 | if at[wire.StageInProgress] == nil { |
| 378 | at[wire.StageInProgress] = s.StartedAt |
| 379 | } |
| 380 | case wire.SpecialistChecks, wire.SpecialistReview: |
| 381 | if at[wire.StageInReview] == nil { |
| 382 | at[wire.StageInReview] = s.StartedAt |
| 383 | } |
| 384 | if s.Specialist == wire.SpecialistReview && s.State == wire.StepDone && s.CandidateSHA == t.CandidateSHA { |
| 385 | at[wire.StageReadyToMerge] = s.FinishedAt |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | if t.Merged != nil { |
| 390 | m := t.Merged.At |
| 391 | at[wire.StageDone] = &m |
| 392 | } else if t.Stage == wire.StageDone { |
| 393 | u := t.UpdatedAt |
| 394 | at[wire.StageDone] = &u |
| 395 | } |
| 396 | return at |
| 397 | } |
| 398 | |
| 399 | // ago is a quiet relative time. |
| 400 | func ago(now, t time.Time) string { |
| 401 | d := now.Sub(t) |
| 402 | switch { |
| 403 | case d < time.Minute: |
| 404 | return copyText("work.ago.now") |
| 405 | case d < time.Hour: |
| 406 | return fmt.Sprintf(copyText("work.ago.minutes"), int(d.Round(time.Minute)/time.Minute)) |
| 407 | case d < 24*time.Hour: |
| 408 | return fmt.Sprintf(copyText("work.ago.hours"), int(d.Round(time.Hour)/time.Hour)) |
| 409 | case d < 48*time.Hour: |
| 410 | return copyText("work.ago.yesterday") |
| 411 | } |
| 412 | return fmt.Sprintf(copyText("work.ago.days"), int(d/(24*time.Hour))) |
| 413 | } |
| 414 | |
| 415 | // span is how long something took: "40 s", "12 min", "1 h 4 min". |
| 416 | func span(d time.Duration) string { |
| 417 | s := int(d.Round(time.Second) / time.Second) |
| 418 | switch { |
| 419 | case s < 60: |
| 420 | return fmt.Sprintf(copyText("work.span.seconds"), max(s, 0)) |
| 421 | case s < 3600: |
| 422 | return fmt.Sprintf(copyText("work.span.minutes"), (s+30)/60) |
| 423 | } |
| 424 | return fmt.Sprintf(copyText("work.span.hours"), s/3600, (s%3600)/60) |
| 425 | } |
| 426 | |
| 427 | func took(s wire.Step) string { |
| 428 | if s.StartedAt == nil || s.FinishedAt == nil { |
| 429 | return "" |
| 430 | } |
| 431 | return span(s.FinishedAt.Sub(*s.StartedAt)) |
| 432 | } |
| 433 | |
| 434 | func short(sha string) string { |
| 435 | if len(sha) >= 7 { |
| 436 | return sha[:7] |
| 437 | } |
| 438 | return sha |
| 439 | } |
| 440 | |
| 441 | // activityText is a step's activity line in words: "Reading live.js". |
| 442 | func activityText(a wire.ActivityLine) string { |
| 443 | key := "work.activity." + a.Kind |
| 444 | if copyText(key) == "" { |
| 445 | key = "work.activity.other" |
| 446 | } |
| 447 | if a.Subject == "" || a.Kind == wire.ActivityOther { |
| 448 | return copyText("work.activity.working") |
| 449 | } |
| 450 | return fmt.Sprintf(copyText(key), a.Subject) |
| 451 | } |
| 452 | |
| 453 | // lastActivity is a running step's newest line, or "Working". |
| 454 | func lastActivity(s *wire.Step) string { |
| 455 | if s == nil || len(s.Activity) == 0 { |
| 456 | return copyText("work.activity.working") |
| 457 | } |
| 458 | return activityText(s.Activity[len(s.Activity)-1]) |
| 459 | } |
| 460 | |
| 461 | func checksBadge(t wire.Task, running *wire.Step) *badgeLine { |
| 462 | if running != nil && running.Specialist == wire.SpecialistChecks { |
| 463 | return &badgeLine{Kind: "checks", Tone: "live", Text: copyText("work.badge.checks"), Title: copyText("work.checks.running"), Live: true} |
| 464 | } |
| 465 | gate := "" |
| 466 | var code *int |
| 467 | for _, s := range t.Steps { |
| 468 | if s.Specialist == wire.SpecialistChecks && s.State == wire.StepDone && s.CandidateSHA == t.CandidateSHA { |
| 469 | gate, code = s.Gate, s.ExitCode |
| 470 | } |
| 471 | } |
| 472 | title := checksLine(t.Checks, t.ChecksReason, gate, code, t.ChecksSandbox) |
| 473 | switch t.Checks { |
| 474 | case wire.ChecksPassed: |
| 475 | return &badgeLine{Kind: "checks", Tone: "positive", Icon: "check", Text: outsideSandbox(copyText("work.badge.checks_passed"), t.ChecksSandbox), Title: title} |
| 476 | case wire.ChecksFailed: |
| 477 | return &badgeLine{Kind: "checks", Tone: "negative", Icon: "x", Text: outsideSandbox(copyText("work.badge.checks_failed"), t.ChecksSandbox), Title: title} |
| 478 | case wire.ChecksCannotRun: |
| 479 | text := copyText("work.badge.cannot." + t.ChecksReason) |
| 480 | if text == "" { |
| 481 | text = copyText("work.badge.cannot_run") |
| 482 | } |
| 483 | return &badgeLine{Kind: "checks", Tone: "neutral", Icon: "help-circle", Text: text, Title: title} |
| 484 | case wire.ChecksNone: |
| 485 | return &badgeLine{Kind: "checks", Tone: "quiet", Text: copyText("work.badge.no_checks"), Title: title} |
| 486 | } |
| 487 | return nil |
| 488 | } |
| 489 | |
| 490 | func reviewBadge(t wire.Task, running *wire.Step) *badgeLine { |
| 491 | if running != nil && running.Specialist == wire.SpecialistReview { |
| 492 | return &badgeLine{Kind: "review", Tone: "live", Text: copyText("work.badge.review"), Title: copyText("work.review.running"), Live: true} |
| 493 | } |
| 494 | title := copyText("work.badge.by." + t.ReviewLabel) |
| 495 | switch t.Verdict { |
| 496 | case wire.VerdictApprove: |
| 497 | return &badgeLine{Kind: "review", Tone: "positive", Icon: "check-circle", Text: copyText("work.badge.approved"), Title: title} |
| 498 | case wire.VerdictChanges: |
| 499 | return &badgeLine{Kind: "review", Tone: "warning", Icon: "alert-triangle", Text: copyText("work.badge.changes"), Title: title} |
| 500 | } |
| 501 | return nil |
| 502 | } |
| 503 | |
| 504 | // richOf adds what the inner screens draw to a task line. |
| 505 | func (v *taskLine) richOf(t wire.Task, viewer string, card *wire.Card, now time.Time) { |
| 506 | v.Mono = monoOf(t.Project, t.ProjectID) |
| 507 | v.Creator = personOf(t.Creator, viewer) |
| 508 | v.Mine = t.Creator == viewer |
| 509 | v.Card = card |
| 510 | v.Group = groupOf(t, viewer, card) |
| 511 | v.Stopped = stoppedStep(t) |
| 512 | running := liveStep(t) |
| 513 | v.Running = running |
| 514 | v.Live = running != nil || t.Coordinator != nil |
| 515 | switch { |
| 516 | case v.Stopped != nil: |
| 517 | v.Tone = "negative" |
| 518 | case t.Stage == wire.StageDone: |
| 519 | v.Tone = "positive" |
| 520 | case v.Group == "needs": |
| 521 | v.Tone = "attention" |
| 522 | } |
| 523 | v.trackOf(t) |
| 524 | v.Changed = t.UpdatedAt |
| 525 | v.Ago = ago(now, t.UpdatedAt) |
| 526 | if t.SourceAt != nil { |
| 527 | v.SourceAt = *t.SourceAt |
| 528 | } else { |
| 529 | v.SourceAt = t.CreatedAt |
| 530 | } |
| 531 | if v.Mine { |
| 532 | v.Source = fmt.Sprintf(copyText("work.source.yours"), v.SourceAt.Format("15:04")) |
| 533 | if t.SourceMessageID != "" { |
| 534 | v.SourceHref = "/conversation?message=" + url.QueryEscape(t.SourceMessageID) + "#m-" + url.PathEscape(t.SourceMessageID) |
| 535 | } |
| 536 | } else { |
| 537 | v.Source = fmt.Sprintf(copyText("work.source.theirs"), v.Creator.Name, v.SourceAt.Format("15:04")) |
| 538 | } |
| 539 | // Who is at work, or who it waits for. |
| 540 | switch { |
| 541 | case running != nil: |
| 542 | since := running.StartedAt |
| 543 | v.Chip = &chipLine{Text: fmt.Sprintf(copyText("work.chip.working"), stepName(*running)), Live: true, Since: since} |
| 544 | if running.Specialist == wire.SpecialistMerge { |
| 545 | v.Chip.Text = copyText("work.chip.merging") |
| 546 | } |
| 547 | case t.Coordinator != nil: |
| 548 | since := t.Coordinator.Since |
| 549 | v.Chip = &chipLine{Text: copyText("work.chip.coordinator_" + t.Coordinator.Kind), Live: true, Since: &since} |
| 550 | case lastStep(t) != nil && lastStep(t).State == wire.StepQueued: |
| 551 | v.Chip = &chipLine{Text: fmt.Sprintf(copyText("work.chip.waiting_for"), stepName(*lastStep(t))), Icon: "clock"} |
| 552 | case waitsForMac(t): |
| 553 | // Approved, and checks need a Mac nobody connected yet: it waits |
| 554 | // on a Mac, not on the person (who may still merge without). |
| 555 | v.Chip = &chipLine{Text: copyText("work.chip.waiting_mac"), Icon: "clock"} |
| 556 | v.WaitsOther = !v.Mine |
| 557 | case v.Group == "needs": |
| 558 | // Under Needs you the heading says it: no chip repeats it. |
| 559 | case !v.Mine && (needsSomeone(t) || t.Stage == wire.StageReadyToMerge && (t.Checks == wire.ChecksCannotRun || t.Checks == wire.ChecksNone)): |
| 560 | v.Chip = &chipLine{Text: fmt.Sprintf(copyText("work.chip.waiting_for"), v.Creator.Name)} |
| 561 | v.WaitsOther = true |
| 562 | } |
| 563 | if t.Stage == wire.StageDone { |
| 564 | if t.Merged != nil { |
| 565 | v.Badges = append(v.Badges, badgeLine{Kind: "merged", Tone: "quiet", Text: copyText("work.badge.merged"), Title: v.MergedLine}) |
| 566 | } |
| 567 | } else if v.NeedsYou != wire.NeedsBranchMoved { |
| 568 | if b := checksBadge(t, running); b != nil { |
| 569 | v.Badges = append(v.Badges, *b) |
| 570 | } |
| 571 | if b := reviewBadge(t, running); b != nil { |
| 572 | v.Badges = append(v.Badges, *b) |
| 573 | } |
| 574 | } |
| 575 | if t.Kind == wire.KindExplore { |
| 576 | v.Badges = append(v.Badges, badgeLine{Kind: "kind", Tone: "quiet", Text: copyText("work.badge.explore"), Title: copyText("work.badge.explore")}) |
| 577 | } |
| 578 | if t.DuplicateOf != nil && !wire.FinalStage(t.Stage) { |
| 579 | v.Dup = t.DuplicateOf |
| 580 | } |
| 581 | if t.Folded != nil { |
| 582 | v.StageLine = fmt.Sprintf(copyText("work.combined_into"), t.Folded.Into.Title) |
| 583 | for i := range v.Track { |
| 584 | if v.Track[i].State == "current" { |
| 585 | v.Track[i].Label = copyText("work.stage.cancelled") |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | if v.Mine && !wire.FinalStage(t.Stage) && t.Stage != wire.StageMerging { |
| 590 | if v.Dup != nil && !wire.FinalStage(v.Dup.Stage) && v.Dup.Creator == t.Creator { |
| 591 | v.Menu = append(v.Menu, menuItem{Action: "duplicate", Value: v.Dup.ID, Label: fmt.Sprintf(copyText("work.menu.fold"), v.Dup.Title), Note: copyText("work.menu.fold_note")}) |
| 592 | } |
| 593 | if v.Merge { |
| 594 | v.Menu = append(v.Menu, menuItem{Action: "merge", Label: copyText("work.merge_now")}) |
| 595 | } |
| 596 | if v.Retry { |
| 597 | v.Menu = append(v.Menu, menuItem{Action: "retry", Label: copyText("work.retry")}) |
| 598 | } |
| 599 | if liveStep(t) == nil { |
| 600 | // Its kind changes only at rest (the store says so too). |
| 601 | if t.Kind == wire.KindExplore { |
| 602 | v.Menu = append(v.Menu, menuItem{Action: "kind", Value: wire.KindChange, Label: copyText("work.menu.make_change")}) |
| 603 | } else { |
| 604 | v.Menu = append(v.Menu, menuItem{Action: "kind", Value: wire.KindExplore, Label: copyText("work.menu.make_explore")}) |
| 605 | } |
| 606 | } |
| 607 | if v.Cancel { |
| 608 | v.Menu = append(v.Menu, menuItem{Action: "cancel", Label: copyText("work.cancel"), Danger: true}) |
| 609 | } |
| 610 | } |
| 611 | if v.Group == "needs" { |
| 612 | v.Decision = decisionOf(t, *v) |
| 613 | } |
| 614 | if t.Moving || t.Stopping { |
| 615 | v.Menu = nil |
| 616 | v.Decision = nil |
| 617 | v.NeedsYou = "" |
| 618 | v.Chip = &chipLine{Text: placementPendingLine(t), Icon: "clock"} |
| 619 | } |
| 620 | v.rowOf() |
| 621 | } |
| 622 | |
| 623 | // rowAction is a Needs you row's one action: the decision's own answer |
| 624 | // when it takes one tap (Post, the handler its card posts to), else a |
| 625 | // link to the decision on the task page (Href). |
| 626 | type rowAction struct { |
| 627 | Kind, Label, Icon, Post, Href string |
| 628 | } |
| 629 | |
| 630 | // rowOf adds what a Tasks row says beyond the task's own lines: the |
| 631 | // status once (the Needs you heading already says "Needs you"), the |
| 632 | // detail line whole, the stepper in words, the From column's origin and |
| 633 | // the row's action, which then leaves the row's menu. |
| 634 | func (v *taskLine) rowOf() { |
| 635 | v.Say = v.StageLine |
| 636 | if v.Group == "needs" { |
| 637 | if rest, ok := strings.CutPrefix(v.Say, copyText("work.group.needs")+": "); ok { |
| 638 | v.Say = sentenceCase(rest) |
| 639 | } |
| 640 | } |
| 641 | var note []string |
| 642 | if v.Chip != nil { |
| 643 | note = append(note, v.Chip.Text+".") |
| 644 | } |
| 645 | for _, b := range v.Badges { |
| 646 | note = append(note, b.Text+".") |
| 647 | } |
| 648 | v.Note = strings.Join(note, " ") |
| 649 | for i, s := range v.Track { |
| 650 | if s.State == "current" { |
| 651 | v.StepLabel = fmt.Sprintf(copyText("work.row.step"), i+1, len(v.Track)) |
| 652 | } |
| 653 | } |
| 654 | v.SourceShort = fmt.Sprintf(copyText("work.source.short"), v.SourceAt.Format("15:04")) |
| 655 | v.Act = actionOf(v.ID, v.Decision, v.Stopped) |
| 656 | if v.Act != nil { |
| 657 | v.Menu = slices.DeleteFunc(v.Menu, func(m menuItem) bool { return m.Action == v.Act.Kind }) |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | // actionOf maps a decision to the row's action, with the handlers its |
| 662 | // card uses: Retry (Retry push when the push failed), Another round, |
| 663 | // Recover; What next? opens an exploration's direction card, and any |
| 664 | // other decision opens on the task page. |
| 665 | func actionOf(id string, d *decisionView, stopped *wire.Step) *rowAction { |
| 666 | if d == nil { |
| 667 | return nil |
| 668 | } |
| 669 | post := "/conversation/tasks/" + id + "/" |
| 670 | switch { |
| 671 | case d.Need == wire.NeedsDirection: |
| 672 | return &rowAction{Kind: "next", Label: copyText("work.row.what_next"), Icon: "split", Href: "/tasks/" + id + "#decide"} |
| 673 | case d.Retry: |
| 674 | label := copyText("work.retry") |
| 675 | if stopped != nil && stopped.Reason == "push_failed" { |
| 676 | label = copyText("work.row.retry_push") |
| 677 | } |
| 678 | return &rowAction{Kind: "retry", Label: label, Icon: "rotate-cw", Post: post + "retry"} |
| 679 | case d.Round: |
| 680 | return &rowAction{Kind: "round", Label: copyText("work.row.round"), Icon: "repeat", Post: post + "round"} |
| 681 | case d.Recover: |
| 682 | return &rowAction{Kind: "recover", Label: copyText("work.recover"), Icon: "rotate-ccw", Post: post + "recover"} |
| 683 | } |
| 684 | return &rowAction{Kind: "decide", Label: copyText("work.row.decide"), Icon: "arrow-right", Href: "/tasks/" + id + "#decide"} |
| 685 | } |
| 686 | |
| 687 | // decisionOf is the one card for what the creator must decide. |
| 688 | func decisionOf(t wire.Task, v taskLine) *decisionView { |
| 689 | if t.Moving || t.Stopping { |
| 690 | return nil |
| 691 | } |
| 692 | d := &decisionView{Need: t.NeedsYou} |
| 693 | if t.CopyHold || t.CopyCanSkip { |
| 694 | d.Need, d.Tone, d.Ask, d.Talk, d.CopyContinue = "copy", "attention", copyText("roster.copy_waiting"), true, t.CopyCanSkip |
| 695 | d.Why = copyText("copy.round_budget") |
| 696 | if s := lastStep(t); s != nil && s.Reason != "" { |
| 697 | d.Why = reasonLine(s.Reason, "work.reason.other") |
| 698 | } |
| 699 | return d |
| 700 | } |
| 701 | who := func(s *wire.Step) string { return taskStepName(t, s) } |
| 702 | switch t.NeedsYou { |
| 703 | case wire.NeedsHeld: |
| 704 | // The scheduler holds the step and asks (connectors spec §8): |
| 705 | // why it waits, Equipo's recommendation, Wait or Run it now. |
| 706 | if v.Hold != nil { |
| 707 | d.Why = v.Hold.Line |
| 708 | } |
| 709 | if c := v.Card; c != nil && c.Kind == wire.CardHeld { |
| 710 | d.Card, d.Ask = c, c.Question |
| 711 | for _, o := range c.Options { |
| 712 | d.Choices = append(d.Choices, decisionChoice{Value: o, Label: copyText("chat.card." + o), Primary: o == "run_now"}) |
| 713 | } |
| 714 | } |
| 715 | case wire.NeedsMerge: |
| 716 | c := v.Card |
| 717 | d.Card = c |
| 718 | if slices.Contains(c.Options, wire.ChoiceNotNow) { |
| 719 | d.Why = copyText("work.decide.adopt_why") |
| 720 | d.Ask = copyText("work.decide.adopt_ask") |
| 721 | } else { |
| 722 | reason := copyText("work.decide.reason." + t.ChecksReason) |
| 723 | switch { |
| 724 | case t.Checks == wire.ChecksNone: |
| 725 | reason = copyText("work.decide.no_gate") |
| 726 | case reason == "": |
| 727 | reason = copyText("work.decide.reason.checks_unavailable") |
| 728 | } |
| 729 | d.Why = fmt.Sprintf(copyText("work.decide.unchecked_why"), reason) |
| 730 | d.Ask = copyText("work.decide.unchecked_ask") |
| 731 | d.Hint = copyText("work.decide.unchecked_hint") |
| 732 | if waitsForMac(t) { |
| 733 | // Asked once: why, and what each answer does. |
| 734 | d.Why, d.Hint, d.Mac = copyText("work.decide.mac_why"), copyText("work.decide.mac_hint"), true |
| 735 | } |
| 736 | } |
| 737 | for _, o := range c.Options { |
| 738 | label := copyText("chat.card." + o) |
| 739 | if d.Mac { |
| 740 | // Merge authority's card offers wait_mac; its label here is |
| 741 | // the Mac decision's Wait. |
| 742 | label = copyText("work.decide.mac." + strings.TrimSuffix(o, "_mac")) |
| 743 | } |
| 744 | d.Choices = append(d.Choices, decisionChoice{Value: o, Label: label, Primary: o == wire.ChoiceMerge}) |
| 745 | } |
| 746 | case wire.NeedsRetry, wire.NeedsReviewBlocked: |
| 747 | d.Tone = "attention" |
| 748 | reason, attempt := "", int64(1) |
| 749 | if v.Stopped != nil { |
| 750 | reason, attempt = copyText("work.reason."+stopReason(t, *v.Stopped)), v.Stopped.Attempt |
| 751 | } |
| 752 | if reason == "" { |
| 753 | reason = copyText("work.reason.other") |
| 754 | } |
| 755 | d.Why = fmt.Sprintf(copyText("work.decide.stopped_why"), who(v.Stopped), reason, attempt) |
| 756 | d.Retry, d.Cancel = v.Retry, v.Cancel |
| 757 | d.Ask = copyText("work.decide.retry_ask") |
| 758 | if v.Stopped != nil && v.Stopped.Specialist == wire.SpecialistMerge && (v.Stopped.Reason == wire.ReasonMergeCredential || v.Stopped.Reason == "merge_forbidden") { |
| 759 | // Trying again cannot help until someone sets up access: |
| 760 | // the reason says what is missing, and nothing asks to try |
| 761 | // again (Retry stays, for once access is given). |
| 762 | d.Ask = "" |
| 763 | } |
| 764 | case wire.NeedsFixRounds: |
| 765 | d.Tone, d.Why, d.Ask, d.Talk, d.Cancel, d.Round = "attention", copyText("work.decide.fix_rounds_why"), copyText("work.decide.talk_ask"), true, v.Cancel, v.Mine |
| 766 | if t.ChecksReason == "base_checks_failed" { |
| 767 | d.Why = sentenceCase(copyText("chat.merge.not.base_failed")) + "." |
| 768 | } |
| 769 | if v.CopyRepeat != nil { |
| 770 | d.Why = copyText("copy.repeated") |
| 771 | } |
| 772 | for _, step := range v.Steps { |
| 773 | if step.Specialist == wire.SpecialistCopy && step.Reason == "copy_question" { |
| 774 | d.Why = copyText("work.reason.copy_question") |
| 775 | } |
| 776 | } |
| 777 | case wire.NeedsBranchMoved: |
| 778 | d.Tone, d.Why, d.Ask, d.Talk, d.Cancel = "attention", copyText("work.decide.branch_moved_why"), copyText("work.decide.talk_ask"), true, v.Cancel |
| 779 | case wire.NeedsMergeConflict: |
| 780 | d.Tone, d.Why, d.Ask, d.Talk, d.Cancel, d.Round = "attention", copyText("work.decide.conflict_why"), copyText("work.decide.talk_ask"), true, v.Cancel, v.Mine |
| 781 | case wire.NeedsMachineRemoved: |
| 782 | d.Tone, d.Why, d.Recover, d.Cancel = "attention", copyText("work.needs.machine_removed"), v.Recover, v.Cancel |
| 783 | case wire.NeedsDirection: |
| 784 | // The card's own question ("What next for ...?") leads, as in the |
| 785 | // conversation; what the answers are, the preview or the branch, |
| 786 | // and the answers themselves follow it. |
| 787 | d.Tone, d.Card, d.Ask, d.Note = "attention", v.Card, copyText("work.decide.direction_ask"), true |
| 788 | d.Why = copyText("work.needs.direction") |
| 789 | if v.Card != nil && v.Card.Question != "" { |
| 790 | d.Why = v.Card.Question |
| 791 | } |
| 792 | if dir := t.Direction; dir != nil { |
| 793 | switch { |
| 794 | case dir.PreviewURL != "" && strings.HasPrefix(dir.PreviewURL, "https://eq-"): |
| 795 | d.Link, d.LinkLabel = "/tasks/"+t.ID+"/preview", fmt.Sprintf(copyText("work.card.preview_tailnet"), previewHostLabel(dir.PreviewURL)) |
| 796 | case dir.PreviewURL != "": |
| 797 | d.Link, d.LinkLabel = "/tasks/"+t.ID+"/preview", fmt.Sprintf(copyText("work.card.preview_open"), previewHostLabel(dir.PreviewURL)) |
| 798 | case dir.PreviewsOff: |
| 799 | // Nothing paired: how to pair one, and the branch as before. |
| 800 | d.LinkText, d.Link, d.LinkLabel = copyText("work.direction.previews_off"), "/conversation/settings/machines#previews", copyText("work.direction.pair") |
| 801 | d.AltLink, d.AltLabel = dir.BranchURL, copyText("work.direction.branch") |
| 802 | default: |
| 803 | d.LinkText, d.Link, d.LinkLabel = copyText("work.direction.no_preview"), dir.BranchURL, copyText("work.direction.branch") |
| 804 | } |
| 805 | d.Gallery, d.BuildNote = galleryOf(t.ID, dir.Options), dir.NoteNeeded |
| 806 | if d.BuildNote { |
| 807 | d.Ask = copyText("work.decide.direction_ask_options") |
| 808 | } |
| 809 | } |
| 810 | if d.Card != nil { |
| 811 | for _, o := range d.Card.Options { |
| 812 | d.Choices = append(d.Choices, decisionChoice{Value: o, Label: copyText("chat.card." + o), Primary: o == wire.ChoiceBuild}) |
| 813 | } |
| 814 | } |
| 815 | case wire.NeedsMacRunner: |
| 816 | // Not a failure: Mac work waits for a Mac runner rather than run on |
| 817 | // Linux, and starts by itself once one is added. |
| 818 | d.Why, d.Ask, d.Mac, d.Cancel = copyText("work.decide.mac_runner_why"), copyText("work.decide.mac_runner_ask"), true, v.Cancel |
| 819 | case wire.NeedsMacEngineering: |
| 820 | // A Mac runner takes only checks so far: the change waits, and |
| 821 | // never goes to Linux. |
| 822 | d.Why, d.Ask, d.Cancel = copyText("work.decide.mac_engineering_why"), copyText("work.decide.mac_engineering_ask"), v.Cancel |
| 823 | case wire.NeedsMacConnection, wire.NeedsMacUpdate: |
| 824 | // A Mac fits but cannot make the change yet: what it lacks, as |
| 825 | // its Engineering status says now, or the update it (or the |
| 826 | // primary) needs. It starts by itself; Cancel stays. |
| 827 | if s := lastStep(t); s != nil { |
| 828 | key := "work.decide.mac_update_why" |
| 829 | if t.NeedsYou == wire.NeedsMacConnection { |
| 830 | state := wire.EngineeringNoConnection |
| 831 | if s.EngineeringStatus != nil && s.EngineeringStatus.State != wire.EngineeringReady { |
| 832 | state = s.EngineeringStatus.State |
| 833 | } |
| 834 | key = "work.decide.mac_connection_why." + state |
| 835 | if (state == wire.EngineeringNoConnection || state == wire.EngineeringSignedOut) && s.WaitingFor != "" { |
| 836 | // What it needs is Claude on that Mac (the one it waits |
| 837 | // for, placed nowhere yet): straight there. |
| 838 | d.AddClaude, d.AddClaudeLabel = "/conversation/settings/connections#machine-"+s.WaitingFor, fmt.Sprintf(copyText("chat.connections.add_to"), s.RunnerLabel) |
| 839 | } |
| 840 | } |
| 841 | d.Why, d.Ask, d.Cancel = fmt.Sprintf(copyText(key), s.RunnerLabel), fmt.Sprintf(copyText("work.decide.mac_wait_ask"), s.RunnerLabel), v.Cancel |
| 842 | } |
| 843 | case wire.NeedsMacSandbox: |
| 844 | // Not a failure: the checks wait, and run by themselves once the |
| 845 | // Mac's sandbox is proven. Only the creator may choose otherwise. |
| 846 | s := t.Steps[len(t.Steps)-1] |
| 847 | d.Why, d.Ask, d.Cancel = fmt.Sprintf(copyText("work.decide.sandbox_why"), s.RunnerLabel), copyText("work.decide.sandbox_ask"), v.Cancel |
| 848 | switch { |
| 849 | case s.SandboxChoice == wire.SandboxChoiceHeld: |
| 850 | // D1: the Mac holds Claude, so its checks run sandboxed or not |
| 851 | // at all. Nothing to choose; the card says why. |
| 852 | d.Ask, d.SandboxHeld = fmt.Sprintf(copyText("work.decide.sandbox_held"), s.RunnerLabel), true |
| 853 | case v.Mine && s.RunnerLabel != "" && s.SandboxChoice == wire.SandboxChoiceOffered: |
| 854 | d.Unsandboxed = &unsandboxedOffer{Step: s.ID, Attempt: s.Attempt, Warning: fmt.Sprintf(copyText("work.decide.unsandboxed_warning"), s.RunnerLabel)} |
| 855 | } |
| 856 | } |
| 857 | triageAdvice(d, t.Triage) |
| 858 | return d |
| 859 | } |
| 860 | |
| 861 | // triageAdvice adds needs-you triage's word to a decision: what Review |
| 862 | // and hosted CI said last, why Equipo did not act itself, and what it |
| 863 | // would do. |
| 864 | func triageAdvice(d *decisionView, tr *wire.TaskTriage) { |
| 865 | if tr == nil || tr.Kind != wire.TriageAsking { |
| 866 | return |
| 867 | } |
| 868 | if tr.Said != "" { |
| 869 | d.Said = fmt.Sprintf(copyText("work.triage.said"), tr.Said) |
| 870 | } |
| 871 | if len(tr.Failed) > 0 { |
| 872 | d.Failed = fmt.Sprintf(copyText("work.triage.failed"), strings.Join(tr.Failed, "; ")) |
| 873 | } |
| 874 | parts := []string{} |
| 875 | if tr.Why != "" { |
| 876 | parts = append(parts, copyText("work.triage.why."+tr.Why)) |
| 877 | } |
| 878 | if tr.Recommend != "" { |
| 879 | parts = append(parts, copyText("work.triage.recommend."+tr.Recommend)) |
| 880 | } |
| 881 | d.Recommend = strings.TrimSpace(strings.Join(parts, " ")) |
| 882 | } |
| 883 | |
| 884 | // triageLine is the task's line while needs-you triage acts on it. |
| 885 | func triageLine(tr *wire.TaskTriage) string { |
| 886 | if tr == nil { |
| 887 | return "" |
| 888 | } |
| 889 | switch tr.Kind { |
| 890 | case wire.TriageRetrying: |
| 891 | reason := copyText("work.reason." + tr.Reason) |
| 892 | if reason == "" { |
| 893 | reason = copyText("work.reason.other") |
| 894 | } |
| 895 | return fmt.Sprintf(copyText("work.triage.retrying"), reason) |
| 896 | case wire.TriageRebasing, wire.TriageRechecking: |
| 897 | return copyText("work.triage." + tr.Kind) |
| 898 | case wire.TriageWaiting: |
| 899 | // One wait so far: the first merge into an empty repository. |
| 900 | if tr.Reason == wire.ReasonMergeUnbornDefault { |
| 901 | return copyText("work.triage.waiting") |
| 902 | } |
| 903 | } |
| 904 | return "" |
| 905 | } |
| 906 | |
| 907 | // waitReason is why a task waits on someone, for "%s. It waits on %s.": |
| 908 | // a phrase of its own for each need, never the stage line's "Needs you:" |
| 909 | // (the template says who it waits on) nor a full stop of its own. |
| 910 | func waitReason(t wire.Task, v taskLine) string { |
| 911 | reason := copyText("work.waits." + t.NeedsYou) |
| 912 | if reason == "" { |
| 913 | reason = v.StageLine |
| 914 | } |
| 915 | return strings.TrimRight(strings.TrimSpace(reason), ".") |
| 916 | } |
| 917 | |
| 918 | // summaryOf is the task page's one line: where it stands, in words. |
| 919 | func summaryOf(t wire.Task, v taskLine) string { |
| 920 | if t.Moving || t.Stopping { |
| 921 | return placementPendingLine(t) |
| 922 | } |
| 923 | waiter := copyText("work.summary.you") |
| 924 | if !v.Mine { |
| 925 | waiter = v.Creator.Name |
| 926 | } |
| 927 | if v.Pause != nil { |
| 928 | // The pause's lead says why it waits or stopped (pause_view.go). |
| 929 | return "" |
| 930 | } |
| 931 | if v.Hold != nil { |
| 932 | // Held by the scheduler: why it waits, in its own words. |
| 933 | return v.Hold.Line + "." |
| 934 | } |
| 935 | switch { |
| 936 | case t.Stage == wire.StageDone && t.Merged != nil && t.Merged.WithoutChecks: |
| 937 | return fmt.Sprintf(copyText("work.summary.merged_unchecked"), t.Merged.Into) |
| 938 | case t.Stage == wire.StageDone && t.Merged != nil: |
| 939 | return fmt.Sprintf(copyText("work.summary.merged"), t.Merged.Into) |
| 940 | case t.Stage == wire.StageDone: |
| 941 | return copyText("work.summary.done") |
| 942 | case t.Folded != nil: |
| 943 | return fmt.Sprintf(copyText("work.combined_into"), t.Folded.Into.Title) + "." |
| 944 | case t.Stage == wire.StageCancelled && t.DuplicateOf != nil: |
| 945 | return fmt.Sprintf(copyText("work.summary.folded"), t.DuplicateOf.Title) |
| 946 | case t.Stage == wire.StageCancelled: |
| 947 | return copyText("work.summary.cancelled") |
| 948 | case t.Stage == wire.StageMerging: |
| 949 | return copyText("work.summary.merging") |
| 950 | case triageLine(t.Triage) != "": |
| 951 | return triageLine(t.Triage) |
| 952 | case v.Stopped != nil: |
| 953 | reason := copyText("work.reason." + stopReason(t, *v.Stopped)) |
| 954 | if reason == "" { |
| 955 | reason = copyText("work.reason.other") |
| 956 | } |
| 957 | return fmt.Sprintf(copyText("work.summary.stopped"), stepName(*v.Stopped), reason, waiter) |
| 958 | case v.Running != nil: |
| 959 | return fmt.Sprintf(copyText("work.summary.running"), stepName(*v.Running), strings.ToLower(lastActivity(v.Running))) |
| 960 | case waitsForMac(t) && v.Mine: |
| 961 | return copyText("work.summary.waiting_mac") |
| 962 | case t.NeedsYou == wire.NeedsMerge && v.Card != nil || !v.Mine && t.Stage == wire.StageReadyToMerge && (t.Checks == wire.ChecksCannotRun || t.Checks == wire.ChecksNone): |
| 963 | return fmt.Sprintf(copyText("work.summary.unchecked"), waiter) |
| 964 | case t.Stage == wire.StageReadyToMerge && t.Checks == wire.ChecksPassed: |
| 965 | return copyText("work.summary.ready") |
| 966 | case t.Stage == wire.StageReadyToMerge: |
| 967 | return copyText("work.summary.ready_waiting") |
| 968 | case t.NeedsYou == wire.NeedsMacSandbox: |
| 969 | return copyText("work.checks.waiting_sandbox") + "." |
| 970 | case needsSomeone(t): |
| 971 | return fmt.Sprintf(copyText("work.summary.needs"), waitReason(t, v), waiter) |
| 972 | case t.Coordinator != nil: |
| 973 | return copyText("work.summary.looking") |
| 974 | case lastStep(t) != nil && lastStep(t).State == wire.StepQueued: |
| 975 | return fmt.Sprintf(copyText("work.summary.queued"), stepName(*lastStep(t))) |
| 976 | case t.Stage == wire.StageInvestigated: |
| 977 | return copyText("work.summary.investigated") |
| 978 | case t.Stage == wire.StageNoted: |
| 979 | return copyText("work.summary.noted") |
| 980 | } |
| 981 | return v.StageLine + "." |
| 982 | } |
| 983 | |
| 984 | // timelineItem is one event on the task page's timeline: who or what (a |
| 985 | // short label with its badge, duration and time), one plain headline, |
| 986 | // and the rest folded behind Details (markdown, findings with their file |
| 987 | // and line, commits). |
| 988 | type timelineItem struct { |
| 989 | Kind, What, Headline string |
| 990 | Live bool |
| 991 | At, Since *time.Time |
| 992 | Took string |
| 993 | Badge *badgeLine |
| 994 | Lines []string |
| 995 | Decision bool |
| 996 | // Details: Markdown, Text lines, Findings, SHA notes; Open when there |
| 997 | // is anything to show. |
| 998 | Markdown string |
| 999 | Text []string |
| 1000 | Findings []wire.Finding |
| 1001 | // Specialist marks a Research step's entry; Research is its findings |
| 1002 | // once done (research_views.go). |
| 1003 | Specialist string |
| 1004 | Research *researchView |
| 1005 | // Design is a done Design attempt's kept captures, by option. |
| 1006 | Design *designGallery |
| 1007 | // RetryPost and RetryStep are a stopped step's own Retry (a failed |
| 1008 | // preview behind the merge), for the task's creator. |
| 1009 | RetryPost, RetryStep string |
| 1010 | } |
| 1011 | |
| 1012 | // HasDetails reports whether the entry has anything behind Details. |
| 1013 | func (it timelineItem) HasDetails() bool { |
| 1014 | return it.Markdown != "" || len(it.Text) > 0 || len(it.Findings) > 0 |
| 1015 | } |
| 1016 | |
| 1017 | func at(t time.Time) *time.Time { return &t } |
| 1018 | |
| 1019 | var ( |
| 1020 | pathToken = regexp.MustCompile("`[^`]*`|\\S*/\\S*|\\b[\\w.-]+\\.[A-Za-z]{1,6}(?::\\d+)?\\b|:\\d+\\b") |
| 1021 | mdNoise = regexp.MustCompile(`^[#>*\-\s]+|\*\*|__`) |
| 1022 | sentenceAt = regexp.MustCompile(`[.!?](?:\s|$)`) |
| 1023 | ) |
| 1024 | |
| 1025 | // headline is the one plain sentence an entry leads with: the |
| 1026 | // specialist's own when it wrote one (and it names no path), else the |
| 1027 | // first sentence of text that names no file, cut at a word boundary. |
| 1028 | func headline(explicit, text string) string { |
| 1029 | if h := strings.TrimSpace(explicit); h != "" && !pathToken.MatchString(h) { |
| 1030 | return clipWords(h, 110) |
| 1031 | } |
| 1032 | for _, line := range strings.Split(text, "\n") { |
| 1033 | if strings.HasPrefix(strings.TrimSpace(line), "#") || strings.HasPrefix(strings.TrimSpace(line), "```") { |
| 1034 | // A heading or code is never the headline. |
| 1035 | continue |
| 1036 | } |
| 1037 | line = strings.TrimSpace(mdNoise.ReplaceAllString(strings.TrimSpace(line), "")) |
| 1038 | if line == "" { |
| 1039 | continue |
| 1040 | } |
| 1041 | for _, s := range sentences(line) { |
| 1042 | if s = strings.TrimSpace(s); s != "" && !pathToken.MatchString(s) && !strings.HasSuffix(s, ":") { |
| 1043 | return clipWords(s, 110) |
| 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | return "" |
| 1048 | } |
| 1049 | |
| 1050 | func sentences(line string) []string { |
| 1051 | var out []string |
| 1052 | for line != "" { |
| 1053 | loc := sentenceAt.FindStringIndex(line) |
| 1054 | if loc == nil { |
| 1055 | return append(out, line) |
| 1056 | } |
| 1057 | out = append(out, line[:loc[0]+1]) |
| 1058 | line = strings.TrimSpace(line[loc[1]:]) |
| 1059 | } |
| 1060 | return out |
| 1061 | } |
| 1062 | |
| 1063 | // clipWords cuts s to at most n bytes at a word boundary. |
| 1064 | func clipWords(s string, n int) string { |
| 1065 | if len(s) <= n { |
| 1066 | return s |
| 1067 | } |
| 1068 | cut := s[:n] |
| 1069 | if i := strings.LastIndexByte(cut, ' '); i > n/2 { |
| 1070 | cut = cut[:i] |
| 1071 | } |
| 1072 | return strings.TrimRight(cut, " ,;:") + "…" |
| 1073 | } |
| 1074 | |
| 1075 | // checksHeadline is a checks result as a person reads it. |
| 1076 | func checksHeadline(outcome, reason string) string { |
| 1077 | if reason == "base_checks_failed" { |
| 1078 | return sentenceCase(copyText("chat.merge.not.base_failed")) + "." |
| 1079 | } |
| 1080 | switch outcome { |
| 1081 | case wire.ChecksPassed: |
| 1082 | return copyText("work.headline.checks_passed") |
| 1083 | case wire.ChecksFailed: |
| 1084 | return copyText("work.headline.checks_failed") |
| 1085 | case wire.ChecksNone: |
| 1086 | return copyText("work.headline.checks_none") |
| 1087 | } |
| 1088 | if s := copyText("work.headline.checks_" + reason); s != "" { |
| 1089 | return s |
| 1090 | } |
| 1091 | return copyText("work.headline.checks_cannot") |
| 1092 | } |
| 1093 | |
| 1094 | // reviewHeadline is Review's verdict as a person reads it. |
| 1095 | func reviewHeadline(s wire.Step) string { |
| 1096 | if h := headline(s.Headline, ""); h != "" { |
| 1097 | return h |
| 1098 | } |
| 1099 | switch { |
| 1100 | case s.Verdict == wire.VerdictApprove && len(s.Findings) == 0: |
| 1101 | return copyText("work.headline.approved") |
| 1102 | case s.Verdict == wire.VerdictApprove: |
| 1103 | return fmt.Sprintf(copyText("work.headline.approved_notes"), len(s.Findings)) |
| 1104 | } |
| 1105 | if h := headline("", s.Summary); h != "" { |
| 1106 | return h |
| 1107 | } |
| 1108 | return copyText("work.headline.changes") |
| 1109 | } |
| 1110 | |
| 1111 | // timelineOf is what happened, oldest first. |
| 1112 | func timelineOf(t wire.Task, v taskLine) []timelineItem { |
| 1113 | var out []timelineItem |
| 1114 | noted := copyText("work.tl.noted_you") |
| 1115 | if !v.Mine { |
| 1116 | noted = fmt.Sprintf(copyText("work.tl.noted_by"), v.Creator.Name) |
| 1117 | } |
| 1118 | out = append(out, timelineItem{Kind: "noted", What: noted, Headline: v.Source, At: at(t.CreatedAt)}) |
| 1119 | if len(t.Findings) > 0 { |
| 1120 | h := "" |
| 1121 | for _, f := range t.Findings { |
| 1122 | if h = headline(f.Headline, f.Text); h != "" { |
| 1123 | break |
| 1124 | } |
| 1125 | } |
| 1126 | out = append(out, timelineItem{Kind: "finding", What: copyText("work.tl.looked"), Headline: h, Findings: t.Findings, At: at(t.Findings[0].At)}) |
| 1127 | } |
| 1128 | if t.Coordinator != nil && t.Coordinator.Kind == wire.CoordinatorLooking { |
| 1129 | out = append(out, timelineItem{Kind: "live", Live: true, What: copyText("work.chip.coordinator_looking"), Since: at(t.Coordinator.Since)}) |
| 1130 | } |
| 1131 | on := func(sha string) string { return fmt.Sprintf(copyText("work.tl.on_sha"), short(sha)) } |
| 1132 | for _, s := range t.Steps { |
| 1133 | name := stepName(s) |
| 1134 | title := fmt.Sprintf(copyText("work.tl.attempt"), name, s.Attempt) |
| 1135 | if s.Specialist == wire.SpecialistEngineering { |
| 1136 | if s.Rebase { |
| 1137 | title += " (" + copyText("work.rebase") + ")" |
| 1138 | } else if s.Round > 0 { |
| 1139 | title += " (" + fmt.Sprintf(copyText("work.round"), s.Round) + ")" |
| 1140 | } |
| 1141 | } else if s.Attempt <= 1 { |
| 1142 | title = name |
| 1143 | } |
| 1144 | if s.Specialist == wire.SpecialistResearch { |
| 1145 | out = append(out, researchItem(t, s, v, title)) |
| 1146 | continue |
| 1147 | } |
| 1148 | if s.RetriedBy == wire.RetriedByCoordinator { |
| 1149 | // The coordinator's retry_step started this attempt. |
| 1150 | out = append(out, timelineItem{Kind: "note", What: fmt.Sprintf(copyText("work.tl.coordinator_retried"), name)}) |
| 1151 | } |
| 1152 | if s.Specialist == wire.SpecialistMerge && (s.State == wire.StepQueued || s.State == wire.StepRunning || s.State == wire.StepDone) { |
| 1153 | // The merging and merged lines say it. |
| 1154 | continue |
| 1155 | } |
| 1156 | switch s.State { |
| 1157 | case wire.StepQueued: |
| 1158 | it := timelineItem{Kind: "queued", What: fmt.Sprintf(copyText("work.chip.waiting_for"), name)} |
| 1159 | if s.Reason == "copy_update_required" { |
| 1160 | it.Headline = copyText("work.reason.copy_update_required") |
| 1161 | } |
| 1162 | if s.Placement == wire.PlacementResearch { |
| 1163 | it.Headline = copyText("work.research.held") |
| 1164 | } |
| 1165 | if s.Placement == wire.PlacementDesign || s.Placement == wire.PlacementDesignLimit { |
| 1166 | // Why Design waits: its runner (too old, its page renderer, |
| 1167 | // no Claude connection there) or the person (the limit). |
| 1168 | it.Headline = copyText("work.design." + s.Reason) |
| 1169 | } |
| 1170 | out = append(out, it) |
| 1171 | continue |
| 1172 | case wire.StepRunning: |
| 1173 | if s.Specialist == wire.SpecialistPreview && s.Preview != nil { |
| 1174 | out = append(out, previewBuiltItem(*s.Preview)) |
| 1175 | } |
| 1176 | it := timelineItem{Kind: "step", Live: true, What: title, Since: s.StartedAt} |
| 1177 | for _, a := range s.Activity { |
| 1178 | it.Lines = append(it.Lines, activityText(a)) |
| 1179 | } |
| 1180 | if len(it.Lines) == 0 { |
| 1181 | it.Lines = []string{copyText("work.activity.working")} |
| 1182 | } |
| 1183 | out = append(out, it) |
| 1184 | continue |
| 1185 | case wire.StepFailed: |
| 1186 | reason := copyText("work.reason." + stopReason(t, s)) |
| 1187 | if reason == "" { |
| 1188 | reason = copyText("work.reason.other") |
| 1189 | } |
| 1190 | it := timelineItem{Kind: "stopped", What: fmt.Sprintf(copyText("work.tl.stopped"), title), Headline: sentenceCase(reason) + ".", At: s.FinishedAt} |
| 1191 | if s.LogTail != "" { |
| 1192 | it.Markdown = "```\n" + s.LogTail + "\n```" |
| 1193 | } |
| 1194 | if previewRetry(t, s, v.Mine) { |
| 1195 | it.RetryPost, it.RetryStep = "/conversation/tasks/"+t.ID+"/retry", s.ID |
| 1196 | } |
| 1197 | out = append(out, it) |
| 1198 | continue |
| 1199 | case wire.StepCancelled: |
| 1200 | it := timelineItem{Kind: "stopped", What: fmt.Sprintf(copyText("work.tl.cancelled"), title), At: s.FinishedAt} |
| 1201 | if previewRetry(t, s, v.Mine) { |
| 1202 | it.RetryPost, it.RetryStep = "/conversation/tasks/"+t.ID+"/retry", s.ID |
| 1203 | } |
| 1204 | out = append(out, it) |
| 1205 | continue |
| 1206 | } |
| 1207 | switch s.Specialist { |
| 1208 | case wire.SpecialistCopy: |
| 1209 | if s.Pushed && s.Reason == "proposal_failed" { |
| 1210 | out = append(out, timelineItem{Kind: "step", What: title, At: s.FinishedAt, Text: []string{reasonLine(s.Reason, "")}}) |
| 1211 | } |
| 1212 | case wire.SpecialistEngineering: |
| 1213 | it := timelineItem{Kind: "step", What: title, Took: took(s), Headline: headline(s.Headline, s.Summary), Markdown: s.Summary, At: s.FinishedAt} |
| 1214 | if s.Design != nil { |
| 1215 | it.Design = galleryOf(t.ID, s.Design.Options) |
| 1216 | } |
| 1217 | if s.Pushed { |
| 1218 | it.Text = append(it.Text, fmt.Sprintf(copyText("work.tl.pushed"), short(s.CandidateSHA))) |
| 1219 | // step_timeout: it ran out of time; this is its work so far. |
| 1220 | if s.Reason == "proposal_failed" || s.Reason == "step_timeout" { |
| 1221 | it.Text = append(it.Text, reasonLine(s.Reason, "")) |
| 1222 | } |
| 1223 | } |
| 1224 | out = append(out, it) |
| 1225 | case wire.SpecialistChecks: |
| 1226 | one := t |
| 1227 | one.Checks, one.ChecksReason, one.CandidateSHA, one.ChecksSandbox = s.Checks, s.Reason, s.CandidateSHA, s.ChecksSandbox |
| 1228 | it := timelineItem{Kind: "step", What: title, Badge: checksBadge(one, nil), Took: took(s), Headline: checksHeadline(s.Checks, s.Reason), At: s.FinishedAt} |
| 1229 | it.Text = append(it.Text, on(s.CandidateSHA)) |
| 1230 | if line := checksLine(s.Checks, s.Reason, s.Gate, s.ExitCode, s.ChecksSandbox); line != "" { |
| 1231 | it.Text = append(it.Text, line+".") |
| 1232 | } |
| 1233 | if s.Checks == wire.ChecksFailed && s.LogTail != "" { |
| 1234 | it.Markdown = "```\n" + s.LogTail + "\n```" |
| 1235 | } |
| 1236 | out = append(out, it) |
| 1237 | case wire.SpecialistPublish: |
| 1238 | if s.Publish == nil { |
| 1239 | continue |
| 1240 | } |
| 1241 | it := timelineItem{Kind: "step", What: copyText("work.tl.publish_unconfirmed"), Took: took(s), At: s.FinishedAt, Text: []string{on(s.CandidateSHA)}} |
| 1242 | if s.Publish.URL != "" { |
| 1243 | it.Markdown = "[" + s.Publish.URL + "](" + s.Publish.URL + ")" |
| 1244 | } |
| 1245 | if s.Publish.Outcome == wire.PublishPublished { |
| 1246 | it.What = copyText("work.tl.published") |
| 1247 | } |
| 1248 | out = append(out, it) |
| 1249 | case wire.SpecialistPreview: |
| 1250 | if s.Preview == nil { |
| 1251 | continue |
| 1252 | } |
| 1253 | out = append(out, previewBuiltItem(*s.Preview)) |
| 1254 | it := timelineItem{Kind: "step", What: copyText("work.tl.preview_unconfirmed"), Took: took(s), At: s.FinishedAt, Text: []string{on(s.CandidateSHA)}} |
| 1255 | if s.Preview.Outcome == wire.PreviewDeployed && s.Preview.URL != "" { |
| 1256 | it.What, it.Markdown = copyText("work.tl.preview_ready"), "["+previewHostLabel(s.Preview.URL)+"](/tasks/"+t.ID+"/preview)" |
| 1257 | } |
| 1258 | if previewRetry(t, s, v.Mine) { |
| 1259 | // Unconfirmed, or its link is gone: Retry, as for a failed one. |
| 1260 | it.RetryPost, it.RetryStep = "/conversation/tasks/"+t.ID+"/retry", s.ID |
| 1261 | } |
| 1262 | out = append(out, it) |
| 1263 | case wire.SpecialistReview: |
| 1264 | one := t |
| 1265 | one.Verdict, one.ReviewLabel = s.Verdict, s.ReviewLabel |
| 1266 | it := timelineItem{Kind: "step", What: title, Badge: reviewBadge(one, nil), Took: took(s), Headline: reviewHeadline(s), Findings: s.Findings, Markdown: s.Summary, At: s.FinishedAt} |
| 1267 | it.Text = append(it.Text, on(s.CandidateSHA)) |
| 1268 | if by := copyText("work.badge.by." + s.ReviewLabel); by != "" { |
| 1269 | it.Text = append(it.Text, by+".") |
| 1270 | } |
| 1271 | out = append(out, it) |
| 1272 | } |
| 1273 | } |
| 1274 | if v.MergeDropped { |
| 1275 | // Its merge history from before it became an exploration, once. |
| 1276 | out = append(out, timelineItem{Kind: "note", What: copyText("work.tl.merge_dropped")}) |
| 1277 | } |
| 1278 | if t.Stage == wire.StageReadyToMerge || t.Stage == wire.StageMerging || t.Stage == wire.StageDone && t.Merged != nil { |
| 1279 | var when *time.Time |
| 1280 | for _, s := range t.Steps { |
| 1281 | if s.Specialist == wire.SpecialistReview && s.State == wire.StepDone && s.CandidateSHA == t.CandidateSHA { |
| 1282 | when = s.FinishedAt |
| 1283 | } |
| 1284 | } |
| 1285 | it := timelineItem{Kind: "ready", What: copyText("work.stage.ready_to_merge"), At: when} |
| 1286 | switch { |
| 1287 | case t.Stage != wire.StageReadyToMerge: |
| 1288 | case v.Decision != nil && v.Decision.Card != nil: |
| 1289 | it.Decision = true |
| 1290 | case v.Card != nil || t.Checks == wire.ChecksCannotRun || t.Checks == wire.ChecksNone: |
| 1291 | it.Headline = fmt.Sprintf(copyText("work.tl.ready_waits"), v.Creator.Name) |
| 1292 | if v.Mine { |
| 1293 | it.Headline = copyText("work.tl.ready_waits_you") |
| 1294 | } |
| 1295 | default: |
| 1296 | it.Headline = copyText("work.tl.ready_next") |
| 1297 | } |
| 1298 | out = append(out, it) |
| 1299 | } |
| 1300 | if t.Stage == wire.StageMerging { |
| 1301 | it := timelineItem{Kind: "merging", Live: true, What: copyText("work.chip.merging")} |
| 1302 | if t.Coordinator != nil { |
| 1303 | it.Since = at(t.Coordinator.Since) |
| 1304 | } |
| 1305 | out = append(out, it) |
| 1306 | } |
| 1307 | if t.Merged != nil { |
| 1308 | told := copyText("work.summary.you") |
| 1309 | if !v.Mine { |
| 1310 | told = v.Creator.Name |
| 1311 | } |
| 1312 | out = append(out, timelineItem{Kind: "merged", What: fmt.Sprintf(copyText("work.tl.merged"), t.Merged.Into), Headline: fmt.Sprintf(copyText("work.tl.merged_told"), told), Text: []string{fmt.Sprintf(copyText("work.tl.merged_as"), short(t.Merged.SHA))}, At: at(t.Merged.At)}) |
| 1313 | } else if t.Stage == wire.StageDone { |
| 1314 | out = append(out, timelineItem{Kind: "merged", What: copyText("work.stage.done"), At: at(t.UpdatedAt)}) |
| 1315 | } |
| 1316 | if t.Folded != nil { |
| 1317 | out = append(out, timelineItem{Kind: "stopped", What: fmt.Sprintf(copyText("work.combined_into"), t.Folded.Into.Title), At: at(t.Folded.At)}) |
| 1318 | } else if t.Stage == wire.StageCancelled { |
| 1319 | it := timelineItem{Kind: "stopped", What: copyText("work.stage.cancelled"), At: at(t.UpdatedAt)} |
| 1320 | if t.DuplicateOf != nil { |
| 1321 | it.Headline = fmt.Sprintf(copyText("work.tl.folded"), t.DuplicateOf.Title) |
| 1322 | } |
| 1323 | out = append(out, it) |
| 1324 | } |
| 1325 | return out |
| 1326 | } |
| 1327 | |
| 1328 | // previewRetry reports whether the person may run a preview again from |
| 1329 | // its own entry: theirs, ended without a link up now (failed, cancelled, |
| 1330 | // unconfirmed, expired, or a devbox link gone with its task), the task's |
| 1331 | // newest preview, of the candidate the task stands on, or on a done task |
| 1332 | // any (it builds the merged commit), the task not cancelled. The store |
| 1333 | // decides again (previewRetryCheck). |
| 1334 | func previewRetry(t wire.Task, s wire.Step, mine bool) bool { |
| 1335 | if !mine || s.Specialist != wire.SpecialistPreview || !wire.StepTerminal(s.State) || !s.Terminated || t.Stage == wire.StageCancelled || previewShown(t, s, time.Now()) && !devboxAfterDone(t, s) { |
| 1336 | return false |
| 1337 | } |
| 1338 | if t.Stage != wire.StageDone && (t.CandidateSHA == "" || s.CandidateSHA != t.CandidateSHA) { |
| 1339 | return false |
| 1340 | } |
| 1341 | for _, o := range t.Steps { |
| 1342 | if o.Specialist == wire.SpecialistPreview && o.Index > s.Index { |
| 1343 | return false |
| 1344 | } |
| 1345 | } |
| 1346 | return true |
| 1347 | } |
| 1348 | |
| 1349 | // previewShown reports whether a preview step's link is up now: deployed, |
| 1350 | // before any expiry, and a devbox link only while its task runs or one |
| 1351 | // that came up after its task was done (the store's previewUp, without a |
| 1352 | // Publish hold, which only keeps a link up longer). |
| 1353 | func previewShown(t wire.Task, s wire.Step, now time.Time) bool { |
| 1354 | p := s.Preview |
| 1355 | if s.State != wire.StepDone || p == nil || p.Outcome != wire.PreviewDeployed || p.URL == "" { |
| 1356 | return false |
| 1357 | } |
| 1358 | if p.ExpiresAt != nil && !p.ExpiresAt.After(now) { |
| 1359 | return false |
| 1360 | } |
| 1361 | if strings.HasPrefix(p.App, "eq-") && wire.FinalStage(t.Stage) { |
| 1362 | return devboxAfterDone(t, s) |
| 1363 | } |
| 1364 | return true |
| 1365 | } |
| 1366 | |
| 1367 | // devboxAfterDone: a devbox link that came up after its task was done (a |
| 1368 | // preview run again on a merged task); any other comes down with the |
| 1369 | // task. It stays up until the task's next preview replaces it. |
| 1370 | func devboxAfterDone(t wire.Task, s wire.Step) bool { |
| 1371 | return s.Preview != nil && strings.HasPrefix(s.Preview.App, "eq-") && t.Stage == wire.StageDone && t.DoneAt != nil && s.FinishedAt != nil && s.FinishedAt.After(*t.DoneAt) |
| 1372 | } |
| 1373 | |
| 1374 | // sentenceCase capitalises a reason fragment for a line of its own. |
| 1375 | func sentenceCase(s string) string { |
| 1376 | if s == "" { |
| 1377 | return s |
| 1378 | } |
| 1379 | r, n := utf8.DecodeRuneInString(s) |
| 1380 | return string(unicode.ToUpper(r)) + s[n:] |
| 1381 | } |
| 1382 | |
| 1383 | // sortGroup orders a group's tasks by their last change, newest first. |
| 1384 | func sortGroup(ts []taskLine) { |
| 1385 | slices.SortStableFunc(ts, func(a, b taskLine) int { return b.Changed.Compare(a.Changed) }) |
| 1386 | } |
| 1387 | |
| 1388 | // taskGroup is one titled group of task rows. More is how many Done |
| 1389 | // rows wait behind "Show N more", which MoreHref opens. |
| 1390 | type taskGroup struct { |
| 1391 | Key string |
| 1392 | Tasks []taskLine |
| 1393 | More int |
| 1394 | MoreHref string |
| 1395 | } |
| 1396 | |
| 1397 | // Count is the group's tasks, shown or folded. |
| 1398 | func (g taskGroup) Count() int { return len(g.Tasks) + g.More } |
| 1399 | |
| 1400 | // doneShown is how many Done rows a list shows before "Show N more". |
| 1401 | const doneShown = 1 |
| 1402 | |
| 1403 | // foldDone keeps Done to its newest row and links the rest, at the same |
| 1404 | // path and filters with done=all, unless done=all asked for them. |
| 1405 | func foldDone(gs []taskGroup, u *url.URL) { |
| 1406 | q := u.Query() |
| 1407 | if q.Get("done") == "all" { |
| 1408 | return |
| 1409 | } |
| 1410 | q.Set("done", "all") |
| 1411 | for i := range gs { |
| 1412 | if gs[i].Key == "done" && len(gs[i].Tasks) > doneShown { |
| 1413 | gs[i].More = len(gs[i].Tasks) - doneShown |
| 1414 | gs[i].Tasks = gs[i].Tasks[:doneShown] |
| 1415 | gs[i].MoreHref = u.Path + "?" + q.Encode() + "#done" |
| 1416 | } |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | func groupTasks(ts []taskLine) []taskGroup { |
| 1421 | var out []taskGroup |
| 1422 | for _, g := range workGroups { |
| 1423 | var in []taskLine |
| 1424 | for _, t := range ts { |
| 1425 | if t.Group == g { |
| 1426 | in = append(in, t) |
| 1427 | } |
| 1428 | } |
| 1429 | if len(in) > 0 { |
| 1430 | sortGroup(in) |
| 1431 | out = append(out, taskGroup{Key: g, Tasks: in}) |
| 1432 | } |
| 1433 | } |
| 1434 | return out |
| 1435 | } |
| 1436 | |
| 1437 | // spreadPart is one colour of a project's task spread. |
| 1438 | type spreadPart struct { |
| 1439 | Key string |
| 1440 | N int |
| 1441 | // Width is the part's share in whole percent ([data-w] in equipo.css). |
| 1442 | Width int |
| 1443 | Title string |
| 1444 | } |
| 1445 | |
| 1446 | // repoLine is a repository attached to a project, with its newest commit. |
| 1447 | type repoLine struct { |
| 1448 | wire.Attachment |
| 1449 | Ago, URL string |
| 1450 | } |
| 1451 | |
| 1452 | var stackIcon = map[string]bool{"Swift": true, "SwiftUI": true, "Android": true} |
| 1453 | |
| 1454 | func projectLineOf(p wire.Project, tasks []taskLine, viewer string, now time.Time) projectLine { |
| 1455 | line := projectLine{Project: p, Mono: monoOf(p.Title, p.ID), StackLine: strings.Join(p.Stack, ", ")} |
| 1456 | n := map[string]int{} |
| 1457 | var last time.Time |
| 1458 | for _, t := range tasks { |
| 1459 | if t.ProjectID != p.ID { |
| 1460 | continue |
| 1461 | } |
| 1462 | line.Tasks++ |
| 1463 | if t.Group == "needs" { |
| 1464 | line.NeedsYou++ |
| 1465 | } |
| 1466 | k := t.Group |
| 1467 | if k != "done" && (k == "needs" || needsSomeone(t.Task) || t.WaitsOther) { |
| 1468 | k = "needs" |
| 1469 | } |
| 1470 | n[k]++ |
| 1471 | if t.Changed.After(last) { |
| 1472 | last = t.Changed |
| 1473 | } |
| 1474 | } |
| 1475 | if !last.IsZero() { |
| 1476 | line.Last, line.LastAt = ago(now, last), last |
| 1477 | } |
| 1478 | line.Open = line.Tasks - n["done"] |
| 1479 | line.Short, line.DescLine = shortDescription(p.Description), wholeWords(p.Description) |
| 1480 | for _, k := range workGroups { |
| 1481 | if n[k] == 0 { |
| 1482 | continue |
| 1483 | } |
| 1484 | w := fmt.Sprintf(copyText("work.spread."+k), n[k]) |
| 1485 | line.Spread = append(line.Spread, spreadPart{Key: k, N: n[k], Width: max(1, int(float64(n[k])*100/float64(line.Tasks)+0.5)), Title: w}) |
| 1486 | } |
| 1487 | // The words lead with the state most tasks are in (waiting first on a |
| 1488 | // tie); the bar keeps one order, so rows compare. |
| 1489 | lead := slices.Clone(line.Spread) |
| 1490 | slices.SortStableFunc(lead, func(a, b spreadPart) int { return b.N - a.N }) |
| 1491 | var words []string |
| 1492 | for _, sp := range lead { |
| 1493 | words = append(words, sp.Title) |
| 1494 | } |
| 1495 | line.Words = strings.Join(words, ", ") |
| 1496 | for _, who := range p.People { |
| 1497 | line.People = append(line.People, personOf(who.Address, viewer)) |
| 1498 | } |
| 1499 | for _, at := range p.Attachments { |
| 1500 | r := repoLine{Attachment: at} |
| 1501 | if at.Kind == wire.AttachmentRepo { |
| 1502 | r.URL = "https://" + at.Ref |
| 1503 | } |
| 1504 | if at.HeadAt != nil { |
| 1505 | r.Ago = ago(now, *at.HeadAt) |
| 1506 | } |
| 1507 | line.Repos = append(line.Repos, r) |
| 1508 | } |
| 1509 | return line |
| 1510 | } |
| 1511 | |
| 1512 | // PhoneStack reports whether a project is a phone app, for its icon. |
| 1513 | func (p projectLine) PhoneStack() bool { |
| 1514 | for _, s := range p.Project.Stack { |
| 1515 | if stackIcon[s] { |
| 1516 | return true |
| 1517 | } |
| 1518 | } |
| 1519 | return false |
| 1520 | } |
| 1521 | |
| 1522 | // ChecksLine is the project's checks fact in words. |
| 1523 | func (p projectLine) ChecksLine() string { |
| 1524 | switch { |
| 1525 | case p.Checks == nil: |
| 1526 | return "" |
| 1527 | case p.Checks.Here: |
| 1528 | return fmt.Sprintf(copyText("work.project.checks_here"), p.Checks.Gate) |
| 1529 | case p.Checks.Reason == "none": |
| 1530 | return copyText("work.project.checks_none") |
| 1531 | } |
| 1532 | if s := copyText("work.project.checks_" + p.Checks.Reason); s != "" { |
| 1533 | return s |
| 1534 | } |
| 1535 | return copyText("work.project.checks_elsewhere") |
| 1536 | } |
| 1537 | |
| 1538 | // projectActivity is a project's recent events, newest first. |
| 1539 | func projectActivity(tasks []taskLine, now time.Time) []projectEvent { |
| 1540 | var out []projectEvent |
| 1541 | for _, t := range tasks { |
| 1542 | who := t.Creator.Name |
| 1543 | if t.Creator.You { |
| 1544 | who = copyText("work.activity.you") |
| 1545 | } |
| 1546 | out = append(out, projectEvent{At: t.CreatedAt, Format: "work.recent.noted", Who: who, TaskID: t.ID, Title: t.Title}) |
| 1547 | for _, s := range t.Steps { |
| 1548 | if s.FinishedAt == nil { |
| 1549 | continue |
| 1550 | } |
| 1551 | ev := projectEvent{At: *s.FinishedAt, TaskID: t.ID, Title: t.Title, Who: stepName(s.Step)} |
| 1552 | switch { |
| 1553 | case s.State == wire.StepFailed: |
| 1554 | ev.Format = "work.recent.stopped" |
| 1555 | case s.Specialist == wire.SpecialistEngineering && s.State == wire.StepDone && s.Pushed: |
| 1556 | ev.Format = "work.recent.pushed" |
| 1557 | case s.Specialist == wire.SpecialistReview && s.Verdict == wire.VerdictApprove: |
| 1558 | ev.Format = "work.recent.approved" |
| 1559 | case s.Specialist == wire.SpecialistReview && s.Verdict == wire.VerdictChanges: |
| 1560 | ev.Format = "work.recent.changes" |
| 1561 | default: |
| 1562 | continue |
| 1563 | } |
| 1564 | out = append(out, ev) |
| 1565 | } |
| 1566 | if t.Merged != nil { |
| 1567 | out = append(out, projectEvent{At: t.Merged.At, Format: "work.recent.merged", TaskID: t.ID, Title: t.Title, Who: t.Merged.Into}) |
| 1568 | } |
| 1569 | } |
| 1570 | slices.SortStableFunc(out, func(a, b projectEvent) int { return b.At.Compare(a.At) }) |
| 1571 | if len(out) > 7 { |
| 1572 | out = out[:7] |
| 1573 | } |
| 1574 | for i := range out { |
| 1575 | out[i].Ago = ago(now, out[i].At) |
| 1576 | } |
| 1577 | return out |
| 1578 | } |
| 1579 | |
| 1580 | // projectNotes are the viewer's coordinator's notebook entries about the |
| 1581 | // project: those naming one of its repositories. |
| 1582 | func projectNotes(p projectLine, entries []wire.NotebookEntry) []projectNote { |
| 1583 | repos := map[string]bool{} |
| 1584 | for _, at := range p.Attachments { |
| 1585 | repos[chat.ProjectName(at.Ref)] = true |
| 1586 | } |
| 1587 | var out []projectNote |
| 1588 | for _, e := range entries { |
| 1589 | if e.Repo == "" || !repos[chat.ProjectName(e.Repo)] { |
| 1590 | continue |
| 1591 | } |
| 1592 | day := e.At.Format("2 Jan") |
| 1593 | var from string |
| 1594 | switch e.Source { |
| 1595 | case wire.NotePerson: |
| 1596 | from = fmt.Sprintf(copyText("work.note.person"), day) |
| 1597 | case wire.NoteRepo: |
| 1598 | from = fmt.Sprintf(copyText("work.note.repo"), e.Path, day) |
| 1599 | default: |
| 1600 | from = fmt.Sprintf(copyText("work.note.own"), day) |
| 1601 | } |
| 1602 | out = append(out, projectNote{Text: e.Text, From: from}) |
| 1603 | } |
| 1604 | return out |
| 1605 | } |
| 1606 | |
| 1607 | // previewBuiltItem is a preview's export on the timeline: "Prepared 12 |
| 1608 | // files (340 KiB)." |
| 1609 | func previewBuiltItem(p wire.PreviewResult) timelineItem { |
| 1610 | return timelineItem{Kind: "step", What: fmt.Sprintf(copyText("work.tl.preview_built"), p.Files, byteSize(p.Bytes))} |
| 1611 | } |
| 1612 | |
| 1613 | // byteSize is a size in KiB or MiB. |
| 1614 | func byteSize(n int64) string { |
| 1615 | if n < 1<<20 { |
| 1616 | return fmt.Sprintf("%d KiB", (n+1023)/1024) |
| 1617 | } |
| 1618 | return fmt.Sprintf("%.1f MiB", float64(n)/(1<<20)) |
| 1619 | } |
| 1620 | |
| 1621 | // previewCard is the task card's preview line: the newest preview that |
| 1622 | // answered with its export and has not expired ("", "" for none). |
| 1623 | func previewCard(t wire.Task, now time.Time) (url, line string) { |
| 1624 | for i := len(t.Steps) - 1; i >= 0; i-- { |
| 1625 | s := t.Steps[i] |
| 1626 | if s.Specialist == wire.SpecialistPreview && !wire.StepTerminal(s.State) { |
| 1627 | return "", "" |
| 1628 | } |
| 1629 | if s.Specialist == wire.SpecialistPreview && s.Preview != nil && (s.Reason == "preview_headers" || s.Reason == "preview_header_missing") { |
| 1630 | return "/tasks/" + t.ID + "/preview", copyText("preview.unsafe_question") |
| 1631 | } |
| 1632 | if s.Specialist != wire.SpecialistPreview || s.State != wire.StepDone || s.Preview == nil || s.Preview.Outcome != wire.PreviewDeployed || s.Preview.URL == "" { |
| 1633 | continue |
| 1634 | } |
| 1635 | p := s.Preview |
| 1636 | if strings.HasPrefix(p.App, "eq-") { |
| 1637 | // A CARLOS devbox link: no expiry; it comes down when the task |
| 1638 | // ends, and so does the line; one run again after the task was |
| 1639 | // done stays until the next run replaces it. |
| 1640 | if wire.FinalStage(t.Stage) && (!devboxAfterDone(t, s) || p.ExpiresAt != nil && !p.ExpiresAt.After(now)) { |
| 1641 | return "", "" |
| 1642 | } |
| 1643 | return "/tasks/" + t.ID + "/preview", fmt.Sprintf(copyText("work.card.preview_tailnet"), previewHostLabel(p.URL)) |
| 1644 | } |
| 1645 | if p.ExpiresAt == nil { |
| 1646 | return "/tasks/" + t.ID + "/preview", fmt.Sprintf(copyText("work.card.preview_open"), previewHostLabel(p.URL)) |
| 1647 | } |
| 1648 | left := p.ExpiresAt.Sub(now) |
| 1649 | if left <= 0 { |
| 1650 | return "", "" |
| 1651 | } |
| 1652 | in := copyText("work.preview.today") |
| 1653 | if left >= 24*time.Hour { |
| 1654 | // Whole days, rounded up: just deployed reads "7 days". |
| 1655 | switch days := int((left + 24*time.Hour - 1) / (24 * time.Hour)); { |
| 1656 | case days == 1: |
| 1657 | in = copyText("work.preview.in_day") |
| 1658 | default: |
| 1659 | in = fmt.Sprintf(copyText("work.preview.in_days"), days) |
| 1660 | } |
| 1661 | } |
| 1662 | return "/tasks/" + t.ID + "/preview", fmt.Sprintf(copyText("work.card.preview"), previewHostLabel(p.URL), in) |
| 1663 | } |
| 1664 | return "", "" |
| 1665 | } |
| 1666 | |
| 1667 | func previewHostLabel(raw string) string { |
| 1668 | u, err := url.Parse(raw) |
| 1669 | if err != nil { |
| 1670 | return "" |
| 1671 | } |
| 1672 | return u.Host |
| 1673 | } |
| 1674 | |