Narrow prefix compaction suffix cleanup

This commit is contained in:
Rohit Arunachalam
2026-04-14 01:43:36 -07:00
parent 3f96ed7a7f
commit 9696c3a8aa
2 changed files with 57 additions and 5 deletions
+14 -5
View File
@@ -503,16 +503,25 @@ fn is_injected_context_item(item: &ResponseItem) -> bool {
crate::event_mapping::is_contextual_dev_message_content(content)
}
ResponseItem::Message { role, content, .. } if role == "user" => {
crate::event_mapping::is_contextual_user_message_content(content)
&& !matches!(
crate::event_mapping::parse_turn_item(item),
Some(TurnItem::HookPrompt(_))
)
is_regenerable_contextual_user_message(content)
}
_ => false,
}
}
fn is_regenerable_contextual_user_message(content: &[ContentItem]) -> bool {
!content.is_empty() && content.iter().all(is_regenerable_contextual_user_fragment)
}
fn is_regenerable_contextual_user_fragment(content_item: &ContentItem) -> bool {
let ContentItem::InputText { text } = content_item else {
return false;
};
codex_instructions::AGENTS_MD_FRAGMENT.matches_text(text)
|| crate::contextual_user_message::ENVIRONMENT_CONTEXT_FRAGMENT.matches_text(text)
}
pub(crate) fn build_compacted_history(
initial_context: Vec<ResponseItem>,
user_messages: &[String],
+43
View File
@@ -179,6 +179,14 @@ fn strip_injected_context_from_retained_suffix_preserves_live_history() {
end_turn: None,
phase: None,
};
let user_instructions = input_message(
"user",
r#"# AGENTS.md instructions for /repo
<INSTRUCTIONS>
Use cargo test.
</INSTRUCTIONS>"#,
);
let contextual_user = input_message(
"user",
r#"<environment_context>
@@ -186,6 +194,32 @@ fn strip_injected_context_from_retained_suffix_preserves_live_history() {
<shell>zsh</shell>
</environment_context>"#,
);
let skill_payload = input_message(
"user",
r#"<skill>
<name>fix-ci</name>
<path>/skills/fix-ci/SKILL.md</path>
Debug the failing check.
</skill>"#,
);
let user_shell_command = input_message(
"user",
r#"<user_shell_command>
<command>
echo hi
</command>
<result>
Exit code: 0
Output:
hi
</result>
</user_shell_command>"#,
);
let subagent_notification = input_message(
"user",
r#"<subagent_notification>{"agent_path":"agent-1","status":"completed"}</subagent_notification>"#,
);
let turn_aborted = input_message("user", "<turn_aborted>interrupted</turn_aborted>");
let real_user = input_message("user", "please continue");
let assistant = ResponseItem::Message {
id: None,
@@ -216,7 +250,12 @@ fn strip_injected_context_from_retained_suffix_preserves_live_history() {
let cleaned = strip_injected_context_from_retained_suffix(vec![
contextual_developer,
mixed_developer,
user_instructions,
contextual_user,
skill_payload.clone(),
user_shell_command.clone(),
subagent_notification.clone(),
turn_aborted.clone(),
real_user.clone(),
assistant.clone(),
function_call.clone(),
@@ -227,6 +266,10 @@ fn strip_injected_context_from_retained_suffix_preserves_live_history() {
assert_eq!(
cleaned,
vec![
skill_payload,
user_shell_command,
subagent_notification,
turn_aborted,
real_user,
assistant,
function_call,