ggen-domain 5.1.3

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
//! Proof-Carrying Decisions: Decisions Only Exist If Proofs Exist
//!
//! Key insight: make decisions literally impossible to construct without
//! attaching a proof object. Different decision severities require different proof levels.
//!
//! Proof traits form a lattice:
//! - WeakProof (≤30 doctrine distance) - for read-only, low-risk ops
//! - StandardProof (≤50 doctrine distance) - for cache/snapshot updates
//! - StrongProof (≤70 doctrine distance) - for ΔΣ proposals
//! - CriticalProof (≤80 doctrine distance) - for marketplace changes

use serde::{Deserialize, Serialize};

// ============================================================================
// PROOF TRAIT HIERARCHY
// ============================================================================

/// Base proof trait (sealed against external implementation)
pub trait Proof: Send + Sync {
    /// Doctrine distance this proof achieves (0-100)
    fn doctrine_distance(&self) -> f64;

    /// Is this proof still valid?
    fn is_valid(&self) -> bool;

    /// Human-readable justification
    fn justification(&self) -> String;

    /// Proof ID for audit trails
    fn proof_id(&self) -> &str;

    /// Get the evidence count
    fn evidence_count(&self) -> usize {
        1
    }
}

/// Weak proof: suitable for read-only operations
/// Max doctrine distance: 30 (Perfect to Good range)
pub trait WeakProof: Proof {
    fn max_distance() -> f64 {
        30.0
    }

    fn can_use(&self) -> Result<(), String> {
        if self.doctrine_distance() <= Self::max_distance() {
            Ok(())
        } else {
            Err(format!(
                "Weak proof doctrine distance {} exceeds limit {}",
                self.doctrine_distance(),
                Self::max_distance()
            ))
        }
    }
}

/// Standard proof: for cache/snapshot updates
/// Max doctrine distance: 50 (Acceptable range)
pub trait StandardProof: Proof {
    fn max_distance() -> f64 {
        50.0
    }

    fn can_use(&self) -> Result<(), String> {
        if self.doctrine_distance() <= Self::max_distance() {
            Ok(())
        } else {
            Err(format!(
                "Standard proof doctrine distance {} exceeds limit {}",
                self.doctrine_distance(),
                Self::max_distance()
            ))
        }
    }
}

/// Strong proof: for ΔΣ proposals and ontology changes
/// Max doctrine distance: 70 (Acceptable to Marginal range)
pub trait StrongProof: Proof {
    fn max_distance() -> f64 {
        70.0
    }

    fn can_use(&self) -> Result<(), String> {
        if self.doctrine_distance() <= Self::max_distance() {
            Ok(())
        } else {
            Err(format!(
                "Strong proof doctrine distance {} exceeds limit {}",
                self.doctrine_distance(),
                Self::max_distance()
            ))
        }
    }
}

/// Critical proof: for marketplace promotions and high-risk changes
/// Max doctrine distance: 80 (Marginal range, just barely)
pub trait CriticalProof: Proof {
    fn max_distance() -> f64 {
        80.0
    }

    fn can_use(&self) -> Result<(), String> {
        if self.doctrine_distance() <= Self::max_distance() {
            Ok(())
        } else {
            Err(format!(
                "Critical proof doctrine distance {} exceeds limit {}",
                self.doctrine_distance(),
                Self::max_distance()
            ))
        }
    }
}

// ============================================================================
// CONCRETE PROOF IMPLEMENTATIONS
// ============================================================================

/// Lightweight proof for read-only operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LightProof {
    proof_id: String,
    doctrine_distance: f64,
    created_at: u64,
}

impl LightProof {
    pub fn new(proof_id: impl Into<String>, doctrine_distance: f64) -> Self {
        Self {
            proof_id: proof_id.into(),
            doctrine_distance,
            created_at: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
        }
    }
}

impl Proof for LightProof {
    fn doctrine_distance(&self) -> f64 {
        self.doctrine_distance
    }

    fn is_valid(&self) -> bool {
        true
    }

    fn justification(&self) -> String {
        "Light proof: basic read access".to_string()
    }

    fn proof_id(&self) -> &str {
        &self.proof_id
    }
}

impl WeakProof for LightProof {}

/// Standard proof: includes test results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestingProof {
    proof_id: String,
    doctrine_distance: f64,
    tests_passed: usize,
    tests_failed: usize,
    created_at: u64,
}

impl TestingProof {
    pub fn new(proof_id: impl Into<String>, doctrine_distance: f64, tests_passed: usize) -> Self {
        Self {
            proof_id: proof_id.into(),
            doctrine_distance,
            tests_passed,
            tests_failed: 0,
            created_at: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
        }
    }

    pub fn tests_passed(&self) -> usize {
        self.tests_passed
    }

    pub fn tests_failed(&self) -> usize {
        self.tests_failed
    }
}

impl Proof for TestingProof {
    fn doctrine_distance(&self) -> f64 {
        self.doctrine_distance
    }

    fn is_valid(&self) -> bool {
        self.tests_passed > 0 && self.tests_failed == 0
    }

    fn justification(&self) -> String {
        format!("Testing proof: {} tests passed", self.tests_passed)
    }

    fn proof_id(&self) -> &str {
        &self.proof_id
    }

    fn evidence_count(&self) -> usize {
        self.tests_passed
    }
}

impl StandardProof for TestingProof {}

/// Strong proof: includes evidence chain and impact analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvidenceProof {
    proof_id: String,
    doctrine_distance: f64,
    evidence_count: usize,
    impact_verified: bool,
    tests_passed: usize,
    created_at: u64,
}

impl EvidenceProof {
    pub fn new(proof_id: impl Into<String>, doctrine_distance: f64, evidence_count: usize) -> Self {
        Self {
            proof_id: proof_id.into(),
            doctrine_distance,
            evidence_count,
            impact_verified: false,
            tests_passed: 0,
            created_at: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
        }
    }

    pub fn verify_impact(mut self) -> Self {
        self.impact_verified = true;
        self
    }

    pub fn with_tests(mut self, count: usize) -> Self {
        self.tests_passed = count;
        self
    }
}

impl Proof for EvidenceProof {
    fn doctrine_distance(&self) -> f64 {
        self.doctrine_distance
    }

    fn is_valid(&self) -> bool {
        self.evidence_count > 0 && self.impact_verified && self.tests_passed > 0
    }

    fn justification(&self) -> String {
        format!(
            "Evidence proof: {} evidence links, impact verified, {} tests",
            self.evidence_count, self.tests_passed
        )
    }

    fn proof_id(&self) -> &str {
        &self.proof_id
    }

    fn evidence_count(&self) -> usize {
        self.evidence_count
    }
}

impl StrongProof for EvidenceProof {}

/// Critical proof: maximum rigor for high-risk decisions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CriticalityProof {
    proof_id: String,
    doctrine_distance: f64,
    evidence_count: usize,
    impact_verified: bool,
    tests_passed: usize,
    governance_approved: bool,
    approval_chain: Vec<String>, // Approvers
    created_at: u64,
}

impl CriticalityProof {
    pub fn new(proof_id: impl Into<String>, doctrine_distance: f64) -> Self {
        Self {
            proof_id: proof_id.into(),
            doctrine_distance,
            evidence_count: 0,
            impact_verified: false,
            tests_passed: 0,
            governance_approved: false,
            approval_chain: Vec::new(),
            created_at: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
        }
    }

    pub fn with_evidence(mut self, count: usize) -> Self {
        self.evidence_count = count;
        self
    }

    pub fn verify_impact(mut self) -> Self {
        self.impact_verified = true;
        self
    }

    pub fn with_tests(mut self, count: usize) -> Self {
        self.tests_passed = count;
        self
    }

    pub fn approve(mut self, approver: String) -> Self {
        self.approval_chain.push(approver);
        if self.approval_chain.len() >= 2 {
            // Requires at least 2 approvals for critical decisions
            self.governance_approved = true;
        }
        self
    }

    pub fn approvers(&self) -> &[String] {
        &self.approval_chain
    }
}

impl Proof for CriticalityProof {
    fn doctrine_distance(&self) -> f64 {
        self.doctrine_distance
    }

    fn is_valid(&self) -> bool {
        self.evidence_count > 0
            && self.impact_verified
            && self.tests_passed > 0
            && self.governance_approved
    }

    fn justification(&self) -> String {
        format!(
            "Critical proof: {} evidence, impact verified, {} tests, {} approvals",
            self.evidence_count,
            self.tests_passed,
            self.approval_chain.len()
        )
    }

    fn proof_id(&self) -> &str {
        &self.proof_id
    }

    fn evidence_count(&self) -> usize {
        self.evidence_count
    }
}

impl CriticalProof for CriticalityProof {}

// ============================================================================
// PROOF-CARRYING DECISIONS
// ============================================================================

/// Decision that requires a weak proof
pub struct WeakDecision<P: WeakProof> {
    #[allow(dead_code)]
    decision_id: String,
    #[allow(dead_code)]
    description: String,
    proof: P,
}

impl<P: WeakProof> WeakDecision<P> {
    /// Construct a decision with proof
    pub fn make(
        decision_id: impl Into<String>, description: impl Into<String>, proof: P,
    ) -> Result<Self, String> {
        proof.can_use()?;

        Ok(Self {
            decision_id: decision_id.into(),
            description: description.into(),
            proof,
        })
    }

    pub fn proof(&self) -> &P {
        &self.proof
    }
}

/// Decision that requires a standard proof
pub struct StandardDecision<P: StandardProof> {
    #[allow(dead_code)]
    decision_id: String,
    #[allow(dead_code)]
    description: String,
    proof: P,
}

impl<P: StandardProof> StandardDecision<P> {
    pub fn make(
        decision_id: impl Into<String>, description: impl Into<String>, proof: P,
    ) -> Result<Self, String> {
        proof.can_use()?;

        Ok(Self {
            decision_id: decision_id.into(),
            description: description.into(),
            proof,
        })
    }

    pub fn proof(&self) -> &P {
        &self.proof
    }
}

/// Decision that requires a strong proof
pub struct StrongDecision<P: StrongProof> {
    #[allow(dead_code)]
    decision_id: String,
    #[allow(dead_code)]
    description: String,
    proof: P,
}

impl<P: StrongProof> StrongDecision<P> {
    pub fn make(
        decision_id: impl Into<String>, description: impl Into<String>, proof: P,
    ) -> Result<Self, String> {
        proof.can_use()?;

        Ok(Self {
            decision_id: decision_id.into(),
            description: description.into(),
            proof,
        })
    }

    pub fn proof(&self) -> &P {
        &self.proof
    }
}

/// Decision that requires critical proof
pub struct CriticalDecision<P: CriticalProof> {
    #[allow(dead_code)]
    decision_id: String,
    #[allow(dead_code)]
    description: String,
    proof: P,
}

impl<P: CriticalProof> CriticalDecision<P> {
    pub fn make(
        decision_id: impl Into<String>, description: impl Into<String>, proof: P,
    ) -> Result<Self, String> {
        proof.can_use()?;

        Ok(Self {
            decision_id: decision_id.into(),
            description: description.into(),
            proof,
        })
    }

    pub fn proof(&self) -> &P {
        &self.proof
    }
}

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

    #[test]
    fn test_weak_proof() {
        let proof = LightProof::new("proof-1", 20.0);
        assert!(proof.is_valid());
        assert_eq!(proof.doctrine_distance(), 20.0);
        assert!(proof.can_use().is_ok());
    }

    #[test]
    fn test_weak_proof_exceeds_limit() {
        let proof = LightProof::new("proof-2", 40.0);
        // Distance 40 exceeds WeakProof limit of 30
        assert!(proof.can_use().is_err());
    }

    #[test]
    fn test_standard_proof() {
        let proof = TestingProof::new("proof-3", 45.0, 10);
        assert!(proof.is_valid());
        assert!(proof.can_use().is_ok());
    }

    #[test]
    fn test_strong_proof_with_evidence() {
        let proof = EvidenceProof::new("proof-4", 60.0, 5)
            .verify_impact()
            .with_tests(15);
        assert!(proof.is_valid());
        assert_eq!(proof.evidence_count(), 5);
    }

    #[test]
    fn test_critical_proof_requires_approvals() {
        let proof = CriticalityProof::new("proof-5", 70.0)
            .with_evidence(10)
            .verify_impact()
            .with_tests(20)
            .approve("approver-1".to_string());

        // Single approval not enough
        assert!(!proof.is_valid());

        let proof2 = proof.approve("approver-2".to_string());
        // Two approvals sufficient
        assert!(proof2.is_valid());
    }

    #[test]
    fn test_decision_requires_proof() {
        let proof = LightProof::new("proof-6", 15.0);
        let decision = WeakDecision::make("decision-1", "Read operation", proof);
        assert!(decision.is_ok());
    }

    #[test]
    fn test_decision_rejects_invalid_proof() {
        let proof = LightProof::new("proof-7", 35.0); // Exceeds weak limit
        let decision = WeakDecision::make("decision-2", "Read operation", proof);
        assert!(decision.is_err());
    }
}