mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-11-20 13:07:57 +09:00
fix: no review push before AI suggestions are scored; start-review 409 after review ends (v0.290.1) (#1246)
Andrew's job `7f89f884` (2026-11-03) got a "review your lyrics" push, then auto-approved about 3.7 minutes later. Tapping the push afterwards left the app on its loader. **False review push.** The screens worker's first score ran at 00:15:40, before the proactive auto-correct finished at 00:15:46. Without suggestions the lyrics scored `needs-review`, so `hold_for_audio` was false and review opened with its push. The audio worker's re-score then saw the suggestions (`auto/ai-resolved`) and auto-approved. In the last 14 days 5 jobs auto-approved, and 4 of them opened review first (typically about 3.7 minutes before). `route_after_scoring` now waits for the suggestions and scores again when the first pass ran without them, then holds or auto-completes. If suggestions can't be had, or the re-score reaches no verdict, the first verdict stands and review opens as before. **start-review on a finished job.** `complete -> in_review` raised an invalid transition: a 500, an ERROR log (4 in prod for this job, which the error monitor can turn into an #alerts message), and the app retrying as if it were transient. It now returns 409 without attempting the transition. The app's handling is in nomad-mobile #44. **Tests:** `test_review_gate.py` and `test_jobs_api.py` pass with new cases. `test_internal_api.py` has 7 auth test failures locally that also fail on the main clone, so they aren't from this change. @coderabbitai ignore 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_012RYLVTajJiZnzPUt9qsVEJ Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
70bdd0e693
commit
08b146fce0
5 files changed
+134
-2
No files matched your search
@@ -22,7 +22,7 @@ from google.cloud.exceptions import NotFound
|
||||
from backend.utils.loop_bridge import run_in_body_thread, run_on_loop
|
||||
from backend.utils.request_helpers import get_client_ip
|
||||
from backend.services.review_state import ensure_in_review
|
||||
from backend.models.job import Job, JobCreate, JobResponse, JobStatus
|
||||
from backend.models.job import Job, JobCreate, JobResponse, JobStatus, STATE_TRANSITIONS
|
||||
from backend.models.requests import (
|
||||
URLSubmissionRequest,
|
||||
CorrectionsSubmission,
|
||||
@@ -1114,6 +1114,12 @@ def start_review(
|
||||
if not _check_job_ownership(job, auth_result):
|
||||
raise HTTPException(status_code=403, detail=t(locale, "jobs.noPermissionAccess"))
|
||||
|
||||
# The review is over (auto-approved, submitted, finished): an expected answer
|
||||
# for a stale link or notification, not a server error. Clients go to the job.
|
||||
if job.status != JobStatus.IN_REVIEW and JobStatus.IN_REVIEW not in STATE_TRANSITIONS.get(job.status, []):
|
||||
logger.info(f"Job {job_id}: start-review refused, status is {job.status}")
|
||||
raise HTTPException(status_code=409, detail=t(locale, "jobs.reviewCannotStart"))
|
||||
|
||||
# Idempotent: concurrent/duplicate opens (React double-mount, double-tap, two
|
||||
# tabs) must not fail with "in_review -> in_review". Already in review == success,
|
||||
# and the one-time side effects (record_review_started) are not repeated.
|
||||
|
||||
@@ -261,7 +261,8 @@ async def maybe_auto_complete_review(job_id: str, trigger: str) -> Dict[str, Any
|
||||
payload["hold_for_audio"] = hold_for_audio
|
||||
job_manager.update_processing_metadata(job_id, "auto_approval", payload)
|
||||
return {"outcome": "review", "overall_auto": verdict.overall_auto,
|
||||
"blockers": blockers, "hold_for_audio": hold_for_audio}
|
||||
"blockers": blockers, "hold_for_audio": hold_for_audio,
|
||||
"ai_suggestions_available": ai_suggestions is not None}
|
||||
|
||||
# ---- ENFORCE: complete the review exactly like a human would ----
|
||||
# The status gate above used a snapshot read. Re-read now so a human who
|
||||
|
||||
@@ -70,6 +70,9 @@ async def route_after_scoring(
|
||||
from backend.services.job_manager import JobManager
|
||||
|
||||
job_manager = job_manager or JobManager()
|
||||
auto_result = await _rescore_if_suggestions_were_missing(job_id, auto_result, job_log)
|
||||
if auto_result.get("outcome") in ("auto_completed", "wrong_status"):
|
||||
return str(auto_result["outcome"])
|
||||
if not auto_result.get("hold_for_audio"):
|
||||
await enter_human_review(job_id, job_log, job_manager)
|
||||
return "review"
|
||||
@@ -91,6 +94,43 @@ async def route_after_scoring(
|
||||
return "released_elsewhere"
|
||||
|
||||
|
||||
async def _rescore_if_suggestions_were_missing(
|
||||
job_id: str, auto_result: Dict[str, Any], job_log: Optional[logging.Logger] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""Score again if the first pass ran before the AI suggestions landed.
|
||||
|
||||
Lyrics finish moments before the proactive auto-correct, and without its
|
||||
suggestions the lyrics score "needs review". Opening review on that verdict
|
||||
sent the customer a "review your lyrics" push, and the audio worker's re-score
|
||||
then auto-approved the job minutes later (4 of 5 auto-approved jobs,
|
||||
2026-10-20..11-03). Returns the verdict to route on; the original one when
|
||||
suggestions can't be had or the re-score didn't reach a verdict.
|
||||
"""
|
||||
if auto_result.get("outcome") != "review" or auto_result.get("ai_suggestions_available") is not False:
|
||||
return auto_result
|
||||
|
||||
from backend.config import get_settings
|
||||
from backend.services.auto_approval.executor import maybe_auto_complete_review
|
||||
|
||||
if not getattr(get_settings(), "auto_correct_proactive_enabled", False):
|
||||
return auto_result
|
||||
try:
|
||||
from backend.workers.auto_correct_worker import process_proactive_auto_correct
|
||||
|
||||
await process_proactive_auto_correct(job_id) # bounded (180s), joins a run in flight
|
||||
except Exception as e: # noqa: BLE001 best-effort, like ensure_and_pre_apply
|
||||
logger.warning(f"[job:{job_id}] Waiting for AI suggestions before review failed: {e}")
|
||||
return auto_result
|
||||
|
||||
rescored = await maybe_auto_complete_review(job_id, trigger="screens_worker")
|
||||
outcome = rescored.get("outcome")
|
||||
logger.info(f"[job:{job_id}] Re-scored once AI suggestions were ready: {outcome} "
|
||||
f"(hold_for_audio={rescored.get('hold_for_audio', False)})")
|
||||
if outcome == "auto_completed" and job_log:
|
||||
job_log.info("Auto-approved once AI suggestions were ready: review skipped")
|
||||
return rescored if outcome in ("review", "auto_completed", "wrong_status") else auto_result
|
||||
|
||||
|
||||
async def release_audio_hold(job_id: str, reason: str) -> Optional[str]:
|
||||
"""Release a held job (audio worker / stuck-job sweep).
|
||||
|
||||
|
||||
@@ -156,3 +156,80 @@ async def test_review_fallback_crash_restores_hold_for_sweep() -> None:
|
||||
auto_result={"outcome": "review"})
|
||||
assert out == "error"
|
||||
jm.park_review_for_auto_check.assert_called_once_with("j1")
|
||||
|
||||
|
||||
def _suggestion_patches(wait=None):
|
||||
return (
|
||||
patch("backend.workers.auto_correct_worker.process_proactive_auto_correct",
|
||||
wait or AsyncMock()),
|
||||
patch("backend.config.get_settings",
|
||||
return_value=SimpleNamespace(auto_correct_proactive_enabled=True)),
|
||||
)
|
||||
|
||||
|
||||
async def _route_without_suggestions(jm, rescored, wait=None):
|
||||
first = {"outcome": "review", "ai_suggestions_available": False}
|
||||
p_wait, p_settings = _suggestion_patches(wait)
|
||||
with p_wait, p_settings:
|
||||
return await _call(review_gate.route_after_scoring, jm, "j1", first, auto_result=rescored)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_missing_suggestions_rescore_holds_instead_of_notifying() -> None:
|
||||
"""Job 7f89f884: the "review your lyrics" push went out minutes before auto-approval."""
|
||||
jm = _jm(_job(audio_complete=False))
|
||||
out, auto, pre = await _route_without_suggestions(
|
||||
jm, {"outcome": "review", "hold_for_audio": True, "ai_suggestions_available": True})
|
||||
assert out == "held"
|
||||
auto.assert_awaited_once_with("j1", trigger="screens_worker")
|
||||
jm.park_review_for_auto_check.assert_called_once_with("j1")
|
||||
jm.transition_to_state.assert_not_called()
|
||||
pre.assert_not_awaited()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_missing_suggestions_rescore_can_auto_complete() -> None:
|
||||
jm = _jm()
|
||||
out, _, pre = await _route_without_suggestions(jm, {"outcome": "auto_completed"})
|
||||
assert out == "auto_completed"
|
||||
jm.transition_to_state.assert_not_called()
|
||||
pre.assert_not_awaited()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_missing_suggestions_rescore_still_review_opens_review() -> None:
|
||||
jm = _jm()
|
||||
out, auto, pre = await _route_without_suggestions(
|
||||
jm, {"outcome": "review", "hold_for_audio": False, "ai_suggestions_available": True})
|
||||
assert out == "review"
|
||||
auto.assert_awaited_once()
|
||||
pre.assert_awaited_once_with("j1")
|
||||
assert jm.transition_to_state.call_args.kwargs["new_status"] == JobStatus.AWAITING_REVIEW
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_suggestions_wait_failure_still_opens_review() -> None:
|
||||
jm = _jm()
|
||||
out, auto, _ = await _route_without_suggestions(
|
||||
jm, {"outcome": "auto_completed"}, wait=AsyncMock(side_effect=RuntimeError("llm down")))
|
||||
assert out == "review"
|
||||
auto.assert_not_awaited()
|
||||
assert jm.transition_to_state.call_args.kwargs["new_status"] == JobStatus.AWAITING_REVIEW
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_rescore_with_no_verdict_keeps_first_verdict() -> None:
|
||||
jm = _jm()
|
||||
out, _, _ = await _route_without_suggestions(jm, {"outcome": "error"})
|
||||
assert out == "review"
|
||||
assert jm.transition_to_state.call_args.kwargs["new_status"] == JobStatus.AWAITING_REVIEW
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_suggestions_present_scores_only_once() -> None:
|
||||
jm = _jm()
|
||||
out, auto, _ = await _call(
|
||||
review_gate.route_after_scoring, jm, "j1",
|
||||
{"outcome": "review", "ai_suggestions_available": True})
|
||||
assert out == "review"
|
||||
auto.assert_not_awaited()
|
||||
@@ -426,6 +426,14 @@ class TestStartReview:
|
||||
mock_job_manager.transition_to_state.assert_not_called()
|
||||
mock_job_manager.record_review_started.assert_not_called()
|
||||
|
||||
@pytest.mark.parametrize("status", [JobStatus.COMPLETE, JobStatus.REVIEW_COMPLETE, JobStatus.ENCODING])
|
||||
def test_start_review_after_review_ended_is_409(self, client, mock_job_manager, mock_job, auth_headers, status):
|
||||
"""A stale link or notification gets an expected 409, with no transition attempt (which logs ERROR)."""
|
||||
mock_job.status = status
|
||||
response = client.post("/api/jobs/test123/start-review", json={}, headers=auth_headers)
|
||||
assert response.status_code == 409
|
||||
mock_job_manager.transition_to_state.assert_not_called()
|
||||
|
||||
def test_start_review_lost_race_is_success(self, client, mock_job_manager, mock_job, auth_headers):
|
||||
"""Both requests saw awaiting_review; the loser's transition raises but job is in_review."""
|
||||
from backend.exceptions import InvalidStateTransitionError
|
||||
|
||||
Reference in new issue
Block a user