Store conversation history in a database
By default, the Bub app persists conversation Tapes as local JSONL files under ~/.bub/tapes/.
By installing bub-tapestore-sqlalchemy, you can switch seamlessly to SQLite, PostgreSQL, or any SQLAlchemy-supported relational database while keeping ,tape.info, ,tape.search, forks, and sidecars unchanged.
1. Install the plugin
Section titled “1. Install the plugin”Install the store plugin in the environment where Bub runs:
bub install bub-tapestore-sqlalchemy@mainbub hooksVerify tapestore-sqlalchemy appears before builtin under provide_tape_store:
provide_tape_store: tapestore-sqlalchemy, builtinHigher-priority implementations appear first, so seeing tapestore-sqlalchemy confirms the plugin is active and overrides the built-in file store.
2. Configure the database URL
Section titled “2. Configure the database URL”The plugin defaults to <BUB_HOME>/tapes.db. Set BUB_TAPESTORE_SQLALCHEMY_URL to any SQLAlchemy connection URL (using a local SQLite file here as an example):
export BUB_TAPESTORE_SQLALCHEMY_URL="sqlite+pysqlite:///$PWD/bub-tapes.db"Optional settings:
| Environment variable | Default | Description |
|---|---|---|
BUB_TAPESTORE_SQLALCHEMY_ECHO |
false |
Set true to log executed SQL statements |
BUB_TAPESTORE_SQLALCHEMY_CONNECT_ARGS |
{} |
JSON object passed to create_engine(connect_args=...) (e.g. timeouts) |
To persist these settings across sessions, add them to your workspace .env file. Tables are created automatically on first connection.
3. Verify writes and reads
Section titled “3. Verify writes and reads”Run a turn and inspect the Tape:
bub run "Say hello in one sentence to verify database storage."bub run ",tape.info"Example output:
name: 86774b31b96845a4__0b871d5e50e7c192entries: 9anchors: 1last_anchor: session/startentries_since_last_anchor: 8Inspect the SQLite tables directly with sqlite3:
sqlite3 "$PWD/bub-tapes.db" "SELECT name, last_entry_id FROM tapes ORDER BY name;"sqlite3 "$PWD/bub-tapes.db" "SELECT entry_id, anchor_name, entry_date FROM tape_entries WHERE kind = 'anchor';"The tapes table tracks session metadata, while tape_entries records each Turn, model reply, and tool call entry by entry.
4. Connect PostgreSQL or MySQL
Section titled “4. Connect PostgreSQL or MySQL”- Install the database driver in Bub’s environment (PostgreSQL example):
Terminal window uv add "psycopg[binary]" - Set the connection URL:
Terminal window export BUB_TAPESTORE_SQLALCHEMY_URL="postgresql+psycopg://user:YOUR_DB_PASSWORD@localhost:5432/bub_db"
In production, test concurrent writes and connection-pool timeouts ahead of time to keep the service stable.
Implement a custom store backend
Section titled “Implement a custom store backend”core.Agent and the built-in app depend only on the store protocol, so you can back Tapes with Redis or another service by implementing it (for the difference between Tape history and context projection, see Tape and model context):
| Protocol / Hook | Where it is used |
|---|---|
bub.core.store.TapeStore |
Synchronous store passed to Agent(store=...) |
bub.core.store.AsyncTapeStore |
Asynchronous store (recommended for databases and network I/O) |
provide_tape_store |
App-layer plugin hook replacing the default FuzzyFileTapeStore |
See Python API: TapeStore for method signatures and contracts. Forks are built on top of any store by ForkTapeStore, so custom backends do not need to implement branching themselves.
Store resolution in the built-in app
Section titled “Store resolution in the built-in app”The built-in app provides FuzzyFileTapeStore (~/.bub/tapes/) via provide_tape_store and hashes the workspace path with the session ID so same-named sessions in different workspaces remain separate.
BubAgent selects its store in this order:
- An explicit
tape_storepassed toBubAgent(...); framework.get_tape_store()at instantiation time (must be insideasync with framework.running():);InMemoryTapeStore.
The built-in app restores session-level settings—such as model, reasoning effort, and code mode—from the Tape automatically; persist any other custom state explicitly via load_state and save_state.
Troubleshooting
Section titled “Troubleshooting”| Symptom | Possible cause | Fix |
|---|---|---|
tapestore-sqlalchemy missing from bub hooks |
Installed in a different Python environment | Check command -v bub and run bub install in that environment |
SQLite Database is locked |
High multi-process write concurrency | Increase timeout in BUB_TAPESTORE_SQLALCHEMY_CONNECT_ARGS or switch to PostgreSQL |
ModuleNotFoundError |
Missing database driver | Install the driver in Bub’s environment (e.g. uv add "psycopg[binary]") |
Cleanup
Section titled “Cleanup”To switch back to Bub’s default local file store, clear the environment variables and remove the test database:
unset BUB_TAPESTORE_SQLALCHEMY_URL BUB_TAPESTORE_SQLALCHEMY_ECHO BUB_TAPESTORE_SQLALCHEMY_CONNECT_ARGSrm -f "$PWD/bub-tapes.db"