TurnLockProtocol
Defined in the protocols module.AI Agent Contract for serialising a session’s turns — cluster-wide or in-process. The gateway holds this lock for the whole agent turn keyed on the resolved session id, so only one turn ever runs against one session’s transcript at a time. With the default in-process backend (:class:
LocalTurnLock) this reproduces today’s asyncio.Lock behaviour
exactly and adds no dependency. With a distributed backend (a
RedisTurnLock in the wrapper/bot package, reusing the scheduler’s proven
owner+TTL lease pattern) the same async with seam serialises turns
across every replica.
Contract:
- :meth:
acquireblocks until the lease forkeyis held, then returns a :class:TurnLeaseToken.ttlbounds how long the lease survives without renewal so a crashed holder self-heals. - :meth:
releaseis identity-checked against the token’sownerand idempotent: releasing an already-expired/reclaimed lease is a no-op, never an error and never another owner’s lease. - :meth:
holdis the ergonomic async context manager wrapping acquire/release, used at theasync with self._turn_lock.hold(...)call site.
Methods
acquire()
Block until the lease for
key is held; return its token.release()
Release
token’s lease (identity-checked, idempotent).hold()
Return an async context manager holding the lease for the block.
Source
View on GitHub
praisonaiagents/gateway/protocols.py at line 5393
