Two Claude Code sessions can message each other. That does not stop them overwriting your files.
I ran two Claude Code sessions against one repository and assumed the hard part was coordination. It is not. The hard part is that coordination looks like it solves a problem it never touches. What is actually built in ๐ง Two sessions can find each other and talk. Anthropic's docs on cross-session messaging describe it: one session warns another that a change broke what it was building on, orโฆ
Two separate Claude Code sessions can communicate with each other, but this does not prevent them from potentially overwriting your files. The Claude system allows for cross-session messaging, where one session can notify another about changes that affect its work, or answer questions the other is blocked on. This is achieved using two tools: ListAgents to see who is reachable and SendMessage to communicate with another session by name.
However, the messaging system is not designed to lock files or coordinate changes made by different sessions.
When two sessions attempt to edit the same file in a shared working directory, they can unintentionally overwrite each other's changes. Claude Code does not have any automatic source-file locking or merge layer to reconcile differences. Instead, each session operates within its own worktree, which is a separate directory with its own branch and copy of the tracked files.
This isolation ensures that edits made in one session do not affect files in another session. Claude Code also refuses to make edits from within a worktree back at the main checkout, enforcing this isolation through the worktree system.
To avoid unexpected overwrites, it is recommended to use worktrees for each session, with each worktree having its own directory, branch, and copy of the tracked files. This way, edits in one session do not impact the files in another session. Additionally, it's advised to add .claude/worktrees/ to your .gitignore to prevent worktree contents from appearing as untracked noise in your main checkout.
Messaging becomes more valuable when used to coordinate changes between sessions. For instance, if Session A makes changes to an API and commits them, it can then send the commit hash and a list of affected files to Session B. Session B can then rebase or cherry-pick the changes, re-read the affected files, and resolve any conflicts that may arise. This approach helps maintain a clear record of changes and allows for smooth coordination between sessions.
However, Claude Code does not handle ownership of files such as package.json. The system does not inherently decide which session should be responsible for making changes to specific files. To avoid conflicts, it's essential to establish a protocol where only one session can own a file at a time. If multiple sessions need to modify the same file, they should coordinate through a message before touching the file and require a commit before claiming ownership.
This ensures that only one session is actively working on a given file at any given time, minimizing the risk of overwriting each other's changes.
Alternative approaches, such as Agent Teams or MCP Agent Mail, provide additional features and coordination mechanisms, but they still do not fully prevent conflicts when two sessions attempt to modify the same file. Agent Teams offers a shared task list, mailbox, and direct messages between agents, with a file lock to protect the task record.
However, it does not prevent two writers from landing on the same file, emphasizing the need for file-level ownership and coordination. MCP Agent Mail provides persistent agent identities, an inbox and outbox with threads, file reservations, and TTL on stale reservations, but these reservations are advisory and do not enforce file-level locking.
In conclusion, isolation through worktrees is crucial for preventing unintended overwrites, while ownership and clear communication between sessions are key to maintaining data integrity. By using worktrees, establishing protocols for file ownership, and coordinating changes through messaging, you can effectively manage multiple Claude Code sessions working on the same project without risking file corruption.
Written by urgent.news from Dev.to's reporting โ not their text. Machine-written โ may contain errors; check the original before relying on it.
This story
This is one outlet's version. Read the fullest account.
- DeepSeek Harness: the open harness that does what Claude Code won't let you touch dev.to
- Metaโs Claude Code rival exits beta with three new subscription tiers โ and itโs pushing hard on price thenewstack.io
- I gave Claude Code a deny list, and stopped watching every command it tried to run xda-developers.com