Split work across subagents with a hook that blocks out-of-scope writes

A developer had a Claude Code PM agent plan three tasks with disjoint file scopes; subagents built them, and a PreToolUse hook refused the one write forced outside its task.

Done withClaude Code· Nov 15, 2026

What they did

The author, who posts on dev.to as Agent Shipped, wanted parallel Claude Code subagents to stop overwriting each other. They had Claude write a PreToolUse hook that reads a plan file and the active task id, then blocks any Edit, Write or MultiEdit outside that task's file scope with exit code 2 and a reason. The task id lives in a file set from the terminal so an agent cannot switch itself to another scope. A PM agent, allowed to write only the plan, split the job into three tasks (a slug helper, a word counter, usage docs) and printed an overlap check. The author committed the plan, then the main agent handed each task to a subagent through the Task tool with a four-line hand-back: result, commit, tests, open items.

How it went

The first hook blocked a legal write: Claude Code sends absolute paths and the gate compared them as plain strings against relative scopes. Normalising paths against the project directory, and treating anything that resolves above it as outside, fixed that, pinned by five tests. In the real run all three tasks finished with one commit each, and the single write the author forced the second subagent to make into the first task's file was refused and logged, with git status showing no change.

Worth knowing

The gate only watches the file edit tools, not Bash, so a shell redirect can still write anywhere. The tasks were small demo tasks on a public repo, and the post is also a video walkthrough of the author's technique.

Try it

With Claude Code· 5 parts to fill in

Set up a scope gate for subagents in [my repo]. First, as the PM, write only [plan file path]: split [the job] into [number] tasks, give each a list of files or folders it may write, and print a check that no path appears in two tasks. Then write a PreToolUse hook for Edit, Write and MultiEdit that reads the active task id from [a file I set from the terminal], resolves the target path against the project directory, and exits with code 2 and a message naming the task, the path and the allowed scope when the write falls outside it. Write tests for absolute paths, relative paths and ../ traversal. Pause and show me the plan before any subagent starts, and stop when every task has a commit and the tests pass.

Square brackets are the parts to fill in. The prompt is ours; the story is theirs.

Where they said it

Related