@@ -128,6 +128,24 @@ enum LegacyQueuePolicy {
128128 AuthoritativeControl ,
129129}
130130
131+ /// Turn lifecycle facts a consumer cannot reconstruct from later stream data.
132+ /// Losing one leaves a projection running forever, or settled without the
133+ /// durable history fence that makes it reconcile canonical content, until a
134+ /// full reload. Text and tool stream data stay best-effort: a delivered
135+ /// lifecycle fence repairs them. Each turn emits only a handful of these, so
136+ /// admitting them past the data budget stays bounded.
137+ fn is_turn_lifecycle_fact ( event : & AgenticEvent ) -> bool {
138+ matches ! (
139+ event,
140+ AgenticEvent :: DialogTurnStarted { .. }
141+ | AgenticEvent :: DialogTurnCompleted { .. }
142+ | AgenticEvent :: DialogTurnCancelled { .. }
143+ | AgenticEvent :: DialogTurnFailed { .. }
144+ | AgenticEvent :: SessionStateChanged { .. }
145+ | AgenticEvent :: SessionHistoryChanged { .. }
146+ )
147+ }
148+
131149fn interrupted_control_identity ( event : & AgenticEvent ) -> Option < ( & str , & str , u32 ) > {
132150 match event {
133151 AgenticEvent :: DialogTurnInterrupted {
@@ -309,6 +327,14 @@ impl EventQueue {
309327 priority : Option < EventPriority > ,
310328 legacy_policy : LegacyQueuePolicy ,
311329 ) -> EventBusResult < ( String , Option < LegacyDequeueAck > ) > {
330+ // A best-effort producer cannot know that the queue is full; never let
331+ // that silently discard a lifecycle fact (see `is_turn_lifecycle_fact`).
332+ let legacy_policy = match legacy_policy {
333+ LegacyQueuePolicy :: BestEffort if is_turn_lifecycle_fact ( & event) => {
334+ LegacyQueuePolicy :: AuthoritativeControl
335+ }
336+ policy => policy,
337+ } ;
312338 let priority = priority. unwrap_or_else ( || event. default_priority ( ) ) ;
313339 let envelope = EventEnvelope :: new ( event, priority) ;
314340 let event_id = envelope. id . clone ( ) ;
@@ -1038,9 +1064,13 @@ mod tests {
10381064 for session_id in [ "first" , "second" ] {
10391065 queue
10401066 . enqueue (
1041- AgenticEvent :: SessionStateChanged {
1067+ AgenticEvent :: TextChunk {
10421068 session_id : session_id. to_string ( ) ,
1043- new_state : "idle" . to_string ( ) ,
1069+ turn_id : "turn" . to_string ( ) ,
1070+ round_id : "round" . to_string ( ) ,
1071+ attempt_id : None ,
1072+ attempt_index : None ,
1073+ text : "data" . to_string ( ) ,
10441074 } ,
10451075 None ,
10461076 )
@@ -1069,6 +1099,73 @@ mod tests {
10691099 ) ;
10701100 }
10711101
1102+ #[ tokio:: test]
1103+ async fn full_queue_keeps_turn_lifecycle_facts_of_another_session ( ) {
1104+ let queue = EventQueue :: new ( EventQueueConfig {
1105+ max_queue_size : 2 ,
1106+ batch_size : 16 ,
1107+ } ) ;
1108+ let chunk = |session_id : & str , text : & str | AgenticEvent :: TextChunk {
1109+ session_id : session_id. to_string ( ) ,
1110+ turn_id : "turn" . to_string ( ) ,
1111+ round_id : "round" . to_string ( ) ,
1112+ attempt_id : None ,
1113+ attempt_index : None ,
1114+ text : text. to_string ( ) ,
1115+ } ;
1116+ // Another session floods the data budget.
1117+ for text in [ "flood-1" , "flood-2" ] {
1118+ queue. enqueue ( chunk ( "busy" , text) , None ) . await . unwrap ( ) ;
1119+ }
1120+ // This session's last chunk is best-effort data and may be skipped...
1121+ queue. enqueue ( chunk ( "quiet" , "tail" ) , None ) . await . unwrap ( ) ;
1122+ // ...but its settlement and history fence must reach the consumer.
1123+ queue
1124+ . enqueue (
1125+ AgenticEvent :: DialogTurnCompleted {
1126+ session_id : "quiet" . to_string ( ) ,
1127+ turn_id : "turn" . to_string ( ) ,
1128+ total_rounds : 1 ,
1129+ total_tools : 0 ,
1130+ duration_ms : 1 ,
1131+ partial_recovery_reason : None ,
1132+ success : Some ( true ) ,
1133+ finish_reason : None ,
1134+ has_final_response : Some ( true ) ,
1135+ background_delivery : None ,
1136+ } ,
1137+ None ,
1138+ )
1139+ . await
1140+ . unwrap ( ) ;
1141+ queue
1142+ . enqueue (
1143+ AgenticEvent :: SessionHistoryChanged {
1144+ session_id : "quiet" . to_string ( ) ,
1145+ settled_turn_id : Some ( "turn" . to_string ( ) ) ,
1146+ } ,
1147+ None ,
1148+ )
1149+ . await
1150+ . unwrap ( ) ;
1151+
1152+ let delivered = queue. dequeue_batch ( 16 ) . await ;
1153+ let quiet: Vec < _ > = delivered
1154+ . iter ( )
1155+ . filter ( |envelope| envelope. event . session_id ( ) == Some ( "quiet" ) )
1156+ . map ( |envelope| & envelope. event )
1157+ . collect ( ) ;
1158+ assert ! ( quiet
1159+ . iter( )
1160+ . any( |event| matches!( event, AgenticEvent :: DialogTurnCompleted { .. } ) ) ) ;
1161+ assert ! ( quiet
1162+ . iter( )
1163+ . any( |event| matches!( event, AgenticEvent :: SessionHistoryChanged { .. } ) ) ) ;
1164+ assert ! ( !quiet
1165+ . iter( )
1166+ . any( |event| matches!( event, AgenticEvent :: TextChunk { .. } ) ) ) ;
1167+ }
1168+
10721169 #[ tokio:: test]
10731170 async fn default_sized_broadcast_preserves_bursts_above_legacy_1024_limit ( ) {
10741171 let queue = EventQueue :: new ( EventQueueConfig :: default ( ) ) ;
0 commit comments