fix: race pending (#16561)

This commit is contained in:
jif-oai authored and GitHub committed 2026-04-02 15:31:30 +02:00
1 parent 97df35c74f
commit 627299c551
5 files changed
+250 -12

No files matched your search

+24
View File
@@ -29,6 +29,17 @@ pub(crate) struct ActiveTurn {
pub(crate) turn_state: Arc<Mutex<TurnState>>,
}
/// Whether mailbox deliveries should still be folded into the current turn.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(crate) enum MailboxDeliveryPhase {
/// Incoming mailbox messages can still be consumed by the current turn.
#[default]
CurrentTurn,
/// The current turn already emitted visible final answer text; mailbox
/// messages should remain queued for a later turn.
NextTurn,
}
impl Default for ActiveTurn {
fn default() -> Self {
Self {
@@ -81,6 +92,7 @@ pub(crate) struct TurnState {
pending_elicitations: HashMap<(String, RequestId), oneshot::Sender<ElicitationResponse>>,
pending_dynamic_tools: HashMap<String, oneshot::Sender<DynamicToolResponse>>,
pending_input: Vec<ResponseInputItem>,
mailbox_delivery_phase: MailboxDeliveryPhase,
granted_permissions: Option<PermissionProfile>,
pub(crate) tool_calls: u64,
pub(crate) token_usage_at_turn_start: TokenUsage,
@@ -202,6 +214,18 @@ impl TurnState {
!self.pending_input.is_empty()
}
pub(crate) fn defer_mailbox_delivery_to_next_turn(&mut self) {
self.mailbox_delivery_phase = MailboxDeliveryPhase::NextTurn;
}
pub(crate) fn accept_mailbox_delivery_for_current_turn(&mut self) {
self.mailbox_delivery_phase = MailboxDeliveryPhase::CurrentTurn;
}
pub(crate) fn accepts_mailbox_delivery_for_current_turn(&self) -> bool {
self.mailbox_delivery_phase == MailboxDeliveryPhase::CurrentTurn
}
pub(crate) fn record_granted_permissions(&mut self, permissions: PermissionProfile) {
self.granted_permissions =
merge_permission_profiles(self.granted_permissions.as_ref(), Some(&permissions));