File size: 31,609 Bytes
ed1b365
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Conflict Detection and Classification Engine



Identifies conflicting claims across agent pairs using token-level confidence scores.

Classifies conflicts by type (contradiction, emphasis, framework) and scores strength

weighted by agent confidence.



Author: Claude Code

"""

import re
import logging
import math
from dataclasses import dataclass, asdict
from typing import Dict, List, Tuple, Optional
from collections import defaultdict

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


# ============================================================================
# Phase 4: Memory-Aware Conflict Strength (Self-Correcting Feedback)
# ============================================================================


def adjust_conflict_strength_with_memory(conflict, memory_weighting=None):
    """

    Enhance conflict strength using historical adapter performance.



    Makes conflict importance adaptive: conflicts involving high-performing

    adapters are weighted heavier, enabling experience-aware reasoning.



    Args:

        conflict: Conflict object with agent_a, agent_b, conflict_strength

        memory_weighting: MemoryWeighting instance (or None for no adjustment)



    Returns:

        Adjusted conflict strength (same type as input)

    """
    if not memory_weighting:
        return conflict.conflict_strength

    try:
        # Get adapter performance weights
        weight_a = memory_weighting.adapter_weights.get(conflict.agent_a.lower(), None)
        weight_b = memory_weighting.adapter_weights.get(conflict.agent_b.lower(), None)

        if not weight_a or not weight_b:
            return conflict.conflict_strength

        # Compute average performance
        avg_weight = (weight_a.weight + weight_b.weight) / 2.0

        # Normalize to modifier [0.5, 1.5]
        # weight=0.0 → modifier=0.5 (suppress weak adapter conflicts)
        # weight=1.0 → modifier=1.0 (neutral)
        # weight=2.0 → modifier=1.5 (amplify strong adapter conflicts)
        modifier = 0.5 + (avg_weight / 2.0) * 0.5
        modifier = max(0.5, min(1.5, modifier))

        adjusted = conflict.conflict_strength * modifier

        return adjusted

    except Exception as e:
        logger.debug(f"Error adjusting conflict strength: {e}")
        return conflict.conflict_strength



@dataclass
class Conflict:
    """A detected conflict between two agents on a specific claim."""

    agent_a: str  # First agent
    agent_b: str  # Second agent
    claim_a: str  # Claim from agent A
    claim_b: str  # Claim from agent B
    conflict_type: str  # "contradiction" | "emphasis" | "framework"
    conflict_strength: float  # [0, 1], weighted by agent confidence
    confidence_a: float  # Agent A's confidence in their claim
    confidence_b: float  # Agent B's confidence in their claim
    semantic_overlap: float  # Cosine similarity of claims [0, 1]
    opposition_score: float  # How opposed are the claims [0, 1]

    def to_dict(self) -> Dict:
        """Serialize for storage."""
        return asdict(self)


class ConflictEngine:
    """Detects and scores conflicts between agent responses."""

    def __init__(

        self,

        token_confidence_engine: Optional[object] = None,

        contradiction_threshold: float = 0.7,

        overlap_threshold: float = 0.3,

        semantic_tension_engine: Optional[object] = None,

        max_conflicts_per_pair: int = 2,  # Cap generation at source

        max_total_conflicts: int = 12,    # Total budget (was 10 after capping from 71)

    ):
        """

        Initialize conflict detection engine.



        Args:

            token_confidence_engine: TokenConfidenceEngine for scoring claims

            contradiction_threshold: Semantic overlap needed to consider claims related

            overlap_threshold: Threshold for identifying same-claim conflicts

            semantic_tension_engine: (Phase 6) SemanticTensionEngine for embedding-based tension

            max_conflicts_per_pair: Max conflicts to generate per agent pair (default: 2)

            max_total_conflicts: Max total conflicts allowed (default: 12)

        """
        self.token_confidence = token_confidence_engine
        self.contradiction_threshold = contradiction_threshold
        self.overlap_threshold = overlap_threshold
        self.semantic_tension_engine = semantic_tension_engine  # Phase 6
        self.max_conflicts_per_pair = max_conflicts_per_pair
        self.max_total_conflicts = max_total_conflicts

        # Contradiction pattern pairs (negation patterns)
        self.negation_patterns = [
            (r"\b(no|not|none|neither|never|cannot|doesn['\"]t)\b", "negation"),
            (r"\b(must|should|always|only)\b", "necessity"),
            (r"\b(reject|disagree|oppose|deny|false|wrong)\b", "rejection"),
        ]

    def detect_conflicts(

        self, agent_analyses: Dict[str, str]

    ) -> List[Conflict]:
        """

        Detect conflicts across agent pairs.



        Args:

            agent_analyses: Dict {agent_name: response_text}



        Returns:

            List of Conflicts sorted by strength (descending)

        """
        conflicts = []

        # Score tokens/claims for each agent
        agent_scores = {}
        agent_names = list(agent_analyses.keys())

        for agent_name in agent_names:
            response = agent_analyses[agent_name]
            if self.token_confidence:
                peer_responses = {
                    a: agent_analyses[a]
                    for a in agent_names
                    if a != agent_name
                }
                scores = self.token_confidence.score_tokens(
                    response, agent_name, peer_responses=peer_responses
                )
                agent_scores[agent_name] = scores
            else:
                logger.warning(
                    "No token_confidence engine provided; using fallback scoring"
                )

        # Check each agent pair
        for i, agent_a in enumerate(agent_names):
            for agent_b in agent_names[i + 1 :]:
                claims_a = (
                    agent_scores[agent_a].claims
                    if agent_a in agent_scores
                    else self._extract_simple_claims(agent_analyses[agent_a])
                )
                claims_b = (
                    agent_scores[agent_b].claims
                    if agent_b in agent_scores
                    else self._extract_simple_claims(agent_analyses[agent_b])
                )

                # === FIX: Cap conflicts at source (per-pair) ===
                pair_conflicts = []

                # Check each claim pair
                for claim_a in claims_a:
                    for claim_b in claims_b:
                        # Stop early if we've already hit per-pair limit
                        if len(pair_conflicts) >= self.max_conflicts_per_pair:
                            break

                        # Compute semantic overlap
                        overlap = self._compute_semantic_overlap(claim_a.text, claim_b.text)

                        # If claims are related (high overlap), check for conflict
                        if overlap > self.overlap_threshold:
                            conflict_type, opposition_score = self._classify_conflict(
                                claim_a.text, claim_b.text, overlap
                            )

                            if opposition_score > 0:  # Only include if there's opposition
                                # Conflict strength = product of confidences × opposition
                                conflict_strength = (
                                    claim_a.confidence
                                    * claim_b.confidence
                                    * opposition_score
                                )

                                conflict = Conflict(
                                    agent_a=agent_a,
                                    agent_b=agent_b,
                                    claim_a=claim_a.text,
                                    claim_b=claim_b.text,
                                    conflict_type=conflict_type,
                                    conflict_strength=conflict_strength,
                                    confidence_a=claim_a.confidence,
                                    confidence_b=claim_b.confidence,
                                    semantic_overlap=overlap,
                                    opposition_score=opposition_score,
                                )
                                pair_conflicts.append(conflict)

                    # Stop outer loop too if limit reached
                    if len(pair_conflicts) >= self.max_conflicts_per_pair:
                        break

                # Add this pair's conflicts to global list
                conflicts.extend(pair_conflicts)

        # Sort by strength descending
        conflicts.sort(key=lambda c: c.conflict_strength, reverse=True)

        # === Phase 4: Adjust conflict strength by adapter performance ===
        # Make conflict importance adaptive using historical memory
        for conflict in conflicts:
            memory_weighting = getattr(self, "memory_weighting", None)
            conflict.conflict_strength = adjust_conflict_strength_with_memory(
                conflict, memory_weighting
            )

        # Re-sort after adjustment
        conflicts.sort(key=lambda c: c.conflict_strength, reverse=True)

        # === FIX: Use configurable max_total_conflicts (default 12, up from 10) ===
        # Prevent combinatorial explosion by limiting to max total
        if len(conflicts) > self.max_total_conflicts:
            logger.info(
                f"Capping conflicts: {len(conflicts)}{self.max_total_conflicts} "
                f"(per-pair cap: {self.max_conflicts_per_pair}, total budget: {self.max_total_conflicts})"
            )
            conflicts = conflicts[: self.max_total_conflicts]

        return conflicts

    def _extract_simple_claims(self, response: str) -> List[object]:
        """

        Fallback: extract simple sentence-based claims without token scoring.



        Returns:

            List of simple claim objects with text and neutral confidence

        """
        claim_pattern = re.compile(r"[.!?]+")
        sentences = claim_pattern.split(response)

        claims = []
        for sentence in sentences:
            if not sentence.strip():
                continue

            # Create simple claim object
            class SimpleClaim:
                def __init__(self, text):
                    self.text = text
                    self.confidence = 0.5  # Neutral
                    self.agent_name = ""

            claims.append(SimpleClaim(sentence.strip()))

        return claims

    def _compute_semantic_overlap(self, claim_a: str, claim_b: str) -> float:
        """

        Compute semantic overlap between two claims via cosine similarity.



        Simple implementation: word overlap ratio.



        Returns:

            Similarity [0, 1]

        """
        words_a = set(claim_a.lower().split())
        words_b = set(claim_b.lower().split())

        # Remove common stop words
        stop_words = {
            "the",
            "a",
            "an",
            "is",
            "are",
            "and",
            "or",
            "of",
            "to",
            "in",
            "that",
            "it",
            "for",
            "with",
        }
        words_a = words_a - stop_words
        words_b = words_b - stop_words

        if not words_a or not words_b:
            return 0.0

        # Jaccard similarity
        intersection = len(words_a & words_b)
        union = len(words_a | words_b)

        if union == 0:
            return 0.0

        return intersection / union

    def _classify_conflict(

        self, claim_a: str, claim_b: str, overlap: float

    ) -> Tuple[str, float]:
        """

        Classify the type of conflict and compute opposition score.



        Phase 6 Enhancement: Blends heuristic opposition_score (discrete 0.4/0.7/1.0)

        with embedding-based semantic tension (continuous [0, 1]) for nuanced conflicts.



        Returns:

            (conflict_type, opposition_score) where:

            - conflict_type: "contradiction" | "emphasis" | "framework" | "paraphrase"

            - opposition_score: [0, 1] how directly opposed are the claims

              (0 = paraphrase/same, 1 = maximum opposition)

        """
        claim_a_lower = claim_a.lower()
        claim_b_lower = claim_b.lower()

        # --- Compute Heuristic Opposition Score (Phase 1-5) ---
        # Look for negation patterns
        negation_in_a = any(
            re.search(pattern, claim_a_lower) for pattern, _ in self.negation_patterns
        )
        negation_in_b = any(
            re.search(pattern, claim_b_lower) for pattern, _ in self.negation_patterns
        )

        # If one has negation and the other doesn't, likely contradiction
        heuristic_opposition = 1.0
        heuristic_type = "contradiction"
        if negation_in_a != negation_in_b:
            logger.debug(f"Direct contradiction detected:\n  A: {claim_a}\n  B: {claim_b}")
            heuristic_opposition = 1.0
            heuristic_type = "contradiction"
        else:
            # Check for explicit negation of key noun phrases
            key_noun_pattern = re.compile(r"\b[A-Z][a-z]+(?:\s+[A-Z][a-z]+)*\b")
            nouns_a = set(m.group() for m in key_noun_pattern.finditer(claim_a))
            nouns_b = set(m.group() for m in key_noun_pattern.finditer(claim_b))

            # If different key nouns mentioned, might be framework conflict
            if nouns_a and nouns_b and nouns_a != nouns_b:
                heuristic_opposition = 0.4
                heuristic_type = "framework"
            else:
                # --- Check for emphasis conflict ---
                # Both mention similar concepts but with different priorities
                emphasis_words = ["important", "prioritize", "focus", "emphasize", "weight", "prefer", "favor"]
                emphasis_pattern = "|".join(emphasis_words)

                has_emphasis_a = bool(re.search(emphasis_pattern, claim_a_lower))
                has_emphasis_b = bool(re.search(emphasis_pattern, claim_b_lower))

                if has_emphasis_a and has_emphasis_b:
                    # Both making prioritization claims
                    logger.debug(f"Emphasis conflict detected:\n  A: {claim_a}\n  B: {claim_b}")
                    heuristic_opposition = 0.7
                    heuristic_type = "emphasis"
                else:
                    # Default: framework conflict (valid under different assumptions)
                    heuristic_opposition = 0.4
                    heuristic_type = "framework"

        # --- Phase 6: Compute Semantic Opposition (Embedding-Based) ---
        semantic_opposition = 0.4  # Default fallback
        semantic_type = "framework"

        if self.semantic_tension_engine:
            try:
                semantic_opposition = self.semantic_tension_engine.compute_semantic_tension(claim_a, claim_b)
                semantic_type = self.semantic_tension_engine.compute_polarity(claim_a, claim_b)
                logger.debug(f"Semantic tension: {semantic_opposition:.3f} ({semantic_type})")
            except Exception as e:
                logger.debug(f"Semantic tension computation failed: {e}, using heuristic only")

        # --- Phase 6: Hybrid Opposition Score ---
        # Blend both signals: semantic (0.6) + heuristic (0.4)
        # This gives nuanced, continuous opposition scores while preserving heuristic insight
        if self.semantic_tension_engine:
            final_opposition = 0.6 * semantic_opposition + 0.4 * heuristic_opposition
            final_type = semantic_type  # Prefer semantic classification
        else:
            final_opposition = heuristic_opposition
            final_type = heuristic_type

        return (final_type, float(final_opposition))

    def resolve_conflict_round(

        self,

        conflict: Conflict,

        agent_a_response_round2: str,

        agent_b_response_round2: str,

    ) -> Dict:
        """

        Score whether agents moved towards resolving a conflict in next round.



        Args:

            conflict: The original conflict

            agent_a_response_round2: Agent A's response in round 2

            agent_b_response_round2: Agent B's response in round 2



        Returns:

            Dict with resolution metrics

        """
        # Check if agents mentioned the other's claim in their response
        addressed_by_a = self._is_claim_addressed(conflict.claim_b, agent_a_response_round2)
        addressed_by_b = self._is_claim_addressed(conflict.claim_a, agent_b_response_round2)

        # Check if they've softened their position (added qualifiers)
        softened_a = self._is_claim_softened(conflict.claim_a, agent_a_response_round2)
        softened_b = self._is_claim_softened(conflict.claim_b, agent_b_response_round2)

        resolution_score = 0.0
        if addressed_by_a and addressed_by_b:
            resolution_score += 0.4
        if softened_a and softened_b:
            resolution_score += 0.3
        if addressed_by_a or addressed_by_b:
            resolution_score += 0.1

        resolution_score = min(1.0, resolution_score)

        return {
            "engaged_with_conflict": addressed_by_a or addressed_by_b,
            "both_addressed": addressed_by_a and addressed_by_b,
            "softened_positions": softened_a or softened_b,
            "resolution_score": resolution_score,
        }

    def _is_claim_addressed(self, claim: str, response: str) -> bool:
        """

        Check if a claim is explicitly addressed in response.



        Detects pronoun references, direct quotes, or semantic restatement.

        """
        response_lower = response.lower()
        claim_lower = claim.lower()

        # Direct substring match
        if claim_lower in response_lower:
            return True

        # Check for key words from claim appearing in response
        key_words = [
            w
            for w in claim.split()
            if len(w) > 4 and w.lower() not in ["this", "that", "these", "other"]
        ]

        matching_words = sum(1 for w in key_words if w.lower() in response_lower)
        return matching_words >= 2  # At least 2 key words must appear

    def _is_claim_softened(self, original_claim: str, followup_response: str) -> bool:
        """

        Check if an agent has softened their original claim in follow-up.



        Detects addition of qualifiers, exceptions, or concessions.

        """
        softening_words = [
            "however",
            "though",
            "but",
            "perhaps",
            "maybe",
            "could",
            "might",
            "arguably",
            "in some cases",
            "exception",
            "qualify",
            "depends",
        ]

        response_lower = followup_response.lower()

        # Check for softening language near the original claim
        has_softening = any(word in response_lower for word in softening_words)

        # Check for explicit concession
        has_concession = bool(re.search(r"\b(granted|acknowledge|admit|agree)\b", response_lower))

        return has_softening or has_concession

    def group_conflicts_by_pair(self, conflicts: List[Conflict]) -> Dict[str, List[Conflict]]:
        """

        Group conflicts by agent pair.



        Returns:

            Dict {agent_pair_key: List[Conflict]}

        """
        grouped = defaultdict(list)
        for conflict in conflicts:
            pair_key = f"{conflict.agent_a}_vs_{conflict.agent_b}"
            grouped[pair_key].append(conflict)
        return dict(grouped)

    def summarize_conflicts(self, conflicts: List[Conflict]) -> Dict:
        """

        Generate summary statistics for conflicts.



        Returns:

            Dict with count, average strength, distribution by type

        """
        if not conflicts:
            return {
                "total_conflicts": 0,
                "avg_conflict_strength": 0.0,
                "by_type": {},
                "top_conflicts": [],
            }

        by_type = defaultdict(list)
        for c in conflicts:
            by_type[c.conflict_type].append(c)

        return {
            "total_conflicts": len(conflicts),
            "avg_conflict_strength": sum(c.conflict_strength for c in conflicts) / len(conflicts),
            "by_type": {
                ctype: len(clist) for ctype, clist in by_type.items()
            },
            "type_avg_strength": {
                ctype: sum(c.conflict_strength for c in clist) / len(clist)
                for ctype, clist in by_type.items()
            },
            "top_conflicts": [
                {
                    "agent_a": c.agent_a,
                    "agent_b": c.agent_b,
                    "type": c.conflict_type,
                    "strength": c.conflict_strength,
                    "claim_a_excerpt": c.claim_a[:100],
                    "claim_b_excerpt": c.claim_b[:100],
                }
                for c in conflicts[:5]
            ],
        }


# ============================================================================
# Phase 3: Multi-Round Conflict Evolution Tracking
# ============================================================================


@dataclass
class ConflictEvolution:
    """Track how a conflict changes across multiple debate rounds."""

    original_conflict: Conflict        # From Round 0
    round_trajectories: Dict[int, Dict]  # {round: {strength, addressing_score, ...}}
    resolution_rate: float = 0.0       # (initial - final) / initial
    resolution_type: str = "new"       # "hard_victory"|"soft_consensus"|"stalled"|"worsened"|"resolved"
    resolved_in_round: int = -1        # Which round resolved it? (-1 if unresolved)

    def _compute_resolution_rate(self) -> float:
        """Calculate (initial - final) / initial."""
        if not self.round_trajectories or 0 not in self.round_trajectories:
            return 0.0

        initial_strength = self.round_trajectories[0].get("strength", 0)
        if initial_strength == 0:
            return 0.0

        final_strength = min(
            (s.get("strength", float('inf')) for s in self.round_trajectories.values()),
            default=initial_strength
        )

        return (initial_strength - final_strength) / initial_strength


class ConflictTracker:
    """Track conflicts across multiple debate rounds (Phase 3)."""

    def __init__(self, conflict_engine):
        """Initialize tracker with reference to ConflictEngine."""
        self.conflict_engine = conflict_engine
        self.evolution_data: Dict[str, ConflictEvolution] = {}

    def track_round(self, round_num: int, agent_analyses: Dict[str, str],

                   previous_round_conflicts: List[Conflict]) -> List[ConflictEvolution]:
        """Track conflicts across rounds."""
        current_round_conflicts = self.conflict_engine.detect_conflicts(agent_analyses)

        evolutions = []

        # Track previous conflicts in current round
        for prev_conflict in previous_round_conflicts:
            matches = self._find_matching_conflicts(prev_conflict, current_round_conflicts)

            if matches:
                current_conflict = matches[0]
                evolution = self._compute_evolution(
                    prev_conflict, current_conflict, round_num, agent_analyses
                )
            else:
                evolution = self._mark_resolved(prev_conflict, round_num)

            evolutions.append(evolution)

        # Track new conflicts
        new_conflicts = self._find_new_conflicts(previous_round_conflicts, current_round_conflicts)
        for new_conflict in new_conflicts:
            evolution = ConflictEvolution(
                original_conflict=new_conflict,
                round_trajectories={round_num: {
                    "strength": new_conflict.conflict_strength,
                    "addressing_score": 0.0,
                    "softening_score": 0.0,
                }},
                resolution_rate=0.0,
                resolution_type="new",
                resolved_in_round=-1,
            )
            evolutions.append(evolution)

        return evolutions

    def _find_matching_conflicts(self, conflict: Conflict,

                                candidates: List[Conflict]) -> List[Conflict]:
        """Find conflicts that likely match across rounds."""
        matches = []
        for candidate in candidates:
            # Match if same agent pair
            same_pair = (
                (conflict.agent_a == candidate.agent_a and conflict.agent_b == candidate.agent_b) or
                (conflict.agent_a == candidate.agent_b and conflict.agent_b == candidate.agent_a)
            )

            if same_pair:
                # Check claim overlap
                overlap = self.conflict_engine._compute_semantic_overlap(
                    conflict.claim_a, candidate.claim_a
                )
                if overlap > 0.5:
                    matches.append(candidate)

        return matches

    def _compute_evolution(self, prev_conflict: Conflict, current_conflict: Conflict,

                          round_num: int, agent_analyses: Dict[str, str]) -> ConflictEvolution:
        """Compute how conflict evolved between rounds."""
        # Check if agents addressed each other
        addressing_a = self.conflict_engine._is_claim_addressed(
            prev_conflict.claim_b, agent_analyses.get(current_conflict.agent_a, "")
        )
        addressing_b = self.conflict_engine._is_claim_addressed(
            prev_conflict.claim_a, agent_analyses.get(current_conflict.agent_b, "")
        )
        addressing_score = (float(addressing_a) + float(addressing_b)) / 2.0

        # Check if agents softened positions
        softening_a = self.conflict_engine._is_claim_softened(
            prev_conflict.claim_a, agent_analyses.get(current_conflict.agent_a, "")
        )
        softening_b = self.conflict_engine._is_claim_softened(
            prev_conflict.claim_b, agent_analyses.get(current_conflict.agent_b, "")
        )
        softening_score = (float(softening_a) + float(softening_b)) / 2.0

        # Classify resolution type
        strength_delta = prev_conflict.conflict_strength - current_conflict.conflict_strength
        if strength_delta > prev_conflict.conflict_strength * 0.5:
            resolution_type = "hard_victory"
        elif strength_delta > 0.05:
            resolution_type = "soft_consensus"
        elif abs(strength_delta) < 0.05:
            resolution_type = "stalled"
        else:
            resolution_type = "worsened"

        # Update evolution data
        key = f"{prev_conflict.agent_a}_vs_{prev_conflict.agent_b}"
        if key not in self.evolution_data:
            self.evolution_data[key] = ConflictEvolution(
                original_conflict=prev_conflict,
                round_trajectories={0: {
                    "strength": prev_conflict.conflict_strength,
                    "addressing_score": 0.0,
                    "softening_score": 0.0,
                }},
                resolution_rate=0.0,
                resolution_type="new",
                resolved_in_round=-1,
            )

        self.evolution_data[key].round_trajectories[round_num] = {
            "strength": current_conflict.conflict_strength,
            "addressing_score": addressing_score,
            "softening_score": softening_score,
        }

        self.evolution_data[key].resolution_rate = self.evolution_data[key]._compute_resolution_rate()
        self.evolution_data[key].resolution_type = resolution_type

        return self.evolution_data[key]

    def _mark_resolved(self, conflict: Conflict, round_num: int) -> ConflictEvolution:
        """Mark conflict as resolved (no longer detected)."""
        key = f"{conflict.agent_a}_vs_{conflict.agent_b}"
        if key not in self.evolution_data:
            self.evolution_data[key] = ConflictEvolution(
                original_conflict=conflict,
                round_trajectories={0: {
                    "strength": conflict.conflict_strength,
                    "addressing_score": 0.0,
                    "softening_score": 0.0,
                }},
                resolution_rate=1.0,
                resolution_type="resolved",
                resolved_in_round=round_num,
            )
            self.evolution_data[key].round_trajectories[round_num] = {
                "strength": 0.0,
                "addressing_score": 1.0,
                "softening_score": 1.0,
            }

        return self.evolution_data[key]

    def _find_new_conflicts(self, previous: List[Conflict],

                           current: List[Conflict]) -> List[Conflict]:
        """Find conflicts that are new."""
        prev_pairs = {(c.agent_a, c.agent_b) for c in previous}
        new = []
        for conflict in current:
            pair = (conflict.agent_a, conflict.agent_b)
            if pair not in prev_pairs:
                new.append(conflict)
        return new

    def get_summary(self) -> Dict:
        """Get summary of all conflict evolutions."""
        if not self.evolution_data:
            return {"total_tracked": 0, "message": "No conflicts tracked yet"}

        resolved = [e for e in self.evolution_data.values() if e.resolution_type == "resolved"]
        hard_victory = [e for e in self.evolution_data.values() if e.resolution_type == "hard_victory"]
        soft_consensus = [e for e in self.evolution_data.values() if e.resolution_type == "soft_consensus"]
        stalled = [e for e in self.evolution_data.values() if e.resolution_type == "stalled"]
        worsened = [e for e in self.evolution_data.values() if e.resolution_type == "worsened"]

        avg_resolution = sum(e.resolution_rate for e in self.evolution_data.values()) / len(self.evolution_data)

        return {
            "total_tracked": len(self.evolution_data),
            "resolved": len(resolved),
            "hard_victory": len(hard_victory),
            "soft_consensus": len(soft_consensus),
            "stalled": len(stalled),
            "worsened": len(worsened),
            "avg_resolution_rate": avg_resolution,
            "by_type": {
                "resolved": len(resolved),
                "hard_victory": len(hard_victory),
                "soft_consensus": len(soft_consensus),
                "stalled": len(stalled),
                "worsened": len(worsened),
            },
        }