| 1 | package prose |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | // contentRule is the operator's direction for prose klaus's workers write. |
| 10 | const contentRule = `Say what a reader can't easily infer from the code: why, what's surprising, what to watch for. Drop the details, boundary conditions and machinery the implementation already shows. Prefer short sentences and plain words.` |
| 11 | |
| 12 | const writerRole = `You edit the prose of a code change for humans and future agents. You don't change what it means. |
| 13 | |
| 14 | Your answer is read by a program: reply with one JSON object and nothing else.` |
| 15 | |
| 16 | const checkerRole = `You fact-check rewrites of a code change's prose. You read the original prose, the rewrite and the code, and find each rewrite that changes the meaning. |
| 17 | |
| 18 | Your answer is read by a program: reply with one JSON object and nothing else.` |
| 19 | |
| 20 | func rewritePrompt(dir string, in *input) string { |
| 21 | var b strings.Builder |
| 22 | b.WriteString("Rewrite the prose this change adds.\n\n## Content rule\n" + contentRule + "\n") |
| 23 | if rules := Rules(dir); rules != "" { |
| 24 | b.WriteString("\n## The repository's writing rules\n" + rules + "\n") |
| 25 | } |
| 26 | b.WriteString(` |
| 27 | ## Steps |
| 28 | 1. Ledger: before rewriting, list each claim the added prose makes, with the conditions it holds under, and the warnings it gives. |
| 29 | 2. Rewrite only lines the change adds (the + lines below; the rest is context). In code files, rewrite only user-facing strings, such as help text and messages, and keep the code valid. |
| 30 | 3. Check your rewrite against the ledger: every claim, condition and warning a reader needs is still there, no guarantee is stronger or weaker, and no cause is new. Leave a passage alone when you can't improve it. |
| 31 | |
| 32 | ## Answer |
| 33 | { |
| 34 | "ledger": [{"id": "C1", "where": "README.md", "claim": "...", "conditions": ["..."]}], |
| 35 | "edits": [{"file": "README.md", "old": "exact text from the file, whole lines", "new": "the replacement", "claims": ["C1"]}] |
| 36 | } |
| 37 | "old" must appear exactly once in the file as it is now. For the commit message, use the file "` + CommitFile + `". Return "edits": [] when nothing needs rewriting. |
| 38 | `) |
| 39 | if in.diff != "" { |
| 40 | b.WriteString("\n## The change's prose hunks\n```diff\n" + in.diff + "```\n") |
| 41 | } |
| 42 | if in.message != "" { |
| 43 | b.WriteString("\n## The head commit's message (" + CommitFile + ")\n```\n" + in.message + "\n```\n") |
| 44 | } |
| 45 | return b.String() |
| 46 | } |
| 47 | |
| 48 | // checkPrompt asks for a fact check of r's unskipped edits, or is "" when |
| 49 | // there are none. |
| 50 | func checkPrompt(in *input, r *Result) string { |
| 51 | type numbered struct { |
| 52 | N int `json:"edit"` |
| 53 | Edit |
| 54 | } |
| 55 | var edits []numbered |
| 56 | for i, e := range r.Edits { |
| 57 | if e.Skipped == "" { |
| 58 | edits = append(edits, numbered{i + 1, e}) |
| 59 | } |
| 60 | } |
| 61 | if len(edits) == 0 { |
| 62 | return "" |
| 63 | } |
| 64 | ledger, _ := json.MarshalIndent(r.Ledger, "", " ") |
| 65 | proposed, _ := json.MarshalIndent(edits, "", " ") |
| 66 | var b strings.Builder |
| 67 | fmt.Fprintf(&b, `Fact-check these rewrites of a change's prose. Each edit replaces "old", text the change adds, with "new". Read the code in the repository where a claim depends on it. |
| 68 | |
| 69 | A rewrite fails when it drops a condition, makes a guarantee stronger or weaker, invents a cause, loses a warning, or says something the code doesn't do. Style isn't your concern. |
| 70 | |
| 71 | ## Answer |
| 72 | {"failures": [{"edit": 1, "claim": "C1", "problem": "what the rewrite changed"}]} |
| 73 | Return "failures": [] when every rewrite keeps the meaning. |
| 74 | |
| 75 | ## Claim ledger |
| 76 | %s |
| 77 | |
| 78 | ## Rewrites |
| 79 | %s |
| 80 | `, ledger, proposed) |
| 81 | if in.diff != "" { |
| 82 | b.WriteString("\n## The change's prose hunks\n```diff\n" + in.diff + "```\n") |
| 83 | } |
| 84 | if in.message != "" { |
| 85 | b.WriteString("\n## The head commit's message (" + CommitFile + ")\n```\n" + in.message + "\n```\n") |
| 86 | } |
| 87 | return b.String() |
| 88 | } |