In the previous post about Claude Code I wrote that I treat the agent's output like a pull request from a colleague: I read it and run it, and only then does it go in. This time it is about what happens earlier. Before the agent says "done", its code goes through the same sieves as mine: formatting, inspection, tests and review. None of this is new. What is new is that the agent does it every single time, without grumbling, in a few minutes.

The loop before the commit

For a change in C#, my order looks like this:

  1. the code change,
  2. dotnet csharpier format . - formatting,
  3. dotnet build - compilation and analyzer warnings,
  4. jb inspectcode from the SUGGESTION threshold - every finding is a fix in the code,
  5. tests for this change, run only for it,
  6. review of the change by a second agent,
  7. fixes after the review, steps 2-5 once more, commit.

Each step catches something different and each misses something. That is why there are several.

Formatting: CSharpier and the end of the discussion

CSharpier is a Prettier-style formatter: it has its own rules and almost no options. For an agent that is an advantage, because it does not have to guess the project's style or fix indentation by hand. It writes, then runs dotnet csharpier format .. The hook (and in some projects CI as well) runs dotnet csharpier check ., which according to the documentation exits with code 1 when a file is not formatted.

One trap: ReSharper has its own formatting rules that can fight CSharpier (a trailing comma in a multi-line list, for example). I switch them off in the solution's .DotSettings file. Formatting belongs to one tool, otherwise the agent will keep fixing what the other tool breaks again a moment later.

The JetBrains inspection: the same engine as Rider

jb inspectcode from the JetBrains.ReSharper.GlobalTools package runs from the command line the same inspections that Rider shows in the editor. The agent has no Rider, but it has a terminal, so it gets the same verdict I do. The script I use, simplified:

Shell
#!/usr/bin/env bash
# JetBrains inspection at SUGGESTION. Exits non-zero on any finding.
set -euo pipefail

if [[ "${1:-}" != "--no-build" ]]; then
    dotnet build App.slnx -v q -nologo
fi

report="$(mktemp)"
trap 'rm -f "$report"' EXIT

jb inspectcode App.slnx --no-build --format=Text --severity=SUGGESTION \
    --exclude="src/ThirdParty/**" --output="$report" >/dev/null

# Findings are the indented lines that carry a file path.
findings="$(grep -cE '^ {6}\S' "$report" || true)"
if [[ "$findings" -gt 0 ]]; then
    cat "$report"
    echo "inspection: $findings finding(s)" >&2
    exit 1
fi
echo "inspection: clean"

A few details from the tool's help: the default threshold is SUGGESTION, and the default report format is SARIF, hence --format=Text, which is easy to read and count. --no-build skips building, because the solution was built a step earlier. --exclude leaves out code that is not ours, such as a client copied from a vendor.

The most important part is the agreement: a finding is a change in the code, not a comment on the report. Narrowing visibility, a redundant using, a property pattern instead of a chain of &&, an unused parameter. If a finding is a false alarm, I suppress it in place with the reason in a comment, or switch the rule off in .DotSettings, also with the reason. The script returns a non-zero code, so nobody can pretend the report is clean.

Hooks keep watch when the agent forgets

The agent has all of this in its instructions, but instructions are not a guarantee. Git hooks through Husky.Net keep watch. The .husky/task-runner.json file:

JSON
{
  "$schema": "https://alirezanet.github.io/Husky.Net/schema.json",
  "tasks": [
    { "name": "csharpier-check", "group": "pre-commit",
      "command": "dotnet", "args": ["csharpier", "check", "."] },
    { "name": "build", "group": "pre-commit",
      "command": "dotnet", "args": ["build", "App.slnx", "--nologo", "-v", "q"] },
    { "name": "build", "group": "pre-push",
      "command": "dotnet", "args": ["build", "App.slnx", "--nologo", "-v", "q"] },
    { "name": "inspect-code", "group": "pre-push",
      "command": "bash", "args": ["scripts/inspect-code.sh", "--no-build"] }
  ]
}

The pre-commit hook calls dotnet husky run --group pre-commit, and pre-push the pre-push group. Formatting and build on every commit, inspection on push, because it takes longer.

Here is the catch. If the inspection runs only on push and the agent did not run it earlier, the push fails and the fixes land in a separate "inspection findings fixed" commit. The same work twice. In a large .NET monolith I work on, about 30 of more than 2100 commits are exactly such after-the-fact fixes. That is my observation from the git history, not a study, but it was enough to change the order: the inspection runs before the feature commit, and the push hook is only a safety net.

Rules written for the inspection up front

The second change works even better. Every finding the agent has fixed becomes a rule in a skill (a file of instructions the agent reads whenever it works on C#). Next time the agent writes code that the inspection lets through on the first try. A few examples from that list:

C#
// Before: "'DateOnly.ToString' is culture-sensitive"
var day = startsOn.ToString("yyyy-MM-dd");
// After: the same result in every container culture
var day = startsOn.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);

// Before: "initialize object properties inside the using"
await using var command = new NpgsqlCommand { Connection = connection };
// After: an exception in the initializer cannot leave the object undisposed
await using var command = new NpgsqlCommand();
command.Connection = connection;

// Before: a chain of && on the same object
if (user.IsDemo && user.Role == AppRoles.Admin) { }
// After: a property pattern
if (user is { IsDemo: true, Role: AppRoles.Admin }) { }

The first example is more interesting than it looks: without a culture the result depends on the container's settings, so it is not just cosmetics. The list grows with every project and is shared by all of them. A finding from one repository stops appearing in the next ones.

Tests for the change

The agent writes tests for what it changed and runs only those, not the whole suite: dotnet test filtered to a class or a module's group. Integration tests run against a real database in a container (Testcontainers), because a database mock checks nothing when the logic lives in procedures. Formatting and inspection say whether the code is tidy. Tests say whether it does what it was supposed to do.

To be honest: the agent's tests check what the agent foresaw. If it misunderstood the requirement, it will write correct code and a correct test for the wrong requirement. That is why there is one more step.

Review by a second agent

In Claude Code a subagent works in its own context window, with its own system prompt and tool access, and returns a summary to the main conversation. For a review that is the key property: the reviewer does not see the author's reasoning. It gets the diff, not the conclusion "everything works", so it has nothing to anchor on.

The simplest way is the built-in /code-review, which according to the documentation reviews the current diff for correctness bugs, and with --fix applies the fixes right away. You can also define your own reviewer as a Markdown file with a header:

markdown
---
name: dotnet-reviewer
description: Reviews C# changes for correctness, async misuse and missing tests. Use before committing.
---

Read the diff and the changed files. Report bugs with file and line,
and the test that would have caught each one. Do not rewrite the code.

The reviewer sometimes finds things that neither the inspection nor the tests catch: a CancellationToken that is not passed on, the empty-list case, a race between two writes. Sometimes it reports something that is not a bug. The author verifies the findings, and when in doubt, I do.

What this does not catch

So that this does not turn into marketing:

  • Formatting and inspection know nothing about the business. A clean report says the code is tidy, not that it is right.
  • Tests check the cases someone foresaw. An edge case nobody thought of goes through every sieve.
  • The reviewer runs on the same model as the author. A separate context helps, but the blind spots can be similar. A second agent is not a second person with different experience.
  • Hooks can be bypassed. --no-verify exists for the agent too. A hook is a net, not a wall; the stronger boundary is CI repeating the same steps before the merge.

A person would work the same way

Nothing in this loop was invented for AI. A formatter, an inspection, tests and a review are exactly what a careful developer does before a pull request. The difference is speed and consistency: at the end of the day a person sometimes skips the inspection "because it is a small change". The agent does not skip it, and it goes through the whole loop in a few minutes. What the agent misses, a person taking the same road would usually miss too, only it would take them longer.

There is always a person who assigns the task

The last step is not automatic. Someone chooses the task, describes the scope and what "done" means, answers the agent's questions along the way, reads the diff and decides about the merge. The tools check the code, but that person, not the agent, is responsible for what reaches production. The better the task is described, the less is left to fix at the end, and that is probably the most boring and most important lesson of this whole loop.