diff --git a/PROJECT_KNOWLEDGE.md b/PROJECT_KNOWLEDGE.md index 4921fa6..203ee20 100644 --- a/PROJECT_KNOWLEDGE.md +++ b/PROJECT_KNOWLEDGE.md @@ -39,6 +39,14 @@ Implemented: configurable safe input budget, falls back to complete plain transcript text when necessary, and fails before any Ollama request if even that input is too large. Silent head/tail truncation is prohibited. +- The `qwen3.8:27b` direct-protocol stage explicitly requests `num_ctx=32768` + and `think=false`; the practical prompt target is approximately 29,000 tokens. + A 31,038-token synthetic prompt passed, but larger prompts are not assumed safe + from the model's advertised 262,144-token native context alone. +- `regenerate_mvp_protocol` updates the run's validated Meeting Context and + regenerates protocol artifacts from the existing diarized transcript when + available. It never reruns audio preparation, Whisper or Pyannote, and it + preserves anonymous speaker labels in the source transcript. - Non-LLM unit tests for chunking, extraction helpers, protocol rendering and gold-test runner validation. - Meeting Context V1 scaffold and documentation for manually maintained diff --git a/src/meeting_lab/orchestration/mvp.py b/src/meeting_lab/orchestration/mvp.py index 7b8c768..3356fee 100644 --- a/src/meeting_lab/orchestration/mvp.py +++ b/src/meeting_lab/orchestration/mvp.py @@ -71,6 +71,64 @@ class MvpRunResult: protocol_path: Path | None +def regenerate_mvp_protocol( + run_dir: Path, + *, + meeting_context: ContextInput, + model: str = DEFAULT_MODEL, + ollama_endpoint: str = DEFAULT_ENDPOINT, + protocol_num_ctx: int = DEFAULT_NUM_CTX, + protocol_safe_input_token_budget: int = DEFAULT_SAFE_INPUT_TOKEN_BUDGET, + progress_sink: ProgressSink | None = None, +) -> MvpRunResult: + """Regenerate only protocol artifacts from an existing completed run.""" + started = time.perf_counter() + run_dir = Path(run_dir) + context = _effective_context(meeting_context) + if context is None: + raise ValueError("Meeting Context is required for protocol regeneration.") + if protocol_num_ctx <= 0: + raise ValueError("Protocol Ollama context size must be positive.") + if protocol_safe_input_token_budget <= 0: + raise ValueError("Protocol safe input token budget must be positive.") + + diarized_transcript = run_dir / "diarization" / "transcript_diarized.json" + plain_transcript = run_dir / "transcript" / "transcript.json" + transcript_path = ( + diarized_transcript if diarized_transcript.is_file() else plain_transcript + ) + if not transcript_path.is_file(): + raise FileNotFoundError( + f"Existing run has no protocol transcript artifact: {run_dir}" + ) + + context_path = run_dir / "context" / "meeting_context.yaml" + write_meeting_context(context, context_path) + _emit(progress_sink, "protocol_generation", "started", started) + try: + result = generate_direct_protocol( + transcript_path, + context_path, + model=model, + endpoint=ollama_endpoint, + num_ctx=protocol_num_ctx, + safe_input_token_budget=protocol_safe_input_token_budget, + ) + protocol_path = _persist_protocol(run_dir, result) + except Exception as exc: + _emit( + progress_sink, + "failed", + "failed", + started, + message=f"protocol_generation: {type(exc).__name__}: {exc}", + ) + raise + _emit(progress_sink, "protocol_generation", "completed", started) + _emit(progress_sink, "completed", "completed", started) + return MvpRunResult(0, run_dir, protocol_path) + + def create_unique_run_dir( output_root: Path, meeting_name: str, diff --git a/src/meeting_lab/protocol/direct_protocol_prompt.py b/src/meeting_lab/protocol/direct_protocol_prompt.py index 429df4a..b3061dc 100644 --- a/src/meeting_lab/protocol/direct_protocol_prompt.py +++ b/src/meeting_lab/protocol/direct_protocol_prompt.py @@ -15,6 +15,8 @@ Beginne mit # Meeting Protocol. Gliedere themenorientiert mit ## und syn Entferne nur Wiederholungen, Füllwörter und Gesprächsrauschen. Erfinde keine Fakten oder Identitäten. Gib kein JSON, keine Sprecherlabels und kein Denkprotokoll aus. Eine belegte themenübergreifende Maßnahmenliste am Ende ist optional.""" +MAPPED_SPEAKER_ATTRIBUTION_INSTRUCTION = """Nutze die autoritativen SPEAKER_XX-zu-Teilnehmer-Zuordnungen im Meeting-Kontext, um ausdrücklich belegte Aussagen, Positionen, Entscheidungen, Zuweisungen und angenommene persönliche Verpflichtungen namentlich zuzuordnen. Eine ausdrückliche Ich-Zusage eines zugeordneten Sprechers belegt persönliche Verantwortung. Unterscheide stets den Sprecher einer Aussage von darin nur erwähnten Personen. Leite für nicht zugeordnete Sprecher keine Identität ab und erfinde keine persönliche Verantwortung. Gib die technischen SPEAKER_XX-Bezeichnungen nicht im nutzerseitigen Protokoll aus.""" + def build_direct_protocol_prompt( transcript: str, diff --git a/src/meeting_lab/protocol/generate_direct_protocol.py b/src/meeting_lab/protocol/generate_direct_protocol.py index 1566ccd..63e96c6 100644 --- a/src/meeting_lab/protocol/generate_direct_protocol.py +++ b/src/meeting_lab/protocol/generate_direct_protocol.py @@ -20,6 +20,7 @@ from src.meeting_lab.models.meeting_context import ( ) from src.meeting_lab.protocol.direct_protocol_prompt import ( COMPACT_DIARIZED_PROTOCOL_INSTRUCTION, + MAPPED_SPEAKER_ATTRIBUTION_INSTRUCTION, build_direct_protocol_prompt, ) from src.meeting_lab.protocol.transcript_input import ( @@ -106,10 +107,16 @@ def select_transcript_input( plain_text = plain_segment_transcript(transcript.get("segments")) except TranscriptInputError as exc: raise DirectProtocolError(str(exc)) from exc + instruction = COMPACT_DIARIZED_PROTOCOL_INSTRUCTION + if ( + rendered_context + and "Confirmed diarization speaker mappings" in rendered_context + ): + instruction = f"{instruction}\n\n{MAPPED_SPEAKER_ATTRIBUTION_INSTRUCTION}" compact_prompt = build_direct_protocol_prompt( compact.text, rendered_context, - instruction=COMPACT_DIARIZED_PROTOCOL_INSTRUCTION, + instruction=instruction, ) compact_estimate = estimate_input_tokens(compact_prompt) if compact_estimate <= safe_input_token_budget: @@ -205,6 +212,19 @@ def generate_direct_protocol( "input_token_estimation_method": "utf8_bytes_divided_by_4.4", "fallback_used": selected.fallback_used, "diarization_enabled": selected.diarization_enabled, + "speaker_attribution_available": ( + True + if selected.representation == "diarized_compact" + else False + if selected.representation == "plain_transcript_fallback" + else None + ), + "speaker_attribution_loss_reason": ( + "plain_transcript_fallback" + if selected.representation == "plain_transcript_fallback" + else None + ), + "speaker_mapping_count": len(context.speaker_mappings) if context else 0, } return DirectProtocolResult( protocol_text=generation.text, diff --git a/tests/test_mvp_api.py b/tests/test_mvp_api.py index f7c5858..623821c 100644 --- a/tests/test_mvp_api.py +++ b/tests/test_mvp_api.py @@ -142,6 +142,65 @@ class MvpApiTests(unittest.TestCase): ], ) self.assertTrue(all(event.progress is None for event in events)) + self.assertEqual(protocol_generator.call_args.kwargs["num_ctx"], 32_768) + self.assertEqual( + protocol_generator.call_args.kwargs["safe_input_token_budget"], + 29_000, + ) + + def test_protocol_only_regeneration_reuses_diarized_artifacts(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + run_dir = root / "existing-run" + diarization_dir = run_dir / "diarization" + diarization_dir.mkdir(parents=True) + source = diarization_dir / "transcript_diarized.json" + source.write_text( + json.dumps( + { + "text": "SPEAKER_00: Existing statement.\n", + "segments": [ + { + "start": 0.0, + "end": 1.0, + "speaker_id": "SPEAKER_00", + "text": "Existing statement.", + } + ], + "speaker_labels_anonymous": True, + } + ), + encoding="utf-8", + ) + source_before = source.read_bytes() + mapped_context = context_data() + mapped_context["speaker_mappings"] = {"SPEAKER_00": "person-1"} + with ( + patch.object(mvp_api, "prepare_audio") as preparation, + patch.object(mvp_api, "transcribe_audio") as transcription, + patch.object(mvp_api, "diarize_audio") as diarization, + patch.object( + mvp_api, "generate_direct_protocol", side_effect=fake_protocol + ) as protocol, + ): + result = mvp_api.regenerate_mvp_protocol( + run_dir, + meeting_context=mapped_context, + model="qwen3.8:27b", + protocol_num_ctx=32_768, + protocol_safe_input_token_budget=29_000, + ) + + self.assertEqual(result.exit_code, 0) + self.assertEqual(result.protocol_path, run_dir / "protocol.md") + preparation.assert_not_called() + transcription.assert_not_called() + diarization.assert_not_called() + self.assertEqual(protocol.call_args.args[0], source) + self.assertEqual(protocol.call_args.kwargs["num_ctx"], 32_768) + self.assertEqual(source.read_bytes(), source_before) + persisted = load_meeting_context(run_dir / "context/meeting_context.yaml") + self.assertEqual(persisted.speaker_mappings, {"SPEAKER_00": "person-1"}) def test_failure_emits_terminal_failure_event(self): with tempfile.TemporaryDirectory() as directory: diff --git a/tests/test_protocol_transcript_input.py b/tests/test_protocol_transcript_input.py index 3fd2921..b4985ae 100644 --- a/tests/test_protocol_transcript_input.py +++ b/tests/test_protocol_transcript_input.py @@ -2,9 +2,10 @@ import json import tempfile import unittest from pathlib import Path -from unittest.mock import Mock +from unittest.mock import Mock, patch from src.meeting_lab.diarization.alignment import diarized_transcript_text +from src.meeting_lab.llm import ollama from src.meeting_lab.llm.ollama import OllamaGeneration from src.meeting_lab.protocol.direct_protocol_prompt import ( COMPACT_DIARIZED_PROTOCOL_INSTRUCTION, @@ -136,6 +137,81 @@ class ProtocolInputBudgetTests(unittest.TestCase): self.assertEqual(result.transcript_input, compact) self.assertEqual(call.call_count, 1) + def test_mapped_speakers_and_statements_reach_final_ollama_payload(self) -> None: + diarized = { + "text": "", + "segments": [ + { + "start": 0.0, + "end": 1.0, + "speaker_id": "SPEAKER_00", + "text": "We will run the trial on Wednesday.", + }, + { + "start": 1.0, + "end": 2.0, + "speaker_id": "SPEAKER_01", + "text": "I will prepare the raw materials before then.", + }, + { + "start": 2.0, + "end": 3.0, + "speaker_id": "SPEAKER_00", + "text": "Good. Anna owns the material preparation.", + }, + ], + "speaker_labels_anonymous": True, + "alignment_source": "exclusive_diarization", + } + context = { + "schema_version": "1", + "meeting": { + "meeting_id": "speaker-test", + "title": "Speaker test", + "language": "en", + }, + "participants": [ + {"participant_id": "martin", "display_name": "Martin"}, + {"participant_id": "anna", "display_name": "Anna"}, + ], + "speaker_mappings": {"SPEAKER_00": "martin", "SPEAKER_01": "anna"}, + "mentioned_people": [], + "organization": {"departments": []}, + "known_entities": {}, + } + response = Mock() + response.raise_for_status.return_value = None + response.json.return_value = {"response": "# Meeting Protocol\n", "done": True} + + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + transcript = self._write(root, diarized) + context_path = root / "context.yaml" + context_path.write_text(json.dumps(context), encoding="utf-8") + with patch.object(ollama.requests, "post", return_value=response) as post: + result = generate_direct_protocol( + transcript, + context_path, + model="qwen3.8:27b", + model_check=Mock(return_value={}), + ) + + prompt = post.call_args.kwargs["json"]["prompt"] + self.assertEqual(result.exact_prompt, prompt) + self.assertIn("- SPEAKER_00: Martin (participant_id: martin)", prompt) + self.assertIn("- SPEAKER_01: Anna (participant_id: anna)", prompt) + self.assertIn("SPEAKER_00: We will run the trial on Wednesday.", prompt) + self.assertIn( + "SPEAKER_01: I will prepare the raw materials before then.", prompt + ) + self.assertIn("SPEAKER_00: Good. Anna owns the material preparation.", prompt) + self.assertNotIn("Martin: We will run the trial on Wednesday.", prompt) + self.assertNotIn("Anna: I will prepare the raw materials before then.", prompt) + self.assertIn("autoritativen SPEAKER_XX-zu-Teilnehmer-Zuordnungen", prompt) + self.assertIn("Ich-Zusage", prompt) + self.assertIn("nur erwähnten Personen", prompt) + self.assertIn("keine persönliche Verantwortung", prompt) + def test_plain_fallback_selected_when_diarized_compact_exceeds_budget(self) -> None: with tempfile.TemporaryDirectory() as directory: root = Path(directory) @@ -180,6 +256,13 @@ class ProtocolInputBudgetTests(unittest.TestCase): ) self.assertTrue(result.runtime_metadata["fallback_used"]) self.assertEqual(result.transcript_input, plain) + self.assertNotIn("SPEAKER_00", result.exact_prompt) + self.assertNotIn("SPEAKER_01", result.exact_prompt) + self.assertFalse(result.runtime_metadata["speaker_attribution_available"]) + self.assertEqual( + result.runtime_metadata["speaker_attribution_loss_reason"], + "plain_transcript_fallback", + ) def test_oversized_plain_transcript_fails_before_any_network_call(self) -> None: with tempfile.TemporaryDirectory() as directory: @@ -225,6 +308,7 @@ class ProtocolInputBudgetTests(unittest.TestCase): ) self.assertFalse(result.runtime_metadata["fallback_used"]) self.assertFalse(result.runtime_metadata["diarization_enabled"]) + self.assertIsNone(result.runtime_metadata["speaker_attribution_available"]) if __name__ == "__main__":