terraphim_orchestrator 1.16.34

AI Dark Factory orchestrator wiring spawner, router, supervisor into a reconciliation loop
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
//! Shared Learning Store for ADF agents.
//!
//! Provides a persistence-backed store for learnings extracted from agent runs.
//! Learnings are shared across agents to improve reliability and efficiency.
//!
//! Uses `terraphim_persistence` for storage, following the same pattern as
//! `metrics_persistence` — a trait with in-memory and device-storage implementations.
//!
//! # Architecture
//!
//! ```text
//! Flow State Parser (Python)
//!     ↓ JSONL learnings
//! SharedLearningStore
//!     ↓ LearningPersistence trait
//!     ├── InMemoryLearningPersistence (tests)
//!     └── DeviceStorageLearningPersistence (production, uses terraphim_persistence)
//!//!     Orchestrator (pre-spawn context injection)
//!          ↓ /tmp/adf-context-{agent}.md
//!     Agent Task
//! ```
//!
//! # Trust Levels
//!
//! - **L0**: Unverified — just extracted, not yet shown to agents
//! - **L1**: Verified once — passed verify_pattern or one effective application
//! - **L2**: Verified multiple times — candidate for Gitea wiki sync
//! - **L3**: Golden — manually verified, always included in context

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use thiserror::Error;
use tracing::{debug, info, warn};
use uuid::Uuid;

// ---------------------------------------------------------------------------
// Errors
// ---------------------------------------------------------------------------

/// Errors for the shared learning store.
#[derive(Debug, Error)]
pub enum LearningError {
    #[error("storage error: {0}")]
    Storage(String),

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

    #[error("invalid trust level: {0}")]
    InvalidTrustLevel(String),

    #[error("learning not found: {0}")]
    NotFound(String),

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

// ---------------------------------------------------------------------------
// Domain types
// ---------------------------------------------------------------------------

/// Trust levels for learnings.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum TrustLevel {
    /// Unverified, just extracted
    L0 = 0,
    /// Verified once
    L1 = 1,
    /// Verified multiple times, candidate for wiki sync
    L2 = 2,
    /// Golden, manually verified
    L3 = 3,
}

impl std::fmt::Display for TrustLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TrustLevel::L0 => write!(f, "L0"),
            TrustLevel::L1 => write!(f, "L1"),
            TrustLevel::L2 => write!(f, "L2"),
            TrustLevel::L3 => write!(f, "L3"),
        }
    }
}

impl std::str::FromStr for TrustLevel {
    type Err = LearningError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_uppercase().as_str() {
            "L0" => Ok(TrustLevel::L0),
            "L1" => Ok(TrustLevel::L1),
            "L2" => Ok(TrustLevel::L2),
            "L3" => Ok(TrustLevel::L3),
            _ => Err(LearningError::InvalidTrustLevel(s.to_string())),
        }
    }
}

/// Categories of learnings.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LearningCategory {
    ModelError,
    StepFailure,
    ToolHealth,
    Pattern,
    Tip,
    TimingAnomaly,
    RecurringPattern,
}

impl std::fmt::Display for LearningCategory {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            LearningCategory::ModelError => write!(f, "model_error"),
            LearningCategory::StepFailure => write!(f, "step_failure"),
            LearningCategory::ToolHealth => write!(f, "tool_health"),
            LearningCategory::Pattern => write!(f, "pattern"),
            LearningCategory::Tip => write!(f, "tip"),
            LearningCategory::TimingAnomaly => write!(f, "timing_anomaly"),
            LearningCategory::RecurringPattern => write!(f, "recurring_pattern"),
        }
    }
}

impl std::str::FromStr for LearningCategory {
    type Err = LearningError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "model_error" => Ok(LearningCategory::ModelError),
            "step_failure" => Ok(LearningCategory::StepFailure),
            "tool_health" => Ok(LearningCategory::ToolHealth),
            "pattern" => Ok(LearningCategory::Pattern),
            "tip" => Ok(LearningCategory::Tip),
            "timing_anomaly" => Ok(LearningCategory::TimingAnomaly),
            "recurring_pattern" => Ok(LearningCategory::RecurringPattern),
            other => Err(LearningError::InvalidTrustLevel(format!(
                "unknown category: {other}"
            ))),
        }
    }
}

/// A shared learning extracted from agent runs.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Learning {
    pub id: String,
    pub source_agent: String,
    pub category: LearningCategory,
    pub summary: String,
    pub details: Option<String>,
    /// Agents this learning applies to. Empty means all agents.
    pub applicable_agents: Vec<String>,
    pub trust_level: TrustLevel,
    /// Shell command that must exit 0 for this learning to remain valid.
    pub verify_pattern: Option<String>,
    pub applied_count: u32,
    pub effective_count: u32,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub archived_at: Option<DateTime<Utc>>,
}

/// Input for creating a new learning (no id/timestamps).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NewLearning {
    pub source_agent: String,
    pub category: LearningCategory,
    pub summary: String,
    #[serde(default)]
    pub details: Option<String>,
    #[serde(default)]
    pub applicable_agents: Vec<String>,
    #[serde(default)]
    pub verify_pattern: Option<String>,
}

// ---------------------------------------------------------------------------
// Persistence trait
// ---------------------------------------------------------------------------

/// Trait for learning persistence, mirroring `MetricsPersistence` pattern.
#[async_trait]
pub trait LearningPersistence: Send + Sync {
    /// Insert a new learning. Returns the generated UUID.
    async fn insert(&self, learning: NewLearning) -> Result<String, LearningError>;

    /// Get a learning by id.
    async fn get(&self, id: &str) -> Result<Option<Learning>, LearningError>;

    /// Query learnings relevant to an agent.
    ///
    /// Returns non-archived learnings with trust_level >= `min_trust`
    /// that either apply to all agents or specifically to `agent_name`.
    async fn query_relevant(
        &self,
        agent_name: &str,
        min_trust: TrustLevel,
    ) -> Result<Vec<Learning>, LearningError>;

    /// Increment applied_count.
    async fn record_applied(&self, id: &str) -> Result<(), LearningError>;

    /// Increment effective_count and auto-promote trust level.
    async fn record_effective(&self, id: &str) -> Result<(), LearningError>;

    /// Archive stale learnings older than `max_age_days` that are still L0.
    async fn archive_stale(&self, max_age_days: u32) -> Result<usize, LearningError>;

    /// List all non-archived learning ids.
    async fn list_ids(&self) -> Result<Vec<String>, LearningError>;

    /// Delete a learning.
    async fn delete(&self, id: &str) -> Result<(), LearningError>;
}

// ---------------------------------------------------------------------------
// In-memory implementation (for tests)
// ---------------------------------------------------------------------------

/// In-memory learning persistence for testing and development.
pub struct InMemoryLearningPersistence {
    data: std::sync::RwLock<HashMap<String, Learning>>,
}

impl InMemoryLearningPersistence {
    pub fn new() -> Self {
        Self {
            data: std::sync::RwLock::new(HashMap::new()),
        }
    }
}

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

#[async_trait]
impl LearningPersistence for InMemoryLearningPersistence {
    async fn insert(&self, input: NewLearning) -> Result<String, LearningError> {
        let id = Uuid::new_v4().to_string();
        let now = Utc::now();
        let learning = Learning {
            id: id.clone(),
            source_agent: input.source_agent,
            category: input.category,
            summary: input.summary,
            details: input.details,
            applicable_agents: input.applicable_agents,
            trust_level: TrustLevel::L0,
            verify_pattern: input.verify_pattern,
            applied_count: 0,
            effective_count: 0,
            created_at: now,
            updated_at: now,
            archived_at: None,
        };
        let mut data = self
            .data
            .write()
            .map_err(|e| LearningError::Storage(format!("lock poisoned: {e}")))?;
        data.insert(id.clone(), learning);
        Ok(id)
    }

    async fn get(&self, id: &str) -> Result<Option<Learning>, LearningError> {
        let data = self
            .data
            .read()
            .map_err(|e| LearningError::Storage(format!("lock poisoned: {e}")))?;
        Ok(data.get(id).cloned())
    }

    async fn query_relevant(
        &self,
        agent_name: &str,
        min_trust: TrustLevel,
    ) -> Result<Vec<Learning>, LearningError> {
        let data = self
            .data
            .read()
            .map_err(|e| LearningError::Storage(format!("lock poisoned: {e}")))?;

        let mut results: Vec<Learning> = data
            .values()
            .filter(|l| {
                l.archived_at.is_none()
                    && l.trust_level >= min_trust
                    && (l.applicable_agents.is_empty()
                        || l.applicable_agents.iter().any(|a| a == agent_name))
            })
            .cloned()
            .collect();

        // Sort: highest trust first, then by effective_count desc
        results.sort_by(|a, b| {
            b.trust_level
                .cmp(&a.trust_level)
                .then(b.effective_count.cmp(&a.effective_count))
        });

        results.truncate(20);
        Ok(results)
    }

    async fn record_applied(&self, id: &str) -> Result<(), LearningError> {
        let mut data = self
            .data
            .write()
            .map_err(|e| LearningError::Storage(format!("lock poisoned: {e}")))?;
        if let Some(l) = data.get_mut(id) {
            l.applied_count += 1;
            l.updated_at = Utc::now();
            Ok(())
        } else {
            Err(LearningError::NotFound(id.to_string()))
        }
    }

    async fn record_effective(&self, id: &str) -> Result<(), LearningError> {
        let mut data = self
            .data
            .write()
            .map_err(|e| LearningError::Storage(format!("lock poisoned: {e}")))?;
        if let Some(l) = data.get_mut(id) {
            l.effective_count += 1;
            l.updated_at = Utc::now();
            // Auto-promote
            l.trust_level = match (l.trust_level, l.effective_count) {
                (TrustLevel::L0, n) if n >= 1 => TrustLevel::L1,
                (TrustLevel::L1, n) if n >= 3 => TrustLevel::L2,
                (TrustLevel::L2, n) if n >= 5 => TrustLevel::L3,
                (current, _) => current,
            };
            Ok(())
        } else {
            Err(LearningError::NotFound(id.to_string()))
        }
    }

    async fn archive_stale(&self, max_age_days: u32) -> Result<usize, LearningError> {
        let cutoff = Utc::now() - chrono::Duration::days(max_age_days as i64);
        let now = Utc::now();
        let mut data = self
            .data
            .write()
            .map_err(|e| LearningError::Storage(format!("lock poisoned: {e}")))?;

        let mut count = 0;
        for l in data.values_mut() {
            if l.archived_at.is_none() && l.trust_level == TrustLevel::L0 && l.updated_at < cutoff {
                l.archived_at = Some(now);
                count += 1;
            }
        }
        Ok(count)
    }

    async fn list_ids(&self) -> Result<Vec<String>, LearningError> {
        let data = self
            .data
            .read()
            .map_err(|e| LearningError::Storage(format!("lock poisoned: {e}")))?;
        Ok(data
            .values()
            .filter(|l| l.archived_at.is_none())
            .map(|l| l.id.clone())
            .collect())
    }

    async fn delete(&self, id: &str) -> Result<(), LearningError> {
        let mut data = self
            .data
            .write()
            .map_err(|e| LearningError::Storage(format!("lock poisoned: {e}")))?;
        data.remove(id);
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// DeviceStorage-backed implementation (production)
// ---------------------------------------------------------------------------

/// Production learning persistence using `terraphim_persistence::DeviceStorage`.
///
/// Each learning is stored as a JSON document keyed by `adf/learnings/{uuid}`.
/// An index document at `adf/learnings/_index` maps ids for listing/querying.
pub struct DeviceStorageLearningPersistence {
    key_prefix: String,
    /// In-memory cache loaded from persistence on startup.
    cache: tokio::sync::RwLock<HashMap<String, Learning>>,
}

impl DeviceStorageLearningPersistence {
    /// Create and initialise, loading existing learnings from storage.
    pub async fn new(key_prefix: impl Into<String>) -> Result<Self, LearningError> {
        let key_prefix = key_prefix.into();
        let store = Self {
            key_prefix,
            cache: tokio::sync::RwLock::new(HashMap::new()),
        };
        store.load_all_from_storage().await?;
        Ok(store)
    }

    fn learning_key(&self, id: &str) -> String {
        format!("{}/{}", self.key_prefix, id)
    }

    fn index_key(&self) -> String {
        format!("{}/_index", self.key_prefix)
    }

    /// Persist a single learning to the fastest operator.
    async fn persist_learning(&self, learning: &Learning) -> Result<(), LearningError> {
        let storage = terraphim_persistence::DeviceStorage::instance()
            .await
            .map_err(|e| LearningError::Storage(format!("DeviceStorage init: {e}")))?;

        let key = self.learning_key(&learning.id);
        let json = serde_json::to_string(learning)?;
        storage
            .fastest_op
            .write(&key, json)
            .await
            .map_err(|e| LearningError::Storage(format!("write {key}: {e}")))?;
        Ok(())
    }

    /// Persist the index (list of ids) so we can enumerate on restart.
    async fn persist_index(&self) -> Result<(), LearningError> {
        let cache = self.cache.read().await;
        let ids: Vec<&str> = cache.keys().map(String::as_str).collect();
        let json = serde_json::to_string(&ids)?;

        let storage = terraphim_persistence::DeviceStorage::instance()
            .await
            .map_err(|e| LearningError::Storage(format!("DeviceStorage init: {e}")))?;

        storage
            .fastest_op
            .write(&self.index_key(), json)
            .await
            .map_err(|e| LearningError::Storage(format!("write index: {e}")))?;
        Ok(())
    }

    /// Load all learnings from storage into the in-memory cache.
    async fn load_all_from_storage(&self) -> Result<(), LearningError> {
        let storage = terraphim_persistence::DeviceStorage::instance()
            .await
            .map_err(|e| LearningError::Storage(format!("DeviceStorage init: {e}")))?;

        // Read index
        let index_key = self.index_key();
        let ids: Vec<String> = match storage.fastest_op.read(&index_key).await {
            Ok(bs) => serde_json::from_slice(&bs.to_vec()).unwrap_or_default(),
            Err(_) => {
                debug!("No learning index found at {index_key}, starting fresh");
                Vec::new()
            }
        };

        let mut cache = self.cache.write().await;
        for id in &ids {
            let key = self.learning_key(id);
            match storage.fastest_op.read(&key).await {
                Ok(bs) => match serde_json::from_slice::<Learning>(&bs.to_vec()) {
                    Ok(learning) => {
                        cache.insert(id.clone(), learning);
                    }
                    Err(e) => warn!("Failed to deserialize learning {id}: {e}"),
                },
                Err(e) => warn!("Failed to read learning {id}: {e}"),
            }
        }

        info!(
            "Loaded {} learnings from persistence (prefix={})",
            cache.len(),
            self.key_prefix
        );
        Ok(())
    }
}

#[async_trait]
impl LearningPersistence for DeviceStorageLearningPersistence {
    async fn insert(&self, input: NewLearning) -> Result<String, LearningError> {
        let id = Uuid::new_v4().to_string();
        let now = Utc::now();
        let learning = Learning {
            id: id.clone(),
            source_agent: input.source_agent,
            category: input.category,
            summary: input.summary,
            details: input.details,
            applicable_agents: input.applicable_agents,
            trust_level: TrustLevel::L0,
            verify_pattern: input.verify_pattern,
            applied_count: 0,
            effective_count: 0,
            created_at: now,
            updated_at: now,
            archived_at: None,
        };

        self.persist_learning(&learning).await?;

        let mut cache = self.cache.write().await;
        cache.insert(id.clone(), learning);
        drop(cache);

        self.persist_index().await?;
        info!("Inserted learning {id}");
        Ok(id)
    }

    async fn get(&self, id: &str) -> Result<Option<Learning>, LearningError> {
        let cache = self.cache.read().await;
        Ok(cache.get(id).cloned())
    }

    async fn query_relevant(
        &self,
        agent_name: &str,
        min_trust: TrustLevel,
    ) -> Result<Vec<Learning>, LearningError> {
        let cache = self.cache.read().await;

        let mut results: Vec<Learning> = cache
            .values()
            .filter(|l| {
                l.archived_at.is_none()
                    && l.trust_level >= min_trust
                    && (l.applicable_agents.is_empty()
                        || l.applicable_agents.iter().any(|a| a == agent_name))
            })
            .cloned()
            .collect();

        results.sort_by(|a, b| {
            b.trust_level
                .cmp(&a.trust_level)
                .then(b.effective_count.cmp(&a.effective_count))
        });
        results.truncate(20);
        Ok(results)
    }

    async fn record_applied(&self, id: &str) -> Result<(), LearningError> {
        let mut cache = self.cache.write().await;
        let learning = cache
            .get_mut(id)
            .ok_or_else(|| LearningError::NotFound(id.to_string()))?;
        learning.applied_count += 1;
        learning.updated_at = Utc::now();
        let snapshot = learning.clone();
        drop(cache);

        self.persist_learning(&snapshot).await
    }

    async fn record_effective(&self, id: &str) -> Result<(), LearningError> {
        let mut cache = self.cache.write().await;
        let learning = cache
            .get_mut(id)
            .ok_or_else(|| LearningError::NotFound(id.to_string()))?;
        learning.effective_count += 1;
        learning.updated_at = Utc::now();
        // Auto-promote
        learning.trust_level = match (learning.trust_level, learning.effective_count) {
            (TrustLevel::L0, n) if n >= 1 => TrustLevel::L1,
            (TrustLevel::L1, n) if n >= 3 => TrustLevel::L2,
            (TrustLevel::L2, n) if n >= 5 => TrustLevel::L3,
            (current, _) => current,
        };
        let snapshot = learning.clone();
        drop(cache);

        self.persist_learning(&snapshot).await
    }

    async fn archive_stale(&self, max_age_days: u32) -> Result<usize, LearningError> {
        let cutoff = Utc::now() - chrono::Duration::days(max_age_days as i64);
        let now = Utc::now();
        let mut cache = self.cache.write().await;

        let mut to_persist = Vec::new();
        let mut count = 0;
        for l in cache.values_mut() {
            if l.archived_at.is_none() && l.trust_level == TrustLevel::L0 && l.updated_at < cutoff {
                l.archived_at = Some(now);
                to_persist.push(l.clone());
                count += 1;
            }
        }
        drop(cache);

        for l in &to_persist {
            self.persist_learning(l).await?;
        }
        if count > 0 {
            info!("Archived {count} stale L0 learnings");
        }
        Ok(count)
    }

    async fn list_ids(&self) -> Result<Vec<String>, LearningError> {
        let cache = self.cache.read().await;
        Ok(cache
            .values()
            .filter(|l| l.archived_at.is_none())
            .map(|l| l.id.clone())
            .collect())
    }

    async fn delete(&self, id: &str) -> Result<(), LearningError> {
        let mut cache = self.cache.write().await;
        cache.remove(id);
        drop(cache);

        // Delete from storage
        let storage = terraphim_persistence::DeviceStorage::instance()
            .await
            .map_err(|e| LearningError::Storage(format!("DeviceStorage init: {e}")))?;
        let key = self.learning_key(id);
        let _ = storage.fastest_op.delete(&key).await; // best-effort

        self.persist_index().await
    }
}

// ---------------------------------------------------------------------------
// SharedLearningStore — high-level API wrapping the trait
// ---------------------------------------------------------------------------

/// High-level API for the shared learning store.
///
/// Wraps a `LearningPersistence` impl and adds context-file generation
/// and JSONL import capabilities.
pub struct SharedLearningStore {
    persistence: Box<dyn LearningPersistence>,
    min_trust: TrustLevel,
}

impl SharedLearningStore {
    /// Create a store backed by the given persistence implementation.
    pub fn new(persistence: Box<dyn LearningPersistence>, min_trust: TrustLevel) -> Self {
        Self {
            persistence,
            min_trust,
        }
    }

    /// Create an in-memory store (for testing).
    pub fn in_memory() -> Self {
        Self {
            persistence: Box::new(InMemoryLearningPersistence::new()),
            min_trust: TrustLevel::L1,
        }
    }

    /// Delegate to the persistence layer.
    pub async fn insert(&self, learning: NewLearning) -> Result<String, LearningError> {
        self.persistence.insert(learning).await
    }

    pub async fn get(&self, id: &str) -> Result<Option<Learning>, LearningError> {
        self.persistence.get(id).await
    }

    pub async fn query_relevant(&self, agent_name: &str) -> Result<Vec<Learning>, LearningError> {
        self.persistence
            .query_relevant(agent_name, self.min_trust)
            .await
    }

    pub async fn record_applied(&self, id: &str) -> Result<(), LearningError> {
        self.persistence.record_applied(id).await
    }

    pub async fn record_effective(&self, id: &str) -> Result<(), LearningError> {
        self.persistence.record_effective(id).await
    }

    pub async fn archive_stale(&self, max_age_days: u32) -> Result<usize, LearningError> {
        self.persistence.archive_stale(max_age_days).await
    }

    /// Generate Markdown context for an agent to consume.
    pub async fn generate_context(&self, agent_name: &str) -> Result<String, LearningError> {
        let learnings = self.query_relevant(agent_name).await?;

        if learnings.is_empty() {
            return Ok("# Shared Learnings\n\nNo relevant learnings found.\n".to_string());
        }

        let mut md = String::new();
        md.push_str("# Shared Learnings (auto-generated, do not edit)\n\n");
        md.push_str(&format!("Generated for: {agent_name}\n\n"));

        // Partition by category type
        let mut errors = Vec::new();
        let mut tips = Vec::new();
        let mut patterns = Vec::new();

        for l in &learnings {
            match l.category {
                LearningCategory::ModelError
                | LearningCategory::StepFailure
                | LearningCategory::ToolHealth => errors.push(l),
                LearningCategory::Tip => tips.push(l),
                _ => patterns.push(l),
            }
        }

        if !errors.is_empty() {
            md.push_str("## ⚠️ Known Issues\n\n");
            for l in &errors {
                md.push_str(&format!(
                    "- [{}] {} (trust: {}, verified {}x)\n",
                    l.category, l.summary, l.trust_level, l.effective_count
                ));
                if let Some(ref details) = l.details {
                    if let Some(first_line) = details.lines().next() {
                        md.push_str(&format!("  > {first_line}\n"));
                    }
                }
            }
            md.push('\n');
        }

        if !tips.is_empty() {
            md.push_str("## 💡 Tips from Peer Agents\n\n");
            for l in &tips {
                md.push_str(&format!(
                    "- {} (from {}, trust: {})\n",
                    l.summary, l.source_agent, l.trust_level
                ));
            }
            md.push('\n');
        }

        if !patterns.is_empty() {
            md.push_str("## 🔄 Recurring Patterns\n\n");
            for l in &patterns {
                md.push_str(&format!("- {} (seen {}x)\n", l.summary, l.applied_count));
            }
            md.push('\n');
        }

        Ok(md)
    }

    /// Write context file for an agent to `/tmp/adf-context-{agent}.md`.
    pub async fn write_context_file(&self, agent_name: &str) -> Result<PathBuf, LearningError> {
        let content = self.generate_context(agent_name).await?;
        let path = PathBuf::from(format!("/tmp/adf-context-{agent_name}.md"));
        tokio::fs::write(&path, content)
            .await
            .map_err(LearningError::Io)?;
        info!("Wrote context file for {agent_name} at {path:?}");
        Ok(path)
    }

    /// Import learnings from JSONL (as produced by `flow-state-parser.py`).
    pub async fn import_jsonl(&self, jsonl: &str) -> Result<usize, LearningError> {
        let mut count = 0;
        for line in jsonl.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }
            match serde_json::from_str::<NewLearning>(line) {
                Ok(input) => match self.insert(input).await {
                    Ok(_) => count += 1,
                    Err(e) => warn!("Failed to insert learning: {e}"),
                },
                Err(e) => {
                    warn!("Failed to parse JSONL line: {e}");
                }
            }
        }
        info!("Imported {count} learnings from JSONL");
        Ok(count)
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[tokio::test]
    async fn test_insert_and_get() {
        let store = SharedLearningStore::in_memory();

        let id = store
            .insert(NewLearning {
                source_agent: "test-agent".into(),
                category: LearningCategory::ModelError,
                summary: "k2p5 model fails".into(),
                details: Some("Use kimi-for-coding/k2p5".into()),
                applicable_agents: vec![],
                verify_pattern: None,
            })
            .await
            .unwrap();

        let l = store.get(&id).await.unwrap().unwrap();
        assert_eq!(l.summary, "k2p5 model fails");
        assert_eq!(l.trust_level, TrustLevel::L0);
    }

    #[tokio::test]
    async fn test_query_respects_trust_level() {
        let store = SharedLearningStore::in_memory();

        // Insert at L0 — should NOT appear with default min_trust=L1
        store
            .insert(NewLearning {
                source_agent: "a".into(),
                category: LearningCategory::Tip,
                summary: "invisible".into(),
                details: None,
                applicable_agents: vec![],
                verify_pattern: None,
            })
            .await
            .unwrap();

        let results = store.query_relevant("any").await.unwrap();
        assert!(results.is_empty(), "L0 learnings should be hidden");
    }

    #[tokio::test]
    async fn test_auto_promotion() {
        let store = SharedLearningStore::in_memory();

        let id = store
            .insert(NewLearning {
                source_agent: "a".into(),
                category: LearningCategory::Tip,
                summary: "helpful tip".into(),
                details: None,
                applicable_agents: vec![],
                verify_pattern: None,
            })
            .await
            .unwrap();

        // L0 → record_effective → L1
        store.record_effective(&id).await.unwrap();
        let l = store.get(&id).await.unwrap().unwrap();
        assert_eq!(l.trust_level, TrustLevel::L1);

        // Now visible in queries
        let results = store.query_relevant("any").await.unwrap();
        assert_eq!(results.len(), 1);
    }

    #[tokio::test]
    async fn test_applicable_agents_filtering() {
        let store = SharedLearningStore::in_memory();

        let id = store
            .insert(NewLearning {
                source_agent: "sec".into(),
                category: LearningCategory::StepFailure,
                summary: "specific to sec-sentinel".into(),
                details: None,
                applicable_agents: vec!["sec-sentinel".into()],
                verify_pattern: None,
            })
            .await
            .unwrap();

        // Promote to L1
        store.record_effective(&id).await.unwrap();

        let for_sentinel = store.query_relevant("sec-sentinel").await.unwrap();
        assert_eq!(for_sentinel.len(), 1);

        let for_other = store.query_relevant("other-agent").await.unwrap();
        assert!(for_other.is_empty());
    }

    #[tokio::test]
    async fn test_context_generation() {
        let store = SharedLearningStore::in_memory();

        let id = store
            .insert(NewLearning {
                source_agent: "security-sentinel".into(),
                category: LearningCategory::ModelError,
                summary: "k2p5/. model ID fails".into(),
                details: Some("Use kimi-for-coding/k2p5 instead".into()),
                applicable_agents: vec![],
                verify_pattern: None,
            })
            .await
            .unwrap();

        store.record_effective(&id).await.unwrap();

        let ctx = store.generate_context("test-agent").await.unwrap();
        assert!(ctx.contains("Known Issues"));
        assert!(ctx.contains("k2p5"));
    }

    #[tokio::test]
    async fn test_archive_stale() {
        let persistence = InMemoryLearningPersistence::new();

        // Insert an old learning manually
        {
            let mut data = persistence.data.write().unwrap();
            data.insert(
                "old-id".into(),
                Learning {
                    id: "old-id".into(),
                    source_agent: "a".into(),
                    category: LearningCategory::Tip,
                    summary: "old".into(),
                    details: None,
                    applicable_agents: vec![],
                    trust_level: TrustLevel::L0,
                    verify_pattern: None,
                    applied_count: 0,
                    effective_count: 0,
                    created_at: Utc::now() - chrono::Duration::days(60),
                    updated_at: Utc::now() - chrono::Duration::days(60),
                    archived_at: None,
                },
            );
        }

        let store = SharedLearningStore::new(Box::new(persistence), TrustLevel::L1);
        let archived = store.archive_stale(30).await.unwrap();
        assert_eq!(archived, 1);

        let l = store.get("old-id").await.unwrap().unwrap();
        assert!(l.archived_at.is_some());
    }

    #[tokio::test]
    async fn test_import_jsonl() {
        let store = SharedLearningStore::in_memory();

        let jsonl = r#"{"source_agent":"test","category":"tip","summary":"use cargo check first","applicable_agents":[]}
{"source_agent":"test","category":"model_error","summary":"k2p5 fails","applicable_agents":[]}
"#;
        let count = store.import_jsonl(jsonl).await.unwrap();
        assert_eq!(count, 2);
    }
}