← All articles

When AI agents get in each other's way: four races and the fixes

· 6 min read · Anton Tkachev

Translated from Russian. Read the Russian original.

One AI agent is like one employee. It can make mistakes, but at least it doesn't argue with itself.

The trouble starts when you have several agents. I usually have two or three sessions open in parallel: one fixes a report in the CRM, another adds a button to the same CRM, and a third translates posts on the server. Each one is sure it's working alone. None of them knows its neighbor is touching the same files right now.

Once, in a single week, parallel sessions deployed the same CRM four times in a way where one rolled back another's work. No downtime and no data loss, but it was enough to make one thing clear: agent conflicts are their own class of problem, and you don't solve them by asking agents to "be more careful."

Class 1. One name for two

I deployed each new CRM version into a folder named "date + sequence number for the day." Two sessions calculated the next number in the same minute, and both got the same name.

The first one uploaded its version and restarted the service. The second one wrote its files on top, into the same folder. The running program kept the old code in memory and kept serving the team like nothing had happened. But the disk now held a mix of two versions, and any later restart would have brought up a broken CRM.

The same class shows up in small things:

  • two sessions took the same number for a new task;
  • two sessions started a test server on the same port;
  • two backup copies of files with the same name, from different folders, overwrote each other.

What helped: names that can't collide: date, version hash, and seconds. And a hard rule: if the folder already exists, stop. No "I'll just add to it."

Class 2. A plan based on a stale snapshot

The second race happened the same day, and it was sneakier. A session was building a clean version: "whatever is on the server right now, plus only my changes." At the start, it checked which version was running. Ten minutes later, when the build was done, it switched the server to its version.

Except in those ten minutes, the neighboring session had already deployed a fresh version that included both sets of changes. The switch technically succeeded, and it rolled the server back five minutes.

The decision was made from a ten-minute-old snapshot. Between "I checked" and "I'm switching," nobody checked again.

The same thing happened on the guides site: a session copied two articles, made an edit nine hours later, and wrote it over the originals. In the meantime, the neighboring session had improved those same articles. The script even showed the diff, but it didn't stop. I restored from backup in a minute.

What helped: a check right before the action, in the same command. "Is the server still on the version I started from? No? Then refuse." Programmers call this compare-and-swap. And a check that halts execution, not one that just prints a warning.

Class 3. The rule lived in the agent's head

After the first two races, I wrote the agents a rule: "build your version from what's on the server now, not from your previous release." Four days later, a session built a version from its previous release and rolled back its neighbor by two minutes.

The rule was in the agent's memory. Memory is text the agent may or may not have read, and understood one way or another. The deploy script knew nothing about that rule.

After the third incident, the rule moved into the script itself: it checks what's on the server right now and won't let you deploy a version that doesn't include it.

Two days later came the fourth race, with the safeguards already in place. One check looked at the server two or three minutes before the switch; the other compared against what it had read itself. The neighbor's fresh version lived on the server for about 15 seconds before being overwritten by a version without its changes. The safeguard was finished the same day: now the script verifies exactly what's running on the server right before the switch.

The lesson isn't pleasant: a safeguard that looks at the wrong moment isn't a safeguard. But each of the four races closed its own gap, and there hasn't been a fifth one yet.

Class 4. A copy mistaken for the original

At my request, an agent on the server created four personal tasks. It saved them to the only place it had access to: the HQ folder. But that folder is a copy: a script on my laptop syncs it every 15 minutes with the originals in the department folders and deletes everything extra. That evening, it deleted all four tasks, including a rent payment due the next day.

A week later, the mirror-image story. A cleanup agent "archived" 34 tasks in that same copy. The originals stayed put, and the mirror dutifully brought them back. The tasks ended up both in progress and in the archive at the same time.

Both times, the agent mistook the copy for the original. Both times, nothing mechanically stopped it from writing where it shouldn't.

What helped: one rule in every agent's instructions ("the source of truth for tasks is the department folder; the copy is read-only") and scripts that archive only in the original.

Class 5. Two authors, one repository

Today's case, from the incident log: I pushed my blog edits to the repository while the translator agent on the server hadn't pushed its own yet. The next push from the server was rejected: "the repository already has changes you don't have." The sync script used to just give up and send an alert.

What helped: now, before pushing, it pulls the other changes and applies its own on top. An alert only comes in for a real conflict, when two authors edited the same line.

General rules

All these cases boiled down to a short list:

  1. Unique names. Anything created in parallel gets a name that can't collide.
  2. Check before the action, not before the plan. The state you started from gets checked again at the moment of the change.
  3. Put the rule in the script. If a rule decides whether the server breaks, it has to live in code, not in the agent's memory.
  4. One source of truth. Every file has one owner; everything else is a read-only copy.
  5. Isolation. Each session gets its own working copy of the project, its own test database, and its own ports that it checked itself.
  6. A human has the final say. Switching the production server or rolling it back requires my explicit "yes." Once this slowed down a correct rollback, and I had to do it by hand. I kept the rule anyway: a slow rollback beats a fast wrong move by an agent that doesn't know about its neighbor.

What to do in your own setup

If you run more than one AI agent on the same project:

  1. List everything shared: folders, databases, ports, numbered files.
  2. For each item, answer: what happens if two agents grab it in the same second?
  3. Wherever the answer is "something breaks," use a unique name, a lock, or a check right before writing. In the script, not in the prompt.