Wake idle root threads on persisted agent messages (#63312)

## Why

Plaintext agent messages injected through `thread/inject_items` could
be saved to root-thread history without waking the thread, even when
agent-message wakes were enabled.

## What changed

After persisting a batch of plaintext agent messages with unspecified
retention, let the native multi-agent v2 root thread's mailbox scheduler
apply its usual wake and turn-admission rules. Use the recorded history
sequence to skip a wake if another turn already read the messages and
to deliver the mail directly from history without re-enqueueing it.

## Testing

Cover app-server injection with wakes enabled and disabled, wake a
reloaded sleeping root with its previous turn context, and verify that
a paused turn-admission gate defers the wake and the eventual model
request contains the message once.

GitOrigin-RevId: 5fde8062a179fd7a7919d240373592f2dbb4a389
This commit is contained in:
jif
2026-11-23 11:16:31 +00:00
committed by copyberry
parent a1604a8c05
commit ca891cecdc
9 changed files with 237 additions and 101 deletions
+4
View File
@@ -88,6 +88,8 @@ pub(crate) struct SessionState {
/// Persisted origin of the session base instructions, when known.
pub(crate) base_instructions_provenance: Option<BaseInstructionsProvenance>,
pub(crate) history: ContextManager,
/// Durable injected mail awaiting an ordinary mailbox wake; never re-enqueued as input.
pub(crate) pending_recorded_mail_sequence: u64,
/// Newly recorded files awaiting best-effort lookup after the input batch is durable.
pub(crate) pending_audio_duration_ids: BTreeSet<String>,
/// Cancels work bound to discarded history or a superseded Guardian evidence policy.
@@ -153,6 +155,7 @@ impl SessionState {
base_instructions_provenance: None,
history,
pending_audio_duration_ids: BTreeSet::new(),
pending_recorded_mail_sequence: 0,
history_reset: CancellationToken::new(),
latest_rate_limits: None,
latest_token_usage_record: None,
@@ -241,6 +244,7 @@ impl SessionState {
HistoryReplacement::Reset => {
self.turn_attribution = None;
self.restored_turn_routing = None;
self.pending_recorded_mail_sequence = 0;
self.history.replace_annotated(items);
true
}