cortex-mem-core 2.7.0

Core memory management engine for Cortex Memory system
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
use crate::{
    embedding::EmbeddingClient,
    filesystem::{CortexFilesystem, FilesystemOperations},
    layers::manager::LayerManager,
    llm::LLMClient,
    types::{Memory, MemoryMetadata},
    vector_store::{QdrantVectorStore, uri_to_vector_id},
    ContextLayer,
    Result,
};
use std::collections::HashSet;
use std::sync::Arc;
use tracing::{debug, info, warn};

// Import VectorStore trait to use its methods
use crate::vector_store::VectorStore as _;

/// 自动同步管理器
///
/// 负责:
/// 1. 扫描文件系统中的所有Markdown文件
/// 2. 为未索引的文件生成embedding
/// 3. 批量同步到Qdrant向量数据库
/// 4. 支持增量更新
pub struct SyncManager {
    filesystem: Arc<CortexFilesystem>,
    embedding: Arc<EmbeddingClient>,
    vector_store: Arc<crate::vector_store::QdrantVectorStore>,
    llm_client: Arc<dyn LLMClient>,
    config: SyncConfig,
}

/// 自动同步管理器配置
#[derive(Debug, Clone)]
pub struct SyncConfig {
    /// 是否自动索引新消息
    pub auto_index: bool,
    /// 是否同步agents维度
    pub sync_agents: bool,
    /// 是否同步threads维度
    pub sync_threads: bool,
    /// 是否同步users维度
    pub sync_users: bool,
    /// 是否同步global维度
    pub sync_global: bool,
}

impl Default for SyncConfig {
    fn default() -> Self {
        Self {
            auto_index: true,
            sync_agents: true,
            sync_threads: true,
            sync_users: true,
            sync_global: true,
        }
    }
}

/// 同步统计
#[derive(Debug, Clone, Default)]
pub struct SyncStats {
    pub total_files: usize,
    pub indexed_files: usize,
    pub skipped_files: usize,
    pub error_files: usize,
}

impl SyncManager {
    /// 创建新的自动同步管理器
    pub fn new(
        filesystem: Arc<CortexFilesystem>,
        embedding: Arc<EmbeddingClient>,
        vector_store: Arc<crate::vector_store::QdrantVectorStore>,
        llm_client: Arc<dyn LLMClient>,
        config: SyncConfig,
    ) -> Self {
        Self {
            filesystem,
            embedding,
            vector_store,
            llm_client,
            config,
        }
    }

    /// 创建默认配置的自动同步管理器
    pub fn with_defaults(
        filesystem: Arc<CortexFilesystem>,
        embedding: Arc<EmbeddingClient>,
        vector_store: Arc<QdrantVectorStore>,
        llm_client: Arc<dyn LLMClient>,
    ) -> Self {
        Self::new(filesystem, embedding, vector_store, llm_client, SyncConfig::default())
    }

    /// 同步所有内容到向量数据库
    pub async fn sync_all(&self) -> Result<SyncStats> {
        info!("Starting full sync to vector database");

        let mut total_stats = SyncStats::default();

        // 同步用户记忆 (preferences, entities, events)
        if self.config.sync_users {
            let stats = self
                .sync_directory("cortex://user", "L2")
                .await?;
            total_stats.add(&stats);
        }

        // 同步Agent记忆 (cases, skills)
        if self.config.sync_agents {
            let stats = self
                .sync_directory("cortex://agent", "L2")
                .await?;
            total_stats.add(&stats);
        }

        // 同步会话
        if self.config.sync_threads {
            let stats = self.sync_directory_recursive("cortex://session").await?;
            total_stats.add(&stats);
        }

        // 同步资源
        if self.config.sync_global {
            // resources目录可能不存在,如果存在则同步
            if let Ok(entries) = self.filesystem.list("cortex://resources").await {
                if !entries.is_empty() {
                    let stats = self
                        .sync_directory("cortex://resources", "L2")
                        .await?;
                    total_stats.add(&stats);
                }
            }
        }

        info!(
            "Sync completed: {} files processed, {} indexed, {} skipped, {} errors",
            total_stats.total_files,
            total_stats.indexed_files,
            total_stats.skipped_files,
            total_stats.error_files
        );

        Ok(total_stats)
    }

    /// 同步特定路径到向量数据库
    /// 
    /// 用于只索引特定session或特定路径的文件
    /// 例如: sync_specific_path("cortex://session/abc123")
    pub async fn sync_specific_path(&self, uri: &str) -> Result<SyncStats> {
        info!("Starting sync for specific path: {}", uri);

        // 检查路径是否存在
        if !self.filesystem.exists(uri).await? {
            warn!("Path does not exist: {}", uri);
            return Ok(SyncStats::default());
        }

        // 判断是session路径还是其他路径
        let stats = if uri.starts_with("cortex://session/") {
            // session路径使用递归同步(包含timeline等子目录)
            self.sync_directory_recursive(uri).await?
        } else if uri.starts_with("cortex://user/") || uri.starts_with("cortex://agent/") {
            // user/agent路径使用非递归同步
            self.sync_directory(uri, "L2").await?
        } else if uri.starts_with("cortex://resources/") {
            self.sync_directory(uri, "L2").await?
        } else {
            // 其他路径尝试递归同步
            self.sync_directory_recursive(uri).await?
        };

        info!(
            "Sync completed for {}: {} files processed, {} indexed, {} skipped, {} errors",
            uri,
            stats.total_files,
            stats.indexed_files,
            stats.skipped_files,
            stats.error_files
        );

        Ok(stats)
    }

    /// 同步单个目录(非递归)
    /// 
    /// 优化:在目录级别批量处理 L0/L1,避免每个文件重复检查
    fn sync_directory<'a>(
        &'a self,
        uri: &'a str,
        _layer: &'a str,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<SyncStats>> + Send + 'a>> {
        Box::pin(async move {
            let entries = self.filesystem.list(uri).await?;
            let mut stats = SyncStats::default();

            // 收集子目录和文件
            let mut subdirs: Vec<String> = Vec::new();
            let mut files: Vec<String> = Vec::new();

            for entry in &entries {
                if entry.is_directory {
                    subdirs.push(entry.uri.clone());
                } else if entry.name.ends_with(".md") && !entry.name.starts_with('.') {
                    files.push(entry.uri.clone());
                }
            }

            // 先在目录级别批量索引 L0/L1(避免重复)
            if !files.is_empty() {
                if let Err(e) = self.sync_directory_layers(uri).await {
                    debug!("Failed to sync L0/L1 for directory {}: {}", uri, e);
                }
            }

            // 然后处理 L2 文件
            for file_uri in files {
                match self.sync_file_l2(&file_uri).await {
                    Ok(true) => stats.indexed_files += 1,
                    Ok(false) => stats.skipped_files += 1,
                    Err(e) => {
                        warn!("Failed to sync {}: {}", file_uri, e);
                        stats.error_files += 1;
                    }
                }
                stats.total_files += 1;
            }

            // 递归处理子目录
            for subdir in subdirs {
                let sub_stats = self.sync_directory(&subdir, "L2").await?;
                stats.add(&sub_stats);
            }

            Ok(stats)
        })
    }

    /// 同步目录(递归,用于threads)
    fn sync_directory_recursive<'a>(
        &'a self,
        uri: &'a str,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<SyncStats>> + Send + 'a>> {
        Box::pin(async move {
            let entries = self.filesystem.list(uri).await?;
            let mut stats = SyncStats::default();

            // ✅ Generate timeline layers ONLY at session root level (not subdirectories)
            // This prevents overwriting session-level summaries with day-level summaries.
            //
            // Skip if BOTH L0 (.abstract.md) and L1 (.overview.md) already exist.
            //
            // Design rationale:
            //   - The authoritative L0/L1 generator for a session is
            //     `CascadeLayerUpdater::update_timeline_layers`, called inside
            //     `MemoryEventCoordinator::on_session_closed` (via `close_session_sync`).
            //     That path runs on every session close and uses content-hash change
            //     detection to avoid redundant LLM calls.
            //   - This code path (`SyncManager`) is only a fallback for the vector-index
            //     pass at exit time. Historical sessions that are already fully closed
            //     have stable L0/L1 files that don't need regeneration.
            //   - The current session's L0/L1 are guaranteed to be generated by the
            //     `close_session_sync` call that precedes this sync pass; so the
            //     exist-check skip is safe here too.
            let is_session_timeline_root = uri.ends_with("/timeline") && !uri.contains("/timeline/");
            if is_session_timeline_root {
                let l0_exists = self
                    .filesystem
                    .exists(&format!("{}/.abstract.md", uri))
                    .await
                    .unwrap_or(false);
                let l1_exists = self
                    .filesystem
                    .exists(&format!("{}/.overview.md", uri))
                    .await
                    .unwrap_or(false);

                if l0_exists && l1_exists {
                    debug!("Timeline layers already exist for {}, skipping generation", uri);
                } else {
                    if let Err(e) = self.generate_timeline_layers(uri).await {
                        warn!("Failed to generate timeline layers for {}: {}", uri, e);
                    } else {
                        info!("Generated session-level timeline layers for {}", uri);
                    }
                }
            }

            // 收集子目录和文件
            let mut subdirs: Vec<String> = Vec::new();
            let mut files: Vec<String> = Vec::new();

            for entry in &entries {
                if entry.is_directory {
                    subdirs.push(entry.uri.clone());
                } else if entry.name.ends_with(".md") && !entry.name.starts_with('.') {
                    files.push(entry.uri.clone());
                }
            }

            // 先在目录级别批量索引 L0/L1(避免重复)
            if !files.is_empty() {
                if let Err(e) = self.sync_directory_layers(uri).await {
                    debug!("Failed to sync L0/L1 for directory {}: {}", uri, e);
                }
            }

            // 然后处理 L2 文件
            for file_uri in files {
                match self.sync_file_l2(&file_uri).await {
                    Ok(true) => stats.indexed_files += 1,
                    Ok(false) => stats.skipped_files += 1,
                    Err(e) => {
                        warn!("Failed to sync {}: {}", file_uri, e);
                        stats.error_files += 1;
                    }
                }
                stats.total_files += 1;
            }

            // 递归处理子目录
            for subdir in subdirs {
                let sub_stats = self.sync_directory_recursive(&subdir).await?;
                stats.add(&sub_stats);
            }

            Ok(stats)
        })
    }

    /// 在目录级别批量索引 L0/L1(优化:避免每个文件重复检查)
    async fn sync_directory_layers(&self, dir_uri: &str) -> Result<()> {
        // 索引 L0 abstract
        let l0_file_uri = format!("{}/.abstract.md", dir_uri);
        let l0_id = uri_to_vector_id(dir_uri, ContextLayer::L0Abstract);
        
        if !self.is_indexed(&l0_id).await? {
            if let Ok(l0_content) = self.filesystem.read(&l0_file_uri).await {
                let l0_embedding = self.embedding.embed(&l0_content).await?;
                let l0_metadata = self.parse_metadata(dir_uri, "L0")?;

                let l0_memory = Memory {
                    id: l0_id,
                    content: l0_content,
                    embedding: l0_embedding,
                    created_at: chrono::Utc::now(),
                    updated_at: chrono::Utc::now(),
                    metadata: l0_metadata,
                };
                self.vector_store.insert(&l0_memory).await?;
                debug!("L0 indexed for directory: {}", dir_uri);
            }
        }

        // 索引 L1 overview
        let l1_file_uri = format!("{}/.overview.md", dir_uri);
        let l1_id = uri_to_vector_id(dir_uri, ContextLayer::L1Overview);
        
        if !self.is_indexed(&l1_id).await? {
            if let Ok(l1_content) = self.filesystem.read(&l1_file_uri).await {
                let l1_embedding = self.embedding.embed(&l1_content).await?;
                let l1_metadata = self.parse_metadata(dir_uri, "L1")?;

                let l1_memory = Memory {
                    id: l1_id,
                    content: l1_content,
                    embedding: l1_embedding,
                    created_at: chrono::Utc::now(),
                    updated_at: chrono::Utc::now(),
                    metadata: l1_metadata,
                };
                self.vector_store.insert(&l1_memory).await?;
                debug!("L1 indexed for directory: {}", dir_uri);
            }
        }

        Ok(())
    }

    /// 仅同步文件的 L2 层(优化:分离 L0/L1 处理)
    async fn sync_file_l2(&self, uri: &str) -> Result<bool> {
        // 检查是否已经索引(检查L2层)
        let l2_id = uri_to_vector_id(uri, ContextLayer::L2Detail);
        if self.is_indexed(&l2_id).await? {
            debug!("File already indexed (L2): {}", uri);
            return Ok(false);
        }

        // 读取并索引L2原始内容
        let l2_content = self.filesystem.read(uri).await?;
        let l2_embedding = self.embedding.embed(&l2_content).await?;
        let l2_metadata = self.parse_metadata(uri, "L2")?;

        let l2_memory = Memory {
            id: l2_id,
            content: l2_content,
            embedding: l2_embedding,
            created_at: chrono::Utc::now(),
            updated_at: chrono::Utc::now(),
            metadata: l2_metadata,
        };
        self.vector_store.insert(&l2_memory).await?;
        debug!("L2 indexed: {}", uri);

        Ok(true)
    }

    /// 检查向量是否已索引
    async fn is_indexed(&self, id: &str) -> Result<bool> {
        match self.vector_store.get(id).await {
            Ok(Some(_)) => Ok(true),
            Ok(None) => Ok(false),
            Err(e) => {
                debug!("Error checking if indexed: {}", e);
                Ok(false)
            }
        }
    }

    /// 解析URI获取元数据(支持layer标识)
    fn parse_metadata(&self, uri: &str, layer: &str) -> Result<MemoryMetadata> {
        use serde_json::Value;

        // 从URI中提取信息
        // 格式: cortex://dimension/path/to/file.md
        let parts: Vec<&str> = uri.split('/').collect();

        let (dimension, path): (&str, String) = if parts.len() >= 3 {
            (parts[2], parts[3..].join("/"))
        } else {
            (
                "session",
                uri.strip_prefix("cortex://").unwrap_or(uri).to_string(),
            )
        };

        let hash = self.calculate_hash(uri);

        let mut custom = std::collections::HashMap::new();
        custom.insert("uri".to_string(), Value::String(uri.to_string()));
        custom.insert("path".to_string(), Value::String(path.clone()));

        Ok(MemoryMetadata {
            uri: Some(uri.to_string()),
            user_id: if dimension == "user" {
                Some(path.clone())
            } else {
                None
            },
            agent_id: if dimension == "agent" {
                Some(path.clone())
            } else {
                None
            },
            run_id: if dimension == "session" {
                Some(path.clone())
            } else {
                None
            },
            actor_id: None,
            role: None,
            layer: layer.to_string(),
            hash,
            importance_score: 0.5,
            entities: vec![],
            topics: vec![],
            custom,
        })
    }

    /// 计算内容的哈希值
    fn calculate_hash(&self, content: &str) -> String {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        content.hash(&mut hasher);
        format!("{:x}", hasher.finish())
    }

    /// 为timeline目录生成L0/L1层
    ///
    /// 调用LayerManager生成timeline级别的abstract和overview
    async fn generate_timeline_layers(&self, timeline_uri: &str) -> Result<()> {
        let layer_manager = LayerManager::new(self.filesystem.clone(), self.llm_client.clone());
        layer_manager.generate_timeline_layers(timeline_uri).await
    }

    /// 批量同步多个目录(供外部使用)
    pub async fn sync_directories(&self, dir_uris: &[String]) -> Result<SyncStats> {
        let mut total_stats = SyncStats::default();
        let mut processed_dirs: HashSet<String> = HashSet::new();

        for dir_uri in dir_uris {
            // 跳过已处理的目录
            if processed_dirs.contains(dir_uri) {
                continue;
            }
            processed_dirs.insert(dir_uri.clone());

            let stats = self.sync_specific_path(dir_uri).await?;
            total_stats.add(&stats);
        }

        Ok(total_stats)
    }
}

impl SyncStats {
    pub fn add(&mut self, other: &SyncStats) {
        self.total_files += other.total_files;
        self.indexed_files += other.indexed_files;
        self.skipped_files += other.skipped_files;
        self.error_files += other.error_files;
    }
}