agi4-core 0.1.0

Pure verdict logic for AGI/4 attestation
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
//! Verdict function and verdict enumeration.
//!
//! The verdict is the load-bearing output of the runner.
//! This function is pure and total: same inputs always produce same outputs,
//! with no panics on any valid input.

use crate::conjunct::ConjunctStatus;
use serde::{Deserialize, Serialize};

/// The top-level verdict from the runner.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "verdict")]
pub enum Verdict {
    #[serde(rename = "attested")]
    Attested,
    #[serde(rename = "not_attested")]
    NotAttested {
        #[serde(skip_serializing_if = "Vec::is_empty")]
        reasons: Vec<String>,
    },
    #[serde(rename = "insufficient_data")]
    InsufficientData {
        #[serde(skip_serializing_if = "Vec::is_empty")]
        missing: Vec<String>,
    },
}

impl Verdict {
    /// Create an attested verdict.
    pub fn attested() -> Self {
        Verdict::Attested
    }

    /// Create a not_attested verdict with reason(s).
    pub fn not_attested(reasons: Vec<&str>) -> Self {
        Verdict::NotAttested {
            reasons: reasons.iter().map(|s| s.to_string()).collect(),
        }
    }

    /// Create an insufficient_data verdict.
    pub fn insufficient_data(missing: Vec<&str>) -> Self {
        Verdict::InsufficientData {
            missing: missing.iter().map(|s| s.to_string()).collect(),
        }
    }
}

/// Compute the verdict from four conjunct statuses and consistency check result.
///
/// Takes conjunct statuses in order: Generality, EconomicSubstitutability,
/// EnvironmentalTransfer, AutonomousAgency.
///
/// Returns a verdict following SPEC.md §5 rules:
/// - All four pass + consistency pass → Attested
/// - All four pass + consistency fail → NotAttested
/// - Any fail → NotAttested (fail dominates)
/// - Any partial or fail (except isolated insufficient_data) → NotAttested
/// - Any insufficient_data with no fail → InsufficientData
pub fn verdict(conjunct_statuses: &[ConjunctStatus; 4], consistency_passed: bool) -> Verdict {
    let conjuncts = [
        ("generality", conjunct_statuses[0]),
        ("economic_substitutability", conjunct_statuses[1]),
        ("environmental_transfer", conjunct_statuses[2]),
        ("autonomous_agency", conjunct_statuses[3]),
    ];

    // Rule: Any fail → not_attested (fail dominates all other statuses)
    let fail_count = conjuncts
        .iter()
        .filter(|(_, s)| *s == ConjunctStatus::Fail)
        .count();
    if fail_count > 0 {
        let failed: Vec<&str> = conjuncts
            .iter()
            .filter(|(_, s)| *s == ConjunctStatus::Fail)
            .map(|(name, _)| *name)
            .collect();
        return Verdict::not_attested(failed);
    }

    // Rule: Any partial → not_attested
    let partial_count = conjuncts
        .iter()
        .filter(|(_, s)| *s == ConjunctStatus::Partial)
        .count();
    if partial_count > 0 {
        let partialed: Vec<&str> = conjuncts
            .iter()
            .filter(|(_, s)| *s == ConjunctStatus::Partial)
            .map(|(name, _)| *name)
            .collect();
        return Verdict::not_attested(partialed);
    }

    // At this point: all statuses are either Pass or InsufficientData
    let pass_count = conjuncts
        .iter()
        .filter(|(_, s)| *s == ConjunctStatus::Pass)
        .count();
    let insufficient_count = conjuncts
        .iter()
        .filter(|(_, s)| *s == ConjunctStatus::InsufficientData)
        .count();

    // Rule: If any insufficient_data exists (and no fail or partial), verdict is insufficient_data
    if insufficient_count > 0 {
        let insufficient: Vec<&str> = conjuncts
            .iter()
            .filter(|(_, s)| *s == ConjunctStatus::InsufficientData)
            .map(|(name, _)| *name)
            .collect();
        return Verdict::insufficient_data(insufficient);
    }

    // All four must be Pass if we reach here
    if pass_count == 4 {
        if consistency_passed {
            return Verdict::attested();
        } else {
            return Verdict::not_attested(vec!["consistency_check"]);
        }
    }

    // This branch should never be reached given the above logic
    // (all statuses are either Pass or InsufficientData, and we already handled insufficient_data above)
    Verdict::not_attested(vec!["unknown_state"])
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn verdict_all_pass_consistency_pass() {
        let statuses = [
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
        ];
        let v = verdict(&statuses, true);
        assert!(matches!(v, Verdict::Attested));
    }

    #[test]
    fn verdict_all_pass_consistency_fail() {
        let statuses = [
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
        ];
        let v = verdict(&statuses, false);
        assert!(matches!(v, Verdict::NotAttested { .. }));
    }

    #[test]
    fn verdict_any_fail() {
        let statuses = [
            ConjunctStatus::Fail,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
        ];
        let v = verdict(&statuses, true);
        assert!(matches!(v, Verdict::NotAttested { .. }));
    }

    #[test]
    fn verdict_any_partial() {
        let statuses = [
            ConjunctStatus::Pass,
            ConjunctStatus::Partial,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
        ];
        let v = verdict(&statuses, true);
        assert!(matches!(v, Verdict::NotAttested { .. }));
    }

    #[test]
    fn verdict_any_insufficient_data_no_fail() {
        let statuses = [
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::InsufficientData,
        ];
        let v = verdict(&statuses, true);
        assert!(matches!(v, Verdict::InsufficientData { .. }));
    }

    #[test]
    fn verdict_fail_dominates_insufficient_data() {
        let statuses = [
            ConjunctStatus::Fail,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::InsufficientData,
        ];
        let v = verdict(&statuses, true);
        // Fail dominates insufficient_data
        assert!(matches!(v, Verdict::NotAttested { .. }));
    }

    #[test]
    fn verdict_partial_dominates_insufficient_data() {
        let statuses = [
            ConjunctStatus::Partial,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::InsufficientData,
        ];
        let v = verdict(&statuses, true);
        // Partial dominates insufficient_data
        assert!(matches!(v, Verdict::NotAttested { .. }));
    }

    #[test]
    fn verdict_exhaustive_512_cases() {
        let statuses_list = vec![
            ConjunctStatus::Pass,
            ConjunctStatus::Partial,
            ConjunctStatus::Fail,
            ConjunctStatus::InsufficientData,
        ];

        // Test all 4^4 = 256 combinations for each consistency check state (2 states)
        // Total: 512 combinations
        let mut case_count = 0;
        for g in &statuses_list {
            for e in &statuses_list {
                for env in &statuses_list {
                    for a in &statuses_list {
                        for consistency in &[true, false] {
                            let statuses = [*g, *e, *env, *a];
                            let _v = verdict(&statuses, *consistency);
                            // If we reach here without panic, the verdict function is total
                            case_count += 1;
                        }
                    }
                }
            }
        }

        assert_eq!(case_count, 512);
    }

    #[test]
    fn verdict_reasons_match_failed_conjuncts() {
        let statuses = [
            ConjunctStatus::Fail,
            ConjunctStatus::Partial,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
        ];
        let v = verdict(&statuses, true);
        match v {
            Verdict::NotAttested { reasons } => {
                // Fail has higher priority, so only generality should be in reasons
                assert!(!reasons.is_empty());
                assert!(reasons.contains(&"generality".to_string()));
            }
            _ => panic!("Expected NotAttested"),
        }
    }

    #[test]
    fn verdict_insufficient_data_reasons() {
        let statuses = [
            ConjunctStatus::Pass,
            ConjunctStatus::InsufficientData,
            ConjunctStatus::Pass,
            ConjunctStatus::InsufficientData,
        ];
        let v = verdict(&statuses, true);
        match v {
            Verdict::InsufficientData { missing } => {
                assert_eq!(missing.len(), 2);
                assert!(missing.contains(&"economic_substitutability".to_string()));
                assert!(missing.contains(&"autonomous_agency".to_string()));
            }
            _ => panic!("Expected InsufficientData"),
        }
    }
}

#[cfg(test)]
mod property_tests {
    use super::*;

    #[test]
    fn property_verdict_deterministic_on_repeated_calls() {
        let statuses = [
            ConjunctStatus::Pass,
            ConjunctStatus::Partial,
            ConjunctStatus::Fail,
            ConjunctStatus::InsufficientData,
        ];
        let v1 = verdict(&statuses, true);
        let v2 = verdict(&statuses, true);
        let v3 = verdict(&statuses, true);
        let d1 = format!("{:?}", v1);
        let d2 = format!("{:?}", v2);
        let d3 = format!("{:?}", v3);
        assert_eq!(d1, d2);
        assert_eq!(d2, d3);
    }

    #[test]
    fn property_verdict_fail_always_not_attested() {
        let status_options = vec![
            ConjunctStatus::Pass,
            ConjunctStatus::Partial,
            ConjunctStatus::Fail,
            ConjunctStatus::InsufficientData,
        ];
        for e in &status_options {
            for env in &status_options {
                for a in &status_options {
                    let statuses = [ConjunctStatus::Fail, *e, *env, *a];
                    let v = verdict(&statuses, true);
                    assert!(matches!(v, Verdict::NotAttested { .. }));
                }
            }
        }
    }

    #[test]
    fn property_verdict_partial_dominates_insufficient() {
        let status_options = vec![
            ConjunctStatus::Pass,
            ConjunctStatus::Partial,
            ConjunctStatus::InsufficientData,
        ];
        for e in &status_options {
            for env in &status_options {
                for a in &status_options {
                    let statuses = [ConjunctStatus::Partial, *e, *env, *a];
                    let v = verdict(&statuses, true);
                    assert!(matches!(v, Verdict::NotAttested { .. }));
                }
            }
        }
    }

    #[test]
    fn property_verdict_insufficient_only_without_fail_partial() {
        let status_options = vec![ConjunctStatus::Pass, ConjunctStatus::InsufficientData];
        for e in &status_options {
            for env in &status_options {
                for a in &status_options {
                    for consistency in &[true, false] {
                        let statuses = [ConjunctStatus::Pass, *e, *env, *a];
                        let v = verdict(&statuses, *consistency);
                        let has_insufficient = [*e, *env, *a]
                            .iter()
                            .any(|s| *s == ConjunctStatus::InsufficientData);
                        if has_insufficient {
                            assert!(matches!(v, Verdict::InsufficientData { .. }));
                        }
                    }
                }
            }
        }
    }

    #[test]
    fn property_verdict_all_pass_requires_consistency() {
        for consistency in &[true, false] {
            let statuses = [
                ConjunctStatus::Pass,
                ConjunctStatus::Pass,
                ConjunctStatus::Pass,
                ConjunctStatus::Pass,
            ];
            let v = verdict(&statuses, *consistency);
            if *consistency {
                assert!(matches!(v, Verdict::Attested));
            } else {
                assert!(matches!(v, Verdict::NotAttested { .. }));
            }
        }
    }

    #[test]
    fn property_verdict_not_attested_always_has_reasons() {
        let status_options = vec![
            ConjunctStatus::Pass,
            ConjunctStatus::Partial,
            ConjunctStatus::Fail,
            ConjunctStatus::InsufficientData,
        ];
        for g in &status_options {
            for e in &status_options {
                for env in &status_options {
                    for a in &status_options {
                        for consistency in &[true, false] {
                            let statuses = [*g, *e, *env, *a];
                            let v = verdict(&statuses, *consistency);
                            if let Verdict::NotAttested { reasons } = v {
                                assert!(!reasons.is_empty(), "NotAttested must have reasons");
                            }
                        }
                    }
                }
            }
        }
    }

    #[test]
    fn property_verdict_insufficient_always_has_missing() {
        let status_options = vec![ConjunctStatus::Pass, ConjunctStatus::InsufficientData];
        for g in &status_options {
            for e in &status_options {
                for env in &status_options {
                    for a in &status_options {
                        for consistency in &[true, false] {
                            let statuses = [*g, *e, *env, *a];
                            let v = verdict(&statuses, *consistency);
                            if let Verdict::InsufficientData { missing } = v {
                                assert!(!missing.is_empty(), "InsufficientData must have missing");
                            }
                        }
                    }
                }
            }
        }
    }
}