From 06984906e2bdf4833e1679c9f75671fcabb38394 Mon Sep 17 00:00:00 2001 From: "juancarlos.cavero" Date: Mon, 24 Aug 2026 00:26:23 +0200 Subject: [PATCH] fix(dashboard): show each clip with its hook and captions as soon as it is done A job in flight published every clip as the clean reframe name, guessed by the status poller. The hook and the captions live in derived files (hooked__, subtitled__hooked__), and only when the WHOLE job finished did the result get rebuilt through _canonical_clip_file and resolve to them. So a six-clip job showed clip 1 hook-less for the five clips after it, which reads as the hook feature being broken. main.py now prints CLIP_READY at the end of each clip's chain, after the caption step and carrying its result, so the name is the file that will actually be delivered. app.py consumes the marker the way it already consumes PROXY_BYTES: intercepted in enqueue_output, never shown as a log line. The poller now serves only announced clips. That also closes the older hazard in the code it replaces, whose own comment admitted it was guessing: presence on disk was treated as done, so a clip could be served while ffmpeg was still writing it. A clip whose marker never arrives (it failed) stays hidden until the job ends and the result is rebuilt from disk, as before. --- app.py | 24 ++++++++-- main.py | 12 ++++- tests/test_clip_ready_marker.py | 77 +++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 tests/test_clip_ready_marker.py diff --git a/app.py b/app.py index 65f6f16..e003d18 100644 --- a/app.py +++ b/app.py @@ -1337,6 +1337,17 @@ def enqueue_output(out, job_id): decoded_line = _scrub_secrets(line.decode('utf-8').strip()) if decoded_line: # Internal marker from main.py's downloader, not a log line. + # Internal marker: a clip finished its whole chain and this is + # the file to serve for it. Consumed here like PROXY_BYTES so it + # never reaches the user's log. + if decoded_line.startswith("CLIP_READY "): + try: + _, index, filename = decoded_line.split(" ", 2) + if job_id in jobs: + jobs[job_id].setdefault('ready_files', {})[int(index)] = filename + except ValueError: + pass + continue if decoded_line.startswith("PROXY_BYTES="): try: if job_id in jobs: @@ -1400,13 +1411,20 @@ async def run_job(job_id, job_data): cost_analysis = data.get('cost_analysis') # Check which clips actually exist on disk + # Only clips main.py has announced as finished. It names + # the file itself, so a clip shows up WITH its hook and + # captions instead of as the bare reframe, and it is never + # served while ffmpeg is still writing it. A clip whose + # marker never arrives (it failed) simply stays hidden + # until the job ends and the result is rebuilt from disk. + ready_files = (jobs.get(job_id) or {}).get('ready_files') or {} ready_clips = [] for i, clip in enumerate(clips): - clip_filename = f"{base_name}_clip_{i+1}.mp4" + clip_filename = ready_files.get(i) + if not clip_filename: + continue clip_path = os.path.join(output_dir, clip_filename) if os.path.exists(clip_path) and os.path.getsize(clip_path) > 0: - # Checking if file is growing? For now assume if it exists and main.py moves it there, it's done. - # main.py writes to temp_... then moves to final name. So presence means ready! clip['video_url'] = f"/videos/{job_id}/{clip_filename}" ready_clips.append(clip) diff --git a/main.py b/main.py index 6e5db1e..6916dfa 100644 --- a/main.py +++ b/main.py @@ -1703,8 +1703,18 @@ if __name__ == '__main__': if hooked: deliver_path, clip['auto_hook'] = hooked if success: - auto_caption_clip(deliver_path, transcript, start, end) + captioned = auto_caption_clip(deliver_path, transcript, start, end) print(f" ✅ Clip {i+1} ready: {clip_final_path}") + # Hand the API the file to actually serve for this clip. + # Without it the status poller guesses the clean reframe + # name, so a job in flight showed every clip stripped of + # its hook and captions until the WHOLE job finished and + # the result got rebuilt through _canonical_clip_file. + # Printed only after the full chain (reframe, watermark, + # hook, captions) so the file is complete when it is + # announced, never one that ffmpeg is still writing. + print(f"CLIP_READY {i} " + f"{os.path.basename(captioned or deliver_path)}") return success finally: if os.path.exists(clip_temp_path): diff --git a/tests/test_clip_ready_marker.py b/tests/test_clip_ready_marker.py new file mode 100644 index 0000000..f66d2fb --- /dev/null +++ b/tests/test_clip_ready_marker.py @@ -0,0 +1,77 @@ +"""The clip a job in flight shows must be the FINISHED clip. + +Before this, the status poller guessed each clip's file as the clean reframe +name, so every clip appeared stripped of its hook and its captions until the +whole job ended and the result was rebuilt through _canonical_clip_file. A +six-clip job meant clip 1 sat there hook-less for the five clips after it, and +it read as "the hooks are broken" (reported 24-ago-2026). + +main.py now announces each clip once its whole chain is done and names the file +to serve; app.py consumes the marker the way it already consumes PROXY_BYTES. +""" +import io + +import pytest + +app = pytest.importorskip("app") + + +def _feed(job_id, *lines): + """Run enqueue_output over these stdout lines, as the log thread would.""" + stream = io.BytesIO(b"".join(line.encode("utf-8") + b"\n" for line in lines)) + app.enqueue_output(stream, job_id) + + +class TestClipReadyMarker: + def setup_method(self): + self.job_id = "test-clip-ready" + app.jobs[self.job_id] = {"logs": []} + + def teardown_method(self): + app.jobs.pop(self.job_id, None) + + def test_records_the_file_to_serve_per_clip(self): + _feed(self.job_id, + "CLIP_READY 0 subtitled_1_hooked_2_My_Video_clip_1.mp4", + "CLIP_READY 2 hooked_3_My_Video_clip_3.mp4") + assert app.jobs[self.job_id]["ready_files"] == { + 0: "subtitled_1_hooked_2_My_Video_clip_1.mp4", + 2: "hooked_3_My_Video_clip_3.mp4", + } + + def test_the_marker_never_reaches_the_user_log(self): + _feed(self.job_id, + "🎬 Processing Clip 1", + "CLIP_READY 0 subtitled_1_My_Video_clip_1.mp4") + assert app.jobs[self.job_id]["logs"] == ["🎬 Processing Clip 1"] + + def test_clips_arriving_out_of_order_keep_their_own_index(self): + # CLIP_WORKERS renders three clips at once, so clip 3 can finish first. + _feed(self.job_id, + "CLIP_READY 2 subtitled_9_My_Video_clip_3.mp4", + "CLIP_READY 0 subtitled_7_My_Video_clip_1.mp4") + ready = app.jobs[self.job_id]["ready_files"] + assert ready[2].endswith("_clip_3.mp4") + assert ready[0].endswith("_clip_1.mp4") + + def test_a_malformed_marker_is_ignored_not_fatal(self): + _feed(self.job_id, "CLIP_READY notanumber file.mp4", "CLIP_READY 1 ok.mp4") + assert app.jobs[self.job_id]["ready_files"] == {1: "ok.mp4"} + + def test_an_unknown_job_does_not_raise(self): + _feed("no-such-job", "CLIP_READY 0 whatever.mp4") + + +class TestMainAnnouncesTheDeliveredFile: + def test_main_prints_the_marker_after_the_caption_step(self): + """The marker must sit AFTER auto_caption_clip and carry its result. + + Announcing the pre-caption path would put the poller back on a file the + pipeline is about to supersede, which is the bug this fixes. + """ + main_src = open("main.py", encoding="utf-8").read() + caption_at = main_src.index("captioned = auto_caption_clip(") + marker_at = main_src.index('print(f"CLIP_READY {i} "') + assert caption_at < marker_at + marker_line = main_src[marker_at:marker_at + 200] + assert "captioned or deliver_path" in marker_line