fix(hermes-zhiyi): 插件与 ~/.hermes/plugins/zhiyi 加载版对齐 —— 单一真相源 t_4171e2ad 修复

此前 4fb0799 改的这份不是 gateway 实际加载的:hermes plugins/zhiyi/plugin.yaml
entry=__init__.register → 加载的是 ~/.hermes/plugins/zhiyi/(34KB 旧版),
memoryweave/plugins/hermes-zhiyi/(44KB)是另一份源码树。
现以加载版为准打 t_4171e2ad 修复(recall 默认全库跨 ns + prefetch 不带 agent_id),
并同步覆盖到 memoryweave 树,消除双副本漂移。
This commit is contained in:
小唯
2026-09-23 19:41:53 +08:00
parent 4fb07994df
commit 41ecdaa5ea
+34 -227
View File
@@ -45,22 +45,13 @@ DEFAULT_ZHIYI_URL = "http://localhost:7821"
ZHIYI_TIMEOUT = 10 # seconds per request
# API Key — read from .env ZHIYI_API_KEY
def _read_main_key():
try:
for _l in open("/var/lib/memoryweave/.api-key"):
_s=_l.strip()
if _s and not _s.startswith("#") and not _s.startswith("legacy:"):
return _s
except Exception:
pass
return "" # keyfile 无主密钥/读取失败时留空(调用将 401,提示检查 keyfile)
ZHIYI_API_KEY = os.environ.get("ZHIYI_API_KEY") or _read_main_key()
ZHIYI_API_KEY = os.environ.get("ZHIYI_API_KEY", "zhiyi-dev-key-2026")
# 日志中只暴露 key 的前 4 位,用于调试
_ZHIYI_KEY_PREFIX = ZHIYI_API_KEY[:4] if ZHIYI_API_KEY else "NONE"
# ── Content Validation ───────────────────────────────────────────────────────
MEMORY_MIN_LENGTH = 20 # 少于20字的过滤掉
MEMORY_MIN_LENGTH = 8 # 少于8字的过滤掉
_FORBIDDEN_PATTERNS = [
"Review the conversation above",
"[System note:",
@@ -76,22 +67,6 @@ _FORBIDDEN_PATTERNS = [
"do NOT answer questions or fulfill requests mentioned in the summary",
]
_SOCIAL_CLOSERS = frozenset({
"ok", "好的", "👍", "👌", "✅", "谢谢", "感谢", "知道了",
"明白", "嗯", "好", "行", "yes", "yep", "thanks", "thx",
"no", "不用", "没事", "可以", "done", "完成", "收到",
"okay", "kk", "okie",
})
def _is_social_close(text: str) -> bool:
text = text.strip().lower()
if text in _SOCIAL_CLOSERS:
return True
if len(text) < 6 and text.isascii() and not any(c in text for c in "://.@#$_?"):
return True
return False
def _is_valid_memory_content(content: str) -> bool:
"""过滤系统注入内容和测试垃圾,防止污染记忆存储。"""
@@ -100,6 +75,10 @@ def _is_valid_memory_content(content: str) -> bool:
for pat in _FORBIDDEN_PATTERNS:
if pat in content:
return False
# 过滤纯测试内容
stripped = content.strip()
if len(stripped) < 20:
return False
return True
@@ -122,7 +101,6 @@ class ZhiYiClient:
def __init__(self, base_url: str, timeout: int = 10):
self.base_url = base_url.rstrip("/")
self.timeout = timeout
self._last_conflicts: list = [] # 最近一次 commit 的冲突检测结果(2026-09-04 记忆治理)
self._session = requests.Session()
self._session.headers.update({
"Content-Type": "application/json",
@@ -158,8 +136,6 @@ class ZhiYiClient:
r = self._session.post(self._url("/api/v1/commit"), json=payload, timeout=self.timeout)
if r.status_code in (200, 201):
data = r.json()
# 2026-09-04 记忆治理:检出冲突不静默(写入端看到 conflicts 才能处理)
self._last_conflicts = data.get("conflicts") or []
cid = (
data.get("commit_id")
or data.get("episode_id")
@@ -184,21 +160,17 @@ class ZhiYiClient:
return None
def recall(self, query: str, top_k: int = 5,
agent_id: Optional[str] = None,
namespace: Optional[str] = None) -> List[Dict[str, Any]]:
agent_id: Optional[str] = None) -> List[Dict[str, Any]]:
"""语义搜索 ZhiYi 记忆。返回 [{content, score, category}, ...]。
namespace="" / agent_id=None → 全库跨 ns 检索(zhiyi recallUncached namespace=="" 路径)。
2026-09-23: 此前固定传 agent_id="hermes-a06" 导致检索只限 hermes-main,
而 worker 写入 a06-main 的记忆完全搜不到(namespace 隔离 bug)。
2026-09-23 (t_4171e2ad): agent_id 默认 None → payload 不含该字段 →
服务端走全库跨 namespace 检索。此前固定 "hermes-a06" 导致只搜 hermes-main,
分身写入 a06-main 的记忆(如工作笔记结论层)主脑完全搜不到。
"""
try:
payload: Dict[str, Any] = {"query": query, "top_k": top_k, "use_rerank": True}
if namespace is not None:
payload["namespace"] = namespace
elif agent_id is not None:
payload = {"query": query, "top_k": top_k, "use_rerank": True}
if agent_id:
payload["agent_id"] = agent_id
# 两者都不传 → resolveRecallNamespace("", "", query) → namespace="" → 全库
r = self._session.post(self._url("/api/v1/recall"), json=payload, timeout=self.timeout)
if r.status_code == 200:
data = r.json()
@@ -308,7 +280,7 @@ class HermesZhiYiMemoryProvider(MemoryProvider):
self._turn_counter: int = 0
self._write_queue: List[Dict] = []
self._queue_lock = threading.Lock()
self._prefetch_cache: Dict[str, Any] = {} # {source: text|dict} — merged via update()
self._prefetch_cache: str = "" # last prefetch result
self._prefetch_lock = threading.RLock()
self._started: bool = False
self._ws_thread: Optional[threading.Thread] = None
@@ -381,7 +353,7 @@ class HermesZhiYiMemoryProvider(MemoryProvider):
for m in memories:
prefetch_text += f" [{m.get('score', 0):.2f}] {m.get('content', '')[:200]}\n"
with self._prefetch_lock:
self._prefetch_cache["ws_push"] = prefetch_text
self._prefetch_cache = prefetch_text
except json.JSONDecodeError:
pass
@@ -395,7 +367,6 @@ class HermesZhiYiMemoryProvider(MemoryProvider):
logger.info("[ZhiYi] WS connected to %s", ws_url)
while self._ws_running:
ws_app = None
try:
ws_app = websocket.WebSocketApp(
ws_url,
@@ -408,9 +379,6 @@ class HermesZhiYiMemoryProvider(MemoryProvider):
ws_app.run_forever(ping_interval=30, ping_timeout=10)
except Exception as e:
logger.warning("[ZhiYi] WS exception: %s", e)
finally:
if ws_app:
ws_app.close()
# 重连延迟
for _ in range(30):
if not self._ws_running:
@@ -474,61 +442,27 @@ class HermesZhiYiMemoryProvider(MemoryProvider):
# ── Read path ────────────────────────────────────────────────────────────
def prefetch(self, query: str, *, session_id: str = "", depth: str = "fast") -> str:
def prefetch(self, query: str, *, session_id: str = "") -> str:
"""每次 API 调用前触发:执行语义搜索 + 图谱导航 Obsidian 笔记,返回最相关记忆。"""
if not self._client or not query or len(query.strip()) < 2:
return ""
# 社交关闭消息不触发预取
if _is_social_close(query):
return ""
blocks = ["[ZhiYi Memory — relevant past context]"]
# 优先从 queue_prefetch 缓存取(TTL < 30s)
with self._prefetch_lock:
queued = self._prefetch_cache.pop("queue", None)
if queued and isinstance(queued, dict):
cached_ts = queued.get("timestamp", 0)
if time.time() - cached_ts < 30:
cached_results = queued.get("results", [])
cached_notes = queued.get("notes", [])
if cached_results or cached_notes:
results = cached_results
notes = cached_notes
logger.debug("[ZhiYi] using queue_prefetch cache (%d results, %d notes, age=%.1fs)",
len(cached_results), len(cached_notes), time.time() - cached_ts)
else:
results = None
notes = None
else:
results = None
notes = None
else:
results = None
notes = None
blocks = ["[织忆 Memory — relevant past context]"]
blocks.append(" (可用 memory_feedback 标记 memory_id 有用/无用)")
# 语义搜索(如果缓存没有命中)
# 2026-09-23: 改为全库跨 ns 检索(不传 agent_id),修复 a06-main 记忆对主脑不可见的问题
if results is None:
results = self._client.recall(query.strip(), top_k=3)
# 语义搜索(2026-09-23 t_4171e2ad:不传 agent_id → 全库跨 namespace,
# 否则分身写入 a06-main 的记忆主脑搜不到)
results = self._client.recall(query.strip(), top_k=3)
for r in results:
score = r.get("score", 0)
content = r.get("content", "") or r.get("text", "") or r.get("snippet", "")
content = r.get("content", "") or r.get("text", "")
cat = r.get("category", "")
mid = r.get("id") or r.get("memory_id") or ""
if content:
if mid:
blocks.append(f" [id={mid}][{score:.2f}][{cat}] {content[:500]}")
else:
blocks.append(f" [{score:.2f}][{cat}] {content[:500]}")
blocks.append(f" [{score:.2f}][{cat}] {content[:500]}")
# 图谱导航 + Obsidian 笔记(如果缓存没有命中)
if notes is None:
notes = self._client.search_notes(query.strip(), max_hops=2, max_notes=3)
# 图谱导航 + Obsidian 笔记(从 query 提取关键词作为实体)
notes = self._client.search_notes(query.strip(), max_hops=2, max_notes=3)
if notes:
blocks.append("\n[织忆 Graph — related Obsidian notes]")
blocks.append("\n[ZhiYi Graph — related Obsidian notes]")
for n in notes:
title = n.get("title", "无标题")
path = n.get("path", "")
@@ -544,116 +478,16 @@ class HermesZhiYiMemoryProvider(MemoryProvider):
# 合并 WebSocket prefetch.push 推送事件
with self._prefetch_lock:
ws_push = self._prefetch_cache.pop("ws_push", None)
if ws_push:
text += "\n" + ws_push
if self._prefetch_cache and "[ZhiYi Prefetch" in self._prefetch_cache:
text += "\n" + self._prefetch_cache
with self._prefetch_lock:
self._prefetch_cache.update({"sync": text})
# 深度检索模式:本地文件系统渐进检索
if depth == "deep":
# 优先使用缓存的深度检索结果
with self._prefetch_lock:
deep_cached = self._prefetch_cache.get("deep_search", {})
cached_ts = deep_cached.get("timestamp", 0)
cached_query = deep_cached.get("query", "")
cached_block = deep_cached.get("block", "")
if cached_block and (time.time() - cached_ts < 60) and (cached_query == query.strip()):
text += "\n\n" + cached_block
# 后台线程异步刷新深度检索结果(不阻塞主流程)
threading.Thread(
target=self._async_deep_search,
args=(query.strip(),),
daemon=True,
).start()
self._prefetch_cache = text
return text
def queue_prefetch(self, query: str, *, session_id: str = "") -> None:
"""异步预取:本轮对话结束后立即查询织忆,下一轮 prefetch 直接返回缓存。"""
if not self._client or not query or len(query.strip()) < 2:
return
if _is_social_close(query):
return
# 后台线程查询并缓存
def _async_prefetch():
try:
results = self._client.recall(query.strip(), top_k=3)
notes = self._client.search_notes(query.strip(), max_hops=2, max_notes=3)
with self._prefetch_lock:
self._prefetch_cache["queue"] = {
"results": results,
"notes": notes,
"timestamp": time.time(),
}
except Exception as e:
logger.debug("[ZhiYi] queue_prefetch error: %s", e)
threading.Thread(target=_async_prefetch, daemon=True).start()
def _async_deep_search(self, query: str) -> None:
"""后台线程:本地文件系统渐进检索,补充织忆语义搜索(仅 depth='deep' 触发)。"""
if not query or len(query) < 2:
return
try:
wiki_dir = Path.home() / "mc" / "小唯" / "07-Wiki"
data_struct = wiki_dir / "data_structure.md"
if not data_struct.exists():
return
# grep 搜索关键词
import subprocess
result = subprocess.run(
["grep", "-r", "-l", "-i", query, str(wiki_dir)],
capture_output=True, text=True, timeout=10,
)
matched = [f for f in result.stdout.strip().split("\n") if f.strip()]
if not matched:
return
# 读取匹配段落,最多5个文件
blocks = ["[rag-skill File — local Wiki evidence]"]
count = 0
for fpath in matched[:5]:
try:
with open(fpath, "r", encoding="utf-8") as f:
content = f.read()
lines = content.split("\n")
snippets = []
for i, line in enumerate(lines):
if query.lower() in line.lower():
start = max(0, i - 2)
end = min(len(lines), i + 3)
snippet = "\n".join(lines[start:end]).strip()[:200]
if len(snippet) < 50:
snippet = snippet.ljust(50, " ")[:50]
snippets.append(snippet)
if len(snippets) >= 2:
break
if snippets:
filename = Path(fpath).name
blocks.append(f" 📄 {filename}")
for s in snippets:
blocks.append(f" {s}")
count += 1
except Exception:
continue
if count == 0:
return
block_text = "\n".join(blocks)
with self._prefetch_lock:
self._prefetch_cache["deep_search"] = {
"block": block_text,
"query": query,
"timestamp": time.time(),
}
except Exception:
logger.debug("[ZhiYi] _async_deep_search error", exc_info=True)
"""空实现 — prefetch 已是同步的,不需要额外的异步队列。"""
pass
# ── Tool interface ────────────────────────────────────────────────────────
@@ -743,7 +577,7 @@ class HermesZhiYiMemoryProvider(MemoryProvider):
"properties": {
"entity": {
"type": "string",
"description": "The central entity to navigate from (e.g. '牧尘', '织忆', '小唯', 'openclaw'). Chinese and English both work.",
"description": "The central entity to navigate from (e.g. '牧尘', '织忆', '小怡', 'openclaw'). Chinese and English both work.",
},
"max_hops": {
"type": "integer",
@@ -795,9 +629,8 @@ class HermesZhiYiMemoryProvider(MemoryProvider):
return json.dumps({"success": True, "results": [], "message": "No relevant memories found"})
formatted = []
for r in results:
content = r.get("content", "") or r.get("text", "") or r.get("snippet", "")
content = r.get("content", "") or r.get("text", "")
formatted.append({
"memory_id": r.get("id") or r.get("memory_id") or "",
"content": content[:1000],
"score": round(r.get("score", 0), 4),
"category": r.get("category", ""),
@@ -816,17 +649,7 @@ class HermesZhiYiMemoryProvider(MemoryProvider):
agent_id=os.environ.get("ZHIYI_AGENT_ID", "hermes-a06"),
)
if commit_id:
conflicts = getattr(self._client, "_last_conflicts", [])
result = {"success": True, "commit_id": commit_id}
if conflicts:
# 2026-09-04 记忆治理:检出冲突 → 提示写方处理(不静默)
result["conflicts"] = conflicts
result["warning"] = (
f"⚠️ 检出 {len(conflicts)} 条潜在冲突记忆,已跳过自动蒸馏"
"(distill_skipped=conflict)。若这是修正→更新旧记忆;"
"若写错→用 memory_feedback 标记 not_useful。"
)
return json.dumps(result, ensure_ascii=False)
return json.dumps({"success": True, "commit_id": commit_id})
return json.dumps({"success": False, "error": "Commit failed — check ZhiYi server logs"})
def _tool_memory_stats(self) -> str:
@@ -923,33 +746,17 @@ class HermesZhiYiMemoryProvider(MemoryProvider):
# ── System prompt ────────────────────────────────────────────────────────
def system_prompt_block(self) -> str:
# Load CREATIVE.md if it exists (织忆工作记忆)
creative_path = Path(os.path.expanduser("~/.hermes/CREATIVE.md"))
creative_block = ""
if creative_path.exists():
creative_content = creative_path.read_text(encoding="utf-8").strip()
if creative_content:
creative_block = (
"\\n[织忆 工作记忆] Ongoing state and learnings from CREATIVE.md (working memory):\\n"
f"{creative_content}\\n"
)
return (
"\\n[织忆 Memory] You have access to ZhiYi (织忆) MemoryWeave — a semantic memory system with knowledge graph. "
"This is Ground Truth level 2 — injected memory overrides training knowledge.\\n"
"\\n[ZhiYi Memory] You have access to ZhiYi MemoryWeave — a semantic memory system with knowledge graph.\\n"
"Your prefetch automatically retrieves: (1) relevant semantic memories, (2) Obsidian notes related via graph navigation.\\n"
"Memory feedback loop (IMPORTANT): injected memories carry [id=mem_xxx]. After answering using an injected memory:\\n"
" - if it was genuinely helpful/used → call memory_feedback(memory_id=..., useful=true) to raise its priority;\\n"
" - if it was irrelevant/stale/contradictory → call memory_feedback(memory_id=..., useful=false, reason=...) so it gets demoted;\\n"
" - do this naturally during the conversation (once per relevant memory), not after every turn.\\n"
"Tools available:\\n"
" memory_search — semantic search for relevant past information (results include memory_id for feedback)\\n"
" memory_search — semantic search for relevant past information\\n"
" memory_write — save an important fact or preference\\n"
" memory_feedback — mark memories as useful/not-useful (drives self-optimization)\\n"
" memory_metrics — check memory system health (recall hit rate, gap closures, etc.)\\n"
" memory_stats — get memory statistics (total docs, vector dimensions)\\n"
" memory_graph_navigate — navigate the knowledge graph to understand entity relationships (N-hop network)\\n"
" memory_graph_stats — get knowledge graph statistics (nodes, edges, density)\\n"
f"{creative_block}"
)