---
title: "Write gate: a check that holds an AI agent's edit before it lands · Cork"
description: "A write gate checks each file edit an AI coding agent is about to make and holds or blocks it when it would collide with another agent's live work on that code."
canonical: "https://corkcli.com/blog/write-gate"
last_updated: "2026-10-03"
published: "2026-10-03"
author: "Kevin Vera"
---

# Write gate: a check that holds an AI agent's edit before it lands

A write gate checks each file edit an AI coding agent is about to make and holds or blocks it when it would collide with another agent's live work on that code.

By Kevin Vera · October 3, 2026 · [Glossary](https://corkcli.com/blog/c/glossary) · 3 min read

A **write gate** is a check that runs before an AI coding agent writes to a file and decides whether the write may proceed, so that an edit which would collide with another agent's live work is held, explained to the agent, and retried once the agent has caught up.

It is a pre-write check, not a post-merge one. The point is to stop a collision at the moment it would be created, while the agent can still adapt cheaply.

## How it works

Modern agent tools let outside code run before a tool call. Claude Code's hooks documentation lists a `PreToolUse` event that fires "before a tool call executes" and "can block it": a hook can exit with code 2, or return JSON with `"permissionDecision": "deny"` and a reason that the agent reads. A write gate is a hook (or an equivalent mechanism) attached to edit and write tools.

A typical gate does three things:

1. **Identify the target.** Which file, and ideally which function or class, the edit changes.
2. **Check live state.** Is another agent or person changing that code right now? Has it changed since this agent last read it?
3. **Decide and explain.** Allow the write, or hold it with a reason specific enough for the agent to act on: whose change it is, what it changed, what to do next.

A good gate never just says "no". A bare denial makes an agent retry blindly or give up; a reason plus the other side's current version lets it merge its intent into the new code and write again.

## Examples

- Agent A is midway through changing `charge_card`. Agent B tries to edit the same function from an older read. The gate holds B's write, shows B what A changed, and B reapplies its edit on top of A's version.
- An agent tries to write a migration file numbered `0042` that another agent already claimed. The gate holds it and names the number the agent should use instead.
- Two agents edit different functions in the same file. A symbol-level gate lets both through; a file-level lock would have blocked one for no reason.

## How teams handle it

Other ways teams prevent concurrent writes:

- **File locks or ownership rules.** Simple, but coarse: they block unrelated edits in the same file and do nothing about dependent code elsewhere.
- **Isolation.** Separate [git worktrees](https://corkcli.com/blog/git-worktrees-parallel-ai-coding-agents) make every write safe in the moment and push the collision to merge time.
- **Permission prompts.** A human approves each write, which works for one agent and does not scale to many.
- **Custom hooks.** Teams write their own `PreToolUse` scripts for protected paths or patterns.

Cork ships a write gate as part of its hooks. Cork is the [coordination layer](https://corkcli.com/blog/coordination-layer-ai-coding-agents) for multiplayer coding agents: it keeps Claude Code, Codex and Cursor agents, on one machine or across a team's machines, from overwriting each other on one codebase, and hands each agent what the others already learned. Its gate holds an edit against code another agent is changing right now until your agent has their version, with no extra model calls to run the check.

## Related terms

- [Agent collision](https://corkcli.com/blog/agent-collision)
- [Agent merge conflict](https://corkcli.com/blog/agent-merge-conflict)
- Coordination layer (for AI coding agents)
- [Agent briefing](https://corkcli.com/blog/agent-briefing)
- [Semantic lint](https://corkcli.com/blog/semantic-lint)

## Sources

- <https://code.claude.com/docs/en/hooks>

## Related reading

- [Code graphs for AI coding agents: why agents need one](https://corkcli.com/blog/code-graph-for-ai-coding-agents): Why AI coding agents work better with a code graph: fewer file reads, exact callers before an edit, and a map that stays current as several agents edit.
- [How to stop Claude Code agents from overwriting each other's changes](https://corkcli.com/blog/stop-claude-code-agents-overwriting-changes): Why parallel Claude Code agents overwrite each other's edits, and how to stop it with worktrees, file ownership, deny rules, PreToolUse hooks and a write gate.
- [How to share context between Codex and Claude Code](https://corkcli.com/blog/share-context-codex-claude-code): Make Codex and Claude Code work from the same context: one AGENTS.md, shared MCP servers, handoff files, and live awareness when both edit the same repo.
- [Cork vs git worktrees: separate folders, or agents that know about each other](https://corkcli.com/blog/cork-vs-git-worktrees): Cork vs git worktrees for parallel AI coding agents: worktrees isolate files while agents work, Cork coordinates them so the work still fits at merge.

[All articles](https://corkcli.com/blog.md) · [Cork in Markdown](https://corkcli.com/index.md) · [llms.txt](https://corkcli.com/llms.txt)

This page in HTML: https://corkcli.com/blog/write-gate
