-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
Describe the bug
Simply viewing old threads without changing anything leads to bumped up "updatedAt" at a thread level in DB (with official chainlit data layer). While not noticeable initially due to session-level state caching (DB is only written not read), if you refresh the page, the recently viewed threads are shown on top.
On viewing chats, the following calls sequence happens, coming from the socket:
File "chainlit/socket.py", line 223, in disconnect
await persist_user_session(session.thread_id, [session.to](http://session.to/)_persistable())
File "chainlit/socket.py", line 48, in persist_user_session
await data_layer.update_thread(thread_id=thread_id, metadata=metadata)
File "chainlit/data/chainlit_data_layer.py": in update_thread
To Reproduce
-
Start up a chainlit app with persistence, create several threads and give them different names
-
Notice threads order
-
Refresh the page.
-
Click on a couple threads at the bottom
-
Refresh the page
-
Notice these viewed threads are now showing at the top
Expected behavior
Only significant changes should bump up a thread in history, such as:
- Adding messages
- Editing or removing messages
Other operations such as viewing the thread, minor session variables changes, or even renaming the thread, must not be bumping it up in history.
Recommended solution
- Introduce a new column "contextUpdatedAt" (will need a prisma migration). Selectively pass it over from places where update_thread() is called where there is a chat context changes occur (mainly from context.py and/or chat_context.py, i guess). Use this new column for history sorting.
This way, thread.updatedAt will still track when any changes ocured (including lesser changes) but displayed history order will not be affected unless a substantial chat context change happened.
- Improve the SQL query in update_thread() to compare new thread state with original. If nothing changed, do not bump up updatedAt timestamp. Note, there is a dependency/overlap with Chainlit Data Layer - preserve metadata in update_thread() #2538 as it is impacting the same piece of code
Additional context
Not sure if the issue might have become more noticeable after timezones bug fix: #2486 (which was rejected, but i have it on my end). There is a chance the timezones mess may have been partially masking the issue - but not fully. That being said, this issue is genuine standalone.