feat(emotion-e6): project event affect deltas
这个提交存在于:
父节点
7b19a1f716
当前提交
e1ca6ab3ce
共有 4 个文件被更改,包括 107 次插入 和 1 次删除
46
core/affect_modulation.py
普通文件
46
core/affect_modulation.py
普通文件
|
|
@ -0,0 +1,46 @@
|
|||
"""Bounded affect modulation DTO shared across companion plugins."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import math
|
||||
from typing import Any, Mapping
|
||||
|
||||
AFFECT_MODULATION_VERSION = "affect_modulation.v1"
|
||||
AFFECT_MODULATION_FIELDS = ("schema_version", "valence", "arousal", "vulnerability", "confidence", "source_event_ids", "computed_at")
|
||||
AFFECT_MODULATION_FINGERPRINT = hashlib.sha256("|".join(AFFECT_MODULATION_FIELDS).encode("ascii")).hexdigest()[:20]
|
||||
|
||||
|
||||
def _number(value: Any, default: float, low: float, high: float) -> float:
|
||||
if type(value) not in {int, float}:
|
||||
return default
|
||||
numeric = float(value)
|
||||
if not math.isfinite(numeric):
|
||||
return default
|
||||
return round(max(low, min(high, numeric)), 4)
|
||||
|
||||
|
||||
def normalize_affect_modulation(value: Any) -> dict[str, Any]:
|
||||
source = dict(value) if isinstance(value, Mapping) else {}
|
||||
raw_ids = source.get("source_event_ids")
|
||||
ids: list[str] = []
|
||||
if isinstance(raw_ids, (list, tuple)):
|
||||
for item in raw_ids[:12]:
|
||||
if type(item) is not str:
|
||||
continue
|
||||
cleaned = " ".join(item.split())[:96]
|
||||
if cleaned and cleaned not in ids:
|
||||
ids.append(cleaned)
|
||||
computed_at = source.get("computed_at")
|
||||
return {
|
||||
"schema_version": AFFECT_MODULATION_VERSION,
|
||||
"valence": _number(source.get("valence"), 0.0, -1.0, 1.0),
|
||||
"arousal": _number(source.get("arousal"), 0.0, 0.0, 1.0),
|
||||
"vulnerability": _number(source.get("vulnerability"), 0.0, 0.0, 1.0),
|
||||
"confidence": _number(source.get("confidence"), 0.0, 0.0, 1.0),
|
||||
"source_event_ids": ids,
|
||||
"computed_at": float(computed_at) if type(computed_at) in {int, float} and math.isfinite(float(computed_at)) else 0.0,
|
||||
}
|
||||
|
||||
|
||||
__all__ = ["AFFECT_MODULATION_FIELDS", "AFFECT_MODULATION_FINGERPRINT", "AFFECT_MODULATION_VERSION", "normalize_affect_modulation"]
|
||||
|
|
@ -68,6 +68,7 @@ _COMPANION_EXPRESSION_REASON_CODES = {
|
|||
"low_energy_expression_cap",
|
||||
"down_mood_expression_cap",
|
||||
"up_mood_expression_lift",
|
||||
"affect_modulation_applied",
|
||||
"relationship_proactive_cap",
|
||||
"interaction_proactive_suppressed",
|
||||
"schedule_proactive_suppressed",
|
||||
|
|
|
|||
|
|
@ -5817,7 +5817,9 @@ class MemoryStore:
|
|||
|
||||
@staticmethod
|
||||
def _emotion_delivery_projection(event: dict[str, Any]) -> dict[str, Any]:
|
||||
return {
|
||||
from .affect_modulation import normalize_affect_modulation
|
||||
|
||||
projection = {
|
||||
"event_id": clean_text(event.get("event_id"), 96),
|
||||
"revision": max(1, int(event.get("revision") or 1)),
|
||||
"trace_id": clean_text(event.get("trace_id"), 96),
|
||||
|
|
@ -5832,6 +5834,14 @@ class MemoryStore:
|
|||
"occurred_at": clean_text(event.get("occurred_at"), 48),
|
||||
"expires_at": clean_text(event.get("expires_at"), 48),
|
||||
}
|
||||
projection["affect_modulation"] = normalize_affect_modulation({
|
||||
"valence": projection["valence"],
|
||||
"arousal": projection["arousal"],
|
||||
"vulnerability": projection["vulnerability"],
|
||||
"confidence": projection["confidence"],
|
||||
"source_event_ids": [projection["event_id"]],
|
||||
})
|
||||
return projection
|
||||
|
||||
@staticmethod
|
||||
def _emotion_event_row(row: sqlite3.Row) -> dict[str, Any]:
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
import unittest
|
||||
|
||||
from core.affect_modulation import normalize_affect_modulation
|
||||
from core.emotion_event_contract import normalize_emotion_event
|
||||
from core.store import MemoryStore
|
||||
|
||||
|
||||
class EmotionE6AffectEventProjectionTests(unittest.IsolatedAsyncioTestCase):
|
||||
def make_store(self) -> MemoryStore:
|
||||
temp_dir = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(temp_dir.cleanup)
|
||||
store = MemoryStore(Path(temp_dir.name) / "memory.db")
|
||||
store.initialize()
|
||||
self.addCleanup(store.close)
|
||||
return store
|
||||
|
||||
async def test_delivery_contains_bounded_event_delta_without_mood_command(self) -> None:
|
||||
store = self.make_store()
|
||||
event = normalize_emotion_event({
|
||||
"event_id": "emo-affect",
|
||||
"origin_kind": "memory_recall",
|
||||
"event_type": "warm_memory",
|
||||
"session_id": "session-a",
|
||||
"valence_hint": 2.0,
|
||||
"arousal_hint": 0.6,
|
||||
"vulnerability_hint": 0.4,
|
||||
"confidence": 0.8,
|
||||
"dedupe_key": "affect",
|
||||
}, producer_plugin="memory_companion")
|
||||
await store.upsert_emotion_event(event)
|
||||
page = await store.list_emotion_event_deliveries(consumer_id="companion", limit=5)
|
||||
delivered = page["events"][0]
|
||||
self.assertEqual(1.0, delivered["affect_modulation"]["valence"])
|
||||
self.assertEqual(["emo-affect"], delivered["affect_modulation"]["source_event_ids"])
|
||||
self.assertNotIn("mood_hint", delivered)
|
||||
self.assertNotIn("memory_id", delivered)
|
||||
|
||||
def test_old_or_nonfinite_values_degrade_to_neutral(self) -> None:
|
||||
result = normalize_affect_modulation({"valence": "1", "arousal": float("nan")})
|
||||
self.assertEqual(0.0, result["valence"])
|
||||
self.assertEqual(0.0, result["arousal"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
正在加载…
添加表格
在新工单中引用