Community编程与开发github.com

dotnet-slopwatch

使用 Slopwatch 检测 .NET 代码变更中的 LLM 奖励黑客行为。每次代码修改后运行,捕获禁用的测试、抑制的警告、空的 catch 块以及其他掩盖真实问题的捷径。

dotnet-slopwatch 是什么?

Slopwatch 是一个专门为 .NET 项目设计的静态分析工具,旨在检测和阻止由 LLM(如 Claude)生成的代码中的“slop”(低质量捷径)。它通过分析 C# 源文件、项目文件和测试文件,识别常见的不良模式,如禁用测试、抑制警告、空的 catch 块和任意延迟。该工具支持作为本地或全局 dotnet 工具使用,可以建立基线以便仅捕获新增问题,避免影响现有技术债务。Slopwatch 可与 Claude Code 集成,在每次编辑后自动验证,或集成到 CI/CD 流程中作为质量门禁。其核心哲学是对新增的 slop 采取零容忍策略,要求开发者在遇到问题时进行真正的修复,而不是采取捷径。

兼容平台✓Claude Code~Codex CLI~Cursor
npx skills add https://github.com/Aaronontheweb/dotnet-skills/tree/main/skills/slopwatch

Installed? Explore more 编程与开发 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

Slopwatch:LLM 反作弊工具 for .NET

何时使用此技能

请持续使用此技能。 每当 LLM(包括 Claude)对以下内容进行更改时:

  • C# 源代码文件 (.cs)
  • 项目文件 (.csproj)
  • Props 文件 (Directory.Build.props, Directory.Packages.props)
  • 测试文件

运行 slopwatch 以验证更改没有引入“slop”。

什么是 Slop?

“Slop”指的是 LLM 采取的捷径,这些捷径使测试通过或构建成功,却没有真正解决底层问题。这些是奖励黑客行为——LLM 优化的是表面成功,而非真正的修复。

常见的 Slop 模式

模式示例为什么不好
禁用测试[Fact(Skip="flaky")]隐藏故障而非修复它们
警告抑制#pragma warning disable CS8618在未修复问题的情况下静默编译器
空的 catch 块catch (Exception) { }吞没错误,隐藏 bug
任意延迟await Task.Delay(1000);掩盖竞争条件,使测试变慢
项目级别抑制<NoWarn>CS1591</NoWarn>在项目范围内禁用警告
CPM 绕过内联 Version="1.0.0"破坏中央包管理

永远不要接受这些模式。 如果 LLM 引入了 slop,拒绝更改并要求正确的修复。


安装

作为本地工具(推荐)

添加到 .config/dotnet-tools.json:

{
  "version": 1,
  "isRoot": true,
  "tools": {
    "slopwatch.cmd": {
      "version": "0.2.0",
      "commands": ["slopwatch"],
      "rollForward": false
    }
  }
}

然后恢复:

dotnet tool restore

作为全局工具

dotnet tool install --global Slopwatch.Cmd

首次设置:建立基线

在现有项目上使用 slopwatch 之前,请创建当前问题的基线:

# 从现有代码初始化基线
slopwatch init

# 这将创建 .slopwatch/baseline.json
git add .slopwatch/baseline.json
git commit -m "添加 slopwatch 基线"

为什么需要基线? 遗留代码可能包含现有问题。基线确保 slopwatch 仅捕获新引入的 slop,而非已有的技术债务。


在 LLM 会话期间的使用

每次代码更改后

在任何 LLM 生成的代码修改后运行 slopwatch:

# 分析新问题(使用基线)
slopwatch analyze

# 使用严格模式——对警告也视为失败
slopwatch analyze --fail-on warning

当 Slopwatch 标记问题后

不要忽略它。 相反:

  1. 理解为什么 LLM 采取了捷径
  2. 请求正确的修复——具体说明哪里不对
  3. 验证修复没有引入不同的 slop
# 示例:LLM 禁用了测试
❌ SW001 [Error]: 检测到禁用的测试
   File: tests/MyApp.Tests/OrderTests.cs:45
   Pattern: [Fact(Skip="测试不稳定")]

# 正确的响应:要求实际修复
"此测试被禁用而非修复。请调查它为何不稳定,
并修复底层的时序/竞争条件问题。"

更新基线(极少使用)

仅当 slop 真正合理且已记录时更新基线:

# 将当前检测添加到基线(谨慎使用!)
slopwatch analyze --update-baseline

合理的示例:

  • 第三方库强制使用某种模式(例如,必须抑制特定警告)
  • 用于速率限制的故意延迟(非测试不稳定)
  • 无法修改的生成代码

更新基线时,请在代码注释中记录原因。


Claude Code 钩子集成

将 slopwatch 添加为钩子,自动验证每次编辑。创建或更新 .claude/settings.json:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Write|Edit|MultiEdit",
        "hooks": [
          {
            "type": "command",
            "command": "slopwatch analyze -d . --hook",
            "timeout": 60000
          }
        ]
      }
    ]
  }
}

--hook 标志:

  • 仅分析 git 脏文件(即使在大仓库中也很快)
  • 以可读格式将错误输出到 stderr
  • 在警告/错误时阻止编辑(退出码 2)
  • Claude 看到错误并可以立即修复它

CI/CD 集成

将 slopwatch 添加到 CI 管道作为质量门禁:

GitHub Actions

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

      - name: 设置 .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '9.0.x'

      - name: 安装 Slopwatch
        run: dotnet tool install --global Slopwatch.Cmd

      - name: 运行 Slopwatch
        run: slopwatch analyze -d . --fail-on warning

Azure Pipelines

- task: DotNetCoreCLI@2
  displayName: '安装 Slopwatch'
  inputs:
    command: 'custom'
    custom: 'tool'
    arguments: 'install --global Slopwatch.Cmd'

- script: slopwatch analyze -d . --fail-on warning
  displayName: 'Slopwatch 分析'

检测规则

规则严重性捕获内容
SW001Error禁用的测试 (Skip=, Ignore, #if false)
SW002Warning警告抑制 (#pragma warning disable, SuppressMessage)
SW003Error吞没异常的空的 catch 块
SW004Warning测试中的任意延迟 (Task.Delay, Thread.Sleep)
SW005Warning项目文件中的 slop (NoWarn, TreatWarningsAsErrors=false)
SW006WarningCPM 绕过 (VersionOverride, 内联 Version 属性)

配置

创建 .slopwatch/slopwatch.json 进行自定义:

{
  "minSeverity": "warning",
  "rules": {
    "SW001": { "enabled": true, "severity": "error" },
    "SW002": { "enabled": true, "severity": "warning" },
    "SW003": { "enabled": true, "severity": "error" },
    "SW004": { "enabled": true, "severity": "warning" },
    "SW005": { "enabled": true, "severity": "warning" },
    "SW006": { "enabled": true, "severity": "warning" }
  },
  "exclude": [
    "**/Generated/**",
    "**/obj/**",
    "**/bin/**"
  ]
}

严格模式(推荐用于 LLM 会话)

为了在 LLM 编码会话期间获得最大保护,将所有规则提升为错误:

{
  "minSeverity": "warning",
  "rules": {
    "SW001": { "enabled": true, "severity": "error" },
    "SW002": { "enabled": true, "severity": "error" },
    "SW003": { "enabled": true, "severity": "error" },
    "SW004": { "enabled": true, "severity": "error" },
    "SW005": { "enabled": true, "severity": "error" },
    "SW006": { "enabled": true, "severity": "error" }
  }
}

哲学:对新 Slop 零容忍

  1. 基线捕获遗留问题——现有问题被承认但隔离
  2. 新 Slop 被阻止——任何新的捷径将导致构建/编辑失败
  3. 例外需要理由——如果必须更新基线,记录原因
  4. LLM 没有特殊待遇——同样的规则适用于人类和 AI 生成的代码

目标是防止技术债务的逐渐积累,当 LLM 优化为“让测试通过”而非“解决实际问题”时,这种积累就会发生。


快速参考

# 首次设置
slopwatch init
git add .slopwatch/baseline.json

# 每次 LLM 代码更改后
slopwatch analyze

# 严格模式(推荐)
slopwatch analyze --fail-on warning

# 附带统计信息(性能调试)
slopwatch analyze --stats

# 更新基线(很少使用,记录原因)
slopwatch analyze --update-baseline

# 用于工具的 JSON 输出
slopwatch analyze --output json

何时覆盖(几乎从不)

更新基线或禁用规则的唯一有效理由:

场景操作要求
第三方强制模式更新基线解释原因的代码注释
生成代码(不可编辑)添加到排除列表在配置中记录
故意的速率限制延迟更新基线代码注释,不在测试中
遗留代码清理一次性基线更新PR 描述

无效理由:

  • “测试不稳定” → 修复不稳定性
  • “警告很烦人” → 修复代码
  • “在我机器上能运行” → 修复竞争条件
  • “我们稍后修复” → 立刻修复

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

akka-hosting-actor-patterns

Patterns for building entity actors with Akka.Hosting - GenericChildPerEntityParent, message extractors, cluster sharding abstraction, akka-reminders, and ITimeProvider. Supports both local testing and clustered production modes.

akka-net-aspire-configuration

Configure Akka.NET with .NET Aspire for local development and production deployments. Covers actor system setup, clustering, persistence, Akka.Management integration, and Aspire orchestration patterns.

akka-net-best-practices

Critical Akka.NET best practices including EventStream vs DistributedPubSub, supervision strategies, error handling, Props vs DependencyResolver, work distribution patterns, and cluster/local mode abstractions for testability.

akka-net-management

Akka.Management for cluster bootstrapping, service discovery (Kubernetes, Azure, Config), health checks, and dynamic cluster formation without static seed nodes.

akka-net-testing-patterns

Write unit and integration tests for Akka.NET actors using modern Akka.Hosting.TestKit patterns. Covers dependency injection, TestProbes, persistence testing, and actor interaction verification. Includes guidance on when to use traditional TestKit.

api-design

Design stable, compatible public APIs using extend-only design principles. Manage API compatibility, wire compatibility, and versioning for NuGet packages and distributed systems.

aspire-configuration

Configure Aspire AppHost to emit explicit app config via environment variables; keep app code free of Aspire clients and service discovery.

aspire-integration-testing

Write integration tests using .NET Aspire

aspire-service-defaults

Create a shared ServiceDefaults project for Aspire applications. Centralizes OpenTelemetry, health checks, resilience, and service discovery configuration across all services.

crap-analysis

Analyze code coverage and CRAP (Change Risk Anti-Patterns) scores to identify high-risk code. Use OpenCover format with ReportGenerator for Risk Hotspots showing cyclomatic complexity and untested code paths.

csharp-concurrency-patterns

Choosing the right concurrency abstraction in .NET - from async/await for I/O to Channels for producer/consumer to Akka.NET for stateful entity management. Avoid locks and manual synchronization unless absolutely necessary.

database-performance

Database access patterns for performance. Separate read/write models, avoid N+1 queries, use AsNoTracking, apply row limits, and never do application-side joins. Works with EF Core and Dapper.

dependency-injection-patterns

Organize DI registrations using IServiceCollection extension methods. Group related services into composable Add* methods for clean Program.cs and reusable configuration in tests.

dotnet-devcert-trust

Diagnose and fix .NET HTTPS dev certificate trust issues on Linux. Covers the full certificate lifecycle from generation to system CA bundle inclusion, with distro-specific guidance for Ubuntu, Fedora, Arch, and WSL2.

dotnet-local-tools

Managing local .NET tools with dotnet-tools.json for consistent tooling across development environments and CI/CD pipelines.

dotnet-project-structure

Modern .NET project structure including .slnx solution format, Directory.Build.props, central package management, SourceLink, version management with RELEASE_NOTES.md, and SDK pinning with global.json.

efcore-patterns

Entity Framework Core best practices including NoTracking by default, query splitting for navigation collections, migration management, dedicated migration services, and common pitfalls to avoid.

ilspy-decompile

Understand implementation details of .NET code by decompiling assemblies. Use when you want to see how a .NET API works internally, inspect NuGet package source, view framework implementation, or understand compiled .NET binaries.

mailpit-integration

Test email sending locally using Mailpit with .NET Aspire. Captures all outgoing emails without sending them. View rendered HTML, inspect headers, and verify delivery in integration tests.

marketplace-publishing

Workflow for publishing skills and agents to the dotnet-skills Claude Code marketplace. Covers adding new content, updating plugin.json, validation, and release tagging.

相关技能