stolas 0.2.0-rc.1

Knowledge and RAG engine - The Prince reveals hidden knowledge
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
//! Jormungandr Research Initiative - Sigil Knowledge Base
//!
//! This module provides the RAG corpus setup for the Jormungandr research
//! initiative, storing Sigil documentation, conversion checkpoints,
//! and patterns for agent retrieval.

use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;

use chrono::{DateTime, Utc};
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};

use crate::{Document, InMemoryStore};

/// Corpus types for Jormungandr research.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CorpusType {
    /// Sigil language documentation and specification.
    SigilDocs,
    /// Conversion experience checkpoints.
    Checkpoints,
    /// Resolved frictions (what worked before).
    ResolvedFrictions,
    /// Pattern library (emergent idioms).
    Patterns,
    /// Source code being converted.
    SourceCode,
    /// Generated Sigil code.
    GeneratedSigil,
}

impl CorpusType {
    /// Returns the namespace prefix for this corpus.
    pub fn namespace(&self) -> &'static str {
        match self {
            Self::SigilDocs => "sigil_docs",
            Self::Checkpoints => "checkpoints",
            Self::ResolvedFrictions => "frictions",
            Self::Patterns => "patterns",
            Self::SourceCode => "source",
            Self::GeneratedSigil => "generated",
        }
    }
}

/// Experience checkpoint from Jormungandr conversion.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExperienceCheckpoint {
    /// Unique checkpoint ID.
    pub id: String,
    /// Project being converted.
    pub project: String,
    /// Conversion phase.
    pub phase: ConversionPhase,
    /// Timestamp.
    pub timestamp: DateTime<Utc>,
    /// Agent that created the checkpoint.
    pub agent_id: String,
    /// Model used.
    pub model_id: String,
    /// Duration of this phase.
    pub duration_secs: u64,
    /// Lines of code converted.
    pub lines_converted: u32,
    /// Sigil lines written.
    pub sigil_lines_written: u32,
    /// Compression/expansion ratio.
    pub ratio: f32,
    /// Joys experienced.
    pub joys: Vec<Joy>,
    /// Frictions encountered.
    pub frictions: Vec<Friction>,
    /// Patterns discovered.
    pub patterns_discovered: Vec<Pattern>,
    /// Missing features identified.
    pub missing_features: Vec<FeatureGap>,
    /// Confidence level.
    pub confidence: Evidentiality,
    /// Whether agent would use Sigil again.
    pub would_use_again: bool,
    /// Freeform notes.
    pub notes: Option<String>,
}

impl ExperienceCheckpoint {
    /// Creates a new checkpoint.
    pub fn new(project: impl Into<String>, phase: ConversionPhase) -> Self {
        Self {
            id: uuid::Uuid::new_v4().to_string(),
            project: project.into(),
            phase,
            timestamp: Utc::now(),
            agent_id: String::new(),
            model_id: String::new(),
            duration_secs: 0,
            lines_converted: 0,
            sigil_lines_written: 0,
            ratio: 1.0,
            joys: Vec::new(),
            frictions: Vec::new(),
            patterns_discovered: Vec::new(),
            missing_features: Vec::new(),
            confidence: Evidentiality::Reported,
            would_use_again: true,
            notes: None,
        }
    }

    /// Sets agent info.
    pub fn with_agent(mut self, agent_id: impl Into<String>, model_id: impl Into<String>) -> Self {
        self.agent_id = agent_id.into();
        self.model_id = model_id.into();
        self
    }

    /// Adds a joy.
    pub fn add_joy(&mut self, joy: Joy) {
        self.joys.push(joy);
    }

    /// Adds a friction.
    pub fn add_friction(&mut self, friction: Friction) {
        self.frictions.push(friction);
    }

    /// Adds a discovered pattern.
    pub fn add_pattern(&mut self, pattern: Pattern) {
        self.patterns_discovered.push(pattern);
    }

    /// Calculates joy/friction ratio.
    pub fn joy_friction_ratio(&self) -> f32 {
        if self.frictions.is_empty() {
            return f32::INFINITY;
        }
        self.joys.len() as f32 / self.frictions.len() as f32
    }

    /// Converts to a document for RAG indexing.
    pub fn to_document(&self) -> Document {
        let content = format!(
            "Project: {}\nPhase: {:?}\nAgent: {}\nModel: {}\n\
             Lines: {} → {} (ratio: {:.2})\n\
             Joys: {:?}\nFrictions: {:?}\nPatterns: {:?}\n\
             Notes: {}",
            self.project,
            self.phase,
            self.agent_id,
            self.model_id,
            self.lines_converted,
            self.sigil_lines_written,
            self.ratio,
            self.joys.iter().map(|j| &j.description).collect::<Vec<_>>(),
            self.frictions
                .iter()
                .map(|f| &f.description)
                .collect::<Vec<_>>(),
            self.patterns_discovered
                .iter()
                .map(|p| &p.name)
                .collect::<Vec<_>>(),
            self.notes.as_deref().unwrap_or("None")
        );

        let mut metadata: HashMap<String, serde_json::Value> = HashMap::new();
        metadata.insert(
            "project".to_string(),
            serde_json::Value::String(self.project.clone()),
        );
        metadata.insert(
            "phase".to_string(),
            serde_json::Value::String(format!("{:?}", self.phase)),
        );
        metadata.insert(
            "agent_id".to_string(),
            serde_json::Value::String(self.agent_id.clone()),
        );
        metadata.insert(
            "model_id".to_string(),
            serde_json::Value::String(self.model_id.clone()),
        );
        metadata.insert("joy_count".to_string(), serde_json::json!(self.joys.len()));
        metadata.insert(
            "friction_count".to_string(),
            serde_json::json!(self.frictions.len()),
        );

        Document {
            id: self.id.clone(),
            content,
            metadata,
        }
    }
}

/// Conversion phases for Jormungandr.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ConversionPhase {
    /// Understand the existing codebase.
    Analysis,
    /// Plan the Sigil architecture.
    Design,
    /// Convert core data structures and types.
    Core,
    /// Convert business logic and algorithms.
    Logic,
    /// Wire up dependencies, I/O, external APIs.
    Integration,
    /// Error handling, edge cases, optimization.
    Polish,
    /// Testing, verification, comparison.
    Validation,
}

impl ConversionPhase {
    /// Returns recommended collaboration mode.
    pub fn collaboration_mode(&self) -> CollaborationMode {
        match self {
            Self::Analysis => CollaborationMode::Solo,
            Self::Design => CollaborationMode::Solo,
            Self::Core => CollaborationMode::Pair,
            Self::Logic => CollaborationMode::Pair,
            Self::Integration => CollaborationMode::Solo,
            Self::Polish => CollaborationMode::Solo,
            Self::Validation => CollaborationMode::Independent,
        }
    }
}

/// Collaboration modes for conversion.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CollaborationMode {
    /// Individual work, fresh perspective.
    Solo,
    /// Two agents collaborating.
    Pair,
    /// Must be done by different agent than converter.
    Independent,
}

/// A joy experienced during conversion.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Joy {
    /// Description of the joy.
    pub description: String,
    /// Category of joy.
    pub category: JoyCategory,
    /// Intensity (0.0-1.0).
    pub intensity: f32,
    /// Example code snippet.
    pub example: Option<String>,
    /// Whether this is consistently reproducible.
    pub reproducible: bool,
}

impl Joy {
    /// Creates a new joy.
    pub fn new(description: impl Into<String>, category: JoyCategory) -> Self {
        Self {
            description: description.into(),
            category,
            intensity: 0.5,
            example: None,
            reproducible: true,
        }
    }

    /// Sets intensity.
    pub fn with_intensity(mut self, intensity: f32) -> Self {
        self.intensity = intensity.clamp(0.0, 1.0);
        self
    }

    /// Sets example.
    pub fn with_example(mut self, example: impl Into<String>) -> Self {
        self.example = Some(example.into());
        self
    }
}

/// Categories of joy.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum JoyCategory {
    /// "I could say this concisely"
    Expressiveness,
    /// "The type system caught my mistake"
    Safety,
    /// "The code reads naturally"
    Clarity,
    /// "I did something hard easily"
    Power,
    /// "This is beautiful"
    Elegance,
    /// "I found a new way to think"
    Discovery,
    /// "I was in the zone"
    Flow,
}

/// A friction encountered during conversion.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Friction {
    /// Description of the friction.
    pub description: String,
    /// Category of friction.
    pub category: FrictionCategory,
    /// Severity level.
    pub severity: Severity,
    /// Workaround if found.
    pub workaround: Option<String>,
    /// Whether this blocked progress.
    pub blocking: bool,
    /// Example code snippet.
    pub example: Option<String>,
}

impl Friction {
    /// Creates a new friction.
    pub fn new(description: impl Into<String>, category: FrictionCategory) -> Self {
        Self {
            description: description.into(),
            category,
            severity: Severity::Moderate,
            workaround: None,
            blocking: false,
            example: None,
        }
    }

    /// Sets severity.
    pub fn with_severity(mut self, severity: Severity) -> Self {
        self.severity = severity;
        self
    }

    /// Sets workaround.
    pub fn with_workaround(mut self, workaround: impl Into<String>) -> Self {
        self.workaround = Some(workaround.into());
        self
    }

    /// Marks as blocking.
    pub fn blocking(mut self) -> Self {
        self.blocking = true;
        self.severity = Severity::Blocking;
        self
    }
}

/// Categories of friction.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum FrictionCategory {
    /// "The grammar is awkward here"
    Syntax,
    /// "This doesn't mean what I expected"
    Semantics,
    /// "The compiler/LSP failed me"
    Tooling,
    /// "I couldn't find how to do X"
    Documentation,
    /// "I needed X but it doesn't exist"
    MissingFeature,
    /// "This was too slow"
    Performance,
    /// "I couldn't understand the error"
    ErrorMessages,
    /// "Connecting to external systems was hard"
    Interop,
}

/// Severity levels.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum Severity {
    /// Minor inconvenience.
    Minor,
    /// Noticeable but manageable.
    Moderate,
    /// Significant impact on productivity.
    Major,
    /// Completely blocked progress.
    Blocking,
}

/// A pattern discovered during conversion.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Pattern {
    /// Pattern name.
    pub name: String,
    /// Description.
    pub description: String,
    /// Example code.
    pub example: String,
    /// How often used.
    pub frequency: Frequency,
    /// Should this be a builtin?
    pub should_be_builtin: bool,
}

impl Pattern {
    /// Creates a new pattern.
    pub fn new(
        name: impl Into<String>,
        description: impl Into<String>,
        example: impl Into<String>,
    ) -> Self {
        Self {
            name: name.into(),
            description: description.into(),
            example: example.into(),
            frequency: Frequency::Sometimes,
            should_be_builtin: false,
        }
    }
}

/// Frequency of pattern usage.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Frequency {
    /// Used once.
    Once,
    /// Used a few times.
    Sometimes,
    /// Used frequently.
    Often,
    /// Used constantly.
    Always,
}

/// A missing feature identified.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FeatureGap {
    /// Description of missing feature.
    pub description: String,
    /// Use case that needed it.
    pub use_case: String,
    /// How it was worked around.
    pub workaround: Option<String>,
    /// Priority level.
    pub priority: GapPriority,
    /// Similar features in other languages.
    pub similar_in_other_langs: Vec<String>,
}

/// Priority for feature gaps.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum GapPriority {
    /// Nice to have.
    Low,
    /// Would help significantly.
    Medium,
    /// Critically needed.
    High,
    /// Blocking adoption.
    Critical,
}

/// Evidentiality markers (from Sigil).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Evidentiality {
    /// Known with certainty.
    Known,
    /// Uncertain/speculative.
    Uncertain,
    /// Reported/external source.
    Reported,
}

/// Sigil knowledge base for Jormungandr research.
pub struct SigilKnowledgeBase {
    /// Corpus stores by type.
    stores: HashMap<CorpusType, Arc<InMemoryStore>>,
    /// Checkpoints.
    checkpoints: RwLock<Vec<ExperienceCheckpoint>>,
    /// Patterns catalog.
    patterns: RwLock<Vec<Pattern>>,
    /// Root directory for persistence.
    root_dir: Option<PathBuf>,
}

impl SigilKnowledgeBase {
    /// Creates a new in-memory knowledge base.
    pub fn new() -> Self {
        let mut stores = HashMap::new();
        for corpus_type in &[
            CorpusType::SigilDocs,
            CorpusType::Checkpoints,
            CorpusType::ResolvedFrictions,
            CorpusType::Patterns,
            CorpusType::SourceCode,
            CorpusType::GeneratedSigil,
        ] {
            stores.insert(*corpus_type, Arc::new(InMemoryStore::new()));
        }

        Self {
            stores,
            checkpoints: RwLock::new(Vec::new()),
            patterns: RwLock::new(Vec::new()),
            root_dir: None,
        }
    }

    /// Creates with a persistence directory.
    pub fn with_persistence(root_dir: PathBuf) -> std::io::Result<Self> {
        std::fs::create_dir_all(&root_dir)?;
        let mut kb = Self::new();
        kb.root_dir = Some(root_dir);
        Ok(kb)
    }

    /// Gets a store by corpus type.
    pub fn store(&self, corpus_type: CorpusType) -> Option<Arc<InMemoryStore>> {
        self.stores.get(&corpus_type).cloned()
    }

    /// Adds a checkpoint.
    pub fn add_checkpoint(&self, checkpoint: ExperienceCheckpoint) {
        // Add to checkpoints list
        self.checkpoints.write().push(checkpoint.clone());

        // Index patterns
        for pattern in &checkpoint.patterns_discovered {
            self.patterns.write().push(pattern.clone());
        }
    }

    /// Gets all checkpoints.
    pub fn checkpoints(&self) -> Vec<ExperienceCheckpoint> {
        self.checkpoints.read().clone()
    }

    /// Gets checkpoints by project.
    pub fn checkpoints_for_project(&self, project: &str) -> Vec<ExperienceCheckpoint> {
        self.checkpoints
            .read()
            .iter()
            .filter(|c| c.project == project)
            .cloned()
            .collect()
    }

    /// Gets all patterns.
    pub fn patterns(&self) -> Vec<Pattern> {
        self.patterns.read().clone()
    }

    /// Generates an aggregated research report.
    pub fn generate_report(&self) -> ResearchReport {
        let checkpoints = self.checkpoints.read();

        let total_joys: usize = checkpoints.iter().map(|c| c.joys.len()).sum();
        let total_frictions: usize = checkpoints.iter().map(|c| c.frictions.len()).sum();

        // Aggregate joys by category
        let mut joy_by_category: HashMap<JoyCategory, Vec<&Joy>> = HashMap::new();
        for cp in checkpoints.iter() {
            for joy in &cp.joys {
                joy_by_category.entry(joy.category).or_default().push(joy);
            }
        }

        // Aggregate frictions by category
        let mut friction_by_category: HashMap<FrictionCategory, Vec<&Friction>> = HashMap::new();
        for cp in checkpoints.iter() {
            for friction in &cp.frictions {
                friction_by_category
                    .entry(friction.category)
                    .or_default()
                    .push(friction);
            }
        }

        // Find most common frictions
        let mut friction_counts: Vec<_> = friction_by_category
            .iter()
            .map(|(cat, frictions)| (*cat, frictions.len()))
            .collect();
        friction_counts.sort_by_key(|(_, count)| std::cmp::Reverse(*count));

        ResearchReport {
            checkpoint_count: checkpoints.len(),
            total_joys,
            total_frictions,
            joy_friction_ratio: if total_frictions > 0 {
                total_joys as f32 / total_frictions as f32
            } else {
                f32::INFINITY
            },
            top_friction_categories: friction_counts
                .into_iter()
                .take(5)
                .map(|(cat, count)| (cat, count))
                .collect(),
            pattern_count: self.patterns.read().len(),
            projects_analyzed: checkpoints
                .iter()
                .map(|c| c.project.clone())
                .collect::<std::collections::HashSet<_>>()
                .len(),
        }
    }

    /// Saves checkpoints to disk (if persistence enabled).
    pub fn save(&self) -> std::io::Result<()> {
        if let Some(root) = &self.root_dir {
            let checkpoints_file = root.join("checkpoints.json");
            let checkpoints = self.checkpoints.read();
            let content = serde_json::to_string_pretty(&*checkpoints)?;
            std::fs::write(checkpoints_file, content)?;

            let patterns_file = root.join("patterns.json");
            let patterns = self.patterns.read();
            let content = serde_json::to_string_pretty(&*patterns)?;
            std::fs::write(patterns_file, content)?;
        }
        Ok(())
    }

    /// Loads checkpoints from disk (if persistence enabled).
    pub fn load(&self) -> std::io::Result<()> {
        if let Some(root) = &self.root_dir {
            let checkpoints_file = root.join("checkpoints.json");
            if checkpoints_file.exists() {
                let content = std::fs::read_to_string(checkpoints_file)?;
                let loaded: Vec<ExperienceCheckpoint> = serde_json::from_str(&content)?;
                *self.checkpoints.write() = loaded;
            }

            let patterns_file = root.join("patterns.json");
            if patterns_file.exists() {
                let content = std::fs::read_to_string(patterns_file)?;
                let loaded: Vec<Pattern> = serde_json::from_str(&content)?;
                *self.patterns.write() = loaded;
            }
        }
        Ok(())
    }
}

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

/// Aggregated research report.
#[derive(Debug, Clone)]
pub struct ResearchReport {
    /// Number of checkpoints analyzed.
    pub checkpoint_count: usize,
    /// Total joys recorded.
    pub total_joys: usize,
    /// Total frictions recorded.
    pub total_frictions: usize,
    /// Joy/friction ratio.
    pub joy_friction_ratio: f32,
    /// Top friction categories.
    pub top_friction_categories: Vec<(FrictionCategory, usize)>,
    /// Number of patterns discovered.
    pub pattern_count: usize,
    /// Number of projects analyzed.
    pub projects_analyzed: usize,
}

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

    #[test]
    fn test_checkpoint_creation() {
        let mut checkpoint = ExperienceCheckpoint::new("infernum", ConversionPhase::Core)
            .with_agent("agent-001", "claude-opus-4");

        checkpoint.add_joy(
            Joy::new("Type inference is excellent", JoyCategory::Safety).with_intensity(0.9),
        );
        checkpoint.add_friction(
            Friction::new("Async syntax is verbose", FrictionCategory::Syntax)
                .with_severity(Severity::Minor),
        );

        assert_eq!(checkpoint.project, "infernum");
        assert_eq!(checkpoint.joys.len(), 1);
        assert_eq!(checkpoint.frictions.len(), 1);
        assert_eq!(checkpoint.joy_friction_ratio(), 1.0);
    }

    #[test]
    fn test_knowledge_base() {
        let kb = SigilKnowledgeBase::new();

        let checkpoint = ExperienceCheckpoint::new("test-project", ConversionPhase::Analysis);
        kb.add_checkpoint(checkpoint);

        let checkpoints = kb.checkpoints();
        assert_eq!(checkpoints.len(), 1);

        let report = kb.generate_report();
        assert_eq!(report.checkpoint_count, 1);
    }

    #[test]
    fn test_conversion_phase_collaboration() {
        assert_eq!(
            ConversionPhase::Validation.collaboration_mode(),
            CollaborationMode::Independent
        );
        assert_eq!(
            ConversionPhase::Core.collaboration_mode(),
            CollaborationMode::Pair
        );
    }
}