anya-core 1.2.0

Enterprise-grade Bitcoin Infrastructure Platform
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
// Canonical Source of Truth Registry Implementation
// This module implements the PRD requirements for work item tracking
// and duplication elimination enforcement

use blake3;
use dashmap::DashMap; // Removed unused DashSet import
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{
    atomic::{AtomicU32, AtomicU64, Ordering},
    Arc,
};
use std::time::{SystemTime, UNIX_EPOCH};
use thiserror::Error;
use tokio::fs;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

/// Errors related to Source of Truth Registry operations
#[derive(Debug, Error)]
pub enum SourceOfTruthError {
    #[error("Work item not found: {0}")]
    WorkItemNotFound(String),

    #[error("Duplication detected: {0}")]
    DuplicationDetected(String),

    #[error("Invalid work item ID format: {0}")]
    InvalidWorkItemId(String),

    #[error("Canonical document conflict: {0}")]
    CanonicalConflict(String),

    #[error("Registry corruption detected: {0}")]
    RegistryCorruption(String),

    #[error("IO error: {0}")]
    IoError(#[from] std::io::Error),

    #[error("Serialization error: {0}")]
    SerializationError(#[from] serde_json::Error),
}

/// Work item status tracking
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum WorkStatus {
    Planning,
    InProgress,
    CodeReview,
    Testing,
    Completed,
    Blocked(String), // Reason for blocking
}

/// Duplication check status
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum DuplicationCheckStatus {
    Passed,
    Failed(String), // Duplication details
    NotChecked,
}

/// Canonical document status
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum CanonicalStatus {
    Draft,
    Review,
    Verified,
    Deprecated,
}

/// Work item tracking structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkItem {
    pub id: String,
    pub title: String,
    pub status: WorkStatus,
    pub component: String,
    pub files_modified: Vec<String>,
    pub duplication_check: DuplicationCheckStatus,
    pub source_of_truth_updated: bool,
    pub verification_hash: [u8; 32],
    pub completion_timestamp: Option<u64>,
    pub evidence_link: String,
    pub dependencies: Vec<String>,
    pub blockers: Vec<String>,
    pub created: u64,
    pub last_updated: u64,
}

/// Canonical document entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CanonicalDocument {
    pub file_path: String,
    pub work_item_id: String,
    pub verification_hash: [u8; 32],
    pub last_updated: u64,
    pub canonical_status: CanonicalStatus,
    pub authority_level: u8, // 1-10, 10 being highest authority
}

/// Source of Truth Registry entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SourceOfTruthEntry {
    pub file_path: String,
    pub work_item_id: String,
    pub verification_hash: [u8; 32],
    pub last_updated: u64,
    pub canonical_status: CanonicalStatus,
}

/// Duplication detection entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DuplicationEntry {
    pub content_hash: [u8; 32],
    pub file_path: String,
    pub function_signature: Option<String>,
    pub first_occurrence: u64, // timestamp
}

/// Function signature for duplication detection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FunctionSignature {
    pub name: String,
    pub parameters: Vec<String>,
    pub return_type: Option<String>,
    pub visibility: String,
    pub file_path: String,
    pub line_number: u32,
}

/// Code fingerprint for advanced duplication detection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodeFingerprint {
    pub content_hash: [u8; 32],
    pub normalized_hash: [u8; 32], // Hash after removing whitespace/comments
    pub function_count: u32,
    pub line_count: u32,
    pub file_path: String,
}

/// Documentation entry for duplication checking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DocumentationEntry {
    pub content_hash: [u8; 32],
    pub title: String,
    pub file_path: String,
    pub section: String,
}

/// Main Source of Truth Registry
#[derive(Debug)]
pub struct SourceOfTruthRegistry {
    /// Registry of all canonical documents
    canonical_documents: DashMap<String, CanonicalDocument>,
    /// Work item tracking
    work_items: DashMap<String, WorkItem>,
    /// Duplication prevention index
    duplication_index: DashMap<String, DuplicationEntry>,
    /// Function signature index
    function_signatures: DashMap<String, FunctionSignature>,
    /// Code fingerprint index
    #[allow(dead_code)]
    code_fingerprints: DashMap<String, CodeFingerprint>,
    /// Documentation content index
    #[allow(dead_code)]
    documentation_index: DashMap<String, DocumentationEntry>,
    /// Last registry update
    last_updated: AtomicU64,
    /// Registry version
    version: AtomicU32,
    /// Registry file path
    registry_path: String,
}

impl SourceOfTruthRegistry {
    /// Create new registry instance
    pub async fn new(registry_path: String) -> Result<Self, SourceOfTruthError> {
        let registry = Self {
            canonical_documents: DashMap::new(),
            work_items: DashMap::new(),
            duplication_index: DashMap::new(),
            function_signatures: DashMap::new(),
            code_fingerprints: DashMap::new(),
            documentation_index: DashMap::new(),
            last_updated: AtomicU64::new(Self::current_timestamp()),
            version: AtomicU32::new(1),
            registry_path,
        };

        // Create registry directory if it doesn't exist
        if let Some(parent) = std::path::Path::new(&registry.registry_path).parent() {
            fs::create_dir_all(parent).await?;
        }

        // Load existing registry if it exists
        if registry.load_from_disk().await.is_err() {
            // If loading fails, initialize with empty registry
            registry.save_to_disk().await?;
        }

        Ok(registry)
    }

    /// Generate unique work item ID
    pub fn generate_work_item_id(&self) -> String {
        let now = chrono::Utc::now();
        let date_prefix = now.format("%Y-%m-%d").to_string();

        // Find next sequence number for today
        let mut sequence = 1;
        loop {
            let candidate_id = format!("WI-{date_prefix}-{sequence}");
            if !self.work_items.contains_key(&candidate_id) {
                return candidate_id;
            }
            sequence += 1;
        }
    }

    /// Create new work item with comprehensive validation
    pub async fn create_work_item(
        &self,
        title: String,
        component: String,
    ) -> Result<WorkItem, SourceOfTruthError> {
        // 1. Generate unique ID
        let work_id = self.generate_work_item_id();

        // 2. Run pre-work duplication check
        let duplication_status = self.check_work_item_duplication(&title, &component).await?;
        if matches!(duplication_status, DuplicationCheckStatus::Failed(_)) {
            return Err(SourceOfTruthError::DuplicationDetected(format!(
                "Work item title or component already exists: {title}"
            )));
        }

        // 3. Create work item
        let work_item = WorkItem {
            id: work_id.clone(),
            title,
            status: WorkStatus::Planning,
            component,
            files_modified: Vec::new(),
            duplication_check: duplication_status,
            source_of_truth_updated: false,
            verification_hash: [0u8; 32],
            completion_timestamp: None,
            evidence_link: String::new(),
            dependencies: Vec::new(),
            blockers: Vec::new(),
            created: Self::current_timestamp(),
            last_updated: Self::current_timestamp(),
        };

        // 4. Register in registry
        self.work_items.insert(work_id.clone(), work_item.clone());
        self.update_last_modified();

        // 5. Save to disk
        self.save_to_disk().await?;

        Ok(work_item)
    }

    /// Update work item status with validation
    pub async fn update_work_item_status(
        &self,
        work_id: &str,
        new_status: WorkStatus,
    ) -> Result<(), SourceOfTruthError> {
        use log::debug;
        debug!("update_work_item_status called for work_id: {work_id}, new_status: {new_status:?}");
        // 1. Validate work item exists
        let mut work_item = match self.work_items.get_mut(work_id) {
            Some(item) => item,
            None => {
                debug!("Work item not found: {work_id}");
                return Err(SourceOfTruthError::WorkItemNotFound(work_id.to_string()));
            }
        };

        // 2. Validate status transition
        if let Err(e) = self.validate_status_transition(&work_item.status, &new_status) {
            debug!("Invalid status transition: {e:?}");
            return Err(e);
        }

        // 3. Update work item
        work_item.status = new_status.clone();
        work_item.last_updated = Self::current_timestamp();

        // 4. Handle completion
        if matches!(new_status, WorkStatus::Completed) {
            work_item.completion_timestamp = Some(Self::current_timestamp());
            work_item.verification_hash = self.generate_verification_hash(&work_item).await?;
            work_item.source_of_truth_updated = true;
        }

        self.update_last_modified();
        self.save_to_disk().await?;

        debug!("update_work_item_status completed for work_id: {work_id}");
        Ok(())
    }

    /// Comprehensive duplication check for code files
    pub async fn check_code_duplication(
        &self,
        file_path: &str,
        content: &str,
    ) -> Result<DuplicationCheckStatus, SourceOfTruthError> {
        // 1. Generate content fingerprint
        let content_hash = blake3::hash(content.as_bytes()).into();

        // 2. Check for exact content duplication
        for entry in self.duplication_index.iter() {
            if entry.value().content_hash == content_hash && entry.value().file_path != file_path {
                return Ok(DuplicationCheckStatus::Failed(format!(
                    "Exact content duplication found in {}",
                    entry.value().file_path
                )));
            }
        }

        // 3. Check function signature duplication
        let functions = self.extract_rust_functions(content)?;
        for function in functions {
            let signature_key = format!("{}::{}", function.name, function.parameters.join(","));
            if let Some(existing) = self.function_signatures.get(&signature_key) {
                if existing.file_path != file_path {
                    return Ok(DuplicationCheckStatus::Failed(format!(
                        "Function signature duplication: {} in {}",
                        signature_key, existing.file_path
                    )));
                }
            }
        }

        // 4. Update indexes
        self.duplication_index.insert(
            file_path.to_string(),
            DuplicationEntry {
                content_hash,
                file_path: file_path.to_string(),
                function_signature: None,
                first_occurrence: Self::current_timestamp(),
            },
        );

        Ok(DuplicationCheckStatus::Passed)
    }

    /// Check for work item title/component duplication
    async fn check_work_item_duplication(
        &self,
        title: &str,
        component: &str,
    ) -> Result<DuplicationCheckStatus, SourceOfTruthError> {
        for item in self.work_items.iter() {
            let work_item = item.value();
            if work_item.title == title && work_item.component == component {
                return Ok(DuplicationCheckStatus::Failed(format!(
                    "Duplicate work item: {title} in {component}"
                )));
            }
        }
        Ok(DuplicationCheckStatus::Passed)
    }

    /// Extract Rust function signatures from source code
    fn extract_rust_functions(
        &self,
        content: &str,
    ) -> Result<Vec<FunctionSignature>, SourceOfTruthError> {
        let mut functions = Vec::new();
        let lines: Vec<&str> = content.lines().collect();

        for (line_num, line) in lines.iter().enumerate() {
            if let Some(func) = self.parse_rust_function_signature(line, line_num as u32 + 1) {
                functions.push(func);
            }
        }

        Ok(functions)
    }

    /// Parse a single Rust function signature
    fn parse_rust_function_signature(
        &self,
        line: &str,
        line_number: u32,
    ) -> Option<FunctionSignature> {
        let trimmed = line.trim();

        // Simple regex-like parsing for Rust functions
        if trimmed.starts_with("pub fn ") || trimmed.starts_with("fn ") {
            let visibility = if trimmed.starts_with("pub ") {
                "pub"
            } else {
                "private"
            };

            // Extract function name and parameters (simplified)
            if let Some(paren_start) = trimmed.find('(') {
                if let Some(fn_start) = trimmed.find("fn ") {
                    let name_start = fn_start + 3;
                    let name = trimmed[name_start..paren_start].trim().to_string();

                    // Extract parameters (simplified - just parameter names)
                    if let Some(paren_end) = trimmed.find(')') {
                        let param_str = &trimmed[paren_start + 1..paren_end];
                        let parameters: Vec<String> = param_str
                            .split(',')
                            .map(|p| p.trim().split(':').next().unwrap_or("").trim().to_string())
                            .filter(|p| !p.is_empty())
                            .collect();

                        // Extract return type
                        let return_type = trimmed.find("->").map(|arrow_pos| {
                            trimmed[arrow_pos + 2..]
                                .split_whitespace()
                                .next()
                                .unwrap_or("")
                                .to_string()
                        });

                        return Some(FunctionSignature {
                            name,
                            parameters,
                            return_type,
                            visibility: visibility.to_string(),
                            file_path: String::new(), // Set by caller
                            line_number,
                        });
                    }
                }
            }
        }

        None
    }

    /// Validate status transition is allowed
    fn validate_status_transition(
        &self,
        current: &WorkStatus,
        new: &WorkStatus,
    ) -> Result<(), SourceOfTruthError> {
        use log::debug;
        match (current, new) {
            (WorkStatus::Planning, WorkStatus::InProgress) => Ok(()),
            (WorkStatus::InProgress, WorkStatus::CodeReview) => Ok(()),
            (WorkStatus::CodeReview, WorkStatus::Testing) => Ok(()),
            (WorkStatus::Testing, WorkStatus::Completed) => Ok(()),
            (_, WorkStatus::Blocked(_)) => Ok(()), // Can always be blocked
            (WorkStatus::Blocked(_), _) => Ok(()), // Can transition from blocked to any state
            _ => {
                debug!("Invalid status transition from {current:?} to {new:?}");
                Err(SourceOfTruthError::InvalidWorkItemId(format!(
                    "Invalid status transition from {current:?} to {new:?}"
                )))
            }
        }
    }

    /// Generate verification hash for completed work item
    async fn generate_verification_hash(
        &self,
        work_item: &WorkItem,
    ) -> Result<[u8; 32], SourceOfTruthError> {
        let mut hasher = blake3::Hasher::new();
        hasher.update(work_item.id.as_bytes());
        hasher.update(work_item.title.as_bytes());
        hasher.update(&work_item.completion_timestamp.unwrap_or(0).to_le_bytes());

        // Include hash of all modified files
        for file_path in &work_item.files_modified {
            if let Ok(content) = fs::read_to_string(file_path).await {
                hasher.update(content.as_bytes());
            }
        }

        Ok(hasher.finalize().into())
    }

    /// Get current timestamp in nanoseconds
    fn current_timestamp() -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos() as u64
    }

    /// Update last modified timestamp
    fn update_last_modified(&self) {
        self.last_updated
            .store(Self::current_timestamp(), Ordering::Relaxed);
        self.version.fetch_add(1, Ordering::Relaxed);
    }

    /// Save registry to disk
    async fn save_to_disk(&self) -> Result<(), SourceOfTruthError> {
        // Create a serializable version of the registry
        let registry_data = RegistryData {
            canonical_documents: self
                .canonical_documents
                .iter()
                .map(|entry| (entry.key().clone(), entry.value().clone()))
                .collect(),
            work_items: self
                .work_items
                .iter()
                .map(|entry| (entry.key().clone(), entry.value().clone()))
                .collect(),
            duplication_index: self
                .duplication_index
                .iter()
                .map(|entry| (entry.key().clone(), entry.value().clone()))
                .collect(),
            last_updated: self.last_updated.load(Ordering::Relaxed),
            version: self.version.load(Ordering::Relaxed),
        };

        let json_data = serde_json::to_string_pretty(&registry_data)?;
        let mut file = fs::File::create(&self.registry_path).await?;
        file.write_all(json_data.as_bytes()).await?;

        Ok(())
    }

    /// Load registry from disk
    async fn load_from_disk(&self) -> Result<(), SourceOfTruthError> {
        let mut file = fs::File::open(&self.registry_path).await?;
        let mut contents = String::new();
        file.read_to_string(&mut contents).await?;

        let registry_data: RegistryData = serde_json::from_str(&contents)?;

        // Clear existing data
        self.canonical_documents.clear();
        self.work_items.clear();
        self.duplication_index.clear();

        // Load data
        for (key, value) in registry_data.canonical_documents {
            self.canonical_documents.insert(key, value);
        }
        for (key, value) in registry_data.work_items {
            self.work_items.insert(key, value);
        }
        for (key, value) in registry_data.duplication_index {
            self.duplication_index.insert(key, value);
        }

        self.last_updated
            .store(registry_data.last_updated, Ordering::Relaxed);
        self.version.store(registry_data.version, Ordering::Relaxed);

        Ok(())
    }
}

/// Serializable registry data structure
#[derive(Debug, Serialize, Deserialize)]
struct RegistryData {
    canonical_documents: HashMap<String, CanonicalDocument>,
    work_items: HashMap<String, WorkItem>,
    duplication_index: HashMap<String, DuplicationEntry>,
    last_updated: u64,
    version: u32,
}

/// Global registry instance
static GLOBAL_REGISTRY: once_cell::sync::Lazy<
    Arc<tokio::sync::RwLock<Option<SourceOfTruthRegistry>>>,
> = once_cell::sync::Lazy::new(|| Arc::new(tokio::sync::RwLock::new(None)));

/// Initialize global registry
pub async fn initialize_global_registry(registry_path: String) -> Result<(), SourceOfTruthError> {
    let registry = SourceOfTruthRegistry::new(registry_path).await?;
    let mut global = GLOBAL_REGISTRY.write().await;
    *global = Some(registry);
    Ok(())
}

/// Get reference to global registry
pub async fn get_global_registry() -> Arc<tokio::sync::RwLock<Option<SourceOfTruthRegistry>>> {
    GLOBAL_REGISTRY.clone()
}

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

    #[tokio::test]
    async fn test_work_item_creation() {
        let temp_dir = tempdir().unwrap();
        let registry_path = temp_dir
            .path()
            .join("registry.json")
            .to_string_lossy()
            .to_string();

        let registry = SourceOfTruthRegistry::new(registry_path).await.unwrap();

        let work_item = registry
            .create_work_item("Test work item".to_string(), "test_component".to_string())
            .await
            .unwrap();

        assert!(work_item.id.starts_with("WI-"));
        assert_eq!(work_item.status, WorkStatus::Planning);
        assert_eq!(work_item.duplication_check, DuplicationCheckStatus::Passed);
    }

    #[tokio::test]
    async fn test_duplication_detection() {
        let temp_dir = tempdir().unwrap();
        let registry_path = temp_dir
            .path()
            .join("registry.json")
            .to_string_lossy()
            .to_string();

        let registry = SourceOfTruthRegistry::new(registry_path).await.unwrap();

        // First work item should pass
        let _work_item1 = registry
            .create_work_item("Unique title".to_string(), "component1".to_string())
            .await
            .unwrap();

        // Duplicate should fail
        let result = registry
            .create_work_item("Unique title".to_string(), "component1".to_string())
            .await;

        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            SourceOfTruthError::DuplicationDetected(_)
        ));
    }

    #[tokio::test]
    async fn test_status_transition_valid() {
        use tokio::time::{timeout, Duration};
        use log::debug;
        let temp_dir = tempdir().unwrap();
        let registry_path = temp_dir
            .path()
            .join("registry_valid.json")
            .to_string_lossy()
            .to_string();

        let registry = SourceOfTruthRegistry::new(registry_path).await.unwrap();
        let work_item = registry
            .create_work_item("Status test valid".to_string(), "test_component".to_string())
            .await
            .unwrap();

        let valid = timeout(Duration::from_secs(10), registry.update_work_item_status(&work_item.id, WorkStatus::InProgress)).await;
        match valid {
            Ok(Ok(_)) => debug!("Valid status transition succeeded"),
            Ok(Err(e)) => panic!("Valid status transition failed: {e:?}"),
            Err(_) => panic!("Timeout on valid status transition"),
        }
    }

    #[tokio::test]
    async fn test_status_transition_invalid() {
        use tokio::time::{timeout, Duration};
        use log::debug;
        let temp_dir = tempdir().unwrap();
        let registry_path = temp_dir
            .path()
            .join("registry_invalid.json")
            .to_string_lossy()
            .to_string();

        let registry = SourceOfTruthRegistry::new(registry_path).await.unwrap();
        let work_item = registry
            .create_work_item("Status test invalid".to_string(), "test_component".to_string())
            .await
            .unwrap();

        // Try invalid transition: Planning -> Completed (should fail)
        let invalid = timeout(Duration::from_secs(10), registry.update_work_item_status(&work_item.id, WorkStatus::Completed)).await;
        match invalid {
            Ok(Ok(_)) => panic!("Invalid status transition unexpectedly succeeded"),
            Ok(Err(_)) => debug!("Invalid status transition correctly failed"),
            Err(_) => panic!("Timeout on invalid status transition"),
        }
    }
}