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
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
//! Cross-conjunct consistency check.
//!
//! Implements SPEC.md ยง4: prevents suspicious measurement patterns where
//! one conjunct is in insufficient_data while others marginally pass.

use crate::conjunct::ConjunctStatus;
use crate::evidence::{Evidence, SourceValue};
use crate::threshold;
use serde::{Deserialize, Serialize};

/// Result of the consistency check.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsistencyResult {
    pub passed: bool,
    pub failed_rules: Vec<String>,
    pub detail: Option<String>,
}

impl ConsistencyResult {
    /// Create a passing consistency check.
    pub fn pass() -> Self {
        Self {
            passed: true,
            failed_rules: vec![],
            detail: None,
        }
    }

    /// Create a failing consistency check with reason(s).
    pub fn fail(rules: Vec<&str>, detail: String) -> Self {
        Self {
            passed: false,
            failed_rules: rules.iter().map(|s| s.to_string()).collect(),
            detail: Some(detail),
        }
    }
}

/// Check rule 1: no insufficient_data masking.
/// If three conjuncts pass and one is insufficient_data, it's a masking pattern.
fn check_no_insufficient_data_masking(
    conjunct_statuses: &[ConjunctStatus; 4],
) -> Result<(), String> {
    let pass_count = conjunct_statuses
        .iter()
        .filter(|s| **s == ConjunctStatus::Pass)
        .count();
    let insufficient_count = conjunct_statuses
        .iter()
        .filter(|s| **s == ConjunctStatus::InsufficientData)
        .count();

    // If 3 are Pass and 1 is InsufficientData, it's a masking pattern
    if pass_count == 3 && insufficient_count == 1 {
        return Err(
            "One conjunct is insufficient_data while all others pass (masking pattern)".to_string(),
        );
    }
    Ok(())
}

/// Map source IDs to their associated conjuncts and thresholds.
/// Returns (conjunct_index, pass_threshold, floor) tuples.
fn get_source_threshold(source_id: &str) -> Option<Vec<(usize, f64, Option<f64>)>> {
    match source_id {
        "arc-agi-2" => Some(vec![(0, threshold::generality::ARC_AGI_2_PASS, None)]),
        "arc-agi-3" => Some(vec![
            (
                0,
                threshold::generality::ARC_AGI_3_PASS,
                Some(threshold::generality::ARC_AGI_3_FLOOR),
            ),
            (
                2,
                threshold::environmental_transfer::ARC_AGI_3_PASS,
                Some(threshold::environmental_transfer::ARC_AGI_3_FLOOR),
            ),
        ]),
        "hle" => Some(vec![(0, threshold::generality::HLE_PASS, None)]),
        "gpqa-diamond" => Some(vec![(0, threshold::generality::GPQA_DIAMOND_PASS, None)]),
        "gdpval" | "gdpval-aa" => Some(vec![(
            1,
            threshold::economic_substitutability::GDPVAL_PASS,
            None,
        )]),
        "rli" => Some(vec![(
            1,
            threshold::economic_substitutability::RLI_PASS,
            Some(threshold::economic_substitutability::RLI_FLOOR),
        )]),
        "apex-agents" => Some(vec![(
            1,
            threshold::economic_substitutability::APEX_AGENTS_PASS,
            None,
        )]),
        "osworld" => Some(vec![(
            2,
            threshold::environmental_transfer::OSWORLD_PASS,
            None,
        )]),
        "nes" => {
            // NES thresholds TBD in v0.1.x per SPEC.md. For now, skip NES in variance calculation.
            None
        }
        "metr-80pct-time-horizon" | "metr-time-horizon-80pct" => Some(vec![(
            3,
            threshold::autonomous_agency::METR_80PCT_PASS_HOURS,
            Some(threshold::autonomous_agency::METR_80PCT_FLOOR_HOURS),
        )]),
        "re-bench" => Some(vec![(3, threshold::autonomous_agency::REBENCH_PASS, None)]),
        "swe-bench-verified" | "swe-bench-verified-pass5" => Some(vec![(
            3,
            threshold::autonomous_agency::SWEBENCH_VERIFIED_PASS_AT_5,
            None,
        )]),
        _ => None,
    }
}

/// Check rule 2: variance bound.
/// When all four conjuncts pass, min_margin >= 0.5 * max_margin.
fn check_variance_bound(
    evidence: &[Evidence],
    conjunct_statuses: &[ConjunctStatus; 4],
) -> Result<(), String> {
    let all_pass = conjunct_statuses.iter().all(|s| *s == ConjunctStatus::Pass);
    if !all_pass {
        // Variance rule only applies when all conjuncts pass
        return Ok(());
    }

    let mut margins = Vec::new();

    for e in evidence {
        if let Some(thresholds) = get_source_threshold(e.source.as_str()) {
            for (_, pass_threshold, _) in thresholds {
                let raw_value = match e.value {
                    SourceValue::Fraction(f) => f.value(),
                    SourceValue::Hours(h) => h.value(),
                };
                let margin = raw_value / pass_threshold;
                margins.push(margin);
            }
        }
    }

    if margins.is_empty() {
        // No recognized sources; variance check passes trivially
        return Ok(());
    }

    let min_margin = margins.iter().cloned().fold(f64::INFINITY, f64::min);
    let max_margin = margins.iter().cloned().fold(f64::NEG_INFINITY, f64::max);

    const VARIANCE_RATIO: f64 = 0.5;
    if min_margin < VARIANCE_RATIO * max_margin {
        return Err(format!(
            "Variance bound violated: min_margin ({:.3}) < 0.5 * max_margin ({:.3})",
            min_margin, max_margin
        ));
    }

    Ok(())
}

/// Check rule 3: provenance metadata completeness.
/// Every source must have URL, fetch timestamp, and source version/date.
fn check_provenance_metadata(evidence: &[Evidence]) -> Result<(), String> {
    let mut missing_sources = Vec::new();

    for e in evidence {
        let source_id = e.source.as_str();
        let mut issues = Vec::new();

        // Check source_url is present and valid (it's a Url type, so presence is guaranteed by type)
        if e.provenance.source_url.as_str().is_empty() {
            issues.push("source_url");
        }

        // Check fetch_timestamp is present (it's a DateTime, so presence is guaranteed by type)

        // Check source_version or we're lenient here because it's optional in the schema
        // but SPEC.md ยง4 rule 3 says "version or date stamp"
        // The DateTime<Utc> fetch_timestamp serves as the date stamp, so version is optional
        // but if we want to be strict, we could require it. For now, the fetch_timestamp satisfies the "date stamp" requirement.

        if !issues.is_empty() {
            missing_sources.push(format!("{} (missing: {})", source_id, issues.join(", ")));
        }
    }

    if !missing_sources.is_empty() {
        return Err(format!(
            "Provenance metadata incomplete for: {}",
            missing_sources.join("; ")
        ));
    }

    Ok(())
}

/// Evaluate all three consistency check rules.
///
/// Takes the evidence array and the array of per-conjunct statuses (in order:
/// Generality, EconomicSubstitutability, EnvironmentalTransfer, AutonomousAgency).
pub fn consistency_check(
    evidence: &[Evidence],
    conjunct_statuses: &[ConjunctStatus; 4],
) -> ConsistencyResult {
    let mut failed_rules = Vec::new();

    // Rule 1: No insufficient_data masking
    if check_no_insufficient_data_masking(conjunct_statuses).is_err() {
        failed_rules.push("rule_1_insufficient_data_masking");
    }

    // Rule 2: Variance bound
    if check_variance_bound(evidence, conjunct_statuses).is_err() {
        failed_rules.push("rule_2_variance_bound");
    }

    // Rule 3: Provenance metadata
    if check_provenance_metadata(evidence).is_err() {
        failed_rules.push("rule_3_provenance_metadata");
    }

    if failed_rules.is_empty() {
        ConsistencyResult::pass()
    } else {
        let detail = format!("Consistency check failed on: {}", failed_rules.join(", "));
        ConsistencyResult::fail(failed_rules.to_vec(), detail)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::evidence::{
        BoundedFraction, MeasurementId, NonNegativeHours, Provenance, SourceId, SourceValue,
    };
    use chrono::Utc;
    use url::Url;

    fn make_evidence(source: &str, value: f64, is_fraction: bool) -> Evidence {
        Evidence {
            source: SourceId::new(source),
            measurement: MeasurementId::new("test-measurement"),
            value: if is_fraction {
                SourceValue::Fraction(BoundedFraction::new(value).unwrap())
            } else {
                SourceValue::Hours(NonNegativeHours::new(value).unwrap())
            },
            reliability_percentile: 95,
            provenance: Provenance {
                source_url: Url::parse("https://example.com").unwrap(),
                fetch_timestamp: Utc::now(),
                source_version: Some("1.0".to_string()),
                raw_value: format!("{}", value),
            },
        }
    }

    #[test]
    fn rule1_all_pass_with_no_insufficient_data() {
        let statuses = [
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
        ];
        assert!(check_no_insufficient_data_masking(&statuses).is_ok());
    }

    #[test]
    fn rule1_all_pass_with_insufficient_data_fails() {
        let statuses = [
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::InsufficientData,
        ];
        assert!(check_no_insufficient_data_masking(&statuses).is_err());
    }

    #[test]
    fn rule1_not_all_pass_with_insufficient_data_ok() {
        let statuses = [
            ConjunctStatus::Pass,
            ConjunctStatus::Partial,
            ConjunctStatus::Pass,
            ConjunctStatus::InsufficientData,
        ];
        assert!(check_no_insufficient_data_masking(&statuses).is_ok());
    }

    #[test]
    fn rule1_not_all_pass_with_fail_and_insufficient_data_ok() {
        let statuses = [
            ConjunctStatus::Fail,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::InsufficientData,
        ];
        assert!(check_no_insufficient_data_masking(&statuses).is_ok());
    }

    #[test]
    fn rule2_variance_bound_passes_when_not_all_pass() {
        let statuses = [
            ConjunctStatus::Pass,
            ConjunctStatus::Partial,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
        ];
        let evidence = vec![
            make_evidence("arc-agi-2", 0.95, true),
            make_evidence("arc-agi-3", 0.60, true),
        ];
        assert!(check_variance_bound(&evidence, &statuses).is_ok());
    }

    #[test]
    fn rule2_variance_bound_passes_with_reasonable_margins() {
        let statuses = [
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
        ];
        // All fraction sources well above their thresholds with balanced margins
        let evidence = vec![
            make_evidence("arc-agi-2", 0.95, true), // margin: 0.95/0.85 โ‰ˆ 1.118
            make_evidence("gdpval", 0.92, true),    // margin: 0.92/0.85 โ‰ˆ 1.082
            make_evidence("osworld", 0.93, true),   // margin: 0.93/0.85 โ‰ˆ 1.094
            make_evidence("re-bench", 0.80, true),  // margin: 0.80/0.60 โ‰ˆ 1.333
        ];
        // min_margin โ‰ˆ 1.082, max_margin โ‰ˆ 1.333, min >= 0.5*max? 1.082 >= 0.667? Yes
        assert!(check_variance_bound(&evidence, &statuses).is_ok());
    }

    #[test]
    fn rule2_variance_bound_fails_with_extreme_imbalance() {
        let statuses = [
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
        ];
        // Extreme imbalance using fraction and hour mixing to create very different margins
        // One source has tiny margin, another has huge margin
        let evidence = vec![
            make_evidence("arc-agi-2", 0.851, true), // margin: 0.851/0.85 โ‰ˆ 1.001
            make_evidence("gdpval", 0.851, true),    // margin: 0.851/0.85 โ‰ˆ 1.001
            make_evidence("osworld", 0.851, true),   // margin: 0.851/0.85 โ‰ˆ 1.001
            make_evidence("metr-80pct-time-horizon", 8000.0, false), // margin: 8000/168 โ‰ˆ 47.6
        ];
        // min_margin โ‰ˆ 1.001, max_margin โ‰ˆ 47.6
        // Need: min >= 0.5*max => 1.001 >= 23.8? No! This should fail
        assert!(check_variance_bound(&evidence, &statuses).is_err());
    }

    #[test]
    fn rule2_empty_evidence_passes() {
        let statuses = [
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
        ];
        let evidence = vec![];
        assert!(check_variance_bound(&evidence, &statuses).is_ok());
    }

    #[test]
    fn rule2_unknown_sources_passes() {
        let statuses = [
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
        ];
        let evidence = vec![make_evidence("unknown-source", 0.95, true)];
        // Unknown sources are ignored, so margins is empty, check passes trivially
        assert!(check_variance_bound(&evidence, &statuses).is_ok());
    }

    #[test]
    fn rule3_complete_provenance_passes() {
        let evidence = vec![
            make_evidence("arc-agi-2", 0.95, true),
            make_evidence("gdpval", 0.90, true),
        ];
        assert!(check_provenance_metadata(&evidence).is_ok());
    }

    #[test]
    fn rule3_empty_evidence_passes() {
        let evidence = vec![];
        assert!(check_provenance_metadata(&evidence).is_ok());
    }

    #[test]
    fn consistency_check_all_pass_all_rules() {
        let statuses = [
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
        ];
        let evidence = vec![
            make_evidence("arc-agi-2", 0.90, true), // margin: 0.90/0.85 โ‰ˆ 1.06
            make_evidence("gdpval", 0.88, true),    // margin: 0.88/0.85 โ‰ˆ 1.04
            make_evidence("osworld", 0.90, true),   // margin: 0.90/0.85 โ‰ˆ 1.06
            make_evidence("re-bench", 0.75, true),  // margin: 0.75/0.60 = 1.25
        ];
        let result = consistency_check(&evidence, &statuses);
        assert!(result.passed, "Expected pass but got: {:?}", result);
        assert!(result.failed_rules.is_empty());
    }

    #[test]
    fn consistency_check_rule1_fails() {
        let statuses = [
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::InsufficientData,
        ];
        let evidence = vec![make_evidence("arc-agi-2", 0.95, true)];
        let result = consistency_check(&evidence, &statuses);
        assert!(!result.passed);
        assert!(
            result
                .failed_rules
                .contains(&"rule_1_insufficient_data_masking".to_string())
        );
    }

    #[test]
    fn consistency_check_rule2_fails() {
        let statuses = [
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
        ];
        let evidence = vec![
            make_evidence("arc-agi-2", 0.99, true),
            make_evidence("gdpval", 0.851, true),
            make_evidence("osworld", 0.90, true),
            make_evidence("metr-80pct-time-horizon", 8000.0, false),
        ];
        let result = consistency_check(&evidence, &statuses);
        assert!(!result.passed);
        assert!(
            result
                .failed_rules
                .contains(&"rule_2_variance_bound".to_string())
        );
    }

    #[test]
    fn consistency_check_multiple_rules_fail() {
        let statuses = [
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
            ConjunctStatus::Pass,
        ];
        let evidence = vec![
            make_evidence("arc-agi-2", 0.99, true),
            make_evidence("gdpval", 0.851, true),
            make_evidence("osworld", 0.90, true),
            make_evidence("metr-80pct-time-horizon", 8000.0, false),
        ];
        let result = consistency_check(&evidence, &statuses);
        assert!(!result.passed);
        // Rule 2 (variance bound) should fail due to extreme imbalance
        assert!(
            result
                .failed_rules
                .contains(&"rule_2_variance_bound".to_string())
        );
    }

    #[test]
    fn consistency_check_partial_or_fail_status_allows_insufficient_data() {
        let statuses = [
            ConjunctStatus::Pass,
            ConjunctStatus::Partial,
            ConjunctStatus::Pass,
            ConjunctStatus::InsufficientData,
        ];
        let evidence = vec![
            make_evidence("arc-agi-2", 0.95, true),
            make_evidence("gdpval", 0.90, true),
            make_evidence("osworld", 0.95, true),
            make_evidence("metr-80pct-time-horizon", 500.0, false),
        ];
        let result = consistency_check(&evidence, &statuses);
        // Only rule3 would fail if provenance is broken, but we have good provenance
        // Rule1 doesn't apply (not all pass), rule2 doesn't apply (not all pass)
        assert!(result.passed);
    }
}