aimx/inference/validate.rs
1//! Validation of inference responses into assignment rules.
2//!
3//! This module maps parsed [`Response`] items into assignment rules in a [`Context`]
4//! / workflow, using the lowercase-assignment / uppercase-response convention.
5//! Only type-correct values are written; all other cases are ignored.
6use crate::{
7 aim::{Context, ContextLike, WorkflowLike},
8 inference::Response,
9};
10use std::collections::HashMap;
11use std::sync::Arc;
12
13/// Validates inference responses and assigns them to matching assignment rules in an AIM structure.
14///
15/// This function processes a collection of parsed inference responses and assigns them to
16/// corresponding assignment rules in the AIM structure. Assignment rules are identified by
17/// all-lowercase identifiers (e.g., `summary`, `temperature`). For a lowercase rule `foo`,
18/// its uppercase form `FOO` is used to match against parsed response keys.
19///
20/// # Process
21///
22/// The validation follows a systematic workflow:
23///
24/// 1. **Rule Enumeration** - Iterates through all rules in the AIM structure
25/// 2. **Assignment Detection** - Identifies rules with lowercase identifiers
26/// 3. **Identifier Conversion** - Converts rule identifiers to uppercase for matching
27/// 4. **Response Matching** - Finds responses with matching uppercase identifiers
28/// 5. **Type Validation** - Validates responses against rule type definitions
29/// 6. **Value Assignment** - Assigns validated values through the execution context
30///
31/// # Fault Tolerance
32///
33/// The validation process is intentionally fault-tolerant to handle the probabilistic
34/// nature of AI model responses:
35///
36/// - **Non-matching Responses**: Silently ignores responses without matching assignment rules
37/// - **Type Validation Failures**: Silently ignores responses that fail type conversion
38/// - **Partial Success**: Returns success if at least one response is validated
39///
40/// This design ensures that workflows continue even when some inference responses
41/// are malformed or unexpected.
42///
43/// # Parameters
44///
45/// * `context` - A mutable reference to the execution context for value assignment
46/// * `responses` - A [`HashMap`] of parsed responses, keyed by uppercase identifiers
47///
48/// # Returns
49///
50/// * `usize` - Number of successfully validated and assigned responses
51///
52pub fn validate_responses(context: &mut Context, responses: HashMap<Arc<str>, Response>) -> usize {
53 let mut validated_responses: usize = 0;
54
55 // Iterate through each rule in the workflow to find assignment rules
56 for rule in context.source().iter_rules() {
57 // Identify assignment rules (all-lowercase identifiers)
58 if rule.is_assignment() {
59 // Convert rule identifier to uppercase for matching with responses
60 // Example: "_example" becomes "EXAMPLE"
61 let ucid = rule.ucid();
62
63 // Check if there's a matching response for this assignment rule
64 if let Some(response) = responses.get(&ucid) {
65 // Attempt to convert the response to the rule's expected type
66 // This performs type validation and conversion
67 if let Ok(value) = response.to_value(rule.typedef()) {
68 // Create a reference to the rule for assignment
69 let identifier = rule.identifier();
70
71 // Assign the validated value through the context
72 // The context handles the actual assignment mechanism
73 if context.set_value(&identifier, value).is_ok() {
74 validated_responses += 1;
75 }
76 }
77 // Note: Type conversion failures are silently ignored
78 // This is intentional fault tolerance for probabilistic AI responses
79 }
80 // Note: Non-matching responses are silently ignored
81 // This allows workflows to continue even with partial responses
82 }
83 }
84
85 // Return the number of validated responses
86 validated_responses
87}