Two Claude Code sessions edited the same file. One of them was wrong. I built a hook to stop that

작성자

카테고리:

← 피드로
DEV Community · hao li · 2026-09-30 개발(SW)

hao li

The failure mode, quoted from a real report: “With several agent sessions on one repository, session A reads createSession, session B changes it, and A then edits on the old assumption.”

I run multiple Claude Code sessions against the same repo all the time. The stale-edit problem is silent — no error, just a quietly wrong merge of intentions.

edit-guard is a PreToolUse hook that fixes it in ~60 lines of stdlib Python. It watches every Read/Edit/Write through the hook, remembers what each session last saw, and blocks the write when the file moved on without you — telling the agent to re-read first.

Design choices worth noting:

  • Fail-open. If the hook itself errors, your edit proceeds. A guard that breaks your workflow is worse than no guard.
  • Backs up your settings. edit-guard install merges three hook entries into ~/.claude/settings.json and backs it up first. Never clobbers.
  • Single file. Copy it anywhere, read it in one sitting, audit it yourself.
# not on PyPI yet — install from source
git clone https://github.com/hahahahahahahahah6/edit-guard.git
cd edit-guard
pip install .
edit-guard install

Enter fullscreen mode Exit fullscreen mode

Repo: https://github.com/hahahahahahahahah6/edit-guard

MIT licensed. The whole thing is small enough to read before you trust it — I’d actually recommend that.

원문에서 계속 ↗