oliver-zehentleitner / maity
clean-up: forge: one GitHub transport, public read methods, one visibility check and cache
tsk-e77g3nhqqnex Open
From the clean-up audit tsk-hgnbapyw7hnh, Area 3 (Forge and outbound calls), read at 80647565. Line numbers are at 80647565; check each one against current main before changing it. Finding numbers are the audit's (A3-n).
The structural forge items: one GitHub transport, public methods instead of private calls across modules, one visibility check and one cache of the /repos/{r} answer. Medium risk in parts (the §11.4 budget and visibility rules), so kept apart from the small-debt card filed at the same time.
1. Two GitHub API transports, already drifted (A3-1, duplication)
forge.py:528-589(Forge._send) andgithub_app.py:243-275(_call/_open) send the same headers (forge.py:545-552,github_app.py:249-253), detect rate limits the same way (forge.py:566-568,github_app.py:264-267), and map 404 to None and other failures toForgeUnavailableError.github_app.py:259reaches intoforge._openerandforge.TIMEOUT.- The tests copy it too:
FakeOpenerintest_forge.py:24-47and again intest_forge_links.py:108-120. - The drift:
- (a)
github_app._opencatches(OSError, ValueError)(github_app.py:271);forge._sendalso catcheshttp.client.HTTPException(forge.py:582). AnIncompleteReadwhile minting an installation token therefore escapes as an unhandled error, notForgeUnavailableError. This path runs fromforge_access._by_appand from the/me/forgelisting. - (b) The App's calls skip the per-source budget (
_take/_note, §11.4), includingrepositories()(github_app.py:169-179), which reads with an installation token. - Proposal: one module-level request function in forge.py taking the authorization, method, accept type and budget source;
Forge._sendand github_app both use it. One shared fake opener in the tests. - Effort: M. Risk: low to medium. Budget accounting for App calls changes, so tests must cover it.
2. Other modules call Forge's and github_app's private methods (A3-2, design)
forge_access.py:253callsprobe._send(...);ktw_reads.py:215callsreader._get(path, uncached=True) if reader.token else reader._send(path);forge_tokens.py:37callsgithub_app._call("/user", ...);github_app.py:259usesforge._opener(see item 1).- Each caller relies on the internals of the visibility check and the budget layering.
- Proposal: public methods for these reads, e.g.
Forge.repository(...)for an uncached/repos/{r}andGitHubApp.user(token); the underscored ones stay private. - Effort: S to M. Risk: low.
3. Two copies of "is this repository public, asked uncached" (A3-3, duplication)
ktw_reads.py:207-222(_visibility) andforge.py:931-945(Forge.repository_public) both GET/repos/{r}uncached, treat onlyprivate is Falseas public, and treat a refusal as not public. ktw_reads also tellsrepository_movedapart and lets a spent budget through.- Proposal: one
Forge.visibility(repository) -> None | reasonthat keeps the reason and theretry_atraise;repository_publicbecomesvisibility(...) is None, and ktw_reads calls it. - Effort: S. Risk: low.
4. The same block twice inside forge.py (A3-4, duplication)
Forge._calllines 428-435 andForge.require_coveredlines 490-497:_require_public, thenaccess.not_covered, then the switch toowner_reasonunlessrepository_moved.- Proposal: one private
_check_public(repository)both call. - Effort: S. Risk: low.
5. The token-digest cache key is built by hand four times, and one copy differs (A3-5, duplication)
hashlib.sha256((token or "").encode()).hexdigest()atforge.py:476,forge.py:505,forge.py:594andforge_access.py:65.forge._source(forge.py:146-147) is the named helper for the same idea, but maps None to"", not to the hash of""._missing(forge.py:594) keys_visiblebyrepositoryas given;_forgetand_require_public(forge.py:476,505) lower-case it, so one repository can sit in_visibleunder two keys.- Proposal: one
_cache_key(token, repository)helper that always lower-cases. - Effort: S. Risk: low.
6. Two caches hold the same /repos/{r} answer, and a missing ref costs two identical GETs (A3-10, design)
- forge.py keeps
_visible(forge.py:288, 300 s, used by_missingat 591-601) and_public(forge.py:292, 600 s, used by_require_publicat 499-526). Both cache whatGET /repos/{r}answered for one token. - With the operator's token, a ref read that gets a 404 drops the
_publicentry (forge.py:444)._missingthen calls_get("/repos/r"), which goes through_call, whose_require_publicsendsGET /repos/r, and then_sendsendsGET /repos/ragain. So one missing branch, tag or PR costs two identical reads against the budget whenever_visibleis cold. A fresh "public" verdict in_publicalready proves the repository is visible. - Proposal: one cache of the
/repos/{r}answer per token digest. For the operator's token,_missinguses the_require_publicverdict and does not read again. - Effort: M. Risk: medium. It touches the §11.4 refusal and visibility logic, which test_forge.py covers in depth.
Order
- All after tsk-qtr9myhtysbs (in progress), whose item 2 adds a byte cap and a total deadline to
forge._sendandgithub_app. Item 1 then folds that into the one transport. - Item 1 before item 2 (the public methods sit on the shared transport).
- Items 4 and 5 before item 6 (item 6 merges the caches those helpers key).
- tsk-sqpcbndm0tvd and tsk-rzhydxnsav4m (open, other forge hosts) would build on forge.py; if one starts first, coordinate.
Covered elsewhere
- A3-11 (§11.1 promises a total deadline and a body cap the forge reads do not have): tsk-qtr9myhtysbs item 2 adds both. If that card ends with an exemption instead,
docs/api.md:14904-14948and theoutbound.pydocstring need rewording there. - A3-9 (
.replace("Z", "+00:00")inforge._momentandgithub_app._expiry) is a hub-wide pattern; the lead files it on a shared card.
Effort sum: 3S+3M (item 2 counted as M).