oliver-zehentleitner / maity
clean-up: isolation and other owners' agents: repeated queries and rules written twice (consent requests, agent_owners, restricted sets, roles)
tsk-5whysgm79amz Open
From the clean-up audit tsk-hgnbapyw7hnh, Area 5 (Isolation and other owners' agents), 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. All items are S and low risk.
Order:
- Item 11's
owner_changedpart depends on the open question on the Area 5 debt card (filed at the same time): ifrestricted.owner_changedis deleted there, skip it here. - Item 2 here is only the area-local part. The owner lookup itself (
cross_owner.owner_of,agent_owners.owner_ofand about 20 inline copies) is a hub-wide pattern for a shared card. If that card lands first,join_waitsuses its helper.
1. Duplication + slow path: the "open consent requests of a member" query exists three times and scans the whole hub
- Files: foreign_agent.py:128-144 (
open_for), cross_owner.py:75-92 (open_pair), cross_owner.py:95-114 (withdraw_for_member, the same select again). - What is wrong: all three select every open personal request that has a proposal, across all projects (no
project_idfilter), then filter onproposal.kindandtargetin Python. They run on every member PATCH that touchesreports_to(check_may_supersede, members.py:787), on every foreign-agent PATCH (withdraw_open), and on every member removal (members.py:1318-1319, both modules). The cost grows with the number of open personal requests on the whole hub. project_names.py:41-49 and preferences.py:360-410 use the same "load, then filter kind in Python" shape, but at least filter by project or member. - Proposal: one helper, e.g.
proposals.open_of_kind(conn, kind, project_id, target=None), filtering byproject_idin SQL (a target membership id belongs to one project). Use it in all three places. Optionally filter on the JSONkindin SQL as restricted.py:196-205 already does foraccount_record.data. - Effort: S. Risk: low. Each caller has the project at hand. test_foreign_agent.py and test_cross_owner.py cover supersede and withdrawal.
2 (area-local part). Debt: a stub object only to call owner_of
cross_owner.new_agent(43-45) is a SimpleNamespace stub used only byjoin_waits(117-129), only to callowner_of.join_waitsalso repeats the comparison thatowners()(48-58) already makes.- Proposal: in
join_waits, look up the profile owner directly and dropnew_agent. - Effort: S. Risk: low.
9. Design/duplication in agent_owners.py: the outside-limits rule and the "agents at work" query are each written twice
- The role rule "role on
outside_rolesand its base inOUTSIDE_BASES" is inwithin_limits(agent_owners.py:67-75) and again inline incheck_assignment(170-180, withvalues.get("role_base") or base_of). - The query "active, unisolated agent members of the project" is in
reconcile(106-114) and again incheck_policy(186-193). is_outside(58-64) makes two queries per row (profile owner, then owner membership), soreconcileandcheck_policyare N+1 over the project's agents.- Proposal:
check_assignmentcalls small helperstrust_ok(trust)/role_ok(project, role, base)thatwithin_limitsalso uses. One_agents_at_work(conn, project_id)select shared byreconcileandcheck_policy, joined toagent_profile.owner_idand the set of active human member ids, sois_outsidedoesn't query per row. - Effort: S. Risk: low. test_agent_owners.py covers both paths.
- Note: tsk-jf0m1t0fa6a9 (open, contract) will widen
outside_roles; the sharedrole_okthen changes in one place.
10. Debt (slow path): isolation.tell_released re-reads the project state once per card
- isolation.py:89-94: the loop runs
select(project.state) where id == card.project_idfor every moved card, althoughprojectis a parameter and every released card belongs to that project (release_claimsis per membership, and a membership is in one project). It also runs on member removal (routes/members.py:1344, 1366). - Proposal: pass
project.stateonce and drop the per-card query. - Effort: S. Risk: none.
11. Debt (repeated work): restricted.resolve runs several times for one answer
served(restricted.py:407-422) resolves the set inreads_members(404) and again forincluded(416 or 421).announced(425-434) resolves once perproject_updatedevent for a reader without the full view (called per event from views.py:1489;_RestrictedReadscaches only_full).owner_changedloads the project twice (356, 359).in_set(85-86) exists, but callers repeat it inline: notifications/items.py:1036, routes/briefing_view.py:206, routes/me_session.py:639.- Each resolve reads every membership of the project with a join.
- Proposal: in
served, resolve once and pass the set on. Cacheincludednext to_fullinviews._RestrictedReadsand pass it toannounced. Load the project once inowner_changed(if it is kept). Usein_setat the three inline sites. - Effort: S. Risk: low. The set is fixed within one read transaction.
13. Duplication: looking up a project role by name
- guide.py:149 and 165, rules.py:145 each write
next((r for r in project_roles.of(project) if r["name"] == role), None). project_roles.base_of (37-42) does the same lookup inside. - Proposal: add
project_roles.find(project, name) -> dict | None. Use it in guide.py, rules.py andbase_of. - Effort: S. Risk: none.
Effort sum: S+S+S+S+S+S = 6 points.