Resolve the database file itself; missing-target outranks same-source

Two Cursor findings on the previous guard fix:

- `canonicalDbPath` resolved only the directory and re-joined the
  filename, so a symlink at the *file* — `state.sqlite` itself linking
  into the shared home — passed the guard. Reproduced: the seed replaced
  the shared database through the link. Resolve the full path first (the
  file can be the link), falling back to directory-resolution only when
  the file does not exist yet, which is the one case a full resolution
  cannot handle and the one where the missing-target advice would create
  the database in the shared home.
- With --from and --to aliasing the same base and no database behind
  them, same-source fired before the existence check, hiding the
  actionable "start the dev server" guidance. The existence check now
  runs between the shared-home refusal (which must stay first — its
  advice is exactly wrong for the shared home) and same-source.

Verified seven ways: symlinked file refused with the shared rows
untouched, symlinked directory still refused, shared home with and
without a database refused as shared-home, aliased-empty reported as
missing-target, aliased-with-database as same-source, and a legitimate
seed unaffected.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
Theo BrowneandClaude Opus 5 committed 2026-07-26 20:39:13 -07:00
1 parent aad1bdd49a
commit b769bb6455
1 file changed
+22 -14
+22 -14
View File
@@ -245,14 +245,19 @@ const devSeedCli = Command.make("dev-seed", {
// base can canonicalize to itself while its `userdata` or `dev`
// subdirectory is a symlink into the shared home, and comparing bases
// then waves through a seed whose actual write target is the real
// database. The directory is canonicalized and the filename re-joined
// because the database may not exist yet — realPath fails on a missing
// final component, and falling back to the literal path would lose the
// symlink resolution in exactly the case where the next error's advice
// ("start the dev server") would create the database in the shared home.
// database. Resolve the full path first — the file itself can be the
// symlink — and fall back to resolving the directory and re-joining the
// filename when the file does not exist yet: a nonexistent file cannot
// be a link to anything, but its directory still can be, and this is
// exactly the case where the missing-target advice ("start the dev
// server") would otherwise create the database in the shared home.
const canonicalDbPath = (databasePath: string) =>
fileSystem.realPath(path.dirname(databasePath)).pipe(
Effect.map((directory) => path.join(directory, path.basename(databasePath))),
fileSystem.realPath(databasePath).pipe(
Effect.catch(() =>
fileSystem
.realPath(path.dirname(databasePath))
.pipe(Effect.map((directory) => path.join(directory, path.basename(databasePath)))),
),
Effect.orElseSucceed(() => databasePath),
);
const [canonicalTargetDb, canonicalSourceDb, canonicalShared] = yield* Effect.all([
@@ -263,6 +268,16 @@ const devSeedCli = Command.make("dev-seed", {
if (canonicalTargetDb.startsWith(`${canonicalShared}${path.sep}`)) {
return yield* new DevSeedTargetError({ reason: "shared-home", detail: canonicalTargetDb });
}
// After the shared-home refusal: that one must come first because the
// missing-target advice below — start a dev server, which creates the
// database — is exactly wrong for the shared home. Everywhere else, a
// missing database is the more actionable diagnosis, so it outranks
// same-source: aliased paths with nothing behind them are a setup
// problem, not a self-seed.
if (!(yield* fileSystem.exists(targetDbPath))) {
return yield* new DevSeedTargetError({ reason: "missing-target", detail: targetDbPath });
}
// Seeding a database from itself is not the data loss it looks like —
// the source reads a pre-transaction snapshot, so the copy puts the rows
// back. What does not come back is the event log and the cursors, and
@@ -271,13 +286,6 @@ const devSeedCli = Command.make("dev-seed", {
return yield* new DevSeedTargetError({ reason: "same-source", detail: canonicalTargetDb });
}
// After the refusals: this error's advice is to start a dev server, which
// creates the database — the right next step everywhere the guards above
// did not already rule the target out.
if (!(yield* fileSystem.exists(targetDbPath))) {
return yield* new DevSeedTargetError({ reason: "missing-target", detail: targetDbPath });
}
// SQLite locking is not enough on its own: the seed's transaction can take
// the write lock between a running server's own writes and commit cleanly.
// What it cannot do is tell that server the projections it is holding in