Route injected plaintext agent mail through the native mailbox (#62292)

## Why

Raw response injection bypasses the native mailbox wake policy, so
injected agent messages cannot wake a sleeping thread through that
policy.

## What changed

In native Multi-Agent v2, route batches consisting entirely of
single-text agent messages with valid agent paths through the mailbox
as queue-only mail. Hold a residency pin across the batch so capacity
eviction cannot interrupt delivery between submissions. Mixed batches,
multipart or encrypted messages, and configurations with native tools
disabled continue to use raw response injection.

Only preempt model sampling for pending mail when the matching turn
still accepts mailbox delivery.

## Testing

Extend coverage for waking live and reloaded sleeping threads,
delivering older queued mail before a new task, and retaining mail in
order when a batch exceeds the submission channel capacity and
eviction is attempted.

GitOrigin-RevId: 3df60d2a4a9e7b193ef298e01f31c565ad8a2785
This commit is contained in:
jif
2026-11-18 14:53:00 +00:00
committed by copyberry
parent ad5dd4a110
commit bd3eaf7829
7 changed files with 231 additions and 37 deletions
+12 -14
View File
@@ -234,30 +234,28 @@ impl InputQueue {
.is_some_and(|(_, _, updates)| *updates.borrow())
}
/// Native mailbox backlog qualifies regardless of turn ID or delivery phase.
/// Injected mail qualifies only while its matching turn still accepts mail.
/// Native and injected mail qualify only while the matching turn still accepts mail.
pub(crate) async fn should_preempt_sampling_for_mail(
&self,
active_turn: &Mutex<Option<ActiveTurn>>,
sub_id: &str,
) -> bool {
if self.has_pending_mailbox_items().await {
return true;
}
let native_mail_pending = self.has_pending_mailbox_items().await;
let Some(turn_state) = self.turn_state_for_sub_id(active_turn, sub_id).await else {
return false;
};
let turn_state = turn_state.lock().await;
turn_state.accepts_mailbox_delivery_for_current_turn()
&& turn_state.pending_input.items.iter().any(|input| {
matches!(
input,
TurnInput::ResponseItem(ResponseItemEnvelope {
item: ResponseItem::AgentMessage { .. },
..
})
)
})
&& (native_mail_pending
|| turn_state.pending_input.items.iter().any(|input| {
matches!(
input,
TurnInput::ResponseItem(ResponseItemEnvelope {
item: ResponseItem::AgentMessage { .. },
..
})
)
}))
}
pub(crate) async fn has_trigger_turn_mailbox_items(&self) -> bool {