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

Sign in to report

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_updated audience (317-323);
  • the watchers (324-341) and the team wakes (343-352);
  • the at floor (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 at instead of passing at.
  • 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. Pass at at 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) then return 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:81 reads it too.
  • The audience check for the reader's own instance token is built by hand at events.py:1090, 1131, 1148, 1162 and 1205, although instances.token() (instances.py:37-39) builds that token.
  • The group-chat test startswith('cnv-') is inline at events.py:697 and 719, beside _group_chat (1220-1221).
  • The watchers-token test is at events.py:288, 332 and views.py:1511.
  • Proposal: add _chosen(row, principal, instance), an own-token helper built on instances.token, use _group_chat everywhere, 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_isolated is a public alias of _isolated (an agent membership with isolated_since). Used at routes/events.py:201 and 251.
  • isolation.py:27 is_isolated: isolated_since only, any kind. Used at isolation.py:140, 195 and agent_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 builds hidden_redactions for each 500-row batch (955), watched_seqs included (619).
  • It then calls hidden_redactions again on the event_redacted rows (1248) and watched_seqs again 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 _scan already calls, so read_waking scans with limit and makes no second pass. The notice: and watching: entries are already in hidden.
  • Effort: M
  • Risk: medium. The cursor rule (examined against 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_redactions per batch and filters with readable_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: _seq and _limit in routes/project_messages.py:33-59 repeat routes/events.py:125, 132 and 334 (not_a_seq, out_of_range), and also routes/attachments.py:369 and routes/people.py:287.
  • Proposal: one events.scan(conn, scope, readable, start, *, descending, limit, budget) generator for all four loops. Add paging.limit_param(request, default, maximum) and paging.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.