@@ -146,10 +146,13 @@ pub struct EventQueue {
146146 /// Used to detect silent reply loss when ACP reports Ok but
147147 /// `buzz messages send` never landed (#2459).
148148 in_flight_self_publishes : HashMap < Uuid , u64 > ,
149- /// Channels whose Ok turn is waiting on a deferred silent-reply check.
150- /// Counts in [`in_flight_self_publishes`] survive [`mark_complete`] while
151- /// a watch is armed so a late ws echo can still clear the notice.
152- silent_reply_watches : HashSet < Uuid > ,
149+ /// Channels whose Ok turn is waiting on a deferred silent-reply check,
150+ /// keyed by a per-channel generation. Counts in
151+ /// [`in_flight_self_publishes`] survive [`mark_complete`] while a watch is
152+ /// armed so a late ws echo can still clear the notice. Generation IDs stop
153+ /// a stale grace timer from consuming a newer turn's watch on the same
154+ /// channel (#2459).
155+ silent_reply_watches : HashMap < Uuid , u64 > ,
153156 retry_after : HashMap < Uuid , Instant > ,
154157 /// Per-channel retry attempt counter for exponential backoff / dead-lettering.
155158 retry_counts : HashMap < Uuid , u32 > ,
@@ -192,7 +195,7 @@ impl EventQueue {
192195 in_flight_deadlines : HashMap :: new ( ) ,
193196 in_flight_batch_sizes : HashMap :: new ( ) ,
194197 in_flight_self_publishes : HashMap :: new ( ) ,
195- silent_reply_watches : HashSet :: new ( ) ,
198+ silent_reply_watches : HashMap :: new ( ) ,
196199 retry_after : HashMap :: new ( ) ,
197200 retry_counts : HashMap :: new ( ) ,
198201 dedup_mode,
@@ -407,7 +410,7 @@ impl EventQueue {
407410 self . in_flight_batch_sizes . remove ( & channel_id) ;
408411 // Keep publish counts while a silent-reply grace watch is armed so a
409412 // late echo can still arrive after turn completion (#2459).
410- if !self . silent_reply_watches . contains ( & channel_id) {
413+ if !self . silent_reply_watches . contains_key ( & channel_id) {
411414 self . in_flight_self_publishes . remove ( & channel_id) ;
412415 }
413416 let now = Instant :: now ( ) ;
@@ -673,7 +676,7 @@ impl EventQueue {
673676 /// channel turn, or during a deferred silent-reply grace watch.
674677 pub fn note_self_publish ( & mut self , channel_id : Uuid ) {
675678 if self . in_flight_channels . contains ( & channel_id)
676- || self . silent_reply_watches . contains ( & channel_id)
679+ || self . silent_reply_watches . contains_key ( & channel_id)
677680 {
678681 * self . in_flight_self_publishes . entry ( channel_id) . or_insert ( 0 ) += 1 ;
679682 }
@@ -688,17 +691,33 @@ impl EventQueue {
688691
689692 /// Arm a post-Ok silent-reply watch so publish counts survive
690693 /// [`mark_complete`] until [`take_silent_reply_watch`].
691- pub fn arm_silent_reply_watch ( & mut self , channel_id : Uuid ) {
692- self . silent_reply_watches . insert ( channel_id) ;
694+ ///
695+ /// Returns the watch generation. Callers must pass that generation back to
696+ /// [`take_silent_reply_watch`] so a stale grace timer cannot consume a
697+ /// newer turn's watch on the same channel.
698+ pub fn arm_silent_reply_watch ( & mut self , channel_id : Uuid ) -> u64 {
699+ let next = self
700+ . silent_reply_watches
701+ . get ( & channel_id)
702+ . copied ( )
703+ . unwrap_or ( 0 )
704+ . wrapping_add ( 1 ) ;
705+ self . silent_reply_watches . insert ( channel_id, next) ;
706+ next
693707 }
694708
695709 /// End a silent-reply watch and take the accumulated publish count.
696- /// Returns `None` if no watch was armed (already consumed / never armed).
697- pub fn take_silent_reply_watch ( & mut self , channel_id : Uuid ) -> Option < u64 > {
698- if !self . silent_reply_watches . remove ( & channel_id) {
699- return None ;
710+ ///
711+ /// Returns `None` if no matching generation is armed (already consumed,
712+ /// never armed, or superseded by a later Ok on the same channel).
713+ pub fn take_silent_reply_watch ( & mut self , channel_id : Uuid , generation : u64 ) -> Option < u64 > {
714+ match self . silent_reply_watches . get ( & channel_id) . copied ( ) {
715+ Some ( current) if current == generation => {
716+ self . silent_reply_watches . remove ( & channel_id) ;
717+ Some ( self . take_self_publishes ( channel_id) )
718+ }
719+ _ => None ,
700720 }
701- Some ( self . take_self_publishes ( channel_id) )
702721 }
703722
704723 // ── Goose-native steer withhold (side table) ──────────────────────────
@@ -4823,19 +4842,41 @@ mod tests {
48234842 // Re-note during in-flight, arm watch, then complete — count must survive.
48244843 q. in_flight_channels . insert ( ch) ;
48254844 q. note_self_publish ( ch) ;
4826- q. arm_silent_reply_watch ( ch) ;
4845+ let gen = q. arm_silent_reply_watch ( ch) ;
48274846 q. mark_complete ( ch) ;
48284847 assert ! ( !q. is_channel_in_flight( ch) ) ;
48294848 // Late echo after completion still counts.
48304849 q. note_self_publish ( ch) ;
4831- assert_eq ! ( q. take_silent_reply_watch( ch) , Some ( 2 ) ) ;
4850+ assert_eq ! ( q. take_silent_reply_watch( ch, gen ) , Some ( 2 ) ) ;
48324851 // Second take is a no-op.
4833- assert_eq ! ( q. take_silent_reply_watch( ch) , None ) ;
4852+ assert_eq ! ( q. take_silent_reply_watch( ch, gen ) , None ) ;
48344853 // Without a watch, post-complete echoes are ignored.
48354854 q. note_self_publish ( ch) ;
48364855 assert_eq ! ( q. take_self_publishes( ch) , 0 ) ;
48374856 }
48384857
4858+ #[ test]
4859+ fn silent_reply_watch_generation_ignores_stale_timer ( ) {
4860+ let mut q = EventQueue :: new ( DedupMode :: Queue ) ;
4861+ let ch = Uuid :: new_v4 ( ) ;
4862+ q. in_flight_channels . insert ( ch) ;
4863+ q. note_self_publish ( ch) ;
4864+ let first = q. arm_silent_reply_watch ( ch) ;
4865+ q. mark_complete ( ch) ;
4866+
4867+ // A second Ok on the same channel re-arms with a new generation.
4868+ q. in_flight_channels . insert ( ch) ;
4869+ let second = q. arm_silent_reply_watch ( ch) ;
4870+ assert_ne ! ( first, second) ;
4871+ q. mark_complete ( ch) ;
4872+ q. note_self_publish ( ch) ;
4873+
4874+ // Stale first timer must not consume the newer watch (count still
4875+ // includes the earlier turn's publish — generations only gate take).
4876+ assert_eq ! ( q. take_silent_reply_watch( ch, first) , None ) ;
4877+ assert_eq ! ( q. take_silent_reply_watch( ch, second) , Some ( 2 ) ) ;
4878+ }
4879+
48394880 #[ test]
48404881 fn mark_complete_clears_publishes_without_silent_reply_watch ( ) {
48414882 let mut q = EventQueue :: new ( DedupMode :: Queue ) ;
0 commit comments