fix: trigger.operator null + 覆盖反馈重试 - Closes #22, Closes #21 #25

Merged
pzhang_zywl merged 1 commits from dev/issue-22-fix-trigger-null into main 2026-05-31 20:22:04 +08:00
3 changed files with 49 additions and 2 deletions
Showing only changes of commit 62266dde4d - Show all commits
@@ -535,6 +535,20 @@ def _quick_validate(
return passed, gaps
def _build_coverage_feedback(gaps: dict) -> str:
"""Generate feedback text for re-prompting when coverage is below threshold."""
lines = []
for item in gaps.get("missing_paths", []):
lines.append(f"- {item}")
if lines:
return (
"\n## 覆盖反馈(上一次运行未满足覆盖要求,请重新生成并修复以下缺口)\n\n"
+ "\n".join(lines)
+ "\n\n请重新审视文档,为缺失的章节和表格行创建对应的 function_unit。"
)
return ""
def _collect_logic_tree_nodes(doc: dict) -> dict[str, dict[str, str]]:
"""Return {image_id: {node_id: node_type}} for all logic trees."""
result = {}
@@ -750,6 +764,29 @@ def run_ensemble_semantic_index(doc: dict) -> dict:
if v:
print(f" {k}: {len(v)} 个问题")
# Feedback retry: re-run with coverage feedback (one retry)
feedback = _build_coverage_feedback(gaps)
if feedback:
print(f"\n 覆盖反馈重试...")
try:
retry_prompt = build_prompt(doc, feedback, all_paths)
retry_result = call_llm(retry_prompt, max_retries=1, temperature=0.0)
n_retry_units = len(retry_result.get("function_units", []))
print(f" 重试返回: {n_retry_units} 功能单元")
if n_retry_units > 0:
# Merge retry into results and re-validate
semantic_indices.append(retry_result)
merged = ensemble_merge(semantic_indices)
merged["ensemble_temperatures"] = list(temperatures) + ["feedback_retry"]
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'}")
except Exception as e:
print(f" 覆盖反馈重试失败: {e}")
return merged
@@ -497,6 +497,13 @@ def main():
print(f"\n[2/3] 逐单元提取 IR 规则...")
fragments = extract_all_rules(semantic_index, doc)
# Filter out fragments with empty rules (LLM extraction failures)
empty_units = [f["unit_id"] for f in fragments
if not f.get("rules") and not f.get("error")]
if empty_units:
print(f" [WARN] {len(empty_units)} 个单元规则为空,已过滤: {empty_units}")
fragments = [f for f in fragments if f.get("rules") or f.get("error")]
# 3. Save
print(f"\n[3/3] 保存 IR 片段...")
config.save_json(fragments, config.IR_FRAGMENTS_JSON)
@@ -139,6 +139,10 @@ def _normalize_rule(rule: dict) -> dict:
trigger = rule["trigger"]
# Ensure trigger-level combining operator (AND/OR) for multi-condition triggers
if not trigger.get("operator"):
trigger["operator"] = "AND"
# If trigger has an event, it's event-based (no conditions needed)
if trigger.get("event") is not None:
return rule
@@ -147,7 +151,7 @@ def _normalize_rule(rule: dict) -> dict:
if "conditions" not in trigger:
trigger["conditions"] = []
# Fix null operators in conditions
# Fix null operators in individual conditions
for cond in trigger["conditions"]:
if not cond.get("operator"):
cond["operator"] = "=="
@@ -158,7 +162,6 @@ def _normalize_rule(rule: dict) -> dict:
# If still no conditions, add a default one
if not trigger["conditions"]:
trigger["operator"] = "AND"
trigger["conditions"] = [{
"signal": "system_state",
"operator": "==",