oliver-zehentleitner / maity

hub clean-up: audit R5 batch 2 (attachment loop twice, placements/instances)

Sign in to report

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 with in_attachment that re-raises through attached.in_project unless single, then members_outcome.
  • That sequence also reads the attachments three times (every, single through _live, recheck).
  • Proposal: move _per_attachment(conn, caller, project, change) into attached.py and have limit._agent pass its change as the callback. every also 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 → lazy from 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, after deps._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) reads attachments() twice in a row.
  • Proposal: one public refuse_isolated(attachment) in the caller layer, used by all four. recheck ends with return _free(live). free_held reads once and filters.
  • Effort S. Risk low. Keep the order count, then not_found, then isolated in deps._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.refresh loop (: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_attachment and membership with detached_at IS NULL.
  • More hand-written copies of the same read:
  • hub/src/maity_hub/runner_revoke.py:62-67, which then calls projects_of (:69), so it reads again
  • hub/src/maity_hub/routes/projects.py:820-825, a subset of auth.caller.attachments (caller.py:675)
  • Proposal: one select builder next to instances.credential_join/attached_now. That builder must not filter revoked credentials, because revoke reads after its own UPDATE. revoke reads the rows once and passes them to the refresh loop, _lead_revoked and holder_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_runner calls _agent (:213-234) but uses only agent["id"] and agent["handle"] (:595-600).
  • Per placement, _agent runs:
  • agent_of (a membership read)
  • the profile read
  • _name (:237-243): profile_owner → agent_of again → the owner → the owner's handle
  • avatars.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_runner reads only the id and handle (agent_of plus one profile read, or new_handle), ideally batched for the page. Keep _agent for 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)
  • _admits at :376 (two statements)
  • the deleted check at :524-528
  • _admits again at :531
  • _admitted (:595-599, owner and handle) plus joins.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_values and _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 :85 and :98)
  • Both must match placement.effort String(16) in tables.py.
  • Proposal: one Effort annotated type in maity_hub.models, used by both routes.
  • Effort S. Risk none.