Environment:1broseidon Ketch Bbolt Storage
| Knowledge Sources | |
|---|---|
| Domains | Infrastructure, Cache, Storage |
| Last Updated | 2026-10-27 00:00 GMT |
Overview
Two on-disk bbolt databases in the user config/cache directories — a TTL page cache (`cache.db`) and a separate durable tag index (`tags.db`) — opened per-transaction so concurrent CLI, crawl, and MCP server processes never lock each other out.
Description
This environment is the persistent state for ketch's scrape and crawl surfaces. It is two separate bbolt files:
- Page cache (`cache.db` under `$XDG_CACHE_HOME/ketch/`): TTL-bounded cached page bodies and crawl link metadata. Writes sweep expired pages at most hourly; `ketch cache clear` deletes the file without touching bookmarks.
- Tag index (`tags.db` under `$XDG_CONFIG_HOME/ketch/`, overridable by `KETCH_TAGS_PATH`): durable bookmarks that outlive the cache (ADR-0004). An expired cache body leaves a durable bookmark; a missing cache entry returns `cached: false` with `cache_status: unavailable`.
Both files are opened for exactly one transaction and closed immediately after (ADR-0006). A long-running process (MCP server, background crawl) holds no bbolt handle between calls, so it can never lock the CLI or another server out. An in-process `sync.Map` of `RWMutex` per path serialises transactions within one process; across processes, bbolt's own file lock (polled every 50ms) coordinates, bounded by a configurable timeout.
No daemon, no queue — the databases are file-based and stateless between calls.
Usage
Use this environment whenever a scrape, crawl, or tag operation should persist across invocations. It is the prerequisite for `--tag` bookmarking, the `ketch tag` command, crawl link-caching, and the page cache that `CachedScrape` serves from. A fresh install creates the files lazily on first write; no setup step is required.
Labs and test environments should override both the page cache path and `KETCH_TAGS_PATH` so an isolated run doesn't collide with the operator's real cache or bookmarks.
System Requirements
| Category | Requirement | Notes |
|---|---|---|
| OS | Linux / macOS / Windows | bbolt is pure Go; no filesystem feature beyond a writable dir |
| Hardware | Minimal | Key-value store; size grows with cached pages and bookmark count |
| Disk | Writable config + cache dirs | `cache.db` in cache dir, `tags.db` in config dir; 0o600/0o700 permissions |
Dependencies
Go Modules
- `go.etcd.io/bbolt` v1.4.3
Filesystem
- `$XDG_CACHE_HOME/ketch/cache.db` (or platform equivalent via `os.UserCacheDir`) — page cache
- `$XDG_CONFIG_HOME/ketch/tags.db` (or `os.UserConfigDir`, overridable by `KETCH_TAGS_PATH`) — tag index
Credentials
No credentials. The files are created with mode `0o600` and their parent directories `0o700`; `tightenDBPermissions` enforces this on every open. Page bodies and bookmark metadata may contain scraped content but no ketch API keys — those live only in `config.json` (also `0o600`).
Quick Install
# No install step — files are created lazily on first write.
# Verify the cache and tag index exist and see stats:
ketch cache
# Override paths for an isolated/test run:
export KETCH_CACHE_DIR=/tmp/ketch-lab/cache # page cache dir (if supported)
export KETCH_TAGS_PATH=/tmp/ketch-lab/tags.db # tag index file
# Clear the page cache only (bookmarks are untouched):
ketch cache clear
Code Evidence
Per-transaction open from `cache/boltfile.go:14-20`:
// bbolt allows one process at a time to hold a database file, and holds it
// for as long as the handle stays open. Every ketch store therefore opens its
// file for exactly one transaction and closes it straight after: a
// long-running process (the MCP server, a background crawl) holds nothing
// between calls, so it can never lock the CLI or another server out.
Two-file separation from `cache/bbolt.go:94`:
return &BBoltStore{path: path, readOnly: readOnly,
tags: &tagDB{path: filepath.Join(filepath.Dir(path), "tags.db")}}
Permission hardening from `cache/boltfile.go:63-66`:
if err := tightenDBPermissions(path); err != nil {
return err
}
db, err := bolt.Open(path, 0o600, &bolt.Options{ReadOnly: !write, Timeout: timeout})
Tag path override (test fixture) from `cache/tag_store_test.go:24`:
native, err := os.UserConfigDir()
// ... expects filepath.Join(native, "ketch", "tags.db") by default
t.Setenv("KETCH_TAGS_PATH", override) // overrides the default
Common Errors
| Error Message | Cause | Solution |
|---|---|---|
| `open cache db: timeout` | Another process holds the file lock longer than the timeout | Stop the other ketch process; per-op handles make this rare, but a crashed process can leave a lock briefly |
| `cache db path is not a regular file` | A directory or symlink occupies the path | Remove the offending entry; ketch expects a regular file |
| `cache_status: unavailable` on a tagged URL | The cache body expired/was cleared but the bookmark remains | Re-scrape the URL; the bookmark is durable by design (ADR-0004) |
| `prepare tag index: ... permission denied` | Parent dir not writable or permissions drifted | Check `0o700` on the config dir and `0o600` on `tags.db` |
Compatibility Notes
- Two files, on purpose: `cache clear` deletes `cache.db` only; `tags.db` is separate so bookmarks survive a cache wipe (ADR-0004). Never assume clearing the cache drops bookmarks.
- No long-held handles: a bbolt file is opened for one transaction and closed at once (ADR-0006). The MCP server, background crawls, and the CLI share the cache without lock contention because no handle is held idle.
- Cross-process locking: bbolt polls its file lock every 50ms; the in-process `pathLocks` `sync.Map` turns that poll into a direct hand-off for same-process handles. Readers share, writers are exclusive.
- Labs must override both paths: override the page cache dir and `KETCH_TAGS_PATH` together; an isolated run that touches only one will still read/write the operator's other file.
- Lazy creation: a read of a non-existent file returns nil (empty store); a write creates the file, its `0o700` parent dir, and the buckets.
Related Pages
- Implementation:1broseidon_Ketch_scrape_CachedScrape
- Implementation:1broseidon_Ketch_cache_NewTagIndex
- Implementation:1broseidon_Ketch_cache_TagPage_TagURL_TagResult
- Implementation:1broseidon_Ketch_cache_ShowTag_TagList
- Implementation:1broseidon_Ketch_cache_RemoveTag_RemoveTagged
- Implementation:1broseidon_Ketch_crawl_Crawl
- Implementation:1broseidon_Ketch_mcp_NewServer
- Implementation:1broseidon_Ketch_mcp_Server_Close