Repository navigation
refactor(desktop): fork the legacy store import off the first window's path #54317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Te-River
wants to merge
3
commits into
anomalyco:dev
Choose a base branch
from
Te-River:desktop-startup-blocking
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| [test] | ||
| preload = ["./test/preload.ts"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { afterEach, describe, expect, test } from "bun:test" | ||
| import { mkdtempSync, writeFileSync } from "node:fs" | ||
| import { rm } from "node:fs/promises" | ||
| import { tmpdir } from "node:os" | ||
| import path from "node:path" | ||
| import { NodeFileSystem, NodePath } from "@effect/platform-node" | ||
| import { Effect, Layer } from "effect" | ||
| import { WindowSnapshotChannel } from "../../shared/ipc-transport" | ||
| import { electron } from "../../../test/preload" | ||
| import { layer } from "./index" | ||
| import { registerStorageSnapshotHandler } from "./snapshot" | ||
|
|
||
| const roots: string[] = [] | ||
|
|
||
| // Bun's node:sqlite shim can retain file handles on Windows after close; only these owned test directories are removed. | ||
| afterEach(() => Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true }).catch(() => undefined)))) | ||
|
|
||
| function tempRoot() { | ||
| const root = mkdtempSync(path.join(tmpdir(), "opencode-storage-layer-")) | ||
| roots.push(root) | ||
|
|
||
| return root | ||
| } | ||
|
|
||
| registerStorageSnapshotHandler() | ||
|
|
||
| describe("storage layer", () => { | ||
| test("the snapshot handler waits for the legacy import", async () => { | ||
| const root = tempRoot() | ||
| writeFileSync(path.join(root, "opencode.global.dat"), JSON.stringify({ model: "m" })) | ||
| electron.userData = root | ||
|
|
||
| // The preload asks for the namespaces its shell reads before the page runs. The answer must not | ||
| // race the legacy import: the renderer loads a namespace once, so an empty answer is final. | ||
| const result = await Effect.runPromise( | ||
| Effect.gen(function* () { | ||
| yield* Layer.build(layer.pipe(Layer.provide(Layer.merge(NodePath.layer, NodeFileSystem.layer)))) | ||
| const handler = electron.handlers.get(WindowSnapshotChannel) | ||
|
|
||
| if (!handler) throw new Error("the snapshot handler was not registered") | ||
|
|
||
| return yield* Effect.promise(() => | ||
| Promise.resolve(handler({ senderFrame: { url: "oc://renderer/index.html" } }, ["opencode.global.dat"])), | ||
| ) | ||
| }).pipe(Effect.scoped), | ||
| ) | ||
|
|
||
| expect(result).toEqual({ | ||
| storage: { "opencode.global.dat": { items: { model: "m" }, revision: 0 } }, | ||
| extensions: [], | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { mock } from "bun:test" | ||
|
|
||
| // The main process imports Electron everywhere, and outside Electron the real module is only a path | ||
| // string, so tests stub the API surface they touch. `userData` and `handlers` are mutable: a test | ||
| // points the storage layer at its own directory and observes the handlers it registers. | ||
| export const electron = { | ||
| userData: "", | ||
| handlers: new Map<string, (event: unknown, names: unknown) => unknown>(), | ||
| app: { | ||
| getPath: () => electron.userData, | ||
| on: () => {}, | ||
| off: () => {}, | ||
| }, | ||
| BrowserWindow: { getAllWindows: () => [] }, | ||
| protocol: {}, | ||
| ipcMain: { | ||
| handle: (channel: string, handler: (event: unknown, names: unknown) => unknown) => { | ||
| electron.handlers.set(channel, handler) | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| mock.module("electron", () => ({ ...electron, default: electron })) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forking here lets the rest of this layer run before the import commits.
setStorageSnapshotProvideris installed a few lines below, and from then on the snapshot handler no longer goes throughreadWindowSnapshot's "legacy files present → empty storage" guard. The IPC port, and with itStorageItems/StorageUpdate, is also handed out after the layers. So on the first launch with legacy.datfiles:StorageItems/ snapshots can return empty namespaces, and the renderer loads each namespace only once, so it keeps that empty state.StorageUpdatethat flushes before the import's transaction creates rows that the import'sonConflictDoNothingkeeps.importLegacyStoresthen deletes the.datfile, so the user's old value is lost for good.If the import has to leave the layer's critical path, the storage handlers and the snapshot provider need to wait for it, e.g. by awaiting a
Deferredbefore answeringStorageItems/StorageUpdateand before swapping the snapshot provider. Another option is a cheap synchronous check, so the import only blocks when legacy files actually exist. When there are none, which is the common case, the cost is just thereadDirectory.