kcode-speaker-v3-llm-protocol 0.1.0

Deterministic Speaker V3 LLM protocol construction and decoding
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
use serde::Serialize;
use serde_json::{Map, Value, json};
use std::{collections::BTreeSet, error::Error, fmt};

pub use kcode_speaker_v3_schema::{
    FEATURE_NAMES, FeatureVector24, LocalSpeakerLabel, StructuredAnalysis, StructuredSpeaker,
    ValidationError as AnalysisValidationError, VocalGenderPresentation,
};

pub const GEMINI_TRANSCRIPT_PROMPT_REVISION: &str = "speaker-v3-gemini-transcript-r1";
pub const GEMINI_FEATURE_PROMPT_ONE_REVISION: &str = "speaker-v3-gemini-feature-1-r2";
pub const GEMINI_FEATURE_PROMPT_TWO_REVISION: &str = "speaker-v3-gemini-feature-2-r2";
pub const GEMINI_FEATURE_PROMPT_THREE_REVISION: &str = "speaker-v3-gemini-feature-3-r2";
pub const GPT_STRUCTURING_PROMPT_REVISION: &str = "speaker-v3-gpt-structure-r2";
pub const TERRA_SPEAKER_LABELS_PROMPT_REVISION: &str = "speaker-v3-terra-labels-r1";

pub const GEMINI_FEATURE_PROMPT_REVISIONS: [&str; 3] = [
    GEMINI_FEATURE_PROMPT_ONE_REVISION,
    GEMINI_FEATURE_PROMPT_TWO_REVISION,
    GEMINI_FEATURE_PROMPT_THREE_REVISION,
];

pub const GEMINI_TRANSCRIPT_PROMPT: &str = include_str!("gemini-transcript-prompt.txt");
pub const GEMINI_FEATURE_PROMPT_ONE: &str = include_str!("gemini-feature-1-prompt.txt");
pub const GEMINI_FEATURE_PROMPT_TWO: &str = include_str!("gemini-feature-2-prompt.txt");
pub const GEMINI_FEATURE_PROMPT_THREE: &str = include_str!("gemini-feature-3-prompt.txt");
pub const GPT_STRUCTURING_PROMPT: &str = include_str!("gpt-structuring-prompt.txt");
pub const TERRA_SPEAKER_LABELS_PROMPT: &str = include_str!("terra-speaker-labels-prompt.txt");

pub const RECORD_SPEAKER_LABELS_TOOL_NAME: &str = "record_speaker_labels";
pub const RECORD_SPEAKER_LABELS_TOOL_DESCRIPTION: &str = "Record every exact local Speaker N label from the supplied Gemini transcript in first-appearance order.";
pub const RECORD_SPEAKER_ANALYSIS_TOOL_NAME: &str = "record_speaker_analysis";
pub const RECORD_SPEAKER_ANALYSIS_TOOL_DESCRIPTION: &str = "Record the exact transcript and complete structured 24-feature analysis for every local speaker.";

pub const FEATURE_PACKETS: [FeaturePacket; 3] =
    [FeaturePacket::One, FeaturePacket::Two, FeaturePacket::Three];

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GeminiRequestPart<'a> {
    Audio {
        media_type: &'static str,
        bytes: &'a [u8],
    },
    Text(String),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum FeaturePacket {
    One,
    Two,
    Three,
}

impl FeaturePacket {
    pub fn index(self) -> usize {
        match self {
            Self::One => 0,
            Self::Two => 1,
            Self::Three => 2,
        }
    }

    pub fn prompt(self) -> &'static str {
        match self {
            Self::One => GEMINI_FEATURE_PROMPT_ONE,
            Self::Two => GEMINI_FEATURE_PROMPT_TWO,
            Self::Three => GEMINI_FEATURE_PROMPT_THREE,
        }
    }

    pub fn revision(self) -> &'static str {
        match self {
            Self::One => GEMINI_FEATURE_PROMPT_ONE_REVISION,
            Self::Two => GEMINI_FEATURE_PROMPT_TWO_REVISION,
            Self::Three => GEMINI_FEATURE_PROMPT_THREE_REVISION,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ProtocolError {
    Blank(&'static str),
    InvalidGeminiResponse(String),
    GeminiTextCandidateCount(usize),
    InvalidSpeakerLabelsArguments(String),
    DuplicateSpeakerLabel(LocalSpeakerLabel),
    InvalidFinalArguments(String),
    InvalidStructuredAnalysis(AnalysisValidationError),
}

impl fmt::Display for ProtocolError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Blank(field) => write!(formatter, "{field} is blank"),
            Self::InvalidGeminiResponse(message) => {
                write!(formatter, "invalid Gemini response: {message}")
            }
            Self::GeminiTextCandidateCount(count) => {
                write!(
                    formatter,
                    "Gemini returned {count} nonblank textual candidates"
                )
            }
            Self::InvalidSpeakerLabelsArguments(message) => {
                write!(formatter, "invalid speaker-label arguments: {message}")
            }
            Self::DuplicateSpeakerLabel(label) => {
                write!(formatter, "duplicate speaker label: {label}")
            }
            Self::InvalidFinalArguments(message) => {
                write!(formatter, "invalid final analysis arguments: {message}")
            }
            Self::InvalidStructuredAnalysis(error) => {
                write!(formatter, "invalid structured analysis: {error}")
            }
        }
    }
}

impl Error for ProtocolError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::InvalidStructuredAnalysis(error) => Some(error),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToolDefinition {
    pub name: &'static str,
    pub description: &'static str,
    pub input_schema: Value,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct TerraSpeakerLabelsInput {
    transcript: String,
}

impl TerraSpeakerLabelsInput {
    pub fn new(transcript: String) -> Result<Self, ProtocolError> {
        require_nonblank(&transcript, "transcript")?;
        Ok(Self { transcript })
    }

    pub fn instruction(&self) -> &'static str {
        TERRA_SPEAKER_LABELS_PROMPT
    }

    pub fn transcript(&self) -> &str {
        &self.transcript
    }

    pub fn render(&self) -> String {
        format!(
            "{}Input:\n{}",
            self.instruction(),
            serde_json::to_string(self).expect("serializing strings cannot fail")
        )
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SpeakerFeatureEvidence {
    speaker: LocalSpeakerLabel,
    feature_packets: [String; 3],
}

impl SpeakerFeatureEvidence {
    pub fn new(
        speaker: LocalSpeakerLabel,
        packet_one: String,
        packet_two: String,
        packet_three: String,
    ) -> Result<Self, ProtocolError> {
        require_nonblank(&packet_one, "feature_packet_one")?;
        require_nonblank(&packet_two, "feature_packet_two")?;
        require_nonblank(&packet_three, "feature_packet_three")?;
        Ok(Self {
            speaker,
            feature_packets: [packet_one, packet_two, packet_three],
        })
    }

    pub fn speaker(&self) -> LocalSpeakerLabel {
        self.speaker
    }

    pub fn packet(&self, packet: FeaturePacket) -> &str {
        &self.feature_packets[packet.index()]
    }

    pub fn packets(&self) -> [&str; 3] {
        self.feature_packets.each_ref().map(String::as_str)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct TerraFinalInput {
    transcript: String,
    speakers: Vec<SpeakerFeatureEvidence>,
}

impl TerraFinalInput {
    pub fn new(
        transcript: String,
        speakers: Vec<SpeakerFeatureEvidence>,
    ) -> Result<Self, ProtocolError> {
        require_nonblank(&transcript, "transcript")?;
        let mut labels = BTreeSet::new();
        for speaker in &speakers {
            if !labels.insert(speaker.speaker()) {
                return Err(ProtocolError::DuplicateSpeakerLabel(speaker.speaker()));
            }
        }
        Ok(Self {
            transcript,
            speakers,
        })
    }

    pub fn instruction(&self) -> &'static str {
        GPT_STRUCTURING_PROMPT
    }

    pub fn transcript(&self) -> &str {
        &self.transcript
    }

    pub fn speakers(&self) -> &[SpeakerFeatureEvidence] {
        &self.speakers
    }

    pub fn render(&self) -> String {
        format!(
            "{}Input:\n{}",
            self.instruction(),
            serde_json::to_string(self).expect("serializing strings cannot fail")
        )
    }
}

pub fn gemini_transcript_request(audio: &[u8]) -> [GeminiRequestPart<'_>; 2] {
    [
        GeminiRequestPart::Audio {
            media_type: "audio/ogg",
            bytes: audio,
        },
        GeminiRequestPart::Text(GEMINI_TRANSCRIPT_PROMPT.to_owned()),
    ]
}

pub fn gemini_feature_cached_prefix<'a>(
    audio: &'a [u8],
    transcript: &str,
) -> [GeminiRequestPart<'a>; 2] {
    let (shared_prefix, _) = feature_prompt_parts(FeaturePacket::One);
    [
        GeminiRequestPart::Audio {
            media_type: "audio/ogg",
            bytes: audio,
        },
        GeminiRequestPart::Text(format!("{shared_prefix}{transcript}")),
    ]
}

pub fn gemini_feature_suffix(packet: FeaturePacket, target: LocalSpeakerLabel) -> String {
    let (_, suffix) = feature_prompt_parts(packet);
    suffix.replace("{{TARGET_SPEAKER}}", &target.to_string())
}

pub fn extract_gemini_text(response: &Value) -> Result<String, ProtocolError> {
    let candidates = response
        .get("candidates")
        .and_then(Value::as_array)
        .ok_or_else(|| ProtocolError::InvalidGeminiResponse("candidates is not an array".into()))?;
    let mut textual_candidates = Vec::new();
    for (candidate_index, candidate) in candidates.iter().enumerate() {
        let parts = candidate
            .get("content")
            .and_then(|content| content.get("parts"))
            .and_then(Value::as_array)
            .ok_or_else(|| {
                ProtocolError::InvalidGeminiResponse(format!(
                    "candidate {candidate_index} has no content parts array"
                ))
            })?;
        let mut text = String::new();
        for (part_index, part) in parts.iter().enumerate() {
            if let Some(value) = part.get("text") {
                let value = value.as_str().ok_or_else(|| {
                    ProtocolError::InvalidGeminiResponse(format!(
                        "candidate {candidate_index} part {part_index} text is not a string"
                    ))
                })?;
                text.push_str(value);
            }
        }
        if !text.trim().is_empty() {
            textual_candidates.push(text);
        }
    }
    if textual_candidates.len() != 1 {
        return Err(ProtocolError::GeminiTextCandidateCount(
            textual_candidates.len(),
        ));
    }
    Ok(textual_candidates.pop().expect("length checked"))
}

pub fn record_speaker_labels_tool() -> ToolDefinition {
    ToolDefinition {
        name: RECORD_SPEAKER_LABELS_TOOL_NAME,
        description: RECORD_SPEAKER_LABELS_TOOL_DESCRIPTION,
        input_schema: json!({
            "type": "object",
            "additionalProperties": false,
            "required": ["speakers"],
            "properties": {
                "speakers": {
                    "type": "array",
                    "items": {
                        "type": "string",
                        "pattern": "^Speaker [1-9][0-9]*$"
                    }
                }
            }
        }),
    }
}

pub fn decode_record_speaker_labels_arguments(
    arguments: &Value,
) -> Result<Vec<LocalSpeakerLabel>, ProtocolError> {
    let object = arguments.as_object().ok_or_else(|| {
        ProtocolError::InvalidSpeakerLabelsArguments("arguments is not an object".into())
    })?;
    require_exact_keys(object, &["speakers"])
        .map_err(ProtocolError::InvalidSpeakerLabelsArguments)?;
    let values = object
        .get("speakers")
        .and_then(Value::as_array)
        .ok_or_else(|| {
            ProtocolError::InvalidSpeakerLabelsArguments("speakers is not an array".into())
        })?;
    let mut labels = Vec::with_capacity(values.len());
    let mut unique = BTreeSet::new();
    for (index, value) in values.iter().enumerate() {
        let raw = value.as_str().ok_or_else(|| {
            ProtocolError::InvalidSpeakerLabelsArguments(format!("speaker {index} is not a string"))
        })?;
        let label = parse_canonical_label(raw).map_err(|message| {
            ProtocolError::InvalidSpeakerLabelsArguments(format!("speaker {index}: {message}"))
        })?;
        if !unique.insert(label) {
            return Err(ProtocolError::DuplicateSpeakerLabel(label));
        }
        labels.push(label);
    }
    Ok(labels)
}

pub fn record_speaker_analysis_tool() -> ToolDefinition {
    let mut feature_properties = Map::new();
    for name in FEATURE_NAMES {
        feature_properties.insert(name.into(), feature_property(name));
    }
    ToolDefinition {
        name: RECORD_SPEAKER_ANALYSIS_TOOL_NAME,
        description: RECORD_SPEAKER_ANALYSIS_TOOL_DESCRIPTION,
        input_schema: json!({
            "type": "object",
            "additionalProperties": false,
            "required": ["transcript", "speakers"],
            "properties": {
                "transcript": {
                    "type": "string"
                },
                "speakers": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "additionalProperties": false,
                        "required": [
                            "speaker",
                            "language",
                            "features",
                            "features_usable_for_training"
                        ],
                        "properties": {
                            "speaker": {
                                "type": "string",
                                "pattern": "^Speaker [1-9][0-9]*$"
                            },
                            "language": {
                                "type": "string"
                            },
                            "features": {
                                "type": "object",
                                "additionalProperties": false,
                                "required": FEATURE_NAMES,
                                "properties": feature_properties
                            },
                            "features_usable_for_training": {
                                "type": "boolean"
                            }
                        }
                    }
                }
            }
        }),
    }
}

pub fn decode_record_speaker_analysis_arguments(
    arguments: &Value,
) -> Result<StructuredAnalysis, ProtocolError> {
    validate_final_shape(arguments)?;
    let analysis: StructuredAnalysis = serde_json::from_value(arguments.clone())
        .map_err(|error| ProtocolError::InvalidFinalArguments(error.to_string()))?;
    analysis
        .validate()
        .map_err(ProtocolError::InvalidStructuredAnalysis)?;
    Ok(analysis)
}

fn require_nonblank(value: &str, field: &'static str) -> Result<(), ProtocolError> {
    if value.trim().is_empty() {
        return Err(ProtocolError::Blank(field));
    }
    Ok(())
}

fn feature_prompt_parts(packet: FeaturePacket) -> (&'static str, &'static str) {
    packet
        .prompt()
        .split_once("{{TRANSCRIPT}}")
        .expect("frozen feature prompt contains transcript placeholder")
}

fn parse_canonical_label(value: &str) -> Result<LocalSpeakerLabel, String> {
    let label = value
        .parse::<LocalSpeakerLabel>()
        .map_err(|error| error.to_string())?;
    if label.to_string() != value {
        return Err(format!("noncanonical speaker label: {value}"));
    }
    Ok(label)
}

fn require_exact_keys(object: &Map<String, Value>, expected: &[&str]) -> Result<(), String> {
    if object.len() != expected.len() || expected.iter().any(|key| !object.contains_key(*key)) {
        let found = object.keys().cloned().collect::<Vec<_>>().join(", ");
        return Err(format!(
            "object keys must be exactly [{}], found [{found}]",
            expected.join(", ")
        ));
    }
    Ok(())
}

fn feature_property(name: &str) -> Value {
    match name {
        "dominant_rhotic_realization" | "dominant_lateral_realization" => {
            json!({ "type": ["string", "null"] })
        }
        "vocal_gender_presentation" => json!({
            "type": ["string", "null"],
            "enum": [
                "strongly_feminine",
                "feminine",
                "androgynous",
                "masculine",
                "strongly_masculine",
                null
            ]
        }),
        _ => json!({ "type": ["number", "null"] }),
    }
}

fn validate_final_shape(arguments: &Value) -> Result<(), ProtocolError> {
    let object = arguments
        .as_object()
        .ok_or_else(|| ProtocolError::InvalidFinalArguments("arguments is not an object".into()))?;
    require_exact_keys(object, &["transcript", "speakers"])
        .map_err(ProtocolError::InvalidFinalArguments)?;
    let speakers = object
        .get("speakers")
        .and_then(Value::as_array)
        .ok_or_else(|| ProtocolError::InvalidFinalArguments("speakers is not an array".into()))?;
    for (speaker_index, speaker) in speakers.iter().enumerate() {
        let speaker_object = speaker.as_object().ok_or_else(|| {
            ProtocolError::InvalidFinalArguments(format!(
                "speaker {speaker_index} is not an object"
            ))
        })?;
        require_exact_keys(
            speaker_object,
            &[
                "speaker",
                "language",
                "features",
                "features_usable_for_training",
            ],
        )
        .map_err(|message| {
            ProtocolError::InvalidFinalArguments(format!("speaker {speaker_index}: {message}"))
        })?;
        let raw_label = speaker_object
            .get("speaker")
            .and_then(Value::as_str)
            .ok_or_else(|| {
                ProtocolError::InvalidFinalArguments(format!(
                    "speaker {speaker_index} label is not a string"
                ))
            })?;
        parse_canonical_label(raw_label).map_err(|message| {
            ProtocolError::InvalidFinalArguments(format!("speaker {speaker_index}: {message}"))
        })?;
        let features = speaker_object
            .get("features")
            .and_then(Value::as_object)
            .ok_or_else(|| {
                ProtocolError::InvalidFinalArguments(format!(
                    "speaker {speaker_index} features is not an object"
                ))
            })?;
        require_exact_keys(features, &FEATURE_NAMES).map_err(|message| {
            ProtocolError::InvalidFinalArguments(format!(
                "speaker {speaker_index} features: {message}"
            ))
        })?;
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Instant;

    fn label(number: u32) -> LocalSpeakerLabel {
        LocalSpeakerLabel::new(number).unwrap()
    }

    fn candidate(parts: Vec<Value>) -> Value {
        json!({ "content": { "parts": parts } })
    }

    fn null_features() -> Map<String, Value> {
        FEATURE_NAMES
            .into_iter()
            .map(|name| (name.into(), Value::Null))
            .collect()
    }

    fn final_arguments() -> Value {
        json!({
            "transcript": "[high] Speaker 1: exact words",
            "speakers": [{
                "speaker": "Speaker 1",
                "language": "English",
                "features": null_features(),
                "features_usable_for_training": true
            }]
        })
    }

    #[test]
    fn revisions_and_frozen_prompts_cover_the_workflow() {
        assert_eq!(
            GEMINI_FEATURE_PROMPT_REVISIONS,
            [
                "speaker-v3-gemini-feature-1-r2",
                "speaker-v3-gemini-feature-2-r2",
                "speaker-v3-gemini-feature-3-r2",
            ]
        );
        assert_eq!(
            TERRA_SPEAKER_LABELS_PROMPT_REVISION,
            "speaker-v3-terra-labels-r1"
        );
        assert!(GEMINI_TRANSCRIPT_PROMPT.contains("[high] Speaker N:"));
        assert!(GPT_STRUCTURING_PROMPT.contains("record_speaker_analysis"));
        assert!(TERRA_SPEAKER_LABELS_PROMPT.contains("record_speaker_labels"));
        for (packet, names) in FEATURE_PACKETS.into_iter().zip([
            &FEATURE_NAMES[..8],
            &FEATURE_NAMES[8..16],
            &FEATURE_NAMES[16..],
        ]) {
            assert!(packet.prompt().contains("{{TRANSCRIPT}}"));
            assert!(packet.prompt().contains("{{TARGET_SPEAKER}}"));
            for name in names {
                assert!(packet.prompt().contains(name));
            }
        }
    }

    #[test]
    fn gemini_requests_keep_audio_prefix_and_target_last() {
        let audio = b"OggS bytes";
        let transcript_request = gemini_transcript_request(audio);
        assert_eq!(
            transcript_request[0],
            GeminiRequestPart::Audio {
                media_type: "audio/ogg",
                bytes: audio,
            }
        );
        assert_eq!(
            transcript_request[1],
            GeminiRequestPart::Text(GEMINI_TRANSCRIPT_PROMPT.into())
        );

        let transcript = "literal {{TARGET_SPEAKER}}\ntranscript";
        let prefix = gemini_feature_cached_prefix(audio, transcript);
        assert_eq!(
            prefix[0],
            GeminiRequestPart::Audio {
                media_type: "audio/ogg",
                bytes: audio,
            }
        );
        let GeminiRequestPart::Text(prefix_text) = &prefix[1] else {
            panic!()
        };
        let (shared, _) = feature_prompt_parts(FeaturePacket::One);
        assert_eq!(prefix_text, &format!("{shared}{transcript}"));

        for packet in FEATURE_PACKETS {
            let suffix = gemini_feature_suffix(packet, label(12));
            assert!(!suffix.contains("{{TARGET_SPEAKER}}"));
            assert!(suffix.ends_with("Speaker 12\n"));
            assert_eq!(
                packet.revision(),
                GEMINI_FEATURE_PROMPT_REVISIONS[packet.index()]
            );
            assert_eq!(
                feature_prompt_parts(packet).0,
                feature_prompt_parts(FeaturePacket::One).0
            );
        }
    }

    #[test]
    fn gemini_extraction_preserves_one_textual_candidate() {
        let response = json!({
            "candidates": [
                candidate(vec![
                    json!({"text": "a\n"}),
                    json!({"inlineData": {}}),
                    json!({"text": " b"})
                ]),
                candidate(vec![json!({"text": "   "})])
            ]
        });
        assert_eq!(extract_gemini_text(&response).unwrap(), "a\n b");

        let ambiguous = json!({
            "candidates": [
                candidate(vec![json!({"text": "one"})]),
                candidate(vec![json!({"text": "two"})])
            ]
        });
        assert_eq!(
            extract_gemini_text(&ambiguous),
            Err(ProtocolError::GeminiTextCandidateCount(2))
        );
        assert!(matches!(
            extract_gemini_text(&json!({})),
            Err(ProtocolError::InvalidGeminiResponse(_))
        ));
    }

    #[test]
    fn label_tool_and_decode_are_strict_ordered_and_unbounded() {
        let tool = record_speaker_labels_tool();
        assert_eq!(tool.name, "record_speaker_labels");
        assert_eq!(tool.input_schema["additionalProperties"], false);
        assert!(
            tool.input_schema["properties"]["speakers"]
                .get("maxItems")
                .is_none()
        );

        let arguments = json!({
            "speakers": (1..=1000)
                .map(|number| format!("Speaker {number}"))
                .collect::<Vec<_>>()
        });
        let decoded = decode_record_speaker_labels_arguments(&arguments).unwrap();
        assert_eq!(decoded.len(), 1000);
        assert_eq!(decoded[0], label(1));
        assert_eq!(decoded[999], label(1000));

        assert!(matches!(
            decode_record_speaker_labels_arguments(
                &json!({"speakers": ["Speaker 1", "Speaker 1"]})
            ),
            Err(ProtocolError::DuplicateSpeakerLabel(_))
        ));
        for invalid in ["Unknown", "Speaker 0", "Speaker 01", "speaker 1"] {
            assert!(
                decode_record_speaker_labels_arguments(&json!({"speakers": [invalid]})).is_err()
            );
        }
        assert!(
            decode_record_speaker_labels_arguments(&json!({"speakers": [], "extra": true}))
                .is_err()
        );

        let input = TerraSpeakerLabelsInput::new("x\n\"Input:\\n\" {{raw}}".into()).unwrap();
        let rendered = input.render();
        let serialized = rendered
            .strip_prefix(TERRA_SPEAKER_LABELS_PROMPT)
            .unwrap()
            .strip_prefix("Input:\n")
            .unwrap();
        let decoded_input: Value = serde_json::from_str(serialized).unwrap();
        assert_eq!(decoded_input["transcript"], input.transcript());
    }

    #[test]
    fn evidence_and_final_input_preserve_adversarial_text_and_order() {
        let first = SpeakerFeatureEvidence::new(
            label(2),
            "one\n\"x\"".into(),
            "two {{raw}}".into(),
            "three Input:\n".into(),
        )
        .unwrap();
        assert_eq!(
            first.packets(),
            ["one\n\"x\"", "two {{raw}}", "three Input:\n"]
        );
        let second =
            SpeakerFeatureEvidence::new(label(1), "four".into(), "five".into(), "six".into())
                .unwrap();
        let input = TerraFinalInput::new(
            "transcript\n\"quoted\"".into(),
            vec![first.clone(), second.clone()],
        )
        .unwrap();
        assert_eq!(input.speakers()[0].speaker(), label(2));
        assert_eq!(input.speakers()[1].speaker(), label(1));
        let rendered = input.render();
        let serialized = rendered
            .strip_prefix(GPT_STRUCTURING_PROMPT)
            .unwrap()
            .strip_prefix("Input:\n")
            .unwrap();
        let decoded: Value = serde_json::from_str(serialized).unwrap();
        assert_eq!(decoded["transcript"], input.transcript());
        assert_eq!(decoded["speakers"][0]["speaker"], "Speaker 2");
        assert_eq!(
            decoded["speakers"][0]["feature_packets"][0],
            first.packet(FeaturePacket::One)
        );
        assert_eq!(
            decoded["speakers"][1]["feature_packets"][2],
            second.packet(FeaturePacket::Three)
        );
        assert!(TerraFinalInput::new("x".into(), vec![first.clone(), first]).is_err());
    }

    #[test]
    fn final_tool_schema_is_closed_complete_and_nullable() {
        let tool = record_speaker_analysis_tool();
        assert_eq!(tool.name, "record_speaker_analysis");
        assert_eq!(tool.input_schema["additionalProperties"], false);
        let speaker = &tool.input_schema["properties"]["speakers"]["items"];
        assert_eq!(speaker["additionalProperties"], false);
        let features = &speaker["properties"]["features"];
        assert_eq!(features["additionalProperties"], false);
        assert_eq!(features["required"].as_array().unwrap().len(), 24);
        assert_eq!(features["properties"].as_object().unwrap().len(), 24);
        for name in FEATURE_NAMES {
            assert!(features["properties"][name].to_string().contains("null"));
        }
    }

    #[test]
    fn final_decode_requires_complete_closed_valid_analysis() {
        let arguments = final_arguments();
        let analysis = decode_record_speaker_analysis_arguments(&arguments).unwrap();
        assert_eq!(analysis.transcript, "[high] Speaker 1: exact words");
        assert_eq!(analysis.speakers.len(), 1);
        assert!(analysis.speakers[0].features_usable_for_training);

        let mut extra = arguments.clone();
        extra
            .as_object_mut()
            .unwrap()
            .insert("extra".into(), json!(true));
        assert!(decode_record_speaker_analysis_arguments(&extra).is_err());

        let mut missing = arguments.clone();
        missing["speakers"][0]["features"]
            .as_object_mut()
            .unwrap()
            .remove("median_f0_hz");
        assert!(decode_record_speaker_analysis_arguments(&missing).is_err());

        let mut nested_extra = arguments.clone();
        nested_extra["speakers"][0]["features"]
            .as_object_mut()
            .unwrap()
            .insert("extra".into(), Value::Null);
        assert!(decode_record_speaker_analysis_arguments(&nested_extra).is_err());

        let mut noncanonical = arguments;
        noncanonical["speakers"][0]["speaker"] = json!("Speaker 01");
        assert!(decode_record_speaker_analysis_arguments(&noncanonical).is_err());
    }

    #[test]
    fn reference_scale_canary_completes_local_work() {
        let started = Instant::now();
        let text = "x".repeat(1_048_576);
        let response = json!({
            "candidates": [
                candidate(vec![json!({"text": text})])
            ]
        });
        assert_eq!(extract_gemini_text(&response).unwrap().len(), 1_048_576);

        let arguments = json!({
            "speakers": (1..=1000)
                .map(|number| format!("Speaker {number}"))
                .collect::<Vec<_>>()
        });
        assert_eq!(
            decode_record_speaker_labels_arguments(&arguments)
                .unwrap()
                .len(),
            1000
        );

        let speakers = (1..=1000)
            .map(|number| {
                SpeakerFeatureEvidence::new(
                    label(number),
                    "a".repeat(1024),
                    "b".repeat(1024),
                    "c".repeat(1024),
                )
                .unwrap()
            })
            .collect();
        let input = TerraFinalInput::new("x".repeat(1_048_576), speakers).unwrap();
        assert!(input.render().len() > 4_000_000);
        assert!(started.elapsed().as_secs() < 10);
    }
}