oliver-zehentleitner / maity
hub clean-up: audit R5 batch 1 (FULL_NAME fullmatch, IN-list chunking, PR-card lookup, handle batch, lock_projects ...)
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:33FULL_NAME = re.compile(r"^(...)/(...)$"), andnamespace.resolve(namespace.py:159) usesFULL_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) addsproject_named, which runsFULL_NAME.fullmatchbeforenamespace.resolve, for exactly this reason (its docstring says so).- The other callers still use
.match:hub/src/maity_hub/allowed_projects.py:34(soparseaccepts and stores"a/b\n"as a name in the list) andhub/src/maity_hub/routes/agents.py:151.namespace.resolvehas about 30 other callers that get the lax form. - Proposal:
namespace.resolvechecks withfullmatch(orFULL_NAMEloses its anchors and every caller usesfullmatch).attached.project_namedthen becomesnamespace.resolve.allowed_projects.parseandroutes/agents.py:151usefullmatch. - 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-66says it copiesexport.CHUNKbecause 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:240and:272-274- Related:
public_projects.full_names(public_projects.py:151-156) only batchesnamespace.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) withCHUNKandchunks(), used by all four. Batch insidenamespace.full_namesitself and droppublic_projects.full_names. Correct the "mirrored" notes inhub/context/public-pages.mdandreports.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 "Asticker.linked_cardsdoes")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.numberin Python. merged.py adds_pr_number(:100-101) but still writes the expression inline at:193and:251._sharedalso reads everytask_mergedpayload of the project (:195-205) and filters in Python. - Proposal:
cards.pr_number(card)andcards.naming_pr(conn, project_id, number), filtering on the JSON path in SQL asroutes/overview.py:181andrestricted.py:247already do. Same for thetask_mergedpayload. - 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:370also 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_handlesbecomes 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:408runsreports.own_view(conn, row)per row.own_viewisown_views([row])now, so it runs onenamespace.full_namesquery per report.- Proposal:
reports.own_views(conn, rows)once, zipped withrow.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) callsreader._forget(repository), defined athub/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 everyagent_session_projectrow, 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 arehub/tests/test_not_listening_digest.py:504and: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, thenlast_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.