A repository's own judge
On this page
The factory file sorts changes by path: these paths are risky, those need no review. Some repositories know more than a path can tell. An edit inside one ordinary function of a large file is not the same as an edit to its sign-in code. A migration that only creates tables is not the same as one that drops them. Deleting a file that was only ever a plan needs nobody's eyes.
A repository that has such rules, as code, can name them as its judge. The gate then asks the judge how much care each change needs, and so does the automatic reviewer. The repository keeps one set of rules, in one place, for everything that asks.
#Naming a judge
intake:
- station: qa
uses: independent-review
standard: docs/REVIEW_STANDARD.md
never_skip: ["**/AGENTS.md"]
judge:
run: scripts/ci/factory_judge.py # a Python script in this repository
with: [scripts/ci/risk_rules.py] # other files it needs beside it
contents: [api/index.py] # changed files whose text, before and after, it is handed
| Key | Meaning |
|---|---|
run | The script. A path in the repository ending .py |
with | Up to 20 other files it needs. They are put at the same paths beside it. A plain import finds the ones in the script's own folder (or in a package under it); for a file elsewhere, add its folder to sys.path in the script. Do not name a file after a standard-library module (json.py): the script would import yours |
contents | Path patterns. A changed file that matches (and is not removed) is handed over with its whole text at the pull request's head and at the point it branched from |
When a judge is named, it decides the level of the whole change. The factory file's risk list and skip_when_only are not used for that.
It needs Open Factory Floor v0.8.0 or later. An earlier gate ignores the key and uses the lists, so keep the lists in the factory file until every workflow that reads it is on v0.8.0 or later: with the lists gone, an earlier gate would call every change ordinary code and nothing high risk. A judge block that is malformed makes the gate report error on every pull request until it is mended.
#What the judge is given and must answer
The gate runs python3 scripts/ci/factory_judge.py with one JSON object on standard input:
{"factory_judge": 1, "pull_request": 12,
"files": [{"filename": "api/index.py", "status": "modified", "patch": "@@ -1 +1 @@ ...",
"content": "the whole file at the head commit", "base_content": "the whole file where the branch left"},
{"filename": "b.py", "status": "renamed", "previous_filename": "a.py"}]}
files is GitHub's list of the pull request's files, each entry as GitHub gives it (filename, status, additions, deletions, changes, sha, patch, previous_filename and so on; a field whose value is itself a list or an object is left out). patch is absent when GitHub gives none (a binary file, a very large diff).
content and base_content are present for every changed file that matches contents and is not removed. Each is the text or null. base_content is "" for a new file, and for a renamed file it is the text under the old name; a file renamed to or away from a path in contents is handed its text too. Either can be null, and the pull request's author can make it so: when the text could not be had, when one text is over ten million characters, when the texts handed over would pass thirty million characters in all, when more than twenty files match, or when time has run out. A rule that needs the text must treat null as high risk, never as "nothing changed".
It must print one JSON object on standard output and exit 0:
{"review": "high",
"reasons": [{"area": "auth", "file": "api/index.py", "rule": "login() changed", "why": "decides who is let in"}],
"code": ["api/index.py"]}
| Field | Meaning |
|---|---|
review | none (no review needed), code (a review) or high (a review, and high risk) |
reasons | For high: one for each thing that makes it so. area is required and is what the status names, so make it one of a fixed set of words and never text from the change; file is the changed file the reason is about, exactly as it is named in the question (a file that is not text makes the answer a failed judge; a reason with no file is about the change as a whole, and a release gate holds every line for it); file, rule and why are shown to the reviewer |
code | The changed files the review is of. Left out, it is every changed file |
A script that already has such rules needs only a few lines:
import json, sys
import risk_rules
files = json.load(sys.stdin)["files"]
print(json.dumps(risk_rules.classify(files)))
#What stays the factory's
Whatever the judge answers:
- The factory file, the review standard, the judge's script and the files named in
withare always high risk. A change to the rules is judged by the rules as they were, and always gets the most careful review. - A path listed under
never_skipalways needs a review. - A reason makes a change high. An answer cannot name a risk and call the change safe.
- A judge that fails makes the change high risk. If it exits with an error, takes more than 60 seconds (or what is left of the run's allowance), prints something that is not the answer above, or one of its files is missing from the default branch, the change is treated as high risk, with the area
judge failedand what went wrong (its exit code or the kind of fault; what it printed to standard error is not kept, so run it by hand to see that). A judge can never make a change need less care by breaking. - The status says "being judged" first. Before each run of a judge, on whichever of its looks at the commit, the gate sets the status to pending ("Working out how much care this change needs"). If the run dies while judging, the commit is left pending and not on an earlier success. Pending blocks the merge only where the status is a required check. Re-run the gate's job.
- One allowance of time, and each pull request judged once. Several open pull requests can share a head commit, and each is judged. One run of the gate has 150 seconds of judging in all; a pull request that gets no time is treated as high risk. The gate looks at a commit more than once in a run, to confirm what it wrote, and it does not ask the judge again for a pull request it has already asked about (unless that pull request's files or base, or the review station's part of the factory file, have changed since), so confirming is quick. The allowance covers the judging and the fetching of file text; it is for the whole job, however many commits it settles. A change to the judge's own files on the default branch during a run is seen by the next run, not by that one; a change to the review station's part of the factory file is seen at the gate's next look.
#How it is run, and what that means for safety
- The judge is read from the default branch, through the API, as the factory file is. A pull request cannot change the judge that judges it; its change to the judge takes effect when it merges, having been reviewed as high risk.
- It runs as a separate program that is handed no secret. Its environment has only a search path and a language setting, and it runs in a temporary directory that holds just its own files. That stops a secret reaching it by accident. It is not a sandbox: a program on the same machine can use the network, read the runner's disk, and look up the environment of the process that started it, where the workflow's token is. In the automatic reviewer's job that process also holds the model credential.
- The pull request reaches it only as data, on standard input: file names, diffs and file text chosen by the pull request's author. The judge must treat all of it as text to examine. It must never execute, import or evaluate any of it. Parsing it (as Python's
ast.parsedoes) is fine; running it is not. - It is your code, running in your workflow. Trust it as you trust any script your workflows run from the default branch, and review changes to it as you would changes to a workflow; the gate makes them high risk for that reason.
- Its words are shown to people, and to the automatic reviewer as quotation. The areas and reasons it returns appear in the commit status and the run's summary, cut to a line each. If it builds them from the pull request's text ("login() changed"), that text appears there. The automatic reviewer is given them inside a fence, as it is given the change, and not as the repository's own statement.
#Seeing what it decides
The gate's status names the judge's areas, and the run's summary lists each reason. factory.preview cannot run a judge: with one named it says so, and shows only what the lists would have said.
To try a judge before relying on it, run it yourself on a change, the way the gate does:
gh api repos/OWNER/REPO/pulls/NUMBER/files --paginate \
| python3 -c 'import json,sys; print(json.dumps({"factory_judge": 1, "files": json.load(sys.stdin)}))' \
| python3 scripts/ci/factory_judge.py
(That hands it the same entries the gate does, but no content or base_content; add them by hand for files your rules read.)
#Writing a judge: what to get right
- Judge a renamed file under both its names. GitHub lists a rename with the new name in
filenameand the old one inprevious_filename. A judge that reads onlyfilenamelets a workflow, a test or anything else risky be moved away, or renamed to look like prose, without the care its old place called for. The factory file's own lists judge both ends; a judge must too. - Treat what you cannot read as high. A missing
patch, a nullcontent, a file that does not parse. - Give areas as fixed words. Never build an area from the change's text.
- Test it the way the gate runs it: JSON in, JSON out, exit 0, and a non-zero exit on input it cannot use.
- Keep it quick. It has 60 seconds at most and shares 150 with every other judge run of the job.
#Edges worth knowing
The first three err on the strict side.
- If the gate announces a judge run on a look after its first, and the commit then stops being any open pull request's head, the status on that commit is left pending. Nothing merges from a commit that is no pull request's head, and the gate judges nothing there, so a re-run does not change it; it is judged afresh when a pull request is on that commit again.
- If new pull requests keep appearing on one commit during a run (three in a row), the gate gives up with
error. Re-run the gate's job. - A new head pushed during a run is settled by the same job from what is left of the allowance; with none left it comes out
judge failed. A later run of the gate on that head (the push's own, or a re-run) judges it properly. - Not strict: a change to the judge's own files during a run is not seen by that run. Within one run, a pull request the judge has been asked about keeps its answer while its head, its base, its list of files and the review station's part of the factory file all stay the same. If the judge's script or a
withfile changes on the default branch during that run, the answer already given stands until the gate next runs on that commit. (A change to the review station's keys in the factory file is seen: the judge is asked again. Such a change between looks can also leave the commit pending, or after three in one run end inerror, as above.) - The allowance bounds the judging, and is checked before each file's text is fetched. It does not cut short a fetch already under way, or the fetch of the judge's own files.
--dry-runruns the judge and announces nothing.
#What is not built
- A judge in a language other than Python.
- Handing the judge more than the changed files: other files of the repository, the pull request's description, earlier results.
- Running the judge from the preview.
This guide is docs/JUDGE.md in the repository, and this page is made from it. Something wrong or unclear? Open an issue or send a pull request. Next: The release gate.