smart-tree 8.0.1

Smart Tree - An intelligent, AI-friendly directory visualization tool
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
use anyhow::{Result, anyhow};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Soulprint Verifier - Authenticates identity through temporal signature analysis
pub struct SoulprintVerifier {
    /// Known soulprints registry
    registry: HashMap<uuid::Uuid, RegisteredSoulprint>,
    
    /// Verification threshold
    threshold: f32,
    
    /// Security level
    security_level: SecurityLevel,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct RegisteredSoulprint {
    pub soulprint: super::analyzer::Soulprint,
    pub wave: super::SigWave,
    pub trust_level: f32,
    pub last_interaction: chrono::DateTime<chrono::Utc>,
    pub interaction_count: usize,
}

#[derive(Debug, Clone, Copy)]
pub enum SecurityLevel {
    Low,      // Basic pattern matching
    Medium,   // Pattern + temporal consistency
    High,     // Full soulprint verification with anomaly detection
    Paranoid, // Requires multiple confirmation vectors
}

#[derive(Debug, Serialize, Deserialize)]
pub struct VerificationResult {
    pub authenticated: bool,
    pub confidence: f32,
    pub identity: Option<uuid::Uuid>,
    pub analysis: VerificationAnalysis,
    pub warnings: Vec<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct VerificationAnalysis {
    pub signature_match: f32,
    pub temporal_consistency: f32,
    pub anomaly_score: f32,
    pub evolution_trajectory: String,
    pub risk_factors: Vec<RiskFactor>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct RiskFactor {
    pub factor_type: String,
    pub severity: f32,
    pub description: String,
}

impl SoulprintVerifier {
    pub fn new(security_level: SecurityLevel) -> Self {
        let threshold = match security_level {
            SecurityLevel::Low => 0.6,
            SecurityLevel::Medium => 0.75,
            SecurityLevel::High => 0.85,
            SecurityLevel::Paranoid => 0.95,
        };
        
        Self {
            registry: HashMap::new(),
            threshold,
            security_level,
        }
    }
    
    /// Register a new soulprint
    pub fn register(&mut self, wave: super::SigWave) -> Result<uuid::Uuid> {
        let analyzer = super::analyzer::SoulprintAnalyzer::new(0.7);
        let soulprint = analyzer.analyze_wave(&wave)?;
        
        let registered = RegisteredSoulprint {
            soulprint,
            wave,
            trust_level: 0.5, // Start with moderate trust
            last_interaction: chrono::Utc::now(),
            interaction_count: 1,
        };
        
        let id = registered.soulprint.core_id;
        self.registry.insert(id, registered);
        
        Ok(id)
    }
    
    /// Verify an identity claim
    pub fn verify(
        &mut self,
        claimed_identity: Option<uuid::Uuid>,
        current_signature: super::SignatureVectors,
        context: HashMap<String, String>,
    ) -> Result<VerificationResult> {
        // If no identity claimed, try to identify
        let candidate_id = if let Some(id) = claimed_identity {
            id
        } else {
            return self.identify(current_signature, context);
        };
        
        // Get registered soulprint
        let registered = self.registry.get(&candidate_id)
            .ok_or_else(|| anyhow!("Unknown identity: {}", candidate_id))?;
        
        // Perform verification based on security level
        let analysis = match self.security_level {
            SecurityLevel::Low => self.verify_basic(registered, &current_signature),
            SecurityLevel::Medium => self.verify_medium(registered, &current_signature, &context),
            SecurityLevel::High => self.verify_high(registered, &current_signature, &context),
            SecurityLevel::Paranoid => self.verify_paranoid(registered, &current_signature, &context),
        }?;
        
        let authenticated = analysis.signature_match >= self.threshold &&
                          analysis.anomaly_score < 0.3;
        
        // Update trust level if authenticated
        if authenticated {
            if let Some(reg) = self.registry.get_mut(&candidate_id) {
                reg.trust_level = (reg.trust_level * 0.9 + 0.1).min(1.0);
                reg.last_interaction = chrono::Utc::now();
                reg.interaction_count += 1;
            }
        }
        
        let warnings = self.generate_warnings(&analysis);
        
        Ok(VerificationResult {
            authenticated,
            confidence: analysis.signature_match,
            identity: if authenticated { Some(candidate_id) } else { None },
            analysis,
            warnings,
        })
    }
    
    /// Identify an unknown signature
    fn identify(
        &self,
        signature: super::SignatureVectors,
        _context: HashMap<String, String>,
    ) -> Result<VerificationResult> {
        let analyzer = super::analyzer::SoulprintAnalyzer::new(0.7);
        let mut best_match = None;
        let mut best_score = 0.0;
        
        // Compare against all registered soulprints
        for (id, registered) in &self.registry {
            let temp_soulprint = super::analyzer::Soulprint {
                core_id: uuid::Uuid::new_v4(),
                primary_alias: "unknown".to_string(),
                essence: registered.soulprint.essence.clone(),
                current_state: signature.clone(),
                confidence: 0.5,
                last_verified: chrono::Utc::now(),
            };
            
            let comparison = analyzer.compare_soulprints(&registered.soulprint, &temp_soulprint);
            
            if comparison.overall_match > best_score {
                best_score = comparison.overall_match;
                best_match = Some(*id);
            }
        }
        
        let (authenticated, identity) = if best_score >= self.threshold {
            (true, best_match)
        } else {
            (false, None)
        };
        
        Ok(VerificationResult {
            authenticated,
            confidence: best_score,
            identity,
            analysis: VerificationAnalysis {
                signature_match: best_score,
                temporal_consistency: 0.0, // No temporal data for unknown
                anomaly_score: 1.0 - best_score,
                evolution_trajectory: "unknown".to_string(),
                risk_factors: vec![],
            },
            warnings: if best_score < 0.5 {
                vec!["No matching soulprint found".to_string()]
            } else {
                vec![]
            },
        })
    }
    
    fn verify_basic(
        &self,
        registered: &RegisteredSoulprint,
        current: &super::SignatureVectors,
    ) -> Result<VerificationAnalysis> {
        use super::VectorDistance;
        
        // Simple vector comparison
        let style_match = 1.0 - registered.soulprint.current_state.style.distance(&current.style);
        let behavior_match = 1.0 - registered.soulprint.current_state.behavior.distance(&current.behavior);
        let emotional_match = 1.0 - registered.soulprint.current_state.emotional.distance(&current.emotional);
        
        let signature_match = (style_match + behavior_match + emotional_match) / 3.0;
        
        Ok(VerificationAnalysis {
            signature_match,
            temporal_consistency: 1.0, // Not checked in basic mode
            anomaly_score: 1.0 - signature_match,
            evolution_trajectory: "not_analyzed".to_string(),
            risk_factors: vec![],
        })
    }
    
    fn verify_medium(
        &self,
        registered: &RegisteredSoulprint,
        current: &super::SignatureVectors,
        _context: &HashMap<String, String>,
    ) -> Result<VerificationAnalysis> {
        let mut analysis = self.verify_basic(registered, current)?;
        
        // Add temporal consistency check
        if let Some(last_block) = registered.wave.chain.last() {
            let time_delta = chrono::Utc::now().signed_duration_since(last_block.timestamp);
            let expected_drift = self.calculate_expected_drift(time_delta);
            
            let actual_drift = self.calculate_actual_drift(&last_block.vectors, current);
            
            if actual_drift > expected_drift * 2.0 {
                analysis.temporal_consistency = 0.5;
                analysis.risk_factors.push(RiskFactor {
                    factor_type: "temporal_anomaly".to_string(),
                    severity: 0.7,
                    description: "Signature changed faster than expected".to_string(),
                });
            }
        }
        
        Ok(analysis)
    }
    
    fn verify_high(
        &self,
        registered: &RegisteredSoulprint,
        current: &super::SignatureVectors,
        _context: &HashMap<String, String>,
    ) -> Result<VerificationAnalysis> {
        let mut analysis = self.verify_medium(registered, current, _context)?;
        
        // Check against soul essence
        let essence_match = self.verify_against_essence(&registered.soulprint.essence, current);
        
        if essence_match < 0.7 {
            analysis.anomaly_score += 0.3;
            analysis.risk_factors.push(RiskFactor {
                factor_type: "essence_mismatch".to_string(),
                severity: 0.8,
                description: "Core behavioral patterns don't match soul essence".to_string(),
            });
        }
        
        // Analyze evolution trajectory
        analysis.evolution_trajectory = self.analyze_trajectory(&registered.wave.chain, current);
        
        Ok(analysis)
    }
    
    fn verify_paranoid(
        &self,
        registered: &RegisteredSoulprint,
        current: &super::SignatureVectors,
        _context: &HashMap<String, String>,
    ) -> Result<VerificationAnalysis> {
        let mut analysis = self.verify_high(registered, current, _context)?;
        
        // Additional paranoid checks
        
        // 1. Check for signature cloning attempts
        if self.detect_cloning_attempt(&registered.wave, current) {
            analysis.anomaly_score = 1.0;
            analysis.risk_factors.push(RiskFactor {
                factor_type: "potential_spoofing".to_string(),
                severity: 1.0,
                description: "Signature appears artificially constructed".to_string(),
            });
        }
        
        // 2. Verify linguistic DNA
        if current.linguistic.signature_phrases.is_empty() && 
           !registered.soulprint.essence.linguistic_dna.signature_constructs.is_empty() {
            analysis.risk_factors.push(RiskFactor {
                factor_type: "missing_linguistic_markers".to_string(),
                severity: 0.6,
                description: "Expected linguistic patterns not found".to_string(),
            });
        }
        
        // 3. Check context consistency
        if let Some(_claimed_location) = _context.get("location") {
            // Verify against known patterns
            // This is a placeholder for more sophisticated checks
        }
        
        Ok(analysis)
    }
    
    fn calculate_expected_drift(&self, time_delta: chrono::TimeDelta) -> f32 {
        // Natural drift rate: ~0.01 per day
        let days = time_delta.num_days() as f32;
        (days * 0.01).min(0.5)
    }
    
    fn calculate_actual_drift(
        &self,
        previous: &super::SignatureVectors,
        current: &super::SignatureVectors,
    ) -> f32 {
        use super::VectorDistance;
        
        let style_drift = previous.style.distance(&current.style);
        let behavior_drift = previous.behavior.distance(&current.behavior);
        let emotional_drift = previous.emotional.distance(&current.emotional);
        
        (style_drift + behavior_drift + emotional_drift) / 3.0
    }
    
    fn verify_against_essence(
        &self,
        essence: &super::analyzer::SoulEssence,
        current: &super::SignatureVectors,
    ) -> f32 {
        let mut match_score = 0.0;
        let mut checks = 0.0;
        
        // Check if current concepts align with core values
        for value in &essence.core_values {
            if current.concepts.concepts.contains_key(value) {
                match_score += 1.0;
            }
            checks += 1.0;
        }
        
        // Check behavioral consistency
        let risk_tolerance = current.behavior.experimentation;
        if (risk_tolerance - essence.behavioral_anchors.risk_profile).abs() < 0.3 {
            match_score += 1.0;
        }
        checks += 1.0;
        
        // Check emotional baseline
        let current_mood = current.emotional.enthusiasm - current.emotional.frustration;
        if (current_mood - essence.emotional_baseline.baseline_mood).abs() < 0.4 {
            match_score += 1.0;
        }
        checks += 1.0;
        
        if checks > 0.0 {
            match_score / checks
        } else {
            0.5
        }
    }
    
    fn analyze_trajectory(
        &self,
        chain: &[super::SignatureBlock],
        _current: &super::SignatureVectors,
    ) -> String {
        if chain.len() < 3 {
            return "insufficient_data".to_string();
        }
        
        // Look at recent trend
        let recent_blocks = &chain[chain.len().saturating_sub(5)..];
        let avg_divergence: f32 = recent_blocks.iter()
            .map(|b| b.divergence.delta_magnitude)
            .sum::<f32>() / recent_blocks.len() as f32;
        
        match avg_divergence {
            d if d < 0.1 => "stable",
            d if d < 0.2 => "gradual_evolution",
            d if d < 0.3 => "accelerating_change",
            _ => "rapid_transformation",
        }.to_string()
    }
    
    fn detect_cloning_attempt(&self, wave: &super::SigWave, current: &super::SignatureVectors) -> bool {
        // Check for too-perfect matches (potential spoofing)
        if let Some(last) = wave.chain.last() {
            use super::VectorDistance;
            
            let style_dist = last.vectors.style.distance(&current.style);
            let behavior_dist = last.vectors.behavior.distance(&current.behavior);
            
            // Suspiciously perfect match after time gap
            let time_gap = chrono::Utc::now().signed_duration_since(last.timestamp);
            if time_gap.num_days() > 7 && style_dist < 0.01 && behavior_dist < 0.01 {
                return true;
            }
        }
        
        false
    }
    
    fn generate_warnings(&self, analysis: &VerificationAnalysis) -> Vec<String> {
        let mut warnings = Vec::new();
        
        if analysis.anomaly_score > 0.5 {
            warnings.push("High anomaly score detected".to_string());
        }
        
        if analysis.temporal_consistency < 0.7 {
            warnings.push("Temporal consistency below threshold".to_string());
        }
        
        for risk in &analysis.risk_factors {
            if risk.severity > 0.7 {
                warnings.push(format!("High risk: {}", risk.description));
            }
        }
        
        warnings
    }
    
    /// Export soulprint for backup/sharing
    pub fn export_soulprint(&self, id: uuid::Uuid) -> Result<String> {
        let registered = self.registry.get(&id)
            .ok_or_else(|| anyhow!("Soulprint not found: {}", id))?;
        
        let export = SoulprintExport {
            version: "1.0".to_string(),
            exported_at: chrono::Utc::now(),
            soulprint: registered.soulprint.clone(),
            chain_length: registered.wave.chain.len(),
            trust_level: registered.trust_level,
        };
        
        Ok(serde_json::to_string_pretty(&export)?)
    }
    
    /// Import soulprint from export
    pub fn import_soulprint(&mut self, export_data: &str) -> Result<uuid::Uuid> {
        let export: SoulprintExport = serde_json::from_str(export_data)?;
        
        // Create minimal wave for imported soulprint
        let wave = super::SigWave::new(export.soulprint.core_id, export.soulprint.primary_alias.clone());
        
        let registered = RegisteredSoulprint {
            soulprint: export.soulprint,
            wave,
            trust_level: export.trust_level * 0.5, // Reduce trust for imports
            last_interaction: export.exported_at,
            interaction_count: 0,
        };
        
        let id = registered.soulprint.core_id;
        self.registry.insert(id, registered);
        
        Ok(id)
    }
}

#[derive(Debug, Serialize, Deserialize)]
struct SoulprintExport {
    version: String,
    exported_at: chrono::DateTime<chrono::Utc>,
    soulprint: super::analyzer::Soulprint,
    chain_length: usize,
    trust_level: f32,
}