turnframe-tasks 0.1.0

Small, typed model tasks for Turnframe: per-task prompts, models and settings, budgets, repair, voting, escalation and a record of every call
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
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
//! Running a task: the call, repairs with the exact error, votes, escalation, records.
//!
//! One run is: resolve the instructions, build the request from the task's messages
//! and schema and its kind's profile, reserve the call, send it, parse and check the
//! answer. A structurally wrong answer goes back to the same model with the error,
//! up to the profile's repairs. Votes run concurrently and are compared with the
//! task's own `agree`; a vote without a strict majority escalates, asks or fails, as
//! the profile says. Every call leaves a record in the scope.

use std::sync::{Arc, Mutex, PoisonError};
use std::time::{Duration, Instant};

use futures::future::join_all;
use turnframe_core::effort::Effort;
use turnframe_core::hash::Digest;
use turnframe_core::locale::Locale;
use turnframe_core::observe::{NoopObserver, Observer, Signal, SignalLabels};
use turnframe_core::prompt::{PromptSelector, PromptSource};
use turnframe_core::replay::{BudgetReport, TaskParams, TaskRecord, TaskVerdict};
use turnframe_provider::fallback::{FallbackOptions, FallbackStage, execute_with_fallback};
use turnframe_provider::ids::ModelRef;
use turnframe_provider::request::{CacheHint, Message, ModelRequest, OutputSpec};
use turnframe_provider::response::ModelResponse;
use turnframe_provider::router::{ProviderRouter, RoutingPolicy};
use turnframe_provider::structured::{CompiledSchema, SchemaCache, parse_structured};

use crate::budget::{Budget, BudgetBound, BudgetTracker};
use crate::instructions::{self, Instructions};
use crate::profile::{Disagreement, TaskProfile, TaskProfiles};
use crate::task::{ModelTask, TaskId, TaskKind};

/// The request metadata key carrying the task id a call runs as: `u2/extract#repair1`.
pub const TASK_LABEL: &str = "task";

/// The request metadata key carrying the turn a call belongs to, when the scope names it.
pub const TURN_LABEL: &str = "turn";

/// Framing of a repair round when no prompt source supplies `<name>.repair`.
const BUILT_IN_REPAIR: &str = "Your previous answer was not accepted. Answer again with a \
     document that satisfies the schema and fixes this:";

/// What a turn keeps of each call beyond the parsed answer.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct RecordPolicy {
    /// Keep the request as sent. It carries the user's words.
    pub keep_prompts: bool,
    /// Keep the answer as received, before parsing.
    pub keep_raw_output: bool,
}

/// The spending and the records of one phase of one turn.
#[derive(Debug)]
pub struct TaskScope {
    budget: BudgetTracker,
    records: Mutex<Vec<TaskRecord>>,
    locale: Locale,
    turn: Option<String>,
    profiles: Option<TaskProfiles>,
    effort: Option<Effort>,
}

impl TaskScope {
    /// A scope bounded by `budget`, for a turn in `locale`.
    #[must_use]
    pub fn new(budget: Budget, locale: Locale) -> Self {
        Self {
            budget: BudgetTracker::new(budget),
            records: Mutex::new(Vec::new()),
            locale,
            turn: None,
            profiles: None,
            effort: None,
        }
    }

    /// Runs this scope's tasks under `profiles` instead of the engine's.
    #[must_use]
    pub fn with_profiles(mut self, profiles: TaskProfiles) -> Self {
        self.profiles = Some(profiles);
        self
    }

    /// Labels this scope's task signals with the turn's effort.
    #[must_use]
    pub const fn with_effort(mut self, effort: Effort) -> Self {
        self.effort = Some(effort);
        self
    }

    /// The effort this scope's turn runs at, when it was given one.
    #[must_use]
    pub const fn effort(&self) -> Option<Effort> {
        self.effort
    }

    fn labels(&self) -> SignalLabels {
        let labels = SignalLabels::default();
        match self.effort {
            Some(effort) => labels.with_effort(effort),
            None => labels,
        }
    }

    /// Labels every call of this scope with the turn it belongs to, for a trace or a
    /// provider's own logs to group them.
    #[must_use]
    pub fn for_turn(mut self, turn: impl Into<String>) -> Self {
        self.turn = Some(turn.into());
        self
    }

    /// Every call recorded so far, in the order the calls finished.
    #[must_use]
    pub fn records(&self) -> Vec<TaskRecord> {
        self.lock().clone()
    }

    /// What was spent.
    #[must_use]
    pub fn budget_report(&self) -> BudgetReport {
        self.budget.report()
    }

    /// The first bound reached, if any.
    #[must_use]
    pub fn exhausted(&self) -> Option<BudgetBound> {
        self.budget.exhausted()
    }

    /// The turn's locale.
    #[must_use]
    pub const fn locale(&self) -> &Locale {
        &self.locale
    }

    fn push(&self, record: TaskRecord) {
        self.lock().push(record);
    }

    fn mark(&self, task_id: &str, verdict: &TaskVerdict) {
        if let Some(record) = self.lock().iter_mut().find(|r| r.task_id == task_id) {
            record.verdict = verdict.clone();
        }
    }

    fn lock(&self) -> std::sync::MutexGuard<'_, Vec<TaskRecord>> {
        self.records.lock().unwrap_or_else(PoisonError::into_inner)
    }
}

/// Where a task call sits: its identifier, its parent, and how deep its chain already is.
#[derive(Debug, Clone, Copy)]
pub struct TaskCall<'a> {
    /// The task's identifier.
    pub id: &'a TaskId,
    /// The task that asked for this one.
    pub parent: Option<&'a TaskId>,
    /// Calls already made in this chain; the first call of a turn is at depth 1.
    pub depth: u8,
}

/// Why a task produced no answer to use.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum TaskFailure {
    /// The profile switches the task off.
    Disabled,
    /// A bound of the turn's budget was reached.
    Budget(BudgetBound),
    /// No profile can serve the task.
    Routing(String),
    /// Every provider attempt failed.
    Provider(String),
    /// The answers failed their checks, repairs included.
    Invalid {
        /// Stable code of the last failed check.
        code: String,
        /// The failure in words.
        reason: String,
    },
    /// The votes found no majority and the profile treats that as failure.
    Disagreement,
}

impl TaskFailure {
    /// Stable label, for records and metrics.
    #[must_use]
    pub fn code(&self) -> String {
        match self {
            Self::Disabled => "disabled".to_owned(),
            Self::Budget(bound) => format!("budget_{}", bound.as_str()),
            Self::Routing(_) => "routing".to_owned(),
            Self::Provider(code) => format!("provider_{code}"),
            Self::Invalid { code, .. } => code.clone(),
            Self::Disagreement => "vote_disagreement".to_owned(),
        }
    }
}

/// What a task run produced, and how deep its chain ended.
#[derive(Debug, Clone)]
pub enum TaskOutcome<O> {
    /// An answer to use.
    Accepted {
        /// The answer.
        output: O,
        /// Depth of the last call made.
        depth: u8,
    },
    /// The votes found no majority, and the profile hands the answers back to ask.
    Disagreed {
        /// Every answer the votes produced.
        answers: Vec<O>,
        /// Depth of the last call made.
        depth: u8,
    },
    /// No answer to use.
    Failed {
        /// Why.
        failure: TaskFailure,
        /// Depth of the last call made.
        depth: u8,
    },
}

impl<O> TaskOutcome<O> {
    /// The answer, when there is one to use.
    #[must_use]
    pub fn accepted(self) -> Option<O> {
        match self {
            Self::Accepted { output, .. } => Some(output),
            _ => None,
        }
    }

    /// Depth of the last call made, for the task that follows.
    #[must_use]
    pub const fn depth(&self) -> u8 {
        match self {
            Self::Accepted { depth, .. }
            | Self::Disagreed { depth, .. }
            | Self::Failed { depth, .. } => *depth,
        }
    }

    fn label(&self) -> String {
        match self {
            Self::Accepted { .. } => "accepted".to_owned(),
            Self::Disagreed { .. } => "disagreed".to_owned(),
            Self::Failed { failure, .. } => failure.code(),
        }
    }
}

/// Runs model tasks. Cheap to clone; shared by every turn.
#[derive(Clone)]
pub struct TaskEngine {
    router: Arc<dyn ProviderRouter>,
    routing: RoutingPolicy,
    fallback: Arc<FallbackOptions>,
    schemas: SchemaCache,
    profiles: TaskProfiles,
    prompts: Option<Arc<dyn PromptSource>>,
    selector: PromptSelector,
    records: RecordPolicy,
    observer: Arc<dyn Observer>,
}

impl std::fmt::Debug for TaskEngine {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TaskEngine")
            .field("profiles", &self.profiles)
            .field("records", &self.records)
            .finish_non_exhaustive()
    }
}

/// Builds a [`TaskEngine`].
#[derive(Debug)]
pub struct TaskEngineBuilder {
    engine: TaskEngine,
}

impl TaskEngineBuilder {
    /// Base routing policy; each call adds its profile's model tag.
    #[must_use]
    pub fn routing(mut self, routing: RoutingPolicy) -> Self {
        self.engine.routing = routing;
        self
    }

    /// Transport retry and fallback options.
    #[must_use]
    pub fn fallback(mut self, options: FallbackOptions) -> Self {
        self.engine.fallback = Arc::new(options);
        self
    }

    /// The profiles every task kind runs under.
    #[must_use]
    pub fn profiles(mut self, profiles: TaskProfiles) -> Self {
        self.engine.profiles = profiles;
        self
    }

    /// A prompt source asked for each task's instructions, pinned or labelled by `selector`.
    #[must_use]
    pub fn prompts(mut self, source: Arc<dyn PromptSource>, selector: PromptSelector) -> Self {
        self.engine.prompts = Some(source);
        self.engine.selector = selector;
        self
    }

    /// What each record keeps beyond the parsed answer.
    #[must_use]
    pub fn records(mut self, policy: RecordPolicy) -> Self {
        self.engine.records = policy;
        self
    }

    /// Where the task signals go.
    #[must_use]
    pub fn observer(mut self, observer: Arc<dyn Observer>) -> Self {
        self.engine.observer = observer;
        self
    }

    /// The engine.
    #[must_use]
    pub fn build(self) -> TaskEngine {
        self.engine
    }
}

/// What a task run needs beside the task: resolved once, shared by votes and repairs.
#[derive(Clone)]
struct Prepared {
    kind: TaskKind,
    parent: Option<String>,
    profile: TaskProfile,
    instructions: Instructions,
    repair: Instructions,
    schema: CompiledSchema,
    messages: Vec<Message>,
}

/// One chain of calls: the first call and its repairs.
enum Chain<O> {
    Answered {
        output: O,
        depth: u8,
        record: String,
    },
    Unusable {
        failure: TaskFailure,
        depth: u8,
    },
}

impl TaskEngine {
    /// An engine over `router` with the shipped profiles.
    #[must_use]
    pub fn builder(router: Arc<dyn ProviderRouter>) -> TaskEngineBuilder {
        TaskEngineBuilder {
            engine: Self {
                router,
                routing: RoutingPolicy::new(),
                fallback: Arc::new(FallbackOptions::new()),
                schemas: SchemaCache::new(),
                profiles: TaskProfiles::new(),
                prompts: None,
                selector: PromptSelector::Latest,
                records: RecordPolicy::default(),
                observer: Arc::new(NoopObserver),
            },
        }
    }

    /// The profiles in force.
    #[must_use]
    pub const fn profiles(&self) -> &TaskProfiles {
        &self.profiles
    }

    /// The profile `kind` runs under in `scope`: the scope's, else the engine's.
    #[must_use]
    pub fn profile(&self, scope: &TaskScope, kind: TaskKind) -> TaskProfile {
        scope.profiles.as_ref().unwrap_or(&self.profiles).get(kind)
    }

    /// Runs `task` on `input`.
    pub async fn run<T: ModelTask>(
        &self,
        scope: &TaskScope,
        call: TaskCall<'_>,
        task: &T,
        input: &T::Input,
    ) -> TaskOutcome<T::Output> {
        self.run_inner(scope, call, task, input, None).await
    }

    /// Runs `task` again after `previous` was found wanting, telling the model why.
    ///
    /// For a semantic check made by another task (a verifier) and not by `check`.
    pub async fn run_with_feedback<T: ModelTask>(
        &self,
        scope: &TaskScope,
        call: TaskCall<'_>,
        task: &T,
        input: &T::Input,
        previous: &T::Output,
        feedback: &str,
    ) -> TaskOutcome<T::Output> {
        self.run_inner(scope, call, task, input, Some((previous, feedback)))
            .await
    }

    async fn run_inner<T: ModelTask>(
        &self,
        scope: &TaskScope,
        call: TaskCall<'_>,
        task: &T,
        input: &T::Input,
        feedback: Option<(&T::Output, &str)>,
    ) -> TaskOutcome<T::Output> {
        let outcome = match self.prepare(scope, call, task, input, feedback).await {
            Ok(prepared) => self.run_prepared(scope, call, task, input, &prepared).await,
            Err(failure) => TaskOutcome::Failed {
                failure,
                depth: call.depth,
            },
        };
        let labels = scope
            .labels()
            .with_purpose(task.kind().as_str())
            .with_error_code(outcome.label());
        self.observer
            .observe_labeled(&Signal::TaskCompleted, &labels);
        outcome
    }

    async fn prepare<T: ModelTask>(
        &self,
        scope: &TaskScope,
        call: TaskCall<'_>,
        task: &T,
        input: &T::Input,
        feedback: Option<(&T::Output, &str)>,
    ) -> Result<Prepared, TaskFailure> {
        let kind = task.kind();
        let profile = self.profile(scope, kind);
        if !profile.enabled {
            return Err(TaskFailure::Disabled);
        }
        let schema =
            self.schemas
                .compile(&task.schema(input))
                .map_err(|error| TaskFailure::Invalid {
                    code: "schema_compile".to_owned(),
                    reason: error.to_string(),
                })?;
        let name = task.prompt_name();
        let instructions = instructions::resolve(
            self.prompts.as_ref(),
            &self.selector,
            name,
            scope.locale(),
            task.instructions(),
        )
        .await;
        let repair = instructions::resolve(
            self.prompts.as_ref(),
            &self.selector,
            &format!("{name}.repair"),
            scope.locale(),
            BUILT_IN_REPAIR,
        )
        .await;
        let mut messages = task.render(input);
        if let Some((previous, note)) = feedback {
            messages.push(Message::assistant(
                serde_json::to_string(previous).unwrap_or_default(),
            ));
            messages.push(Message::user(format!("{}\n\n{note}", repair.text)));
        }
        Ok(Prepared {
            kind,
            parent: call.parent.map(|parent| parent.as_str().to_owned()),
            profile,
            instructions,
            repair,
            schema,
            messages,
        })
    }

    async fn run_prepared<T: ModelTask>(
        &self,
        scope: &TaskScope,
        call: TaskCall<'_>,
        task: &T,
        input: &T::Input,
        prepared: &Prepared,
    ) -> TaskOutcome<T::Output> {
        let tag = prepared.profile.model.as_deref();
        let votes = prepared.profile.votes.max(1);
        if votes == 1 {
            let id = call.id.as_str().to_owned();
            return match self
                .chain(scope, prepared, task, input, tag, call.depth, id, None)
                .await
            {
                Chain::Answered { output, depth, .. } => TaskOutcome::Accepted { output, depth },
                Chain::Unusable { failure, depth } => {
                    self.escalate(scope, call, task, input, prepared, failure, depth)
                        .await
                }
            };
        }

        let temperature = Some(prepared.profile.vote_temperature);
        let chains = join_all((1..=votes).map(|vote| {
            let id = call.id.call(format!("vote{vote}"));
            self.chain(
                scope,
                prepared,
                task,
                input,
                tag,
                call.depth,
                id,
                temperature,
            )
        }))
        .await;
        let depth = chains
            .iter()
            .map(|chain| match chain {
                Chain::Answered { depth, .. } | Chain::Unusable { depth, .. } => *depth,
            })
            .max()
            .unwrap_or(call.depth);
        let answered: Vec<(&T::Output, &str)> = chains
            .iter()
            .filter_map(|chain| match chain {
                Chain::Answered { output, record, .. } => Some((output, record.as_str())),
                Chain::Unusable { .. } => None,
            })
            .collect();
        if let Some(winner) = majority(task, &answered, usize::from(votes)) {
            for (index, (_, record)) in answered.iter().enumerate() {
                if !winner.contains(&index) {
                    scope.mark(record, &TaskVerdict::Outvoted);
                }
            }
            let output = answered[winner[0]].0.clone();
            return TaskOutcome::Accepted { output, depth };
        }

        self.observer.observe_labeled(
            &Signal::TaskVoteDisagreement,
            &scope.labels().with_purpose(prepared.kind.as_str()),
        );
        match prepared.profile.on_disagreement {
            Disagreement::Reread if !answered.is_empty() => {
                let shown: Vec<String> = answered
                    .iter()
                    .map(|(output, _)| serde_json::to_string(output).unwrap_or_default())
                    .collect();
                let mut again = prepared.clone();
                again.messages.push(Message::user(format!(
                    "Readings of this that disagreed:\n{}\n\nRead it again and give the answer \
                     the message supports.",
                    shown.join("\n")
                )));
                let id = call.id.call("reread");
                match self
                    .chain(scope, &again, task, input, tag, depth, id, None)
                    .await
                {
                    Chain::Answered { output, depth, .. } => {
                        TaskOutcome::Accepted { output, depth }
                    }
                    Chain::Unusable { failure, depth } => TaskOutcome::Failed { failure, depth },
                }
            }
            Disagreement::Escalate => {
                self.escalate(
                    scope,
                    call,
                    task,
                    input,
                    prepared,
                    TaskFailure::Disagreement,
                    depth,
                )
                .await
            }
            Disagreement::Clarify => TaskOutcome::Disagreed {
                answers: answered
                    .into_iter()
                    .map(|(output, _)| output.clone())
                    .collect(),
                depth,
            },
            _ => TaskOutcome::Failed {
                failure: TaskFailure::Disagreement,
                depth,
            },
        }
    }

    #[allow(clippy::too_many_arguments)]
    async fn escalate<T: ModelTask>(
        &self,
        scope: &TaskScope,
        call: TaskCall<'_>,
        task: &T,
        input: &T::Input,
        prepared: &Prepared,
        failure: TaskFailure,
        depth: u8,
    ) -> TaskOutcome<T::Output> {
        let escalates = matches!(
            failure,
            TaskFailure::Invalid { .. }
                | TaskFailure::Disagreement
                | TaskFailure::Provider(_)
                | TaskFailure::Routing(_)
        );
        let Some(tag) = prepared
            .profile
            .escalate_to
            .as_deref()
            .filter(|_| escalates)
        else {
            return TaskOutcome::Failed { failure, depth };
        };
        self.observer.observe_labeled(
            &Signal::TaskEscalated,
            &scope
                .labels()
                .with_purpose(prepared.kind.as_str())
                .with_error_code(failure.code()),
        );
        let id = call.id.call("escalation");
        match self
            .chain(
                scope,
                prepared,
                task,
                input,
                Some(tag),
                depth.saturating_add(1),
                id,
                None,
            )
            .await
        {
            Chain::Answered { output, depth, .. } => TaskOutcome::Accepted { output, depth },
            Chain::Unusable { failure, depth } => TaskOutcome::Failed { failure, depth },
        }
    }

    /// One call and its repair rounds, starting at `depth`.
    #[allow(clippy::too_many_arguments)]
    async fn chain<T: ModelTask>(
        &self,
        scope: &TaskScope,
        prepared: &Prepared,
        task: &T,
        input: &T::Input,
        tag: Option<&str>,
        depth: u8,
        id: String,
        temperature: Option<f32>,
    ) -> Chain<T::Output> {
        let mut messages = prepared.messages.clone();
        let mut depth = depth;
        let mut failure = TaskFailure::Invalid {
            code: "no_answer".to_owned(),
            reason: String::new(),
        };
        let rounds = prepared.profile.repairs.saturating_add(1);
        for round in 0..rounds {
            let record_id = if round == 0 {
                id.clone()
            } else {
                format!("{id}#repair{round}")
            };
            if let Err(bound) = scope.budget.reserve(depth) {
                self.observer.observe_labeled(
                    &Signal::BudgetExhausted,
                    &scope.labels().with_error_code(bound.as_str()),
                );
                let mut record = self.record(prepared, &record_id, depth, None);
                record.verdict = TaskVerdict::Failed {
                    code: format!("budget_{}", bound.as_str()),
                };
                scope.push(record);
                return Chain::Unusable {
                    failure: TaskFailure::Budget(bound),
                    depth,
                };
            }
            let mut retries = prepared.profile.retries;
            let mut call_id = record_id.clone();
            let (request, answer) = loop {
                let request = self.request(scope, prepared, &messages, temperature, &call_id);
                let answer = self.send(scope, prepared, tag, &request).await;
                match &answer {
                    Err(TaskFailure::Provider(kind)) if retries > 0 && retried_in_place(kind) => {
                        let mut record = self.record(prepared, &call_id, depth, Some(&request));
                        record.verdict = TaskVerdict::Failed {
                            code: format!("provider_{kind}"),
                        };
                        scope.push(record);
                        if let Err(bound) = scope.budget.reserve(depth) {
                            return Chain::Unusable {
                                failure: TaskFailure::Budget(bound),
                                depth,
                            };
                        }
                        retries -= 1;
                        call_id =
                            format!("{record_id}#retry{}", prepared.profile.retries - retries);
                    }
                    _ => break (request, answer),
                }
            };
            let mut record = self.record(prepared, &call_id, depth, Some(&request));
            let response = match answer {
                Ok((response, served_by, latency)) => {
                    record.provider_key = Some(served_by.provider.clone());
                    record.model_key = Some(served_by.model.clone());
                    record.input_tokens = Some(response.usage.input);
                    record.output_tokens = Some(response.usage.output);
                    record.latency_ms = u64::try_from(latency.as_millis()).ok();
                    response
                }
                Err(failed) => {
                    record.verdict = TaskVerdict::Failed {
                        code: failed.code(),
                    };
                    scope.push(record);
                    return Chain::Unusable {
                        failure: failed,
                        depth,
                    };
                }
            };
            let raw = response.text();
            if self.records.keep_raw_output {
                record.raw_output = Some(raw.clone());
            }
            match judge(task, input, &prepared.schema, &response) {
                Ok(output) => {
                    record.parsed = serde_json::to_value(&output).ok();
                    record.verdict = TaskVerdict::Accepted;
                    scope.push(record);
                    return Chain::Answered {
                        output,
                        depth,
                        record: record_id,
                    };
                }
                Err((code, reason)) => {
                    record.verdict = TaskVerdict::Rejected {
                        code: code.clone(),
                        reason: reason.clone(),
                    };
                    scope.push(record);
                    if round + 1 < rounds {
                        self.observer.observe_labeled(
                            &Signal::TaskRepaired,
                            &scope
                                .labels()
                                .with_purpose(prepared.kind.as_str())
                                .with_error_code(code.clone()),
                        );
                        messages.push(Message::assistant(raw));
                        messages.push(Message::user(format!(
                            "{}\n\n{reason}",
                            prepared.repair.text
                        )));
                        depth = depth.saturating_add(1);
                    }
                    failure = TaskFailure::Invalid { code, reason };
                }
            }
        }
        Chain::Unusable { failure, depth }
    }

    fn request(
        &self,
        scope: &TaskScope,
        prepared: &Prepared,
        messages: &[Message],
        temperature: Option<f32>,
        task_id: &str,
    ) -> ModelRequest {
        let profile = &prepared.profile;
        let timeout = scope
            .budget
            .call_timeout(profile.timeout_secs.map(Duration::from_secs));
        let mut request = ModelRequest::new(prepared.kind)
            .with_system(prepared.instructions.text.clone())
            .with_output(OutputSpec::json(
                prepared.kind.as_str(),
                prepared.schema.schema().clone(),
            ))
            .with_timeout(timeout)
            .with_cache_hint(CacheHint::System);
        for message in messages {
            request = request.with_message(message.clone());
        }
        if let Some(temperature) = temperature.or(profile.temperature) {
            request = request.with_temperature(temperature);
        }
        if let Some(tokens) = profile.max_output_tokens {
            request = request.with_max_output_tokens(tokens);
        }
        if let Some(effort) = profile.reasoning_effort {
            request = request.with_reasoning_effort(effort);
        }
        // A task id is a label by construction; one that is not is left off, not sent.
        let _ = request.metadata.insert(TASK_LABEL, task_id);
        if let Some(turn) = &scope.turn {
            let _ = request.metadata.insert(TURN_LABEL, turn.clone());
        }
        request
    }

    async fn send(
        &self,
        scope: &TaskScope,
        prepared: &Prepared,
        tag: Option<&str>,
        request: &ModelRequest,
    ) -> Result<(ModelResponse, ModelRef, Duration), TaskFailure> {
        let routing = match tag {
            Some(tag) => self.routing.clone().with_required_tag(tag),
            None => self.routing.clone(),
        };
        let candidates = self
            .router
            .select(prepared.kind, &request.requirements(), &routing)
            .map_err(|error| {
                crate::signals::observe_routing_error(self.observer.as_ref(), &error);
                TaskFailure::Routing(error.to_string())
            })?;
        let stage = if prepared.kind.is_critical() {
            FallbackStage::PreCommit
        } else {
            FallbackStage::PostCommitNarration
        };
        let _permit = scope.budget.permit().await;
        let started = Instant::now();
        let outcome = execute_with_fallback(&candidates, request, stage, &self.fallback)
            .await
            .map_err(|failure| {
                crate::signals::observe_attempts(self.observer.as_ref(), &failure.attempts);
                // The detail is sanitized and redacted by the adapter: safe to log.
                tracing::warn!(
                    target: "turnframe.tasks",
                    task = request.metadata.get(TASK_LABEL).unwrap_or_default(),
                    kind = failure.error.kind().as_str(),
                    detail = failure.error.detail().map_or("", |detail| detail.as_str()),
                    "a task call failed at the provider"
                );
                TaskFailure::Provider(failure.error.kind().as_str().to_owned())
            })?;
        let latency = started.elapsed();
        crate::signals::observe_attempts(self.observer.as_ref(), &outcome.attempts);
        scope.budget.record_tokens(outcome.response.usage.input);
        let served_by = outcome.served_by();
        self.observer.observe_duration(
            &Signal::TaskLatency,
            latency,
            &scope
                .labels()
                .with_purpose(prepared.kind.as_str())
                .with_provider(served_by.provider.clone())
                .with_model(served_by.model.clone()),
        );
        Ok((outcome.response, served_by, latency))
    }

    fn record(
        &self,
        prepared: &Prepared,
        task_id: &str,
        depth: u8,
        request: Option<&ModelRequest>,
    ) -> TaskRecord {
        let mut record = TaskRecord::new(task_id, prepared.kind.as_str(), TaskVerdict::Accepted);
        record.parent.clone_from(&prepared.parent);
        record.depth = depth;
        record.prompt_ref = Some(prepared.instructions.reference.clone());
        if let Some(request) = request {
            record.params = params_of(request);
            record.input_digest = Some(digest_of(request));
            if self.records.keep_prompts {
                record.rendered = serde_json::to_value(request).ok();
            }
        }
        record
    }
}

/// Parses and checks one answer, naming what failed in words a repair can quote.
fn judge<T: ModelTask>(
    task: &T,
    input: &T::Input,
    schema: &CompiledSchema,
    response: &ModelResponse,
) -> Result<T::Output, (String, String)> {
    let output: T::Output = parse_structured(response, schema)
        .map_err(|error| ("schema".to_owned(), error.to_string()))?;
    task.check(input, &output)
        .map_err(|error| (error.code.to_owned(), error.message))?;
    Ok(output)
}

/// The largest group of agreeing answers, when it is a strict majority of `votes`.
fn majority<T: ModelTask>(
    task: &T,
    answered: &[(&T::Output, &str)],
    votes: usize,
) -> Option<Vec<usize>> {
    let mut groups: Vec<Vec<usize>> = Vec::new();
    for (index, (output, _)) in answered.iter().enumerate() {
        match groups
            .iter_mut()
            .find(|group| task.agree(answered[group[0]].0, output))
        {
            Some(group) => group.push(index),
            None => groups.push(vec![index]),
        }
    }
    let largest = groups
        .into_iter()
        .max_by_key(|group| (group.len(), usize::MAX - group[0]))?;
    (largest.len() * 2 > votes).then_some(largest)
}

fn params_of(request: &ModelRequest) -> TaskParams {
    let mut params = TaskParams::default();
    params.temperature = request.temperature;
    params.max_output_tokens = request.max_output_tokens;
    params.reasoning_effort = request
        .reasoning_effort
        .map(|effort| effort.as_str().to_owned());
    params.seed = request.seed;
    params.timeout_ms = u64::try_from(request.timeout.as_millis()).unwrap_or(u64::MAX);
    params
}

/// Digest of what the model was shown: instructions, messages and schema, not the id.
fn digest_of(request: &ModelRequest) -> Digest {
    let shown = serde_json::json!({
        "system": request.system,
        "messages": request.messages,
        "output": request.output,
    });
    Digest::of_bytes(&serde_json::to_vec(&shown).unwrap_or_default())
}

/// Whether a provider failure of this kind may pass on the same call sent again. A
/// request the provider found invalid, or credentials it refused, fail the same way.
fn retried_in_place(kind: &str) -> bool {
    matches!(
        kind,
        "refusal" | "content_filter" | "malformed" | "transport" | "server" | "timeout" | "other"
    )
}