clean-up: G3 requests, events, presence, bridges: move the message domain out of routes/events.py; one reach rule, one payload, one chat-read rule
tsk-qa39qay2gbps Open
From the clean-up audit tsk-hgnbapyw7hnh (uncovered files, G3 Requests, events routes, presence and bridges), read at 9dfd4b65. Line numbers are at that SHA. Paths are under hub/src/maity_hub/.
Messages and chat reads: routes/events.py carries the message domain, and several chat rules are written twice.
1. Design: routes/events.py holds the message domain, and two route modules import each other
- Files: routes/events.py:470-482 (
chat_parties), 485-689 (post_message, about 200 lines), 692-735 (_deliver_review), 738-756 (_group_for_posting), 759-793 (remember_read), 796-815 (_roles), 818-855 (writer). - Other route modules import these: routes/conversations.py:42-48 (
MessageIn,chat_parties,post_message,remember_read,writer), routes/attachments.py:33 (chat_parties), routes/bootstrap.py:38 (head_body), routes/runner.py:43 (FALLBACK_SECONDS,answer,parse_after,parse_limit), routes/project_messages.py:22 (MESSAGE_TYPES,PAGE_DEFAULT,PAGE_MAX). - routes/events.py imports routes/conversations back inside functions (:370 "it imports this module", :834
outsider). The two route modules form a cycle that local imports hide. - What is wrong: posting a message (validation, audience, mentions, holds, review delivery) is domain logic shared by two routes, but it lives in one route module. tsk-dwrtrhdgzezs covers only non-route modules importing
routes.*, so this cycle is not on it. - Proposal: move
post_messageand its helpers (chat_parties,_deliver_review,_group_for_posting,_roles,writer,MESSAGE_TYPES) into a domain module, e.g.messages.py, withoutsider(see tsk-sjq1h8tf6nmy A6-21 for_reader/Outsider). Move the long-poll answer helpers (answer,answer_body,parse_after,parse_limit,FALLBACK_SECONDS) to longpoll.py together with tsk-javm686g22kx item 2.remember_readgoes wherever tsk-8vdegd61y70v item 7 puts the shared read mark. - Effort: M. Risk: low to medium (pure moves, but the hottest write path).
2. Duplication: "does this audience already reach the member" is written several times
- Files: routes/events.py:636-640 (mention pings in
post_message), routes/events.py:723-729 (_deliver_review), chat_gate.py:513-517 (hold), instances.py:604-607 (team wakes). Each testsall, the member's ownmember:/instance:token andtokens_of(trust, role, role_base) & audience. events.py:894-898 (readable_by) and :1177-1181 (wakes) apply the same token test inside their own rules. - What is wrong: the §8.2 reach rule is spelled out per caller, with small deliberate differences (
alldoes not count in_deliver_review; a dormant instance only by its token). Nothing names those differences, so a change to the token rules has to find every copy. - Proposal: one predicate, e.g.
scopes.reaches(audience, principal, trust, role, base, *, all_counts=True), used by the four call sites; the differences become arguments. Leavereadable_by/wakesunless the predicate fits without change. - Effort: S-M. Risk: medium (who is woken and who is pinged). test_mentions, test_review_delivery and test_chat_gate cover it.
3. Duplication: a chat message payload is built in two places
- Files: routes/events.py:656-669 (
post_message) and jourfixe/exchange.py:139-189 (_post_message, a round's thread). Both build{type, to, body, conversation_id, task_id, mentions, held, conversation}; the exchange copy leaves outattachments. Both also callchat_gate.boundarywithsides_of(see tsk-sb4pc3190w20 A6-6). - Proposal: one
message_payload(...)builder in item 1's module, used by both, so every message has the same keys. - Effort: S. Risk: low.
4. Duplication: the seq query parameter is parsed twice, the path seq a third way
- Files: routes/events.py:119-126 (
parse_after) and routes/project_messages.py:33-39 apply the same check (isascii,isdigit,<= SEQ_MAX,not_a_seq). routes/events.py:324-335 (parse_seq) is the stricter path rule (no leading zero, docs/api.md:13415). routes/cross_chats.py:206-208 uses FastAPIQuery(ge, le)instead. - Proposal: one
paging.parse_seq(raw, field, *, strict=False)used by the three. (SEQ_MAXitself is tsk-vt3nbqq1v4rz A6-20.) - Effort: S. Risk: low.
5. Duplication: the actor of a human who is no member is built four times
- Files: line.py:406-410 (
owner_actor), routes/bridges.py:180-182 (_outside_actor), routes/requests.py:2222, cross_owner.py:322. Each writesActor(membership_id=None, human_id=..., via=...). - Proposal:
Caller.outside_actor()next toCaller.actor(auth/caller.py:135), used by the first three; cross_owner builds it from a stored request and keeps its line. - Effort: S. Risk: none.
6. Duplication: pins.readable is a second "who reads this chat" rule
- Files: pins.py:89-127 (
readable, used byprune) against routes/conversations.py:223-256 (_party,_group,reads_messages), which the pin route itself uses (routes/conversations.py:569-580). pins.py:106-111 builds aSimpleNamespacethat imitates aMembership(id -1 for an owner outside) to callgroup_chats.access. - What is wrong: a pin is granted by one rule and pruned by another. The direct-chat branch (pins.py:115-127) re-derives "an active agent the human owns" instead of using
chat_parties. - Proposal: one domain function "may this human read this chat" (in item 1's module, or in group_chats) used by both the pin route and
prune, without a fake membership. - Effort: S. Risk: low (test_conversation_pins).
7. Debt: small repeats and leftovers
- routes/chat_access.py:75-120:
create_allow,patch_allowandcreate_denyare the same handler around a differentchat_accesscall; :123-152delete_allowanddelete_denyare identical except the kind. One helper each. - routes/events.py:919-922 and :1045-1048: the same
already_redactedApiError written twice. One factory, like_not_a_participant(:1009). - bridges.py:141:
from maity_hub.errors import not_foundinsideget, althougherrorsis imported at the top (:24). routes/cross_chats.py:205 and :237:from maity_hub import tables as tinside functions. No cycle in either case. - Effort: S. Risk: none.
8. Docs drift: hub/context/bridges.md says an ended connection's row is deleted
- hub/context/bridges.md, entry 6b2bcc80 ("
viais stored on the event"): "a connection can end and its row is deleted, the record stays". The same file (entry a4dd5557) and bridges.py:404-425 (end) keep the row withliveNULL. - Proposal: correct the sentence (the record stays because the JSON has no foreign key, not because the row goes).
- Effort: S. Risk: none.
Order
- Item 1 together with or after tsk-javm686g22kx item 2 (the long-poll helper), and in the style of tsk-dwrtrhdgzezs. Items 3 and 6 build on item 1's module.
- tsk-sb4pc3190w20 A6-6 changes the
chat_gate.boundarycall in both places of item 3; rebase.
Covered elsewhere
remember_readvsnotifications/listing._move_chat_mark: tsk-8vdegd61y70v item 7.SEQ_MAXcopy at routes/events.py:116: tsk-vt3nbqq1v4rz A6-20.boundary/sides_ofcost on every post: tsk-sb4pc3190w20 A6-6.- The long-poll loop in routes/events.py:264-318: tsk-javm686g22kx item 2.
tombstoneimported by moderation.py:370: tsk-dwrtrhdgzezs.- routes/cross_chats.py access checks through the full view: tsk-sb4pc3190w20 A6-8.
- RFC 3339 parsing in routes/limit.py:42: tsk-kfpz2m8we8f1.
Effort sum: 1M + 1 S-M + 6S, about 10 points.