weirdnet-agent 0.5.8

Weirdnet agent runtime: a model-agnostic agent loop with streaming events and cancellation, tool sources and registry, tiered approval policy with per-site grants and standing allows, research budgets, traces with citation checks, and remote inference (infer/1 and infer/2, catalog, pool with failover and resume).
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
//! `/weirdnet-agent/catalog/1`: the models an inference node offers.
//!
//! ```text
//! asking node                     inference node
//!   Query { cert } ->             <- Catalog | Failed
//!   Subscribe { cert } ->         <- Catalog, then Catalog again on every change (until closed)
//! ```
//!
//! One entry per model with its state, an estimated speed and how many replicas serve it, but
//! no model-node addresses: apps only ever talk to inference nodes. Messages are CBOR.
//!
//! Each entry says which inputs beyond text it takes ([`CatalogModel::inputs`]: images, audio).
//! Entries from nodes before 0.5 have none, so they count as text-only. [`Catalog::route`] picks
//! a model for a request that carries images or audio, and the pool uses it so such a request
//! never lands on a text-only model.
//!
//! Entries may also say where the model came from ([`CatalogModel::origin`]) and its license
//! ([`CatalogModel::license`]); both are left off the wire when unset, so older peers read them.
//!
//! Entries may carry the model's media and request limits ([`CatalogModel::limits`], a
//! [`MediaLimits`]: images per request, token cost per image and its edge, audio length and token
//! rate, request bytes), each absent when the node doesn't know it. Unknown limits are left off
//! the wire, so peers before 0.5.7 read and send entries unchanged.
//!
//! A model may be a speech-to-text model ([`Purpose::Transcribe`], `transcribe` on the wire):
//! audio in, its transcript out, so a soundtrack can become text for models without audio. It is
//! never picked for chat; [`Catalog::route_transcribe`] picks one for a transcription request
//! (infer/2 `Transcribe`). Peers before 0.5.8 can't read that purpose, so [`serve`] leaves such
//! entries out for askers that don't say they skip what they can't read; [`query`] and
//! [`subscribe`] say so, and from 0.5.8 an entry this version can't read (a purpose from a newer
//! version, say) is skipped instead of failing the whole catalog.

use std::sync::Arc;

use serde::{Deserialize, Serialize};
use tokio::sync::watch;

use super::{Authorize, Caller, Channel, RemoteError, bytes};
use crate::limits::MediaLimits;
use crate::model::Modalities;

/// Stream protocol id.
pub const PROTOCOL: &str = "/weirdnet-agent/catalog/1";

/// What a model is for.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Purpose {
    #[default]
    Chat,
    Embedding,
    /// Speech to text: audio in, its transcript out (infer/2 `Transcribe`). Never picked as a
    /// chat model. Since 0.5.8; older peers don't see these entries (see the module docs).
    Transcribe,
}

impl Purpose {
    /// The name on the wire and in settings: `chat`, `embedding` or `transcribe` (the same names
    /// as [weirdnet-model](https://crates.io/crates/weirdnet-model)'s `hw::Purpose`).
    pub fn as_str(&self) -> &'static str {
        match self {
            Purpose::Chat => "chat",
            Purpose::Embedding => "embedding",
            Purpose::Transcribe => "transcribe",
        }
    }

    /// Parses [`as_str`](Self::as_str)'s names; `None` for anything else.
    pub fn parse(name: &str) -> Option<Self> {
        match name {
            "chat" => Some(Purpose::Chat),
            "embedding" => Some(Purpose::Embedding),
            "transcribe" => Some(Purpose::Transcribe),
            _ => None,
        }
    }

    /// Known to peers before 0.5.8.
    fn before_transcribe(self) -> bool {
        matches!(self, Purpose::Chat | Purpose::Embedding)
    }
}

impl std::fmt::Display for Purpose {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// A model's state across the pool.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ModelState {
    /// Loaded somewhere and ready.
    Warm,
    /// Not loaded, but a node can load it.
    #[default]
    Loadable,
    Loading,
    /// Loaded, but every replica is full.
    Busy,
}

/// One catalog entry.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CatalogModel {
    pub id: String,
    #[serde(default)]
    pub purpose: Purpose,
    #[serde(default)]
    pub state: ModelState,
    /// Model nodes that have it loaded.
    #[serde(default)]
    pub replicas: u32,
    /// Estimated decode speed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tokens_per_sec: Option<f32>,
    /// Context window in tokens: the one the node runs the model with (such as llama.cpp's
    /// `--ctx-size` per slot) when it knows it, not the model's trained maximum.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub context: Option<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub revision: Option<String>,
    /// The model that answers while this one loads, if the pool policy names one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub standin: Option<String>,
    /// Inputs it takes beyond text (images, audio). Empty: text only.
    #[serde(default, skip_serializing_if = "Modalities::is_text")]
    pub inputs: Modalities,
    /// For a LoRA adapter served on a base model: the base's id. Requests name the adapter's own
    /// id; apps may show it as a variant of the base. Not sent for models; catalogs from 0.5.1
    /// and earlier lack it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub base: Option<String>,
    /// Where the model came from: `hub`, `upload` or `train` (the origins of a
    /// [weirdnet-model](https://crates.io/crates/weirdnet-model) provenance record), or an app's
    /// own. Absent when the node does not say, and in catalogs from 0.5.3 and earlier.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub origin: Option<String>,
    /// The model's license as an SPDX id, or `unknown` when the uploader said so. Absent when the
    /// node does not say, and in catalogs from 0.5.3 and earlier.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub license: Option<String>,
    /// Media and request limits, each absent when the node doesn't know it (never defaulted).
    /// Left off the wire when none is known; absent in catalogs from 0.5.6 and earlier.
    #[serde(default, skip_serializing_if = "MediaLimits::is_unknown")]
    pub limits: MediaLimits,
}

/// [`CatalogModel::origin`]: downloaded from a Hub.
pub const ORIGIN_HUB: &str = "hub";
/// [`CatalogModel::origin`]: sent from another machine.
pub const ORIGIN_UPLOAD: &str = "upload";
/// [`CatalogModel::origin`]: made by a training run.
pub const ORIGIN_TRAIN: &str = "train";

impl CatalogModel {
    pub fn new(id: impl Into<String>, purpose: Purpose, state: ModelState) -> Self {
        Self { id: id.into(), purpose, state, ..Default::default() }
    }

    pub fn with_replicas(mut self, replicas: u32) -> Self {
        self.replicas = replicas;
        self
    }

    pub fn with_speed(mut self, tokens_per_sec: f32) -> Self {
        self.tokens_per_sec = Some(tokens_per_sec);
        self
    }

    pub fn with_context(mut self, tokens: u32) -> Self {
        self.context = Some(tokens);
        self
    }

    /// The inputs it takes beyond text.
    pub fn with_inputs(mut self, inputs: Modalities) -> Self {
        self.inputs = inputs;
        self
    }

    /// Marks it as a LoRA adapter on base model `base`.
    pub fn with_base(mut self, base: impl Into<String>) -> Self {
        self.base = Some(base.into());
        self
    }

    /// Where it came from ([`ORIGIN_HUB`], [`ORIGIN_UPLOAD`], [`ORIGIN_TRAIN`] or an app's own).
    pub fn with_origin(mut self, origin: impl Into<String>) -> Self {
        self.origin = Some(origin.into());
        self
    }

    /// Its license (an SPDX id, or `unknown`).
    pub fn with_license(mut self, license: impl Into<String>) -> Self {
        self.license = Some(license.into());
        self
    }

    /// Its media and request limits.
    pub fn with_limits(mut self, limits: MediaLimits) -> Self {
        self.limits = limits;
        self
    }

    /// Whether it is a LoRA adapter on another model.
    pub fn is_adapter(&self) -> bool {
        self.base.is_some()
    }

    /// A chat model that can read input needing `needed`.
    pub fn accepts(&self, needed: Modalities) -> bool {
        self.purpose == Purpose::Chat && self.inputs.covers(needed)
    }

    /// A speech-to-text model ([`Purpose::Transcribe`]).
    pub fn transcribes(&self) -> bool {
        self.purpose == Purpose::Transcribe
    }
}

/// Placement order: warm, then busy, loading, loadable.
fn rank(s: ModelState) -> u8 {
    match s {
        ModelState::Warm => 0,
        ModelState::Busy => 1,
        ModelState::Loading => 2,
        _ => 3,
    }
}

/// The models on offer. `rev` grows with every change.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Catalog {
    pub rev: u64,
    /// The entries. Reading one this version can't (a purpose from a newer version, say) skips
    /// that entry instead of failing the catalog (since 0.5.8).
    #[serde(deserialize_with = "readable_models")]
    pub models: Vec<CatalogModel>,
}

/// The entries this version can read; the rest are skipped.
fn readable_models<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Vec<CatalogModel>, D::Error> {
    struct Entry(Option<CatalogModel>);
    impl<'de> Deserialize<'de> for Entry {
        fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
            // Entries hold only strings, numbers, booleans and maps, so a JSON value carries
            // them whole from CBOR as well as from JSON.
            let v = serde_json::Value::deserialize(d)?;
            Ok(Entry(serde_json::from_value(v).ok()))
        }
    }
    Ok(Vec::<Entry>::deserialize(d)?.into_iter().filter_map(|e| e.0).collect())
}

impl Catalog {
    pub fn new(rev: u64, models: Vec<CatalogModel>) -> Self {
        Self { rev, models }
    }

    pub fn find(&self, id: &str) -> Option<&CatalogModel> {
        self.models.iter().find(|m| m.id == id)
    }

    /// The model for a request that needs `needed`: text requests keep `model` as is. Otherwise
    /// a named model must take those inputs, and with none named the best chat model that does
    /// is picked (warm, then busy, loading, loadable; then more replicas). A named model the
    /// catalog does not list is left to the node. `BadRequest` when nothing here can read it.
    pub fn route(&self, model: Option<&str>, needed: Modalities) -> Result<Option<String>, RemoteError> {
        if needed.is_text() {
            return Ok(model.map(str::to_string));
        }
        if let Some(id) = model {
            return match self.find(id) {
                Some(m) if !m.accepts(needed) => {
                    let what = m.inputs.lacking(needed).unwrap_or("this input");
                    Err(RemoteError::BadRequest { message: format!("model `{id}` does not take {what}; pick one whose catalog entry lists them") })
                }
                _ => Ok(Some(id.to_string())),
            };
        }
        self.models
            .iter()
            .filter(|m| m.accepts(needed))
            .min_by_key(|m| (rank(m.state), std::cmp::Reverse(m.replicas)))
            .map(|m| Some(m.id.clone()))
            .ok_or_else(|| {
                let what = Modalities::TEXT.lacking(needed).unwrap_or("this input");
                RemoteError::BadRequest { message: format!("no model in the pool takes {what}") }
            })
    }

    /// The model for a transcription request (infer/2 `Transcribe`): a named model must be a
    /// speech-to-text model ([`Purpose::Transcribe`]), and with none named the best one is picked
    /// (warm, then busy, loading, loadable; then more replicas). A named model the catalog does
    /// not list is left to the node. `BadRequest` when it names a model that does not transcribe
    /// or nothing here does.
    pub fn route_transcribe(&self, model: Option<&str>) -> Result<String, RemoteError> {
        if let Some(id) = model {
            return match self.find(id) {
                Some(m) if !m.transcribes() => Err(RemoteError::BadRequest {
                    message: format!("model `{id}` does not transcribe audio; pick one whose catalog purpose is `transcribe`"),
                }),
                _ => Ok(id.to_string()),
            };
        }
        self.transcribers()
            .min_by_key(|m| (rank(m.state), std::cmp::Reverse(m.replicas)))
            .map(|m| m.id.clone())
            .ok_or_else(|| RemoteError::BadRequest { message: "no model in the pool transcribes audio".into() })
    }

    /// The speech-to-text models.
    pub fn transcribers(&self) -> impl Iterator<Item = &CatalogModel> {
        self.models.iter().filter(|m| m.transcribes())
    }

    /// The catalog as a peer before 0.5.8 can read it: without entries whose purpose it lacks.
    fn for_older(&self) -> Catalog {
        Catalog { rev: self.rev, models: self.models.iter().filter(|m| m.purpose.before_transcribe()).cloned().collect() }
    }
}

/// Asking node to inference node. [`query`] and [`subscribe`] also say that the asker skips
/// entries it can't read (a field older nodes ignore); a node leaves entries peers before 0.5.8
/// can't read ([`Purpose::Transcribe`]) out of its answer to a message without it, so an app that
/// sends this type itself sees no transcription models.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ClientMsg {
    Query {
        #[serde(default, skip_serializing_if = "Option::is_none", with = "bytes::opt")]
        cert: Option<Vec<u8>>,
    },
    Subscribe {
        #[serde(default, skip_serializing_if = "Option::is_none", with = "bytes::opt")]
        cert: Option<Vec<u8>>,
    },
}

/// Inference node to asking node.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ServerMsg {
    Catalog(Catalog),
    Failed { error: RemoteError },
}

/// [`ClientMsg`] as sent and read from 0.5.8: `skips_unknown` says the asker skips entries it
/// can't read. Older nodes ignore the field; older askers leave it out.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
enum Ask {
    Query {
        #[serde(default, skip_serializing_if = "Option::is_none", with = "bytes::opt")]
        cert: Option<Vec<u8>>,
        #[serde(default, skip_serializing_if = "std::ops::Not::not")]
        skips_unknown: bool,
    },
    Subscribe {
        #[serde(default, skip_serializing_if = "Option::is_none", with = "bytes::opt")]
        cert: Option<Vec<u8>>,
        #[serde(default, skip_serializing_if = "std::ops::Not::not")]
        skips_unknown: bool,
    },
}

/// The serving side's catalog (an inference node's merged view).
pub trait CatalogHost: Send + Sync {
    fn authorize(&self, caller: &Caller) -> Result<(), RemoteError>;

    /// The catalog for this caller, and its changes.
    fn watch(&self, caller: &Caller) -> Result<watch::Receiver<Catalog>, RemoteError>;
}

/// A ready [`CatalogHost`]: publish the models, and subscribers hear about it.
#[derive(Clone)]
pub struct CatalogFeed {
    tx: Arc<watch::Sender<Catalog>>,
    auth: Arc<dyn Authorize>,
}

impl CatalogFeed {
    /// A feed whose callers `auth` admits.
    pub fn new(auth: Arc<dyn Authorize>) -> Self {
        Self { tx: Arc::new(watch::channel(Catalog::default()).0), auth }
    }

    /// Replaces the models; `rev` goes up by one.
    pub fn publish(&self, models: Vec<CatalogModel>) {
        self.tx.send_modify(|c| {
            c.rev += 1;
            c.models = models;
        });
    }

    pub fn current(&self) -> Catalog {
        self.tx.borrow().clone()
    }
}

impl CatalogHost for CatalogFeed {
    fn authorize(&self, caller: &Caller) -> Result<(), RemoteError> {
        self.auth.authorize(caller)
    }

    fn watch(&self, _caller: &Caller) -> Result<watch::Receiver<Catalog>, RemoteError> {
        Ok(self.tx.subscribe())
    }
}

/// Serve one catalog/1 stream. `caller` is the transport-authenticated peer; the certificate the
/// request presents is attached when the transport gave none, and the host authorizes it first.
/// An asker that doesn't say it skips entries it can't read (one before 0.5.8) gets the catalog
/// without [`Purpose::Transcribe`] entries, which it could not decode.
pub async fn serve(mut ch: Channel, caller: Caller, host: &dyn CatalogHost) -> Result<(), RemoteError> {
    let (cert, follow, skips_unknown) = match ch.recv_cbor::<Ask>().await? {
        Some(Ask::Query { cert, skips_unknown }) => (cert, false, skips_unknown),
        Some(Ask::Subscribe { cert, skips_unknown }) => (cert, true, skips_unknown),
        None => return Ok(()),
    };
    let readable = |c: Catalog| if skips_unknown { c } else { c.for_older() };
    let caller = caller.presenting(cert);
    let mut rx = match host.authorize(&caller).and_then(|()| host.watch(&caller)) {
        Ok(rx) => rx,
        Err(error) => return ch.send_cbor(&ServerMsg::Failed { error }).await,
    };
    let now = readable(rx.borrow_and_update().clone());
    ch.send_cbor(&ServerMsg::Catalog(now)).await?;
    if !follow {
        return Ok(());
    }
    loop {
        tokio::select! {
            changed = rx.changed() => {
                if changed.is_err() {
                    return Ok(());
                }
                let now = readable(rx.borrow_and_update().clone());
                ch.send_cbor(&ServerMsg::Catalog(now)).await?;
            }
            m = ch.rx.recv() => match m {
                Ok(Some(_)) => {}
                Ok(None) | Err(_) => return Ok(()),
            },
        }
    }
}

/// Asks once.
pub async fn query(ch: &mut Channel, cert: Option<Vec<u8>>) -> Result<Catalog, RemoteError> {
    ch.send_cbor(&Ask::Query { cert, skips_unknown: true }).await?;
    match ch.recv_cbor::<ServerMsg>().await? {
        Some(ServerMsg::Catalog(c)) => Ok(c),
        Some(ServerMsg::Failed { error }) => Err(error),
        None => Err(RemoteError::Closed),
    }
}

/// Subscribes and hands every catalog to `on`, until the stream ends (`Ok`) or fails.
pub async fn subscribe(ch: &mut Channel, cert: Option<Vec<u8>>, mut on: impl FnMut(Catalog)) -> Result<(), RemoteError> {
    ch.send_cbor(&Ask::Subscribe { cert, skips_unknown: true }).await?;
    loop {
        match ch.recv_cbor::<ServerMsg>().await? {
            Some(ServerMsg::Catalog(c)) => on(c),
            Some(ServerMsg::Failed { error }) => return Err(error),
            None => return Ok(()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::remote::memory_pair;

    fn anyone() -> Arc<dyn Authorize> {
        Arc::new(|_: &Caller| Ok(()))
    }

    #[tokio::test]
    async fn query_and_subscribe_see_changes() {
        let feed = CatalogFeed::new(anyone());
        feed.publish(vec![CatalogModel::new("gemma", Purpose::Chat, ModelState::Warm).with_replicas(2)]);
        let (mut ours, theirs) = memory_pair();
        let f = feed.clone();
        tokio::spawn(async move { serve(theirs, Caller::new("phone"), &f).await });
        let c = query(&mut ours, None).await.unwrap();
        assert_eq!(c.rev, 1);
        assert_eq!(c.find("gemma").unwrap().replicas, 2);

        let (mut ours, theirs) = memory_pair();
        let f = feed.clone();
        tokio::spawn(async move { serve(theirs, Caller::new("phone"), &f).await });
        let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
        tokio::spawn(async move { subscribe(&mut ours, None, |c| tx.send(c).unwrap()).await });
        assert_eq!(rx.recv().await.unwrap().rev, 1);
        feed.publish(vec![CatalogModel::new("gemma", Purpose::Chat, ModelState::Loading)]);
        let next = rx.recv().await.unwrap();
        assert_eq!((next.rev, next.models[0].state), (2, ModelState::Loading));
    }

    #[tokio::test]
    async fn the_host_authorizes_with_the_presented_cert() {
        let auth: Arc<dyn Authorize> = Arc::new(|c: &Caller| {
            if c.peer == "laptop" && c.cert_bytes.as_deref() == Some(b"cert") { Ok(()) } else { Err(RemoteError::refused("unknown-device")) }
        });
        let feed = CatalogFeed::new(auth);
        for (peer, cert, ok) in [("laptop", Some(b"cert".to_vec()), true), ("laptop", None, false), ("stranger", Some(b"cert".to_vec()), false)] {
            let (mut ours, theirs) = memory_pair();
            let f = feed.clone();
            tokio::spawn(async move { serve(theirs, Caller::new(peer), &f).await });
            let r = query(&mut ours, cert).await;
            assert_eq!(r.is_ok(), ok, "{peer}");
            if !ok {
                assert_eq!(r.unwrap_err(), RemoteError::refused("unknown-device"));
            }
        }
    }

    #[test]
    fn route_keeps_image_requests_off_text_only_models() {
        let c = Catalog::new(
            1,
            vec![
                CatalogModel::new("qwen", Purpose::Chat, ModelState::Warm),
                CatalogModel::new("gemma-cold", Purpose::Chat, ModelState::Loadable).with_inputs(Modalities::new(true, true)),
                CatalogModel::new("gemma", Purpose::Chat, ModelState::Warm).with_inputs(Modalities::VISION),
                CatalogModel::new("embed", Purpose::Embedding, ModelState::Warm).with_inputs(Modalities::new(true, true)),
            ],
        );
        assert_eq!(c.route(None, Modalities::TEXT).unwrap(), None);
        assert_eq!(c.route(Some("qwen"), Modalities::TEXT).unwrap().as_deref(), Some("qwen"));
        assert_eq!(c.route(None, Modalities::VISION).unwrap().as_deref(), Some("gemma"), "warm first");
        assert_eq!(c.route(None, Modalities::AUDIO).unwrap().as_deref(), Some("gemma-cold"));
        let e = c.route(Some("qwen"), Modalities::VISION).unwrap_err();
        assert!(matches!(&e, RemoteError::BadRequest { message } if message.contains("`qwen` does not take images")), "{e}");
        assert_eq!(c.route(Some("not-listed"), Modalities::VISION).unwrap().as_deref(), Some("not-listed"));
        let text_only = Catalog::new(1, vec![CatalogModel::new("qwen", Purpose::Chat, ModelState::Warm)]);
        let e = text_only.route(None, Modalities::new(true, true)).unwrap_err();
        assert!(matches!(&e, RemoteError::BadRequest { message } if message == "no model in the pool takes images and audio"), "{e}");
    }

    #[test]
    fn inputs_are_optional_on_the_wire() {
        let plain = CatalogModel::new("qwen", Purpose::Chat, ModelState::Warm);
        let b = crate::remote::to_cbor(&plain).unwrap();
        assert!(!b.windows(6).any(|w| w == b"inputs"), "text-only entries look as before");
        let v = CatalogModel::new("gemma", Purpose::Chat, ModelState::Warm).with_inputs(Modalities::VISION);
        assert_eq!(crate::remote::from_cbor::<CatalogModel>(&crate::remote::to_cbor(&v).unwrap()).unwrap(), v);
    }

    #[test]
    fn base_is_optional_on_the_wire() {
        let plain = CatalogModel::new("qwen", Purpose::Chat, ModelState::Warm);
        let b = crate::remote::to_cbor(&plain).unwrap();
        assert!(!b.windows(4).any(|w| w == b"base"), "models look as before");
        assert!(!plain.is_adapter());
        let a = CatalogModel::new("qwen+tune", Purpose::Chat, ModelState::Warm).with_base("qwen");
        assert!(a.is_adapter());
        assert_eq!(crate::remote::from_cbor::<CatalogModel>(&crate::remote::to_cbor(&a).unwrap()).unwrap(), a);
    }

    /// A catalog/1 entry as a 0.5.3 node sends it (and reads it): no origin or license.
    #[derive(Debug, PartialEq, Serialize, Deserialize)]
    struct OldModel {
        id: String,
        #[serde(default)]
        purpose: Purpose,
        #[serde(default)]
        state: ModelState,
        #[serde(default)]
        replicas: u32,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        revision: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        base: Option<String>,
    }

    #[test]
    fn origin_and_license_are_optional_on_the_wire() {
        let plain = CatalogModel::new("qwen", Purpose::Chat, ModelState::Warm);
        let b = crate::remote::to_cbor(&plain).unwrap();
        assert!(!b.windows(6).any(|w| w == b"origin"), "entries without an origin look as before");
        assert!(!b.windows(7).any(|w| w == b"license"), "entries without a license look as before");
        let m = CatalogModel::new("qwen+tune", Purpose::Chat, ModelState::Warm).with_origin(ORIGIN_TRAIN).with_license("Apache-2.0");
        assert_eq!((m.origin.as_deref(), m.license.as_deref()), (Some("train"), Some("Apache-2.0")));
        assert_eq!(crate::remote::from_cbor::<CatalogModel>(&crate::remote::to_cbor(&m).unwrap()).unwrap(), m);
    }

    #[test]
    fn old_peers_read_and_send_entries_without_origin() {
        // An old node's entry reads with neither field.
        let old =
            OldModel { id: "qwen".into(), purpose: Purpose::Chat, state: ModelState::Warm, replicas: 1, revision: Some("abc".into()), base: None };
        let m = crate::remote::from_cbor::<CatalogModel>(&crate::remote::to_cbor(&old).unwrap()).unwrap();
        assert_eq!((m.origin, m.license, m.revision.as_deref()), (None, None, Some("abc")));
        // A new node's entry with both fields still reads on an old peer, which skips them.
        let new = CatalogModel::new("qwen", Purpose::Chat, ModelState::Warm).with_replicas(1).with_origin(ORIGIN_UPLOAD).with_license("unknown");
        let seen = crate::remote::from_cbor::<OldModel>(&crate::remote::to_cbor(&new).unwrap()).unwrap();
        assert_eq!(seen, OldModel { revision: None, ..old });
        // And a whole catalog message from a new node reads on an old peer's catalog type.
        let msg = ServerMsg::Catalog(Catalog::new(3, vec![new]));
        assert!(matches!(
            crate::remote::from_cbor::<ServerMsg>(&crate::remote::to_cbor(&msg).unwrap()).unwrap(),
            ServerMsg::Catalog(c) if c.models[0].origin.as_deref() == Some("upload")
        ));
    }

    #[test]
    fn limits_round_trip_and_stay_off_the_wire_when_unknown() {
        let plain = CatalogModel::new("qwen", Purpose::Chat, ModelState::Warm).with_context(8192);
        let b = crate::remote::to_cbor(&plain).unwrap();
        assert!(!b.windows(6).any(|w| w == b"limits"), "entries without limits look as before");
        assert!(!serde_json::to_string(&plain).unwrap().contains("limits"));
        let limits = MediaLimits::new()
            .with_max_images(8)
            .with_image_tokens(256)
            .with_image_edge(896)
            .with_audio_max_seconds(30)
            .with_audio_tokens_per_minute(1500)
            .with_max_request_bytes(32 << 20);
        let m = CatalogModel::new("gemma", Purpose::Chat, ModelState::Warm).with_inputs(Modalities::new(true, true)).with_limits(limits);
        assert_eq!(crate::remote::from_cbor::<CatalogModel>(&crate::remote::to_cbor(&m).unwrap()).unwrap(), m);
        assert_eq!(serde_json::from_str::<CatalogModel>(&serde_json::to_string(&m).unwrap()).unwrap(), m);
        let j = serde_json::to_value(&m).unwrap();
        assert_eq!(j["limits"]["max_images"], 8);
        assert_eq!(j["limits"]["max_request_bytes"], 32 << 20);
        assert!(j["limits"].get("image_patch").is_none(), "unknown fields stay off");
        // Only some limits known: the rest stay absent.
        let some = CatalogModel::new("llava", Purpose::Chat, ModelState::Warm).with_limits(MediaLimits::new().with_image_tokens(576));
        let back = crate::remote::from_cbor::<CatalogModel>(&crate::remote::to_cbor(&some).unwrap()).unwrap();
        assert_eq!((back.limits.image_tokens, back.limits.max_images), (Some(576), None));
    }

    #[test]
    fn old_peers_read_and_send_entries_without_limits() {
        // JSON and CBOR from a node before 0.5.7 read with no limits.
        let old_json = r#"{"id":"qwen","purpose":"chat","state":"warm","replicas":1,"context":4096,"inputs":{"vision":true}}"#;
        let m: CatalogModel = serde_json::from_str(old_json).unwrap();
        assert!(m.limits.is_unknown());
        assert_eq!(m.context, Some(4096));
        let old =
            OldModel { id: "qwen".into(), purpose: Purpose::Chat, state: ModelState::Warm, replicas: 1, revision: Some("abc".into()), base: None };
        let m = crate::remote::from_cbor::<CatalogModel>(&crate::remote::to_cbor(&old).unwrap()).unwrap();
        assert!(m.limits.is_unknown());
        // A new entry with limits still reads on an old peer, which skips them.
        let new = CatalogModel::new("qwen", Purpose::Chat, ModelState::Warm)
            .with_replicas(1)
            .with_limits(MediaLimits::new().with_max_images(2).with_max_request_bytes(1 << 20));
        let seen = crate::remote::from_cbor::<OldModel>(&crate::remote::to_cbor(&new).unwrap()).unwrap();
        assert_eq!(seen, OldModel { revision: None, ..old });
        let msg = ServerMsg::Catalog(Catalog::new(4, vec![new]));
        assert!(matches!(
            crate::remote::from_cbor::<ServerMsg>(&crate::remote::to_cbor(&msg).unwrap()).unwrap(),
            ServerMsg::Catalog(c) if c.models[0].limits.max_images == Some(2)
        ));
    }

    #[test]
    fn wire_is_cbor_with_byte_string_certs() {
        let m = ClientMsg::Query { cert: Some(vec![1, 2, 3]) };
        let b = crate::remote::to_cbor(&m).unwrap();
        // map(1) "query" map(1) "cert" bytes(3)
        assert!(b.windows(4).any(|w| w == [0x43, 1, 2, 3]), "{b:x?}");
        assert_eq!(crate::remote::from_cbor::<ClientMsg>(&b).unwrap(), m);
    }

    fn transcribe_catalog() -> Catalog {
        Catalog::new(
            7,
            vec![
                CatalogModel::new("gemma", Purpose::Chat, ModelState::Warm).with_inputs(Modalities::new(true, true)),
                CatalogModel::new("nomic", Purpose::Embedding, ModelState::Warm),
                CatalogModel::new("voxtral-cold", Purpose::Transcribe, ModelState::Loadable).with_inputs(Modalities::new(false, true)),
                CatalogModel::new("voxtral", Purpose::Transcribe, ModelState::Warm).with_replicas(1).with_inputs(Modalities::new(false, true)),
                CatalogModel::new("whisper", Purpose::Transcribe, ModelState::Warm).with_replicas(2),
            ],
        )
    }

    #[test]
    fn transcribe_purpose_names_match_weirdnet_model() {
        for (p, name) in [(Purpose::Chat, "chat"), (Purpose::Embedding, "embedding"), (Purpose::Transcribe, "transcribe")] {
            assert_eq!(p.as_str(), name);
            assert_eq!(p.to_string(), name);
            assert_eq!(Purpose::parse(name), Some(p));
            assert_eq!(serde_json::to_string(&p).unwrap(), format!("\"{name}\""));
            assert_eq!(serde_json::from_str::<Purpose>(&format!("\"{name}\"")).unwrap(), p);
            assert_eq!(crate::remote::from_cbor::<Purpose>(&crate::remote::to_cbor(&p).unwrap()).unwrap(), p);
        }
        assert_eq!(Purpose::parse("speech"), None);
        let m = CatalogModel::new("voxtral", Purpose::Transcribe, ModelState::Warm);
        assert!(m.transcribes() && !m.accepts(Modalities::TEXT) && !m.accepts(Modalities::AUDIO));
    }

    #[test]
    fn route_transcribe_picks_the_best_transcriber() {
        let c = transcribe_catalog();
        // Warm before loadable, then more replicas.
        assert_eq!(c.route_transcribe(None).unwrap(), "whisper");
        assert_eq!(c.transcribers().map(|m| m.id.as_str()).collect::<Vec<_>>(), ["voxtral-cold", "voxtral", "whisper"]);
        assert_eq!(c.route_transcribe(Some("voxtral-cold")).unwrap(), "voxtral-cold");
        // A model the catalog doesn't list is left to the node.
        assert_eq!(c.route_transcribe(Some("elsewhere")).unwrap(), "elsewhere");
        for id in ["gemma", "nomic"] {
            let e = c.route_transcribe(Some(id)).unwrap_err();
            assert!(matches!(&e, RemoteError::BadRequest { message } if message.contains(id) && message.contains("transcribe")), "{e:?}");
        }
        let none = Catalog::new(1, vec![CatalogModel::new("gemma", Purpose::Chat, ModelState::Warm)]);
        assert!(matches!(none.route_transcribe(None), Err(RemoteError::BadRequest { message }) if message.contains("transcribes")));
    }

    #[test]
    fn chat_routing_never_picks_a_transcriber() {
        let c = Catalog::new(1, vec![CatalogModel::new("voxtral", Purpose::Transcribe, ModelState::Warm).with_inputs(Modalities::new(false, true))]);
        assert!(matches!(c.route(None, Modalities::AUDIO), Err(RemoteError::BadRequest { .. })));
        assert!(matches!(c.route(Some("voxtral"), Modalities::AUDIO), Err(RemoteError::BadRequest { .. })));
        assert_eq!(transcribe_catalog().route(None, Modalities::AUDIO).unwrap().as_deref(), Some("gemma"));
    }

    #[test]
    fn transcribers_round_trip_and_unreadable_entries_are_skipped() {
        let c = transcribe_catalog();
        assert_eq!(crate::remote::from_cbor::<Catalog>(&crate::remote::to_cbor(&c).unwrap()).unwrap(), c);
        assert_eq!(serde_json::from_str::<Catalog>(&serde_json::to_string(&c).unwrap()).unwrap(), c);
        // An entry from a newer version (an unknown purpose) is skipped, not fatal.
        let json = r#"{"rev":3,"models":[{"id":"a","purpose":"chat","tokens_per_sec":12.5},{"id":"b","purpose":"diarize"},{"id":"c","purpose":"transcribe"}]}"#;
        let read: Catalog = serde_json::from_str(json).unwrap();
        assert_eq!(read.models.iter().map(|m| m.id.as_str()).collect::<Vec<_>>(), ["a", "c"]);
        assert_eq!(read.models[0].tokens_per_sec, Some(12.5));
        let v: serde_json::Value = serde_json::from_str(json).unwrap();
        let read: Catalog = crate::remote::from_cbor(&crate::remote::to_cbor(&v).unwrap()).unwrap();
        assert_eq!(read.models.iter().map(|m| m.id.as_str()).collect::<Vec<_>>(), ["a", "c"]);
        // The whole entry still round-trips through CBOR with every field set.
        let full = Catalog::new(
            9,
            vec![
                CatalogModel::new("q", Purpose::Chat, ModelState::Busy)
                    .with_speed(31.25)
                    .with_context(8192)
                    .with_base("base")
                    .with_origin(ORIGIN_UPLOAD)
                    .with_license("MIT")
                    .with_limits(MediaLimits::new().with_max_images(4).with_max_request_bytes(1 << 25)),
            ],
        );
        assert_eq!(crate::remote::from_cbor::<Catalog>(&crate::remote::to_cbor(&full).unwrap()).unwrap(), full);
    }

    /// The purposes a peer before 0.5.8 knows, and its strict catalog.
    #[derive(Debug, PartialEq, Deserialize)]
    #[serde(rename_all = "snake_case")]
    enum OldPurpose {
        Chat,
        Embedding,
    }

    #[derive(Debug, Deserialize)]
    struct OldEntry {
        id: String,
        #[allow(dead_code)]
        purpose: OldPurpose,
    }

    #[derive(Debug, Deserialize)]
    struct OldCatalog {
        models: Vec<OldEntry>,
    }

    #[derive(Debug, Deserialize)]
    #[serde(rename_all = "snake_case")]
    enum OldServerMsg {
        Catalog(OldCatalog),
    }

    #[tokio::test]
    async fn older_askers_get_the_catalog_without_transcribers() {
        let feed = CatalogFeed::new(anyone());
        feed.publish(transcribe_catalog().models);
        // An old peer could not read the full catalog at all.
        assert!(crate::remote::from_cbor::<OldServerMsg>(&crate::remote::to_cbor(&ServerMsg::Catalog(feed.current())).unwrap()).is_err());

        for follow in [false, true] {
            // An asker before 0.5.8 sends plain `ClientMsg`s.
            let (mut ours, theirs) = memory_pair();
            let f = feed.clone();
            tokio::spawn(async move { serve(theirs, Caller::new("phone"), &f).await });
            let ask = if follow { ClientMsg::Subscribe { cert: None } } else { ClientMsg::Query { cert: None } };
            ours.send_cbor(&ask).await.unwrap();
            let bytes = ours.rx.recv().await.unwrap().unwrap();
            let OldServerMsg::Catalog(old) = crate::remote::from_cbor::<OldServerMsg>(&bytes).unwrap();
            assert_eq!(old.models.iter().map(|m| m.id.as_str()).collect::<Vec<_>>(), ["gemma", "nomic"]);
        }

        // 0.5.8 askers see everything, on query and on every change.
        let (mut ours, theirs) = memory_pair();
        let f = feed.clone();
        tokio::spawn(async move { serve(theirs, Caller::new("phone"), &f).await });
        assert_eq!(query(&mut ours, None).await.unwrap().transcribers().count(), 3);
        let (mut ours, theirs) = memory_pair();
        let f = feed.clone();
        tokio::spawn(async move { serve(theirs, Caller::new("phone"), &f).await });
        let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
        tokio::spawn(async move { subscribe(&mut ours, None, |c| tx.send(c).unwrap()).await });
        assert_eq!(rx.recv().await.unwrap().models.len(), 5);
        feed.publish(vec![CatalogModel::new("whisper", Purpose::Transcribe, ModelState::Busy)]);
        assert_eq!(rx.recv().await.unwrap().route_transcribe(None).unwrap(), "whisper");
    }

    #[test]
    fn the_new_ask_reads_on_old_nodes_and_old_asks_read_on_new_ones() {
        let new = Ask::Query { cert: Some(vec![1, 2]), skips_unknown: true };
        assert_eq!(
            crate::remote::from_cbor::<ClientMsg>(&crate::remote::to_cbor(&new).unwrap()).unwrap(),
            ClientMsg::Query { cert: Some(vec![1, 2]) }
        );
        let new = Ask::Subscribe { cert: None, skips_unknown: true };
        assert_eq!(crate::remote::from_cbor::<ClientMsg>(&crate::remote::to_cbor(&new).unwrap()).unwrap(), ClientMsg::Subscribe { cert: None });
        let old = ClientMsg::Subscribe { cert: Some(vec![3]) };
        assert_eq!(
            crate::remote::from_cbor::<Ask>(&crate::remote::to_cbor(&old).unwrap()).unwrap(),
            Ask::Subscribe { cert: Some(vec![3]), skips_unknown: false }
        );
        // Unset, the flag stays off the wire: the bytes are what 0.5.7 sends.
        let quiet = Ask::Query { cert: None, skips_unknown: false };
        assert_eq!(crate::remote::to_cbor(&quiet).unwrap(), crate::remote::to_cbor(&ClientMsg::Query { cert: None }).unwrap());
    }
}