bonsai

Sessions

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.

Where sessions live

$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.

Lifecycle

Resume

Peer sessions

Multiple bonsai processes can work in one project root concurrently:

Budgets

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.

The usage ledger and /usage

Every 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.

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.

Where this lives in the code

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