Imported from gencau/test-practices-agent-configurations (
dataset/repos/benbjohnson§litestream/AGENTS.md). Install upstream withnpx skills add gencau/test-practices-agent-configurations --skill benbjohnson§litestream. Copyright stays with the author.
AGENTS.md - Litestream AI Agent Guide
Litestream is a disaster recovery tool for SQLite that runs as a background process, monitors the WAL, converts changes to immutable LTX files, and replicates them to cloud storage. It uses modernc.org/sqlite (pure Go, no CGO required).
Before You Start
- Read AI_PR_GUIDE.md for contribution requirements
- Check CONTRIBUTING.md for what we accept (bug fixes welcome, features need discussion)
- Review recent PRs for current patterns
Critical Rules
- Lock page at 1GB: SQLite reserves page at 0x40000000. Always skip it. See docs/SQLITE_INTERNALS.md
- LTX files are immutable: Never modify after creation. See docs/LTX_FORMAT.md
- Single replica per database: Each DB replicates to exactly one destination
- Use
litestream ltx: Notlitestream wal(deprecated) - Use
litestream reset: Clears corrupted local LTX state for a database. Seecmd/litestream/reset.go auto-recoverconfig: Replica option that automatically resets local state on LTX errors. Disabled by default. Seereplica.go- Retention enabled by default:
Store.RetentionEnabledistrueby default. Disable only when cloud lifecycle policies handle cleanup. Seestore.go - IPC socket disabled by default: Control socket is off by default. Enable with
socket.enabled: truein config. Seeserver.go $PIDconfig expansion: Config files support$PIDto expand to the current process ID, plus standard$ENV_VARexpansion. Seecmd/litestream/main.golitestream ltx -level: Use-level 0–9or-level allto inspect specific compaction levels. Seecmd/litestream/ltx.go- Return errors, don't log them: Always return errors to callers. Never
log.Printf(err)and continue — this silently hides failures in a disaster recovery tool. Only use DEBUG log for best-effort operations where failure doesn't affect correctness and a valid fallback exists (e.g., reading SHM mxFrame optimization hint). See docs/PATTERNS.md
Layer Boundaries
| Layer | File | Responsibility |
|---|---|---|
| DB | db.go |
Database state, restoration, WAL monitoring, library API (SyncStatus, SyncAndWait, EnsureExists) |
| Replica | replica.go |
Replication mechanics only |
| Storage | **/replica_client.go |
Backend implementations (includes ReplicaClientV3 for v0.3.x restore) |
| IPC | server.go |
Unix socket control API (register/unregister, /txid, pprof) |
| Leasing | leaser.go, s3/leaser.go |
Distributed lease acquisition via conditional writes |
Database state logic belongs in DB layer, not Replica layer.
Quick Reference
Build:
go build -o bin/litestream ./cmd/litestream
go test -race -v ./...
Code quality:
pre-commit run --all-files
Documentation
| Document | When to Read |
|---|---|
| docs/PATTERNS.md | Code patterns and anti-patterns |
| docs/ARCHITECTURE.md | Deep component details |
| docs/SQLITE_INTERNALS.md | WAL format, lock page |
| docs/LTX_FORMAT.md | Replication format |
| docs/TESTING_GUIDE.md | Test strategies |
| docs/REPLICA_CLIENT_GUIDE.md | Adding storage backends |
| docs/PROVIDER_COMPATIBILITY.md | Provider-specific S3/cloud configs |
Checklist
Before submitting changes:
- Read relevant docs above
- Follow patterns in docs/PATTERNS.md
- Test with race detector (
go test -race) - Run
pre-commit run --all-files - For page iteration: test with >1GB databases
- Show investigation evidence in PR (see AI_PR_GUIDE.md)