Compare commits

...
Author SHA1 Message Date
Andrew Beveridge ☄️andClaude Sonnet 4.5 67cd6494b4 Fix admin restart state transition for preserve_audio_stems mode (#377)
## Summary

Fixes a state machine validation error when using the admin "Full
Restart" button with preserve_audio_stems mode.

## Problem

When restarting job 1eab1172 after the Unicode fix (PR #375), the
restart failed with:
```
Invalid state transition for job 1eab1172: transcribing -> JobStatus.GENERATING_SCREENS. 
Valid transitions: ['correcting', 'failed']
```

## Root Cause

The restart endpoint was setting the job status to `transcribing`, then
triggering the screens worker. The screens worker immediately tries to
transition to `GENERATING_SCREENS`, but `transcribing ->
generating_screens` is not a valid transition according to the state
machine in `backend/models/job.py`.

## Fix

Changed the restart endpoint to set status to `downloading` instead of
`transcribing`. According to STATE_TRANSITIONS:
- ✅ `downloading` → `generating_screens` (valid)
- ✅ `audio_complete` → `generating_screens` (valid)
- ✅ `lyrics_complete` → `generating_screens` (valid)
- ❌ `transcribing` → `generating_screens` (invalid)

Using `downloading` is the safest choice as it's the most flexible entry
state.

## Changes

- `backend/api/routes/admin.py:2161` - Change status from "transcribing"
to "downloading"
- `docs/LESSONS-LEARNED.md` - Added lesson about validating state
transitions in admin operations
- `pyproject.toml` - Bump version to 0.115.3

## Testing

- Existing tests pass (8 pre-existing emulator test failures unrelated
to this change)
- Will verify in production by restarting job 1eab1172 after deployment

@coderabbitai ignore

---
🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-05 18:32:46 +00:00
3 changed files with 21 additions and 4 deletions

No files matched your search

+3 -3
View File
@@ -2158,18 +2158,18 @@ async def restart_job(
# Set coordination flags to trigger screens worker
update_payload["state_data.audio_complete"] = True
update_payload["state_data.lyrics_complete"] = True
update_payload["status"] = "transcribing" # Allows screens worker to run
update_payload["status"] = "downloading" # Allows transition to generating_screens
update_payload["message"] = "Restarting with preserved audio stems"
update_payload["error_message"] = DELETE_FIELD
update_payload["error_details"] = DELETE_FIELD
update_payload["timeline"] = ArrayUnion([{
"status": "transcribing",
"status": "downloading",
"timestamp": datetime.now(timezone.utc).isoformat(),
"message": f"Admin {admin_email} restarted job (preserving audio stems)"
}])
job_ref.update(update_payload)
new_status = "transcribing"
new_status = "downloading"
# Trigger screens worker
try:
+17
View File
@@ -154,6 +154,23 @@ Sanitize user input (artist/title) to ASCII for HTTP headers. Smart quotes, em d
**Test coverage**: Added 10 comprehensive tests covering Cyrillic, Chinese, Arabic, emoji, and accented characters to prevent regression.
### Admin Job Restart Must Use Valid State Transitions (Feb 2026)
**What happened**: After fixing the Unicode bug (v0.115.2), job `1eab1172` was restarted using the admin "Full Restart" button. The restart triggered successfully, but then immediately failed with: "Invalid state transition for job 1eab1172: transcribing -> JobStatus.GENERATING_SCREENS. Valid transitions: ['correcting', 'failed']"
**Root cause**: The admin restart endpoint (`backend/api/routes/admin.py:2161`) was setting the job status to `transcribing` when `preserve_audio_stems=True`, then triggering the screens worker. The screens worker immediately tries to transition to `GENERATING_SCREENS` state. However, the state machine (`backend/models/job.py:STATE_TRANSITIONS`) doesn't allow `transcribing -> generating_screens` - only `transcribing -> correcting` or `transcribing -> failed`.
**Why this happened**: The code was written intuitively ("we're preserving audio stems and want to restart from screens, so set to transcribing") without checking the state machine's allowed transitions. The comment even said "Allows screens worker to run" but that was incorrect.
**Fix** (v0.115.3): Change restart endpoint to set status to `downloading` instead of `transcribing`. Looking at STATE_TRANSITIONS:
- `downloading` → `generating_screens` ✓ (valid)
- `audio_complete` → `generating_screens` ✓ (valid)
- `lyrics_complete` → `generating_screens` ✓ (valid)
- `transcribing` → `generating_screens` ✗ (invalid)
Using `downloading` is the safest choice because it's the most flexible entry state that allows transitioning to screens generation.
**Pattern**: When writing admin operations or restart logic that sets job status and triggers workers, always check `backend/models/job.py:STATE_TRANSITIONS` to ensure the target state allows transitioning to whatever state the worker will try to enter. Don't make assumptions about valid transitions - verify against the state machine definition.
### Fonts in Docker
Base Docker images have no fonts. Install `fonts-noto-core`, `fonts-noto-cjk` for video rendering.
+1 -1
View File
@@ -1,6 +1,6 @@
[tool.poetry]
name = "karaoke-gen"
version = "0.115.2"
version = "0.115.3"
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 <andrew@beveridge.uk>"]
license = "MIT"