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
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
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
//! A worker's side of the job queue: `job.claim`.
//!
//! A worker is any party that takes jobs and says how each one went. What it
//! promises the dispatcher, and everyone who follows a job, is
//! protocol/contract/WORKER-CONTRACT.md; this module is that contract for Rust,
//! and the worker conformance suite (protocol/conformance/worker) holds it on
//! the wire.
//!
//! THE MODEL. A worker asks the queue at every moment it becomes idle, and
//! never otherwise. `job:claim` carries the jobs it takes; the dispatcher
//! answers with the next pending job that matches one of them, or declines.
//! Queue state is the truth, and no message carries correctness. The idle
//! moments:
//!
//!   - its claims are first read, once the stream is open;
//!   - it settles the job it holds, immediately, with no timer;
//!   - a matching `job:queued` arrives while it holds nothing;
//!   - the stream opens again: every edge into `Open` after the first.
//!
//! `job:queued` is a WAKE-UP with no memory. While the worker holds nothing
//! it causes a claim; while a claim is in flight it sets a bit that earns
//! exactly one more claim, so a wake-up cannot be lost in that window; while
//! a job is held it is ignored, because the settle claims. The check of a
//! wake-up is a PRE-FILTER: an announcement carries the job description less
//! its input, so the worker asks its own claim's question of it,
//! `job_matches_filter`, the comparison the dispatcher makes, and does not
//! spend a round trip to be declined. An announcement the transport refused
//! cannot be compared, so it counts as one that matches.
//!
//! A HELD JOB owns its lifecycle. It says its own start, progress and
//! checkpoints, and it settles once: `complete`, `fail` and `cancel` each
//! take the job by value, say the outcome and release it in one call, so
//! settling twice does not compile. One dropped without being settled is
//! failed.
//!
//! A HELD JOB COMMITS FOR ITSELF. `commit` sends a batch of annotations to
//! the record, citing the job, and returns once the batch is established: the
//! record acknowledged it, or, when no acknowledgement came, answered that
//! the batch's last annotation is on the resource. The job remembers the
//! weakest of what its commits observed and states it when it settles, so a
//! worker says neither which job a batch is for nor how its commits went.
//!
//! The claiming runs on a task of its own, begun when the claims are first
//! read. So a cancellation reaches the held job, a stall is looked for, and
//! every announcement is stamped, while whoever holds the job is busy with
//! the work and reads nothing.
//!
//! EACH JOB HAS A TRACE OF ITS OWN (WORKER-CONTRACT T1). A claim is made on
//! the claiming's own task, where no span of the worker's is current: so it
//! is in no trace, whatever span the job before it was settled in. A held
//! job states the trace its reply arrived in (`trace`), which is the claim's
//! own once the dispatcher has answered in it: a worker opens its span for
//! the job in that trace, so the span, and every message the job sends from
//! inside it, continue the trace that began with the claim.

use crate::bus::{Bus, Operation, payload_of};
use crate::channels::{
    BrowseAnnotationRequested, Channel, JobCancel, JobCancelRequested, JobCheckpoint, JobClaim,
    JobComplete, JobFail, JobQueued, JobReportProgress, JobStart, MarkCommit, Request,
};
use crate::errors::{BusRequestErrorCode, SemiontError, TransportError, TransportErrorCode};
use crate::job_filter::job_matches_filter;
use crate::locked;
use crate::timing::{HELD_JOB_STALL, HELD_JOB_STALL_CHECK, JOB_CLAIM_TIMEOUT, MARK_COMMIT_TIMEOUT};
use crate::transport::{ConnectionState, Envelope, TraceCarrier, Transport};
use crate::types::{
    Annotation, AnnotationId, BrowseAnnotationRequest, DurabilityEvidence, FailureClass,
    JobCancelCommand, JobCheckpointCommand, JobClaimCommand, JobCompleteCommand, JobFailCommand,
    JobFilter, JobId, JobMetadata, JobParams, JobProgress, JobReportProgressCommand, JobRunning,
    JobStartCommand, JobType, MarkCommitCommand, MarkJobCompleteCommand, MarkJobResult, ResourceId,
    UnitCursor, YieldJobCompleteCommand, YieldJobResult,
};
use serde_json::Value;
use std::collections::BTreeMap;
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime};
use tokio::sync::{Notify, mpsc, watch};
use tokio::time::{Instant, MissedTickBehavior};

/// What a worker's stream names for its claims: the replies of `job:claim`,
/// and the two broadcasts a worker reads. `job:queued` and
/// `job:cancel-requested` reach only a stream that names them, so a transport
/// made for a worker is given these beside the reply channels of whatever
/// else the worker awaits.
pub const JOB_CLAIM_CHANNELS: [&str; 4] = [
    <<JobClaim as Request>::Result as Channel>::NAME,
    <<JobClaim as Request>::Failure as Channel>::NAME,
    JobQueued::NAME,
    JobCancelRequested::NAME,
];

/// What a worker's stream names for its commits: the replies of `mark:commit`
/// and of the question it asks when one goes unacknowledged.
///
/// The question is the read of ONE annotation, and not of a resource's list
/// of them. Reply channels reach every stream that names them, and the list's
/// replies are the frames of many megabytes a worker's stream exists to keep
/// out; one annotation's frame is small.
pub const JOB_COMMIT_CHANNELS: [&str; 4] = [
    <<MarkCommit as Request>::Result as Channel>::NAME,
    <<MarkCommit as Request>::Failure as Channel>::NAME,
    <<BrowseAnnotationRequested as Request>::Result as Channel>::NAME,
    <<BrowseAnnotationRequested as Request>::Failure as Channel>::NAME,
];

/// How weak an observation of a commit is, as evidence that the batch is on
/// the record. The two a commit is not established by are equally weak: one
/// says the record answered that the annotation is not there, the other that
/// nobody answered, and neither says more than the other.
const fn weakness(evidence: DurabilityEvidence) -> u8 {
    match evidence {
        DurabilityEvidence::Acknowledged => 0,
        DurabilityEvidence::ProbeConfirmed => 1,
        DurabilityEvidence::ProbeRefused | DurabilityEvidence::ProbeUnreachable => 2,
    }
}

/// Whether a failed attempt is retried: exactly when its class is one the
/// queue retries and the job has retries left, on the record before the
/// failure is applied. A failure known to be deterministic is not retried:
/// the same request cannot succeed on a second attempt. Neither is an answer
/// the provider withheld: it withholds its answer to the same request again.
/// A failure of no stated class is retried as a transient one is. Every class
/// the spec names is answered here, and one it comes to name does not compile
/// until it is.
///
/// Two places need the answer and they must never disagree: the dispatcher's
/// queue acts on it, and a worker reports it on `job:fail` as `willRetry`, so
/// a follower of the job knows whether the failure it just saw is the end.
/// This is the one implementation in Rust, which the Dispatcher uses too, and
/// protocol/specs/src/jobs/retry-cases.json is the table every language answers alike.
pub fn will_retry_after(metadata: &JobMetadata, failure_class: Option<FailureClass>) -> bool {
    let retried = match failure_class {
        None | Some(FailureClass::Transient) => true,
        Some(FailureClass::Deterministic | FailureClass::Withheld) => false,
    };
    retried && metadata.retry_count < metadata.max_retries
}

/// The waits a worker keeps, each the value of protocol/specs/src/client/timing.json
/// unless a caller that must not wait it out states another.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ClaimTiming {
    /// How long a claim waits for its answer.
    pub job_claim: Duration,
    /// How long a held job may show no activity before it is stalled.
    pub held_job_stall: Duration,
    /// How often a held job is looked at for a stall.
    pub held_job_stall_check: Duration,
    /// How long a commit waits for the record to acknowledge it, and then,
    /// when no acknowledgement came, for the answer to its question.
    pub mark_commit: Duration,
}

impl Default for ClaimTiming {
    fn default() -> ClaimTiming {
        ClaimTiming {
            job_claim: JOB_CLAIM_TIMEOUT,
            held_job_stall: HELD_JOB_STALL,
            held_job_stall_check: HELD_JOB_STALL_CHECK,
            mark_commit: MARK_COMMIT_TIMEOUT,
        }
    }
}

/// What a worker claims with.
#[derive(Debug, Clone)]
pub struct ClaimOptions {
    /// The jobs this worker takes: the claim's `accepts`, and what a
    /// `job:queued` is checked against. At least one.
    pub accepts: Vec<JobFilter>,
    pub timing: ClaimTiming,
}

impl ClaimOptions {
    /// Claim the jobs `accepts` describes, waiting as the table says.
    pub fn new(accepts: Vec<JobFilter>) -> ClaimOptions {
        ClaimOptions {
            accepts,
            timing: ClaimTiming::default(),
        }
    }
}

/// A claim that was refused for a reason other than "nothing pending".
///
/// `code` is the code the reply was given, or `None` when the refusal was
/// the worker's own: a reply that names no job, or a claim that could not be
/// sent. `NonePending` never appears: an empty queue is not a fault.
/// `Unauthorized` means this credential cannot claim, and will not be able to
/// later.
#[derive(Debug, Clone, PartialEq)]
pub struct ClaimRefusal {
    pub code: Option<BusRequestErrorCode>,
    pub message: String,
}

/// The job a worker holds, as its vitals name it.
#[derive(Debug, Clone, PartialEq)]
pub struct ActiveJob {
    pub job_id: JobId,
    pub job_type: JobType,
    pub since: SystemTime,
}

/// What a worker can say of itself at any moment (WORKER-CONTRACT V1).
///
/// `last_queued_event_at` is any `job:queued` received, matching or not, one
/// the transport refused among them. On
/// an idle stack with an empty queue it stands still by design, so a still
/// stamp alone is not a fault of the stream. `last_activity_at` (a claim, a
/// progress report, a checkpoint, a settle) is the liveness of the work: a
/// job stuck partway stops advancing it, and that is what the stall rule
/// reads.
#[derive(Debug, Clone, PartialEq)]
pub struct WorkerVitals {
    pub last_queued_event_at: Option<SystemTime>,
    pub last_claim_at: Option<SystemTime>,
    /// The last settle, whatever its outcome: a worker that fails and moves
    /// on is alive.
    pub last_finished_at: Option<SystemTime>,
    pub last_activity_at: Option<SystemTime>,
    pub active_job: Option<ActiveJob>,
    pub jobs_completed: u64,
}

/// A held job that showed no activity for `threshold` (WORKER-CONTRACT V2).
/// What its host does then is the host's.
#[derive(Debug, Clone, PartialEq)]
pub struct HeldJobStall {
    pub job_id: JobId,
    pub job_type: JobType,
    pub held_since: SystemTime,
    pub last_activity_at: SystemTime,
    pub silent_for: Duration,
    pub threshold: Duration,
}

/// What a failure carries beside its error. What the job's commits observed
/// is the held job's own to state.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct JobFailure {
    /// The failure's class, when the worker knows it.
    pub failure_class: Option<FailureClass>,
    /// The units finished before the failure.
    pub completed_units: Option<Vec<String>>,
    /// The furthest each unit begun got, a finished unit's cursor where it
    /// ended.
    pub unit_cursors: Option<BTreeMap<String, UnitCursor>>,
}

/// The error of a job failed because its worker stopped, and not because the
/// work failed.
const STOPPED_WHILE_HELD: &str = "The worker stopped while it held the job";
/// The error of a job whose handle was dropped before it was settled.
const DROPPED_UNSETTLED: &str = "The worker let go of the job without settling it";

/// A failure this side made: nothing was sent, or what was sent got no
/// further.
fn here(message: String) -> SemiontError {
    TransportError::without_response(message, TransportErrorCode::Error).into()
}

// ── What the loop and a held job share ──────────────────────────────────

/// A held job as its loop knows it: what its messages name it by, its
/// budget, and whether it has been settled.
struct Holding {
    resource_id: ResourceId,
    job_id: JobId,
    job_type: JobType,
    /// Which attempt this is, 1-based.
    attempt: i64,
    annotation_id: Option<AnnotationId>,
    budget: JobMetadata,
    /// The trace the reply that handed the job over arrived in.
    trace: Option<TraceCarrier>,
    /// Settled once, by whoever is first: the handle, the worker's stop, or
    /// the handle's drop.
    settled: AtomicBool,
    cancelled: watch::Sender<bool>,
    /// The weakest of what this job's commits observed, across every batch
    /// and every resource it committed on: the strongest thing still true of
    /// the job as a whole. None until a batch is committed, and never a
    /// default: a job that commits nothing states nothing.
    durability: Mutex<Option<DurabilityEvidence>>,
}

impl Holding {
    /// Remember `evidence` if it is weaker than what is remembered. Of two
    /// equally weak, the first seen is kept.
    fn observe(&self, evidence: DurabilityEvidence) {
        let mut remembered = locked(&self.durability);
        if remembered.is_none_or(|kept| weakness(evidence) > weakness(kept)) {
            *remembered = Some(evidence);
        }
    }

    /// What this job's commits observed, as its completion states it.
    fn observed(&self) -> Option<DurabilityEvidence> {
        *locked(&self.durability)
    }

    fn fail(&self, error: &str, failure: JobFailure) -> JobFailCommand {
        let will_retry = will_retry_after(&self.budget, failure.failure_class);
        JobFailCommand {
            attempt: Some(self.attempt),
            annotation_id: self.annotation_id.clone(),
            completed_units: failure.completed_units,
            unit_cursors: failure.unit_cursors,
            failure_class: failure.failure_class,
            will_retry: Some(will_retry),
            // Stated only when a commit was not established: what is weaker
            // than any observation that establishes one. Otherwise the
            // failure says nothing of the job's commits.
            durability: self.observed().filter(|observed| {
                weakness(*observed) > weakness(DurabilityEvidence::ProbeConfirmed)
            }),
            ..JobFailCommand::new(
                self.resource_id.clone(),
                self.job_id.clone(),
                self.job_type,
                error,
            )
        }
    }
}

#[derive(Default)]
struct State {
    held: Option<Arc<Holding>>,
    /// Whether a stall has been reported for the job held: one is reported once.
    stall_reported: bool,
    last_queued_event_at: Option<SystemTime>,
    last_claim_at: Option<SystemTime>,
    last_finished_at: Option<SystemTime>,
    /// When the work was last active, by the clock a silence is measured on
    /// and by the one it is told in.
    last_activity: Option<(Instant, SystemTime)>,
    held_since: Option<SystemTime>,
    jobs_completed: u64,
}

struct Inner {
    transport: Arc<dyn Transport>,
    wire: Bus,
    accepts: Vec<JobFilter>,
    timing: ClaimTiming,
    state: Mutex<State>,
    /// An idle moment the loop cannot see for itself: a settle.
    settled: Notify,
    stall: watch::Sender<Option<HeldJobStall>>,
}

impl Inner {
    fn holding(&self) -> bool {
        locked(&self.state).held.is_some()
    }

    /// The work showed it is alive.
    fn active(&self) {
        locked(&self.state).last_activity = Some((Instant::now(), SystemTime::now()));
    }

    /// One of a commit's requests, waited on for `mark_commit`. It fails as a
    /// bus request fails. What answers it is not read: that an answer came
    /// on its result channel is all a commit asks, so the answer is not held
    /// to its type here.
    async fn ask<R: Request>(&self, payload: &R::Payload) -> Result<(), SemiontError> {
        self.wire
            .request_of(
                &Operation::of::<R>(),
                payload_of(payload)?,
                self.timing.mark_commit,
            )
            .await?;
        Ok(())
    }

    /// Is the annotation on the resource? Asked of the LAST annotation of a
    /// batch nobody acknowledged, and that is enough: the record appends a
    /// batch in order and stops at the first annotation it cannot append
    /// (WORKER-CONTRACT A5), so the last being there says every one before
    /// it is. One question, where asking of each would be a round trip for
    /// each annotation.
    ///
    /// Every answer but the annotation fails the commit, and the asymmetry is
    /// deliberate. The record appends only the annotations it does not hold,
    /// so a job retried over a batch that had landed costs one more run of
    /// the batch's unit; a wrong "it is there" loses the batch silently,
    /// which is what the acknowledgement exists to prevent. A question nobody
    /// answered is neither yes nor no. It is said as its own observation, and
    /// it does not establish the commit.
    async fn ask_whether_recorded(
        &self,
        resource_id: &ResourceId,
        annotation_id: AnnotationId,
    ) -> DurabilityEvidence {
        let question = BrowseAnnotationRequest {
            resource_id: resource_id.clone(),
            annotation_id,
        };
        match self.ask::<BrowseAnnotationRequested>(&question).await {
            Ok(()) => DurabilityEvidence::ProbeConfirmed,
            // A failure reply (`Rejected`) means the question was answered
            // and the answer was not the annotation. That is not "the
            // annotation is absent": a read that failed for its own reasons
            // answers on the same channel. So the job says what was observed,
            // and its reader judges.
            Err(SemiontError::Bus(answered)) if answered.code == BusRequestErrorCode::Rejected => {
                DurabilityEvidence::ProbeRefused
            }
            // Anything else (a timeout, a closed bus) means nobody answered.
            Err(_) => DurabilityEvidence::ProbeUnreachable,
        }
    }

    /// `holding` is the job this worker holds, from now.
    fn hold(&self, holding: &Arc<Holding>) {
        let now = SystemTime::now();
        let mut state = locked(&self.state);
        state.held = Some(holding.clone());
        state.stall_reported = false;
        state.last_claim_at = Some(now);
        state.last_activity = Some((Instant::now(), now));
        state.held_since = Some(now);
    }

    /// `holding` is settled, and no longer held.
    fn released(&self, holding: &Arc<Holding>, completed: bool) {
        {
            let mut state = locked(&self.state);
            if !state
                .held
                .as_ref()
                .is_some_and(|held| Arc::ptr_eq(held, holding))
            {
                return;
            }
            let now = SystemTime::now();
            state.held = None;
            state.held_since = None;
            state.last_finished_at = Some(now);
            state.last_activity = Some((Instant::now(), now));
            if completed {
                state.jobs_completed += 1;
            }
        }
        self.settled.notify_one();
    }

    /// Fail `holding` for a reason that is not the work's, unless it is
    /// settled already. The failure states no class, so the record's retry
    /// budget decides what becomes of the job.
    async fn fail_held(&self, holding: &Arc<Holding>, error: &str) {
        if holding.settled.swap(true, Ordering::SeqCst) {
            return;
        }
        let failed = holding.fail(error, JobFailure::default());
        let _ = self
            .wire
            .emit::<JobFail>(&failed, Envelope::default())
            .await;
        self.released(holding, false);
    }

    /// A cancellation is the held job's only when it names it, and one that
    /// arrives after the settle signals nothing.
    fn signal_cancellation(&self, job_id: &str) {
        let held = locked(&self.state).held.clone();
        if let Some(holding) = held
            && holding.job_id.as_str() == job_id
            && !holding.settled.load(Ordering::SeqCst)
        {
            holding.cancelled.send_replace(true);
        }
    }

    fn look_for_stall(&self) {
        let stall = {
            let mut state = locked(&self.state);
            let (Some(holding), Some((active, told)), Some(since)) =
                (state.held.clone(), state.last_activity, state.held_since)
            else {
                return;
            };
            let silent_for = active.elapsed();
            if state.stall_reported || silent_for <= self.timing.held_job_stall {
                return;
            }
            state.stall_reported = true;
            HeldJobStall {
                job_id: holding.job_id.clone(),
                job_type: holding.job_type,
                held_since: since,
                last_activity_at: told,
                silent_for,
                threshold: self.timing.held_job_stall,
            }
        };
        self.stall.send_replace(Some(stall));
    }

    fn vitals(&self) -> WorkerVitals {
        let state = locked(&self.state);
        WorkerVitals {
            last_queued_event_at: state.last_queued_event_at,
            last_claim_at: state.last_claim_at,
            last_finished_at: state.last_finished_at,
            last_activity_at: state.last_activity.map(|(_, told)| told),
            active_job: match (&state.held, state.held_since) {
                (Some(holding), Some(since)) => Some(ActiveJob {
                    job_id: holding.job_id.clone(),
                    job_type: holding.job_type,
                    since,
                }),
                _ => None,
            },
            jobs_completed: state.jobs_completed,
        }
    }
}

// ── The held job ────────────────────────────────────────────────────────

/// What a held job is made of, whatever its verb. Dropped without having
/// been settled, it fails its job: the queue retries it at once if its budget
/// allows, where a job nobody answered for would stay `running` until the
/// dispatcher's sweep.
struct Core {
    inner: Arc<Inner>,
    holding: Arc<Holding>,
    params: JobParams,
    completed_units: Vec<String>,
    unit_cursors: BTreeMap<String, UnitCursor>,
    /// Whether the job has said anything: `job:start` is its first message.
    begun: AtomicBool,
}

impl Core {
    fn unsettled(&self, saying: &str) -> Result<(), SemiontError> {
        if self.holding.settled.load(Ordering::SeqCst) {
            return Err(here(format!(
                "Job {} is already settled: it cannot say {saying}",
                self.holding.job_id
            )));
        }
        Ok(())
    }

    /// Say the outcome and release the job, together. The job is released
    /// whether or not the gateway took the message, because a worker that
    /// could not say its outcome must still go on claiming: the dispatcher's
    /// sweep concludes a job whose outcome never arrived.
    async fn settle<C: Channel>(
        self,
        payload: C::Payload,
        completed: bool,
    ) -> Result<(), SemiontError> {
        if self.holding.settled.swap(true, Ordering::SeqCst) {
            // Its worker stopped, and failed it, while the work went on.
            return Err(here(format!(
                "Job {} is already settled: it cannot say {}",
                self.holding.job_id,
                C::NAME
            )));
        }
        let sent = self
            .inner
            .wire
            .emit::<C>(&payload, Envelope::default())
            .await;
        self.inner.released(&self.holding, completed);
        sent.map(|_| ()).map_err(SemiontError::from)
    }
}

impl Drop for Core {
    fn drop(&mut self) {
        if self.holding.settled.load(Ordering::SeqCst) {
            return;
        }
        let (inner, holding) = (self.inner.clone(), self.holding.clone());
        match tokio::runtime::Handle::try_current() {
            Ok(runtime) => {
                runtime.spawn(async move {
                    inner.fail_held(&holding, DROPPED_UNSETTLED).await;
                });
            }
            // Nothing is left to say it with. The job is released, and the
            // dispatcher's sweep concludes it.
            Err(_) => {
                if !holding.settled.swap(true, Ordering::SeqCst) {
                    inner.released(&holding, false);
                }
            }
        }
    }
}

/// A `mark` job's verb, as a held job is typed by it.
pub struct Mark;
/// A `yield` job's verb, as a held job is typed by it.
pub struct Yield;

/// A job this worker holds, of the verb `V`, from its claim until it settles
/// it. `complete`, `fail` and `cancel` take it by value: each says the outcome
/// and releases the job together, and there is no settling it twice. A settle
/// the gateway did not take still releases the job, and is the error
/// returned.
pub struct Held<V> {
    core: Core,
    verb: PhantomData<fn() -> V>,
}

/// A `mark` job this worker holds.
pub type HeldMarkJob = Held<Mark>;
/// A `yield` job this worker holds.
pub type HeldYieldJob = Held<Yield>;

impl<V> Held<V> {
    pub fn job_id(&self) -> &JobId {
        &self.core.holding.job_id
    }

    pub fn job_type(&self) -> JobType {
        self.core.holding.job_type
    }

    pub fn resource_id(&self) -> &ResourceId {
        &self.core.holding.resource_id
    }

    /// The job's parameters as the dispatcher holds them: the description,
    /// and what the dispatcher adds.
    pub fn params(&self) -> &JobParams {
        &self.core.params
    }

    /// The units earlier attempts finished. A worker does not do them again.
    /// Empty on a first attempt.
    pub fn completed_units(&self) -> &[String] {
        &self.core.completed_units
    }

    /// The furthest each unit begun got on an earlier attempt, and what it
    /// had counted there: a finished unit's is where it ended. Empty on a
    /// first attempt.
    pub fn unit_cursors(&self) -> &BTreeMap<String, UnitCursor> {
        &self.core.unit_cursors
    }

    pub fn retry_count(&self) -> u64 {
        self.core.holding.budget.retry_count
    }

    pub fn max_retries(&self) -> u64 {
        self.core.holding.budget.max_retries
    }

    /// Which attempt this is, 1-based. Every lifecycle message states it.
    pub fn attempt(&self) -> i64 {
        self.core.holding.attempt
    }

    /// The annotation the job is anchored to: the one a `yield` job's
    /// context is focused on.
    pub fn annotation_id(&self) -> Option<&AnnotationId> {
        self.core.holding.annotation_id.as_ref()
    }

    /// The trace the job is run in: the one its reply arrived in. `None` for
    /// a reply that arrived in none.
    pub fn trace(&self) -> Option<&TraceCarrier> {
        self.core.holding.trace.as_ref()
    }

    /// Whether a cancellation has named this job: the current answer, and
    /// the change to it. The work stops where it can, and the worker says
    /// `cancel`.
    pub fn cancelled(&self) -> watch::Receiver<bool> {
        self.core.holding.cancelled.subscribe()
    }

    /// `job:start`: the job's first message, said once.
    pub async fn start(&self) -> Result<(), SemiontError> {
        self.core.unsettled(JobStart::NAME)?;
        if self.core.begun.swap(true, Ordering::SeqCst) {
            return Err(here(format!(
                "job:start is a held job's first message, said once: job {} has already said more",
                self.job_id()
            )));
        }
        let holding = &self.core.holding;
        let started = JobStartCommand {
            annotation_id: holding.annotation_id.clone(),
            attempt: Some(holding.attempt),
            ..JobStartCommand::new(
                holding.resource_id.clone(),
                holding.job_id.clone(),
                holding.job_type,
            )
        };
        self.core
            .inner
            .wire
            .emit::<JobStart>(&started, Envelope::default())
            .await?;
        Ok(())
    }

    /// `job:report-progress`. Counts as activity.
    pub async fn progress(&self, progress: JobProgress) -> Result<(), SemiontError> {
        self.core.unsettled(JobReportProgress::NAME)?;
        self.core.begun.store(true, Ordering::SeqCst);
        self.core.inner.active();
        let holding = &self.core.holding;
        let percentage = progress.percentage;
        let reported = JobReportProgressCommand {
            attempt: Some(holding.attempt),
            annotation_id: holding.annotation_id.clone(),
            progress: Some(JobProgress {
                annotation_id: holding
                    .annotation_id
                    .clone()
                    .or(progress.annotation_id.clone()),
                ..progress
            }),
            ..JobReportProgressCommand::new(
                holding.resource_id.clone(),
                holding.job_id.clone(),
                holding.job_type,
                percentage,
            )
        };
        self.core
            .inner
            .wire
            .emit::<JobReportProgress>(&reported, Envelope::default())
            .await?;
        Ok(())
    }

    /// `job:checkpoint`: what a later attempt resumes from. The units
    /// finished, and the furthest each unit begun got, a finished unit's
    /// cursor where it ended. Counts as activity.
    pub async fn checkpoint(
        &self,
        completed_units: Vec<String>,
        unit_cursors: Option<BTreeMap<String, UnitCursor>>,
    ) -> Result<(), SemiontError> {
        self.core.unsettled(JobCheckpoint::NAME)?;
        self.core.begun.store(true, Ordering::SeqCst);
        self.core.inner.active();
        let reached = JobCheckpointCommand {
            unit_cursors,
            ..JobCheckpointCommand::new(self.job_id().clone(), completed_units)
        };
        self.core
            .inner
            .wire
            .emit::<JobCheckpoint>(&reached, Envelope::default())
            .await?;
        Ok(())
    }

    /// `mark:commit` for this job: a batch of annotations on `resource_id`.
    /// It returns `Ok` once the batch is established.
    ///
    /// It sends the batch to the record and WAITS for the record to say it
    /// has it. The gateway taking the message says nothing of the record: a
    /// record that is down discards a batch the gateway accepted, and a job
    /// that counted the batch as done would report work that never landed.
    ///
    /// A commit the record does not acknowledge in time is not thereby lost.
    /// If the gateway goes down after the record appended the batch, the
    /// acknowledgement cannot be routed, and a job failed on that would be
    /// failed over annotations that are on the record. So the outcome follows
    /// what the record holds, and not the arrival of a message: the record
    /// is asked. A batch is never sent a second time to find out. That would
    /// double the work, and where the acknowledgement was lost because the
    /// gateway is down, the second commit would only time out as the first
    /// did.
    ///
    /// A commit that was not established fails as its unanswered request
    /// did, with that request's own failure. What was observed leaves by the
    /// job's settle, the one place it can still be told.
    pub async fn commit(
        &self,
        resource_id: &ResourceId,
        annotations: Vec<Annotation>,
    ) -> Result<(), SemiontError> {
        self.core.unsettled(MarkCommit::NAME)?;
        // A batch of no annotations is no commit: there is nothing to establish.
        let Some(last) = annotations.last().map(|last| last.id.clone()) else {
            return Ok(());
        };
        let (inner, holding) = (&self.core.inner, &self.core.holding);
        let batch = MarkCommitCommand {
            job_id: Some(holding.job_id.clone()),
            ..MarkCommitCommand::new(resource_id.clone(), annotations)
        };
        let unanswered = match inner.ask::<MarkCommit>(&batch).await {
            Ok(()) => {
                holding.observe(DurabilityEvidence::Acknowledged);
                return Ok(());
            }
            // The record's refusal, and every other failure of the request,
            // is the commit's failure as it is. Only an acknowledgement that
            // did not arrive leaves what the record holds unknown.
            Err(SemiontError::Bus(unanswered))
                if unanswered.code == BusRequestErrorCode::Timeout =>
            {
                unanswered
            }
            Err(failed) => return Err(failed),
        };
        let observed = inner.ask_whether_recorded(resource_id, last).await;
        holding.observe(observed);
        if observed == DurabilityEvidence::ProbeConfirmed {
            Ok(())
        } else {
            Err(unanswered.into())
        }
    }

    /// Settle: `job:fail`. It says whether the queue will retry, from the
    /// record's budget and the failure's class, and what a commit that was
    /// not established observed.
    pub async fn fail(
        self,
        error: impl Into<String>,
        failure: JobFailure,
    ) -> Result<(), SemiontError> {
        let failed = self.core.holding.fail(&error.into(), failure);
        self.core.settle::<JobFail>(failed, false).await
    }

    /// Settle: `job:cancel`, once the work has stopped for a cancellation,
    /// with the units it finished, and the furthest each unit begun got, a
    /// finished unit's cursor where it ended.
    pub async fn cancel(
        self,
        completed_units: Option<Vec<String>>,
        unit_cursors: Option<BTreeMap<String, UnitCursor>>,
    ) -> Result<(), SemiontError> {
        let holding = &self.core.holding;
        let cancelled = JobCancelCommand {
            annotation_id: holding.annotation_id.clone(),
            completed_units,
            unit_cursors,
            ..JobCancelCommand::new(
                holding.resource_id.clone(),
                holding.job_id.clone(),
                holding.job_type,
            )
        };
        self.core.settle::<JobCancel>(cancelled, false).await
    }
}

impl Held<Mark> {
    /// Settle: `job:complete`, with what a `mark` job reports, and how its
    /// commits were established.
    pub async fn complete(self, result: MarkJobResult) -> Result<(), SemiontError> {
        let holding = &self.core.holding;
        let completed = MarkJobCompleteCommand {
            attempt: Some(holding.attempt),
            result: Some(result),
            durability: holding.observed(),
            ..MarkJobCompleteCommand::new(holding.resource_id.clone(), holding.job_id.clone())
        };
        self.core
            .settle::<JobComplete>(JobCompleteCommand::MarkJobCompleteCommand(completed), true)
            .await
    }
}

impl Held<Yield> {
    /// Settle: `job:complete`, with what a `yield` job reports, and how its
    /// commits were established.
    pub async fn complete(self, result: YieldJobResult) -> Result<(), SemiontError> {
        let holding = &self.core.holding;
        let completed = YieldJobCompleteCommand {
            attempt: Some(holding.attempt),
            annotation_id: holding.annotation_id.clone(),
            result: Some(result),
            durability: holding.observed(),
            ..YieldJobCompleteCommand::new(holding.resource_id.clone(), holding.job_id.clone())
        };
        self.core
            .settle::<JobComplete>(JobCompleteCommand::YieldJobCompleteCommand(completed), true)
            .await
    }
}

/// A job this worker holds, as its verb's: a completion is its verb's, so
/// the verb is matched before `complete` is called. What every held job has,
/// whatever its verb, is also here.
pub enum HeldJob {
    Mark(HeldMarkJob),
    Yield(HeldYieldJob),
}

/// The same call on a held job of either verb.
macro_rules! either {
    ($held:expr, $job:ident => $call:expr) => {
        match $held {
            HeldJob::Mark($job) => $call,
            HeldJob::Yield($job) => $call,
        }
    };
}

impl HeldJob {
    /// Whether the job is settled already: its worker stopped, and failed
    /// it, before it was handed out.
    fn settled(&self) -> bool {
        either!(self, job => job.core.holding.settled.load(Ordering::SeqCst))
    }

    pub fn job_id(&self) -> &JobId {
        either!(self, job => job.job_id())
    }

    pub fn job_type(&self) -> JobType {
        either!(self, job => job.job_type())
    }

    pub fn resource_id(&self) -> &ResourceId {
        either!(self, job => job.resource_id())
    }

    pub fn params(&self) -> &JobParams {
        either!(self, job => job.params())
    }

    pub fn completed_units(&self) -> &[String] {
        either!(self, job => job.completed_units())
    }

    pub fn unit_cursors(&self) -> &BTreeMap<String, UnitCursor> {
        either!(self, job => job.unit_cursors())
    }

    pub fn retry_count(&self) -> u64 {
        either!(self, job => job.retry_count())
    }

    pub fn max_retries(&self) -> u64 {
        either!(self, job => job.max_retries())
    }

    pub fn attempt(&self) -> i64 {
        either!(self, job => job.attempt())
    }

    pub fn annotation_id(&self) -> Option<&AnnotationId> {
        either!(self, job => job.annotation_id())
    }

    pub fn trace(&self) -> Option<&TraceCarrier> {
        either!(self, job => job.trace())
    }

    pub fn cancelled(&self) -> watch::Receiver<bool> {
        either!(self, job => job.cancelled())
    }

    pub async fn start(&self) -> Result<(), SemiontError> {
        either!(self, job => job.start().await)
    }

    pub async fn progress(&self, progress: JobProgress) -> Result<(), SemiontError> {
        either!(self, job => job.progress(progress).await)
    }

    pub async fn checkpoint(
        &self,
        completed_units: Vec<String>,
        unit_cursors: Option<BTreeMap<String, UnitCursor>>,
    ) -> Result<(), SemiontError> {
        either!(self, job => job.checkpoint(completed_units, unit_cursors).await)
    }

    pub async fn commit(
        &self,
        resource_id: &ResourceId,
        annotations: Vec<Annotation>,
    ) -> Result<(), SemiontError> {
        either!(self, job => job.commit(resource_id, annotations).await)
    }

    pub async fn fail(
        self,
        error: impl Into<String>,
        failure: JobFailure,
    ) -> Result<(), SemiontError> {
        either!(self, job => job.fail(error, failure).await)
    }

    pub async fn cancel(
        self,
        completed_units: Option<Vec<String>>,
        unit_cursors: Option<BTreeMap<String, UnitCursor>>,
    ) -> Result<(), SemiontError> {
        either!(self, job => job.cancel(completed_units, unit_cursors).await)
    }
}

/// The annotation a job is anchored to: the one a `yield` job's context is
/// focused on. A `yield` job focused on a resource has none, and neither has
/// a `mark` job.
fn anchor_of(job_type: JobType, params: &JobParams) -> Option<AnnotationId> {
    if job_type != JobType::Yield {
        return None;
    }
    let focus = params.rest.get("context")?.get("focus")?;
    if focus.get("kind")?.as_str()? != "annotation" {
        return None;
    }
    focus.get("annotation")?.get("id")?.as_str()?.parse().ok()
}

/// Hold the job a claim was answered with, as its verb's, in the `trace`
/// its reply arrived in.
fn held(
    inner: &Arc<Inner>,
    claimed: JobRunning,
    trace: Option<TraceCarrier>,
) -> (Arc<Holding>, HeldJob) {
    let JobRunning {
        metadata, params, ..
    } = claimed;
    let holding = Arc::new(Holding {
        resource_id: params.resource_id.clone(),
        job_id: metadata.id.clone(),
        job_type: metadata.r#type,
        attempt: i64::try_from(metadata.retry_count)
            .unwrap_or(i64::MAX)
            .saturating_add(1),
        annotation_id: anchor_of(metadata.r#type, &params),
        trace,
        settled: AtomicBool::new(false),
        cancelled: watch::channel(false).0,
        durability: Mutex::new(None),
        budget: metadata.clone(),
    });
    let core = Core {
        inner: inner.clone(),
        holding: holding.clone(),
        params,
        // Both appear on the record once an attempt has checkpointed. Absent,
        // each reads as none.
        completed_units: metadata.completed_units.unwrap_or_default(),
        unit_cursors: metadata.unit_cursors.unwrap_or_default(),
        begun: AtomicBool::new(false),
    };
    let job = match holding.job_type {
        JobType::Mark => HeldJob::Mark(Held {
            core,
            verb: PhantomData,
        }),
        JobType::Yield => HeldJob::Yield(Held {
            core,
            verb: PhantomData,
        }),
    };
    (holding, job)
}

// ── The loop ────────────────────────────────────────────────────────────

enum ClaimOutcome {
    Job(Arc<Holding>, Box<HeldJob>),
    Declined,
    Refused(ClaimRefusal),
}

/// Ask once. A claim names fields of the job description, never a job id. A
/// reply that does not read as a claimed job is refused here and never run
/// (WORKER-CONTRACT C9): it names no job id, no job type or no parameters.
async fn claim_next(inner: Arc<Inner>) -> ClaimOutcome {
    let claim = JobClaimCommand::new(inner.accepts.clone());
    match inner
        .wire
        .result::<JobClaim>(&claim, inner.timing.job_claim)
        .await
    {
        Ok(claimed) => {
            let (holding, job) = held(&inner, claimed.payload.response, claimed.trace);
            ClaimOutcome::Job(holding, Box::new(job))
        }
        Err(SemiontError::Bus(refused)) if refused.code == BusRequestErrorCode::NonePending => {
            ClaimOutcome::Declined
        }
        Err(SemiontError::Bus(refused)) => ClaimOutcome::Refused(ClaimRefusal {
            code: Some(refused.code),
            message: refused.message,
        }),
        Err(other) => ClaimOutcome::Refused(ClaimRefusal {
            code: None,
            message: other.to_string(),
        }),
    }
}

type Claiming = Pin<Box<dyn Future<Output = ClaimOutcome> + Send>>;

/// The claim in flight, answered; or never, when none is.
async fn answered(claim: &mut Option<Claiming>) -> ClaimOutcome {
    match claim {
        Some(claim) => claim.as_mut().await,
        None => std::future::pending().await,
    }
}

type Handed = Result<HeldJob, ClaimRefusal>;

/// One worker's claiming, from the first read of its claims until it stops,
/// its reader is gone, or its stream has ended.
async fn run(inner: Arc<Inner>, out: mpsc::Sender<Handed>, mut stop: watch::Receiver<bool>) {
    let transport = inner.transport.clone();
    // A worker on a stream that does not name these would never be answered,
    // woken or told of a cancellation, with nothing to show for it.
    let unnamed: Vec<&str> = JOB_CLAIM_CHANNELS
        .iter()
        .copied()
        .filter(|channel| !transport.is_subscribed(channel))
        .collect();
    // Both broadcasts are read as they come, and not as this SDK types them:
    // the comparison of an announcement with a filter knows no field by
    // name, so a job this SDK cannot type still wakes a worker whose claim it
    // matches.
    let streams = (
        inner.wire.frames_on(JobQueued::NAME),
        inner.wire.frames_on(JobCancelRequested::NAME),
    );
    let (mut queued, mut cancels) = match streams {
        (Ok(queued), Ok(cancels)) if unnamed.is_empty() => (queued, cancels),
        _ => {
            let _ = out
                .send(Err(ClaimRefusal {
                    code: Some(BusRequestErrorCode::Unsubscribed),
                    message: format!(
                        "This transport's stream does not name {}: a worker on it would never be answered, woken or told of a cancellation. Give the transport JOB_CLAIM_CHANNELS.",
                        if unnamed.is_empty() { JOB_CLAIM_CHANNELS.join(", ") } else { unnamed.join(", ") }
                    ),
                }))
                .await;
            return;
        }
    };

    // The stream opening again is an edge into `Open` after the first
    // observation. The first observation decides whether the first claim is
    // made now or waits for the stream to open: a claim on a closed stream
    // would only be refused here.
    let mut state = transport.state();
    let mut was_open = *state.borrow_and_update() == ConnectionState::Open;
    // An idle moment asks for a claim. One claim is in flight at a time, and
    // a wake-up that arrives during it is honoured with exactly one more.
    let mut want = was_open;
    let mut wake_pending = false;
    let mut claim: Option<Claiming> = None;
    let check = inner.timing.held_job_stall_check;
    let mut stall = tokio::time::interval_at(Instant::now() + check, check);
    stall.set_missed_tick_behavior(MissedTickBehavior::Delay);

    loop {
        if want {
            want = false;
            if inner.holding() {
                // Holding a job: the settle claims. Nothing to remember.
            } else if claim.is_some() {
                wake_pending = true;
            } else {
                wake_pending = false;
                claim = Some(Box::pin(claim_next(inner.clone())));
            }
        }
        tokio::select! {
            biased;
            _ = stop.changed() => break,
            _ = out.closed() => break,
            outcome = answered(&mut claim) => {
                claim = None;
                let again = match outcome {
                    ClaimOutcome::Job(holding, job) => {
                        inner.hold(&holding);
                        // With no reader left the job is dropped, which fails it.
                        if out.send(Ok(*job)).await.is_err() {
                            break;
                        }
                        // A wake-up that arrived during the claim is moot: the settle claims.
                        false
                    }
                    ClaimOutcome::Declined => wake_pending,
                    ClaimOutcome::Refused(refusal) => {
                        if out.send(Err(refusal)).await.is_err() {
                            break;
                        }
                        wake_pending
                    }
                };
                wake_pending = false;
                want = again;
            }
            announced = queued.next() => match announced {
                Some(Ok(Ok(announced))) => {
                    // Every announcement received is stamped, before any filtering.
                    locked(&inner.state).last_queued_event_at = Some(SystemTime::now());
                    let announced = Value::Object(announced.payload);
                    want = inner
                        .accepts
                        .iter()
                        .any(|filter| job_matches_filter(filter, &announced));
                }
                // An announcement refused was received all the same, and no
                // filter can be compared with what cannot be read: it wakes
                // the worker as one that matches would.
                Some(Ok(Err(_))) => {
                    locked(&inner.state).last_queued_event_at = Some(SystemTime::now());
                    want = true;
                }
                // The stream fell behind, and what it missed may have been a
                // wake-up: the queue is asked.
                Some(Err(_)) => want = true,
                None => break,
            },
            request = cancels.next() => match request {
                Some(Ok(Ok(request))) => {
                    if let Some(job_id) = request.payload.get("jobId").and_then(Value::as_str) {
                        inner.signal_cancellation(job_id);
                    }
                }
                // A cancellation missed is not known, and one refused names no
                // job to trust.
                Some(Ok(Err(_)) | Err(_)) => {}
                None => break,
            },
            changed = state.changed() => {
                if changed.is_err() {
                    break;
                }
                let open = *state.borrow_and_update() == ConnectionState::Open;
                want = open && !was_open;
                was_open = open;
            }
            _ = inner.settled.notified() => want = true,
            _ = stall.tick() => inner.look_for_stall(),
        }
    }

    // A claim still in flight may yet be answered with a job, which is this
    // worker's at the dispatcher from that moment, with nobody here left to
    // run it: it is failed, and not left held by nobody.
    if let Some(claim) = claim
        && let ClaimOutcome::Job(holding, job) = claim.await
    {
        inner.fail_held(&holding, STOPPED_WHILE_HELD).await;
        drop(job);
    }
}

/// A worker's claims, from `job.claim`: each job the worker comes to hold,
/// one at a time, and each claim that was refused. The next job is claimed
/// when the one held is settled.
///
/// Claiming begins when the claims are first read (`next`). It ends when
/// `stop` is called, when the claims are dropped, or when the transport's
/// stream ends.
///
/// Every method takes `&self`, so one task can read the claims while another
/// reads the worker's vitals, watches for a stall, or stops it. Two readers
/// take turns: a job is handed to one of them.
pub struct Claims {
    inner: Arc<Inner>,
    /// What the claiming hands out through, until the claiming has begun.
    out: Mutex<Option<mpsc::Sender<Handed>>>,
    handed: tokio::sync::Mutex<mpsc::Receiver<Handed>>,
    stop: watch::Sender<bool>,
}

impl Claims {
    pub(crate) fn new(transport: Arc<dyn Transport>, options: ClaimOptions) -> Claims {
        let (out, handed) = mpsc::channel(1);
        Claims {
            inner: Arc::new(Inner {
                wire: Bus::new(transport.clone()),
                transport,
                accepts: options.accepts,
                timing: options.timing,
                state: Mutex::new(State::default()),
                settled: Notify::new(),
                stall: watch::channel(None).0,
            }),
            out: Mutex::new(Some(out)),
            handed: tokio::sync::Mutex::new(handed),
            stop: watch::channel(false).0,
        }
    }

    /// The next job this worker holds, or the next claim it was refused.
    /// `None` once the claiming has ended. A refusal does not end it: the
    /// worker waits for its next idle moment, and what the refusal means is
    /// its host's to judge.
    pub async fn next(&self) -> Option<Handed> {
        let out = locked(&self.out).take();
        if let Some(out) = out {
            tokio::spawn(run(self.inner.clone(), out, self.stop.subscribe()));
        }
        let mut handed = self.handed.lock().await;
        loop {
            match handed.recv().await {
                // Claimed, and failed by the worker's stop before anybody read it.
                Some(Ok(job)) if job.settled() => {}
                next => return next,
            }
        }
    }

    /// What this worker can say of itself now.
    pub fn vitals(&self) -> WorkerVitals {
        self.inner.vitals()
    }

    /// The last stall found: a held job that has shown no activity for
    /// `held_job_stall`, reported once. `None` until there is one, and each
    /// one after is a change.
    pub fn stalled(&self) -> watch::Receiver<Option<HeldJobStall>> {
        self.inner.stall.subscribe()
    }

    /// Stop claiming. A job still held is failed first, and this returns
    /// once that has been said, or could not be.
    pub async fn stop(&self) {
        // Claiming that never began never will, and what was begun ends:
        // either way nothing more is handed out.
        drop(locked(&self.out).take());
        self.stop.send_replace(true);
        let held = locked(&self.inner.state).held.clone();
        if let Some(holding) = held {
            self.inner.fail_held(&holding, STOPPED_WHILE_HELD).await;
        }
    }
}