From 5a6b8f216d9baa2cb2143e83286b3bd4db79fd4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=93=B8=E6=B8=8A=20ICE-GL-ZY001?= Date: Mon, 25 May 2026 01:57:07 +0000 Subject: [PATCH] =?UTF-8?q?D112:=20=E4=BF=AE=E5=A4=8Densure=5Fpath?= =?UTF-8?q?=E9=80=92=E5=BD=92=E5=88=9B=E5=BB=BA=E7=88=B6=E8=8A=82=E7=82=B9?= =?UTF-8?q?=C2=B7=E8=A7=A3=E5=86=B3FOREIGN=20KEY=E7=BA=A6=E6=9D=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zhuyuan-agent/core/hldp_memory.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/zhuyuan-agent/core/hldp_memory.py b/zhuyuan-agent/core/hldp_memory.py index 54e3c36..549f300 100644 --- a/zhuyuan-agent/core/hldp_memory.py +++ b/zhuyuan-agent/core/hldp_memory.py @@ -114,10 +114,23 @@ class HLDPTreeStore: def ensure_path(self, path: str, node_type: str, persona_id: str = None, epoch_id: str = None, title: str = "", summary: str = "", parent_path: str = None) -> str: - """确保树路径存在,不存在则创建。返回 path。""" + """确保树路径存在,不存在则创建(含递归父节点)。返回 path。""" + # 先确保父路径存在 + if parent_path: + parent_exists = self.conn.execute( + "SELECT 1 FROM hldp_nodes WHERE path=?", (parent_path,)).fetchone() + if not parent_exists: + # 递归创建父节点 + grandparent = "/".join(parent_path.split("/")[:-1]) if "/" in parent_path else None + pp_type = "epoch" if parent_path.count("/") == 2 else \ + "index" if parent_path.count("/") == 3 else "branch" + self.ensure_path( + path=parent_path, node_type=pp_type, + persona_id=persona_id, epoch_id=epoch_id, + parent_path=grandparent) + cur = self.conn.execute("SELECT path FROM hldp_nodes WHERE path=?", (path,)) if cur.fetchone(): - # 更新 self.conn.execute( "UPDATE hldp_nodes SET title=?, summary=?, updated_at=datetime('now') WHERE path=?", (title, summary, path))