oliver-zehentleitner / maity
clean-up: G3 requests, events, presence, bridges: slow paths in the request inbox, bridge requests, agent lease renewal, coordinator gate and resume preferences
tsk-kwfr5gs7tz82 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/.
Slow paths on hot routes. All S and low risk.
1. GET /requests (cross-project inbox) costs several statements per project
- routes/requests.py:1183-1210. Per membership project it calls
membership(conn, caller, namespace.full_name(...))(:1190), which resolves the project again by name, computesnamespace.full_namea second time (:1201), and runs_filteredand_asker_handles(2-3 queries) per project. _personal_inboxreads_project_inlineper project (:2819, a project query plusfull_name),_org_inline_ofper org (:2820), andprincipal_idper row withasked_by(:2787).- test_request_list_batching.py keeps the count flat per row, but with one project only.
- Proposal: read all memberships with their projects once, use
namespace.full_namesin a batch, build eachMembershipfrom the rows read, batch the inline project and org reads, and let_project_inline(:2420-2427) replace the inline copy at :1199-1204. Add a statement-count test with 1 and 5 projects. - Effort: S-M. Risk: low.
2. Bridge requests are found by scanning every open proposal request on the hub
- bridges.py:153-180 (
open_requests) and :442-455 (inend_for_member) select every open request with a proposal, in all projects, personal or not, and filterkind/served/bridgein Python. - Callers: every bridge POST, PATCH and DELETE (routes/bridges.py:70, 75, 109, via
end),checkandsettle, every member removal (routes/members.py:1322) and role change to proxy (:671).owner_left(bridges.py:488-501) callsend_for_memberper owned agent, so two scans per agent. - This is the same shape as tsk-5whysgm79amz item 1 (
foreign_agent.open_for,cross_owner.open_pair), which does not list bridges. - Proposal: use the helper of tsk-5whysgm79amz item 1 (filter
project_idin SQL; a membership id belongs to one project) in both places. - Effort: S. Risk: low (test_bridges).
3. Every agent call pays about three reads for the lease renewal, even without a claim
- auth/caller.py:607-614 (
_agent) readsproject.lease_seconds, thenavailability.renewed_lease(availability.py:644-656), whosehold_cap(:621-641) reads the membership and the project again (and the human for a human row), before the card UPDATE. bridges.py:739-747 (_renew_carried) uses the same function. - tsk-dfatkddmt565 (done) limited the cookie path to memberships that hold an open claim and left the agent path out; it runs on every agent request.
- Proposal: let
hold_captake the rows the caller already has (or addlimit_until,outage_since,lease_seconds,absence_untilto_credential's join), and skip the reads whenopen_claims(membership_id, instance)matches nothing. - Effort: S. Risk: low to medium (lease semantics). test_availability, test_limit_race and test_human_lease must pass on PostgreSQL.
4. coordinator_gate builds full member views on every agent assignment or takeover
- availability.py:327-375, called through
refuse_coordinator_out(:309) from routes/tasks.py:881, 1121, 1394, 1723 and takeovers.py:168. - For every coordinator row (under
trust:admin, every agent admin) it runspresences_of(views.member_views) anddormants_ofbefore_out_beyond_grace, which can only be true withoutage_sinceset or a grace of 0.coordinator_of→isolation_principalsruns twice per gate (:332 viacoordinator_ids, and :373) and twice inchanged_audience(:276-277). - Proposal: return None early when a coordinator row has
outage_sinceNone and a grace above 0; compute the seat once and pass it on. - Effort: S. Risk: low (test_availability, test_cover).
5. N+1 in the preferences block of every agent resume and briefing
- preferences.py:263-292 (
texts): per candidate human it callsitem(:251-260) →view(:46-63: the preferences row, and_version_view's_handleofby), then reads the human's handle again althoughtextsjoined it (:268).versions(:80-92) runs_handleper row (up to 200). - Callers: routes/me_session.py:582 (
packet, every resume), routes/briefing_view.py:175. - Proposal: one
INread of the candidates'preferencesrows, batchedbyhandles, and the handle from the join. - Effort: S. Risk: low.
Order
- Item 2 after tsk-5whysgm79amz item 1 (its helper).
- Item 3: tsk-z0tpe6pz3fjq and the other auth cards touch auth/caller.py; rebase.
Covered elsewhere
- Cross-chat listing and resume packet: tsk-sb4pc3190w20 A6-7.
- The agent side of the lease renewal is on no card (searched renewed_lease, hold_cap; tsk-dfatkddmt565 scoped it out).
Effort sum: 1 S-M + 4S, about 6 points.