mirror of
https://wget.la/https://github.com/leookun/cursor-byok
synced 2026-12-01 01:05:10 +08:00
feat: enhance context token estimation and compaction logic
- Added `estimate_context_tokens` function to calculate provider-visible context size based on prompt specifications and projected messages. - Updated `CheckpointBuilder` to record estimated context tokens during message processing. - Refactored compaction logic to utilize the new token estimation, ensuring proper context management during model runs. - Introduced tests to validate context estimation and compaction behavior under various scenarios.
This commit is contained in:
1 parent
ee2592c469
commit
29fde7d7c7
9 files changed
+723
-188
No files matched your search
@@ -1,62 +1,53 @@
|
||||
//! Decides when to compact context and builds a stable fallback summary.
|
||||
//! Decides when to compact provider-visible context and builds a stable fallback summary.
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
use crate::model::{CanonicalMessage, LlmCallUsageAnchor, PreparedRun, ProjectedMessage};
|
||||
use crate::model::{estimate_context_tokens, CanonicalMessage, PreparedRun, ProjectedMessage};
|
||||
|
||||
const FALLBACK_CHARS: usize = 12_000;
|
||||
|
||||
pub(super) const RESERVE_TOKENS: u64 = 10_000;
|
||||
pub(super) const OUTPUT_TOKENS: u64 = 4_096;
|
||||
pub(super) const INSTRUCTIONS: &str = "Summarize the conversation for the next model turn. Preserve goals, constraints, decisions, files, commands, errors, results, and unfinished work. Do not call tools. Return only the concise durable summary.";
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(super) struct ContextUsageAnchor {
|
||||
input_tokens: u64,
|
||||
message_count: usize,
|
||||
tool_count: usize,
|
||||
pub(super) fn input_budget(prepared: &PreparedRun) -> Option<u64> {
|
||||
prepared
|
||||
.model
|
||||
.context_window_tokens
|
||||
.map(|window| window.saturating_sub(RESERVE_TOKENS))
|
||||
}
|
||||
|
||||
impl ContextUsageAnchor {
|
||||
pub(super) fn from_llm_call(anchor: LlmCallUsageAnchor) -> Option<Self> {
|
||||
Some(Self {
|
||||
input_tokens: anchor.usage.context_input_tokens(anchor.request_type)?,
|
||||
message_count: anchor.message_count,
|
||||
tool_count: anchor.tool_count,
|
||||
})
|
||||
}
|
||||
pub(super) fn estimated_tokens(
|
||||
prepared: &PreparedRun,
|
||||
projected_messages: &[ProjectedMessage],
|
||||
) -> u64 {
|
||||
estimate_context_tokens(&prepared.prompt, projected_messages)
|
||||
}
|
||||
|
||||
pub(super) fn should_compact(
|
||||
prepared: &PreparedRun,
|
||||
messages: &[CanonicalMessage],
|
||||
projected_messages: &[ProjectedMessage],
|
||||
anchor: Option<ContextUsageAnchor>,
|
||||
) -> bool {
|
||||
let Some(context_window) = prepared.model.context_window_tokens else {
|
||||
let Some(budget) = input_budget(prepared) else {
|
||||
return false;
|
||||
};
|
||||
if context_window == 0 || messages.len() <= prepared.initial_messages.len() {
|
||||
return false;
|
||||
estimated_tokens(prepared, projected_messages) > budget
|
||||
}
|
||||
|
||||
pub(super) fn validate_compacted(
|
||||
prepared: &PreparedRun,
|
||||
projected_messages: &[ProjectedMessage],
|
||||
) -> std::result::Result<u64, String> {
|
||||
let estimated = estimated_tokens(prepared, projected_messages);
|
||||
let Some(budget) = input_budget(prepared) else {
|
||||
return Ok(estimated);
|
||||
};
|
||||
if estimated <= budget {
|
||||
return Ok(estimated);
|
||||
}
|
||||
let estimated_input = anchor
|
||||
.filter(|anchor| {
|
||||
anchor.message_count <= projected_messages.len()
|
||||
&& anchor.tool_count == prepared.prompt.tools.len()
|
||||
})
|
||||
.map(|anchor| {
|
||||
anchor
|
||||
.input_tokens
|
||||
.saturating_add(estimate_serialized_tokens(
|
||||
&serde_json::to_string(&projected_messages[anchor.message_count..])
|
||||
.unwrap_or_default(),
|
||||
))
|
||||
})
|
||||
.unwrap_or_else(|| {
|
||||
estimate_serialized_tokens(
|
||||
&serde_json::to_string(&(&prepared.prompt, messages)).unwrap_or_default(),
|
||||
)
|
||||
});
|
||||
estimated_input > context_window
|
||||
Err(format!(
|
||||
"context overflow after compaction: estimated input {estimated} tokens exceeds budget {budget} tokens"
|
||||
))
|
||||
}
|
||||
|
||||
pub(super) fn partition(
|
||||
@@ -95,15 +86,6 @@ pub(super) fn fallback_summary(messages: &[CanonicalMessage]) -> String {
|
||||
)
|
||||
}
|
||||
|
||||
fn estimate_serialized_tokens(serialized: &str) -> u64 {
|
||||
serialized
|
||||
.chars()
|
||||
.fold(0_u64, |units, character| {
|
||||
units.saturating_add(if character.is_ascii() { 273 } else { 550 })
|
||||
})
|
||||
.div_ceil(1_000)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -112,11 +94,10 @@ mod tests {
|
||||
RunAction, RunId, RunKind,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn automatic_compaction_runs_for_start_and_resume_actions_after_the_limit() {
|
||||
fn prepared(context_window_tokens: u64) -> PreparedRun {
|
||||
let mut model = ModelSpec::new("model");
|
||||
model.context_window_tokens = Some(200_000);
|
||||
let mut prepared = PreparedRun {
|
||||
model.context_window_tokens = Some(context_window_tokens);
|
||||
PreparedRun {
|
||||
run_id: RunId::new("run"),
|
||||
cursor_request_id: None,
|
||||
conversation_id: ConversationId::new("conversation"),
|
||||
@@ -129,50 +110,50 @@ mod tests {
|
||||
initial_messages: Vec::new(),
|
||||
action: RunAction::Start,
|
||||
base_checkpoint_id: CheckpointId(1),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn automatic_compaction_uses_fixed_reserve_for_every_action() {
|
||||
let messages = vec![CanonicalMessage::text(
|
||||
"user",
|
||||
Role::User,
|
||||
Origin::Runtime,
|
||||
"hello",
|
||||
"x".repeat(40_000),
|
||||
)];
|
||||
let projected = project_messages(&messages).unwrap();
|
||||
let tail_tokens = estimate_serialized_tokens(&serde_json::to_string(&projected).unwrap());
|
||||
let anchor = |estimated_input| {
|
||||
Some(ContextUsageAnchor {
|
||||
input_tokens: estimated_input - tail_tokens,
|
||||
message_count: 0,
|
||||
tool_count: 0,
|
||||
})
|
||||
};
|
||||
let estimated = estimate_context_tokens(&prepared(1).prompt, &projected);
|
||||
let mut prepared = prepared(estimated + RESERVE_TOKENS);
|
||||
|
||||
assert!(!should_compact(
|
||||
&prepared,
|
||||
&messages,
|
||||
&projected,
|
||||
anchor(199_999)
|
||||
));
|
||||
assert!(!should_compact(
|
||||
&prepared,
|
||||
&messages,
|
||||
&projected,
|
||||
anchor(200_000)
|
||||
));
|
||||
assert!(should_compact(
|
||||
&prepared,
|
||||
&messages,
|
||||
&projected,
|
||||
anchor(200_001)
|
||||
));
|
||||
assert!(!should_compact(&prepared, &projected));
|
||||
prepared.model.context_window_tokens = Some(estimated + RESERVE_TOKENS - 1);
|
||||
assert!(should_compact(&prepared, &projected));
|
||||
|
||||
prepared.action = RunAction::Resume {
|
||||
pending_tool_round: None,
|
||||
};
|
||||
assert!(should_compact(
|
||||
&prepared,
|
||||
&messages,
|
||||
&projected,
|
||||
anchor(200_001)
|
||||
));
|
||||
assert!(should_compact(&prepared, &projected));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compacted_history_is_validated_against_the_same_budget() {
|
||||
let messages = vec![CanonicalMessage::text(
|
||||
"user",
|
||||
Role::User,
|
||||
Origin::Runtime,
|
||||
"x".repeat(40_000),
|
||||
)];
|
||||
let projected = project_messages(&messages).unwrap();
|
||||
let estimated = estimate_context_tokens(&prepared(1).prompt, &projected);
|
||||
|
||||
assert_eq!(
|
||||
validate_compacted(&prepared(estimated + RESERVE_TOKENS), &projected),
|
||||
Ok(estimated)
|
||||
);
|
||||
assert!(
|
||||
validate_compacted(&prepared(estimated + RESERVE_TOKENS - 1), &projected)
|
||||
.unwrap_err()
|
||||
.contains("context overflow after compaction")
|
||||
);
|
||||
}
|
||||
}
|
||||
+16
-23
@@ -161,7 +161,6 @@ impl RunEngine {
|
||||
};
|
||||
}
|
||||
|
||||
let mut auto_compacted = prepared.action == RunAction::Compact;
|
||||
'model: loop {
|
||||
if cancellation.is_cancelled() {
|
||||
return (RunOutcome::Cancelled, usage);
|
||||
@@ -170,31 +169,13 @@ impl RunEngine {
|
||||
Ok(messages) => messages,
|
||||
Err(error) => return (RunOutcome::Failed(error.into()), usage),
|
||||
};
|
||||
let context_anchor = if !auto_compacted {
|
||||
match self
|
||||
.store
|
||||
.latest_llm_call_usage_anchor(
|
||||
&prepared.conversation_id,
|
||||
&prepared.model.model_id,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(anchor) => {
|
||||
anchor.and_then(super::compaction::ContextUsageAnchor::from_llm_call)
|
||||
}
|
||||
Err(error) => return (RunOutcome::Failed(error.into()), usage),
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let history = match crate::model::project_messages(&messages) {
|
||||
Ok(history) => history,
|
||||
Err(error) => return (RunOutcome::Failed(error.into()), usage),
|
||||
};
|
||||
if !auto_compacted
|
||||
&& super::compaction::should_compact(prepared, &messages, &history, context_anchor)
|
||||
if prepared.action != RunAction::Compact
|
||||
&& super::compaction::should_compact(prepared, &history)
|
||||
{
|
||||
auto_compacted = true;
|
||||
match self
|
||||
.auto_compact(prepared, checkpoint, &messages, client, cancellation)
|
||||
.await
|
||||
@@ -583,7 +564,15 @@ impl RunEngine {
|
||||
let (compactable, retained_request_context) =
|
||||
super::compaction::partition(messages, ¤t_ids);
|
||||
if compactable.is_empty() {
|
||||
return Ok((checkpoint, None));
|
||||
let projected = crate::model::project_messages(messages)
|
||||
.map_err(|error| RunOutcome::Failed(error.into()))?;
|
||||
let message = super::compaction::validate_compacted(prepared, &projected)
|
||||
.err()
|
||||
.unwrap_or_else(|| {
|
||||
"context overflow after compaction: no conversation history can be compacted"
|
||||
.into()
|
||||
});
|
||||
return Err(RunOutcome::Failed(RunFailure::Protocol(message)));
|
||||
}
|
||||
|
||||
emit(client, RunEvent::AutoCompactionStarted)
|
||||
@@ -689,7 +678,7 @@ impl RunEngine {
|
||||
)
|
||||
}
|
||||
};
|
||||
let event_id = format!("summary:auto:{}", prepared.run_id);
|
||||
let event_id = format!("summary:auto:{}:{provider_call_index}", prepared.run_id);
|
||||
let summary_message = CanonicalMessage {
|
||||
message_id: format!("runtime:{event_id}"),
|
||||
role: Role::User,
|
||||
@@ -704,6 +693,10 @@ impl RunEngine {
|
||||
let mut replacement = retained_request_context.into_iter().collect::<Vec<_>>();
|
||||
replacement.push(summary_message);
|
||||
replacement.extend(prepared.initial_messages.iter().cloned());
|
||||
let projected_replacement = crate::model::project_messages(&replacement)
|
||||
.map_err(|error| RunOutcome::Failed(error.into()))?;
|
||||
super::compaction::validate_compacted(prepared, &projected_replacement)
|
||||
.map_err(|message| RunOutcome::Failed(RunFailure::Protocol(message)))?;
|
||||
let mut checkpoint = self
|
||||
.store
|
||||
.replace_checkpoint(
|
||||
|
||||
Reference in new issue
Block a user