SQLite Durable Agent State
Design local SQLite state for multi-agent daemons and CLIs that survives brew upgrade, npm reinstall, concurrent writers, and crashes — and recover when it already hasn't.
Use This For
- Picking a canonical SQLite path for a daemon/CLI pair, pinned by one env var, before the first line of storage code is written.
- Choosing journal mode (WAL vs DELETE) and
busy_timeout for a daemon with a CLI, a snapshot exporter, and a web API all touching the same DB.
- Designing idempotent, atomic migrations with a real post-apply verification probe instead of trusting a migration-history "applied" row.
- Reviewing a
pd-fleet.yml, ADR, or storage design doc before it reintroduces a Cellar-path, per-worktree, or per-tool-default DB regression.
- Diagnosing an already-fragmented multi-
.db mess where CLI/snapshot/export tooling see different counts for the same entity.
Do Not Use This For
- General relational schema design, indexing strategy, or query tuning — that's data modeling, not durability engineering.
- Postgres/MySQL server-based durability, where WAL, replication, and connection pooling mean something structurally different.
- Deciding whether SQLite is the right engine versus a client-server DB — this skill assumes SQLite is already the choice for a local, mostly-single-daemon store.
Durability Design Loop
flowchart TD
A[Resolve canonical DB path and conflicting config policy] --> B[Choose journal mode for platform and access topology]
B --> C[Set caller-deadline timeout and explicit busy/stale-snapshot handling]
C --> D[Design idempotent migration and target-state verification]
D --> E[Declare writer serialization, checkpoint, and backup plan]
E --> F[Run static plan audit and inspect its exact version]
F --> G{Plan findings or unresolved assumptions?}
G -->|Yes| H[Revise plan or hold change]
H --> A
G -->|No| I[Still require local migration, contention, crash, and restore tests]
- Name exactly one canonical path, resolved by every reader and writer through the same env var (e.g.
PORT_DADDY_DB). No per-tool fallback default — that is how CLI/snapshot/export tooling end up reading three different files.
- Choose journal mode:
WAL for a single writer with concurrent readers (the common daemon shape); DELETE/TRUNCATE only for a genuinely single-connection tool.
- If using WAL, declare a per-connection busy-handling policy that fits the caller deadline and transaction semantics: a bounded
busy_timeout, safe caller retry, serialized writes, or a justified combination. WAL permits readers with one writer; it does not serialize application-level multi-step operations. Handle SQLITE_BUSY and stale-snapshot cases explicitly.
- Write migrations as idempotent, transaction-wrapped SQL. If a transaction spans attached databases, consult SQLite’s journal-mode atomicity limits: transactions across attached databases are not atomic as a set in WAL mode. Use one database or a durable intent/reconciliation protocol when cross-file consistency is required.
- Attach a post-apply verification probe to every migration that queries the actual target table/column, not the migration-history table. A migration-history update alone does not prove target schema/data changed; inspect the tool behavior and query the target.
- Declare writer topology explicitly:
single-writer, queue, or serialized. More than one writer without a serialization/transaction plan can produce contention or application-level lost-update bugs; demonstrate the exact schedule rather than claiming upserts generally disappear.
- Run
scripts/db_path_audit.mjs on the assembled plan before shipping. Any blocker finding means don't ship yet.
Output Contract
scripts/db_path_audit.mjs returns:
The bundled auditor's result shape and blocker taxonomy are local tool contracts; inspect its source/schema version before relying on a specific code or promotion meaning. Static plan validation is not a test of SQLite concurrency, migration application, backup restoration, or runtime state. Inspect scripts/db_path_audit.mjs and its schema for the exact output fields. Do not infer SQL execution, crash safety, or runtime state from a pass.
Use scripts/db_path_audit.mjs to inspect a db-plan.schema.json-shaped plan. Read its source/schema and inspect output: a static pass is not evidence that a migration ran, concurrency is safe, backup restores, or the daemon uses that path.
Anti-Patterns
Cellar/Version Path As Home
Novice: Lets the DB default resolve inside a Homebrew Cellar, an nvm/pyenv version directory, or node_modules because that's where the running binary already lives.
Expert: Pins the canonical path to a dotfile-home or app-support directory via a single env var, independent of the installed tool's version — so brew upgrade/reinstall never touches it.
Detection: The raw candidate path string (not the self-declared kind field) matches /Cellar/, a version-manager versions/ segment, node_modules/, or a cache/tmp/worktree directory.
Migration History Is Not Migration
Novice: Runs migration repair --status applied (or equivalent) after a migration mismatch and treats the green output as proof the schema changed.
Expert: Runs the migration SQL directly, then queries the actual target table/column to confirm it exists before trusting the history table's "applied" state.
Detection: A migration entry has no postVerify probe, or the probe only re-reads the migrations/history table instead of the target schema object.
WAL Makes Concurrency Free
Novice: "We're in WAL mode, so the daemon and three CLIs can all write whenever they want."
Expert: Declares caller-deadline-bounded waiting, safe transaction retry, or write serialization as appropriate; WAL permits readers alongside one writer but does not remove contention or make multi-step operations atomic.
Detection: writer topology lacks an application-level serialization plan, or the caller’s configured wait/transaction policy does not handle SQLITE_BUSY and fresh-snapshot retries.
References
| File |
Load When |
references/durable-path-and-wal-discipline.md |
Choosing a canonical path, journal mode, timeout, WAL checkpoint, backup, or migration verification. |
references/retry-and-migration-recovery.md |
Handling SQLITE_BUSY, stale snapshots, idempotent retries, or mismatched target/history state. |
references/fragmented-multidb-recovery.md |
The fragmentation already happened: diagnosing scattered .db files and planning a safe consolidation. |
examples/expected-output.md |
Need a finished, filled-in audit report and consolidation note for a realistic scenario. |
templates/output-template.md |
Need a reusable template for a db-plan review or consolidation writeup. |
schemas/db-plan.schema.json |
Need to validate a --input plan's shape before running the script. |
scripts/db_path_audit.mjs |
Need a static candidate-path and plan-shape review. |
agents/openai.yaml |
Need a subagent descriptor for delegated durable-state review. |
Skill Bundle Index
Every file in this skill, and when to open it. Auto-generated; run scripts/index_references.py --fix.
root
CHANGELOG.md — SQLite Durable Agent State — Changelog — - Initial skill creation - Core process defined - Reference files and deterministic db_path_audit.mjs script added
README.md — SQLite Durable Agent State — Procedural guidance and a deterministic auditor for local SQLite state that must survive package upgrades, concurrent writers, and crashes.
agents/
agents/openai.yaml — openai (data/schema)
examples/
examples/expected-output.md — Example Output: SQLite Durable Agent State — A daemon's storage plan is under review before a release.
examples/sample-input.json — sample input (data/schema)
references/
references/durable-path-and-wal-discipline.md — Durable Path Selection And Journaling Discipline — Use this when choosing where a daemon's SQLite file lives, which journal mode it runs in, and how migrations get applied and verified.
references/retry-and-migration-recovery.md — Retry and migration recovery — Use this for bounded SQLITE_BUSY handling, stale-snapshot retries, idempotency, or mismatched migration history/target state.
references/fragmented-multidb-recovery.md — Diagnosing And Consolidating An Already-Fragmented Multi-DB Mess — Use this when the damage is already done: several .db files exist, different tools report different counts for "the same" data, and nobody
schemas/
schemas/db-plan.schema.json — db plan.schema (data/schema)
scripts/
scripts/db_path_audit.mjs
templates/
templates/output-template.md — SQLite Durable State Review — [db name] — - Env pin: [ENV_VAR_NAME] - Canonical path: [path] - Non-canonical/fossil paths found: [path list, or none] | Setting | Value | Ration
SQLite behavior and recovery boundary
busy_timeout is configured per connection and waits up to the requested bound; it may still return SQLITE_BUSY. Select it from the caller deadline and retry contract, not a universal floor. WAL permits concurrent readers with a writer but serializes writers; a stale read transaction that tries to upgrade may need a full transaction restart from fresh state. Keep WAL on a supported same-host filesystem, account for checkpoints, and make backup/copy procedures WAL-aware. Attached-database transactions have a WAL-mode atomicity limitation across the database set. A migration-history row is not target-state truth.
The bundled auditor/schema can evaluate plan shape only. No SQLite stress schedule, backup restore, or migration execution is implied by that static result. Sources: SQLite WAL, transactions, busy timeout, backup API, WAL-reset fix.
- WAL reader/writer schedule and safe retry
- Migration verification and history separation
1---2name: sqlite-durable-agent-state3description: >-4 Design durable local SQLite state for multi-agent developer tooling that survives package5 upgrades, concurrent writers, and crashes: canonical env-pinned paths, WAL/busy_timeout6 discipline, idempotent verified migrations, and safe writer topology. Use when a daemon, CLI,7 or agent fleet needs a persistent local DB, when reviewing a schema/path change before it ships,8 or when diagnosing an already-fragmented multi-.db mess. NOT for relational schema/query design9 (use daemon-development for the daemon's broader lifecycle), server-based Postgres/MySQL10 durability, or deciding whether SQLite is the right engine at all.11license: Apache-2.012allowed-tools: Read,Write,Edit,Bash,Grep,Glob13metadata:14 source: https://skillmd.com/skills/curiositech/sqlite-durable-agent-state15 category: Data & Storage16 tags:17 - sqlite18 - durable-state19 - wal-mode20 - migration-integrity21 - multi-writer-safety22 provenance:23 kind: first-party24 owners:25 - port-daddy26 pairs-with:27 - skill: port-daddy-internal-dev28 reason: Port Daddy's own 7-.db fragmentation (ADR-0090/0044) is the canonical case this skill prevents and recovers from.29 - skill: daemon-development30 reason: The daemon process is usually the single writer that must enforce serialization and PRAGMA discipline.31 - skill: runtime-verification-for-agents32 reason: Post-migration verification must check live schema state, not migration-history metadata.33 io-contract:34 kind: deliverable35 consumes:36 - kind: db-plan37 format: json38 produces:39 - kind: db-audit-report40 format: json41 - kind: consolidation-plan42 format: markdown43---4445# SQLite Durable Agent State4647Design local SQLite state for multi-agent daemons and CLIs that survives `brew upgrade`, npm reinstall, concurrent writers, and crashes — and recover when it already hasn't.4849## Use This For5051- Picking a canonical SQLite path for a daemon/CLI pair, pinned by one env var, before the first line of storage code is written.52- Choosing journal mode (WAL vs DELETE) and `busy_timeout` for a daemon with a CLI, a snapshot exporter, and a web API all touching the same DB.53- Designing idempotent, atomic migrations with a real post-apply verification probe instead of trusting a migration-history "applied" row.54- Reviewing a `pd-fleet.yml`, ADR, or storage design doc before it reintroduces a Cellar-path, per-worktree, or per-tool-default DB regression.55- Diagnosing an already-fragmented multi-`.db` mess where CLI/snapshot/export tooling see different counts for the same entity.5657## Do Not Use This For5859- General relational schema design, indexing strategy, or query tuning — that's data modeling, not durability engineering.60- Postgres/MySQL server-based durability, where WAL, replication, and connection pooling mean something structurally different.61- Deciding whether SQLite is the right engine versus a client-server DB — this skill assumes SQLite is already the choice for a local, mostly-single-daemon store.6263## Durability Design Loop6465```mermaid66flowchart TD67 A[Resolve canonical DB path and conflicting config policy] --> B[Choose journal mode for platform and access topology]68 B --> C[Set caller-deadline timeout and explicit busy/stale-snapshot handling]69 C --> D[Design idempotent migration and target-state verification]70 D --> E[Declare writer serialization, checkpoint, and backup plan]71 E --> F[Run static plan audit and inspect its exact version]72 F --> G{Plan findings or unresolved assumptions?}73 G -->|Yes| H[Revise plan or hold change]74 H --> A75 G -->|No| I[Still require local migration, contention, crash, and restore tests]76```77781. Name exactly one canonical path, resolved by every reader and writer through the same env var (e.g. `PORT_DADDY_DB`). No per-tool fallback default — that is how CLI/snapshot/export tooling end up reading three different files.792. Choose journal mode: `WAL` for a single writer with concurrent readers (the common daemon shape); `DELETE`/`TRUNCATE` only for a genuinely single-connection tool.803. If using WAL, declare a per-connection busy-handling policy that fits the caller deadline and transaction semantics: a bounded `busy_timeout`, safe caller retry, serialized writes, or a justified combination. WAL permits readers with one writer; it does not serialize application-level multi-step operations. Handle `SQLITE_BUSY` and stale-snapshot cases explicitly.814. Write migrations as idempotent, transaction-wrapped SQL. If a transaction spans attached databases, consult SQLite’s journal-mode atomicity limits: transactions across attached databases are not atomic as a set in WAL mode. Use one database or a durable intent/reconciliation protocol when cross-file consistency is required.825. Attach a post-apply verification probe to every migration that queries the actual target table/column, not the migration-history table. A migration-history update alone does not prove target schema/data changed; inspect the tool behavior and query the target.836. Declare writer topology explicitly: `single-writer`, `queue`, or `serialized`. More than one writer without a serialization/transaction plan can produce contention or application-level lost-update bugs; demonstrate the exact schedule rather than claiming upserts generally disappear.847. Run `scripts/db_path_audit.mjs` on the assembled plan before shipping. Any blocker finding means don't ship yet.8586## Output Contract8788`scripts/db_path_audit.mjs` returns:8990The bundled auditor's result shape and blocker taxonomy are local tool contracts; inspect its source/schema version before relying on a specific code or promotion meaning. Static plan validation is not a test of SQLite concurrency, migration application, backup restoration, or runtime state. Inspect `scripts/db_path_audit.mjs` and its schema for the exact output fields. Do not infer SQL execution, crash safety, or runtime state from a pass.9192Use `scripts/db_path_audit.mjs` to inspect a `db-plan.schema.json`-shaped plan. Read its source/schema and inspect output: a static pass is not evidence that a migration ran, concurrency is safe, backup restores, or the daemon uses that path.9394## Anti-Patterns9596### Cellar/Version Path As Home9798**Novice**: Lets the DB default resolve inside a Homebrew Cellar, an `nvm`/`pyenv` version directory, or `node_modules` because that's where the running binary already lives.99**Expert**: Pins the canonical path to a dotfile-home or app-support directory via a single env var, independent of the installed tool's version — so `brew upgrade`/reinstall never touches it.100**Detection**: The raw candidate path string (not the self-declared `kind` field) matches `/Cellar/`, a version-manager `versions/` segment, `node_modules/`, or a cache/tmp/worktree directory.101102### Migration History Is Not Migration103104**Novice**: Runs `migration repair --status applied` (or equivalent) after a migration mismatch and treats the green output as proof the schema changed.105**Expert**: Runs the migration SQL directly, then queries the actual target table/column to confirm it exists before trusting the history table's "applied" state.106**Detection**: A migration entry has no `postVerify` probe, or the probe only re-reads the migrations/history table instead of the target schema object.107108### WAL Makes Concurrency Free109110**Novice**: "We're in WAL mode, so the daemon and three CLIs can all write whenever they want."111**Expert**: Declares caller-deadline-bounded waiting, safe transaction retry, or write serialization as appropriate; WAL permits readers alongside one writer but does not remove contention or make multi-step operations atomic.112**Detection**: writer topology lacks an application-level serialization plan, or the caller’s configured wait/transaction policy does not handle `SQLITE_BUSY` and fresh-snapshot retries.113114## References115116| File | Load When |117| --- | --- |118| `references/durable-path-and-wal-discipline.md` | Choosing a canonical path, journal mode, timeout, WAL checkpoint, backup, or migration verification. |119| `references/retry-and-migration-recovery.md` | Handling SQLITE_BUSY, stale snapshots, idempotent retries, or mismatched target/history state. |120| `references/fragmented-multidb-recovery.md` | The fragmentation already happened: diagnosing scattered `.db` files and planning a safe consolidation. |121| `examples/expected-output.md` | Need a finished, filled-in audit report and consolidation note for a realistic scenario. |122| `templates/output-template.md` | Need a reusable template for a db-plan review or consolidation writeup. |123| `schemas/db-plan.schema.json` | Need to validate a `--input` plan's shape before running the script. |124| `scripts/db_path_audit.mjs` | Need a static candidate-path and plan-shape review. |125| `agents/openai.yaml` | Need a subagent descriptor for delegated durable-state review. |126127<!-- BEGIN BUNDLE INDEX (auto: index_references.py) -->128129## Skill Bundle Index130131*Every file in this skill, and when to open it. Auto-generated; run `scripts/index_references.py --fix`.*132133**root**134- [`CHANGELOG.md`](CHANGELOG.md) — SQLite Durable Agent State — Changelog — - Initial skill creation - Core process defined - Reference files and deterministic db_path_audit.mjs script added135- [`README.md`](README.md) — SQLite Durable Agent State — Procedural guidance and a deterministic auditor for local SQLite state that must survive package upgrades, concurrent writers, and crashes.136137**`agents/`**138- [`agents/openai.yaml`](agents/openai.yaml) — openai (data/schema)139140**`examples/`**141- [`examples/expected-output.md`](examples/expected-output.md) — Example Output: SQLite Durable Agent State — A daemon's storage plan is under review before a release.142- [`examples/sample-input.json`](examples/sample-input.json) — sample input (data/schema)143144**`references/`**145- [`references/durable-path-and-wal-discipline.md`](references/durable-path-and-wal-discipline.md) — Durable Path Selection And Journaling Discipline — Use this when choosing where a daemon's SQLite file lives, which journal mode it runs in, and how migrations get applied and verified.146- [`references/retry-and-migration-recovery.md`](references/retry-and-migration-recovery.md) — Retry and migration recovery — Use this for bounded SQLITE_BUSY handling, stale-snapshot retries, idempotency, or mismatched migration history/target state.147- [`references/fragmented-multidb-recovery.md`](references/fragmented-multidb-recovery.md) — Diagnosing And Consolidating An Already-Fragmented Multi-DB Mess — Use this when the damage is already done: several `.db` files exist, different tools report different counts for "the same" data, and nobody148149**`schemas/`**150- [`schemas/db-plan.schema.json`](schemas/db-plan.schema.json) — db plan.schema (data/schema)151152**`scripts/`**153- [`scripts/db_path_audit.mjs`](scripts/db_path_audit.mjs)154155**`templates/`**156- [`templates/output-template.md`](templates/output-template.md) — SQLite Durable State Review — [db name] — - Env pin: `[ENV_VAR_NAME]` - Canonical path: `[path]` - Non-canonical/fossil paths found: `[path list, or none]` | Setting | Value | Ration157158<!-- END BUNDLE INDEX -->159160161## SQLite behavior and recovery boundary162163`busy_timeout` is configured per connection and waits up to the requested bound; it may still return `SQLITE_BUSY`. Select it from the caller deadline and retry contract, not a universal floor. WAL permits concurrent readers with a writer but serializes writers; a stale read transaction that tries to upgrade may need a full transaction restart from fresh state. Keep WAL on a supported same-host filesystem, account for checkpoints, and make backup/copy procedures WAL-aware. Attached-database transactions have a WAL-mode atomicity limitation across the database set. A migration-history row is not target-state truth.164165The bundled auditor/schema can evaluate plan shape only. No SQLite stress schedule, backup restore, or migration execution is implied by that static result. Sources: [SQLite WAL](https://www.sqlite.org/wal.html), [transactions](https://www.sqlite.org/lang_transaction.html), [busy timeout](https://www.sqlite.org/c3ref/busy_timeout.html), [backup API](https://www.sqlite.org/backup.html), [WAL-reset fix](https://www.sqlite.org/forum/forumpost/8dc0d0055e).166167- [WAL reader/writer schedule and safe retry](diagrams/research-sql01-wal-reader-writer-schedule-and-safe-retry.md)168- [Migration verification and history separation](diagrams/research-sql02-migration-verification-and-history-separation.md)169170