-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix compaction loop for small models or large input #5803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
25c9354
0609252
e6f58cf
5d4dbd3
0917920
059bd33
383e719
7864533
5468906
3a304d1
7911c46
2dcc913
657aa0a
94eee11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -940,6 +940,7 @@ impl Agent { | |||||||||||||||||||||||||||||||||
| let _ = reply_span.enter(); | ||||||||||||||||||||||||||||||||||
| let mut turns_taken = 0u32; | ||||||||||||||||||||||||||||||||||
| let max_turns = session_config.max_turns.unwrap_or(DEFAULT_MAX_TURNS); | ||||||||||||||||||||||||||||||||||
| let mut compaction_attempts = 0; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| loop { | ||||||||||||||||||||||||||||||||||
| if is_token_cancelled(&cancel_token) { | ||||||||||||||||||||||||||||||||||
|
|
@@ -991,6 +992,8 @@ impl Agent { | |||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| match next { | ||||||||||||||||||||||||||||||||||
| Ok((response, usage)) => { | ||||||||||||||||||||||||||||||||||
| compaction_attempts = 0; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Emit model change event if provider is lead-worker | ||||||||||||||||||||||||||||||||||
| let provider = self.provider().await?; | ||||||||||||||||||||||||||||||||||
| if let Some(lead_worker) = provider.as_lead_worker() { | ||||||||||||||||||||||||||||||||||
|
|
@@ -1202,6 +1205,19 @@ impl Agent { | |||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| Err(ref provider_err @ ProviderError::ContextLengthExceeded(_)) => { | ||||||||||||||||||||||||||||||||||
| crate::posthog::emit_error(provider_err.telemetry_type()); | ||||||||||||||||||||||||||||||||||
| compaction_attempts += 1; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if compaction_attempts >= 2 { | ||||||||||||||||||||||||||||||||||
| error!("Context limit exceeded after compaction - prompt too large"); | ||||||||||||||||||||||||||||||||||
| yield AgentEvent::Message( | ||||||||||||||||||||||||||||||||||
| Message::assistant().with_system_notification( | ||||||||||||||||||||||||||||||||||
| SystemNotificationType::InlineMessage, | ||||||||||||||||||||||||||||||||||
| "Unable to continue: Context limit still exceeded after compaction. Try using a shorter message, a model with a larger context window, or start a new session." | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| yield AgentEvent::Message( | ||||||||||||||||||||||||||||||||||
| Message::assistant().with_system_notification( | ||||||||||||||||||||||||||||||||||
| SystemNotificationType::InlineMessage, | ||||||||||||||||||||||||||||||||||
|
|
@@ -1222,15 +1238,10 @@ impl Agent { | |||||||||||||||||||||||||||||||||
| conversation = compacted_conversation; | ||||||||||||||||||||||||||||||||||
| did_recovery_compact_this_iteration = true; | ||||||||||||||||||||||||||||||||||
| yield AgentEvent::HistoryReplaced(conversation.clone()); | ||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| Err(e) => { | ||||||||||||||||||||||||||||||||||
| error!("Error: {}", e); | ||||||||||||||||||||||||||||||||||
| yield AgentEvent::Message( | ||||||||||||||||||||||||||||||||||
| Message::assistant().with_text( | ||||||||||||||||||||||||||||||||||
| format!("Ran into this error trying to compact: {e}.\n\nPlease retry if you think this is a transient or recoverable error.") | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
| error!("Compaction failed: {}", e); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
| error!("Compaction failed: {}", e); | |
| error!("Compaction failed: {}", e); | |
| yield AgentEvent::Message( | |
| Message::assistant().with_system_notification( | |
| SystemNotificationType::InlineMessage, | |
| "Unable to continue: Compaction failed due to an internal error. Try using a shorter message, a model with a larger context window, or start a new session." | |
| ) | |
| ); |
Copilot
AI
Dec 16, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When compaction fails, no user-facing message is shown. The old code yielded an AgentEvent::Message explaining the error. Now users will see the conversation end without explanation, which is confusing. Consider adding back a user notification before the break.
| error!("Compaction failed: {}", e); | |
| error!("Compaction failed: {}", e); | |
| yield AgentEvent::Message( | |
| Message::assistant().with_system_notification( | |
| SystemNotificationType::InlineMessage, | |
| "Unable to continue: Compaction failed due to context limit. Try using a shorter message, a model with a larger context window, or start a new session." | |
| ) | |
| ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we could just check if the message length is a certain value? Conversation only contains a single user message and compaction failed?
This would be durable to other failure modes on the compaction request not working (like the provider connection is bad, or something else goes wrong)
What do you think?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most common situation this occurs is when pasting something gigantic, so I don't think the single message case is all that general.
That triggers out of context error -> triggers compaction -> loop.
Agree on the sentiment of seeing if the message length is a certain size, but a bit messy in practice and would need to both estimate token counts + have a good understanding of context window size and system prompt size.
I think will go with as is for now, but might be something there using the data from canonical models.