Hold a rate-limited refresh, explain a blocked device login, and read invalid_grant by its code

Merged
JE
2mo ago
+557-132

What

  • A 429 on refresh is held on the login. When a refresh comes back 429, the CLI stores a hold on the login until the Retry-After, using the same hold an agent's mint already keeps. Every later process (another shell, the next run of a scheduled job) answers it locally, without a request. The hold:
    • is capped at MaxServerWait, like the agent's;
    • names the refresh token it was set for, so a login another process has rotated, or a fresh login, isn't held;
    • is cleared by a successful refresh;
    • shows up in auth status under renewal_refused.
  • A device login refused with 429 explains itself. Instead of device authorization failed with status 429, it says: "Basecamp is refusing sign-ins from this address for now; try again after Oct 6 4:40 PM PDT (in 2h)". The hint says that an old copy of a login somewhere else keeps the block going.
  • invalid_grant is read from the SDK's typed refusal (OAuthError, OAuthErrorDescription), not by matching "token error: invalid_grant" in the message. Deleting a stored login no longer depends on how an error is worded.
  • Rename only: MintHold is now RenewalHold (in its own commit, so it can be skipped in review). The stored key stays mint_hold, so old and new versions read each other's holds.

Why

A user locked themselves out. Two containers held copies of one login, and the second refresh revoked it. A scheduled job then retried every two minutes, and bc3's abuse tracker escalated to a 4-hour block on every OAuth endpoint for basecamp-cli from that address. That included device authorization, so they couldn't sign in again either, and all they saw was "status 429". 0.12.0 already forgets a login after its first invalid_grant. This covers the rest:

  • Don't resend into a block that answers every request with a 429.
  • When the way back in is blocked, say so, and say until when.

A request made while a block is active is answered 429 without being counted against the address. So the 30-minute cap costs one request per cap, it never escalates the block, and the CLI notices if the block lifts early.

Depends on

basecamp/basecamp-sdk#972, pinned here at db402d93 (its last code change; later commits there touch only tests) with make bump-sdk REF=db402d937b7482fd6894249613c713a1ca89edd6. That SDK PR is itself stacked on basecamp/basecamp-sdk#971, so the pin is an unreleased pseudo-version until both merge, the same way the pin was held during the event-feed work. The vendored MCP model was re-synced from that commit (provenance only, no model change).

Testing

New tests in internal/auth/refusal_test.go. Each failed before the change:

  • TestInvalidGrant_ReadsTheTypedCode: the typed code is detected, and text that only looks like it isn't.

  • TestRefresh_RateLimitIsHeldOnTheLogin: one request; then a held answer with the remaining wait and nothing sent; then a fresh request once the wait is over.

  • TestRefresh_RateLimitHoldIsCappedButSaysWhatTheServerAsked: a 4-hour Retry-After is held for 30 minutes, and the message gives the server's own deadline.

  • TestRefreshRefusal_ReportsTheHold and TestRefresh_SuccessClearsTheHold.

  • TestRefresh_HoldIsForTheRefusedToken: guard only. It passed before as well, because nothing was held then.

  • TestLoginDevice_RateLimitSaysUntilWhen and TestLoginDevice_RateLimitWithoutRetryAfter.

  • make check passes


Summary by cubic

Holds a rate-limited refresh on the stored login until its Retry-After, so every later process — another shell, the next run of a scheduled job — answers it locally instead of resending a doomed request. This stops a scheduled job with a rotated-away login from hammering Basecamp's abuse block, which can answer every OAuth request from an address for up to a day.

Also makes two related failures readable: a device login refused with a 429 now says "Basecamp is refusing sign-ins from this address for now; try again after …" instead of "device authorization failed with status 429", and invalid_grant is read off the SDK's typed refusal rather than matched in the error message's wording.

Changes

  • Held refreshes are capped at MaxServerWait, named to the refresh token they were set for, cleared by a successful refresh, and reported in auth status under renewal_refused.
  • A stored hold is answered only after the local checks and the token lane are built, so it never masks a credential that could not refresh anyway.
  • The SDK's typed refusal stays in a held refresh's cause, so its request id and OAuth error still reach the error envelope.
  • MintHold is renamed RenewalHold; the stored key stays mint_hold so old and new versions read each other's holds.
  • The SDK is pinned to an unreleased head, with the vendored MCP model and Nix vendor hash re-synced to it, so token-endpoint refusals are typed (basecamp/basecamp-sdk #972).