Skip to content

fix(cloudflare/statestore): report a rejected credential as an authentication error - #3098

Open
pattobrien wants to merge 1 commit into
alchemy-run:mainfrom
pattobrien:fix/state-store-rejected-credential
Open

pattobrien wants to merge 1 commit into
alchemy-run:mainfrom
pattobrien:fix/state-store-rejected-credential

Conversation

@pattobrien

Copy link
Copy Markdown
Contributor

A Cloudflare credential that Cloudflare rejects now fails state store init as an authentication error. Before, it ended as a rate limit with "Run the command again".

Builds on #2605, which made a lookup whose answer is unknown fail instead of bootstrapping.

Cloudflare answers a rejected token with 401 Authentication error four times, then rate limits that token with 429. The SDK retries Authentication error nine times, so the lookup always ended on the 429.

 observeServedVersion
   lookup (listDomains / getScriptSettings / getSubdomain)
+    first Unauthorized is remembered
+    lookup runs again with the SDK's usual retries
+      succeeds            -> carry on (a valid token that blipped)
+      ends TooManyRequests -> fail as the remembered Unauthorized
+      ends any other way   -> fail as that error
   mapError(existenceUnknown)
+    Unauthorized -> "Cloudflare authentication failed while looking up …"
     Forbidden    -> "… Check that the Cloudflare credentials can read Workers scripts"
     otherwise    -> "… Run the command again."

It does not fail fast. A dead token still takes about 24 s. Failing on the first Unauthorized would be quick, but it can turn a valid token into a hard failure: Cloudflare returns Authentication error for valid tokens for a moment under high request concurrency, which is why distilled marks it retryable, and since beta.85 every state store init makes this lookup. So the lookup keeps the SDK's full retries and only the reported error changes. A valid token that blips gets one extra attempt and succeeds as before.

Evidence

CI=true, an empty ALCHEMY_HOME, a well-formed token that was never issued:

  • Before (main, 24.8 s):
    StateStoreError: Could not determine whether Cloudflare State Store 'alchemy-state-store' exists, so it was left untouched: Rate limited. Please wait and consider throttling your request speed. Run the command again.
    
    After (24.6 s):
    StateStoreError: Cloudflare authentication failed while looking up Cloudflare State Store 'alchemy-state-store': Authentication error. Check that the Cloudflare credentials are still valid: an API token may have expired or been revoked.
    
  • A store that is really missing (valid token, 0.6 s), unchanged:
    StateStoreError: Cloudflare State Store 'doesnotexist' not found. Run 'alchemy state bootstrap --backend cloudflare --name doesnotexist --profile <your-ci-profile>' first, or pass --yes.
    
  • Lifecycle.test.ts › "a rejected Cloudflare credential fails as an authentication error" runs the real init against Cloudflare with a random token. It passes in 24.3 s.
  • TransientErrors.test.ts › keepCredentialRejection drives the real workers.getSubdomain against stubbed replies:
    401 401 401 401 429…  -> fails Unauthorized "Authentication error"   (fails TooManyRequests without the fix)
    401 401 401 200       -> succeeds, 4 calls
    429…                  -> fails TooManyRequests
    

Merge Danger

Door: two-way

Reverting restores the previous message. No state or stored data changes.

Blast Radius: narrow

Only the Cloudflare state store's existence lookups. A lookup that hits Unauthorized once and then succeeds makes one more request than before. A run that starts while Cloudflare is already rate limiting the same dead token (another job used it in the last few minutes) sees only 429s and still gets "Rate limited … Run the command again".

@alchemy-version-bot

alchemy-version-bot Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Install the packages built from this commit:

alchemy

pnpm install https://pkg.alchemy.run/alchemy/pr:3098:275bfd8
@alchemy.run (21)
pnpm install https://pkg.alchemy.run/@alchemy.run/better-auth/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/git/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/drizzle/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/acme/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/planetscale/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/neon/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/hetzner/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/railway/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/fly/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/cloudflare/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/gcp/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/aws/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/domain/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/kubernetes/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/docker/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/redis/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/sql/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/cloudflare-runtime/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/frontend-frameworks/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/node-utils/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/pkg/pr:3098:275bfd8
Transitive Dependencies (17)
pnpm install https://pkg.alchemy.run/@distilled.cloud/aws/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@distilled.cloud/acme/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@distilled.cloud/core/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@distilled.cloud/planetscale/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@distilled.cloud/neon/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@distilled.cloud/s3/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@distilled.cloud/hetzner/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@distilled.cloud/railway/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@distilled.cloud/fly-io/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@distilled.cloud/github/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@distilled.cloud/aws4signer/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@distilled.cloud/cloudflare/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@distilled.cloud/iceberg/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@distilled.cloud/gcp/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/effect-cluster/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@alchemy.run/rivet/pr:3098:275bfd8
pnpm install https://pkg.alchemy.run/@distilled.cloud/rivet/pr:3098:275bfd8

Published Nov 25, 2026, 3:40 PM UTC. Expires Dec 2, 2026, 3:40 PM UTC, extended while this pull request is open.

…tication error

Cloudflare answers a rejected credential with 401 'Authentication error' four times and then rate limits it, so a state store lookup retried by the SDK ended as 'Rate limited ... Run the command again'. The lookup now keeps the rejection and names authentication as the problem. It is still retried as before, because a valid token can be rejected for a moment under load.
@pattobrien
pattobrien force-pushed the fix/state-store-rejected-credential branch from 132a5b7 to 275bfd8 Compare November 25, 2026 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant