From 462f2467685e6992b647ccf9b92b273026711ecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrew=20Beveridge=20=E2=98=84=EF=B8=8F?= Date: Tue, 15 Sep 2026 05:22:39 -0400 Subject: [PATCH] internal: admin endpoint to re-run auto-approval eval on parked review jobs (#1005) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Adds `POST /api/internal/jobs/{job_id}/auto-approval-eval` (admin-only): re-runs the auto-approval evaluator on a job parked at AWAITING_REVIEW. The KaraokeHunt outreach batch runs with `made_for_you: true` (stale-review exemption), which is also an enforcement blocker at scoring time — this endpoint lets us lift the flag and re-evaluate so confident jobs full-auto through review. ## Testing - `test_internal_api.py::TestAutoApprovalEval` — outcome passthrough + auth required; file green in full-collection run. 🤖 Generated with [Claude Code](https://claude.com/claude-code) ## Summary by CodeRabbit - **New Features** - Administrators can manually re-run auto-approval evaluation for jobs awaiting review. - Jobs remain in review unless the evaluation is fully confident, providing a fail-safe review process. - **Bug Fixes** - Improved batch job processing so an issue updating an optional job setting no longer stops the remaining workflow. - Prevented missing cached results from causing failures during recanonicalization tasks. - **Tests** - Added coverage for authorized evaluation requests and unauthorized access. --------- Co-authored-by: Claude Opus 4.8 --- backend/api/routes/internal.py | 19 +++++++++++++++ backend/tests/test_internal_api.py | 23 +++++++++++++++++++ pyproject.toml | 2 +- .../phase1c_recanonicalize.py | 2 ++ .../phase_batch_generate.py | 14 +++++++---- 5 files changed, 55 insertions(+), 5 deletions(-) diff --git a/backend/api/routes/internal.py b/backend/api/routes/internal.py index c72603e5..6f8bce3d 100644 --- a/backend/api/routes/internal.py +++ b/backend/api/routes/internal.py @@ -185,6 +185,25 @@ async def trigger_audio_worker( ) +@router.post("/jobs/{job_id}/auto-approval-eval") +async def trigger_auto_approval_eval( + job_id: str, + auth_data: Tuple[str, UserType, int] = Depends(require_admin) +): + """ + Re-run the auto-approval evaluation on a job parked at AWAITING_REVIEW + (admin only). Normally scoring runs from the screens/audio workers; this + endpoint lets an admin re-evaluate after conditions change (e.g. a batch + job's made_for_you enforcement blocker was lifted). Fail-safe: anything + short of fully confident leaves the job in review untouched. + """ + from backend.services.auto_approval.executor import maybe_auto_complete_review + + logger.info(f"[job:{job_id}] admin auto-approval eval requested") + result = await maybe_auto_complete_review(job_id, trigger="admin_eval") + return {"job_id": job_id, **result} + + @router.post("/workers/lyrics", response_model=WorkerResponse) async def trigger_lyrics_worker( request: WorkerRequest, diff --git a/backend/tests/test_internal_api.py b/backend/tests/test_internal_api.py index 98848f1c..e49e2113 100644 --- a/backend/tests/test_internal_api.py +++ b/backend/tests/test_internal_api.py @@ -341,3 +341,26 @@ class TestWorkerIdempotency: data = response.json() assert data["status"] == "not_found" + + +class TestAutoApprovalEval: + """POST /api/internal/jobs/{job_id}/auto-approval-eval — admin re-evaluation.""" + + def test_eval_calls_executor_and_returns_outcome(self, client): + with patch( + 'backend.services.auto_approval.executor.maybe_auto_complete_review', + new=AsyncMock(return_value={"outcome": "auto_completed"}), + ) as mock_eval: + response = client.post( + "/api/internal/jobs/test123/auto-approval-eval", + headers={"Authorization": "Bearer test-admin-token"}, + ) + assert response.status_code == 200 + data = response.json() + assert data["job_id"] == "test123" + assert data["outcome"] == "auto_completed" + mock_eval.assert_awaited_once_with("test123", trigger="admin_eval") + + def test_eval_requires_admin(self, client): + response = client.post("/api/internal/jobs/test123/auto-approval-eval") + assert response.status_code == 401 diff --git a/pyproject.toml b/pyproject.toml index 0733a612..49879d78 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "karaoke-gen" -version = "0.225.6" +version = "0.225.7" description = "Generate karaoke videos with synchronized lyrics. Handles the entire process from downloading audio and lyrics to creating the final video with title screens." authors = ["Andrew Beveridge "] license = "MIT" diff --git a/scripts/karaokehunt_outreach/phase1c_recanonicalize.py b/scripts/karaokehunt_outreach/phase1c_recanonicalize.py index 86369e93..8dbd6b09 100644 --- a/scripts/karaokehunt_outreach/phase1c_recanonicalize.py +++ b/scripts/karaokehunt_outreach/phase1c_recanonicalize.py @@ -154,6 +154,8 @@ def main() -> int: # --- 2. re-search corrected names (research + community-first fallbacks) --- to_search = [] for k, d in decisions.items(): + if k not in cache: + continue if d["action"] == "research" or \ (d["action"] == "community-first" and not cache[k].get("recheck_v2", {}).get("available")): if "search_v2" not in cache[k]: diff --git a/scripts/karaokehunt_outreach/phase_batch_generate.py b/scripts/karaokehunt_outreach/phase_batch_generate.py index 31db3301..6b3fd136 100644 --- a/scripts/karaokehunt_outreach/phase_batch_generate.py +++ b/scripts/karaokehunt_outreach/phase_batch_generate.py @@ -195,10 +195,16 @@ def main() -> int: job_id = job.get("job_id") or job.get("id") entry["job_id"] = job_id entry["submitted_at"] = now_iso() - # Exempt from stale-review auto-expiry immediately. - st, _ = api_post(f"/api/admin/jobs/{job_id}", - {"made_for_you": True}, args.token, method="PATCH") - entry["made_for_you_set"] = (st == 200) + # Exempt from stale-review auto-expiry immediately. A PATCH + # failure must not mask the successful job creation — record + # it so a later pass can re-flag. + try: + st, _ = api_post(f"/api/admin/jobs/{job_id}", + {"made_for_you": True}, args.token, method="PATCH") + entry["made_for_you_set"] = (st == 200) + except Exception as pe: + entry["made_for_you_set"] = False + entry["mfy_error"] = str(pe)[:200] msg = (f"OK {a} – {t}: job {job_id} " f"[{u['kind']}] mfy={'✅' if entry['made_for_you_set'] else '❌'}") print(f" {msg}", flush=True)