keepcalculator

06 Numbers and Powers

AI Code Patches: How to Score a Diff Before Trusting It

Card No. 06·60601 · Updated · Standard formula, shown below

Score an AI-generated code patch by checking its correctness, test coverage, scope, security, and maintainability—not by relying on one numeric rating. Treat any score as a review aid: inspect the diff, run relevant tests, and verify the behavior the change is meant to produce before trusting or merging it.

Why a diff needs more than a score

A diff shows what changed, usually as added and removed lines. It does not by itself prove that the change is correct, that tests exercise it, or that the code fits the surrounding design. A small patch can introduce a serious regression, while a large patch may be a necessary refactor. Lines changed are therefore useful context, not a measure of quality.

There is no universal, validated score that determines whether an AI patch is safe to merge. Teams can use a rubric to make reviews consistent, but the result depends on the repository, risk, and evidence available. A score of 90 out of 100 is not a guarantee; a failed security check or unexplained behavior change should outweigh a favorable average.

A practical review rubric

Rate each category from 0 to 5: 0 means missing or clearly unacceptable, 1 means serious concerns, 2 means weak evidence, 3 means adequate, 4 means strong, and 5 means well-supported. Multiply each rating by its weight, then divide the total by 5 to get a score out of 100. The weights below are a starting point, not a standard.

  • Correctness and requirements — 30%: Does the patch implement the requested behavior, including relevant edge cases? Is the intended behavior clear?
  • Tests and evidence — 20%: Do appropriate tests cover the changed behavior? Were they actually run, and did they pass? A test added but never executed is not equivalent to passing evidence.
  • Scope and regression risk — 15%: Are changes limited to what the task requires? Do they alter public interfaces, data formats, configuration, or unrelated behavior?
  • Security and data handling — 15%: Could the patch expose secrets, trust unvalidated input, weaken access checks, or mishandle sensitive data?
  • Maintainability and consistency — 10%: Does the code follow local conventions and remain understandable alongside nearby code?
  • Dependencies and operational effects — 10%: Does it add packages, migrations, configuration, network calls, or deployment requirements that need separate review?

For example, a patch could score 4, 3, 4, 5, 4, and 3 in that order. Its weighted score is 78 out of 100: (4×30 + 3×20 + 4×15 + 5×15 + 4×10 + 3×10) ÷ 5. That number summarizes judgments; it does not replace them. A high-risk security flaw is not made acceptable by strong scores elsewhere.

Review the patch in a reliable order

  1. Restate the intended change. Write down the expected input, output, and important constraints. If the request is ambiguous, resolve that before judging whether the patch meets it.
  2. Check the full diff. Read changed files and surrounding code. Look for unrelated edits, generated files, configuration changes, and accidental deletions. Confirm the patch does not hide behavior in a broad rename or formatting change.
  3. Trace important paths. Follow the changed logic from its entry point through relevant callers and data. Consider empty, malformed, boundary, repeated, and failure inputs where applicable.
  4. Run checks in the project’s normal environment. Use the documented test, lint, type-check, or build commands. Record which commands ran and their results; do not treat a tool’s claim that tests passed as proof when they were not run.
  5. Inspect tests for meaningful assertions. A test can pass while checking the wrong condition, duplicating the implementation, or missing the regression. Confirm it would fail if the behavior were broken.
  6. Check operational and security effects. Review dependency changes, permissions, secrets, input validation, error handling, migrations, and logging where relevant.
  7. Make a decision with explicit blockers. Record unresolved questions or required changes. Do not average away a failed essential check.

How to interpret tests and automated checks

Passing tests increase confidence only for the behavior they cover. A unit test may show that a function returns an expected value but say nothing about integration with a database, a caller, or a deployed configuration. Conversely, a failing test may reveal a patch defect, a pre-existing failure, or an unsuitable test environment; investigate rather than assuming which explanation is correct.

Static analysis, formatting, type checking, and dependency scanning add different kinds of evidence. They can catch certain mistakes efficiently, but they cannot establish that product behavior is right. Coverage percentages also have limits: executing a line does not mean a test checks its result. Review the assertions and scenarios, not only the summary numbers.

Common scoring mistakes

  • Using diff size as a quality score: Large changes deserve more scrutiny, but small changes are not automatically safe.
  • Giving full credit for tests that exist: Check whether they ran and whether they would detect the likely failure.
  • Letting a high average hide a blocker: A broken authorization check, data-loss risk, or unexplained failing test needs direct resolution.
  • Confusing plausible code with verified behavior: Readability is useful, but convincing-looking code can still mishandle edge cases or violate an interface contract.
  • Scoring without a baseline: Compare conventions and behavior with nearby code and project documentation, rather than an imagined universal ideal.

Set a trust threshold that fits the risk

A team can define what score prompts additional review, but it should also identify non-negotiable checks. For example, a low-risk documentation change may need a focused diff review and a build check where relevant. A change affecting authentication, persistence, or external interfaces merits deeper tests and review by someone familiar with those areas. No single threshold suits every repository.

Keep the rubric stable enough to compare reviews, and adjust its weights when recurring failures show that an important risk is underrepresented. Record evidence alongside ratings: “4 for tests” is less useful than “new boundary test passes; integration path not exercised.” If reviewers disagree, discuss the evidence and assumptions rather than treating the average of their scores as truth.

Frequently Asked Questions

Can a high score prove an AI patch is safe?
No. It summarizes a review and available evidence. It cannot guarantee correctness, absence of vulnerabilities, or compatibility with every environment.

Should I trust a patch if all tests pass?
Not automatically. Confirm the relevant tests ran, inspect what they assert, and review behavior that the suite does not cover.

Is a smaller diff safer?
Not necessarily. A smaller diff is often easier to inspect, but one altered condition can cause a serious defect. Judge the behavior and risk, not only the line count.

What should block a merge regardless of the score?
Examples include a known correctness or security defect, unexplained failures in required checks, unreviewed high-impact behavior, or uncertainty about a critical requirement. Teams should define blockers for their own systems.

Can automated tools score code quality?
Tools can report specific signals such as test results, lint findings, or dependency alerts. Those signals help reviewers, but no tool result alone establishes that the patch fulfills its purpose.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →