fix(backends): make streamed replies and session state consistent
- Validate stage prefixes before forwarding model tokens. Invalid prefixes may use the existing repair budget; once output starts, validation failure aborts the turn without appending a regenerated reply. Bound the token queue and close inner streams when consumption stops. - Replace delete-time lock eviction with reference-counted session locks covering holders and waiters. Serialize set/get with chat/stream/delete and release cancelled waiters without replacing a live lock. - Store person synonyms under workflow-native driver keys and expose consistent alias views to single/group reads and graph prompt injection. - Separate committed field patches from public formUpdate visibility. Commit callbacks keep repository fields synchronized even when updates are hidden or the consumer closes after graph completion. - Disable OpenAI SDK automatic retries. Own bounded retry attempts in the application, use 1/2/4 second backoff, retry streams only before text is emitted, enforce attempt timeouts, and close upstream/client resources. - Translate FastGPT SDK max_retries (total attempts) from the configured extra-retry count, and verify the real SDK loop uses four attempts. - Document the per-LLM budget and streaming failure semantics without claiming the whole multi-call HTTP turn has the same timeout. Validation: 267 offline tests passed, including actual SDK requests through MockTransport, stream repair/abort, timeout/close, lock races, aliases and formUpdate visibility; existing four dependency deprecation warnings.
This commit is contained in:
@@ -52,55 +52,60 @@ def make_service(form_update=None):
|
||||
|
||||
# ---- set/get:白名单、分组、外部表示 ----
|
||||
|
||||
def test_set_then_get_single_key_roundtrip():
|
||||
@pytest.mark.asyncio
|
||||
async def test_set_then_get_single_key_roundtrip():
|
||||
service, backend = make_service()
|
||||
code, msg = service.set_info("s1", "hphm1", "苏B12345")
|
||||
code, msg = await service.set_info("s1", "hphm1", "苏B12345")
|
||||
assert (code, msg) == ("200", "")
|
||||
assert json.loads(service.get_info("s1", "hphm1")) == "苏B12345"
|
||||
assert json.loads(await service.get_info("s1", "hphm1")) == "苏B12345"
|
||||
# set/get 零模型调用
|
||||
assert backend.llm_calls == 0
|
||||
|
||||
|
||||
def test_get_groups_match_existing_contract():
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_groups_match_existing_contract():
|
||||
service, _ = make_service()
|
||||
service.set_info("s1", "ywrysw", "0") # False
|
||||
service.set_info("s1", "jdcsl", "0") # int 0
|
||||
service.set_info("s1", "xm2", "张三")
|
||||
await service.set_info("s1", "ywrysw", "0") # False
|
||||
await service.set_info("s1", "jdcsl", "0") # int 0
|
||||
await service.set_info("s1", "xm2", "张三")
|
||||
|
||||
acd = json.loads(service.get_info("s1", "acdinfo"))
|
||||
acd = json.loads(await service.get_info("s1", "acdinfo"))
|
||||
assert acd["ywrysw"] == "0" # False -> "0"
|
||||
assert acd["jdcsl"] == "0" # int 0 -> "0"(内部类型不同)
|
||||
assert acd["sgyy"] == "" # 未知 -> 空串
|
||||
|
||||
human2 = json.loads(service.get_info("s1", "acdhuman2"))
|
||||
human2 = json.loads(await service.get_info("s1", "acdhuman2"))
|
||||
assert human2["xm2"] == "张三"
|
||||
assert human2["sfzmwh2"] == "" # 全字段齐全,未填为空
|
||||
|
||||
all_groups = json.loads(service.get_info("s1", "all"))
|
||||
all_groups = json.loads(await service.get_info("s1", "all"))
|
||||
assert set(all_groups) == {"acdinfo", "acdhuman1", "acdhuman2"}
|
||||
|
||||
|
||||
def test_get_unknown_key_returns_json_empty_string():
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_unknown_key_returns_json_empty_string():
|
||||
"""兼容现状:未知 key 返回 JSON 空串 '""'。"""
|
||||
service, _ = make_service()
|
||||
service.set_info("s1", "hphm1", "苏B12345")
|
||||
assert service.get_info("s1", "no_such_key") == '""'
|
||||
assert service.get_info("ghost-session", "hphm1") == '""'
|
||||
await service.set_info("s1", "hphm1", "苏B12345")
|
||||
assert await service.get_info("s1", "no_such_key") == '""'
|
||||
assert await service.get_info("ghost-session", "hphm1") == '""'
|
||||
|
||||
|
||||
def test_set_unknown_or_internal_key_rejected():
|
||||
@pytest.mark.asyncio
|
||||
async def test_set_unknown_or_internal_key_rejected():
|
||||
service, backend = make_service()
|
||||
code, msg = service.set_info("s1", "no_such_key", "x")
|
||||
code, msg = await service.set_info("s1", "no_such_key", "x")
|
||||
assert code == "500" and msg == SET_INFO_INVALID_KEY_MSG
|
||||
code, _ = service.set_info("s1", "stage_code", "1002")
|
||||
code, _ = await service.set_info("s1", "stage_code", "1002")
|
||||
assert code == "500" # 内部字段保护
|
||||
assert service.get_info("s1", "hphm1") == '""' # 未被污染
|
||||
assert await service.get_info("s1", "hphm1") == '""' # 未被污染
|
||||
assert backend.llm_calls == 0
|
||||
|
||||
|
||||
def test_set_rejects_unparsable_value():
|
||||
@pytest.mark.asyncio
|
||||
async def test_set_rejects_unparsable_value():
|
||||
service, _ = make_service()
|
||||
code, msg = service.set_info("s1", "ywrysw", "maybe")
|
||||
code, msg = await service.set_info("s1", "ywrysw", "maybe")
|
||||
assert code == "500" and "无法解析" in msg
|
||||
|
||||
|
||||
@@ -110,8 +115,8 @@ def test_set_rejects_unparsable_value():
|
||||
async def test_chat_sees_set_info_values_and_writes_back_patch():
|
||||
"""set_info 写入 -> chat 输入注入 -> 图差量写回 -> get_info 读到。"""
|
||||
service, backend = make_service(form_update={"ywrysw": False})
|
||||
service.set_info("s1", "hphm1", "苏B12345")
|
||||
service.set_info("s1", "xm2", "李四")
|
||||
await service.set_info("s1", "hphm1", "苏B12345")
|
||||
await service.set_info("s1", "xm2", "李四")
|
||||
|
||||
result = await service.chat(
|
||||
_input("s1", "我是刚才报警的驾驶员")
|
||||
@@ -124,8 +129,8 @@ async def test_chat_sees_set_info_values_and_writes_back_patch():
|
||||
assert injected["person_fields"]["xm2"] == "李四"
|
||||
|
||||
# 图产出的差量写回 repository,get_info 立即可读
|
||||
assert json.loads(service.get_info("s1", "acdinfo"))["ywrysw"] == "0"
|
||||
assert json.loads(service.get_info("s1", "hphm1")) == "苏B12345"
|
||||
assert json.loads(await service.get_info("s1", "acdinfo"))["ywrysw"] == "0"
|
||||
assert json.loads(await service.get_info("s1", "hphm1")) == "苏B12345"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -135,21 +140,21 @@ async def test_chat_form_patch_mapped_to_business_keys():
|
||||
"has_injury": False, "vehicle_count": 2, "accident_cause": "追尾",
|
||||
})
|
||||
await service.chat(_input("s1", "两辆汽车追尾,没有人受伤"))
|
||||
acd = json.loads(service.get_info("s1", "acdinfo"))
|
||||
acd = json.loads(await service.get_info("s1", "acdinfo"))
|
||||
assert acd["ywrysw"] == "0"
|
||||
assert acd["jdcsl"] == "2"
|
||||
assert acd["sgyy"] == "追尾"
|
||||
# 表单键本身不落 repository(未知业务键 -> JSON 空串)
|
||||
assert json.loads(service.get_info("s1", "has_injury")) == ""
|
||||
assert json.loads(await service.get_info("s1", "has_injury")) == ""
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_set_info_acd_value_mapped_into_form_injection():
|
||||
"""set_info 写业务键 -> chat 注入为图表单键(反向映射)。"""
|
||||
service, backend = make_service()
|
||||
service.set_info("s1", "ywrysw", "0")
|
||||
service.set_info("s1", "jdcsl", "2")
|
||||
service.set_info("s1", "bjrjs", "1") # 无表单来源,不注入
|
||||
await service.set_info("s1", "ywrysw", "0")
|
||||
await service.set_info("s1", "jdcsl", "2")
|
||||
await service.set_info("s1", "bjrjs", "1") # 无表单来源,不注入
|
||||
await service.chat(_input("s1", "继续办理"))
|
||||
injected = backend.calls[0].injected_fields
|
||||
assert injected["accident_fields"] == {"has_injury": False, "vehicle_count": 2}
|
||||
@@ -166,7 +171,7 @@ async def test_chat_without_business_state_passes_no_injection():
|
||||
async def test_stream_injects_fields_writes_back_form_update():
|
||||
"""流式:注入同 chat;FormUpdate 事件差量捕获写回后透传。"""
|
||||
service, backend = make_service(form_update={"has_injury": False})
|
||||
service.set_info("s1", "xm1", "王五")
|
||||
await service.set_info("s1", "xm1", "王五")
|
||||
events = [
|
||||
event async for event in service.stream(_input("s1", "继续办理"))
|
||||
]
|
||||
@@ -174,7 +179,7 @@ async def test_stream_injects_fields_writes_back_form_update():
|
||||
assert kinds == ["TextDelta", "FormUpdate", "TextDelta"]
|
||||
assert backend.stream_calls[0].injected_fields["person_fields"]["xm1"] == "王五"
|
||||
# FormUpdate 差量已写回 repository(表单键 -> 业务键)
|
||||
assert json.loads(service.get_info("s1", "acdinfo"))["ywrysw"] == "0"
|
||||
assert json.loads(await service.get_info("s1", "acdinfo"))["ywrysw"] == "0"
|
||||
|
||||
|
||||
# ---- delete:清理 + 幂等 ----
|
||||
@@ -187,13 +192,13 @@ async def test_delete_clears_business_state_and_is_idempotent():
|
||||
drops.append(session_id)
|
||||
|
||||
service, backend = make_service()
|
||||
service.set_info("s1", "hphm1", "苏B12345")
|
||||
await service.set_info("s1", "hphm1", "苏B12345")
|
||||
await service.chat(_input("s1", "你好"))
|
||||
|
||||
await service.delete_session("s1", drop_checkpoint=drop_checkpoint)
|
||||
assert drops == ["s1"]
|
||||
# 再读为空、再聊天不注入旧数据
|
||||
assert service.get_info("s1", "hphm1") == '""'
|
||||
assert await service.get_info("s1", "hphm1") == '""'
|
||||
await service.chat(_input("s1", "还在吗"))
|
||||
assert backend.calls[-1].injected_fields is None
|
||||
|
||||
@@ -210,16 +215,16 @@ async def test_delete_tolerates_missing_checkpoint():
|
||||
raise RuntimeError("no such thread")
|
||||
|
||||
service, _ = make_service()
|
||||
service.set_info("s1", "xm1", "王五")
|
||||
await service.set_info("s1", "xm1", "王五")
|
||||
# checkpoint 不存在视为删除成功的一部分,不向上抛
|
||||
await service.delete_session("s1", drop_checkpoint=missing)
|
||||
assert service.get_info("s1", "xm1") == '""'
|
||||
assert await service.get_info("s1", "xm1") == '""'
|
||||
|
||||
|
||||
def _input(session_id, text):
|
||||
from src.backends.chat import ChatInput
|
||||
|
||||
return ChatInput(session_id=session_id, text=text, client_mode="direct")
|
||||
return ChatInput(session_id=session_id, text=text, client_mode="direct", need_form_update=True)
|
||||
|
||||
|
||||
# ---- 并发隔离(A2):同会话串行、不同会话并行、delete 与 chat 互斥 ----
|
||||
@@ -311,13 +316,13 @@ async def test_concurrent_delete_serialization():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_concurrent_chat_and_set_info_no_corruption():
|
||||
"""set_info(同步无锁)与并发 chat 交错:repository 键值保持完整。"""
|
||||
"""set_info 与 chat 按同一会话串行,最终保留写入的完整车牌。"""
|
||||
backend = SerialProbeBackend()
|
||||
service = AccidentAppService(backend, InMemorySessionRepository())
|
||||
results = await asyncio.gather(
|
||||
service.chat(_input("s1", "a")),
|
||||
asyncio.to_thread(service.set_info, "s1", "hphm1", "苏B12345"),
|
||||
service.set_info("s1", "hphm1", "苏B12345"),
|
||||
)
|
||||
assert results[1] == ("200", "")
|
||||
# 无论时序如何,最终两个写者之一的车牌值完整存在(无撕裂)
|
||||
assert json.loads(service.get_info("s1", "hphm1")) in ("苏B12345", "")
|
||||
# set 写入在 chat 结束后执行,不能丢失。
|
||||
assert json.loads(await service.get_info("s1", "hphm1")) == "苏B12345"
|
||||
|
||||
Reference in New Issue
Block a user