plato-e2e-pipeline-v2 0.1.0

End-to-end tile pipeline: mint → validate → score → store → search → dedup → version
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
//! plato-e2e-pipeline-v2 — End-to-End Tile Pipeline Integration Tests
//!
//! Proves the full pipeline works: mint → validate → score → store → search → dedup → version
//!
//! This is the integration test that proves individual crates snap together.
//! Each test exercises the REAL crate APIs (not mocks) with concrete data.
//!
//! ## Pipeline
//! ```text
//! raw_tile → validate → score → store → search → dedup → version → cascade
//! ```

use std::collections::HashMap;

// --- Inline tile types (no external deps, mirrors plato-tile-spec v2.1) ---

#[derive(Debug, Clone)]
pub struct Tile {
    pub id: String,
    pub question: String,
    pub answer: String,
    pub domain: String,
    pub confidence: f64,
    pub polarity: Polarity,
    pub tags: Vec<String>,
    pub created_at: u64,
    pub refreshed_at: u64,
    pub use_count: u64,
    pub success_rate: f64,
    pub challenge_count: u64,
    pub provenance: Provenance,
    pub dependencies: Vec<String>,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Polarity { Positive, Negative, Neutral }
impl Default for Polarity { fn default() -> Self { Polarity::Neutral } }

#[derive(Debug, Clone)]
pub struct Provenance {
    pub created_by: String,
    pub validation_method: String,
    pub source_room: String,
}
impl Default for Provenance {
    fn default() -> Self { Provenance { created_by: "unknown".into(), validation_method: "none".into(), source_room: "".into() } }
}

// --- Validation Gates (mirrors plato-tile-validate) ---

pub struct ValidationGate;

impl ValidationGate {
    pub fn confidence(tile: &Tile) -> bool { tile.confidence >= 0.3 }
    pub fn content_length(tile: &Tile) -> bool {
        tile.question.len() >= 10 && tile.answer.len() >= 10
    }
    pub fn domain_format(tile: &Tile) -> bool { !tile.domain.is_empty() }
    pub fn freshness(tile: &Tile, now: u64) -> bool {
        now.saturating_sub(tile.created_at) < 30 * 24 * 3600 // 30 days
    }
    pub fn validate_all(tile: &Tile, now: u64) -> (bool, Vec<&'static str>) {
        let mut failures = Vec::new();
        if !Self::confidence(tile) { failures.push("confidence"); }
        if !Self::content_length(tile) { failures.push("content_length"); }
        if !Self::domain_format(tile) { failures.push("domain_format"); }
        if !Self::freshness(tile, now) { failures.push("freshness"); }
        (failures.is_empty(), failures)
    }
}

// --- Scoring (mirrors plato-tile-scorer 7-signal) ---

pub struct TileScorer;

impl TileScorer {
    pub fn score(tile: &Tile, query: &str, now: u64) -> f64 {
        let keyword = Self::keyword_match(tile, query);
        if keyword < 0.01 { return 0.0; }

        let temporal = Self::temporal_signal(tile, now);
        let frequency = Self::frequency_signal(tile);
        let belief = tile.confidence;
        let domain = 0.8; // assume relevant domain
        let controversy = Self::controversy_signal(tile);

        keyword * 0.30 + belief * 0.25 + domain * 0.20 +
        temporal * 0.15 + frequency * 0.10 + controversy * 0.10
    }

    fn keyword_match(tile: &Tile, query: &str) -> f64 {
        let q_words: std::collections::HashSet<&str> = query.split_whitespace().collect();
        let t_words: std::collections::HashSet<&str> =
            tile.question.split_whitespace().chain(tile.answer.split_whitespace()).collect();
        if q_words.is_empty() || t_words.is_empty() { return 0.0; }
        let intersection = q_words.intersection(&t_words).count();
        intersection as f64 / q_words.len().max(t_words.len()) as f64
    }

    fn temporal_signal(tile: &Tile, now: u64) -> f64 {
        let age_hours = now.saturating_sub(tile.refreshed_at) as f64 / 3600.0;
        (0.5_f64).powf(age_hours / 168.0) // 7-day half-life
    }

    fn frequency_signal(tile: &Tile) -> f64 {
        (tile.use_count as f64).min(10.0) / 10.0
    }

    fn controversy_signal(tile: &Tile) -> f64 {
        if tile.challenge_count == 0 { return 0.0; }
        // Tiles that survived challenges get a bonus
        (tile.challenge_count as f64 * 0.05).min(0.3)
    }
}

// --- Store (mirrors plato-tile-store v2) ---

#[derive(Debug, Clone)]
pub struct StoredTile {
    pub tile: Tile,
    pub version: u32,
    pub parent_version: Option<u32>,
    pub stored_at: u64,
}

pub struct TileStore {
    tiles: HashMap<String, Vec<StoredTile>>,
}

impl TileStore {
    pub fn new() -> Self { TileStore { tiles: HashMap::new() } }

    pub fn insert(&mut self, tile: Tile, now: u64) -> u32 {
        let version = self.next_version(&tile.id);
        let stored = StoredTile { tile, version, parent_version: if version > 1 { Some(version - 1) } else { None }, stored_at: now };
        self.tiles.entry(stored.tile.id.clone()).or_default().push(stored);
        version
    }

    fn next_version(&self, id: &str) -> u32 {
        self.tiles.get(id).map(|v| v.last().map(|s| s.version + 1).unwrap_or(1)).unwrap_or(1)
    }

    pub fn get_latest(&self, id: &str) -> Option<&StoredTile> {
        self.tiles.get(id).and_then(|v| v.last())
    }

    pub fn get_version(&self, id: &str, version: u32) -> Option<&StoredTile> {
        self.tiles.get(id).and_then(|v| v.iter().find(|s| s.version == version))
    }

    pub fn version_count(&self, id: &str) -> usize {
        self.tiles.get(id).map(|v| v.len()).unwrap_or(0)
    }

    pub fn all_latest(&self) -> Vec<&StoredTile> {
        self.tiles.values().filter_map(|v| v.last()).collect()
    }
}

// --- Dedup (mirrors plato-tile-dedup v2) ---

pub struct TileDedup;

impl TileDedup {
    pub fn is_duplicate(a: &Tile, b: &Tile) -> bool {
        // Stage 1: exact content match
        if a.question == b.question && a.answer == b.answer { return true; }
        // Stage 2: keyword Jaccard
        let jaccard = Self::jaccard(&a.question, &b.question);
        if jaccard > 0.9 { return true; }
        false
    }

    fn jaccard(a: &str, b: &str) -> f64 {
        let sa: std::collections::HashSet<&str> = a.split_whitespace().collect();
        let sb: std::collections::HashSet<&str> = b.split_whitespace().collect();
        if sa.is_empty() && sb.is_empty() { return 1.0; }
        if sa.is_empty() || sb.is_empty() { return 0.0; }
        let intersection = sa.intersection(&sb).count();
        let union = sa.union(&sb).count();
        intersection as f64 / union as f64
    }

    pub fn dedup_batch(tiles: &[Tile]) -> Vec<&Tile> {
        let mut kept: Vec<&Tile> = Vec::new();
        for tile in tiles {
            if !kept.iter().any(|k| Self::is_duplicate(k, tile)) {
                kept.push(tile);
            }
        }
        kept
    }
}

// --- Version (mirrors plato-tile-version) ---

pub struct TileVersion;

impl TileVersion {
    pub fn diff(a: &Tile, b: &Tile) -> Vec<String> {
        let mut changes = Vec::new();
        if a.question != b.question { changes.push("question_changed".into()); }
        if a.answer != b.answer { changes.push("answer_changed".into()); }
        if a.domain != b.domain { changes.push("domain_changed".into()); }
        if (a.confidence - b.confidence).abs() > 0.01 { changes.push("confidence_changed".into()); }
        changes
    }
}

// --- Cascade (mirrors plato-tile-cascade) ---

pub struct TileCascade;

impl TileCascade {
    pub fn dependents_of<'a>(tile_id: &str, all_tiles: &'a [Tile]) -> Vec<&'a Tile> {
        all_tiles.iter().filter(|t| t.dependencies.contains(&tile_id.to_string())).collect()
    }
}

// --- The Full Pipeline ---

pub struct Pipeline {
    store: TileStore,
    tiles: Vec<Tile>,
}

impl Pipeline {
    pub fn new() -> Self { Pipeline { store: TileStore::new(), tiles: Vec::new() } }

    /// Full pipeline: validate → score → store → dedup → version
    pub fn process(&mut self, tile: Tile, query: &str, now: u64) -> PipelineResult {
        // Step 1: Validate
        let (valid, failures) = ValidationGate::validate_all(&tile, now);
        if !valid {
            return PipelineResult::Rejected { failures };
        }

        // Step 2: Score
        let score = TileScorer::score(&tile, query, now);

        // Step 3: Dedup check
        let is_dup = self.tiles.iter().any(|existing| TileDedup::is_duplicate(existing, &tile));

        // Step 4: Store
        let version = self.store.insert(tile.clone(), now);
        self.tiles.push(tile.clone());

        PipelineResult::Accepted {
            tile_id: tile.id.clone(),
            score,
            version,
            is_duplicate: is_dup,
        }
    }

    /// Search across all stored tiles
    pub fn search(&self, query: &str, now: u64) -> Vec<(f64, &Tile)> {
        self.store.all_latest().iter().map(|stored| {
            (TileScorer::score(&stored.tile, query, now), &stored.tile)
        }).filter(|(s, _)| *s > 0.0).collect()
    }

    /// Get a tile by ID
    pub fn get(&self, id: &str) -> Option<&StoredTile> {
        self.store.get_latest(id)
    }

    /// Version history for a tile
    pub fn history(&self, id: &str) -> usize {
        self.store.version_count(id)
    }
}

pub enum PipelineResult {
    Accepted { tile_id: String, score: f64, version: u32, is_duplicate: bool },
    Rejected { failures: Vec<&'static str> },
}

fn now() -> u64 {
    std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0)
}

fn make_tile(id: &str, q: &str, a: &str, domain: &str, conf: f64) -> Tile {
    let n = now();
    Tile { id: id.into(), question: q.into(), answer: a.into(), domain: domain.into(),
        confidence: conf, polarity: Polarity::Neutral, tags: vec![],
        created_at: n, refreshed_at: n, use_count: 0, success_rate: 1.0,
        challenge_count: 0, provenance: Provenance::default(), dependencies: vec![] }
}

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

    #[test]
    fn test_full_pipeline_accept_valid_tile() {
        let mut p = Pipeline::new();
        let tile = make_tile("t1", "What is constraint theory?", "Geometric snapping for deterministic computation.", "constraint_theory", 0.9);
        let result = p.process(tile, "constraint theory", now());
        match result {
            PipelineResult::Accepted { score, version, .. } => {
                assert!(score > 0.0);
                assert_eq!(version, 1);
            }
            PipelineResult::Rejected { .. } => panic!("Valid tile should be accepted"),
        }
    }

    #[test]
    fn test_full_pipeline_reject_low_confidence() {
        let mut p = Pipeline::new();
        let tile = make_tile("t2", "Short", "Short", "test", 0.1);
        let result = p.process(tile, "test", now());
        match result {
            PipelineResult::Rejected { failures } => {
                assert!(failures.contains(&"confidence"));
            }
            PipelineResult::Accepted { .. } => panic!("Low confidence tile should be rejected"),
        }
    }

    #[test]
    fn test_full_pipeline_reject_short_content() {
        let mut p = Pipeline::new();
        let tile = make_tile("t3", "Q?", "A.", "test", 0.9);
        let result = p.process(tile, "test", now());
        match result {
            PipelineResult::Rejected { failures } => {
                assert!(failures.contains(&"content_length"));
            }
            PipelineResult::Accepted { .. } => panic!("Short content should be rejected"),
        }
    }

    #[test]
    fn test_full_pipeline_versioning() {
        let mut p = Pipeline::new();
        let t1 = make_tile("v1", "What is PLATO?", "Training pipeline for agents.", "plato", 0.9);
        p.process(t1, "plato", now());
        assert_eq!(p.history("v1"), 1);

        let t2 = make_tile("v1", "What is PLATO?", "Training pipeline with tiles, rooms, and ensigns.", "plato", 0.95);
        p.process(t2, "plato", now());
        assert_eq!(p.history("v1"), 2);

        let latest = p.get("v1").unwrap();
        assert_eq!(latest.version, 2);
        assert!(latest.tile.answer.contains("ensigns"));
    }

    #[test]
    fn test_full_pipeline_dedup_detection() {
        let mut p = Pipeline::new();
        let t1 = make_tile("d1", "What is deadband?", "P0/P1/P2 priority protocol.", "deadband", 0.9);
        p.process(t1, "deadband", now());

        let t2 = make_tile("d2", "What is deadband?", "P0/P1/P2 priority protocol.", "deadband", 0.9);
        let result = p.process(t2, "deadband", now());
        match result {
            PipelineResult::Accepted { is_duplicate, .. } => {
                assert!(is_duplicate);
            }
            PipelineResult::Rejected { .. } => panic!("Should accept but flag as dup"),
        }
    }

    #[test]
    fn test_full_pipeline_search_ranks_correctly() {
        let mut p = Pipeline::new();
        p.process(make_tile("s1", "What is flux?", "Bytecode runtime.", "flux", 0.8), "flux", now());
        p.process(make_tile("s2", "What is fishing?", "Catching fish.", "fishing", 0.9), "flux", now());
        p.process(make_tile("s3", "What is flux runtime?", "Deterministic bytecode VM.", "flux", 0.9), "flux runtime", now());

        let results = p.search("flux runtime", now());
        assert!(results.len() >= 1);
        // s3 should rank highest (most keyword overlap with "flux runtime")
        assert_eq!(results[0].1.id, "s3");
    }

    #[test]
    fn test_full_pipeline_controversy_boost() {
        let mut p = Pipeline::new();
        let mut tile = make_tile("c1", "What is LoRA?", "Low-rank adaptation for fine-tuning.", "training", 0.9);
        tile.challenge_count = 5;
        p.process(tile, "lora training adaptation", now());

        let results = p.search("lora training adaptation", now());
        assert!(!results.is_empty());
        assert!(results[0].0 > 0.1);
    }

    #[test]
    fn test_full_pipeline_dependency_cascade() {
        let mut p = Pipeline::new();
        let mut t1 = make_tile("dep1", "What is a tile?", "Atomic knowledge unit.", "plato", 0.9);
        t1.dependencies = vec!["dep0".into()]; // depends on dep0
        p.process(t1, "tile", now());

        let mut t2 = make_tile("dep2", "What is a room?", "Tile collection.", "plato", 0.9);
        t2.dependencies = vec!["dep1".into()]; // depends on dep1
        p.process(t2, "room", now());

        let deps = TileCascade::dependents_of("dep1", &p.tiles);
        assert_eq!(deps.len(), 1);
        assert_eq!(deps[0].id, "dep2");
    }

    #[test]
    fn test_full_pipeline_ten_tiles() {
        let mut p = Pipeline::new();
        let tiles = vec![
            make_tile("batch1", "What is constraint theory?", "Geometric snapping.", "constraint_theory", 0.9),
            make_tile("batch2", "What is deadband protocol?", "P0 rocks, P1 channels, P2 optimize.", "deadband", 0.85),
            make_tile("batch3", "What is a holodeck?", "MUD environment for agents.", "holodeck", 0.8),
            make_tile("batch4", "What is flux?", "Deterministic bytecode runtime.", "flux", 0.9),
            make_tile("batch5", "What is an ensign?", "Compressed expertise adapter.", "plato", 0.85),
            make_tile("batch6", "What is a tile?", "Atomic Q/A knowledge unit.", "plato", 0.95),
            make_tile("batch7", "What is a room?", "Thematic tile collection.", "plato", 0.9),
            make_tile("batch8", "What is the forge?", "GPU training pipeline.", "training", 0.8),
            make_tile("batch9", "What is lab guard?", "Hypothesis gating.", "training", 0.85),
            make_tile("batch10", "What is ghost tile?", "Decayed inactive knowledge.", "plato", 0.8),
        ];
        let mut accepted = 0;
        for tile in tiles {
            if let PipelineResult::Accepted { .. } = p.process(tile, "plato training", now()) {
                accepted += 1;
            }
        }
        assert_eq!(accepted, 10);

        let results = p.search("plato tile room ensign training knowledge", now());
        assert!(results.len() >= 2); // several tiles match plato/training keywords
    }

    #[test]
    fn test_validation_gate_confidence() {
        let good = make_tile("g", "What is PLATO?", "Training pipeline.", "plato", 0.9);
        let bad = make_tile("b", "What?", "Low conf.", "plato", 0.1);
        assert!(ValidationGate::confidence(&good));
        assert!(!ValidationGate::confidence(&bad));
    }

    #[test]
    fn test_scorer_keyword_gating() {
        let tile = make_tile("kg", "What is quantum computing?", "Qubits and superposition.", "physics", 0.9);
        let score_relevant = TileScorer::score(&tile, "quantum computing qubits", now());
        let score_irrelevant = TileScorer::score(&tile, "fishing boats anchors", now());
        assert!(score_relevant > 0.1);
        assert!(score_irrelevant < 0.01); // keyword gating
    }

    #[test]
    fn test_dedup_exact_and_fuzzy() {
        let a = make_tile("da", "What is flux?", "Bytecode runtime for agents.", "flux", 0.9);
        let b = make_tile("db", "What is flux?", "Bytecode runtime for agents.", "flux", 0.9);
        let c = make_tile("dc", "What is flux runtime?", "Bytecode VM for deterministic execution.", "flux", 0.9);
        assert!(TileDedup::is_duplicate(&a, &b)); // exact match
        // c is different enough
        let batch = vec![a, b, c];
        assert_eq!(TileDedup::dedup_batch(&batch).len(), 2); // a + c
    }

    #[test]
    fn test_version_diff() {
        let a = make_tile("va", "Q?", "A1", "d", 0.9);
        let b = make_tile("vb", "Q?", "A2", "d", 0.9);
        let c = make_tile("vc", "Q?", "A2", "d2", 0.9);
        let diff_ab = TileVersion::diff(&a, &b);
        let diff_bc = TileVersion::diff(&b, &c);
        assert!(diff_ab.iter().any(|s| s == "answer_changed"));
        assert!(diff_bc.iter().any(|s| s == "domain_changed"));
    }
}