State Management in Multi-Agent Systems - OS Is Shared State

Fazm Team··3 min read

State Management in Multi-Agent Systems

Running one AI agent on your desktop is straightforward. Running three at the same time is a concurrency nightmare. The operating system is shared mutable state, and agents do not coordinate by default.

The Shared State Problem

Agent A opens a file in VS Code to make edits. Agent B opens the same file to add a different feature. Agent A saves. Agent B saves. Agent A's changes are gone. This is not a hypothetical - it happens constantly when running parallel coding agents.

The filesystem is just the start. Agents share the clipboard, the active window focus, keyboard input, and mouse control. If two agents try to type at the same time, you get interleaved garbage. If one agent switches apps while another is mid-click, the click lands in the wrong window.

File Locks Are the Minimum

The simplest fix is file-level locking. Before an agent writes to a file, it acquires a lock. Other agents wait until the lock is released. This is the same approach databases have used for decades and it works for the same reasons.

For coding agents, git worktrees provide natural isolation. Each agent works in its own worktree with its own copy of the files. Conflicts are resolved at merge time, not during editing. This is significantly more robust than file locks alone.

Coordination Protocols

Beyond file access, agents need a way to communicate intent. A simple shared queue or message bus lets agents declare "I am working on the authentication module" so other agents avoid that area.

The practical implementation is a coordination file - a JSON file that each agent reads before starting work. It lists which files and directories are claimed by which agent. Simple, file-based, no infrastructure needed.

Window and Input Isolation

For desktop automation agents, input isolation is harder. Only one agent can control the mouse and keyboard at a time. The solution is either time-slicing - agents take turns - or running agents in separate user sessions or virtual displays.

On macOS, you can run agents in separate spaces or use virtual displays to give each agent its own screen to control. This eliminates the shared input problem entirely.

Fazm is an open source macOS AI agent. Open source on GitHub.

More on This Topic

Related Posts