Skip to main content

sc/sync/
mod.rs

1//! JSONL sync operations.
2//!
3//! This module provides git-friendly synchronization via JSONL files:
4//!
5//! - **Export**: Dirty records → JSONL files (incremental or full)
6//! - **Import**: JSONL files → SQLite with merge strategies
7//! - **Hashing**: SHA256 content hashing for change detection
8//! - **Status**: View pending exports and file statistics
9//!
10//! # Architecture
11//!
12//! The sync system uses a dirty tracking pattern:
13//! 1. SQLite triggers mark records as "dirty" on INSERT/UPDATE
14//! 2. Export reads dirty records, writes to JSONL, clears dirty flags
15//! 3. Import reads JSONL, applies merge strategy, upserts records
16//!
17//! # File Format
18//!
19//! Each JSONL file contains one record per line with a `type` tag:
20//! ```json
21//! {"type":"session","id":"sess_123","name":"My Session",...,"content_hash":"abc","exported_at":"2025-01-20T10:00:00Z"}
22//! ```
23//!
24//! # Example
25//!
26//! ```ignore
27//! use savecontext::sync::{Exporter, Importer, MergeStrategy, status};
28//!
29//! // Export dirty records
30//! let mut exporter = Exporter::new(&mut storage, output_dir);
31//! let stats = exporter.export(false)?;  // incremental
32//!
33//! // Import with conflict resolution
34//! let mut importer = Importer::new(&mut storage, MergeStrategy::PreferNewer);
35//! let stats = importer.import_all(&input_dir)?;
36//!
37//! // Check sync status
38//! let status = status::get_sync_status(&storage, &export_dir)?;
39//! ```
40
41mod export;
42mod file;
43mod hash;
44mod import;
45mod status;
46mod types;
47
48// Re-export main types and functions
49pub use export::{default_export_dir, project_export_dir, Exporter};
50pub use file::{
51    append_jsonl, atomic_write, count_lines, ensure_gitignore, file_size, gitignore_content,
52    read_jsonl, write_jsonl,
53};
54pub use hash::{content_hash, has_changed};
55pub use import::Importer;
56pub use status::{get_sync_status, print_status};
57pub use types::{
58    CheckpointRecord, ContextItemRecord, DeletionRecord, EntityStats, EntityType, ExportFileInfo,
59    ExportStats, ImportStats, IssueRecord, MemoryRecord, MergeStrategy, PlanRecord, SessionRecord,
60    SyncError, SyncRecord, SyncResult, SyncStatus,
61};