| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "the-valley/flow/internal/review" |
| 7 | "io" |
| 8 | "os" |
| 9 | "strings" |
| 10 | "time" |
| 11 | |
| 12 | gh "the-valley/flow/internal/github" |
| 13 | "the-valley/flow/internal/integration" |
| 14 | "the-valley/flow/internal/jobs" |
| 15 | "the-valley/flow/internal/restack" |
| 16 | "the-valley/flow/internal/run" |
| 17 | "the-valley/flow/internal/tmux" |
| 18 | "github.com/spf13/cobra" |
| 19 | ) |
| 20 | |
| 21 | var statusCmd = &cobra.Command{ |
| 22 | Use: "status", |
| 23 | Short: "Show all runs and their current state", |
| 24 | Long: `Shows all runs in the session and their current state. |
| 25 | |
| 26 | STATUS is one of: running (the agent's pane is still running it, judged as |
| 27 | the dashboard judges it: a dead pane, or an idle one once the run finished, |
| 28 | isn't running), needs-attention (finalize salvaged its work, it failed |
| 29 | after taking on the work of runs that needed attention, or the files its |
| 30 | clone shares across worktrees, its git config, info/attributes or hooks, |
| 31 | changed while it ran: a line below the table names them, and that holds |
| 32 | whatever became of its work), superseded (it |
| 33 | needed attention, but a run of its change, the same change ID in the same |
| 34 | repo, took its pushed work on and is running or finished: that run |
| 35 | recorded it, as launch does for a --pr, --change or --resume-from run |
| 36 | continuing a branch that holds the work, and finalize does when the head a |
| 37 | run left on origin contains it; that run carries the attention from then |
| 38 | on, and shows needs-attention itself if it ended needing attention or |
| 39 | failing; neither status nor the dashboard counts the superseded run as |
| 40 | needing attention), pr-created, merged, closed, |
| 41 | no-access (GitHub won't show the PR with the current credentials), exited, |
| 42 | and, for runs that finished by pushing a branch without a PR: |
| 43 | |
| 44 | awaiting-integration the branch is on origin and hasn't landed |
| 45 | integrated its work is on origin's default branch, by ancestry |
| 46 | or by an equal patch-id, or the valley says its |
| 47 | change landed |
| 48 | branch-gone the branch was deleted from origin without landing |
| 49 | |
| 50 | and, for a branch finalize recorded as a version of its change on a valley |
| 51 | (ready, or a draft when ready_at_finalize is off), where the valley's record |
| 52 | says the change stands: |
| 53 | |
| 54 | awaiting-review a draft finalize left for review, as it does when |
| 55 | ready_at_finalize is off; approve refuses it until |
| 56 | 'klaus ready' marks it ready once its review passes |
| 57 | awaiting-approval ready and waiting for you to approve it |
| 58 | approved approved: the integrator hasn't decided, or judged it |
| 59 | stale only because its checks are still running (CI |
| 60 | shows them). Nothing to do |
| 61 | uncertain a request was consumed or failed with no landing |
| 62 | evidence: look at it |
| 63 | checks-failed stale, and the version's checks failed |
| 64 | conflicting stale, and the change's current restack report |
| 65 | says a rebase conflicted |
| 66 | stale, rejected, the integrator's decision on the request (stale |
| 67 | failed with passed checks, or on a valley that lists |
| 68 | none, is for you to look at) |
| 69 | withdrawn the change was withdrawn |
| 70 | older-version the change has a newer version, made by the |
| 71 | valley's restacker or a later run, and hasn't |
| 72 | landed; the run's row says nothing of whether the |
| 73 | change is done |
| 74 | draft returned to draft ('valley unready'), as launch |
| 75 | does when a follow-up run continues the change; |
| 76 | approve refuses it until a head is ready again. |
| 77 | A draft reads draft even when its landing is |
| 78 | uncertain; only landing evidence comes first |
| 79 | |
| 80 | A run that isn't running and isn't done shows working while another run of |
| 81 | its change (the same change ID in the same repo) is running: a follow-up |
| 82 | (--pr, --resume-from or a pipeline fix) is still pushing to it, so there is |
| 83 | nothing to approve yet. |
| 84 | |
| 85 | A valley change added with klaus track shows where the valley's listing |
| 86 | says its active version stands, by the statuses above (awaiting-approval, |
| 87 | approved, integrated, draft, withdrawn and so on), with its title as the |
| 88 | prompt; it reads tracking while the valley can't be asked. A tracked PR |
| 89 | reads tracking until GitHub says it is merged or closed. |
| 90 | |
| 91 | CHANGE names the change a run belongs to: its PR's number (#319), else the |
| 92 | number of its change's PR (the PR of the newest run of the change that has |
| 93 | one), else its valley change's handle, else the first 8 characters of its |
| 94 | change ID, so runs of one change share it, running or not. The handle is the valley |
| 95 | listing's, else (when the valley can't be asked) the one a run of the |
| 96 | change recorded when finalize marked it ready or launch returned it to |
| 97 | draft. A valley run shows its change's title as the |
| 98 | prompt. |
| 99 | |
| 100 | Integration is checked with one "valley changes --all --json" when a valley |
| 101 | is involved, and otherwise against each run's clone (one ls-remote and fetch |
| 102 | per repo). If the valley can't be asked, its runs are checked against their |
| 103 | clones. If the check can't complete, the run stays awaiting-integration (or |
| 104 | awaiting-approval, or awaiting-review). A deleted branch alone never settles a valley change: it |
| 105 | stays awaiting-approval rather than branch-gone. |
| 106 | |
| 107 | A valley's host runs a ready change's required checks. CI reads where they |
| 108 | stand on the change's active version, as it reads a PR's CI: pending, |
| 109 | failing or passing, and a list after the table names each change's failed |
| 110 | checks. CI reads - while the change isn't ready, or the valley lists no |
| 111 | checks (one from before it listed them). The checks don't change the |
| 112 | status: a ready change reads awaiting-approval whatever they say, since you |
| 113 | may approve it before they finish, and it lands once both hold. The |
| 114 | dashboard dispatches a run to fix a failed head of a change klaus owns (see |
| 115 | klaus failed-checks). |
| 116 | |
| 117 | A valley's restacker rebases ready changes onto main after each landing, |
| 118 | and reports a rebase that conflicts. For a change klaus owns (its record's |
| 119 | owner is the repo's valley.principal), CONFLICTS reads restack while the |
| 120 | conflict stands, and a list after the table says where each stands: a |
| 121 | resolution run due, dispatched (attempt n of 3), being launched (klaus |
| 122 | hasn't yet seen the run start), waiting out the cooldown, a run already |
| 123 | working on the change, stopped after 3 runs, not dispatched (and why: the |
| 124 | change couldn't be returned to draft, or the launch failed, perhaps after |
| 125 | starting its worker), or left alone (the conflict is in a change below it, |
| 126 | or the change sits on other changes). klaus restack-conflicts, which the |
| 127 | dashboard runs, does the dispatching; status only reads. |
| 128 | |
| 129 | A line after them sums the session's cost, counting the runs klaus cleanup |
| 130 | removed. |
| 131 | |
| 132 | After the runs, a jobs table lists the session's consults, reviews and prose passes (see |
| 133 | klaus jobs): every one running, and those that finished in the last 3 hours, |
| 134 | with how long each has run or took. It is left out when there are none.`, |
| 135 | RunE: func(cmd *cobra.Command, args []string) error { |
| 136 | store, err := sessionStore() |
| 137 | if err != nil { |
| 138 | return err |
| 139 | } |
| 140 | if err := printRuns(cmd.Context(), store); err != nil { |
| 141 | return err |
| 142 | } |
| 143 | printRecentJobs(os.Stdout, store, time.Now()) |
| 144 | return nil |
| 145 | }, |
| 146 | } |
| 147 | |
| 148 | // printRecentJobs prints, after a blank line, the jobs table of the |
| 149 | // session's running jobs and those that finished within recentJobs; nothing |
| 150 | // when there are none. |
| 151 | func printRecentJobs(w io.Writer, store run.StateStore, now time.Time) { |
| 152 | hds, ok := store.(*run.HomeDirStore) |
| 153 | if !ok { |
| 154 | return |
| 155 | } |
| 156 | list, err := jobs.NewStore(hds.BaseDir()).List() |
| 157 | if err != nil { |
| 158 | fmt.Fprintf(os.Stderr, "warning: reading jobs: %v\n", err) |
| 159 | return |
| 160 | } |
| 161 | if list = jobs.Recent(list, now, recentJobs); len(list) > 0 { |
| 162 | fmt.Fprintln(w) |
| 163 | printJobsTable(w, list, now) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // printRuns prints the runs table and the session's spend, or says there |
| 168 | // are no runs. |
| 169 | func printRuns(ctx context.Context, store run.StateStore) error { |
| 170 | states, err := store.List() |
| 171 | if err != nil { |
| 172 | return err |
| 173 | } |
| 174 | cleared := sessionCleared(store, states) |
| 175 | if len(states) == 0 && cleared.Runs == 0 { |
| 176 | fmt.Println("No runs found.") |
| 177 | return nil |
| 178 | } |
| 179 | if len(states) > 0 { |
| 180 | if _, err := runStatuses(ctx, store, states, os.Stdout, nil); err != nil { |
| 181 | return err |
| 182 | } |
| 183 | } |
| 184 | fmt.Fprintln(os.Stdout) |
| 185 | fmt.Fprintf(os.Stdout, "Session cost: $%.2f", computeTotalCost(states)+cleared.CostUSD) |
| 186 | if cleared.Runs > 0 { |
| 187 | fmt.Fprintf(os.Stdout, " ($%.2f of it by %d runs cleanup removed)", cleared.CostUSD, cleared.Runs) |
| 188 | } |
| 189 | fmt.Fprintln(os.Stdout) |
| 190 | return nil |
| 191 | } |
| 192 | |
| 193 | // sessionCleared sums the runs cleanup removed from the session; zero when |
| 194 | // store isn't a session directory or the ledger can't be read. |
| 195 | func sessionCleared(store run.StateStore, live []*run.State) run.Cleared { |
| 196 | c, err := clearedOf(store, live) |
| 197 | if err != nil { |
| 198 | fmt.Fprintf(os.Stderr, "warning: reading cleared runs: %v\n", err) |
| 199 | } |
| 200 | return c |
| 201 | } |
| 202 | |
| 203 | // clearedOf sums the runs cleanup removed from store's session, other than |
| 204 | // those in live; zero when store isn't a session directory. |
| 205 | func clearedOf(store run.StateStore, live []*run.State) (run.Cleared, error) { |
| 206 | hds, ok := store.(*run.HomeDirStore) |
| 207 | if !ok { |
| 208 | return run.Cleared{}, nil |
| 209 | } |
| 210 | return run.ReadCleared(hds.BaseDir(), live) |
| 211 | } |
| 212 | |
| 213 | // runStanding returns where each run of states stands, by index, as klaus |
| 214 | // status shows it, and the integration check it read. |
| 215 | func runStanding(ctx context.Context, store run.StateStore, states []*run.State) ([]string, integration.Report, error) { |
| 216 | var report integration.Report |
| 217 | statuses, err := runStatuses(ctx, store, states, io.Discard, &report) |
| 218 | return statuses, report, err |
| 219 | } |
| 220 | |
| 221 | // runStatuses writes the runs table for states to out and returns where |
| 222 | // each run stands, by index into states, as the table shows it. A non-nil |
| 223 | // checked receives the integration check it read. |
| 224 | func runStatuses(ctx context.Context, store run.StateStore, states []*run.State, out io.Writer, checked *integration.Report) ([]string, error) { |
| 225 | tmuxClient := tmux.NewExecClient() |
| 226 | ghClient := gh.NewGHCLIClient("") |
| 227 | |
| 228 | fmt.Fprintf(out, "%-22s %-10s %-26s %-8s %-6s %-20s %-15s %-8s %-10s %-10s %-8s %-10s %s\n", |
| 229 | "RUN ID", "STATUS", "MODEL", "COST", "ISSUE", "REPO", "HOST", "CHANGE", "CI", "CONFLICTS", "BEHIND", "MERGE", "PROMPT") |
| 230 | fmt.Fprintf(out, "%-22s %-10s %-26s %-8s %-6s %-20s %-15s %-8s %-10s %-10s %-8s %-10s %s\n", |
| 231 | "------", "------", "-----", "----", "-----", "----", "----", "------", "--", "---------", "------", "-----", "------") |
| 232 | |
| 233 | statuses := make([]string, len(states)) |
| 234 | running := make(map[string]bool) |
| 235 | for i, s := range states { |
| 236 | if err := ctx.Err(); err != nil { |
| 237 | return nil, err |
| 238 | } |
| 239 | statuses[i] = determineStatus(ctx, s, tmuxClient) |
| 240 | running[s.ID] = statuses[i] == "running" |
| 241 | } |
| 242 | // A run whose work a later run took on hands its attention to that run. |
| 243 | superseded := supersededRuns(states, func(s *run.State) bool { return running[s.ID] }) |
| 244 | var pushed []*run.State |
| 245 | for i, s := range states { |
| 246 | if statuses[i] == "needs-attention" && superseded[s.ID] && !s.SharedGitChanged() { |
| 247 | statuses[i] = "superseded" |
| 248 | } |
| 249 | if statuses[i] == "awaiting-integration" || integration.TracksChange(s) { |
| 250 | pushed = append(pushed, s) |
| 251 | } |
| 252 | } |
| 253 | // Where pushed branches stand: one valley listing for the runs a |
| 254 | // valley knows and the tracked valley changes, one check per repo |
| 255 | // against each run's own clone for the rest. |
| 256 | report := integration.NewValleyChecker(nil).Check(ctx, pushed) |
| 257 | if checked != nil { |
| 258 | *checked = report |
| 259 | } |
| 260 | if note := integration.ValleyNote(report.ValleyErr); note != "" { |
| 261 | fmt.Fprintln(os.Stderr, note) |
| 262 | } |
| 263 | handles := changeHandles(states, report.Changes) |
| 264 | prs := changePRs(states) |
| 265 | // The restack conflicts on changes klaus owns, and what klaus did |
| 266 | // about them; reading them changes nothing. |
| 267 | var restackList []restack.View |
| 268 | var err error |
| 269 | if hds, ok := store.(*run.HomeDirStore); ok && (report.Listing != nil || !report.Asked) { |
| 270 | if restackList, err = restackPass(ctx, hds, report.Listing, false); err != nil { |
| 271 | fmt.Fprintf(os.Stderr, "warning: reading restack conflicts: %v\n", err) |
| 272 | } |
| 273 | } |
| 274 | restacks := restackViews(restackList) |
| 275 | |
| 276 | // The changes some run is still running on: any other run of one |
| 277 | // that isn't done is working. |
| 278 | changeOf := changeKeys(states) |
| 279 | busy := make(map[integration.ChangeKey]bool) |
| 280 | for i, s := range states { |
| 281 | if k, ok := changeOf(s); ok && statuses[i] == "running" { |
| 282 | busy[k] = true |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | for i, s := range states { |
| 287 | if err := ctx.Err(); err != nil { |
| 288 | return nil, err |
| 289 | } |
| 290 | status := statuses[i] |
| 291 | prompt := truncate(s.Prompt, 40) |
| 292 | var change *integration.Change |
| 293 | if status == "awaiting-integration" { |
| 294 | res := report.Runs[s.ID] |
| 295 | change = res.Change |
| 296 | if change == nil { |
| 297 | change = integration.RecordedChange(s) |
| 298 | } |
| 299 | status = pushedStatus(res.Verdict, change != nil) |
| 300 | if change != nil && awaitsReview(s, res.Verdict) { |
| 301 | status = "awaiting-review" |
| 302 | } |
| 303 | if change != nil && change.Title != "" { |
| 304 | prompt = truncate(change.Title, 40) |
| 305 | } |
| 306 | } |
| 307 | // A tracked valley change stands where the valley's listing says |
| 308 | // its active version does; tracking while the valley can't be |
| 309 | // asked. |
| 310 | if integration.TracksChange(s) && !s.SharedGitChanged() { |
| 311 | if view := report.Changes[valleyKey(s)]; view.Change != nil { |
| 312 | status = pushedStatus(view.Verdict, true) |
| 313 | if view.Change.Title != "" { |
| 314 | prompt = truncate(view.Change.Title, 40) |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | cost := formatCost(s) |
| 319 | issue := "-" |
| 320 | if s.Issue != nil { |
| 321 | issue = *s.Issue |
| 322 | } |
| 323 | repo := "-" |
| 324 | if s.TargetRepo != nil { |
| 325 | repo = truncate(*s.TargetRepo, 20) |
| 326 | } |
| 327 | host := "-" |
| 328 | if s.Host != nil { |
| 329 | host = *s.Host |
| 330 | } |
| 331 | ci, conflicts, behind, merge := "-", "-", "-", "-" |
| 332 | if prRef := extractPRRef(s); prRef != "" { |
| 333 | prState := ghClient.GetState(ctx, prRef) |
| 334 | switch prState { |
| 335 | case "MERGED": |
| 336 | status = prSettled(status, s, "merged") |
| 337 | merge = "merged" |
| 338 | case "CLOSED": |
| 339 | status = prSettled(status, s, "closed") |
| 340 | case gh.StateNoAccess: |
| 341 | // GitHub won't show this PR to us: nothing else to ask. |
| 342 | status = prSettled(status, s, "no-access") |
| 343 | default: |
| 344 | ci = ghClient.GetCI(ctx, prRef) |
| 345 | conflicts = ghClient.GetConflicts(ctx, prRef) |
| 346 | behindBy := ghClient.GetCommitsBehind(ctx, prRef) |
| 347 | if behindBy > 0 { |
| 348 | behind = fmt.Sprintf("%d", behindBy) |
| 349 | } |
| 350 | merge = computeMergeStatus(ci, conflicts, ghClient.GetReviewDecision(ctx, prRef), behindBy) |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | if s.ChangeID != "" && ci == "-" { |
| 355 | if view := report.Changes[valleyKey(s)]; view.Change != nil { |
| 356 | if c := checksCI(view.Change.Checks); c != "" { |
| 357 | ci = c |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | if _, ok := restacks[valleyKey(s)]; ok && s.ChangeID != "" && conflicts == "-" { |
| 363 | conflicts = "restack" |
| 364 | } |
| 365 | |
| 366 | if k, ok := changeOf(s); ok && busy[k] && live(status) && !s.SharedGitChanged() { |
| 367 | status = "working" |
| 368 | } |
| 369 | |
| 370 | statuses[i] = status |
| 371 | fmt.Fprintf(out, "%-22s %-10s %-26s %-8s %-6s %-20s %-15s %-8s %-10s %-10s %-8s %-10s %s\n", |
| 372 | s.ID, status, runBackendName(s), cost, issue, repo, host, changeLabel(s, changeOf, prs, handles), ci, conflicts, behind, merge, prompt) |
| 373 | } |
| 374 | |
| 375 | // A run that changed the files its clone shares across worktrees |
| 376 | // says which, for the operator to check before git runs there again. |
| 377 | first := true |
| 378 | for _, s := range states { |
| 379 | if !s.SharedGitChanged() { |
| 380 | continue |
| 381 | } |
| 382 | if first { |
| 383 | fmt.Fprintln(out) |
| 384 | first = false |
| 385 | } |
| 386 | fmt.Fprintf(out, "%s changed its clone's shared git files while it ran: %s (in %s); check them before running git in that clone\n", |
| 387 | s.ID, strings.Join(s.SharedGit.Changed, ", "), s.SharedGit.Dir) |
| 388 | } |
| 389 | |
| 390 | if failing := failingChecks(states, report.Changes); len(failing) > 0 { |
| 391 | fmt.Fprintln(out) |
| 392 | fmt.Fprintln(out, "Failed valley checks:") |
| 393 | for _, f := range failing { |
| 394 | fmt.Fprintf(out, " %s\n", f) |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | if len(restackList) > 0 { |
| 399 | fmt.Fprintln(out) |
| 400 | fmt.Fprintln(out, "Restack conflicts on changes klaus owns:") |
| 401 | for _, v := range restackList { |
| 402 | fmt.Fprintf(out, " %s\n", v) |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | return statuses, nil |
| 407 | } |
| 408 | |
| 409 | // prSettled is the status of run s, status so far, once GitHub says its PR |
| 410 | // is settled (merged, closed or out of reach): settled, unless the run |
| 411 | // still needs attention it took on with other runs' work (TookOver), which |
| 412 | // its PR's fate settles nothing about, as on the dashboard. |
| 413 | func prSettled(status string, s *run.State, settled string) string { |
| 414 | if status == "needs-attention" && (len(s.TookOver) > 0 || s.SharedGitChanged()) { |
| 415 | return status |
| 416 | } |
| 417 | return settled |
| 418 | } |
| 419 | |
| 420 | func determineStatus(ctx context.Context, s *run.State, tc tmux.Client) string { |
| 421 | if s.Type == "session" { |
| 422 | if _, err := os.Stat(s.Worktree); err == nil { |
| 423 | return "active" |
| 424 | } |
| 425 | return "ended" |
| 426 | } |
| 427 | |
| 428 | if s.Type == "track" { |
| 429 | if s.MergedAt != nil { |
| 430 | return "merged" |
| 431 | } |
| 432 | return "tracking" |
| 433 | } |
| 434 | |
| 435 | // Running as the dashboard decides it: a pane that exists isn't enough |
| 436 | // when its command is dead, or, once the run finalized, idle. |
| 437 | if s.IsAgentRunningWith(run.TmuxDepsFor(ctx, tc)) { |
| 438 | return "running" |
| 439 | } |
| 440 | |
| 441 | // The attention a run took on with other runs' work (TookOver) outlasts |
| 442 | // its PR's merge, as on the dashboard, and so does a change to the |
| 443 | // clone's shared git files. |
| 444 | if needsAttention(s) && len(s.TookOver) > 0 || s.SharedGitChanged() { |
| 445 | return "needs-attention" |
| 446 | } |
| 447 | if s.MergedAt != nil { |
| 448 | return "merged" |
| 449 | } |
| 450 | if needsAttention(s) { |
| 451 | return "needs-attention" |
| 452 | } |
| 453 | if s.PRURL != nil { |
| 454 | return "pr-created" |
| 455 | } |
| 456 | if s.PushedSHA != nil { |
| 457 | // Direct-push finish: the branch on origin waits for the operator. |
| 458 | // The status command refines this with an integration check. |
| 459 | return "awaiting-integration" |
| 460 | } |
| 461 | return "exited" |
| 462 | } |
| 463 | |
| 464 | // live reports whether a status leaves something to do for a run that isn't |
| 465 | // running: salvaged work, an open PR, or a pushed branch that hasn't landed, |
| 466 | // gone, or been withdrawn or gained a newer version. |
| 467 | func live(status string) bool { |
| 468 | switch status { |
| 469 | case "needs-attention", "pr-created", "awaiting-integration", "awaiting-review", "awaiting-approval", |
| 470 | "approved", "checks-failed", "conflicting", "uncertain", "stale", "rejected", "failed", "draft": |
| 471 | return true |
| 472 | } |
| 473 | return false |
| 474 | } |
| 475 | |
| 476 | // pushedStatus is the status of a run awaiting integration once an |
| 477 | // integration check has looked at it; valley says a valley knows the run's |
| 478 | // change. An unknown verdict (no clone, origin unreachable) keeps it |
| 479 | // awaiting. |
| 480 | func pushedStatus(v integration.Verdict, valley bool) string { |
| 481 | switch v { |
| 482 | case integration.Integrated, integration.IntegratedContent: |
| 483 | return "integrated" |
| 484 | case integration.Gone: |
| 485 | return "branch-gone" |
| 486 | case integration.Ready: |
| 487 | return "awaiting-approval" |
| 488 | case integration.Unknown, integration.Awaiting: |
| 489 | if valley { |
| 490 | return "awaiting-approval" |
| 491 | } |
| 492 | return "awaiting-integration" |
| 493 | case integration.Superseded: |
| 494 | // The change has a newer version, the restacker's or a later |
| 495 | // run's; the change itself may still wait for approval. |
| 496 | return "older-version" |
| 497 | } |
| 498 | // approved, checks-failed, conflicting, uncertain, stale, rejected, |
| 499 | // failed, withdrawn, draft |
| 500 | return v.String() |
| 501 | } |
| 502 | |
| 503 | // awaitsReview reports whether a valley change's version, which run s |
| 504 | // pushed and a check says v of, awaits review rather than approval: |
| 505 | // finalize left it a draft (ready_at_finalize off), klaus ready hasn't |
| 506 | // marked it ready since, and the valley says it is still a draft, or can't |
| 507 | // be asked. A draft whose review passed is marked ready with klaus ready. |
| 508 | func awaitsReview(s *run.State, v integration.Verdict) bool { |
| 509 | if s == nil || s.Valley == nil || !s.Valley.Draft { |
| 510 | return false |
| 511 | } |
| 512 | return v == integration.Draft || v == integration.Unknown || v == integration.Awaiting |
| 513 | } |
| 514 | |
| 515 | func formatCost(s *run.State) string { |
| 516 | if s.CostUSD != nil { |
| 517 | return fmt.Sprintf("$%.2f", *s.CostUSD) |
| 518 | } |
| 519 | if s.Budget != nil { |
| 520 | return fmt.Sprintf("<$%s", *s.Budget) |
| 521 | } |
| 522 | return "-" |
| 523 | } |
| 524 | |
| 525 | // changeLabel names the change a run belongs to, for the CHANGE column: its |
| 526 | // PR's number (#319), else its change's PR's (changePRs), else its valley |
| 527 | // change's handle (changeHandles), else the first 8 characters of its |
| 528 | // change ID, as the dashboard names a PR's or a change's row; "-" for a run |
| 529 | // with none of them. |
| 530 | func changeLabel(s *run.State, changeOf func(*run.State) (integration.ChangeKey, bool), prs, handles map[integration.ChangeKey]string) string { |
| 531 | k, _ := changeOf(s) |
| 532 | switch { |
| 533 | case s.PRURL != nil: |
| 534 | return formatPR(s) |
| 535 | case prs[k] != "": |
| 536 | return prs[k] |
| 537 | case handles[k] != "": |
| 538 | return handles[k] |
| 539 | case s.ChangeID != "": |
| 540 | return s.ChangeID[:min(8, len(s.ChangeID))] |
| 541 | } |
| 542 | return "-" |
| 543 | } |
| 544 | |
| 545 | // changePRs names each change some run with a PR belongs to by that PR's |
| 546 | // number, the newest such run's, as the dashboard puts the change's runs |
| 547 | // without a PR on that PR's row. |
| 548 | func changePRs(states []*run.State) map[integration.ChangeKey]string { |
| 549 | changeOf := changeKeys(states) |
| 550 | out := make(map[integration.ChangeKey]string) |
| 551 | by := make(map[integration.ChangeKey]*run.State) // the run whose PR names each change |
| 552 | for _, s := range states { |
| 553 | k, ok := changeOf(s) |
| 554 | if !ok || s.PRURL == nil || by[k] != nil && createdBefore(s, by[k]) { |
| 555 | continue |
| 556 | } |
| 557 | out[k], by[k] = formatPR(s), s |
| 558 | } |
| 559 | return out |
| 560 | } |
| 561 | |
| 562 | // changeHandles names each valley change some run belongs to, so every run |
| 563 | // on it, running or not, shows the same handle: the valley's listing's, which |
| 564 | // is current (a handle lengthens when another change comes to share its |
| 565 | // prefix); else, when the valley can't be asked or doesn't list the change, |
| 566 | // the handle the latest run to record one got from finalize's ready or |
| 567 | // launch's unready. |
| 568 | func changeHandles(states []*run.State, listed map[integration.ChangeKey]integration.Result) map[integration.ChangeKey]string { |
| 569 | changeOf := changeKeys(states) |
| 570 | out := make(map[integration.ChangeKey]string) |
| 571 | by := make(map[integration.ChangeKey]*run.State) // the run whose record names each unlisted change |
| 572 | for _, s := range states { |
| 573 | k, ok := changeOf(s) |
| 574 | if !ok { |
| 575 | continue |
| 576 | } |
| 577 | if c := listed[valleyKey(s)].Change; c != nil { |
| 578 | out[k] = c.Handle |
| 579 | continue |
| 580 | } |
| 581 | h := recordedHandle(s) |
| 582 | if h == "" || by[k] != nil && createdBefore(s, by[k]) { |
| 583 | continue |
| 584 | } |
| 585 | out[k], by[k] = h, s |
| 586 | } |
| 587 | return out |
| 588 | } |
| 589 | |
| 590 | // recordedHandle is the handle a run recorded for its change: from |
| 591 | // finalize's ready, else from launch's unready; "" when neither got one. |
| 592 | func recordedHandle(s *run.State) string { |
| 593 | if s.Valley != nil && s.Valley.Handle != "" { |
| 594 | return s.Valley.Handle |
| 595 | } |
| 596 | return s.UnreadyHandle() |
| 597 | } |
| 598 | |
| 599 | func formatPR(s *run.State) string { |
| 600 | if s.PRURL == nil { |
| 601 | return "-" |
| 602 | } |
| 603 | url := *s.PRURL |
| 604 | // Extract PR number from URL |
| 605 | for i := len(url) - 1; i >= 0; i-- { |
| 606 | if url[i] == '/' { |
| 607 | return "#" + url[i+1:] |
| 608 | } |
| 609 | } |
| 610 | return url |
| 611 | } |
| 612 | |
| 613 | // extractPRNumber returns the PR number (e.g. "18") from a run state, or "". |
| 614 | func extractPRNumber(s *run.State) string { |
| 615 | if s.PRURL == nil { |
| 616 | return "" |
| 617 | } |
| 618 | url := *s.PRURL |
| 619 | for i := len(url) - 1; i >= 0; i-- { |
| 620 | if url[i] == '/' { |
| 621 | return url[i+1:] |
| 622 | } |
| 623 | } |
| 624 | return "" |
| 625 | } |
| 626 | |
| 627 | // extractPRRef returns the full PR URL from a run state (for use with gh CLI), |
| 628 | // or "" if no PRURL is set. Using the full URL ensures gh can find the PR |
| 629 | // regardless of the current working directory. |
| 630 | func extractPRRef(s *run.State) string { |
| 631 | if s.PRURL == nil { |
| 632 | return "" |
| 633 | } |
| 634 | return *s.PRURL |
| 635 | } |
| 636 | |
| 637 | // computeMergeStatus determines overall merge readiness. behind is the base-ahead |
| 638 | // commit count; a would-be-ready-but-behind PR reads "behind N" (rebased at merge) |
| 639 | // so it is never shown as a clean "ready". |
| 640 | func computeMergeStatus(ci, conflicts, reviewDecision string, behind int) string { |
| 641 | if conflicts == "yes" { |
| 642 | return "blocked" |
| 643 | } |
| 644 | if ci == "failing" { |
| 645 | return "blocked" |
| 646 | } |
| 647 | if strings.EqualFold(reviewDecision, "CHANGES_REQUESTED") { |
| 648 | return "blocked" |
| 649 | } |
| 650 | if ci == "pending" || reviewDecision == "unknown" { |
| 651 | return "pending" |
| 652 | } |
| 653 | if ci == "passing" && conflicts == "none" && (strings.EqualFold(reviewDecision, "APPROVED") || reviewDecision == "") { |
| 654 | if behind > 0 { |
| 655 | return fmt.Sprintf("behind %d", behind) |
| 656 | } |
| 657 | return "ready" |
| 658 | } |
| 659 | return "pending" |
| 660 | } |
| 661 | |
| 662 | func truncate(s string, max int) string { |
| 663 | if len(s) <= max { |
| 664 | return s |
| 665 | } |
| 666 | return s[:max] + "..." |
| 667 | } |
| 668 | |
| 669 | func init() { |
| 670 | rootCmd.AddCommand(statusCmd) |
| 671 | } |
| 672 | |
| 673 | func runBackendName(s *run.State) string { |
| 674 | if s.Type == "track" { |
| 675 | return "-" |
| 676 | } |
| 677 | return runBackendModel(s) |
| 678 | } |
| 679 | |
| 680 | // runBackendModel is a run's backend and model: the model the worker |
| 681 | // reported, else the one launched with, else "(default model)". |
| 682 | func runBackendModel(s *run.State) string { |
| 683 | b := s.Backend |
| 684 | if b == "" { |
| 685 | b = "claude" |
| 686 | } |
| 687 | model := s.ResolvedModel |
| 688 | if model == "" && s.Model != nil { |
| 689 | model = *s.Model |
| 690 | } |
| 691 | return review.BackendModel(b, model) |
| 692 | } |