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