zed/crates/language_models_cloud
Richard Feldman 76aab0c35c
Fix RefCell panic in cloud model token counting (#54188)
Fixes #54140

When `RulesLibrary::count_tokens` calls
`CloudLanguageModel::count_tokens` for Google cloud models, it does so
inside a `cx.update` closure, which holds a mutable borrow on the global
`AppCell`. The Google provider branch then called
`token_provider.auth_context(&cx.to_async())`, which created a new
`AsyncApp` handle and tried to take a shared borrow on the same
`RefCell` — causing a "RefCell already mutably borrowed" panic.

This only affects Google models because they are the only provider that
counts tokens server-side via an HTTP request (requiring
authentication). The other providers (Anthropic, OpenAI, xAI) count
tokens locally using tiktoken, so they never call `auth_context` during
`count_tokens`.

The fix makes `CloudLlmTokenProvider::auth_context` generic over `impl
AppContext` instead of requiring `&AsyncApp`. This allows the
`count_tokens` call site to pass `&App` directly (which reads entities
without re-borrowing the `RefCell`), while all other call sites that
already pass `&AsyncApp` (e.g. `stream_completion`, `refresh_models`)
continue to work unchanged.

Release Notes:

- Fixed a crash ("RefCell already mutably borrowed") that could occur
when counting tokens with Google cloud language models.
2026-04-17 11:03:46 -04:00
..
src Fix RefCell panic in cloud model token counting (#54188) 2026-04-17 11:03:46 -04:00
Cargo.toml language_models: Refactor deps and extract cloud (#53270) 2026-04-07 12:28:19 -03:00
LICENSE-GPL language_models: Refactor deps and extract cloud (#53270) 2026-04-07 12:28:19 -03:00