Every conversation is a durable session: transcript, model-facing context, tool calls, usage ledger, plan, todos, and episode state persist in SQLite and survive restarts. This page covers the session lifecycle, persistence, resume, concurrent sessions, and budgets.
$BONSAI_HOME/bonsai.db (default ~/.bonsai/bonsai.db) — SQLite in WAL
mode, permissions hardened (0700 home, 0600 database). Ordered
migrations run at open; before applying pending migrations to an existing
database, bonsai snapshots it to $BONSAI_HOME/backups/ and a failed
migration restores the snapshot automatically. See
Compatibility for the upgrade/rollback contract.
What a session snapshot holds: the rendered transcript, the model-facing
context messages (resume-authoritative), tool calls with args/results/diffs,
compaction and episode records, read evidence, the per-turn usage ledger,
verification and self-review runs, plan and todos, and a full-text-searchable
copy of plain messages (powering /search and recall’s query mode).
Secrets never enter the database — credentials are stored by
reference only.
/resume ordering stays content-recency-based. An
active-run timer accumulates foreground execution time for the
session-time budget; a crashed segment is recovered only up to its last
heartbeat.set_session_title (also an
episode boundary hint); otherwise the first
user prompt seeds a derived title./quit, /clear, or /new complete the session.
/clear//new rotate to a fresh row (new cache key, cleared episodes and
plan canvas); crash leftovers are promoted to interrupted at next boot,
excluding rows with a fresh heartbeat (that’s what makes concurrent
sessions safe). Terminal reasons — including typed budget exhaustion — are
recorded on the row, not reported as generic failures.bonsai -c resumes the latest session for the project; bonsai -c <id>
a specific one. In the TUI, /sessions lists, /resume [id] resumes,
/forget <id> deletes, /search <query> searches message history.BONSAI_PROVIDER on resume; --model can still
override for that run), and restores its conversation cache key so the
provider-side prompt cache stays warm.Multiple bonsai processes can work in one project root concurrently:
peers tool lists live peers, sends bounded,
redacted, rate-limited messages (≤6,000 chars, ≤30/hour), registers a
one-shot wake-when-done on another session (mutual waits are refused;
auto-wakes are hop- and rate-limited to prevent loops), and takes advisory
claims on work areas. Message delivery uses durable per-consumer
leases, so a crash never drops or double-delivers a message./peers shows a live read-only view. Per-session changed-file records
attribute concurrent edits correctly (the self-review pass subtracts
peer-attributed paths from its diff).All budgets are off by default; enable presets in /settings or pass
headless flags (flags override saved values for that run).
Per-run (reset each run): max turns, run time, generation (per
provider attempt), output (streamed chars per attempt), tool time (per
tool call — a stalled call is cancelled without killing the run).
Per-session (cumulative across resumes, consuming the persisted ledger —
including output from failed provider retries): session turns,
session output, session time (active run time), session cost.
max_turns, session_cost, provider_rate_limit, …) in
SQLite, and — in headless JSON — emits status: "budget_exhausted" with a
typed budget_exhaustion object. See Headless mode./usageEvery provider response is one ledger turn: lane (parent / subagent / self-review / compaction), tokens (prompt, completion, cache read/write), cost and a no-cache baseline cost (their difference is the cache saving), finish reason, latency and time-to-first-token, cache-route telemetry (expected vs actual cache-read percent, prefix hashes, rewrite events), and inspection counters. Subagent turns keep their own lane and per-turn model identity while folding into session totals.
/usage — the cross-project analytics dashboard (activity, models, cost,
sessions, tools, cache tabs plus a heatmap)./perf (alias /cost) — the current session’s performance/usage report./ctx — where the context window itself is going; see
Context management.Cost math is honest about unknowns: turns without pricing flag the session’s cost as inexact, and dashboard totals are floors, with unknown-cost turns counted separately.
| Concern | Location |
|---|---|
| Database, migrations, backups | src/storage/mod.rs, migrations/ |
| Session rows & resume | src/storage/sessions.rs, src/tui/run/persistence.rs |
| Snapshot writer | src/session_persist.rs |
| Peers & workspace leases | src/peer.rs, src/storage/peers.rs, src/storage/workspace_locks.rs |
| Budgets | src/run_budget.rs |
| Usage ledger & dashboard | src/agent/usage_ledger.rs, src/storage/usage_stats.rs |