File size: 29,933 Bytes
c2992d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
"""
tool_agent — Agente Especializado para Ferramentas (Tools).

Implementa:
- 2.1. Memória individualizada de instruções e dados por ferramenta
- 2.2. Suporte Model Context Protocol (MCP)
- 2.3. Resiliência: falha isolada, restart, circuit breaker
- 2.3.1. Arquitetura distribuída otimizada (análise matemática)

Arquitetura recomendada (análise):
- Hybrid: 1 Agente Coordenador + N Agentes Especialistas (1 por ferramenta)
- Vantagens sobre Multi-Agent (1 agente p/ tudo) e Single-Agent (1 p/ todas):
  * Isolamento de falhas (1 ferramenta falha → só aquele agente reinicia)
  * Especialização (cada agente tem memória otimizada para sua ferramenta)
  * Paralelismo (agentes independentes rodam em paralelo)
  * Escalabilidade (adicionar ferramenta = adicionar agente)

Matemática da arquitetura:
- Latência esperada: T = max_i(T_i) + T_coord (paralelo + coordenação)
- Vs Single-Agent: T_single = Σ T_i (sequencial)
- Speedup: S = Σ T_i / (max T_i + T_coord) ≈ N / (1 + T_coord/max T_i)
- Quando T_coord << max T_i: S → N (speedup linear)
"""

from __future__ import annotations

import asyncio
import hashlib
import json
import os
import time
import threading
from collections import defaultdict, deque
from concurrent.futures import Future, ThreadPoolExecutor, as_completed
from dataclasses import dataclass, field
from enum import Enum
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union

import torch
import torch.nn as nn


# ============================================================================
# Exceções
# ============================================================================

class ToolError(RuntimeError):
    """Erro genérico de ferramenta."""


class ToolTimeoutError(ToolError):
    """Timeout ao executar ferramenta."""


class ToolCircuitBreakerOpen(ToolError):
    """Circuit breaker aberto — ferramenta em estado de falha."""


class MCPConnectionError(ToolError):
    """Erro de conexão MCP."""


class ToolMemoryError(ToolError):
    """Erro de memória da ferramenta."""


# ============================================================================
# 2.1. ToolMemory — memória individualizada por ferramenta
# ============================================================================

class ToolMemory:
    """Memória individualizada de instruções e dados por ferramenta.

    Cada ferramenta tem sua própria ToolMemory, contendo:
    - instructions: instruções de uso (templates, exemplos)
    - data_cache: cache de resultados anteriores (key → value)
    - error_history: histórico de erros (para diagnóstico)
    - usage_stats: estatísticas de uso (n_calls, n_success, n_failures)

    Persistência: salva/carrega de disco (JSON).
    """

    def __init__(
        self,
        tool_name: str,
        max_cache_size: int = 1000,
        max_instructions: int = 100,
        max_error_history: int = 50,
        persist_path: Optional[str] = None,
    ):
        self.tool_name = tool_name
        self.max_cache_size = max_cache_size
        self.max_instructions = max_instructions
        self.max_error_history = max_error_history
        self.persist_path = persist_path
        self._lock = threading.Lock()
        # Estado.
        self.instructions: List[Dict[str, Any]] = []
        self.data_cache: Dict[str, Any] = {}  # key → cached result
        self.error_history: deque = deque(maxlen=max_error_history)
        self.usage_stats = {
            "n_calls": 0,
            "n_success": 0,
            "n_failures": 0,
            "total_time_s": 0.0,
            "avg_time_s": 0.0,
        }
        # Carrega do disco se disponível.
        if persist_path and os.path.exists(persist_path):
            self.load()

    def add_instruction(
        self,
        instruction: str,
        context: Optional[Dict[str, Any]] = None,
        priority: int = 0,
    ) -> None:
        """Adiciona instrução de uso."""
        with self._lock:
            entry = {
                "instruction": instruction,
                "context": context or {},
                "priority": priority,
                "timestamp": time.time(),
            }
            self.instructions.append(entry)
            # Ordena por prioridade (maior = mais importante).
            self.instructions.sort(key=lambda x: -x["priority"])
            # Limita tamanho.
            if len(self.instructions) > self.max_instructions:
                self.instructions = self.instructions[:self.max_instructions]
            self._maybe_persist()

    def get_instructions(
        self,
        context_filter: Optional[Dict[str, Any]] = None,
        top_k: int = 10,
    ) -> List[Dict[str, Any]]:
        """Recupera instruções relevantes."""
        with self._lock:
            if context_filter is None:
                return self.instructions[:top_k]
            # Filtra por contexto.
            filtered = [
                inst for inst in self.instructions
                if all(inst["context"].get(k) == v for k, v in context_filter.items())
            ]
            return filtered[:top_k]

    def cache_result(self, key: str, value: Any, ttl: float = 3600) -> None:
        """Cacheia resultado com TTL (time-to-live)."""
        with self._lock:
            if len(self.data_cache) >= self.max_cache_size:
                # Remove entrada mais antiga (FIFO).
                oldest_key = next(iter(self.data_cache))
                del self.data_cache[oldest_key]
            self.data_cache[key] = {
                "value": value,
                "timestamp": time.time(),
                "ttl": ttl,
            }
            self._maybe_persist()

    def get_cached(self, key: str) -> Optional[Any]:
        """Recupera resultado do cache se não expirou."""
        with self._lock:
            if key not in self.data_cache:
                return None
            entry = self.data_cache[key]
            if time.time() - entry["timestamp"] > entry["ttl"]:
                del self.data_cache[key]
                return None
            return entry["value"]

    def record_error(self, error: str, context: Optional[Dict] = None) -> None:
        """Registra erro no histórico."""
        with self._lock:
            self.error_history.append({
                "error": error,
                "context": context or {},
                "timestamp": time.time(),
            })

    def record_call(self, success: bool, duration_s: float) -> None:
        """Registra estatísticas de uso."""
        with self._lock:
            self.usage_stats["n_calls"] += 1
            if success:
                self.usage_stats["n_success"] += 1
            else:
                self.usage_stats["n_failures"] += 1
            self.usage_stats["total_time_s"] += duration_s
            n = self.usage_stats["n_calls"]
            self.usage_stats["avg_time_s"] = self.usage_stats["total_time_s"] / n

    def get_stats(self) -> Dict[str, Any]:
        """Retorna estatísticas de uso."""
        with self._lock:
            return {
                **self.usage_stats,
                "cache_size": len(self.data_cache),
                "instructions_count": len(self.instructions),
                "error_count": len(self.error_history),
                "success_rate": (
                    self.usage_stats["n_success"] / max(self.usage_stats["n_calls"], 1)
                ),
            }

    def clear_cache(self) -> None:
        """Limpa cache."""
        with self._lock:
            self.data_cache.clear()

    def save(self) -> None:
        """Salva estado em disco."""
        if not self.persist_path:
            return
        os.makedirs(os.path.dirname(self.persist_path), exist_ok=True)
        data = {
            "tool_name": self.tool_name,
            "instructions": self.instructions,
            "usage_stats": self.usage_stats,
            "error_history": list(self.error_history),
        }
        with open(self.persist_path, "w") as f:
            json.dump(data, f, indent=2, default=str)

    def load(self) -> None:
        """Carrega estado do disco."""
        if not self.persist_path or not os.path.exists(self.persist_path):
            return
        with open(self.persist_path) as f:
            data = json.load(f)
        self.instructions = data.get("instructions", [])
        self.usage_stats = data.get("usage_stats", self.usage_stats)
        self.error_history = deque(
            data.get("error_history", []),
            maxlen=self.max_error_history,
        )

    def _maybe_persist(self) -> None:
        if self.persist_path:
            self.save()


# ============================================================================
# 2.2. MCP (Model Context Protocol) — conexão e contexto
# ============================================================================

@dataclass
class MCPMessage:
    """Mensagem no formato Model Context Protocol."""
    role: str  # 'user', 'assistant', 'tool', 'system'
    content: str
    tool_name: Optional[str] = None
    tool_call_id: Optional[str] = None
    metadata: Dict[str, Any] = field(default_factory=dict)


@dataclass
class MCPContext:
    """Contexto MCP para uma sessão."""
    session_id: str
    messages: List[MCPMessage] = field(default_factory=list)
    active_tools: Set[str] = field(default_factory=set)
    shared_state: Dict[str, Any] = field(default_factory=dict)

    def add_message(self, msg: MCPMessage) -> None:
        self.messages.append(msg)

    def get_context_window(self, max_messages: int = 20) -> List[MCPMessage]:
        """Retorna as últimas max_messages mensagens."""
        return self.messages[-max_messages:]

    def set_shared(self, key: str, value: Any) -> None:
        self.shared_state[key] = value

    def get_shared(self, key: str, default: Any = None) -> Any:
        return self.shared_state.get(key, default)


class MCPConnection:
    """Conexão MCP entre agente e ferramentas.

    Permite:
    - Registrar ferramentas (tools)
    - Enviar/receber mensagens
    - Compartilhar estado entre ferramentas
    - Gerenciar contexto da sessão
    """

    def __init__(self, session_id: Optional[str] = None):
        self.session_id = session_id or hashlib.md5(
            str(time.time()).encode()
        ).hexdigest()[:12]
        self.context = MCPContext(session_id=self.session_id)
        self._tools: Dict[str, "ToolWrapper"] = {}
        self._lock = threading.Lock()

    def register_tool(self, tool: "ToolWrapper") -> None:
        with self._lock:
            self._tools[tool.name] = tool
            self.context.active_tools.add(tool.name)

    def unregister_tool(self, name: str) -> None:
        with self._lock:
            self._tools.pop(name, None)
            self.context.active_tools.discard(name)

    def get_tool(self, name: str) -> Optional["ToolWrapper"]:
        return self._tools.get(name)

    def list_tools(self) -> List[str]:
        return list(self._tools.keys())

    def send_message(
        self,
        role: str,
        content: str,
        tool_name: Optional[str] = None,
        metadata: Optional[Dict] = None,
    ) -> MCPMessage:
        """Envia mensagem no contexto MCP."""
        msg = MCPMessage(
            role=role, content=content,
            tool_name=tool_name, metadata=metadata or {},
        )
        self.context.add_message(msg)
        return msg

    def get_context_window(self, max_messages: int = 20) -> List[MCPMessage]:
        return self.context.get_context_window(max_messages)

    def set_shared_state(self, key: str, value: Any) -> None:
        self.context.set_shared(key, value)

    def get_shared_state(self, key: str, default: Any = None) -> Any:
        return self.context.get_shared(key, default)


# ============================================================================
# 2.3. CircuitBreaker — resiliência à falhas
# ============================================================================

class CircuitState(Enum):
    CLOSED = "closed"       # Funcionando normalmente
    OPEN = "open"           # Falhando, rejeita chamadas
    HALF_OPEN = "half_open" # Testando se recuperou


class CircuitBreaker:
    """Circuit breaker para uma ferramenta.

    Estados:
    - CLOSED: chamadas passam normalmente. Conta falhas.
    - OPEN: após N falhas consecutivas, rejeita todas as chamadas.
    - HALF_OPEN: após timeout, permite 1 chamada de teste.
      Se sucesso → CLOSED. Se falha → OPEN.

    Matemática:
    - P(falha) = n_failures / n_calls
    - Threshold: se P(falha) > θ → OPEN
    - Recovery: após T_recovery segundos → HALF_OPEN
    """

    def __init__(
        self,
        failure_threshold: int = 5,
        recovery_timeout_s: float = 30.0,
        half_open_max_calls: int = 1,
        verbose: bool = False,
    ):
        self.failure_threshold = failure_threshold
        self.recovery_timeout_s = recovery_timeout_s
        self.half_open_max_calls = half_open_max_calls
        self.verbose = verbose
        self._state = CircuitState.CLOSED
        self._failure_count = 0
        self._success_count = 0
        self._last_failure_time = 0.0
        self._half_open_calls = 0
        self._lock = threading.Lock()

    @property
    def state(self) -> CircuitState:
        with self._lock:
            if self._state == CircuitState.OPEN:
                # Verifica se deve transitar para HALF_OPEN.
                if time.time() - self._last_failure_time > self.recovery_timeout_s:
                    self._state = CircuitState.HALF_OPEN
                    self._half_open_calls = 0
                    if self.verbose:
                        print(f"  CircuitBreaker: OPEN → HALF_OPEN")
            return self._state

    def can_execute(self) -> bool:
        """Verifica se a chamada pode prosseguir."""
        state = self.state
        if state == CircuitState.CLOSED:
            return True
        if state == CircuitState.HALF_OPEN:
            with self._lock:
                if self._half_open_calls < self.half_open_max_calls:
                    self._half_open_calls += 1
                    return True
            return False
        return False  # OPEN

    def record_success(self) -> None:
        """Registra sucesso."""
        with self._lock:
            if self._state == CircuitState.HALF_OPEN:
                self._success_count += 1
                self._state = CircuitState.CLOSED
                self._failure_count = 0
                if self.verbose:
                    print(f"  CircuitBreaker: HALF_OPEN → CLOSED (recuperou)")
            elif self._state == CircuitState.CLOSED:
                self._failure_count = 0  # reset consecutive failures

    def record_failure(self) -> None:
        """Registra falha."""
        with self._lock:
            self._failure_count += 1
            self._last_failure_time = time.time()
            if self._state == CircuitState.HALF_OPEN:
                self._state = CircuitState.OPEN
                if self.verbose:
                    print(f"  CircuitBreaker: HALF_OPEN → OPEN (falhou no teste)")
            elif self._state == CircuitState.CLOSED:
                if self._failure_count >= self.failure_threshold:
                    self._state = CircuitState.OPEN
                    if self.verbose:
                        print(f"  CircuitBreaker: CLOSED → OPEN "
                              f"({self._failure_count} falhas consecutivas)")

    def reset(self) -> None:
        """Reseta para CLOSED."""
        with self._lock:
            self._state = CircuitState.CLOSED
            self._failure_count = 0
            self._success_count = 0
            self._half_open_calls = 0

    def get_state(self) -> Dict[str, Any]:
        return {
            "state": self.state.value,
            "failure_count": self._failure_count,
            "success_count": self._success_count,
            "failure_threshold": self.failure_threshold,
            "recovery_timeout_s": self.recovery_timeout_s,
        }


# ============================================================================
# 2.3. ToolWrapper — envolve ferramenta com memória + circuit breaker
# ============================================================================

class ToolWrapper:
    """Envolve uma ferramenta (callable) com:
    - ToolMemory (memória individualizada)
    - CircuitBreaker (resiliência)
    - Timeout (evita hang)
    - Cache (evita recomputação)

    Uma ferramenta é qualquer callable: fn(input) -> output.
    """

    def __init__(
        self,
        name: str,
        fn: Callable,
        description: str = "",
        timeout_s: float = 30.0,
        enable_cache: bool = True,
        enable_circuit_breaker: bool = True,
        failure_threshold: int = 5,
        recovery_timeout_s: float = 30.0,
        persist_path: Optional[str] = None,
        verbose: bool = False,
    ):
        self.name = name
        self.fn = fn
        self.description = description
        self.timeout_s = timeout_s
        self.verbose = verbose
        # Componentes.
        self.memory = ToolMemory(
            tool_name=name,
            persist_path=persist_path,
        )
        self.circuit_breaker = CircuitBreaker(
            failure_threshold=failure_threshold,
            recovery_timeout_s=recovery_timeout_s,
            verbose=verbose,
        ) if enable_circuit_breaker else None
        self.enable_cache = enable_cache

    def execute(
        self,
        input_data: Any,
        cache_key: Optional[str] = None,
        use_cache: bool = True,
    ) -> Any:
        """Executa a ferramenta com resiliência.

        Args:
            input_data: input para a ferramenta
            cache_key: chave para cache (None = sem cache)
            use_cache: se True, tenta cache antes de executar

        Returns: resultado da ferramenta

        Raises:
            ToolCircuitBreakerOpen: se circuit breaker está OPEN
            ToolTimeoutError: se timeout
            ToolError: se a ferramenta falha
        """
        # 1. Verifica circuit breaker.
        if self.circuit_breaker and not self.circuit_breaker.can_execute():
            raise ToolCircuitBreakerOpen(
                f"Ferramenta '{self.name}' em circuit breaker OPEN "
                f"(estado: {self.circuit_breaker.state.value})"
            )
        # 2. Verifica cache.
        if self.enable_cache and use_cache and cache_key:
            cached = self.memory.get_cached(cache_key)
            if cached is not None:
                if self.verbose:
                    print(f"  Tool '{self.name}': cache hit ({cache_key})")
                return cached
        # 3. Executa com timeout.
        t0 = time.time()
        success = False
        try:
            result = self._execute_with_timeout(input_data)
            success = True
            duration = time.time() - t0
            # 4. Cache resultado.
            if self.enable_cache and cache_key:
                self.memory.cache_result(cache_key, result)
            # 5. Registra sucesso.
            if self.circuit_breaker:
                self.circuit_breaker.record_success()
            self.memory.record_call(success=True, duration_s=duration)
            return result
        except ToolTimeoutError:
            duration = time.time() - t0
            self.memory.record_error("timeout", {"input": str(input_data)[:100]})
            self.memory.record_call(success=False, duration_s=duration)
            if self.circuit_breaker:
                self.circuit_breaker.record_failure()
            raise
        except Exception as e:
            duration = time.time() - t0
            self.memory.record_error(str(e), {"input": str(input_data)[:100]})
            self.memory.record_call(success=False, duration_s=duration)
            if self.circuit_breaker:
                self.circuit_breaker.record_failure()
            raise ToolError(f"Ferramenta '{self.name}' falhou: {e}") from e

    def _execute_with_timeout(self, input_data: Any) -> Any:
        """Executa fn com timeout via ThreadPoolExecutor."""
        future = ThreadPoolExecutor(max_workers=1).submit(self.fn, input_data)
        try:
            return future.result(timeout=self.timeout_s)
        except TimeoutError:
            raise ToolTimeoutError(
                f"Ferramenta '{self.name}' timeout após {self.timeout_s}s"
            )

    def get_stats(self) -> Dict[str, Any]:
        stats = self.memory.get_stats()
        if self.circuit_breaker:
            stats["circuit_breaker"] = self.circuit_breaker.get_state()
        return stats

    def restart(self) -> None:
        """Reinicia a ferramenta (reset circuit breaker + clear cache)."""
        if self.circuit_breaker:
            self.circuit_breaker.reset()
        self.memory.clear_cache()
        if self.verbose:
            print(f"  Tool '{self.name}': reiniciada")


# ============================================================================
# 2.3.1. ToolAgentCoordinator — arquitetura distribuída otimizada
# ============================================================================

class ToolAgentCoordinator:
    """Coordenador de Agentes de Ferramentas (arquitetura híbrida).

    Arquitetura: 1 Coordenador + N Agentes Especialistas (1 por ferramenta).

    Análise matemática da arquitetura:
    - Single-Agent (1 agente, N ferramentas): T = Σ T_i (sequencial)
    - Multi-Agent (N agentes, 1 ferramenta cada): T = max(T_i) + T_coord
    - Hybrid (este): T = max(T_i) + T_coord, com isolamento de falhas

    Quando T_coord << max(T_i): speedup ≈ N.
    Isolamento: se ferramenta i falha, só agente i reinicia (não afeta outros).

    O Coordenador:
    1. Roteia requisições para o agente especialista apropriado
    2. Agrega resultados
    3. Gerencia resiliência (restart de agentes falhos)
    4. Mantém contexto MCP compartilhado
    """

    def __init__(
        self,
        n_workers: Optional[int] = None,
        verbose: bool = False,
    ):
        self.n_workers = n_workers or min(4, os.cpu_count() or 4)
        self.verbose = verbose
        self._tools: Dict[str, ToolWrapper] = {}
        self._mcp = MCPConnection()
        self._executor = ThreadPoolExecutor(max_workers=self.n_workers)
        self._lock = threading.Lock()

    def register_tool(
        self,
        name: str,
        fn: Callable,
        description: str = "",
        timeout_s: float = 30.0,
        failure_threshold: int = 5,
        recovery_timeout_s: float = 30.0,
    ) -> ToolWrapper:
        """Registra uma ferramenta."""
        tool = ToolWrapper(
            name=name, fn=fn, description=description,
            timeout_s=timeout_s,
            failure_threshold=failure_threshold,
            recovery_timeout_s=recovery_timeout_s,
            verbose=self.verbose,
        )
        with self._lock:
            self._tools[name] = tool
            self._mcp.register_tool(tool)
        if self.verbose:
            print(f"  Coordenador: ferramenta '{name}' registrada")
        return tool

    def unregister_tool(self, name: str) -> None:
        with self._lock:
            self._tools.pop(name, None)
            self._mcp.unregister_tool(name)

    def execute_tool(
        self,
        name: str,
        input_data: Any,
        cache_key: Optional[str] = None,
    ) -> Any:
        """Executa uma ferramenta específica."""
        tool = self._tools.get(name)
        if tool is None:
            raise ToolError(f"Ferramenta '{name}' não registrada")
        return tool.execute(input_data, cache_key=cache_key)

    def execute_parallel(
        self,
        requests: List[Tuple[str, Any]],
        fail_fast: bool = False,
    ) -> Dict[str, Any]:
        """Executa múltiplas ferramentas em paralelo.

        Args:
            requests: lista de (tool_name, input_data)
            fail_fast: se True, primeira falha cancela todas

        Retorna: {tool_name: result} (ou {tool_name: error})
        """
        results: Dict[str, Any] = {}
        futures: Dict[Future, str] = {}
        for tool_name, input_data in requests:
            tool = self._tools.get(tool_name)
            if tool is None:
                results[tool_name] = ToolError(f"Ferramenta '{tool_name}' não registrada")
                continue
            # Submete.
            fut = self._executor.submit(self._safe_execute, tool_name, input_data)
            futures[fut] = tool_name
        # Coleta.
        for fut in as_completed(futures):
            tool_name = futures[fut]
            try:
                result = fut.result()
                results[tool_name] = result
            except Exception as e:
                results[tool_name] = e
                if fail_fast:
                    # Cancela restantes.
                    for f in futures:
                        f.cancel()
                    break
        return results

    def _safe_execute(self, tool_name: str, input_data: Any) -> Any:
        """Executa ferramenta com tratamento de erro."""
        try:
            return self.execute_tool(tool_name, input_data)
        except ToolCircuitBreakerOpen:
            # Tenta restart.
            if self.verbose:
                print(f"  Coordenador: restart da ferramenta '{tool_name}'")
            tool = self._tools[tool_name]
            tool.restart()
            # Tenta novamente após restart.
            return self.execute_tool(tool_name, input_data)
        except Exception as e:
            if self.verbose:
                print(f"  Coordenador: ferramenta '{tool_name}' falhou: {e}")
            raise

    def execute_with_fallback(
        self,
        primary_tool: str,
        fallback_tool: str,
        input_data: Any,
    ) -> Any:
        """Executa ferramenta primária; se falhar, usa fallback."""
        try:
            return self.execute_tool(primary_tool, input_data)
        except (ToolError, ToolCircuitBreakerOpen, ToolTimeoutError) as e:
            if self.verbose:
                print(f"  Coordenador: '{primary_tool}' falhou ({e}), "
                      f"usando fallback '{fallback_tool}'")
            return self.execute_tool(fallback_tool, input_data)

    def get_all_stats(self) -> Dict[str, Dict[str, Any]]:
        """Estatísticas de todas as ferramentas."""
        return {name: tool.get_stats() for name, tool in self._tools.items()}

    def get_mcp_context(self, max_messages: int = 20) -> List[MCPMessage]:
        """Retorna contexto MCP."""
        return self._mcp.get_context_window(max_messages)

    def restart_all(self) -> None:
        """Reinicia todas as ferramentas."""
        for tool in self._tools.values():
            tool.restart()

    def summary(self) -> str:
        lines = [f"ToolAgentCoordinator ({len(self._tools)} ferramentas):"]
        for name, tool in self._tools.items():
            stats = tool.get_stats()
            cb_state = stats.get("circuit_breaker", {}).get("state", "N/A")
            lines.append(
                f"  {name}: calls={stats['n_calls']}, "
                f"success_rate={stats['success_rate']:.1%}, "
                f"avg_time={stats['avg_time_time_s' if 'avg_time_time_s' in stats else 'avg_time_s']:.3f}s, "
                f"circuit={cb_state}"
            )
        return "\n".join(lines)

    def close(self) -> None:
        """Fecha executor."""
        self._executor.shutdown(wait=True)


# ============================================================================
# 2.3.1. Arquitetura — análise matemática
# ============================================================================

def analyze_architecture(
    n_tools: int,
    tool_latencies: List[float],
    coord_overhead_s: float = 0.01,
) -> Dict[str, Any]:
    """Analisa 3 arquiteturas e recomenda a melhor.

    Arquiteturas:
    1. Single-Agent: 1 agente executa todas N ferramentas sequencialmente
       T = Σ T_i
    2. Multi-Agent: N agentes, 1 ferramenta cada, em paralelo
       T = max(T_i) + T_coord
    3. Hybrid (Coordinator + N Specialists): mesmo que Multi-Agent mas com
       isolamento de falhas e memória individualizada
       T = max(T_i) + T_coord (mesmo tempo, mais resiliência)

    Args:
        n_tools: número de ferramentas
        tool_latencies: latência esperada de cada ferramenta (s)
        coord_overhead_s: overhead de coordenação (s)

    Retorna: análise com recomendação
    """
    assert len(tool_latencies) == n_tools
    total_seq = sum(tool_latencies)
    max_lat = max(tool_latencies)
    # Single-Agent.
    t_single = total_seq
    # Multi-Agent / Hybrid.
    t_parallel = max_lat + coord_overhead_s
    speedup = t_single / t_parallel if t_parallel > 0 else 0
    # Recomendação.
    if speedup > 1.5:
        recommendation = "Hybrid (Coordinator + N Specialists)"
        reason = f"Speedup {speedup:.1f}× justifica overhead de coordenação"
    elif n_tools <= 2:
        recommendation = "Single-Agent"
        reason = f"Poucas ferramentas ({n_tools}), overhead não compensa"
    else:
        recommendation = "Hybrid"
        reason = f"Isolamento de falhas importante para {n_tools} ferramentas"
    # Análise de resiliência.
    # Em Single-Agent: 1 falha derruba tudo (P(falha total) = 1 - ∏(1-p_i))
    # Em Hybrid: 1 falha afeta só 1 agente (P(falha total) = ∏ p_i, muito menor)
    return {
        "n_tools": n_tools,
        "tool_latencies": tool_latencies,
        "single_agent_time_s": t_single,
        "parallel_time_s": t_parallel,
        "speedup": speedup,
        "coord_overhead_s": coord_overhead_s,
        "recommendation": recommendation,
        "reason": reason,
        "resilience_advantage": (
            "Hybrid: 1 falha afeta 1 agente (isolamento). "
            "Single-Agent: 1 falha derruba todas."
        ),
    }