mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-11-20 13:07:57 +09:00
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>
565 lines
26 KiB
Python
565 lines
26 KiB
Python
"""Auto-approval executor: complete the combined review without a human when safe.
|
|
|
|
Called from two places (covering both orderings of audio vs lyrics completion):
|
|
- ``screens_worker`` just before it would transition to AWAITING_REVIEW
|
|
(trigger="screens_worker", job still GENERATING_SCREENS), and
|
|
- ``audio_worker`` after stems + backing analysis land, when the job is already
|
|
sitting in AWAITING_REVIEW (trigger="audio_worker").
|
|
|
|
Every call scores the job and records the verdict in
|
|
``processing_metadata.auto_approval`` (so shadow data accumulates on every job).
|
|
Enforcement — actually skipping the review screens — happens only when ALL of:
|
|
- the feature flag is on and the job's ``review_mode`` is "auto" (the default);
|
|
- the job is not a made-for-you order (the human QC pass is part of that
|
|
product);
|
|
- the job is ELIGIBLE: with a user-supplied instrumental (tenant bulk uploads,
|
|
the existing-instrumental upload option, mute-region edits) there is no
|
|
instrumental decision to make, so confident lyrics alone qualify and the
|
|
selection is "custom" — mirroring the human tenant complete-review flow.
|
|
Otherwise the scorer's ``overall_auto`` must be True (confident lyrics AND a
|
|
non-subjective backing decision);
|
|
- audio separation is complete and the required instrumental exists (clean or
|
|
with-backing stem; the video worker validates custom sources itself).
|
|
|
|
The enforce path replicates exactly what a human clicking "Complete Review"
|
|
does after the UI's on-load auto-apply: apply the cached AI suggestions
|
|
(conflict groups resolved identically), save ``corrections_updated.json``,
|
|
store the instrumental selection, clear worker progress keys, transition to
|
|
REVIEW_COMPLETE and trigger the render worker.
|
|
|
|
FAIL-SAFE: any anomaly (stale suggestions, duplicate-word artifacts, missing
|
|
data, exceptions) aborts enforcement and the job proceeds to normal human
|
|
review. Auto-approval must never be the reason a job fails.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import hashlib
|
|
import json
|
|
import logging
|
|
from datetime import datetime, timezone
|
|
from typing import Any, Dict, List, Optional, Tuple
|
|
|
|
from backend.models.job import JobStatus
|
|
from backend.services.auto_approval.instrumental import (
|
|
backing_decision,
|
|
has_custom_instrumental,
|
|
)
|
|
from backend.services.auto_approval.models import LyricsVerdict
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
CORRECTIONS_PATH = "jobs/{job_id}/lyrics/corrections.json"
|
|
CORRECTIONS_UPDATED_PATH = "jobs/{job_id}/lyrics/corrections_updated.json"
|
|
AUTO_CORRECT_CACHE_PREFIX = "jobs/{job_id}/lyrics/auto_correct_cache/"
|
|
|
|
REVIEW_MODE_AUTO = "auto"
|
|
REVIEW_MODE_ALWAYS_REVIEW = "always_review"
|
|
|
|
|
|
def _load_ai_suggestions(storage, job_id: str) -> Optional[List[Dict[str, Any]]]:
|
|
"""The proactive auto-correct cache the review UI would auto-apply on load."""
|
|
try:
|
|
prefix = AUTO_CORRECT_CACHE_PREFIX.format(job_id=job_id)
|
|
for cache_path in sorted(storage.list_files(prefix)):
|
|
cached = storage.download_json(cache_path)
|
|
suggestions = cached.get("suggestions")
|
|
if isinstance(suggestions, list):
|
|
return suggestions
|
|
except Exception as e:
|
|
logger.info(f"[job:{job_id}] auto-approval: no auto-correct cache readable ({e})")
|
|
return None
|
|
|
|
|
|
def _enforcement_blockers(job, settings) -> List[str]:
|
|
"""Why this job may not be ENFORCED (shadow scoring still happens)."""
|
|
blockers: List[str] = []
|
|
if not settings.auto_approval_enforce_enabled:
|
|
blockers.append("flag_disabled")
|
|
review_mode = getattr(job, "review_mode", REVIEW_MODE_AUTO) or REVIEW_MODE_AUTO
|
|
if review_mode != REVIEW_MODE_AUTO:
|
|
blockers.append(f"review_mode:{review_mode}")
|
|
if getattr(job, "made_for_you", False):
|
|
blockers.append("made_for_you")
|
|
return blockers
|
|
|
|
|
|
async def maybe_auto_complete_review(job_id: str, trigger: str) -> Dict[str, Any]:
|
|
"""Score the job, record the verdict, and auto-complete the review if safe.
|
|
|
|
Returns a small status dict; ``{"outcome": "auto_completed"}`` means the
|
|
review screens were skipped and the render worker has been triggered.
|
|
Never raises.
|
|
"""
|
|
from backend.config import get_settings
|
|
from backend.services.auto_approval.scorer import score_job
|
|
from backend.services.job_manager import JobManager
|
|
from backend.services.storage_service import StorageService
|
|
|
|
outcome: Dict[str, Any] = {"outcome": "skipped"}
|
|
try:
|
|
settings = get_settings()
|
|
job_manager = JobManager()
|
|
storage = StorageService()
|
|
|
|
job = job_manager.get_job(job_id)
|
|
if not job:
|
|
return {"outcome": "no_job"}
|
|
|
|
# Only act on the two known launch points; a human already in the
|
|
# editor (IN_REVIEW) or any other state is out of scope.
|
|
# "audio_hold" = re-score of a job the screens worker held out of
|
|
# review while stems separated (review_gate.release_audio_hold); the
|
|
# job is still GENERATING_SCREENS, exactly as on the first pass.
|
|
if trigger in ("screens_worker", "audio_hold"):
|
|
expected_statuses = (JobStatus.GENERATING_SCREENS,)
|
|
else:
|
|
expected_statuses = (JobStatus.AWAITING_REVIEW,)
|
|
if job.status not in expected_statuses:
|
|
return {"outcome": "wrong_status", "status": str(job.status)}
|
|
if job.state_data.get("instrumental_selection"):
|
|
return {"outcome": "already_selected"}
|
|
|
|
corrections_path = CORRECTIONS_PATH.format(job_id=job_id)
|
|
if not storage.file_exists(corrections_path):
|
|
return {"outcome": "no_corrections"}
|
|
corrections = storage.download_json(corrections_path)
|
|
|
|
ai_suggestions = _load_ai_suggestions(storage, job_id)
|
|
state_data = job.state_data or {}
|
|
backing_analysis = state_data.get("backing_vocals_analysis")
|
|
audio_complete = bool(state_data.get("audio_complete", False))
|
|
stems = (job.file_urls or {}).get("stems", {}) if job.file_urls else {}
|
|
|
|
# Missing-lyrics gate input: sung stretches the transcription has no words
|
|
# for. Gates only when reference lyrics put lines in the gap ("evidenced");
|
|
# fail-OPEN on pending audio / analysis errors (recorded below). Stem
|
|
# download + decode: keep it off the event loop.
|
|
vocal_gaps_info, vocal_gaps = await asyncio.to_thread(
|
|
_vocal_gaps_shadow,
|
|
job_id, job, corrections, ai_suggestions, stems, audio_complete, storage, job_manager,
|
|
)
|
|
|
|
verdict = score_job(corrections, backing_analysis, ai_suggestions, vocal_gaps=vocal_gaps)
|
|
|
|
# Timing-plausibility gate: word timing vs the lead-vocal stem. Audio
|
|
# IO (~15MB stem + a few seconds of numpy), so it runs only when the
|
|
# text signals would otherwise let the lyrics auto-ship — the only
|
|
# case where it can change the outcome. Fail-OPEN on analysis errors
|
|
# (recorded below; an environmental break must not demote the whole
|
|
# auto class), fail-CLOSED on pending audio (the audio_worker
|
|
# second-chance trigger re-scores once stems exist, and the C1 summary
|
|
# keeps the lyrics screen until then).
|
|
timing_info: Dict[str, Any] = {"status": "not_needed"}
|
|
if verdict.lyrics.verdict == LyricsVerdict.AUTO:
|
|
if not getattr(settings, "auto_approval_timing_gate_enabled", True):
|
|
timing_info = {"status": "disabled"}
|
|
elif not audio_complete:
|
|
timing_info = {"status": "pending_audio"}
|
|
else:
|
|
signals = await asyncio.to_thread(
|
|
_compute_timing_signals,
|
|
job_id, corrections, ai_suggestions, stems, storage,
|
|
)
|
|
if signals is None:
|
|
timing_info = {"status": "no_lead_stem"}
|
|
elif signals.error:
|
|
timing_info = {"status": "error", "error": signals.error}
|
|
else:
|
|
timing_info = {"status": "checked", **signals.to_dict()}
|
|
if signals.fired:
|
|
verdict = score_job(
|
|
corrections, backing_analysis, ai_suggestions,
|
|
timing_signals=signals, vocal_gaps=vocal_gaps,
|
|
)
|
|
|
|
blockers = _enforcement_blockers(job, settings)
|
|
custom_instrumental = has_custom_instrumental(job)
|
|
backing_preference = getattr(job, "backing_preference", "auto") or "auto"
|
|
backing_keep_enabled = getattr(settings, "auto_approval_backing_keep_enabled", False)
|
|
# Single source of truth for the instrumental decision (shared with the
|
|
# complete-review "auto" resolver and the per-screen-skip UI summary).
|
|
decision = backing_decision(
|
|
backing_verdict=verdict.backing.verdict.value,
|
|
backing_non_subjective=verdict.backing.non_subjective,
|
|
backing_preference=backing_preference,
|
|
backing_keep_enabled=backing_keep_enabled,
|
|
custom_instrumental=custom_instrumental,
|
|
)
|
|
keep_backing = decision["keep_backing"]
|
|
|
|
# Eligibility: confident lyrics AND a resolvable instrumental decision.
|
|
# With a user-supplied instrumental the backing decision is moot, so
|
|
# confident lyrics alone qualify (selection="custom").
|
|
lyrics_auto = verdict.lyrics.verdict == LyricsVerdict.AUTO
|
|
if custom_instrumental:
|
|
eligible = lyrics_auto
|
|
enforcement_basis = "lyrics_only_custom_instrumental"
|
|
else:
|
|
eligible = lyrics_auto and decision["ok"]
|
|
enforcement_basis = (
|
|
"overall_auto"
|
|
if backing_preference == "auto"
|
|
else f"lyrics_auto_backing_pref:{backing_preference}"
|
|
)
|
|
|
|
# Stem / audio blockers (the instrumental the selection needs must exist).
|
|
if not audio_complete:
|
|
blockers.append("audio_incomplete")
|
|
elif custom_instrumental:
|
|
# User-supplied instrumental -> "custom"; the video worker validates
|
|
# the custom source itself, no separated-stem requirements.
|
|
pass
|
|
elif keep_backing:
|
|
# Confident-keep (3-stem decider) selects the with-backing
|
|
# instrumental — gated shadow-first behind its own flag.
|
|
if not backing_keep_enabled:
|
|
blockers.append("backing_keep_disabled")
|
|
if not stems.get("instrumental_with_backing"):
|
|
blockers.append("no_with_backing_stem")
|
|
elif not stems.get("instrumental_clean"):
|
|
# Clean selection (auto-clean verdict OR backing_preference="clean").
|
|
blockers.append("no_clean_stem")
|
|
|
|
payload = verdict.to_dict()
|
|
payload.update({
|
|
"mode": "shadow", # flipped to "enforce" only when we actually enforce
|
|
"enforcement_eligible": eligible and not blockers,
|
|
"enforcement_basis": enforcement_basis,
|
|
"custom_instrumental": custom_instrumental,
|
|
"backing_preference": backing_preference,
|
|
"resolved_instrumental_selection": decision["selection"],
|
|
"trigger": trigger,
|
|
"enforcement_blockers": blockers,
|
|
"timing": timing_info,
|
|
"vocal_gaps": vocal_gaps_info,
|
|
"audio_complete_at_scoring": audio_complete,
|
|
"backing_analysis_available": backing_analysis is not None,
|
|
"ai_suggestions_available": ai_suggestions is not None,
|
|
"scored_at": datetime.now(timezone.utc).isoformat(),
|
|
})
|
|
|
|
summary = (
|
|
f"auto-approval [{trigger}]: eligible={eligible} ({enforcement_basis}) "
|
|
f"lyrics={verdict.lyrics.verdict.value}/{verdict.lyrics.tier} "
|
|
f"backing={verdict.backing.verdict.value} blockers={blockers or 'none'} "
|
|
f"(scorer v{verdict.scorer_version})"
|
|
)
|
|
logger.info(f"[job:{job_id}] {summary}")
|
|
|
|
if not eligible or blockers:
|
|
# Worth holding review for the stems: the text verdict is AUTO and
|
|
# the ONLY thing missing is separated audio (backing analysis,
|
|
# timing + vocal-gap checks). The re-score then decides for real.
|
|
hold_for_audio = (
|
|
lyrics_auto
|
|
and blockers == ["audio_incomplete"]
|
|
and (custom_instrumental or backing_preference != "review")
|
|
and getattr(settings, "auto_approval_audio_hold_enabled", True)
|
|
)
|
|
payload["outcome"] = "review"
|
|
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,
|
|
"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
|
|
# opened the editor in the meantime (AWAITING_REVIEW -> IN_REVIEW) is not
|
|
# overwritten — IN_REVIEW -> REVIEW_COMPLETE is a legal transition and
|
|
# would otherwise pass validation mid-edit.
|
|
fresh = job_manager.get_job(job_id)
|
|
if (
|
|
not fresh
|
|
or fresh.status not in expected_statuses
|
|
or (fresh.state_data or {}).get("instrumental_selection")
|
|
):
|
|
payload["outcome"] = "aborted"
|
|
payload["abort_reason"] = "state_changed_during_enforce"
|
|
job_manager.update_processing_metadata(job_id, "auto_approval", payload)
|
|
return {"outcome": "aborted", "reason": "state_changed_during_enforce"}
|
|
|
|
applied_info = _build_auto_corrections(
|
|
job_id, corrections, ai_suggestions or [], storage
|
|
)
|
|
if applied_info.get("aborted"):
|
|
payload["outcome"] = "aborted"
|
|
payload["abort_reason"] = applied_info["aborted"]
|
|
job_manager.update_processing_metadata(job_id, "auto_approval", payload)
|
|
logger.warning(
|
|
f"[job:{job_id}] auto-approval aborted -> human review "
|
|
f"({applied_info['aborted']})"
|
|
)
|
|
return {"outcome": "aborted", "reason": applied_info["aborted"]}
|
|
|
|
# Instrumental selection resolved by the shared decision: user-supplied
|
|
# instrumental -> "custom"; otherwise the non-subjective backing verdict
|
|
# (or the user's backing_preference) picks the separated stem.
|
|
selection = decision["selection"] or ("with_backing" if keep_backing else "clean")
|
|
job_manager.update_state_data(job_id, "instrumental_selection", selection)
|
|
|
|
# Clear worker progress keys so downstream workers run fresh (mirrors
|
|
# complete_review — idempotency keys would otherwise skip re-runs).
|
|
progress_keys = ["render_progress", "video_progress", "encoding_progress"]
|
|
job_manager.delete_state_data_keys(job_id, progress_keys)
|
|
|
|
job_manager.transition_to_state(
|
|
job_id=job_id,
|
|
new_status=JobStatus.REVIEW_COMPLETE,
|
|
progress=70,
|
|
message=(
|
|
"Auto-approved: lyrics verified against synced references and "
|
|
+ (
|
|
"using your provided instrumental"
|
|
if custom_instrumental
|
|
else (
|
|
"clear backing vocals retained"
|
|
if keep_backing
|
|
else "no audible backing vocals"
|
|
)
|
|
)
|
|
+ " — skipping review, rendering video"
|
|
),
|
|
)
|
|
|
|
# Record the verdict AFTER the transition so the metadata never claims
|
|
# auto-completion for a job that in fact fell back to human review.
|
|
payload["mode"] = "enforce"
|
|
payload["outcome"] = "auto_completed"
|
|
payload["applied_suggestions"] = len(applied_info.get("applied_ids") or [])
|
|
job_manager.update_processing_metadata(job_id, "auto_approval", payload)
|
|
|
|
from backend.services.worker_service import get_worker_service
|
|
await get_worker_service().trigger_render_video_worker(job_id)
|
|
|
|
logger.info(f"[job:{job_id}] auto-approval COMPLETED review "
|
|
f"(applied {payload['applied_suggestions']} AI suggestions)")
|
|
return {"outcome": "auto_completed"}
|
|
|
|
except Exception as e:
|
|
logger.warning(f"[job:{job_id}] auto-approval failed non-fatally: {e}", exc_info=True)
|
|
try:
|
|
from backend.services.job_manager import JobManager
|
|
JobManager().update_processing_metadata(job_id, "auto_approval", {
|
|
"outcome": "error",
|
|
"error": str(e),
|
|
"trigger": trigger,
|
|
"scored_at": datetime.now(timezone.utc).isoformat(),
|
|
})
|
|
except Exception:
|
|
pass
|
|
return {"outcome": "error", "error": str(e)}
|
|
|
|
|
|
def _published_segments(corrections, ai_suggestions):
|
|
"""The segments auto-ship would publish: raw + auto-applied AI suggestions."""
|
|
from backend.services.auto_approval.apply import build_applied_segments
|
|
|
|
segments = corrections.get("corrected_segments") or []
|
|
if ai_suggestions:
|
|
applied = build_applied_segments(corrections, ai_suggestions)
|
|
if not applied.get("aborted"):
|
|
segments = applied["segments"]
|
|
return segments
|
|
|
|
|
|
def analyze_vocal_gaps(job_id, corrections, segments, stems, storage):
|
|
"""Download the lead-vocal stem and find sung stretches with no transcribed words.
|
|
|
|
Returns ``None`` when no lead/vocals stem is registered, else a
|
|
``VocalGapsResult`` (``.error`` set on analysis failure). Never raises.
|
|
"""
|
|
import os
|
|
import tempfile
|
|
|
|
from backend.services.auto_approval.vocal_gaps import VocalGapsResult, compute_vocal_gaps
|
|
|
|
stem_path = stems.get("lead_vocals") or stems.get("vocals_clean")
|
|
if not stem_path:
|
|
return None
|
|
try:
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
local = os.path.join(temp_dir, "lead_vocals.flac")
|
|
storage.download_file(stem_path, local)
|
|
result = compute_vocal_gaps(segments, local, corrections)
|
|
logger.info(
|
|
f"[job:{job_id}] vocal gaps: {len(result.gaps)} gap(s), "
|
|
f"suspect={result.suspect_count} max_run={result.max_suspect_run_s}s error={result.error}"
|
|
)
|
|
return result
|
|
except Exception as e: # noqa: BLE001 — shadow analysis is best-effort
|
|
logger.warning(f"[job:{job_id}] vocal gap analysis failed non-fatally: {e}")
|
|
return VocalGapsResult(error=str(e))
|
|
|
|
|
|
def vocal_gaps_input_key(segments, stems) -> str:
|
|
"""Fingerprint of what a vocal-gap analysis looked at: word timings, stem, detector
|
|
version. A reset, re-transcription, audio edit or new AI suggestions changes it."""
|
|
from backend.services.auto_approval.vocal_gaps import VOCAL_GAPS_VERSION
|
|
|
|
words = [(w.get("id"), round(w.get("start_time") or 0, 2), round(w.get("end_time") or 0, 2))
|
|
for seg in segments for w in (seg.get("words") or [])]
|
|
stem = stems.get("lead_vocals") or stems.get("vocals_clean")
|
|
blob = json.dumps([VOCAL_GAPS_VERSION, stem, words], ensure_ascii=False)
|
|
return hashlib.sha1(blob.encode("utf-8")).hexdigest()[:16]
|
|
|
|
|
|
def _vocal_gaps_shadow(job_id, job, corrections, ai_suggestions, stems, audio_complete,
|
|
storage, job_manager) -> Tuple[Dict[str, Any], Optional[Dict[str, Any]]]:
|
|
"""Analyze when stems exist and store ``state_data.vocal_gaps``; reuse a stored
|
|
result only if it was computed from the same lyrics + stem (``input_key``).
|
|
|
|
Returns ``(summary for the payload, full vocal-gaps dict or None)``. Never raises.
|
|
Blocking (stem download + decode): call via ``asyncio.to_thread``.
|
|
"""
|
|
try:
|
|
if not audio_complete:
|
|
return {"status": "pending_audio"}, None
|
|
segments = _published_segments(corrections, ai_suggestions)
|
|
input_key = vocal_gaps_input_key(segments, stems)
|
|
existing = (job.state_data or {}).get("vocal_gaps")
|
|
if existing and existing.get("input_key") == input_key:
|
|
return {"status": "cached", "suspect_count": existing.get("suspect_count"),
|
|
"evidenced_count": existing.get("evidenced_count", 0),
|
|
"max_suspect_run_s": existing.get("max_suspect_run_s")}, existing
|
|
result = analyze_vocal_gaps(job_id, corrections, segments, stems, storage)
|
|
if result is None:
|
|
return {"status": "no_lead_stem"}, None
|
|
if result.error:
|
|
return {"status": "error", "error": result.error}, None
|
|
stored = {**result.to_dict(), "analyzed_at": datetime.now(timezone.utc).isoformat(),
|
|
"source": "auto_approval", "input_key": input_key}
|
|
job_manager.update_state_data(job_id, "vocal_gaps", stored)
|
|
return {"status": "checked", "suspect_count": result.suspect_count,
|
|
"evidenced_count": result.evidenced_count,
|
|
"max_suspect_run_s": result.max_suspect_run_s}, stored
|
|
except Exception as e: # noqa: BLE001
|
|
logger.warning(f"[job:{job_id}] vocal gap analysis failed non-fatally: {e}")
|
|
return {"status": "error", "error": str(e)}, None
|
|
|
|
|
|
def analyze_job_vocal_gaps(job_id: str, store: bool = True) -> Dict[str, Any]:
|
|
"""Backfill/inspect: vocal gaps for the lyrics a job actually has now.
|
|
|
|
Uses the reviewed ``corrections_updated.json`` segments when present (what was
|
|
rendered), with reference lyrics + anchors from ``corrections.json``. Runs
|
|
synchronously (stem download + numpy, a few seconds). Never raises.
|
|
"""
|
|
from backend.services.job_manager import JobManager
|
|
from backend.services.storage_service import StorageService
|
|
|
|
try:
|
|
job_manager, storage = JobManager(), StorageService()
|
|
job = job_manager.get_job(job_id)
|
|
if not job:
|
|
return {"status": "no_job"}
|
|
corrections_path = CORRECTIONS_PATH.format(job_id=job_id)
|
|
if not storage.file_exists(corrections_path):
|
|
return {"status": "no_corrections"}
|
|
corrections = storage.download_json(corrections_path)
|
|
segments = corrections.get("corrected_segments") or []
|
|
lyrics_source = "corrections"
|
|
updated_path = CORRECTIONS_UPDATED_PATH.format(job_id=job_id)
|
|
if storage.file_exists(updated_path):
|
|
updated = storage.download_json(updated_path)
|
|
if updated.get("corrected_segments"):
|
|
segments, lyrics_source = updated["corrected_segments"], "corrections_updated"
|
|
stems = (job.file_urls or {}).get("stems", {}) if job.file_urls else {}
|
|
result = analyze_vocal_gaps(job_id, corrections, segments, stems, storage)
|
|
if result is None:
|
|
return {"status": "no_lead_stem"}
|
|
if result.error:
|
|
return {"status": "error", "error": result.error}
|
|
payload = {**result.to_dict(), "analyzed_at": datetime.now(timezone.utc).isoformat(),
|
|
"source": "backfill", "lyrics_source": lyrics_source,
|
|
"input_key": vocal_gaps_input_key(segments, stems)}
|
|
if store:
|
|
job_manager.update_state_data(job_id, "vocal_gaps", payload)
|
|
return {"status": "checked", "stored": store, "vocal_gaps": payload}
|
|
except Exception as e: # noqa: BLE001
|
|
logger.warning(f"[job:{job_id}] vocal gap backfill failed: {e}")
|
|
return {"status": "error", "error": str(e)}
|
|
|
|
|
|
def _compute_timing_signals(job_id, corrections, ai_suggestions, stems, storage):
|
|
"""Download the lead-vocal stem and run the timing-plausibility analysis
|
|
on the POST-AI word stream (the state auto-ship would publish).
|
|
|
|
Returns ``None`` when no lead/vocals stem is registered, else a
|
|
``TimingSignals`` (whose ``.error`` is set on analysis failure — caller
|
|
decides fail-open). Never raises.
|
|
"""
|
|
import os
|
|
import tempfile
|
|
|
|
from backend.services.auto_approval.timing_check import (
|
|
TimingSignals,
|
|
compute_timing_signals,
|
|
)
|
|
|
|
stem_path = stems.get("lead_vocals") or stems.get("vocals_clean")
|
|
if not stem_path:
|
|
return None
|
|
try:
|
|
# The published state is raw + auto-applied suggestions; on an apply
|
|
# abort the executor falls back to human review anyway, so analyzing
|
|
# the raw segments there is only shadow data.
|
|
segments = _published_segments(corrections, ai_suggestions)
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
local = os.path.join(temp_dir, "lead_vocals.flac")
|
|
storage.download_file(stem_path, local)
|
|
signals = compute_timing_signals(segments, local)
|
|
logger.info(
|
|
f"[job:{job_id}] timing check: fired={signals.fired or 'no'} "
|
|
f"shadow={signals.shadow_fired or 'no'} "
|
|
f"(start_inactive={signals.pct_start_inactive}% "
|
|
f"suspect_bad={signals.n_suspect_bad} "
|
|
f"unclaimed_run={signals.max_unclaimed_run_s}s error={signals.error})"
|
|
)
|
|
return signals
|
|
except Exception as e: # noqa: BLE001 — gate is best-effort by design
|
|
logger.warning(f"[job:{job_id}] timing check failed non-fatally: {e}")
|
|
return TimingSignals(error=str(e))
|
|
|
|
|
|
def _build_auto_corrections(
|
|
job_id: str,
|
|
corrections: Dict[str, Any],
|
|
ai_suggestions: List[Dict[str, Any]],
|
|
storage,
|
|
) -> Dict[str, Any]:
|
|
"""Apply the AI suggestions and save corrections_updated.json.
|
|
|
|
Returns ``{"aborted": <reason>}`` on any anomaly (caller falls back to
|
|
human review), else ``{"applied_ids": [...]}``.
|
|
"""
|
|
from backend.services.auto_approval.apply import build_applied_segments
|
|
|
|
result = build_applied_segments(corrections, ai_suggestions)
|
|
if result.get("aborted"):
|
|
return {"aborted": result["aborted"]}
|
|
|
|
updated = dict(corrections)
|
|
updated["corrected_segments"] = result["segments"]
|
|
metadata = dict(updated.get("metadata") or {})
|
|
metadata["auto_approval"] = {
|
|
"auto_approved": True,
|
|
"applied_suggestion_ids": result["applied_ids"],
|
|
"rejected_suggestion_ids": result["rejected_ids"],
|
|
"applied_at": datetime.now(timezone.utc).isoformat(),
|
|
}
|
|
updated["metadata"] = metadata
|
|
|
|
updated_path = CORRECTIONS_UPDATED_PATH.format(job_id=job_id)
|
|
storage.upload_json(updated_path, updated)
|
|
|
|
from backend.services.job_manager import JobManager
|
|
JobManager().update_file_url(job_id, "lyrics", "corrections_updated", updated_path)
|
|
logger.info(
|
|
f"[job:{job_id}] auto-approval saved corrections_updated.json "
|
|
f"({len(result['applied_ids'])} suggestions applied, "
|
|
f"{len(result['rejected_ids'])} conflict-losers rejected)"
|
|
)
|
|
return {"applied_ids": result["applied_ids"]}
|