Skip to main content

Module sync

Module sync 

Source
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:

  1. SQLite triggers mark records as “dirty” on INSERT/UPDATE
  2. Export reads dirty records, writes to JSONL, clears dirty flags
  3. 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§

CheckpointRecord
Checkpoint with sync metadata.
ContextItemRecord
Context item with sync metadata.
DeletionRecord
A deletion record for sync.
EntityStats
Per-entity statistics for import operations.
ExportFileInfo
Information about an export file.
ExportStats
Statistics for an export operation.
Exporter
Exporter for JSONL sync files.
ImportStats
Statistics for an import operation.
Importer
Importer for JSONL sync files.
IssueRecord
Issue with sync metadata.
MemoryRecord
Memory with sync metadata.
PlanRecord
Plan with sync metadata.
SessionRecord
Session with sync metadata.
SyncStatus
Sync status information.

Enums§

EntityType
Entity types for deletion tracking.
MergeStrategy
Conflict resolution strategy for imports.
SyncError
Sync-specific errors.
SyncRecord
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§

SyncResult
Result type for sync operations.