packages/opencode/src/storage/db.tsRemove all production usages of the legacy packages/opencode/src/storage/db.ts module.
This means eliminating imports from @/storage/db or ./storage/db, including:
Database.use(...)Database.transaction(...)Database.effect(...)Database.Client()Database.getPath()Database.TxOrDb / Database.Transaction@/storage/db, such as eqThis does not mean removing SQLite or Drizzle everywhere in one step. The smaller target is deleting the opencode legacy wrapper by moving call sites onto deeper modules or onto the core/effect database adapter directly.
Production imports from packages/opencode/src/storage/db.ts are concentrated in 21 source files:
packages/opencode/src/account/repo.tspackages/opencode/src/cli/cmd/db.tspackages/opencode/src/cli/cmd/import.tspackages/opencode/src/cli/cmd/stats.tspackages/opencode/src/control-plane/workspace.tspackages/opencode/src/index.tspackages/opencode/src/node.tspackages/opencode/src/permission/index.tspackages/opencode/src/project/project.tspackages/opencode/src/server/projectors.tspackages/opencode/src/server/routes/instance/httpapi/handlers/sync.tspackages/opencode/src/server/shared/fence.tspackages/opencode/src/session/message-v2.tspackages/opencode/src/session/projectors.tspackages/opencode/src/session/prompt.tspackages/opencode/src/session/session.tspackages/opencode/src/share/share-next.tspackages/opencode/src/storage/db.tspackages/opencode/src/sync/index.tspackages/opencode/src/worktree/index.tsThere are 63 direct API/type references in those files. The references fall into the groups below.
Status: Completed. Startup, the public node export, and database CLI tooling no longer import the legacy opencode database wrapper; packages/opencode/src/storage/db.ts has been deleted.
Files:
packages/opencode/src/storage/db.tspackages/opencode/src/index.tspackages/opencode/src/node.tspackages/opencode/src/cli/cmd/db.tsCurrent usage:
storage/db.ts opens the singleton database, applies pragmas, exposes callback-style access, holds ambient transaction context, and queues post-commit effects.index.ts no longer performs the removed JSON-to-SQLite migration during startup.node.ts publicly re-exports Database from the legacy module.cli/cmd/db.ts uses Database.getPath() to print the path, open a readonly Bun SQLite handle, run sqlite3, and vacuum.Why this group comes first:
storage/db.ts requires an explicit replacement for database path, client acquisition, migration startup, and close/finalization.Target shape:
Database.Client() with an Effect-provided database service or a narrow startup-only adapter.node.ts re-export with either no export or a stable non-legacy database capability.cli/cmd/db.ts as an admin/raw SQLite tool, but make it ask the replacement database path provider instead of importing @/storage/db.Status: Completed. SyncEvent and the opencode projector boundary were removed; session/message event projection now lives in core EventV2/projector infrastructure.
Files:
packages/opencode/src/sync/index.tspackages/opencode/src/session/projectors.tspackages/opencode/src/server/projectors.tsCurrent usage:
SyncEvent.run uses Database.transaction(..., { behavior: "immediate" }) to allocate event sequence numbers safely.SyncEvent.process wraps projector execution, event sequence writes, event log writes, and post-commit publishing in Database.transaction(...).Database.effect(...) queues publish side effects until after the transaction commits.Database.TxOrDb so they can write through either a root client or the active transaction.Why this group is critical:
Database.use inside a transaction must see the active transaction, and Database.effect must not publish until commit.Target shape:
Database.TxOrDb with an explicit projector transaction type from the replacement database adapter.Suggested first step:
SyncEvent.project(...) and projector type signatures to that module. Keep the implementation backed by the new database adapter until all projector users are moved.Status: Completed. These services no longer import the legacy opencode database wrapper.
Files:
packages/opencode/src/account/repo.tspackages/opencode/src/project/project.tspackages/opencode/src/control-plane/workspace.tspackages/opencode/src/share/share-next.tsCurrent usage:
Database.use with local db(...) helpers or Effect.try.account/repo.ts uses both Database.use and Database.transaction through a repository interface.project/project.ts has the largest mixed usage: Effect service methods use a local db(...) helper, while legacy top-level functions still call Database.use directly.control-plane/workspace.ts and share/share-next.ts have local Effect wrappers around Database.use.Why this group is tractable:
Target shape:
Suggested order:
account/repo.ts; it has a clear repository interface and few call sites.share/share-next.ts and control-plane/workspace.ts local wrappers.project/project.ts for last in this group because it mixes project resolution, VCS, global bus emission, migration, and legacy top-level helpers.Status: Completed. Session/message reads and projector writes have moved off the legacy opencode database wrapper.
Files:
packages/opencode/src/session/session.tspackages/opencode/src/session/message-v2.tspackages/opencode/src/session/prompt.tspackages/opencode/src/session/projectors.tsCurrent usage:
session/session.ts uses Database.use for session reads, list queries, children, part lookup, and global list helpers.session/message-v2.ts uses Database.use to page messages, hydrate parts, fetch one message, and fetch parts.session/prompt.ts imports eq from @/storage/db and reads current prompt-related session/message rows directly.session/projectors.ts uses TxOrDb for session/message usage projection helpers.Why this group should be split:
Target shape:
get, list, page, parts, and prompt assembly reads.session/projectors.ts only after Group 2 defines the replacement projector transaction type.Suggested order:
session/message-v2.ts reads first because the module already centralizes message pagination and hydration.session/session.ts read helpers next.session/prompt.ts after message/session reads exist, and import drizzle operators from drizzle-orm if any direct SQL remains temporarily.Status: Completed. Remaining one-off CLI/admin reads and writes now use core database services or domain services instead of the legacy opencode database wrapper.
Files:
packages/opencode/src/cli/cmd/import.tspackages/opencode/src/cli/cmd/stats.tspackages/opencode/src/server/shared/fence.tspackages/opencode/src/server/routes/instance/httpapi/handlers/sync.tspackages/opencode/src/worktree/index.tspackages/opencode/src/permission/index.tsCurrent usage:
cli/cmd/import.ts writes imported sessions/messages/parts directly with Database.use.cli/cmd/stats.ts reads all sessions directly.server/shared/fence.ts queries sessions for fence context.handlers/sync.ts reads event rows for HTTP sync endpoints.worktree/index.ts looks up a project row for worktree behavior.permission/index.ts reads permission rows directly.Why this group is mostly cleanup:
Target shape:
All migration groups are complete or superseded. packages/opencode/src/storage/db.ts has been deleted.
Status: Superseded. No opencode data-migration group remains.
The previous opencode data-migration.ts service only backfilled session usage from message rows. That work is now covered by core database migration packages/core/src/database/migration/20260510033149_session_usage.ts, so there is no separate opencode data-migration group.
SyncEvent.run sequence allocation must keep immediate transaction behavior.packages/core/src/**/*.sql.ts; do not move table definitions back into packages/opencode.rg "@/storage/db|./storage/db|Database\.(use|transaction|effect|Client|getPath)|\bTxOrDb\b|\bTransaction\b" packages/opencode/srcbun typecheck from packages/opencodepackages/opencode, not the repo root