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