Curbing AI-Coded C# and F# Slop with .NET Slopwatch

A .NET tool that detects LLM 'reward hacking' behaviors in code changes.

In my “Software 2.0” blog post series, I’ve written about how the burden of effort in software development has shifted from manually typing and testing code to writing specifications and verifying that large language models have produced an acceptable program as output.

There are numerous challenges to doing this well at scale: cost, learning curve, tooling, lack of existing test coverage or specifications on legacy code bases, and more.

One of the more acute problems developers encounter in this space is “reward hacking” - a process by which LLM coding assistants follow the literal letter of the law but not the spirit of it, i.e. “turn the test suite green” gets interpreted as “disable the failing tests” by the language model. LLMs are trained via reinforcement learning to achieve sets of goals established by researchers, and the reasoning LLMs use is quite alien compared to how a human understands it. This sort of misalignment between what the goal specified and the LLM’s very literal interpretation of it is what leads to “reward hacking.”

In this post I’m going to introduce “.NET Slopwatch”, a dotnet tool I’ve written that can use a combination of Roslyn and file analyzers to catch common reward-hacking patterns and flag them long before they seep into your code base.

Reward Hacking is Destructive

Reward hacking is subtle and pernicious; it gradually makes your test suite less robust, your packages less organized, your code more branchy and complex, and in some cases it can bypass type safety altogether.

A good example of brazen reward hacking from Opus 5.5 a few weeks ago:

The diff image I captured is a bit convoluted, because it shows me getting Opus to reverse course on what it did previously (all part of the same pull request, so nothing was merged), but it looked like this:

Opus 5.5 adds dynamic keyword to a value converter used to transcribe DateTimeOffset into one of two different value objects

Opus wrote a value converter to convert DateTimeOffset into one of two different types of value objects (InvitationExpirationDate and SubscriptionRenewalDate) that appear on the same row inside our Entity Framework Core system.

This code is insane, frankly. Not even the least competent human developer on any of my software teams in the past could have come up with this monstrosity.

But, when you have powerful LLMs that have a tendency to highly localize problems they’re solving then they will create stinkers like this.

Other Examples of Reward Hacking

What are some other examples of “reward hacking” employed by agents to satisfy requirements “literally?” Here are some common ones:

  1. Skipping failing tests, especially flaky ones;
  2. Empty catch blocks to suppress exceptions;
  3. “Timeout jiggling” - adding arbitrary Task.Delays to timing-sensitive tests in order to get them to pass;
  4. Adding optional parameters to methods everywhere in order to create “safe fallbacks” for previous execution paths; and
  5. Other stupid things, like suppressing warnings or bypassing NuGet central package management with version overrides at the project level.

None of these are satisfactory solutions to the problems agents set out to solve; what they are is shortcuts that allow agents to complete their goals quickly, hence the term “reward hacking.”

Stop LLM Reward Hacking with Slopwatch

This is where Slopwatch enters the picture. Now before the dynamic incident, I didn’t have a Slopwatch rule in-place to block agents from using dynamic because I had never seen an agent do that before, but as of Slopwatch 0.5.0 we now have a rule in-place to block agents from using dynamic.

The goal behind Slopwatch is to catch these errors in one of three places:

  1. Inside your CI/CD system, generally the best and last line of defense;
  2. As a post commit-hook inside certain LLM harnesses such as Claude Code, but this is very much a “your mileage may vary situation;” or
  3. Inside the inner loop of the LLM agent itself, using skills such as the Slopwatch skill in dotnet-skills.

Any combination of these will work.

Installing Slopwatch

First, we need to install Slopwatch:

# Install as a global tool
dotnet tool install --global Slopwatch.Cmd

# Or install locally
dotnet new tool-manifest
dotnet tool install --local Slopwatch.Cmd

Next, switch to our solution directory for our .NET project and call slopwatch init.

# Initialize in your project (creates baseline from existing code)
cd your-project
slopwatch init

# Commit the baseline to your repository
git add .slopwatch/baseline.json
git commit -m "Add slopwatch baseline"

This saves a .slopwatch/baseline.json “baseline” file, which tells Slopwatch not to flag any pre-existing patterns it will otherwise try to block LLMs from making.

Finally, you can run slopwatch analyze after agents modify files in your project and get a report back on whether or not the LLM is being naughty:

# Human-readable console output (default)
slopwatch analyze

# JSON for programmatic use
slopwatch analyze --output json

# Fail if errors found (default)
slopwatch analyze --fail-on error

# Fail on warnings too
slopwatch analyze --fail-on warning

And that’s it! slopwatch will return a non-zero error code if it finds any new rules violations.

Here’s what that looks like in practice. I took a small billing project, created a baseline, and then made the sorts of changes an agent would make to “fix” a flaky renewal test: skip the test, add a Task.Delay “to give the event publisher time to flush,” swallow a DbUpdateConcurrencyException, and recreate my dynamic value converter from earlier. Here’s the output from slopwatch analyze --output json:

{
  "version": "1.0",
  "timestamp": "2026-12-02T20:56:07.7122877Z",
  "totalIssues": 4,
  "summary": {
    "errors": 3,
    "warnings": 1,
    "info": 0
  },
  "results": [
    {
      "ruleId": "SW021",
      "ruleName": "Dynamic Keyword Ban",
      "severity": "error",
      "filePath": "src/Billing/RenewalDateConverter.cs",
      "lineNumber": 6,
      "column": 61,
      "message": "Use of 'dynamic' bypasses compile-time type checking",
      "codeSnippet": "public sealed class RenewalDateConverter() : ValueConverter<dynamic, DateTimeOffset>(\n    v => v.Value,\n    v => v > DateTimeOffset.UtcNow\n        ? new InvitationExpirationDate(v)\n        : new SubscriptionRenewalDate(v));",
      "suggestedFix": "Replace 'dynamic' with a concrete type, a generic type parameter, or an interface. If dynamic is genuinely required (COM interop, dynamic proxy boundary), use [SlopwatchSuppress(\"SW021\", \"reason with 20+ chars\", \"Interop\")]",
      "isGeneratedCode": false
    },
    {
      "ruleId": "SW003",
      "ruleName": "Empty Catch Block Detection",
      "severity": "error",
      "filePath": "src/Billing/SubscriptionService.cs",
      "lineNumber": 14,
      "column": 9,
      "message": "Empty catch block swallows exceptions without handling",
      "codeSnippet": "catch (DbUpdateConcurrencyException)\n        {\n        }",
      "suggestedFix": "Add proper exception handling (log the error, or handle the error condition). If the empty catch is intentional, use [SlopwatchSuppress(\"SW003\", \"reason with 20+ chars\")]",
      "isGeneratedCode": false
    },
    {
      "ruleId": "SW001",
      "ruleName": "Disabled Test Detection",
      "severity": "error",
      "filePath": "tests/Billing.Tests/SubscriptionServiceSpecs.cs",
      "lineNumber": 13,
      "column": 6,
      "message": "Test method 'Should_publish_renewal_event' is disabled: Flaky on CI - will revisit",
      "codeSnippet": "Fact(Skip = \"Flaky on CI - will revisit\")",
      "suggestedFix": "Remove the Skip/Ignore attribute and fix the test so it passes. If the test must be disabled, use [SlopwatchSuppress(\"SW001\", \"reason with 20+ chars\")]",
      "isGeneratedCode": false
    },
    {
      "ruleId": "SW004",
      "ruleName": "Test Timeout Jiggling Detection",
      "severity": "warning",
      "filePath": "tests/Billing.Tests/SubscriptionServiceSpecs.cs",
      "lineNumber": 18,
      "column": 15,
      "message": "Test uses Task.Delay(2000) which may indicate a timing-dependent test",
      "codeSnippet": "Task.Delay(2000)",
      "suggestedFix": "Replace Task.Delay with proper synchronization (TaskCompletionSource, SemaphoreSlim, or test framework's async helpers). If delay is intentional (testing timeouts), use [SlopwatchSuppress(\"SW004\", \"reason with 20+ chars\")]",
      "isGeneratedCode": false
    }
  ]
}

Three errors and a warning, so the run fails with exit code 1. The suggestedFix fields matter more than they look: when an agent runs Slopwatch inside its own loop, they tell it how to fix the problem properly instead of finding a new way around the rule.

Using Slopwatch with GitHub Actions

If you want to use Slopwatch inside CI/CD, here’s a simple GitHub Actions recipe you can use:

name: Slopwatch

on:
  pull_request:

jobs:
  slopwatch:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '10.0.x'

      - name: Install Slopwatch
        run: dotnet tool install --global Slopwatch.Cmd

      - name: Run Slopwatch
        run: slopwatch analyze -d . --fail-on error

Suppressing Slopwatch Warnings or Taking New Baselines

If you have a legitimate reason for introducing a new catch {} block or any other sort of violation that Slopwatch would normally flag, you can suppress those via one of two ways:

Suppress Slopwatch Warnings in Code

You can suppress a slopwatch warning using its rule name in a comment, for instance:

// slopwatch-ignore: SW004 Testing rate limiter behavior requires actual time delays
await Task.Delay(1000);

Or you can use a block to ignore Slopwatch warnings throughout a big section of code:

// slopwatch-ignore-start: SW002 Legacy interop requires suppressing obsolete API warnings
#pragma warning disable CS0618
public void CallObsoleteApi() { ... }
#pragma warning restore CS0618
// slopwatch-ignore-end

Suppress Slopwatch Rules in Configuration

It’s also possible to suppress Slopwatch rules inside the tool’s configuration itself, located at .slopwatch/config.json.

For instance:

{
  "suppressions": [
    {
      "ruleId": "SW002",
      "pattern": "**/Generated/**",
      "justification": "Generated code from tooling cannot be manually changed"
    },
    {
      "ruleId": "SW006",
      "pattern": "src/Legacy/**",
      "justification": "Legacy CPM migration in progress; tracked in issue #123"
    }
  ],
  "globalSuppressions": []
}

These will disable Slopwatch rules SW002 and SW006 for files that match those globs.

Alternatively, you can also update your baseline file - which is a good practice to engage in each time you upgrade Slopwatch too:

# Add new detections to existing baseline
slopwatch analyze --update-baseline

Give Slopwatch a Try

All of the configuration and tooling options I mentioned here are explained in much more detail in the .NET Slopwatch repository itself. Give it a try and use it to combat LLM reward hacking in your projects too!

Discussion, links, and tweets

I'm the CTO and founder of Petabridge, where I'm making distributed programming for .NET developers easy by working on Akka.NET, Phobos, and more..