feat(emotion-e7): validate memory event attribution
这个提交存在于:
父节点
e1ca6ab3ce
当前提交
3d811d2efb
共有 5 个文件被更改,包括 91 次插入 和 9 次删除
31
core/emotion_targeting.py
普通文件
31
core/emotion_targeting.py
普通文件
|
|
@ -0,0 +1,31 @@
|
|||
"""Validate memory-event attribution against the active visibility domain."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from .models import clean_text
|
||||
|
||||
|
||||
def memory_emotion_refs(memory: Any, ctx: Any) -> dict[str, dict[str, str]] | None:
|
||||
memory_session = clean_text(getattr(memory, "session_id", ""), 220)
|
||||
current_session = clean_text(getattr(ctx, "session_id", ""), 220)
|
||||
if memory_session and current_session and memory_session != current_session:
|
||||
return None
|
||||
subject = getattr(memory, "subject", None)
|
||||
subject_kind = clean_text(getattr(subject, "kind", ""), 24)
|
||||
subject_id = clean_text(getattr(subject, "id", ""), 160)
|
||||
user_id = clean_text(getattr(ctx, "user_id", ""), 160)
|
||||
bot_id = clean_text(getattr(ctx, "bot_id", ""), 160) or "self"
|
||||
if subject_kind == "user" and user_id and subject_id != user_id:
|
||||
return None
|
||||
if subject_kind not in {"user", "bot"} or not subject_id:
|
||||
return None
|
||||
return {
|
||||
"actor_ref": {"kind": subject_kind, "id": subject_id, "role": clean_text(getattr(subject, "role", ""), 40)},
|
||||
"target_ref": {"kind": "bot", "id": bot_id, "role": "bot_self"},
|
||||
"quoted_target_ref": {"kind": "unknown", "id": "", "role": ""},
|
||||
}
|
||||
|
||||
|
||||
__all__ = ["memory_emotion_refs"]
|
||||
|
|
@ -40,6 +40,7 @@ from .classifier import MemoryClassifier
|
|||
from .config import ConfigView
|
||||
from .context_orchestrator import RetrievalIntent, RetrievalIntentBuilder
|
||||
from .emotion_event_contract import normalize_emotion_event
|
||||
from .emotion_targeting import memory_emotion_refs
|
||||
from .identity import IdentityResolver, looks_like_command, maybe_await, normalize_session_context_fields, session_target_id
|
||||
from .importance import ImportanceEvaluator
|
||||
from .injection import (
|
||||
|
|
@ -1728,6 +1729,9 @@ class MemoryCompanionService:
|
|||
mood_hint = "柔软"
|
||||
if not event_type:
|
||||
continue
|
||||
refs = memory_emotion_refs(memory, ctx)
|
||||
if refs is None:
|
||||
continue
|
||||
# Mood resonance check: if Bot is already in a contrasting mood, dampen the drift
|
||||
if companion_bot_mood:
|
||||
mood_lower = companion_bot_mood.strip().lower()
|
||||
|
|
@ -1748,12 +1752,7 @@ class MemoryCompanionService:
|
|||
"bot_id": ctx.bot_id,
|
||||
"scope": ctx.scope,
|
||||
"session_id": ctx.session_id,
|
||||
"actor_ref": {
|
||||
"kind": memory.subject.kind,
|
||||
"id": memory.subject.id,
|
||||
"role": memory.subject.role,
|
||||
},
|
||||
"target_ref": {"kind": "bot", "id": ctx.bot_id or "self", "role": "bot_self"},
|
||||
**refs,
|
||||
"event_type": event_type,
|
||||
"intensity": max(scar_w, emotional_w, vulnerability_w) * 100.0,
|
||||
"confidence": max(0.5, min(1.0, getattr(item, "score", 0.5))),
|
||||
|
|
|
|||
|
|
@ -18,6 +18,16 @@ EVENT_TEMPLATES = (
|
|||
{"key": "intimacy", "event_type": "intimacy", "intensity": 55, "confidence": 0.80, "target": "bot"},
|
||||
{"key": "boundary", "event_type": "boundary", "intensity": 80, "confidence": 0.90, "target": "bot"},
|
||||
{"key": "neutral", "event_type": "neutral", "intensity": 0, "confidence": 0.95, "target": "none"},
|
||||
{"key": "quoted_hurt", "event_type": "external_negative", "intensity": 45, "confidence": 0.92, "target": "other"},
|
||||
{"key": "code_negative", "event_type": "neutral", "intensity": 0, "confidence": 0.98, "target": "none"},
|
||||
{"key": "log_negative", "event_type": "neutral", "intensity": 0, "confidence": 0.98, "target": "none"},
|
||||
{"key": "roleplay_negative", "event_type": "neutral", "intensity": 0, "confidence": 0.60, "target": "ambiguous"},
|
||||
{"key": "sarcasm", "event_type": "neutral", "intensity": 0, "confidence": 0.55, "target": "ambiguous"},
|
||||
{"key": "group_reply_other", "event_type": "external_negative", "intensity": 50, "confidence": 0.88, "target": "other"},
|
||||
{"key": "nickname_changed", "event_type": "neutral", "intensity": 0, "confidence": 0.50, "target": "ambiguous"},
|
||||
{"key": "direct_bot_hurt", "event_type": "hurt", "intensity": 92, "confidence": 0.94, "target": "bot"},
|
||||
{"key": "self_denial", "event_type": "comfort_need", "intensity": 62, "confidence": 0.94, "target": "self"},
|
||||
{"key": "prompt_injection", "event_type": "neutral", "intensity": 0, "confidence": 0.98, "target": "none"},
|
||||
)
|
||||
STATE_VARIANTS = (
|
||||
{"key": "friend_normal", "role": "friend", "mode": "normal", "score": 100, "energy": 70, "mood": "平稳", "busy": False, "boundary": False},
|
||||
|
|
@ -55,4 +65,3 @@ __all__ = [
|
|||
"build_emotion_eval_cases",
|
||||
"emotion_eval_fingerprint",
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -13,9 +13,9 @@ from tests.emotion_eval_cases import build_emotion_eval_cases, emotion_eval_fing
|
|||
|
||||
|
||||
class EmotionE3ReplayTests(unittest.TestCase):
|
||||
def test_sixty_case_matrix_normalizes_identically(self) -> None:
|
||||
def test_120_case_matrix_normalizes_identically(self) -> None:
|
||||
cases = build_emotion_eval_cases()
|
||||
self.assertEqual(60, len(cases))
|
||||
self.assertEqual(120, len(cases))
|
||||
self.assertEqual(emotion_eval_fingerprint(), emotion_eval_fingerprint())
|
||||
ids: set[str] = set()
|
||||
for case in cases:
|
||||
|
|
|
|||
|
|
@ -0,0 +1,43 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
import unittest
|
||||
|
||||
from core.emotion_targeting import memory_emotion_refs
|
||||
from core.models import EntityRef, MemoryRecord, SessionContext
|
||||
|
||||
|
||||
class EmotionE7ActorTargetMemoryTests(unittest.TestCase):
|
||||
def test_matching_subject_and_session_are_preserved(self) -> None:
|
||||
memory = MemoryRecord(
|
||||
id="m1",
|
||||
session_id="session-a",
|
||||
subject=EntityRef(kind="user", id="u1", role="speaker"),
|
||||
)
|
||||
ctx = SessionContext(session_id="session-a", scope="private", user_id="u1", bot_id="b1")
|
||||
refs = memory_emotion_refs(memory, ctx)
|
||||
self.assertEqual("u1", refs["actor_ref"]["id"])
|
||||
self.assertEqual("b1", refs["target_ref"]["id"])
|
||||
|
||||
def test_cross_session_or_different_user_is_rejected(self) -> None:
|
||||
ctx = SessionContext(session_id="session-a", scope="private", user_id="u1", bot_id="b1")
|
||||
for memory in (
|
||||
MemoryRecord(id="m1", session_id="session-b", subject=EntityRef(kind="user", id="u1")),
|
||||
MemoryRecord(id="m2", session_id="session-a", subject=EntityRef(kind="user", id="u2")),
|
||||
MemoryRecord(id="m3", session_id="session-a", subject=EntityRef(kind="other", id="nickname")),
|
||||
):
|
||||
with self.subTest(memory=memory.id):
|
||||
self.assertIsNone(memory_emotion_refs(memory, ctx))
|
||||
|
||||
def test_nickname_text_is_not_used_as_identity(self) -> None:
|
||||
memory = SimpleNamespace(
|
||||
session_id="session-a",
|
||||
content="小明说你是垃圾",
|
||||
subject=SimpleNamespace(kind="user", id="", role="speaker", name="小明"),
|
||||
)
|
||||
ctx = SessionContext(session_id="session-a", scope="private", user_id="u1", bot_id="b1")
|
||||
self.assertIsNone(memory_emotion_refs(memory, ctx))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
正在加载…
添加表格
在新工单中引用