115 行
4.1 KiB
Python
115 行
4.1 KiB
Python
from __future__ import annotations
|
||
|
||
import json
|
||
|
||
from backend.services.workflow_round_feedback_service import WorkflowRoundFeedbackService
|
||
|
||
|
||
CANDIDATES = {
|
||
"TX-15-01": "candidate-opaque-a",
|
||
"TX-15-02": "candidate-opaque-b",
|
||
"TX-15-03": "candidate-opaque-c",
|
||
}
|
||
|
||
|
||
def test_model_receives_bounded_prompt_context_without_opaque_candidate_ids() -> None:
|
||
captured: list[dict] = []
|
||
|
||
def complete(payload: dict):
|
||
captured.append(payload)
|
||
return {
|
||
"feedback": {
|
||
"liked": ["TX-15-01", "TX-15-02"],
|
||
"strict_rejected": ["TX-15-03"],
|
||
},
|
||
"positive_patch": "keep the stronger face and silhouette",
|
||
"negative_patch": "avoid the extra shoulder ornament",
|
||
"material_intent": "black wool coat",
|
||
}
|
||
|
||
service = WorkflowRoundFeedbackService(
|
||
structured_text_callable=complete,
|
||
model_route="prompt-planning-route",
|
||
)
|
||
result = service.interpret(
|
||
"1、2喜欢,3严格拒绝;下一轮加强脸部;素材:黑色外套",
|
||
candidate_whitelist=CANDIDATES,
|
||
candidate_context={
|
||
"TX-15-01": {
|
||
"positive_prompt": "silver-haired male knight, close portrait",
|
||
"negative_prompt": "blurry face",
|
||
"model_label": "adapter-a / model-a",
|
||
},
|
||
"TX-15-02": {
|
||
"positive_prompt": "silver-haired male knight, clean silhouette",
|
||
"negative_prompt": "extra fingers",
|
||
"model_label": "adapter-b / model-b",
|
||
},
|
||
"TX-15-03": {
|
||
"positive_prompt": "silver-haired male knight, shoulder ornament",
|
||
"negative_prompt": "",
|
||
"model_label": "adapter-c / model-c",
|
||
},
|
||
},
|
||
)
|
||
|
||
serialized = json.dumps(captured[0], ensure_ascii=False)
|
||
assert "candidate-opaque" not in serialized
|
||
assert "candidate_id" not in serialized
|
||
assert captured[0]["candidate_whitelist"][0]["positive_prompt"] == (
|
||
"silver-haired male knight, close portrait"
|
||
)
|
||
assert captured[0]["candidate_whitelist"][1]["model_label"] == "adapter-b / model-b"
|
||
assert [item.display_code for item in result.liked] == ["TX-15-01", "TX-15-02"]
|
||
assert result.negative_patch == "avoid the extra shoulder ornament"
|
||
assert result.source == "llm_validated"
|
||
|
||
|
||
def test_hallucinated_candidate_id_is_discarded_and_local_parser_restarts_cleanly() -> None:
|
||
def complete(_payload: dict):
|
||
return {
|
||
"feedback": {
|
||
"liked": [
|
||
{
|
||
"display_code": "TX-15-01",
|
||
"candidate_id": "candidate-invented",
|
||
}
|
||
]
|
||
},
|
||
"positive_patch": "must not survive fallback",
|
||
}
|
||
|
||
service = WorkflowRoundFeedbackService(
|
||
structured_text_callable=complete,
|
||
model_route="prompt-planning-route",
|
||
)
|
||
result = service.interpret(
|
||
"1喜欢,3严格拒绝;下一轮:保留短发",
|
||
candidate_whitelist=CANDIDATES,
|
||
)
|
||
|
||
assert [item.candidate_id for item in result.liked] == ["candidate-opaque-a"]
|
||
assert [item.candidate_id for item in result.strict_rejected] == ["candidate-opaque-c"]
|
||
assert result.positive_patch == "保留短发"
|
||
assert result.source == "deterministic_fallback"
|
||
assert result.metadata["fallback_reason"] == "invalid_llm_result"
|
||
|
||
|
||
def test_provider_failure_uses_deterministic_feedback_without_model_patch_leakage() -> None:
|
||
def complete(_payload: dict):
|
||
raise TimeoutError("provider request details must not leak")
|
||
|
||
service = WorkflowRoundFeedbackService(
|
||
structured_text_callable=complete,
|
||
model_route="prompt-planning-route",
|
||
)
|
||
result = service.interpret(
|
||
"TX-15-02采用",
|
||
candidate_whitelist=CANDIDATES,
|
||
)
|
||
|
||
assert result.final is not None
|
||
assert result.final.candidate_id == "candidate-opaque-b"
|
||
assert result.positive_patch is None
|
||
assert result.source == "deterministic_fallback"
|
||
assert result.metadata["fallback_reason"] == "TimeoutError"
|