fix: step3 _normalize_rule 处理 section 为 list 的 LLM 格式问题 - Closes #69
CI / test (pull_request) Successful in 9s

LLM 输出 section 字段有时为 list 而非 string,导致 .strip() 崩溃。
添加 _clean_section() 将 list→首元素 string,空 list 回退到 rule path。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-06-02 17:44:56 +08:00
parent e54a221f34
commit efb5ed481e
2 changed files with 40 additions and 1 deletions
@@ -174,11 +174,25 @@ def _normalize_rule(rule: dict) -> dict:
sources = rule.get("sources", []) sources = rule.get("sources", [])
valid_types = {"table", "text", "logic_tree"} valid_types = {"table", "text", "logic_tree"}
def _clean_section(val):
"""Normalize section value: list→first element, ensure string."""
if isinstance(val, list):
return str(val[0]).strip() if val else ""
if isinstance(val, str):
return val.strip()
return str(val).strip() if val else ""
# Normalize section fields that might be lists (LLM format instability)
for s in sources:
sec = s.get("section")
if sec is not None:
s["section"] = _clean_section(sec)
# try to infer a default section from the rule path # try to infer a default section from the rule path
default_section = "" default_section = ""
for s in sources: for s in sources:
sec = s.get("section", "") sec = s.get("section", "")
if sec and sec.strip(): if sec and isinstance(sec, str) and sec.strip():
default_section = sec.strip() default_section = sec.strip()
break break
if not default_section: if not default_section:
@@ -538,3 +538,28 @@ class TestNormalizeRule:
assert len(normalized["sources"]) == 1 assert len(normalized["sources"]) == 1
assert normalized["sources"][0]["type"] == "text" assert normalized["sources"][0]["type"] == "text"
assert normalized["sources"][0]["section"] == "3.1 策略" assert normalized["sources"][0]["section"] == "3.1 策略"
def test_normalize_section_is_list(self):
"""Section field that is a list (LLM format bug) is normalized to string."""
rule = {
"trigger": {"conditions": [{"signal": "x", "operator": "==", "value": "1"}]},
"sources": [
{"type": "table", "section": ["状态", "系统设置"], "row": 1},
{"type": "text", "section": ["后台限制"], "text_snippet": "x"},
],
}
normalized = _normalize_rule(rule)
assert normalized["sources"][0]["section"] == "状态"
assert normalized["sources"][1]["section"] == "后台限制"
def test_normalize_section_is_empty_list(self):
"""Empty list section falls back to rule path."""
rule = {
"trigger": {"conditions": [{"signal": "x", "operator": "==", "value": "1"}]},
"path": "4.2 关闭流程 > decision",
"sources": [
{"type": "table", "section": [], "row": 1},
],
}
normalized = _normalize_rule(rule)
assert normalized["sources"][0]["section"] == "4.2 关闭流程"