use serde_json::{Value, json};
use super::{
RelationshipKind, RelationshipPolicy, TeamContextPolicy, TeamHistoryPolicy, TeamMemberSpec,
TeamPolicy, TeamRelationship, TeamSpec,
};
pub const PERCEIVER_CAPABILITY: &str = "vision.perceive.local";
pub const REASONER_CAPABILITY: &str = "vision.reason.released";
pub const VERIFIER_CAPABILITY: &str = "vision.verify.pixels";
const DIGEST_PATTERN: &str = "^sha256:[0-9a-f]{64}$";
pub fn perceiver_input_schema() -> Value {
json!({
"type": "object",
"additionalProperties": false,
"required": ["image"],
"properties": {
"image": { "type": "string", "minLength": 1, "description": "A sha256: digest or an artifact name." },
"question": { "type": "string" },
"classes": { "type": "array", "items": { "type": "string", "minLength": 1 } }
}
})
}
pub fn reasoner_input_schema() -> Value {
json!({
"type": "object",
"additionalProperties": false,
"required": ["question", "regions"],
"properties": {
"question": { "type": "string", "minLength": 1 },
"verdicts": {
"type": "array",
"items": { "type": "string", "minLength": 1 },
"minItems": 2,
"description": "The verdicts the answer must be one of."
},
"regions": { "type": "object", "description": "A RegionSet in source pixels." },
"measurements": { "type": "array", "items": { "type": "object" } },
"crops": {
"type": "array",
"items": { "type": "string", "pattern": DIGEST_PATTERN },
"description": "Egress digests of crops the egress gate released."
}
}
})
}
pub fn verifier_input_schema() -> Value {
json!({
"type": "object",
"additionalProperties": false,
"required": ["claim", "image"],
"properties": {
"claim": opinion_schema(),
"image": { "type": "string", "pattern": DIGEST_PATTERN },
"regions": { "type": "object", "description": "A RegionSet in source pixels." },
"question": { "type": "string" }
}
})
}
pub fn opinion_schema() -> Value {
json!({
"type": "object",
"required": ["verdict", "confidence"],
"properties": {
"verdict": { "type": "string", "minLength": 1 },
"label": { "type": "string" },
"confidence": { "type": "number", "minimum": 0, "maximum": 1 },
"regions": { "type": "object" },
"rationale": { "type": "string" }
}
})
}
fn role_policy(input: Value, output: Option<Value>) -> RelationshipPolicy {
RelationshipPolicy {
input_schema: Some(input),
output_schema: output,
context: Some(TeamContextPolicy::Isolated),
history: TeamHistoryPolicy::None,
..RelationshipPolicy::default()
}
}
pub(super) fn lower(
name: &str,
coordinator: &str,
perceiver: &str,
reasoner: &str,
verifier: &str,
policy: &TeamPolicy,
) -> TeamSpec {
TeamSpec {
name: name.to_string(),
description: "Perceiver, reasoner and verifier team compiled from a portable architecture \
template"
.to_string(),
coordinator: coordinator.to_string(),
members: vec![
TeamMemberSpec::new(coordinator).with_description(
"Asks the perceiver, reasoner and verifier, and escalates a disagreement to a \
person with both opinions and the evidence.",
),
TeamMemberSpec::new(perceiver)
.with_description(
"Perceives on the device with a local model or none; holds no tool with a \
remote destination.",
)
.with_capabilities([PERCEIVER_CAPABILITY]),
TeamMemberSpec::new(reasoner)
.with_description(
"Reasons over regions, measurements and redacted crops the egress gate \
released; holds no perception tool.",
)
.with_capabilities([REASONER_CAPABILITY]),
TeamMemberSpec::new(verifier)
.with_description(
"Re-checks a claim against the pixels with local perception, by default on \
a different provider from the reasoner.",
)
.with_capabilities([VERIFIER_CAPABILITY]),
],
relationships: vec![
TeamRelationship::new(coordinator, perceiver, RelationshipKind::Delegate)
.with_policy(role_policy(perceiver_input_schema(), None)),
TeamRelationship::new(coordinator, reasoner, RelationshipKind::Delegate)
.with_policy(role_policy(reasoner_input_schema(), Some(opinion_schema()))),
TeamRelationship::new(coordinator, verifier, RelationshipKind::Delegate)
.with_policy(role_policy(verifier_input_schema(), Some(opinion_schema()))),
],
policy: policy.clone(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::TeamArchitectureTemplate;
fn template() -> TeamArchitectureTemplate {
TeamArchitectureTemplate::PerceiveReasonVerify {
name: "inspector".to_string(),
coordinator: "lead".to_string(),
perceiver: "perceiver".to_string(),
reasoner: "reasoner".to_string(),
verifier: "verifier".to_string(),
policy: TeamPolicy::default(),
}
}
#[test]
fn lowers_to_three_isolated_role_edges() {
let spec = template().lower().unwrap();
assert_eq!(spec.coordinator, "lead");
let capabilities: Vec<(&str, Vec<String>)> = spec
.members
.iter()
.map(|member| (member.name.as_str(), member.required_capabilities.clone()))
.collect();
assert_eq!(
capabilities,
vec![
("lead", vec![]),
("perceiver", vec![PERCEIVER_CAPABILITY.to_string()]),
("reasoner", vec![REASONER_CAPABILITY.to_string()]),
("verifier", vec![VERIFIER_CAPABILITY.to_string()]),
]
);
for edge in &spec.relationships {
assert_eq!(edge.from, "lead");
assert_eq!(edge.kind, RelationshipKind::Delegate);
assert_eq!(edge.policy.context, Some(TeamContextPolicy::Isolated));
assert_eq!(edge.policy.history, TeamHistoryPolicy::None);
}
assert_eq!(spec.relationships[1].policy.input_schema, Some(reasoner_input_schema()));
assert_eq!(spec.relationships[1].policy.output_schema, Some(opinion_schema()));
}
#[test]
fn a_reasoner_request_cannot_carry_pixels() {
let validator = jsonschema::validator_for(&reasoner_input_schema()).unwrap();
let digest = format!("sha256:{}", "a".repeat(64));
let admitted = json!({
"question": "Is the part defective?",
"regions": { "image": digest, "regions": [] },
"crops": [digest],
});
assert!(validator.is_valid(&admitted));
let inline_image = json!({
"question": "Is the part defective?",
"regions": {},
"image": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGNgYGBgAAAABQABpfZFQAAAAABJRU5ErkJggg==",
});
assert!(!validator.is_valid(&inline_image));
let base64_as_crop = json!({
"question": "Is the part defective?",
"regions": {},
"crops": ["iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJ"],
});
assert!(!validator.is_valid(&base64_as_crop));
}
#[test]
fn a_role_named_twice_is_refused() {
let error = TeamArchitectureTemplate::PerceiveReasonVerify {
name: "inspector".to_string(),
coordinator: "lead".to_string(),
perceiver: "same".to_string(),
reasoner: "same".to_string(),
verifier: "verifier".to_string(),
policy: TeamPolicy::default(),
}
.lower()
.unwrap_err();
assert_eq!(error, crate::TeamError::DuplicateMember("same".to_string()));
}
#[test]
fn the_template_round_trips_as_data() {
let value = serde_json::to_value(template()).unwrap();
assert_eq!(value["architecture"], "perceiveReasonVerify");
let back: TeamArchitectureTemplate = serde_json::from_value(value).unwrap();
assert_eq!(back, template());
}
}