105 lines
4.4 KiB
Python
105 lines
4.4 KiB
Python
import json
|
|
|
|
from sqlalchemy import select
|
|
|
|
from app.models import WorkflowRun, WorkflowStep
|
|
from app.services.turn_review import POLICY, candidates, manually_accepted
|
|
from test_dialogue_workflow import asset
|
|
from test_native_dialogue_workflow import native_story, _start, capabilities
|
|
|
|
|
|
async def test_named_review_is_scoped_to_frozen_report_and_replay_safe(system):
|
|
capabilities(system)
|
|
did, draft = await native_story(system)
|
|
sid = draft["manifest"]["shot_ids"][0]
|
|
rid = await _start(system, did, draft["version"])
|
|
async with system.app.state.db.session() as session:
|
|
run = await session.get(WorkflowRun, rid)
|
|
steps = (
|
|
await session.scalars(select(WorkflowStep).where(WorkflowStep.run_id == rid))
|
|
).all()
|
|
gate = next(s for s in steps if s.kind == "dialogue.accepted")
|
|
video_step = next(s for s in steps if s.step_key == gate.recipe_json["video_key"])
|
|
video = await asset(session, system, run.owner_id, "video", b"test-video")
|
|
roles = ["a" * 32, "b" * 32]
|
|
intent = [
|
|
dict(utterance_id=str(i) * 32, speaker_id=speaker) for i, speaker in enumerate(roles)
|
|
]
|
|
turns = [
|
|
dict(
|
|
utterance_id=t["utterance_id"],
|
|
expected_speaker_id=t["speaker_id"],
|
|
listener_id=roles[1 - i],
|
|
start_sample=i * 48000,
|
|
end_sample=(i + 1) * 48000,
|
|
decision="indeterminate",
|
|
)
|
|
for i, t in enumerate(intent)
|
|
]
|
|
report = dict(
|
|
policy=POLICY,
|
|
decision="indeterminate",
|
|
turns=turns,
|
|
source_sha256={"video": video.sha256},
|
|
)
|
|
check = await asset(session, system, run.owner_id, "json", json.dumps(report).encode())
|
|
binding = dict(kind="json", asset_id=check.id, sha256=check.sha256, report=report)
|
|
video_step.result_json = {
|
|
"asset_id": video.id,
|
|
"sha256": video.sha256,
|
|
"duration_frames": 124,
|
|
}
|
|
audit = WorkflowStep(
|
|
run_id=rid,
|
|
step_key="nt_fixture",
|
|
kind="audio.dialogue.inspect",
|
|
semantic_input_hash="a" * 64,
|
|
state="succeeded",
|
|
result_json=binding,
|
|
resolved_input_json={"parameters": {"speaker_ids": roles, "turns": intent}},
|
|
)
|
|
session.add(audit)
|
|
gate.recipe_json = {**gate.recipe_json, "turn_report_key": audit.step_key}
|
|
gate.state = "awaiting_review"
|
|
gate.result_json = {"review_request": {"asset_id": check.id, "sha256": check.sha256}}
|
|
run.spec_snapshot = {**run.spec_snapshot, "schema_version": 6}
|
|
run.state, run.error_json = (
|
|
"blocked",
|
|
{"code": "native_turn_review_required", "step_key": gate.step_key},
|
|
)
|
|
await session.commit()
|
|
assert (await candidates(session, run, [*steps, audit]))[0]["can_accept"]
|
|
from app.services.turn_review import reviewable_turns
|
|
|
|
for invalid in ([], [*turns[:1], {**turns[1], "decision": "reject"}], turns[::-1]):
|
|
assert not reviewable_turns(
|
|
audit,
|
|
gate,
|
|
{**binding, "report": {**report, "turns": invalid}},
|
|
video_step.result_json,
|
|
)
|
|
body = dict(
|
|
expected_version=run.version,
|
|
report_sha256=check.sha256,
|
|
decision="accept",
|
|
note="已逐轮试听",
|
|
)
|
|
url = f"/api/editors/{did}/runs/{rid}/turns/{sid}/review"
|
|
invalid = await system.client.post(
|
|
url, json={**body, "report_sha256": "f" * 64}, headers={"Idempotency-Key": "wrong"}
|
|
)
|
|
assert invalid.status_code == 409
|
|
reply = await system.client.post(url, json=body, headers={"Idempotency-Key": "review"})
|
|
assert reply.status_code == 200, reply.text
|
|
assert reply.json()["run"]["state"] == "queued"
|
|
assert reply.json()["run"]["turn_reviews"][0]["manual_review"]["reviewer_id"]
|
|
replay = await system.client.post(url, json=body, headers={"Idempotency-Key": "review"})
|
|
assert (
|
|
replay.status_code == 200
|
|
and replay.json()["run"]["version"] == reply.json()["run"]["version"]
|
|
)
|
|
async with system.app.state.db.session() as session:
|
|
run = await session.get(WorkflowRun, rid)
|
|
assert await manually_accepted(session, run, gate, binding)
|
|
assert not await manually_accepted(session, run, gate, {**binding, "sha256": "b" * 64})
|