A short rules file your coding agent loads at the start of every session, so it stops
repeating the Windows shell mistakes that cost us whole sessions. It comes with
pid_is_alive.py, a small, tested, stdlib-only helper that fixes the worst of them in code.
Works with Claude Code, and with any agent that reads a Markdown rules file (Cursor, Codex, Aider, your own).
| File | What it is |
|---|---|
windows-shell.md |
The drop-in rules file the agent reads |
pid_is_alive.py |
A cross-platform "is this process running?" check that's safe on Windows |
tests/test_pid_is_alive.py |
pytest suite, including a guard that runs on Linux CI |
A person who hits a Windows shell trap swears once and remembers it. An agent loses its
session, comes back with no memory of why, and runs the same command again. We watched
one of ours do that five times in an afternoon. The cause was os.kill(pid, 0), the
standard POSIX "is it alive?" check. On Windows, signal 0 is CTRL_C_EVENT, so the check
sends Ctrl+C to the whole console. The shell dies, the agent dies with it, and the event
logs stay empty. (Field Notes #1)
The second trap is quieter. On Windows, a Bash tool command over about 7 KB never runs at all. cmd.exe's 8,191-character command-line limit cuts off the wrapper's closing quote, and bash reports an unmatched quote on "line 1". The agent takes that at face value and fixes the quoting, which leaves the command the same size, so it fails again. We measured the real cutoff across 29,012 calls. (Field Notes #2)
Code fixes stay in the repo you made them in. Rules go with the agent into every repo it works in next. The file covers MSYS path conversion, PowerShell 5.1 quote mangling, symlinks, the signal-0 trap, the 7 KB limit and the heredoc gotchas. Each rule comes with the evidence behind it and the fix to use.
For every project (recommended): copy the rules file into your user-level rules directory. Claude Code loads it in every session.
macOS / Linux: ~/.claude/rules/windows-shell.md
Windows: %USERPROFILE%\.claude\rules\windows-shell.md
For one project: keep the file in the repo and include it from CLAUDE.md:
@docs/agent-rules/windows-shell.mdThe @ path is resolved relative to the CLAUDE.md that contains it.
Paste the contents of windows-shell.md into the agent's system prompt, or into its
project rules file (for example AGENTS.md or .cursor/rules/). It's plain Markdown with
no tool-specific syntax. The Write-tool advice in rule 5 applies to any agent that has a
file-writing tool alongside its shell tool.
Keep it loaded even on macOS and Linux if your agents ever touch a Windows machine, a Windows CI runner, or code that might run on one.
Copy pid_is_alive.py into your project. It needs only the standard library and Python
3.9+.
from pid_is_alive import pid_is_alive
pid = int(Path("service.pid").read_text())
if pid_is_alive(pid):
raise SystemExit("already running")
# stale lock file: safe to take it over- POSIX: uses
os.kill(pid, 0), which is correct there. - Windows: never calls
os.kill. It opens the process withSYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATIONand pollsWaitForSingleObjectwith a zero timeout.ERROR_ACCESS_DENIEDcounts as alive. pid <= 0always returnsFalse.- The answer is a snapshot. A process can exit, or its PID can be reused, right after the call returns.
Then find the calls it replaces:
grep -rnE "os\.kill\([^,]+, *0\)" .Run the tests:
python -m pytest tests -qThe suite fakes a non-POSIX os.name and asserts that os.kill is never called, so the
Windows guard runs on Linux CI too. We confirmed it fails when the bug is put back. On
Windows, the suite also swaps os.kill for a function that fails loudly, so a regression
shows up as a failed test, not as a dead terminal.
- Boternal Field Notes: a free weekly email. One real failure from running AI agents unattended, and the fix.
- The Boternal Stack: this module plus four more (a deletion guard hook, job leases for shared cloud machines, delegation briefs, unattended runbooks). $49, one-time.
Issues and pull requests are welcome. If a rule here is wrong on your setup, open an issue with the command and the output.
MIT. © 2026 Leaf Logic LLC.