Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Keep prefix-cache shared prefix byte-identical when summary timestamps are on by mirroring the timestamped transcript in the title call (#316/#304)
Murtaza Nasir19ad9c3f9217
diff --git a/src/tasks/processing.py b/src/tasks/processing.pyindex e5456fb2..0d5e4ae6 100644--- a/src/tasks/processing.py+++ b/src/tasks/processing.py@@ -424,7 +424,21 @@ def _generate_ai_title(recording):# mid-sequence, which makes json.loads() fail and leaves the literal `\uXXXX`# escapes in the prompt (issue #260).transcript_limit = SystemSetting.get_setting('transcript_length_limit', 30000)- formatted_transcription = format_transcription_for_llm(recording.transcription)+ # The title normally uses the plain transcript (it has no use for timestamps).+ # BUT when prefix-cache prompts are on, the title and summary calls must share+ # a byte-identical transcript for the KV cache to be reusable — and the summary+ # may include timestamps (#304). So mirror the owner's summary-timestamp setting+ # here too; otherwise the title prefills a plain transcript and the summary's+ # timestamped transcript can't reuse it, defeating the optimization.+ _t_owner = recording.owner+ if PREFIX_CACHE_OPTIMIZED_PROMPTS and _t_owner and _t_owner.summary_include_timestamps:+ formatted_transcription = format_transcription_for_llm(+ recording.transcription,+ include_timestamps=True,+ template_format=_resolve_timestamp_template_format(_t_owner, _t_owner.summary_timestamp_template_id),+ )+ else:+ formatted_transcription = format_transcription_for_llm(recording.transcription)if transcript_limit == -1:transcript_text = formatted_transcriptionelse:diff --git a/tests/test_prefix_cache_prompts.py b/tests/test_prefix_cache_prompts.pyindex 08557e47..6407ce1c 100644--- a/tests/test_prefix_cache_prompts.py+++ b/tests/test_prefix_cache_prompts.py@@ -145,6 +145,42 @@ def test_flag_on_shares_system_and_transcript_prefix(app_with_recording):assert "Summarization Instructions:" not in summary_prefix+def test_flag_on_with_summary_timestamps_keeps_shared_prefix(app_with_recording):+ """Flag ON + summary timestamps ON (#304): the title must mirror the+ summary's TIMESTAMPED transcript, otherwise the shared prefix diverges and+ the KV cache can't be reused. The shared prefix must stay byte-identical and+ actually contain timestamps."""+ import json+ app, db, user, recording, created_user = app_with_recording+ # JSON-segment transcript so timestamps are actually applied.+ recording.transcription = json.dumps([+ {"speaker": "Alice", "sentence": "Let's lock the launch date.", "start_time": 0.0, "end_time": 3.0},+ {"speaker": "Bob", "sentence": "I propose the 14th, this is long enough.", "start_time": 65.0, "end_time": 68.0},+ ])+ user.summary_include_timestamps = True+ user.summary_timestamp_template_id = None+ db.session.commit()+ try:+ with patch.object(processing, "PREFIX_CACHE_OPTIMIZED_PROMPTS", True):+ title_msgs, summary_msgs = _run_both_calls(app, recording, user)++ title_prefix = _shared_region(title_msgs["user"])+ summary_prefix = _shared_region(summary_msgs["user"])+ assert title_prefix is not None and summary_prefix is not None+ assert title_prefix == summary_prefix, (+ "title/summary transcript prefix diverges when summary timestamps are on:\n"+ f"title: {title_prefix!r}\n"+ f"summary: {summary_prefix!r}"+ )+ # The shared transcript carries timestamps (proving the title mirrored+ # the summary's timestamped format, not the plain one).+ assert "[00:00:00]" in title_prefix+ assert "[00:01:05]" in title_prefix+ finally:+ user.summary_include_timestamps = False+ db.session.commit()++def test_flag_off_keeps_upstream_prompts(app_with_recording):"""With the flag OFF, the prompts are the original upstream prompts: distinctsystem messages and the original transcript wrappers."""