Skip to content

fix(desktop): keep drafts storage out of the CLI database - #64203

Merged
neriousy merged 1 commit into
devfrom
drafts-db-isolation
Nov 16, 2026
Merged

neriousy merged 1 commit into
devfrom
drafts-db-isolation

Conversation

@neriousy

@neriousy neriousy commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Issue for this PR

Part of #63913 (the desktop half). The core 20261025123711_blob migration still needs its own fix.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When drafts.sqlite is a symlink to (or a copy of) the CLI's opencode.db, the desktop ran its own migrations against the CLI database. Both apps define a blob table and both keep their migration list in a table called migration, so they can't share one file:

  • the old drafts store already wrote blob(id, data) and document into the CLI database, so the core blob migration fails with table blob already exists
  • the desktop only adopts a pre-journal drafts.sqlite when migration is empty, and here it holds the CLI's rows, so 20260906234153_drafts fails the same way and the app quits on start

Changes:

  • migrateDatabase throws before writing anything when the file has a session or session_v2 table. That covers the early window snapshot read, which already falls back to empty state on errors
  • openDatabase renames that drafts.sqlite to drafts.sqlite.cli-<timestamp> and opens a fresh one. Renaming only moves the directory entry, so a symlink's target (the CLI database) isn't touched
  • the storage layer logs a warning with the new path

The few drafts the old desktop wrote into the CLI database stay there and aren't carried over.

No flag, it's a fix.

How did you verify your code works?

  • new test in database.test.ts that runs once with a symlink and once with a copy: it sets up a CLI-like database, runs the window snapshot read and openDatabase, then checks the desktop got its own tables and the CLI database still has exactly its original tables and migration rows. The symlink case is skipped on Windows
  • checked that the test fails without each half: without the guard in migrateDatabase, desktop tables land in the CLI database; without the rename, openDatabase throws
  • bun test src/main/storage (62 pass), bun typecheck, bun run check

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #63913

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. On the base, a drafts.sqlite symlinked to a CLI-like opencode.db ends up with blob, document, state and plugin written into the CLI database. With this change the file is moved aside to drafts.sqlite.cli-<ts>, the desktop gets a fresh database, and the CLI database keeps exactly its original tables and migration rows. bun test src/main/storage passes (62 tests). The typecheck script doesn't run on the installed Bun 1.4.2 ("Script not found"), but tsc -b in packages/desktop passes. I didn't launch the Electron app.

Public API is unchanged. Everything touched is internal to the desktop main process. On disk, a drafts.sqlite that holds CLI tables is now renamed and replaced with a fresh file.

No blocking concerns. No desktop migration creates a session table, so the check can't misfire on a real drafts database. A WAL file left behind after the rename is harmless, because SQLite discards a WAL next to an empty database file.

Related: #63913

@neriousy
neriousy merged commit 4c3e17b into dev Nov 16, 2026
13 checks passed
@neriousy
neriousy deleted the drafts-db-isolation branch November 16, 2026 11:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant