cloud_api_client: Key the cached LlmApiToken by organization - #58915
Merged
Conversation
tomhoule
force-pushed
the
tomhoule-zpwtwnynmqxo
branch
from
June 9, 2026 11:46
ce800ae to
0830065
Compare
tomhoule
force-pushed
the
tomhoule-zpwtwnynmqxo
branch
from
June 9, 2026 12:19
0830065 to
74800d6
Compare
tomhoule
marked this pull request as ready for review
June 9, 2026 12:33
maxdeviant
reviewed
Jun 9, 2026
tomhoule
force-pushed
the
tomhoule-zpwtwnynmqxo
branch
2 times, most recently
from
June 9, 2026 12:59
b1fda56 to
2bf96d3
Compare
LLM tokens are organization-specific: the organization id in the JWT claims. Before this commit, the `LlmApiToken` cache was an `Option<String>`, and `cached()` ignored the `organization_id` argument on cache hit. Correctness relied on `RefreshLlmTokenListener` clearing and refreshing the token when it observed an `OrganizationChanged` event. That leaves two windows where a token minted for the wrong organization could be served from the cache: - the refresh runs in a spawned task, so between the organization switch and the task acquiring the write lock, any request calling `cached()` would get the previous organization's token. - `Client::authenticated_llm_request` snapshots the organization id at call time; if the server demanded a token refresh after the user had switched organizations mid-request, the retry path would mint a fresh token for the old organization and write it into the shared cache, poisoning it for all subsequent requests until the next refresh event. The fix is to store the organization id corresponding to the token next to it in the cache, and to check that the cached token is for the correct organization in `cached()`. Release Notes: - Fixed a race where LLM requests made immediately after switching organizations could be attributed to the previously selected organization.
tomhoule
force-pushed
the
tomhoule-zpwtwnynmqxo
branch
from
June 9, 2026 13:02
2bf96d3 to
e37c5b9
Compare
tomhoule
commented
Jun 9, 2026
| token: String, | ||
| } | ||
|
|
||
| impl fmt::Debug for CachedLlmApiToken { |
Contributor
Author
There was a problem hiding this comment.
Adding this to avoid tokens ending up in logs, but obviously that's orthogonal to this PR's purpose.
maxdeviant
approved these changes
Jun 9, 2026
This was referenced Jun 18, 2026
Closed
This was referenced Jul 1, 2026
This was referenced Jul 10, 2026
jonx
pushed a commit
to jonx/zed-aros
that referenced
this pull request
Jul 17, 2026
…ustries#58915) The tokens are organization-specific (the organization id in the JWT claims). Before this commit, the `LlmApiToken` cache was an `Option<String>`, and `cached()` ignored the `organization_id` argument on cache hit. Correctness relied on `RefreshLlmTokenListener` clearing and refreshing the token when it observed an `OrganizationChanged` event. That leaves two windows where a token minted for the wrong organization could be served from the cache: - the refresh runs in a spawned task, so between the organization switch and the task acquiring the write lock, any request calling `cached()` would get the previous organization's token. - `Client::authenticated_llm_request` snapshots the organization id at call time; if the server demanded a token refresh after the user had switched organizations mid-request, the retry path would mint a fresh token for the old organization and write it into the shared cache, poisoning it for all subsequent requests until the next refresh event. The fix is to store the organization id corresponding to the token next to it in the cache, and to check that the cached token is for the correct organization in `cached()`. Release Notes: - Fixed a race where LLM and edit prediction requests made immediately after switching organizations could be attributed to the previously selected organization.
jolutz
pushed a commit
to jolutz/zed
that referenced
this pull request
Aug 8, 2026
…ustries#58915) The tokens are organization-specific (the organization id in the JWT claims). Before this commit, the `LlmApiToken` cache was an `Option<String>`, and `cached()` ignored the `organization_id` argument on cache hit. Correctness relied on `RefreshLlmTokenListener` clearing and refreshing the token when it observed an `OrganizationChanged` event. That leaves two windows where a token minted for the wrong organization could be served from the cache: - the refresh runs in a spawned task, so between the organization switch and the task acquiring the write lock, any request calling `cached()` would get the previous organization's token. - `Client::authenticated_llm_request` snapshots the organization id at call time; if the server demanded a token refresh after the user had switched organizations mid-request, the retry path would mint a fresh token for the old organization and write it into the shared cache, poisoning it for all subsequent requests until the next refresh event. The fix is to store the organization id corresponding to the token next to it in the cache, and to check that the cached token is for the correct organization in `cached()`. Release Notes: - Fixed a race where LLM and edit prediction requests made immediately after switching organizations could be attributed to the previously selected organization.
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.
The tokens are organization-specific (the organization id in the JWT claims). Before this commit, the
LlmApiTokencache was anOption<String>, andcached()ignored theorganization_idargument on cache hit. Correctness relied onRefreshLlmTokenListenerclearing and refreshing the token when it observed anOrganizationChangedevent.That leaves two windows where a token minted for the wrong organization could be served from the cache:
cached()would get the previous organization's token.Client::authenticated_llm_requestsnapshots the organization id at call time; if the server demanded a token refresh after the user had switched organizations mid-request, the retry path would mint a fresh token for the old organization and write it into the shared cache, poisoning it for all subsequent requests until the next refresh event.The fix is to store the organization id corresponding to the token next to it in the cache, and to check that the cached token is for the correct organization in
cached().Release Notes: