adk-vision 3.0.0

Local-first perception for ADK-Rust agents: artifact-backed images, regions in source pixels with their transform chains, model manifests pinned by revision and SHA-256, perception traits with conformance suites, routing with crop escalation, and a governed egress boundary
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
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
//! The perceiver, reasoner and verifier roles.

use std::sync::Arc;

use adk_core::egress::ToolEgress;
use adk_core::{
    AdkError, Agent, Content, ErrorCategory, ErrorComponent, EventStream, InvocationContext, Llm,
    LlmRequest, Part, Tool, ToolContext, ToolEffect,
};
use async_trait::async_trait;
use serde_json::{Value, json};

use super::{
    Opinion, TeamRoleError, answer_text, cx_for, load_image, parse_opinion, provider_of,
    request_of, text_event,
};
use crate::backend::{BackendDescriptor, Locality};
use crate::cx::VisionCx;
use crate::digest::Digest;
use crate::egress::{
    DataClass, EgressGate, EgressReceipt, ReleaseRequest, ReleasedImage, StagedEgress,
};
use crate::error::{Result, VisionError};
use crate::geometry::{BBox, CoordFrame, Region, RegionSet};
use crate::manifest::VisionTask;
use crate::media::SourceImage;
use crate::runtime::VisionRuntime;
use crate::toolset::{VISION_TOOL_NAMES, VisionToolset};
use crate::traits::DetectQuery;

const ALL_TASKS: [VisionTask; 7] = [
    VisionTask::Detect,
    VisionTask::Segment,
    VisionTask::Read,
    VisionTask::Embed,
    VisionTask::Describe,
    VisionTask::AnalyzeVideo,
    VisionTask::Edit,
];

/// The margin, in pixels, around a region when its crop is sent to a reasoner or verifier.
const CROP_MARGIN: f32 = 16.0;

/// Refuses a runtime that has a remote backend.
fn check_local_runtime(
    role: &'static str,
    member: &str,
    runtime: &VisionRuntime,
) -> std::result::Result<(), TeamRoleError> {
    for id in runtime.backends() {
        for task in ALL_TASKS {
            if let Some(descriptor) = runtime.descriptor(task, &id)
                && let Locality::Remote { hosts } = &descriptor.locality
            {
                return Err(TeamRoleError::RemoteBackend {
                    role,
                    member: member.to_string(),
                    backend: id.to_string(),
                    hosts: hosts.join(", "),
                });
            }
        }
    }
    Ok(())
}

/// Refuses a tool that declares any network use, or a vision tool given without its runtime.
fn check_local_tool(
    role: &'static str,
    member: &str,
    tool: &dyn Tool,
) -> std::result::Result<(), TeamRoleError> {
    if VISION_TOOL_NAMES.contains(&tool.name()) {
        return Err(TeamRoleError::UncheckedVisionTool {
            role,
            member: member.to_string(),
            tool: tool.name().to_string(),
        });
    }
    match tool.egress(&json!({})) {
        ToolEgress::None => Ok(()),
        other => Err(TeamRoleError::RemoteTool {
            role,
            member: member.to_string(),
            tool: tool.name().to_string(),
            egress: describe_egress(&other),
        }),
    }
}

fn describe_egress(egress: &ToolEgress) -> String {
    match egress {
        ToolEgress::None => "no network use".to_string(),
        ToolEgress::Undeclared => "nothing about its network use".to_string(),
        ToolEgress::Configured => "endpoints fixed when it was built".to_string(),
        ToolEgress::Enforced => "connections checked by the run's egress policy".to_string(),
        ToolEgress::Destinations(rules) => format!("destinations {rules:?}"),
    }
}

/// A tool that runs only calls that declare no network use (spec 3.7 §7.12).
///
/// A perceiver's and a verifier's tools are wrapped in it: a call whose declared egress is
/// anything but [`ToolEgress::None`] — a vision call routed to a remote backend, say — is
/// refused before the tool runs, so the role holds no tool that can send data off the
/// machine even when one of its tools could route a call remotely.
///
/// # Example
///
/// ```
/// use std::sync::Arc;
/// use adk_core::{Tool, ToolContext};
/// use adk_vision::team::LocalOnlyTool;
/// use serde_json::Value;
///
/// struct Count;
///
/// #[async_trait::async_trait]
/// impl Tool for Count {
///     fn name(&self) -> &str { "count" }
///     fn description(&self) -> &str { "Counts regions" }
///     async fn execute(&self, _: Arc<dyn ToolContext>, _: Value) -> adk_core::Result<Value> {
///         Ok(Value::Null)
///     }
/// }
///
/// let guarded = LocalOnlyTool::new(Arc::new(Count), "perceiver");
/// assert_eq!(guarded.name(), "count");
/// ```
pub struct LocalOnlyTool {
    inner: Arc<dyn Tool>,
    member: String,
}

impl LocalOnlyTool {
    /// Wraps `inner` for the member named `member`.
    pub fn new(inner: Arc<dyn Tool>, member: impl Into<String>) -> Self {
        Self { inner, member: member.into() }
    }
}

#[async_trait]
impl Tool for LocalOnlyTool {
    fn name(&self) -> &str {
        self.inner.name()
    }

    fn description(&self) -> &str {
        self.inner.description()
    }

    fn declaration(&self) -> Value {
        self.inner.declaration()
    }

    fn parameters_schema(&self) -> Option<Value> {
        self.inner.parameters_schema()
    }

    fn response_schema(&self) -> Option<Value> {
        self.inner.response_schema()
    }

    fn origin(&self) -> &str {
        self.inner.origin()
    }

    fn is_read_only(&self) -> bool {
        self.inner.is_read_only()
    }

    fn effect(&self) -> ToolEffect {
        self.inner.effect()
    }

    fn is_concurrency_safe(&self) -> bool {
        self.inner.is_concurrency_safe()
    }

    fn egress(&self, args: &Value) -> ToolEgress {
        self.inner.egress(args)
    }

    fn approval_evidence(&self, args: &Value) -> Vec<adk_core::Evidence> {
        self.inner.approval_evidence(args)
    }

    async fn execute(&self, ctx: Arc<dyn ToolContext>, args: Value) -> adk_core::Result<Value> {
        match self.inner.egress(&args) {
            ToolEgress::None => self.inner.execute(ctx, args).await,
            other => Err(AdkError::new(
                ErrorComponent::Tool,
                ErrorCategory::Forbidden,
                "vision.team.remote_call_refused",
                format!(
                    "`{}` was not run: this call declares {}, and the member '{}' holds only \
                     tools that stay on the device",
                    self.inner.name(),
                    describe_egress(&other),
                    self.member
                ),
            )),
        }
    }
}

// ---------------------------------------------------------------------------
// Perceiver
// ---------------------------------------------------------------------------

/// The perceiver: on-device perception with a local model or none (spec 3.7 §7.12).
///
/// Built only through [`Perceiver::builder`], which refuses a remote model, a runtime with a
/// remote backend and any tool that declares network use. With a model it runs as an
/// `LlmAgent` over its tools, each a [`LocalOnlyTool`]; without one, its agent form detects
/// in the image its request names and answers the `RegionSet`.
///
/// # Example
///
/// ```
/// use std::sync::Arc;
/// use adk_vision::team::Perceiver;
/// use adk_vision::{
///     BBox, BackendDescriptor, CoordFrame, DetectQuery, Detector, Locality, RawRegion,
///     SentImage, VisionCx, VisionRuntime,
/// };
///
/// struct Marks(BackendDescriptor);
///
/// #[async_trait::async_trait]
/// impl Detector for Marks {
///     fn descriptor(&self) -> &BackendDescriptor { &self.0 }
///     async fn detect(&self, _: &VisionCx, _: &SentImage, _: &DetectQuery) -> adk_vision::Result<Vec<RawRegion>> {
///         Ok(vec![RawRegion::boxed(BBox::new(4.0, 4.0, 12.0, 12.0), "scratch", 0.4)])
///     }
/// }
///
/// let local = BackendDescriptor::new("marks", Locality::InProcess, CoordFrame::SentPixels);
/// let runtime = VisionRuntime::builder().detector(Arc::new(Marks(local))).build().unwrap();
/// let perceiver = Perceiver::builder("perceiver").runtime(Arc::new(runtime)).build().unwrap();
/// assert_eq!(perceiver.name(), "perceiver");
/// ```
pub struct Perceiver {
    name: String,
    description: String,
    runtime: Option<Arc<VisionRuntime>>,
    tools: Vec<Arc<dyn Tool>>,
    classes: Vec<String>,
    agent: Option<Arc<dyn Agent>>,
}

impl std::fmt::Debug for Perceiver {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Perceiver")
            .field("name", &self.name)
            .field("tools", &self.tools.iter().map(|tool| tool.name()).collect::<Vec<_>>())
            .field("model", &self.agent.is_some())
            .finish_non_exhaustive()
    }
}

/// Builds a [`Perceiver`].
#[must_use]
pub struct PerceiverBuilder {
    name: String,
    description: String,
    runtime: Option<Arc<VisionRuntime>>,
    toolset: Option<VisionToolset>,
    model: Option<Arc<dyn Llm>>,
    tools: Vec<Arc<dyn Tool>>,
    classes: Vec<String>,
    instruction: Option<String>,
}

impl Perceiver {
    /// A builder for a perceiver named `name`.
    pub fn builder(name: impl Into<String>) -> PerceiverBuilder {
        PerceiverBuilder {
            name: name.into(),
            description: "Perceives on the device and reports regions in source pixels."
                .to_string(),
            runtime: None,
            toolset: None,
            model: None,
            tools: Vec::new(),
            classes: Vec::new(),
            instruction: None,
        }
    }

    /// The member name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// The tools the perceiver holds, each a [`LocalOnlyTool`].
    pub fn tools(&self) -> &[Arc<dyn Tool>] {
        &self.tools
    }

    /// Detects `query` in `image` on the device.
    ///
    /// # Errors
    ///
    /// [`VisionError::InvalidInput`] for a perceiver built without a runtime, and the
    /// runtime's error.
    pub async fn perceive(
        &self,
        cx: &VisionCx,
        image: &SourceImage,
        query: &DetectQuery,
    ) -> Result<RegionSet> {
        let runtime = self.runtime.as_ref().ok_or_else(|| {
            VisionError::InvalidInput(format!(
                "the perceiver '{}' has no runtime to detect with; give it one with \
                 PerceiverBuilder::runtime",
                self.name
            ))
        })?;
        let found = runtime.detect(cx, image, query).await?;
        let (width, height) = image.size();
        Ok(RegionSet::new(image.digest(), width, height, found.value))
    }

    /// The classes the agent form detects when a request names none.
    pub fn classes(&self) -> &[String] {
        &self.classes
    }
}

impl PerceiverBuilder {
    /// The model-facing description.
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = description.into();
        self
    }

    /// The runtime [`Perceiver::perceive`] detects with; every backend must be on the device.
    pub fn runtime(mut self, runtime: Arc<VisionRuntime>) -> Self {
        self.runtime = Some(runtime);
        self
    }

    /// The vision tools; their runtime must have only on-device backends. Also the runtime
    /// [`Perceiver::perceive`] uses when none is given.
    pub fn toolset(mut self, toolset: VisionToolset) -> Self {
        self.toolset = Some(toolset);
        self
    }

    /// A model that runs on the device; without one the perceiver runs no model.
    pub fn model(mut self, model: Arc<dyn Llm>) -> Self {
        self.model = Some(model);
        self
    }

    /// Another tool; it must declare no network use ([`ToolEgress::None`]).
    pub fn tool(mut self, tool: Arc<dyn Tool>) -> Self {
        self.tools.push(tool);
        self
    }

    /// The classes the agent form detects when a request names none.
    pub fn classes(mut self, classes: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.classes = classes.into_iter().map(Into::into).collect();
        self
    }

    /// The model's instruction.
    pub fn instruction(mut self, instruction: impl Into<String>) -> Self {
        self.instruction = Some(instruction.into());
        self
    }

    /// Builds the perceiver.
    ///
    /// # Errors
    ///
    /// [`TeamRoleError::RemoteModel`] for a model whose requests leave the machine,
    /// [`TeamRoleError::RemoteBackend`] for a runtime with a remote backend,
    /// [`TeamRoleError::RemoteTool`] for a tool that declares any network use,
    /// [`TeamRoleError::UncheckedVisionTool`] for a vision tool given through
    /// [`tool`](Self::tool), and [`TeamRoleError::Missing`] for a perceiver with neither a
    /// runtime nor a model.
    pub fn build(self) -> std::result::Result<Perceiver, TeamRoleError> {
        const ROLE: &str = "perceiver";
        if let Some(model) = &self.model
            && model.data_locality().leaves_machine()
        {
            return Err(TeamRoleError::RemoteModel {
                role: ROLE,
                member: self.name,
                model: model.name().to_string(),
            });
        }
        let runtime =
            self.runtime.or_else(|| self.toolset.as_ref().map(|set| Arc::clone(set.runtime())));
        if let Some(runtime) = &runtime {
            check_local_runtime(ROLE, &self.name, runtime)?;
        }
        if let Some(toolset) = &self.toolset {
            check_local_runtime(ROLE, &self.name, toolset.runtime())?;
        }
        for tool in &self.tools {
            check_local_tool(ROLE, &self.name, tool.as_ref())?;
        }
        if runtime.is_none() && self.model.is_none() {
            return Err(TeamRoleError::Missing(format!(
                "the perceiver '{}' needs a runtime to detect with or a local model to drive its \
                 tools",
                self.name
            )));
        }
        let tools: Vec<Arc<dyn Tool>> = self
            .toolset
            .iter()
            .flat_map(VisionToolset::tools)
            .chain(self.tools)
            .map(|tool| Arc::new(LocalOnlyTool::new(tool, self.name.clone())) as Arc<dyn Tool>)
            .collect();
        let agent = match self.model {
            Some(model) => {
                let instruction = self.instruction.unwrap_or_else(|| {
                    "You are the perceiver of a vision team. Look at the image you are given \
                     with your tools, which all run on this device, and report what you found \
                     as regions in source pixels, with labels and scores."
                        .to_string()
                });
                let mut builder = adk_agent::LlmAgentBuilder::new(self.name.clone())
                    .description(self.description.clone())
                    .model(model)
                    .instruction(instruction);
                for tool in &tools {
                    builder = builder.tool(Arc::clone(tool));
                }
                let agent = builder
                    .build()
                    .map_err(|err| TeamRoleError::Missing(format!("perceiver agent: {err}")))?;
                Some(Arc::new(agent) as Arc<dyn Agent>)
            }
            None => None,
        };
        Ok(Perceiver {
            name: self.name,
            description: self.description,
            runtime,
            tools,
            classes: self.classes,
            agent,
        })
    }
}

#[async_trait]
impl Agent for Perceiver {
    fn name(&self) -> &str {
        &self.name
    }

    fn description(&self) -> &str {
        &self.description
    }

    fn sub_agents(&self) -> &[Arc<dyn Agent>] {
        &[]
    }

    async fn run(&self, ctx: Arc<dyn InvocationContext>) -> adk_core::Result<EventStream> {
        if let Some(agent) = &self.agent {
            return agent.run(ctx).await;
        }
        let request = request_of(ctx.as_ref());
        let image = request.get("image").and_then(Value::as_str).unwrap_or_default().to_string();
        let classes: Vec<String> = match request.get("classes").and_then(Value::as_array) {
            Some(items) => items.iter().filter_map(Value::as_str).map(str::to_string).collect(),
            None => self.classes.clone(),
        };
        let cx = cx_for(ctx.as_ref());
        let source = load_image(ctx.as_ref(), &image).await?;
        let regions = self.perceive(&cx, &source, &DetectQuery::Classes(classes)).await?;
        let text = serde_json::to_string(&regions)
            .map_err(|err| AdkError::agent(format!("regions could not be encoded: {err}")))?;
        let event = text_event(ctx.as_ref(), &self.name, text);
        Ok(Box::pin(futures::stream::iter([Ok(event)])))
    }
}

// ---------------------------------------------------------------------------
// Reasoner
// ---------------------------------------------------------------------------

/// What the coordinator asks a reasoner: a question, the perceiver's regions, measurements,
/// and the regions of the source image to send as crops.
///
/// The crops are released through the reasoner's egress gate — cropped, redacted, decided
/// and recorded — before the model sees them; a reasoner never receives the source image.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct ReasonRequest<'a> {
    /// The question.
    pub question: String,
    /// The verdicts the answer must be one of.
    pub verdicts: Vec<String>,
    /// The perceiver's regions, in source pixels.
    pub regions: RegionSet,
    /// Measurements, such as `vision_measure` results.
    pub measurements: Vec<Value>,
    /// The source image the crops are cut from.
    pub source: Option<&'a SourceImage>,
    /// The regions to crop, in source pixels; a margin is added.
    pub crops: Vec<BBox>,
    /// Egress digests a person approved, for a rule that asks.
    pub approved: Vec<Digest>,
}

impl<'a> ReasonRequest<'a> {
    /// A request asking `question` over `regions`, answered with one of `verdicts`.
    pub fn new(
        question: impl Into<String>,
        verdicts: impl IntoIterator<Item = impl Into<String>>,
        regions: RegionSet,
    ) -> Self {
        Self {
            question: question.into(),
            verdicts: verdicts.into_iter().map(Into::into).collect(),
            regions,
            measurements: Vec::new(),
            source: None,
            crops: Vec::new(),
            approved: Vec::new(),
        }
    }

    /// Sends `crop` of `source`, after the egress gate.
    #[must_use]
    pub fn with_crop(mut self, source: &'a SourceImage, crop: BBox) -> Self {
        self.source = Some(source);
        self.crops.push(crop);
        self
    }

    /// Adds measurements.
    #[must_use]
    pub fn with_measurements(mut self, measurements: Vec<Value>) -> Self {
        self.measurements = measurements;
        self
    }

    /// Releases a crop whose egress digest a person approved.
    #[must_use]
    pub fn approving(mut self, digest: Digest) -> Self {
        self.approved.push(digest);
        self
    }
}

/// A reasoner's answer and the receipts of the crops it was sent.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Reasoned {
    /// The opinion.
    pub opinion: Opinion,
    /// One receipt per released crop.
    pub released: Vec<EgressReceipt>,
}

/// The reasoner: a model, usually in the cloud, that answers over regions, measurements and
/// crops the egress gate released, and holds no tool (spec 3.7 §7.12).
///
/// Every image a request carries goes through the gate first; the request is then screened by
/// an `ImagePrivacyGuardrail` on the same gate, so an image that did not pass the gate never
/// reaches a remote model.
///
/// # Example
///
/// ```
/// use std::sync::Arc;
/// use adk_core::egress::EgressPolicy;
/// use adk_vision::team::Reasoner;
/// use adk_vision::{EgressConfig, EgressGate, InMemoryEgressRecorder};
///
/// # struct Cloud;
/// # #[async_trait::async_trait]
/// # impl adk_core::Llm for Cloud {
/// #     fn name(&self) -> &str { "claude-haiku-5-5" }
/// #     async fn generate_content(&self, _: adk_core::LlmRequest, _: bool) -> adk_core::Result<adk_core::LlmResponseStream> {
/// #         Ok(Box::pin(futures::stream::empty()))
/// #     }
/// # }
/// let gate = Arc::new(EgressGate::new(
///     EgressPolicy::allow_private_networks(),
///     EgressConfig::default(),
///     Arc::new(InMemoryEgressRecorder::new()),
/// ));
/// let reasoner = Reasoner::builder("reasoner")
///     .model(Arc::new(Cloud))
///     .egress_gate(gate)
///     .hosts(["api.anthropic.com"])
///     .build()
///     .unwrap();
/// assert_eq!(reasoner.provider(), "anthropic");
/// ```
pub struct Reasoner {
    name: String,
    description: String,
    model: Arc<dyn Llm>,
    gate: Option<Arc<EgressGate>>,
    destination: BackendDescriptor,
    data_class: DataClass,
    provider: String,
    #[cfg(feature = "guardrail")]
    guardrail: Option<crate::guardrail::ImagePrivacyGuardrail>,
    max_output_tokens: Option<i32>,
    config: adk_core::GenerateContentConfig,
}

impl std::fmt::Debug for Reasoner {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Reasoner")
            .field("name", &self.name)
            .field("model", &self.model.name())
            .field("provider", &self.provider)
            .finish_non_exhaustive()
    }
}

/// Builds a [`Reasoner`].
#[must_use]
pub struct ReasonerBuilder {
    name: String,
    description: String,
    model: Option<Arc<dyn Llm>>,
    gate: Option<Arc<EgressGate>>,
    hosts: Vec<String>,
    data_class: String,
    provider: Option<String>,
    max_output_tokens: Option<i32>,
    config: adk_core::GenerateContentConfig,
}

impl Reasoner {
    /// A builder for a reasoner named `name`.
    pub fn builder(name: impl Into<String>) -> ReasonerBuilder {
        ReasonerBuilder {
            name: name.into(),
            description: "Reasons over regions, measurements and redacted crops, and answers \
                          with a verdict."
                .to_string(),
            model: None,
            gate: None,
            hosts: Vec::new(),
            data_class: "team_crop".to_string(),
            provider: None,
            max_output_tokens: None,
            config: adk_core::GenerateContentConfig::default(),
        }
    }

    /// The member name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// The provider the reasoner runs on.
    pub fn provider(&self) -> &str {
        &self.provider
    }

    /// The model.
    pub fn model(&self) -> &Arc<dyn Llm> {
        &self.model
    }

    /// Whether the reasoner's requests leave the machine.
    pub fn is_remote(&self) -> bool {
        self.model.data_locality().leaves_machine()
    }

    /// The egress gate the reasoner's crops pass.
    pub fn egress_gate(&self) -> Option<&Arc<EgressGate>> {
        self.gate.as_ref()
    }

    /// The data class the reasoner's crops are released as.
    pub fn data_class(&self) -> &DataClass {
        &self.data_class
    }

    /// Stages `crop` of `source` for a person's approval when its egress rule asks: the
    /// destination, minimise and redact steps, the egress copy stored. `None` for a reasoner
    /// on the device, or under a rule that allows the release.
    ///
    /// # Errors
    ///
    /// The gate's refusals ([`EgressGate::stage`]), [`VisionError::EgressRefused`] for a
    /// rule that denies.
    pub async fn stage(
        &self,
        cx: &VisionCx,
        source: &SourceImage,
        crop: BBox,
    ) -> Result<Option<StagedEgress>> {
        let Some(gate) = self.gate.as_ref().filter(|_| self.is_remote()) else {
            return Ok(None);
        };
        if allows(gate, &self.destination, &self.data_class) {
            return Ok(None);
        }
        let request = ReleaseRequest::new(source, &self.destination, self.data_class.clone())
            .with_crop(with_margin(crop, source));
        gate.stage(cx, &request).await.map(Some)
    }

    /// Asks the reasoner.
    ///
    /// # Errors
    ///
    /// The gate's refusals for a crop ([`VisionError::EgressHeld`] under a rule that asks
    /// without an approved digest), the model's error, and [`VisionError::Parse`] for an
    /// answer that is not an opinion with one of the verdicts.
    pub async fn reason(&self, cx: &VisionCx, request: ReasonRequest<'_>) -> Result<Reasoned> {
        let mut parts = Vec::new();
        let mut released = Vec::new();
        if !request.crops.is_empty() {
            let source = request.source.ok_or_else(|| {
                VisionError::InvalidInput("crops need the source image they are cut from".into())
            })?;
            for crop in &request.crops {
                let crop = with_margin(*crop, source);
                if self.is_remote() {
                    let gate = self.gate.as_ref().ok_or_else(|| VisionError::NotReleased {
                        backend: self.destination.id.to_string(),
                    })?;
                    let mut release =
                        ReleaseRequest::new(source, &self.destination, self.data_class.clone())
                            .with_crop(crop);
                    for digest in &request.approved {
                        release = release.approved(*digest);
                    }
                    let image: ReleasedImage = gate.release(cx, release).await?;
                    parts.push(image.to_part());
                    released.push(image.receipt().clone());
                } else {
                    let (w, h) = source.size();
                    let (x, y, cw, ch) = crop.pixel_bounds(w, h);
                    let pixels = crate::edit::ops::crop(source.pixels(), x, y, cw, ch);
                    parts.push(Part::InlineData {
                        mime_type: "image/png".into(),
                        data: pixels.encode_png()?,
                        uri: None,
                        annotations: None,
                    });
                }
            }
        }
        let opinion = self
            .ask(
                cx,
                &request.question,
                &request.verdicts,
                &request.regions,
                &request.measurements,
                parts,
            )
            .await?;
        Ok(Reasoned { opinion, released })
    }

    async fn ask(
        &self,
        cx: &VisionCx,
        question: &str,
        verdicts: &[String],
        regions: &RegionSet,
        measurements: &[Value],
        images: Vec<Part>,
    ) -> Result<Opinion> {
        let count = images.len();
        let mut text = format!(
            "You are the reasoner of a vision team. You hold no tools and never see the whole \
             image: you see the regions a perceiver on the device found, measurements, and \
             {count} crop(s) of the image around those regions, redacted before they were sent.\n\n\
             Question: {question}\n\
             Answer with one verdict from: {}.\n\n\
             Regions, in source pixels: {}\n",
            verdicts.iter().map(|v| format!("\"{v}\"")).collect::<Vec<_>>().join(", "),
            serde_json::to_string(regions).unwrap_or_default(),
        );
        if !measurements.is_empty() {
            text.push_str(&format!(
                "Measurements: {}\n",
                serde_json::to_string(measurements).unwrap_or_default()
            ));
        }
        text.push_str(
            "\nAnswer with JSON only: {\"verdict\": <one of the verdicts>, \"label\": <the class \
             you judged>, \"confidence\": <0 to 1>, \"rationale\": <one sentence>}.",
        );
        let mut parts = images;
        parts.push(Part::Text { text });
        let mut content = Content { role: "user".into(), parts };
        #[cfg(feature = "guardrail")]
        if let Some(guardrail) = &self.guardrail {
            let outcome = guardrail.gate_content(cx, &mut content).await;
            if !outcome.withheld.is_empty() {
                tracing::warn!(
                    vision.withheld = outcome.withheld.len(),
                    team.member = %self.name,
                    "images withheld from the reasoner"
                );
            }
        }
        let mut request = LlmRequest::new(self.model.name(), vec![content]);
        let mut config = self.config.clone();
        if self.max_output_tokens.is_some() {
            config.max_output_tokens = self.max_output_tokens;
        }
        request.config = Some(config);
        let text = answer_text(cx, &self.model, request).await?;
        parse_opinion(self.model.name(), &text, verdicts, &self.name, &self.provider)
    }
}

impl ReasonerBuilder {
    /// The model-facing description.
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = description.into();
        self
    }

    /// The model.
    pub fn model(mut self, model: Arc<dyn Llm>) -> Self {
        self.model = Some(model);
        self
    }

    /// The egress gate every crop for a remote model passes; required for one.
    pub fn egress_gate(mut self, gate: Arc<EgressGate>) -> Self {
        self.gate = Some(gate);
        self
    }

    /// The hosts the model sends to, for the egress policy and records, in place of the hosts
    /// the model names (`Llm::data_destinations`); the model's name when it names none.
    pub fn hosts(mut self, hosts: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.hosts = hosts.into_iter().map(Into::into).collect();
        self
    }

    /// The data class crops are released as; `team_crop` by default.
    pub fn data_class(mut self, class: &str) -> Self {
        self.data_class = class.to_string();
        self
    }

    /// The provider, when the model's id does not name it ([`provider_of`]).
    pub fn provider(mut self, provider: impl Into<String>) -> Self {
        self.provider = Some(provider.into());
        self
    }

    /// The most output tokens an answer may take.
    pub fn max_output_tokens(mut self, tokens: i32) -> Self {
        self.max_output_tokens = Some(tokens);
        self
    }

    /// The generation settings every request starts from, such as a provider extension.
    pub fn generate_config(mut self, config: adk_core::GenerateContentConfig) -> Self {
        self.config = config;
        self
    }

    /// Builds the reasoner.
    ///
    /// # Errors
    ///
    /// [`TeamRoleError::Missing`] without a model, or for a remote model without an egress
    /// gate.
    pub fn build(self) -> std::result::Result<Reasoner, TeamRoleError> {
        let model = self.model.ok_or_else(|| {
            TeamRoleError::Missing(format!("the reasoner '{}' needs a model", self.name))
        })?;
        let remote = model.data_locality().leaves_machine();
        if remote && self.gate.is_none() {
            return Err(TeamRoleError::Missing(format!(
                "the reasoner '{}' runs on the remote model '{}' and needs the egress gate its \
                 crops pass (ReasonerBuilder::egress_gate)",
                self.name,
                model.name()
            )));
        }
        let destination = destination_of(model.as_ref(), remote, self.hosts);
        let hosts = match &destination.locality {
            Locality::Remote { hosts } => hosts.clone(),
            _ => Vec::new(),
        };
        let provider = provider_of(model.name(), self.provider.as_deref());
        #[cfg(feature = "guardrail")]
        let guardrail = self.gate.as_ref().map(|gate| {
            crate::guardrail::ImagePrivacyGuardrail::for_model(
                Arc::clone(gate),
                model.as_ref(),
                hosts.clone(),
            )
        });
        Ok(Reasoner {
            name: self.name,
            description: self.description,
            model,
            gate: self.gate,
            destination,
            data_class: DataClass::new(self.data_class),
            provider,
            #[cfg(feature = "guardrail")]
            guardrail,
            max_output_tokens: self.max_output_tokens,
            config: self.config,
        })
    }
}

#[async_trait]
impl Agent for Reasoner {
    fn name(&self) -> &str {
        &self.name
    }

    fn description(&self) -> &str {
        &self.description
    }

    fn sub_agents(&self) -> &[Arc<dyn Agent>] {
        &[]
    }

    async fn run(&self, ctx: Arc<dyn InvocationContext>) -> adk_core::Result<EventStream> {
        let request = request_of(ctx.as_ref());
        let question = request.get("question").and_then(Value::as_str).unwrap_or_default();
        let regions: RegionSet = request
            .get("regions")
            .cloned()
            .and_then(|value| serde_json::from_value(value).ok())
            .ok_or_else(|| AdkError::agent("a reasoner request needs `regions`, a RegionSet"))?;
        let measurements: Vec<Value> =
            request.get("measurements").and_then(Value::as_array).cloned().unwrap_or_default();
        let verdicts: Vec<String> = match request.get("verdicts").and_then(Value::as_array) {
            Some(items) => items.iter().filter_map(Value::as_str).map(str::to_string).collect(),
            None => vec!["yes".to_string(), "no".to_string()],
        };
        let mut parts = Vec::new();
        for crop in request.get("crops").and_then(Value::as_array).into_iter().flatten() {
            let name = crop.as_str().unwrap_or_default();
            let digest = name
                .strip_prefix("sha256:")
                .and_then(|hex| Digest::from_hex(hex).ok())
                .ok_or_else(|| AdkError::agent(format!("`{name}` is not an image digest")))?;
            // Only an image this reasoner's gate released may reach its model.
            if self.is_remote() && !self.gate.as_ref().is_some_and(|g| g.has_released(&digest)) {
                return Err(
                    VisionError::NotReleased { backend: self.destination.id.to_string() }.into()
                );
            }
            let data = super::load_bytes(ctx.as_ref(), name).await?;
            parts.push(Part::InlineData {
                mime_type: "image/png".into(),
                data,
                uri: None,
                annotations: Some(json!({
                    "adkVision": { "digest": digest, "release": { "egress": digest } }
                })),
            });
        }
        let cx = cx_for(ctx.as_ref());
        let opinion = self.ask(&cx, question, &verdicts, &regions, &measurements, parts).await?;
        let text = serde_json::to_string(&opinion)
            .map_err(|err| AdkError::agent(format!("the opinion could not be encoded: {err}")))?;
        let event = text_event(ctx.as_ref(), &self.name, text);
        Ok(Box::pin(futures::stream::iter([Ok(event)])))
    }
}

/// Where a role's model sends its crops: on the device, or remote at `hosts`, else the hosts
/// and providers the model names, else its name, so a rule can still name it.
fn destination_of(model: &dyn Llm, remote: bool, hosts: Vec<String>) -> BackendDescriptor {
    if !remote {
        return BackendDescriptor::new(model.name(), Locality::InProcess, CoordFrame::SentPixels);
    }
    let destinations = model.data_destinations();
    let mut hosts = hosts;
    if hosts.is_empty() {
        hosts = adk_core::DataDestination::all_hosts(&destinations);
    }
    if hosts.is_empty() {
        hosts.push(model.name().to_string());
    }
    let mut descriptor =
        BackendDescriptor::new(model.name(), Locality::Remote { hosts }, CoordFrame::SentPixels);
    for destination in destinations {
        descriptor = descriptor.with_provider(destination.provider);
    }
    descriptor
}

/// Whether the egress rules release an image of `class` to `destination` without asking.
fn allows(gate: &EgressGate, destination: &BackendDescriptor, class: &DataClass) -> bool {
    match &destination.locality {
        Locality::Remote { .. } => {
            gate.decide(destination, class).0 == crate::egress::EgressAction::Allow
        }
        _ => true,
    }
}

fn with_margin(crop: BBox, source: &SourceImage) -> BBox {
    let (w, h) = source.size();
    crop.expanded(CROP_MARGIN).clamped(w, h)
}

// ---------------------------------------------------------------------------
// Verifier
// ---------------------------------------------------------------------------

/// What the verifier's on-device check found for one claim.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct RegionCheck {
    /// The class looked for.
    pub label: String,
    /// The claimed region, in source pixels.
    pub claimed: BBox,
    /// What local perception found for the class, in source pixels.
    pub found: Vec<Region>,
    /// The best overlap of a found region with the claimed one.
    pub best_iou: f32,
    /// Whether a found region overlaps the claim enough to confirm it.
    pub confirmed: bool,
}

/// What the coordinator asks a verifier: re-check `claim` about `region` of `source`.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct VerifyRequest<'a> {
    /// The question the claim answers.
    pub question: String,
    /// The two verdicts: the claim holds, the claim does not.
    pub verdicts: [String; 2],
    /// The claim to re-check.
    pub claim: Opinion,
    /// The class to look for; the claim's label when `None`.
    pub label: Option<String>,
    /// The claimed region, in source pixels.
    pub region: BBox,
    /// The source image on the device.
    pub source: &'a SourceImage,
    /// Egress digests a person approved, for a remote verifier model's crop.
    pub approved: Vec<Digest>,
}

impl<'a> VerifyRequest<'a> {
    /// A request to re-check `claim` about `region` of `source`; `positive` is the verdict
    /// that says the class is there and `negative` the one that says it is not.
    pub fn new(
        question: impl Into<String>,
        positive: impl Into<String>,
        negative: impl Into<String>,
        claim: Opinion,
        region: BBox,
        source: &'a SourceImage,
    ) -> Self {
        Self {
            question: question.into(),
            verdicts: [positive.into(), negative.into()],
            claim,
            label: None,
            region,
            source,
            approved: Vec::new(),
        }
    }

    /// Looks for `label` instead of the claim's.
    #[must_use]
    pub fn with_label(mut self, label: impl Into<String>) -> Self {
        self.label = Some(label.into());
        self
    }
}

/// A verifier's answer, its on-device check and the receipt of any crop it sent.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Verified {
    /// The opinion.
    pub opinion: Opinion,
    /// What local perception found.
    pub check: RegionCheck,
    /// The receipt of the crop a remote verifier model was sent.
    pub released: Option<EgressReceipt>,
}

/// The verifier: re-checks a claim against the pixels with local perception, optionally with
/// a model, by default on another provider than the reasoner's (spec 3.7 §7.12).
///
/// The check runs the verifier's on-device detector for the claimed class and compares the
/// regions it finds with the claimed one by overlap. Without a model, the check decides; with
/// one, the model sees the claim, the check and a crop of the region — released through the
/// egress gate when the model is remote — and decides.
///
/// # Example
///
/// ```
/// use std::sync::Arc;
/// use adk_vision::team::Verifier;
/// use adk_vision::{
///     BackendDescriptor, CoordFrame, DetectQuery, Detector, Locality, RawRegion, SentImage,
///     VisionCx, VisionRuntime,
/// };
///
/// struct Nothing(BackendDescriptor);
///
/// #[async_trait::async_trait]
/// impl Detector for Nothing {
///     fn descriptor(&self) -> &BackendDescriptor { &self.0 }
///     async fn detect(&self, _: &VisionCx, _: &SentImage, _: &DetectQuery) -> adk_vision::Result<Vec<RawRegion>> {
///         Ok(Vec::new())
///     }
/// }
///
/// let local = BackendDescriptor::new("strict", Locality::InProcess, CoordFrame::SentPixels);
/// let runtime = VisionRuntime::builder().detector(Arc::new(Nothing(local))).build().unwrap();
/// let verifier = Verifier::builder("verifier").runtime(Arc::new(runtime)).build().unwrap();
/// assert_eq!(verifier.provider(), "local");
/// ```
pub struct Verifier {
    name: String,
    description: String,
    runtime: Arc<VisionRuntime>,
    model: Option<Arc<dyn Llm>>,
    gate: Option<Arc<EgressGate>>,
    destination: Option<BackendDescriptor>,
    data_class: DataClass,
    provider: String,
    iou: f32,
    min_score: f32,
    max_output_tokens: Option<i32>,
    config: adk_core::GenerateContentConfig,
}

impl std::fmt::Debug for Verifier {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Verifier")
            .field("name", &self.name)
            .field("model", &self.model.as_ref().map(|m| m.name().to_string()))
            .field("provider", &self.provider)
            .finish_non_exhaustive()
    }
}

/// Builds a [`Verifier`].
#[must_use]
pub struct VerifierBuilder {
    name: String,
    description: String,
    runtime: Option<Arc<VisionRuntime>>,
    model: Option<Arc<dyn Llm>>,
    gate: Option<Arc<EgressGate>>,
    hosts: Vec<String>,
    data_class: String,
    provider: Option<String>,
    iou: f32,
    min_score: f32,
    max_output_tokens: Option<i32>,
    config: adk_core::GenerateContentConfig,
}

impl Verifier {
    /// A builder for a verifier named `name`.
    pub fn builder(name: impl Into<String>) -> VerifierBuilder {
        VerifierBuilder {
            name: name.into(),
            description: "Re-checks a claim against the pixels with local perception.".to_string(),
            runtime: None,
            model: None,
            gate: None,
            hosts: Vec::new(),
            data_class: "team_crop".to_string(),
            provider: None,
            iou: 0.3,
            min_score: 0.25,
            max_output_tokens: None,
            config: adk_core::GenerateContentConfig::default(),
        }
    }

    /// The member name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// The provider the verifier decides on: its model's, or `local` for a check alone.
    pub fn provider(&self) -> &str {
        &self.provider
    }

    /// Whether the verifier's model requests leave the machine.
    pub fn is_remote(&self) -> bool {
        self.model.as_ref().is_some_and(|m| m.data_locality().leaves_machine())
    }

    /// The egress gate a remote verifier model's crops pass.
    pub fn egress_gate(&self) -> Option<&Arc<EgressGate>> {
        self.gate.as_ref()
    }

    /// Stages a crop for a remote verifier model when its egress rule asks, as
    /// [`Reasoner::stage`] does. `None` when nothing would leave or the rule allows it.
    ///
    /// # Errors
    ///
    /// The gate's refusals.
    pub async fn stage(
        &self,
        cx: &VisionCx,
        source: &SourceImage,
        crop: BBox,
    ) -> Result<Option<StagedEgress>> {
        let (Some(gate), Some(destination)) = (&self.gate, &self.destination) else {
            return Ok(None);
        };
        if !self.is_remote() || allows(gate, destination, &self.data_class) {
            return Ok(None);
        }
        let request = ReleaseRequest::new(source, destination, self.data_class.clone())
            .with_crop(with_margin(crop, source));
        gate.stage(cx, &request).await.map(Some)
    }

    /// Runs the on-device check for `label` around `region`.
    ///
    /// # Errors
    ///
    /// The runtime's error.
    pub async fn check(
        &self,
        cx: &VisionCx,
        source: &SourceImage,
        label: &str,
        region: BBox,
    ) -> Result<RegionCheck> {
        let found = self
            .runtime
            .detect(cx, source, &DetectQuery::Classes(vec![label.to_string()]))
            .await?
            .value;
        let found: Vec<Region> = found
            .into_iter()
            .filter(|r| r.label.as_deref().is_none_or(|l| l == label))
            .filter(|r| r.score.is_none_or(|s| s >= self.min_score))
            .collect();
        let best_iou = found
            .iter()
            .filter_map(|r| r.shape.bbox())
            .map(|bbox| bbox.iou(&region))
            .fold(0.0_f32, f32::max);
        Ok(RegionCheck {
            label: label.to_string(),
            claimed: region,
            found,
            best_iou,
            confirmed: best_iou >= self.iou,
        })
    }

    /// Re-checks a claim.
    ///
    /// # Errors
    ///
    /// The check's error, the gate's refusals for a remote model's crop, the model's error,
    /// and [`VisionError::Parse`] for an answer that is not an opinion with one of the two
    /// verdicts.
    pub async fn verify(&self, cx: &VisionCx, request: VerifyRequest<'_>) -> Result<Verified> {
        let label = request
            .label
            .clone()
            .or_else(|| request.claim.label.clone())
            .unwrap_or_else(|| request.verdicts[0].clone());
        let check = self.check(cx, request.source, &label, request.region).await?;
        let Some(model) = &self.model else {
            let verdict = if check.confirmed { &request.verdicts[0] } else { &request.verdicts[1] };
            let confidence = if check.confirmed { check.best_iou } else { 1.0 - check.best_iou };
            let mut opinion = Opinion::new(verdict.clone(), confidence).with_label(label.clone());
            opinion.rationale = if check.confirmed {
                format!(
                    "local perception finds `{label}` over the claimed region (overlap {:.2})",
                    check.best_iou
                )
            } else {
                format!(
                    "local perception finds no `{label}` over the claimed region (best overlap \
                     {:.2}, {} found elsewhere)",
                    check.best_iou,
                    check.found.len()
                )
            };
            opinion.member = self.name.clone();
            opinion.provider = self.provider.clone();
            return Ok(Verified { opinion, check, released: None });
        };
        let crop = with_margin(request.region, request.source);
        let mut released = None;
        let image = if self.is_remote() {
            let (Some(gate), Some(destination)) = (&self.gate, &self.destination) else {
                return Err(VisionError::NotReleased { backend: model.name().to_string() });
            };
            let mut release =
                ReleaseRequest::new(request.source, destination, self.data_class.clone())
                    .with_crop(crop);
            for digest in &request.approved {
                release = release.approved(*digest);
            }
            let image = gate.release(cx, release).await?;
            released = Some(image.receipt().clone());
            image.to_part()
        } else {
            let (w, h) = request.source.size();
            let (x, y, cw, ch) = crop.pixel_bounds(w, h);
            Part::InlineData {
                mime_type: "image/png".into(),
                data: crate::edit::ops::crop(request.source.pixels(), x, y, cw, ch).encode_png()?,
                uri: None,
                annotations: None,
            }
        };
        let verdicts = request.verdicts.to_vec();
        let text = format!(
            "You are the verifier of a vision team. Another model made a claim about a region of \
             an image; check it against the pixels. The image is a crop around the claimed \
             region, redacted before it was sent.\n\n\
             Question: {question}\n\
             Claim: {claim}\n\
             Local perception on the device looked for `{label}` around the claimed region: \
             {found} region(s) found, best overlap with the claim {iou:.2}.\n\n\
             Answer with JSON only: {{\"verdict\": <one of {list}>, \"label\": <the class>, \
             \"confidence\": <0 to 1>, \"rationale\": <one sentence>}}.",
            question = request.question,
            claim = serde_json::to_string(&request.claim).unwrap_or_default(),
            found = check.found.len(),
            iou = check.best_iou,
            list = verdicts.iter().map(|v| format!("\"{v}\"")).collect::<Vec<_>>().join(", "),
        );
        let mut content = Content { role: "user".into(), parts: vec![image, Part::Text { text }] };
        #[cfg(feature = "guardrail")]
        if let (Some(gate), true) = (&self.gate, self.is_remote()) {
            let hosts = match self.destination.as_ref().map(|d| &d.locality) {
                Some(Locality::Remote { hosts }) => hosts.clone(),
                _ => vec![model.name().to_string()],
            };
            crate::guardrail::ImagePrivacyGuardrail::for_model(
                Arc::clone(gate),
                model.as_ref(),
                hosts,
            )
            .gate_content(cx, &mut content)
            .await;
        }
        let mut llm_request = LlmRequest::new(model.name(), vec![content]);
        let mut config = self.config.clone();
        if self.max_output_tokens.is_some() {
            config.max_output_tokens = self.max_output_tokens;
        }
        llm_request.config = Some(config);
        let answer = answer_text(cx, model, llm_request).await?;
        let opinion = parse_opinion(model.name(), &answer, &verdicts, &self.name, &self.provider)?;
        Ok(Verified { opinion, check, released })
    }
}

impl VerifierBuilder {
    /// The model-facing description.
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = description.into();
        self
    }

    /// The runtime the check perceives with; every backend must be on the device. Required.
    pub fn runtime(mut self, runtime: Arc<VisionRuntime>) -> Self {
        self.runtime = Some(runtime);
        self
    }

    /// A model that decides with the check; any provider, by default another than the
    /// reasoner's.
    pub fn model(mut self, model: Arc<dyn Llm>) -> Self {
        self.model = Some(model);
        self
    }

    /// The egress gate a remote model's crops pass; required for one.
    pub fn egress_gate(mut self, gate: Arc<EgressGate>) -> Self {
        self.gate = Some(gate);
        self
    }

    /// The hosts a remote model sends to, in place of the hosts the model names
    /// (`Llm::data_destinations`); the model's name when it names none.
    pub fn hosts(mut self, hosts: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.hosts = hosts.into_iter().map(Into::into).collect();
        self
    }

    /// The data class crops are released as; `team_crop` by default.
    pub fn data_class(mut self, class: &str) -> Self {
        self.data_class = class.to_string();
        self
    }

    /// The provider, when the model's id does not name it ([`provider_of`]).
    pub fn provider(mut self, provider: impl Into<String>) -> Self {
        self.provider = Some(provider.into());
        self
    }

    /// The overlap a found region needs with the claim to confirm it; 0.3 by default.
    pub fn min_iou(mut self, iou: f32) -> Self {
        self.iou = iou;
        self
    }

    /// The score a found region needs to count; 0.25 by default.
    pub fn min_score(mut self, score: f32) -> Self {
        self.min_score = score;
        self
    }

    /// The most output tokens a model answer may take.
    pub fn max_output_tokens(mut self, tokens: i32) -> Self {
        self.max_output_tokens = Some(tokens);
        self
    }

    /// The generation settings every model request starts from, such as a provider
    /// extension.
    pub fn generate_config(mut self, config: adk_core::GenerateContentConfig) -> Self {
        self.config = config;
        self
    }

    /// Builds the verifier.
    ///
    /// # Errors
    ///
    /// [`TeamRoleError::Missing`] without a runtime, or for a remote model without an egress
    /// gate, and [`TeamRoleError::RemoteBackend`] for a runtime with a remote backend.
    pub fn build(self) -> std::result::Result<Verifier, TeamRoleError> {
        let runtime = self.runtime.ok_or_else(|| {
            TeamRoleError::Missing(format!(
                "the verifier '{}' needs a runtime of on-device backends to check claims with",
                self.name
            ))
        })?;
        check_local_runtime("verifier", &self.name, &runtime)?;
        let remote = self.model.as_ref().is_some_and(|m| m.data_locality().leaves_machine());
        if remote && self.gate.is_none() {
            return Err(TeamRoleError::Missing(format!(
                "the verifier '{}' runs on a remote model and needs the egress gate its crops \
                 pass (VerifierBuilder::egress_gate)",
                self.name
            )));
        }
        let destination = self
            .model
            .as_ref()
            .map(|model| destination_of(model.as_ref(), remote, self.hosts.clone()));
        let provider = match &self.model {
            Some(model) => provider_of(model.name(), self.provider.as_deref()),
            None => self.provider.unwrap_or_else(|| "local".to_string()),
        };
        Ok(Verifier {
            name: self.name,
            description: self.description,
            runtime,
            model: self.model,
            gate: self.gate,
            destination,
            data_class: DataClass::new(self.data_class),
            provider,
            iou: self.iou,
            min_score: self.min_score,
            max_output_tokens: self.max_output_tokens,
            config: self.config,
        })
    }
}

#[async_trait]
impl Agent for Verifier {
    fn name(&self) -> &str {
        &self.name
    }

    fn description(&self) -> &str {
        &self.description
    }

    fn sub_agents(&self) -> &[Arc<dyn Agent>] {
        &[]
    }

    async fn run(&self, ctx: Arc<dyn InvocationContext>) -> adk_core::Result<EventStream> {
        let request = request_of(ctx.as_ref());
        let claim: Opinion = request
            .get("claim")
            .cloned()
            .and_then(|value| serde_json::from_value(value).ok())
            .ok_or_else(|| AdkError::agent("a verifier request needs `claim`, an opinion"))?;
        let image = request.get("image").and_then(Value::as_str).unwrap_or_default();
        let source = load_image(ctx.as_ref(), image).await?;
        let regions: Option<RegionSet> =
            request.get("regions").cloned().and_then(|value| serde_json::from_value(value).ok());
        let region = regions
            .as_ref()
            .and_then(|set| set.regions.first())
            .or_else(|| claim.regions.as_ref().and_then(|set| set.regions.first()))
            .and_then(|region| region.shape.bbox())
            .ok_or_else(|| AdkError::agent("a verifier request needs the claimed region"))?;
        let question = request.get("question").and_then(Value::as_str).unwrap_or_default();
        let positive = claim.verdict.clone();
        let negative = format!("not_{positive}");
        let cx = cx_for(ctx.as_ref());
        let verified = self
            .verify(&cx, VerifyRequest::new(question, positive, negative, claim, region, &source))
            .await?;
        let text = serde_json::to_string(&verified.opinion)
            .map_err(|err| AdkError::agent(format!("the opinion could not be encoded: {err}")))?;
        let event = text_event(ctx.as_ref(), &self.name, text);
        Ok(Box::pin(futures::stream::iter([Ok(event)])))
    }
}