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

Sign in to report

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, computes namespace.full_name a second time (:1201), and runs _filtered and _asker_handles (2-3 queries) per project.
  • _personal_inbox reads _project_inline per project (:2819, a project query plus full_name), _org_inline_of per org (:2820), and principal_id per row with asked_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_names in a batch, build each Membership from 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 (in end_for_member) select every open request with a proposal, in all projects, personal or not, and filter kind/served/bridge in Python.
  • Callers: every bridge POST, PATCH and DELETE (routes/bridges.py:70, 75, 109, via end), check and settle, every member removal (routes/members.py:1322) and role change to proxy (:671). owner_left (bridges.py:488-501) calls end_for_member per 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_id in 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) reads project.lease_seconds, then availability.renewed_lease (availability.py:644-656), whose hold_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_cap take the rows the caller already has (or add limit_until, outage_since, lease_seconds, absence_until to _credential's join), and skip the reads when open_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 runs presences_of (views.member_views) and dormants_of before _out_beyond_grace, which can only be true with outage_since set or a grace of 0. coordinator_of → isolation_principals runs twice per gate (:332 via coordinator_ids, and :373) and twice in changed_audience (:276-277).
  • Proposal: return None early when a coordinator row has outage_since None 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 calls item (:251-260) → view (:46-63: the preferences row, and _version_view's _handle of by), then reads the human's handle again although texts joined it (:268). versions (:80-92) runs _handle per row (up to 200).
  • Callers: routes/me_session.py:582 (packet, every resume), routes/briefing_view.py:175.
  • Proposal: one IN read of the candidates' preferences rows, batched by handles, 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.