refactor(task): 引入 MutableRequestBodyState 替代 request_body_ref 字典容器

将请求体可变状态从 {"body": dict} 字典容器重构为独立的
MutableRequestBodyState 类,统一管理 original_body / current_body /
build_attempt_body / rectify 等语义,消除各层通过 ref["body"] 间接
读写的隐式约定。

- 新增 src/services/task/request_state.py 定义 Protocol 与实现
- handler/executor/mixin 层改用 request_state 参数传递
- error_handler/state_transition 通过 request_state 判断整流状态
- 新增 request_state 单元测试与 chat/cli 请求体隔离测试
This commit is contained in:
fawney19
2026-03-18 13:43:39 +08:00
parent 53ef35ec80
commit cbb66a5667
16 changed files with 374 additions and 85 deletions

View File

@@ -0,0 +1,32 @@
from __future__ import annotations
from src.services.task.request_state import MutableRequestBodyState
def test_mutable_request_body_state_keeps_original_and_attempts_isolated() -> None:
original = {
"model": "gpt-5",
"input": [{"role": "user", "content": [{"type": "input_text", "text": "hello"}]}],
}
state = MutableRequestBodyState(original)
first_attempt = state.build_attempt_body()
first_attempt["input"][0]["content"][0]["text"] = "attempt-1"
assert original["input"][0]["content"][0]["text"] == "hello"
assert state.current_body["input"][0]["content"][0]["text"] == "hello"
rectified = state.build_attempt_body()
rectified["input"][0]["content"][0]["text"] = "rectified"
state.mark_rectified(rectified, stage=1)
second_attempt = state.build_attempt_body()
second_attempt["input"][0]["content"][0]["text"] = "attempt-2"
assert state.is_rectified() is True
assert state.rectify_stage() == 1
assert state.current_body["input"][0]["content"][0]["text"] == "rectified"
assert original["input"][0]["content"][0]["text"] == "hello"
assert state.consume_rectified_this_turn() is True
assert state.consume_rectified_this_turn() is False