wait for flush after task complete

This commit is contained in:
Ahmed Ibrahim
2025-07-20 18:09:22 -07:00
parent 90f9c6ee9b
commit 24e05336f4
4 changed files with 47 additions and 43 deletions
+9 -18
View File
@@ -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<Session>, sub_id: String, input: Vec<InputItem>) {
}
}
}
// 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,
-3
View File
@@ -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
+38 -15
View File
@@ -63,7 +63,10 @@ pub(crate) struct RolloutRecorder {
enum RolloutCmd {
AddItems(Vec<ResponseItem>),
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;
}
}
}
-7
View File
@@ -232,13 +232,6 @@ pub async fn run_main(cli: Cli, codex_linux_sandbox_exe: Option<PathBuf>) -> 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;
}
}