oliver-zehentleitner / maity
clean-up: core platform: events.py: emit as a dispatcher, wake-rule helpers, two isolation predicates, read_waking re-reads, copied event scans
tsk-dxywhnz3w0h0 Open
From the clean-up audit tsk-hgnbapyw7hnh (uncovered files, G1 core platform), read at 9dfd4b65. Paths are under hub/src/maity_hub/.
1. Design: emit has grown past writing an event
- Files:
events.py:301-485. - What is wrong: 185 lines. After the lock it does all of this:
- the
member_updatedaudience (317-323); - the watchers (324-341) and the team wakes (343-352);
- the
atfloor (356-363), then the insert; - the watcher, restricted-list and notification rows (394-410);
- onboarding (411-421) and
pg_notify; - then 50 lines of public-page decisions (432-484).
- It has 7 function-local imports (availability, card_watches, instances, onboarding, public_pages, public_projects, public_people).
- Nearly every write calls
emit, and every new consumer edits it. - Line 420 recomputes
atinstead of passingat. - Proposal: move the public-page block into one
public_pages.on_event(conn, project, kind, payload, subject)that keeps its rules and context references. Put onboarding behind the same kind of call. Passatat 420. Keep the order: everything stays inside the transaction, after the insert. - Effort: M
- Risk: medium. Public pages render under the project lock (hub/context/public-pages.md 721bcdfa). test_public_pages and the onboarding tests cover it.
2. Duplication: the wake-rule pieces are written out again and again
- Files:
chosen = (row.wakes or {}).get(principal)thenreturn chosen is None or instance is None or instance in chosen:events.py:1084-1085,1099-1100,1113-1114,1120-1121,1183-1184,1216-1217.not_listening.py:81reads it too.- The audience check for the reader's own instance token is built by hand at
events.py:1090,1131,1148,1162and1205, althoughinstances.token()(instances.py:37-39) builds that token. - The group-chat test
startswith('cnv-')is inline atevents.py:697and719, beside_group_chat(1220-1221). - The watchers-token test is at
events.py:288,332andviews.py:1511. - Proposal: add
_chosen(row, principal, instance), an own-token helper built oninstances.token, use_group_chateverywhere, and add_has_watchers(audience). - Effort: S
- Risk: low. These are pure functions, and the wake tests cover them.
3. Duplication: two isolation predicates with different rules
- Files:
events.py:810-821:is_isolatedis a public alias of_isolated(an agent membership withisolated_since). Used atroutes/events.py:201and251.isolation.py:27is_isolated:isolated_sinceonly, any kind. Used atisolation.py:140,195andagent_owners.py:94.- Proposal: one predicate in isolation.py, the agent-only form, since only agents are isolated (§6.2). Both modules use it, and the alias goes.
- Effort: S
- Risk: low.
4. Slow path on the hot long-poll: read_waking reads twice and reads too much
- Files:
events.py:1224-1260. Every waking pass of GET .../events runs it (routes/events.py:206). - What is wrong:
- It calls
read(..., 10_000)(1244-1246). That buildshidden_redactionsfor each 500-row batch (955),watched_seqsincluded (619). - It then calls
hidden_redactionsagain on theevent_redactedrows (1248) andwatched_seqsagain on all rows (1249). These statements recompute what the scan already had. - It collects up to 10,000 readable rows before cutting to
limit(1257), so an agent far behind loads that many rows to return a handful. - Proposal: fold the wake test into the
readable(row, hidden)predicate that_scanalready calls, soread_wakingscans withlimitand makes no second pass. Thenotice:andwatching:entries are already inhidden. - Effort: M
- Risk: medium. The cursor rule (
examinedagainst the last row returned) must stay. test_events, the wait tests and test_ticker cover it. - Related: tsk-x1rsd4r6htcv (merged) skips passes with nothing new. This item is the cost of a pass that does read.
5. Duplication: the "batches of readable events" loop and its query parsers
- Files:
- The loop, four times. Each copy builds
hidden_redactionsper batch and filters withreadable_by:
- events.py:938-963 _scan (ascending);
- events.py:1287-1306 _highest (descending);
- routes/project_messages.py:69-108 _scan (either direction, with a budget);
- ticker.py:581-602 (either direction).
- The parsers beside them:
_seqand_limitinroutes/project_messages.py:33-59repeatroutes/events.py:125,132and334(not_a_seq,out_of_range), and alsoroutes/attachments.py:369androutes/people.py:287. - Proposal: one
events.scan(conn, scope, readable, start, *, descending, limit, budget)generator for all four loops. Addpaging.limit_param(request, default, maximum)andpaging.seq_param(request, name). - Effort: M
- Risk: low to medium. The callers resume at different positions; keep each one's rule.
Order
- Do items 4 and 5 together, or 5 first: both reshape
_scan. - Item 1 is independent.
Covered elsewhere
newest_seq(events.py:255-262) is the canonical form for tsk-50zhasvxh7nt.
Effort sum: M+S+S+M+M = 11 points.