Multi-agent shared state: prevent stale writes, not just lost memory
A concrete two-agent conflict example, revision-based write protocol, transition table, and acceptance checks for shared agent state.
Updated
Sources are linked in the guide. Read our editorial policy.
On this page 7 sections
A shared database gives agents somewhere to read and write. It does not decide whose information is authoritative, whether a proposal is stale, or whether an external action already happened. Those are application contracts.
A question about connecting agents to the same data appeared in Agent Brief’s November 4, 2026 roundup. Reddit blocked direct access during research, so this guide does not attribute advice to its commenters. The practical problem stands independently: two workers can read the same valid state and later produce incompatible updates.
The following is an illustrative single-authority database design, not a distributed-consensus algorithm or a guarantee of exactly-once external execution.
Separate authority, evidence, and execution history
Use three logical record types, even if one database holds them:
| Record | Purpose | Suggested contents |
|---|---|---|
| Authoritative task | Current accepted requirements and workflow status | Task ID, revision, requirements, status, owner |
| Evidence | Traceable observations and artifacts | Evidence ID, source revision, actor, artifact reference, observation |
| Operation | Deduplication and action outcome | Stable operation ID, argument identity, status, external reference |
Only the authoritative task determines the current requirements. An agent’s conversation is a working copy, not another authority. Evidence should be append-oriented: a failed test at revision 7 remains a real observation, but it does not establish that revision 9 fails.
This distinction also appears at the runtime level. LangGraph’s persistence documentation separates thread-scoped checkpoints from stores containing cross-thread application data. A recovered conversation checkpoint is useful for continuity; it is not, by itself, proof that its embedded requirements still match the current business record. Reconcile them when resuming work.
Two agents, one stale write
Suppose revision 12 says: “Export a CSV containing name and email.” Agent A starts implementing it. Agent B receives an authorized requirement change: remove email from the export.
| Step | Authoritative state | Worker behavior |
|---|---|---|
| Both read | Revision 12 includes email | Both hold revision 12 locally |
| B commits change | Revision 13 excludes email | B writes against expected revision 12 |
| A finishes old work | Still revision 13 | A proposes completion based on revision 12 |
| Authority rejects A | Still revision 13 | A’s expected revision no longer matches |
| A rereads | Revision 13 excludes email | A revises implementation and evidence |
| A commits valid result | Revision 14 records completion | Completion is checked against revision 13 |
Without the revision condition, A could overwrite the new requirement or mark obsolete work complete. “Always reread before writing” is insufficient: another update can land between that read and write. The check must occur atomically with the mutation.
Put optimistic concurrency in the write boundary
For an illustrative PostgreSQL table with a unique task_id, an integer revision, and a status column, this parameterized statement shows the essential condition:
UPDATE agent_tasks
SET status = $1, revision = revision + 1
WHERE task_id = $2 AND revision = $3
RETURNING task_id, revision, status;
This is a SQL pattern, not a complete application or schema. The service must validate actor permissions, allowed transitions, and evidence before issuing it. Every authoritative mutation must participate in the revision protocol, including edits made by humans or administrative jobs.
PostgreSQL documents that RETURNING reports updated rows and that an update affecting zero rows is not an SQL error. Treat no returned row as a failed precondition or missing record, not success. Its Read Committed rules explain how an update rechecks its condition after a concurrent updater commits.
On conflict, discard the proposed state mutation, reread authoritative state, and reconsider the work against that revision. Do not merely change $3 to the latest revision and replay the old result. That defeats the protection. Bound retries and route unresolved semantic conflicts to an owner rather than letting agents repeatedly overwrite one another.
Keep database transactions short; do not hold a row lock through a model call. If a business invariant spans several records, a revision on one row may be insufficient. Define the transaction boundary and use appropriate database constraints, locks, or isolation. PostgreSQL’s serializable transactions can require retrying the entire transaction; a prompt cannot substitute for that handling.
Make transitions explicit
This illustrative contract is small enough to review:
| Requested transition | Preconditions | Failure behavior |
|---|---|---|
| Ready → working | Current revision; worker authorized | Reject stale claim |
| Working → needs review | Current revision; artifacts and evidence reference that revision | Require refreshed evidence |
| Needs review → complete | Current revision; authorized acceptance | Reject obsolete approval |
| Any active state → requirements changed | Authorized editor; current revision | Increment revision and invalidate affected pending acceptance |
For consequential tool execution, pair this with human approval bound to the actual tool call. Completion and authorization are different decisions.
Deduplicate operations separately from evidence
A state conflict and an external side effect are different problems. If an agent sends an email and crashes before recording success, restarting its checkpoint may send another email even though all database revisions are correct.
Assign a stable operation ID to the logical action, not a fresh ID to every retry. Enforce uniqueness at the authoritative operation boundary and reject the same ID with different arguments. Use a downstream idempotency mechanism when available. If the downstream system cannot deduplicate, an uncertain result needs reconciliation; recording “pending” locally cannot guarantee exactly-once delivery.
Store evidence separately so a stale proposal can be rejected without erasing useful observations. Protect those records with access controls too: append-only does not mean every observation is trusted or safe to execute.
Acceptance checks for the coordination contract
Before relying on this design, require these observable outcomes:
- Two updates using the same revision cannot both advance the same authoritative record.
- A rejected worker rereads and changes its proposal when requirements changed.
- Resuming an old checkpoint does not restore old requirements over newer ones.
- Repeating one operation ID cannot create a second accepted logical operation.
- Evidence from an older revision cannot silently satisfy current acceptance.
- A crash after an external request produces an explicit reconciliation path, not assumed failure and blind replay.
These are checks to implement in your system, not tests run for this article. For defining the result those checks protect, use the coding-agent acceptance guide.
For parallel coding tasks, Worktrunk manages separate Git working directories. That helps prevent file-edit collisions, not shared-database races: give each task appropriate test data and service configuration, and review the combined changes before accepting them.
Sources and discussion
- PostgreSQL UPDATE and transaction isolation: conditional writes, concurrent updates, and transaction retries.
- LangGraph persistence: checkpoints versus cross-thread stores.
- November 4 Agent Brief: discovery lead for the inaccessible Reddit question, not evidence of community consensus.