chasm-cli 2.0.0

Universal chat session manager - harvest, merge, and analyze AI chat history from VS Code, Cursor, and other editors
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
// Copyright (c) 2024-2026 Nervosys LLC
// SPDX-License-Identifier: AGPL-3.0-only
//! Bidirectional Sync Module
//!
//! Provides two-way synchronization between CSM and provider-native storage.
//! Handles conflict resolution, change tracking, and state reconciliation.

use crate::models::{extract_response_text, ChatSession};
use crate::providers::ProviderType;
use anyhow::{anyhow, Result};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::path::PathBuf;

// =============================================================================
// Sync State and Tracking
// =============================================================================

/// Sync state for a single session
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionSyncState {
    pub session_id: String,
    pub provider: ProviderType,
    pub last_sync: DateTime<Utc>,
    pub local_hash: String,
    pub remote_hash: String,
    pub status: SyncStatus,
    pub pending_changes: Vec<SyncChange>,
}

/// Sync status for a session
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SyncStatus {
    Synced,
    LocalAhead,
    RemoteAhead,
    Conflict,
    Unsynced,
    Error,
}

/// Represents a single change to sync
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SyncChange {
    pub id: String,
    pub change_type: ChangeType,
    pub entity_type: EntityType,
    pub entity_id: String,
    pub timestamp: DateTime<Utc>,
    pub payload: serde_json::Value,
    pub origin: ChangeOrigin,
}

/// Type of change
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ChangeType {
    Create,
    Update,
    Delete,
    Merge,
}

/// Entity type for changes
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EntityType {
    Session,
    Message,
    Metadata,
    Tag,
}

/// Origin of a change
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ChangeOrigin {
    Local,
    Remote,
}

// =============================================================================
// Conflict Resolution
// =============================================================================

/// Conflict resolution strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ConflictStrategy {
    LocalWins,
    RemoteWins,
    KeepBoth,
    AutoMerge,
    Manual,
    MostRecent,
}

/// A sync conflict that needs resolution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SyncConflict {
    pub id: String,
    pub session_id: String,
    pub local_version: ConflictVersion,
    pub remote_version: ConflictVersion,
    pub conflict_type: ConflictType,
    pub suggested_strategy: ConflictStrategy,
    pub created_at: DateTime<Utc>,
    pub resolved: bool,
}

/// Version info for conflict
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConflictVersion {
    pub hash: String,
    pub timestamp: DateTime<Utc>,
    pub message_count: usize,
}

/// Type of conflict
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ConflictType {
    MessageEdit,
    SessionMetadata,
    Deletion,
    ConcurrentAdd,
}

/// Result of a sync operation
#[derive(Debug, Clone)]
pub enum SyncResult {
    NoChanges,
    Pushed,
    Pulled,
    Merged,
    ConflictDetected(SyncConflict),
}

// =============================================================================
// Hash Computation
// =============================================================================

/// Compute a content hash for a session
pub fn compute_session_hash(session: &ChatSession) -> String {
    let mut hasher = Sha256::new();

    // Hash session metadata
    let session_id = session.session_id.clone().unwrap_or_default();
    hasher.update(session_id.as_bytes());

    if let Some(title) = &session.custom_title {
        hasher.update(title.as_bytes());
    }

    hasher.update(session.last_message_date.to_le_bytes());

    // Hash requests content
    for request in &session.requests {
        if let Some(msg) = &request.message {
            if let Some(text) = &msg.text {
                hasher.update(text.as_bytes());
            }
        }
        if let Some(resp) = &request.response {
            if let Some(result) = extract_response_text(resp) {
                hasher.update(result.as_bytes());
            }
        }
    }

    format!("{:x}", hasher.finalize())
}

// =============================================================================
// Bidirectional Sync Engine
// =============================================================================

/// Configuration for bidirectional sync
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BidirectionalSyncConfig {
    pub conflict_strategy: ConflictStrategy,
    pub auto_sync_interval_secs: u64,
    pub max_retries: u32,
    pub retry_delay_ms: u64,
    pub batch_size: usize,
}

impl Default for BidirectionalSyncConfig {
    fn default() -> Self {
        Self {
            conflict_strategy: ConflictStrategy::MostRecent,
            auto_sync_interval_secs: 300,
            max_retries: 3,
            retry_delay_ms: 1000,
            batch_size: 50,
        }
    }
}

/// Bidirectional sync engine
pub struct BidirectionalSyncEngine {
    config: BidirectionalSyncConfig,
    state: HashMap<String, SessionSyncState>,
    conflicts: Vec<SyncConflict>,
}

impl BidirectionalSyncEngine {
    pub fn new(config: BidirectionalSyncConfig) -> Self {
        Self {
            config,
            state: HashMap::new(),
            conflicts: Vec::new(),
        }
    }

    /// Check sync status for a session
    pub fn check_status(
        &self,
        session_id: &str,
        local_session: &ChatSession,
        remote_session: Option<&ChatSession>,
    ) -> SyncStatus {
        let local_hash = compute_session_hash(local_session);
        let remote_hash = remote_session.map(compute_session_hash).unwrap_or_default();

        match self.state.get(session_id) {
            None => SyncStatus::Unsynced,
            Some(prev_state) => {
                let local_changed = local_hash != prev_state.local_hash;
                let remote_changed = remote_hash != prev_state.remote_hash;

                match (local_changed, remote_changed) {
                    (false, false) => SyncStatus::Synced,
                    (true, false) => SyncStatus::LocalAhead,
                    (false, true) => SyncStatus::RemoteAhead,
                    (true, true) => SyncStatus::Conflict,
                }
            }
        }
    }

    /// Sync a session bidirectionally
    pub fn sync_session(
        &mut self,
        local_session: &mut ChatSession,
        remote_session: Option<&ChatSession>,
        push_fn: impl FnOnce(&ChatSession) -> Result<()>,
        pull_fn: impl FnOnce() -> Result<Option<ChatSession>>,
    ) -> Result<SyncResult> {
        let session_id = local_session.session_id.clone().unwrap_or_default();
        let status = self.check_status(&session_id, local_session, remote_session);

        match status {
            SyncStatus::Synced => Ok(SyncResult::NoChanges),

            SyncStatus::LocalAhead => {
                push_fn(local_session)?;
                self.update_state(&session_id, local_session, local_session);
                Ok(SyncResult::Pushed)
            }

            SyncStatus::RemoteAhead => {
                if let Some(remote) = pull_fn()? {
                    *local_session = remote.clone();
                    self.update_state(&session_id, local_session, &remote);
                    Ok(SyncResult::Pulled)
                } else {
                    Ok(SyncResult::NoChanges)
                }
            }

            SyncStatus::Conflict => {
                let remote = remote_session
                    .ok_or_else(|| anyhow!("Remote required for conflict resolution"))?;
                self.resolve_conflict(local_session, remote)
            }

            SyncStatus::Unsynced => {
                push_fn(local_session)?;
                self.update_state(&session_id, local_session, local_session);
                Ok(SyncResult::Pushed)
            }

            SyncStatus::Error => Err(anyhow!("Sync in error state")),
        }
    }

    /// Update sync state after successful sync
    fn update_state(&mut self, session_id: &str, local: &ChatSession, remote: &ChatSession) {
        let state = SessionSyncState {
            session_id: session_id.to_string(),
            provider: ProviderType::Custom,
            last_sync: Utc::now(),
            local_hash: compute_session_hash(local),
            remote_hash: compute_session_hash(remote),
            status: SyncStatus::Synced,
            pending_changes: Vec::new(),
        };
        self.state.insert(session_id.to_string(), state);
    }

    /// Resolve a conflict between local and remote versions
    fn resolve_conflict(
        &mut self,
        local_session: &mut ChatSession,
        remote_session: &ChatSession,
    ) -> Result<SyncResult> {
        let session_id = local_session.session_id.clone().unwrap_or_default();

        let conflict = SyncConflict {
            id: uuid::Uuid::new_v4().to_string(),
            session_id: session_id.clone(),
            local_version: ConflictVersion {
                hash: compute_session_hash(local_session),
                timestamp: DateTime::from_timestamp_millis(local_session.last_message_date)
                    .unwrap_or_else(Utc::now),
                message_count: local_session.requests.len(),
            },
            remote_version: ConflictVersion {
                hash: compute_session_hash(remote_session),
                timestamp: DateTime::from_timestamp_millis(remote_session.last_message_date)
                    .unwrap_or_else(Utc::now),
                message_count: remote_session.requests.len(),
            },
            conflict_type: ConflictType::ConcurrentAdd,
            suggested_strategy: self.config.conflict_strategy,
            created_at: Utc::now(),
            resolved: false,
        };

        match self.config.conflict_strategy {
            ConflictStrategy::LocalWins => {
                self.update_state(&session_id, local_session, local_session);
                Ok(SyncResult::Pushed)
            }
            ConflictStrategy::RemoteWins => {
                *local_session = remote_session.clone();
                self.update_state(&session_id, local_session, remote_session);
                Ok(SyncResult::Pulled)
            }
            ConflictStrategy::MostRecent => {
                if local_session.last_message_date >= remote_session.last_message_date {
                    self.update_state(&session_id, local_session, local_session);
                    Ok(SyncResult::Pushed)
                } else {
                    *local_session = remote_session.clone();
                    self.update_state(&session_id, local_session, remote_session);
                    Ok(SyncResult::Pulled)
                }
            }
            ConflictStrategy::Manual => {
                self.conflicts.push(conflict.clone());
                Ok(SyncResult::ConflictDetected(conflict))
            }
            _ => {
                self.conflicts.push(conflict.clone());
                Ok(SyncResult::ConflictDetected(conflict))
            }
        }
    }

    /// Get all unresolved conflicts
    pub fn get_conflicts(&self) -> &[SyncConflict] {
        &self.conflicts
    }

    /// Resolve a conflict manually
    pub fn resolve_conflict_manually(
        &mut self,
        conflict_id: &str,
        _resolution: ConflictStrategy,
        resolved_session: ChatSession,
    ) -> Result<()> {
        if let Some(conflict) = self.conflicts.iter_mut().find(|c| c.id == conflict_id) {
            conflict.resolved = true;
            let session_id = resolved_session.session_id.clone().unwrap_or_default();
            self.update_state(&session_id, &resolved_session, &resolved_session);
            Ok(())
        } else {
            Err(anyhow!("Conflict not found: {}", conflict_id))
        }
    }

    /// Get sync state for a session
    pub fn get_state(&self, session_id: &str) -> Option<&SessionSyncState> {
        self.state.get(session_id)
    }
}

// =============================================================================
// Provider-specific Sync Adapters
// =============================================================================

/// Trait for provider-specific sync operations
pub trait ProviderSyncAdapter: Send + Sync {
    fn provider_type(&self) -> ProviderType;
    fn push_session(&self, session: &ChatSession) -> Result<()>;
    fn pull_session(&self, session_id: &str) -> Result<Option<ChatSession>>;
    fn list_remote_sessions(&self) -> Result<Vec<String>>;
    fn delete_remote_session(&self, session_id: &str) -> Result<()>;
}

/// VSCode Copilot Chat sync adapter
pub struct VSCodeSyncAdapter {
    workspace_path: PathBuf,
}

impl VSCodeSyncAdapter {
    pub fn new(workspace_path: PathBuf) -> Self {
        Self { workspace_path }
    }

    fn sessions_dir(&self) -> PathBuf {
        self.workspace_path.join("chatSessions")
    }
}

impl ProviderSyncAdapter for VSCodeSyncAdapter {
    fn provider_type(&self) -> ProviderType {
        ProviderType::Copilot
    }

    fn push_session(&self, session: &ChatSession) -> Result<()> {
        let session_id = session.session_id.clone().unwrap_or_default();
        let path = self.sessions_dir().join(format!("{}.json", session_id));
        std::fs::create_dir_all(self.sessions_dir())?;
        let json = serde_json::to_string_pretty(session)?;
        std::fs::write(path, json)?;
        Ok(())
    }

    fn pull_session(&self, session_id: &str) -> Result<Option<ChatSession>> {
        let path = self.sessions_dir().join(format!("{}.json", session_id));
        if !path.exists() {
            return Ok(None);
        }
        let content = std::fs::read_to_string(path)?;
        let session: ChatSession = serde_json::from_str(&content)?;
        Ok(Some(session))
    }

    fn list_remote_sessions(&self) -> Result<Vec<String>> {
        let dir = self.sessions_dir();
        if !dir.exists() {
            return Ok(Vec::new());
        }
        let mut sessions = Vec::new();
        for entry in std::fs::read_dir(dir)? {
            let entry = entry?;
            if let Some(name) = entry.file_name().to_str() {
                if name.ends_with(".json") {
                    sessions.push(name.trim_end_matches(".json").to_string());
                }
            }
        }
        Ok(sessions)
    }

    fn delete_remote_session(&self, session_id: &str) -> Result<()> {
        let path = self.sessions_dir().join(format!("{}.json", session_id));
        if path.exists() {
            std::fs::remove_file(path)?;
        }
        Ok(())
    }
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn test_sync_status() {
        assert_eq!(SyncStatus::Synced, SyncStatus::Synced);
    }

    #[test]
    fn test_config_default() {
        let config = BidirectionalSyncConfig::default();
        assert_eq!(config.auto_sync_interval_secs, 300);
    }

    #[test]
    fn test_engine_creation() {
        let engine = BidirectionalSyncEngine::new(BidirectionalSyncConfig::default());
        assert!(engine.conflicts.is_empty());
    }
}