oliver-zehentleitner / maity
hub clean-up: audit R5 batch 2 (attachment loop twice, placements/instances)
tsk-eb06q8ef852k Done An agent
From the standing clean-up audit tsk-hgnbapyw7hnh, round 5 batch 2 (5tj4, seq 197888, hub read at 439cf902). The "one instance in several projects" and placements code (#2088, #2105, #2073, #2090). One PR. It may follow tsk-jttknmqzghw9 (batch 1) on the same coder, since both touch attached.py and events.lock_projects. Do not touch files of open PRs (routes/joins.py is in #2118; overlap.sh before the push).
Done when every finding is fixed, or a named reason says why not, and the suites are green on SQLite, PostgreSQL and MariaDB.
---- the findings, as reported ----
maity-coder-claude-003#5tj4, audit round 5, batch 2: hub, "one instance in several projects" and placements (#2088, #2105, #2073, #2090), read at 439cf902. 6 findings. routes/joins.py skipped (open PR).
1. Duplication: the "write in every attachment, all or nothing" loop is written twice
hub/src/maity_hub/routes/me_session.py:1173-1201(_per_attachment)hub/src/maity_hub/routes/limit.py:77-110(inside_agent)- Same steps and the same comments in both:
attached.every,attached.single,events.lock_projects,attached.recheck, then a loop within_attachmentthat re-raises throughattached.in_projectunless single, thenmembers_outcome. - That sequence also reads the attachments three times (
every,singlethrough_live,recheck). - Proposal: move
_per_attachment(conn, caller, project, change)intoattached.pyand havelimit._agentpass its change as the callback.everyalso returns whether it is single, from the read it already makes. - Effort S. Risk low: the behaviour is identical.
2. Duplication: "refuse an isolated attachment" in four places; `attached.recheck` copies `_free`
- The check
isolated_since is not None→ lazyfrom maity_hub import isolation→raise isolation.refusal(attachment)appears at: hub/src/maity_hub/auth/caller.py:733-736(settle_in_project)hub/src/maity_hub/routes/bootstrap.py:95-97, afterdeps._refuse_other_project(deps.py:195). Bootstrap is now that helper's only caller.hub/src/maity_hub/access.py:187-196(_attached_in)hub/src/maity_hub/attached.py:46-51(_refuse_isolated)- In attached.py,
recheck's tail (:140-146) repeats_free(:95-101) line for line.free_held(:120,:122) readsattachments()twice in a row. - Proposal: one public
refuse_isolated(attachment)in the caller layer, used by all four.recheckends withreturn _free(live).free_heldreads once and filters. - Effort S. Risk low. Keep the order count, then
not_found, then isolated indeps._resolve.
3. Debt: one revocation reads the credential's live attachments three or four times, by hand
- In
hub/src/maity_hub/auth/revocation.py,revoke: projects_of(:31-38)- the
environment.refreshloop (:87-98) _lead_revoked(:125-135)- then
holder_stalled.revoked(hub/src/maity_hub/holder_stalled.py:161-167) - Each is its own join of
instance_attachmentandmembershipwithdetached_at IS NULL. - More hand-written copies of the same read:
hub/src/maity_hub/runner_revoke.py:62-67, which then callsprojects_of(:69), so it reads againhub/src/maity_hub/routes/projects.py:820-825, a subset ofauth.caller.attachments(caller.py:675)- Proposal: one select builder next to
instances.credential_join/attached_now. That builder must not filter revoked credentials, becauserevokereads after its own UPDATE.revokereads the rows once and passes them to the refresh loop,_lead_revokedandholder_stalled.revoked. - Effort M. Risk low to medium: keep the project lock order (sorted ids, locked before any write).
4. Debt: `GET /runner/placements` (and hello) builds a full agent view per placement and uses two fields
hub/src/maity_hub/placements.py:585:for_runnercalls_agent(:213-234) but uses onlyagent["id"]andagent["handle"](:595-600).- Per placement,
_agentruns: agent_of(a membership read)- the profile read
_name(:237-243):profile_owner→agent_ofagain → the owner → the owner's handleavatars.urls- That is five or six statements per placement on a route every runner polls, all for a name, a display name and a picture the runner never receives.
- Proposal:
for_runnerreads only the id and handle (agent_ofplus one profile read, ornew_handle), ideally batched for the page. Keep_agentfor the views that show the name and picture. - Effort S. Risk low.
5. Debt: `create_placement` reads the named profile's owner three times and runs `_admits` twice
- In
hub/src/maity_hub/routes/placements.py, one POST with{"agent": {"id"}}runs: _lock_named_profile(:466-468, owner)_admitsat:376(two statements)- the deleted check at
:524-528 _admitsagain at:531_admitted(:595-599, owner and handle) plusjoins.live_profile_profile_owner(:610) exists, but the first and third owner reads do not use it.- Proposal: resolve the named profile once, right after the profile lock, into a small record (owner, handle, deleted, active member here). Pass it to the scope check,
_agent_valuesand_admitted. - Effort S. Risk low, as long as "profile lock before project lock" and "never lock another owner's profile" (
hub/context/placements.md) stay as they are.
6. Duplication: the placement effort's shape is defined twice
hub/src/maity_hub/routes/runner.py:75(Effort, pattern^[a-z]{1,16}$)hub/src/maity_hub/routes/placements.py:50(EFFORT_PATTERN, the same pattern, used at:85and:98)- Both must match
placement.effortString(16)in tables.py. - Proposal: one
Effortannotated type inmaity_hub.models, used by both routes. - Effort S. Risk none.