Compare commits

...

4 Commits

Author SHA1 Message Date
pzhang_zywl 93bbfe6029 fix: step3 _normalize_rule 将 table source 的 null row 转为 0 - Closes #73
CI / test (pull_request) Successful in 8s
LLM 输出 table source 时 row 字段可能为 null,导致 Layer A schema 失败。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-02 18:05:28 +08:00
pzhang_zywl 6b1424b1c4 Merge pull request 'fix: [bug] step2 IR extraction 生成 list 类型 section 字段导致 conftest 崩溃 - 来自 #64 修复 - Closes #69' (#72) from dev/issue-69-fix-list-section into main
CI / test (push) Successful in 12s
2026-06-02 17:45:37 +08:00
pzhang_zywl efb5ed481e 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>
2026-06-02 17:44:56 +08:00
pzhang_zywl e54a221f34 Merge pull request 'fix: [test] conftest ir_data fixture 防御 LLM 产出的 list-type section - Closes #70' (#71) from test/issue-70 into main
CI / test (push) Successful in 8s
2026-06-02 17:38:31 +08:00
2 changed files with 58 additions and 2 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:
@@ -192,7 +206,12 @@ def _normalize_rule(rule: dict) -> dict:
if stype and stype not in valid_types: if stype and stype not in valid_types:
src["type"] = "text" src["type"] = "text"
stype = "text" stype = "text"
if stype in ("table", "text"): if stype == "table":
if not src.get("section"):
src["section"] = default_section
if src.get("row") is None:
src["row"] = 0
elif stype == "text":
if not src.get("section"): if not src.get("section"):
src["section"] = default_section src["section"] = default_section
else: else:
@@ -512,6 +512,18 @@ class TestNormalizeRule:
normalized = _normalize_rule(rule) normalized = _normalize_rule(rule)
assert "section" not in normalized["sources"][0] assert "section" not in normalized["sources"][0]
def test_normalize_table_source_null_row(self):
"""Table source with null row gets row=0 (defensive)."""
rule = {
"trigger": {"conditions": [{"signal": "x", "operator": "==", "value": "1"}]},
"sources": [
{"type": "table", "section": "3.1 功能", "row": None},
],
}
normalized = _normalize_rule(rule)
assert normalized["sources"][0]["row"] == 0
def test_normalize_source_invalid_type(self): def test_normalize_source_invalid_type(self):
"""Invalid source types (LLM hallucinations) are normalized to text.""" """Invalid source types (LLM hallucinations) are normalized to text."""
rule = { rule = {
@@ -538,3 +550,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 关闭流程"