Files
codex/prs/bolinfest/PR-2746.md
2025-09-02 15:17:45 -07:00

1.6 KiB

PR #2746: Race condition in compact

Description

This fixes the flakiness in summarize_context_three_requests_and_instructions because we should trim history before sending task complete.

Full Diff

diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs
index 365969ac02..8443c534fb 100644
--- a/codex-rs/core/src/codex.rs
+++ b/codex-rs/core/src/codex.rs
@@ -1884,6 +1884,12 @@ async fn run_compact_task(
     }
 
     sess.remove_task(&sub_id);
+
+    {
+        let mut state = sess.state.lock_unchecked();
+        state.history.keep_last_messages(1);
+    }
+
     let event = Event {
         id: sub_id.clone(),
         msg: EventMsg::AgentMessage(AgentMessageEvent {
@@ -1898,9 +1904,6 @@ async fn run_compact_task(
         }),
     };
     sess.send_event(event).await;
-
-    let mut state = sess.state.lock_unchecked();
-    state.history.keep_last_messages(1);
 }
 
 async fn handle_response_item(

Review Comments

codex-rs/core/src/codex.rs

@@ -1873,6 +1879,9 @@ async fn run_compact_task(
     }
 
     sess.remove_task(&sub_id);
+
+    sess.trim_history_to_last_messages(1);

Should we just inline this since it is only called once?

{
    let mut state = sess.state.lock_unchecked();
    state.history.keep_last_messages(1);
}