mirror of
https://github.com/openai/codex.git
synced 2026-11-21 15:10:25 +00:00
Store model history in response item envelopes (#38045)
## What changed - Add `ResponseItemEnvelope` as the history-owned wrapper for a `ResponseItem`. - Store envelopes in `ContextManager` while keeping raw-item iteration and consuming APIs for existing callers. - Preserve envelopes when compaction and history normalization rewrite retained items, and wrap newly synthesized items. ## Testing - Cover envelope access, mutation, and conversion back to raw response items. - Cover annotated history replacement and annotated user-message collection for compaction. GitOrigin-RevId: 3fa2e7bdd981b08cc7aec410c80deccaecdabab4
This commit is contained in:
1 parent
be751dd1df
commit
99915080b6
20 files changed
+382
-224
No files matched your search
@@ -28,6 +28,7 @@ use codex_analytics::CompactionStatus;
|
||||
use codex_analytics::CompactionStrategy;
|
||||
use codex_analytics::CompactionTrigger;
|
||||
use codex_analytics::now_unix_seconds;
|
||||
use codex_history::ResponseItemEnvelope;
|
||||
use codex_protocol::error::CodexErr;
|
||||
use codex_protocol::error::CodexErrorDetails;
|
||||
use codex_protocol::error::Result as CodexResult;
|
||||
@@ -340,10 +341,11 @@ async fn run_compact_task_inner_impl(
|
||||
}
|
||||
|
||||
let history_snapshot = sess.clone_history().await;
|
||||
let history_items = history_snapshot.raw_items();
|
||||
let summary_suffix = get_last_assistant_message_from_turn(history_items).unwrap_or_default();
|
||||
let history_items = history_snapshot.annotated_items();
|
||||
let summary_suffix =
|
||||
get_last_assistant_message_from_turn(history_snapshot.raw_items()).unwrap_or_default();
|
||||
let summary_text = format!("{SUMMARY_PREFIX}\n{summary_suffix}");
|
||||
let user_messages = collect_user_messages(history_items);
|
||||
let user_messages = collect_annotated_user_messages(history_items);
|
||||
|
||||
let mut new_history = build_compacted_history(Vec::new(), &user_messages, &summary_text);
|
||||
if let Some(summary_item) = new_history.last_mut() {
|
||||
@@ -517,31 +519,39 @@ pub(crate) struct CompactedUserMessage {
|
||||
internal_chat_message_metadata_passthrough: Option<InternalChatMessageMetadataPassthrough>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn collect_user_messages(items: &[ResponseItem]) -> Vec<CompactedUserMessage> {
|
||||
items.iter().filter_map(compacted_user_message).collect()
|
||||
}
|
||||
|
||||
pub(crate) fn collect_annotated_user_messages(
|
||||
items: &[ResponseItemEnvelope],
|
||||
) -> Vec<CompactedUserMessage> {
|
||||
items
|
||||
.iter()
|
||||
.filter_map(|item| match crate::event_mapping::parse_turn_item(item) {
|
||||
Some(TurnItem::UserMessage(user)) => {
|
||||
if is_summary_message(&user.message()) {
|
||||
None
|
||||
} else {
|
||||
Some(CompactedUserMessage {
|
||||
message: user.message(),
|
||||
internal_chat_message_metadata_passthrough: match item {
|
||||
ResponseItem::Message {
|
||||
internal_chat_message_metadata_passthrough,
|
||||
..
|
||||
} => internal_chat_message_metadata_passthrough.clone(),
|
||||
_ => None,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.filter_map(|envelope| compacted_user_message(&envelope.item))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn compacted_user_message(item: &ResponseItem) -> Option<CompactedUserMessage> {
|
||||
let Some(TurnItem::UserMessage(user)) = crate::event_mapping::parse_turn_item(item) else {
|
||||
return None;
|
||||
};
|
||||
if is_summary_message(&user.message()) {
|
||||
return None;
|
||||
}
|
||||
Some(CompactedUserMessage {
|
||||
message: user.message(),
|
||||
internal_chat_message_metadata_passthrough: match item {
|
||||
ResponseItem::Message {
|
||||
internal_chat_message_metadata_passthrough,
|
||||
..
|
||||
} => internal_chat_message_metadata_passthrough.clone(),
|
||||
_ => None,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn is_summary_message(message: &str) -> bool {
|
||||
message.starts_with(format!("{SUMMARY_PREFIX}\n").as_str())
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user