This commit is contained in:
Owen Lin
2026-06-08 15:31:33 -07:00
parent 7b2fb1a6f4
commit de87fa6155
3 changed files with 12 additions and 42 deletions

View File

@@ -519,36 +519,14 @@ pub(crate) fn build_compacted_history(
user_messages,
summary_text,
COMPACT_USER_MESSAGE_MAX_TOKENS,
CompactedHistoryMessageIds::New,
)
}
pub(crate) fn rebuild_legacy_compacted_history(
initial_context: Vec<ResponseItem>,
user_messages: &[String],
summary_text: &str,
) -> Vec<ResponseItem> {
build_compacted_history_with_limit(
initial_context,
user_messages,
summary_text,
COMPACT_USER_MESSAGE_MAX_TOKENS,
CompactedHistoryMessageIds::Missing,
)
}
#[derive(Clone, Copy)]
enum CompactedHistoryMessageIds {
New,
Missing,
}
fn build_compacted_history_with_limit(
mut history: Vec<ResponseItem>,
user_messages: &[String],
summary_text: &str,
max_tokens: usize,
message_ids: CompactedHistoryMessageIds,
) -> Vec<ResponseItem> {
let mut selected_messages: Vec<String> = Vec::new();
if max_tokens > 0 {
@@ -571,7 +549,7 @@ fn build_compacted_history_with_limit(
}
for message in &selected_messages {
history.push(compacted_user_message(message.clone(), message_ids));
history.push(compacted_user_message(message.clone()));
}
let summary_text = if summary_text.is_empty() {
@@ -580,22 +558,14 @@ fn build_compacted_history_with_limit(
summary_text.to_string()
};
history.push(compacted_user_message(summary_text, message_ids));
history.push(compacted_user_message(summary_text));
history
}
fn compacted_user_message(text: String, message_ids: CompactedHistoryMessageIds) -> ResponseItem {
fn compacted_user_message(text: String) -> ResponseItem {
let content = vec![ContentItem::InputText { text }];
match message_ids {
CompactedHistoryMessageIds::New => ResponseItem::new_message("user", content),
CompactedHistoryMessageIds::Missing => ResponseItem::Message {
id: None,
role: "user".to_string(),
content,
phase: None,
},
}
ResponseItem::new_message("user", content)
}
async fn drain_to_completed(

View File

@@ -162,7 +162,6 @@ fn build_token_limited_compacted_history_truncates_overlong_user_messages() {
std::slice::from_ref(&big),
"SUMMARY",
max_tokens,
super::CompactedHistoryMessageIds::New,
);
assert_eq!(history.len(), 2);

View File

@@ -254,16 +254,17 @@ impl Session {
history.replace(replacement_history.clone());
} else {
saw_legacy_compaction_without_replacement_history = true;
// Legacy rollouts without `replacement_history` should rebuild the
// historical TurnContext at the correct insertion point from persisted
// `TurnContextItem`s. These are rare enough that we currently just clear
// `reference_context_item`, reinject canonical context at the end of the
// resumed conversation, and accept the temporary out-of-distribution
// prompt shape.
// Legacy rollouts without `replacement_history` rebuild fresh compacted
// replacement messages from the historical user-message text and summary.
// They should also rebuild the historical TurnContext at the correct
// insertion point from persisted `TurnContextItem`s. These are rare enough
// that we currently just clear `reference_context_item`, reinject canonical
// context at the end of the resumed conversation, and accept the temporary
// out-of-distribution prompt shape.
// TODO(ccunningham): if we drop support for None replacement_history compaction items,
// we can get rid of this second loop entirely and just build `history` directly in the first loop.
let user_messages = collect_user_messages(history.raw_items());
let rebuilt = compact::rebuild_legacy_compacted_history(
let rebuilt = compact::build_compacted_history(
Vec::new(),
&user_messages,
&compacted.message,