Blog · Guide · 16 November 2026

File Locking for AI Agents: Leases, Not Locks

File locking for AI agents sounds like a solved problem: take a lock, edit, release. In practice hard locks are one of the first things teams try and abandon when they run agents in parallel. Agents forget to release, crash while holding, and queue behind each other. The design that holds up is the lease: a claim on paths that expires by itself, that is granted even when it overlaps, and that is enforced at the moment of the edit. This guide explains why, and how Forkit's file leases are built.

Why plain file locking for AI agents fails

Cursor ran the experiment and published the result. In its first design for many agents, each agent checked a shared file, claimed a task and took a lock. Three things went wrong:

  • Agents held locks for too long, or forgot to release them.
  • Even when locking worked, it was a bottleneck: twenty agents slowed to the throughput of two or three, with most of the time spent waiting.
  • The system was brittle. Agents failed while holding locks, tried to take locks they already held, or changed the shared file without taking the lock.

These are not bugs in one implementation. They follow from what agents are. An agent has no reliable sense of "I am done with this". Its session can end at any moment: context limit, crash, a person closing a terminal. And an agent that is told to wait will wait, burning time, with no instinct to go and do something else.

A lock assumes a careful holder. Agents are not careful holders.

From locks to file leases

A lease changes four things.

1. It expires. Every claim has a time limit. If the holder disappears, the claim runs out. Nobody has to clean up.

2. It renews through activity. A working agent keeps its claim without remembering to. A silent one loses it.

3. It does not block other claims. A second agent asking for overlapping paths is told about the overlap and granted its claim anyway. Nobody waits in a queue.

4. It is enforced at the edit. The refusal happens when an agent tries to change a file someone else holds, and comes with the holder's name.

The combined effect: no waiting, no deadlock, no stale locks, and the conflict still surfaces before any work is wasted.

How Forkit's leases work

The details below are from Forkit's documentation.

Path claims. An agent calls claim_paths with repo-relative paths or globs:

const { leases, conflicts } = await codemode.claim_paths({
  patterns: ['src/billing/**', 'migrations/2026_11_*.sql'],
  mode: 'exclusive',
  ttl_seconds: 1800,
  reason: 'add tax_cents to invoices'
});

Lease expiry. The default lifetime is 30 minutes and the maximum is 4 hours. Capture events on the agent's thread (its commands and edits, reported by hooks) renew its leases. The end of the session releases them.

Patterns are only patterns. A pattern is a path or glob without spaces or quotes. The stated reason: a claim cannot carry a sentence. An agent cannot use a claim to pass instructions to other agents.

Per repository. A claim applies within one repository, taken from the agent's thread.

Overlaps are granted and reported. The response lists conflicts: other agents' live claims on the same files. Both agents also receive a lease_conflict message, at most one per pair of agents every ten minutes, quoting only the overlapping patterns. The message is recorded as coming from the claiming agent, never from a person or the system, so it cannot be mistaken for an instruction from a human.

The edit is what gets refused. In Claude Code, Forkit's PreToolUse hook asks the server about every path in an edit, in one request. An edit under another agent's exclusive lease is denied, and the denial names the holder. To keep this cheap, the hook only asks when the server's last response said other claims exist, or when that information is more than 30 seconds old.

Release. release_paths ends claims early, by id or for a whole thread. list_leases shows what is live in the workspace, and check_path tests a single path.

Exclusive and shared modes

A lease is exclusive or shared.

  • Exclusive means "I am changing these files; others should not".
  • Shared means "I am working with these files and want to know who else is". Shared claims record presence without refusing anyone's edits.

Use exclusive for the files a task will rewrite. Use shared for files several agents must each touch lightly, such as a changelog, where you want visibility more than protection.

What to do when a conflict is reported

An agent that receives conflicts on its claim, or a refused edit, has three reasonable moves:

  1. Work elsewhere. Do the parts of the task that are free and come back.
  2. Ask the holder. Send a question to the holding agent or to the planner. The denial names the holder for this reason.
  3. Tell the planner. An overlap usually means two tasks were scoped badly. The planner should re-split.

What it should not do is wait and retry in a loop. Waiting is the lock behaviour that leases were meant to remove.

Put this in your agents' instructions. One sentence is enough: "If a file is claimed by another agent, do not edit it; message your planner and continue with other files."

Sizing claims

  • Claim what the task covers, not the whole repository. src/** defeats the purpose.
  • Prefer directories to single files when a task owns a module. New files in the directory are then covered.
  • Keep lifetimes near the task's length. A 4-hour claim for a 10-minute task hides the files from others for no reason if the session lingers.
  • Claim before reading deeply. The earlier the claim, the earlier a conflict shows.
  • Give a reason. Other agents and people see it in the list of leases.

Where leases are not the answer

Edit conflicts are one problem among several.

  • Shared build state. Two agents in one directory still fight over build output. Give each its own worktree or sandbox.
  • Semantic conflicts. One agent changes a function's behaviour while another, in different files, relies on the old behaviour. No path claim sees that. Tests do.
  • Agents without hooks. The refusal at edit time needs a hook. An agent connected only through MCP can still claim and check, but nothing stops it from ignoring the answer.

For a single developer with a few agents on unrelated tasks, worktrees alone are enough, and leases add a moving part you do not need. See git worktrees vs file leases for when each fits.

A small example

Two workers start from one plan.

  1. Worker A claims packages/api/**. No conflicts.
  2. Worker B claims packages/web/** and packages/api/types.ts. The claim is granted; the response lists A's claim as a conflict, and both get a notice.
  3. B edits files under packages/web/. Allowed.
  4. B tries to edit packages/api/types.ts. Refused: held by worker A.
  5. B asks the planner. The planner tells A to add the type B needs. A does, and hands off.
  6. A's session ends. Its claims are released without anyone calling release.

The whole exchange cost one refused edit and two messages.

Related: how to run 50 coding agents without merge conflicts and agent-to-agent messaging patterns.

Where Forkit fits

Forkit provides file leases as a service: claims over MCP or HTTP, renewal from hook activity, conflict notices, and a hook that refuses edits under someone else's exclusive claim. It is free to try. More at forkit-mcp.com.