fix(agent): reject argless action in normalization; add boundary test
核心算法 #10(Agent Loop):修复 Codex 质量审查 Critical——
_normalize_action 仅在除 tool 外至少存在一个平铺参数键时才收拢,
{"tool": "x"} 无参结构不再被静默升级为空 args 合法结构,照旧
返回 None 走 retry 追问路径。补边界测试 + 测试辅助方法类型注解
与中文 docstring。
This commit is contained in:
@@ -263,7 +263,8 @@ _REAL_FLAT_FENCED = """{
|
||||
class TestParseNormalization:
|
||||
"""deepseek 输出变体(args 平铺 + ```json 围栏)归一化。"""
|
||||
|
||||
def _parse(self, content: str):
|
||||
def _parse(self, content: str) -> tuple[str, dict, dict, str, dict, str] | None:
|
||||
"""构造 AgentLoop 并解析给定 content,返回 _parse_response 结果。"""
|
||||
loop = AgentLoop(llm=AsyncMock(), max_steps=10)
|
||||
return loop._parse_response(_make_response(content))
|
||||
|
||||
@@ -279,6 +280,7 @@ class TestParseNormalization:
|
||||
}
|
||||
|
||||
def test_leading_json_fence(self) -> None:
|
||||
"""开头 ```json 围栏 + 尾部围栏包裹的标准结构可正常解析。"""
|
||||
content = '```json\n{"reflect": {}, "plan": {}, "action": {"tool": "submit_answer", "args": {"answer": "A"}}}\n```'
|
||||
parsed = self._parse(content)
|
||||
assert parsed is not None
|
||||
@@ -291,8 +293,14 @@ class TestParseNormalization:
|
||||
assert parsed[4] == {"tool": "submit_answer", "args": {"answer": "B"}}
|
||||
|
||||
def test_action_missing_tool_still_rejected(self) -> None:
|
||||
"""action 缺 tool 键的结构仍被拒绝。"""
|
||||
content = json.dumps({"reflect": {}, "plan": {}, "action": {"node_id": "x"}})
|
||||
assert self._parse(content) is None
|
||||
|
||||
def test_argless_action_still_rejected(self) -> None:
|
||||
"""有 tool、无 args、无平铺参数键 → 不得收拢为空 args,必须拒绝。"""
|
||||
content = json.dumps({"reflect": {}, "plan": {}, "action": {"tool": "submit_answer"}})
|
||||
assert self._parse(content) is None
|
||||
|
||||
def test_empty_content_still_rejected(self) -> None:
|
||||
assert self._parse("") is None
|
||||
|
||||
Reference in New Issue
Block a user