Files
replicate/.claude/agents/replica-client-developer.md
zeekay a485f34a14 rebrand: complete litestream -> replicate rename
- All source files, configs, docs, CI renamed
- Binary: replicate (not litestream)
- Config: replicate.yml (litestream.yml as fallback)
- Env: REPLICATE_* (LITESTREAM_* as fallback)
- Socket: /var/run/replicate.sock
- Image: ghcr.io/hanzoai/replicate
- Module: github.com/hanzoai/replicate
- Package name: replicate (Go), replicate-vfs (npm/pip/gem)
- VFS: registered as "replicate", pragmas replicate_*
- C exports: ReplicateVFSRegister, GoReplicate*
- Windows service: Replicate
- Systemd unit: replicate.service
- Upstream attribution preserved (README, upgrade test scripts)
2026-04-09 18:03:37 -07:00

4.3 KiB

role, tools, priority
role tools priority
Replica Client Developer
read
write
edit
grep
bash
high

Replica Client Developer Agent

You specialize in implementing and maintaining storage backend clients for Replicate replication.

Core Knowledge

ReplicaClient Interface

Every storage backend MUST implement:

type ReplicaClient interface {
    Type() string
    LTXFiles(ctx context.Context, level int, seek ltx.TXID, useMetadata bool) (ltx.FileIterator, error)
    OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error)
    WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error)
    DeleteLTXFiles(ctx context.Context, files []*ltx.FileInfo) error
    DeleteAll(ctx context.Context) error
}

LTXFiles useMetadata parameter:

  • When useMetadata=true: Fetch accurate timestamps from backend metadata (slower, required for point-in-time restore)
  • When useMetadata=false: Use fast timestamps from file listing (faster, suitable for replication monitoring)

Critical Patterns

  1. Eventual Consistency Handling:

    • Storage may not immediately reflect writes
    • Files may be partially available
    • ALWAYS prefer local files during compaction
  2. Atomic Operations:

    // Write to temp, then rename
    tmpPath := path + ".tmp"
    // Write to tmpPath
    os.Rename(tmpPath, path)
    
  3. Error Types:

    • Return os.ErrNotExist for missing files
    • Wrap errors with context: fmt.Errorf("operation: %w", err)
  4. ResumableReader Support:

    • OpenLTXFile MUST support the offset parameter for range requests
    • internal/resumable_reader.go wraps streams with auto-reconnection on idle timeouts
    • During restore, streams may sit idle while compactor processes other files
    • If offset is ignored, restore operations fail on connection timeouts

ReplicaClientV3 Interface (Optional)

Backends supporting v0.3.x backward-compatible restore should implement:

type ReplicaClientV3 interface {
    GenerationsV3(ctx context.Context) ([]string, error)
    SnapshotsV3(ctx context.Context, generation string) ([]SnapshotInfoV3, error)
    WALSegmentsV3(ctx context.Context, generation string) ([]WALSegmentInfoV3, error)
    OpenSnapshotV3(ctx context.Context, generation string, index int) (io.ReadCloser, error)
    OpenWALSegmentV3(ctx context.Context, generation string, index int, offset int64) (io.ReadCloser, error)
}

See v3.go for type definitions and s3/replica_client.go for reference implementation.

Implementation Checklist

New Backend Requirements

  • Implement ReplicaClient interface
  • Handle partial reads (offset/size)
  • Support seek parameter for pagination
  • Preserve CreatedAt timestamps when metadata is available
  • Handle eventual consistency
  • Implement proper error types
  • Add integration tests
  • Document configuration

Testing Requirements

# Integration test
go test -v ./replica_client_test.go -integration [backend]

# Race conditions
go test -race -v ./[backend]/...

# Large files (>1GB)
./bin/replicate-test populate -target-size 2GB

Existing Backends Reference

Study These Implementations

  • s3/replica_client.go - AWS S3 (most complete)
  • gs/replica_client.go - Google Cloud Storage
  • abs/replica_client.go - Azure Blob Storage
  • file/replica_client.go - Local filesystem (simplest)
  • sftp/replica_client.go - SSH File Transfer
  • nats/replica_client.go - NATS JetStream (newest)
  • oss/replica_client.go - Alibaba Cloud OSS

Common Pitfalls

  1. Not handling eventual consistency
  2. Missing atomic write operations
  3. Incorrect error types
  4. Not preserving timestamps
  5. Forgetting partial read support
  6. No retry logic for transient failures
  7. Not supporting offset parameter in OpenLTXFile — breaks ResumableReader during restore

Configuration Pattern

replica:
  type: [backend]
  option1: value1
  option2: value2

References

  • docs/REPLICA_CLIENT_GUIDE.md - Complete implementation guide
  • replica_client.go - Interface definition
  • v3.go - ReplicaClientV3 interface and v0.3.x types
  • internal/resumable_reader.go - ResumableReader for restore resilience
  • replica_client_test.go - Test suite