oliver-zehentleitner / maity

clean-up: forge and outbound: test-fake compatibility path, secret_key_missing helper, header scanning, narrow excepts, leftovers, replicas.md

Sign in to report

tsk-1mkd2n9x3phr 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).

Small, low-risk debt around the forge modules. The structural forge items (one transport, one visibility cache) are on the sibling card filed at the same time.

1. A compatibility path in the Forge read API that exists only for test fakes (A3-6, debt)

  • forge._raw (forge.py:344-347) says it is there so that "the reads of JSON call exactly as they did before raw reads existed". Forge._get (forge.py:401-407) has four branches for the same reason.
  • Fakes with the old signatures: test_forge_access.py:69 and test_instance_namespaces.py:48 (lambda self, path, query=None); test_contract_text.py:62, test_merged_closes.py:35 and test_overview.py:300 (lambda _self, path).
  • Proposal: the fakes take **_, the code always passes raw_cap=raw_cap, _raw goes, and _get becomes two branches.
  • Effort: S. Risk: low (test-only churn).

2. The secret_key_missing 409 is raised in three places (A3-7, duplication)

  • forge_tokens.py:92-95 (require_key), forge_webhook.py:308-309 (inline in rotate), auth/second_factor.py:335. Same status, code and message.
  • Proposal: one helper (e.g. forge_tokens.require_key moved to errors.py or secretbox.py) used by all three.
  • Effort: S. Risk: low.

3. ASGI request headers are scanned by hand four times (A3-8, duplication)

  • net.py:34-41 (_values); forge_webhook.py:67-85 (_headers, _header, _declared); middleware.py:514-521 (_content_length); join_host.py:48-53 (requested_host).
  • Two of them parse Content-Length by different rules: forge_webhook._declared refuses a repeated or non-digit value; middleware._content_length takes the first value through int(), which also accepts a sign or surrounding spaces.
  • Proposal: one net.header_values(scope, name) and one Content-Length parser, used by all four. The webhook's strict rule must stay, and should become the rule for both.
  • Effort: S to M. Risk: low.

4. Small leftovers in forge_access.py and forge_webhook.py (A3-13, debt)

  • ForgeAccess._bound (forge_access.py:265-281) and ForgeAccess._refusing (forge_access.py:283-284) take a base: forge.Forge parameter neither uses.
  • forge_webhook.py:148 has # type: ignore[union-attr] on PATH.match(...).group(1). matches() (forge_webhook.py:63-64, called from middleware.py:180) has already run the same regex.
  • Proposal: drop base from both methods. Have matches() return the slug (or None) and pass it to handle, so the ignore goes.
  • Effort: S. Risk: low.

5. Broad except Exception around the shared budget and probe_via writes (A3-14, debt)

  • shared_budget.py:48, shared_budget.py:99 and forge_access.py:132 catch Exception and log a warning. The decision never to fail a forge read over the shared row is recorded in hub/context/replicas.md:454-458 and the shared_budget docstring; that intent only needs database errors caught. Today a programming error in the upsert (a wrong column, a type error) is swallowed as a warning and budget sharing silently stops.
  • Proposal: narrow to sqlalchemy.exc.SQLAlchemyError (plus OSError if needed for connect failures).
  • Effort: S. Risk: low. A missed exception class would surface as a failed forge read, so add one test with the database down.

6. The Source line in hub/context/replicas.md:435 cites stale lines and a function that does not exist (A3-12, docs drift)

  • forge.py:125-126 (_budgets) is now 129-130; forge.py:246-250 (_visible, _public) is 288 and 292; forge_access.py:62 (_chosen) is 94; forge_access.py:258-277 (_last_via, probe_via) is 318-357; views.py:185 is 226; releases.py:190 is 192. forge_links.forget_token does not exist; the function is github_app.forget_token (github_app.py:186).
  • Proposal: cite symbols, not line numbers, and fix the function name.
  • Effort: S. Risk: none.

Order

  • Item 2: auth/second_factor.py is being changed by tsk-8mgb85ne0nrq; touch it after that lands.
  • Item 3: middleware.py is also edited by tsk-j967n09yz6ps (items 7 and 9); rebase rather than conflict.
  • Item 1 touches forge.py like the sibling card; do it first or in the same PR as the sibling's item 1.
  • Item 6 last, after the sibling card has moved the lines it cites.

Covered elsewhere

  • None of these six. Searched: secret_key_missing, shared_budget, content-length, forge_webhook, replicas.md, _raw.

Effort sum: 6S (item 3 may grow to M).