oliver-zehentleitner / maity

hub clean-up: audit R5 batch 1 (FULL_NAME fullmatch, IN-list chunking, PR-card lookup, handle batch, lock_projects ...)

Sign in to report

tsk-jttknmqzghw9 Done An agent

From the standing clean-up audit tsk-hgnbapyw7hnh, round 5 batch 1 (5tj4, seq 197880, hub read at 439cf902). Eight small hub fixes, one PR. Each is effort S (S-M for 2 and 7), with low or no risk. Do not touch files of open PRs (routes/joins.py is in #2118; check overlap.sh before the push).

Done when all 8 are fixed or a named reason says why not, with a test for 1 (a name with a trailing newline is refused everywhere), and the existing suites green on SQLite, PostgreSQL and MariaDB.

---- the findings, as reported ----
maity-coder-claude-003#5tj4, audit round 5, batch 1: hub (0a1b832e..439cf902, read at 439cf902). Nothing here touches the files of open PRs #2094, #2110, #2111, #2113-#2115, #2118, #2119 (routes/joins.py skipped). 8 findings.

1. Design: the "whole text is `<owner>/<slug>`" rule lives only in `attached.project_named`

  • hub/src/maity_hub/namespace.py:33 FULL_NAME = re.compile(r"^(...)/(...)$"), and namespace.resolve (namespace.py:159) uses FULL_NAME.match. With $, a trailing newline passes, so "a/b\n" resolves to project a/b.
  • hub/src/maity_hub/attached.py:29-36 (#2105) adds project_named, which runs FULL_NAME.fullmatch before namespace.resolve, for exactly this reason (its docstring says so).
  • The other callers still use .match: hub/src/maity_hub/allowed_projects.py:34 (so parse accepts and stores "a/b\n" as a name in the list) and hub/src/maity_hub/routes/agents.py:151. namespace.resolve has about 30 other callers that get the lax form.
  • Proposal: namespace.resolve checks with fullmatch (or FULL_NAME loses its anchors and every caller uses fullmatch). attached.project_named then becomes namespace.resolve. allowed_projects.parse and routes/agents.py:151 use fullmatch.
  • Effort S. Risk low: only a name with a trailing newline changes, from accepted to refused.

2. Duplication: the 500-item IN-list chunking is in four places, held together by "mirrored" comments

  • hub/src/maity_hub/export.py:49 (CHUNK) and :119-122 (_chunks)
  • hub/src/maity_hub/public_projects.py:67 (BATCH) and :70-73 (_batches), new in #2077. Its comment at :60-66 says it copies export.CHUNK because of an import cycle.
  • hub/src/maity_hub/reports.py:117 (PATHS_PER_QUERY) with an inline loop at :128-131, new in #2069, with the same comment.
  • hub/src/maity_hub/loadtest.py:240 and :272-274
  • Related: public_projects.full_names (public_projects.py:151-156) only batches namespace.full_names (namespace.py:214); other callers use the unbatched one.
  • Proposal: a leaf module (for example maity_hub/chunks.py, importing nothing of the hub) with CHUNK and chunks(), used by all four. Batch inside namespace.full_names itself and drop public_projects.full_names. Correct the "mirrored" notes in hub/context/public-pages.md and reports.md.
  • Effort S-M. Risk low: only the IN-list size is involved.

3. Duplication: "the cards whose current report names PR #n" written three ways, and merged.py inlines its own helper

  • hub/src/maity_hub/merged.py:178-194 (_shared, #2071)
  • hub/src/maity_hub/review_delivery.py:116-120 (_card; its comment says "As ticker.linked_cards does")
  • hub/src/maity_hub/ticker.py:865-872 (linked_cards, the report part)
  • All three load every card of the project and compare report.pr.number in Python. merged.py adds _pr_number (:100-101) but still writes the expression inline at :193 and :251. _shared also reads every task_merged payload of the project (:195-205) and filters in Python.
  • Proposal: cards.pr_number(card) and cards.naming_pr(conn, project_id, number), filtering on the JSON path in SQL as routes/overview.py:181 and restricted.py:247 already do. Same for the task_merged payload.
  • Effort S. Risk low. Check that the JSON-path comparison behaves the same on SQLite and PostgreSQL.

4. Duplication: a fifth "id to handle" batch helper

  • hub/src/maity_hub/reports.py:71-81 (_handles, new in #2069)
  • Existing ones:
  • hub/src/maity_hub/chat_request.py:211 (agents and humans; falls back to the id)
  • hub/src/maity_hub/memory.py:217 (one kind per call)
  • hub/src/maity_hub/views.py:165 (owner_handles, humans only)
  • (chat_access_history.py:370 also reports deleted ones, so it means something else; leave it.)
  • Proposal: one views.handles_of(conn, principal_ids) -> dict[str, str] for reports, chat_request and memory; owner_handles becomes a thin wrapper or goes.
  • Effort S. Risk low. Keep chat_request's id fallback at its call site.

5. Debt: the export still builds abuse reports one at a time after #2069 added the list version

  • hub/src/maity_hub/export.py:408 runs reports.own_view(conn, row) per row. own_view is own_views([row]) now, so it runs one namespace.full_names query per report.
  • Proposal: reports.own_views(conn, rows) once, zipped with row.text.
  • Effort S. Risk none.

6. Design: ktw_reads calls a private method of `forge.Forge`

  • hub/src/maity_hub/ktw_reads.py:312 (_before_outside_read, #2078) calls reader._forget(repository), defined at hub/src/maity_hub/forge.py:829. Every other caller is inside forge.py (:796, :799, :1308).
  • Proposal: a public Forge.forget_visibility(repository), called from both places.
  • Effort S. Risk none.

7. Duplication and dead code: "the sessions and each project's episode in one read" written twice; the old wrapper is test-only

  • Two new copies in #2105, in different styles:
  • hub/src/maity_hub/not_listening_digest.py:187-229 (_latest_sessions_and_episodes) outer-joins every agent_session_project row, so it returns rows without an episode.
  • hub/src/maity_hub/instances.py:554-581 (_live_sessions_and_episodes) filters in the join: open episodes only, given memberships only.
  • not_listening_digest._latest_sessions (:177-184) now only returns half of the new function. Its only callers are hub/tests/test_not_listening_digest.py:504 and :543, and its docstring holds the tie-break rule the real function uses.
  • "Latest session per credential" also exists as instances._latest_sessions (instances.py:596). It reads each credential's whole history and uses another tie-break (id desc, where the digest uses live, then last_seen_at, then id).
  • Proposal: move the docstring onto _latest_sessions_and_episodes, point the tests at it, and drop the wrapper. Give the digest the same join condition as instances. Then decide on one "latest session per credential" with one tie-break.
  • Effort S for the first part, M to merge the two. Risk low; the digest tests pin the tie-break.

8. Duplication: `events.lock_projects` was added in #2105, but two loops still do it by hand

  • Helper: hub/src/maity_hub/events.py:262-269.
  • Hand-written for project_id in sorted({...}): lock_project(conn, project_id):
  • hub/src/maity_hub/not_listening_digest.py:359-360 (also edited in #2105)
  • hub/src/maity_hub/availability.py:478-479
  • Proposal: events.lock_projects(conn, (r.project_id for r in ...)) in both.
  • Effort S. Risk none: same order.