Schema
The cache uses four tables: one for file versions, one for per-session read pointers, and two for statistics.How it fits together
file_versionsis a content-addressed store. The composite primary key(path, hash)means the same file can have multiple versions stored simultaneously. When a file changes and then changes back (e.g. a branch switch), the old version is already cached.session_readstracks which version of each file a given session last saw. This is keyed by(session_id, path)so each session independently tracks its own read state.statsandsession_statstrack cumulative token savings globally and per session.
Connecting
Storing a file version
When a file is read for the first time (or has changed), store its content keyed by a hash.INSERT OR IGNORE makes this a no-op if the same version already exists:
Tracking reads per session
Each session needs to know what it last saw for a given file. On first read, record the session’s read pointer:Tracking token savings
Use an atomic counter for global statistics and an upsert for per-session tracking:Querying statistics
Cleanup
When a file is deleted from disk, remove all its versions and read pointers:Key design points
- Content-addressed storage means branch switches are handled naturally. If you switch from
mainto a feature branch and back, the original file versions are still in the cache. - Session isolation ensures that each consumer independently tracks what it has seen. Session B gets full file content even if Session A already cached it.
- Hash-based change detection at read time means there is no polling or file watching required for correctness.
- An embedded Turso database keeps everything in a single file with zero network overhead.