From 7b19a1f716748ed710442a90c75c88e07120536d Mon Sep 17 00:00:00 2001 From: agent Date: Wed, 5 Aug 2026 00:27:02 +0800 Subject: [PATCH] feat(emotion-e5): consume recovery projections --- core/bridge.py | 18 +++++- core/models.py | 5 ++ core/service.py | 11 ++++ tests/test_emotion_e5_authority_projection.py | 60 +++++++++++++++++++ 4 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 tests/test_emotion_e5_authority_projection.py diff --git a/core/bridge.py b/core/bridge.py index 732f4d6..c73ccaf 100644 --- a/core/bridge.py +++ b/core/bridge.py @@ -167,13 +167,29 @@ def sanitize_companion_relationship_projection(value: Any) -> dict[str, Any]: if type(current_interaction) is dict: expression_band = current_interaction.get("expression_band") if type(expression_band) is str and expression_band in _COMPANION_INTERACTION_BANDS: - projection["current_interaction"] = { + interaction_projection = { "expression_band": expression_band, "label": clean_text(current_interaction.get("label"), 40), "source": clean_text(current_interaction.get("source"), 40), "reason": clean_text(current_interaction.get("reason"), 120), "manual_override": current_interaction.get("manual_override") is True, } + dynamics_version = current_interaction.get("dynamics_version") + recovery_band = current_interaction.get("recovery_band") + expires_at = current_interaction.get("expires_at") + projection_revision = current_interaction.get("projection_revision") + if dynamics_version == "interaction_dynamics.v1" and recovery_band in {"steady", "recovering", "reinforced"}: + if type(expires_at) not in {int, float} or type(expires_at) is bool: + return fallback + if type(projection_revision) is not int or not 1 <= projection_revision <= 1000000: + return fallback + interaction_projection.update({ + "dynamics_version": dynamics_version, + "recovery_band": recovery_band, + "expires_at": max(0.0, min(10**12, float(expires_at))), + "projection_revision": projection_revision, + }) + projection["current_interaction"] = interaction_projection return {"status": "accepted", "read_only": True, "projection": projection} diff --git a/core/models.py b/core/models.py index f74ef52..fe8eb62 100644 --- a/core/models.py +++ b/core/models.py @@ -84,6 +84,11 @@ class SessionContext: companion_relationship_phase_label: str = "" companion_relationship_tone: str = "" companion_relationship_address_level: str = "" + companion_interaction_dynamics_version: str = "" + companion_interaction_band: str = "" + companion_interaction_recovery_band: str = "" + companion_interaction_expires_at: float = 0.0 + companion_interaction_projection_revision: int = 0 companion_expression_contract: str = "" companion_expression_band: str = "" companion_expression_allowed_behaviors: tuple[str, ...] = () diff --git a/core/service.py b/core/service.py index 4ca9c84..8b8e47a 100644 --- a/core/service.py +++ b/core/service.py @@ -726,6 +726,17 @@ class MemoryCompanionService: ctx.companion_relationship_phase_label = clean_text(projection.get("phase_label"), 40) ctx.companion_relationship_tone = clean_text(projection.get("tone"), 160) ctx.companion_relationship_address_level = clean_text(projection.get("address_level"), 120) + interaction = projection.get("current_interaction") + if isinstance(interaction, dict): + ctx.companion_interaction_dynamics_version = clean_text(interaction.get("dynamics_version"), 48) + ctx.companion_interaction_band = clean_text(interaction.get("expression_band"), 24) + ctx.companion_interaction_recovery_band = clean_text(interaction.get("recovery_band"), 24) + try: + ctx.companion_interaction_expires_at = max(0.0, min(10**12, float(interaction.get("expires_at") or 0.0))) + ctx.companion_interaction_projection_revision = max(0, min(1000000, int(interaction.get("projection_revision") or 0))) + except (TypeError, ValueError): + ctx.companion_interaction_expires_at = 0.0 + ctx.companion_interaction_projection_revision = 0 @staticmethod def _apply_private_companion_preferred_address( diff --git a/tests/test_emotion_e5_authority_projection.py b/tests/test_emotion_e5_authority_projection.py new file mode 100644 index 0000000..49837d1 --- /dev/null +++ b/tests/test_emotion_e5_authority_projection.py @@ -0,0 +1,60 @@ +from __future__ import annotations + +from types import SimpleNamespace +import unittest + +from core.bridge import sanitize_companion_relationship_projection +from core.models import SessionContext +from core.service import MemoryCompanionService + + +def projection() -> dict: + return { + "schema_version": "chat.relationship_projection.v1", + "authority": "private_companion.relationship_score", + "read_only": True, + "score": 205, + "phase_key": "acquaintance", + "phase_label": "初识", + "tone": "友好", + "address_level": "中性", + "proactive_care_limit": 0, + "soft_behaviors": { + "allow_playful_jokes": False, + "allow_followup": True, + "allow_memory_mention": False, + "allow_daily_care": False, + }, + "current_interaction": { + "expression_band": "hurt", + "label": "受伤", + "source": "automatic", + "reason": "hurt_event", + "manual_override": False, + "dynamics_version": "interaction_dynamics.v1", + "recovery_band": "recovering", + "expires_at": 2000.0, + "projection_revision": 2, + }, + } + + +class EmotionE5AuthorityProjectionTests(unittest.TestCase): + def test_memory_consumes_bounded_dynamics_without_becoming_authority(self) -> None: + accepted = sanitize_companion_relationship_projection(projection()) + self.assertEqual("accepted", accepted["status"]) + ctx = SessionContext(scope="private") + event = SimpleNamespace(private_companion_context={"relationship_projection": projection()}) + MemoryCompanionService._apply_companion_relationship_projection(ctx, event=event) + self.assertEqual("private_companion.relationship_score", ctx.relationship_authority_source) + self.assertEqual("interaction_dynamics.v1", ctx.companion_interaction_dynamics_version) + self.assertEqual("recovering", ctx.companion_interaction_recovery_band) + + def test_invalid_dynamics_fail_closed(self) -> None: + hostile = projection() + hostile["current_interaction"]["expires_at"] = "NaN" + self.assertEqual("invalid", sanitize_companion_relationship_projection(hostile)["status"]) + + +if __name__ == "__main__": + unittest.main()