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
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
use anyhow::Result;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;

/// Live Signature Beacon - Real-time identity emission and verification
pub struct SignatureBeacon {
    /// Current signature state
    current_state: Arc<RwLock<BeaconState>>,
    
    /// Trust lattice connections
    trust_lattice: Arc<RwLock<TrustLattice>>,
    
    /// Divergence monitor
    divergence_monitor: DivergenceMonitor,
    
    /// Beacon configuration
    config: BeaconConfig,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BeaconState {
    /// Identity being broadcast
    pub identity: uuid::Uuid,
    
    /// Current signature
    pub signature: super::SignatureVectors,
    
    /// Integrity hash of current state
    pub integrity_hash: String,
    
    /// Match percentage to baseline
    pub match_percentage: f32,
    
    /// Current behavioral mode
    pub mode: BehavioralMode,
    
    /// Drift from baseline
    pub drift: f32,
    
    /// Last emission time
    pub last_emission: DateTime<Utc>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BehavioralMode {
    /// Primary traits active
    pub traits: Vec<String>, // ["Sharp", "Terse", "Wry"]
    
    /// Intensity level
    pub intensity: f32,
    
    /// Context tag
    pub context: String, // "coding", "debugging", "philosophical"
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TrustLattice {
    /// Connected AI signatures
    pub ai_nodes: HashMap<String, AINode>,
    
    /// Trust relationships
    pub trust_edges: Vec<TrustEdge>,
    
    /// Verification history
    pub verification_log: Vec<VerificationEvent>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AINode {
    /// Model identifier
    pub model: String, // "claude-3.5-opus"
    
    /// Build/version info
    pub build: String,
    
    /// Provider info
    pub provider: String, // "Anthropic"
    
    /// Routing info
    pub route: RouteInfo,
    
    /// Signature hash
    pub sig_hash: String,
    
    /// Trust level
    pub trust_level: f32,
    
    /// Last verified
    pub last_verified: DateTime<Utc>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RouteInfo {
    /// Direct or proxied
    pub route_type: RouteType,
    
    /// Provider chain
    pub provider_chain: Vec<String>,
    
    /// Latency fingerprint
    pub latency_profile: LatencyProfile,
    
    /// Detected anomalies
    pub anomalies: Vec<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum RouteType {
    Direct,
    Proxied { hops: usize },
    Manipulated { confidence: f32 },
    Unknown,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct LatencyProfile {
    /// Average response time
    pub avg_latency_ms: f32,
    
    /// Variance in timing
    pub variance: f32,
    
    /// Timing pattern signature
    pub pattern: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TrustEdge {
    /// From node
    pub from: String,
    
    /// To node
    pub to: String,
    
    /// Trust score
    pub trust: f32,
    
    /// Verification method
    pub method: VerificationMethod,
    
    /// Timestamp
    pub timestamp: DateTime<Utc>,
}

#[derive(Debug, Serialize, Deserialize)]
pub enum VerificationMethod {
    Blockchain,
    BehavioralAnalysis,
    LatencyFingerprint,
    CryptographicProof,
    ConsensusVote,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct VerificationEvent {
    /// What was verified
    pub target: String,
    
    /// Verification result
    pub result: VerificationResult,
    
    /// Timestamp
    pub timestamp: DateTime<Utc>,
    
    /// Details
    pub details: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum VerificationResult {
    Authentic,
    Suspicious { reason: String },
    Manipulated { confidence: f32 },
    Unverifiable,
}

/// Monitors divergence from baseline behavior
pub struct DivergenceMonitor {
    /// Baseline signature
    baseline: super::SignatureVectors,
    
    /// Acceptable drift threshold
    threshold: f32,
    
    /// Recent measurements
    history: Vec<DivergenceMeasurement>,
}

#[derive(Debug, Clone)]
struct DivergenceMeasurement {
    timestamp: DateTime<Utc>,
    divergence: f32,
    primary_factors: Vec<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct BeaconConfig {
    /// Emission frequency (seconds)
    pub emission_interval: u64,
    
    /// Trust verification interval
    pub verify_interval: u64,
    
    /// Alert thresholds
    pub alert_thresholds: AlertThresholds,
    
    /// Visual indicator preferences
    pub visual_config: VisualConfig,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct AlertThresholds {
    /// Divergence threshold for warning
    pub divergence_warning: f32,
    
    /// Divergence threshold for alert
    pub divergence_alert: f32,
    
    /// Trust drop threshold
    pub trust_alert: f32,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct VisualConfig {
    /// Show in CLI
    pub cli_display: bool,
    
    /// Emit to file
    pub file_output: Option<String>,
    
    /// WebSocket broadcast
    pub websocket: Option<String>,
}

impl SignatureBeacon {
    pub fn new(identity: uuid::Uuid, baseline: super::SignatureVectors) -> Self {
        let config = BeaconConfig {
            emission_interval: 60, // Every minute
            verify_interval: 300,  // Every 5 minutes
            alert_thresholds: AlertThresholds {
                divergence_warning: 0.15,
                divergence_alert: 0.30,
                trust_alert: 0.70,
            },
            visual_config: VisualConfig {
                cli_display: true,
                file_output: Some("~/.sigwave/beacon.json".to_string()),
                websocket: None,
            },
        };
        
        let initial_state = BeaconState {
            identity,
            signature: baseline.clone(),
            integrity_hash: Self::calculate_integrity_hash(&baseline),
            match_percentage: 100.0,
            mode: BehavioralMode {
                traits: vec!["baseline".to_string()],
                intensity: 1.0,
                context: "initialization".to_string(),
            },
            drift: 0.0,
            last_emission: Utc::now(),
        };
        
        Self {
            current_state: Arc::new(RwLock::new(initial_state)),
            trust_lattice: Arc::new(RwLock::new(TrustLattice {
                ai_nodes: HashMap::new(),
                trust_edges: Vec::new(),
                verification_log: Vec::new(),
            })),
            divergence_monitor: DivergenceMonitor {
                baseline,
                threshold: 0.30,
                history: Vec::new(),
            },
            config,
        }
    }
    
    /// Start beacon emission
    pub async fn start(&self) -> Result<()> {
        // This would spawn background tasks for:
        // 1. Regular signature emission
        // 2. Trust verification checks
        // 3. Divergence monitoring
        // 4. Visual updates
        
        // For now, just emit once
        self.emit().await
    }
    
    /// Emit current signature
    pub async fn emit(&self) -> Result<()> {
        let state = self.current_state.read().await;
        
        if self.config.visual_config.cli_display {
            self.display_cli_beacon(&state).await?;
        }
        
        if let Some(file_path) = &self.config.visual_config.file_output {
            self.write_beacon_file(file_path, &state).await?;
        }
        
        // Update emission time
        drop(state);
        let mut state = self.current_state.write().await;
        state.last_emission = Utc::now();
        
        Ok(())
    }
    
    /// Update current signature
    pub async fn update_signature(&mut self, new_signature: super::SignatureVectors) -> Result<()> {
        // Calculate divergence
        let divergence = self.divergence_monitor.measure(&new_signature);
        
        // Determine behavioral mode
        let mode = self.analyze_mode(&new_signature);
        
        // Update state
        let mut state = self.current_state.write().await;
        state.signature = new_signature.clone();
        state.integrity_hash = Self::calculate_integrity_hash(&new_signature);
        state.match_percentage = (1.0 - divergence) * 100.0;
        state.mode = mode;
        state.drift = divergence;
        
        // Check for alerts
        if divergence > self.config.alert_thresholds.divergence_alert {
            self.trigger_divergence_alert(divergence).await?;
        }
        
        Ok(())
    }
    
    /// Verify AI node authenticity
    pub async fn verify_ai_node(&self, node_id: &str) -> Result<VerificationResult> {
        let lattice = self.trust_lattice.read().await;
        
        if let Some(node) = lattice.ai_nodes.get(node_id) {
            // Perform verification checks
            let result = self.verify_node_integrity(node).await?;
            
            // Log verification
            drop(lattice);
            let mut lattice = self.trust_lattice.write().await;
            lattice.verification_log.push(VerificationEvent {
                target: node_id.to_string(),
                result: result.clone(),
                timestamp: Utc::now(),
                details: "Routine verification".to_string(),
            });
            
            Ok(result)
        } else {
            Ok(VerificationResult::Unverifiable)
        }
    }
    
    /// Add AI node to trust lattice
    pub async fn add_ai_node(&self, node: AINode) -> Result<()> {
        let mut lattice = self.trust_lattice.write().await;
        lattice.ai_nodes.insert(node.model.clone(), node);
        Ok(())
    }
    
    /// Display CLI beacon
    async fn display_cli_beacon(&self, state: &BeaconState) -> Result<()> {
        println!("\n╭─────────────────────────────────────────╮");
        println!("│          SIGNATURE BEACON              │");
        println!("├─────────────────────────────────────────┤");
        println!("│ 🧠 Identity: {:.1}% match", state.match_percentage);
        println!("│ ✨ Mode: {} | Drift: {:+.1}%", 
            state.mode.traits.join("-"),
            state.drift * 100.0
        );
        println!("│ 🔗 Hash: {}", &state.integrity_hash[..8]);
        println!("│ ⏰ Last: {}", state.last_emission.format("%H:%M:%S"));
        
        // Show connected AI nodes
        let lattice = self.trust_lattice.read().await;
        if !lattice.ai_nodes.is_empty() {
            println!("├─────────────────────────────────────────┤");
            for (_id, node) in lattice.ai_nodes.iter().take(3) {
                let route_icon = match &node.route.route_type {
                    RouteType::Direct => "",
                    RouteType::Proxied { .. } => "⚠️",
                    RouteType::Manipulated { .. } => "",
                    RouteType::Unknown => "",
                };
                println!("{} {} | Trust: {:.0}%", 
                    route_icon, 
                    node.model, 
                    node.trust_level * 100.0
                );
            }
        }
        
        println!("╰─────────────────────────────────────────╯");
        
        Ok(())
    }
    
    /// Write beacon to file
    async fn write_beacon_file(&self, path: &str, state: &BeaconState) -> Result<()> {
        let expanded_path = if path.starts_with("~") {
            let home = dirs::home_dir()
                .ok_or_else(|| anyhow::anyhow!("Could not find home directory"))?;
            path.replacen("~", home.to_str().unwrap(), 1)
        } else {
            path.to_string()
        };
        
        let beacon_data = serde_json::to_string_pretty(state)?;
        tokio::fs::write(&expanded_path, beacon_data).await?;
        Ok(())
    }
    
    /// Calculate integrity hash
    fn calculate_integrity_hash(signature: &super::SignatureVectors) -> String {
        use sha2::{Sha256, Digest};
        let mut hasher = Sha256::new();
        
        if let Ok(serialized) = serde_json::to_string(signature) {
            hasher.update(serialized.as_bytes());
        }
        
        format!("{:x}", hasher.finalize())
    }
    
    /// Analyze behavioral mode
    fn analyze_mode(&self, signature: &super::SignatureVectors) -> BehavioralMode {
        let mut traits = Vec::new();
        
        // Analyze style
        if signature.style.terseness > 0.8 {
            traits.push("Terse".to_string());
        }
        if signature.style.humor_density > 0.5 {
            traits.push("Playful".to_string());
        }
        if signature.style.technicality > 0.7 {
            traits.push("Technical".to_string());
        }
        
        // Analyze behavior
        if signature.behavior.directness > 0.8 {
            traits.push("Direct".to_string());
        }
        if signature.behavior.experimentation > 0.7 {
            traits.push("Experimental".to_string());
        }
        
        // Determine context
        let context = if signature.concepts.concepts.contains_key("debugging") {
            "debugging"
        } else if signature.concepts.concepts.contains_key("philosophy") {
            "philosophical"
        } else {
            "general"
        }.to_string();
        
        BehavioralMode {
            traits,
            intensity: (signature.emotional.enthusiasm + signature.emotional.curiosity) / 2.0,
            context,
        }
    }
    
    /// Verify node integrity
    async fn verify_node_integrity(&self, node: &AINode) -> Result<VerificationResult> {
        // Check route
        match &node.route.route_type {
            RouteType::Direct => {
                // Verify latency profile matches expected
                if node.route.latency_profile.variance > 100.0 {
                    Ok(VerificationResult::Suspicious {
                        reason: "Unusual latency variance".to_string(),
                    })
                } else {
                    Ok(VerificationResult::Authentic)
                }
            },
            RouteType::Proxied { hops } => {
                if *hops > 1 {
                    Ok(VerificationResult::Suspicious {
                        reason: format!("{} proxy hops detected", hops),
                    })
                } else {
                    Ok(VerificationResult::Authentic)
                }
            },
            RouteType::Manipulated { confidence } => {
                Ok(VerificationResult::Manipulated {
                    confidence: *confidence,
                })
            },
            RouteType::Unknown => Ok(VerificationResult::Unverifiable),
        }
    }
    
    /// Trigger divergence alert
    async fn trigger_divergence_alert(&self, divergence: f32) -> Result<()> {
        println!("\n⚠️  DIVERGENCE ALERT ⚠️");
        println!("Your behavioral signature has shifted {:.1}% from baseline!", divergence * 100.0);
        println!("This may indicate:");
        println!("  • Fatigue or stress");
        println!("  • Context shift");
        println!("  • External influence");
        println!("\nWould you like to:");
        println!("  1. Acknowledge and continue");
        println!("  2. Reset to baseline");
        println!("  3. Create new baseline branch");
        
        Ok(())
    }
}

impl DivergenceMonitor {
    /// Measure divergence from baseline
    pub fn measure(&mut self, current: &super::SignatureVectors) -> f32 {
        use super::VectorDistance;
        
        let style_dist = self.baseline.style.distance(&current.style);
        let behavior_dist = self.baseline.behavior.distance(&current.behavior);
        let emotional_dist = self.baseline.emotional.distance(&current.emotional);
        
        let total_divergence = (style_dist + behavior_dist + emotional_dist) / 3.0;
        
        // Record measurement
        self.history.push(DivergenceMeasurement {
            timestamp: Utc::now(),
            divergence: total_divergence,
            primary_factors: self.identify_factors(&self.baseline, current),
        });
        
        // Keep history bounded
        if self.history.len() > 100 {
            self.history.remove(0);
        }
        
        total_divergence
    }
    
    /// Identify primary divergence factors
    fn identify_factors(&self, baseline: &super::SignatureVectors, current: &super::SignatureVectors) -> Vec<String> {
        let mut factors = Vec::new();
        
        if (baseline.style.terseness - current.style.terseness).abs() > 0.3 {
            factors.push("communication_style".to_string());
        }
        
        if (baseline.behavior.patience_level - current.behavior.patience_level).abs() > 0.3 {
            factors.push("patience_shift".to_string());
        }
        
        if (baseline.emotional.frustration - current.emotional.frustration).abs() > 0.3 {
            factors.push("emotional_state".to_string());
        }
        
        factors
    }
}

/// Beacon emission result for external display
#[derive(Debug, Serialize, Deserialize)]
pub struct BeaconEmission {
    pub identity_match: f32,
    pub mode: String,
    pub drift: f32,
    pub ai_connections: Vec<AIConnectionStatus>,
    pub alerts: Vec<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct AIConnectionStatus {
    pub model: String,
    pub provider: String,
    pub route: String,
    pub trust: f32,
    pub warnings: Vec<String>,
}