Skip to content

Store

Storage contract for a conversation shared by independent workers.

StoredConversation dataclass

The snapshot held under a lock. A new conversation starts with state=None.

Source code in src/openai_agents/codex/store.py
@dataclass
class StoredConversation:
    """The snapshot held under a lock. A new conversation starts with state=None."""

    state: ConversationState | None

ConversationStore

Bases: Protocol

Source code in src/openai_agents/codex/store.py
class ConversationStore(Protocol):
    def lock(self, conversation_id: str) -> AbstractAsyncContextManager[StoredConversation]:
        """Acquire exclusive access to the ID before reading its last committed state.

        Acquire even for an ID that has no snapshot yet. Yield a private copy.
        On normal exit, commit the new state if set, then release the lock.
        On exceptional exit, discard changes and release the lock. A worker
        that loses the lock must be unable to commit its snapshot.
        """
        ...

lock

lock(
    conversation_id: str,
) -> AbstractAsyncContextManager[StoredConversation]

Acquire exclusive access to the ID before reading its last committed state.

Acquire even for an ID that has no snapshot yet. Yield a private copy. On normal exit, commit the new state if set, then release the lock. On exceptional exit, discard changes and release the lock. A worker that loses the lock must be unable to commit its snapshot.

Source code in src/openai_agents/codex/store.py
def lock(self, conversation_id: str) -> AbstractAsyncContextManager[StoredConversation]:
    """Acquire exclusive access to the ID before reading its last committed state.

    Acquire even for an ID that has no snapshot yet. Yield a private copy.
    On normal exit, commit the new state if set, then release the lock.
    On exceptional exit, discard changes and release the lock. A worker
    that loses the lock must be unable to commit its snapshot.
    """
    ...

InMemorySessionStorage

Share snapshots by ID for the lifetime of this object.

Source code in src/openai_agents/codex/store.py
class InMemorySessionStorage:
    """Share snapshots by ID for the lifetime of this object."""

    def __init__(self) -> None:
        self._states: dict[str, ConversationState] = {}
        self._active: set[str] = set()
        self._guard = Lock()

    @asynccontextmanager
    async def lock(self, conversation_id: str) -> AsyncIterator[StoredConversation]:
        _validate_id(conversation_id)
        with self._guard:
            if conversation_id in self._active:
                raise _busy(conversation_id)
            self._active.add(conversation_id)
        try:
            stored = StoredConversation(deepcopy(self._states.get(conversation_id)))
            yield stored
            if stored.state is not None:
                self._states[conversation_id] = deepcopy(stored.state)
        finally:
            with self._guard:
                self._active.remove(conversation_id)