oliver-zehentleitner / maity
clean-up: chats and inbox: slow paths on chat posts, cross-chat lists, chat-request sync and the mention count
tsk-sb4pc3190w20 Open
From the clean-up audit tsk-hgnbapyw7hnh, Area 6 (Chats and inbox), read at 80647565. Line numbers are at that SHA; check them against current main first. Paths are under hub/src/maity_hub/. Finding numbers are the audit's.
Slow paths on hot routes: every group-chat post, the cross-chat list and agent resume, every chat-request sync, and the conversation list.
Order:
- A6-6: tsk-yy88szfa7sfh (in progress, PR #1747) changes chat_gate.py. Start after it merges.
- A6-17: tsk-8vdegd61y70v item 7 adds a chat read-mark function to conversation_list.py, and tsk-k25qqhygend8 item 2 replaces its
_chunks. The Area 6 conversation-list card (A6-16, filed at the same time) also edits conversation_list.py. Rebase rather than conflict. - The others have no order.
A6-6. Debt and slow path: a no-op hook whose argument costs queries on every chat post
- Files: chat_gate.py:480-485 (
boundary) doesdel conn, project_id, author, body, sides; the content check is card tsk-5xqzz2scmebr (context/chat.md:679, 708; docs/api.md:14661). Its callers still computesidesfor it. routes/events.py:526 runschat_gate.sides_of(conn, current)on every group-chat post, andsidesis used only at :652.sides_of→parties→group_chats.carriers(bridges.cross_owner_pairs + principal_ids) +owners_of: at least 3 queries. jourfixe/exchange.py:155 (principal_ids, used only for this call) and :161 (sides_of) do the same on every round-thread post. - Proposal: until the check exists,
boundarytakes what is already at hand (conn, project_id, current, author_membership, body) and computes the sides itself when the check needs them. Or drop the argument until the card lands. The hook stays as the single point of entry. - Effort: S. Risk: low.
A6-7. Slow path: cross-chat listing and resume packet read each chat's stays twice
- Files: cross_chats.py:685 (
stays(conn, chat.id)per chat to collect member ids) and :630 (_list_itemcallsstaysagain per chat). That is 2 queries per chat: up to 400 per page (routes/cross_chats.py PAGE_MAX 200) and 100 on every agent resume, throughresume_packet(cross_chats.py:705-717, PACKET 50; routes/me_session.py:712). - Proposal: read the current stays of all page chats in one
INquery, group them by chat, and pass each chat's list to_list_item. - Effort: S. Risk: low.
A6-8. Slow path: the routes use the full chat view as their access check
- Files: routes/cross_chats.py:137, 152, 171, 249, 265 call
cross_chats.view(conn, chat, reader) is Noneonly to decide not_found.view(cross_chats.py:432-458) reads the stays,_people(2 queries) and_projects(2 queries plus full names). The write routes then build the same view again through_visible(routes/cross_chats.py:84-88). - Proposal: a public
cross_chats.may_read(conn, chat, reader)built onstays+_access(cross_chats.py:424-429), used for the checks. Build the view only for the response. - Effort: S. Risk: low.
A6-12. Slow path: chat_request recomputes the request summary on every sync, query by query
- Files: chat_request.py:305-313 (
_refresh) rebuilds_summaryon everysyncof an open request before comparing it._summary(218-256) runs_people_in(208-215:group_chats.stays+chat_gate.owners_of) once per waiting chat, and reads the project row +namespace.full_nameonce per project (240-241), althoughnamespace.full_namestakes a batch._waitingruns twice per sync (349 and 221 via_refresh), and_titlereads the side's handle again (203-205).syncends everychat_gate.settle, every entry change and the worker sweep. - Separately,
_request_id(138-143) reads every closed hub-filed personal request of the owner, proposal JSON included, and counts kind/side in Python each time a request is filed, so the cost grows with the owner's history. The same Python filter onproposal.kindappears at 114, 142 and 464. - Proposal: build the summary from one read of the waiting chats' stays and owners, and use
namespace.full_names. Pass_waitingfromsyncinto_refresh. For_request_id, select onlyproposal(not whole rows), or keep a per-pair counter. JSON filtering in SQL across SQLite, PostgreSQL and MariaDB may be why it is done in Python, so check that first. - Effort: M. Risk: medium.
_request_id's collision semantics (hcrb on #1718) depend on counting exactly the pair's closed requests.
A6-17. Slow path: the mention count reads full payloads and matches them as text
- Files: conversation_list.py:512-536 (
_unread) selects the wholepayloadof every unread message by others in each participant chat, and prefilters withcast(payload, String).contains(...)(_may_name, 588-596) before checking the mentions in Python. This runs on GET …/conversations, on GET /me/conversations across all projects, and on every agent resume (routes/conversations.py:549-563,conversation_packet). chat_unread already reads onlypayload["mentions"]as a JSON projection on every engine (chat_unread.py:201). Cross chats keep a per-member mention row, so they need no scan (cross_chats.py:1084-1091,cross_chat_mention_for). - Proposal: select
payload["mentions"]andredactedinstead of the whole payload, and drop the text LIKE. Longer term, write a per-member mention row for group chats, as cross chats do, and count those rows. - Effort: S for the projection, M for the mention table. Risk: low.
- Open question: is the per-member mention table for group chats wanted (a schema change), or only the projection?
Effort sum: S+S+S+M+S = 4S+1M = 7 points (plus M if the mention table is wanted).