Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Correct Copilot token-limit metadata (#64195)

MartinYe1234739fdbef762f
diff --git a/crates/agent/src/thread.rs b/crates/agent/src/thread.rs
index 39f99aac0ca..884a87b123b 100644
--- a/crates/agent/src/thread.rs
+++ b/crates/agent/src/thread.rs
@@ -2434,6 +2434,16 @@ impl Thread {
self.cumulative_token_usage
}
+ /// Maximum input after reserving the selected model's output allowance.
+ pub fn input_token_capacity(&self) -> Option<u64> {
+ let model = self.model()?;
+ Some(compaction_input_capacity(
+ model.max_input_tokens(),
+ model.max_total_tokens(),
+ model.max_output_tokens(),
+ ))
+ }
+
pub fn latest_token_usage(&self) -> Option<acp_thread::TokenUsage> {
let usage = self.latest_request_token_usage()?;
let model = self.model()?;
@@ -4392,7 +4402,7 @@ impl Thread {
let model = self.model()?;
let auto_compact = AgentSettings::get_global(cx).auto_compact;
let max_tokens = model.max_token_count();
- let max_input_tokens = max_tokens.saturating_sub(model.max_output_tokens().unwrap_or(0));
+ let max_input_tokens = self.input_token_capacity()?;
let tokens_before = self
.latest_request_token_usage()
.map(|usage| total_input_tokens(usage).saturating_add(usage.output_tokens));
@@ -4431,10 +4441,7 @@ impl Thread {
return None;
}
- let model = self.model()?;
- let max_token_count = model.max_token_count();
- let max_input_tokens =
- max_token_count.saturating_sub(model.max_output_tokens().unwrap_or(0));
+ let max_input_tokens = self.input_token_capacity()?;
// Models with a small context window don't leave enough headroom for a
// compaction pass; the UI warns the user about the token limit instead.
if max_input_tokens < MIN_COMPACTION_CONTEXT_WINDOW {
@@ -4600,6 +4607,17 @@ fn total_input_tokens(usage: language_model::TokenUsage) -> u64 {
.saturating_add(usage.cache_read_input_tokens)
}
+/// Reserves output without subtracting it from an independent input ceiling.
+fn compaction_input_capacity(
+ input_limit: u64,
+ combined_limit: Option<u64>,
+ output_limit: Option<u64>,
+) -> u64 {
+ combined_limit.map_or(input_limit, |combined| {
+ input_limit.min(combined.saturating_sub(output_limit.unwrap_or(0)))
+ })
+}
+
fn auto_compact_threshold_token_count(
threshold: AutoCompactThreshold,
max_token_count: u64,
@@ -6883,6 +6901,30 @@ mod tests {
use settings::LanguageModelProviderSetting;
use std::sync::Arc;
+ #[test]
+ fn compaction_capacity_respects_prompt_and_combined_limits() {
+ assert_eq!(
+ compaction_input_capacity(90_000, Some(200_000), Some(16_384)),
+ 90_000
+ );
+ assert_eq!(
+ compaction_input_capacity(128_000, Some(128_000), Some(64_000)),
+ 64_000
+ );
+ assert_eq!(
+ compaction_input_capacity(90_000, None, Some(64_000)),
+ 90_000
+ );
+ assert_eq!(
+ compaction_input_capacity(2_000, Some(2_000), Some(3_000)),
+ 0
+ );
+ assert_eq!(
+ compaction_input_capacity(128_000, Some(128_000), None),
+ 128_000
+ );
+ }
+
async fn setup_thread_for_test(cx: &mut TestAppContext) -> (Entity<Thread>, ThreadEventStream) {
cx.update(|cx| {
let settings_store = settings::SettingsStore::test(cx);
@@ -7191,6 +7233,47 @@ mod tests {
});
}
+ #[gpui::test]
+ async fn test_compaction_threshold_respects_independent_input_limit(cx: &mut TestAppContext) {
+ let (thread, _event_stream) = setup_thread_for_test(cx).await;
+ let mut model = FakeLanguageModel::default();
+ model.set_max_token_count(200_000);
+ model.set_max_input_tokens(90_000);
+ model.set_max_output_tokens(Some(16_384));
+ let model = Arc::new(model);
+ let user_message_id = ClientUserMessageId::new();
+
+ cx.update(|cx| {
+ set_auto_compact_settings(
+ cx,
+ agent_settings::AutoCompactSettings {
+ enabled: true,
+ threshold: AutoCompactThreshold::Percentage(0.9),
+ },
+ );
+ thread.update(cx, |thread, cx| {
+ thread.set_model(model, cx);
+ thread.messages.push(user_text_message(
+ user_message_id.clone(),
+ "near the independent input limit",
+ ));
+ for (input_tokens, expected_target) in [(80_999, None), (81_000, Some(1))] {
+ thread.request_token_usage.insert(
+ user_message_id.clone(),
+ language_model::TokenUsage {
+ input_tokens,
+ ..Default::default()
+ },
+ );
+
+ assert_eq!(thread.compaction_message_target_ix(cx), expected_target);
+ assert_eq!(thread.input_token_capacity(), Some(90_000));
+ assert_eq!(thread.latest_token_usage().unwrap().max_tokens, 200_000);
+ }
+ });
+ });
+ }
+
#[gpui::test]
async fn test_compaction_threshold_respects_enabled_setting(cx: &mut TestAppContext) {
let (thread, _event_stream) = setup_thread_for_test(cx).await;
diff --git a/crates/agent_ui/src/conversation_view/thread_view.rs b/crates/agent_ui/src/conversation_view/thread_view.rs
index 95b4b4fdb1a..e5a862e7529 100644
--- a/crates/agent_ui/src/conversation_view/thread_view.rs
+++ b/crates/agent_ui/src/conversation_view/thread_view.rs
@@ -4762,13 +4762,20 @@ impl ThreadView {
let workspace = self.workspace.clone();
- let max_output_tokens = self
+ let (max_input_tokens, max_output_tokens) = self
.as_native_thread(cx)
- .and_then(|thread| thread.read(cx).model())
- .and_then(|model| model.max_output_tokens())
- .unwrap_or(0);
- let input_max_label =
- crate::humanize_token_count(usage.max_tokens.saturating_sub(max_output_tokens));
+ .map(|thread| {
+ let thread = thread.read(cx);
+ (
+ thread.input_token_capacity().unwrap_or(usage.max_tokens),
+ thread
+ .model()
+ .and_then(|model| model.max_output_tokens())
+ .unwrap_or(0),
+ )
+ })
+ .unwrap_or((usage.max_tokens, 0));
+ let input_max_label = crate::humanize_token_count(max_input_tokens);
let output_max_label = crate::humanize_token_count(max_output_tokens);
let build_tooltip = {
@@ -4804,7 +4811,7 @@ impl ThreadView {
};
if show_split {
- let input_max_raw = usage.max_tokens.saturating_sub(max_output_tokens);
+ let input_max_raw = max_input_tokens;
let output_max_raw = max_output_tokens;
let input_ratio = if input_max_raw > 0 {
diff --git a/crates/copilot_chat/src/copilot_chat.rs b/crates/copilot_chat/src/copilot_chat.rs
index 71ef0895d96..9b9b208a1b7 100644
--- a/crates/copilot_chat/src/copilot_chat.rs
+++ b/crates/copilot_chat/src/copilot_chat.rs
@@ -279,6 +279,11 @@ impl Model {
self.capabilities.limits.max_context_window_tokens as u64
}
+ pub fn max_prompt_tokens(&self) -> Option<u64> {
+ let limit = self.capabilities.limits.max_prompt_tokens;
+ (limit > 0).then_some(limit)
+ }
+
pub fn max_output_tokens(&self) -> Option<u64> {
let limit = self.capabilities.limits.max_output_tokens as u64;
(limit > 0).then_some(limit)
diff --git a/crates/copilot_chat/src/model.rs b/crates/copilot_chat/src/model.rs
index 12d524c318a..5d2ea914a53 100644
--- a/crates/copilot_chat/src/model.rs
+++ b/crates/copilot_chat/src/model.rs
@@ -130,6 +130,16 @@ impl LanguageModel for CopilotChatLanguageModel {
self.model.max_token_count()
}
+ fn max_input_tokens(&self) -> u64 {
+ self.model
+ .max_prompt_tokens()
+ .unwrap_or_else(|| self.model.max_token_count())
+ }
+
+ fn max_output_tokens(&self) -> Option<u64> {
+ self.model.max_output_tokens()
+ }
+
fn stream_completion(
&self,
request: LanguageModelRequest,
@@ -1301,6 +1311,85 @@ mod tests {
use language_model::ProviderErrorCategory;
use serde_json::json;
+ #[gpui::test]
+ fn language_model_exposes_token_limits(cx: &mut gpui::TestAppContext) {
+ use gpui::AppContext as _;
+
+ let copilot_chat = cx.new(|cx| {
+ CopilotChat::new(
+ Arc::new(http_client::BlockedHttpClient::new()),
+ Arc::new(EmptyCredentialsProvider),
+ crate::CopilotChatConfiguration::default(),
+ cx,
+ )
+ });
+ for (limits, input, context, output) in [
+ (
+ json!({"max_context_window_tokens": 200_000, "max_prompt_tokens": 90_000, "max_output_tokens": 16_384}),
+ 90_000,
+ 200_000,
+ Some(16_384),
+ ),
+ (
+ json!({"max_context_window_tokens": 128_000, "max_output_tokens": 4096}),
+ 128_000,
+ 128_000,
+ Some(4096),
+ ),
+ (
+ json!({"max_context_window_tokens": 128_000, "max_prompt_tokens": 100_000}),
+ 100_000,
+ 128_000,
+ None,
+ ),
+ (
+ json!({"max_context_window_tokens": 128_000, "max_prompt_tokens": 0, "max_output_tokens": 0}),
+ 128_000,
+ 128_000,
+ None,
+ ),
+ ] {
+ let mut value = serde_json::to_value(test_responses_model()).unwrap();
+ value["capabilities"]["limits"] = limits;
+ let descriptor = serde_json::from_value(value).unwrap();
+ let model = create_language_model(descriptor, copilot_chat.clone());
+ assert_eq!(model.max_token_count(), context);
+ assert_eq!(model.max_input_tokens(), input);
+ assert_eq!(model.max_total_tokens(), Some(context));
+ assert_eq!(model.max_output_tokens(), output);
+ }
+ }
+
+ struct EmptyCredentialsProvider;
+
+ impl credentials_provider::CredentialsProvider for EmptyCredentialsProvider {
+ fn read_credentials<'a>(
+ &'a self,
+ _url: &'a str,
+ _cx: &'a AsyncApp,
+ ) -> Pin<Box<dyn Future<Output = Result<Option<(String, Vec<u8>)>>> + 'a>> {
+ Box::pin(async { Ok(None) })
+ }
+
+ fn write_credentials<'a>(
+ &'a self,
+ _url: &'a str,
+ _username: &'a str,
+ _password: &'a [u8],
+ _cx: &'a AsyncApp,
+ ) -> Pin<Box<dyn Future<Output = Result<()>> + 'a>> {
+ Box::pin(async { Ok(()) })
+ }
+
+ fn delete_credentials<'a>(
+ &'a self,
+ _url: &'a str,
+ _cx: &'a AsyncApp,
+ ) -> Pin<Box<dyn Future<Output = Result<()>> + 'a>> {
+ Box::pin(async { Ok(()) })
+ }
+ }
+
fn map_events(events: Vec<responses::StreamEvent>) -> Vec<LanguageModelCompletionEvent> {
futures::executor::block_on(async {
CopilotResponsesEventMapper::new()
diff --git a/crates/language_model/src/fake_provider.rs b/crates/language_model/src/fake_provider.rs
index 94f4487f65e..f9e77327077 100644
--- a/crates/language_model/src/fake_provider.rs
+++ b/crates/language_model/src/fake_provider.rs
@@ -121,6 +121,7 @@ pub struct FakeLanguageModel {
supports_images: AtomicBool,
supports_server_side_compaction: AtomicBool,
max_token_count: AtomicU64,
+ max_input_tokens: Option<u64>,
max_output_tokens: AtomicU64,
input_token_counts: Mutex<VecDeque<u64>>,
input_token_count_requests: Mutex<Vec<LanguageModelRequest>>,
@@ -141,6 +142,7 @@ impl Default for FakeLanguageModel {
supports_images: AtomicBool::new(false),
supports_server_side_compaction: AtomicBool::new(false),
max_token_count: AtomicU64::new(1_000_000),
+ max_input_tokens: None,
max_output_tokens: AtomicU64::new(0),
input_token_counts: Mutex::new(VecDeque::new()),
input_token_count_requests: Mutex::new(Vec::new()),
@@ -196,6 +198,10 @@ impl FakeLanguageModel {
self.max_token_count.store(count, SeqCst);
}
+ pub fn set_max_input_tokens(&mut self, count: u64) {
+ self.max_input_tokens = Some(count);
+ }
+
pub fn set_max_output_tokens(&self, count: Option<u64>) {
self.max_output_tokens
.store(count.unwrap_or_default(), SeqCst);
@@ -342,6 +348,11 @@ impl LanguageModel for FakeLanguageModel {
self.max_token_count.load(SeqCst)
}
+ fn max_input_tokens(&self) -> u64 {
+ self.max_input_tokens
+ .unwrap_or_else(|| self.max_token_count())
+ }
+
fn max_output_tokens(&self) -> Option<u64> {
let max_output_tokens = self.max_output_tokens.load(SeqCst);
if max_output_tokens == 0 {
diff --git a/crates/language_model/src/language_model.rs b/crates/language_model/src/language_model.rs
index 90353facb8e..dc12a4559bf 100644
--- a/crates/language_model/src/language_model.rs
+++ b/crates/language_model/src/language_model.rs
@@ -215,9 +215,16 @@ pub trait LanguageModel: Send + Sync {
false
}
- /// Returns the input token ceiling.
+ /// Returns the model's context-window capacity.
fn max_token_count(&self) -> u64;
+ /// Returns the input ceiling before reserving output from any shared window.
+ ///
+ /// Models with a separate prompt limit override the context-window default.
+ fn max_input_tokens(&self) -> u64 {
+ self.max_token_count()
+ }
+
/// Counts request input without generating output, when supported by the provider.
///
/// Counts may be estimates and differ from subsequent measured usage. Callers
@@ -233,7 +240,7 @@ pub trait LanguageModel: Send + Sync {
/// Returns the combined input and output ceiling, if one applies.
///
- /// The conservative default shares the input ceiling with output. `None`
+ /// The conservative default shares the context window with output. `None`
/// means generation does not consume that window, not merely that the API
/// validates input separately or stops generation at the window boundary.
fn max_total_tokens(&self) -> Option<u64> {