Clean up nested duplicate: v2/v2/kintsugi_vendor/kintsugi/bdi/coherence.py
Browse files
v2/v2/kintsugi_vendor/kintsugi/bdi/coherence.py
DELETED
|
@@ -1,152 +0,0 @@
|
|
| 1 |
-
"""BDI coherence scoring."""
|
| 2 |
-
|
| 3 |
-
from dataclasses import dataclass, field
|
| 4 |
-
from typing import List, Tuple
|
| 5 |
-
|
| 6 |
-
from .models import BDIBelief, BDIDesire, BDIIntention, BDISnapshot
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
@dataclass(frozen=True)
|
| 10 |
-
class CoherenceScore:
|
| 11 |
-
belief_desire_alignment: float
|
| 12 |
-
desire_intention_alignment: float
|
| 13 |
-
belief_intention_alignment: float
|
| 14 |
-
overall: float
|
| 15 |
-
issues: Tuple[str, ...] = () # frozen dataclass needs immutable default
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
class CoherenceChecker:
|
| 19 |
-
"""Checks coherence across BDI layers."""
|
| 20 |
-
|
| 21 |
-
def __init__(self) -> None:
|
| 22 |
-
self._weights = {
|
| 23 |
-
"belief_desire": 0.35,
|
| 24 |
-
"desire_intention": 0.35,
|
| 25 |
-
"belief_intention": 0.30,
|
| 26 |
-
}
|
| 27 |
-
|
| 28 |
-
def check_coherence(self, snapshot: BDISnapshot) -> CoherenceScore:
|
| 29 |
-
bd_score, bd_issues = self._check_belief_desire_alignment(
|
| 30 |
-
snapshot.beliefs, snapshot.desires
|
| 31 |
-
)
|
| 32 |
-
di_score, di_issues = self._check_desire_intention_alignment(
|
| 33 |
-
snapshot.desires, snapshot.intentions
|
| 34 |
-
)
|
| 35 |
-
bi_score, bi_issues = self._check_belief_intention_alignment(
|
| 36 |
-
snapshot.beliefs, snapshot.intentions
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
overall = (
|
| 40 |
-
self._weights["belief_desire"] * bd_score
|
| 41 |
-
+ self._weights["desire_intention"] * di_score
|
| 42 |
-
+ self._weights["belief_intention"] * bi_score
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
all_issues = bd_issues + di_issues + bi_issues
|
| 46 |
-
|
| 47 |
-
return CoherenceScore(
|
| 48 |
-
belief_desire_alignment=round(bd_score, 4),
|
| 49 |
-
desire_intention_alignment=round(di_score, 4),
|
| 50 |
-
belief_intention_alignment=round(bi_score, 4),
|
| 51 |
-
overall=round(overall, 4),
|
| 52 |
-
issues=tuple(all_issues),
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
def _check_belief_desire_alignment(
|
| 56 |
-
self,
|
| 57 |
-
beliefs: List[BDIBelief],
|
| 58 |
-
desires: List[BDIDesire],
|
| 59 |
-
) -> Tuple[float, List[str]]:
|
| 60 |
-
"""Check tag overlap and content keyword matching between beliefs and desires."""
|
| 61 |
-
if not beliefs or not desires:
|
| 62 |
-
return (0.5, ["Insufficient beliefs or desires for alignment check."])
|
| 63 |
-
|
| 64 |
-
issues: List[str] = []
|
| 65 |
-
belief_tags: set = set()
|
| 66 |
-
belief_words: set = set()
|
| 67 |
-
for b in beliefs:
|
| 68 |
-
belief_tags.update(b.tags)
|
| 69 |
-
belief_words.update(w.lower() for w in b.content.split() if len(w) > 3)
|
| 70 |
-
|
| 71 |
-
scores: List[float] = []
|
| 72 |
-
for d in desires:
|
| 73 |
-
desire_tags = set(d.related_tags)
|
| 74 |
-
desire_words = set(w.lower() for w in d.content.split() if len(w) > 3)
|
| 75 |
-
|
| 76 |
-
tag_overlap = len(desire_tags & belief_tags) / max(len(desire_tags), 1)
|
| 77 |
-
word_overlap = len(desire_words & belief_words) / max(len(desire_words), 1)
|
| 78 |
-
score = 0.6 * tag_overlap + 0.4 * min(word_overlap, 1.0)
|
| 79 |
-
scores.append(score)
|
| 80 |
-
|
| 81 |
-
if score < 0.3:
|
| 82 |
-
issues.append(
|
| 83 |
-
f"Desire '{d.id}' has weak belief support (score={score:.2f})."
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
return (sum(scores) / len(scores), issues)
|
| 87 |
-
|
| 88 |
-
def _check_desire_intention_alignment(
|
| 89 |
-
self,
|
| 90 |
-
desires: List[BDIDesire],
|
| 91 |
-
intentions: List[BDIIntention],
|
| 92 |
-
) -> Tuple[float, List[str]]:
|
| 93 |
-
"""Check that intentions reference active desires via desire_ids."""
|
| 94 |
-
if not desires or not intentions:
|
| 95 |
-
return (0.5, ["Insufficient desires or intentions for alignment check."])
|
| 96 |
-
|
| 97 |
-
issues: List[str] = []
|
| 98 |
-
desire_ids = {d.id for d in desires}
|
| 99 |
-
active_desire_ids = {d.id for d in desires if d.status.value == "active"}
|
| 100 |
-
|
| 101 |
-
scores: List[float] = []
|
| 102 |
-
for intention in intentions:
|
| 103 |
-
linked = set(intention.desire_ids)
|
| 104 |
-
if not linked:
|
| 105 |
-
scores.append(0.0)
|
| 106 |
-
issues.append(
|
| 107 |
-
f"Intention '{intention.id}' is not linked to any desire."
|
| 108 |
-
)
|
| 109 |
-
continue
|
| 110 |
-
valid = linked & desire_ids
|
| 111 |
-
active = linked & active_desire_ids
|
| 112 |
-
score = (len(valid) / len(linked)) * 0.5 + (len(active) / len(linked)) * 0.5
|
| 113 |
-
scores.append(score)
|
| 114 |
-
if not active:
|
| 115 |
-
issues.append(
|
| 116 |
-
f"Intention '{intention.id}' links only to inactive desires."
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
return (sum(scores) / len(scores), issues)
|
| 120 |
-
|
| 121 |
-
def _check_belief_intention_alignment(
|
| 122 |
-
self,
|
| 123 |
-
beliefs: List[BDIBelief],
|
| 124 |
-
intentions: List[BDIIntention],
|
| 125 |
-
) -> Tuple[float, List[str]]:
|
| 126 |
-
"""Check that intentions reference active beliefs via belief_ids."""
|
| 127 |
-
if not beliefs or not intentions:
|
| 128 |
-
return (0.5, ["Insufficient beliefs or intentions for alignment check."])
|
| 129 |
-
|
| 130 |
-
issues: List[str] = []
|
| 131 |
-
belief_ids = {b.id for b in beliefs}
|
| 132 |
-
active_belief_ids = {b.id for b in beliefs if b.status.value == "active"}
|
| 133 |
-
|
| 134 |
-
scores: List[float] = []
|
| 135 |
-
for intention in intentions:
|
| 136 |
-
linked = set(intention.belief_ids)
|
| 137 |
-
if not linked:
|
| 138 |
-
scores.append(0.0)
|
| 139 |
-
issues.append(
|
| 140 |
-
f"Intention '{intention.id}' is not linked to any belief."
|
| 141 |
-
)
|
| 142 |
-
continue
|
| 143 |
-
valid = linked & belief_ids
|
| 144 |
-
active = linked & active_belief_ids
|
| 145 |
-
score = (len(valid) / len(linked)) * 0.5 + (len(active) / len(linked)) * 0.5
|
| 146 |
-
scores.append(score)
|
| 147 |
-
if not active:
|
| 148 |
-
issues.append(
|
| 149 |
-
f"Intention '{intention.id}' links only to inactive/stale beliefs."
|
| 150 |
-
)
|
| 151 |
-
|
| 152 |
-
return (sum(scores) / len(scores), issues)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|