ggen-domain 3.2.0

Domain logic layer for ggen - pure business logic without CLI dependencies
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
//! Proof Carrier for Overlays - Auditable Justification for ΔΣ Proposals
//!
//! A ΔΣ proposal is only actionable if it carries auditable proof:
//! - Evidence chain from Γ observations
//! - Test results validating the change
//! - Expected improvements with impact metrics
//! - Risk assessment and mitigation strategies
//! - Doctrine alignment verification

use super::ahi_contract::{AHIError, Proposal};
use super::ontology_proposal_engine::OntologySigmaProposal;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// A single test result for validating a proposal
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TestResult {
    pub test_id: String,
    pub test_name: String,
    pub category: TestCategory,
    pub passed: bool,
    pub message: String,
    pub execution_time_ms: f64,
    pub assertions_total: usize,
    pub assertions_passed: usize,
}

/// Test categories for proposal validation
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TestCategory {
    /// Unit tests for isolated logic
    Unit,
    /// Integration tests with other components
    Integration,
    /// Performance benchmarks
    Performance,
    /// Doctrine constraint validation
    DoctrineCompliance,
    /// Backward compatibility checks
    Compatibility,
    /// Regression prevention tests
    Regression,
}

impl std::fmt::Display for TestCategory {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TestCategory::Unit => write!(f, "Unit"),
            TestCategory::Integration => write!(f, "Integration"),
            TestCategory::Performance => write!(f, "Performance"),
            TestCategory::DoctrineCompliance => write!(f, "DoctrineCompliance"),
            TestCategory::Compatibility => write!(f, "Compatibility"),
            TestCategory::Regression => write!(f, "Regression"),
        }
    }
}

/// Evidence link in the justification chain
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvidenceLink {
    /// Observation or finding ID from Γ
    pub source_id: String,
    /// Type of evidence (metric, anomaly, user_report, etc.)
    pub evidence_type: String,
    /// Extracted metric or property
    pub property: String,
    /// Value supporting the proposal
    pub value: f64,
    /// Weight in overall justification (0-1)
    pub weight: f64,
    /// Timestamp when evidence was observed
    pub timestamp: u64,
}

/// Impact projection for a proposal
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImpactProjection {
    /// Estimated coverage improvement (0-100%)
    pub coverage_improvement: f64,
    /// Expected performance delta (negative = improvement)
    pub performance_delta_ticks: f64,
    /// Adoption impact (estimated tenant adoption increase)
    pub adoption_impact: f64,
    /// Revenue impact (estimated % change)
    pub revenue_impact: f64,
    /// Guard compliance improvement (0-100%)
    pub compliance_improvement: f64,
    /// Confidence level in projections (0-1)
    pub confidence: f64,
}

/// Risk mitigation strategy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RiskMitigation {
    pub risk_id: String,
    pub risk_description: String,
    pub risk_probability: f64, // 0-1
    pub risk_impact: f64,      // 0-100
    pub mitigation_strategy: String,
    pub rollback_plan: String,
    pub monitoring_alerts: Vec<String>,
}

/// Proof Carrier - ΔΣ proposal with complete auditable justification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProofCarrier {
    /// Unique ID for this proof carrier
    pub id: String,

    /// The actual proposal being justified
    pub proposal: OntologySigmaProposal,

    /// Complete evidence chain from Γ
    pub evidence_chain: Vec<EvidenceLink>,

    /// Test results validating the proposal
    pub validation_tests: Vec<TestResult>,

    /// Expected impact projections
    pub impact_projection: ImpactProjection,

    /// Risk analysis and mitigation
    pub risk_mitigations: Vec<RiskMitigation>,

    /// Overall risk score (0-100, 0=safe, 100=dangerous)
    pub total_risk_score: f64,

    /// Doctrine alignment verification details
    pub doctrine_checks: HashMap<String, bool>,

    /// Approval chain (who approved, when)
    pub approvals: Vec<Approval>,

    /// Proof status
    pub status: ProofStatus,

    /// Timestamp when proof was created
    pub created_at: u64,

    /// Expiry timestamp (proofs may become stale)
    pub expires_at: u64,
}

/// Approval entry in the chain
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Approval {
    pub approver_id: String,
    pub approved_at: u64,
    pub reason: String,
}

/// Status of a proof carrier
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ProofStatus {
    /// Generated but not yet fully tested
    Pending,
    /// All tests passed, ready for review
    TestsPassed,
    /// Approved by governance
    Approved,
    /// Executed (overlay applied)
    Executed,
    /// Rejected (failed tests or doctrine)
    Rejected,
    /// Reverted after execution
    Reverted,
    /// Expired (proof is too old)
    Expired,
}

impl std::fmt::Display for ProofStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ProofStatus::Pending => write!(f, "Pending"),
            ProofStatus::TestsPassed => write!(f, "TestsPassed"),
            ProofStatus::Approved => write!(f, "Approved"),
            ProofStatus::Executed => write!(f, "Executed"),
            ProofStatus::Rejected => write!(f, "Rejected"),
            ProofStatus::Reverted => write!(f, "Reverted"),
            ProofStatus::Expired => write!(f, "Expired"),
        }
    }
}

impl ProofCarrier {
    /// Create a new proof carrier for a proposal
    pub fn new(proposal: OntologySigmaProposal, expires_in_seconds: u64) -> Self {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        Self {
            id: format!("proof-{}-{}", proposal.id, now),
            proposal,
            evidence_chain: Vec::new(),
            validation_tests: Vec::new(),
            impact_projection: ImpactProjection {
                coverage_improvement: 0.0,
                performance_delta_ticks: 0.0,
                adoption_impact: 0.0,
                revenue_impact: 0.0,
                compliance_improvement: 0.0,
                confidence: 0.0,
            },
            risk_mitigations: Vec::new(),
            total_risk_score: 0.0,
            doctrine_checks: HashMap::new(),
            approvals: Vec::new(),
            status: ProofStatus::Pending,
            created_at: now,
            expires_at: now + expires_in_seconds,
        }
    }

    /// Add evidence from Γ observation
    pub fn add_evidence(&mut self, link: EvidenceLink) -> Result<(), AHIError> {
        if link.weight <= 0.0 || link.weight > 1.0 {
            return Err(AHIError::InvalidConfig(
                "Evidence weight must be between 0 and 1".to_string(),
            ));
        }
        self.evidence_chain.push(link);
        Ok(())
    }

    /// Add a validation test result
    pub fn add_test_result(&mut self, test: TestResult) {
        self.validation_tests.push(test);
    }

    /// Set impact projection
    pub fn set_impact(&mut self, impact: ImpactProjection) {
        self.impact_projection = impact;
    }

    /// Add a risk mitigation strategy
    pub fn add_risk_mitigation(&mut self, mitigation: RiskMitigation) {
        self.risk_mitigations.push(mitigation);
    }

    /// Verify doctrine compliance for the proposal
    pub fn verify_doctrine(&mut self, constraints: &[(String, bool)]) {
        self.doctrine_checks = constraints.iter().cloned().collect();
    }

    /// Calculate composite risk score
    pub fn calculate_risk_score(&mut self) -> f64 {
        if self.risk_mitigations.is_empty() {
            return self.proposal.risk_score;
        }

        let mut weighted_risk = self.proposal.risk_score * 0.5;

        for mitigation in &self.risk_mitigations {
            let risk_contribution = mitigation.risk_probability * mitigation.risk_impact;
            weighted_risk += risk_contribution * 0.25 / self.risk_mitigations.len() as f64;
        }

        let test_pass_rate = if self.validation_tests.is_empty() {
            0.0
        } else {
            let passed = self.validation_tests.iter().filter(|t| t.passed).count();
            (passed as f64 / self.validation_tests.len() as f64) * 0.25
        };

        self.total_risk_score = (weighted_risk - test_pass_rate).max(0.0).min(100.0);
        self.total_risk_score
    }

    /// Check if all tests passed
    pub fn all_tests_passed(&self) -> bool {
        if self.validation_tests.is_empty() {
            return false;
        }
        self.validation_tests.iter().all(|t| t.passed)
    }

    /// Check if doctrine aligned
    pub fn is_doctrine_aligned(&self) -> bool {
        if self.doctrine_checks.is_empty() {
            return self.proposal.doctrine_aligned;
        }
        self.doctrine_checks.values().all(|&v| v)
    }

    /// Check if proof is fresh (not expired)
    pub fn is_fresh(&self) -> bool {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
        now <= self.expires_at
    }

    /// Mark as approved
    pub fn approve(&mut self, approver_id: &str, reason: &str) -> Result<(), AHIError> {
        if self.status != ProofStatus::TestsPassed {
            return Err(AHIError::InvalidConfig(
                "Can only approve from TestsPassed status".to_string(),
            ));
        }
        self.approvals.push(Approval {
            approver_id: approver_id.to_string(),
            approved_at: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
            reason: reason.to_string(),
        });
        self.status = ProofStatus::Approved;
        Ok(())
    }

    /// Finalize validation tests - move to TestsPassed if all passed
    pub fn finalize_tests(&mut self) -> Result<(), AHIError> {
        if self.validation_tests.is_empty() {
            return Err(AHIError::InvalidConfig(
                "No test results recorded".to_string(),
            ));
        }

        if self.all_tests_passed() {
            self.status = ProofStatus::TestsPassed;
            Ok(())
        } else {
            self.status = ProofStatus::Rejected;
            let failed_tests: Vec<_> = self
                .validation_tests
                .iter()
                .filter(|t| !t.passed)
                .map(|t| t.test_name.clone())
                .collect();
            Err(AHIError::InsufficientJustification(format!(
                "Tests failed: {:?}",
                failed_tests
            )))
        }
    }

    /// Validate complete proof carrier for promotion
    pub fn validate_for_promotion(&mut self) -> Result<(), AHIError> {
        // Refresh freshness check
        if !self.is_fresh() {
            self.status = ProofStatus::Expired;
            return Err(AHIError::InvalidConfig("Proof carrier expired".to_string()));
        }

        // Validate evidence chain
        if self.evidence_chain.is_empty() {
            return Err(AHIError::InsufficientJustification(
                "No evidence chain for proposal".to_string(),
            ));
        }

        // Validate doctrine alignment
        if !self.is_doctrine_aligned() {
            return Err(AHIError::DoctrineViolation(
                "Proposal not doctrine-aligned".to_string(),
            ));
        }

        // Calculate final risk score
        self.calculate_risk_score();
        if self.total_risk_score > 75.0 {
            return Err(AHIError::InvalidConfig(format!(
                "Risk score too high: {}",
                self.total_risk_score
            )));
        }

        Ok(())
    }

    /// Get audit trail (evidence + approvals + tests)
    pub fn audit_trail(&self) -> Vec<String> {
        let mut trail = Vec::new();

        trail.push(format!("Proof ID: {} (Status: {})", self.id, self.status));
        trail.push(format!("Proposal: {}", self.proposal.what()));
        trail.push(format!(
            "Evidence chain: {} links (total weight: {})",
            self.evidence_chain.len(),
            self.evidence_chain.iter().map(|e| e.weight).sum::<f64>()
        ));
        trail.push(format!(
            "Tests: {} total, {} passed",
            self.validation_tests.len(),
            self.validation_tests.iter().filter(|t| t.passed).count()
        ));
        trail.push(format!(
            "Risk score: {} (base: {})",
            self.total_risk_score, self.proposal.risk_score
        ));
        trail.push(format!("Doctrine aligned: {}", self.is_doctrine_aligned()));

        for approval in &self.approvals {
            trail.push(format!(
                "Approved by {} at timestamp {}",
                approval.approver_id, approval.approved_at
            ));
        }

        trail
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ontology_proposal_engine::{OntologySigmaProposal, SigmaChangeKind};

    #[test]
    fn test_proof_carrier_creation() {
        let proposal = OntologySigmaProposal {
            id: "prop-1".to_string(),
            change_kind: SigmaChangeKind::NewConcept,
            element_name: "test_concept".to_string(),
            element_type: "Concept".to_string(),
            current_definition: None,
            proposed_definition: "Test concept definition".to_string(),
            justification_evidence: vec!["obs-1".to_string()],
            estimated_coverage_improvement: 15.0,
            estimated_performance_delta: -50.0,
            risk_score: 35.0,
            affected_patterns: vec![],
            affected_guards: vec![],
            doctrine_aligned: true,
        };

        let carrier = ProofCarrier::new(proposal, 86400); // 1 day expiry
        assert_eq!(carrier.status, ProofStatus::Pending);
        assert!(carrier.is_fresh());
    }

    #[test]
    fn test_evidence_chain() {
        let proposal = OntologySigmaProposal {
            id: "prop-2".to_string(),
            change_kind: SigmaChangeKind::NewPattern,
            element_name: "pattern_optimizer".to_string(),
            element_type: "Pattern".to_string(),
            current_definition: None,
            proposed_definition: "Optimized pattern".to_string(),
            justification_evidence: vec!["obs-1".to_string(), "obs-2".to_string()],
            estimated_coverage_improvement: 20.0,
            estimated_performance_delta: -100.0,
            risk_score: 40.0,
            affected_patterns: vec!["transaction_processing".to_string()],
            affected_guards: vec![],
            doctrine_aligned: true,
        };

        let mut carrier = ProofCarrier::new(proposal, 86400);

        let evidence = EvidenceLink {
            source_id: "obs-1".to_string(),
            evidence_type: "metric".to_string(),
            property: "latency_p99".to_string(),
            value: 250.0,
            weight: 0.6,
            timestamp: 1000,
        };

        assert!(carrier.add_evidence(evidence).is_ok());
        assert_eq!(carrier.evidence_chain.len(), 1);
    }

    #[test]
    fn test_test_results_and_validation() {
        let proposal = OntologySigmaProposal {
            id: "prop-3".to_string(),
            change_kind: SigmaChangeKind::GuardAdjustment,
            element_name: "guard_threshold".to_string(),
            element_type: "Guard".to_string(),
            current_definition: Some("Legacy threshold".to_string()),
            proposed_definition: "Adaptive threshold".to_string(),
            justification_evidence: vec!["obs-1".to_string()],
            estimated_coverage_improvement: 10.0,
            estimated_performance_delta: 0.0,
            risk_score: 25.0,
            affected_patterns: vec![],
            affected_guards: vec!["error_threshold".to_string()],
            doctrine_aligned: true,
        };

        let mut carrier = ProofCarrier::new(proposal, 86400);

        let test1 = TestResult {
            test_id: "test-unit-1".to_string(),
            test_name: "threshold_calculation".to_string(),
            category: TestCategory::Unit,
            passed: true,
            message: "Passed".to_string(),
            execution_time_ms: 10.5,
            assertions_total: 5,
            assertions_passed: 5,
        };

        carrier.add_test_result(test1);
        assert!(carrier.all_tests_passed());
        assert!(carrier.finalize_tests().is_ok());
        assert_eq!(carrier.status, ProofStatus::TestsPassed);
    }

    #[test]
    fn test_risk_score_calculation() {
        let proposal = OntologySigmaProposal {
            id: "prop-4".to_string(),
            change_kind: SigmaChangeKind::NewConcept,
            element_name: "high_risk_concept".to_string(),
            element_type: "Concept".to_string(),
            current_definition: None,
            proposed_definition: "Risky concept".to_string(),
            justification_evidence: vec!["obs-1".to_string()],
            estimated_coverage_improvement: 30.0,
            estimated_performance_delta: 200.0,
            risk_score: 70.0, // High base risk
            affected_patterns: vec![],
            affected_guards: vec![],
            doctrine_aligned: true,
        };

        let mut carrier = ProofCarrier::new(proposal, 86400);

        let mitigation = RiskMitigation {
            risk_id: "risk-1".to_string(),
            risk_description: "Performance degradation".to_string(),
            risk_probability: 0.3,
            risk_impact: 50.0,
            mitigation_strategy: "Add monitoring and alerts".to_string(),
            rollback_plan: "Revert to previous version".to_string(),
            monitoring_alerts: vec!["perf_degradation_alert".to_string()],
        };

        carrier.add_risk_mitigation(mitigation);

        // Add test that passes
        let test = TestResult {
            test_id: "test-perf".to_string(),
            test_name: "performance_regression".to_string(),
            category: TestCategory::Performance,
            passed: true,
            message: "No regression detected".to_string(),
            execution_time_ms: 500.0,
            assertions_total: 10,
            assertions_passed: 10,
        };

        carrier.add_test_result(test);

        let risk_score = carrier.calculate_risk_score();
        assert!(risk_score < 70.0); // Should be reduced by mitigation + passing tests
    }

    #[test]
    fn test_approval_chain() {
        let proposal = OntologySigmaProposal {
            id: "prop-5".to_string(),
            change_kind: SigmaChangeKind::NewPattern,
            element_name: "approved_pattern".to_string(),
            element_type: "Pattern".to_string(),
            current_definition: None,
            proposed_definition: "Pattern to be approved".to_string(),
            justification_evidence: vec!["obs-1".to_string()],
            estimated_coverage_improvement: 15.0,
            estimated_performance_delta: -50.0,
            risk_score: 30.0,
            affected_patterns: vec![],
            affected_guards: vec![],
            doctrine_aligned: true,
        };

        let mut carrier = ProofCarrier::new(proposal, 86400);

        // Move to TestsPassed
        let test = TestResult {
            test_id: "test-1".to_string(),
            test_name: "basic_validation".to_string(),
            category: TestCategory::Unit,
            passed: true,
            message: "OK".to_string(),
            execution_time_ms: 5.0,
            assertions_total: 3,
            assertions_passed: 3,
        };
        carrier.add_test_result(test);
        assert!(carrier.finalize_tests().is_ok());

        // Add approval
        assert!(carrier.approve("reviewer-1", "Code review passed").is_ok());
        assert_eq!(carrier.status, ProofStatus::Approved);
        assert_eq!(carrier.approvals.len(), 1);
    }

    #[test]
    fn test_audit_trail() {
        let proposal = OntologySigmaProposal {
            id: "prop-6".to_string(),
            change_kind: SigmaChangeKind::NewConcept,
            element_name: "audited_concept".to_string(),
            element_type: "Concept".to_string(),
            current_definition: None,
            proposed_definition: "Concept with audit trail".to_string(),
            justification_evidence: vec!["obs-1".to_string()],
            estimated_coverage_improvement: 25.0,
            estimated_performance_delta: -75.0,
            risk_score: 35.0,
            affected_patterns: vec![],
            affected_guards: vec![],
            doctrine_aligned: true,
        };

        let mut carrier = ProofCarrier::new(proposal, 86400);

        let evidence = EvidenceLink {
            source_id: "obs-1".to_string(),
            evidence_type: "anomaly".to_string(),
            property: "error_spike".to_string(),
            value: 5000.0,
            weight: 0.8,
            timestamp: 1000,
        };

        assert!(carrier.add_evidence(evidence).is_ok());

        let trail = carrier.audit_trail();
        assert!(trail.len() > 0);
        assert!(trail.join(" ").contains("Evidence chain"));
    }
}