-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscoring.py
More file actions
126 lines (113 loc) · 4.42 KB
/
Copy pathscoring.py
File metadata and controls
126 lines (113 loc) · 4.42 KB
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
from __future__ import annotations
from dataclasses import dataclass
from typing import Protocol
from memory_engine.anomaly import AnomalyPolicy, ThresholdAnomalyPolicy
from memory_engine.memory_state import MemoryStatePolicy
from memory_engine.schema import ActivationContext, MemoryNode
from memory_engine.semantics import query_role_alignment_score, semantic_score_signals
@dataclass(slots=True)
class ScoreBreakdown:
semantic_score: float
structural_score: float
anomaly_score: float
importance_score: float
exception_score: float
contradiction_score: float
total_score: float
role_alignment_score: float = 0.0
class ScoringStrategy(Protocol):
def score_node(
self,
*,
query: str,
node: MemoryNode,
semantic_score: float,
context: ActivationContext,
depth: int,
source_node_id: str | None = None,
) -> ScoreBreakdown:
"""Return a score breakdown for a node."""
class WeightedSumScoringStrategy:
def __init__(
self,
anomaly_threshold: float = 0.8,
depth_penalty: float = 0.25,
anomaly_policy: AnomalyPolicy | None = None,
memory_state_policy: MemoryStatePolicy | None = None,
) -> None:
self.anomaly_threshold = anomaly_threshold
self.depth_penalty = depth_penalty
self.anomaly_policy = anomaly_policy or ThresholdAnomalyPolicy(
risk_threshold=anomaly_threshold,
novelty_threshold=anomaly_threshold,
)
self.memory_state_policy = memory_state_policy or MemoryStatePolicy()
def score_node(
self,
*,
query: str,
node: MemoryNode,
semantic_score: float,
context: ActivationContext,
depth: int,
source_node_id: str | None = None,
) -> ScoreBreakdown:
structural_score = max(0.0, 1.0 - depth * self.depth_penalty)
anomaly_score = 1.0 if self._is_anomalous(node) else 0.0
importance_score = self.memory_state_policy.effective_weight_score(node.weights)
semantic_signals = semantic_score_signals(node, source_node_id=source_node_id)
exception_score = semantic_signals.exception_score
contradiction_score = semantic_signals.contradiction_score
role_alignment_score = query_role_alignment_score(query, node)
weighted_bonus_gate = semantic_score
total_score = (
semantic_score * context.semantic_weight
+ structural_score * context.structural_weight
+ anomaly_score * context.anomaly_weight * weighted_bonus_gate
+ importance_score * context.importance_weight * weighted_bonus_gate
+ exception_score * context.exception_weight * weighted_bonus_gate
+ contradiction_score * context.contradiction_weight * weighted_bonus_gate
+ role_alignment_score * 0.12 * weighted_bonus_gate
)
total_score *= self.memory_state_policy.recall_multiplier(node)
return ScoreBreakdown(
semantic_score=semantic_score,
structural_score=structural_score,
anomaly_score=anomaly_score,
importance_score=importance_score,
exception_score=exception_score,
contradiction_score=contradiction_score,
role_alignment_score=role_alignment_score,
total_score=total_score,
)
def _is_anomalous(self, node: MemoryNode) -> bool:
return bool(self.anomaly_policy.signals_for_node(node=node))
class StructureOnlyScoringStrategy:
def __init__(self, depth_penalty: float = 0.25) -> None:
self.depth_penalty = depth_penalty
def score_node(
self,
*,
query: str,
node: MemoryNode,
semantic_score: float,
context: ActivationContext,
depth: int,
source_node_id: str | None = None,
) -> ScoreBreakdown:
del query, node, source_node_id
structural_score = max(0.0, 1.0 - depth * self.depth_penalty)
total_score = (
semantic_score * context.semantic_weight
+ structural_score * context.structural_weight
)
return ScoreBreakdown(
semantic_score=semantic_score,
structural_score=structural_score,
anomaly_score=0.0,
importance_score=0.0,
exception_score=0.0,
contradiction_score=0.0,
role_alignment_score=0.0,
total_score=total_score,
)