ggen-dod 5.0.2

Definition of Done: Type-safe observation system, deterministic kernel, provenance tracking, and MAPE-K autonomic governance
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
//! MAPE-K Autonomic Loop: Monitor-Analyze-Plan-Execute-Knowledge
//!
//! The autonomic loop enables ggen to self-improve without human intervention.
//! Each cycle:
//! 1. Monitor: Collect observations (O)
//! 2. Analyze: Detect anomalies and drift
//! 3. Plan: Propose schema changes (ΔΣ)
//! 4. Execute: Apply changes with proofs and rollback capability
//! 5. Knowledge: Record findings and decisions in Γ

use crate::error::DoDResult;
use crate::observation::Observation;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;

/// Phase 1: Monitor - Collect observations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ObservationPhase {
    /// Collected observations
    observations: Vec<Observation>,
    /// Monitoring started at
    started_at: DateTime<Utc>,
}

impl ObservationPhase {
    /// Create a new monitoring phase
    pub fn new() -> Self {
        Self {
            observations: Vec::new(),
            started_at: Utc::now(),
        }
    }

    /// Add an observation
    pub fn observe(mut self, obs: Observation) -> Self {
        self.observations.push(obs);
        self
    }

    /// Get observations
    pub fn observations(&self) -> &[Observation] {
        &self.observations
    }

    /// Transition to analysis
    pub fn analyze(self) -> DoDResult<AnalysisPhase> {
        if self.observations.is_empty() {
            return Err(crate::error::DoDError::MAPEKPhase(
                "no observations collected".to_string(),
            ));
        }

        Ok(AnalysisPhase::from_observations(self.observations))
    }
}

impl Default for ObservationPhase {
    fn default() -> Self {
        Self::new()
    }
}

/// Finding from analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Finding {
    /// Finding type
    finding_type: String,
    /// Severity (info, warning, error)
    severity: String,
    /// Description
    description: String,
    /// Affected subsystems
    affected_subsystems: Vec<String>,
}

impl Finding {
    /// Create a new finding
    pub fn new(
        finding_type: impl Into<String>, severity: impl Into<String>,
        description: impl Into<String>,
    ) -> Self {
        Self {
            finding_type: finding_type.into(),
            severity: severity.into(),
            description: description.into(),
            affected_subsystems: Vec::new(),
        }
    }

    /// Add affected subsystem
    pub fn with_subsystem(mut self, subsystem: impl Into<String>) -> Self {
        self.affected_subsystems.push(subsystem.into());
        self
    }

    /// Get finding type
    pub fn finding_type(&self) -> &str {
        &self.finding_type
    }

    /// Get severity
    pub fn severity(&self) -> &str {
        &self.severity
    }

    /// Is this error-level or higher?
    pub fn is_critical(&self) -> bool {
        self.severity == "error" || self.severity == "critical"
    }
}

/// Phase 2: Analyze - Detect anomalies and drift
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnalysisPhase {
    /// Input observations
    observations: Vec<Observation>,
    /// Detected findings
    findings: Vec<Finding>,
    /// Analysis started at
    started_at: DateTime<Utc>,
}

impl AnalysisPhase {
    /// Create analysis phase from observations
    fn from_observations(observations: Vec<Observation>) -> Self {
        Self {
            observations,
            findings: Vec::new(),
            started_at: Utc::now(),
        }
    }

    /// Record a finding
    pub fn with_finding(mut self, finding: Finding) -> Self {
        self.findings.push(finding);
        self
    }

    /// Get findings
    pub fn findings(&self) -> &[Finding] {
        &self.findings
    }

    /// Transition to planning
    pub fn plan(self) -> DoDResult<PlanningPhase> {
        Ok(PlanningPhase::from_findings(self.findings))
    }
}

/// Proposed schema change (ΔΣ)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SchemaProposal {
    /// Proposal ID
    id: String,
    /// Description
    description: String,
    /// Proposed changes
    changes: Vec<String>,
    /// Risk level (low, medium, high)
    risk_level: String,
    /// Expected benefits
    expected_benefits: Vec<String>,
}

impl SchemaProposal {
    /// Create a new schema proposal
    pub fn new(description: impl Into<String>) -> Self {
        Self {
            id: uuid::Uuid::new_v4().to_string(),
            description: description.into(),
            changes: Vec::new(),
            risk_level: "low".to_string(),
            expected_benefits: Vec::new(),
        }
    }

    /// Get proposal ID
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Add a change
    pub fn with_change(mut self, change: impl Into<String>) -> Self {
        self.changes.push(change.into());
        self
    }

    /// Add benefit
    pub fn with_benefit(mut self, benefit: impl Into<String>) -> Self {
        self.expected_benefits.push(benefit.into());
        self
    }

    /// Set risk level
    pub fn with_risk_level(mut self, risk: impl Into<String>) -> Self {
        self.risk_level = risk.into();
        self
    }
}

/// Phase 3: Plan - Propose schema changes
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlanningPhase {
    /// Detected findings
    findings: Vec<Finding>,
    /// Proposed changes
    proposals: Vec<SchemaProposal>,
    /// Planning started at
    started_at: DateTime<Utc>,
}

impl PlanningPhase {
    /// Create planning phase from findings
    fn from_findings(findings: Vec<Finding>) -> Self {
        Self {
            findings,
            proposals: Vec::new(),
            started_at: Utc::now(),
        }
    }

    /// Add a proposal
    pub fn with_proposal(mut self, proposal: SchemaProposal) -> Self {
        self.proposals.push(proposal);
        self
    }

    /// Get proposals
    pub fn proposals(&self) -> &[SchemaProposal] {
        &self.proposals
    }

    /// Transition to execution
    pub fn execute(self) -> DoDResult<ExecutionPhase> {
        Ok(ExecutionPhase::from_proposals(self.proposals))
    }
}

/// Execution result
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ExecutionResult {
    /// Executed successfully
    Success,
    /// Rolled back due to failure
    RolledBack,
    /// Rejected (didn't meet requirements)
    Rejected,
}

/// Phase 4: Execute - Apply changes
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionPhase {
    /// Proposed changes
    proposals: Vec<SchemaProposal>,
    /// Execution results
    results: Vec<(String, ExecutionResult)>,
    /// Execution started at
    started_at: DateTime<Utc>,
}

impl ExecutionPhase {
    /// Create execution phase from proposals
    fn from_proposals(proposals: Vec<SchemaProposal>) -> Self {
        Self {
            proposals,
            results: Vec::new(),
            started_at: Utc::now(),
        }
    }

    /// Record an execution result
    pub fn record_result(
        mut self, proposal_id: impl Into<String>, result: ExecutionResult,
    ) -> Self {
        self.results.push((proposal_id.into(), result));
        self
    }

    /// Get results
    pub fn results(&self) -> &[(String, ExecutionResult)] {
        &self.results
    }

    /// Transition to knowledge update
    pub fn learn(self) -> KnowledgePhase {
        KnowledgePhase::from_execution(self.results)
    }
}

/// Phase 5: Knowledge - Record findings and decisions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KnowledgePhase {
    /// Execution results
    results: Vec<(String, ExecutionResult)>,
    /// Knowledge updated at
    updated_at: DateTime<Utc>,
}

impl KnowledgePhase {
    /// Create knowledge phase from execution
    fn from_execution(results: Vec<(String, ExecutionResult)>) -> Self {
        Self {
            results,
            updated_at: Utc::now(),
        }
    }

    /// Get results
    pub fn results(&self) -> &[(String, ExecutionResult)] {
        &self.results
    }
}

/// The MAPE-K loop: cycles through all phases
#[derive(Debug)]
pub struct MAPEKLoop {
    /// History of completed cycles
    cycles: VecDeque<MAPEKCycle>,
    /// Current cycle (if in progress)
    current_cycle: Option<MAPEKCycleState>,
}

/// A completed MAPE-K cycle
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MAPEKCycle {
    /// Cycle number
    cycle_num: u64,
    /// Observations collected
    observation_count: usize,
    /// Findings detected
    finding_count: usize,
    /// Proposals made
    proposal_count: usize,
    /// Successful executions
    successful_executions: usize,
    /// Failed executions
    failed_executions: usize,
    /// Cycle completed at
    completed_at: DateTime<Utc>,
}

/// Current state of MAPE-K cycle
#[derive(Debug, Clone)]
pub enum MAPEKCycleState {
    Monitoring(ObservationPhase),
    Analyzing(AnalysisPhase),
    Planning(PlanningPhase),
    Executing(ExecutionPhase),
    Learning(KnowledgePhase),
}

impl MAPEKLoop {
    /// Create a new MAPE-K loop
    pub fn new() -> Self {
        Self {
            cycles: VecDeque::new(),
            current_cycle: None,
        }
    }

    /// Start a new monitoring phase
    pub fn start_monitor(&mut self) {
        self.current_cycle = Some(MAPEKCycleState::Monitoring(ObservationPhase::new()));
    }

    /// Get the number of completed cycles
    pub fn completed_cycles(&self) -> usize {
        self.cycles.len()
    }

    /// Get the last N cycles
    pub fn recent_cycles(&self, n: usize) -> Vec<&MAPEKCycle> {
        self.cycles.iter().rev().take(n).collect()
    }

    /// Current phase name
    pub fn current_phase(&self) -> Option<&'static str> {
        match &self.current_cycle {
            Some(MAPEKCycleState::Monitoring(_)) => Some("Monitoring"),
            Some(MAPEKCycleState::Analyzing(_)) => Some("Analyzing"),
            Some(MAPEKCycleState::Planning(_)) => Some("Planning"),
            Some(MAPEKCycleState::Executing(_)) => Some("Executing"),
            Some(MAPEKCycleState::Learning(_)) => Some("Learning"),
            None => None,
        }
    }
}

impl Default for MAPEKLoop {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_observation_phase() {
        let phase = ObservationPhase::new();
        assert_eq!(phase.observations().len(), 0);
    }

    #[test]
    fn test_finding() {
        let finding = Finding::new("drift", "warning", "detected drift in metric");
        assert_eq!(finding.finding_type(), "drift");
        assert!(!finding.is_critical());
    }

    #[test]
    fn test_schema_proposal() {
        let proposal = SchemaProposal::new("add new field")
            .with_change("fields += 1")
            .with_benefit("better observability");

        assert_eq!(proposal.changes.len(), 1);
        assert_eq!(proposal.expected_benefits.len(), 1);
    }

    #[test]
    fn test_mape_k_loop() {
        let loop_ = MAPEKLoop::new();
        assert_eq!(loop_.completed_cycles(), 0);
    }
}