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
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
"""
Message Acknowledgment System for AegisLM Framework

Production-ready message acknowledgment system with reliable delivery,
retry mechanisms, and comprehensive tracking for WebSocket communications.
"""

import asyncio
import uuid
import json
import logging
from typing import Dict, Set, Optional, List, Any
from datetime import datetime, timedelta
from collections import deque
from dataclasses import dataclass, field

logger = logging.getLogger(__name__)


@dataclass
class PendingMessage:
    """Pending message information for acknowledgment tracking."""
    message_id: str
    connection_id: str
    room_id: Optional[str]
    message: Dict[str, Any]
    sent_at: datetime
    retry_count: int = 0
    max_retries: int = 3
    ack_timeout: int = 30  # seconds
    last_retry: Optional[datetime] = None


@dataclass
class AcknowledgmentStats:
    """Message acknowledgment statistics."""
    total_sent: int = 0
    total_acknowledged: int = 0
    total_failed: int = 0
    total_retries: int = 0
    average_delivery_time: float = 0.0
    success_rate: float = 0.0


class MessageAcknowledgmentSystem:
    """
    Ensure reliable message delivery with acknowledgments.
    
    Provides message tracking, retry mechanisms, delivery guarantees,
    and comprehensive statistics for WebSocket communications.
    """
    
    def __init__(self, websocket_manager, ack_timeout: int = 30, max_retries: int = 3, cleanup_interval: int = 60):
        """
        Initialize message acknowledgment system.
        
        Args:
            websocket_manager: WebSocket manager instance
            ack_timeout: Timeout for message acknowledgment in seconds
            max_retries: Maximum number of retry attempts
            cleanup_interval: Interval for cleanup operations in seconds
        """
        self.websocket_manager = websocket_manager
        self.ack_timeout = ack_timeout
        self.max_retries = max_retries
        self.cleanup_interval = cleanup_interval
        self.pending_acks: Dict[str, PendingMessage] = {}
        self.ack_stats = AcknowledgmentStats()
        self._cleanup_task = None
        self._delivery_times: deque = deque(maxlen=1000)
    
    async def start_ack_system(self):
        """Start acknowledgment cleanup task."""
        if self._cleanup_task is None:
            self._cleanup_task = asyncio.create_task(self._periodic_ack_cleanup())
            logger.info("Message acknowledgment system started")
    
    async def stop_ack_system(self):
        """Stop acknowledgment cleanup task."""
        if self._cleanup_task:
            self._cleanup_task.cancel()
            try:
                await self._cleanup_task
            except asyncio.CancelledError:
                pass
            self._cleanup_task = None
            logger.info("Message acknowledgment system stopped")
    
    async def send_with_ack(self, connection_id: str, message: Dict, room_id: Optional[str] = None) -> str:
        """
        Send message with acknowledgment tracking.
        
        Args:
            connection_id: Target connection ID
            message: Message to send
            room_id: Optional room ID for broadcasting
            
        Returns:
            str: Message ID for tracking
            
        Raises:
            Exception: If send fails
        """
        message_id = str(uuid.uuid4())
        
        # Add acknowledgment metadata
        ack_message = {
            **message,
            'message_id': message_id,
            'requires_ack': True,
            'timestamp': datetime.utcnow().isoformat(),
            'sent_at': datetime.utcnow().isoformat()
        }
        
        # Store pending acknowledgment
        pending_msg = PendingMessage(
            message_id=message_id,
            connection_id=connection_id,
            room_id=room_id,
            message=message,
            sent_at=datetime.utcnow(),
            retry_count=0,
            max_retries=self.max_retries,
            ack_timeout=self.ack_timeout
        )
        
        self.pending_acks[message_id] = pending_msg
        
        try:
            # Send message
            if room_id:
                await self.websocket_manager.broadcast_to_room(room_id, ack_message)
            else:
                await self.websocket_manager.send_to_connection(connection_id, ack_message)
            
            self.ack_stats.total_sent += 1
            logger.debug(f"Message sent with acknowledgment tracking: {message_id}")
            
            return message_id
            
        except Exception as e:
            # Remove from pending if send failed
            self.pending_acks.pop(message_id, None)
            self.ack_stats.total_failed += 1
            logger.error(f"Failed to send message {message_id}: {e}")
            raise e
    
    async def send_batch_with_ack(self, messages: List[Dict[str, Any]], connection_id: str, room_id: Optional[str] = None) -> List[str]:
        """
        Send multiple messages with acknowledgment tracking.
        
        Args:
            messages: List of messages to send
            connection_id: Target connection ID
            room_id: Optional room ID for broadcasting
            
        Returns:
            List[str]: Message IDs for tracking
        """
        message_ids = []
        
        for message in messages:
            try:
                message_id = await self.send_with_ack(connection_id, message, room_id)
                message_ids.append(message_id)
            except Exception as e:
                logger.error(f"Failed to send batch message: {e}")
                # Continue with other messages
        
        return message_ids
    
    async def handle_acknowledgment(self, connection_id: str, message_id: str) -> bool:
        """
        Handle message acknowledgment from client.
        
        Args:
            connection_id: Connection ID that sent acknowledgment
            message_id: Message ID being acknowledged
            
        Returns:
            bool: True if acknowledgment was processed successfully
        """
        if message_id not in self.pending_acks:
            logger.warning(f"Received ack for unknown message: {message_id}")
            return False
        
        pending_msg = self.pending_acks.pop(message_id)
        
        # Calculate delivery time
        delivery_time = (datetime.utcnow() - pending_msg.sent_at).total_seconds()
        self._delivery_times.append(delivery_time)
        
        # Update statistics
        self.ack_stats.total_acknowledged += 1
        self._update_success_rate()
        
        logger.debug(f"Message {message_id} acknowledged after {delivery_time:.2f}s")
        
        return True
    
    async def handle_nack(self, connection_id: str, message_id: str, reason: str) -> bool:
        """
        Handle negative acknowledgment from client.
        
        Args:
            connection_id: Connection ID that sent NACK
            message_id: Message ID being NACKed
            reason: Reason for NACK
            
        Returns:
            bool: True if NACK was processed successfully
        """
        if message_id not in self.pending_acks:
            logger.warning(f"Received NACK for unknown message: {message_id}")
            return False
        
        pending_msg = self.pending_acks[message_id]
        
        logger.warning(f"Message {message_id} NACKed: {reason}")
        
        # Try to resend if retries available
        if pending_msg.retry_count < pending_msg.max_retries:
            await self._retry_message(message_id, pending_msg)
        else:
            # Max retries exceeded, mark as failed
            self.pending_acks.pop(message_id, None)
            self.ack_stats.total_failed += 1
            self._update_success_rate()
            logger.error(f"Message {message_id} failed after {pending_msg.max_retries} retries")
        
        return True
    
    async def _periodic_ack_cleanup(self):
        """Periodic cleanup of expired acknowledgments and retry failed messages."""
        while True:
            try:
                await asyncio.sleep(10)  # Check every 10 seconds
                cleanup_result = await self.cleanup_expired_acks()
                if cleanup_result['cleaned'] > 0 or cleanup_result['retried'] > 0:
                    logger.debug(f"Ack cleanup: {cleanup_result}")
            except asyncio.CancelledError:
                break
            except Exception as e:
                logger.error(f"Ack cleanup error: {e}")
    
    async def cleanup_expired_acks(self) -> Dict[str, Any]:
        """
        Clean up expired acknowledgments and retry failed messages.
        
        Returns:
            Dict containing cleanup statistics
        """
        current_time = datetime.utcnow()
        expired_messages = []
        retry_messages = []
        
        for message_id, pending_msg in self.pending_acks.items():
            time_since_sent = current_time - pending_msg.sent_at
            
            if time_since_sent.total_seconds() > pending_msg.ack_timeout:
                if pending_msg.retry_count < pending_msg.max_retries:
                    retry_messages.append(message_id)
                else:
                    expired_messages.append(message_id)
        
        # Handle expired messages
        for message_id in expired_messages:
            pending_msg = self.pending_acks.pop(message_id, None)
            self.ack_stats.total_failed += 1
            logger.error(f"Message {message_id} expired after {pending_msg.max_retries} retries")
        
        # Retry messages
        for message_id in retry_messages:
            pending_msg = self.pending_acks[message_id]
            await self._retry_message(message_id, pending_msg)
        
        # Update statistics
        self._update_success_rate()
        
        return {
            'total_pending': len(self.pending_acks),
            'expired_count': len(expired_messages),
            'retry_count': len(retry_messages),
            'cleaned': len(expired_messages),
            'retried': len(retry_messages)
        }
    
    async def _retry_message(self, message_id: str, pending_msg: PendingMessage):
        """Retry sending a message."""
        try:
            # Update retry count
            pending_msg.retry_count += 1
            pending_msg.last_retry = datetime.utcnow()
            
            # Add retry metadata
            retry_message = {
                **pending_msg.message,
                'message_id': message_id,
                'requires_ack': True,
                'retry': pending_msg.retry_count,
                'timestamp': datetime.utcnow().isoformat(),
                'original_sent_at': pending_msg.sent_at.isoformat()
            }
            
            # Resend message
            if pending_msg.room_id:
                await self.websocket_manager.broadcast_to_room(pending_msg.room_id, retry_message)
            else:
                await self.websocket_manager.send_to_connection(pending_msg.connection_id, retry_message)
            
            self.ack_stats.total_retries += 1
            logger.info(f"Retried message {message_id} (attempt {pending_msg.retry_count})")
            
        except Exception as e:
            logger.error(f"Failed to retry message {message_id}: {e}")
            # Mark as failed
            self.pending_acks.pop(message_id, None)
            self.ack_stats.total_failed += 1
    
    def _update_success_rate(self):
        """Update success rate statistics."""
        total = self.ack_stats.total_sent
        if total > 0:
            self.ack_stats.success_rate = (self.ack_stats.total_acknowledged / total) * 100
        
        # Update average delivery time
        if self._delivery_times:
            self.ack_stats.average_delivery_time = sum(self._delivery_times) / len(self._delivery_times)
    
    async def get_pending_ack_count(self) -> int:
        """Get number of pending acknowledgments."""
        return len(self.pending_acks)
    
    async def get_pending_acks_for_connection(self, connection_id: str) -> List[Dict[str, Any]]:
        """Get pending acknowledgments for a specific connection."""
        pending = []
        
        for message_id, pending_msg in self.pending_acks.items():
            if pending_msg.connection_id == connection_id:
                pending.append({
                    'message_id': message_id,
                    'sent_at': pending_msg.sent_at.isoformat(),
                    'retry_count': pending_msg.retry_count,
                    'max_retries': pending_msg.max_retries,
                    'time_since_sent': (datetime.utcnow() - pending_msg.sent_at).total_seconds()
                })
        
        return pending
    
    async def get_ack_statistics(self) -> AcknowledgmentStats:
        """Get acknowledgment statistics."""
        self._update_success_rate()
        return self.ack_stats
    
    async def force_retry_message(self, message_id: str) -> bool:
        """
        Force retry of a specific message.
        
        Args:
            message_id: Message ID to retry
            
        Returns:
            bool: True if retry was initiated
        """
        if message_id not in self.pending_acks:
            return False
        
        pending_msg = self.pending_acks[message_id]
        
        if pending_msg.retry_count < pending_msg.max_retries:
            await self._retry_message(message_id, pending_msg)
            return True
        
        return False
    
    async def cancel_pending_message(self, message_id: str) -> bool:
        """
        Cancel a pending message.
        
        Args:
            message_id: Message ID to cancel
            
        Returns:
            bool: True if message was cancelled
        """
        if message_id in self.pending_acks:
            self.pending_acks.pop(message_id, None)
            self.ack_stats.total_failed += 1
            self._update_success_rate()
            logger.info(f"Cancelled pending message: {message_id}")
            return True
        
        return False
    
    async def get_delivery_report(self) -> Dict[str, Any]:
        """Get comprehensive delivery report."""
        current_time = datetime.utcnow()
        
        # Analyze pending messages by age
        age_buckets = {
            '0-10s': 0,
            '10-30s': 0,
            '30-60s': 0,
            '60s+': 0
        }
        
        for pending_msg in self.pending_acks.values():
            age = (current_time - pending_msg.sent_at).total_seconds()
            if age <= 10:
                age_buckets['0-10s'] += 1
            elif age <= 30:
                age_buckets['10-30s'] += 1
            elif age <= 60:
                age_buckets['30-60s'] += 1
            else:
                age_buckets['60s+'] += 1
        
        return {
            'statistics': {
                'total_sent': self.ack_stats.total_sent,
                'total_acknowledged': self.ack_stats.total_acknowledged,
                'total_failed': self.ack_stats.total_failed,
                'total_retries': self.ack_stats.total_retries,
                'pending_count': len(self.pending_acks),
                'success_rate': self.ack_stats.success_rate,
                'average_delivery_time': self.ack_stats.average_delivery_time
            },
            'pending_by_age': age_buckets,
            'retry_distribution': self._get_retry_distribution(),
            'timestamp': current_time.isoformat()
        }
    
    def _get_retry_distribution(self) -> Dict[str, int]:
        """Get distribution of retry counts."""
        distribution = {str(i): 0 for i in range(self.max_retries + 1)}
        distribution[f'{self.max_retries}+'] = 0
        
        for pending_msg in self.pending_acks.values():
            if pending_msg.retry_count <= self.max_retries:
                distribution[str(pending_msg.retry_count)] += 1
            else:
                distribution[f'{self.max_retries}+'] += 1
        
        return distribution
    
    async def reset_statistics(self):
        """Reset acknowledgment statistics."""
        self.ack_stats = AcknowledgmentStats()
        self._delivery_times.clear()
        logger.info("Message acknowledgment statistics reset")
    
    async def cleanup_connection_acks(self, connection_id: str) -> int:
        """
        Clean up all pending acknowledgments for a connection.
        
        Args:
            connection_id: Connection ID to clean up
            
        Returns:
            int: Number of acknowledgments cleaned up
        """
        to_remove = []
        
        for message_id, pending_msg in self.pending_acks.items():
            if pending_msg.connection_id == connection_id:
                to_remove.append(message_id)
        
        for message_id in to_remove:
            self.pending_acks.pop(message_id, None)
            self.ack_stats.total_failed += 1
        
        logger.info(f"Cleaned up {len(to_remove)} pending acknowledgments for connection {connection_id}")
        
        return len(to_remove)


# Factory function
def create_message_acknowledgment_system(websocket_manager, ack_timeout: int = 30, max_retries: int = 3, cleanup_interval: int = 60) -> MessageAcknowledgmentSystem:
    """
    Create a message acknowledgment system instance.
    
    Args:
        websocket_manager: WebSocket manager instance
        ack_timeout: Timeout for acknowledgments in seconds
        max_retries: Maximum retry attempts
        cleanup_interval: Cleanup interval in seconds
        
    Returns:
        MessageAcknowledgmentSystem: Configured system
    """
    return MessageAcknowledgmentSystem(websocket_manager, ack_timeout, max_retries, cleanup_interval)