rullama-seal 0.12.0

Self-Evolving Agentic Learning (SEAL) for rullama — coreference resolution, query-core extraction, learned-pattern store (LanceDB-backed), reflection, knowledge integration
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
//! SEAL Pattern Store
//!
//! Provides persistent storage for learned query patterns used by the SEAL
//! (Self-Evolving Agentic Learning) system. Patterns are stored in LanceDB
//! with embeddings for semantic similarity matching.

use anyhow::{Context, Result};
use arrow_array::{
    Array, ArrayRef, FixedSizeListArray, Float32Array, Int32Array, Int64Array, RecordBatch,
    RecordBatchIterator, StringArray,
};
use arrow_schema::{DataType, Field, Schema};
use futures::TryStreamExt;
use lancedb::query::{ExecutableQuery, QueryBase};
use std::sync::Arc;

use rullama_storage::CachedEmbeddingProvider;
use rullama_storage::LanceDatabase;

use super::learning::QueryPattern;
use super::query_core::QuestionType;

/// LanceDB extension trait: SEAL-patterns table management.
///
/// Lives next to [`PatternStore`] because the SEAL patterns table is its
/// only consumer; previously declared in the CLI's `crate::storage`
/// aggregator. Implemented for [`LanceDatabase`] below.
pub trait LanceDatabaseExt {
    /// Ensure the SEAL patterns table exists, creating it if missing.
    fn ensure_seal_patterns_table(
        &self,
        embedding_dim: usize,
    ) -> impl std::future::Future<Output = Result<()>> + Send;

    /// Open the SEAL patterns table.
    fn seal_patterns_table(
        &self,
    ) -> impl std::future::Future<Output = Result<lancedb::Table>> + Send;

    /// Schema for the SEAL patterns table at the given embedding dimension.
    fn seal_patterns_schema(dimension: usize) -> Arc<Schema>;
}

impl LanceDatabaseExt for LanceDatabase {
    async fn ensure_seal_patterns_table(&self, embedding_dim: usize) -> Result<()> {
        let table_name = "seal_patterns";
        let table_names = self.connection().table_names().execute().await?;

        if table_names.contains(&table_name.to_string()) {
            return Ok(());
        }

        let schema = Self::seal_patterns_schema(embedding_dim);
        let empty_batch = RecordBatch::new_empty(schema.clone());
        let batches = RecordBatchIterator::new(vec![Ok(empty_batch)], schema.clone());

        self.connection()
            .create_table(
                table_name,
                Box::new(batches) as Box<dyn arrow_array::RecordBatchReader + Send>,
            )
            .execute()
            .await
            .context("Failed to create seal_patterns table")?;

        Ok(())
    }

    async fn seal_patterns_table(&self) -> Result<lancedb::Table> {
        self.connection()
            .open_table("seal_patterns")
            .execute()
            .await
            .context("Failed to open seal_patterns table")
    }

    fn seal_patterns_schema(dimension: usize) -> Arc<Schema> {
        Arc::new(Schema::new(vec![
            Field::new(
                "vector",
                DataType::FixedSizeList(
                    Arc::new(Field::new("item", DataType::Float32, true)),
                    dimension as i32,
                ),
                false,
            ),
            Field::new("pattern_id", DataType::Utf8, false),
            Field::new("question_type", DataType::Utf8, false),
            Field::new("template", DataType::Utf8, false),
            Field::new("entity_types", DataType::Utf8, false),
            Field::new("success_count", DataType::Int32, false),
            Field::new("failure_count", DataType::Int32, false),
            Field::new("avg_results", DataType::Float32, false),
            Field::new("last_used", DataType::Int64, false),
            Field::new("created_at", DataType::Int64, false),
        ]))
    }
}

/// Metadata for a persisted SEAL pattern.
#[derive(Debug, Clone)]
pub struct PatternMetadata {
    /// Stable identifier for the pattern row.
    pub pattern_id: String,
    /// Serialized [`super::query_core::QuestionType`] (tag string).
    pub question_type: String,
    /// Pattern template string (S-expression-like form).
    pub template: String,
    /// Entity-type tags this pattern was derived from.
    pub entity_types: Vec<String>,
    /// Times this pattern produced a useful result.
    pub success_count: u32,
    /// Times this pattern failed to produce a useful result.
    pub failure_count: u32,
    /// Average number of results returned when this pattern was used.
    pub avg_results: f32,
    /// Last-used timestamp (Unix seconds).
    pub last_used: i64,
    /// Creation timestamp (Unix seconds).
    pub created_at: i64,
}

impl PatternMetadata {
    /// Calculate pattern reliability score
    pub fn reliability(&self) -> f32 {
        let total = self.success_count + self.failure_count;
        if total == 0 {
            return 0.0;
        }
        self.success_count as f32 / total as f32
    }
}

/// Store for SEAL learned patterns with semantic search
pub struct PatternStore {
    client: Arc<LanceDatabase>,
    embeddings: Arc<CachedEmbeddingProvider>,
}

impl PatternStore {
    /// Create a new pattern store
    pub fn new(client: Arc<LanceDatabase>, embeddings: Arc<CachedEmbeddingProvider>) -> Self {
        Self { client, embeddings }
    }

    /// Save a new pattern or update an existing one
    pub async fn save_pattern(&self, pattern: &QueryPattern, template: &str) -> Result<()> {
        // Generate embedding from template
        let embedding = self.embeddings.embed(template)?;

        // Get current timestamp
        let now = chrono::Utc::now().timestamp();

        // Check if pattern exists
        if self.get_pattern(&pattern.id).await?.is_some() {
            // Update existing pattern
            self.update_pattern(pattern, now).await
        } else {
            // Insert new pattern
            self.insert_pattern(pattern, template, &embedding, now)
                .await
        }
    }

    /// Insert a new pattern
    async fn insert_pattern(
        &self,
        pattern: &QueryPattern,
        template: &str,
        embedding: &[f32],
        timestamp: i64,
    ) -> Result<()> {
        let table = self.client.seal_patterns_table().await?;
        let dimension = self.embeddings.dimension();

        // Create schema
        let schema = Arc::new(Schema::new(vec![
            Field::new(
                "vector",
                DataType::FixedSizeList(
                    Arc::new(Field::new("item", DataType::Float32, true)),
                    dimension as i32,
                ),
                false,
            ),
            Field::new("pattern_id", DataType::Utf8, false),
            Field::new("question_type", DataType::Utf8, false),
            Field::new("template", DataType::Utf8, false),
            Field::new("entity_types", DataType::Utf8, false),
            Field::new("success_count", DataType::Int32, false),
            Field::new("failure_count", DataType::Int32, false),
            Field::new("avg_results", DataType::Float32, false),
            Field::new("last_used", DataType::Int64, false),
            Field::new("created_at", DataType::Int64, false),
        ]));

        // Create embedding array
        let embedding_array = Float32Array::from(embedding.to_vec());
        let vector_field = Arc::new(Field::new("item", DataType::Float32, true));
        let vectors = FixedSizeListArray::new(
            vector_field,
            dimension as i32,
            Arc::new(embedding_array),
            None,
        );

        let pattern_id = StringArray::from(vec![pattern.id.as_str()]);
        let question_type =
            StringArray::from(vec![format!("{:?}", pattern.question_type).as_str()]);
        let template_arr = StringArray::from(vec![template]);
        let entity_types = StringArray::from(vec![serde_json::to_string(&pattern.required_types)?]);
        let success_count = Int32Array::from(vec![pattern.success_count as i32]);
        let failure_count = Int32Array::from(vec![pattern.failure_count as i32]);
        let avg_results = Float32Array::from(vec![pattern.avg_results]);
        let last_used = Int64Array::from(vec![timestamp]);
        let created_at = Int64Array::from(vec![timestamp]);

        let batch = RecordBatch::try_new(
            schema.clone(),
            vec![
                Arc::new(vectors) as ArrayRef,
                Arc::new(pattern_id),
                Arc::new(question_type),
                Arc::new(template_arr),
                Arc::new(entity_types),
                Arc::new(success_count),
                Arc::new(failure_count),
                Arc::new(avg_results),
                Arc::new(last_used),
                Arc::new(created_at),
            ],
        )?;

        let batches = RecordBatchIterator::new(vec![Ok(batch)], schema);

        table
            .add(Box::new(batches) as Box<dyn arrow_array::RecordBatchReader + Send>)
            .execute()
            .await
            .context("Failed to insert pattern")?;

        Ok(())
    }

    /// Update an existing pattern's statistics
    async fn update_pattern(&self, pattern: &QueryPattern, timestamp: i64) -> Result<()> {
        let table = self.client.seal_patterns_table().await?;

        // LanceDB doesn't support direct updates, so we need to delete and re-insert
        // Delete existing record
        table
            .delete(&format!("pattern_id = '{}'", pattern.id))
            .await
            .context("Failed to delete old pattern")?;

        // Re-insert with updated stats
        let embedding = self.embeddings.embed(&pattern.template)?;
        self.insert_pattern(pattern, &pattern.template, &embedding, timestamp)
            .await
    }

    /// Get a pattern by ID
    pub async fn get_pattern(&self, pattern_id: &str) -> Result<Option<PatternMetadata>> {
        let table = self.client.seal_patterns_table().await?;

        let filter = format!("pattern_id = '{}'", pattern_id);
        let stream = table
            .query()
            .only_if(filter)
            .execute()
            .await
            .context("Failed to query pattern")?;

        let batches: Vec<RecordBatch> = stream.try_collect().await?;

        if batches.is_empty() {
            return Ok(None);
        }

        let batch = &batches[0];
        if batch.num_rows() == 0 {
            return Ok(None);
        }

        Ok(Some(self.batch_to_metadata(batch, 0)?))
    }

    /// Search for similar patterns using semantic similarity
    pub async fn search_similar(
        &self,
        query: &str,
        limit: usize,
        min_score: f32,
    ) -> Result<Vec<(PatternMetadata, f32)>> {
        let embedding = self.embeddings.embed(query)?;
        let table = self.client.seal_patterns_table().await?;

        let stream = table
            .vector_search(embedding)
            .context("Failed to create vector search")?
            .limit(limit)
            .execute()
            .await
            .context("Failed to execute vector search")?;

        let batches: Vec<RecordBatch> = stream.try_collect().await?;

        let mut patterns = Vec::new();
        for batch in &batches {
            for i in 0..batch.num_rows() {
                // Get distance score (convert to similarity)
                let distance_col = batch
                    .column_by_name("_distance")
                    .context("Missing distance column")?;
                let distance = distance_col
                    .as_any()
                    .downcast_ref::<Float32Array>()
                    .context("Invalid distance type")?
                    .value(i);

                // Convert L2 distance to similarity score (0-1)
                let similarity = 1.0 / (1.0 + distance);

                if similarity >= min_score {
                    let metadata = self.batch_to_metadata(batch, i)?;
                    patterns.push((metadata, similarity));
                }
            }
        }

        // Sort by similarity descending
        patterns.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        Ok(patterns)
    }

    /// Get patterns by question type
    pub async fn get_by_question_type(
        &self,
        question_type: &QuestionType,
    ) -> Result<Vec<PatternMetadata>> {
        let table = self.client.seal_patterns_table().await?;
        let type_str = format!("{:?}", question_type);

        let filter = format!("question_type = '{}'", type_str);
        let stream = table
            .query()
            .only_if(filter)
            .execute()
            .await
            .context("Failed to query patterns by type")?;

        let batches: Vec<RecordBatch> = stream.try_collect().await?;

        let mut patterns = Vec::new();
        for batch in &batches {
            for i in 0..batch.num_rows() {
                patterns.push(self.batch_to_metadata(batch, i)?);
            }
        }

        Ok(patterns)
    }

    /// Get high-reliability patterns (for learning context)
    pub async fn get_reliable_patterns(
        &self,
        min_reliability: f32,
    ) -> Result<Vec<PatternMetadata>> {
        let table = self.client.seal_patterns_table().await?;

        let stream = table
            .query()
            .execute()
            .await
            .context("Failed to query all patterns")?;

        let batches: Vec<RecordBatch> = stream.try_collect().await?;

        let mut patterns = Vec::new();
        for batch in &batches {
            for i in 0..batch.num_rows() {
                let metadata = self.batch_to_metadata(batch, i)?;
                if metadata.reliability() >= min_reliability {
                    patterns.push(metadata);
                }
            }
        }

        // Sort by reliability descending
        patterns.sort_by(|a, b| {
            b.reliability()
                .partial_cmp(&a.reliability())
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        Ok(patterns)
    }

    /// Delete patterns with low reliability (cleanup)
    pub async fn prune_low_reliability(
        &self,
        min_reliability: f32,
        min_uses: u32,
    ) -> Result<usize> {
        let table = self.client.seal_patterns_table().await?;

        // Get all patterns
        let stream = table
            .query()
            .execute()
            .await
            .context("Failed to query patterns for pruning")?;

        let batches: Vec<RecordBatch> = stream.try_collect().await?;

        let mut to_delete = Vec::new();
        for batch in &batches {
            for i in 0..batch.num_rows() {
                let metadata = self.batch_to_metadata(batch, i)?;
                let total_uses = metadata.success_count + metadata.failure_count;

                // Only prune patterns that have been used enough times
                if total_uses >= min_uses && metadata.reliability() < min_reliability {
                    to_delete.push(metadata.pattern_id);
                }
            }
        }

        // Delete low-reliability patterns
        for pattern_id in &to_delete {
            table
                .delete(&format!("pattern_id = '{}'", pattern_id))
                .await
                .context("Failed to delete low-reliability pattern")?;
        }

        Ok(to_delete.len())
    }

    /// Get pattern count
    pub async fn count(&self) -> Result<usize> {
        let table = self.client.seal_patterns_table().await?;
        let count = table.count_rows(None).await?;
        Ok(count)
    }

    /// Convert a record batch row to PatternMetadata
    fn batch_to_metadata(&self, batch: &RecordBatch, row: usize) -> Result<PatternMetadata> {
        let pattern_id = batch
            .column_by_name("pattern_id")
            .context("Missing pattern_id")?
            .as_any()
            .downcast_ref::<StringArray>()
            .context("Invalid pattern_id type")?
            .value(row)
            .to_string();

        let question_type = batch
            .column_by_name("question_type")
            .context("Missing question_type")?
            .as_any()
            .downcast_ref::<StringArray>()
            .context("Invalid question_type type")?
            .value(row)
            .to_string();

        let template = batch
            .column_by_name("template")
            .context("Missing template")?
            .as_any()
            .downcast_ref::<StringArray>()
            .context("Invalid template type")?
            .value(row)
            .to_string();

        let entity_types_json = batch
            .column_by_name("entity_types")
            .context("Missing entity_types")?
            .as_any()
            .downcast_ref::<StringArray>()
            .context("Invalid entity_types type")?
            .value(row);

        let entity_types: Vec<String> = serde_json::from_str(entity_types_json).unwrap_or_default();

        let success_count = batch
            .column_by_name("success_count")
            .context("Missing success_count")?
            .as_any()
            .downcast_ref::<Int32Array>()
            .context("Invalid success_count type")?
            .value(row) as u32;

        let failure_count = batch
            .column_by_name("failure_count")
            .context("Missing failure_count")?
            .as_any()
            .downcast_ref::<Int32Array>()
            .context("Invalid failure_count type")?
            .value(row) as u32;

        let avg_results = batch
            .column_by_name("avg_results")
            .context("Missing avg_results")?
            .as_any()
            .downcast_ref::<Float32Array>()
            .context("Invalid avg_results type")?
            .value(row);

        let last_used = batch
            .column_by_name("last_used")
            .context("Missing last_used")?
            .as_any()
            .downcast_ref::<Int64Array>()
            .context("Invalid last_used type")?
            .value(row);

        let created_at = batch
            .column_by_name("created_at")
            .context("Missing created_at")?
            .as_any()
            .downcast_ref::<Int64Array>()
            .context("Invalid created_at type")?
            .value(row);

        Ok(PatternMetadata {
            pattern_id,
            question_type,
            template,
            entity_types,
            success_count,
            failure_count,
            avg_results,
            last_used,
            created_at,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rullama_core::graph::EntityType;
    use rullama_storage::databases::VectorDatabase;
    use tempfile::TempDir;

    async fn create_test_store() -> (PatternStore, TempDir) {
        let temp = TempDir::new().unwrap();
        let db_path = temp.path().join("test.lance");

        let client = Arc::new(LanceDatabase::new(db_path.to_str().unwrap()).await.unwrap());
        client.initialize(384).await.unwrap();
        client.ensure_seal_patterns_table(384).await.unwrap();

        let embeddings = Arc::new(CachedEmbeddingProvider::new().unwrap());
        let store = PatternStore::new(client, embeddings);

        (store, temp)
    }

    fn create_test_pattern() -> QueryPattern {
        QueryPattern {
            id: "test-pattern-1".to_string(),
            question_type: QuestionType::Dependency,
            template: "What uses {entity}?".to_string(),
            required_types: vec![EntityType::File],
            success_count: 5,
            failure_count: 1,
            avg_results: 3.5,
            created_at: chrono::Utc::now().timestamp(),
            last_used_at: chrono::Utc::now().timestamp(),
        }
    }

    #[tokio::test]
    async fn test_save_and_get_pattern() {
        if std::env::var("TEST_EMBED_NETWORK").ok().as_deref() != Some("1") {
            eprintln!("skipping: set TEST_EMBED_NETWORK=1 to run (needs FastEmbed model)");
            return;
        }
        let (store, _temp) = create_test_store().await;
        let pattern = create_test_pattern();

        // Save pattern
        store
            .save_pattern(&pattern, "What uses {entity}?")
            .await
            .unwrap();

        // Retrieve pattern
        let retrieved = store.get_pattern(&pattern.id).await.unwrap();
        assert!(retrieved.is_some());

        let metadata = retrieved.unwrap();
        assert_eq!(metadata.pattern_id, pattern.id);
        assert_eq!(metadata.success_count, pattern.success_count);
        assert_eq!(metadata.failure_count, pattern.failure_count);
    }

    #[tokio::test]
    async fn test_reliability_calculation() {
        let metadata = PatternMetadata {
            pattern_id: "test".to_string(),
            question_type: "Definition".to_string(),
            template: "What is {entity}?".to_string(),
            entity_types: vec!["File".to_string()],
            success_count: 8,
            failure_count: 2,
            avg_results: 1.0,
            last_used: 0,
            created_at: 0,
        };

        assert!((metadata.reliability() - 0.8).abs() < 0.01);
    }

    #[tokio::test]
    async fn test_count_patterns() {
        if std::env::var("TEST_EMBED_NETWORK").ok().as_deref() != Some("1") {
            eprintln!("skipping: set TEST_EMBED_NETWORK=1 to run (needs FastEmbed model)");
            return;
        }
        let (store, _temp) = create_test_store().await;

        // Initially empty
        assert_eq!(store.count().await.unwrap(), 0);

        // Add a pattern
        let pattern = create_test_pattern();
        store
            .save_pattern(&pattern, "What uses {entity}?")
            .await
            .unwrap();

        // Should have one pattern
        assert_eq!(store.count().await.unwrap(), 1);
    }
}