By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A pull request swaps a formatted SQL string for a parameterised query. The review bot approves it. The diff is small and the tests pass. In the same week the bot blocks a pull request that renames a local variable, and posts eleven comments about naming.
The team mutes the bot. Two weeks later the next SQL string ships. Both verdicts came from one design: no rule list, so the bot invented criteria, and whole-repository reading, so it judged code nobody had touched.
Scenario 5 gives you a pipeline that reviews, generates tests and comments. The exam asks which control makes it trustworthy.
| Job | What it must produce | What it must never do |
|---|---|---|
| Automated review | Findings with a rule id, path, line and severity | Invent a rule outside the policy file |
| Test generation | Tests that fail before the fix | Report a test it never ran |
| Pull request feedback | One comment, blocking findings first | Repost the same text on every push |
No. A schema-valid object is data, not a verdict. The job fails only when your code reads the object, applies the threshold, and exits non-zero. JSON output changes the shape of the result. The schema changes what the run may return. Neither one sets an exit code.
Headless mode runs Claude Code with no interactive session, so a runner can call it. You pass the prompt with -p (also written --print) and read the result from standard output. Every interaction must become a flag, a setting or a file in the repository.
| Interactive habit | Headless replacement |
|---|---|
| Answering a permission prompt | --allowedTools names the tools the run may use |
| Reading prose and guessing | --output-format json returns a parseable result |
| Trusting the shape by eye | --json-schema rejects a malformed result |
| Deciding that it looks fine | Your parser returns the exit code |
A vague instruction such as review for quality produces the eleven comments. Write the rules your team already argues about, give each one an id, and ask for nothing more. Drop any finding whose rule_id is not on the list.
Diff scope is the second lever. Pass the changed paths and changed lines, and allow findings only there. A finding on untouched code is not wrong. It is out of scope, and it is the main source of noise.
| Noise source | Control |
|---|---|
| Style opinions | Ban formatting and naming in the policy |
| Whole repository reading | Give the run the diff, not the tree |
| Invented rules | Drop findings without a known rule id |
| The same bug on every push | One comment per rule per file, updated in place |
A generated test is only useful if it fails first. Generate tests for the changed functions, run them before the fix, and keep only the ones that fail. Then run the full suite.
A finding has no value until a reviewer sees it. Post one comment per rule per file, and update it in place. Two comments about the same line teach the reviewer to skim.
Put blocking findings first and label them. A reviewer who reaches the ninth finding before learning the first three were advisory stops at the fourth.
Include the run id and the schema version in the comment. A reviewer can then tell whether it came from this run or the one before the fix.
Three exit codes are enough, so document them. 0 means the review ran and found nothing blocking. 1 means a policy violation. 2 means the job could not trust the result.
| Exit code | Meaning | What the pipeline does |
|---|---|---|
| 0 | Ran, nothing blocked | The check passes, no comment |
| 1 | A rule at or above the threshold | The check fails, one comment per file |
| 2 | Malformed or missing result | The check fails as a job fault |
POLICY = {"no-string-sql", "missing-authz", "unhandled-error", "test-required"}
BLOCKING = {"high", "critical"}
CHANGED = {("app/db.py", 42), ("app/db.py", 43), ("app/api.py", 17)}
def triage(report: dict) -> int:
"""Keep only findings the policy can justify, then set the exit code."""
kept = []
dropped = 0
for finding in report.get("findings", []):
rule_ok = finding.get("rule_id") in POLICY
scope_ok = (finding.get("path"), finding.get("line")) in CHANGED
if rule_ok and scope_ok:
kept.append(finding)
else:
dropped += 1
blocking = [f for f in kept if f.get("severity") in BLOCKING]
for finding in blocking:
print(
f"::error file={finding['path']},line={finding['line']}::"
f"{finding['rule_id']}"
)
print(f"kept={len(kept)} dropped={dropped} blocking={len(blocking)}")
return 1 if blocking else 0
REVIEW = {
"summary": "five findings on the changed lines",
"findings": [
{"path": "app/db.py", "line": 42, "severity": "high", "rule_id": "no-string-sql"},
{"path": "app/db.py", "line": 12, "severity": "high", "rule_id": "no-string-sql"},
{"path": "app/api.py", "line": 17, "severity": "medium", "rule_id": "unhandled-error"},
{"path": "app/api.py", "line": 21, "severity": "low", "rule_id": "import-order"},
{"path": "app/cli.py", "line": 3, "severity": "high", "rule_id": "print-debug"},
],
}
exit_code = triage(REVIEW)
print(f"job exit code: {exit_code}")Keep the policy in code and the schema in a file. A threshold change is then a one-line review, and the prompt does not move.
Open a pull request that only renames variables. Expect exit 0 and no comment.
Add a rule id your policy does not define. Expect the finding to be dropped.
Plant a string-built SQL query on a changed line. Expect exit 1 and the rule name.
Send a review object with an extra field. Expect exit 2, not 1.
Definition of done: you predict the exit code before each run, and you are right four times out of four. The fourth case turns a broken prompt into a green build.
Key takeaways
Headless mode is -p or --print: no session, standard output only.
JSON output and a JSON Schema change the shape; your parser sets the exit code.
Explicit rule ids plus diff scope remove most false positives, and both are testable.
A generated test must fail before the fix, or it proves nothing.
Use three exit codes: 0 pass, 1 policy violation, 2 untrusted.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.