semiont 0.7.2

Semiont's Rust SDK: the protocol's types, the transport contract and a client of the bus over it, and what a client needs to name its principals and hold its roles, as protocol/specs/ states it.
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
//! Browse: reads. The queries (`Cached`) answer from the client's cache and
//! stay true as the knowledge base changes; the one-shot reads are asked
//! once and never kept; and the signals are this viewer's own, with the one
//! report among them going over the wire.
//!
//! What keeps a query true is protocol/specs/src/client/refresh.json
//! (`crate::refresh`), which the client's refresher applies to the caches
//! here (`super::refresher`): each event on the bus, and the reopening of a
//! dropped stream, asks again for the queries its row names, writes the ones
//! whose new value the event carries, and ends the ones whose entity is gone
//! (protocol/contract/CACHE-SEMANTICS.md B12–B13b, B19, B20).

use super::refresher::Subject;
use crate::bus::{LIMITS_OPERATIONS, Operation, operation};
use crate::cache::{Cache, CacheKey, CachePersister, CacheState, CacheValue, StoragePersister};
use crate::cached::{Cached, Keyed, Observed, Source};
use crate::channels::{
    BrowseAnnotationHistoryRequested, BrowseAnnotationRequested, BrowseClick,
    BrowseDirectoryRequested, BrowseEnginesRequested, BrowseEntityTypesRequested,
    BrowseEventsRequested, BrowseKbRequested, BrowseResourceOpen, BrowseResourceRequested,
    BrowseResourceViewed, BrowseResourcesRequested, BrowseTagSchemasRequested,
};
use crate::client::{CachePersistence, Links};
use crate::errors::{BusRequestError, BusRequestErrorCode, SemiontError};
use crate::locked;
use crate::refresh::CacheQuery;
use crate::resources::decode_representation;
use crate::session::cache_key;
use crate::state_unit::StateUnit;
use crate::timing::{MAX_STORED_BYTES_COUNT, SAVE_DEBOUNCE};
use crate::transport::{BoxFuture, Content, ContentStream, ContentTransport, Envelope};
use crate::types::{
    AnchoredTextAnswer, Annotation, AttributedEvent, BrowseAnnotationHistoryRequest,
    BrowseAnnotationRequest, BrowseClickEvent, BrowseDirectoryRequest, BrowseDirectoryRequestSort,
    BrowseDirectoryResultResponse, BrowseEnginesRequest, BrowseEntityTypesRequest,
    BrowseEventsRequest, BrowseKbRequest, BrowseResourceOpenEvent, BrowseResourceRequest,
    BrowseResourceViewedEvent, BrowseResourcesRequest, BrowseTagSchemasRequest, EngineEntry,
    EngineLimits, GetAnnotationHistoryResponse, GetAnnotationsResponse, GetResourceResponse,
    InferenceLimits, InferenceLimitsResultResponse, KbDescription, ListResourcesResponse,
    ResourceDescriptor, TagSchema,
};
use crate::types::{AnnotationId, ResourceId};
use futures_core::Stream;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::Map;
use std::collections::HashMap;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};
use tokio_stream::wrappers::WatchStream;

/// How many resources a list or a search asks for when its caller states no
/// limit. Every SDK asks for as many: the cases of
/// protocol/specs/src/client/surface.json hold each to it.
const LIST_LIMIT: u64 = 100;

/// The key of a query the knowledge base has one of.
pub(super) const WHOLE: &str = "_";

/// The version of what the persisted caches hold. A document of another
/// version reads as nothing kept, so this changes when a kept value's shape
/// does.
const PERSISTED_VERSION: u64 = 1;

/// Which resources a list, or a search, is of. Each field that is stated
/// narrows it.
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
pub struct ResourceFilters {
    /// How many resources at most: a hundred when not stated.
    pub limit: Option<u64>,
    /// How many of the resources admitted are passed over before the page
    /// begins: none when not stated.
    pub offset: Option<u64>,
    pub archived: Option<bool>,
    pub entity_type: Option<String>,
}

impl ResourceFilters {
    /// How many resources a request with these filters asks for.
    pub(crate) fn limit(&self) -> u64 {
        self.limit.unwrap_or(LIST_LIMIT)
    }

    /// How many resources a request with these filters passes over.
    pub(crate) fn offset(&self) -> u64 {
        self.offset.unwrap_or(0)
    }
}

/// One of the knowledge base's engines: its entry, and its limits when the
/// service holding its credentials reported them. As JSON the limits sit
/// beside the entry's own fields.
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct EngineWithLimits {
    #[serde(flatten)]
    pub entry: EngineEntry,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub limits: Option<InferenceLimits>,
}

/// The engines, each with the limits reported of it.
fn joined(engines: Vec<EngineEntry>, reported: &[EngineLimits]) -> Vec<EngineWithLimits> {
    engines
        .into_iter()
        .map(|entry| {
            let limits = reported
                .iter()
                .find(|report| report.engine == entry.engine)
                .map(|report| report.limits.clone());
            EngineWithLimits { entry, limits }
        })
        .collect()
}

/// Every future's output, in their order, each run as far as it will go
/// before the next is looked at.
async fn all<'a, T>(futures: Vec<BoxFuture<'a, T>>) -> Vec<T> {
    let mut pending: Vec<Option<BoxFuture<'a, T>>> = futures.into_iter().map(Some).collect();
    let mut done: Vec<Option<T>> = pending.iter().map(|_| None).collect();
    std::future::poll_fn(|cx| {
        for (slot, output) in pending.iter_mut().zip(done.iter_mut()) {
            if let Some(future) = slot
                && let Poll::Ready(value) = future.as_mut().poll(cx)
            {
                *output = Some(value);
                *slot = None;
            }
        }
        if pending.iter().all(Option::is_none) {
            Poll::Ready(())
        } else {
            Poll::Pending
        }
    })
    .await;
    done.into_iter().flatten().collect()
}

/// The engines one key holder reports the limits of. A holder that is down,
/// silent or mistaken reports none: its engines show no limits, and the
/// engines are not held up by it for longer than a request waits.
async fn limits_reported(links: &Links, operation: &Operation) -> Vec<EngineLimits> {
    let Ok(Some(response)) = links
        .wire
        .request_of(operation, Map::new(), links.timing.bus_request)
        .await
    else {
        return Vec::new();
    };
    serde_json::from_value::<InferenceLimitsResultResponse>(response)
        .map(|reported| reported.limits)
        .unwrap_or_default()
}

// ── The caches ──────────────────────────────────────────────────────────

/// The caches the namespace's queries answer from.
pub(super) struct Live {
    pub(super) resource: Cache<ResourceId, ResourceDescriptor>,
    pub(super) lists: Cache<ResourceFilters, ListResourcesResponse>,
    pub(super) annotations: Cache<ResourceId, GetAnnotationsResponse>,
    pub(super) annotation: Cache<AnnotationId, Annotation>,
    /// The resource each annotation that was asked for is of: an annotation
    /// is kept by its own id, and a request for it names its resource too.
    pub(super) annotation_of: Arc<Mutex<HashMap<AnnotationId, ResourceId>>>,
    pub(super) entity_types: Cache<String, Vec<String>>,
    pub(super) tag_schemas: Cache<String, Vec<TagSchema>>,
    pub(super) engines: Cache<String, Vec<EngineEntry>>,
    /// Each key holder's report of its engines' limits, by the operation
    /// that asks it.
    pub(super) limits: Cache<&'static str, Vec<EngineLimits>>,
    pub(super) events: Cache<ResourceId, Vec<AttributedEvent>>,
}

/// A cache the client's storage keeps under `name`, when it has one: the
/// small ones a returning client shows at once.
fn kept<K, V, F, Fut>(persistence: Option<&CachePersistence>, name: &str, fetch: F) -> Cache<K, V>
where
    K: CacheKey + Serialize + DeserializeOwned,
    V: CacheValue + Serialize + DeserializeOwned,
    F: Fn(K) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Result<V, Box<dyn std::error::Error + Send + Sync>>> + Send + 'static,
{
    match persistence {
        None => Cache::new(fetch),
        Some(persistence) => {
            let persister: Arc<dyn CachePersister<K, V>> = Arc::new(StoragePersister::new(
                persistence.storage.clone(),
                cache_key(&persistence.key_prefix, name),
                PERSISTED_VERSION,
                MAX_STORED_BYTES_COUNT,
            ));
            Cache::persisted(fetch, persister, SAVE_DEBOUNCE)
        }
    }
}

fn events_of(resource_id: &ResourceId) -> BrowseEventsRequest {
    BrowseEventsRequest {
        resource_id: resource_id.clone(),
        r#type: None,
        user_id: None,
        limit: None,
    }
}

impl Live {
    fn new(
        links: &Links,
        content: &Arc<dyn ContentTransport>,
        persistence: Option<&CachePersistence>,
    ) -> Arc<Live> {
        let annotation_of: Arc<Mutex<HashMap<AnnotationId, ResourceId>>> = Arc::default();
        let asking = |links: &Links| links.clone();

        let resource = kept(persistence, "resource", {
            let links = asking(links);
            move |resource_id: ResourceId| {
                let links = links.clone();
                async move {
                    let answer = links
                        .request::<BrowseResourceRequested>(&BrowseResourceRequest { resource_id })
                        .await?;
                    Ok(answer.response.resource)
                }
            }
        });
        let lists = Cache::new({
            let links = asking(links);
            move |filters: ResourceFilters| {
                let links = links.clone();
                async move {
                    let request = BrowseResourcesRequest {
                        limit: Some(filters.limit()),
                        archived: filters.archived,
                        offset: Some(filters.offset()),
                        entity_type: filters.entity_type,
                    };
                    let answer = links.request::<BrowseResourcesRequested>(&request).await?;
                    Ok(answer.response)
                }
            }
        });
        let annotations = kept(persistence, "annotations", {
            let content = content.clone();
            move |resource_id: ResourceId| {
                let content = content.clone();
                async move { Ok(content.get_annotations(&resource_id).await?) }
            }
        });
        let annotation = kept(persistence, "annotation-detail", {
            let links = asking(links);
            let annotation_of = annotation_of.clone();
            move |annotation_id: AnnotationId| {
                let links = links.clone();
                let resource_id = locked(&annotation_of).get(&annotation_id).cloned();
                async move {
                    let Some(resource_id) = resource_id else {
                        return Err(BusRequestError::new(
                            BusRequestErrorCode::Rejected,
                            format!(
                                "Cannot ask for annotation {annotation_id}: the resource it is of is not known"
                            ),
                        )
                        .into());
                    };
                    let answer = links
                        .request::<BrowseAnnotationRequested>(&BrowseAnnotationRequest {
                            resource_id,
                            annotation_id,
                        })
                        .await?;
                    Ok(answer.response.annotation)
                }
            }
        });
        let entity_types = kept(persistence, "entity-types", {
            let links = asking(links);
            move |_: String| {
                let links = links.clone();
                async move {
                    let answer = links
                        .request::<BrowseEntityTypesRequested>(&BrowseEntityTypesRequest {})
                        .await?;
                    Ok(answer.response.entity_types)
                }
            }
        });
        let tag_schemas = kept(persistence, "tag-schemas", {
            let links = asking(links);
            move |_: String| {
                let links = links.clone();
                async move {
                    let answer = links
                        .request::<BrowseTagSchemasRequested>(&BrowseTagSchemasRequest {})
                        .await?;
                    Ok(answer.response.tag_schemas)
                }
            }
        });
        let engines = Cache::new({
            let links = asking(links);
            move |_: String| {
                let links = links.clone();
                async move {
                    let answer = links
                        .request::<BrowseEnginesRequested>(&BrowseEnginesRequest {})
                        .await?;
                    Ok(answer.response.engines)
                }
            }
        });
        let limits = Cache::new({
            let links = asking(links);
            move |holder: &'static str| {
                let links = links.clone();
                async move {
                    Ok(match operation(holder) {
                        Some(operation) => limits_reported(&links, operation).await,
                        None => Vec::new(),
                    })
                }
            }
        });
        let events = Cache::new({
            let links = asking(links);
            move |resource_id: ResourceId| {
                let links = links.clone();
                async move {
                    let answer = links
                        .request::<BrowseEventsRequested>(&events_of(&resource_id))
                        .await?;
                    Ok(answer.response.events)
                }
            }
        });

        Arc::new(Live {
            resource,
            lists,
            annotations,
            annotation,
            annotation_of,
            entity_types,
            tag_schemas,
            engines,
            limits,
            events,
        })
    }

    /// B13b: the event carries the value. The table's own build refuses a
    /// row that writes a query no event carries a value for.
    pub(super) fn write(&self, query: CacheQuery, subject: &Subject) {
        let (Some(resource), Some(written)) = (&subject.resource, &subject.written) else {
            return;
        };
        match query {
            // Into the resource's list, when the client holds it: where the
            // annotation was, or at its end.
            CacheQuery::Annotations => {
                if let Some(mut list) = self.annotations.get(resource) {
                    match list.annotations.iter_mut().find(|a| a.id == written.id) {
                        Some(was) => *was = written.clone(),
                        None => list.annotations.push(written.clone()),
                    }
                    self.annotations.set(resource, list);
                }
            }
            CacheQuery::Annotation => {
                locked(&self.annotation_of).insert(written.id.clone(), resource.clone());
                self.annotation.set(&written.id, written.clone());
            }
            _ => {}
        }
    }

    /// B13a: the event says the entity is gone. Only an annotation is ever
    /// said to be.
    pub(super) fn remove(&self, query: CacheQuery, subject: &Subject) {
        if let (CacheQuery::Annotation, Some(annotation)) = (query, &subject.annotation) {
            self.annotation_gone(annotation);
        }
    }

    /// An annotation the cache holds is ended as not found. What says which
    /// resource it was of is kept: an observer arriving at the ended key asks
    /// the service, and that request names the resource.
    fn annotation_gone(&self, annotation_id: &AnnotationId) {
        let id = annotation_id.clone();
        if self.annotation.known(&id) {
            self.annotation.remove(
                &id,
                Box::new(SemiontError::from(BusRequestError::new(
                    BusRequestErrorCode::NotFound,
                    format!("Annotation {annotation_id} was removed"),
                ))),
            );
        }
    }

    fn dispose(&self) {
        self.resource.dispose();
        self.lists.dispose();
        self.annotations.dispose();
        self.annotation.dispose();
        self.entity_types.dispose();
        self.tag_schemas.dispose();
        self.engines.dispose();
        self.limits.dispose();
        self.events.dispose();
        locked(&self.annotation_of).clear();
    }
}

impl Drop for Live {
    fn drop(&mut self) {
        self.dispose();
    }
}

/// The engines are out of date, and so is what each key holder reported.
pub(super) fn invalidate_engines(
    engines: &Cache<String, Vec<EngineEntry>>,
    limits: &Cache<&'static str, Vec<EngineLimits>>,
) {
    engines.invalidate(&WHOLE.to_owned());
    for holder in LIMITS_OPERATIONS {
        limits.invalidate(holder);
    }
}

// ── What a query answers from ───────────────────────────────────────────

/// The engines with each key holder's limits joined on.
struct Engines {
    engines: Cache<String, Vec<EngineEntry>>,
    limits: Cache<&'static str, Vec<EngineLimits>>,
}

/// The engines' state, with what the key holders have reported so far: a
/// holder that has not answered delays only its own engines' limits.
struct Joined {
    engines: WatchStream<CacheState<Vec<EngineEntry>>>,
    reports: Vec<WatchStream<CacheState<Vec<EngineLimits>>>>,
    entries: Option<CacheState<Vec<EngineEntry>>>,
    reported: Vec<Vec<EngineLimits>>,
}

impl Stream for Joined {
    type Item = CacheState<Vec<EngineWithLimits>>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let this = &mut *self;
        let mut changed = false;
        loop {
            match Pin::new(&mut this.engines).poll_next(cx) {
                Poll::Ready(Some(state)) => {
                    this.entries = Some(state);
                    changed = true;
                }
                Poll::Ready(None) => return Poll::Ready(None),
                Poll::Pending => break,
            }
        }
        for (report, reported) in this.reports.iter_mut().zip(this.reported.iter_mut()) {
            while let Poll::Ready(Some(state)) = Pin::new(&mut *report).poll_next(cx) {
                *reported = state.ready().cloned().unwrap_or_default();
                changed = true;
            }
        }
        match (&this.entries, changed) {
            (Some(entries), true) => Poll::Ready(Some(
                entries
                    .clone()
                    .map(|entries| joined(entries, &this.reported.concat())),
            )),
            _ => Poll::Pending,
        }
    }
}

impl Source<Vec<EngineWithLimits>> for Engines {
    fn fresh(
        &self,
    ) -> BoxFuture<'static, Result<Vec<EngineWithLimits>, Arc<dyn std::error::Error + Send + Sync>>>
    {
        enum Answer {
            Engines(Result<Vec<EngineEntry>, Arc<dyn std::error::Error + Send + Sync>>),
            Limits(Vec<EngineLimits>),
        }
        // The engines are asked for first; each key holder's limits after them.
        let mut asked: Vec<BoxFuture<'static, Answer>> = vec![Box::pin({
            let engines = self.engines.fetch(&WHOLE.to_owned());
            async move { Answer::Engines(engines.await) }
        })];
        for holder in LIMITS_OPERATIONS {
            let limits = self.limits.fetch(holder);
            asked.push(Box::pin(async move {
                Answer::Limits(limits.await.unwrap_or_default())
            }));
        }
        Box::pin(async move {
            let mut engines = Vec::new();
            let mut reported = Vec::new();
            for answer in all(asked).await {
                match answer {
                    Answer::Engines(entries) => engines = entries?,
                    Answer::Limits(limits) => reported.extend(limits),
                }
            }
            Ok(joined(engines, &reported))
        })
    }

    fn watch(&self) -> Observed<Vec<EngineWithLimits>> {
        Observed::new(
            Joined {
                engines: self.engines.observe(&WHOLE.to_owned()),
                reports: LIMITS_OPERATIONS
                    .iter()
                    .map(|holder| self.limits.observe(holder))
                    .collect(),
                entries: None,
                reported: LIMITS_OPERATIONS.iter().map(|_| Vec::new()).collect(),
            },
            None,
        )
    }

    fn invalidate(&self) {
        invalidate_engines(&self.engines, &self.limits);
    }
}

pub struct BrowseNamespace {
    links: Links,
    content: Arc<dyn ContentTransport>,
    pub(super) live: Arc<Live>,
}

impl BrowseNamespace {
    pub(crate) fn new(
        links: Links,
        content: Arc<dyn ContentTransport>,
        persistence: Option<&CachePersistence>,
    ) -> BrowseNamespace {
        BrowseNamespace {
            live: Live::new(&links, &content, persistence),
            links,
            content,
        }
    }

    /// Whether every cache the client's storage keeps is at rest.
    pub(crate) fn persistence_settled(&self) -> bool {
        let live = &self.live;
        !(live.resource.persistence_pending()
            || live.annotations.persistence_pending()
            || live.annotation.persistence_pending()
            || live.entity_types.persistence_pending()
            || live.tag_schemas.persistence_pending())
    }

    /// End the queries: every watcher's stream ends, and what was owed to
    /// storage is saved.
    pub(crate) fn dispose(&self) {
        self.live.dispose();
    }

    /// A query of one resource: watching it holds the resource's scope.
    fn of_resource<K, V, T>(
        &self,
        cache: &Cache<K, V>,
        key: &K,
        resource_id: &ResourceId,
        view: fn(V) -> T,
    ) -> Cached<T>
    where
        K: CacheKey,
        V: CacheValue,
        T: 'static,
    {
        Cached::of(Keyed {
            cache: cache.clone(),
            key: key.clone(),
            view,
            scope: Some((self.links.transport.clone(), resource_id.clone())),
        })
    }

    /// A query of the knowledge base as a whole.
    fn of_the_whole<V>(&self, cache: &Cache<String, V>) -> Cached<V>
    where
        V: CacheValue,
    {
        Cached::of(Keyed {
            cache: cache.clone(),
            key: WHOLE.to_owned(),
            view: |value| value,
            scope: None,
        })
    }

    // ── Queries ─────────────────────────────────────────────────────────

    pub fn resource(&self, resource_id: &ResourceId) -> Cached<ResourceDescriptor> {
        self.of_resource(&self.live.resource, resource_id, resource_id, |value| value)
    }

    /// A page of the resources `filters` admits. A list is a query's answer:
    /// it is asked for again when a resource is created, updated, cloned or
    /// moved, and when a dropped stream reopens, and not by a change to a
    /// resource whose scope the client does not hold. Finding resources by
    /// text is `match_.resources`.
    pub fn resources(&self, filters: ResourceFilters) -> Cached<ListResourcesResponse> {
        Cached::of(Keyed {
            cache: self.live.lists.clone(),
            key: filters,
            view: |value| value,
            scope: None,
        })
    }

    /// A resource's annotations. Read as content is, and not over the bus.
    pub fn annotations(&self, resource_id: &ResourceId) -> Cached<Vec<Annotation>> {
        self.of_resource(&self.live.annotations, resource_id, resource_id, |list| {
            list.annotations
        })
    }

    pub fn annotation(
        &self,
        resource_id: &ResourceId,
        annotation_id: &AnnotationId,
    ) -> Cached<Annotation> {
        locked(&self.live.annotation_of).insert(annotation_id.clone(), resource_id.clone());
        self.of_resource(&self.live.annotation, annotation_id, resource_id, |value| {
            value
        })
    }

    pub fn entity_types(&self) -> Cached<Vec<String>> {
        self.of_the_whole(&self.live.entity_types)
    }

    pub fn tag_schemas(&self) -> Cached<Vec<TagSchema>> {
        self.of_the_whole(&self.live.tag_schemas)
    }

    /// The knowledge base's engines, asked for first, each with its limits
    /// as the service holding its credentials reports them.
    pub fn engines(&self) -> Cached<Vec<EngineWithLimits>> {
        Cached::of(Engines {
            engines: self.live.engines.clone(),
            limits: self.live.limits.clone(),
        })
    }

    pub fn events(&self, resource_id: &ResourceId) -> Cached<Vec<AttributedEvent>> {
        self.of_resource(&self.live.events, resource_id, resource_id, |value| value)
    }

    // ── One-shot reads ──────────────────────────────────────────────────

    /// A resource's bytes as the text they are
    /// (`resources::decode_representation`): in the charset their content
    /// type states, UTF-8 when it states none, and in the encoding of a
    /// byte-order mark where they begin with one. Without the `charsets`
    /// feature, only when that is UTF-8. A resource whose text is not its
    /// bytes is refused: a PDF, whose text is `resource_anchored_text`'s, and
    /// an image, which has none.
    pub async fn resource_content(&self, resource_id: &ResourceId) -> Result<String, SemiontError> {
        let content = self.content.get_binary(resource_id).await?;
        Ok(decode_representation(
            &content.bytes,
            &content.content_type,
        )?)
    }

    /// A resource's description as linked data: itself and its annotations.
    pub async fn resource_graph(
        &self,
        resource_id: &ResourceId,
    ) -> Result<GetResourceResponse, SemiontError> {
        Ok(self.content.get_resource_graph(resource_id).await?)
    }

    /// A resource's recovered text and the runs that index it, or the named
    /// reason there is none. Read as content is, and not over the bus: it is
    /// as large as the resource.
    pub async fn resource_anchored_text(
        &self,
        resource_id: &ResourceId,
    ) -> Result<AnchoredTextAnswer, SemiontError> {
        Ok(self.content.get_anchored_text(resource_id).await?)
    }

    /// A resource's bytes, unchanged, with their media type.
    pub async fn resource_representation(
        &self,
        resource_id: &ResourceId,
    ) -> Result<Content, SemiontError> {
        Ok(self.content.get_binary(resource_id).await?)
    }

    /// The same, as a stream.
    pub async fn resource_representation_stream(
        &self,
        resource_id: &ResourceId,
    ) -> Result<ContentStream, SemiontError> {
        Ok(self.content.get_binary_stream(resource_id).await?)
    }

    pub async fn resource_events(
        &self,
        resource_id: &ResourceId,
    ) -> Result<Vec<AttributedEvent>, SemiontError> {
        let answer = self
            .links
            .request::<BrowseEventsRequested>(&events_of(resource_id))
            .await?;
        Ok(answer.response.events)
    }

    pub async fn annotation_history(
        &self,
        resource_id: &ResourceId,
        annotation_id: &AnnotationId,
    ) -> Result<GetAnnotationHistoryResponse, SemiontError> {
        let answer = self
            .links
            .request::<BrowseAnnotationHistoryRequested>(&BrowseAnnotationHistoryRequest {
                resource_id: resource_id.clone(),
                annotation_id: annotation_id.clone(),
            })
            .await?;
        Ok(answer.response)
    }

    /// The entries of a directory of the knowledge base's tree: its root
    /// when no path is stated, by name when no order is.
    pub async fn files(
        &self,
        path: Option<&str>,
        sort: Option<BrowseDirectoryRequestSort>,
    ) -> Result<BrowseDirectoryResultResponse, SemiontError> {
        let answer = self
            .links
            .request::<BrowseDirectoryRequested>(&BrowseDirectoryRequest {
                path: path.unwrap_or(".").to_owned(),
                sort: Some(sort.unwrap_or(BrowseDirectoryRequestSort::Name)),
            })
            .await?;
        Ok(answer.response)
    }

    /// What the knowledge base says of itself: its name and domain, and its
    /// working tree's branch. Asked every time: a branch changes with no
    /// event to say so.
    pub async fn kb(&self) -> Result<KbDescription, SemiontError> {
        let answer = self
            .links
            .request::<BrowseKbRequested>(&BrowseKbRequest {})
            .await?;
        Ok(answer.response)
    }

    // ── Signals ─────────────────────────────────────────────────────────

    /// Signal: open an annotation for this viewer.
    pub fn click(&self, annotation_id: &AnnotationId) {
        self.links.signal::<BrowseClick>(
            &BrowseClickEvent {
                annotation_id: annotation_id.clone(),
            },
            Envelope::default(),
        );
    }

    /// Signal: open a resource for this viewer.
    pub fn open_resource(&self, resource_id: &ResourceId) {
        self.links.signal::<BrowseResourceOpen>(
            &BrowseResourceOpenEvent {
                resource_id: resource_id.clone(),
            },
            Envelope::default(),
        );
    }

    /// Report, over the wire, that this viewer arrived at a resource.
    pub fn resource_viewed(&self, resource_id: &ResourceId) {
        self.links
            .report::<BrowseResourceViewed>(&BrowseResourceViewedEvent {
                resource_id: resource_id.clone(),
            });
    }
}