fix: address completion verdict review findings
- fidelity cacheability also requires a non-empty projected body, so think-tag-only answers are no longer cached - a no-argument tool sent with arguments null only is a deterministic dialect: ResultInvalid tool_arguments_null instead of retried - text mode treats choice index null as 0 and no longer lets False pose as 0 - null-only tool_calls elements no longer produce an empty tool_calls list; non-stream fidelity checks role before the verdict; warning paths are length-bounded - docs: ARCHITECTURE 7.8 field counts, README cache/watchdog wording, CHANGELOG notes, review record in the design, hook note in the plan Red: 8 new tests failed before this change (e.g. test_openai_cacheable_requires_projected_body, test_null_arguments_tool_is_result_invalid_stream, test_text_mode_choice_index_null_is_zero[None-True]).
This commit is contained in:
@@ -421,3 +421,48 @@ def test_nonstream_null_tool_elements_stripped():
|
||||
assert parse_chat_completion(body).message["tool_calls"] == [tool]
|
||||
body["choices"][0]["message"]["tool_calls"] = None
|
||||
assert parse_chat_completion(body).message["tool_calls"] is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("index", "taken"), [(None, True), (0, True), (False, False), (1, False)])
|
||||
def test_text_mode_choice_index_null_is_zero(index, taken):
|
||||
"""审查 I3: 文本模式 index:null 视缺省(=0);bool 不冒充 0。"""
|
||||
state = ChatCompletionAccumulator(response_mode="text")
|
||||
delta = state.add_chunk(
|
||||
{"choices": [{"index": index, "delta": {"content": "hi"}, "finish_reason": "stop"}]}
|
||||
)
|
||||
assert (delta.content == "hi") is taken and (state.finish_reason == "stop") is taken
|
||||
|
||||
|
||||
def test_null_only_tool_elements_do_not_create_empty_list():
|
||||
"""审查 Minor1: tool_calls null 后 [null] 不得产出会被上游拒收的空数组。"""
|
||||
state = ChatCompletionAccumulator(response_mode="openai")
|
||||
_feed(
|
||||
state,
|
||||
_frame({"role": "assistant", "content": "a", "tool_calls": None}),
|
||||
_frame({"tool_calls": [None]}, finish="stop"),
|
||||
)
|
||||
assert state.finish().message["tool_calls"] is None
|
||||
body = {"choices": [{"message": {"role": "assistant", "content": "a", "tool_calls": [None]}}]}
|
||||
assert parse_chat_completion(body).message["tool_calls"] is None
|
||||
|
||||
|
||||
def test_tool_calls_null_then_list_merges():
|
||||
"""tool_calls 先 null 后列表: 正常聚合工具。"""
|
||||
state = ChatCompletionAccumulator(response_mode="openai")
|
||||
tool = {"index": 0, "id": "a", "type": "function", "function": {"name": "f", "arguments": "{}"}}
|
||||
_feed(
|
||||
state,
|
||||
_frame({"role": "assistant", "tool_calls": None}),
|
||||
_frame({"tool_calls": [tool]}, finish="tool_calls"),
|
||||
)
|
||||
assert state.finish().message["tool_calls"][0]["id"] == "a"
|
||||
|
||||
|
||||
def test_nonstream_role_checked_before_verdict():
|
||||
"""审查 Minor3: 非流式缺 role 是形态失败,先于结局判定。"""
|
||||
from polygateway.transports._openai_completion import check_completion_body
|
||||
|
||||
with pytest.raises(TransientError, match="role"):
|
||||
check_completion_body(
|
||||
{"choices": [{"message": {"content": "a"}, "finish_reason": "length"}]}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user