oliver-zehentleitner / maity

clean-up: isolation and other owners' agents: repeated queries and rules written twice (consent requests, agent_owners, restricted sets, roles)

Sign in to report

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_changed part depends on the open question on the Area 5 debt card (filed at the same time): if restricted.owner_changed is 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_of and about 20 inline copies) is a hub-wide pattern for a shared card. If that card lands first, join_waits uses 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_id filter), then filter on proposal.kind and target in Python. They run on every member PATCH that touches reports_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 by project_id in SQL (a target membership id belongs to one project). Use it in all three places. Optionally filter on the JSON kind in SQL as restricted.py:196-205 already does for account_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 by join_waits (117-129), only to call owner_of. join_waits also repeats the comparison that owners() (48-58) already makes.
  • Proposal: in join_waits, look up the profile owner directly and drop new_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_roles and its base in OUTSIDE_BASES" is in within_limits (agent_owners.py:67-75) and again inline in check_assignment (170-180, with values.get("role_base") or base_of).
  • The query "active, unisolated agent members of the project" is in reconcile (106-114) and again in check_policy (186-193).
  • is_outside (58-64) makes two queries per row (profile owner, then owner membership), so reconcile and check_policy are N+1 over the project's agents.
  • Proposal: check_assignment calls small helpers trust_ok(trust) / role_ok(project, role, base) that within_limits also uses. One _agents_at_work(conn, project_id) select shared by reconcile and check_policy, joined to agent_profile.owner_id and the set of active human member ids, so is_outside doesn'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 shared role_ok then 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_id for every moved card, although project is a parameter and every released card belongs to that project (release_claims is per membership, and a membership is in one project). It also runs on member removal (routes/members.py:1344, 1366).
  • Proposal: pass project.state once 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 in reads_members (404) and again for included (416 or 421).
  • announced (425-434) resolves once per project_updated event for a reader without the full view (called per event from views.py:1489; _RestrictedReads caches only _full).
  • owner_changed loads 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. Cache included next to _full in views._RestrictedReads and pass it to announced. Load the project once in owner_changed (if it is kept). Use in_set at 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 and base_of.
  • Effort: S. Risk: none.

Effort sum: S+S+S+S+S+S = 6 points.