oxios-kernel 1.0.2

Oxios kernel: supervisor, event bus, state store
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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
//! Auto-memory bridge: synchronization between external memory systems and Oxios.
//!
//! Bridges Oxios MemoryManager with Claude Code's MEMORY.md format and
//! similar external memory stores. Supports bidirectional sync:
//!
//! - **to-auto**: Export Oxios patterns/insights → external MEMORY.md format
//! - **from-auto**: Import external memories → Oxios MemoryStore
//! - **bidirectional**: Full two-way synchronization
//!
//! The bridge converts between Oxios's structured memory entries and
//! free-form markdown memory formats used by external tools.

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use anyhow::Result;
use chrono::Utc;
use serde::{Deserialize, Serialize};

use super::{MemoryEntry, MemoryManager, MemoryType};

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

/// Direction of memory synchronization.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SyncDirection {
    /// Export Oxios → external format.
    ToAuto,
    /// Import external → Oxios.
    FromAuto,
    /// Two-way sync.
    Bidirectional,
}

/// Category of an imported memory insight.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InsightCategory {
    /// Project-level patterns and conventions.
    ProjectPatterns,
    /// Debugging strategies and fixes.
    Debugging,
    /// Architecture decisions.
    Architecture,
    /// Performance observations.
    Performance,
    /// Security-related insights.
    Security,
    /// General knowledge.
    General,
}

impl InsightCategory {
    /// Parse from a string.
    pub fn from_str_loose(s: &str) -> Self {
        match s.to_lowercase().as_str() {
            "project-patterns" | "project_patterns" | "patterns" => {
                InsightCategory::ProjectPatterns
            }
            "debugging" | "debug" => InsightCategory::Debugging,
            "architecture" | "arch" => InsightCategory::Architecture,
            "performance" | "perf" => InsightCategory::Performance,
            "security" | "sec" => InsightCategory::Security,
            _ => InsightCategory::General,
        }
    }

    /// Convert to a tag string.
    pub fn to_tag(&self) -> &'static str {
        match self {
            InsightCategory::ProjectPatterns => "project-patterns",
            InsightCategory::Debugging => "debugging",
            InsightCategory::Architecture => "architecture",
            InsightCategory::Performance => "performance",
            InsightCategory::Security => "security",
            InsightCategory::General => "general",
        }
    }
}

/// A single imported memory insight from an external system.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryInsight {
    /// Category of the insight.
    pub category: InsightCategory,
    /// Brief summary (1 line).
    pub summary: String,
    /// Optional detailed content.
    pub detail: Option<String>,
    /// Source identifier (e.g., "claude-code", "user").
    pub source: String,
    /// Confidence score (0.0 - 1.0).
    pub confidence: f32,
}

/// Result of importing memories from an external system.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ImportResult {
    /// Number of insights successfully imported.
    pub imported: usize,
    /// Number of duplicates skipped.
    pub skipped_duplicates: usize,
    /// Number of failed imports.
    pub failed: usize,
    /// Error messages for failed imports.
    pub errors: Vec<String>,
}

/// Result of exporting memories to an external system.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ExportResult {
    /// Number of patterns exported.
    pub exported: usize,
    /// Number of categories created/updated.
    pub categories_updated: usize,
}

/// Result of a bidirectional sync operation.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SyncResult {
    /// Import results.
    pub import: ImportResult,
    /// Export results.
    pub export: ExportResult,
}

/// A guidance pattern for export to external format.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GuidancePattern {
    /// Pattern identifier.
    pub id: String,
    /// Category.
    pub category: InsightCategory,
    /// Brief description.
    pub description: String,
    /// Confidence/importance.
    pub confidence: f32,
    /// Usage count.
    pub usage_count: u32,
}

// ---------------------------------------------------------------------------
// AutoMemoryBridge
// ---------------------------------------------------------------------------

/// Bridge between Oxios memory and external memory systems.
///
/// Bridges Oxios MemoryManager (agent session memory) and optional
/// KnowledgeBase (markdown knowledge base) with external MEMORY.md
/// files. Supports bidirectional sync with Claude Code and similar
/// external tools.
///
/// **RFC-003: KnowledgeBase is the single source of truth for markdown.
/// MemoryManager stores agent session memory. The bridge can sync from
/// either source or both.**
pub struct AutoMemoryBridge {
    /// Directory containing external memory files.
    auto_memory_dir: PathBuf,
    /// Oxios memory manager (agent session memory).
    oxios_memory: std::sync::Arc<MemoryManager>,
    /// Optional markdown knowledge base (global, not per-Space).
    knowledge_base: Option<std::sync::Arc<oxios_markdown::KnowledgeBase>>,
}

impl AutoMemoryBridge {
    /// Create a new bridge.
    ///
    /// # Arguments
    /// * `auto_memory_dir` - Path to directory containing MEMORY.md files
    /// * `oxios_memory` - Oxios MemoryManager instance
    pub fn new(auto_memory_dir: PathBuf, oxios_memory: std::sync::Arc<MemoryManager>) -> Self {
        Self {
            auto_memory_dir,
            oxios_memory,
            knowledge_base: None,
        }
    }

    /// Set the optional markdown knowledge base.
    ///
    /// When set, `export_knowledge_to_auto()` can read directly from
    /// `.md` files instead of relying on MemoryManager entries.
    pub fn with_knowledge_base(
        mut self,
        kb: std::sync::Arc<oxios_markdown::KnowledgeBase>,
    ) -> Self {
        self.knowledge_base = Some(kb);
        self
    }

    /// Import memories from external format into Oxios.
    ///
    /// Reads MEMORY.md files from the auto_memory_dir, parses them into
    /// structured insights, and stores them via MemoryManager.
    pub async fn import_from_auto(&self) -> Result<ImportResult> {
        let mut result = ImportResult::default();

        // Find all MEMORY.md files
        let memory_files = self.find_memory_files()?;

        for file_path in &memory_files {
            match self.import_file(file_path).await {
                Ok(file_result) => {
                    result.imported += file_result.imported;
                    result.skipped_duplicates += file_result.skipped_duplicates;
                    result.failed += file_result.failed;
                    result.errors.extend(file_result.errors);
                }
                Err(e) => {
                    result.failed += 1;
                    result
                        .errors
                        .push(format!("{}: {}", file_path.display(), e));
                }
            }
        }

        Ok(result)
    }

    /// Export Oxios patterns to external MEMORY.md format.
    ///
    /// Reads patterns from Oxios memory and writes them as structured
    /// markdown files in the auto_memory_dir.
    pub async fn export_to_auto(&self, patterns: &[GuidancePattern]) -> Result<ExportResult> {
        let mut result = ExportResult::default();

        // Ensure output directory exists
        tokio::fs::create_dir_all(&self.auto_memory_dir).await?;

        // Group patterns by category
        let mut by_category: HashMap<InsightCategory, Vec<&GuidancePattern>> = HashMap::new();
        for pattern in patterns {
            by_category
                .entry(pattern.category.clone())
                .or_default()
                .push(pattern);
        }

        // Write each category to its own file
        for (category, cat_patterns) in &by_category {
            let filename = match category {
                InsightCategory::ProjectPatterns => "patterns.md",
                InsightCategory::Debugging => "debugging.md",
                InsightCategory::Architecture => "architecture.md",
                InsightCategory::Performance => "performance.md",
                InsightCategory::Security => "security.md",
                InsightCategory::General => "general.md",
            };

            let content = self.format_patterns_md(cat_patterns);
            let path = self.auto_memory_dir.join(filename);

            tokio::fs::write(&path, &content).await?;
            result.categories_updated += 1;
        }

        // Write main MEMORY.md with all patterns sorted by confidence
        let mut all_patterns: Vec<&GuidancePattern> = patterns.iter().collect();
        all_patterns.sort_by(|a, b| {
            b.confidence
                .partial_cmp(&a.confidence)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        let main_content = self.format_main_md(&all_patterns);
        let main_path = self.auto_memory_dir.join("MEMORY.md");
        tokio::fs::write(&main_path, &main_content).await?;

        result.exported = patterns.len();
        result.categories_updated += 1; // MEMORY.md itself
        Ok(result)
    }

    /// Perform a bidirectional sync.
    pub async fn sync_session(&self, direction: SyncDirection) -> Result<SyncResult> {
        let mut sync_result = SyncResult::default();

        match direction {
            SyncDirection::FromAuto => {
                sync_result.import = self.import_from_auto().await?;
            }
            SyncDirection::ToAuto => {
                sync_result.export = self.export_knowledge_to_auto().await?;
            }
            SyncDirection::Bidirectional => {
                sync_result.import = self.import_from_auto().await?;
                sync_result.export = self.export_knowledge_to_auto().await?;
            }
        }

        Ok(sync_result)
    }

    /// Returns the auto-memory directory path.
    pub fn auto_memory_dir(&self) -> &Path {
        &self.auto_memory_dir
    }

    /// Sync learned patterns from SQLite to external MEMORY.md (Phase 7).
    ///
    /// Reads patterns from the SQLite `patterns` table and exports
    /// them as structured markdown to the auto-memory directory.
    #[cfg(feature = "sqlite-memory")]
    pub async fn sync_sqlite_to_auto(
        &self,
        store: &crate::memory::sqlite_store::SqliteMemoryStore,
    ) -> Result<ExportResult> {
        let rows = store.load_patterns()?;
        if rows.is_empty() {
            return Ok(ExportResult::default());
        }

        let patterns: Vec<GuidancePattern> = rows
            .iter()
            .map(|r| GuidancePattern {
                id: r.id.clone(),
                category: r
                    .domain
                    .as_deref()
                    .map(InsightCategory::from_str_loose)
                    .unwrap_or(InsightCategory::General),
                description: format!(
                    "[{}] quality={:.2} uses={}",
                    r.strategy, r.quality, r.use_count
                ),
                confidence: r.quality,
                usage_count: r.use_count,
            })
            .collect();

        self.export_to_auto(&patterns).await
    }

    /// Import external MEMORY.md insights into SQLite patterns (Phase 7).
    ///
    /// Reads from auto-memory directory and stores as patterns.
    #[cfg(feature = "sqlite-memory")]
    pub async fn sync_auto_to_sqlite(
        &self,
        store: &crate::memory::sqlite_store::SqliteMemoryStore,
    ) -> Result<ImportResult> {
        let import_result = self.import_from_auto().await?;

        // Also store imported insights as patterns in SQLite
        let memory_files = self.find_memory_files()?;
        for file_path in &memory_files {
            if let Ok(content) = tokio::fs::read_to_string(file_path).await {
                let insights = self.parse_insights(&content);
                for insight in insights {
                    let id = uuid::Uuid::new_v4().to_string();
                    let data = serde_json::to_string(&insight)?;
                    let _ = store.save_pattern(
                        &id,
                        "imported",
                        Some(insight.category.to_tag()),
                        insight.confidence,
                        &data,
                    );
                }
            }
        }

        Ok(import_result)
    }

    /// Export all knowledge memories from Oxios to external format.
    ///
    /// Reads from MemoryManager (primary) or falls back to KnowledgeBase
    /// `.md` files when `knowledge_base` is set and MemoryManager has no
    /// `MemoryType::Knowledge` entries.
    async fn export_knowledge_to_auto(&self) -> Result<ExportResult> {
        // Try MemoryManager first (primary source)
        let entries = self
            .oxios_memory
            .list(MemoryType::Knowledge, 1000)
            .await
            .unwrap_or_default();

        if !entries.is_empty() {
            let patterns: Vec<GuidancePattern> = entries
                .iter()
                .map(|e| GuidancePattern {
                    id: e.id.clone(),
                    category: e
                        .tags
                        .first()
                        .map(|t| InsightCategory::from_str_loose(t))
                        .unwrap_or(InsightCategory::General),
                    description: e.content.clone(),
                    confidence: e.importance,
                    usage_count: e.access_count,
                })
                .collect();
            return self.export_to_auto(&patterns).await;
        }

        // Fall back to KnowledgeBase .md files (RFC-003)
        if let Some(kb) = &self.knowledge_base {
            let entries = kb.index_all()?;
            if entries > 0 {
                let patterns = kb
                    .note_tree("/")
                    .unwrap_or_default()
                    .into_iter()
                    .filter(|e| !e.is_dir && e.name.ends_with(".md"))
                    .map(|e| {
                        let path = if e.parent_dir == "/" || e.parent_dir.is_empty() {
                            e.name.clone()
                        } else {
                            format!("{}/{}", e.parent_dir, e.name)
                        };
                        let content = kb.note_read(&path).ok().flatten().unwrap_or_default();
                        let headings = kb.extract_headings(&content);
                        let tag = headings.first().map(|s| s.as_str()).unwrap_or("general");
                        GuidancePattern {
                            id: format!("note-{}", path.replace('/', "-").trim_end_matches(".md")),
                            category: InsightCategory::from_str_loose(tag),
                            description: content.chars().take(300).collect(),
                            confidence: 0.6,
                            usage_count: 0,
                        }
                    })
                    .collect::<Vec<_>>();
                if !patterns.is_empty() {
                    return self.export_to_auto(&patterns).await;
                }
            }
        }

        // No entries from either source — export empty result
        Ok(ExportResult::default())
    }
}

// ---------------------------------------------------------------------------
// Private helpers
// ---------------------------------------------------------------------------

impl AutoMemoryBridge {
    /// Find all MEMORY.md files in the auto_memory_dir.
    /// Parse insights from a markdown content string.
    fn parse_insights(&self, content: &str) -> Vec<MemoryInsight> {
        let mut insights = Vec::new();

        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() || line.starts_with('#') {
                continue;
            }
            // Each bullet point is an insight
            if line.starts_with("- ") || line.starts_with("* ") {
                let text = line.trim_start_matches("- ").trim_start_matches("* ");
                let category = if text.contains("debug") || text.contains("fix") {
                    InsightCategory::Debugging
                } else if text.contains("arch") || text.contains("design") {
                    InsightCategory::Architecture
                } else if text.contains("perf") || text.contains("speed") {
                    InsightCategory::Performance
                } else if text.contains("security") || text.contains("vuln") {
                    InsightCategory::Security
                } else if text.contains("pattern") || text.contains("convention") {
                    InsightCategory::ProjectPatterns
                } else {
                    InsightCategory::General
                };
                insights.push(MemoryInsight {
                    category,
                    summary: text.to_string(),
                    detail: None,
                    source: "auto-bridge".to_string(),
                    confidence: 0.7,
                });
            }
        }

        insights
    }

    fn find_memory_files(&self) -> Result<Vec<PathBuf>> {
        let mut files = Vec::new();

        if self.auto_memory_dir.exists() {
            // Look for MEMORY.md directly
            let main = self.auto_memory_dir.join("MEMORY.md");
            if main.exists() {
                files.push(main);
            }

            // Look for topic files
            for topic in &[
                "patterns.md",
                "debugging.md",
                "architecture.md",
                "performance.md",
                "security.md",
                "general.md",
            ] {
                let path = self.auto_memory_dir.join(topic);
                if path.exists() {
                    files.push(path);
                }
            }

            // Look for *.md files in subdirectories
            if let Ok(entries) = std::fs::read_dir(&self.auto_memory_dir) {
                for entry in entries.flatten() {
                    let path = entry.path();
                    if path.extension().is_some_and(|ext| ext == "md") {
                        let name = path.file_name().unwrap_or_default().to_string_lossy();
                        // Skip already-added files
                        if ![
                            "MEMORY.md",
                            "patterns.md",
                            "debugging.md",
                            "architecture.md",
                            "performance.md",
                            "security.md",
                            "general.md",
                        ]
                        .contains(&name.as_ref())
                        {
                            files.push(path);
                        }
                    }
                }
            }
        }

        Ok(files)
    }

    /// Import a single markdown file into Oxios memory.
    async fn import_file(&self, path: &Path) -> Result<ImportResult> {
        let content = tokio::fs::read_to_string(path).await?;
        let insights = self.parse_markdown_insights(&content);
        let mut result = ImportResult::default();

        for insight in &insights {
            // Check for duplicates
            if self.oxios_memory.is_duplicate(&insight.summary).await {
                result.skipped_duplicates += 1;
                continue;
            }

            let entry = MemoryEntry {
                id: format!(
                    "auto-{}-{}",
                    insight.source,
                    chrono::Utc::now().timestamp_nanos_opt().unwrap_or(0)
                ),
                memory_type: MemoryType::Knowledge,
                tier: super::MemoryTier::Warm,
                content: match &insight.detail {
                    Some(d) => format!("{}\n\n{}", insight.summary, d),
                    None => insight.summary.clone(),
                },
                content_hash: 0,
                source: insight.source.clone(),
                session_id: None,
                tags: vec![insight.category.to_tag().to_string()],
                importance: insight.confidence,
                pinned: false,
                protection: super::ProtectionLevel::None,
                auto_classified: false,
                session_appearances: 0,
                user_corrected: false,
                seen_in_sessions: vec![],
                created_at: Utc::now(),
                accessed_at: Utc::now(),
                modified_at: Utc::now(),
                access_count: 0,
                decay_score: 1.0,
                compaction_level: 0,
                compacted_from: vec![],
                related_ids: vec![],
                contradicts: None,
            };

            match self.oxios_memory.remember(entry).await {
                Ok(_) => result.imported += 1,
                Err(e) => {
                    result.failed += 1;
                    result.errors.push(e.to_string());
                    tracing::warn!(error = %e, "Failed to import memory insight");
                }
            }
        }

        Ok(result)
    }

    /// Parse insights from markdown content.
    ///
    /// Supports multiple formats:
    /// - List items: `- **Category**: Description`
    /// - Headers: `## Category` followed by bullet points
    /// - Free-form text split into chunks
    fn parse_markdown_insights(&self, content: &str) -> Vec<MemoryInsight> {
        let mut insights = Vec::new();
        let mut current_category = InsightCategory::General;

        for line in content.lines() {
            let trimmed = line.trim();

            // Skip empty lines
            if trimmed.is_empty() {
                continue;
            }

            // Detect category headers: ## Category or # Category
            if trimmed.starts_with('#') {
                let header = trimmed.trim_start_matches('#').trim().to_lowercase();
                current_category = InsightCategory::from_str_loose(&header);
                continue;
            }

            // Parse bullet list items: - **Category**: Description
            if trimmed.starts_with('-') || trimmed.starts_with('*') {
                // Strip the leading list marker and whitespace, preserving ** bold markers
                let item = if trimmed.starts_with('-') {
                    trimmed.trim_start_matches('-').trim_start()
                } else {
                    trimmed.trim_start_matches('*').trim_start()
                };

                // Check for bold category prefix: **Category**:
                if let Some(rest) = Self::extract_bold_category(item) {
                    let (cat_name, description) = rest;
                    let category = InsightCategory::from_str_loose(cat_name);

                    // Split on colon for detail
                    let (summary, detail) = if let Some(pos) = description.find(':') {
                        let s = description[..pos].trim();
                        let d = description[pos + 1..].trim();
                        (
                            s.to_string(),
                            if d.is_empty() {
                                None
                            } else {
                                Some(d.to_string())
                            },
                        )
                    } else {
                        (description.to_string(), None)
                    };

                    if !summary.is_empty() {
                        insights.push(MemoryInsight {
                            category,
                            summary,
                            detail,
                            source: "auto-import".to_string(),
                            confidence: 0.7,
                        });
                    }
                } else if !item.is_empty() {
                    // Plain bullet point
                    insights.push(MemoryInsight {
                        category: current_category.clone(),
                        summary: item.to_string(),
                        detail: None,
                        source: "auto-import".to_string(),
                        confidence: 0.6,
                    });
                }
            }
        }

        // If no structured content found, treat the whole content as a single insight
        if insights.is_empty() && !content.trim().is_empty() {
            let summary: String = content
                .lines()
                .take(3)
                .collect::<Vec<_>>()
                .join(" ")
                .chars()
                .take(200)
                .collect();

            if !summary.trim().is_empty() {
                insights.push(MemoryInsight {
                    category: InsightCategory::General,
                    summary,
                    detail: Some(content.to_string()),
                    source: "auto-import".to_string(),
                    confidence: 0.5,
                });
            }
        }

        insights
    }

    /// Extract a bold category prefix from a markdown item.
    ///
    /// Returns Some((category, rest)) if the item starts with **Category**:
    fn extract_bold_category(item: &str) -> Option<(&str, &str)> {
        if !item.starts_with("**") {
            return None;
        }

        let end = item[2..].find("**")?;
        let category = &item[2..2 + end];
        let rest = item[2 + end + 2..].trim_start_matches([' ', ':']);

        Some((category, rest))
    }

    /// Format patterns as a category-specific markdown file.
    fn format_patterns_md(&self, patterns: &[&GuidancePattern]) -> String {
        let mut md = String::new();
        md.push_str("# Memory Insights\n\n");

        for pattern in patterns {
            let confidence_bar = format_confidence_bar(pattern.confidence);
            md.push_str(&format!(
                "- **{}**: {} [{}]\n",
                pattern.category.to_tag(),
                pattern.description,
                confidence_bar,
            ));
        }

        md.push('\n');
        md
    }

    /// Format all patterns as the main MEMORY.md file.
    fn format_main_md(&self, patterns: &[&GuidancePattern]) -> String {
        let mut md = String::new();
        md.push_str("# Oxios Memory\n\n");
        md.push_str(&format!(
            "Auto-generated at {}\n\n",
            Utc::now().to_rfc3339()
        ));
        md.push_str("## Insights\n\n");

        for pattern in patterns {
            let confidence_pct = (pattern.confidence * 100.0) as u8;
            md.push_str(&format!(
                "- **{}** [{}%]: {}\n",
                pattern.category.to_tag(),
                confidence_pct,
                pattern.description,
            ));
        }

        md.push('\n');
        md
    }
}

/// Format a confidence value as a visual bar.
fn format_confidence_bar(confidence: f32) -> String {
    let bars = (confidence * 5.0).round() as usize;
    let bars = bars.min(5);
    let filled: String = "".repeat(bars);
    let empty: String = "".repeat(5 - bars);
    format!("{}{} {:.0}%", filled, empty, confidence * 100.0)
}

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

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

    fn make_bridge(dir: &Path) -> AutoMemoryBridge {
        let store = Arc::new(crate::state_store::StateStore::new(dir.join("state")).unwrap());
        let memory = Arc::new(MemoryManager::new(store));
        AutoMemoryBridge::new(dir.join("auto"), memory).with_knowledge_base(Arc::new(
            oxios_markdown::KnowledgeBase::new(dir.join("kb")).unwrap(),
        ))
    }

    #[test]
    fn test_parse_bold_category() {
        let result =
            AutoMemoryBridge::extract_bold_category("**Debugging**: Use trace-level logging");
        assert!(result.is_some());
        let (cat, rest) = result.unwrap();
        assert_eq!(cat, "Debugging");
        assert_eq!(rest, "Use trace-level logging");
    }

    #[test]
    fn test_parse_bold_category_no_colon() {
        let result = AutoMemoryBridge::extract_bold_category("**Security** important rule");
        assert!(result.is_some());
        let (cat, rest) = result.unwrap();
        assert_eq!(cat, "Security");
        assert_eq!(rest, "important rule");
    }

    #[test]
    fn test_parse_no_bold() {
        let result = AutoMemoryBridge::extract_bold_category("Just a plain item");
        assert!(result.is_none());
    }

    #[test]
    fn test_parse_markdown_insights() {
        let temp_dir = tempfile::tempdir().unwrap();
        let bridge = make_bridge(temp_dir.path());

        let md = r#"# Project Patterns

- **Debugging**: Use trace-level logging for async tasks
- **Architecture**: Follow the kernel module pattern
- **Performance**: Batch embeddings when possible

## Security

- Always validate input at the boundary
- Use RBAC for multi-agent access control
"#;

        let insights = bridge.parse_markdown_insights(md);
        assert!(!insights.is_empty());

        // Should have parsed the bullet points
        let debugging = insights
            .iter()
            .find(|i| i.category == InsightCategory::Debugging);
        assert!(debugging.is_some());

        let security = insights
            .iter()
            .find(|i| i.category == InsightCategory::Security);
        assert!(security.is_some());
    }

    #[test]
    fn test_parse_empty_markdown() {
        let temp_dir = tempfile::tempdir().unwrap();
        let bridge = make_bridge(temp_dir.path());

        let insights = bridge.parse_markdown_insights("");
        assert!(insights.is_empty());
    }

    #[test]
    fn test_parse_plain_text_as_single_insight() {
        let temp_dir = tempfile::tempdir().unwrap();
        let bridge = make_bridge(temp_dir.path());

        let text = "This is a plain text memory entry without markdown formatting.";
        let insights = bridge.parse_markdown_insights(text);
        assert_eq!(insights.len(), 1);
        assert_eq!(insights[0].category, InsightCategory::General);
    }

    #[test]
    fn test_insight_category_parsing() {
        assert_eq!(
            InsightCategory::from_str_loose("patterns"),
            InsightCategory::ProjectPatterns
        );
        assert_eq!(
            InsightCategory::from_str_loose("debug"),
            InsightCategory::Debugging
        );
        assert_eq!(
            InsightCategory::from_str_loose("arch"),
            InsightCategory::Architecture
        );
        assert_eq!(
            InsightCategory::from_str_loose("perf"),
            InsightCategory::Performance
        );
        assert_eq!(
            InsightCategory::from_str_loose("sec"),
            InsightCategory::Security
        );
        assert_eq!(
            InsightCategory::from_str_loose("unknown"),
            InsightCategory::General
        );
    }

    #[test]
    fn test_confidence_bar() {
        let bar = format_confidence_bar(0.8);
        assert!(bar.contains("80%"));

        let bar_low = format_confidence_bar(0.2);
        assert!(bar_low.contains("20%"));
    }

    #[tokio::test]
    async fn test_import_from_empty_dir() {
        let temp_dir = tempfile::tempdir().unwrap();
        let bridge = make_bridge(temp_dir.path());

        // No files to import
        let result = bridge.import_from_auto().await.unwrap();
        assert_eq!(result.imported, 0);
        assert_eq!(result.failed, 0);
    }

    #[tokio::test]
    async fn test_import_from_file() {
        let temp_dir = tempfile::tempdir().unwrap();
        let auto_dir = temp_dir.path().join("auto");
        tokio::fs::create_dir_all(&auto_dir).await.unwrap();

        // Create a MEMORY.md file
        let md = r#"- **Debugging**: Use println! for quick debugging
- **Architecture**: Keep modules small and focused
"#;
        tokio::fs::write(auto_dir.join("MEMORY.md"), md)
            .await
            .unwrap();

        let bridge = make_bridge(temp_dir.path());
        let result = bridge.import_from_auto().await.unwrap();
        assert!(result.imported >= 1, "Should import at least 1 insight");
    }

    #[tokio::test]
    async fn test_export_to_auto() {
        let temp_dir = tempfile::tempdir().unwrap();
        let bridge = make_bridge(temp_dir.path());

        let patterns = vec![
            GuidancePattern {
                id: "p1".to_string(),
                category: InsightCategory::Debugging,
                description: "Always check error chains".to_string(),
                confidence: 0.9,
                usage_count: 5,
            },
            GuidancePattern {
                id: "p2".to_string(),
                category: InsightCategory::Architecture,
                description: "Use actor model for concurrency".to_string(),
                confidence: 0.7,
                usage_count: 3,
            },
        ];

        let result = bridge.export_to_auto(&patterns).await.unwrap();
        assert_eq!(result.exported, 2);
        assert!(result.categories_updated >= 2);

        // Verify files were created
        assert!(bridge.auto_memory_dir.join("MEMORY.md").exists());
        assert!(bridge.auto_memory_dir.join("debugging.md").exists());
        assert!(bridge.auto_memory_dir.join("architecture.md").exists());

        // Verify content
        let main = tokio::fs::read_to_string(bridge.auto_memory_dir.join("MEMORY.md"))
            .await
            .unwrap();
        assert!(main.contains("Oxios Memory"));
        assert!(main.contains("Always check error chains"));
        assert!(main.contains("Use actor model for concurrency"));
    }

    #[tokio::test]
    async fn test_bidirectional_sync() {
        let temp_dir = tempfile::tempdir().unwrap();
        let auto_dir = temp_dir.path().join("auto");
        tokio::fs::create_dir_all(&auto_dir).await.unwrap();

        // Create an external memory file
        tokio::fs::write(
            auto_dir.join("MEMORY.md"),
            "- **Debugging**: Test insight for sync",
        )
        .await
        .unwrap();

        let bridge = make_bridge(temp_dir.path());
        let result = bridge
            .sync_session(SyncDirection::Bidirectional)
            .await
            .unwrap();

        // Should have imported the insight
        assert!(result.import.imported >= 1 || result.import.skipped_duplicates > 0);

        // Should have exported current state (usize, always >= 0)
    }

    #[test]
    fn test_sync_direction_serialization() {
        let dir = SyncDirection::ToAuto;
        let json = serde_json::to_string(&dir).unwrap();
        assert_eq!(json, "\"to_auto\"");

        let parsed: SyncDirection = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, SyncDirection::ToAuto);
    }
}