Count the 401 refresh replay against the attempt budget, and amend SPEC §4 to match
Closes #565.
max_retries: 0 documents exactly one attempt and sends two. A refreshable 401 puts a second request on the wire from inside Python's _single_request and Ruby's single_request, governed by its own _retry_count / retry_count rather than the caller's budget. SPEC §14 makes the same promise for the download hop — "disabling retry yields exactly one hop-1 attempt" — so the contradiction was written down in two places and shipping in both.
Why this is a decision, not a re-application
Counting the replay was tried once on #563 (b9662222d) and reverted (aca2c481b). The revert was right. On its own, counting regresses SPEC §4, which says refresh is attempted once per request, tracked "with a boolean (e.g. refresh_attempted) rather than a counter". Gated only by "have we already refreshed", max_retries: 1 refreshed the token and then rethrew the stale 401 without ever sending the refreshed request — for every GET in Python and Ruby, not just downloads. That is worse than the bug it fixed, and #565 was filed rather than smuggling the decision in.
The resolution needs both halves in the same change:
- The replayed wire request counts against the total-attempt budget, preserving the total-attempt semantics #461 settled (observed attempts 4→3).
- SPEC §4 is amended so the refresh is attempted once only when another attempt remains.
The second is what stops the first from regressing §4. With it, there is no state in which the SDK refreshes and then discards the result.
The SPEC §4 wording
Step 2 gains a clause, and the section gains three paragraphs:
- If the token provider supports refresh (
refreshable() == true), refresh has not yet been attempted for this request, and the attempt budget has another attempt left:
The replay spends an attempt. It puts a request on the wire, so it draws from the same total-attempt budget as a transient retry (§7): max_retries counts requests, not failure kinds, and a cap of one means one request no matter what would have caused the second. That is what makes §14's "disabling retry yields exactly ONE hop-1 attempt" literally true — refresh included — and it preserves the total-attempt semantics settled for observed attempts in #461.
Step 2's budget gate is checked before refresh() is called, not after. Refreshing a token the SDK has no budget left to use would burn a rotation for nothing and still hand the caller the stale 401; declining to refresh at all is both cheaper and easier to reason about. The consequence is worth stating plainly: with a budget of one attempt, a refreshable 401 is NOT replayed and surfaces as auth_required. Callers who want the refresh replay must leave an attempt for it.
This gate applies wherever a total-attempt budget governs the path. Go's hand-written mutation path has no such budget — mutations are deliberately single-attempt for transient failures — and keeps its documented mutation-specific single re-attempt after a successful refresh (see §7's Cross-SDK Divergence).
§14 needs no change: its per-SDK budget table already reads "max_retries as total attempts, floored at one (max_retries: 0 still sends one attempt)". It is simply true now.
Scope: which SDKs actually have this
Checked all six rather than assuming.
| SDK | 401 replay in the transport | Change |
|---|---|---|
| Python (sync + async) | in _single_request, own counter | fixed — replays from the retry loop |
| Ruby | in single_request, own counter | fixed — replays from the retry loop |
| Go | main GET loop already counts it: a 401 with a successful refresh returns a retryable error that the loop re-issues as the next attempt, bounded by MaxRetries | none |
| Go (mutations) | doRequestURL's non-GET arm replays outside any budget | none — mutations have no transient-retry budget to draw from; §7's documented divergence, and §4's new gate binds only where a budget governs |
| TypeScript / Kotlin / Swift | none at all | none |
Direct single-request callers keep the in-primitive replay. Python's and Ruby's mutations bypass the retry loop entirely, so without a seam they would lose 401 refresh outright; a refresh_replay: parameter marks which side owns it. In Ruby that also stops handle_error from rotating credentials as a side effect of classifying an error — the refresh now happens where the budget is known.
Red proofs
Against merged main, with git show origin/main:<path> swapped in. Both the plain GET path and the download hop, because the every-GET case is exactly what the earlier revert protected — it is covered by a test here rather than by absence.
Python — uv run pytest tests/test_http.py -k RefreshReplay tests/test_download.py -k refresh_replay against pristine _http.py:
> assert route.call_count == 1
E AssertionError: assert 2 == 1
> assert hop1.call_count == 1
E AssertionError: assert 2 == 1
FAILED tests/test_http.py::TestRefreshReplayAttemptBudget::test_no_budget_means_one_request_and_no_refresh
FAILED tests/test_http.py::TestRefreshReplayAttemptBudget::test_replay_and_transient_retries_share_one_budget
FAILED tests/test_download.py::TestHop1Retry::test_refresh_replay_is_not_attempted_without_budget
3 failed, 7 passed, 101 deselected
Ruby — against pristine http.rb:
The request GET https://3.basecampapi.com/test.json was expected to execute 1 time but it executed 2 times
The request GET https://3.basecampapi.com/test.json was expected to execute 2 times but it executed 3 times
3 runs, 7 assertions, 2 failures, 0 errors, 0 skips
The request GET .../12345/attachments/abc/download/file.txt was expected to execute 1 time but it executed 2 times
3 runs, 9 assertions, 2 failures, 0 errors, 0 skips
test_401_refresh_replay_shares_the_budget_with_transient_retries — "expected 2, executed 3" — is the load-bearing one: at max_retries: 2 a 401 (attempt 1, refresh) followed by a 503 should exhaust the cap at two requests, and the replay used to ride outside it for three.
The remaining new tests pass against main by construction and pin the ceiling rather than the floor: that the refreshed request is actually sent (a 200 body, not a rethrown 401), that only one refresh happens per request, and that mutations keep their in-primitive replay.
Review round
Codex P2 — a refresh network failure escaped the retry loop. Real, and a regression this branch introduced: an exception raised inside an except suite is not offered to that try's sibling handlers (Ruby's rescue behaves the same), so calling refresh() from the AuthError handler put the token endpoint outside the loop's reach. A timing-out refresh ended the request with budget unspent, where before the move it landed in the transient handler and retried.
All three paths now use the directive shape the Swift and Kotlin download loops adopted in #517: the handlers only classify the attempt, and every side effect runs at the loop tail, outside any handler. A refresh that raises is a transient failure of that attempt and retries under the same budget; one that returns false surfaces the original 401; one that succeeds replays immediately with no backoff, since the token is fresh and the server never asked us to wait. Red-proofed in Python and Ruby against the previous commit — the NetworkError propagated raw out of client.get(...) with two attempts still available.
Verification
make check green end to end (REAL_EXIT=0, grep-checked — one run reported "All checks passed!" mid-stream while exiting 2 on a later ruff format step):
| Suite | Result |
|---|---|
| Go | ok all packages |
| TypeScript | 1051 tests passed |
| Python | 792 passed |
| Ruby | 1023 runs, 2348 assertions, 0 failures, 0 errors |
| Kotlin | BUILD SUCCESSFUL |
| Swift | 285 tests, 0 failures |
Conformance: Go 135/0/2, Kotlin 136/0/1, TypeScript 157 passed / 2 skipped, Ruby 126/0/11, Python 137/0/0.
One flake seen locally and not reproducible: tests/client.test.ts > request timeout > propagates a caller-supplied signal through the combined signal failed once in a local make check and passed 3/3 on re-run. This branch changes no TypeScript files at all (git diff origin/main...HEAD -- typescript/ is empty), and the TypeScript CI job is green. Cited numbers are the CI job's.
One aside for whoever hits it next: make go-check first failed here with 557 phantom issues in ../../validation-errors/go/pkg/generated/client.gen.go — a golangci-lint cache shared across worktrees. golangci-lint cache clean cleared it; 0 issues after.