fix(ai): resolve Cloudflare account id from ambient env for key-only credentials#6292
Merged
badlogic merged 2 commits intoJul 11, 2026
Conversation
…entials
Cloudflare Workers AI / AI Gateway resolved provider config from the
credential only, never consulting ambient env for a field the credential
omitted. The coding-agent /login flow stores just the API key, so
CLOUDFLARE_ACCOUNT_ID lives only in the environment; the key-only credential
short-circuited the env lookup, the account id stayed unresolved, and requests
hit the literal {CLOUDFLARE_ACCOUNT_ID} base URL -> 404.
resolveValue now merges per field: prefer the credential value, fall back to
ctx.env(name).
closes earendil-works#6021
Signed-off-by: Mark Phelps <209477+markphelps@users.noreply.github.com>
Contributor
|
This PR was auto-closed. Only contributors approved with Maintainers review auto-closed issues daily. Issues that do not meet the quality bar in CONTRIBUTING.md will not be reopened or receive a reply. If a maintainer replies See CONTRIBUTING.md. |
This was referenced Jul 4, 2026
Collaborator
|
cheers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cloudflare Workers AI (and AI Gateway) still returns
404on 0.80.x even with credentials configured. #6021 was closed by ef231c4 but the failure reproduces for multiple users, because that change fixed a different layer (routing request-scoped auth into resolution) and never addressed the actual cause.Root cause
resolveValueinpackages/ai/src/providers/cloudflare-auth.tsread provider config from the credential only. Once any credential object was present it returnedcredential.env?.[name]and never consulted ambient env:The coding-agent
/loginflow stores only the API key —showApiKeyLoginDialogprompts for the key and writes{ type: "api_key", key }, never capturing the account id. SoCLOUDFLARE_ACCOUNT_IDlives only in the environment. At request time the resolved key is passed as an override,resolveProviderAuthbuilds a synthetic credential from it, andresolveValuethen returnsundefinedfor the account id instead of readingprocess.env. Resolution fails, the rawmodel.baseUrlis used, and the request hits the literal.../accounts/{CLOUDFLARE_ACCOUNT_ID}/ai/v1(URL-encoded to%7B...%7D) → 404.This violates the documented
ApiKeyAuth.resolvecontract, which specifies a per-field merge (credential.env?.NAME ?? env("...")).Fix
resolveValuenow merges per field: prefer the credential value, fall back toctx.env(name). This resolves the account id (and gateway id) from the environment when the credential carries only the API key, and also covers the mirror case (stored account id paired with an env-supplied key).Verification
Reproduced end-to-end through the compat
streampath the coding-agent uses: before the change the outgoing base URL kept the{CLOUDFLARE_ACCOUNT_ID}placeholder; after it, it resolves to the real account id and the request reaches Cloudflare. Added a regression test inpackages/ai/test/providers.test.tscovering a key-only credential with the account id in ambient env.closes #6021