On Postgres with Redis, deleting a chat whose background sub-agent was still working often failed without an error. The sub-agent stopped and its thread went away, but the chat stayed in the sidebar. Stopping the sub-agent makes it post its result into that same chat, and that write and the delete blocked each other until the database cancelled one of them, usually the delete. Deleting a chat now locks the chat first, like every other write to a chat, so the two run one after the other. Deleting all chats or a folder's chats could clash the same way and gets the same change.
Fixes#35034
With ENABLE_ADMIN_CHAT_ACCESS turned off, admins could still open another user's sub-agent, timer and note chats by id. Those chats hold the same conversation content as the parent chat, so they now follow the same setting as every other chat.
Since #34689 stored every message on its own, every action that loads many chats with their messages read them one chat at a time, so exporting chats, a folder's export, the admin's All Chats export and the archived chats list got several times slower, growing with the number of chats. The messages of the listed chats now come in one read per 500 chats. Unarchive All only needs to know which chats are archived and no longer loads any messages.
<table>
<tr><th>1000 chats, 10 messages each</th><th>SQLite before #34689</th><th>SQLite now</th><th>SQLite fix</th><th>PostgreSQL before #34689</th><th>PostgreSQL now</th><th>PostgreSQL fix</th></tr>
<tr><td>Export Chats</td><td>0.26 s</td><td>1.00 s</td><td>0.58 s</td><td>0.27 s</td><td>1.82 s</td><td>0.46 s</td></tr>
<tr><td>Folder export</td><td>0.21 s</td><td>1.86 s</td><td>0.51 s</td><td>0.19 s</td><td>1.09 s</td><td>0.42 s</td></tr>
<tr><td>Archived chats list</td><td>0.25 s</td><td>1.08 s</td><td>0.57 s</td><td>0.21 s</td><td>1.11 s</td><td>0.40 s</td></tr>
<tr><td>Unarchive All</td><td>0.07 s</td><td>1.13 s</td><td>0.02 s</td><td>0.13 s</td><td>0.94 s</td><td>0.05 s</td></tr>
<tr><td>Admin All Chats export</td><td>0.14 s</td><td>1.96 s</td><td>0.79 s</td><td>0.33 s</td><td>1.45 s</td><td>0.59 s</td></tr>
</table>
Fixes#35001
Every sent message, finished reply, regenerate, edit and saved follow-up rewrote the whole stored conversation, so long chats got slower with each turn, most of all on PostgreSQL. Messages now live only in the message table the app already keeps, and a change writes only the messages it touches. Chats open, share, export and import as before. API responses no longer carry the flat message list older clients saved next to the conversation, and the answers to pinning, archiving, sharing or moving a chat no longer repeat its messages. Existing chats are converted once during the upgrade, in batches that continue where they stopped if the upgrade is interrupted.
| 130 MB chat, instant test model | SQLite before | SQLite after | PostgreSQL before | PostgreSQL after |
| --- | --- | --- | --- | --- |
| Message accepted | 2.4 s | 0.20 s | 10.9 s | 0.32 s |
| Reply done | 4.9 s | 0.9 s | 22.5 s | 1.4 s |
| Regenerate done | 4.6 s | 0.9 s | 19.6 s | 1.4 s |
| Saving an edit | 3.7 s | 1.8 s | 5.8 s | 1.7 s |
| Opening the chat | 2.3 s | 0.9 s | 1.6 s | 1.2 s |
| Written to the database per message | 533 MB | 2 KB | 533 MB | 2 KB |
Fixes#34688
A window that had missed a turn, for example a laptop that just woke up and has not reconnected yet, saved its own, older idea of where the chat was when it saved an edited message. The chat then reopened on that older turn, and the next message started a new branch there, so the newest reply moved behind the branch switch and the model no longer saw it. A save now keeps the chat on the newest turn of the branch it is on, the same turn the chat already shows once it has caught up.
Fixes#34126
Every time a chat is opened, the server marks it as read, and to do so it loaded and parsed the full chat, images included, just to save the last-read time. Marking a chat as unread and removing a tag from a chat did the same. These now touch only the few chat fields they change, which makes them 4 to 6 times faster on large chats.
Measured on SQLite, best of 7 runs, sizes are the chat as stored in the database:
| Chat | Action | Before | After |
| --- | --- | --- | --- |
| 7 MB, text only | open (mark read) | 19.8 ms | 3.5 ms |
| 7 MB, text only | mark as unread | 20.7 ms | 3.5 ms |
| 7 MB, text only | remove tag | 21.2 ms | 3.4 ms |
| 49 MB, with images | open (mark read) | 122.2 ms | 30.0 ms |
| 49 MB, with images | mark as unread | 124.2 ms | 29.1 ms |
| 49 MB, with images | remove tag | 120.5 ms | 28.6 ms |
Typing indicators, sending a message, shared links, comments and file access all check whether the user may access the chat, and each check loaded the full chat, images included, only to look at who owns it and which folder it is in. On large chats that delayed every other request on the same server process by up to a few hundred milliseconds per check. The check now reads just those few fields, and who gets access is unchanged.
Measured on SQLite, best of 5 runs, sizes are the chat as stored in the database:
| Chat | Checked for | Before | After |
| --- | --- | --- | --- |
| 7 MB, text only | the chat owner | 34.6 ms | 0.36 ms |
| 7 MB, text only | a user with access through a shared folder | 35.1 ms | 2.58 ms |
| 57 MB, with images | the chat owner | 249.8 ms | 0.72 ms |
| 57 MB, with images | a user with access through a shared folder | 249.0 ms | 2.80 ms |
When the owner of a shared folder moved a chat a member created to another folder inside the shared folder, the chat lost the member's pin and dropped out of their Pinned list. The owner's move now keeps the member's pin, just as it already keeps the chat's unread status for them. Moving your own chat into a folder still unpins it, as before, so it shows up in that folder.
Fixes#33341
Filing an old chat into a folder made it look recently active: it jumped to the top of the sidebar under Today, and a batch of old chats sorted into folders ended up ordered by when each was moved. Moving a chat into or out of a folder now keeps its last activity date, so it is sorted and grouped by when it was really last active. Moving your own chat still clears its unread dot as before, and when the owner of a shared folder reorganizes your chat within it, the chat no longer turns unread for you.
Closes#30362
* perf: renaming a folder or deleting all chats takes seconds when many users are online
Renaming, sharing, moving or deleting a folder, and deleting all chats, each made the server recheck access for every chat that any online user had open in a browser tab, one database lookup at a time, before the request finished, and every one of those open chats reloaded its contents afterwards. Any user can do these, so with a few thousand people online every such click took several seconds and reloaded everyone's open chat. Owners keep access to their own chats (a role change or account deletion already disconnects them), so the recheck now skips owners entirely, with no lookup and no reload, and only checks people viewing someone else's chat and chats that no longer exist. Anyone who lost access is still removed from the chat as before.
Postgres 16, one open chat per online user, 2% of them viewing someone else's chat, time for a folder rename (the other actions above run the same recheck):
| Online users | No Redis, before | No Redis, after | Redis, before | Redis, after |
| --- | --- | --- | --- | --- |
| 1,000 | 2.2 s | 0.12 s | 2.5 s | 0.23 s |
| 3,000 | 6.9 s | 0.60 s | 7.7 s | 0.78 s |
* refac: collect the open chats with a plain loop
Same behaviour, the owner lookup now reads more plainly.
* refac: shorten the owner comment in the chat access sweep
When the model asked the user a question, or a tool call was approved or rejected, the reply was saved in a way that broke adding anything to it later in the same turn. If the model then generated an image, the image was created but never shown, the tool returned "unsupported operand type(s) for +: 'NoneType' and 'list'" and the model kept retrying, generating more hidden images. Progress status updates for that reply were also silently dropped. Both now work after a question or tool approval.
Fixes#32678