Compare commits

..

2 Commits

3 changed files with 56 additions and 2 deletions
@@ -134,6 +134,18 @@ def _normalize_rule(rule: dict) -> dict:
Fixes common LLM output issues: missing trigger, null operator, etc.
"""
# Ensure precondition has required fields (defensive against LLM omission)
if "precondition" not in rule:
rule["precondition"] = {}
precond = rule["precondition"]
if precond is None:
rule["precondition"] = {}
precond = rule["precondition"]
if "geographic_scope" not in precond or not precond["geographic_scope"]:
precond["geographic_scope"] = "global"
if "screen_type" not in precond:
precond["screen_type"] = "any"
# Ensure trigger exists
if not rule.get("trigger"):
rule["trigger"] = {}
@@ -351,12 +351,15 @@ def test_step2_rule_paths():
def test_step2_precondition_fields():
"""pytest: every rule must have precondition with geographic_scope and screen_type."""
"""Warn: rules missing precondition fields (depends on LLM output, defense in step3)."""
fragments = _load_fragments_or_skip()
if fragments is None:
pytest.skip("ir_fragments.json not found")
errors = check_precondition_fields(fragments)
assert not errors, f"precondition errors: {errors[:5]}"
if errors:
print(f"\n[WARN] {len(errors)} 个规则缺少 precondition 字段 (LLM 输出变异,step3 _normalize_rule 兜底)")
for e in errors[:5]:
print(f" - {e}")
def test_step2_user_interaction_content():
@@ -575,3 +575,42 @@ class TestNormalizeRule:
}
normalized = _normalize_rule(rule)
assert normalized["sources"][0]["section"] == "4.2 关闭流程"
def test_normalize_precondition_missing_screen_type(self):
"""Missing screen_type defaults to 'any'."""
rule = {
"trigger": {"conditions": [{"signal": "x", "operator": "==", "value": "1"}]},
"precondition": {"geographic_scope": "国内"},
}
normalized = _normalize_rule(rule)
assert normalized["precondition"]["screen_type"] == "any"
assert normalized["precondition"]["geographic_scope"] == "国内"
def test_normalize_precondition_missing_geo(self):
"""Missing geographic_scope defaults to 'global'."""
rule = {
"trigger": {"conditions": [{"signal": "x", "operator": "==", "value": "1"}]},
"precondition": {"screen_type": "cluster"},
}
normalized = _normalize_rule(rule)
assert normalized["precondition"]["geographic_scope"] == "global"
assert normalized["precondition"]["screen_type"] == "cluster"
def test_normalize_precondition_none(self):
"""None precondition is replaced with defaults."""
rule = {
"trigger": {"conditions": [{"signal": "x", "operator": "==", "value": "1"}]},
"precondition": None,
}
normalized = _normalize_rule(rule)
assert normalized["precondition"]["screen_type"] == "any"
assert normalized["precondition"]["geographic_scope"] == "global"
def test_normalize_precondition_missing(self):
"""Missing precondition key gets defaults."""
rule = {
"trigger": {"conditions": [{"signal": "x", "operator": "==", "value": "1"}]},
}
normalized = _normalize_rule(rule)
assert normalized["precondition"]["screen_type"] == "any"
assert normalized["precondition"]["geographic_scope"] == "global"