Add SQLite, Postgres and MongoDB backends for `ConversationStore`, and rename the Pax session store to `SqliteSessionStore`
Merged
DO
8d ago+3148+3148+0123456789012345678901234567890123456789-505-505-012345678901234567890123456789
Part of #9786.
Why
Core's ConversationStore (#10658) ships only an in-memory store. This adds SQLite, Postgres and MongoDB backends in a new pydantic_ai_harness.conversation_store module, with their own docs page. They take the same connection arguments as the matching step stores and reuse their database code, but they store sessions, which Step Persistence isn't for, so they don't live under it. The Pax session-browser store already used the name SqliteConversationStore, so it's renamed to SqliteSessionStore.
New public surface
pydantic_ai_harness.conversation_store:SqliteConversationStore,PostgresConversationStore,MongoConversationStoreSqliteSessionStoreandSessionConflict; the oldSqliteConversationStoreandConversationConflictstill import fromstep_persistence.conversations, with aHarnessDeprecationWarning
Behavior
- Each message's JSON is stored once by SHA-256, and large parts go to the media store. A history is read in one query (a recursive CTE, or
$graphLookup). - SQLite and Postgres write a save's entries and record in one transaction. A delete removes the entries and messages no other conversation uses. Postgres foreign keys turn a save racing a delete into
ConversationConflict. - MongoDB uses no transactions, so a delete removes only the record.
- All three implement
write_details, soupdateworks. Title and metadata are stored beside the record JSON, so a save never undoes anupdate.
Verification
- One contract suite,
_conversation_store_contract.py, runs against SQLite (file and caller connection), Postgres on the SQLite stand-in, MongoDB on mongomock, and a live Postgres 17 (test_live_conversation_contract.py; 23 live conversation tests pass locally). test_an_update_on_a_caller_connection_commitsfails without the fix in this push ([(None,)] == [('Trip',)]).- Three fresh adversarial reviews found: an uncommitted SQLite update on a default-isolation connection, a metadata key-order mismatch, contract gaps, and three Postgres races between a save and a delete in another history sharing a message (a deadlock, then two spurious
ConversationConflicts). All fixed. Postgres now upserts messages in key order, so a save holds every message's row lock, in the order deletes take them. - On a live Postgres 17, the reviewers' race scripts deadlocked or conflicted before the fixes and end cleanly after, and a 10-worker run of random saves, forks and deletes over shared messages had no deadlock or unexpected error.
test_conversation_store_save_sharing_a_message_a_delete_is_releasing_keeps_itpauses a delete after its lock and fails on the old lock order. - Lock cost, measured on a live Postgres 17 (2 CPUs) with 1, 8 and 32 concurrent writers, against the same store with the message lock removed: saves that add their own messages show no difference (32 writers: 1814 vs 1925 saves/s, p99 41 vs 61 ms). When every writer adds a byte-identical message at once, throughput holds and p99 rises from 48 to 186 ms. That needs identical timestamps, since a message's key covers them. With saves and deletes sharing a message, the version without the lock failed up to 36 saves per run. Forks of one conversation cap at about 400 saves/s in both versions, from the per-history lock.
What changes for existing users
Code importing SqliteConversationStore or ConversationConflict from step_persistence.conversations keeps working, with a deprecation warning pointing at the new names.
Follow-ups
- #14900: let
PostgresConversationStoretake a caller-owned connection, so a save can join the application's transaction. Batched deletes are folded in there, to keep only if a new measurement shows a delete still grows badly with history length. - #14901 (tracked with #9786): keep entries and messages per
scope, so tenants can be isolated in the database. Core forks share entries across scopes, so this needs a decision on cross-scope forks first.
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
- PR title is fit for the release changelog.