ADR-0024: Conversations stored outside OpenAI: the ConversationStore port
- Status: Accepted on 2026-11-25 (task z1), as built by tasks 1, 2, a2, 3a, e1 and 4. Decisions 6 and 10 are amended below ("As built").
- Date: 2026-11-23
- Extends: ADR-0003. The OpenAI Conversations API stays the default, with no database. ADR-0003's rule "always send
store: true" and its consequences 1 to 3 hold only for that default (decision 4 below). - Extended by: ADR-0037: attachments in the app's own file store, sent inline on replay and deleted through decision 6's path.
- Evidence: the tasks that built it (below); spike S7 (findings); replay needs from S6a (findings, Q3) and S6b (findings, section 2).
Context
ADR-0003 named a ConversationStore port but the code never grew one: creating, listing, deleting and settling conversations are methods of ModelGateway (src/gateway.ts), and every turn runs inside an OpenAI conversation. That blocks four things on the roadmap:
- Data residency and zero data retention. Conversations are not ZDR-eligible, so a ZDR organisation or an EU-only setup can't use askdare today.
- Retention and admin. OpenAI has no endpoint that lists conversations, so nothing can sweep old ones, and admin reviews (ADR-0016) end when OpenAI's copy is deleted.
- Other providers. Claude (S6a) and Open Responses endpoints (S6b) keep nothing between requests; they need the whole history replayed from somewhere.
- Long history.
listMessagesreads the latest 100 items and stops.
Spike S7 built the port with three implementations (memory, Postgres, OpenAI Conversations) and one shared contract suite, 31 of 31 tests green, and ran the same 20 turns both ways. Replaying from Postgres with store: false matched conversation mode on cost (0.0135 vs 0.0138 USD) and recall, kept 96.9 % of input cached, and finished turns about 1 s faster.
Decision
- A public
ConversationStoreport (S7'sport.ts, names final in the implementing task):create(owner),info(id),context(id, { maxItems? }),append(id, items, { expect? }),page(id, { before?, limit? }),delete(id), and the optionalsweep(idleSince, limit)anddiagnose().owneris{ botId, owner }, whereownerisvisitor:<id>oruser:<hmac>(ADR-0009).- Items are provider-neutral: messages with parts (text with citations, page context, file references, refusals), function calls, function call outputs, and
nativeitems for opaque provider state (encrypted reasoning, later thinking blocks or compaction items), tagged with provider and model family. context()returns one of two kinds. Provider stores return a reference: the provider records the model's items itself. Item stores return the items to replay. Both carry a version.- The port, its item types and the error a stale
expectraises contain noopenaitypes. The mapping between stored items and Responses items lives insrc/openai/(ADR-0002). - Names as implemented (task 1):
ConversationStore,ConversationOwner,ConversationInfo,StoredItem,StoredPart,StoredCitation(withsourceId, ADR-0025),PositionedItem,ConversationContext(the turn context),ConversationPage,ConversationStoreCheck(whatdiagnose()returns) andVersionConflictError, from@boldare/askdare;openaiConversationsfrom@boldare/askdare/next, next tocreateChatHandler, since it needsopenai. A store's conversation IDs are letters, digits,_and-, at most 128 characters, so they fit the resume token's check.
- Implementations:
openaiConversations(): the default, a provider store. Today's behaviour, unchanged.memoryConversations(): an item store for tests and local development. In production it warns that it is per instance, as the in-memory rate-limit store does.postgresConversations({ … })in a new add-on,@boldare/askdare-postgres, withpgas its only dependency. The same package holds the pgvector index of ADR-0025, as a separate export, so oneDATABASE_URLserves both. This replaces the package name@boldare/askdare-store-postgresfrom ADR-0003.- The store is chosen in
createChatHandler({ conversations }), on the server only. The client never chooses it (ADR-0004).
- Rules for item stores:
- Turns send
store: falseand replay the stored items. Nothing of the conversation stays at OpenAI beyond its 30-day abuse-monitoring retention of requests, so ZDR organisations work. - Append-only and deterministic: a stored item is never rewritten, tool outputs included, and the same item always maps to the same request JSON. That keeps the prompt cache and the replayed reasoning valid.
- Write order: read the context; append the visitor's message with
expectbefore the first model call; after each step, append its output items and the outputs of its calls in one atomic append; append thesuggest_repliesoutputs with the last step; guardrail notes (ADR-0022) are appended like any other item. A crash loses at most the step in flight, and a call is never stored without its output. - Concurrent turns on one conversation: the loser of an
expectconflict writes nothing and ends with the existingrate_limitedcode, as ADR-0022's hold does. - Reasoning is replayed only to the model family that wrote it.
- No
truncation. Item stores replay the whole conversation, whichlimits.maxTurnsalready bounds; a request that still overflows ends witherror.conversationTooLong, as today.context({ maxItems })cuts its window forward to a visitor message so it never starts mid-turn; it is what compaction (spike S9) or a later option builds on.
- Turns send
- ADR-0003 under an item store: its "always send
store: true" rule applies toopenaiConversations()only. Consequence 1 (no retention) and 2 (data stays at OpenAI) don't apply. Consequence 3 changes as decision 3 says. - Ownership becomes a real check. The resume token is unchanged (a JWE whose
cidis now the store's ID), and the server comparesinfo(id)'s bot and owner with the token's claims. On a mismatch it answers exactly as for an unknown conversation. Adopting an anonymous conversation at sign-in (reassign) is not in 0.6, so an adopted conversation keeps its visitor as owner; a signed-in user's token, which follows them through new visitor IDs, therefore matches its ownuser:owner or anyvisitor:owner, never another user's. Conversations a signed-in user started before 0.6 are stored under their visitor too. - Deletion and retention:
delete(id)returns the file IDs its visitor messages reference, and the server deletes those files through the gateway, as today.sweepdeletes conversations idle since a time and returns their IDs and file IDs. The library can't schedule work, so the add-on exports it for a cron route or job; the docs show both. A sweep goes through the same deletion path asDELETE /conversation, so the admin store's transcripts and ratings go too (ADR-0016).openaiConversations()has nosweep; ADR-0003's consequence 1 stands for it.
- History beyond 100 items:
POST /historytakes an opaquebeforecursor and answers one page of whole turns, newest page first, oldest first within it, plus the next cursor when there is more. Both stores page: OpenAI withitems.listcursors, item stores withpage(). An additive change within protocol v1. - Citations are stored on the text part in item stores (title, URL and source ID), so reloaded history needs neither
file_search_call.resultsnor a search. - Schema: the Postgres add-on keeps its schema in code and creates or upgrades it only through an explicit
migrate(an exported function and a bin). It never migrates at request time.doctorreports a missing or older schema. - Contract suites ship in a new subpath,
@boldare/askdare/testing: the conversation store suite, the rate-limit store suite (today only reachable by relative import) and the knowledge index suite (ADR-0025). They take the test runner'sdescribe,itandexpect, so the subpath depends on no runner. The suites delete everything they create, also after a failed test (S7 leaked one empty conversation without that). - What stays separate:
AdminStore(ADR-0016): reports, ratings and optional transcripts. It may share a database with the conversation store, never an interface. With an own store,onTranscriptand admin reviews can read from it, which removes ADR-0016's caveat that reviews end when OpenAI's copy is deleted.- Stream resume has its own port (ADR-0027): a different lifetime and write rate, and the OpenAI default needs it too.
ModelGatewayshrinks.createConversation,listMessages,deleteConversationandsettleAnswermove onto the store;runTurntakes the turn context.ModelGatewaystays internal in 0.6 (ADR-0026).- Public surface (added to ADR-0006 when this is accepted): the
ConversationStoreport and its item and context types,openaiConversations,memoryConversations, theconversationsoption ofcreateChatHandler, the@boldare/askdare/testingsubpath with its three suites,@boldare/askdare-postgres'spostgresConversationsandmigrate, and thebeforecursor ofPOST /history.
As built in 0.6
- Names are those of decision 1.
ConversationStore.sweepalso takes an optionalsignal, as the other methods do (z1). The OpenAI store's IDs areconv_…, the Postgres store's lowercase UUIDs, the memory store's UUIDs. An answer's ID in an item store isitm_…, so the feedback token takesmsg_…anditm_…. - Stateless turns (task 2):
turn.tsreadscontext()first and branches on itskind. A provider context keeps 0.5's request (conversation,store: true,truncation: 'auto', the repair of dangling calls). An items context sendsstore: falsewith the replayed history, appends the visitor's message withexpectbefore the first model call and each step's output with its call outputs in one append. Of the model's other output, only its reasoning (replayed only to the model family that wrote it), its file searches and compaction items (ADR-0029) are stored, asnativeitems; any other type is dropped, so nothing askdare didn't request comes back as input. A lostexpectrace ends withrate_limitedand a retry after 5 s. - A store that fails after
info()succeeded (task 2): core asksinfo(id)again;undefinedanswers as for a deleted conversation (not_found,error.conversationGone), anything else keeps the original error. The port has no not-found error type: a store only has to makeinfo()answerundefined. - Decision 6, amended: a sweep is the app's call.
sweepreturns the conversations it deleted and their file IDs; the app's cron route or job deletes the files and its own transcripts. Core has no public helper that deletes through the gateway, and the admin store doesn't exist yet (0.7), so "the same deletion path asDELETE /conversation" holds only for the store's rows. The own-your-data guide and the Postgres add-on's README show the route. A visitor'sDELETE /conversationcalls no hook either: transcripts an app keeps (onTranscript) outlive it until the app's retention removes them. Deleting them with the conversation is the 0.7 admin store's job (ADR-0016, as decided in z1). - Decision 7 (task 3a): the
beforecursor is a JWE sealed to the bot and the conversation (typ: askdare-history+jwt), valid as long as the resume token; a cursor of another conversation, even the same visitor's, answers 400invalid_inputwitherror.history. A page is at most eight store reads; a turn of more than about 800 items is shown split. - Decision 9:
postgresConversations().diagnose()reports the schema status and, for an unreachable database, the error code only.openaiConversations().diagnose()checks that the key can create and delete a conversation, sodoctormakes a test conversation at OpenAI only for apps on that store (z1; before, every bot made one, whatever its store). - Decision 10, amended:
@boldare/askdare/testingholds four suites, the turn log's of ADR-0027 included, all called as(name, create, { describe, it, expect, …options }), and the fixturesfakeEmbedderandknowledgeDocument. - Decision 12:
ModelGatewaylost the four methods and gaineddeleteFiles; it stays internal. Its OpenAI-named fields (safetyIdentifier,vectorStoreId,answerItemId) are not renamed yet: they are, before the port is made public (ADR-0026, decision 5). - Decision 13: the surface is listed in ADR-0006.
Evidence
- Contract suite:
conversationStoreContract, with its paging and compaction cases, passes formemoryConversations()and, against Postgres 17 with pgvector 0.8.7 in CI, forpostgresConversations()(a2). The provider variant foropenaiConversations()runs live withASKDARE_CONTRACT_OPENAI=1 pnpm smoke, which no 0.6 task ran. - Turns: unit tests run the turn loop in both modes (task 2), and
e2e/postgres.spec.ts(e1) runs a stateless conversation with a tool call against Postgres in the browser, reloads it and deletes it. Paging is checked on both stores by reading every page and comparing with one read of the whole conversation (3a). The live smoke test of a stateless conversation (gateway.smoke.ts) was written in task 2 and run only in its compaction variant (task 4, ongpt-6.1-terra). - The guide: d1 followed Own your data on a fresh
create-next-appwith the packed packages, a pgvector container and the mock OpenAI server: migrate, a cited answer from Postgres, a reload,doctor, transcripts and the cron sweep.
Consequences
- A site can keep every conversation in its own Postgres, with retention, and run with ZDR or in the EU. Sites that set nothing keep today's setup with no database.
- Item stores send larger requests: the whole history, 32 KB at turn 20 in S7, of which 22 KB was encrypted reasoning. They also add about 40 ms of store reads and writes per turn (local Docker in S7). The cost is the same, since the replay is almost entirely cached.
- An own store is one more service to run and back up, and the site owns its retention and its
migratestep. The docs cover Neon, Supabase and Heroku Postgres. - The turn loop gets one code path with two modes. Recorded-stream tests have to run in both.
- Claude needs its assistant content replayed exactly (thinking blocks with signatures, text with citations). The
nativeitem can carry it; how exactly is decided with the native Claude adapter, not in 0.6. - With the default store, OpenAI still serialises turns (
conversation_locked), andMAX_REPAIRSstays for calls OpenAI writes before we answer them.
Alternatives considered
- OpenAI Conversations as an item store (list the items each turn, replay statelessly): listing 62 items took 567 ms on top of a 2.6 s first token, and the API appends at most 20 items per request, so appends can't be atomic (S7).
- Store each provider's native transcript (Responses items, Claude message arrays): history display, ownership and paging would need a mapper per provider. Neutral items with an opaque
nativeescape hatch keep the store provider-free. - One interface for conversations and admin data: different lifetimes, writers and readers. Two ports that can share one database.
- Postgres in core instead of a port: every site would need a database, against ADR-0003's "an API key and a secret are enough".
- Migrating the schema on first use: a cold start on serverless would race other instances and need DDL rights for the app's database user.
- Two Postgres packages (store and index): two installs, two migrations and two connection pools for what S8 found to be one database.