| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "os" |
| 9 | "strings" |
| 10 | "time" |
| 11 | |
| 12 | "the-valley/flow/internal/backend" |
| 13 | "the-valley/flow/internal/config" |
| 14 | "the-valley/flow/internal/flowenv" |
| 15 | "the-valley/flow/internal/jobs" |
| 16 | "the-valley/flow/internal/location" |
| 17 | "the-valley/flow/internal/prose" |
| 18 | "the-valley/flow/internal/run" |
| 19 | ) |
| 20 | |
| 21 | // startProsePass runs _pre-review's prose pass, on what HEAD changes in dir |
| 22 | // since its merge base with base, beside the peer review. The function it |
| 23 | // returns waits for the pass and prints its report. The pass never fails |
| 24 | // _pre-review. |
| 25 | func startProsePass(ctx context.Context, dir, base string, cfg config.Config, author backend.Kind, houseRules string) func(w io.Writer) { |
| 26 | pc := cfg.Prose() |
| 27 | if !*pc.Enabled { |
| 28 | return func(w io.Writer) { fmt.Fprintln(w, "Prose rewrite: off (pre_review.prose.enabled).") } |
| 29 | } |
| 30 | var out bytes.Buffer |
| 31 | done := make(chan struct{}) |
| 32 | go func() { |
| 33 | defer close(done) |
| 34 | runProsePass(ctx, &out, dir, base, cfg, pc, author, houseRules) |
| 35 | }() |
| 36 | return func(w io.Writer) { |
| 37 | <-done |
| 38 | w.Write(out.Bytes()) |
| 39 | fmt.Fprintln(w) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func runProsePass(ctx context.Context, w io.Writer, dir, base string, cfg config.Config, pc config.ProseConfig, author backend.Kind, houseRules string) { |
| 44 | // A sandbox host has no session to record the pass in. |
| 45 | var store *jobs.Store |
| 46 | if flowenv.RunEvents.Get() != runEventsOutput { |
| 47 | if s, err := sessionStore(); err == nil { |
| 48 | if hds, ok := s.(*run.HomeDirStore); ok { |
| 49 | js := jobs.NewStore(hds.BaseDir()) |
| 50 | store = &js |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | job := &jobs.Job{ID: eventID("prose"), Kind: jobs.KindProse, Dir: dir, Run: flowenv.RunID.Get()} |
| 55 | r := prose.Run(ctx, prose.Options{ |
| 56 | Dir: dir, Base: base, Author: author, |
| 57 | Order: pc.Order, Patterns: pc.Paths, MaxInput: pc.MaxInputBytes, |
| 58 | Timeout: time.Duration(pc.TimeoutSeconds) * time.Second, |
| 59 | HouseRules: houseRules, |
| 60 | Writer: func(k backend.Kind) (string, string) { return cfg.ProseWriter(string(k)) }, |
| 61 | Checker: func(k backend.Kind) (string, string) { return cfg.ProseChecker(string(k)) }, |
| 62 | Started: func(r *prose.Result) { |
| 63 | if store != nil { |
| 64 | job.Backend, job.Model = r.Writer, r.WriterModel |
| 65 | names := r.Files |
| 66 | if len(names) == 0 { |
| 67 | names = []string{"commit message"} |
| 68 | } |
| 69 | job.Prompt = "rewrite " + strings.Join(names, ", ") |
| 70 | warnJob(w, job, store.Start(job)) |
| 71 | } |
| 72 | }, |
| 73 | }) |
| 74 | fmt.Fprintf(w, "Prose rewrite (writer: %s; fact check: %s; author: %s):\n", orDash(r.Writer), orDash(r.Checker), author) |
| 75 | r.PatchFile = saveProse(w, store, job, ".patch", r.Patch) |
| 76 | r.MessageFile = saveProse(w, store, job, ".msg", r.Message) |
| 77 | if store != nil && job.Backend != "" { |
| 78 | job.Prose = r |
| 79 | status := jobs.Succeeded |
| 80 | if r.Error != "" { |
| 81 | status = jobs.Failed |
| 82 | } |
| 83 | warnJob(w, job, store.Finish(job, status, truncateLine(r.Error, 200))) |
| 84 | } |
| 85 | printProse(w, r) |
| 86 | if r.PatchFile != "" || r.MessageFile != "" || len(r.Failures) > 0 { |
| 87 | fmt.Fprintln(w, " Apply the rewrites that keep the meaning; fix or reject the rest. Say in your final report what you applied and why you rejected any.") |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // saveProse writes a pass's patch or commit message beside its job record, |
| 92 | // else in a temporary file, and returns its path; "" when there is nothing |
| 93 | // to write. |
| 94 | func saveProse(w io.Writer, store *jobs.Store, job *jobs.Job, ext, text string) string { |
| 95 | if text == "" { |
| 96 | return "" |
| 97 | } |
| 98 | if store != nil && job.Backend != "" { |
| 99 | path, err := store.WriteFile(job.ID, ext, []byte(text)) |
| 100 | if err == nil { |
| 101 | return path |
| 102 | } |
| 103 | warnJob(w, job, err) |
| 104 | } |
| 105 | f, err := os.CreateTemp("", location.TempPattern("prose-*"+ext)) |
| 106 | if err != nil { |
| 107 | fmt.Fprintf(w, " warning: saving the %s: %v\n", ext[1:], err) |
| 108 | return "" |
| 109 | } |
| 110 | defer f.Close() |
| 111 | if _, err := f.WriteString(text); err != nil { |
| 112 | fmt.Fprintf(w, " warning: saving the %s: %v\n", ext[1:], err) |
| 113 | return "" |
| 114 | } |
| 115 | return f.Name() |
| 116 | } |
| 117 | |
| 118 | // printProse prints a pass's proposal, for _pre-review and klaus jobs show. |
| 119 | func printProse(w io.Writer, r *prose.Result) { |
| 120 | if len(r.Omitted) > 0 { |
| 121 | fmt.Fprintf(w, " Left out, past pre_review.prose.max_input_bytes: %s\n", strings.Join(r.Omitted, ", ")) |
| 122 | } |
| 123 | switch { |
| 124 | case r.Skipped != "": |
| 125 | fmt.Fprintf(w, " Skipped: %s.\n", r.Skipped) |
| 126 | return |
| 127 | case r.Error != "": |
| 128 | fmt.Fprintf(w, " The rewrite failed: %s\n", r.Error) |
| 129 | return |
| 130 | case len(r.Edits) == 0: |
| 131 | fmt.Fprintln(w, " No rewrites proposed.") |
| 132 | return |
| 133 | case r.CheckError != "": |
| 134 | fmt.Fprintf(w, " The fact check failed: %s\n No rewrite passed it; check their meaning yourself.\n", r.CheckError) |
| 135 | } |
| 136 | for i, e := range r.Edits { |
| 137 | status := "passed" |
| 138 | switch { |
| 139 | case e.Skipped != "": |
| 140 | status = "skipped: " + e.Skipped |
| 141 | case r.Failed(i): |
| 142 | status = "failed the fact check" |
| 143 | case r.CheckError != "": |
| 144 | status = "not fact-checked" |
| 145 | } |
| 146 | if e.Outcome != "" { |
| 147 | status += "; " + e.Outcome |
| 148 | } |
| 149 | where := e.File |
| 150 | if where == prose.CommitFile { |
| 151 | where = "commit message" |
| 152 | } |
| 153 | fmt.Fprintf(w, " [%d] %s: %s\n", i+1, where, status) |
| 154 | for _, f := range r.Failures { |
| 155 | if f.Edit == i+1 { |
| 156 | fmt.Fprintf(w, " ✗ %s\n", failureText(f)) |
| 157 | } |
| 158 | } |
| 159 | for _, l := range strings.Split(strings.TrimRight(e.Old, "\n"), "\n") { |
| 160 | fmt.Fprintf(w, " - %s\n", l) |
| 161 | } |
| 162 | for _, l := range strings.Split(strings.TrimRight(e.New, "\n"), "\n") { |
| 163 | fmt.Fprintf(w, " + %s\n", l) |
| 164 | } |
| 165 | } |
| 166 | for _, f := range r.Failures { |
| 167 | if f.Edit < 1 || f.Edit > len(r.Edits) { |
| 168 | fmt.Fprintf(w, " ✗ %s\n", failureText(f)) |
| 169 | } |
| 170 | } |
| 171 | if r.PatchFile != "" { |
| 172 | fmt.Fprintf(w, " Patch of the passing rewrites: git apply %s\n", shellQuote(r.PatchFile)) |
| 173 | } |
| 174 | if r.MessageFile != "" { |
| 175 | fmt.Fprintf(w, " Commit message with its passing rewrites: git commit --amend -F %s\n", shellQuote(r.MessageFile)) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | func failureText(f prose.Failure) string { |
| 180 | if f.Claim != "" { |
| 181 | return f.Claim + ": " + f.Problem |
| 182 | } |
| 183 | return f.Problem |
| 184 | } |
| 185 | |
| 186 | func orDash(s string) string { |
| 187 | if s == "" { |
| 188 | return "-" |
| 189 | } |
| 190 | return s |
| 191 | } |
| 192 | |
| 193 | // settleProseJobs records, in run id's prose jobs, which rewrites the |
| 194 | // worktree took. Finalize calls it before removing the worktree. |
| 195 | func settleProseJobs(baseDir, id, worktree string) { |
| 196 | if baseDir == "" || worktree == "" { |
| 197 | return |
| 198 | } |
| 199 | store := jobs.NewStore(baseDir) |
| 200 | list, _ := store.List() |
| 201 | for _, j := range list { |
| 202 | if j.Kind == jobs.KindProse && j.Run == id && j.Prose != nil && j.Finished() && j.Status != jobs.Died { |
| 203 | prose.Settle(worktree, j.Prose) |
| 204 | _ = store.Save(j) |
| 205 | } |
| 206 | } |
| 207 | } |