oliver-zehentleitner / maity
clean-up: chats and inbox: conversation-list rules once in production, shared chat helpers, one reports_to walk, one message paging
tsk-vt3nbqq1v4rz 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. Area 6 includes conversation_list.py and routes/conversations.py (closest-caller rule).
Order:
- A6-16: 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 slow-paths card (A6-17, filed at the same time) edits_unreadin the same file. Rebase rather than conflict. - A6-11: the Area 6 mention-resolver card (A6-1, filed at the same time) also reshapes mentions.resolve's member read. Do the people-join part with it.
- A6-13: tsk-dwrtrhdgzezs step 3 moves
routes/members.chaininto a domain module. Put the shared walk in line.py and let that move import it, or do A6-13 after the move. - A6-19 and A6-20 touch the same paging code; one PR is fine.
A6-16. Design and duplication: routes/conversations.py keeps a second, one-chat-at-a-time copy of the list rules
- routes/conversations.py:379-395 (
_owned_agents) has no caller in hub/src. Its comment says it is kept as the reference for hub/tests/test_conversation_list.py:36; the batched version is conversation_list.py:168-185. These pairs are the same: _preview274-285 and conversation_list.preview 53-64_short301-303 and conversation_list.short 48-50_direct_unread347-362 and conversation_list._direct_unread 286-297_window365-376 and conversation_list._windows 471-493 (the query "agent ids owned by the observer" is written in both, conversation_list.py:478-483 and routes/conversations.py:371-375; audit finding A6-18)_direct_item318-344 (with_with_meat_proxy306-315) and conversation_list.direct_entries 228-269- conversation_list._access 443-468, which repeats group_chats.access (group_chats.py:199-214)
- Of the single-chat copies, only
_direct_itemserves a route: GET of a direct conversation (routes/conversations.py:702). - What is wrong: each rule exists twice in production code, and only a test keeps the copies equal.
_owned_agentsis production code that exists only for a test. - Proposal: GET of a direct chat calls
conversation_list.direct_entries(conn, [mine], caller, {project: [agent_id]}). Move the independent reference (_owned_agents,_direct_item,_direct_unread,_preview,_short,_with_meat_proxy) into hub/tests/test_conversation_list.py, so the test keeps its oracle and production keeps one copy. Keep_accessin step withgroup_chats.access, either by a test or by havinggroup_chats.accesscall the batched_accesswith the rows it has read. - Effort: M. Risk: low-medium. The test oracle moves, and GET of a direct chat changes path but should give the same output.
A6-10. Duplication: membership_ended and isolation_settled implement the same rule
- cross_chats.py:1159-1173 (
membership_ended, called from routes/members.py:1408) and :1176-1186 (isolation_settled, called from isolation.py:158). Both readisolated_sinceof the agent's active memberships, and both leave every chat withmembership_isolatedwhen all of them are isolated.membership_endedalso handles "none left". - Proposal: one function, e.g.
settle_profile(conn, agent_id, by): none left →membership_removed, all isolated →membership_isolated. Both callers use it. - Effort: S. Risk: low.
A6-11. Duplication: group chats and cross-project chats have parallel helpers
refuse_closedis identical in group_chats.py:438-445 and cross_chats.py:93-100.- The participation dict has the same shape in group_chats.py:318-328 and cross_chats.py:303-311.
- The "membership + agent_profile + owner human + human" people join appears in group_chats.py:114-149 (
people) and again in mentions.py:178-194 (resolve); cross_chats.py:314-363 (_people) builds the same{member_id, kind, handle, name, owner_id}from ids. - Proposal: one
refuse_closed(row)and oneparticipation_view(stay, member_id)shared by both modules (group_chats imports it from cross_chats, or from a small chat_views module). mentions.resolve reads its members through group_chats.people or a shared query helper. The table-specific stay queries stay where they are. - Effort: S-M. Risk: low.
A6-13. Duplication: three walks up the reports_to chain
- chat_gate.py:549-558 (
_in_line, over the reports_to of all project memberships read at :522, active or not), line.py:125-136 (_above, active only, one query per step), and routes/members.py:353-364 (chain, active only, one query per step). - What is wrong: the rule "is X in Y's line" (§2.3) is written three times, and the active filter differs (chat_gate reads inactive rows too). Removal clears reports_to (routes/members.py:1379), so today the outcome is probably the same, but nothing makes sure it stays that way.
- Proposal: one helper in line.py that walks a
{member: boss}map read once (active rows). chat_gate.hold and the members route both use it. - Effort: S. Risk: low.
A6-19. Duplication: the paging of a message page is written twice
- routes/conversations.py:873-908 (
read_messages) and cross_chats.py:754-794 (messages) run the same algorithm on two tables:before→ newest-first, reversed, cursor 0; otherwiseseq > max(after, window), cursor = last seq or start; then a one-row look below the first row forearlier. - Proposal: one helper, e.g.
paging.seq_page(conn, query, seq_col, after, before, limit, window), used by both. - Effort: S. Risk: low.
A6-20. Duplication: constants defined more than once
PREVIEW = 200is defined in conversation_list.py:28, routes/conversations.py:271 and cross_chats.py:33.SEQ_MAX = 2**63 - 1is defined in routes/conversations.py:54 and routes/events.py:116, although paging.py:16 already hasSEQ_MAX, which routes/cross_chats.py and routes/project_messages.py:27 use.- Proposal: one
PREVIEW(in conversation_list, imported by cross_chats), andpaging.SEQ_MAXeverywhere. - (
PAGE_DEFAULT, PAGE_MAX = 50, 200, repeated in 16 route modules, is repo-wide and not part of this card.) - Effort: S. Risk: low.
Effort sum: M+S+S-M+S+S+S = 1M + 1 S-M + 4S = about 9 points.