mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:50:52 +00:00
codex: address compaction exclusion review
This commit is contained in:
@@ -154,7 +154,7 @@ async fn run_remote_compact_task_inner_impl(
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
let prompt_input = history.for_prompt(&turn_context.model_info.input_modalities);
|
||||
let prompt_input = history.for_compaction_prompt(&turn_context.model_info.input_modalities);
|
||||
let tool_router = built_tools(
|
||||
sess.as_ref(),
|
||||
turn_context.as_ref(),
|
||||
|
||||
@@ -118,9 +118,38 @@ impl ContextManager {
|
||||
/// include `InputModality::Image`, images are stripped from messages and tool
|
||||
/// outputs.
|
||||
pub(crate) fn for_prompt(mut self, input_modalities: &[InputModality]) -> Vec<ResponseItem> {
|
||||
self.normalize_history(input_modalities);
|
||||
self.prepare_for_prompt(input_modalities, CompactionExclusionPolicy::Keep);
|
||||
self.items
|
||||
.retain(|item| !matches!(item, ResponseItem::GhostSnapshot { .. }));
|
||||
}
|
||||
|
||||
/// Returns the history prepared for the remote compaction endpoint.
|
||||
pub(crate) fn for_compaction_prompt(
|
||||
mut self,
|
||||
input_modalities: &[InputModality],
|
||||
) -> Vec<ResponseItem> {
|
||||
self.prepare_for_prompt(input_modalities, CompactionExclusionPolicy::Drop);
|
||||
self.items
|
||||
}
|
||||
|
||||
fn prepare_for_prompt(
|
||||
&mut self,
|
||||
input_modalities: &[InputModality],
|
||||
compaction_exclusion_policy: CompactionExclusionPolicy,
|
||||
) {
|
||||
self.normalize_history(input_modalities);
|
||||
self.items.retain(|item| {
|
||||
!matches!(item, ResponseItem::GhostSnapshot { .. })
|
||||
&& !matches!(
|
||||
(compaction_exclusion_policy, item),
|
||||
(
|
||||
CompactionExclusionPolicy::Drop,
|
||||
ResponseItem::Message {
|
||||
exclude_from_compaction: true,
|
||||
..
|
||||
}
|
||||
)
|
||||
)
|
||||
});
|
||||
for item in &mut self.items {
|
||||
if let ResponseItem::Message {
|
||||
exclude_from_compaction,
|
||||
@@ -130,7 +159,6 @@ impl ContextManager {
|
||||
*exclude_from_compaction = false;
|
||||
}
|
||||
}
|
||||
self.items
|
||||
}
|
||||
|
||||
/// Returns raw items in the history.
|
||||
@@ -465,6 +493,12 @@ impl ContextManager {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum CompactionExclusionPolicy {
|
||||
Keep,
|
||||
Drop,
|
||||
}
|
||||
|
||||
fn truncate_function_output_payload(
|
||||
output: &FunctionCallOutputPayload,
|
||||
policy: TruncationPolicy,
|
||||
|
||||
@@ -349,6 +349,26 @@ fn for_prompt_strips_compaction_exclusion_marker() {
|
||||
assert_eq!(prompt_items, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn for_compaction_prompt_drops_items_marked_excluded_from_compaction() {
|
||||
let mut generated_warning = user_input_text_msg("generated warning");
|
||||
let ResponseItem::Message {
|
||||
exclude_from_compaction,
|
||||
..
|
||||
} = &mut generated_warning
|
||||
else {
|
||||
panic!("expected message item");
|
||||
};
|
||||
*exclude_from_compaction = true;
|
||||
let real_user_message = user_input_text_msg("keep me");
|
||||
let history = create_history_with_items(vec![generated_warning, real_user_message.clone()]);
|
||||
|
||||
assert_eq!(
|
||||
history.for_compaction_prompt(&default_input_modalities()),
|
||||
vec![real_user_message]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drop_last_n_user_turns_treats_inter_agent_assistant_messages_as_instruction_turns() {
|
||||
let first_turn = user_input_text_msg("first");
|
||||
|
||||
Reference in New Issue
Block a user