tx2-pack
Binary world snapshot format for ECS persistence, checkpointing, and time-travel replay.
tx2-pack is the storage layer of the TX-2 ecosystem, providing efficient serialization of Entity-Component-System worlds to disk with compression, encryption, and time-travel capabilities. It enables save/load, replay, and session persistence without requiring a traditional database.
Features
Efficient Storage Format
- Struct-of-arrays layout - Cache-friendly memory organization
- Multiple serialization formats - Bincode (fast) or MessagePack (compact)
- Component archetype grouping - Entities organized by component types
- Versioned format - Magic number + version for compatibility checks
Compression
- Zstd compression - Best compression ratio (configurable levels 1-19)
- LZ4 compression - Fast compression/decompression
- No compression - Raw storage for maximum speed
- Transparent operation - Compression handled automatically
Encryption
- AES-256-GCM - Authenticated encryption with galois counter mode
- Key management - Generate or provide encryption keys
- Optional per-snapshot - Enable encryption as needed
- Secure storage - Protect sensitive world data
Checkpointing
- Checkpoint manager - Save/load/delete checkpoints by ID
- Parent tracking - Checkpoint chains for history
- Metadata support - Tags, descriptions, custom fields
- Pruning - Keep only N most recent checkpoints
- Chain traversal - Navigate checkpoint history
Replay & Time-Travel
- Replay engine - Step forward/backward through checkpoints
- Loop support - Wrap around at start/end
- Seek operations - Jump to specific checkpoint
- Time-travel - Store snapshots at specific timestamps
- Time-based queries - Find snapshot closest to target time
- Forking - Clone world state at any point in time
- Pruning - Remove snapshots before/after timestamp
Data Integrity
- SHA-256 checksums - Verify data integrity on load
- Header validation - Detect corrupted or incompatible files
- Version checking - Ensure format compatibility
Quick Start
Saving and Loading Snapshots
use ;
// Create a snapshot from your world
let snapshot = from_world_snapshot;
// Write to file with compression
let writer = new
.with_compression;
writer.write_to_file?;
// Read back
let reader = new;
let loaded = reader.read_from_file?;
Checkpoints
use ;
// Create checkpoint manager
let mut manager = new?;
// Save checkpoint
let snapshot = from_world_snapshot;
let metadata = new
.with_name
.with_description
.with_tag;
manager.create_checkpoint?;
// Load checkpoint
let checkpoint = manager.load_checkpoint?;
world.restore_from_snapshot?;
// List all checkpoints
let checkpoints = manager.list_checkpoints?;
// Delete old checkpoints
manager.prune_old_checkpoints?; // Keep only 5 most recent
Replay Engine
use ;
// Create replay engine
let mut replay = new;
// Load checkpoints from manager
replay.load_from_manager?;
// Navigate through checkpoints
replay.next; // Move forward
replay.previous; // Move backward
replay.seek?; // Jump to index 5
replay.seek_to_start; // Jump to beginning
replay.seek_to_end; // Jump to end
// Get current checkpoint
if let Some = replay.current
// Enable looping
let mut replay = new.with_loop;
Time-Travel
use TimeTravel;
// Create time-travel system
let mut tt = new;
// Record snapshots at specific times
for t in 0..100
// Seek to specific time
if let Some = tt.seek_to_time
// Fork from a specific time
if let Some = tt.fork_at_time
// Prune old snapshots
tt.prune_before; // Remove snapshots before t=20
tt.prune_after; // Remove snapshots after t=80
Encryption
use ;
// Generate encryption key
let key = generate;
// Write encrypted snapshot
let writer = new
.with_compression
.with_encryption;
writer.write_to_file?;
// Read encrypted snapshot
let reader = new
.with_encryption;
let loaded = reader.read_from_file?;
Architecture
File Format
[Header][Data]
Header (bincode-serialized):
Data (compressed, optionally encrypted):
Component Archetype
Components are stored in struct-of-arrays layout:
Example SoA layout for Position component:
component_id: "Position"
entity_ids: [1, 2, 3, 4, 5]
field_names: ["x", "y", "z"]
field_types: [F32, F32, F32]
field_data: [
[10.0, 20.0, 30.0, 40.0, 50.0], // x values
[15.0, 25.0, 35.0, 45.0, 55.0], // y values
[5.0, 10.0, 15.0, 20.0, 25.0], // z values
]
Benefits:
- Cache-friendly iteration
- SIMD-friendly operations
- Efficient compression (similar values together)
Compression Performance
Tested with 10,000 entities containing Position, Velocity, and Health components:
| Codec | Compressed Size | Compression Ratio | Write Time | Read Time |
|---|---|---|---|---|
| None | 1,200 KB | 1.0× | 5ms | 4ms |
| LZ4 | 450 KB | 2.7× | 8ms | 6ms |
| Zstd (level 3) | 180 KB | 6.7× | 15ms | 10ms |
| Zstd (level 19) | 120 KB | 10.0× | 150ms | 12ms |
Storage Operations
SnapshotStore
Manages multiple snapshots in a directory:
use ;
// Create store
let store = new?;
// Save snapshot with metadata
let writer = new;
let metadata = new;
store.save?;
// Files created:
// - ./snapshots/save-001.tx2pack
// - ./snapshots/save-001.meta.json
// Load snapshot
let reader = new;
let = store.load?;
// List all snapshots
let ids = store.list?;
// Delete snapshot
store.delete?;
Metadata
Usage:
let metadata = new
.with_name
.with_description
.with_tag
.with_tag
.with_custom_field
.with_custom_field;
Use Cases
Game Save/Load
// Save game
let snapshot = game_world.create_snapshot;
let metadata = new
.with_name
.with_tag;
manager.create_checkpoint?;
// Load game
let checkpoint = manager.load_checkpoint?;
game_world.restore_from_snapshot?;
Replay System
// Record gameplay every 5 seconds
loop
// Replay from any time
let start_time = 120.0;
if let Some = tt.seek_to_time
Session Persistence (Agent IDEs)
// Save session on exit
let snapshot = agent_world.create_snapshot;
let metadata = new
.with_tag
.with_custom_field;
store.save?;
// Restore session on launch
if let Ok = store.load
Time-Travel Debugging
// Record checkpoints during simulation
for tick in 0..1000
// Jump to problematic tick
if let Some = tt.seek_to_time
Integration with TX-2 Ecosystem
tx2-pack works with the broader TX-2 stack:
- tx2-core (Rust): Uses tx2-pack for save/load operations
- tx2-link: Shares WorldSnapshot format for network sync
- tx2-ecs (TypeScript): Can load tx2-pack snapshots via WASM
Relationship to tx2-link
tx2-pack and tx2-link both work with world snapshots but serve different purposes:
- tx2-link: Network synchronization with delta compression
- tx2-pack: Disk persistence with compression and encryption
Both use the same WorldSnapshot structure from tx2-link.
Running Tests
All 14 tests should pass, covering:
- Write/read snapshots with checksum validation
- Compression (Zstd, LZ4, None)
- Encryption (AES-256-GCM)
- Checkpoint management
- Replay engine navigation
- Time-travel queries
Running Benchmarks
Benchmarks measure:
- Write performance (compression formats, entity counts)
- Read performance (decompression overhead)
- Roundtrip time (write + read)
- Compression ratios
- Encryption overhead
Error Handling
All operations return Result<T, PackError> for proper error handling.
Development Status
- Binary snapshot format with SoA layout
- Versioned serialization (Bincode, MessagePack)
- Compression (Zstd, LZ4, None)
- AES-256-GCM encryption
- SHA-256 checksums
- Checkpoint management
- Replay engine
- Time-travel system
- Metadata with tags
- SnapshotStore for multi-file management
- Comprehensive tests
- Benchmarks
- Incremental snapshots (only changed archetypes)
- Snapshot diffs for version control
- Streaming read/write for large worlds
Dependencies
tx2-link- Shared snapshot formatserde- Serialization frameworkbincode- Fast binary serializationrmp-serde- MessagePack formatzstd- Zstd compressionlz4- LZ4 compressionsha2- SHA-256 checksumsaes-gcm- AES-256-GCM encryptionchrono- Timestamp handlingahash- Fast hashing
License
MIT
Contributing
Contributions are welcome! This is part of the broader TX-2 project for building isomorphic applications with a unified world model.