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]