Sleep decay compresses a batch of stable memories into one summary row and archives the originals. The summary was built without memory_domain, and MemoryRecord.memory_domain defaults to "unknown" -- a truthy value, so the `if not self.memory_domain:` classification branch in ensure_defaults has never once executed. The summary stored as unknown, the retrieval gate only admits user_memory and drops it before scoring, and the sources were already archived out of the candidate query. Summary unreachable plus originals unreachable meant the whole thread disappeared, silently, every night the job ran. Summaries now inherit the domain of their source records, and the decay bucket key includes the domain so a bucket cannot mix domains and leave the question undecidable. Before archiving anything, the summary is read back and checked through the same gate the retrieval side uses; if the source was reachable and the summary is not, the archive is skipped with a warning rather than turning live memory into dead rows. A maintenance.memory_decay_ dry_run switch runs the whole selection and summarisation without writing. ensure_defaults itself is left alone -- changing that default would reclassify every write that omits a domain. The two test_security_cache tests that have been excluded from the test command for five rounds turned out to have the same root cause: the fixture built a record without a domain, so search returned an empty set. Those tests are about ACL and cache invalidation, not domain classification; with an explicit domain the file passes 6/6 and no longer needs excluding. Memory suite 330 passed, was 323 passed / 2 failed. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
206 行
8.3 KiB
Python
206 行
8.3 KiB
Python
from __future__ import annotations
|
||
|
||
import json
|
||
import sys
|
||
import tempfile
|
||
import time
|
||
import types
|
||
import unittest
|
||
from pathlib import Path
|
||
from types import SimpleNamespace
|
||
|
||
|
||
ROOT = Path(__file__).resolve().parents[1]
|
||
if str(ROOT.parent) not in sys.path:
|
||
sys.path.insert(0, str(ROOT.parent))
|
||
|
||
if "quart" not in sys.modules:
|
||
quart_stub = types.ModuleType("quart")
|
||
quart_stub.jsonify = lambda payload=None, **kwargs: payload or kwargs
|
||
quart_stub.request = SimpleNamespace(args={}, method="GET")
|
||
|
||
async def _send_file(path):
|
||
return path
|
||
|
||
quart_stub.send_file = _send_file
|
||
sys.modules["quart"] = quart_stub
|
||
|
||
from core.models import EntityRef, MemoryRecord, SessionContext
|
||
from core.service import MemoryCompanionService
|
||
from page_api import PluginPageApi
|
||
|
||
|
||
class SecurityAndCacheTests(unittest.IsolatedAsyncioTestCase):
|
||
def make_service(self, config: dict | None = None) -> MemoryCompanionService:
|
||
temp_dir = tempfile.TemporaryDirectory()
|
||
self.addCleanup(temp_dir.cleanup)
|
||
service = MemoryCompanionService(
|
||
context=None,
|
||
config=config or {},
|
||
plugin_root=ROOT,
|
||
data_dir=Path(temp_dir.name),
|
||
)
|
||
self.addCleanup(service.close)
|
||
return service
|
||
|
||
@staticmethod
|
||
def group_memory(content: str = "唯一锚点蓝风铃") -> MemoryRecord:
|
||
return MemoryRecord(
|
||
memory_type="conversation_summary",
|
||
# 必须显式给域:MemoryRecord.memory_domain 的默认值 "unknown" 是真值,
|
||
# ensure_defaults 里 `if not self.memory_domain:` 那条分类分支因此永不执行,
|
||
# 不传域就落库成 unknown,被 RetrievalEngine._memory_domain_block_reason
|
||
# 判成 missing_user_subject 整体屏蔽,search 恒返回空集——本组两个用例
|
||
# 曾因此连续五轮被排除在测试口径外,而它们要测的是 ACL 与缓存失效,不是域分类。
|
||
memory_domain="user_memory",
|
||
subject=EntityRef(kind="user", id="group-user", name="群成员"),
|
||
object=EntityRef(kind="group", id="g1", name="测试群"),
|
||
scope="group",
|
||
session_id="qq:GroupMessage:g1",
|
||
platform="qq",
|
||
group_id="g1",
|
||
visibility="group_public",
|
||
lifecycle="stable_memory",
|
||
content=content,
|
||
importance=0.9,
|
||
)
|
||
|
||
async def test_acl_revoke_invalidates_cached_search(self) -> None:
|
||
service = self.make_service(
|
||
{
|
||
"retrieval": {"mode": "basic"},
|
||
"visibility": {
|
||
"enable_acl_rules": True,
|
||
"allow_group_public_in_private": False,
|
||
},
|
||
}
|
||
)
|
||
memory_id = await service.store.insert_memory(self.group_memory())
|
||
rule = await service.store.upsert_acl_rule(
|
||
owner_scope="group",
|
||
owner_id="g1",
|
||
reader_scope="private",
|
||
reader_id="u1",
|
||
effect="allow",
|
||
)
|
||
ctx = SessionContext(
|
||
session_id="qq:FriendMessage:u1",
|
||
scope="private",
|
||
platform="qq",
|
||
user_id="u1",
|
||
bot_id="b1",
|
||
)
|
||
|
||
first = await service.search("蓝风铃", ctx)
|
||
second = await service.search("蓝风铃", ctx)
|
||
self.assertIn(memory_id, {item.memory.id for item in first})
|
||
self.assertIn(memory_id, {item.memory.id for item in second})
|
||
self.assertGreaterEqual(service._retrieval_result_cache_stats["hits"], 1)
|
||
|
||
await service.store.delete_acl_rule(rule["id"])
|
||
revoked = await service.search("蓝风铃", ctx)
|
||
self.assertNotIn(memory_id, {item.memory.id for item in revoked})
|
||
|
||
async def test_visibility_config_change_cannot_reuse_old_cache(self) -> None:
|
||
config = {
|
||
"retrieval": {"mode": "basic"},
|
||
"visibility": {
|
||
"enable_acl_rules": False,
|
||
"allow_group_public_in_private": True,
|
||
},
|
||
}
|
||
service = self.make_service(config)
|
||
memory_id = await service.store.insert_memory(self.group_memory("配置切换锚点绿松石"))
|
||
ctx = SessionContext(
|
||
session_id="qq:FriendMessage:u1",
|
||
scope="private",
|
||
platform="qq",
|
||
user_id="u1",
|
||
bot_id="b1",
|
||
)
|
||
visible = await service.search("绿松石", ctx)
|
||
self.assertIn(memory_id, {item.memory.id for item in visible})
|
||
|
||
config["visibility"]["allow_group_public_in_private"] = False
|
||
hidden = await service.search("绿松石", ctx)
|
||
self.assertNotIn(memory_id, {item.memory.id for item in hidden})
|
||
|
||
async def test_access_tracking_does_not_invalidate_retrieval_revision(self) -> None:
|
||
service = self.make_service()
|
||
memory_id = await service.store.insert_memory(self.group_memory("版本锚点"))
|
||
before = await service.store.memory_revision()
|
||
await service.store.mark_accessed([memory_id])
|
||
self.assertEqual(before, await service.store.memory_revision())
|
||
|
||
async def test_service_clear_resets_persisted_and_runtime_state(self) -> None:
|
||
service = self.make_service()
|
||
await service.store.insert_memory(self.group_memory("清空状态"))
|
||
ctx = SessionContext(
|
||
session_id="qq:FriendMessage:u1",
|
||
scope="private",
|
||
platform="qq",
|
||
user_id="u1",
|
||
bot_id="b1",
|
||
)
|
||
service._get_relationship_phase(ctx)["phase"] = "close"
|
||
service._save_relationship_phase_state()
|
||
service._emotional_event_queue[ctx.session_id] = [{"ts": 1.0}]
|
||
service._retrieval_result_cache["cached"] = {"payload": {}}
|
||
|
||
result = await service.clear_all_memory_data()
|
||
self.assertIn("backup", result)
|
||
self.assertEqual({}, service._relationship_phase_state)
|
||
self.assertEqual({}, service._emotional_event_queue)
|
||
self.assertEqual({}, service._retrieval_result_cache)
|
||
self.assertEqual({}, json.loads(service._RELATIONSHIP_PHASE_FILE.read_text(encoding="utf-8")))
|
||
|
||
async def test_cross_window_emotional_reads_are_opt_in(self) -> None:
|
||
config: dict = {"private_companion_bridge": {}}
|
||
service = self.make_service(config)
|
||
service._emotional_event_queue["qq:FriendMessage:u1"] = [
|
||
{
|
||
"id": "emotion-1",
|
||
"session_id": "qq:FriendMessage:u1",
|
||
"event_type": "warm_memory",
|
||
"ts": time.time(),
|
||
}
|
||
]
|
||
self.assertEqual([], service.bridge_get_emotional_events(session_id=""))
|
||
self.assertEqual(1, len(service._emotional_event_queue["qq:FriendMessage:u1"]))
|
||
|
||
config["private_companion_bridge"]["cross_window_emotional_continuity_enabled"] = True
|
||
events = service.bridge_get_emotional_events(session_id="")
|
||
self.assertEqual(["emotion-1"], [item["id"] for item in events])
|
||
|
||
def test_photo_path_is_confined_and_local_path_is_not_exposed(self) -> None:
|
||
with tempfile.TemporaryDirectory() as temp:
|
||
base = Path(temp)
|
||
allowed = base / "companion-data"
|
||
own_data = base / "memory-data"
|
||
allowed.mkdir()
|
||
own_data.mkdir()
|
||
inside = allowed / "inside.jpg"
|
||
outside = base / "outside.jpg"
|
||
inside.write_bytes(b"jpeg-placeholder")
|
||
outside.write_bytes(b"jpeg-placeholder")
|
||
|
||
page_plugin = SimpleNamespace(service=SimpleNamespace(data_dir=own_data))
|
||
companion = SimpleNamespace(data_dir=allowed, data_file=allowed / "companions.json")
|
||
api = PluginPageApi(page_plugin)
|
||
|
||
self.assertEqual(inside.resolve(), api._safe_companion_photo_path(inside, companion))
|
||
self.assertEqual(inside.resolve(), api._safe_companion_photo_path("inside.jpg", companion))
|
||
self.assertIsNone(api._safe_companion_photo_path(outside, companion))
|
||
|
||
album = api._private_companion_album(
|
||
{"daily_outfit_photo": {"path": str(inside), "date": "2026-07-10"}},
|
||
"2026-07-10",
|
||
plugin=companion,
|
||
)
|
||
self.assertEqual(1, len(album))
|
||
self.assertNotIn("path", album[0])
|
||
self.assertNotIn("_local_path", album[0])
|
||
|
||
|
||
if __name__ == "__main__":
|
||
unittest.main()
|