Expand description
JSONL sync operations.
This module provides git-friendly synchronization via JSONL files:
- Export: Dirty records → JSONL files (incremental or full)
- Import: JSONL files → SQLite with merge strategies
- Hashing: SHA256 content hashing for change detection
- Status: View pending exports and file statistics
§Architecture
The sync system uses a dirty tracking pattern:
- SQLite triggers mark records as “dirty” on INSERT/UPDATE
- Export reads dirty records, writes to JSONL, clears dirty flags
- Import reads JSONL, applies merge strategy, upserts records
§File Format
Each JSONL file contains one record per line with a type tag:
{"type":"session","id":"sess_123","name":"My Session",...,"content_hash":"abc","exported_at":"2025-01-20T10:00:00Z"}§Example
ⓘ
use savecontext::sync::{Exporter, Importer, MergeStrategy, status};
// Export dirty records
let mut exporter = Exporter::new(&mut storage, output_dir);
let stats = exporter.export(false)?; // incremental
// Import with conflict resolution
let mut importer = Importer::new(&mut storage, MergeStrategy::PreferNewer);
let stats = importer.import_all(&input_dir)?;
// Check sync status
let status = status::get_sync_status(&storage, &export_dir)?;Structs§
- Checkpoint
Record - Checkpoint with sync metadata.
- Context
Item Record - Context item with sync metadata.
- Deletion
Record - A deletion record for sync.
- Entity
Stats - Per-entity statistics for import operations.
- Export
File Info - Information about an export file.
- Export
Stats - Statistics for an export operation.
- Exporter
- Exporter for JSONL sync files.
- Import
Stats - Statistics for an import operation.
- Importer
- Importer for JSONL sync files.
- Issue
Record - Issue with sync metadata.
- Memory
Record - Memory with sync metadata.
- Plan
Record - Plan with sync metadata.
- Session
Record - Session with sync metadata.
- Sync
Status - Sync status information.
Enums§
- Entity
Type - Entity types for deletion tracking.
- Merge
Strategy - Conflict resolution strategy for imports.
- Sync
Error - Sync-specific errors.
- Sync
Record - Tagged union for JSONL records.
Functions§
- append_
jsonl - Append a sync record to a JSONL file.
- atomic_
write - Write content to a file atomically.
- content_
hash - Compute a SHA256 hash of a serializable value.
- count_
lines - Count the number of lines in a JSONL file.
- default_
export_ dir - Get the default export directory for a database.
- ensure_
gitignore - Ensure .gitignore exists in the export directory.
- file_
size - Get the size of a file in bytes.
- get_
sync_ status - Get the current sync status for a project.
- gitignore_
content - Generate .gitignore content for the .savecontext directory.
- has_
changed - Check if an entity has changed since last export.
- print_
status - Print sync status to stdout in a human-readable format.
- project_
export_ dir - Get the export directory for a project.
- read_
jsonl - Read all sync records from a JSONL file.
- write_
jsonl - Write multiple sync records to a JSONL file atomically.
Type Aliases§
- Sync
Result - Result type for sync operations.