feat(emotion-e5): consume recovery projections

这个提交存在于:
agent 2026-08-05 00:27:02 +08:00
父节点 d43e704b70
当前提交 7b19a1f716
共有 4 个文件被更改,包括 93 次插入 和 1 次删除

查看文件

@ -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}

查看文件

@ -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, ...] = ()

查看文件

@ -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(

查看文件

@ -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()