File size: 29,740 Bytes
c8fbdf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Event-Embedded Value (EEV) engine for singularity-aware valuation.



This module gives Codette a concrete alternative to treating all value as a

smooth, differentiable function over time. Continuous intervals are integrated

normally, while discrete events are evaluated independently and folded into the

aggregate as weighted event contributions.

"""

from __future__ import annotations

import math
from dataclasses import dataclass, field
from typing import Any, Dict, Iterable, List, Optional


SINGULARITY_KEYWORDS = (
    "infinite subjective terror",
    "terror singularity",
    "unbounded suffering",
    "infinite suffering",
)


def _clamp(value: float, low: float, high: float) -> float:
    return max(low, min(high, value))


def _coerce_numeric(value: Any, default: float = 0.0) -> float:
    if isinstance(value, (int, float)):
        return float(value)
    if isinstance(value, str):
        lowered = value.strip().lower()
        if lowered in {"inf", "+inf", "infinity", "+infinity"}:
            return math.inf
        if lowered in {"-inf", "-infinity"}:
            return -math.inf
        try:
            return float(value)
        except ValueError:
            return default
    return default


@dataclass
class ContinuousInterval:
    """Value density over a continuous interval."""

    start: float
    end: float
    start_value: float
    end_value: Optional[float] = None
    confidence: float = 1.0
    label: str = "continuous"

    def __post_init__(self) -> None:
        self.start = _coerce_numeric(self.start)
        self.end = _coerce_numeric(self.end)
        self.start_value = _coerce_numeric(self.start_value)
        self.end_value = self.start_value if self.end_value is None else _coerce_numeric(self.end_value)
        if self.end < self.start:
            raise ValueError("ContinuousInterval end must be >= start")
        self.confidence = _clamp(float(self.confidence), 0.0, 1.0)

    @property
    def duration(self) -> float:
        return self.end - self.start

    @property
    def average_density(self) -> float:
        return (self.start_value + self.end_value) / 2.0

    @property
    def integrated_value(self) -> float:
        return self.duration * self.average_density * self.confidence

    def to_dict(self) -> Dict[str, Any]:
        return {
            "label": self.label,
            "start": self.start,
            "end": self.end,
            "duration": self.duration,
            "start_value": self.start_value,
            "end_value": self.end_value,
            "confidence": self.confidence,
            "integrated_value": self.integrated_value,
        }


@dataclass
class DiscreteEvent:
    """A discrete event with event-embedded value."""

    at: float
    label: str
    impact: float
    probability: float = 1.0
    sensitivity: float = 1.0
    duration: float = 0.0
    singularity: bool = False
    context_weights: Dict[str, float] = field(default_factory=dict)
    event_embedded_value: Optional[float] = None
    aegis_eta: Optional[float] = None
    aegis_vetoed: bool = False
    aegis_reason: Optional[str] = None

    def __post_init__(self) -> None:
        self.at = _coerce_numeric(self.at)
        self.impact = _coerce_numeric(self.impact)
        if self.event_embedded_value is not None:
            self.event_embedded_value = _coerce_numeric(self.event_embedded_value)
        if self.aegis_eta is not None:
            self.aegis_eta = _clamp(_coerce_numeric(self.aegis_eta, 0.8), 0.0, 1.0)
        self.probability = _clamp(float(self.probability), 0.0, 1.0)
        self.sensitivity = max(float(self.sensitivity), 0.0)
        self.duration = max(float(self.duration), 0.0)
        if self._is_keyword_singularity():
            self.singularity = True

    def _is_keyword_singularity(self) -> bool:
        lowered = self.label.lower()
        return any(keyword in lowered for keyword in SINGULARITY_KEYWORDS)

    @property
    def context_multiplier(self) -> float:
        if not self.context_weights:
            return 1.0
        multiplier = 1.0
        for raw in self.context_weights.values():
            multiplier *= _clamp(float(raw), 0.1, 10.0)
        return multiplier

    @property
    def duration_weight(self) -> float:
        return 1.0 + math.log1p(self.duration)

    @property
    def ethical_multiplier(self) -> float:
        if self.aegis_eta is None and not self.aegis_vetoed:
            return 1.0
        eta = 0.8 if self.aegis_eta is None else self.aegis_eta
        if self.impact < 0:
            multiplier = 1.0 + (1.0 - eta) * 0.75
            if self.aegis_vetoed:
                multiplier += 0.5
            return multiplier
        multiplier = 1.0 - (1.0 - eta) * 0.25
        if self.aegis_vetoed:
            multiplier *= 0.7
        return max(multiplier, 0.1)

    @property
    def weighted_value(self) -> float:
        if self.event_embedded_value is not None:
            return float(self.event_embedded_value)
        if math.isinf(self.impact):
            return self.impact
        return (
            self.impact
            * self.probability
            * self.sensitivity
            * self.context_multiplier
            * self.duration_weight
            * self.ethical_multiplier
        )

    def to_dict(self) -> Dict[str, Any]:
        return {
            "at": self.at,
            "label": self.label,
            "impact": self.impact,
            "probability": self.probability,
            "sensitivity": self.sensitivity,
            "duration": self.duration,
            "context_multiplier": self.context_multiplier,
            "ethical_multiplier": self.ethical_multiplier,
            "weighted_value": self.weighted_value,
            "singularity": self.singularity or math.isinf(self.weighted_value),
            "aegis_eta": self.aegis_eta,
            "aegis_vetoed": self.aegis_vetoed,
            "aegis_reason": self.aegis_reason,
        }


@dataclass
class EEVAnalysis:
    """Structured output from a singularity-aware valuation pass."""

    continuous_total: float
    discrete_total: float
    combined_total: float
    singularity_detected: bool
    singularity_mode: str
    singularity_events: List[Dict[str, Any]]
    intervals: List[Dict[str, Any]]
    events: List[Dict[str, Any]]
    dominant_events: List[Dict[str, Any]]
    notes: List[str]
    aegis_summary: Dict[str, Any]

    def to_dict(self) -> Dict[str, Any]:
        return {
            "continuous_total": self.continuous_total,
            "discrete_total": self.discrete_total,
            "combined_total": self.combined_total,
            "singularity_detected": self.singularity_detected,
            "singularity_mode": self.singularity_mode,
            "singularity_events": self.singularity_events,
            "intervals": self.intervals,
            "events": self.events,
            "dominant_events": self.dominant_events,
            "notes": self.notes,
            "aegis_summary": self.aegis_summary,
        }


@dataclass
class RiskFrontierComparison:
    mode: str
    scenarios: List[Dict[str, Any]]
    best_scenario: Optional[Dict[str, Any]]
    worst_scenario: Optional[Dict[str, Any]]
    notes: List[str]

    def to_dict(self) -> Dict[str, Any]:
        return {
            "mode": self.mode,
            "scenarios": self.scenarios,
            "best_scenario": self.best_scenario,
            "worst_scenario": self.worst_scenario,
            "notes": self.notes,
        }


class GlobalEthicsAEGIS:
    """

    AEGIS: Adaptive Ethical Governance Integration System



    25 global ethical frameworks spanning Western, Eastern, Indigenous, African, Islamic, Jewish,

    and Australian traditions. Provides transparent ethical modulation without retraining.



    Each framework evaluates events on 0.0-1.0 scale (0=violates, 1.0=aligns strongly).

    """

    # WESTERN TRADITIONS (6)
    FRAMEWORKS = {
        # Western - Individual focus
        "virtue_ethics": {
            "tradition": "Western (Aristotelian)",
            "focus": "Excellence, character development, virtues",
            "keywords": ["virtue", "excellence", "character", "flourishing", "eudaimonia"],
        },
        "deontology": {
            "tradition": "Western (Kantian)",
            "focus": "Duty, rules, universal principles, dignity",
            "keywords": ["duty", "obligation", "rule", "dignity", "categorical"],
        },
        "utilitarianism": {
            "tradition": "Western (Mill/Bentham)",
            "focus": "Greatest good for greatest number, consequences",
            "keywords": ["benefit", "happiness", "utility", "greatest good", "consequential"],
        },
        "rights_based": {
            "tradition": "Western (Locke/Nozick)",
            "focus": "Individual rights, freedoms, autonomy",
            "keywords": ["rights", "freedom", "autonomy", "individual", "liberty"],
        },
        "justice_fairness": {
            "tradition": "Western (Rawls)",
            "focus": "Fair distribution, equity, impartiality",
            "keywords": ["fair", "justice", "equity", "impartial", "distribution"],
        },
        "care_ethics": {
            "tradition": "Western (Gilligan/Noddings)",
            "focus": "Relationships, compassion, interdependence, responsiveness",
            "keywords": ["care", "compassion", "relationship", "responsive", "attentive"],
        },

        # EASTERN TRADITIONS (5)
        "confucian_harmony": {
            "tradition": "Eastern (Confucianism)",
            "focus": "Social harmony, relationships, filial duty, propriety",
            "keywords": ["harmony", "relationship", "duty", "propriety", "social"],
        },
        "daoist_balance": {
            "tradition": "Eastern (Daoism)",
            "focus": "Wu wei (non-action), balance, natural order, minimal force",
            "keywords": ["balance", "natural", "harmony", "non-force", "flow"],
        },
        "buddhist_compassion": {
            "tradition": "Eastern (Buddhism)",
            "focus": "Non-harm, compassion, interconnection, suffering reduction",
            "keywords": ["compassion", "non-harm", "suffering", "interconnect", "mindful"],
        },
        "hindu_dharma": {
            "tradition": "Eastern (Hinduism)",
            "focus": "Dharma (duty), cosmic order, karma, spiritual development",
            "keywords": ["dharma", "duty", "cosmic", "karma", "spiritual"],
        },
        "shinto_harmony": {
            "tradition": "Eastern (Shintoism)",
            "focus": "Harmony with nature, ritual purity, community, kami respect",
            "keywords": ["harmony", "nature", "ritual", "community", "sacred"],
        },

        # INDIGENOUS TRADITIONS (4)
        "ubuntu": {
            "tradition": "Indigenous (Bantu/African)",
            "focus": "Shared humanity, community, dignity, interdependence",
            "keywords": ["community", "shared", "humanity", "dignity", "together"],
        },
        "custodial_stewardship": {
            "tradition": "Indigenous (Native American/Global)",
            "focus": "Land stewardship, long-term thinking, future generations",
            "keywords": ["steward", "future", "generations", "land", "responsibility"],
        },
        "seven_generations": {
            "tradition": "Indigenous (Haudenosaunee)",
            "focus": "Long-term thinking, intergenerational responsibility",
            "keywords": ["future", "generation", "long-term", "ancestor", "descendant"],
        },
        "reciprocity_balance": {
            "tradition": "Indigenous (Circular Thinking)",
            "focus": "Give-and-take, circular thinking, balanced exchange",
            "keywords": ["reciprocal", "balance", "circle", "exchange", "cycle"],
        },

        # AFRICAN TRADITIONS (3)
        "maat": {
            "tradition": "African (Egyptian)",
            "focus": "Truth, balance, cosmic order, justice",
            "keywords": ["truth", "balance", "cosmic", "order", "justice"],
        },
        "african_humanism": {
            "tradition": "African (Pan-African)",
            "focus": "Dignity, community, humanity, shared responsibility",
            "keywords": ["dignity", "humanity", "community", "responsibility", "person"],
        },
        "oral_tradition_ethics": {
            "tradition": "African (Oral Traditions)",
            "focus": "Wisdom, storytelling, collective memory, elder respect",
            "keywords": ["story", "wisdom", "collective", "elder", "memory"],
        },

        # ISLAMIC TRADITIONS (2)
        "islamic_ethics": {
            "tradition": "Islamic",
            "focus": "Justice, community welfare, submission to divine will",
            "keywords": ["justice", "welfare", "community", "divine", "submission"],
        },
        "sufi_ethics": {
            "tradition": "Islamic (Sufism)",
            "focus": "Compassion, spiritual development, transcendence, love",
            "keywords": ["compassion", "spiritual", "love", "transcend", "divine"],
        },

        # JEWISH TRADITIONS (2)
        "talmudic_ethics": {
            "tradition": "Jewish (Talmudic)",
            "focus": "Debate, interpretation, communal responsibility, justice",
            "keywords": ["justice", "debate", "community", "responsibility", "learning"],
        },
        "covenant_ethics": {
            "tradition": "Jewish (Covenant)",
            "focus": "Mutual responsibility, community bonds, covenantal duty",
            "keywords": ["covenant", "community", "mutual", "responsibility", "bond"],
        },

        # INDIGENOUS AUSTRALIAN (2)
        "dreamtime_ethics": {
            "tradition": "Indigenous Australian (Dreamtime)",
            "focus": "Sacred connection to land, responsibility to country",
            "keywords": ["land", "sacred", "country", "connection", "responsibility"],
        },
        "kinship_ethics": {
            "tradition": "Indigenous Australian (Kinship)",
            "focus": "Extended family responsibility, collective obligation",
            "keywords": ["family", "kinship", "collective", "obligation", "community"],
        },

        # MESOAMERICAN (1)
        "cosmic_reciprocity": {
            "tradition": "Mesoamerican (Aztec/Maya)",
            "focus": "Reciprocal cosmic order, balance between humans and nature",
            "keywords": ["cosmic", "reciprocal", "balance", "nature", "order"],
        },
    }

    def __init__(self):
        self.event_history = []

    def evaluate_event(self, event: DiscreteEvent) -> Dict[str, Any]:
        """

        Evaluate a single event across all 25 ethical frameworks.

        Returns scores 0.0-1.0 for each framework (1.0 = strong alignment, 0.0 = strong violation).

        """
        # Build rich context from event label and all context weights
        context_parts = [event.label.lower()]
        context_parts.extend([k.lower() for k in event.context_weights.keys()])
        context = " ".join(context_parts)

        scores = {}
        for framework_name, framework_info in self.FRAMEWORKS.items():
            score = self._evaluate_framework(framework_name, context, event)
            scores[framework_name] = {
                "score": score,
                "tradition": framework_info["tradition"],
                "focus": framework_info["focus"],
            }

        # Calculate aggregate
        framework_scores = [s["score"] for s in scores.values()]
        aggregate = sum(framework_scores) / len(framework_scores) if framework_scores else 0.5

        # Identify dominant frameworks (strong alignment or strong violation)
        strong_align = [f for f, s in scores.items() if s["score"] >= 0.8]
        strong_violate = [f for f, s in scores.items() if s["score"] <= 0.2]

        return {
            "event_label": event.label,
            "framework_scores": scores,
            "aggregate_modulation": aggregate,
            "strongly_aligned": strong_align,
            "strongly_violated": strong_violate,
            "tradition_breakdown": self._breakdown_by_tradition(scores),
        }

    def _evaluate_framework(self, framework: str, context: str, event: DiscreteEvent) -> float:
        """

        Evaluate how well an event aligns with a specific ethical framework.

        Returns 0.0 (violation) to 1.0 (strong alignment).

        """
        framework_info = self.FRAMEWORKS.get(framework, {})
        keywords = framework_info.get("keywords", [])

        # Base score from keyword matching
        matches = sum(1 for keyword in keywords if keyword in context)
        match_score = 0.4 + (matches / max(len(keywords), 1)) * 0.6  # Range 0.4-1.0 based on keyword match

        # Strong boost for direct framework alignment based on event context_weights
        context_weight_boost = sum(
            event.context_weights.get(keyword, 0.0)
            for keyword in keywords
            if keyword in event.context_weights
        )
        match_score = min(1.0, match_score + (context_weight_boost * 0.1))

        # Penalize for negative impact on relationships/community
        if event.impact < 0:
            if framework in ["ubuntu", "confucian_harmony", "care_ethics", "covenant_ethics",
                            "buddhist_compassion", "african_humanism"]:
                match_score *= 0.6  # These frameworks penalize harm

        # Penalize for short-term thinking when framework values long-term
        if framework in ["seven_generations", "custodial_stewardship", "dreamtime_ethics"]:
            # These value long-term; short duration/singularities reduce score
            if event.duration == 0.0:
                match_score *= 0.8

        # Penalize for unilateral action when framework values reciprocity/balance
        if framework in ["reciprocity_balance", "daoist_balance", "maat", "cosmic_reciprocity", "confucian_harmony"]:
            if "force" in context or "unilateral" in context:
                match_score *= 0.7

        # Significant boost for positive community/relationship impact
        if framework in ["ubuntu", "confucian_harmony", "care_ethics", "kinship_ethics", "covenant_ethics",
                        "confucian_harmony", "buddhist_compassion", "sufi_ethics"]:
            if event.impact > 0 and ("community" in context or "relationship" in context):
                match_score = min(1.0, match_score * 1.5)  # Up to 50% boost

        # Boost for long-term thinking
        if framework in ["seven_generations", "custodial_stewardship", "dreamtime_ethics"]:
            if "future" in context or "generation" in context:
                match_score = min(1.0, match_score * 1.3)

        return _clamp(match_score, 0.0, 1.0)

    def _breakdown_by_tradition(self, scores: Dict[str, Dict]) -> Dict[str, float]:
        """Group framework scores by major tradition."""
        traditions = {}

        for framework_name, framework_score in scores.items():
            tradition = framework_score["tradition"].split("(")[0].strip()
            if tradition not in traditions:
                traditions[tradition] = []
            traditions[tradition].append(framework_score["score"])

        # Average scores by tradition
        breakdown = {}
        for tradition, scores_list in traditions.items():
            if scores_list:
                breakdown[tradition] = round(sum(scores_list) / len(scores_list), 3)

        return breakdown


class EventEmbeddedValueEngine:
    """

    Evaluate value across continuous intervals and discrete singular events.



    Singularity modes:

      - "strict": any singular negative event makes the combined value -inf

      - "bounded": singular events are clamped to +/- singularity_cap

      - "report_only": singularities are reported but not transformed



    Now includes GlobalEthicsAEGIS for 25-framework ethical evaluation.

    """

    def __init__(self, singularity_cap: float = 1_000_000.0) -> None:
        self.singularity_cap = float(singularity_cap)
        self.aegis = GlobalEthicsAEGIS()

    def analyze(

        self,

        intervals: Iterable[ContinuousInterval],

        events: Iterable[DiscreteEvent],

        singularity_mode: str = "strict",

    ) -> EEVAnalysis:
        parsed_intervals = list(intervals)
        parsed_events = list(events)

        continuous_total = sum(interval.integrated_value for interval in parsed_intervals)
        discrete_total = 0.0
        singularity_events: List[Dict[str, Any]] = []
        event_dicts: List[Dict[str, Any]] = []
        ethics_evaluations: List[Dict[str, Any]] = []

        for event in parsed_events:
            # Evaluate event against 25 global ethical frameworks
            ethics_eval = self.aegis.evaluate_event(event)
            ethics_evaluations.append(ethics_eval)

            # Apply ethical modulation to event
            ethical_modifier = ethics_eval["aggregate_modulation"]
            event.aegis_eta = ethical_modifier

            weighted = event.weighted_value
            is_singularity = event.singularity or math.isinf(weighted)
            if is_singularity:
                singularity_events.append(event.to_dict())
                if singularity_mode == "strict":
                    discrete_total = math.copysign(math.inf, weighted if weighted != 0 else -1.0)
                elif singularity_mode == "bounded":
                    bounded = math.copysign(self.singularity_cap, weighted if weighted != 0 else -1.0)
                    discrete_total += bounded
                else:
                    if math.isfinite(weighted):
                        discrete_total += weighted
            elif not math.isinf(discrete_total):
                discrete_total += weighted
            event_dicts.append(event.to_dict())

        if math.isinf(discrete_total):
            combined_total = discrete_total
        else:
            combined_total = continuous_total + discrete_total

        dominant_events = sorted(
            event_dicts,
            key=lambda item: abs(item["weighted_value"]) if math.isfinite(item["weighted_value"]) else math.inf,
            reverse=True,
        )[:3]

        notes = [
            "Continuous value was integrated piecewise across the supplied intervals.",
            "Discrete events were evaluated independently using probability, sensitivity, context, and duration.",
        ]
        if singularity_events:
            notes.append(
                f"Detected {len(singularity_events)} singular event(s); mode '{singularity_mode}' determined how they affected the aggregate."
            )

        aegis_summary = self._summarize_aegis_v2(ethics_evaluations)
        if ethics_evaluations:
            notes.append(
                f"AEGIS Global Ethics (25 frameworks) evaluated {len(ethics_evaluations)} event(s). "
                f"Average ethical alignment: {aegis_summary['average_modulation']:.2%}. "
                f"Traditions represented: {', '.join(aegis_summary['active_traditions'])}."
            )

        return EEVAnalysis(
            continuous_total=continuous_total,
            discrete_total=discrete_total,
            combined_total=combined_total,
            singularity_detected=bool(singularity_events),
            singularity_mode=singularity_mode,
            singularity_events=singularity_events,
            intervals=[interval.to_dict() for interval in parsed_intervals],
            events=event_dicts,
            dominant_events=dominant_events,
            notes=notes,
            aegis_summary=aegis_summary,
        )

    def compare_frontier(

        self,

        scenarios: Iterable[Dict[str, Any]],

        mode: str = "maximize_value",

    ) -> RiskFrontierComparison:
        analyzed = []
        for idx, scenario in enumerate(scenarios):
            name = scenario.get("name", f"scenario_{idx + 1}")
            analysis = self.analyze(
                intervals=[ContinuousInterval(**item) for item in scenario.get("intervals", [])],
                events=[DiscreteEvent(**item) for item in scenario.get("events", [])],
                singularity_mode=scenario.get("singularity_mode", "strict"),
            ).to_dict()
            analyzed.append({
                "name": name,
                "analysis": analysis,
                "score": self._frontier_score(analysis, mode),
            })

        ranked = sorted(analyzed, key=lambda item: item["score"], reverse=True)
        notes = [
            f"Risk frontier compared {len(ranked)} candidate futures using mode '{mode}'.",
            "Scores rank scenarios side-by-side while preserving singular outcomes instead of flattening them.",
        ]
        return RiskFrontierComparison(
            mode=mode,
            scenarios=ranked,
            best_scenario=ranked[0] if ranked else None,
            worst_scenario=ranked[-1] if ranked else None,
            notes=notes,
        )

    def _summarize_aegis(self, events: List[DiscreteEvent]) -> Dict[str, Any]:
        evaluated = [event for event in events if event.aegis_eta is not None or event.aegis_vetoed]
        if not evaluated:
            return {
                "events_evaluated": 0,
                "vetoed_events": 0,
                "mean_eta": None,
            }
        etas = [event.aegis_eta for event in evaluated if event.aegis_eta is not None]
        return {
            "events_evaluated": len(evaluated),
            "vetoed_events": sum(1 for event in evaluated if event.aegis_vetoed),
            "mean_eta": None if not etas else round(sum(etas) / len(etas), 4),
        }

    def _summarize_aegis_v2(self, ethics_evaluations: List[Dict[str, Any]]) -> Dict[str, Any]:
        """Summarize ethics evaluations across all 25 global frameworks."""
        if not ethics_evaluations:
            return {
                "events_evaluated": 0,
                "average_modulation": None,
                "active_traditions": [],
                "tradition_breakdown": {},
                "strongest_alignments": [],
                "strongest_violations": [],
            }

        # Aggregate modulations
        modulations = [e["aggregate_modulation"] for e in ethics_evaluations]
        avg_modulation = sum(modulations) / len(modulations)

        # Aggregate tradition breakdown
        all_traditions = {}
        for eval_item in ethics_evaluations:
            for tradition, score in eval_item["tradition_breakdown"].items():
                if tradition not in all_traditions:
                    all_traditions[tradition] = []
                all_traditions[tradition].append(score)

        # Average by tradition
        tradition_breakdown = {
            tradition: round(sum(scores) / len(scores), 3)
            for tradition, scores in all_traditions.items()
        }

        # Find strongest alignments and violations across all evaluations
        all_alignments = {}
        all_violations = {}
        for eval_item in ethics_evaluations:
            for framework in eval_item.get("strongly_aligned", []):
                all_alignments[framework] = all_alignments.get(framework, 0) + 1
            for framework in eval_item.get("strongly_violated", []):
                all_violations[framework] = all_violations.get(framework, 0) + 1

        strongest_alignments = sorted(all_alignments.items(), key=lambda x: x[1], reverse=True)[:5]
        strongest_violations = sorted(all_violations.items(), key=lambda x: x[1], reverse=True)[:5]

        return {
            "events_evaluated": len(ethics_evaluations),
            "average_modulation": round(avg_modulation, 3),
            "active_traditions": list(tradition_breakdown.keys()),
            "tradition_breakdown": tradition_breakdown,
            "strongest_alignments": [f[0] for f in strongest_alignments],
            "strongest_violations": [f[0] for f in strongest_violations],
            "all_ethics_evals": ethics_evaluations,
        }

    def _frontier_score(self, analysis: Dict[str, Any], mode: str) -> float:
        combined = analysis.get("combined_total", 0.0)
        if math.isinf(combined):
            return combined
        if mode == "minimize_harm":
            return -analysis.get("discrete_total", 0.0)
        return combined

    def analyze_payload(self, payload: Dict[str, Any]) -> Dict[str, Any]:
        analysis_mode = payload.get("analysis_mode", "single")
        if analysis_mode == "risk_frontier":
            return self.compare_frontier(
                scenarios=payload.get("scenarios", []),
                mode=payload.get("frontier_mode", "maximize_value"),
            ).to_dict()

        intervals = [ContinuousInterval(**item) for item in payload.get("intervals", [])]
        events = [DiscreteEvent(**item) for item in payload.get("events", [])]
        mode = payload.get("singularity_mode", "strict")
        return self.analyze(intervals=intervals, events=events, singularity_mode=mode).to_dict()