From 268520d4534af0cf6f081a2874eeab87bef6173f Mon Sep 17 00:00:00 2001 From: Peter Zhang <18501667167@qq.com> Date: Tue, 2 Jun 2026 16:16:47 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20step3=20=E8=BF=87=E6=BB=A4=E9=9D=9E?= =?UTF-8?q?=E6=B3=95=20source=20type=20+=20step1=20=E9=87=8D=E8=AF=95?= =?UTF-8?q?=E8=B4=A8=E9=87=8F=E9=97=A8=E6=8E=A7=20-=20Closes=20#57?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - step3 _normalize_rule: 将 function_unit_description 等非法 source type 标准化为 text - step1 覆盖反馈重试: 仅纳入实际提升覆盖率的 retry 结果,避免低质量输出稀释 ensemble - 新增 UT: test_normalize_source_invalid_type Co-Authored-By: Claude Opus 4.7 --- .../step1_semantic_index.py | 37 ++++++++++++++----- .../step3_merge_and_audit.py | 7 ++++ .../ir_generation_skill/tests/test_step3.py | 15 ++++++++ 3 files changed, 49 insertions(+), 10 deletions(-) diff --git a/skills/ir_generation_skill/step1_semantic_index.py b/skills/ir_generation_skill/step1_semantic_index.py index 15897a6..bf38da6 100644 --- a/skills/ir_generation_skill/step1_semantic_index.py +++ b/skills/ir_generation_skill/step1_semantic_index.py @@ -880,7 +880,7 @@ def run_ensemble_semantic_index(doc: dict) -> dict: if v: print(f" {k}: {len(v)} 个问题") - # Feedback retry: re-run with coverage feedback (up to 2 retries) + # Feedback retry: re-run with coverage feedback (up to 2 retries, quality-gated) retry_count = 0 while retry_count < 2: feedback = _build_coverage_feedback(gaps) @@ -889,6 +889,10 @@ def run_ensemble_semantic_index(doc: dict) -> dict: retry_count += 1 print(f"\n 覆盖反馈重试 #{retry_count} (feedback长度={len(feedback)}字符)...", flush=True) try: + # record pre-retry coverage to gate quality + pre_warnings = len(gaps.get("coverage_warnings", [])) + pre_missing_rows = len(gaps.get("missing_table_rows", [])) + retry_prompt = build_prompt(doc, feedback, all_paths) print(f" 重试 prompt 长度: {len(retry_prompt)} 字符", flush=True) retry_result = call_llm(retry_prompt, max_retries=1, temperature=0.3) @@ -902,15 +906,28 @@ def run_ensemble_semantic_index(doc: dict) -> dict: if src.get("section"): retry_sections.add(src["section"]) print(f" 重试新增 sections: {sorted(retry_sections)}", flush=True) - semantic_indices.append(retry_result) - merged = ensemble_merge(semantic_indices) - merged["ensemble_temperatures"] = list(temperatures) + [f"feedback_retry_{retry_count}"] - passed, gaps = _quick_validate(merged, doc, all_paths) - merged["validation_passed"] = passed - merged["validation_gaps"] = { - k: v for k, v in gaps.items() if v - } - print(f" 重试后验证: {'PASS' if passed else 'GAPS FOUND'}", flush=True) + # Quality gate: only include retry if it improves coverage + trial_indices = semantic_indices + [retry_result] + trial_merged = ensemble_merge(trial_indices) + trial_passed, trial_gaps = _quick_validate(trial_merged, doc, all_paths) + trial_warnings = len(trial_gaps.get("coverage_warnings", [])) + trial_missing = len(trial_gaps.get("missing_table_rows", [])) + if trial_warnings < pre_warnings or trial_missing < pre_missing_rows: + semantic_indices.append(retry_result) + merged = trial_merged + passed, gaps = trial_passed, trial_gaps + merged["ensemble_temperatures"] = list(temperatures) + [f"feedback_retry_{retry_count}"] + merged["validation_passed"] = passed + merged["validation_gaps"] = { + k: v for k, v in gaps.items() if v + } + print(f" 重试后验证 (已采纳): {'PASS' if passed else 'GAPS FOUND'} " + f"(warnings {pre_warnings}→{trial_warnings}, " + f"missing_rows {pre_missing_rows}→{trial_missing})", flush=True) + else: + print(f" 重试结果未提升覆盖率,丢弃 " + f"(warnings {pre_warnings}→{trial_warnings}, " + f"missing_rows {pre_missing_rows}→{trial_missing})", flush=True) except Exception as e: print(f" 覆盖反馈重试失败: {e}", flush=True) import traceback diff --git a/skills/ir_generation_skill/step3_merge_and_audit.py b/skills/ir_generation_skill/step3_merge_and_audit.py index df8a87d..5ce94d6 100644 --- a/skills/ir_generation_skill/step3_merge_and_audit.py +++ b/skills/ir_generation_skill/step3_merge_and_audit.py @@ -170,8 +170,11 @@ def _normalize_rule(rule: dict) -> dict: }] # Ensure table/text sources have a section field (defensive against LLM omission) + # Also normalize invalid source types (LLM hallucinations like function_unit_description) sources = rule.get("sources", []) if sources: + valid_types = {"table", "text", "logic_tree"} + # try to infer a default section from sibling sources or the rule path default_section = "" for s in sources: @@ -186,6 +189,10 @@ def _normalize_rule(rule: dict) -> dict: for src in sources: stype = src.get("type", "") + # Normalize invalid source types to "text" + if stype and stype not in valid_types: + src["type"] = "text" + stype = "text" if stype in ("table", "text"): if not src.get("section"): src["section"] = default_section diff --git a/skills/ir_generation_skill/tests/test_step3.py b/skills/ir_generation_skill/tests/test_step3.py index 00ae150..fa6e9e8 100644 --- a/skills/ir_generation_skill/tests/test_step3.py +++ b/skills/ir_generation_skill/tests/test_step3.py @@ -511,3 +511,18 @@ class TestNormalizeRule: } normalized = _normalize_rule(rule) assert "section" not in normalized["sources"][0] + + def test_normalize_source_invalid_type(self): + """Invalid source types (LLM hallucinations) are normalized to text.""" + rule = { + "trigger": {"conditions": [{"signal": "x", "operator": "==", "value": "1"}]}, + "sources": [ + {"type": "function_unit_description", "text_snippet": "desc", + "section": "3.1 功能"}, + {"type": "unknown_type", "text_snippet": "also invalid"}, + ], + } + normalized = _normalize_rule(rule) + assert normalized["sources"][0]["type"] == "text" + assert normalized["sources"][1]["type"] == "text" + assert normalized["sources"][0]["section"] == "3.1 功能"