Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
One finishing step for every processing exit (#412): status, auto-shares, export and Inquire chunks; user-titled recordings without a summary are now indexed, short transcriptions are exported and shared, and a missing LLM client no longer leaves the recording PROCESSING
Murtaza Nasirfb3a17787145
diff --git a/src/tasks/processing.py b/src/tasks/processing.pyindex 940a6b38..2b40c590 100644--- a/src/tasks/processing.py+++ b/src/tasks/processing.py@@ -355,6 +355,37 @@ def _end_transaction_before_external_call(*instances):session.expire_on_commit = previous+def finish_processing(recording_id):+ """The last step of every processing path (#412).++ Marks the recording COMPLETED, then applies team-tag auto-shares, writes the+ auto-export and builds the Inquire chunks. Every exit of the pipeline calls+ this (title only, summary, empty or skipped summary, short transcription),+ so none of them can skip a step. The steps after COMPLETED are best-effort:+ a failure is logged and the recording stays COMPLETED.+ """+ recording = db.session.get(Recording, recording_id)+ if not recording:+ return+ recording.status = 'COMPLETED'+ recording.completed_at = datetime.utcnow()+ db.session.commit()+ try:+ apply_team_tag_auto_shares(recording_id)+ except Exception as e:+ current_app.logger.error(f"Auto-shares failed for recording {recording_id} (recording stays COMPLETED): {e}")+ if ENABLE_AUTO_EXPORT:+ try:+ export_recording(recording_id)+ except Exception as e:+ current_app.logger.error(f"Auto-export failed for recording {recording_id} (recording stays COMPLETED): {e}")+ if ENABLE_INQUIRE_MODE:+ try:+ process_recording_chunks(recording_id)+ except Exception as e:+ current_app.logger.error(f"Error processing chunks for completed recording {recording_id}: {e}")++def generate_title_task(app_context, recording_id, will_auto_summarize=False):"""Generates only a title for a recording based on transcription.@@ -376,10 +407,9 @@ def generate_title_task(app_context, recording_id, will_auto_summarize=False):from src.utils.titles import is_placeholder_titleif not is_placeholder_title(recording.title, recording.original_filename):current_app.logger.info(f"Recording {recording_id} has user-provided title '{recording.title}', skipping AI title generation")- if not will_auto_summarize:- recording.status = 'COMPLETED'- recording.completed_at = datetime.utcnow()db.session.commit()+ if not will_auto_summarize:+ finish_processing(recording_id)return# The title step (naming template, AI title, fallbacks) is shared with the@@ -393,23 +423,13 @@ def generate_title_task(app_context, recording_id, will_auto_summarize=False):else:current_app.logger.warning(f"Could not generate title for recording {recording_id}")- # Only set status to COMPLETED if auto-summarization won't happen next- # If auto-summarization is enabled, the summary task will set COMPLETED+ db.session.commit()+ # Without a summary to follow, the title is the last step: finish here.+ # Otherwise the summary task finishes the recording.if not will_auto_summarize:- recording.status = 'COMPLETED'- recording.completed_at = datetime.utcnow()- db.session.commit()- current_app.logger.info(f"Title generation complete, status set to COMPLETED for recording {recording_id}")-- # Process chunks for semantic search after completion (if inquire mode is enabled)- if ENABLE_INQUIRE_MODE:- try:- process_recording_chunks(recording_id)- except Exception as e:- current_app.logger.error(f"Error processing chunks for completed recording {recording_id}: {e}")+ finish_processing(recording_id)+ current_app.logger.info(f"Title generation complete, recording {recording_id} finished")else:- # Just commit the title without changing status- db.session.commit()current_app.logger.info(f"Title generation complete, leaving status unchanged (auto-summarization will follow) for recording {recording_id}")@@ -619,8 +639,8 @@ def generate_summary_only_task(app_context, recording_id, custom_prompt_overrideif not recording.transcription or len(recording.transcription.strip()) < 10:current_app.logger.warning(f"Transcription for recording {recording_id} is too short or empty. Skipping summarization.")recording.summary = "[Summary skipped due to short transcription]"- recording.status = 'COMPLETED'db.session.commit()+ finish_processing(recording_id)return# Get user preferences and tag custom prompts@@ -889,53 +909,23 @@ Summarization Instructions:if recording.owner and recording.owner.extract_events:extract_events_from_transcript(recording_id, formatted_transcription, summary)- # Mark as completed AFTER event extraction- recording.status = 'COMPLETED'- recording.completed_at = datetime.utcnow()- # Calculate and save summarization duration+ # Events are extracted before the recording is finished.summarization_end_time = time.time()recording.summarization_duration_seconds = int(summarization_end_time - summarization_start_time)db.session.commit()current_app.logger.info(f"Summarization completed for recording {recording_id} in {recording.summarization_duration_seconds}s.")-- # Best-effort post-completion side effects. The recording is- # already committed COMPLETED with a good summary; if either of- # these throws it must NOT bubble to the outer except, which- # would overwrite the summary with an error and flip the row to- # FAILED (a genuinely-complete recording corrupted by a share/- # export hiccup).- try:- apply_team_tag_auto_shares(recording_id)- if ENABLE_AUTO_EXPORT:- export_recording(recording_id)- except Exception as post_err:- current_app.logger.error(f"Post-completion step failed for recording {recording_id} (recording stays COMPLETED): {post_err}")else:current_app.logger.warning(f"Empty summary generated for recording {recording_id}")recording.summary = "[Summary not generated]"- recording.status = 'COMPLETED'- # Calculate and save summarization duration even for empty summarysummarization_end_time = time.time()recording.summarization_duration_seconds = int(summarization_end_time - summarization_start_time)db.session.commit()- # Best-effort post-completion side effects (see the non-empty- # branch above): must not corrupt the completed recording.- try:- apply_team_tag_auto_shares(recording_id)- if ENABLE_AUTO_EXPORT:- export_recording(recording_id)- except Exception as post_err:- current_app.logger.error(f"Post-completion step failed for recording {recording_id} (recording stays COMPLETED): {post_err}")-- # Process chunks for semantic search after completion (if inquire mode is enabled).- # Mirrors the non-summary path in generate_title_task; without this, Inquire- # embeddings are never generated when auto-summarization is enabled (issue #305).- if ENABLE_INQUIRE_MODE:- try:- process_recording_chunks(recording_id)- except Exception as chunk_err:- current_app.logger.error(f"Error processing chunks for completed recording {recording_id}: {chunk_err}")+ # Status, auto-shares, export and Inquire chunks (#305, #412). The+ # steps after COMPLETED are best-effort inside finish_processing, so+ # a share/export hiccup cannot reach the except below and overwrite+ # a good summary with an error.+ finish_processing(recording_id)except Exception as e:error_msg = format_api_error_message(str(e))@@ -2382,26 +2372,19 @@ def transcribe_with_connector(app_context, recording_id, filepath, original_fileadmin_disabled = admin_setting if isinstance(admin_setting, bool) else str(admin_setting).lower() == 'true'user = db.session.get(User, recording.user_id)user_disabled = user and user.auto_summarization is False- will_auto_summarize = not admin_disabled and not user_disabled+ # Without an LLM client there is no summary step: the title step+ # finishes the recording (it stayed PROCESSING before, #412).+ no_client = client is None+ will_auto_summarize = not admin_disabled and not user_disabled and not no_client# Generate title immediatelygenerate_title_task(app_context, recording_id, will_auto_summarize=will_auto_summarize)if not will_auto_summarize:- reason = "admin setting" if admin_disabled else "user preference"+ # generate_title_task has already finished the recording+ # (finish_processing: status, auto-shares, export, Inquire chunks).+ reason = "admin setting" if admin_disabled else ("user preference" if user_disabled else "no LLM client")current_app.logger.info(f"Auto-summarization disabled ({reason}), skipping summary for recording {recording_id}")- recording = db.session.get(Recording, recording_id)- if recording:- recording.status = 'COMPLETED'- recording.completed_at = datetime.utcnow()- db.session.commit()-- # Apply auto-shares for group tags after processing completes- apply_team_tag_auto_shares(recording_id)-- # Export transcription-only if auto-export is enabled- if ENABLE_AUTO_EXPORT:- export_recording(recording_id)else:# Auto-generate summary for all recordingscurrent_app.logger.info(f"Auto-generating summary for recording {recording_id}")diff --git a/tests/test_finish_processing.py b/tests/test_finish_processing.pynew file mode 100644index 00000000..7f4fe7aa--- /dev/null+++ b/tests/test_finish_processing.py@@ -0,0 +1,177 @@+"""Every exit of the processing pipeline runs the same finishing step (#412).++finish_processing marks the recording COMPLETED, then applies team-tag+auto-shares, writes the auto-export and builds the Inquire chunks. Before+#412 three exits skipped part of it:+- a user-titled recording with auto-summarization off was never indexed for+ Inquire;+- with auto-summarization on and no LLM client, the recording stayed PROCESSING;+- a too-short transcription was marked COMPLETED without shares, export or chunks.++SHARED-DB: the users and recordings created here are removed afterwards.+"""++import os+import sys+import time+import uuid+from unittest.mock import MagicMock, patch++import pytest++sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))++from src.app import app, db+from src.models import Recording, User+import src.tasks.processing as proc+++def made():+ created = []+ yield created+ with app.app_context():+ db.session.rollback()+ for model, oid in reversed(created):+ obj = db.session.get(model, oid)+ if obj is not None:+ db.session.delete(obj)+ db.session.commit()+++def _user(made, **kwargs):+ s = uuid.uuid4().hex[:8]+ u = User(username=f"finish_{s}", email=f"finish_{s}@local.test", password="x", **kwargs)+ db.session.add(u)+ db.session.commit()+ made.append((User, u.id))+ return u+++def _rec(made, user_id, **kwargs):+ kwargs.setdefault("title", "Recording - fin.mp3")+ kwargs.setdefault("transcription", "word " * 50)+ kwargs.setdefault("status", "PROCESSING")+ r = Recording(user_id=user_id, original_filename="fin.mp3", audio_path="local://fin.mp3", **kwargs)+ db.session.add(r)+ db.session.commit()+ made.append((Recording, r.id))+ return r+++def _side_effects():+ return (patch.object(proc, "apply_team_tag_auto_shares"),+ patch.object(proc, "export_recording"),+ patch.object(proc, "process_recording_chunks"),+ patch.object(proc, "ENABLE_AUTO_EXPORT", True),+ patch.object(proc, "ENABLE_INQUIRE_MODE", True))+++def _completion(text):+ msg = MagicMock(content=text, reasoning=None)+ return MagicMock(choices=[MagicMock(message=msg)])+++def test_user_title_without_summary_is_indexed_for_inquire(made):+ with app.app_context():+ u = _user(made)+ r = _rec(made, u.id, title="Board meeting")+ shares, export, chunks, *flags = _side_effects()+ with shares as m_shares, export as m_export, chunks as m_chunks, flags[0], flags[1]:+ proc.generate_title_task(app.app_context(), r.id, will_auto_summarize=False)+ db.session.expire_all()+ assert db.session.get(Recording, r.id).status == "COMPLETED"+ m_chunks.assert_called_once_with(r.id)+ m_shares.assert_called_once_with(r.id)+ m_export.assert_called_once_with(r.id)+++def test_short_transcription_still_runs_the_finishing_steps(made):+ with app.app_context():+ u = _user(made)+ r = _rec(made, u.id, transcription="hi")+ shares, export, chunks, *flags = _side_effects()+ with shares as m_shares, export as m_export, chunks as m_chunks, flags[0], flags[1], \+ patch.object(proc, "client", MagicMock()):+ proc.generate_summary_only_task(app.app_context(), r.id)+ db.session.expire_all()+ out = db.session.get(Recording, r.id)+ assert out.status == "COMPLETED"+ assert out.summary == "[Summary skipped due to short transcription]"+ m_shares.assert_called_once_with(r.id)+ m_export.assert_called_once_with(r.id)+ m_chunks.assert_called_once_with(r.id)+++def test_normal_summary_runs_each_finishing_step_once(made):+ with app.app_context():+ u = _user(made)+ r = _rec(made, u.id)+ shares, export, chunks, *flags = _side_effects()+ with shares as m_shares, export as m_export, chunks as m_chunks, flags[0], flags[1], \+ patch.object(proc, "client", MagicMock()), \+ patch.object(proc, "call_llm_completion", return_value=_completion("## Summary\nDone.")):+ proc.generate_summary_only_task(app.app_context(), r.id)+ db.session.expire_all()+ out = db.session.get(Recording, r.id)+ assert out.status == "COMPLETED" and out.summary.startswith("## Summary")+ assert out.summarization_duration_seconds is not None+ m_shares.assert_called_once_with(r.id)+ m_export.assert_called_once_with(r.id)+ m_chunks.assert_called_once_with(r.id)+++def test_a_failing_export_leaves_the_recording_completed(made):+ with app.app_context():+ u = _user(made)+ r = _rec(made, u.id)+ shares, export, chunks, *flags = _side_effects()+ with shares, export as m_export, chunks as m_chunks, flags[0], flags[1]:+ m_export.side_effect = RuntimeError("disk full")+ proc.finish_processing(r.id)+ db.session.expire_all()+ assert db.session.get(Recording, r.id).status == "COMPLETED"+ m_chunks.assert_called_once_with(r.id)+++def _make_connector(text):+ connector = MagicMock()+ connector.PROVIDER_NAME = "test-connector"+ connector.supports_diarization = False+ connector.default_diarize = False+ specs = MagicMock(max_duration_seconds=1800, handles_chunking_internally=False,+ recommended_chunk_seconds=600, unsupported_codecs=[])+ connector.specifications = specs+ response = MagicMock(text=text, segments=None, speakers=None, speaker_embeddings=None)+ response.has_diarization.return_value = False+ connector.transcribe.return_value = response+ return connector+++def test_without_an_llm_client_the_recording_is_completed_not_stuck(made):+ with app.app_context():+ u = _user(made)+ r = _rec(made, u.id, transcription=None, status="PROCESSING")+ path = os.path.join(app.config["UPLOAD_FOLDER"], f"finish_{r.id}.mp3")+ with open(path, "wb") as f:+ f.write(b"\x00" * 1024)+ conv = MagicMock(was_converted=False)+ shares, export, chunks, *flags = _side_effects()+ try:+ with patch("src.services.transcription.get_connector", return_value=_make_connector("A full transcript of the meeting.")), \+ patch.object(proc, "is_video_file", return_value=False), \+ patch.object(proc, "convert_if_needed", return_value=conv), \+ patch.object(proc, "client", None), \+ patch.object(proc, "chunking_service") as svc, \+ shares, export, chunks, flags[0], flags[1]:+ svc.needs_chunking.return_value = False+ svc.get_audio_duration.return_value = 60.0+ proc.transcribe_with_connector(app.app_context(), r.id, path, "fin.mp3", time.time(),+ mime_type="audio/mpeg")+ finally:+ if os.path.exists(path):+ os.remove(path)+ db.session.expire_all()+ out = db.session.get(Recording, r.id)+ assert out.transcription == "A full transcript of the meeting."+ assert out.status == "COMPLETED"