From da233f0a9674c91fce37d9263176fd302182830a Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Sat, 25 Jul 2026 23:21:34 -0700 Subject: [PATCH] Break the state-dir tie on mtime, not a fixed preference MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Preferring `userdata` whenever both directories exist was wrong for the case that motivated resolving at all: an implicit `bun run dev` stores state in `dev`, so a base directory that had also been used with an explicit `--home-dir` would seed the database the running server does not open — looking like the seed did nothing. Neither directory is categorically right, so the tie now goes to whichever was written most recently: the one the last server actually used. `userdata` still wins an equal or unavailable mtime. The chosen path is already printed, so a wrong guess is visible rather than silent. Verified both directions against a target holding both databases. Caught by Cursor Bugbot. Co-Authored-By: Claude Opus 5 (1M context) --- scripts/dev-seed.ts | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/scripts/dev-seed.ts b/scripts/dev-seed.ts index 988b9c8cae..ae2debb79e 100644 --- a/scripts/dev-seed.ts +++ b/scripts/dev-seed.ts @@ -81,18 +81,40 @@ class DevSeedTargetError extends Schema.TaggedErrorClass()( * the seed reported "no database, start the dev server so migrations run" about * a server that was already running. * - * Whichever one exists wins; `userdata` breaks the tie, since that is what an - * explicit `--home-dir` produces. + * When both exist, neither is categorically right — a worktree run gets + * `userdata` (the dev runner passes an explicit home), a main-checkout run gets + * `dev` — so the tie goes to whichever was written most recently, i.e. the one + * the last server to run actually opened. The chosen path is printed, so a + * wrong guess is visible rather than silent. */ const resolveStateDir = Effect.fn("devSeed.resolveStateDir")(function* (baseDir: string) { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; + + const candidates: Array<{ readonly stateDir: string; readonly modifiedAtMs: number }> = []; for (const stateDir of ["userdata", "dev"] as const) { - if (yield* fileSystem.exists(path.join(baseDir, stateDir, "state.sqlite"))) { - return stateDir; + const info = yield* fileSystem + .stat(path.join(baseDir, stateDir, "state.sqlite")) + .pipe(Effect.option); + if (Option.isSome(info)) { + candidates.push({ + stateDir, + modifiedAtMs: Option.match(info.value.mtime, { + onSome: (mtime) => mtime.getTime(), + onNone: () => 0, + }), + }); } } - return "userdata"; + + // `userdata` first above, so an equal or missing mtime keeps the old default. + return ( + candidates.reduce<{ readonly stateDir: string; readonly modifiedAtMs: number } | undefined>( + (best, candidate) => + best === undefined || candidate.modifiedAtMs > best.modifiedAtMs ? candidate : best, + undefined, + )?.stateDir ?? "userdata" + ); }); const stateDbPath = (path: Path.Path, baseDir: string, stateDir: string) =>