oxirag 0.1.1

A four-layer RAG engine with SMT-based logic verification and knowledge graph support
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
//! Streaming verification support for the Speculator layer.
//!
//! This module provides streaming capabilities for verification results,
//! allowing consumers to receive partial results as they become available.

use async_trait::async_trait;
use std::pin::Pin;

use crate::error::SpeculatorError;
use crate::layer2_speculator::traits::Speculator;
use crate::types::{Draft, SearchResult, SpeculationDecision, SpeculationResult};

#[cfg(feature = "native")]
use futures::Stream;
#[cfg(feature = "native")]
use tokio::sync::mpsc;

/// A chunk of streaming verification output.
#[derive(Debug, Clone)]
pub struct VerificationChunk {
    /// Unique identifier for this chunk within the stream.
    pub chunk_id: usize,
    /// The content of this chunk.
    pub content: String,
    /// Whether this is the final chunk in the stream.
    pub is_final: bool,
    /// Partial decision if available at this point.
    pub partial_decision: Option<SpeculationDecision>,
    /// Partial confidence score (0.0 to 1.0).
    pub partial_confidence: f32,
}

impl VerificationChunk {
    /// Create a new verification chunk.
    #[must_use]
    pub fn new(chunk_id: usize, content: impl Into<String>) -> Self {
        Self {
            chunk_id,
            content: content.into(),
            is_final: false,
            partial_decision: None,
            partial_confidence: 0.0,
        }
    }

    /// Mark this chunk as final.
    #[must_use]
    pub fn with_final(mut self) -> Self {
        self.is_final = true;
        self
    }

    /// Set a partial decision for this chunk.
    #[must_use]
    pub fn with_decision(mut self, decision: SpeculationDecision) -> Self {
        self.partial_decision = Some(decision);
        self
    }

    /// Set the partial confidence for this chunk.
    #[must_use]
    pub fn with_confidence(mut self, confidence: f32) -> Self {
        self.partial_confidence = confidence;
        self
    }
}

impl Default for VerificationChunk {
    fn default() -> Self {
        Self::new(0, "")
    }
}

/// Streaming verification result that allows consuming chunks as they arrive.
#[cfg(feature = "native")]
pub struct StreamingVerification {
    receiver: mpsc::Receiver<VerificationChunk>,
    final_result: Option<SpeculationResult>,
    collected_chunks: Vec<VerificationChunk>,
}

#[cfg(feature = "native")]
impl StreamingVerification {
    /// Create a new streaming verification with the given receiver.
    #[must_use]
    pub fn new(receiver: mpsc::Receiver<VerificationChunk>) -> Self {
        Self {
            receiver,
            final_result: None,
            collected_chunks: Vec::new(),
        }
    }

    /// Create a streaming verification with a pre-computed result.
    #[must_use]
    pub fn from_result(result: SpeculationResult) -> Self {
        let (_, receiver) = mpsc::channel(1);
        Self {
            receiver,
            final_result: Some(result),
            collected_chunks: Vec::new(),
        }
    }

    /// Get the next chunk from the stream.
    ///
    /// Returns `None` when the stream is exhausted.
    pub async fn next_chunk(&mut self) -> Option<VerificationChunk> {
        let chunk = self.receiver.recv().await;
        if let Some(ref c) = chunk {
            self.collected_chunks.push(c.clone());
        }
        chunk
    }

    /// Convert this streaming verification into a futures Stream.
    #[must_use]
    pub fn into_stream(self) -> Pin<Box<dyn Stream<Item = VerificationChunk> + Send>> {
        Box::pin(tokio_stream::wrappers::ReceiverStream::new(self.receiver))
    }

    /// Collect all remaining chunks and return the final result.
    pub async fn collect(mut self) -> SpeculationResult {
        if let Some(result) = self.final_result {
            return result;
        }

        // Collect all remaining chunks
        while let Some(chunk) = self.receiver.recv().await {
            self.collected_chunks.push(chunk);
        }

        // Build the final result from collected chunks
        self.build_result_from_chunks()
    }

    /// Build a speculation result from collected chunks.
    fn build_result_from_chunks(&self) -> SpeculationResult {
        if self.collected_chunks.is_empty() {
            return SpeculationResult::new(SpeculationDecision::Revise, 0.5)
                .with_explanation("No chunks received");
        }

        // Find the final chunk or use the last one
        let final_chunk = self
            .collected_chunks
            .iter()
            .rfind(|c| c.is_final)
            .or_else(|| self.collected_chunks.last());

        let (decision, confidence) = if let Some(chunk) = final_chunk {
            (
                chunk
                    .partial_decision
                    .clone()
                    .unwrap_or(SpeculationDecision::Revise),
                chunk.partial_confidence,
            )
        } else {
            (SpeculationDecision::Revise, 0.5)
        };

        // Combine all content
        let explanation: String = self
            .collected_chunks
            .iter()
            .map(|c| c.content.as_str())
            .collect::<Vec<_>>()
            .join("");

        SpeculationResult::new(decision, confidence).with_explanation(explanation)
    }

    /// Check if the stream has a pre-computed final result.
    #[must_use]
    pub fn has_final_result(&self) -> bool {
        self.final_result.is_some()
    }

    /// Get a reference to the collected chunks so far.
    #[must_use]
    pub fn collected_chunks(&self) -> &[VerificationChunk] {
        &self.collected_chunks
    }
}

/// Non-native (WASM) placeholder for streaming verification.
#[cfg(not(feature = "native"))]
pub struct StreamingVerification {
    result: SpeculationResult,
}

#[cfg(not(feature = "native"))]
impl StreamingVerification {
    /// Create a streaming verification from a result (WASM doesn't support true streaming).
    #[must_use]
    pub fn from_result(result: SpeculationResult) -> Self {
        Self { result }
    }

    /// Collect the result (immediately available in WASM).
    pub async fn collect(self) -> SpeculationResult {
        self.result
    }
}

/// Trait extension for streaming verification capabilities.
#[async_trait]
pub trait StreamingSpeculator: Speculator {
    /// Verify a draft with streaming output.
    ///
    /// # Arguments
    /// * `draft` - The draft answer to verify
    /// * `context` - The retrieved documents providing context
    ///
    /// # Returns
    /// A streaming verification that can be consumed chunk by chunk.
    async fn verify_draft_streaming(
        &self,
        draft: &Draft,
        context: &[SearchResult],
    ) -> Result<StreamingVerification, SpeculatorError>;
}

/// A wrapper that adds streaming capabilities to any Speculator.
pub struct StreamingSpeculatorWrapper<S: Speculator> {
    inner: S,
    chunk_size: usize,
}

impl<S: Speculator> StreamingSpeculatorWrapper<S> {
    /// Create a new streaming wrapper around an existing speculator.
    #[must_use]
    pub fn new(inner: S) -> Self {
        Self {
            inner,
            chunk_size: 50, // Default: 50 characters per chunk
        }
    }

    /// Set the chunk size for streaming output.
    #[must_use]
    pub fn with_chunk_size(mut self, chunk_size: usize) -> Self {
        self.chunk_size = chunk_size.max(1);
        self
    }

    /// Get a reference to the inner speculator.
    #[must_use]
    pub fn inner(&self) -> &S {
        &self.inner
    }

    /// Get the configured chunk size.
    #[must_use]
    pub fn chunk_size(&self) -> usize {
        self.chunk_size
    }
}

#[cfg(feature = "native")]
#[async_trait]
impl<S: Speculator + Send + Sync> StreamingSpeculator for StreamingSpeculatorWrapper<S> {
    async fn verify_draft_streaming(
        &self,
        draft: &Draft,
        context: &[SearchResult],
    ) -> Result<StreamingVerification, SpeculatorError> {
        // Get the full result first
        let result = self.inner.verify_draft(draft, context).await?;

        // Create a channel for streaming
        let (tx, rx) = mpsc::channel(32);
        let chunk_size = self.chunk_size;
        let explanation = result.explanation.clone();
        let decision = result.decision.clone();
        let confidence = result.confidence;

        // Spawn a task to send chunks
        tokio::spawn(async move {
            let chars: Vec<char> = explanation.chars().collect();
            let total_chunks = chars.len().div_ceil(chunk_size);

            for (i, chunk_chars) in chars.chunks(chunk_size).enumerate() {
                let chunk_content: String = chunk_chars.iter().collect();
                let is_final = i == total_chunks - 1;

                #[allow(clippy::cast_precision_loss)]
                let progress = (i + 1) as f32 / total_chunks as f32;

                let chunk =
                    VerificationChunk::new(i, chunk_content).with_confidence(confidence * progress);

                // Add decision only on final chunk
                let chunk = if is_final {
                    chunk.with_final().with_decision(decision.clone())
                } else {
                    chunk
                };

                if tx.send(chunk).await.is_err() {
                    break;
                }

                // Small delay to simulate streaming
                tokio::time::sleep(tokio::time::Duration::from_millis(1)).await;
            }
        });

        Ok(StreamingVerification::new(rx))
    }
}

#[cfg(not(feature = "native"))]
#[async_trait]
impl<S: Speculator + Send + Sync> StreamingSpeculator for StreamingSpeculatorWrapper<S> {
    async fn verify_draft_streaming(
        &self,
        draft: &Draft,
        context: &[SearchResult],
    ) -> Result<StreamingVerification, SpeculatorError> {
        let result = self.inner.verify_draft(draft, context).await?;
        Ok(StreamingVerification::from_result(result))
    }
}

#[async_trait]
impl<S: Speculator + Send + Sync> Speculator for StreamingSpeculatorWrapper<S> {
    async fn verify_draft(
        &self,
        draft: &Draft,
        context: &[SearchResult],
    ) -> Result<SpeculationResult, SpeculatorError> {
        self.inner.verify_draft(draft, context).await
    }

    async fn revise_draft(
        &self,
        draft: &Draft,
        context: &[SearchResult],
        speculation: &SpeculationResult,
    ) -> Result<Draft, SpeculatorError> {
        self.inner.revise_draft(draft, context, speculation).await
    }

    fn config(&self) -> &crate::layer2_speculator::traits::SpeculatorConfig {
        self.inner.config()
    }
}

#[cfg(all(test, feature = "native"))]
#[allow(clippy::float_cmp)]
mod tests {
    use super::*;
    use crate::layer2_speculator::traits::RuleBasedSpeculator;
    use crate::types::Document;

    fn create_context() -> Vec<SearchResult> {
        vec![
            SearchResult::new(
                Document::new("The capital of France is Paris. It is known for the Eiffel Tower."),
                0.9,
                0,
            ),
            SearchResult::new(
                Document::new("Paris has a population of about 2 million in the city proper."),
                0.85,
                1,
            ),
        ]
    }

    #[tokio::test]
    async fn test_verification_chunk_creation() {
        let chunk = VerificationChunk::new(0, "test content")
            .with_confidence(0.8)
            .with_decision(SpeculationDecision::Accept)
            .with_final();

        assert_eq!(chunk.chunk_id, 0);
        assert_eq!(chunk.content, "test content");
        assert!(chunk.is_final);
        assert_eq!(chunk.partial_confidence, 0.8);
        assert!(matches!(
            chunk.partial_decision,
            Some(SpeculationDecision::Accept)
        ));
    }

    #[tokio::test]
    async fn test_streaming_wrapper_creation() {
        let speculator = RuleBasedSpeculator::default();
        let wrapper = StreamingSpeculatorWrapper::new(speculator).with_chunk_size(100);

        assert_eq!(wrapper.chunk_size(), 100);
    }

    #[tokio::test]
    async fn test_streaming_verification() {
        let speculator = RuleBasedSpeculator::default();
        let wrapper = StreamingSpeculatorWrapper::new(speculator).with_chunk_size(10);

        let draft = Draft::new(
            "The capital of France is Paris, which is famous for the Eiffel Tower.",
            "What is the capital of France?",
        )
        .with_confidence(0.9);

        let context = create_context();
        let streaming = wrapper
            .verify_draft_streaming(&draft, &context)
            .await
            .unwrap();
        let result = streaming.collect().await;

        assert!(result.confidence > 0.0);
    }

    #[tokio::test]
    async fn test_streaming_next_chunk() {
        let speculator = RuleBasedSpeculator::default();
        let wrapper = StreamingSpeculatorWrapper::new(speculator).with_chunk_size(5);

        let draft = Draft::new(
            "Paris is the capital of France with the Eiffel Tower.",
            "What is the capital of France?",
        )
        .with_confidence(0.9);

        let context = create_context();
        let mut streaming = wrapper
            .verify_draft_streaming(&draft, &context)
            .await
            .unwrap();

        let mut chunk_count = 0;
        while let Some(chunk) = streaming.next_chunk().await {
            chunk_count += 1;
            assert!(chunk.chunk_id < 100); // Sanity check

            if chunk.is_final {
                assert!(chunk.partial_decision.is_some());
                break;
            }
        }

        assert!(chunk_count > 0);
    }

    #[tokio::test]
    async fn test_streaming_from_result() {
        let result = SpeculationResult::new(SpeculationDecision::Accept, 0.95)
            .with_explanation("Test explanation");

        let streaming = StreamingVerification::from_result(result.clone());
        assert!(streaming.has_final_result());

        let collected = streaming.collect().await;
        assert!(matches!(collected.decision, SpeculationDecision::Accept));
        assert_eq!(collected.confidence, 0.95);
    }

    #[tokio::test]
    async fn test_wrapper_delegates_verify() {
        let speculator = RuleBasedSpeculator::default();
        let wrapper = StreamingSpeculatorWrapper::new(speculator);

        let draft = Draft::new(
            "Paris is the capital of France.",
            "What is the capital of France?",
        )
        .with_confidence(0.8);

        let context = create_context();
        let result = wrapper.verify_draft(&draft, &context).await.unwrap();

        assert!(result.confidence > 0.0);
    }

    #[tokio::test]
    async fn test_wrapper_delegates_revise() {
        let speculator = RuleBasedSpeculator::default();
        let wrapper = StreamingSpeculatorWrapper::new(speculator);

        let draft = Draft::new("Paris", "What is the capital of France?");
        let context = create_context();

        let speculation = wrapper.verify_draft(&draft, &context).await.unwrap();
        let revised = wrapper
            .revise_draft(&draft, &context, &speculation)
            .await
            .unwrap();

        assert!(revised.content.len() > draft.content.len());
    }

    #[tokio::test]
    async fn test_chunk_size_minimum() {
        let speculator = RuleBasedSpeculator::default();
        let wrapper = StreamingSpeculatorWrapper::new(speculator).with_chunk_size(0);

        // Should be clamped to at least 1
        assert_eq!(wrapper.chunk_size(), 1);
    }

    #[tokio::test]
    async fn test_streaming_collected_chunks() {
        let speculator = RuleBasedSpeculator::default();
        let wrapper = StreamingSpeculatorWrapper::new(speculator).with_chunk_size(10);

        let draft = Draft::new("Paris is the capital of France.", "What is the capital?")
            .with_confidence(0.9);

        let context = create_context();
        let mut streaming = wrapper
            .verify_draft_streaming(&draft, &context)
            .await
            .unwrap();

        // Consume some chunks
        let _ = streaming.next_chunk().await;
        let _ = streaming.next_chunk().await;

        assert!(streaming.collected_chunks().len() >= 2);
    }

    #[tokio::test]
    async fn test_verification_chunk_default() {
        let chunk = VerificationChunk::default();
        assert_eq!(chunk.chunk_id, 0);
        assert!(chunk.content.is_empty());
        assert!(!chunk.is_final);
        assert!(chunk.partial_decision.is_none());
        assert_eq!(chunk.partial_confidence, 0.0);
    }
}