A chat with an assistant is usually a private conversation between one person and one model. Everything about it assumes that: one person writes, one model answers, one account pays, and whatever the model reads, the reader was allowed to read.
Shared rooms in Summa break every one of those assumptions at once. Several people and the model share one timeline. Anyone with the right role can ask the model something, and everyone in the room reads the answer. Some members may belong to another organisation entirely.
The hard parts here turned out not to be inference. They were ordering, queueing, access to files and attribution of cost. This essay describes how we handled each one, and what each choice costs.
What goes wrong when you just add a model
Take a room with three people. One of them asks the model to summarise last quarter’s figures. The answer takes half a minute to write. While it streams, a colleague reads the first paragraph and types “that revenue number looks wrong”.
If you order messages by the time they were stored, the colleague’s comment is stored first, because the model is still writing. When the answer finishes and is saved, it takes the next position. On reload, the comment sits directly under the question, above the answer it is criticising. The conversation now reads as nonsense.
That is the first of several failures that a personal chat never has to think about:
- Ordering. Two people can press send in the same millisecond, and a model’s answer is written over seconds, not at one instant.
- Context. If people keep typing while the model works, which version of the room is it answering?
- Cost. A model that responds to every message in a busy room spends tokens nobody meant to spend.
- Concurrency. Two questions asked at once produce two answers interleaved into one timeline.
- Privacy. A file one member uploaded privately must not leak into an answer everyone reads.
- Billing. When a guest from another organisation asks a question, someone has to pay for it, and it should be the right someone.
One counter, reserved early
Timestamps cannot order a room. A tie is broken differently by different readers, and a reload can show a third order. So each room has a counter on its conversation row, and every message and event takes its position from it:
UPDATE conversations
SET next_sequence = next_sequence + 1
WHERE id = :conversation_id
RETURNING next_sequence - 1
The row lock serialises concurrent senders into a single order until the transaction commits. Sends into different rooms never contend.
The fix for the misplaced comment is about when the model’s answer takes its number. It is reserved when the turn is accepted, not when the answer finishes writing. The reservation is stored on the run as AgentRun.answer_sequence:
if superseded_message_id is not None:
existing = await db.scalar(select(ChatMessage.sequence).where(ChatMessage.id == superseded_message_id))
if existing is not None:
return int(existing)
return await allocate_sequence(db, conversation_id)
The first branch handles regeneration. A re-asked answer reuses the position of the one it replaces, which is deleted in the same transaction. Allocating a fresh number would move the new answer to the bottom of everyone else’s transcript.
Answering a question that holds still
Reserving the position solves where the answer goes. It does not solve what the answer is about. If a colleague adds a message while the model is working, and the model reads the room again on its next step, the reader gets a reply to a question that has silently moved.
So each run records AgentRun.context_sequence: the position of the message that asked. The history query that builds the model’s context stops there. Later messages stay visible to people, but they do not change an answer already being written.
The same query has a floor as well as a ceiling. A member whose access to the room’s history starts at the point they joined must not have earlier messages fed to the model on their behalf. As the test puts it, “a run must not feed the model what the person who asked cannot see.”
Talking to people is the default
Most messages in a room are for colleagues. Each message therefore carries a mode, team or ask_model. In a personal chat the distinction does not exist and every message goes to the model. In a room, the composer defaults to team. The comment in the code gives the reason: “a mode that silently costs tokens is the wrong thing to land on.”
Mentioning the assistant in a draft switches it to ask_model, visibly, before the message is sent. The server checks that the sender’s role allows asking the model, and it never silently upgrades a team message. A viewer can read the room but cannot start a model run.
One run at a time, in a queue people can see
When three people ask the model something within a few seconds, the answers have to come one after another. An assistant interleaving three conversations in one timeline is unreadable.
The queue lives in Postgres rather than in a counter in a cache. That choice follows from who needs to use it: a facilitator has to be able to see what is waiting, cancel somebody else’s request and reorder the rest. A counter cannot be inspected or rewritten. Rows can.
Dispatch is triggered from three places: a new request joining the queue, a run finishing, and a periodic sweep. Any number of server processes may try at once. Exclusivity comes from a per-room advisory lock, held for the whole decision:
_ROOM_DISPATCH_LOCK_SQL = text("SELECT pg_advisory_xact_lock(hashtextextended(:key, 0))")
Inside that transaction the dispatcher checks that nothing is running, picks the head of the queue, takes the sender’s token budget and concurrency slot, and marks the run as running. A second dispatcher waits for that commit and then sees the room busy, which is the correct outcome. An earlier version that relied on row locks alone could let a second dispatcher start the next queued run while the first was still admitting the head.
Admission is split in two. An over-limit user is still rejected the moment they send, so they find out immediately. But the token hold and the concurrency slot are only taken at dispatch. A slot held by a request waiting behind three others would otherwise block that same person’s private branches, which are meant to run alongside the room.
A shared answer can only use shared information
The rule the whole feature turns on is short:
A shared response may only use information that every recipient of that response is authorised to see.
The tempting implementation is to let the model read everything and hide citations to private files. That does not work. A summary of a document you cannot open still tells you what the document says.
So the rule is enforced by keeping files out of the model’s context. Every attachment has a visibility, owner or room. When a run answers in a shared room, the set of files it may read comes from shared_run_attachment_ids(), which selects visibility == "room" and nothing else, however the model asks. There is no per-user escape hatch in that query, and a test fails if anyone widens it.
A member’s private file remains usable in their own private branch, where they are the only reader.
Private branches publish only the result
A branch is a separate conversation that one member opens off the room, to work something out with the model before showing anyone. Its history includes the room up to the point where it was taken.
Publishing copies one selected answer back into the room, with a new position. The prompts are never copied. Citations still resolve, because new citation rows point at the existing sources rather than duplicating them.
If the branch’s answer was built on a file the room cannot see, publishing is refused with a 409 unless the publisher sets acknowledge_private_sources. The default is to refuse, so an omitted flag cannot leak a file. When the publisher does acknowledge it, the message records used_private_sources, so a reader months later still knows the answer drew on something they were not shown.
The sender pays
Sharing starts from an email address, so a guest may come from another tenant. The room does not move. It stays in the tenant that owns it, and that is where every row about it lives.
That created a problem with one column. AgentRun.tenant_id has to be the room’s tenant, because that is what row-level security checks. It cannot also mean “whose budget this came out of”. So runs carry a second column, billing_tenant_id, for the sender’s tenant. Admission, settlement, release of held budget and the usage ledger all read it. A guest spends their own allowance, and the host’s monthly quota is not consumed by visitors.
Attribution to the person is a separate question, answered by usage_events.requested_by_user_id. That table has a unique constraint on the run, so a run is counted once no matter how many people read the answer.
Not a CRDT
It is easy to reach for collaborative-editing machinery here. We did not. A chat is an append-only log that needs authoritative sequencing, and nothing more. A CRDT solves concurrent edits to shared state, which an append-only log does not have.
Postgres is the record of truth. Redis carries events to open connections so that people see a new message in milliseconds, but it is only a latency layer. A reader replays from Postgres after the last sequence it holds, then follows the Redis stream, and goes back to Postgres whenever the sequence it receives is not the one it expected, or when nothing arrives for a while. A server dying between commit and publish, a Redis restart or a trimmed stream all degrade to “one poll later”. That is why there is no outbox worker.
What it costs
None of this is free.
- The sequence has holes. A reserved position whose run is cancelled or expires is never used. Readers treat the unexpected number as a reason to re-read Postgres, so a hole costs one poll.
- Waiting looks like dying. A queued run has no heartbeat, because it has not started. The sweep that retires abandoned runs had to be limited to running ones, and queued runs got their own, much longer deadline.
- Questions wait. One run per room means a busy room queues. We think a readable timeline is worth that, and the queue is visible so nobody wonders.
- Refusals add friction. Publishing from a branch can stop and ask. That is deliberate, but it is a step people have to take.
- Usage reports split. A guest’s spending appears in their own usage, not in the host organisation’s report.
What we would generalise
For anyone putting a model into a space several people share:
- Reserve the answer’s position when the request is accepted. Generation takes time, and the world keeps moving.
- Pin the context a run answers, so later messages do not change an answer mid-flight.
- Make the costly mode opt-in where most traffic is between people.
- Put a queue where people can see and change it, and guard dispatch with a lock that covers the whole decision.
- Enforce visibility at the point of retrieval. Hiding a citation does not un-read a document.
- Separate the tenant that owns the data from the tenant that pays for the tokens.
- Keep one durable authority, and let the fast path be allowed to fail.