Merge pull request 'fix: [product] _normalize_rule 增加 screen_type 默认值防御 + step2 test 降级 warn - Closes #86' (#87) from dev/issue-86-screen-type-defense into main
CI / test (push) Waiting to run
CI / test (push) Waiting to run
This commit was merged in pull request #87.
This commit is contained in:
@@ -134,6 +134,18 @@ def _normalize_rule(rule: dict) -> dict:
|
|||||||
|
|
||||||
Fixes common LLM output issues: missing trigger, null operator, etc.
|
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
|
# Ensure trigger exists
|
||||||
if not rule.get("trigger"):
|
if not rule.get("trigger"):
|
||||||
rule["trigger"] = {}
|
rule["trigger"] = {}
|
||||||
|
|||||||
@@ -351,12 +351,15 @@ def test_step2_rule_paths():
|
|||||||
|
|
||||||
|
|
||||||
def test_step2_precondition_fields():
|
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()
|
fragments = _load_fragments_or_skip()
|
||||||
if fragments is None:
|
if fragments is None:
|
||||||
pytest.skip("ir_fragments.json not found")
|
pytest.skip("ir_fragments.json not found")
|
||||||
errors = check_precondition_fields(fragments)
|
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():
|
def test_step2_user_interaction_content():
|
||||||
|
|||||||
@@ -575,3 +575,42 @@ class TestNormalizeRule:
|
|||||||
}
|
}
|
||||||
normalized = _normalize_rule(rule)
|
normalized = _normalize_rule(rule)
|
||||||
assert normalized["sources"][0]["section"] == "4.2 关闭流程"
|
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"
|
||||||
|
|||||||
Reference in New Issue
Block a user