mirror of
https://github.com/openai/codex.git
synced 2026-11-16 06:54:36 +00:00
Add a fallback phase before automatic context rollover (#33255)
## Why Token-budget sessions need a final opportunity to preserve important state before the current context is compacted. ## What changed - When `auto_compact_fallback_prompt` is configured, reserve `auto_compact_fallback_buffer_tokens` beyond the base auto-compaction limit. - At the base limit, inject the fallback prompt once as a developer message while keeping the normal tool surface available. Continue reporting zero base-window tokens during this reserve. - Roll over when the reserve or full model context is exhausted, and skip the fallback when a new context was already requested. ## Testing Added coverage for using the fallback reserve, rolling over after it is exhausted, and bypassing the fallback for an explicit `new_context` request. GitOrigin-RevId: 32983ecadc5cb4927e15ecd7b1e896cb52a9ad3b
This commit is contained in:
1 parent
3afbd8dd45
commit
768330dd6c
10 files changed
+303
-53
No files matched your search
@@ -6,35 +6,56 @@ use codex_features::Feature;
|
||||
pub(super) async fn maybe_record(
|
||||
sess: &Session,
|
||||
turn_context: &TurnContext,
|
||||
tokens_until_compaction: Option<i64>,
|
||||
base_window_tokens_remaining: Option<i64>,
|
||||
allow_auto_compact_fallback: bool,
|
||||
) {
|
||||
if !turn_context.config.features.enabled(Feature::TokenBudget) {
|
||||
return;
|
||||
}
|
||||
let Some(tokens_until_compaction) = tokens_until_compaction else {
|
||||
let Some(base_window_tokens_remaining) = base_window_tokens_remaining else {
|
||||
return;
|
||||
};
|
||||
|
||||
let Some(config) = turn_context.config.token_budget.as_ref().filter(|config| {
|
||||
config
|
||||
.reminder_threshold_tokens
|
||||
.is_some_and(|threshold| tokens_until_compaction <= threshold)
|
||||
}) else {
|
||||
let Some(config) = turn_context.config.token_budget.as_ref() else {
|
||||
return;
|
||||
};
|
||||
|
||||
let reminder_due = {
|
||||
if config
|
||||
.reminder_threshold_tokens
|
||||
.is_some_and(|threshold| base_window_tokens_remaining <= threshold)
|
||||
{
|
||||
let reminder_due = {
|
||||
let mut state = sess.state.lock().await;
|
||||
state.claim_token_budget_reminder()
|
||||
};
|
||||
if reminder_due {
|
||||
let response_item =
|
||||
ContextualUserFragment::into(crate::context::TokenBudgetReminder::new(
|
||||
&config.reminder_message_template,
|
||||
base_window_tokens_remaining,
|
||||
));
|
||||
sess.record_conversation_items(turn_context, std::slice::from_ref(&response_item))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
if !allow_auto_compact_fallback || base_window_tokens_remaining != 0 {
|
||||
return;
|
||||
}
|
||||
let Some(prompt) = config.auto_compact_fallback_prompt.as_deref() else {
|
||||
return;
|
||||
};
|
||||
|
||||
let fallback_due = {
|
||||
let mut state = sess.state.lock().await;
|
||||
state.claim_token_budget_reminder()
|
||||
state.claim_auto_compact_fallback()
|
||||
};
|
||||
if !reminder_due {
|
||||
if !fallback_due {
|
||||
return;
|
||||
}
|
||||
|
||||
let response_item = ContextualUserFragment::into(crate::context::TokenBudgetReminder::new(
|
||||
&config.reminder_message_template,
|
||||
tokens_until_compaction,
|
||||
));
|
||||
let response_item =
|
||||
ContextualUserFragment::into(crate::context::AutoCompactFallbackPrompt::new(prompt));
|
||||
sess.record_conversation_items(turn_context, std::slice::from_ref(&response_item))
|
||||
.await;
|
||||
}
|
||||
Reference in new issue
Block a user