Zum Inhalt springen

Working out the cost of code review with coding agents

A recurring review comment can move into the coding agent's loop as a rule. A worked case shows what that costs and when it pays off.

by CodeCharter
  • code-review
  • code-quality
  • economics

A team lead owns a .NET solution whose developers work with coding agents. In pull requests she keeps writing the same comment: the new service should use IOrderRepository, not the SQL class SqlOrderRepository. She is considering turning the concern into a rule that the agent runs itself while it works. Before she does, she wants to know what the rule costs and when it pays off against the comment. The solution and its Git history are constructed for this article; the output comes from real runs of CodeCharter v1.7.0. The agent's steps are reenacted, and the commits were set by hand.

Recurring review concerns

The three studies that put numbers on recurring review concerns count comments and concerns; none of them measures working minutes. Bacchelli and Bird classified 570 review comments at Microsoft in 2013. 29% were improvements such as readability, unused code or better practices, 14% were defects. In the interviews, developers named checking team conventions as a regular part of review. The same study also found knowledge transfer and alternative solutions in review. That part of review stays with a person even after a rule is in place.

Gunawardena, Tempero and Blincoe checked in 2022 whether a tool would have found 417 review concerns from seven open-source Java projects. SonarQube, Checkstyle, PMD and similar tools would have detected 22% accurately, and simple new heuristics another 16%. The authors give design concerns such as "Poor design" as their example of concerns that cannot be automated accurately. A project-specific layer boundary is more sharply defined than "poor design", and the study says nothing about it.

For pull requests written by coding agents there is a study by Khelifi, Ouni and Khemaja (MSR 2026). They compared 5,869 agent PRs with 5,869 human PRs from the same open-source repositories. Any comment, review or commit by a human counted as an intervention. Humans intervened in 52.17% of agent PRs and in 83.59% of human PRs. Agent PRs with an intervention had a median of 196 changed lines and stayed open for 42.9 hours. For human PRs with an intervention, the figures were 66 lines and 22.2 hours.

The authors classified every intervention in 342 agent PRs by hand. 58.02% were guidance without a code change of their own, that is feedback, constraints or discussion. In their summary the authors highlight restricting the agent and enforcing project conventions. In the study, however, conventions are a subcategory: 35.45% of guidance concerns code quality and scope, and 29.56% of that concerns conventions. Own calculation: 58.02% × 35.45% × 29.56% comes to about 6% of all classified interventions. By the same calculation, general discussion, delegating tasks and removing unnecessary changes together make up a good quarter. No layer rule decides that work. Which concerns recur in her team is something the team lead therefore has to count in her own pull requests.

A violation without a rule

In the example, Shop.slnx consists of Shop.Domain with IOrderRepository, Shop.Infrastructure with SqlOrderRepository, and Shop.Application with the services. For historical reasons Shop.Application also references the infrastructure. There, the class ApplicationComposition acts as the composition root and wires in the SQL implementation. The agent gets the task of making orders cancellable and writes CancelOrderService:

public sealed class CancelOrderService
{
    private readonly SqlOrderRepository _orders;

    public CancelOrderService(SqlOrderRepository orders) => _orders = orders;

    public bool Cancel(int id)
    {
        var order = _orders.LoadForUpdate(id);
        …

LoadForUpdate exists only on the SQL class, not on the interface. In the second commit the agent adds BulkCancellationJob following the same pattern, in the third the tests. The build and all three tests pass. The team checks pull requests with the profile codeguard/csharp-all v1.3.0 and --fail-on error:

> codecharter analyze Shop.slnx --git-ref base..HEAD --fail-on error
Rules: 248 from codeguard/csharp-all@1.3.0
  --- Warning (2) ---

  WARN   [Design] Constructor injects concrete class instead of interface
        Shop.Application.Ordering.BulkCancellationJob
        …\src\Shop.Application\Ordering\BulkCancellationJob.cs:6
        …
  WARN   [Design] Constructor injects concrete class instead of interface
        Shop.Application.Ordering.CancelOrderService
        …\src\Shop.Application\Ordering\CancelOrderService.cs:6
        …
  4 violations (2 warnings, 2 infos)
  Analyzed 10 types, 14 methods in 6,2s

The run ends with exit 0, because --fail-on error lets warnings through. With --diff-context, which also reports findings outside the changed lines in the touched files, the same four findings remain. So the profile rule inject-interface-not-class did report the violation; it just does not block.

The team lead finds the violation in review and writes her comment. The fix lifts LoadForUpdate into the interface as FindForUpdate and switches both services over:

> git show --stat 4d221f2
…
 src/Shop.Application/Ordering/BulkCancellationJob.cs      | 8 ++++----
 src/Shop.Application/Ordering/CancelOrderService.cs       | 8 ++++----
 src/Shop.Domain/Ordering/IOrderRepository.cs              | 3 +++
 src/Shop.Infrastructure/Persistence/SqlOrderRepository.cs | 5 +++--
 4 files changed, 14 insertions(+), 10 deletions(-)

The fix touches four files in three projects, including Domain and Infrastructure, which the pull request had not touched before. The team lead then reviews again, and CI runs once more. This round trip of comment, fix and second review repeats every time the concern comes up.

The cheapest automation would be a severity override in .codecharter/config.yml:

overrides:
  inject-interface-not-class:
    severity: error

With it, the diff run on the pull request reports both services as ERROR and ends with exit 1. The rule, however, recognises the dependency by its type name. It fires for concrete classes whose name ends in Repository, Service, Factory or a similar suffix. In a variant of the case, a RefundService takes the infrastructure class OrderTable in its constructor. The run with the override and --severity error reports "No violations found." and ends with exit 0; the layer rule reports RefundService with exit 1. Conversely, by its condition the rule also reports a concrete …Service from the same layer, even though no project boundary is crossed there. The override costs three lines of configuration and recognises concrete dependencies by their names. It does not check the boundary between two projects.

The cost of the rule

The team lead writes the concern as a CodeCharter rule of ten lines. It reports every type in Shop.Application that reaches a type in Shop.Infrastructure through declared dependencies, including through intermediate types. It comes with a spec of two cases the rule must report and two it must not. One of the two cases without a report is the composition root. The first version fails the spec:

> codecharter test .codecharter/rules/application-without-infrastructure.ccr --no-host
FAIL application-without-infrastructure  (3/4 cases)
     ? Miss 'Composition root wires the SQL implementation' - Expected the rule to ignore this snippet (miss), but it produced 1 violation(s).
     …
1/1 spec(s) failed (1/4 cases failing).

The false positive shows up in the spec before the rule blocks a single pull request. One extra condition excludes the namespace Shop.Application.Composition, after which all four cases pass with exit 0. The final version reads:

@name "Application does not depend on Infrastructure"
@severity error
@category "Architecture"
@recommendation "Depend on the interface from Shop.Domain (for example IOrderRepository) and let the composition root pass the SQL implementation."

from t in AllTypesFlattened
where t.FullName.StartsWith("Shop.Application.")
where !t.FullName.StartsWith("Shop.Application.Composition.")
where t.Reaches("Shop.Infrastructure.*", "declared")
select t

On main, the rule with --fail-on error immediately finds the existing class LegacyOrderExport, which takes SqlOrderRepository in its constructor (exit 1). The team decides whether to rework the class now or put it into a baseline. --write-baseline records more than this one finding. In the example the file holds 11 fingerprints, including warnings and infos from other profile rules such as inject-interface-not-class and primitive-in-constructor. After that, the full analysis with --baseline and --fail-on error ends with exit 0. How a baseline is reviewed and maintained is covered in the article on a baseline quality gate for legacy solutions.

Introducing the rule therefore takes three items: the rule with its spec, fixing the false positive, and the decision about existing code. Maintenance follows whenever the architecture changes and the spec needs new cases.

A violation with the rule in the agent loop

With the rule on main, the agent starts the same task. After the first commit it runs the diff analysis:

> codecharter analyze Shop.slnx --git-ref main..HEAD --diff-context --baseline .codecharter/baseline.json --fail-on error
Rules: 248 from codeguard/csharp-all@1.3.0, 1 from .codecharter/rules
  --- Error (1) ---

  ERROR  [Architecture] Application does not depend on Infrastructure
        Shop.Application.Ordering.CancelOrderService
        …\src\Shop.Application\Ordering\CancelOrderService.cs:6
        ? Depend on the interface from Shop.Domain (for example IOrderRepository) and let the composition root pass the SQL implementation.
…
  3 violations (1 error, 1 warning, 1 info)
  Analyzed 8 types, 11 methods in 3,3s

The run ends with exit 1. The agent switches CancelOrderService to the interface; the fix touches three files with 10 lines added and 6 removed. The next analysis with the same command reports no error, only two infos about missing parameter documentation, and ends with exit 0. In the example, BulkCancellationJob is then written against the interface from the start. That the agent follows the pattern of the class it has already fixed is an assumption of the constructed case. In the end both branches contain the same code, but the pull request needs no comment on this concern.

The article on architecture feedback for coding agents shows how the agent calls the analysis through MCP. It also explains why a check on the head commit is still needed before merging. If the same rule runs only in CI, it reports both services as ERROR on the finished pull request (exit 1). The team lead then does not have to find the violation herself. The fix, however, is as large as in the case without a rule, because the second class already builds on the SQL class.

Machine time

Each run puts computing time on the cost side. The diff analysis on the agent branch ran three times each with and without the host, build and test once each. Wall time was measured on Windows 10 with 16 logical cores and .NET SDK 10.0.402. The values are summarised by a script:

Run Wall time
codecharter analyze --git-ref with --no-host 7.8 to 8.9 s
codecharter analyze --git-ref through the warm local host 3.6 to 4.7 s
dotnet build 3.1 s
dotnet test 4.5 s

For ten types that is a matter of seconds. The figure does not carry over to a large solution; there the team measures how long a run takes and how often the agent starts one. The local CodeCharter host stores every analysis run that goes through it, with duration, rule count and session ID, in the file insights.db. It records neither runs with --no-host nor runs in CI, and no human minutes. That makes only the machine side on the developer's computer countable.

The calculation per concern

The saving accrues each time the concern comes up, regardless of the number of pull requests. The calculation therefore needs four figures from the team itself. n is the number of review comments on this concern per month, t the human minutes of one round trip of comment, fix and second review. E is the hours for rule, spec and existing code, p the hours of maintenance per month. S is the saving in hours per month, M the number of months to break even.

S = n × t / 60 − p
M = E / S

The formula assumes that the rule replaces the whole human round trip and that machine time per run is negligible. A numerical example with assumed values that the case does not measure: the concern comes up six times a month, and a round trip costs 20 minutes. That makes 2 hours a month. With 4 hours to introduce the rule and half an hour of maintenance a month, 1.5 hours remain. The rule has paid for itself after just under three months. If the same concern comes up twice a year, the rule saves about 3 minutes a month. That is less than the half hour of maintenance, and break-even never comes.

Three conditions tip the calculation. A concern that cannot be decided from types and projects does not make a reliable rule. If the rule often reports false positives, p rises, because agent runs, follow-up questions and exceptions are added. If the review comment also prompts a design discussion, that discussion disappears along with it. The formula does not capture this loss. In the example, the frequency and a clear boundary between two projects speak for the rule.

Measuring in a pilot

Before the introduction, the team marks in the review comments of the past few weeks which concern each one addresses. That gives n. For t it is enough to log a few round trips: minutes for the comment, for the fix and for the second review. After activation the team counts three things. First, the review comments on the same concern, whose number should approach zero. Second, suppressions and baseline entries for the new rule, as a sign of false positives. Third, the analysis runs from the host data and from CI.

Calendar time does not belong in the hours calculation. The 42.9 hours from the MSR study state how long agent PRs with an intervention stayed open and do not separate waiting from working. Nor is the evaluation fit to rank individual developers: what is counted is how often a concern comes up, not who causes it. After a few weeks the formula, filled with measured values, shows whether the rule stays or the concern goes back to review.