fix: normalize non-scalar prediction; harden predictions insert

This commit is contained in:
2026-07-16 06:06:58 -04:00
parent 7d02cded99
commit 25918a73ff
2 changed files with 91 additions and 3 deletions
+59
View File
@@ -559,6 +559,65 @@ class TestPredictionAlwaysWritten:
assert rows[0]["prediction"] is None
def _make_nonscalar_llm_response() -> LLMResponse:
"""构造 submit_answer 提交非标量 answerlist)的 LLMResponse。"""
content = json.dumps(
{
"reflect": {"observation": "找到答案"},
"plan": {"next_step": "提交"},
"action": {
"tool": "submit_answer",
"args": {
"answer": ["B"],
"evidence": "证据文本",
"reasoning": "推理过程",
},
},
}
)
return LLMResponse(
content=content,
thinking="思考过程",
model="test-model",
provider="test",
prompt_tokens=100,
completion_tokens=50,
latency_ms=200,
ttft_ms=30.0,
max_inter_token_ms=5.0,
cache_hit=False,
call_id="test-call-nonscalar",
)
class TestNonScalarPrediction:
"""非标量 prediction 归一化 + 落库加固测试。"""
@pytest.mark.asyncio
async def test_nonscalar_prediction_does_not_crash(self, harness_log: HarnessLog) -> None:
"""submit_answer 返回 {'answer': ['B']} 时归一化落库,不抛 sqlite 绑定异常。"""
llm = AsyncMock()
llm.chat.return_value = _make_nonscalar_llm_response()
result = await run_inference(
[_make_question(answer="B")],
llm=llm,
tool_dispatch_fn=_stub_tool_dispatch,
prompt_builder=_stub_prompt_builder,
log=harness_log,
run_id="run-nonscalar",
concurrency=1,
max_steps=10,
skill_mode="auto",
)
assert result.total == 1
rows = harness_log.query("SELECT * FROM predictions WHERE run_id = ?", ("test-run",))
assert len(rows) == 1
# prediction 被 JSON 序列化为字符串,不再是 Python list
assert rows[0]["prediction"] == '["B"]'
class TestPluginsFactory:
"""plugins_factory 调用测试。"""