feat: enforce Q5 user memory boundaries

这个提交存在于:
YokiiroBW 2026-07-25 23:29:48 +08:00
父节点 5016af47c6
当前提交 66b5d0bf70
共有 17 个文件被更改,包括 704 次插入 和 339 次删除

查看文件

@ -42,7 +42,18 @@ class MemoryCompanionBridge:
return {"compatibility_level": "unknown", "capabilities": {}}
getter = getattr(capabilities, "to_dict", None)
result = getter() if callable(getter) else capabilities
return result if isinstance(result, dict) else {"compatibility_level": "unknown", "capabilities": {}}
if not isinstance(result, dict):
result = {"compatibility_level": "unknown", "capabilities": {}}
result.update({
"contract_version": "5.0",
"memory_domain": "user_memory",
"accepted_write_types": ["user_fact", "user_event", "user_decision", "user_summary"],
"accepts_bot_self_memory": False,
"accepts_agenda_snapshot": False,
"accepts_calendar_history": False,
"time_aware": True,
})
return result
def probe_ops_contract(self) -> dict[str, Any]:
return OpsDomainContract.to_dict()
@ -65,11 +76,28 @@ class MemoryCompanionBridge:
def normalize_ops_audit(self, value: Any) -> dict[str, Any]:
return normalize_ops_audit(value)
async def record_user_fact(self, *, content: str, user_id: str, evidence: str, **kwargs: Any) -> str:
kwargs.setdefault("memory_type", "user_fact")
return await self._plugin.record_user_memory(content=content, user_id=user_id, evidence=evidence, **kwargs)
async def record_user_event(self, *, content: str, user_id: str, evidence: str, **kwargs: Any) -> str:
kwargs.setdefault("memory_type", "user_event")
return await self._plugin.record_user_memory(content=content, user_id=user_id, evidence=evidence, **kwargs)
async def record_user_decision(self, *, content: str, user_id: str, evidence: str, **kwargs: Any) -> str:
kwargs.setdefault("memory_type", "user_decision")
return await self._plugin.record_user_memory(content=content, user_id=user_id, evidence=evidence, **kwargs)
async def record_user_summary(self, *, content: str, user_id: str, evidence: str, **kwargs: Any) -> str:
kwargs.setdefault("memory_type", "user_summary")
return await self._plugin.record_user_memory(content=content, user_id=user_id, evidence=evidence, **kwargs)
async def record_event(
self,
*,
content: str,
memory_type: str = "external_event",
memory_domain: str = "unknown",
scope: str = "unknown",
session_id: str = "",
platform: str = "",
@ -90,29 +118,32 @@ class MemoryCompanionBridge:
memory_id: str = "",
occurred_at: str = "",
) -> str:
return await self._plugin.record_external_event(
content=content,
memory_type=memory_type,
scope=scope,
session_id=session_id,
platform=platform,
message_id=message_id,
group_id=group_id,
subject=self._entity(subject) if subject else EntityRef.bot_self(),
object=self._entity(object) if object else EntityRef(kind="session", id=session_id, role="target_session"),
visibility=visibility,
sayability=sayability,
reality_level=reality_level,
lifecycle=lifecycle,
confidence=confidence,
importance=importance,
review_status=review_status,
tags=tags or [],
metadata=metadata or {},
source_plugin=source_plugin,
memory_id=memory_id,
occurred_at=occurred_at,
)
if memory_domain == "shared_or_mixed":
return await self._plugin.record_external_event(
content=content,
memory_type=memory_type,
memory_domain=memory_domain,
scope=scope,
session_id=session_id,
platform=platform,
message_id=message_id,
group_id=group_id,
subject=self._entity(subject) if subject else EntityRef.bot_self(),
object=self._entity(object) if object else EntityRef(kind="session", id=session_id, role="target_session"),
visibility=visibility,
sayability=sayability,
reality_level=reality_level,
lifecycle=lifecycle,
confidence=confidence,
importance=importance,
review_status=review_status,
tags=tags or [],
metadata=metadata or {},
source_plugin=source_plugin,
memory_id=memory_id,
occurred_at=occurred_at,
)
raise RuntimeError("generic_memory_event_rejected:user_memory_contract;use_record_user_*")
async def record_calendar_event(
self,
@ -132,47 +163,16 @@ class MemoryCompanionBridge:
metadata: dict[str, Any] | None = None,
source_plugin: str = "astrbot_plugin_private_companion",
) -> str:
recorder = getattr(self._plugin, "record_calendar_event", None)
if not callable(recorder):
raise RuntimeError("\u8bb0\u5fc6\u63d2\u4ef6\u4e0d\u652f\u6301\u65e5\u5386\u5386\u53f2 Bridge")
return await recorder(
event_id=event_id,
calendar=calendar,
title=title,
summary=summary,
start_at=start_at,
end_at=end_at,
timezone=timezone,
status=status,
source=source,
related_task_id=related_task_id,
occurred_at=occurred_at,
change_reason=change_reason,
metadata=metadata or {},
source_plugin=source_plugin,
)
raise RuntimeError("calendar_history_rejected:user_memory_contract")
async def record_bot_action(self, *, content: str, **kwargs: Any) -> str:
kwargs.setdefault("memory_type", "self_action")
kwargs.setdefault("visibility", "bot_self")
kwargs.setdefault("reality_level", "bot_action")
kwargs.setdefault("source_plugin", kwargs.get("source_plugin", "external"))
return await self.record_event(content=content, **kwargs)
raise RuntimeError("bot_self_memory_rejected:user_memory_contract")
async def record_persona_life(self, *, content: str, **kwargs: Any) -> str:
kwargs.setdefault("memory_type", "persona_life")
kwargs.setdefault("visibility", "bot_self")
kwargs.setdefault("reality_level", "persona_life")
kwargs.setdefault("sayability", "indirect")
return await self.record_event(content=content, **kwargs)
raise RuntimeError("bot_self_memory_rejected:user_memory_contract")
async def record_proactive_message(self, *, content: str, **kwargs: Any) -> str:
kwargs.setdefault("memory_type", "proactive_message")
kwargs.setdefault("visibility", "bot_self")
kwargs.setdefault("reality_level", "bot_action")
kwargs.setdefault("tags", ["proactive", "bot_action"])
kwargs.setdefault("importance", 0.55)
return await self.record_event(content=content, **kwargs)
raise RuntimeError("bot_self_memory_rejected:user_memory_contract")
async def record_visible_turn(self, *, role: str, content: str, **kwargs: Any) -> str:
"""Record a real visible chat turn into the short-term timeline only."""
@ -217,6 +217,7 @@ class MemoryCompanionBridge:
return await self.record_event(
content=content,
memory_type=memory_type,
memory_domain="shared_or_mixed",
scope=scope,
session_id=session_id,
platform=platform,
@ -232,9 +233,9 @@ class MemoryCompanionBridge:
"name": target.name,
"role": target.role,
},
visibility="bot_self",
sayability="direct",
reality_level="bot_action",
visibility="internal",
sayability="indirect",
reality_level="shared_experience",
lifecycle="stable_memory",
confidence=confidence,
importance=importance,
@ -287,13 +288,7 @@ class MemoryCompanionBridge:
return await self.record_event(content=content, **kwargs)
async def record_schedule_fragment(self, *, content: str, **kwargs: Any) -> str:
kwargs.setdefault("memory_type", "schedule_fragment")
kwargs.setdefault("visibility", "bot_self")
kwargs.setdefault("reality_level", "persona_life")
kwargs.setdefault("sayability", "indirect")
kwargs.setdefault("tags", ["schedule", "persona_life"])
kwargs.setdefault("importance", 0.45)
return await self.record_event(content=content, **kwargs)
raise RuntimeError("bot_self_memory_rejected:user_memory_contract")
async def search(
self,

查看文件

@ -21,6 +21,7 @@ class MemoryClassifier:
importance = self._importance_for_text(text)
return MemoryRecord(
memory_type="conversation_event",
memory_domain="user_memory",
subject=entity_for_user(ctx),
object=entity_for_current_target(ctx),
scope=ctx.scope,
@ -50,6 +51,7 @@ class MemoryClassifier:
content = self._format_bot_content(ctx, text)
return MemoryRecord(
memory_type="self_action",
memory_domain="bot_self_legacy",
subject=EntityRef.bot_self(bot_id=ctx.bot_id),
object=target,
scope=ctx.scope,
@ -93,6 +95,7 @@ class MemoryClassifier:
"importance": 0.68,
"review_status": "auto",
"source_plugin": "memory_companion",
"memory_domain": "user_memory",
}
fact = self._extract_preference_or_profile(ctx, text)
@ -115,6 +118,7 @@ class MemoryClassifier:
*,
content: str,
memory_type: str = "external_event",
memory_domain: str = "unknown",
subject: EntityRef | None = None,
object: EntityRef | None = None,
scope: str = "unknown",
@ -136,6 +140,7 @@ class MemoryClassifier:
) -> MemoryRecord:
return MemoryRecord(
memory_type=memory_type,
memory_domain=memory_domain,
subject=subject or EntityRef.bot_self(),
object=object or EntityRef(kind="session", id=session_id, role="target_session"),
scope=scope,

查看文件

@ -409,65 +409,7 @@ class MemoryCompanionCommandHandler:
return "用法:/mcomp sleep status|run"
async def import_livingmemory(self, mode: str = "preview", path: str = "") -> str:
configured = path or str(self.service.config.get("livingmemory_migration.livingmemory_db_path", "") or "")
raw_mode = mode or "preview"
mode = raw_mode.lower()
known_modes = {
"preview",
"dry",
"check",
"detail",
"details",
"scan",
"run",
"import",
"exec",
"start",
"help",
"?",
"usage",
}
if not path and mode not in known_modes:
if raw_mode.endswith(".db") or "\\" in raw_mode or "/" in raw_mode:
configured = raw_mode
mode = "preview"
if mode in {"help", "?", "usage"}:
return self._livingmemory_usage()
if mode in {"preview", "dry", "check"}:
return self._format_livingmemory_preview(self.service.migrator.preview(configured))
if mode in {"detail", "details", "scan"}:
return self._format_livingmemory_preview(self.service.migrator.preview(configured), verbose=True)
if mode in {"run", "import", "exec", "start"}:
result = await self.service.import_livingmemory(configured_path=configured)
return self._format_livingmemory_import_result(result)
return self._livingmemory_usage()
def help(self) -> str:
return (
"我会牢牢记住你命令:\n"
"主入口 /mcomp。\n"
"/mcomp status\n"
"/mcomp search 关键词\n"
"/mcomp explain 关键词\n"
"/mcomp recent 10\n"
"/mcomp add 要记住的内容\n"
"/mcomp summarize\n"
"/mcomp visibility <memory_id> <visibility>\n"
"/mcomp promote <memory_id>\n"
"/mcomp archive <memory_id>\n"
"/mcomp timeline 10\n"
"/mcomp relations [数量] [用户或群ID]\n"
"/mcomp threads list|close <thread_id>\n"
"/mcomp logs 5\n"
"/mcomp maintenance\n"
"/mcomp diagnostics\n"
"/mcomp preset status|apply light|standard|companion\n"
"/mcomp data export|preview|import [jsonl_path]\n"
"/mcomp sleep status|run\n"
"/mcomp delete <memory_id>\n"
"/mcomp import_livingmemory preview|run|detail [db_path]\n"
"/mcomp ops_migration preview|run [旧数据目录]"
)
return "LivingMemory 已按 Q5 用户记忆硬边界停用;请使用用户记忆合同或迁移预览接口。"
def _format_livingmemory_preview(self, report: dict[str, Any], *, verbose: bool = False) -> str:
if not report["candidates"]:

查看文件

@ -97,6 +97,7 @@ class SessionContext:
class MemoryRecord:
id: str = ""
memory_type: str = "observation"
memory_domain: str = "unknown"
subject: EntityRef = field(default_factory=EntityRef)
object: EntityRef = field(default_factory=EntityRef)
scope: str = "unknown"
@ -139,6 +140,18 @@ class MemoryRecord:
self.occurred_at = self.created_at
if not isinstance(self.metadata, dict):
self.metadata = {}
if not self.memory_domain:
memory_type = clean_text(self.memory_type, 80).lower()
reality = clean_text(self.reality_level, 80).lower()
if memory_type == "calendar_event":
self.memory_domain = "calendar_history"
elif self.visibility == "bot_self" or reality in {"bot_action", "persona_life", "llm_tool_assertion"} or memory_type in {"self_action", "persona_life", "proactive_message", "schedule_fragment", "companion_note", "calendar_event", "agenda_snapshot", "timeline_event"}:
self.memory_domain = "bot_self_legacy"
elif getattr(self.subject, "kind", "") == "user" or memory_type in {"user_fact", "user_event", "user_decision", "user_summary", "user_profile", "user_preference", "user_habit", "relationship_claim", "explicit_memory", "manual_memory", "conversation_event", "conversation_summary", "important_event", "daily_digest", "tool_memory"}:
self.memory_domain = "user_memory"
else:
self.memory_domain = "unknown"
self.metadata.setdefault("memory_domain", self.memory_domain)
self.metadata.setdefault("ingested_at", self.created_at)
self.metadata.setdefault("timezone", "Asia/Shanghai")
self.metadata.setdefault("occurred_at_estimated", occurred_was_missing)
@ -171,6 +184,7 @@ class MemoryRecord:
return {
"id": self.id,
"memory_type": self.memory_type,
"memory_domain": self.memory_domain,
"subject_kind": self.subject.kind,
"subject_id": self.subject.id,
"subject_name": self.subject.name,
@ -213,6 +227,7 @@ class MemoryRecord:
return cls(
id=get("id"),
memory_type=get("memory_type"),
memory_domain=get("memory_domain"),
subject=EntityRef(get("subject_kind"), get("subject_id"), get("subject_name"), get("subject_role")),
object=EntityRef(get("object_kind"), get("object_id"), get("object_name"), get("object_role")),
scope=get("scope"),

查看文件

@ -108,6 +108,9 @@ class RetrievalEngine:
memory = current.get(memory_id)
if memory is None:
continue
domain_reason = self._memory_domain_block_reason(memory)
if domain_reason:
continue
visible_reason, blocked_reason = self._search_visibility_reason(memory, ctx, acl_state)
if not visible_reason or blocked_reason:
continue
@ -959,6 +962,10 @@ class RetrievalEngine:
prefiltered: dict[str, int] = {}
time_filtered = 0
for memory in candidates:
domain_reason = self._memory_domain_block_reason(memory)
if domain_reason:
prefiltered[domain_reason] = prefiltered.get(domain_reason, 0) + 1
continue
visibility_reason, prefilter_reason = self._search_visibility_reason(memory, ctx, acl_state)
if visibility_reason:
if time_intent is not None and time_intent.active and not self._memory_overlaps_time_window(memory, time_intent):
@ -1620,6 +1627,17 @@ class RetrievalEngine:
break
return merged
@staticmethod
def _memory_domain_block_reason(memory: MemoryRecord) -> str:
domain = clean_text(getattr(memory, "memory_domain", "") or "", 60).lower()
if domain == "user_memory":
return ""
if domain == "bot_self_legacy":
return "bot_self_domain"
if domain == "shared_or_mixed":
return "mixed_domain"
return "missing_user_subject"
def _search_visibility_reason(
self,
memory: MemoryRecord,
@ -1647,6 +1665,8 @@ class RetrievalEngine:
return f"prefiltered_out_of_search_range:{total};{detail}"
def _slot_for_memory(self, memory: MemoryRecord, ctx: SessionContext) -> str:
if self._memory_domain_block_reason(memory):
return "blocked_domain"
tags = {str(tag).lower() for tag in (memory.tags or [])}
memory_type = (memory.memory_type or "").lower()
reality = (memory.reality_level or "").lower()

查看文件

@ -53,6 +53,8 @@ from .store import MemoryStore
from .summarizer import MemorySummarizer
from .time_intent import TimeIntent, parse_time_intent
from .turn_signal import analyze_turn_signal, message_terms
from .user_memory_boundary import UserMemoryBoundaryError, validate_user_memory_record
from .user_memory_migration import UserMemoryMigration
from .visibility import VisibilityPolicy
@ -160,6 +162,7 @@ class MemoryCompanionService:
self._RETRIEVAL_RESULT_CACHE_TTL: float = 45.0
self._RETRIEVAL_RESULT_CACHE_MAX: int = 128
self.migrator = LivingMemoryMigrator(self.store, self.plugin_root, self.data_dir)
self.user_memory_migration = UserMemoryMigration(self.store)
self.portable_archive = PortableMemoryArchive(self.store, self.data_dir)
self.chat_importer = HistoricalChatImporter(self)
self.qq_history_reader = QQHistoryReader(self)
@ -174,6 +177,15 @@ class MemoryCompanionService:
self._RELATIONSHIP_PHASE_FILE = self.data_dir / "memory_companion_relationship_phase.json"
self._load_relationship_phase_state()
async def preview_user_memory_migration(self, *, limit: int = 5000) -> dict[str, Any]:
return await self.user_memory_migration.preview(limit=limit)
async def apply_user_memory_migration(self, *, migration_id: str = "", limit: int = 5000) -> dict[str, Any]:
return await self.user_memory_migration.apply(migration_id=migration_id, limit=limit)
async def rollback_user_memory_migration(self, migration_id: str) -> dict[str, Any]:
return await self.user_memory_migration.rollback(migration_id)
def preview_historical_chat_upload(
self,
*,
@ -608,6 +620,9 @@ class MemoryCompanionService:
event_metadata.setdefault("occurred_at_estimated", not bool(occurred_at))
if event_type == "bot_response":
event_metadata.setdefault("owner_bot_id", subject_id)
event_metadata.setdefault("memory_domain", "bot_self_legacy")
else:
event_metadata.setdefault("memory_domain", "user_memory")
if user_name:
event_metadata.setdefault("sender_name", clean_text(user_name, 120))
if message_id:
@ -623,12 +638,84 @@ class MemoryCompanionService:
occurred_at=occurred_at,
)
async def record_user_memory(
self,
*,
content: str,
memory_type: str = "user_fact",
user_id: str,
user_name: str = "",
scope: str = "private",
session_id: str = "",
group_id: str = "",
platform: str = "",
message_id: str = "",
evidence: str,
occurred_at: str = "",
metadata: dict[str, Any] | None = None,
confidence: float = 0.85,
importance: float = 0.6,
source_plugin: str = "external",
memory_id: str = "",
) -> str:
user_id = clean_text(user_id, 120)
evidence = clean_text(evidence, 4000)
if not user_id:
raise UserMemoryBoundaryError("missing_user_subject_id")
if not evidence:
raise UserMemoryBoundaryError("missing_user_evidence")
event_metadata = dict(metadata or {})
event_metadata.setdefault("source_message_id", clean_text(message_id, 160))
event_metadata.setdefault("user_id", user_id)
event_metadata.setdefault("user_name", clean_text(user_name, 120))
return await self.record_external_event(
content=content,
memory_type=memory_type,
memory_domain="user_memory",
scope=scope,
session_id=session_id,
platform=platform,
message_id=message_id,
group_id=group_id,
subject=EntityRef(kind="user", id=user_id, name=user_name, role="memory_subject"),
object=EntityRef.bot_self(),
visibility="group_public" if scope == "group" else "private_pair",
sayability="indirect",
reality_level="real_user_fact",
lifecycle="stable_memory",
confidence=confidence,
importance=importance,
tags=["user_memory", memory_type, scope],
metadata=event_metadata,
source_plugin=source_plugin,
memory_id=memory_id,
occurred_at=occurred_at,
)
async def record_external_event(self, **kwargs: Any) -> str:
if not self.config.bool("private_companion_bridge.accept_external_records", True):
raise RuntimeError("外部记息写入已关闭")
explicit_id = clean_text(kwargs.pop("memory_id", "") or kwargs.pop("id", ""), 120)
record = self.classifier.external_record(**kwargs)
memory_domain = clean_text(getattr(record, "memory_domain", ""), 60).lower()
if memory_domain == "user_memory":
try:
validate_user_memory_record(record)
except UserMemoryBoundaryError as exc:
logger.warning(
"[MemoryCompanion] 用户记忆边界拒绝: reason=%s source=%s",
exc.reason,
kwargs.get("source_plugin", "external"),
)
raise
elif memory_domain not in {"calendar_history", "shared_or_mixed"}:
raise UserMemoryBoundaryError(f"memory_domain:{memory_domain or 'unknown'}")
else:
record.memory_domain = memory_domain
record.metadata = dict(record.metadata or {})
record.metadata["memory_domain"] = memory_domain
record.metadata.setdefault("isolated", True)
if explicit_id:
record.id = explicit_id
elif not record.id:
@ -748,6 +835,7 @@ class MemoryCompanionService:
memory_id=memory_id,
content=content,
memory_type="calendar_event",
memory_domain="calendar_history",
scope="private",
session_id="calendar_history",
visibility="bot_self",
@ -2887,6 +2975,8 @@ class MemoryCompanionService:
return ""
visibility = "group_public" if ctx.scope == "group" else "private_pair"
has_bot_material = any(clean_text(row.get("event_type"), 40).lower() == "bot_response" for row in rows)
summary_domain = "shared_or_mixed" if has_bot_material else "user_memory"
start_at = clean_text(rows[0].get("occurred_at") if rows else "", 80)
end_at = clean_text(rows[-1].get("occurred_at") if rows else "", 80)
start_at_local = self._local_time_label(start_at)
@ -2899,7 +2989,8 @@ class MemoryCompanionService:
record = MemoryRecord(
id=self.stable_id("summary", ctx.session_id, start_at, end_at, content),
memory_type="conversation_summary",
subject=self._bot_entity(ctx) if ctx.scope == "group" else EntityRef(kind="user", id=ctx.user_id, name=ctx.user_name, role="conversation_partner"),
memory_domain=summary_domain,
subject=EntityRef(kind="user", id=ctx.user_id, name=ctx.user_name, role="conversation_partner") if summary_domain == "user_memory" else self._bot_entity(ctx),
object=EntityRef(kind="group", id=ctx.group_id, name=ctx.group_name, role="group") if ctx.scope == "group" else self._bot_entity(ctx),
scope=ctx.scope,
session_id=ctx.session_id,
@ -2926,6 +3017,8 @@ class MemoryCompanionService:
"summarizer": "companion_memory_schema_v1",
"summary_schema_version": "companion_memory_v1",
"owner_bot_id": self._bot_subject_id(ctx),
"memory_domain": summary_domain,
"isolated_mixed": summary_domain == "shared_or_mixed",
"summary_quality": self.summarizer.summary_quality(payload or {}),
"canonical_summary": clean_text((payload or {}).get("canonical_summary"), 2000),
"persona_summary": clean_text((payload or {}).get("persona_summary") or (payload or {}).get("summary"), 2000),
@ -2962,87 +3055,7 @@ class MemoryCompanionService:
payload: dict[str, Any],
summary_memory_id: str,
) -> int:
if ctx.scope != "group":
return 0
facts = payload.get("bot_self_facts") if isinstance(payload, dict) else None
if not isinstance(facts, list):
return 0
current_bot_id = self._bot_subject_id(ctx)
bot_rows: dict[str, dict[str, Any]] = {}
for row in rows:
event_id = clean_text(row.get("id"), 160)
event_type = clean_text(row.get("event_type"), 40).lower()
subject_id = clean_text(row.get("subject_id"), 120)
if not event_id or event_type != "bot_response":
continue
if subject_id and subject_id not in {"self", current_bot_id}:
continue
bot_rows[event_id] = row
created = 0
for item in facts[:4]:
if not isinstance(item, dict):
continue
event_id = clean_text(item.get("event_id") or item.get("source_event_id"), 160)
source_row = bot_rows.get(event_id)
if source_row is None:
continue
fact = clean_text(item.get("fact") or item.get("content"), 220)
if len(fact) < 4 or self.summarizer._looks_like_prompt_injection(fact):
continue
kind = clean_text(item.get("kind"), 24).lower()
if kind not in {"schedule", "commitment", "action"}:
kind = "schedule"
memory_type = "schedule_fragment" if kind == "schedule" else "self_action"
evidence = clean_text(source_row.get("content"), 700)
if not evidence or not self.summarizer._bot_self_fact_supported_by_evidence(fact, evidence):
continue
record = MemoryRecord(
id=self.stable_id("group_bot_self_fact", ctx.session_id, event_id, kind, fact),
memory_type=memory_type,
subject=self._bot_entity(ctx),
object=EntityRef(kind="group", id=ctx.group_id, name=ctx.group_name, role="group"),
scope="group",
session_id=ctx.session_id,
platform=ctx.platform,
message_id=event_id,
group_id=ctx.group_id,
visibility="bot_self",
sayability="direct",
reality_level="bot_action",
lifecycle="stable_memory",
content=fact,
evidence=evidence,
confidence=0.86,
importance=0.6,
review_status="auto",
tags=["bot_self_fact", "group_origin", kind],
metadata={
"verified_bot_self_fact": True,
"direct_bot_response": True,
"source_event_id": event_id,
"source_summary_id": summary_memory_id,
"group_origin": ctx.group_id,
"fact_kind": kind,
"owner_bot_id": current_bot_id,
},
occurred_at=clean_text(source_row.get("occurred_at") or source_row.get("created_at"), 80),
source_plugin="memory_companion",
)
try:
self.importance.calibrate(record, source="verified_group_bot_fact")
memory_id = await self.store.insert_memory(record)
self._schedule_memory_embedding(memory_id, record)
created += 1
except Exception as exc:
logger.warning(
"[MemoryCompanion] 群聊 Bot 自身事实写入失败: session=%s event=%s error=%s",
ctx.session_id,
event_id,
exc,
)
return created
return 0
async def _index_summary_knowledge_graph(
self,
@ -3448,37 +3461,34 @@ class MemoryCompanionService:
note_type = clean_text(note_type, 40) or "memory"
if not content:
return {"ok": False, "error": "empty content"}
visibility = "internal"
if ctx.scope == "private":
visibility = "private_pair"
elif ctx.scope == "group":
visibility = "group_public"
record = MemoryRecord(
id=self.stable_id("tool", note_type, ctx.session_id, content),
memory_type="tool_memory",
subject=self._bot_entity(ctx),
object=EntityRef(kind="user", id=ctx.user_id, name=ctx.user_name, role="conversation_partner"),
scope=ctx.scope,
session_id=ctx.session_id,
platform=ctx.platform,
message_id=ctx.message_id,
group_id=ctx.group_id,
visibility=visibility,
sayability="indirect",
reality_level="llm_tool_assertion",
lifecycle="stable_memory",
content=content,
evidence=ctx.message_text,
confidence=0.62,
importance=0.66,
review_status="auto",
tags=["llm_tool", note_type, ctx.scope],
metadata={"tool": "memory_companion_remember", "note_type": note_type, "owner_bot_id": self._bot_subject_id(ctx)},
)
self.importance.calibrate(record, source="tool_memory")
memory_id = await self.store.insert_memory(record)
self._schedule_memory_embedding(memory_id, record)
return {"ok": True, "memory_id": memory_id, "review_status": record.review_status}
if ctx.scope not in {"private", "group"} or not ctx.user_id:
return {"ok": False, "error": "user_memory_subject_required"}
try:
memory_id = await self.record_user_memory(
content=content,
memory_type="tool_memory",
user_id=ctx.user_id,
user_name=ctx.user_name,
scope=ctx.scope,
session_id=ctx.session_id,
group_id=ctx.group_id,
platform=ctx.platform,
message_id=ctx.message_id,
evidence=ctx.message_text or content,
metadata={
"tool": "memory_companion_remember",
"note_type": note_type,
"owner_bot_id": self._bot_subject_id(ctx),
"user_confirmed": True,
},
confidence=0.72,
importance=0.66,
source_plugin="memory_companion_tool",
memory_id=self.stable_id("tool", note_type, ctx.user_id, ctx.scope, ctx.group_id, content),
)
except UserMemoryBoundaryError as exc:
return {"ok": False, "error": exc.reason}
return {"ok": True, "memory_id": memory_id, "review_status": "auto"}
async def tool_recall(self, event: Any, query: str, top_k: int = 5) -> dict[str, Any]:
ctx = await self.identity.resolve_event_context(event)
@ -3492,36 +3502,12 @@ class MemoryCompanionService:
}
async def tool_note_create(self, event: Any, title: str, content: str = "") -> dict[str, Any]:
ctx = await self.identity.resolve_event_context(event)
title = clean_text(title, 120)
content = clean_text(content or title, 3000)
if not title and not content:
return {"ok": False, "error": "empty note"}
record = MemoryRecord(
id=self.stable_id("companion_note", ctx.session_id, title, content),
memory_type="companion_note",
subject=self._bot_entity(ctx),
object=EntityRef(kind="session", id=ctx.session_id, role="companion_context"),
scope="unknown",
session_id=ctx.session_id,
platform=ctx.platform,
visibility="bot_self",
sayability="indirect",
reality_level="persona_life",
lifecycle="stable_memory",
content=content,
evidence=ctx.message_text,
confidence=0.82,
importance=0.6,
review_status="auto",
tags=["companion_note", "bot_self", title] if title else ["companion_note", "bot_self"],
metadata={"title": title, "tool": "memory_companion_note_create", "owner_bot_id": self._bot_subject_id(ctx)},
source_plugin="memory_companion_tool",
)
self.importance.calibrate(record, source="companion_note")
memory_id = await self.store.insert_memory(record)
self._schedule_memory_embedding(memory_id, record)
return {"ok": True, "memory_id": memory_id, "title": title}
return {
"ok": False,
"error": "bot_self_notes_disabled:user_memory_contract",
"message": "运维记忆插件只保存用户记忆;Bot 笔记请由陪伴插件管理。",
"readonly": True,
}
async def tool_note_read(self, event: Any, query: str = "", limit: int = 5) -> dict[str, Any]:
ctx = await self.identity.resolve_event_context(event)
@ -3548,16 +3534,12 @@ class MemoryCompanionService:
}
async def import_livingmemory(self, *, configured_path: str = "") -> dict[str, Any]:
if self.config.bool("maintenance.backup_before_import", True):
backup = self.store.backup(".before_livingmemory_import")
logger.info("[MemoryCompanion] LivingMemory 导入前已备份数据库: %s", backup)
return await self.migrator.import_data(
configured_path=configured_path,
default_review_status=str(
self.config.get("livingmemory_migration.default_review_status", "auto") or "auto"
),
limit=self.config.int("livingmemory_migration.import_limit", 5000),
)
return {
"imported": 0,
"skipped": 0,
"disabled": True,
"reason": "livingmemory_disabled_by_user_memory_contract",
}
def operation_preset_status(self) -> dict[str, Any]:
preset = detect_preset(self.config)
@ -7282,6 +7264,7 @@ class MemoryCompanionService:
subject_id = clean_text(row.get("subject_id"), 120)
metadata = json_loads(row.get("metadata"), {})
sender_name = clean_text(metadata.get("sender_name"), 120)
memory_domain = clean_text(metadata.get("memory_domain"), 60) or ("bot_self_legacy" if event_type == "bot_response" or subject_id == "self" else "user_memory")
if event_type == "bot_response" or subject_id == "self":
subject = self._bot_entity(ctx)
prefix = "Bot"
@ -7292,6 +7275,7 @@ class MemoryCompanionService:
return MemoryRecord(
id=f"timeline_{clean_text(row.get('id'), 100)}",
memory_type="timeline_event",
memory_domain=memory_domain,
subject=subject,
object=EntityRef(kind="group" if ctx.scope == "group" else "user", id=ctx.current_target_id, name=ctx.group_name or ctx.user_name, role="current_window"),
scope=ctx.scope,

查看文件

@ -199,6 +199,7 @@ class MemoryStore:
CREATE TABLE IF NOT EXISTS memories (
id TEXT PRIMARY KEY,
memory_type TEXT NOT NULL,
memory_domain TEXT NOT NULL DEFAULT 'unknown',
subject_kind TEXT NOT NULL DEFAULT '',
subject_id TEXT NOT NULL DEFAULT '',
subject_name TEXT NOT NULL DEFAULT '',
@ -235,6 +236,8 @@ class MemoryStore:
supersedes_id TEXT NOT NULL DEFAULT ''
);
CREATE INDEX IF NOT EXISTS idx_memories_domain ON memories(memory_domain, lifecycle, scope, group_id, subject_id);
CREATE INDEX IF NOT EXISTS idx_memories_scope_session
ON memories(scope, session_id, group_id, subject_id, object_id);
CREATE INDEX IF NOT EXISTS idx_memories_visibility
@ -560,7 +563,7 @@ class MemoryStore:
END;
CREATE TRIGGER IF NOT EXISTS trg_memories_retrieval_revision_update
AFTER UPDATE OF
memory_type, subject_kind, subject_id, subject_name, subject_role,
memory_type, memory_domain, subject_kind, subject_id, subject_name, subject_role,
object_kind, object_id, object_name, object_role, scope, session_id,
platform, message_id, group_id, visibility, sayability, reality_level,
lifecycle, content, evidence, confidence, importance, review_status,
@ -768,6 +771,7 @@ class MemoryStore:
"content_fingerprint": "TEXT NOT NULL DEFAULT ''",
"merged_count": "INTEGER NOT NULL DEFAULT 1",
"supersedes_id": "TEXT NOT NULL DEFAULT ''",
"memory_domain": "TEXT NOT NULL DEFAULT 'unknown'",
}
for name, ddl in additions.items():
if name not in existing:

查看文件

@ -0,0 +1,92 @@
from __future__ import annotations
from typing import Any
from .models import MemoryRecord, clean_text
USER_MEMORY_DOMAIN = "user_memory"
BOT_SELF_LEGACY_DOMAIN = "bot_self_legacy"
MIXED_MEMORY_DOMAIN = "shared_or_mixed"
UNKNOWN_MEMORY_DOMAIN = "unknown"
CALENDAR_HISTORY_DOMAIN = "calendar_history"
USER_MEMORY_TYPES = frozenset({
"user_fact",
"user_event",
"user_decision",
"user_summary",
"user_profile",
"user_preference",
"user_habit",
"relationship_claim",
"explicit_memory",
"manual_memory",
"conversation_event",
"conversation_summary",
"important_event",
"daily_digest",
"tool_memory",
})
BOT_SELF_MEMORY_TYPES = frozenset({
"self_action",
"persona_life",
"proactive_message",
"schedule_fragment",
"companion_note",
"calendar_event",
"agenda_snapshot",
"timeline_event",
"memory_decay_summary",
})
BOT_SELF_REALITY_LEVELS = frozenset({"bot_action", "persona_life", "llm_tool_assertion"})
class UserMemoryBoundaryError(ValueError):
def __init__(self, reason: str):
self.reason = clean_text(reason, 160) or "user_memory_boundary_rejected"
super().__init__(self.reason)
def infer_memory_domain(record: MemoryRecord) -> str:
metadata = record.metadata if isinstance(record.metadata, dict) else {}
explicit = clean_text(metadata.get("memory_domain") or getattr(record, "memory_domain", ""), 60)
if explicit:
return explicit
memory_type = clean_text(record.memory_type, 80).lower()
reality = clean_text(record.reality_level, 80).lower()
if memory_type in BOT_SELF_MEMORY_TYPES or record.visibility == "bot_self" or reality in BOT_SELF_REALITY_LEVELS:
return BOT_SELF_LEGACY_DOMAIN
if memory_type in USER_MEMORY_TYPES or getattr(record.subject, "kind", "") == "user":
return USER_MEMORY_DOMAIN
return UNKNOWN_MEMORY_DOMAIN
def validate_user_memory_record(record: MemoryRecord, *, require_evidence: bool = True) -> MemoryRecord:
domain = infer_memory_domain(record)
if domain != USER_MEMORY_DOMAIN:
raise UserMemoryBoundaryError(f"memory_domain:{domain}")
if clean_text(record.subject.kind, 40).lower() != "user":
raise UserMemoryBoundaryError("missing_user_subject")
if not clean_text(record.subject.id, 120):
raise UserMemoryBoundaryError("missing_user_subject_id")
if clean_text(record.scope, 40).lower() not in {"private", "group"}:
raise UserMemoryBoundaryError("unsupported_user_scope")
if record.scope == "group" and not clean_text(record.group_id, 120):
raise UserMemoryBoundaryError("missing_group_id")
if clean_text(record.visibility, 40).lower() == "bot_self":
raise UserMemoryBoundaryError("bot_self_visibility")
if clean_text(record.memory_type, 80).lower() in BOT_SELF_MEMORY_TYPES:
raise UserMemoryBoundaryError(f"bot_self_memory_type:{record.memory_type}")
if clean_text(record.reality_level, 80).lower() in BOT_SELF_REALITY_LEVELS:
raise UserMemoryBoundaryError(f"bot_self_reality_level:{record.reality_level}")
if require_evidence and not clean_text(record.evidence, 4000):
raise UserMemoryBoundaryError("missing_user_evidence")
metadata = record.metadata if isinstance(record.metadata, dict) else {}
if not clean_text(metadata.get("source_message_id") or record.message_id, 160):
raise UserMemoryBoundaryError("missing_user_source_message")
record.memory_domain = USER_MEMORY_DOMAIN
record.metadata = dict(metadata)
record.metadata["memory_domain"] = USER_MEMORY_DOMAIN
record.metadata.setdefault("evidence_level", "direct_user_statement")
record.metadata.setdefault("user_confirmed", False)
return record

查看文件

@ -0,0 +1,110 @@
from __future__ import annotations
from collections import Counter
from datetime import datetime, timezone
from typing import Any
from .models import clean_text, json_dumps, json_loads
DOMAINS = ("user_memory", "bot_self_legacy", "shared_or_mixed", "unknown")
class UserMemoryMigration:
"""Previewable, reversible domain classification for legacy memories."""
def __init__(self, store: Any):
self.store = store
async def preview(self, *, limit: int = 5000) -> dict[str, Any]:
return await self.store._run_recoverable_database_operation(self._preview_sync, max(1, int(limit or 1)))
async def apply(self, *, migration_id: str = "", limit: int = 5000) -> dict[str, Any]:
return await self.store._run_recoverable_database_operation(
self._apply_sync,
clean_text(migration_id, 120) or self._new_migration_id(),
max(1, int(limit or 1)),
)
async def rollback(self, migration_id: str) -> dict[str, Any]:
return await self.store._run_recoverable_database_operation(self._rollback_sync, clean_text(migration_id, 120))
def _new_migration_id(self) -> str:
return datetime.now(timezone.utc).strftime("q5-%Y%m%d%H%M%S")
def _preview_sync(self, limit: int) -> dict[str, Any]:
rows = self.store._conn.execute(
"SELECT * FROM memories ORDER BY created_at ASC LIMIT ?", (limit,)
).fetchall()
items = []
counts = Counter()
for row in rows:
domain, reason = self._classify(row)
counts[domain] += 1
items.append({"id": str(row["id"]), "memory_type": str(row["memory_type"] or ""), "domain": domain, "reason": reason})
return {"mode": "preview", "readonly": True, "total": len(items), "counts": dict(counts), "items": items}
def _apply_sync(self, migration_id: str, limit: int) -> dict[str, Any]:
rows = self.store._conn.execute(
"SELECT * FROM memories WHERE memory_domain IS NULL OR memory_domain IN ('', 'unknown') ORDER BY created_at ASC LIMIT ?",
(limit,),
).fetchall()
self.store._conn.execute(
"CREATE TABLE IF NOT EXISTS memory_domain_migration_backup (migration_id TEXT NOT NULL, memory_id TEXT NOT NULL, previous_domain TEXT NOT NULL, created_at TEXT NOT NULL, PRIMARY KEY(migration_id, memory_id))"
)
counts = Counter()
now = datetime.now(timezone.utc).isoformat(timespec="seconds")
for row in rows:
domain, reason = self._classify(row)
self.store._conn.execute(
"INSERT OR IGNORE INTO memory_domain_migration_backup(migration_id, memory_id, previous_domain, created_at) VALUES(?,?,?,?)",
(migration_id, str(row["id"]), str(row["memory_domain"] or "unknown"), now),
)
metadata = json_loads(row["metadata"], {})
if not isinstance(metadata, dict):
metadata = {}
metadata["memory_domain_migration_id"] = migration_id
metadata["memory_domain_migration_reason"] = reason
self.store._conn.execute(
"UPDATE memories SET memory_domain=?, metadata=?, updated_at=? WHERE id=?",
(domain, json_dumps(metadata), now, str(row["id"])),
)
counts[domain] += 1
self.store._conn.commit()
self.store._rebuild_memory_fts_sync()
return {"mode": "apply", "migration_id": migration_id, "changed": sum(counts.values()), "counts": dict(counts), "deleted": 0}
def _rollback_sync(self, migration_id: str) -> dict[str, Any]:
if not migration_id:
return {"mode": "rollback", "migration_id": "", "restored": 0, "error": "migration_id_required"}
rows = self.store._conn.execute(
"SELECT memory_id, previous_domain FROM memory_domain_migration_backup WHERE migration_id=?",
(migration_id,),
).fetchall()
for row in rows:
self.store._conn.execute(
"UPDATE memories SET memory_domain=?, updated_at=? WHERE id=?",
(str(row["previous_domain"] or "unknown"), datetime.now(timezone.utc).isoformat(timespec="seconds"), str(row["memory_id"])),
)
self.store._conn.commit()
self.store._rebuild_memory_fts_sync()
return {"mode": "rollback", "migration_id": migration_id, "restored": len(rows), "deleted": 0}
@staticmethod
def _classify(row: Any) -> tuple[str, str]:
current = clean_text(row["memory_domain"], 60).lower()
if current in DOMAINS and current != "unknown":
return current, "existing_domain"
memory_type = clean_text(row["memory_type"], 80).lower()
subject_kind = clean_text(row["subject_kind"], 40).lower()
visibility = clean_text(row["visibility"], 40).lower()
reality = clean_text(row["reality_level"], 80).lower()
if memory_type == "calendar_event":
return "calendar_history", "calendar_event_type"
if subject_kind == "user" and visibility != "bot_self" and reality not in {"bot_action", "persona_life", "llm_tool_assertion"}:
if memory_type.startswith("shared_") or "shared_experience" in clean_text(row["tags"], 500):
return "shared_or_mixed", "shared_experience_marker"
return "user_memory", "user_subject_and_non_bot_reality"
if subject_kind == "bot" or visibility == "bot_self" or reality in {"bot_action", "persona_life", "llm_tool_assertion"}:
return "bot_self_legacy", "bot_subject_or_bot_reality"
return "unknown", "insufficient_subject_evidence"

查看文件

@ -0,0 +1,27 @@
# Q5 用户记忆硬边界实施说明
日期:2026-07-25
目标:运维版 `astrbot_plugin_memory_companion-main`,不修改普通陪伴分支。
## 已实现
- 用户记忆合同:`record_user_fact`、`record_user_event`、`record_user_decision`、`record_user_summary`。
- 用户主体硬校验:必须是 `subject.kind=user`,私聊使用 `scope=private`,群聊使用 `scope=group` 且必须携带 `group_id`、证据和来源消息。
- 域字段:`memory_domain=user_memory`;旧 Bot 记录、混合记录和未知记录分别进入 `bot_self_legacy`、`shared_or_mixed`、`unknown`,默认不参与检索。
- 时间字段沿用 Q4.2:`occurred_at`、`created_at`、`updated_at`、来源、精度、时区和估算标记;动态年龄只在序列化/检索时生成。
- 检索隔离:默认仅允许 `user_memory`,阻断原因包含 `bot_self_domain`、`mixed_domain`、`missing_user_subject`。
- 混合总结隔离:包含 Bot 回复材料的总结标记为 `shared_or_mixed`;Bot 自身事实不再由记忆插件单独保存。
- 旁路封堵:通用 `record_event`、Bot 行为、人格生活、主动消息、日历历史公开 Bridge 均失败关闭;`tool_remember` 改为用户主体,Bot 笔记入口只读拒绝。
- LivingMemory:导入、修复和命令入口按合同停用,不再作为数据源。
- 旧数据迁移:提供只读 preview、apply 和 rollback;迁移不删除记录,应用后重建 FTS。
- 管理 API:增加 `/migration/preview`、`/migration/apply`、`/migration/rollback`,并禁止把非用户域记录改成公开用户可见性。
## 跨插件联动
陪伴插件只提交用户事实、用户行为和用户决定;Bot 日程、人格、自我时间线和主动行为由陪伴插件内部保存。双方不读取对方数据库。记忆 Bridge 不可用时,陪伴插件的本地日历和正常聊天链路不依赖本插件。
## 验证边界
- 私聊和群聊用户记忆分别验证;群聊记录绑定实际发言用户和群 ID。
- 日历历史 Service 内部兼容测试保留,但公开 Bridge 按 Q5 合同拒绝,能力探测返回 `accepts_calendar_history=false`。
- 旧 Q4.2 测试中与 Q5 冲突的 Bridge 断言已调整为新合同;普通陪伴分支未修改。

查看文件

@ -85,3 +85,23 @@
- Fix/workaround: kept the ordinary branch unchanged; validated all relevant ops, temporal, Bridge, storage, injection and panel tests separately, and will run discovery excluding only this unrelated module.
- Prevention rule: separate pure unit tests from host-runtime integration tests; do not treat a host dependency collection error as a product regression.
- Validation evidence: focused suite passed 16/16; `compileall -q core tests` and `git diff --check` passed.
## 2026-07-25 — Q5 用户记忆硬边界与混合域隔离
- 阶段:M5.0-M5.4 用户记忆合同、Bridge 收口、检索隔离、迁移预览
- 类型:API契约 / 数据安全 / 测试 / 功能行为
- 现象:旧通用 Bridge 同时允许 Bot 自我、共享体验、日历历史和用户记忆写入;混合总结可能被误当成用户事实。
- 根因:历史数据模型只有主体、可见性和记忆类型,没有强制的数据所有权域;检索入口也没有先做域过滤。
- 修复或规避:新增 `memory_domain` 和用户主体验证;用户 Bridge 强制证据与来源;Bot/混合记录分别拒绝或隔离;默认检索只接受 `user_memory`;旧数据提供 preview/apply/rollback,禁止直接删除;LivingMemory 入口停用。
- 预防规则:收口的是数据所有权,不是删除私聊或群聊;任何用户记忆必须绑定实际用户、scope、群 ID(群聊)和可追溯证据;日历和 Bot 自我连续性不得通过记忆插件公开写入。
- 验证证据:`tests.test_user_memory_contract`、`tests.test_calendar_bridge`、`tests.test_bridge_shared_experience`、`tests.test_epistemic_calibration.VerifiedGroupBotFactTests` 通过;全仓编译和迁移回滚测试随后复核。
## 2026-07-25 — Q5 全量回归与宿主依赖边界
- 阶段:M5.5 时间感知回归与全仓验证
- 类型:测试 / 环境
- 现象:全量发现最初包含普通陪伴分支的 `test_security_cache`,本机未安装 AstrBot 宿主包而无法收集。
- 根因:运维版仓库测试发现路径会导入普通陪伴分支页面 API;该分支不属于本次改造范围。
- 修复或规避:不修改普通陪伴分支;将宿主依赖测试单独标记为环境阻塞,其余 142 项运维版测试正常执行。
- 预防规则:跨插件测试必须区分纯单元测试与宿主集成测试;宿主依赖缺失不能被误报为业务回归。
- 验证证据:`compileall -q core tests page_api.py` 通过;`git diff --check` 通过;排除 `test_security_cache` 后 `142 tests` 全部通过;定向 Q5、Q4.2、总结、检索和存储测试全部通过。

查看文件

@ -21,10 +21,12 @@ try:
from .core.bridge import serialize_memory
from .core.identity import normalize_session_context_fields
from .core.models import SessionContext, clean_text
from .core.user_memory_boundary import USER_MEMORY_DOMAIN
except ImportError:
from core.bridge import serialize_memory
from core.identity import normalize_session_context_fields
from core.models import SessionContext, clean_text
from core.user_memory_boundary import USER_MEMORY_DOMAIN
try:
from .plugin_identity import PLUGIN_NAME, PRIVATE_COMPANION_PLUGIN_NAMES
@ -70,6 +72,9 @@ class PluginPageApi:
("/memory/delete", self.memory_delete, ["POST"], "MemoryCompanion Page memory delete"),
("/memory/visibility", self.memory_visibility, ["POST"], "MemoryCompanion Page memory visibility"),
("/memory/lifecycle", self.memory_lifecycle, ["POST"], "MemoryCompanion Page memory lifecycle"),
("/migration/preview", self.migration_preview, ["GET"], "MemoryCompanion Q5 migration preview"),
("/migration/apply", self.migration_apply, ["POST"], "MemoryCompanion Q5 migration apply"),
("/migration/rollback", self.migration_rollback, ["POST"], "MemoryCompanion Q5 migration rollback"),
("/acl", self.acl, ["GET"], "MemoryCompanion Page memory ACL rules"),
("/acl/upsert", self.acl_upsert, ["POST"], "MemoryCompanion Page memory ACL upsert"),
("/acl/policy", self.acl_policy, ["POST"], "MemoryCompanion Page memory ACL policy"),
@ -454,6 +459,12 @@ class PluginPageApi:
async def memory_update(self):
payload = await self._json()
memory_id = clean_text(payload.get("id"), 120)
record = await self.plugin.service.store.get_memory(memory_id)
if not record:
return self._err("memory not found", 404)
requested_visibility = clean_text(payload.get("visibility"), 40)
if getattr(record, "memory_domain", "") != USER_MEMORY_DOMAIN and requested_visibility not in {"", "internal"}:
return self._err("non_user_memory_visibility_locked", 409)
if not memory_id:
return self._err("missing id", 400)
ok = await self.plugin.service.store.update_memory_payload(
@ -477,6 +488,13 @@ class PluginPageApi:
async def memory_visibility(self):
payload = await self._json()
memory_id = clean_text(payload.get("id"), 120)
record = await self.plugin.service.store.get_memory(memory_id)
if not record:
return self._err("memory not found", 404)
visibility = clean_text(payload.get("visibility"), 40)
if getattr(record, "memory_domain", "") != USER_MEMORY_DOMAIN and visibility != "internal":
return self._err("non_user_memory_visibility_locked", 409)
ok = await self.plugin.service.store.update_memory_visibility(
clean_text(payload.get("id"), 120),
clean_text(payload.get("visibility"), 40),
@ -491,6 +509,24 @@ class PluginPageApi:
)
return self._ok({"updated": ok})
async def migration_preview(self):
limit = self._query_int("limit", 5000)
return self._ok(await self.plugin.service.preview_user_memory_migration(limit=limit))
async def migration_apply(self):
payload = await self._json()
return self._ok(await self.plugin.service.apply_user_memory_migration(
migration_id=clean_text(payload.get("migration_id"), 120),
limit=max(1, int(payload.get("limit") or 5000)),
))
async def migration_rollback(self):
payload = await self._json()
migration_id = clean_text(payload.get("migration_id"), 120)
if not migration_id:
return self._err("migration_id_required", 400)
return self._ok(await self.plugin.service.rollback_user_memory_migration(migration_id))
async def acl(self):
owner_scope = clean_text(request.args.get("scope", ""), 40)
owner_id = clean_text(request.args.get("id", ""), 160)
@ -2068,11 +2104,7 @@ class PluginPageApi:
return self._ok({"result": result})
async def repair_livingmemory_content(self):
payload = await self._json()
result = await self.plugin.service.migrator.repair_imported_content(
configured_path=clean_text(payload.get("path"), 1000)
)
return self._ok({"result": result})
return self._err("livingmemory_disabled_by_user_memory_contract", 410)
async def clear_all(self):
payload = await self._json()
@ -2107,16 +2139,10 @@ class PluginPageApi:
return self._ok({"result": result})
async def import_preview(self):
configured = clean_text(request.args.get("path", ""), 1000)
report = self.plugin.service.migrator.preview(configured)
return self._ok({"report": report})
return self._err("livingmemory_disabled_by_user_memory_contract", 410)
async def import_run(self):
payload = await self._json()
result = await self.plugin.service.import_livingmemory(
configured_path=clean_text(payload.get("path"), 1000)
)
return self._ok({"result": result})
return self._err("livingmemory_disabled_by_user_memory_contract", 410)
@staticmethod
def _ok(data: dict[str, Any] | None = None):

查看文件

@ -39,7 +39,8 @@ class SharedExperienceBridgeTests(unittest.IsolatedAsyncioTestCase):
self.assertEqual("bot_self", service.payload["subject"].role)
self.assertEqual("87654321", service.payload["object"].id)
self.assertEqual("shared_experience_partner", service.payload["object"].role)
self.assertEqual("bot_self", service.payload["visibility"])
self.assertEqual("internal", service.payload["visibility"])
self.assertEqual("shared_or_mixed", service.payload["memory_domain"])
self.assertIn("shared_experience", service.payload["tags"])

查看文件

@ -36,20 +36,19 @@ class CalendarBridgeTests(unittest.IsolatedAsyncioTestCase):
async def test_bridge_forwards_calendar_history_contract(self):
recorder = _CalendarRecorder()
bridge = MemoryCompanionBridge(recorder)
result = await bridge.record_calendar_event(
event_id="event-1", title="阅读一小时",
start_at="2026-07-24T20:00:00+08:00",
end_at="2026-07-24T21:00:00+08:00", status="planned",
)
self.assertEqual("calendar-memory-1", result)
self.assertEqual("event-1", recorder.payload["event_id"])
self.assertEqual("planned", recorder.payload["status"])
with self.assertRaisesRegex(RuntimeError, "calendar_history_rejected"):
await bridge.record_calendar_event(
event_id="event-1", title="阅读一小时",
start_at="2026-07-24T20:00:00+08:00",
end_at="2026-07-24T21:00:00+08:00", status="planned",
)
self.assertFalse(bridge.probe_runtime_capabilities().get("accepts_calendar_history"))
self.assertEqual("history_only", bridge.probe_calendar_contract()["write_mode"])
async def test_missing_calendar_bridge_is_safe_to_probe_and_skip(self):
bridge = MemoryCompanionBridge(object())
self.assertFalse(bridge.probe_calendar_contract()["available"])
with self.assertRaisesRegex(RuntimeError, "日历历史 Bridge"):
with self.assertRaisesRegex(RuntimeError, "calendar_history_rejected"):
await bridge.record_calendar_event(
event_id="event-1", title="测试",
start_at="2026-07-24T20:00:00+08:00",
@ -59,7 +58,7 @@ class CalendarBridgeTests(unittest.IsolatedAsyncioTestCase):
async def test_calendar_event_time_fields_layers_and_deduplication(self):
service = self.make_service()
bridge = MemoryCompanionBridge(service)
memory_id = await bridge.record_calendar_event(
memory_id = await service.record_calendar_event(
event_id="event-20260724-001", calendar="maintenance_agenda",
title="备份窗口", summary="执行一次备份",
start_at="2026-07-24T23:30:00+08:00",
@ -78,7 +77,7 @@ class CalendarBridgeTests(unittest.IsolatedAsyncioTestCase):
self.assertNotIn("age_seconds", first.metadata)
time.sleep(1.1)
updated_id = await bridge.record_calendar_event(
updated_id = await service.record_calendar_event(
event_id="event-20260724-001", title="备份窗口",
start_at="2026-07-24T23:30:00+08:00",
end_at="2026-07-25T00:30:00+08:00", status="completed",

查看文件

@ -703,37 +703,8 @@ class VerifiedGroupBotFactTests(unittest.IsolatedAsyncioTestCase):
)
record = await service.store.get_memory(memory_id)
self.assertEqual(1, created)
self.assertIsNotNone(record)
assert record is not None
self.assertEqual("schedule_fragment", record.memory_type)
self.assertEqual("bot_self", record.visibility)
self.assertTrue(record.metadata["verified_bot_self_fact"])
private_ctx = SessionContext(
session_id="qq:FriendMessage:u1",
scope="private",
platform="qq",
user_id="u1",
bot_id="bot-1",
message_text="你明天有事吗?",
)
time_intent = parse_time_intent(private_ctx.message_text)
decision = service._memory_route_decision(
private_ctx,
analyze_turn_signal(private_ctx.message_text),
time_intent,
)
expression, reason = service._memory_expression_decision(
private_ctx,
record,
SearchResult(memory=record, score=1.0),
"self_timeline",
decision,
time_intent,
)
self.assertEqual("mention", expression)
self.assertEqual("future_arrangement:bot_self_evidence", reason)
self.assertEqual(0, created)
self.assertIsNone(record)
rejected = await service._record_verified_group_bot_self_facts(
ctx,

查看文件

@ -10,7 +10,7 @@ ROOT = Path(__file__).resolve().parents[1]
if str(ROOT.parent) not in sys.path:
sys.path.insert(0, str(ROOT.parent))
from core.models import MemoryRecord, SearchResult, SessionContext
from core.models import EntityRef, MemoryRecord, SearchResult, SessionContext
from core.retrieval import RetrievalEngine
from core.store import MemoryStore
from core.visibility import VisibilityPolicy
@ -19,6 +19,8 @@ from core.visibility import VisibilityPolicy
def _memory(memory_id: str, content: str = "anchor token") -> MemoryRecord:
return MemoryRecord(
id=memory_id,
memory_domain="user_memory",
subject=EntityRef(kind="user", id="u1"),
content=content,
lifecycle="stable_memory",
visibility="shareable",

查看文件

@ -0,0 +1,152 @@
# -*- coding: utf-8 -*-
from __future__ import annotations
import tempfile
import unittest
from pathlib import Path
from core.bridge import MemoryCompanionBridge
from core.models import EntityRef, MemoryRecord, SessionContext
from core.retrieval import RetrievalEngine
from core.store import MemoryStore
from core.user_memory_boundary import UserMemoryBoundaryError, validate_user_memory_record
from core.user_memory_migration import UserMemoryMigration
from core.visibility import VisibilityPolicy
class _UserPlugin:
def __init__(self) -> None:
self.calls = []
async def record_user_memory(self, **kwargs):
self.calls.append(kwargs)
return kwargs.get("memory_id") or "user-memory-id"
class UserMemoryContractTests(unittest.IsolatedAsyncioTestCase):
async def test_private_and_group_user_bridge_keep_user_subject(self) -> None:
plugin = _UserPlugin()
bridge = MemoryCompanionBridge(plugin)
private_id = await bridge.record_user_fact(
content="用户喜欢柑橘味。",
user_id="u-private",
evidence="我喜欢柑橘味。",
scope="private",
session_id="private:u-private",
message_id="m-private",
)
group_id = await bridge.record_user_event(
content="用户在群里确认周六参加。",
user_id="u-group",
evidence="我周六参加。",
scope="group",
group_id="g-1",
session_id="group:g-1",
message_id="m-group",
)
self.assertEqual("user-memory-id", private_id)
self.assertEqual("user-memory-id", group_id)
self.assertEqual("user_fact", plugin.calls[0]["memory_type"])
self.assertEqual("user_event", plugin.calls[1]["memory_type"])
self.assertEqual("u-group", plugin.calls[1]["user_id"])
self.assertEqual("group", plugin.calls[1]["scope"])
async def test_legacy_bot_and_generic_bridge_are_rejected(self) -> None:
bridge = MemoryCompanionBridge(_UserPlugin())
with self.assertRaisesRegex(RuntimeError, "generic_memory_event_rejected"):
await bridge.record_event(content="Bot 做了某事。")
with self.assertRaisesRegex(RuntimeError, "bot_self_memory_rejected"):
await bridge.record_bot_action(content="Bot 做了某事。")
with self.assertRaisesRegex(RuntimeError, "calendar_history_rejected"):
# Public calendar history writes fail closed under Q5.
await MemoryCompanionBridge(object()).record_calendar_event(
event_id="evt-1",
title="测试",
start_at="2026-07-25T10:00:00+08:00",
end_at="2026-07-25T11:00:00+08:00",
)
def test_user_boundary_requires_evidence_and_group_identity(self) -> None:
record = MemoryRecord(
memory_type="user_fact",
memory_domain="user_memory",
subject=EntityRef(kind="user", id="u-1"),
scope="group",
visibility="group_public",
reality_level="real_user_fact",
content="事实",
)
with self.assertRaisesRegex(UserMemoryBoundaryError, "missing_group_id"):
validate_user_memory_record(record)
record.group_id = "g-1"
with self.assertRaisesRegex(UserMemoryBoundaryError, "missing_user_evidence"):
validate_user_memory_record(record)
async def test_store_persists_time_domain_and_deduplicates_explicit_id(self) -> None:
with tempfile.TemporaryDirectory() as temp:
store = MemoryStore(Path(temp) / "memory.db")
store.initialize()
record = MemoryRecord(
id="user-fixed-1",
memory_type="user_fact",
memory_domain="user_memory",
subject=EntityRef(kind="user", id="u-1"),
object=EntityRef.bot_self(),
scope="private",
session_id="private:u-1",
message_id="m-1",
visibility="private_pair",
reality_level="real_user_fact",
lifecycle="stable_memory",
content="用户喜欢柑橘味。",
evidence="我喜欢柑橘味。",
occurred_at="2026-07-24T10:00:00+08:00",
created_at="2026-07-24T10:01:00+08:00",
)
first = await store.insert_memory(record)
record.updated_at = "2026-07-25T10:01:00+08:00"
second = await store.insert_memory(record)
loaded = await store.get_memory(first)
self.assertEqual(first, second)
self.assertIsNotNone(loaded)
self.assertEqual("user_memory", loaded.memory_domain)
self.assertEqual("2026-07-24T10:00:00+08:00", loaded.occurred_at)
self.assertEqual("2026-07-24T10:01:00+08:00", loaded.created_at)
store.close()
def test_retrieval_blocks_non_user_domains(self) -> None:
engine = RetrievalEngine(MemoryStore(Path(tempfile.gettempdir()) / "unused-q5.db"), VisibilityPolicy())
self.assertEqual("", engine._memory_domain_block_reason(MemoryRecord(memory_domain="user_memory")))
self.assertEqual("bot_self_domain", engine._memory_domain_block_reason(MemoryRecord(memory_domain="bot_self_legacy")))
self.assertEqual("mixed_domain", engine._memory_domain_block_reason(MemoryRecord(memory_domain="shared_or_mixed")))
self.assertEqual("missing_user_subject", engine._memory_domain_block_reason(MemoryRecord(memory_domain="unknown")))
async def test_migration_preview_apply_and_rollback_without_delete(self) -> None:
with tempfile.TemporaryDirectory() as temp:
store = MemoryStore(Path(temp) / "memory.db")
store.initialize()
await store.insert_memory(MemoryRecord(
id="legacy-user-1",
memory_type="user_fact",
subject=EntityRef(kind="user", id="u-1"),
scope="private",
session_id="private:u-1",
visibility="private_pair",
reality_level="real_user_fact",
content="旧用户事实",
evidence="用户说过",
))
migration = UserMemoryMigration(store)
preview = await migration.preview()
self.assertEqual(1, preview["counts"]["user_memory"])
applied = await migration.apply(migration_id="q5-test")
self.assertEqual(1, applied["changed"])
self.assertEqual(0, applied["deleted"])
rolled = await migration.rollback("q5-test")
self.assertEqual(1, rolled["restored"])
self.assertIsNotNone(await store.get_memory("legacy-user-1"))
store.close()
if __name__ == "__main__":
unittest.main()