Skip to content

Commit 4a5d6f7

Browse files
authored
Make ESC button work when auto-compaction (#3857)
Only emit a task finished when the compaction comes from a `/compact`
1 parent 1b3c8b8 commit 4a5d6f7

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

codex-rs/core/src/codex/compact.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,29 @@ pub(super) async fn run_compact_task(
7979
input: Vec<InputItem>,
8080
compact_instructions: String,
8181
) {
82+
let start_event = Event {
83+
id: sub_id.clone(),
84+
msg: EventMsg::TaskStarted(TaskStartedEvent {
85+
model_context_window: turn_context.client.get_model_context_window(),
86+
}),
87+
};
88+
sess.send_event(start_event).await;
8289
run_compact_task_inner(
83-
sess,
90+
sess.clone(),
8491
turn_context,
85-
sub_id,
92+
sub_id.clone(),
8693
input,
8794
compact_instructions,
8895
true,
8996
)
9097
.await;
98+
let event = Event {
99+
id: sub_id,
100+
msg: EventMsg::TaskComplete(TaskCompleteEvent {
101+
last_agent_message: None,
102+
}),
103+
};
104+
sess.send_event(event).await;
91105
}
92106

93107
async fn run_compact_task_inner(
@@ -98,15 +112,6 @@ async fn run_compact_task_inner(
98112
compact_instructions: String,
99113
remove_task_on_completion: bool,
100114
) {
101-
let model_context_window = turn_context.client.get_model_context_window();
102-
let start_event = Event {
103-
id: sub_id.clone(),
104-
msg: EventMsg::TaskStarted(TaskStartedEvent {
105-
model_context_window,
106-
}),
107-
};
108-
sess.send_event(start_event).await;
109-
110115
let initial_input_for_turn: ResponseInputItem = ResponseInputItem::from(input);
111116
let instructions_override = compact_instructions;
112117
let turn_input = sess.turn_input_with_history(vec![initial_input_for_turn.clone().into()]);
@@ -195,13 +200,6 @@ async fn run_compact_task_inner(
195200
}),
196201
};
197202
sess.send_event(event).await;
198-
let event = Event {
199-
id: sub_id.clone(),
200-
msg: EventMsg::TaskComplete(TaskCompleteEvent {
201-
last_agent_message: None,
202-
}),
203-
};
204-
sess.send_event(event).await;
205203
}
206204

207205
fn content_items_to_text(content: &[ContentItem]) -> Option<String> {

codex-rs/core/tests/suite/compact.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,15 +861,30 @@ async fn auto_compact_allows_multiple_attempts_when_interleaved_with_other_turn_
861861
.await
862862
.unwrap();
863863

864+
let mut auto_compact_lifecycle_events = Vec::new();
864865
loop {
865866
let event = codex.next_event().await.unwrap();
867+
if event.id.starts_with("auto-compact-")
868+
&& matches!(
869+
event.msg,
870+
EventMsg::TaskStarted(_) | EventMsg::TaskComplete(_)
871+
)
872+
{
873+
auto_compact_lifecycle_events.push(event);
874+
continue;
875+
}
866876
if let EventMsg::TaskComplete(_) = &event.msg
867877
&& !event.id.starts_with("auto-compact-")
868878
{
869879
break;
870880
}
871881
}
872882

883+
assert!(
884+
auto_compact_lifecycle_events.is_empty(),
885+
"auto compact should not emit task lifecycle events"
886+
);
887+
873888
let request_bodies: Vec<String> = responder
874889
.recorded_requests()
875890
.into_iter()

0 commit comments

Comments
 (0)