Skip to main content

spar/
schema.rs

1//! JSON schemas for the three structured exchanges.
2//!
3//! These are what make convergence machine checkable instead of regex matching
4//! prose for "LGTM". Three properties are load bearing for strict structured
5//! output and are asserted in the tests: every property appears in `required`,
6//! every object sets `additionalProperties: false`, and an optional field is
7//! spelled as one that may be null rather than one that may be absent.
8//!
9//! The `description` on each field is also the cheapest place to ask for
10//! brevity, since it travels with the request rather than sitting a thousand
11//! tokens back in the prompt.
12
13use serde_json::{json, Value};
14
15pub fn triage() -> Value {
16    json!({
17        "type": "object",
18        "additionalProperties": false,
19        "properties": {
20            "issues": {
21                "type": "array",
22                "items": {
23                    "type": "object",
24                    "additionalProperties": false,
25                    "properties": {
26                        "issue": {"type": "integer", "description": "The issue number."},
27                        "worth_doing": {
28                            "type": "boolean",
29                            "description": "False for duplicates, stale requests, things already fixed, vague reports with nothing reproducible, changes that would make the codebase worse, and tracking issues. Set tracker as well for the last of those."
30                        },
31                        "tracker": {
32                            "type": "boolean",
33                            "description": "True when the issue exists to hold context for work that is filed elsewhere: an umbrella, an epic, a meta issue whose parts are their own issues. Judge it by what the issue is, not by whether you agree with it. Nothing is opened for a tracker, but it is not finished either: its parts are still open, and the shared context and rejected alternatives it records are why somebody wrote it. False for an ordinary issue, whatever you decided about it."
34                        },
35                        "reason": {
36                            "type": "string",
37                            "description": "One sentence. This is posted verbatim on the issue when both agents decline it, and the issue may well stay open afterwards, so write it for the person who opened it rather than as a verdict."
38                        },
39                        "complexity": {
40                            "type": "string",
41                            "enum": ["s", "m", "l"],
42                            "description": "How much work the change is. s: one file, or a handful of lines across two, covered by an existing test or one obvious one. m: several files in one area, or a new function others call, or one that needs its own tests written. l: a new module, a change to an interface other code depends on, a format that has to migrate, or anything you cannot verify by running something that already exists. This decides the order the run works in, cheapest first, and the larger of the two reviewers' answers is used."
43                        },
44                        "depends_on": {
45                            "type": "array",
46                            "items": {"type": "integer"},
47                            "description": "Issue numbers from this same list that should land first. Empty if none."
48                        },
49                        "risk": {
50                            "type": "string",
51                            "enum": ["low", "med", "high"],
52                            "description": "The blast radius if the change is wrong, and how easily it is undone. low: contained in one place and a revert puts everything back. med: other code calls into it, or it changes behaviour somebody may rely on, but the failure is visible. high: it touches data that persists, a security or permission boundary, or a path whose failure is silent. The larger of the two reviewers' answers is used."
53                        }
54                    },
55                    "required": ["issue", "worth_doing", "tracker", "reason", "complexity", "depends_on", "risk"]
56                }
57            }
58        },
59        "required": ["issues"]
60    })
61}
62
63pub fn review() -> Value {
64    json!({
65        "type": "object",
66        "additionalProperties": false,
67        "properties": {
68            "verdict": {
69                "type": "string",
70                "enum": ["approve", "changes_requested"],
71                "description": "Your overall call. The findings and next_action are what act, so this has to agree with them: approve means you raised nothing blocking. A disagreement between the two is reported and the findings decide."
72            },
73            "next_action": {"type": "string", "enum": ["merge", "fix_myself", "hand_back"]},
74            "summary": {
75                "type": "string",
76                "description": "One sentence, at most 200 characters. No preamble, no restating the diff."
77            },
78            "findings": {
79                "type": "array",
80                "items": {
81                    "type": "object",
82                    "additionalProperties": false,
83                    "properties": {
84                        "severity": {
85                            "type": "string",
86                            "enum": ["blocking", "non-blocking", "nit"],
87                            "description": "blocking: this should not merge as is, real defects only. non-blocking: real, and smaller than holding the merge for. A minor defect belongs here as much as an improvement does. nit: style or taste."
88                        },
89                        "title": {
90                            "type": "string",
91                            "description": "Under 80 characters. State the defect, not the fix."
92                        },
93                        "detail": {
94                            "type": "string",
95                            "description": "Say what goes wrong, how to reproduce it, and where in the code. For a blocking finding, say what you did to confirm it. Do not restate the title. Lead with one sentence that stands on its own: a shortened form of this appears in the pull request thread, while the full text becomes the body if this is filed as its own issue. A fenced code block is welcome and is never truncated."
96                        },
97                        "file": {
98                            "type": "string",
99                            "description": "Path, with a line number if you have one. Empty string if the finding is general."
100                        },
101                        "problem": {
102                            "type": ["string", "null"],
103                            "description": "Only when in_scope is false, null otherwise. What is wrong, with the specifics: the function, the call it does not make, the condition it does not check. Name things in backticks. This becomes the Problem section of an issue somebody picks up cold, so write what they need rather than what fits on a line."
104                        },
105                        "reproduction": {
106                            "type": ["string", "null"],
107                            "description": "Only when in_scope is false, null otherwise. Numbered steps to reproduce it, then a short 'Actual result:' list of what happens. If part of what happens is correct and only part is the defect, say which, so nobody chases the wrong thing."
108                        },
109                        "impact": {
110                            "type": ["string", "null"],
111                            "description": "Only when in_scope is false, null otherwise. What it costs somebody: what an operator or a user can do, or loses, because of this. One short paragraph."
112                        },
113                        "expected": {
114                            "type": ["string", "null"],
115                            "description": "Only when in_scope is false, null otherwise. What it should do instead, as a list of requirements specific enough to implement and to test. Say if the behaviour predates this branch."
116                        },
117                        "in_scope": {
118                            "type": "boolean",
119                            "description": "False only for a real defect that exists, that this PR did not cause, and that is worth somebody stopping to fix. It becomes a tracked item a maintainer has to read and triage, so the bar is a defect, not an observation. A thorough reviewer can always find something adjacent; that is not a reason to file it. If you are not sure it is worth a maintainer's time, say your piece in the finding and label it non-blocking."
120                        }
121                    },
122                    "required": [
123                        "severity",
124                        "title",
125                        "detail",
126                        "file",
127                        "in_scope",
128                        "problem",
129                        "reproduction",
130                        "impact",
131                        "expected"
132                    ]
133                }
134            }
135        },
136        "required": ["verdict", "next_action", "summary", "findings"]
137    })
138}
139
140/// What the implementor reports back, and the pull request body it becomes.
141///
142/// The body used to be one scraped `SUMMARY:` line under a `Closes #N`, which
143/// told a reviewer opening the diff cold nothing: not what was wrong, not what
144/// the change does about it, not how to check it. Asking for those separately
145/// is what puts them there, and composing the body from the fields rather than
146/// from the model's prose is what keeps it short enough to read.
147pub fn implementation() -> Value {
148    json!({
149        "type": "object",
150        "additionalProperties": false,
151        "properties": {
152            "not_worth_doing": {
153                "type": "boolean",
154                "description": "True if, having read the code, this should not be implemented: a duplicate, already fixed, too vague to act on, or a change that would make the codebase worse. Make no changes when this is true."
155            },
156            "reason": {
157                "type": "string",
158                "description": "Only when not_worth_doing is true, empty string otherwise. One or two sentences, posted verbatim on the issue, so write it for the person who opened it."
159            },
160            "summary": {
161                "type": "string",
162                "description": "One plain sentence saying what changed, at most 200 characters. It leads the pull request body, and it carries one fact: what this does now that it did not before. Not the signatures, not the null handling, not the edge cases, all of which belong in changes. If it needs a comma to join two ideas, or reads like a changelog line, it is carrying too much."
163            },
164            "problem": {
165                "type": "string",
166                "description": "Two to four short sentences on what was actually wrong and what it cost, as you understand it now that you have read the code. One fact each: a sentence naming three functions and their signatures is one the reviewer has to decipher, and splitting it costs a few words and saves them that. Not a restatement of the issue, which the reviewer can open for themselves: what you found. Empty string for a feature request with no defect behind it, where a sentence on why it is worth having belongs here instead."
167            },
168            "changes": {
169                "type": "array",
170                "items": {"type": "string"},
171                "description": "One short line per change that alters behaviour, in the order a reader should meet them. Say what the code now does, and name the function or file in backticks. This is where a signature or a null case belongs, one per line, rather than crowded into the summary. Not a list of touched files: the diff already has that. Empty when the summary covers it, which for a small change it does."
172            },
173            "testing": {
174                "type": "array",
175                "items": {"type": "string"},
176                "description": "How a reviewer confirms this works, as lines they can act on: the exact command in backticks, or the steps and what to look for. Say what you actually ran, not what could be run. Name the test that covers the fix. Empty only when there is genuinely nothing to run."
177            },
178            "notes": {
179                "type": ["string", "null"],
180                "description": "Null unless there is something the reviewer would otherwise have to ask about: a deliberate omission, a decision worth defending, a risk you are taking knowingly. Not a summary of the above, and not an apology."
181            }
182        },
183        "required": ["not_worth_doing", "reason", "summary", "problem", "changes", "testing", "notes"]
184    })
185}
186
187pub fn response() -> Value {
188    json!({
189        "type": "object",
190        "additionalProperties": false,
191        "properties": {
192            "summary": {
193                "type": "string",
194                "description": "One sentence, at most 200 characters."
195            },
196            "dispositions": {
197                "type": "array",
198                "items": {
199                    "type": "object",
200                    "additionalProperties": false,
201                    "properties": {
202                        "title": {
203                            "type": "string",
204                            "description": "Copy the reviewer's finding title exactly, so the two can be matched up."
205                        },
206                        "file": {
207                            "type": "string",
208                            "description": "Copy the reviewer's file for this finding exactly. Empty string if it had none."
209                        },
210                        "action": {
211                            "type": "string",
212                            "enum": ["fixed", "refuted", "filed_issue"],
213                            "description": "fixed: valid and in scope, you fixed it. refuted: the point is wrong or not worth acting on. filed_issue: valid but unrelated to this PR."
214                        },
215                        "reasoning": {
216                            "type": "string",
217                            "description": "One or two sentences. For a refutation this is the whole argument, so make it the reason and not an apology."
218                        },
219                        "new_issue_title": {
220                            "type": ["string", "null"],
221                            "description": "Only for filed_issue, null otherwise."
222                        },
223                        "new_issue_body": {
224                            "type": ["string", "null"],
225                            "description": "Only for filed_issue, null otherwise. This becomes an issue body somebody picks up cold, so use these markdown sections, skipping any that do not apply: `## Problem` with the specifics, `## Reproduction` with numbered steps and an Actual result list, `## Impact` with what it costs somebody, and `## Expected behavior` as requirements specific enough to implement and to test. Substance rather than length: no preamble, no restating the title. A fenced code block is welcome and is never truncated."
226                        }
227                    },
228                    "required": ["title", "file", "action", "reasoning", "new_issue_title", "new_issue_body"]
229                }
230            }
231        },
232        "required": ["summary", "dispositions"]
233    })
234}
235
236/// One reviewer judging the other reviewer's findings.
237///
238/// Used only in review only mode, where nobody is going to fix anything and the
239/// product is the finding list itself. Asking each model to read the code and
240/// rule on the other's claims is what separates a defect worth a maintainer's
241/// attention from one model's pattern match.
242pub fn adjudication() -> Value {
243    json!({
244        "type": "object",
245        "additionalProperties": false,
246        "properties": {
247            "verdicts": {
248                "type": "array",
249                "items": {
250                    "type": "object",
251                    "additionalProperties": false,
252                    "properties": {
253                        "title": {
254                            "type": "string",
255                            "description": "Copy the finding's title exactly, so it can be matched up."
256                        },
257                        "file": {
258                            "type": "string",
259                            "description": "Copy the finding's file exactly. Empty string if it had none."
260                        },
261                        "agrees": {
262                            "type": "boolean",
263                            "description": "True only if you read the code and the defect is real. Do not defer to the other reviewer, and do not agree to be agreeable: a finding you cannot confirm is one a maintainer should not have to spend time on."
264                        },
265                        "severity": {
266                            "type": "string",
267                            "enum": ["blocking", "non-blocking", "nit"],
268                            "description": "Your own view of how badly it matters, even where you agree the defect is real."
269                        },
270                        "reasoning": {
271                            "type": "string",
272                            "description": "One or two sentences. If you disagree, this is the whole argument, so give the reason rather than an opinion."
273                        }
274                    },
275                    "required": ["title", "file", "agrees", "severity", "reasoning"]
276                }
277            }
278        },
279        "required": ["verdicts"]
280    })
281}
282
283/// Both agents, after a reviewed pull request moved: a ruling per earlier
284/// finding, matched back by the number the prompt gave it, and ordinary
285/// findings for anything new in the delta. The finding item is `review()`'s,
286/// so there is one definition of a finding.
287pub fn recheck() -> Value {
288    let mut findings = review()["properties"]["findings"].clone();
289    findings["description"] = json!(
290        "Only what is new since the last review. An earlier point belongs in its ruling, not here."
291    );
292    json!({
293        "type": "object",
294        "additionalProperties": false,
295        "properties": {
296            "summary": {
297                "type": "string",
298                "description": "One sentence, at most 200 characters, on what changed since the last review and whether it holds up."
299            },
300            "rulings": {
301                "type": "array",
302                "items": {
303                    "type": "object",
304                    "additionalProperties": false,
305                    "properties": {
306                        "index": {
307                            "type": "integer",
308                            "description": "The number the earlier finding was given in the list. Copy it exactly, so the ruling can be matched back to the point it is about."
309                        },
310                        "outcome": {
311                            "type": "string",
312                            "enum": ["fixed", "rebutted", "open"],
313                            "description": "fixed: the code now answers the point. rebutted: the code did not change for it, and a quoted response shows the point was wrong or does not apply here. open: neither. Say open when you are unsure: an open point costs one more look, a wrongly closed one ships."
314                        },
315                        "reasoning": {
316                            "type": "string",
317                            "description": "One or two sentences. For fixed, the file and line that answers it and what you did to confirm that. For rebutted, which response and why it holds against the code. For open, what is still missing. This is posted for the author, so write it to them."
318                        }
319                    },
320                    "required": ["index", "outcome", "reasoning"]
321                }
322            },
323            "findings": findings
324        },
325        "required": ["summary", "rulings", "findings"]
326    })
327}
328
329/// One agent ruling on recorded follow-ups, before any becomes an issue.
330///
331/// Deliberately not the triage schema. Triage asks whether an issue is worth a
332/// pull request; this asks whether a note written weeks ago is still true of the
333/// code, which is a different question with a different expensive mistake:
334/// dropping something real, rather than scheduling something small.
335pub fn screen() -> Value {
336    json!({
337        "type": "object",
338        "additionalProperties": false,
339        "properties": {
340            "entries": {
341                "type": "array",
342                "items": {
343                    "type": "object",
344                    "additionalProperties": false,
345                    "properties": {
346                        "entry": {
347                            "type": "integer",
348                            "description": "The number this entry was given in the list above. Copy it exactly, so the verdict can be matched back to the entry it is about."
349                        },
350                        "verdict": {
351                            "type": "string",
352                            "enum": ["still_relevant", "already_fixed", "not_worth_it", "duplicate"],
353                            "description": "still_relevant files it as an issue. The other three take it out of the queue and file nothing, so say still_relevant when you are unsure: what survives is triaged by both agents afterwards and can be declined there, while what is dropped here is dropped."
354                        },
355                        "title": {
356                            "type": "string",
357                            "description": "The entry's title, which becomes the issue title. Copy it across unchanged unless it is wrong or says nothing, in which case write one that states the defect."
358                        },
359                        "reason": {
360                            "type": "string",
361                            "description": "One sentence. For already_fixed, name the function or the change that fixed it, so somebody can check you. For anything but still_relevant this is the only record of why the entry was dropped, so give the reason rather than the verdict again."
362                        },
363                        "duplicate_of_issue": {
364                            "type": ["integer", "null"],
365                            "description": "Only when the verdict is duplicate and an open issue already covers it. The issue number, null otherwise."
366                        },
367                        "duplicate_of_entry": {
368                            "type": ["integer", "null"],
369                            "description": "Only when the verdict is duplicate and another entry in this same list covers it. That entry's number from the list above, null otherwise. Kept apart from duplicate_of_issue because an entry is not an issue: it may itself be dropped, and then this one is kept rather than lost."
370                        }
371                    },
372                    "required": ["entry", "verdict", "title", "reason", "duplicate_of_issue", "duplicate_of_entry"]
373                }
374            }
375        },
376        "required": ["entries"]
377    })
378}
379
380/// One agent ruling on a whole queue at once: is this worth splitting.
381///
382/// One call rather than N, for the reason `screen` is one call: a repository
383/// aware pass is the dominant cost, and "is this too big to work in one piece"
384/// is partly a judgement across the queue rather than about each item alone.
385pub fn split_screen() -> Value {
386    json!({
387        "type": "object",
388        "additionalProperties": false,
389        "properties": {
390            "items": {
391                "type": "array",
392                "items": {
393                    "type": "object",
394                    "additionalProperties": false,
395                    "properties": {
396                        "item": {
397                            "type": "integer",
398                            "description": "The issue or pull request number from the list above, copied exactly, so the verdict can be matched back to what it is about."
399                        },
400                        "split": {
401                            "type": "boolean",
402                            "description": "True only for something that is plainly several separate pieces of work and would be reviewed better as several. Say false when you are unsure: no is the common answer, and a split proposed on a whim is a proposal somebody now has to read. Size alone is not the test, since a forty file rename is one piece of work and a three file mess can be three."
403                        },
404                        "reason": {
405                            "type": "string",
406                            "description": "One sentence. For true, name the pieces you can see, so somebody can check you before two more agent calls are spent on it."
407                        }
408                    },
409                    "required": ["item", "split", "reason"]
410                }
411            }
412        },
413        "required": ["items"]
414    })
415}
416
417/// One agent decomposing one issue or one pull request.
418///
419/// The descriptions carry the two rules the command rests on: a part that
420/// cannot stand on its own is not a part, and the whole value of splitting is
421/// that each piece can be reviewed and merged by itself.
422pub fn split_proposal() -> Value {
423    json!({
424        "type": "object",
425        "additionalProperties": false,
426        "properties": {
427            "should_split": {
428                "type": "boolean",
429                "description": "False when this is one piece of work, however large. A rename across forty files is one change; three unrelated fixes in one file are three. False is a fine answer and costs one person one read of something that stays as it was."
430            },
431            "reason": {
432                "type": "string",
433                "description": "One or two sentences on why it should or should not be split. For false this is the whole argument, so give the reason rather than the verdict again."
434            },
435            "stacked": {
436                "type": "boolean",
437                "description": "True only when the parts are genuinely sequential, so each needs its predecessor to make sense. Stacked parts are created in order, each branched off the one before. False means each part stands on the base branch and can be reviewed and merged in any order, which is what makes the review loop cheaper, so prefer it wherever the change allows."
438            },
439            "parts": {
440                "type": "array",
441                "items": {
442                    "type": "object",
443                    "additionalProperties": false,
444                    "properties": {
445                        "title": {
446                            "type": "string",
447                            "description": "Under 80 characters. States the piece of work, not the fact that it was split out."
448                        },
449                        "body": {
450                            "type": "string",
451                            "description": "For an issue, the body of the issue this part becomes, written for somebody picking it up cold with no sight of the parent: what is wrong, how to see it, what it should do instead. For a pull request slice, what this slice is and why it stands on its own. Either way, no preamble and no restating the title."
452                        },
453                        "files": {
454                            "type": ["array", "null"],
455                            "items": {"type": "string"},
456                            "description": "Only when splitting a pull request, null when splitting an issue. The paths from the change that this slice carries, copied exactly from the list you were given. Every path belongs to at most one part, and a part whose files cannot build and pass on their own is not a part: fold it into another or leave it out."
457                        }
458                    },
459                    "required": ["title", "body", "files"]
460                }
461            }
462        },
463        "required": ["should_split", "reason", "stacked", "parts"]
464    })
465}
466
467/// The second agent ruling on the first one's decomposition.
468///
469/// Not a second proposal. Two decompositions of one thing cannot be reconciled
470/// mechanically, so this agent rules on the one in front of it: accept, reject,
471/// or accept with named parts struck.
472pub fn split_check() -> Value {
473    json!({
474        "type": "object",
475        "additionalProperties": false,
476        "properties": {
477            "accept": {
478                "type": "boolean",
479                "description": "True only if you read the code and this decomposition is right. Do not defer to the other agent and do not agree to be agreeable. Getting a rejection wrong costs one person one read of something that stays as it was; getting an acceptance wrong costs them issues to close, a checklist to strip out of somebody's body, and branches and pull requests to delete."
480            },
481            "stacked": {
482                "type": "boolean",
483                "description": "Your own view of whether the parts are sequential, whatever the other agent said. True means each part is branched off the one before it."
484            },
485            "strike": {
486                "type": "array",
487                "items": {"type": "integer"},
488                "description": "The numbers of any parts, from 1, that should not be split out: a part that cannot stand on its own, one that is really the same work as another, one that is too small to be worth its own review. Empty to accept the parts as proposed. Striking so many that fewer than two remain means nothing is split, which is the right answer when that is what you think."
489            },
490            "reasoning": {
491                "type": "string",
492                "description": "One or two sentences saying what you checked and what it showed. Required when you reject or strike anything, and useful when you accept."
493            }
494        },
495        "required": ["accept", "stacked", "strike", "reasoning"]
496    })
497}
498
499/// One agent judging the comments other people left on a pull request.
500///
501/// The descriptions carry the asymmetry the whole command rests on, because
502/// they travel with the request rather than sitting a thousand tokens back in
503/// the prompt: getting a decline wrong costs a person one read of a thread that
504/// stays open for them, and getting an implement wrong costs them a commit they
505/// did not ask for on a branch they own.
506pub fn checkin() -> Value {
507    json!({
508        "type": "object",
509        "additionalProperties": false,
510        "properties": {
511            "verdicts": {
512                "type": "array",
513                "items": {
514                    "type": "object",
515                    "additionalProperties": false,
516                    "properties": {
517                        "ref_id": {
518                            "type": "string",
519                            "description": "The handle printed beside the comment, copied across exactly, so your answer can be matched back to it."
520                        },
521                        "ask": {
522                            "type": "string",
523                            "enum": ["implement", "defer", "decline", "answer", "nothing"],
524                            "description": "implement: the change is right and belongs on this branch. defer: the change is right and is really its own piece of work, so supply new_issue_title and new_issue_body. decline: the change should not be made. answer: a question rather than a request. nothing: nothing is being asked for."
525                        },
526                        "request": {
527                            "type": "string",
528                            "description": "What is being asked for, in one sentence, in your own words. This is how the harness checks the comment was understood before acting on it, so restate the request rather than the comment."
529                        },
530                        "reasoning": {
531                            "type": "string",
532                            "description": "One or two sentences. For decline this is the whole argument and it is posted in the thread, so give the reason and write it for the person who raised the point, not for this harness."
533                        },
534                        "unambiguous": {
535                            "type": "boolean",
536                            "description": "False if the comment could be read more than one way, or if you are guessing at what it wants. False costs a reply asking what was meant; a wrong true costs somebody a commit on their branch that they did not ask for."
537                        },
538                        "new_issue_title": {
539                            "type": ["string", "null"],
540                            "description": "Only when ask is defer, null otherwise. States the defect, not the fix."
541                        },
542                        "new_issue_body": {
543                            "type": ["string", "null"],
544                            "description": "Only when ask is defer, null otherwise. Written for somebody picking it up cold months later, not for whoever is reading this thread today."
545                        }
546                    },
547                    "required": ["ref_id", "ask", "request", "reasoning", "unambiguous", "new_issue_title", "new_issue_body"]
548                }
549            }
550        },
551        "required": ["verdicts"]
552    })
553}
554
555/// The second agent ruling on the first one's calls.
556pub fn checkin_check() -> Value {
557    json!({
558        "type": "object",
559        "additionalProperties": false,
560        "properties": {
561            "checks": {
562                "type": "array",
563                "items": {
564                    "type": "object",
565                    "additionalProperties": false,
566                    "properties": {
567                        "ref_id": {
568                            "type": "string",
569                            "description": "The handle printed beside the comment, copied across exactly."
570                        },
571                        "agrees": {
572                            "type": "boolean",
573                            "description": "True only if you went to the code and confirmed the call. Do not defer to the other agent and do not agree to be agreeable: a decision you cannot confirm is one that is about to put a commit on somebody's branch in their name."
574                        },
575                        "ask": {
576                            "type": "string",
577                            "enum": ["implement", "defer", "decline", "answer", "nothing"],
578                            "description": "What you would do instead. Read only when agrees is false."
579                        },
580                        "unambiguous": {
581                            "type": "boolean",
582                            "description": "False if the comment could be read more than one way, whatever the other agent said about it."
583                        },
584                        "reasoning": {
585                            "type": "string",
586                            "description": "One or two sentences saying what you checked and what it showed. Required when you disagree, and useful when you agree."
587                        }
588                    },
589                    "required": ["ref_id", "agrees", "ask", "unambiguous", "reasoning"]
590                }
591            }
592        },
593        "required": ["checks"]
594    })
595}
596
597/// What the fix pass reports back about each change it was asked to make.
598pub fn checkin_fix() -> Value {
599    json!({
600        "type": "object",
601        "additionalProperties": false,
602        "properties": {
603            "done": {
604                "type": "array",
605                "items": {
606                    "type": "object",
607                    "additionalProperties": false,
608                    "properties": {
609                        "ref_id": {
610                            "type": "string",
611                            "description": "The handle printed beside the comment, copied across exactly."
612                        },
613                        "changed": {
614                            "type": "boolean",
615                            "description": "False if the change turned out to be wrong once you were in the code. You are not obliged to make a change you now believe is a mistake, and saying so is a better answer than making it."
616                        },
617                        "summary": {
618                            "type": "string",
619                            "description": "One sentence naming what changed, or why it was left alone. This is posted in the thread the comment sits in, so write it for the person who asked rather than for this harness."
620                        },
621                        "files": {
622                            "type": "array",
623                            "items": {"type": "string"},
624                            "description": "Repository relative paths this comment's change touched, empty when nothing was changed. Checked against the diff: a comment whose files are not in it is answered in words rather than reported as fixed."
625                        }
626                    },
627                    "required": ["ref_id", "changed", "summary", "files"]
628                }
629            }
630        },
631        "required": ["done"]
632    })
633}
634
635/// A reviewer's report on fixing its own findings.
636///
637/// Same shape as a response, minus the parts that only make sense from the
638/// author's side: a reviewer fixing its own point cannot refute it, and filing
639/// it as somebody else's issue is a decision for the round rather than for this
640/// call.
641pub fn own_fix() -> Value {
642    json!({
643        "type": "object",
644        "additionalProperties": false,
645        "properties": {
646            "summary": {
647                "type": "string",
648                "description": "One sentence naming what changed, at most 200 characters. This becomes the commit subject, so write it as one."
649            },
650            "fixes": {
651                "type": "array",
652                "items": {
653                    "type": "object",
654                    "additionalProperties": false,
655                    "properties": {
656                        "title": {
657                            "type": "string",
658                            "description": "Copy your own finding title exactly, so the two can be matched up."
659                        },
660                        "file": {
661                            "type": "string",
662                            "description": "Copy the file from that finding exactly. Empty string if it had none."
663                        },
664                        "fixed": {
665                            "type": "boolean",
666                            "description": "True only if this point is answered by a change in the working tree. False if you left it, whatever the reason."
667                        },
668                        "reason": {
669                            "type": "string",
670                            "description": "For a fix, what you changed and how it answers the point. For one you left, why, in a sentence the author will read: it stays open and goes to them."
671                        }
672                    },
673                    "required": ["title", "file", "fixed", "reason"]
674                }
675            }
676        },
677        "required": ["summary", "fixes"]
678    })
679}
680
681/// One idea's shape, shared by the diverge answer and a cross round's build.
682///
683/// `combines` is required and described at length because it is the whole
684/// method: an idea that cannot name the existing pieces it pairs is a wish.
685fn idea() -> Value {
686    json!({
687        "type": "object",
688        "additionalProperties": false,
689        "properties": {
690            "title": {
691                "type": "string",
692                "description": "One line naming the idea by what it does, not by its ingredients. It becomes a heading, and may become an issue title."
693            },
694            "combines": {
695                "type": "array",
696                "items": {"type": "string"},
697                "description": "The existing techniques, primitives, results, tools, or patterns this idea pairs, one per entry, each named specifically enough to look up. At least two. This is the whole method: the pieces exist already and the pairing is what is new."
698            },
699            "how_it_works": {
700                "type": "string",
701                "description": "The mechanism, concretely enough that somebody could start on it. A paragraph, not a pitch."
702            },
703            "why_new": {
704                "type": "string",
705                "description": "The nearest thing that already exists, named, and what this does that it does not. If you cannot name a nearest thing, look harder before claiming there is none."
706            },
707            "enables": {
708                "type": "string",
709                "description": "What becomes possible that was not, in one or two sentences."
710            },
711            "risks": {
712                "type": "string",
713                "description": "What would make it fail, honestly. The reader decides whether to try it on this."
714            },
715            "first_experiment": {
716                "type": "string",
717                "description": "The cheapest thing that would show whether it works: a prototype, a calculation, a query, a conversation with somebody who would know."
718            }
719        },
720        "required": ["title", "combines", "how_it_works", "why_new", "enables", "risks", "first_experiment"]
721    })
722}
723
724pub fn brainstorm_ideas() -> Value {
725    json!({
726        "type": "object",
727        "additionalProperties": false,
728        "properties": {
729            "ideas": {
730                "type": "array",
731                "items": idea(),
732                "description": "The ideas, best first. Each pairs existing pieces in a way that has not been done."
733            }
734        },
735        "required": ["ideas"]
736    })
737}
738
739/// A cross round: one agent ruling on ideas the other proposed.
740///
741/// Matched back by number rather than title, for the reason the screen schema
742/// carries an entry number: a model rewrites titles.
743pub fn brainstorm_cross() -> Value {
744    let mut build = idea();
745    build["type"] = json!(["object", "null"]);
746    build["description"] = json!("The revised idea in full when the verdict is build, naming everything it combines including the new piece. Null for any other verdict.");
747    json!({
748        "type": "object",
749        "additionalProperties": false,
750        "properties": {
751            "verdicts": {
752                "type": "array",
753                "items": {
754                    "type": "object",
755                    "additionalProperties": false,
756                    "properties": {
757                        "idea": {
758                            "type": "integer",
759                            "description": "The number the idea was given in the list above. Copy it exactly, so the ruling can be matched back to the idea it is about."
760                        },
761                        "verdict": {
762                            "type": "string",
763                            "enum": ["build", "challenge", "keep"],
764                            "description": "build: you can see a further piece that completes it or makes it stronger, and you have written the revised idea in build. challenge: it already exists and you can name where, or it cannot work and you can say why. keep: real and new as far as you can tell, nothing to add."
765                        },
766                        "reason": {
767                            "type": "string",
768                            "description": "For challenge, the thing that already exists or the reason it cannot work, specifically: the proposer answers this with specifics. For build, what the new piece adds. For keep, one sentence or empty."
769                        },
770                        "build": build
771                    },
772                    "required": ["idea", "verdict", "reason", "build"]
773                }
774            }
775        },
776        "required": ["verdicts"]
777    })
778}
779
780/// The last round: defend or withdraw what was challenged, and rank the rest.
781pub fn brainstorm_converge() -> Value {
782    json!({
783        "type": "object",
784        "additionalProperties": false,
785        "properties": {
786            "defences": {
787                "type": "array",
788                "items": {
789                    "type": "object",
790                    "additionalProperties": false,
791                    "properties": {
792                        "idea": {
793                            "type": "integer",
794                            "description": "The number of an idea you proposed that was challenged. One entry per challenged idea of yours, and none for anything else."
795                        },
796                        "stands": {
797                            "type": "boolean",
798                            "description": "True only if you can give the specific evidence that settles the objection in reply. False withdraws the idea, which is the right answer when the objection is correct."
799                        },
800                        "reply": {
801                            "type": "string",
802                            "description": "The evidence when it stands: the prior art that does not actually do this, the mechanism the objection missed, the number that makes it work. One sentence when it does not."
803                        }
804                    },
805                    "required": ["idea", "stands", "reply"]
806                }
807            },
808            "ranking": {
809                "type": "array",
810                "items": {"type": "integer"},
811                "description": "Every idea number that is not withdrawn, best first, including your own and the ones you challenged. Novelty and feasibility together: an idea nobody has done that could be tried this month beats a moonshot, and a moonshot beats a small improvement on something that exists."
812            }
813        },
814        "required": ["defences", "ranking"]
815    })
816}
817
818pub fn all() -> Vec<(&'static str, Value)> {
819    vec![
820        ("triage", triage()),
821        ("implementation", implementation()),
822        ("review", review()),
823        ("recheck", recheck()),
824        ("response", response()),
825        ("own_fix", own_fix()),
826        ("screen", screen()),
827        ("checkin", checkin()),
828        ("checkin_check", checkin_check()),
829        ("checkin_fix", checkin_fix()),
830        ("split_screen", split_screen()),
831        ("split_proposal", split_proposal()),
832        ("split_check", split_check()),
833        ("brainstorm_ideas", brainstorm_ideas()),
834        ("brainstorm_cross", brainstorm_cross()),
835        ("brainstorm_converge", brainstorm_converge()),
836    ]
837}
838
839#[cfg(test)]
840mod tests {
841    use super::*;
842
843    /// Yield every object schema, however deeply nested.
844    fn objects(node: &Value, path: String, out: &mut Vec<(String, Value)>) {
845        if let Some(map) = node.as_object() {
846            if map.get("type").and_then(Value::as_str) == Some("object")
847                && map.contains_key("properties")
848            {
849                out.push((path.clone(), node.clone()));
850                if let Some(props) = map.get("properties").and_then(Value::as_object) {
851                    for (key, child) in props {
852                        objects(child, format!("{path}.{key}"), out);
853                    }
854                }
855            }
856            if let Some(items) = map.get("items") {
857                objects(items, format!("{path}[]"), out);
858            }
859        }
860    }
861
862    fn walk(name: &str, schema: &Value) -> Vec<(String, Value)> {
863        let mut out = Vec::new();
864        objects(schema, name.to_string(), &mut out);
865        out
866    }
867
868    /// Strict structured output rejects any property that is not also in
869    /// `required`. The Python original violated this in the response schema
870    /// from the start and nothing caught it, because the response schema is
871    /// only reached when a review is handed back with blocking findings, and
872    /// almost every run approved in round one.
873    #[test]
874    fn every_property_is_required() {
875        for (name, schema) in all() {
876            for (path, node) in walk(name, &schema) {
877                let props: Vec<&String> = node["properties"].as_object().unwrap().keys().collect();
878                let required: Vec<String> = node["required"]
879                    .as_array()
880                    .unwrap_or(&vec![])
881                    .iter()
882                    .filter_map(|v| v.as_str().map(str::to_string))
883                    .collect();
884                for prop in &props {
885                    assert!(
886                        required.contains(prop),
887                        "{path}: {prop} is in properties but not in required. \
888                         Make optional fields nullable instead."
889                    );
890                }
891                assert_eq!(props.len(), required.len(), "{path}: required has extras");
892            }
893        }
894    }
895
896    /// The guard that was missing. A schema field can be added to the struct
897    /// and forgotten in the schema, and every test still passes: the tests
898    /// build the struct in Rust, so they never notice the model was never
899    /// asked. That shipped once, as four bug-report fields the agents were
900    /// never told about, which quietly did nothing.
901    #[test]
902    fn the_review_schema_asks_for_every_field_a_finding_holds() {
903        use crate::model::Finding;
904
905        let asked: Vec<String> = review()["properties"]["findings"]["items"]["properties"]
906            .as_object()
907            .expect("finding properties")
908            .keys()
909            .cloned()
910            .collect();
911
912        // Round-tripping a fully populated Finding names every field serde
913        // knows about, without repeating the list here to drift out of date.
914        let populated = Finding {
915            problem: Some("p".into()),
916            reproduction: Some("r".into()),
917            impact: Some("i".into()),
918            expected: Some("e".into()),
919            ..Finding::default()
920        };
921        let held: Vec<String> = serde_json::to_value(&populated)
922            .expect("serialisable")
923            .as_object()
924            .expect("object")
925            .keys()
926            .cloned()
927            .collect();
928
929        for field in &held {
930            assert!(
931                asked.contains(field),
932                "a Finding holds `{field}` and the schema never asks for it, so the model will \
933                 not fill it and the code reading it will always see nothing"
934            );
935        }
936    }
937
938    /// The same guard for the second look: its findings are `review()`'s
939    /// items, and its rulings have to ask for every field a ruling holds.
940    #[test]
941    fn the_recheck_schema_asks_for_every_field_a_finding_and_a_ruling_hold() {
942        use crate::model::{RecheckRuling, Ruling};
943
944        assert_eq!(
945            review()["properties"]["findings"]["items"],
946            recheck()["properties"]["findings"]["items"],
947            "a second look's findings are first look findings"
948        );
949
950        let asked: Vec<String> = recheck()["properties"]["rulings"]["items"]["properties"]
951            .as_object()
952            .expect("ruling properties")
953            .keys()
954            .cloned()
955            .collect();
956        let held: Vec<String> = serde_json::to_value(RecheckRuling {
957            index: 1,
958            outcome: Ruling::Open,
959            reasoning: "r".into(),
960        })
961        .expect("serialisable")
962        .as_object()
963        .expect("object")
964        .keys()
965        .cloned()
966        .collect();
967        for field in &held {
968            assert!(
969                asked.contains(field),
970                "a ruling holds `{field}`, unasked for"
971            );
972        }
973    }
974
975    /// Every outcome the schema offers is one the parser reads back.
976    #[test]
977    fn the_recheck_outcome_enum_matches_the_parser() {
978        use crate::model::Ruling;
979
980        let offered = recheck()["properties"]["rulings"]["items"]["properties"]["outcome"]["enum"]
981            .as_array()
982            .expect("an enum")
983            .clone();
984        assert_eq!(3, offered.len());
985        for value in offered {
986            let word = value.as_str().expect("a word");
987            assert!(
988                Ruling::parse_lenient(word).is_some(),
989                "{word} is offered and cannot be read back"
990            );
991        }
992    }
993
994    /// The same guard for the check-in exchange. A field added to the struct
995    /// and forgotten in the schema is one the model is never asked for, so the
996    /// code reading it always sees nothing: an `unambiguous` that is never
997    /// filled would default false and answer every comment in words.
998    #[test]
999    fn the_checkin_schema_asks_for_every_field_a_verdict_holds() {
1000        use crate::model::{Ask, CommentVerdict};
1001
1002        let asked: Vec<String> = checkin()["properties"]["verdicts"]["items"]["properties"]
1003            .as_object()
1004            .expect("verdict properties")
1005            .keys()
1006            .cloned()
1007            .collect();
1008
1009        let populated = CommentVerdict {
1010            ref_id: "c1".into(),
1011            ask: Ask::Decline,
1012            request: "r".into(),
1013            reasoning: "w".into(),
1014            unambiguous: true,
1015            new_issue_title: Some("t".into()),
1016            new_issue_body: Some("b".into()),
1017        };
1018        let held: Vec<String> = serde_json::to_value(&populated)
1019            .expect("serialisable")
1020            .as_object()
1021            .expect("object")
1022            .keys()
1023            .cloned()
1024            .collect();
1025
1026        for field in &held {
1027            assert!(
1028                asked.contains(field),
1029                "a CommentVerdict holds `{field}` and the schema never asks for it, so the model \
1030                 will not fill it and the code reading it will always see nothing"
1031            );
1032        }
1033    }
1034
1035    /// The same guard for the split exchange. A part field added to the struct
1036    /// and forgotten here is one the model is never asked for, so a slice would
1037    /// arrive with no files and carry nothing.
1038    #[test]
1039    fn the_split_schemas_ask_for_every_field_they_hold() {
1040        use crate::model::{SplitCheck, SplitPart, SplitProposal};
1041
1042        let cases: Vec<(&str, Value, serde_json::Value)> = vec![
1043            (
1044                "SplitProposal",
1045                split_proposal()["properties"].clone(),
1046                serde_json::to_value(SplitProposal::default()).expect("serialisable"),
1047            ),
1048            (
1049                "SplitPart",
1050                split_proposal()["properties"]["parts"]["items"]["properties"].clone(),
1051                serde_json::to_value(SplitPart::default()).expect("serialisable"),
1052            ),
1053            (
1054                "SplitCheck",
1055                split_check()["properties"].clone(),
1056                serde_json::to_value(SplitCheck::default()).expect("serialisable"),
1057            ),
1058        ];
1059        for (what, asked, held) in cases {
1060            let asked: Vec<String> = asked
1061                .as_object()
1062                .expect("properties")
1063                .keys()
1064                .cloned()
1065                .collect();
1066            for field in held.as_object().expect("object").keys() {
1067                assert!(
1068                    asked.contains(field),
1069                    "a {what} holds `{field}` and the schema never asks for it, so the code \
1070                     reading it will always see nothing"
1071                );
1072            }
1073        }
1074    }
1075
1076    /// A slice with no files carries nothing, so the one field that decides
1077    /// what a part is has to be spelled as nullable rather than left out.
1078    #[test]
1079    fn a_split_part_may_carry_no_files_without_omitting_the_field() {
1080        let types = split_proposal()["properties"]["parts"]["items"]["properties"]["files"]["type"]
1081            .to_string();
1082        assert!(types.contains("null"), "files must accept null: {types}");
1083    }
1084
1085    /// Every value the schema offers has to be one the parser accepts, or an
1086    /// answer that matched the schema exactly is thrown away.
1087    #[test]
1088    fn the_checkin_ask_enum_matches_the_parser() {
1089        use crate::model::Ask;
1090
1091        for schema in [
1092            checkin()["properties"]["verdicts"]["items"]["properties"]["ask"].clone(),
1093            checkin_check()["properties"]["checks"]["items"]["properties"]["ask"].clone(),
1094        ] {
1095            for value in schema["enum"].as_array().expect("an enum") {
1096                let text = value.as_str().expect("a string");
1097                assert!(
1098                    Ask::parse_lenient(text).is_some(),
1099                    "the schema offers `{text}` and the parser refuses it"
1100                );
1101            }
1102        }
1103    }
1104
1105    /// The same guard for the implementation exchange, where a forgotten field
1106    /// means a pull request body with an empty section in it and nobody the
1107    /// wiser.
1108    #[test]
1109    fn the_implementation_schema_asks_for_every_field_it_holds() {
1110        use crate::model::Implementation;
1111
1112        let asked: Vec<String> = implementation()["properties"]
1113            .as_object()
1114            .expect("properties")
1115            .keys()
1116            .cloned()
1117            .collect();
1118
1119        let populated = Implementation {
1120            notes: Some("n".into()),
1121            ..Implementation::default()
1122        };
1123        let held: Vec<String> = serde_json::to_value(&populated)
1124            .expect("serialisable")
1125            .as_object()
1126            .expect("object")
1127            .keys()
1128            .cloned()
1129            .collect();
1130
1131        for field in &held {
1132            assert!(
1133                asked.contains(field),
1134                "an Implementation holds `{field}` and the schema never asks for it, so the \
1135                 pull request body will always be missing that part"
1136            );
1137        }
1138    }
1139
1140    /// Same guard for the other direction of the same exchange.
1141    #[test]
1142    fn the_response_schema_asks_for_every_field_a_disposition_holds() {
1143        use crate::model::{Action, Disposition};
1144
1145        let asked: Vec<String> = response()["properties"]["dispositions"]["items"]["properties"]
1146            .as_object()
1147            .expect("disposition properties")
1148            .keys()
1149            .cloned()
1150            .collect();
1151
1152        let populated = Disposition {
1153            title: "t".into(),
1154            file: "f".into(),
1155            action: Action::Fixed,
1156            reasoning: "r".into(),
1157            new_issue_title: Some("t".into()),
1158            new_issue_body: Some("b".into()),
1159        };
1160        let held: Vec<String> = serde_json::to_value(&populated)
1161            .expect("serialisable")
1162            .as_object()
1163            .expect("object")
1164            .keys()
1165            .cloned()
1166            .collect();
1167
1168        for field in &held {
1169            assert!(
1170                asked.contains(field),
1171                "a Disposition holds `{field}`, unasked for"
1172            );
1173        }
1174    }
1175
1176    #[test]
1177    fn objects_forbid_additional_properties() {
1178        for (name, schema) in all() {
1179            for (path, node) in walk(name, &schema) {
1180                assert_eq!(
1181                    Some(false),
1182                    node["additionalProperties"].as_bool(),
1183                    "{path} allows additional properties"
1184                );
1185            }
1186        }
1187    }
1188
1189    #[test]
1190    fn optional_fields_are_spelled_as_nullable() {
1191        let item = &response()["properties"]["dispositions"]["items"];
1192        for field in ["new_issue_title", "new_issue_body"] {
1193            let types = item["properties"][field]["type"].to_string();
1194            assert!(types.contains("null"), "{field} must accept null: {types}");
1195        }
1196    }
1197
1198    /// The re-litigation guard hashes a refutation by title *and* file. If the
1199    /// disposition cannot carry the file, the key it records can never match
1200    /// the key the next round's finding hashes to, and the guard is dead code.
1201    #[test]
1202    fn a_disposition_carries_the_file_so_the_ledger_key_can_match() {
1203        let props = response()["properties"]["dispositions"]["items"]["properties"].clone();
1204        assert!(
1205            props.get("file").is_some(),
1206            "dispositions must carry a file"
1207        );
1208    }
1209
1210    #[test]
1211    fn severity_and_verdict_enums_match_the_parser() {
1212        use crate::model::{Severity, Verdict};
1213        let sev =
1214            review()["properties"]["findings"]["items"]["properties"]["severity"]["enum"].clone();
1215        for value in sev.as_array().unwrap() {
1216            assert!(
1217                Severity::parse_lenient(value.as_str().unwrap()).is_some(),
1218                "schema offers {value} but the parser rejects it"
1219            );
1220        }
1221        let verdicts = review()["properties"]["verdict"]["enum"].clone();
1222        for value in verdicts.as_array().unwrap() {
1223            assert!(Verdict::parse_lenient(value.as_str().unwrap()).is_some());
1224        }
1225    }
1226
1227    #[test]
1228    fn triage_enums_match_the_parser() {
1229        use crate::model::{Complexity, Risk};
1230        let item = &triage()["properties"]["issues"]["items"]["properties"];
1231        for value in item["complexity"]["enum"].as_array().unwrap() {
1232            assert!(Complexity::parse_lenient(value.as_str().unwrap()).is_some());
1233        }
1234        for value in item["risk"]["enum"].as_array().unwrap() {
1235            assert!(Risk::parse_lenient(value.as_str().unwrap()).is_some());
1236        }
1237    }
1238
1239    #[test]
1240    fn response_action_enum_matches_the_parser() {
1241        use crate::model::Action;
1242        let actions = response()["properties"]["dispositions"]["items"]["properties"]["action"]
1243            ["enum"]
1244            .clone();
1245        for value in actions.as_array().unwrap() {
1246            assert!(Action::parse_lenient(value.as_str().unwrap()).is_some());
1247        }
1248    }
1249}