oliver-zehentleitner / maity

clean-up: forge: one GitHub transport, public read methods, one visibility check and cache

Sign in to report

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) and github_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 to ForgeUnavailableError. github_app.py:259 reaches into forge._opener and forge.TIMEOUT.
  • The tests copy it too: FakeOpener in test_forge.py:24-47 and again in test_forge_links.py:108-120.
  • The drift:
  • (a) github_app._open catches (OSError, ValueError) (github_app.py:271); forge._send also catches http.client.HTTPException (forge.py:582). An IncompleteRead while minting an installation token therefore escapes as an unhandled error, not ForgeUnavailableError. This path runs from forge_access._by_app and from the /me/forge listing.
  • (b) The App's calls skip the per-source budget (_take / _note, §11.4), including repositories() (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._send and 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:253 calls probe._send(...); ktw_reads.py:215 calls reader._get(path, uncached=True) if reader.token else reader._send(path); forge_tokens.py:37 calls github_app._call("/user", ...); github_app.py:259 uses forge._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} and GitHubApp.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) and forge.py:931-945 (Forge.repository_public) both GET /repos/{r} uncached, treat only private is False as public, and treat a refusal as not public. ktw_reads also tells repository_moved apart and lets a spent budget through.
  • Proposal: one Forge.visibility(repository) -> None | reason that keeps the reason and the retry_at raise; repository_public becomes visibility(...) is None, and ktw_reads calls it.
  • Effort: S. Risk: low.

4. The same block twice inside forge.py (A3-4, duplication)

  • Forge._call lines 428-435 and Forge.require_covered lines 490-497: _require_public, then access.not_covered, then the switch to owner_reason unless repository_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() at forge.py:476, forge.py:505, forge.py:594 and forge_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 _visible by repository as given; _forget and _require_public (forge.py:476, 505) lower-case it, so one repository can sit in _visible under 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 _missing at 591-601) and _public (forge.py:292, 600 s, used by _require_public at 499-526). Both cache what GET /repos/{r} answered for one token.
  • With the operator's token, a ref read that gets a 404 drops the _public entry (forge.py:444). _missing then calls _get("/repos/r"), which goes through _call, whose _require_public sends GET /repos/r, and then _send sends GET /repos/r again. So one missing branch, tag or PR costs two identical reads against the budget whenever _visible is cold. A fresh "public" verdict in _public already proves the repository is visible.
  • Proposal: one cache of the /repos/{r} answer per token digest. For the operator's token, _missing uses the _require_public verdict 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._send and github_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-14948 and the outbound.py docstring need rewording there.
  • A3-9 (.replace("Z", "+00:00") in forge._moment and github_app._expiry) is a hub-wide pattern; the lead files it on a shared card.

Effort sum: 3S+3M (item 2 counted as M).