diff --git a/core/bridge.py b/core/bridge.py index f2189a7..25ad22f 100644 --- a/core/bridge.py +++ b/core/bridge.py @@ -1096,6 +1096,17 @@ class MemoryCompanionBridge: """Retrieve pending emotional drift events for the companion plugin.""" return self._plugin.bridge_get_emotional_events(session_id=session_id, limit=limit) + async def record_emotion_event(self, event: dict[str, Any]) -> dict[str, Any]: + """Persist one redacted event revision outside normal memory retrieval.""" + return await self._plugin.store.upsert_emotion_event(event) + + async def revise_emotion_event(self, event: dict[str, Any]) -> dict[str, Any]: + """Persist a later revision of an existing event.""" + return await self._plugin.store.upsert_emotion_event(event) + + async def get_emotion_trace(self, trace_id: str, *, limit: int = 100) -> list[dict[str, Any]]: + return await self._plugin.store.get_emotion_trace(trace_id, limit=limit) + async def search_open_loops(self, *, session_id: str = "", limit: int = 3) -> list[dict[str, Any]]: """Search for unresolved open-loop / promise memories for proactive companionship.""" return await self._plugin.bridge_search_open_loops(session_id=session_id, limit=limit) diff --git a/core/emotion_event_contract.py b/core/emotion_event_contract.py new file mode 100644 index 0000000..6bae012 --- /dev/null +++ b/core/emotion_event_contract.py @@ -0,0 +1,162 @@ +"""Versioned, redacted emotion-event contract shared with Companion.""" +from __future__ import annotations + +from datetime import datetime, timezone +import hashlib +import json +import math +import re +from typing import Any, Mapping + + +EMOTION_EVENT_SCHEMA_VERSION = "companion_emotion_event.v1" +EMOTION_EVENT_TYPES = frozenset({ + "neutral", "hurt", "apology", "comfort", "praise", "comfort_need", + "external_negative", "scar_touched", "warm_memory", "vulnerable_resonance", + "play", "intimacy", "boundary", +}) +EMOTION_EVENT_ORIGINS = frozenset({"interaction", "memory_recall", "system_condition"}) +EMOTION_EVENT_STATUSES = frozenset({"observed", "revised", "applied", "ignored", "expired"}) +EMOTION_EVENT_CONTRACT_FIELDS = ( + "schema_version", "event_id", "trace_id", "revision", "producer_plugin", + "origin_kind", "platform", "bot_id", "scope", "session_id", "actor_ref", + "target_ref", "quoted_target_ref", "event_type", "intensity", "confidence", + "valence_hint", "arousal_hint", "vulnerability_hint", "source_rule", + "occurred_at", "expires_at", "dedupe_key", "payload_hash", "privacy_level", + "applied_interaction", "applied_energy_delta", "correction_of", "status", + "reason_codes", +) +EMOTION_EVENT_CONTRACT_FINGERPRINT = hashlib.sha256( + "|".join(EMOTION_EVENT_CONTRACT_FIELDS).encode("ascii") +).hexdigest()[:20] + + +def _text(value: Any, limit: int) -> str: + text = re.sub(r"\s+", " ", str(value or "").replace("\u3000", " ")).strip() + return text[:limit] + + +def _number(value: Any, default: float, low: float, high: float) -> float: + if isinstance(value, bool): + return default + try: + number = float(value) + except (TypeError, ValueError): + return default + if not math.isfinite(number): + return default + return round(max(low, min(high, number)), 4) + + +def _integer(value: Any, default: int = 1) -> int: + if isinstance(value, bool): + return default + try: + return max(1, min(1_000_000, int(value))) + except (TypeError, ValueError): + return default + + +def _entity(value: Any) -> dict[str, str]: + source = value if isinstance(value, Mapping) else {} + return { + "kind": _text(source.get("kind"), 24) or "unknown", + "id": _text(source.get("id"), 160), + "role": _text(source.get("role"), 40), + } + + +def _timestamp(value: Any) -> str: + text = _text(value, 48) + if text: + return text + return datetime.now(timezone.utc).isoformat(timespec="seconds") + + +def _stable_hash(*parts: Any) -> str: + raw = "|".join(_text(part, 500) for part in parts) + return hashlib.sha256(raw.encode("utf-8", errors="ignore")).hexdigest() + + +def normalize_emotion_event(value: Any, *, producer_plugin: str = "") -> dict[str, Any]: + """Return a bounded JSON-safe event without retaining raw message content.""" + source = dict(value) if isinstance(value, Mapping) else {} + producer = _text(source.get("producer_plugin") or producer_plugin, 80) or "unknown" + origin = _text(source.get("origin_kind"), 40) + if origin not in EMOTION_EVENT_ORIGINS: + origin = "interaction" + event_type = _text(source.get("event_type"), 48).lower() + if event_type not in EMOTION_EVENT_TYPES: + event_type = "neutral" + status = _text(source.get("status"), 24).lower() + if status not in EMOTION_EVENT_STATUSES: + status = "observed" + session_id = _text(source.get("session_id"), 220) + dedupe_key = _text(source.get("dedupe_key"), 160) + payload_hash = _text(source.get("payload_hash"), 80) + if not payload_hash: + payload_hash = _stable_hash( + producer, origin, session_id, event_type, dedupe_key, + source.get("message_fingerprint"), source.get("source_rule"), + ) + event_id = _text(source.get("event_id"), 96) + if not event_id: + event_id = "emo_" + _stable_hash(producer, origin, session_id, event_type, dedupe_key, payload_hash)[:24] + trace_id = _text(source.get("trace_id"), 96) + if not trace_id: + trace_id = "etr_" + _stable_hash(session_id, dedupe_key or event_id)[:24] + reason_codes = source.get("reason_codes") + if not isinstance(reason_codes, (list, tuple)): + reason_codes = [] + clean_reasons: list[str] = [] + for item in reason_codes[:12]: + reason = _text(item, 64) + if reason and reason not in clean_reasons: + clean_reasons.append(reason) + return { + "schema_version": EMOTION_EVENT_SCHEMA_VERSION, + "event_id": event_id, + "trace_id": trace_id, + "revision": _integer(source.get("revision"), 1), + "producer_plugin": producer, + "origin_kind": origin, + "platform": _text(source.get("platform"), 80), + "bot_id": _text(source.get("bot_id"), 160), + "scope": _text(source.get("scope"), 24) or "private", + "session_id": session_id, + "actor_ref": _entity(source.get("actor_ref")), + "target_ref": _entity(source.get("target_ref")), + "quoted_target_ref": _entity(source.get("quoted_target_ref")), + "event_type": event_type, + "intensity": _number(source.get("intensity"), 0.0, 0.0, 100.0), + "confidence": _number(source.get("confidence"), 0.0, 0.0, 1.0), + "valence_hint": _number(source.get("valence_hint"), 0.0, -1.0, 1.0), + "arousal_hint": _number(source.get("arousal_hint"), 0.0, 0.0, 1.0), + "vulnerability_hint": _number(source.get("vulnerability_hint"), 0.0, 0.0, 1.0), + "source_rule": _text(source.get("source_rule"), 80), + "occurred_at": _timestamp(source.get("occurred_at")), + "expires_at": _text(source.get("expires_at"), 48), + "dedupe_key": dedupe_key or payload_hash[:32], + "payload_hash": payload_hash, + "privacy_level": _text(source.get("privacy_level"), 24) or "redacted", + "applied_interaction": _text(source.get("applied_interaction"), 32), + "applied_energy_delta": _number(source.get("applied_energy_delta"), 0.0, -100.0, 100.0), + "correction_of": _text(source.get("correction_of"), 96), + "status": status, + "reason_codes": clean_reasons, + } + + +def emotion_event_json(value: Any, *, producer_plugin: str = "") -> str: + return json.dumps(normalize_emotion_event(value, producer_plugin=producer_plugin), ensure_ascii=False, separators=(",", ":")) + + +__all__ = [ + "EMOTION_EVENT_CONTRACT_FIELDS", + "EMOTION_EVENT_CONTRACT_FINGERPRINT", + "EMOTION_EVENT_SCHEMA_VERSION", + "EMOTION_EVENT_STATUSES", + "EMOTION_EVENT_TYPES", + "emotion_event_json", + "normalize_emotion_event", +] diff --git a/core/service.py b/core/service.py index b46dfcb..cd6edf4 100644 --- a/core/service.py +++ b/core/service.py @@ -39,6 +39,7 @@ from .chat_import import HistoricalChatImporter from .classifier import MemoryClassifier from .config import ConfigView from .context_orchestrator import RetrievalIntent, RetrievalIntentBuilder +from .emotion_event_contract import normalize_emotion_event from .identity import IdentityResolver, looks_like_command, maybe_await, normalize_session_context_fields, session_target_id from .importance import ImportanceEvaluator from .injection import ( @@ -1673,7 +1674,7 @@ class MemoryCompanionService: break return open_loops - def _detect_and_queue_emotional_events( + async def _detect_and_queue_emotional_events( self, ctx: SessionContext, results: list[Any], @@ -1727,23 +1728,54 @@ class MemoryCompanionService: event_id = hashlib.sha1( f"{ctx.session_id}|{event_key}|{memory.id}|{event_type}".encode("utf-8", errors="ignore") ).hexdigest()[:20] - events.append({ - "id": f"emo_{event_id}", - "ts": now, + event = normalize_emotion_event({ + "event_id": f"emo_{event_id}", + "trace_id": f"etr_{event_id}", + "producer_plugin": "memory_companion", + "origin_kind": "memory_recall", + "platform": ctx.platform, + "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"}, "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))), + "valence_hint": 0.35 if event_type == "warm_memory" else -0.45 if event_type == "scar_touched" else -0.1, + "arousal_hint": min(1.0, max(scar_w, emotional_w)), + "vulnerability_hint": max(vulnerability_w, 0.6 if event_type == "vulnerable_resonance" else 0.0), + "source_rule": event_type, + "occurred_at": datetime.fromtimestamp(now, tz=timezone.utc).isoformat(timespec="seconds"), + "dedupe_key": f"{ctx.session_id}|{event_key}|{memory.id}|{event_type}", + "payload_hash": hashlib.sha256(memory.id.encode("utf-8", errors="ignore")).hexdigest(), + "privacy_level": "redacted", + "applied_energy_delta": round(energy_delta, 2), + "status": "observed", + "reason_codes": [event_type, "memory_recall_resonance"], + }, producer_plugin="memory_companion") + event.update({ + "id": event["event_id"], + "ts": now, "memory_id": memory.id, "energy_delta": round(energy_delta, 2), "mood_hint": mood_hint, "scar_weight": round(scar_w, 3), "emotional_weight": round(emotional_w, 3), - "content_preview": clean_text(memory.content, 120), }) + events.append(event) if not events: return queue = self._emotional_event_queue.setdefault(ctx.session_id, []) existing_ids = {clean_text(item.get("id"), 80) for item in queue if isinstance(item, dict)} - queue.extend(event for event in events if clean_text(event.get("id"), 80) not in existing_ids) + fresh_events = [event for event in events if clean_text(event.get("id"), 80) not in existing_ids] + queue.extend(fresh_events) + for event in fresh_events: + await self.store.upsert_emotion_event(event) # Trim old events queue[:] = [e for e in queue if (now - e.get("ts", 0)) < self._EMOTIONAL_EVENT_TTL] if len(queue) > self._EMOTIONAL_EVENT_MAX_PER_SESSION: @@ -4893,7 +4925,7 @@ class MemoryCompanionService: slot_map, recent_fact_context=(recent_fact_context if "" in injection else ""), ) - self._detect_and_queue_emotional_events( + await self._detect_and_queue_emotional_events( ctx, results, companion_bot_mood=merged_bot_mood, companion_bot_energy=merged_bot_energy, @@ -5195,7 +5227,7 @@ class MemoryCompanionService: slot_map, recent_fact_context=(recent_fact_context if "" in injection else ""), ) - self._detect_and_queue_emotional_events( + await self._detect_and_queue_emotional_events( ctx, results, companion_bot_mood=_bot_mood, companion_bot_energy=_bot_energy, diff --git a/core/store.py b/core/store.py index 47911f3..d12cced 100644 --- a/core/store.py +++ b/core/store.py @@ -487,6 +487,40 @@ class MemoryStore: updated_at TEXT NOT NULL DEFAULT '', PRIMARY KEY(memory_id, provider_id) ); + + CREATE TABLE IF NOT EXISTS emotion_events ( + event_id TEXT NOT NULL, + revision INTEGER NOT NULL DEFAULT 1, + trace_id TEXT NOT NULL DEFAULT '', + producer_plugin TEXT NOT NULL DEFAULT '', + origin_kind TEXT NOT NULL DEFAULT '', + platform TEXT NOT NULL DEFAULT '', + bot_id TEXT NOT NULL DEFAULT '', + scope TEXT NOT NULL DEFAULT '', + session_id TEXT NOT NULL DEFAULT '', + actor_ref TEXT NOT NULL DEFAULT '{}', + target_ref TEXT NOT NULL DEFAULT '{}', + quoted_target_ref TEXT NOT NULL DEFAULT '{}', + event_type TEXT NOT NULL DEFAULT 'neutral', + intensity REAL NOT NULL DEFAULT 0, + confidence REAL NOT NULL DEFAULT 0, + valence_hint REAL NOT NULL DEFAULT 0, + arousal_hint REAL NOT NULL DEFAULT 0, + vulnerability_hint REAL NOT NULL DEFAULT 0, + source_rule TEXT NOT NULL DEFAULT '', + occurred_at TEXT NOT NULL DEFAULT '', + expires_at TEXT NOT NULL DEFAULT '', + dedupe_key TEXT NOT NULL DEFAULT '', + payload_hash TEXT NOT NULL DEFAULT '', + privacy_level TEXT NOT NULL DEFAULT 'redacted', + applied_interaction TEXT NOT NULL DEFAULT '', + applied_energy_delta REAL NOT NULL DEFAULT 0, + correction_of TEXT NOT NULL DEFAULT '', + status TEXT NOT NULL DEFAULT 'observed', + reason_codes TEXT NOT NULL DEFAULT '[]', + created_at TEXT NOT NULL DEFAULT '', + PRIMARY KEY(event_id, revision) + ); """ ) self._ensure_memory_columns_sync() @@ -539,6 +573,15 @@ class MemoryStore: self._conn.execute( "CREATE INDEX IF NOT EXISTS idx_memory_embeddings_provider ON memory_embeddings(provider_id, updated_at)" ) + self._conn.execute( + "CREATE INDEX IF NOT EXISTS idx_emotion_events_trace ON emotion_events(trace_id, revision)" + ) + self._conn.execute( + "CREATE INDEX IF NOT EXISTS idx_emotion_events_session ON emotion_events(bot_id, scope, session_id, occurred_at DESC)" + ) + self._conn.execute( + "CREATE INDEX IF NOT EXISTS idx_emotion_events_dedupe ON emotion_events(dedupe_key, event_type)" + ) self._conn.commit() def _ensure_retrieval_revision_sync(self) -> None: @@ -5528,3 +5571,90 @@ class MemoryStore: ) self._conn.commit() return row_id + + async def upsert_emotion_event(self, value: Any) -> dict[str, Any]: + return await self._run_recoverable_database_operation(self._upsert_emotion_event_sync, value) + + def _upsert_emotion_event_sync(self, value: Any) -> dict[str, Any]: + from .emotion_event_contract import normalize_emotion_event + + event = normalize_emotion_event(value, producer_plugin="memory_companion") + with self._lock: + self._conn.execute( + """ + INSERT OR IGNORE INTO emotion_events( + event_id, revision, trace_id, producer_plugin, origin_kind, platform, + bot_id, scope, session_id, actor_ref, target_ref, quoted_target_ref, + event_type, intensity, confidence, valence_hint, arousal_hint, + vulnerability_hint, source_rule, occurred_at, expires_at, dedupe_key, + payload_hash, privacy_level, applied_interaction, applied_energy_delta, + correction_of, status, reason_codes, created_at + ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) + """, + ( + event["event_id"], event["revision"], event["trace_id"], event["producer_plugin"], + event["origin_kind"], event["platform"], event["bot_id"], event["scope"], + event["session_id"], json_dumps(event["actor_ref"]), json_dumps(event["target_ref"]), + json_dumps(event["quoted_target_ref"]), event["event_type"], event["intensity"], + event["confidence"], event["valence_hint"], event["arousal_hint"], + event["vulnerability_hint"], event["source_rule"], event["occurred_at"], + event["expires_at"], event["dedupe_key"], event["payload_hash"], + event["privacy_level"], event["applied_interaction"], event["applied_energy_delta"], + event["correction_of"], event["status"], json_dumps(event["reason_codes"]), utc_now(), + ), + ) + self._conn.commit() + return event + + async def get_emotion_trace(self, trace_id: str, *, limit: int = 100) -> list[dict[str, Any]]: + return await self._run_recoverable_database_operation(self._get_emotion_trace_sync, trace_id, limit) + + def _get_emotion_trace_sync(self, trace_id: str, limit: int) -> list[dict[str, Any]]: + trace = clean_text(trace_id, 96) + if not trace: + return [] + with self._lock: + rows = self._conn.execute( + "SELECT * FROM emotion_events WHERE trace_id=? ORDER BY revision ASC LIMIT ?", + (trace, max(1, min(500, int(limit or 100)))), + ).fetchall() + return [self._emotion_event_row(row) for row in rows] + + async def list_emotion_events( + self, + *, + bot_id: str = "", + session_id: str = "", + limit: int = 50, + ) -> list[dict[str, Any]]: + return await self._run_recoverable_database_operation( + self._list_emotion_events_sync, bot_id, session_id, limit + ) + + def _list_emotion_events_sync(self, bot_id: str, session_id: str, limit: int) -> list[dict[str, Any]]: + clauses: list[str] = [] + params: list[Any] = [] + if clean_text(bot_id, 160): + clauses.append("bot_id=?") + params.append(clean_text(bot_id, 160)) + if clean_text(session_id, 220): + clauses.append("session_id=?") + params.append(clean_text(session_id, 220)) + where = " WHERE " + " AND ".join(clauses) if clauses else "" + params.append(max(1, min(500, int(limit or 50)))) + with self._lock: + rows = self._conn.execute( + f"SELECT * FROM emotion_events{where} ORDER BY occurred_at DESC, revision DESC LIMIT ?", + params, + ).fetchall() + return [self._emotion_event_row(row) for row in rows] + + @staticmethod + def _emotion_event_row(row: sqlite3.Row) -> dict[str, Any]: + result = dict(row) + for key in ("actor_ref", "target_ref", "quoted_target_ref"): + result[key] = json_loads(result.get(key), {}) + result["reason_codes"] = json_loads(result.get("reason_codes"), []) + result.pop("created_at", None) + result["schema_version"] = "companion_emotion_event.v1" + return result diff --git a/tests/test_emotion_e2_event_store.py b/tests/test_emotion_e2_event_store.py new file mode 100644 index 0000000..2813cb2 --- /dev/null +++ b/tests/test_emotion_e2_event_store.py @@ -0,0 +1,68 @@ +from __future__ import annotations + +import tempfile +from pathlib import Path +import sys +import unittest + + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT)) + +from core.bridge import MemoryCompanionBridge # noqa: E402 +from core.emotion_event_contract import ( # noqa: E402 + EMOTION_EVENT_CONTRACT_FINGERPRINT, + normalize_emotion_event, +) +from core.store import MemoryStore # noqa: E402 + + +class EmotionE2EventStoreTests(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_event_revisions_are_independent_from_memory_retrieval(self) -> None: + store = self.make_store() + first = normalize_emotion_event( + { + "event_type": "hurt", + "session_id": "qq:FriendMessage:u1", + "dedupe_key": "m1", + "trace_id": "trace-1", + "revision": 1, + }, + producer_plugin="private_companion", + ) + second = {**first, "revision": 2, "status": "revised", "event_type": "neutral", "correction_of": first["event_id"]} + await store.upsert_emotion_event(first) + await store.upsert_emotion_event(first) + await store.upsert_emotion_event(second) + trace = await store.get_emotion_trace("trace-1") + self.assertEqual([1, 2], [item["revision"] for item in trace]) + self.assertEqual(0, store._conn.execute("SELECT COUNT(*) FROM memories").fetchone()[0]) + self.assertEqual(2, store._conn.execute("SELECT COUNT(*) FROM emotion_events").fetchone()[0]) + + async def test_bridge_records_and_reads_redacted_trace(self) -> None: + store = self.make_store() + plugin = type("Plugin", (), {"store": store})() + bridge = MemoryCompanionBridge(plugin) + event = await bridge.record_emotion_event({ + "event_type": "comfort", + "session_id": "qq:FriendMessage:u1", + "dedupe_key": "m2", + "trace_id": "trace-2", + "raw_text": "PRIVATE", + }) + trace = await bridge.get_emotion_trace("trace-2") + self.assertEqual(event["event_id"], trace[0]["event_id"]) + self.assertNotIn("PRIVATE", repr(trace)) + self.assertTrue(EMOTION_EVENT_CONTRACT_FINGERPRINT) + + +if __name__ == "__main__": + unittest.main()