This directory contains three kinds of scripts. Only the first kind runs automatically through Claude Code.
These receive the hook payload as JSON on stdin and run automatically while Claude works:
| Script | Event | Purpose |
|---|---|---|
session-start.sh |
SessionStart (startup, clear, compact) | In a .NET repo (a .csproj, .fsproj, .sln or .slnx within three levels), adds the skill index and a digest of the 8 rules from session-context.json (about 1.5k tokens). Prints nothing elsewhere. CWM_SESSION_CONTEXT=off disables it. When the superpowers plugin is enabled (user or project settings), it also adds a rule that leaves process to superpowers and .NET specifics to the kit; CWM_SUPERPOWERS=off drops that rule. Regenerate the context with python3 .github/scripts/generate_session_context.py |
pre-bash-guard.sh |
PreToolUse (Bash) | Blocks destructive commands (force push, git reset --hard, unsafe rm -rf, unscoped process kills, shared-cache deletes) — see below |
post-edit-format.sh |
PostToolUse (Edit|Write) | Queues each edited .cs file (a few milliseconds) for stop-format.sh |
stop-format.sh |
Stop | When Claude finishes a turn, formats the queued files once, one dotnet format (or CSharpier) call per project — see below |
post-scaffold-restore.sh |
PostToolUse (Edit|Write) | Runs dotnet restore after .csproj changes |
post-edit-slop-guard.sh |
PostToolUse (Edit|Write|MultiEdit) | Blocks shortcuts in the lines an edit adds to a .cs file — see below |
The one the repository already uses. Walking up from the edited file, the hook looks for any of:
.csharpierrc,.csharpierrc.json,.csharpierrc.yamlor.csharpierrc.yml- a
csharpierentry indotnet-tools.jsonor.config/dotnet-tools.json
If it finds one, it runs dotnet csharpier format <file> (CSharpier 1.0+) and
never dotnet format. Otherwise it runs dotnet format <project> --include <file>
against the nearest .csproj or .sln.
Running both is not an option: dotnet format and CSharpier disagree on
indentation in places (unbraced nested foreach over a wrapped expression,
multi-line property patterns), so each would undo the other and
dotnet csharpier check would fail on files only the hook touched.
Either way a failed format is printed and the hook still exits 0 — a file that does not compile mid-edit must not interrupt the edit.
Options
| (unset) | Queue edits and format once per turn. Formatting after every edit cost about 4 s per edit; a 15-edit turn now waits about 6 s once instead of a minute |
CWM_POST_EDIT_FORMAT=edit |
Format right after each edit, the old behaviour, if you want Claude to see the formatted file mid-turn |
CWM_POST_EDIT_FORMAT=off |
Disable formatting entirely. Claude Code cannot turn off a single plugin hook, so this is the way to keep the plugin and drop the formatting |
Set it in the environment Claude Code starts from, or in the env block of a
project's .claude/settings.json.
The cheapest way to turn a red build or test green is to silence it. The guard
looks at only the lines the edit added (git diff HEAD for a tracked file,
every line for an untracked one) and blocks, with the reason on stderr so the
model sees it:
| Pattern | Why |
|---|---|
#pragma warning disable with no // reason after it |
Hides the warning instead of fixing it. A comment saying why is allowed |
[Fact(Skip...)], [Theory(Skip...)], Skip = ... (test files) |
A skipped test proves nothing |
Assert.True(true), Assert.Pass |
Passes by construction |
catch { }, catch (Exception) { } |
Swallows the failure |
throw new NotImplementedException() in non-test code |
A stub presented as an implementation |
A violation already in the file is never reported, so an edit next to old debt
is not blocked. Matching is single-line, and a file outside a git repository is
skipped (no baseline to diff against). A test file is any whose path inside the
repository contains test.
CWM_SLOP_GUARD=warn |
Print the findings but never block (exit 0) |
CWM_SLOP_GUARD=off |
Disable the hook entirely |
Because grepping the command string cannot tell a command from a mention of
one. The guard used to match rm\s+-[a-zA-Z]*r[a-zA-Z]*f against the whole
string, which blocked all of these — none of which deletes anything:
grep -rn 'rm -rf' docs/ # the search pattern
python3 build.py # the old cleanup used rm -rf
git log --grep='git reset --hard' # a --grep value
echo "never run git push --force on main" # a quoted argumentSo the guard splits the command on ;, &&, ||, |, newlines and grouping,
and matches only each segment's command word — token 0, after stripping
VAR=value assignments, sudo/env/command/nohup, and any leading path so
/bin/rm still reads as rm. Quoted strings, # comments, and heredoc bodies
are data and never trigger a rule.
Parsing also closes the gaps a text scan left open. These reach a real rm -rf
and are all blocked:
sudo rm -rf /project xargs rm -rf find . -exec rm -rf {} \;
bash -lc 'rm -rf /project' $(rm -rf /project) /bin/rm -rf /projectThe -c handling matches any short-flag cluster containing c (-lc, -cx,
-ec), not an exact -c token — an exact match is one letter from a bypass.
Deliberate obfuscation (eval, base64, a variable holding the command name)
still gets through. This is a guardrail against accidents, not a sandbox.
Rationale, alternatives, and the process for adding a rule:
ADR-007.
| Rule | Blocked | Allowed |
|---|---|---|
| Force push | git push --force, git push -f |
git push origin feature/x |
| History and worktree loss | git reset --hard, git clean -fd, git checkout . |
git clean -nd, git checkout main |
| Recursive delete | rm -rf /any/other/path |
rm -rf bin, obj, TestResults, node_modules, .vs, anything under /tmp |
| Shared package caches | rm -r ~/.nuget, ~/.dotnet, ~/.templateengine, ~/.local/share/NuGet — with or without -f |
project build output, including src/App/bin |
| Unscoped process kills | pkill, killall, kill $(pgrep …), kill <name> |
kill 12345, kill -s TERM 4242, kill $SERVER_PID, pgrep |
| Shared daemons | dotnet build-server shutdown |
dotnet build, dotnet nuget locals all --list |
| Risky but allowed | — | dotnet run (warns about launchSettings.json) |
The process rules exist because a developer box often runs more than one agent.
pkill -f dotnet does not know which dotnet was yours. If you hold a PID,
kill it; the guard only stops kills that go looking for a target.
These are standard git hooks, not Claude Code hooks. Wire them into your repo's pre-commit hook:
| Script | Purpose |
|---|---|
pre-commit-format.sh |
Fails the commit if dotnet format --verify-no-changes finds issues |
pre-commit-antipattern.sh |
Blocks commits that add async void (AP001), sync-over-async (AP002), new HttpClient() (AP003), or DateTime.Now/UtcNow (AP004) |
# One-time setup per clone — .git/hooks/pre-commit
#!/usr/bin/env bash
bash hooks/pre-commit-format.sh && bash hooks/pre-commit-antipattern.shpre-commit-antipattern.sh reads its rules from lib/antipattern-scan.awk —
keep the two files together.
Options
// cwm:ignore |
Suppress every rule on that line |
// cwm:ignore AP004 |
Suppress only the named rule(s) |
CWM_ANTIPATTERN_WARN_ONLY=1 |
Report findings without blocking the commit |
Because a git hook cannot reach the analyzer. The kit detects these same four patterns in three tiers, and each tier is placed where it can actually run:
| Tier | Where it runs | Cost | Purpose |
|---|---|---|---|
1. pre-commit-antipattern.sh |
git pre-commit, bare shell | milliseconds, no build | Last gate before history |
2. detect_antipatterns (Roslyn MCP) |
Claude Code session | workspace load | The authoritative pass |
| 3. Analyzer packages in your build | IDE + CI | full compile | Team-wide enforcement |
Tier 2 is strictly more precise — it has a SemanticModel, so it knows whether
.Result sits on a Task<T> or on your own Result<T>. But a git hook runs in
a bare shell where no MCP server is reachable, and shelling out to dotnet build
in front of every commit costs seconds and fails outright on a red build, which
is exactly when work-in-progress gets committed.
So tier 1 scans text, and is written to deserve that spot rather than to approximate tier 2:
- Only the lines the commit adds are checked — a legacy
DateTime.Nowin an untouched line never blocks an unrelated edit. - Comments and string literals are stripped, tracking block comments, verbatim strings, and raw strings across line boundaries.
- Generated, test, and migration sources are exempted per rule, matching
SourceClassifierin the MCP server exactly. - Ambiguous matches stay silent.
.Resultis reported only when the receiver is visibly task-like;new HttpClient(handler)is never reported. Tier 1 takes false negatives to guarantee zero false positives — tier 2 catches the rest.
Run tier 2 (via /verify) for the exhaustive pass. Rationale and the
rule-addition process: ADR-006.
Run these directly or let kit commands (/verify, /tdd, /verify health)
invoke them:
| Script | Usage |
|---|---|
pre-build-validate.sh |
bash hooks/pre-build-validate.sh [solution-dir] — checks solution structure (sln file, Directory.Build.props, global.json, test projects) |
post-test-analyze.sh |
dotnet test 2>&1 | bash hooks/post-test-analyze.sh — summarizes test results with actionable next steps |