Part of #23.
Background
No persistent store exists. F2 (keys), F3 (events), and F4 (key_id resolution) need shared SQLite. Bun ships bun:sqlite built-in — zero new runtime dependency.
Goal
A bun:sqlite database at ~/.local/share/copilot-api/copilot-api.db with WAL, a migration runner using PRAGMA user_version, and typed query helpers.
Tasks
Acceptance criteria
bun run start on empty data dir creates the DB with journal_mode=wal (verify via sqlite3 .dbinfo)
- Adding a new migration file is the only step needed for schema changes
- File mode is
0600 for all SQLite files
- HTTP listener does not bind until migrations complete
File pointers
- New:
src/lib/db.ts, src/lib/migrations/001_init.sql, tests/db.test.ts
- Touch:
src/lib/paths.ts (add dbPath()), src/start.ts (wire migration sync)
Dependencies
Blocks F2.B, F3.A.
Part of #23.
Background
No persistent store exists. F2 (keys), F3 (events), and F4 (key_id resolution) need shared SQLite. Bun ships
bun:sqlitebuilt-in — zero new runtime dependency.Goal
A
bun:sqlitedatabase at~/.local/share/copilot-api/copilot-api.dbwith WAL, a migration runner usingPRAGMA user_version, and typed query helpers.Tasks
src/lib/db.ts: open db, set PRAGMAs OUTSIDE any transaction (per backend review B3 —PRAGMA journal_modeis a no-op insideBEGIN):PRAGMA journal_mode=WALPRAGMA synchronous=NORMALPRAGMA foreign_keys=ONuser_version, apply pendingmigrations/NNN_*.sqlinside its own transaction, bumpuser_versionin same transaction; commit001_init.sqlis empty (placeholder); subsequent migrations come from F2.B and F3.Aumask 077before opening DB on first creation;chmod 0600on*.db,*.db-wal,*.db-shmafter creation; verify on startup and warn loudly if perms are wider (security hardening per S12).db+.db-wal+.db-shmtogether, OR useVACUUM INTO/sqlite3 .backupfor consistent snapshots (per backend review previous_response_id stateful chain support #21)Acceptance criteria
bun run starton empty data dir creates the DB withjournal_mode=wal(verify viasqlite3 .dbinfo)0600for all SQLite filesFile pointers
src/lib/db.ts,src/lib/migrations/001_init.sql,tests/db.test.tssrc/lib/paths.ts(adddbPath()),src/start.ts(wire migration sync)Dependencies
Blocks F2.B, F3.A.