CSE Roadmap

Common tier · about 4 hours · 12 parts

Part 3 of 12 · 2 min read

Optimistic concurrency catches every conflict, but a retry re-runs the agent's reasoning

Checking a version number before you write is cheap for a database and expensive for an agent, because the agent's retry means paying for the model call again.

Think of a shared document that refuses to save your edit if someone else has changed it since you opened it. You don't lose your change, but you do have to reopen the document, redo your edit, and try saving again.

In plain words: optimistic concurrency means proceeding without locking and, at write time, checking that nothing changed via a version number. If it did change, the work is redone. This uses compare-and-set, which writes only if the current version matches what the writer read. It guarantees no update is ever silently lost. But for an agent, "redo the work" means re-running the model call that produced the update, so every conflict has a real token cost.

A worked example. With optimistic concurrency, the same six-agent, five-round test loses zero items and the token counter is exactly correct. That is a real fix. But it needs 83 retries to get there, which raises the total model calls from 30 (one per agent per round) to 113. At twelve agents, that effect compounds: model calls rise to 387, well beyond the 60 calls a conflict-free run would need. Contention, and therefore retries, grows faster than the number of agents.

The key lines. From code/mini-statebus/shared.py:

# from code/mini-statebus/shared.py
def cas(self, k, x, expect):
    if self.ver[k] != expect:
        return False
    self.write(k, x)
    return True

Read it as: cas compares self.ver[k], the field's current version, against expect, the version the caller read earlier, and refuses the write entirely if they don't match. The check costs nothing extra in tokens. But the caller's response to a refusal, re-running the whole reasoning step and trying again, is where the real cost of a conflict actually lands.

Optimistic concurrency, compare-and-set (CAS)

Optimistic concurrency means proceeding without locking, and at write time checking that nothing changed (a version number); if it did, redo the work. Compare-and-set (CAS) writes only if the current value or version matches what the writer read.

Example: 83 retries turning into 83 extra model calls is optimistic concurrency's entire trade-off stated as a number. It buys perfect correctness, and the price is denominated in exactly the currency that matters for agents: calls, not milliseconds.