From 9339facf9a51f90c528232b969fe12a30c6e5554 Mon Sep 17 00:00:00 2001 From: ThearchyHelios Date: Wed, 21 Oct 2026 15:50:15 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=90=E8=A1=8C=E5=AF=B9=E6=AF=94=E9=87=8C?= =?UTF-8?q?=E7=9A=84=E5=88=86=E6=9E=90=E5=8D=A1=E5=8F=AA=E8=AE=A4=E5=81=9A?= =?UTF-8?q?=E5=AE=8C=E4=BA=86=E7=9A=84=E9=82=A3=E6=AC=A1=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=EF=BC=9Aanalysis.end=20=E5=85=88=E6=8C=89=E5=8D=A1=E8=AE=B0?= =?UTF-8?q?=E5=9C=A8=E8=BF=99=E4=B8=80=E6=AC=A1=E6=89=A7=E8=A1=8C=E9=87=8C?= =?UTF-8?q?=EF=BC=8C=E8=A7=81=E5=88=B0=E8=BF=99=E5=BC=A0=E5=8D=A1=E7=9A=84?= =?UTF-8?q?=20node.finished=20=E6=89=8D=E7=AE=97=E6=95=B0=EF=BC=88?= =?UTF-8?q?=E6=96=B0=E7=9A=84=20node.started=20=E6=B8=85=E6=8E=89=E4=B8=8A?= =?UTF-8?q?=E4=B8=80=E6=AC=A1=E6=B2=A1=E5=81=9A=E5=AE=8C=E7=9A=84=EF=BC=89?= =?UTF-8?q?=E3=80=82=E5=88=86=E6=9E=90=E5=8D=A1=E6=89=A7=E8=A1=8C=E5=88=B0?= =?UTF-8?q?=E4=B8=80=E5=8D=8A=E5=A4=B1=E8=B4=A5=E6=97=B6=E5=89=8D=E5=87=A0?= =?UTF-8?q?=E9=A1=B9=E5=B7=B2=E7=BB=8F=E5=8F=91=E4=BA=86=20analysis.end?= =?UTF-8?q?=EF=BC=8C=E4=BB=A5=E5=89=8D=E8=BF=90=E8=A1=8C=E5=AF=B9=E6=AF=94?= =?UTF-8?q?=E4=BC=9A=E6=8A=8A=E8=BF=99=E6=AC=A1=E5=A4=B1=E8=B4=A5=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E7=9A=84=E5=88=A4=E5=AE=9A=E5=BD=93=E6=88=90=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E5=88=97=E5=87=BA=E6=9D=A5=E3=80=81=E4=B9=9F=E7=AE=97?= =?UTF-8?q?=E8=BF=9B=E6=89=A7=E8=A1=8C=E6=AC=A1=E6=95=B0=E3=80=82=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=EF=BC=9A=E4=B8=AD=E9=80=94=E5=A4=B1=E8=B4=A5=E7=9A=84?= =?UTF-8?q?=E9=82=A3=E6=AC=A1=E4=B8=8D=E5=88=97=E3=80=81=E4=B8=8D=E7=AE=97?= =?UTF-8?q?=E6=AC=A1=E6=95=B0=EF=BC=8C=E5=A4=B1=E8=B4=A5=E5=90=8E=E9=87=8D?= =?UTF-8?q?=E6=9D=A5=E5=81=9A=E5=AE=8C=E7=9A=84=E6=8C=89=E5=81=9A=E5=AE=8C?= =?UTF-8?q?=E7=9A=84=E9=82=A3=E6=AC=A1=E5=AF=B9=E6=AF=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/engine/compare.py | 17 +++++++++++------ backend/tests/test_analysis_compare.py | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/backend/app/engine/compare.py b/backend/app/engine/compare.py index 61b7ed80..6afdab43 100644 --- a/backend/app/engine/compare.py +++ b/backend/app/engine/compare.py @@ -336,17 +336,22 @@ def _queries(side: Side) -> list[_Query]: def _analysis_items(side: Side) -> dict[str, tuple[dict[str, dict[str, Any]], int]]: - """封存范围内每张分析卡各项最后一次的结论(analysis.end,engine/nodes/analysis.py 每项一条):{卡: ({项: 事件数据}, - 执行了几次)},项按记下的先后。循环里执行了几次的,同一项后一次的盖过前一次。""" + """封存范围内每张分析卡各项最后一次做完的结论(analysis.end,engine/nodes/analysis.py 每项一条):{卡: ({项: 事件数据}, + 执行了几次)},项按记下的先后。只认后面跟着这张卡 node.finished 的那几次执行:执行到一半失败的(前几项已经发了 + analysis.end,后面一项让卡失败),那次的判定不是这次运行的结果,不列,也不算执行次数。循环里执行了几次的,同一项后 + 一次的盖过前一次。""" out: dict[str, tuple[dict[str, dict[str, Any]], int]] = {} + pending: dict[str, dict[str, dict[str, Any]]] = {} # 卡 → 这一次执行里已经做完的项(还没见到 node.finished) for _, etype, node_id, data in side.events: if not node_id or not isinstance(data, dict): continue - if etype == EventType.ANALYSIS_END and isinstance(data.get("item"), str): + if etype == EventType.NODE_STARTED: + pending.pop(node_id, None) + elif etype == EventType.ANALYSIS_END and isinstance(data.get("item"), str): + pending.setdefault(node_id, {})[data["item"]] = data + elif etype == EventType.NODE_FINISHED and node_id in pending: items, runs = out.setdefault(node_id, ({}, 0)) - items[data["item"]] = data - elif etype == EventType.NODE_FINISHED and node_id in out: - items, runs = out[node_id] + items.update(pending.pop(node_id)) out[node_id] = (items, runs + 1) return out diff --git a/backend/tests/test_analysis_compare.py b/backend/tests/test_analysis_compare.py index 33da6a32..1df4aa0e 100644 --- a/backend/tests/test_analysis_compare.py +++ b/backend/tests/test_analysis_compare.py @@ -40,3 +40,24 @@ def test_compare_aligns_analysis_items(): assert wow["change"] == "phrase" # 没有分析卡的两次运行:analyses 是空的 assert compare.compare(week("r5").side(), week("r6").side())["analyses"] == [] + + +def test_a_card_that_failed_half_way_lists_only_what_a_finished_execution_decided(): + """分析卡执行到一半失败:前几项已经发了 analysis.end,后一项让卡失败(node.failed,没有 node.finished)。那次执行的判定 + 不是这次运行的结果:只认后面跟着 node.finished 的执行,失败的那次不列、也不算执行次数。""" + base = _with_card(week("r7"), [("wow", "within_band", "在常规波动范围内")]) + failed = week("r8") + failed.add("node.started", "insight", {"node_type": "analysis"}) + failed.add("analysis.end", "insight", {"item": "wow", "op": "baseline", "mode": None, "status": "ok", + "verdict": "outside_band", "direction": "down", "phrase": "超出常规波动范围", + "analysis_artifact": "wow-failed"}) + failed.add("node.failed", "insight", {"error": "分析项「where」(变化拆解):比率不能按段相加"}) + [wow] = compare.compare(base.side(), failed.side())["analyses"] + assert wow["change"] == "removed" and wow["target"] is None and wow["base"]["verdict"] == "within_band" + + # 失败后重来一次做完了:按做完的那次对比,执行次数只算做完的 + _with_card(failed, [("wow", "far_outside_band", "明显超出常规波动范围")]) + result = compare.compare(base.side(), failed.side()) + [wow] = result["analyses"] + assert wow["change"] == "verdict" and wow["target"]["verdict"] == "far_outside_band" + assert not [n for n in result["notes"] if "执行了" in n]