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