From 24e05336f494c5245112705f600b206b62928aa3 Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Sun, 20 Jul 2025 18:09:22 -0700 Subject: [PATCH] wait for flush after task complete --- codex-rs/core/src/codex.rs | 27 ++++++------------ codex-rs/core/src/protocol.rs | 3 -- codex-rs/core/src/rollout.rs | 53 +++++++++++++++++++++++++---------- codex-rs/exec/src/lib.rs | 7 ----- 4 files changed, 47 insertions(+), 43 deletions(-) diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 959da04b0c..05cfd90a0b 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -805,24 +805,6 @@ async fn submission_loop( } }); } - Op::FlushRollout => { - // Best-effort flush: shutdown recorder and ack so caller can wait. - if let Some(sess_arc) = sess.as_ref() { - let rec_opt = { sess_arc.rollout.lock().unwrap().as_ref().cloned() }; - if let Some(rec) = rec_opt { - if let Err(e) = rec.shutdown().await { - warn!("failed to flush rollout recorder: {e}"); - } - } - } - let event = Event { - id: sub.id, - msg: EventMsg::BackgroundEvent(BackgroundEventEvent { - message: "rollout_flushed".to_string(), - }), - }; - tx_event.send(event).await.ok(); - } } } @@ -1039,6 +1021,15 @@ async fn run_task(sess: Arc, sub_id: String, input: Vec) { } } } + // Flush rollout so that all recorded items for this task are durable before TaskComplete. + if let Some(rec) = { + let guard = sess.rollout.lock().unwrap(); + guard.as_ref().cloned() + } { + if let Err(e) = rec.sync().await { + warn!("failed to flush rollout at task end: {e}"); + } + } sess.remove_task(&sub_id); let event = Event { id: sub_id, diff --git a/codex-rs/core/src/protocol.rs b/codex-rs/core/src/protocol.rs index e0309c4df3..08d55b9749 100644 --- a/codex-rs/core/src/protocol.rs +++ b/codex-rs/core/src/protocol.rs @@ -112,9 +112,6 @@ pub enum Op { /// Request a single history entry identified by `log_id` + `offset`. GetHistoryEntryRequest { offset: usize, log_id: u64 }, - - /// Flush and persist any pending rollout items (testing / graceful shutdown aid). - FlushRollout, } /// Determines the conditions under which the user is consulted to approve diff --git a/codex-rs/core/src/rollout.rs b/codex-rs/core/src/rollout.rs index 0d114b22a2..689b91e435 100644 --- a/codex-rs/core/src/rollout.rs +++ b/codex-rs/core/src/rollout.rs @@ -63,7 +63,10 @@ pub(crate) struct RolloutRecorder { enum RolloutCmd { AddItems(Vec), UpdateState(SessionStateSnapshot), - Shutdown(oneshot::Sender<()>), + Sync { + exit: bool, + ack: oneshot::Sender<()>, + }, } impl RolloutRecorder { @@ -203,11 +206,34 @@ impl RolloutRecorder { Ok((Self { tx }, saved)) } - pub async fn shutdown(&self) -> std::io::Result<()> { - // Send a shutdown command and wait for the writer task to flush. + pub async fn sync(&self) -> std::io::Result<()> { let (tx_done, rx_done) = oneshot::channel(); - if let Err(e) = self.tx.send(RolloutCmd::Shutdown(tx_done)).await { - // If the channel is closed, the writer is already gone – treat as success. + if let Err(e) = self + .tx + .send(RolloutCmd::Sync { + exit: false, + ack: tx_done, + }) + .await + { + warn!("failed to send rollout sync command: {e}"); + return Ok(()); + } + rx_done + .await + .map_err(|e| IoError::other(format!("failed waiting for rollout sync: {e}"))) + } + + pub async fn shutdown(&self) -> std::io::Result<()> { + let (tx_done, rx_done) = oneshot::channel(); + if let Err(e) = self + .tx + .send(RolloutCmd::Sync { + exit: true, + ack: tx_done, + }) + .await + { warn!("failed to send rollout shutdown command: {e}"); return Ok(()); } @@ -274,6 +300,7 @@ async fn rollout_writer( let mut buf = serde_json::to_vec(value)?; buf.push(b'\n'); file.write_all(&buf).await?; + file.flush().await?; Ok(()) } @@ -304,9 +331,6 @@ async fn rollout_writer( ResponseItem::Reasoning { .. } | ResponseItem::Other => {} } } - if let Err(e) = file.flush().await { - warn!("Failed to flush items: {e}"); - } } RolloutCmd::UpdateState(state) => { #[derive(Serialize)] @@ -322,16 +346,15 @@ async fn rollout_writer( if let Err(e) = write_json_line(&mut file, &line).await { warn!("Failed to write state: {e}"); } - if let Err(e) = file.flush().await { - warn!("Failed to flush state: {e}"); - } } - RolloutCmd::Shutdown(done_tx) => { + RolloutCmd::Sync { exit, ack } => { if let Err(e) = file.flush().await { - warn!("Failed to flush on shutdown: {e}"); + warn!("Failed to flush on sync: {e}"); + } + let _ = ack.send(()); + if exit { + break; } - let _ = done_tx.send(()); - break; } } } diff --git a/codex-rs/exec/src/lib.rs b/codex-rs/exec/src/lib.rs index 8861da6bf9..b557c89397 100644 --- a/codex-rs/exec/src/lib.rs +++ b/codex-rs/exec/src/lib.rs @@ -232,13 +232,6 @@ pub async fn run_main(cli: Cli, codex_linux_sandbox_exe: Option) -> any event_processor.process_event(event); if is_last_event { handle_last_message(last_assistant_message, last_message_file.as_deref())?; - // After task completes, flush rollout so external tests can observe all entries. - let flush_id = codex.submit(Op::FlushRollout).await?; - while let Some(event) = rx.recv().await { - if event.id == flush_id { - break; - } - } break; } }