From 96c4be17dc5947bb8cdec3e97bc04743e05bce39 Mon Sep 17 00:00:00 2001 From: ZiWei <131428629+ZiWei09@users.noreply.github.com> Date: Tue, 28 Apr 2026 11:59:29 +0800 Subject: [PATCH] =?UTF-8?q?merge=5Fremote=5Fresources:=20=E4=BB=A5?= =?UTF-8?q?=E8=BF=9C=E7=AB=AF=E4=B8=BA=E5=87=86=EF=BC=8C=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E5=B7=B2=E4=B8=8D=E5=9C=A8=E8=BF=9C=E7=AB=AF?= =?UTF-8?q?=E7=9A=84=E7=89=A9=E6=96=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 远端(bioyond)不存在的物料不应保留在本地资源树中, 否则这些过期物料会在 PLR 反序列化时产生命名冲突。 同步时对两级子节点均执行移除: - 三级物料(设备→仓库→物料) - 三级子物料(设备→物料→子物料) 同时修复 else 分支缺少 remote_child_name 存在性检查的潜在 KeyError。 Co-Authored-By: Claude Sonnet 4.6 --- unilabos/resources/resource_tracker.py | 38 ++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/unilabos/resources/resource_tracker.py b/unilabos/resources/resource_tracker.py index b45ab57a..119af760 100644 --- a/unilabos/resources/resource_tracker.py +++ b/unilabos/resources/resource_tracker.py @@ -868,13 +868,34 @@ class ResourceTreeSet(object): f"已存在,跳过" ) + # 移除本地有但远端已不存在的物料(以远端为准) + remote_material_names = {m.res_content.name for m in remote_child.children} + removed_count = 0 + for child in list(local_sub_device.children): + if child.res_content.name not in remote_material_names: + local_sub_device.children.remove(child) + removed_count += 1 + logger.info( + f"移除远端已不存在的物料: '{remote_root_id}/{remote_child_name}/{child.res_content.name}'" + ) + if added_count > 0: logger.info( f"Device '{remote_root_id}/{remote_child_name}': " f"从远端同步了 {added_count} 个物料子树" ) + if removed_count > 0: + logger.info( + f"Device '{remote_root_id}/{remote_child_name}': " + f"移除了 {removed_count} 个远端已删除的物料" + ) else: # 二级物料已存在,比较三级子节点是否缺失 + if remote_child_name not in local_children_map: + logger.warning( + f"物料 '{remote_root_id}/{remote_child_name}' 在远端存在但本地不存在,跳过" + ) + continue local_material = local_children_map[remote_child_name] local_material_children_map = {child.res_content.name: child for child in local_material.children} @@ -890,11 +911,28 @@ class ResourceTreeSet(object): f"物料 '{remote_root_id}/{remote_child_name}/{remote_sub_name}' " f"已存在,跳过" ) + + # 移除本地有但远端已不存在的子物料(以远端为准) + remote_sub_names = {s.res_content.name for s in remote_child.children} + removed_count = 0 + for child in list(local_material.children): + if child.res_content.name not in remote_sub_names: + local_material.children.remove(child) + removed_count += 1 + logger.info( + f"移除远端已不存在的子物料: '{remote_root_id}/{remote_child_name}/{child.res_content.name}'" + ) + if added_count > 0: logger.info( f"物料 '{remote_root_id}/{remote_child_name}': " f"从远端同步了 {added_count} 个子物料" ) + if removed_count > 0: + logger.info( + f"物料 '{remote_root_id}/{remote_child_name}': " + f"移除了 {removed_count} 个远端已删除的子物料" + ) else: # 情况1: 一级节点是物料(不是 device) # 检查是否已存在