diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 287bfe8f..2028c424 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -584,9 +584,19 @@ impl Session { cwd: PathBuf, reason: Option, ) -> oneshot::Receiver { + // Add the tx_approve callback to the map before sending the request. let (tx_approve, rx_approve) = oneshot::channel(); + let event_id = sub_id.clone(); + let prev_entry = { + let mut state = self.state.lock_unchecked(); + state.pending_approvals.insert(sub_id, tx_approve) + }; + if prev_entry.is_some() { + warn!("Overwriting existing pending approval for sub_id: {event_id}"); + } + let event = Event { - id: sub_id.clone(), + id: event_id, msg: EventMsg::ExecApprovalRequest(ExecApprovalRequestEvent { call_id, command, @@ -595,10 +605,6 @@ impl Session { }), }; let _ = self.tx_event.send(event).await; - { - let mut state = self.state.lock_unchecked(); - state.pending_approvals.insert(sub_id, tx_approve); - } rx_approve } @@ -610,9 +616,19 @@ impl Session { reason: Option, grant_root: Option, ) -> oneshot::Receiver { + // Add the tx_approve callback to the map before sending the request. let (tx_approve, rx_approve) = oneshot::channel(); + let event_id = sub_id.clone(); + let prev_entry = { + let mut state = self.state.lock_unchecked(); + state.pending_approvals.insert(sub_id, tx_approve) + }; + if prev_entry.is_some() { + warn!("Overwriting existing pending approval for sub_id: {event_id}"); + } + let event = Event { - id: sub_id.clone(), + id: event_id, msg: EventMsg::ApplyPatchApprovalRequest(ApplyPatchApprovalRequestEvent { call_id, changes: convert_apply_patch_to_protocol(action), @@ -621,10 +637,6 @@ impl Session { }), }; let _ = self.tx_event.send(event).await; - { - let mut state = self.state.lock_unchecked(); - state.pending_approvals.insert(sub_id, tx_approve); - } rx_approve }