openkind-engine 0.2.0

DecisionEngine dispatch, mock inference, and immutable execution-profile contracts.
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
//! Unit tests for engine dispatch, registration, and token estimation.

use std::collections::HashMap;
use std::sync::Arc;

use async_trait::async_trait;
use openkind_core::{
    Answer, ChoiceAnswer, ChoiceQuestion, NoulAnswer, NoulQuestion, Question, ScoreAnswer,
    ScoreQuestion, State, SystemRequest, SystemResponse, Usage, ValidationError,
};

use crate::dispatch::dispatch;
use crate::engine::DecisionEngine;
use crate::error::{EngineError, EngineResult};
use crate::mock::MockEngine;
use crate::registry::EngineRegistry;

fn make_test_request(model: &str) -> SystemRequest {
    let mut questions = HashMap::default();
    questions.insert(
        "urgent".into(),
        Question::Noul(NoulQuestion {
            instructions: serde_json::json!("Is this urgent?"),
            criteria: None,
        }),
    );
    SystemRequest {
        state: State::Text("Please fix now!".into()),
        model: model.into(),
        questions,
    }
}

#[test]
fn engine_registry_register_and_get() {
    let mut reg = EngineRegistry::new();
    assert!(reg.get("mock").is_none());
    reg.register("mock", Arc::new(MockEngine::new()));
    assert!(reg.get("mock").is_some());
}

#[test]
fn engine_registry_models_are_sorted() {
    let mut reg = EngineRegistry::new();
    reg.register("zeta", Arc::new(MockEngine::new()));
    reg.register("alpha", Arc::new(MockEngine::new()));
    reg.register("beta", Arc::new(MockEngine::new()));
    assert_eq!(reg.models(), vec!["alpha", "beta", "zeta"]);
}

#[test]
fn engine_registry_list_models_overrides_canonical_alias() {
    let mut reg = EngineRegistry::new();
    reg.register("alias-to-mock", Arc::new(MockEngine::new()));
    let models = reg.list_models();
    assert_eq!(models.len(), 1);
    assert_eq!(models[0].name, "alias-to-mock");
}

#[test]
fn engine_registry_debug_format() {
    let mut reg = EngineRegistry::new();
    reg.register("m1", Arc::new(MockEngine::new()));
    let rendered = format!("{reg:?}");
    assert!(rendered.contains("m1"));
}

#[tokio::test]
async fn dispatch_returns_unknown_model_error() {
    let reg = EngineRegistry::new();
    let req = make_test_request("nonexistent");
    let err = dispatch(req, &reg).await.unwrap_err();
    assert!(matches!(err, EngineError::UnknownModel(m) if m == "nonexistent"));
}

#[tokio::test]
async fn dispatch_returns_validation_error_on_invalid_request() {
    let mut reg = EngineRegistry::new();
    reg.register("mock", Arc::new(MockEngine::new()));
    let invalid_req = SystemRequest {
        state: State::Text("hello".into()),
        model: "mock".into(),
        questions: HashMap::default(), // empty questions is invalid
    };
    let err = dispatch(invalid_req, &reg).await.unwrap_err();
    assert!(matches!(
        err,
        EngineError::Invalid(ValidationError::NoQuestions)
    ));
}

#[tokio::test]
async fn dispatch_rejects_backend_answer_with_wrong_primitive() {
    struct WrongPrimitiveBackend;
    #[async_trait]
    impl DecisionEngine for WrongPrimitiveBackend {
        fn backend_id(&self) -> &str {
            "wrong-primitive"
        }

        async fn evaluate(&self, req: SystemRequest) -> EngineResult<SystemResponse> {
            let answers = req
                .questions
                .into_keys()
                .map(|id| (id, Answer::Noul(NoulAnswer { noul: 0.5 })))
                .collect();
            Ok(SystemResponse {
                model: "wrong-primitive".into(),
                answers,
                usage: Usage {
                    input_tokens: 1,
                    output_tokens: 1,
                },
            })
        }
    }

    let mut registry = EngineRegistry::new();
    registry.register("wrong-primitive", Arc::new(WrongPrimitiveBackend));
    let request = SystemRequest {
        state: State::Text("test".into()),
        model: "wrong-primitive".into(),
        questions: HashMap::from_iter([(
            "route".into(),
            Question::Choice(ChoiceQuestion {
                instructions: serde_json::json!("Pick"),
                criteria: HashMap::from_iter([("inspect".into(), None)]),
            }),
        )]),
    };
    let err = dispatch(request, &registry).await.unwrap_err();
    assert!(
        matches!(
            &err,
            EngineError::BackendValidation {
                source: ValidationError::AnswerTypeMismatch { .. },
                ..
            }
        ),
        "{err:?}"
    );
    assert!(err.to_string().contains("expected choice answer, got noul"));
}

#[tokio::test]
async fn dispatch_successful_and_populates_token_usage() {
    let mut reg = EngineRegistry::new();
    reg.register("mock", Arc::new(MockEngine::new()));
    let req = make_test_request("mock");
    let resp = dispatch(req, &reg).await.unwrap();
    assert_eq!(resp.model, "mock");
    assert!(resp.usage.input_tokens > 0);
    assert!(resp.usage.output_tokens > 0);
    assert_eq!(resp.answers.len(), 1);
}

#[tokio::test]
async fn dispatch_preserves_registered_alias_with_different_backend_id() {
    let mut registry = EngineRegistry::new();
    registry.register(
        "public-model",
        Arc::new(MockEngine::with_backend("internal")),
    );

    let response = dispatch(make_test_request("public-model"), &registry)
        .await
        .unwrap();

    assert_eq!(response.model, "public-model");
}

#[tokio::test]
async fn dispatch_preserves_engine_reported_tokens() {
    struct ExplicitTokensBackend;
    #[async_trait]
    impl DecisionEngine for ExplicitTokensBackend {
        fn backend_id(&self) -> &str {
            "explicit"
        }
        async fn evaluate(&self, req: SystemRequest) -> EngineResult<SystemResponse> {
            let mut answers = HashMap::default();
            for id in req.questions.keys() {
                answers.insert(id.clone(), Answer::Noul(NoulAnswer { noul: 0.5 }));
            }
            Ok(SystemResponse {
                model: "explicit".into(),
                answers,
                usage: Usage {
                    input_tokens: 999,
                    output_tokens: 888,
                },
            })
        }
    }

    let mut reg = EngineRegistry::new();
    reg.register("explicit", Arc::new(ExplicitTokensBackend));
    let req = make_test_request("explicit");
    let resp = dispatch(req, &reg).await.unwrap();
    assert_eq!(resp.usage.input_tokens, 999);
    assert_eq!(resp.usage.output_tokens, 888);
}

#[tokio::test]
async fn dispatch_estimates_output_tokens_per_answer_kind() {
    struct MultiAnswerBackend;
    #[async_trait]
    impl DecisionEngine for MultiAnswerBackend {
        fn backend_id(&self) -> &str {
            "multi"
        }
        async fn evaluate(&self, req: SystemRequest) -> EngineResult<SystemResponse> {
            let mut answers = HashMap::default();
            for (id, q) in &req.questions {
                match q {
                    Question::Noul(_) => {
                        answers.insert(id.clone(), Answer::Noul(NoulAnswer { noul: 0.5 }));
                    }
                    Question::Choice(c) => {
                        let pick = c.criteria.keys().next().cloned().unwrap();
                        let mut probs = HashMap::default();
                        for k in c.criteria.keys() {
                            probs.insert(k.clone(), 1.0 / c.criteria.len() as f64);
                        }
                        answers.insert(
                            id.clone(),
                            Answer::Choice(ChoiceAnswer {
                                choice: pick,
                                probabilities: probs,
                                confidence: 0.8,
                            }),
                        );
                    }
                    Question::Score(s) => {
                        let mut probs = HashMap::default();
                        let mut legend = HashMap::default();
                        for (i, name) in s.criteria.iter().enumerate() {
                            probs.insert(i.to_string(), 1.0 / s.criteria.len() as f64);
                            legend.insert(i.to_string(), name.clone());
                        }
                        answers.insert(
                            id.clone(),
                            Answer::Score(ScoreAnswer {
                                score: (s.criteria.len() - 1) as f64 / 2.0,
                                legend,
                                probabilities: probs,
                                confidence: 0.9,
                            }),
                        );
                    }
                }
            }
            Ok(SystemResponse {
                model: "multi".into(),
                answers,
                usage: Usage {
                    input_tokens: 0,
                    output_tokens: 0,
                },
            })
        }
    }

    let mut reg = EngineRegistry::new();
    reg.register("multi", Arc::new(MultiAnswerBackend));
    let mut questions = HashMap::default();
    questions.insert(
        "q1".into(),
        Question::Noul(NoulQuestion {
            instructions: serde_json::json!("Check"),
            criteria: None,
        }),
    );
    let mut choice_criteria = HashMap::default();
    choice_criteria.insert("a".into(), None);
    choice_criteria.insert("b".into(), None);
    questions.insert(
        "q2".into(),
        Question::Choice(ChoiceQuestion {
            instructions: serde_json::json!("Pick"),
            criteria: choice_criteria,
        }),
    );
    questions.insert(
        "q3".into(),
        Question::Score(ScoreQuestion {
            instructions: serde_json::json!("Rate"),
            criteria: vec!["Low".into(), "High".into()],
        }),
    );

    let req = SystemRequest {
        state: State::Text("Context".into()),
        model: "multi".into(),
        questions,
    };
    let resp = dispatch(req, &reg).await.unwrap();
    // 1 (Noul) + 1 (Choice) + 4 (Score) = 6 tokens estimated
    assert_eq!(resp.usage.output_tokens, 6);
}

#[tokio::test]
async fn dispatch_rejects_backend_that_skips_answers() {
    struct DroppingBackend;
    #[async_trait]
    impl DecisionEngine for DroppingBackend {
        fn backend_id(&self) -> &str {
            "drop"
        }
        async fn evaluate(&self, _req: SystemRequest) -> EngineResult<SystemResponse> {
            Ok(SystemResponse {
                model: "drop".into(),
                answers: HashMap::default(), // returns nothing!
                usage: Usage {
                    input_tokens: 1,
                    output_tokens: 1,
                },
            })
        }
    }

    let mut reg = EngineRegistry::new();
    reg.register("drop", Arc::new(DroppingBackend));
    let req = make_test_request("drop");
    let err = dispatch(req, &reg).await.unwrap_err();
    assert!(
        matches!(err, EngineError::BackendValidation { ref backend, .. } if backend == "drop"),
        "expected Backend error, got {err:?}"
    );
}

#[tokio::test]
async fn dispatch_rejects_backend_with_nan_probabilities() {
    struct NanBackend;
    #[async_trait]
    impl DecisionEngine for NanBackend {
        fn backend_id(&self) -> &str {
            "nan"
        }
        async fn evaluate(&self, req: SystemRequest) -> EngineResult<SystemResponse> {
            let mut answers = HashMap::default();
            for id in req.questions.keys() {
                answers.insert(id.clone(), Answer::Noul(NoulAnswer { noul: f64::NAN }));
            }
            Ok(SystemResponse {
                model: "nan".into(),
                answers,
                usage: Usage {
                    input_tokens: 1,
                    output_tokens: 1,
                },
            })
        }
    }

    let mut reg = EngineRegistry::new();
    reg.register("nan", Arc::new(NanBackend));
    let req = make_test_request("nan");
    let err = dispatch(req, &reg).await.unwrap_err();
    assert!(
        matches!(err, EngineError::BackendValidation { ref backend, .. } if backend == "nan"),
        "expected Backend error due to NaN in answer, got {err:?}"
    );
}

#[test]
fn default_model_metadata_names_backend() {
    struct BareEngine;
    #[async_trait]
    impl DecisionEngine for BareEngine {
        fn backend_id(&self) -> &str {
            "bare"
        }
        async fn evaluate(&self, _req: SystemRequest) -> EngineResult<SystemResponse> {
            unimplemented!("metadata-only test")
        }
    }
    let engine = BareEngine;
    let meta = engine.model_metadata();
    assert_eq!(meta.name, "bare");
    assert!(meta.description.contains("bare"));
}

#[test]
fn token_estimation_handles_text_object_and_array_states() {
    let engine = MockEngine::new();

    // 1. Text state
    let req_text = SystemRequest {
        state: State::Text("12345678".into()), // 8 chars -> 2 tokens
        model: "mock".into(),
        questions: {
            let mut q = HashMap::default();
            q.insert(
                "q1".into(),
                Question::Noul(NoulQuestion {
                    instructions: serde_json::json!("1234"), // 4 chars
                    criteria: None,
                }),
            );
            q
        },
    };
    // (8 + 6) chars = 14 chars -> div_ceil(4) = 4 tokens
    assert_eq!(engine.estimate_input_tokens(&req_text), 4);

    // 2. Object state
    let mut map = serde_json::Map::new();
    map.insert("key".into(), serde_json::json!("value")); // serialized JSON length
    let req_obj = SystemRequest {
        state: State::Object(map),
        model: "mock".into(),
        questions: HashMap::default(),
    };
    assert!(engine.estimate_input_tokens(&req_obj) > 0);

    // 3. Array state
    let req_arr = SystemRequest {
        state: State::Array(vec![serde_json::json!(1), serde_json::json!(2)]),
        model: "mock".into(),
        questions: HashMap::default(),
    };
    assert!(engine.estimate_input_tokens(&req_arr) > 0);
}

#[test]
fn token_estimation_counts_criteria_for_every_question_kind() {
    let engine = MockEngine::new();
    let description = "1234".repeat(1_000);
    let requests = [
        (
            Question::Noul(NoulQuestion {
                instructions: serde_json::json!("Check"),
                criteria: None,
            }),
            Question::Noul(NoulQuestion {
                instructions: serde_json::json!("Check"),
                criteria: Some(openkind_core::NoulCriteria {
                    r#true: description.clone(),
                    r#false: "No".into(),
                }),
            }),
        ),
        (
            Question::Choice(ChoiceQuestion {
                instructions: serde_json::json!("Check"),
                criteria: HashMap::from_iter([("a".into(), None)]),
            }),
            Question::Choice(ChoiceQuestion {
                instructions: serde_json::json!("Check"),
                criteria: HashMap::from_iter([("a".into(), Some(description.clone()))]),
            }),
        ),
        (
            Question::Score(ScoreQuestion {
                instructions: serde_json::json!("Check"),
                criteria: vec!["a".into()],
            }),
            Question::Score(ScoreQuestion {
                instructions: serde_json::json!("Check"),
                criteria: vec![description],
            }),
        ),
    ];

    for (short, long) in requests {
        let request = |question| SystemRequest {
            state: State::Text("state".into()),
            model: "mock".into(),
            questions: HashMap::from_iter([("q".into(), question)]),
        };
        let short_tokens = engine.estimate_input_tokens(&request(short));
        let long_tokens = engine.estimate_input_tokens(&request(long));
        assert!(
            long_tokens >= short_tokens + 999,
            "a 4,000-byte criterion must contribute to estimated usage"
        );
    }
}

#[tokio::test]
async fn registry_unload_preserves_accepted_handles_and_updates_clones() {
    let mut registry = EngineRegistry::new();
    let engine: Arc<dyn DecisionEngine> = Arc::new(MockEngine::new());
    let weak = Arc::downgrade(&engine);
    registry.register("model", engine);
    let grpc_registry = registry.clone();
    let accepted = grpc_registry.get("model").unwrap();
    assert!(!registry.register_if_absent("model".into(), Arc::new(MockEngine::new())));
    drop(registry.unregister("model"));
    assert!(grpc_registry.models().is_empty());
    assert!(matches!(
        dispatch(make_test_request("model"), &grpc_registry).await,
        Err(EngineError::UnknownModel(_))
    ));
    assert!(accepted.evaluate(make_test_request("model")).await.is_ok());
    assert!(weak.upgrade().is_some());
    drop(accepted);
    assert!(weak.upgrade().is_none());
    assert!(registry.register_if_absent("model".into(), Arc::new(MockEngine::new())));
    assert!(grpc_registry.get("model").is_some());
}

// ------------------------------------------------------------------
// Backend-fault matrix: every contract violation a backend can commit
// maps to EngineError::Backend (HTTP 500), never to a client 422.
// ------------------------------------------------------------------

struct FaultBackend {
    backend_id: &'static str,
    answer: Answer,
    extra_id: Option<&'static str>,
    error: Option<EngineError>,
}

impl FaultBackend {
    fn answer(backend_id: &'static str, answer: Answer) -> Self {
        Self {
            backend_id,
            answer,
            extra_id: None,
            error: None,
        }
    }
}

#[async_trait]
impl DecisionEngine for FaultBackend {
    fn backend_id(&self) -> &str {
        self.backend_id
    }

    async fn evaluate(&self, req: SystemRequest) -> EngineResult<SystemResponse> {
        if let Some(error) = &self.error {
            return Err(match error {
                EngineError::Overloaded {
                    backend,
                    retry_after_ms,
                } => EngineError::Overloaded {
                    backend: backend.clone(),
                    retry_after_ms: *retry_after_ms,
                },
                _ => unreachable!("the fault test only synthesizes Overloaded"),
            });
        }
        let mut answers = HashMap::default();
        for id in req.questions.keys() {
            answers.insert(id.clone(), self.answer.clone());
        }
        if let Some(extra) = self.extra_id {
            answers.insert(extra.to_string(), self.answer.clone());
        }
        Ok(SystemResponse {
            model: req.model,
            answers,
            usage: Usage {
                input_tokens: 1,
                output_tokens: 1,
            },
        })
    }
}

fn choice_question(keys: &[&str]) -> Question {
    Question::Choice(ChoiceQuestion {
        instructions: serde_json::json!("Pick"),
        criteria: keys.iter().map(|key| ((*key).to_string(), None)).collect(),
    })
}

fn score_question() -> Question {
    Question::Score(ScoreQuestion {
        instructions: serde_json::json!("Rate"),
        criteria: vec!["low".into(), "high".into()],
    })
}

fn fault_request(question: Question) -> SystemRequest {
    SystemRequest {
        state: State::Text("state".into()),
        model: "faulty".into(),
        questions: HashMap::from_iter([("q".into(), question)]),
    }
}

fn fault_registry(answer: Answer) -> EngineRegistry {
    let mut registry = EngineRegistry::new();
    registry.register(
        "faulty",
        Arc::new(FaultBackend::answer("faulty-backend", answer)),
    );
    registry
}

fn assert_backend_fault(error: EngineError, needle: &str) {
    assert!(
        matches!(error, EngineError::BackendValidation { .. }),
        "expected a backend fault, got: {error:?}"
    );
    assert!(error.to_string().contains(needle), "unexpected: {error}");
}

#[tokio::test]
async fn extra_answers_are_a_backend_fault() {
    let mut registry = EngineRegistry::new();
    let mut backend = FaultBackend::answer(
        "faulty-backend",
        Answer::Choice(ChoiceAnswer {
            choice: "a".into(),
            probabilities: HashMap::from_iter([("a".to_string(), 1.0)]),
            confidence: 0.5,
        }),
    );
    backend.extra_id = Some("ghost");
    registry.register("faulty", Arc::new(backend));

    let error = dispatch(fault_request(choice_question(&["a"])), &registry)
        .await
        .unwrap_err();
    assert_backend_fault(error, "unknown question `ghost`");
}

#[tokio::test]
async fn out_of_list_choices_are_a_backend_fault() {
    let registry = fault_registry(Answer::Choice(ChoiceAnswer {
        choice: "z".into(),
        probabilities: HashMap::from_iter([("a".to_string(), 1.0)]),
        confidence: 0.5,
    }));
    let error = dispatch(fault_request(choice_question(&["a", "b"])), &registry)
        .await
        .unwrap_err();
    assert_backend_fault(error, "not in criteria keys");
}

#[tokio::test]
async fn probability_key_mismatches_are_a_backend_fault() {
    let registry = fault_registry(Answer::Choice(ChoiceAnswer {
        choice: "a".into(),
        probabilities: HashMap::from_iter([("ghost".to_string(), 1.0)]),
        confidence: 0.5,
    }));
    let error = dispatch(fault_request(choice_question(&["a", "b"])), &registry)
        .await
        .unwrap_err();
    assert_backend_fault(error, "keys must match criteria");
}

#[tokio::test]
async fn out_of_range_probabilities_are_a_backend_fault() {
    let registry = fault_registry(Answer::Choice(ChoiceAnswer {
        choice: "a".into(),
        probabilities: HashMap::from_iter([("a".to_string(), 1.5), ("b".to_string(), -0.5)]),
        confidence: 0.5,
    }));
    let error = dispatch(fault_request(choice_question(&["a", "b"])), &registry)
        .await
        .unwrap_err();
    assert_backend_fault(error, "probability must be in 0..=1");
}

#[tokio::test]
async fn non_summing_probabilities_are_a_backend_fault() {
    let registry = fault_registry(Answer::Choice(ChoiceAnswer {
        choice: "a".into(),
        probabilities: HashMap::from_iter([("a".to_string(), 0.5), ("b".to_string(), 0.4)]),
        confidence: 0.5,
    }));
    let error = dispatch(fault_request(choice_question(&["a", "b"])), &registry)
        .await
        .unwrap_err();
    assert_backend_fault(error, "do not sum to 1");
}

#[tokio::test]
async fn out_of_range_confidence_is_a_backend_fault() {
    let registry = fault_registry(Answer::Choice(ChoiceAnswer {
        choice: "a".into(),
        probabilities: HashMap::from_iter([("a".to_string(), 1.0)]),
        confidence: 1.5,
    }));
    let error = dispatch(fault_request(choice_question(&["a"])), &registry)
        .await
        .unwrap_err();
    assert_backend_fault(error, "confidence");
}

#[tokio::test]
async fn score_legend_mismatch_is_a_backend_fault() {
    let registry = fault_registry(Answer::Score(ScoreAnswer {
        score: 0.0,
        probabilities: HashMap::from_iter([("0".to_string(), 1.0)]),
        legend: HashMap::from_iter([("1".to_string(), "high".to_string())]),
        confidence: 0.5,
    }));
    let error = dispatch(fault_request(score_question()), &registry)
        .await
        .unwrap_err();
    assert_backend_fault(error, "legend");
}

#[tokio::test]
async fn word_keyed_score_legends_violate_the_requested_rubric() {
    // Through dispatch the rubric always binds first: a self-consistent
    // word-keyed legend (`low`/`high`) still fails because the requested
    // two-level rubric pins legend slots `0` and `1`. The non-numeric-index
    // branch itself is only reachable for rubric-less responses (covered at
    // the core validator level).
    let registry = fault_registry(Answer::Score(ScoreAnswer {
        score: 0.0,
        probabilities: HashMap::from_iter([("low".to_string(), 1.0), ("high".to_string(), 0.0)]),
        legend: HashMap::from_iter([
            ("low".to_string(), "low".to_string()),
            ("high".to_string(), "high".to_string()),
        ]),
        confidence: 0.5,
    }));
    let error = dispatch(fault_request(score_question()), &registry)
        .await
        .unwrap_err();
    assert_backend_fault(error, "legend must match");
}

#[tokio::test]
async fn out_of_range_scores_are_a_backend_fault() {
    let registry = fault_registry(Answer::Score(ScoreAnswer {
        score: 5.0,
        probabilities: HashMap::from_iter([("0".to_string(), 1.0)]),
        legend: HashMap::from_iter([("0".to_string(), "low".to_string())]),
        confidence: 0.5,
    }));
    let error = dispatch(fault_request(score_question()), &registry)
        .await
        .unwrap_err();
    assert_backend_fault(error, "score");
}

#[tokio::test]
async fn score_expectation_mismatch_is_a_backend_fault() {
    // A full legend whose mass sits entirely at level 0 cannot produce the
    // reported score of 1.0.
    let registry = fault_registry(Answer::Score(ScoreAnswer {
        score: 1.0,
        probabilities: HashMap::from_iter([("0".to_string(), 1.0), ("1".to_string(), 0.0)]),
        legend: HashMap::from_iter([
            ("0".to_string(), "low".to_string()),
            ("1".to_string(), "high".to_string()),
        ]),
        confidence: 0.5,
    }));
    let error = dispatch(fault_request(score_question()), &registry)
        .await
        .unwrap_err();
    assert_backend_fault(error, "expected probability-weighted score 0");
}

#[tokio::test]
async fn backend_errors_propagate_unchanged_through_dispatch() {
    let mut registry = EngineRegistry::new();
    registry.register(
        "faulty",
        Arc::new(FaultBackend {
            backend_id: "faulty-backend",
            answer: Answer::Noul(NoulAnswer { noul: 0.5 }),
            extra_id: None,
            error: Some(EngineError::Overloaded {
                backend: "faulty-backend".into(),
                retry_after_ms: 25,
            }),
        }),
    );
    let error = dispatch(fault_request(choice_question(&["a"])), &registry)
        .await
        .unwrap_err();
    assert!(
        matches!(
            error,
            EngineError::Overloaded {
                retry_after_ms: 25,
                ..
            }
        ),
        "backend errors must not be rewrapped: {error:?}"
    );
}

#[tokio::test]
async fn unknown_model_wins_over_request_validation() {
    // An invalid request (no questions) naming an unregistered model fails
    // at the registry lookup, matching the documented precedence.
    let registry = EngineRegistry::new();
    let request = SystemRequest {
        state: State::Text("state".into()),
        model: "ghost".into(),
        questions: HashMap::default(),
    };
    let error = dispatch(request, &registry).await.unwrap_err();
    assert!(
        matches!(error, EngineError::UnknownModel(ref model) if model == "ghost"),
        "unexpected error: {error:?}"
    );
}