From 9bb2bce595d6890ae3d790da9b895c7e59896d30 Mon Sep 17 00:00:00 2001 From: agent-bot Date: Mon, 27 Jul 2026 02:11:17 +0800 Subject: [PATCH] refactor: remove LivingMemory integration entirely MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 删除 core/migration_livingmemory.py 与全部 LivingMemory 代码: 迁移器、导入命令与页面路由、store 的 livingmemory 来源记录维护方法、 retrieval 的 livingmemory_graph 去重键与召回护栏、schema 的迁移配置段。 保留 core/operations.py 的插件冲突检测条目——那是对「若用户仍安装了 LivingMemory」的告警,删掉会让系统对该冲突失明。 线上记忆库确认 0 条 source_plugin='livingmemory' 记录,删除无数据影响。 测试 319 项通过。 Co-Authored-By: Claude Opus 5 --- _conf_schema.json | 23 +- core/commands.py | 73 ----- core/migration_livingmemory.py | 520 --------------------------------- core/retrieval.py | 49 +--- core/service.py | 13 +- core/store.py | 97 ------ main.py | 7 - page_api.py | 9 - 8 files changed, 5 insertions(+), 786 deletions(-) delete mode 100644 core/migration_livingmemory.py diff --git a/_conf_schema.json b/_conf_schema.json index e72baf7..d7d2c54 100644 --- a/_conf_schema.json +++ b/_conf_schema.json @@ -469,7 +469,7 @@ "items": { "recent_events_for_followup": { "description": "追问补全事件数", - "hint": "处理\u201c还有的吧\u201d\u201c刚才那个\u201d等连续追问时,最多读取多少条最近群聊事件来补全检索词。", + "hint": "处理“还有的吧”“刚才那个”等连续追问时,最多读取多少条最近群聊事件来补全检索词。", "type": "int", "default": 12 }, @@ -706,7 +706,7 @@ }, "prefer_memory_companion_memory": { "description": "MemoryCompanion 接管记忆上下文", - "hint": "开启后陪伴插件会尽量让出自我时间线、私聊上下文和旧 LivingMemory 指引,由 MemoryCompanion 统一注入。", + "hint": "开启后陪伴插件会尽量让出自我时间线与私聊上下文,由 MemoryCompanion 统一注入。", "type": "bool", "default": true }, @@ -823,25 +823,6 @@ } } }, - "livingmemory_migration": { - "description": "LivingMemory 导入", - "hint": "从旧 LivingMemory 数据库导入 documents 摘要。导入前建议预览并备份。", - "type": "object", - "items": { - "import_limit": { - "description": "单次导入上限", - "hint": "单次最多导入多少条 LivingMemory documents 完整记忆。0 表示不限制。", - "type": "int", - "default": 5000 - }, - "livingmemory_db_path": { - "description": "LivingMemory 数据库路径", - "hint": "留空则自动扫描常见位置。建议先执行 /mcomp import_livingmemory preview。", - "type": "string", - "default": "" - } - } - }, "maintenance": { "description": "维护", "hint": "控制数据保留和清理策略。", diff --git a/core/commands.py b/core/commands.py index beaf627..888b4db 100644 --- a/core/commands.py +++ b/core/commands.py @@ -408,68 +408,9 @@ class MemoryCompanionCommandHandler: ) return "用法:/mcomp sleep status|run" - async def import_livingmemory(self, mode: str = "preview", path: str = "") -> str: - return "LivingMemory 已按 Q5 用户记忆硬边界停用;请使用用户记忆合同或迁移预览接口。" - def _format_livingmemory_preview(self, report: dict[str, Any], *, verbose: bool = False) -> str: - if not report["candidates"]: - return ( - "没找到 LivingMemory 数据库。\n" - "可在配置页填写 livingmemory_db_path,或执行:\n" - "/mcomp import_livingmemory preview " - ) - first = report["candidates"][0] - if first.get("error"): - return f"LivingMemory 读取失败:{first.get('error')}" - importable_rows = sum(int(table.get("count") or 0) for table in first.get("tables", []) if table.get("importable")) - skipped_rows = sum(int(table.get("count") or 0) for table in first.get("tables", []) if not table.get("importable")) - lines = [ - "LivingMemory 预览:", - f"将导入 {importable_rows} 条完整记忆,跳过 {skipped_rows} 条碎片。", - f"来源:{first.get('path')}", - "执行:/mcomp import_livingmemory run", - ] - if verbose: - lines.insert(1, "策略:只导入 documents,跳过派生碎片。") - lines.append("表:") - for table in first.get("tables", [])[:10]: - lines.append(f"- {self._format_livingmemory_table(table)}") - if len(report["candidates"]) > 1: - lines.append("其他候选:") - for item in report["candidates"][1:5]: - summary = self._candidate_summary(item) - lines.append(f"- {item.get('path')}|{summary}") - else: - lines.append("详情:/mcomp import_livingmemory detail") - return "\n".join(lines) - - def _format_livingmemory_import_result(self, result: dict[str, Any]) -> str: - if result.get("reason") and not result.get("source_path"): - return ( - f"导入未执行:{result.get('reason')}\n" - "先试:/mcomp import_livingmemory preview" - ) - imported = int(result.get("imported") or 0) - skipped = int(result.get("skipped") or 0) - lines = [ - f"LivingMemory 导入完成:{imported} 条。", - f"来源:{result.get('source_path') or '-'}", - ] - if skipped: - lines.append(f"跳过异常行:{skipped} 条") - if imported == 0: - lines.append("没有新增内容,可能是旧库没有 documents 完整摘要。") - lines.append("可在记忆面板或 /mcomp search 中查看。") - return "\n".join(lines) - - def _format_livingmemory_table(self, table: dict[str, Any]) -> str: - name = table.get("name") - count = table.get("count", 0) - if table.get("importable"): - return f"{name}|{count} 行|将导入完整记忆" - return f"{name}|{count} 行|跳过:{self._livingmemory_note(table.get('note'))}" def _candidate_summary(self, item: dict[str, Any]) -> str: if item.get("error"): @@ -478,18 +419,4 @@ class MemoryCompanionCommandHandler: skipped_rows = sum(int(table.get("count") or 0) for table in item.get("tables", []) if not table.get("importable")) return f"可导入 {importable_rows},跳过 {skipped_rows}" - def _livingmemory_note(self, note: Any) -> str: - text = clean_text(note, 80) - mapping = { - "derived_fragment_not_imported": "派生事实碎片", - "not_importable": "没有可导入正文", - } - return mapping.get(text, text or "不是完整记忆") - def _livingmemory_usage(self) -> str: - return ( - "LivingMemory 迁移:\n" - "/mcomp import_livingmemory preview 预览\n" - "/mcomp import_livingmemory run 导入\n" - "/mcomp import_livingmemory detail 详情" - ) diff --git a/core/migration_livingmemory.py b/core/migration_livingmemory.py deleted file mode 100644 index 4695b56..0000000 --- a/core/migration_livingmemory.py +++ /dev/null @@ -1,520 +0,0 @@ -from __future__ import annotations - -import sqlite3 -import json -from datetime import datetime, timezone -from pathlib import Path -from typing import Any - -from .importance import ImportanceEvaluator -from .models import EntityRef, MemoryRecord, clean_text, new_id -from .store import MemoryStore - - -CONTENT_COLUMNS = ("text", "content", "summary", "memory", "canonical_summary", "persona_summary", "page_content") -SESSION_COLUMNS = ("session_id", "session", "origin", "unified_msg_origin") -PERSONA_COLUMNS = ("persona_id", "persona", "bot_id") -TIME_COLUMNS = ("created_at", "updated_at", "timestamp", "time", "last_accessed") -IMPORT_TABLE_PRIORITY = { - "documents": 0, -} -DERIVED_TABLES = {"memory_atoms", "graph_entries", "messages"} -SKIP_TABLES = { - "db_version", - "migration_status", - "memory_write_ops", - "graph_edges", - "graph_nodes", - "graph_entry_nodes", -} - - -class LivingMemoryMigrator: - def __init__(self, store: MemoryStore, plugin_root: Path, data_dir: Path): - self.store = store - self.plugin_root = Path(plugin_root) - self.data_dir = Path(data_dir) - self.importance = ImportanceEvaluator() - - def candidate_paths(self, configured_path: str = "") -> list[Path]: - candidates: list[Path] = [] - if configured_path: - candidates.append(Path(configured_path).expanduser()) - - plugins_dir = self.plugin_root.parent - data_parent = self.data_dir.parent - candidates.extend( - [ - plugins_dir / "astrbot_plugin_livingmemory" / "data" / "livingmemory.db", - plugins_dir / "astrbot_plugin_livingmemory" / "livingmemory.db", - data_parent / "astrbot_plugin_livingmemory" / "livingmemory.db", - data_parent / "astrbot_plugin_livingmemory" / "memory.db", - data_parent / "livingmemory.db", - ] - ) - - for base in [plugins_dir / "astrbot_plugin_livingmemory", data_parent / "astrbot_plugin_livingmemory"]: - if base.exists(): - candidates.extend(base.rglob("*.db")) - - unique: list[Path] = [] - seen: set[str] = set() - for path in candidates: - try: - key = str(path.resolve()) - except Exception: - key = str(path) - if key not in seen: - seen.add(key) - unique.append(path) - return unique - - def preview(self, configured_path: str = "") -> dict[str, Any]: - paths = self.candidate_paths(configured_path) - reports = [] - for path in paths: - if not path.exists() or not path.is_file(): - continue - report = self._inspect_db(path) - if report: - reports.append(report) - return {"candidates": reports, "count": len(reports)} - - async def import_data( - self, - *, - configured_path: str = "", - default_review_status: str = "auto", - limit: int = 5000, - ) -> dict[str, Any]: - preview = self.preview(configured_path) - if not preview["candidates"]: - return {"imported": 0, "skipped": 0, "reason": "未找到可用 LivingMemory 数据库"} - - source = preview["candidates"][0] - path = Path(source["path"]) - batch_id = await self.store.add_import_batch( - source_plugin="livingmemory", - source_path=str(path), - mode="import", - stats={"tables": source.get("tables", [])}, - ) - imported = 0 - skipped = 0 - importable_tables: list[dict[str, Any]] = [] - skipped_tables: list[dict[str, Any]] = [] - for table in source.get("tables", []): - if not table.get("importable"): - skipped_tables.append( - { - "name": table.get("name"), - "count": table.get("count", 0), - "note": table.get("note") or "not_importable", - } - ) - continue - before_table = imported - remaining = max(0, limit - imported) if limit > 0 else int(table.get("count") or 0) - rows = self._read_rows(path, table["name"], table["columns"], limit=remaining) - for row in rows: - record = self._row_to_record(row, table["columns"], table["name"], batch_id, default_review_status) - if not record: - skipped += 1 - continue - await self.store.insert_memory(record) - imported += 1 - if limit > 0 and imported >= limit: - break - if limit > 0 and imported >= limit: - importable_tables.append( - { - "name": table.get("name"), - "count": table.get("count", 0), - "imported": imported - before_table, - } - ) - break - importable_tables.append( - { - "name": table.get("name"), - "count": table.get("count", 0), - "imported": imported - before_table, - } - ) - return { - "imported": imported, - "skipped": skipped, - "batch_id": batch_id, - "source_path": str(path), - "strategy": "只导入 documents 完整摘要,跳过 graph_entries/memory_atoms/messages 等派生碎片。", - "default_review_status": default_review_status, - "import_limit": limit, - "importable_tables": importable_tables, - "skipped_tables": skipped_tables, - } - - async def repair_imported_content(self, configured_path: str = "") -> dict[str, Any]: - preview = self.preview(configured_path) - if not preview["candidates"]: - return {"updated": 0, "skipped": 0, "reason": "未找到可用 LivingMemory 数据库"} - path = Path(preview["candidates"][0]["path"]) - candidates = await self.store.list_livingmemory_content_repair_candidates() - if not candidates: - return {"updated": 0, "skipped": 0, "source_path": str(path)} - - updated = 0 - skipped = 0 - try: - conn = sqlite3.connect(str(path)) - conn.row_factory = sqlite3.Row - for item in candidates: - payload = self._repair_payload(conn, item) - if not payload: - skipped += 1 - continue - if await self.store.update_livingmemory_import_payload(item["id"], payload): - updated += 1 - else: - skipped += 1 - conn.close() - except Exception as exc: - return {"updated": updated, "skipped": skipped, "source_path": str(path), "error": str(exc)} - return {"updated": updated, "skipped": skipped, "source_path": str(path)} - - def _inspect_db(self, path: Path) -> dict[str, Any] | None: - try: - conn = sqlite3.connect(str(path)) - conn.row_factory = sqlite3.Row - rows = conn.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name").fetchall() - tables = [] - for row in rows: - table = row["name"] - table_key = table.lower() - if table.startswith("sqlite_") or "_fts" in table_key or table_key in SKIP_TABLES: - continue - columns = [item["name"] for item in conn.execute(f"PRAGMA table_info({self._quote(table)})").fetchall()] - count = conn.execute(f"SELECT COUNT(*) FROM {self._quote(table)}").fetchone()[0] - content_col = self._pick_content_column(columns) - is_derived = table_key in DERIVED_TABLES - tables.append( - { - "name": table, - "columns": columns, - "count": count, - "content_column": content_col, - "session_column": self._pick_exact(columns, SESSION_COLUMNS), - "persona_column": self._pick(columns, PERSONA_COLUMNS), - "time_column": self._pick_exact(columns, TIME_COLUMNS), - "importable": bool(content_col and count > 0 and not is_derived), - "note": "derived_fragment_not_imported" if is_derived else "", - } - ) - conn.close() - if not tables: - return None - tables.sort(key=lambda item: (IMPORT_TABLE_PRIORITY.get(str(item.get("name") or "").lower(), 50), str(item.get("name") or ""))) - return {"path": str(path), "tables": tables} - except Exception as exc: - return {"path": str(path), "error": str(exc), "tables": []} - - def _read_rows(self, path: Path, table: str, columns: list[str], limit: int) -> list[sqlite3.Row]: - if limit <= 0: - return [] - try: - conn = sqlite3.connect(str(path)) - conn.row_factory = sqlite3.Row - rows = conn.execute(f"SELECT * FROM {self._quote(table)} LIMIT ?", (limit,)).fetchall() - conn.close() - return rows - except Exception: - return [] - - def _row_to_record( - self, - row: sqlite3.Row, - columns: list[str], - table_name: str, - batch_id: str, - default_review_status: str, - ) -> MemoryRecord | None: - table_key = table_name.lower() - if table_key in SKIP_TABLES: - return None - if table_key == "memory_atoms": - status = clean_text(self._row_get(row, "status"), 40).lower() - if status in {"expired", "deleted", "archived", "disabled"}: - return None - - content_col = self._pick_content_column(columns) - if not content_col: - return None - content = clean_text(row[content_col], 3000) - if not content: - return None - - source_metadata = self._loads(self._row_get(row, "metadata"), {}) - if not isinstance(source_metadata, dict): - source_metadata = {} - session_col = self._pick_exact(columns, SESSION_COLUMNS) - session_id = clean_text(row[session_col], 200) if session_col else "" - session_id = clean_text(session_id or source_metadata.get("session_id"), 200) - scope, target = self._scope_from_session(session_id) - group_id = target if scope == "group" else "" - object_ref = EntityRef(kind="group" if scope == "group" else "user", id=target, role="imported_target") - visibility = "group_public" if scope == "group" else ("private_pair" if scope == "private" else "internal") - occurred_col = self._pick_exact(columns, TIME_COLUMNS) - occurred_at = self._normalize_time(row[occurred_col]) if occurred_col else "" - - memory_type = self._memory_type_for_table(table_key, row) - importance = self._float(self._row_get(row, "importance"), self._float(source_metadata.get("importance"), 0.35)) - confidence = self._float(self._row_get(row, "confidence"), self._float(source_metadata.get("confidence"), 0.55)) - tags = ["livingmemory_import", table_name] - atom_type = clean_text(self._row_get(row, "atom_type"), 60) - if atom_type: - tags.append(atom_type) - entry_type = clean_text(self._row_get(row, "entry_type"), 60) - if entry_type: - tags.append(entry_type) - status = clean_text(self._row_get(row, "status"), 60) - if status: - tags.append(f"status:{status}") - metadata = { - "source_table": table_name, - "source_row_id": self._row_get(row, "id"), - "source_doc_id": self._row_get(row, "doc_id"), - "source_memory_id": self._row_get(row, "source_memory_id") or self._row_get(row, "parent_memory_id"), - "persona_id": clean_text(self._row_get(row, "persona_id") or source_metadata.get("persona_id"), 120), - "livingmemory_metadata": source_metadata, - } - metadata.update(self._promoted_metadata(source_metadata)) - metadata = {key: value for key, value in metadata.items() if value not in ("", None, {}, [])} - - record = MemoryRecord( - id=new_id("imported"), - memory_type=memory_type, - subject=EntityRef(kind="unknown", id="", role="imported_subject"), - object=object_ref, - scope=scope, - session_id=session_id, - platform=session_id.split(":", 1)[0] if ":" in session_id else "", - group_id=group_id, - visibility=visibility, - sayability="indirect", - reality_level="imported_summary", - lifecycle="stable_memory", - content=content, - evidence=content, - confidence=confidence, - importance=importance, - review_status=default_review_status if default_review_status in {"pending", "auto"} else "auto", - tags=tags, - metadata=metadata, - occurred_at=occurred_at, - source_plugin="livingmemory", - import_batch_id=batch_id, - ) - return self.importance.calibrate(record, source="livingmemory_import") - - def _memory_type_for_table(self, table_name: str, row: sqlite3.Row) -> str: - if table_name == "memory_atoms": - atom_type = clean_text(self._row_get(row, "atom_type"), 60) - return f"livingmemory_atom:{atom_type}" if atom_type else "livingmemory_atom" - if table_name == "graph_entries": - entry_type = clean_text(self._row_get(row, "entry_type"), 60) - return f"livingmemory_graph:{entry_type}" if entry_type else "livingmemory_graph" - if table_name == "documents": - return "conversation_summary" - if table_name == "messages": - return "conversation_event" - return "imported_memory" - - def _promoted_metadata(self, source_metadata: dict[str, Any]) -> dict[str, Any]: - promoted: dict[str, Any] = {} - for key in ("canonical_summary", "persona_summary", "sentiment", "interaction_type", "summary_quality"): - value = clean_text(source_metadata.get(key), 1200 if key.endswith("summary") else 80) - if value: - promoted[key] = value - for key, limit, item_limit in ( - ("topics", 8, 80), - ("key_facts", 10, 180), - ("participants", 12, 80), - ): - value = source_metadata.get(key) - if isinstance(value, list): - items = [clean_text(item, item_limit) for item in value if clean_text(item, item_limit)] - if items: - promoted[key] = items[:limit] - source_window = source_metadata.get("source_window") - if isinstance(source_window, dict): - promoted["source_window"] = source_window - return promoted - - def _pick(self, columns: list[str], names: tuple[str, ...]) -> str: - return self._pick_exact(columns, names) - - def _pick_exact(self, columns: list[str], names: tuple[str, ...]) -> str: - lowered = {column.lower(): column for column in columns} - for name in names: - if name in lowered: - return lowered[name] - return "" - - def _pick_content_column(self, columns: list[str]) -> str: - exact = self._pick_exact(columns, CONTENT_COLUMNS) - if exact: - return exact - safe_tokens = {"summary", "memory", "text", "content"} - for column in columns: - lower = column.lower() - if lower in {"id", "doc_id", "edge_key", "node_key", "metadata", "status", "memory_id", "parent_memory_id", "source_memory_id"}: - continue - if any(token in lower for token in safe_tokens): - return column - return "" - - def _repair_payload(self, conn: sqlite3.Connection, item: dict[str, Any]) -> dict[str, Any] | None: - metadata = self._loads(item.get("metadata"), {}) - source_table = clean_text(metadata.get("source_table"), 80) - source_row_id = self._int(item.get("content")) - if not source_table or source_row_id <= 0: - return None - - if source_table == "graph_edges": - row = conn.execute("SELECT * FROM graph_edges WHERE id=?", (source_row_id,)).fetchone() - if not row: - return self._document_repair_payload(conn, source_row_id, metadata) - edge_meta = self._loads(row["metadata"], {}) - text = clean_text(edge_meta.get("summary"), 4000) - if not text: - return None - doc_meta: dict[str, Any] = {} - doc_row = None - if row["source_memory_id"]: - doc_row = conn.execute("SELECT * FROM documents WHERE id=?", (row["source_memory_id"],)).fetchone() - if doc_row: - doc_meta = self._loads(doc_row["metadata"], {}) - session_id = clean_text(doc_meta.get("session_id"), 200) - scope, target = self._scope_from_session(session_id) - group_id = target if scope == "group" else "" - metadata.update( - { - "source_table": source_table, - "source_row_id": source_row_id, - "source_memory_id": row["source_memory_id"], - "source_relation_type": row["relation_type"], - "source_edge_key": row["edge_key"], - "repaired_from_numeric_content": True, - } - ) - return { - "content": text, - "evidence": text, - "metadata": metadata, - "scope": scope, - "session_id": session_id, - "group_id": group_id, - "visibility": "group_public" if scope == "group" else ("private_pair" if scope == "private" else "internal"), - "object_kind": "group" if scope == "group" else "user", - "object_id": target, - "object_role": "imported_target", - "occurred_at": clean_text((doc_row["created_at"] if doc_row else "") or row["created_at"], 80), - } - - if source_table == "documents": - return self._document_repair_payload(conn, source_row_id, metadata) - return None - - def _document_repair_payload( - self, - conn: sqlite3.Connection, - source_row_id: int, - metadata: dict[str, Any], - ) -> dict[str, Any] | None: - row = conn.execute("SELECT * FROM documents WHERE id=?", (source_row_id,)).fetchone() - if not row: - return None - text = clean_text(row["text"], 4000) - if not text: - return None - doc_meta = self._loads(row["metadata"], {}) - session_id = clean_text(doc_meta.get("session_id"), 200) - scope, target = self._scope_from_session(session_id) - group_id = target if scope == "group" else "" - metadata.update( - { - "source_table": "documents", - "source_row_id": source_row_id, - "source_doc_id": row["doc_id"], - "repaired_from_numeric_content": True, - } - ) - return { - "content": text, - "evidence": text, - "metadata": metadata, - "scope": scope, - "session_id": session_id, - "group_id": group_id, - "visibility": "group_public" if scope == "group" else ("private_pair" if scope == "private" else "internal"), - "object_kind": "group" if scope == "group" else "user", - "object_id": target, - "object_role": "imported_target", - "occurred_at": clean_text(row["created_at"], 80), - } - - def _loads(self, value: Any, fallback: Any) -> Any: - if isinstance(value, dict): - return value - try: - return json.loads(value or "") - except Exception: - return fallback - - def _int(self, value: Any) -> int: - try: - return int(str(value).strip()) - except Exception: - return 0 - - def _float(self, value: Any, default: float) -> float: - try: - return float(value) - except Exception: - return default - - def _row_get(self, row: sqlite3.Row, key: str, default: Any = "") -> Any: - try: - if key in row.keys(): - return row[key] - except Exception: - pass - return default - - def _normalize_time(self, value: Any) -> str: - if value in (None, ""): - return "" - if isinstance(value, (int, float)): - try: - return datetime.fromtimestamp(float(value), tz=timezone.utc).isoformat() - except Exception: - return clean_text(value, 80) - text = clean_text(value, 80) - try: - numeric = float(text) - if numeric > 1000000000: - return datetime.fromtimestamp(numeric, tz=timezone.utc).isoformat() - except Exception: - pass - return text - - def _scope_from_session(self, session_id: str) -> tuple[str, str]: - if ":GroupMessage:" in session_id: - return "group", session_id.rsplit(":GroupMessage:", 1)[-1] - if ":FriendMessage:" in session_id: - return "private", session_id.rsplit(":FriendMessage:", 1)[-1] - if ":PrivateMessage:" in session_id: - return "private", session_id.rsplit(":PrivateMessage:", 1)[-1] - return "unknown", "" - - def _quote(self, name: str) -> str: - return '"' + name.replace('"', '""') + '"' diff --git a/core/retrieval.py b/core/retrieval.py index b3c9839..b82205c 100644 --- a/core/retrieval.py +++ b/core/retrieval.py @@ -639,11 +639,8 @@ class RetrievalEngine: ) -> tuple[list[SearchResult], list[dict[str, str]]]: if len(results) < 2: return results, [] + # 原先只按 livingmemory_graph 的来源做跨条去重;该来源已随 LivingMemory 一并移除。 groups: dict[tuple[str, str, str], list[tuple[int, SearchResult]]] = {} - for index, item in enumerate(results): - key = self._redundant_source_key(item.memory) - if key: - groups.setdefault(key, []).append((index, item)) drop_reasons: dict[str, str] = {} for key, entries in groups.items(): if len(entries) < 2: @@ -672,26 +669,6 @@ class RetrievalEngine: ] return collapsed, blocked - def _redundant_source_key(self, memory: MemoryRecord) -> tuple[str, str, str] | None: - source_plugin = clean_text(memory.source_plugin, 80).lower() - memory_type = clean_text(memory.memory_type, 120).lower() - if source_plugin != "livingmemory" or not memory_type.startswith("livingmemory_graph:"): - return None - metadata = memory.metadata if isinstance(memory.metadata, dict) else {} - source_memory_id = clean_text(metadata.get("source_memory_id"), 120) - nested = metadata.get("livingmemory_metadata") - if not source_memory_id and isinstance(nested, dict): - source_memory_id = clean_text(nested.get("source_memory_id"), 120) - if not source_memory_id: - return None - persona_id = clean_text(metadata.get("persona_id"), 120) - if not persona_id and isinstance(nested, dict): - persona_id = clean_text(nested.get("persona_id"), 120) - session_id = clean_text(memory.session_id, 200) - if not session_id and isinstance(nested, dict): - session_id = clean_text(nested.get("session_id"), 200) - scope_key = f"{memory.scope}:{session_id or memory.group_id}:{persona_id}" - return ("livingmemory_graph_source", source_memory_id, scope_key) def _collapse_mutable_fact_results( self, @@ -2227,9 +2204,6 @@ class RetrievalEngine: vector_relevant = vector_score >= self.embedding_score_threshold if self.embedding_enabled else False if terms and not exact_hit and term_hits < min_hits and not vector_relevant: return 0.0, f"keyword_hit_too_weak hits={term_hits}/{min_hits}" - graph_guard_reason = self._livingmemory_graph_relevance_guard(memory, term_hits, exact_hit, min_hits) - if graph_guard_reason and vector_score < min(0.98, self.embedding_score_threshold + 0.12): - return 0.0, graph_guard_reason bm25 = 0.0 if terms: for term in terms: @@ -2340,28 +2314,7 @@ class RetrievalEngine: bonus += min(0.040, max(freshness, emotional) * 0.040) return min(0.18, bonus) - def _livingmemory_graph_relevance_guard( - self, - memory: MemoryRecord, - term_hits: int, - exact_hit: bool, - min_hits: int, - ) -> str: - if not self._is_livingmemory_graph_memory(memory) or exact_hit: - return "" - required_hits = max(3, min_hits + 1) - age_days = self._age_days(memory.occurred_at or memory.created_at) - if age_days is not None and age_days >= 30: - required_hits = max(required_hits, 4) - if term_hits < required_hits: - return f"livingmemory_graph_hit_too_weak hits={term_hits}/{required_hits};age_days={age_days:.1f}" if age_days is not None else f"livingmemory_graph_hit_too_weak hits={term_hits}/{required_hits}" - return "" - def _is_livingmemory_graph_memory(self, memory: MemoryRecord) -> bool: - return ( - clean_text(memory.source_plugin, 80).lower() == "livingmemory" - and clean_text(memory.memory_type, 120).lower().startswith("livingmemory_graph:") - ) def _recency_bonus(self, iso_text: str) -> float: days = self._age_days(iso_text) diff --git a/core/service.py b/core/service.py index bb56397..da07e3b 100644 --- a/core/service.py +++ b/core/service.py @@ -67,7 +67,6 @@ from .injection import ( MEMORY_COMPANION_INJECTION_HEADER, InjectionComposer, ) -from .migration_livingmemory import LivingMemoryMigrator from .models import EntityRef, MemoryRecord, SearchResult, SessionContext, clean_text, json_dumps, json_loads, utc_now from .operations import ( PRESET_LABELS, @@ -192,7 +191,6 @@ class MemoryCompanionService: self._retrieval_result_cache_stats: dict[str, int] = {"hits": 0, "misses": 0, "evictions": 0} 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) @@ -3882,13 +3880,6 @@ class MemoryCompanionService: ], } - async def import_livingmemory(self, *, configured_path: str = "") -> dict[str, Any]: - 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) @@ -6712,7 +6703,7 @@ class MemoryCompanionService: if not self.config.bool("private_companion_bridge.dedupe_prompt_context", True): return False normalized = clean_text(section, 80) - if normalized in {"self_timeline", "private_context", "livingmemory_guidance"}: + if normalized in {"self_timeline", "private_context"}: return self.config.bool("private_companion_bridge.prefer_memory_companion_memory", True) if normalized in {"companion_memory", "dialogue_history"}: return self.config.bool("private_companion_bridge.prefer_memory_companion_memory", True) @@ -6800,7 +6791,7 @@ class MemoryCompanionService: drop("self_timeline", "companion_context_detected:self_timeline_slot_suppressed") if self.config.bool("private_companion_bridge.suppress_user_context_when_companion_seen", True): private_context_deferred = bool( - {"private_context", "companion_memory", "dialogue_history", "livingmemory_guidance"} & deferred_sections + {"private_context", "companion_memory", "dialogue_history"} & deferred_sections ) if ( companion_state.get("has_state") diff --git a/core/store.py b/core/store.py index 216f92c..7a014fa 100644 --- a/core/store.py +++ b/core/store.py @@ -4779,108 +4779,11 @@ class MemoryStore: self._conn.commit() return cur.rowcount > 0 - async def approve_livingmemory_imports(self) -> dict[str, Any]: - return await asyncio.to_thread(self._approve_livingmemory_imports_sync) - def _approve_livingmemory_imports_sync(self) -> dict[str, Any]: - now = utc_now() - with self._lock: - rows = self._conn.execute( - """ - SELECT id - FROM memories - WHERE source_plugin='livingmemory' AND review_status='pending' - """ - ).fetchall() - ids = [row["id"] for row in rows] - if not ids: - return {"updated": 0, "review_queue_updated": 0} - placeholders = ",".join("?" for _ in ids) - memory_cur = self._conn.execute( - f""" - UPDATE memories - SET review_status='auto', - lifecycle='stable_memory', - updated_at=? - WHERE id IN ({placeholders}) - """, - [now] + ids, - ) - queue_cur = self._conn.execute( - f""" - UPDATE review_queue - SET status='auto', - updated_at=? - WHERE memory_id IN ({placeholders}) AND status='pending' - """, - [now] + ids, - ) - self._conn.commit() - return { - "updated": int(memory_cur.rowcount or 0), - "review_queue_updated": int(queue_cur.rowcount or 0), - } - async def list_livingmemory_content_repair_candidates(self) -> list[dict[str, Any]]: - return await asyncio.to_thread(self._list_livingmemory_content_repair_candidates_sync) - def _list_livingmemory_content_repair_candidates_sync(self) -> list[dict[str, Any]]: - with self._lock: - rows = self._conn.execute( - """ - SELECT id, content, evidence, metadata - FROM memories - WHERE source_plugin='livingmemory' - """ - ).fetchall() - candidates = [] - for row in rows: - content = clean_text(row["content"], 80) - if content.isdigit(): - candidates.append(dict(row)) - return candidates - async def update_livingmemory_import_payload(self, memory_id: str, payload: dict[str, Any]) -> bool: - return await asyncio.to_thread(self._update_livingmemory_import_payload_sync, memory_id, payload) - def _update_livingmemory_import_payload_sync(self, memory_id: str, payload: dict[str, Any]) -> bool: - with self._lock: - cur = self._conn.execute( - """ - UPDATE memories - SET content=?, - evidence=?, - metadata=?, - scope=?, - session_id=?, - group_id=?, - visibility=?, - object_kind=?, - object_id=?, - object_role=?, - occurred_at=COALESCE(NULLIF(?, ''), occurred_at), - content_fingerprint='', - updated_at=? - WHERE id=? AND source_plugin='livingmemory' - """, - ( - clean_text(payload.get("content"), 4000), - clean_text(payload.get("evidence"), 4000), - json_dumps(payload.get("metadata") or {}), - clean_text(payload.get("scope"), 40), - clean_text(payload.get("session_id"), 200), - clean_text(payload.get("group_id"), 120), - clean_text(payload.get("visibility"), 40), - clean_text(payload.get("object_kind"), 40), - clean_text(payload.get("object_id"), 120), - clean_text(payload.get("object_role"), 80), - clean_text(payload.get("occurred_at"), 80), - utc_now(), - clean_text(memory_id, 120), - ), - ) - self._conn.commit() - return cur.rowcount > 0 async def update_memory_visibility(self, memory_id: str, visibility: str) -> bool: return await asyncio.to_thread(self._update_memory_visibility_sync, memory_id, visibility) diff --git a/main.py b/main.py index c8eb9a7..f4f5e2b 100644 --- a/main.py +++ b/main.py @@ -320,13 +320,6 @@ class MemoryCompanionPlugin(Star): ) -> AsyncGenerator[MessageEventResult, None]: yield event.plain_result(await self.commands.sleep(action)) - @permission_type(PermissionType.ADMIN) - @mcomp.command("import_livingmemory", priority=10) - async def cmd_mcomp_import_livingmemory( - self, event: AstrMessageEvent, mode: str = "preview", path: str = "" - ) -> AsyncGenerator[MessageEventResult, None]: - yield event.plain_result(await self.commands.import_livingmemory(mode, path)) - @permission_type(PermissionType.ADMIN) @mcomp.command("help", priority=10) async def cmd_mcomp_help(self, event: AstrMessageEvent) -> AsyncGenerator[MessageEventResult, None]: diff --git a/page_api.py b/page_api.py index 78d1a22..f867efc 100644 --- a/page_api.py +++ b/page_api.py @@ -115,11 +115,8 @@ class PluginPageApi: ("/companion/personal-photo-data", self.companion_personal_photo_data, ["GET"], "MemoryCompanion Page companion personal photo data"), ("/maintenance", self.maintenance, ["POST"], "MemoryCompanion Page maintenance"), ("/maintenance/sleep", self.sleep_maintenance, ["GET", "POST"], "MemoryCompanion Page sleep maintenance"), - ("/maintenance/repair_livingmemory_content", self.repair_livingmemory_content, ["POST"], "MemoryCompanion Page repair LivingMemory content"), ("/maintenance/clear_all", self.clear_all, ["POST"], "MemoryCompanion Page clear all memory data"), ("/maintenance/clear_scope", self.clear_scope, ["POST"], "MemoryCompanion Page clear scoped memory data"), - ("/import/livingmemory/preview", self.import_preview, ["GET"], "MemoryCompanion Page import preview"), - ("/import/livingmemory/run", self.import_run, ["POST"], "MemoryCompanion Page import run"), ("/persona-state", self.persona_state, ["GET"], "MemoryCompanion Page persona state"), ("/acl/matrix", self.acl_matrix, ["GET"], "MemoryCompanion Page ACL matrix"), # M6.1.6:Bot 长期归档只读视图。只读、只出 Bot 域,写入仍走受控归档路径。 @@ -2203,8 +2200,6 @@ class PluginPageApi: result = self.plugin.service.sleep_status() return self._ok({"result": result}) - async def repair_livingmemory_content(self): - return self._err("livingmemory_disabled_by_user_memory_contract", 410) async def clear_all(self): payload = await self._json() @@ -2238,11 +2233,7 @@ class PluginPageApi: return self._err(str(exc), 400) return self._ok({"result": result}) - async def import_preview(self): - return self._err("livingmemory_disabled_by_user_memory_contract", 410) - async def import_run(self): - return self._err("livingmemory_disabled_by_user_memory_contract", 410) @staticmethod def _ok(data: dict[str, Any] | None = None):