switchyard-libsy 0.4.0

Provider-neutral multi-LLM routing and orchestration for Switchyard
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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Signal-driven stage routing for coding agents.
//!
//! [`StageRouter`] is the assembled algorithm: a [`FallThrough`] pre-wired with
//! the tool-signal processor that reads each turn's tool results and the
//! [`StageClassifier`] that scores them onto the capable/efficient tiers. The cascade
//! is an internal detail — callers drive the algorithm, not its parts.
//!
//! Signals do not decide every turn. An under-threshold turn abstains and falls
//! through to the optional [`LlmTaskClassifier`] — the capability route's judge,
//! joined in unchanged — and then to the picker's default tier, or to an
//! override a decider ahead of stage set in its place.
//!
use std::sync::Arc;

use async_trait::async_trait;

use super::fall_through::FallThrough;
use super::llm_class::{LlmClassifierConfig, LlmTaskClassifier, TaskClassifierConfig};
use super::util::affinity::{ClassifyTrigger, has_new_user_turn};
use super::util::stage::{
    DecisionSource, HandoffNoteConfig, PickerMode, StageClassifier, Tier, fall_open_tier,
    record_routing_decision,
};
use super::util::tool_signals::{DEFAULT_RECENT_WINDOW, ToolSemantics, ToolSignalProcessor};
use crate::core::algorithm::{Algorithm, Driver};
use crate::core::classifier::{Classification, Classifier, Score};
use crate::core::processor::{Event, Processor};
use crate::core::state::State;
use crate::{LibsyError, Result};
use switchyard_protocol::{Category, Request, Response};

/// Telemetry name for a router this module assembles.
const STAGE_ROUTER: &str = "stage_router";

struct TierPromptProcessor {
    capable: Option<String>,
    efficient: Option<String>,
}

#[async_trait]
impl Processor for TierPromptProcessor {
    async fn process(&self, state: &mut State, event: Event<'_>) -> Result<()> {
        if let Event::Decision {
            driver,
            category,
            selected_model_id,
            ..
        } = event
        {
            state.target_prompts.clear();
            for (tier, prompt) in [
                (Category::Capable, &self.capable),
                (Category::Efficient, &self.efficient),
            ] {
                if let Some(prompt) = prompt {
                    for target in driver.models_for(&tier) {
                        if target != selected_model_id || category.as_ref() == Some(&tier) {
                            state.target_prompts.insert(target.clone(), prompt.clone());
                        }
                    }
                }
            }
        }
        Ok(())
    }
}

/// Attributes a turn to the classifier it wraps, when that classifier decides it.
///
/// The classifiers themselves are composition-agnostic and write no state; only
/// this router knows where each sits in its cascade.
struct SourceStamp {
    inner: Arc<dyn Classifier>,
    source: DecisionSource,
    judge: Option<switchyard_protocol::ModelId>,
    skip_user_turn: bool,
}

#[async_trait]
impl Classifier for SourceStamp {
    async fn score(
        &self,
        state: &mut State,
        request: &mut Request,
        driver: &Driver,
    ) -> Result<(Classification, Option<Response>)> {
        if self.skip_user_turn && has_new_user_turn(&request.llm_request.messages) {
            return Ok((Classification::Ambiguous(Vec::new()), None));
        }
        let scoped = self
            .judge
            .as_ref()
            .map(|judge| driver.for_judge(judge.clone()));
        let driver = scoped.as_ref().unwrap_or(driver);
        let (classification, served) = self.inner.score(state, request, driver).await?;
        // An abstaining classifier passes the turn on, so it is not its to claim.
        if let Some(winner) = classification.argmax(false)? {
            record_routing_decision(self.source, driver.provider_model(&winner.target));
            driver.set_evidence_if_empty(serde_json::json!({
                "source": self.source.as_str(),
            }));
        }
        Ok((classification, served))
    }
}

/// Closes the cascade at zero confidence: a fallback, not a judgement.
struct FallOpen {
    default_tier: Tier,
}

#[async_trait]
impl Classifier for FallOpen {
    async fn score(
        &self,
        state: &mut State,
        _request: &mut Request,
        driver: &Driver,
    ) -> Result<(Classification, Option<Response>)> {
        let tier = fall_open_tier(state).unwrap_or(self.default_tier);
        let category = match tier {
            Tier::Capable => Category::Capable,
            Tier::Efficient => Category::Efficient,
        };
        let target = driver.first_model_for(&category)?.clone();
        Ok((
            Classification::Scores(vec![Score {
                target,
                confidence: 0.0,
                category: Some(category),
            }]),
            None,
        ))
    }
}

/// The capability judge a stage router falls through to.
pub struct LlmFallback {
    /// Overrides the runtime judge for this stage configuration.
    pub target: Option<switchyard_protocol::ModelId>,
    /// Judge configuration. Its `recent_turn_window` counts trailing messages, while
    /// this router's `recent_window` counts tool results.
    /// Note: `classify_trigger` and `message_hash_fallback` have no effect here. The judge
    /// runs as a cascade classifier on every ambiguous turn, not as a standalone algorithm,
    /// so no verdict is retained.
    pub config: TaskClassifierConfig,
}

/// How a stage router scores turns, and what it hands the model it picks.
pub struct StageRouterConfig {
    /// Tier a turn falls open to when the scorer is not confident.
    pub mode: PickerMode,
    /// How much corroboration a decisive pick needs, in `[0.0, 1.0]`.
    pub confidence_threshold: f64,
    /// Threshold after an outer classifier selects capable-first. Defaults to `0.0`.
    pub capable_first_threshold: f64,
    /// Trailing tool results the signals are computed over. `None` uses
    /// [`DEFAULT_RECENT_WINDOW`].
    pub recent_window: Option<usize>,
    /// Exact tool-name semantics added to the built-in coding vocabulary.
    pub tool_semantics: ToolSemantics,
    /// Escalate on context compaction. Enabled by default.
    pub compaction_override: bool,
    /// Requests to keep on capable after an escalation. `0` disables the hold;
    /// `-1` pins after switching to this tier.
    pub capable_hold_turns: i64,
    /// Requests to keep on efficient after a switch. `None` defaults to `-1`
    /// under composite's capable-first policy, otherwise `0`. `Some(0)` disables
    /// the hold; `Some(-1)` pins until the session state is cleared.
    pub efficient_hold_turns: Option<i64>,
    /// Note handed to the model on a signal-driven escalation, and on a
    /// hand-back to the efficient tier when a de-escalation note is configured.
    pub handoff_notes: Option<HandoffNoteConfig>,
    /// System prompt handed to the runtime capable model.
    pub capable_system_prompt: Option<String>,
    /// System prompt handed to the runtime efficient model.
    pub efficient_system_prompt: Option<String>,
    /// Capability judge consulted on turns the signals leave undecided. It uses
    /// the runtime judge model and the standalone capability route's settings.
    pub llm_fallback: Option<LlmFallback>,
}

impl StageRouterConfig {
    /// The signal-only configuration: no notes, no per-tier prompts, no judge.
    /// Set the optional fields to add them.
    pub fn new(mode: PickerMode, confidence_threshold: f64) -> Self {
        Self {
            mode,
            confidence_threshold,
            capable_first_threshold: 0.0,
            recent_window: None,
            tool_semantics: ToolSemantics::default(),
            compaction_override: true,
            capable_hold_turns: 2,
            efficient_hold_turns: None,
            handoff_notes: None,
            capable_system_prompt: None,
            efficient_system_prompt: None,
            llm_fallback: None,
        }
    }
}

/// Routes coding-agent turns between a capable and an efficient tier: tool signals
/// decide first, an optional capability judge takes the turns they cannot, and
/// the picker's default tier closes the cascade so a turn is never left unrouted.
pub struct StageRouter {
    route: FallThrough,
}

impl StageRouter {
    /// Routes between the runtime `capable` and `efficient` models. The judge,
    /// when configured, is called through the runtime `judge` model.
    ///
    /// Errors if either threshold in `config` is outside `[0.0, 1.0]`, or if the
    /// hold counts are outside `-1..=u32::MAX`. Also rejects a
    /// fallback judge configured to retain verdicts: it runs on every ambiguous
    /// turn, so `classify_trigger` and `message_hash_fallback` would do nothing.
    pub fn new(config: StageRouterConfig) -> Result<Self> {
        Ok(Self {
            route: build_stage_route(config, false)?,
        })
    }
}

#[async_trait]
impl Algorithm for StageRouter {
    fn name(&self) -> &str {
        STAGE_ROUTER
    }

    async fn route(
        self: Arc<Self>,
        driver: Driver,
        request: Request,
    ) -> Result<crate::RoutingOutcome> {
        self.route.execute(driver, request).await
    }
}

/// Wires the cascade the wrapper drives. Exposed so a composition above can
/// stack a prelude onto it.
pub(crate) fn build_stage_route(
    config: StageRouterConfig,
    reset_on_user_turn: bool,
) -> Result<FallThrough> {
    if let Some(fallback) = &config.llm_fallback
        && (fallback.config.classify_trigger != ClassifyTrigger::EveryRequest
            || fallback.config.message_hash_fallback)
    {
        return Err(LibsyError::AlgorithmError {
            message: "classify_trigger and message_hash_fallback have no effect on the \
                          stage fallback judge, which runs on every ambiguous turn. Remove \
                          them, or use a composite route to judge once per user turn"
                .to_string(),
        });
    }
    for (name, threshold) in [
        ("confidence_threshold", config.confidence_threshold),
        ("capable_first_threshold", config.capable_first_threshold),
    ] {
        if !(0.0..=1.0).contains(&threshold) {
            return Err(LibsyError::AlgorithmError {
                message: format!("{name} must be between 0 and 1, got {threshold}"),
            });
        }
    }
    config.tool_semantics.validate()?;
    for (name, turns) in [
        ("capable_hold_turns", Some(config.capable_hold_turns)),
        ("efficient_hold_turns", config.efficient_hold_turns),
    ] {
        let Some(turns) = turns else { continue };
        if !(-1..=i64::from(u32::MAX)).contains(&turns) {
            return Err(LibsyError::AlgorithmError {
                message: format!(
                    "{name} must be -1 or between 0 and {}, got {turns}",
                    u32::MAX
                ),
            });
        }
    }
    let default_tier = config.mode.default_tier();
    let fall_open = FallOpen { default_tier };

    let mut classifier = StageClassifier::new(config.mode, config.confidence_threshold)
        .with_capable_first_threshold(config.capable_first_threshold)
        .with_compaction_override(config.compaction_override)
        .with_capable_hold_turns(config.capable_hold_turns);
    classifier.reset_on_user_turn = reset_on_user_turn;
    if let Some(turns) = config.efficient_hold_turns {
        classifier = classifier.with_efficient_hold_turns(turns);
    }
    if let Some(notes) = config.handoff_notes {
        classifier = classifier.with_handoff_notes(notes);
    }
    let signals = ToolSignalProcessor {
        recent_window: config.recent_window.unwrap_or(DEFAULT_RECENT_WINDOW),
        tool_semantics: config.tool_semantics,
    };
    let classifier = Arc::new(classifier.with_signals(signals));
    let mut router = FallThrough::new_with_state()
        .with_name(STAGE_ROUTER)
        .with_processor(classifier.clone())
        .with_classifier(classifier);
    if let Some(fallback) = config.llm_fallback {
        router = router.with_classifier(Arc::new(SourceStamp {
            inner: Arc::new(LlmTaskClassifier::new(LlmClassifierConfig::Capability {
                config: fallback.config,
            })?),
            source: DecisionSource::LlmClassifier,
            judge: fallback.target,
            skip_user_turn: reset_on_user_turn,
        }));
    }
    // Nothing behind this, so no turn is left unrouted.
    router = router.with_classifier(Arc::new(SourceStamp {
        inner: Arc::new(fall_open),
        source: DecisionSource::FallOpen,
        judge: None,
        skip_user_turn: false,
    }));
    if config.capable_system_prompt.is_some() || config.efficient_system_prompt.is_some() {
        router = router.with_processor(Arc::new(TierPromptProcessor {
            capable: config.capable_system_prompt,
            efficient: config.efficient_system_prompt,
        }));
    }
    Ok(router)
}

#[cfg(test)]
mod tests {
    use crate::{CapabilityJudgeConfig, LlmCapabilityConfig};
    use std::collections::HashMap;
    use std::sync::Arc;

    use async_trait::async_trait;
    use parking_lot::Mutex;

    use super::*;
    use crate::algorithms::util::stage::{clear_fall_open, set_fall_open};
    use crate::algorithms::util::tier_fixtures::{JUDGE, Recorder, turn_request};
    use crate::core::testing::test_drive_with_models;
    use switchyard_protocol::{Category, ModelId};

    /// A classifier that always picks `target`, standing in for a cascade member.
    struct Fixed(&'static str);

    #[async_trait]
    impl Classifier for Fixed {
        async fn score(
            &self,
            _state: &mut State,
            _request: &mut Request,
            _driver: &Driver,
        ) -> Result<(Classification, Option<Response>)> {
            Ok((
                Classification::Scores(vec![Score {
                    target: ModelId::from(self.0),
                    confidence: 1.0,
                    category: None,
                }]),
                None,
            ))
        }
    }

    /// A classifier that never decides.
    struct Abstains;

    #[async_trait]
    impl Classifier for Abstains {
        async fn score(
            &self,
            _state: &mut State,
            _request: &mut Request,
            _driver: &Driver,
        ) -> Result<(Classification, Option<Response>)> {
            Ok((Classification::Ambiguous(vec![]), None))
        }
    }

    async fn stamped(inner: Arc<dyn Classifier>) -> Result<Option<String>> {
        let stamp = SourceStamp {
            inner,
            source: DecisionSource::LlmClassifier,
            judge: None,
            skip_user_turn: false,
        };
        let (driver, mut rx) = Driver::new("test", Arc::new(crate::RuntimeModels::default()));
        stamp
            .score(&mut State::default(), &mut Request::default(), &driver)
            .await?;
        driver
            .finish(Ok(crate::RoutingOutcome::route_to(
                "strong".into(),
                Vec::new(),
                Request::default(),
            )))
            .await?;
        let Some(Ok(crate::Step::Done(outcome))) = rx.recv().await else {
            panic!("expected Done step");
        };
        Ok(outcome
            .metadata
            .and_then(|metadata| metadata.evidence)
            .and_then(|evidence| evidence.get("source")?.as_str().map(str::to_string)))
    }

    #[tokio::test]
    async fn a_deciding_classifier_is_credited_with_the_turn() -> Result<()> {
        assert_eq!(
            stamped(Arc::new(Fixed("strong"))).await?.as_deref(),
            Some("llm-classifier")
        );
        Ok(())
    }

    #[tokio::test]
    async fn an_abstaining_classifier_claims_nothing() -> Result<()> {
        // It passed the turn on, so the next classifier is the one that decided.
        assert_eq!(stamped(Arc::new(Abstains)).await?, None);
        Ok(())
    }

    fn config() -> StageRouterConfig {
        StageRouterConfig::new(PickerMode::EfficientFirst, 0.5)
    }

    fn runtime_models() -> HashMap<Category, Vec<ModelId>> {
        runtime_models_for("strong", "weak")
    }

    fn runtime_models_for(capable: &str, efficient: &str) -> HashMap<Category, Vec<ModelId>> {
        [
            (Category::Judge, vec![ModelId::from(JUDGE)]),
            (Category::Efficient, vec![ModelId::from(efficient)]),
            (Category::Capable, vec![ModelId::from(capable)]),
            (
                Category::Any,
                vec![ModelId::from(capable), ModelId::from(efficient)],
            ),
        ]
        .into()
    }

    #[test]
    fn rejects_an_out_of_range_confidence_threshold() {
        let mut config = config();
        config.confidence_threshold = 1.5;
        assert!(matches!(
            StageRouter::new(config),
            Err(LibsyError::AlgorithmError { .. })
        ));
    }

    #[test]
    fn rejects_an_out_of_range_judge_threshold() {
        let mut config = config();
        config.llm_fallback = Some(LlmFallback {
            target: None,
            config: TaskClassifierConfig {
                judge: CapabilityJudgeConfig::Llm(LlmCapabilityConfig {
                    base_threshold: -0.1,
                    ..LlmCapabilityConfig::default()
                }),
                ..Default::default()
            },
        });
        assert!(matches!(
            StageRouter::new(config),
            Err(LibsyError::AlgorithmError { .. })
        ));
    }

    #[test]
    fn rejects_retaining_triggers_on_the_fallback_judge() {
        for (trigger, message_hash_fallback) in [
            (ClassifyTrigger::UserTurn, false),
            (ClassifyTrigger::NewSession, false),
            (ClassifyTrigger::UserTurn, true),
        ] {
            let mut config = config();
            config.llm_fallback = Some(LlmFallback {
                target: None,
                config: TaskClassifierConfig {
                    classify_trigger: trigger,
                    message_hash_fallback,
                    ..Default::default()
                },
            });
            assert!(
                matches!(
                    StageRouter::new(config),
                    Err(LibsyError::AlgorithmError { .. })
                ),
                "{trigger:?} with message_hash_fallback = {message_hash_fallback}"
            );
        }
    }

    #[test]
    fn builds() -> Result<()> {
        let router = StageRouter::new(config())?;
        assert_eq!(router.name(), STAGE_ROUTER);
        Ok(())
    }

    #[test]
    fn hold_turns_accept_pins_and_reject_invalid_counts() {
        for capable in [true, false] {
            for turns in [-2, -1, 0, 1, i64::from(u32::MAX), i64::from(u32::MAX) + 1] {
                let mut config = config();
                if capable {
                    config.capable_hold_turns = turns;
                } else {
                    config.efficient_hold_turns = Some(turns);
                }
                assert_eq!(
                    StageRouter::new(config).is_ok(),
                    (-1..=i64::from(u32::MAX)).contains(&turns)
                );
            }
        }
    }

    /// A clean write selects efficient without ending finite capable holds early.
    fn production_request() -> Request {
        let mut request = turn_request(false);
        if let switchyard_protocol::ContentBlock::ToolCall(call) =
            &mut request.llm_request.messages[1].content[0]
        {
            call.name = "Write".to_string();
        }
        request
    }

    /// Disabling compaction escalation preserves normal signals in both pickers.
    #[tokio::test]
    async fn compaction_override_can_be_disabled() -> Result<()> {
        for mode in [PickerMode::EfficientFirst, PickerMode::CapableFirst] {
            for enabled in [true, false] {
                let mut config = StageRouterConfig::new(mode, 0.5);
                assert!(config.compaction_override);
                config.compaction_override = enabled;
                config.capable_hold_turns = 0;
                let router = recording_router(config)?;
                let recorder = Arc::new(Recorder::default());
                for mut request in [production_request(), turn_request(true)] {
                    request.llm_request.has_compaction = true;
                    test_drive_with_models(
                        router.clone(),
                        request,
                        runtime_models(),
                        recorder.serve(),
                    )
                    .await?;
                }
                let actual: Vec<_> = recorder
                    .routed()
                    .into_iter()
                    .map(|call| call.target)
                    .collect();
                assert_eq!(actual, [if enabled { "strong" } else { "weak" }, "strong"]);
            }
        }
        Ok(())
    }

    #[tokio::test]
    async fn standalone_pickers_default_to_no_efficient_hold() -> Result<()> {
        for mode in [PickerMode::EfficientFirst, PickerMode::CapableFirst] {
            let mut config = StageRouterConfig::new(mode, 0.5);
            config.capable_hold_turns = 0;
            let router = recording_router(config)?;
            let recorder = Arc::new(Recorder::default());
            for request in [turn_request(true), production_request(), turn_request(true)] {
                test_drive_with_models(router.clone(), request, runtime_models(), recorder.serve())
                    .await?;
            }
            let actual: Vec<_> = recorder
                .routed()
                .into_iter()
                .map(|call| call.target)
                .collect();
            assert_eq!(actual, ["strong", "weak", "strong"]);
        }
        Ok(())
    }

    /// Both pickers honor both holds; the switch request is not one of the held turns.
    #[tokio::test]
    async fn holds_expire_or_pin_after_switching_in_both_pickers() -> Result<()> {
        for mode in [PickerMode::EfficientFirst, PickerMode::CapableFirst] {
            for capable in [true, false] {
                for turns in [0, 1, 2, -1] {
                    let mut config = StageRouterConfig::new(mode, 0.5);
                    config.capable_hold_turns = if capable { turns } else { 0 };
                    config.efficient_hold_turns = Some(if capable { 0 } else { turns });
                    let router = recording_router(config)?;
                    let recorder = Arc::new(Recorder::default());
                    let (switch, opposing, held_target, other_target) = if capable {
                        (turn_request(true), production_request(), "strong", "weak")
                    } else {
                        (production_request(), turn_request(true), "weak", "strong")
                    };
                    let mut requests = vec![opposing.clone(), switch];
                    requests.extend(std::iter::repeat_n(opposing, 4));
                    for request in requests {
                        test_drive_with_models(
                            router.clone(),
                            request,
                            runtime_models(),
                            recorder.serve(),
                        )
                        .await?;
                    }
                    let actual: Vec<_> = recorder
                        .routed()
                        .into_iter()
                        .map(|call| call.target)
                        .collect();
                    let mut expected = vec![other_target, held_target];
                    expected.extend((0..4).map(|i| {
                        if turns == -1 || i < turns {
                            held_target
                        } else {
                            other_target
                        }
                    }));
                    assert_eq!(
                        actual, expected,
                        "{mode:?}, capable={capable}, turns={turns}"
                    );
                }
            }
        }
        Ok(())
    }

    /// Initial selection does not pin. Once switched, pins survive missing signals,
    /// passing tests, and critical errors, and remain scoped to one session/agent.
    #[tokio::test]
    async fn pins_survive_signals_and_remain_isolated() -> Result<()> {
        for mode in [PickerMode::EfficientFirst, PickerMode::CapableFirst] {
            for capable in [true, false] {
                let mut config = StageRouterConfig::new(mode, 0.5);
                config.capable_hold_turns = -1;
                config.efficient_hold_turns = Some(-1);
                let router = recording_router(config)?;
                let recorder = Arc::new(Recorder::default());
                let (initial, switch, held, other) = if capable {
                    (production_request(), turn_request(true), "strong", "weak")
                } else {
                    (turn_request(true), production_request(), "weak", "strong")
                };
                let mut no_signals = turn_request(false);
                no_signals.llm_request.messages.truncate(1);
                let mut passing = turn_request(false);
                if let switchyard_protocol::ContentBlock::ToolResult(result) =
                    &mut passing.llm_request.messages[2].content[0]
                {
                    result.content = vec![switchyard_protocol::ContentBlock::Text {
                        text: "test result: ok. 1 passed; 0 failed".into(),
                    }];
                }
                let mut child = initial.clone();
                child.metadata.as_mut().unwrap().agent_id = Some("child".into());
                let mut other_session = initial.clone();
                other_session.metadata.as_mut().unwrap().session_id = Some("other".into());
                let mut final_request = initial.clone();
                final_request.metadata.as_mut().unwrap().session_final = Some(true);
                let requests = [
                    initial.clone(),
                    switch,
                    no_signals,
                    passing,
                    initial.clone(),
                    child,
                    other_session,
                    final_request,
                    initial,
                ];
                for request in requests {
                    test_drive_with_models(
                        router.clone(),
                        request,
                        runtime_models(),
                        recorder.serve(),
                    )
                    .await?;
                }
                let actual: Vec<_> = recorder
                    .routed()
                    .into_iter()
                    .map(|call| call.target)
                    .collect();
                assert_eq!(
                    actual,
                    [other, held, held, held, held, other, other, held, other]
                );
            }
        }
        Ok(())
    }

    /// Judge-driven switches must arm the same pin as signal-driven switches.
    #[tokio::test]
    async fn judge_switches_arm_pins_and_held_turns_skip_the_judge() -> Result<()> {
        for capable in [true, false] {
            let recorder = Arc::new(Recorder::default());
            let mut config = config_with_judge(&recorder, if capable { 1.0 } else { 0.0 });
            config.capable_hold_turns = -1;
            config.efficient_hold_turns = Some(-1);
            let router = recording_router(config)?;
            for p_solve in [
                if capable { 1.0 } else { 0.0 },
                if capable { 0.0 } else { 1.0 },
                if capable { 1.0 } else { 0.0 },
            ] {
                *recorder.judge_p_solve.lock() = p_solve;
                test_drive_with_models(
                    router.clone(),
                    turn_request(false),
                    runtime_models(),
                    recorder.serve(),
                )
                .await?;
            }
            let actual: Vec<_> = recorder
                .routed()
                .into_iter()
                .map(|call| call.target)
                .collect();
            assert_eq!(
                actual,
                if capable {
                    ["weak", "strong", "strong"]
                } else {
                    ["strong", "weak", "weak"]
                }
            );
            assert_eq!(recorder.judge_calls(), 2);
        }
        Ok(())
    }

    // ── routing integration tests ────────────────────────────────────────────

    const ESCALATION: &str = "the previous model was stalling; pick up the diagnosis";
    fn recording_router(config: StageRouterConfig) -> Result<Arc<StageRouter>> {
        Ok(Arc::new(StageRouter::new(config)?))
    }

    fn config_with_notes() -> StageRouterConfig {
        let mut c = config();
        c.handoff_notes = Some(HandoffNoteConfig::new(ESCALATION, None, true));
        c
    }

    fn config_with_judge(recorder: &Arc<Recorder>, p_solve: f64) -> StageRouterConfig {
        *recorder.judge_p_solve.lock() = p_solve;
        let mut c = config();
        c.llm_fallback = Some(LlmFallback {
            target: None,
            config: TaskClassifierConfig {
                judge: CapabilityJudgeConfig::Llm(LlmCapabilityConfig {
                    base_threshold: 0.5,
                    ..LlmCapabilityConfig::default()
                }),
                recent_turn_window: Some(3),
                ..Default::default()
            },
        });
        c
    }

    /// Stands in for a decider ahead of stage: sets the override once, clears it
    /// once, and leaves the turns between alone.
    #[derive(Default)]
    struct TierDecider {
        requests: Mutex<u32>,
    }

    #[async_trait]
    impl Processor for TierDecider {
        async fn process(&self, state: &mut State, event: Event<'_>) -> Result<()> {
            if matches!(event, Event::Request { .. }) {
                let mut requests = self.requests.lock();
                *requests += 1;
                match *requests {
                    1 => set_fall_open(state, Tier::Efficient),
                    4 => clear_fall_open(state),
                    _ => {}
                }
            }
            Ok(())
        }
    }

    #[tokio::test]
    async fn an_override_replaces_the_picker_default_and_leaves_the_signals_alone() -> Result<()> {
        let recorder = Arc::new(Recorder::default());
        // The picker would fall open to "strong"; the override says "weak".
        let config = StageRouterConfig::new(PickerMode::CapableFirst, 0.5);
        let route: Arc<dyn Algorithm> = Arc::new(
            build_stage_route(config, false)?.with_processor(Arc::new(TierDecider::default())),
        );

        let models = runtime_models();
        for is_turn_failed in [false, false, true, false] {
            test_drive_with_models(
                route.clone(),
                turn_request(is_turn_failed),
                models.clone(),
                recorder.serve(),
            )
            .await?;
        }

        let routed = recorder.routed();
        assert_eq!(
            routed[0].target, "weak",
            "an undecided turn takes the override"
        );
        assert_eq!(
            routed[1].target, "weak",
            "which outlives the turn that set it"
        );
        assert_eq!(
            routed[2].target, "strong",
            "a critical failure still reaches the signals"
        );
        assert_eq!(
            routed[3].target, "strong",
            "clearing restores the picker default"
        );
        Ok(())
    }

    #[tokio::test]
    async fn a_signal_driven_escalation_hands_the_note_to_the_model() -> Result<()> {
        let recorder = Arc::new(Recorder::default());
        let router = recording_router(config_with_notes())?;

        test_drive_with_models(
            router.clone(),
            turn_request(false),
            runtime_models(),
            recorder.serve(),
        )
        .await?;
        test_drive_with_models(
            router.clone(),
            turn_request(true),
            runtime_models_for("runtime-strong", "runtime-weak"),
            recorder.serve(),
        )
        .await?;

        let calls = recorder.routed();
        assert_eq!(calls[0].target, "weak");
        assert_eq!(calls[1].target, "runtime-strong");
        assert!(
            !calls[0].messages.iter().any(|t| t.contains(ESCALATION)),
            "steady-state turn should carry no note: {:?}",
            calls[0].messages
        );
        assert!(
            calls[1]
                .messages
                .last()
                .is_some_and(|t| t.ends_with(ESCALATION)),
            "escalating turn should carry the note last: {:?}",
            calls[1].messages
        );
        Ok(())
    }

    #[tokio::test]
    async fn the_judge_decides_a_turn_the_signals_leave_undecided() -> Result<()> {
        let recorder = Arc::new(Recorder::default());
        let router = recording_router(config_with_judge(&recorder, 0.1))?;

        let (selected_model, _) = test_drive_with_models(
            router.clone(),
            turn_request(false),
            runtime_models(),
            recorder.serve(),
        )
        .await?;

        let calls = recorder.calls.lock();
        assert!(
            calls.iter().any(|call| call.target == JUDGE),
            "the judge should be recorded as a routing side call"
        );
        assert!(
            calls.iter().any(|call| call.target == "strong"),
            "the selected target should be recorded as an answer call"
        );
        drop(calls);
        assert_eq!(selected_model, "strong");
        Ok(())
    }

    #[tokio::test]
    async fn a_decisive_signal_never_reaches_the_judge() -> Result<()> {
        let recorder = Arc::new(Recorder::default());
        let router = recording_router(config_with_judge(&recorder, 0.9))?;

        test_drive_with_models(
            router.clone(),
            turn_request(true),
            runtime_models(),
            recorder.serve(),
        )
        .await?;

        assert!(
            !recorder.calls.lock().iter().any(|c| c.target == JUDGE),
            "a resolved turn should not pay for a judge call"
        );
        assert_eq!(recorder.routed()[0].target, "strong");
        Ok(())
    }

    #[tokio::test]
    async fn the_judges_verdict_is_not_pinned_to_the_session() -> Result<()> {
        let recorder = Arc::new(Recorder::default());
        let router = recording_router(config_with_judge(&recorder, 0.1))?;

        test_drive_with_models(
            router.clone(),
            turn_request(false),
            runtime_models(),
            recorder.serve(),
        )
        .await?;
        *recorder.judge_p_solve.lock() = 0.9;
        test_drive_with_models(
            router.clone(),
            turn_request(false),
            runtime_models(),
            recorder.serve(),
        )
        .await?;

        let routed = recorder.routed();
        assert_eq!(routed[0].target, "strong");
        assert_eq!(routed[1].target, "weak");
        assert_eq!(
            recorder
                .calls
                .lock()
                .iter()
                .filter(|c| c.target == JUDGE)
                .count(),
            2,
            "each undecided turn is its own question"
        );
        Ok(())
    }

    #[tokio::test]
    async fn a_judge_that_cannot_tell_lands_on_the_picker_default() -> Result<()> {
        let recorder = Arc::new(Recorder::default());
        let router = recording_router(config_with_judge(&recorder, 42.0))?;

        test_drive_with_models(
            router.clone(),
            turn_request(false),
            runtime_models(),
            recorder.serve(),
        )
        .await?;

        assert_eq!(recorder.routed()[0].target, "weak");
        Ok(())
    }

    #[tokio::test]
    async fn the_judge_reads_the_window_it_was_configured_with() -> Result<()> {
        let recorder = Arc::new(Recorder::default());
        let router = recording_router(config_with_judge(&recorder, 0.9))?;

        test_drive_with_models(
            router.clone(),
            turn_request(false),
            runtime_models(),
            recorder.serve(),
        )
        .await?;

        let judged = recorder
            .calls
            .lock()
            .iter()
            .find(|c| c.target == JUDGE)
            .map(|c| c.messages.join("|"));
        let Some(judged) = judged else {
            panic!("the judge was never called");
        };
        assert!(
            judged.contains("fix the build"),
            "the judge should see the opening task: {judged}"
        );
        Ok(())
    }
}