perf(app): serialize persisted stores on a schedule instead of per setter call (#47705)

This commit is contained in:
Luke Parker
2026-09-07 13:01:52 +10:00
committed by GitHub
Unverified
parent dc46ecfc55
commit 44513bd0b0
11 changed files with 348 additions and 53 deletions
+1
View File
@@ -8,5 +8,6 @@ export { ServerConnection, useServers } from "./runtime/server/registry"
export { useTabs } from "./shell/tabs/tabs"
export { createDraftStore } from "./runtime/persistence/drafts"
export { createNamespaceStorage, type NamespaceStorage } from "./runtime/persistence/namespace"
export { flushPersisted } from "./runtime/persistence/persist"
export { useWslServers } from "./servers/wsl/context"
export { type UpdaterPlatform, type UpdaterState } from "./shell/updates/types"
@@ -49,6 +49,19 @@ migration rules remain explicit in their schemas.
invalid entries individually. Valid entries still pass through their codecs.
- Recovery is not a substitute for an explicit historical shape transformation.
## Writes
The setter returned by `persisted()` only marks the store dirty (`persist.ts`). The store is
serialized once per save window (`persistSaveDelay`), on owner cleanup, and when the page
hides, and the write is skipped when the serialized form did not change. Reactive observers
therefore see every mutation immediately and a burst of setter calls costs one encode. Call
`flushPersisted()` when a test or a shutdown path needs the write to have happened; the
desktop platform calls it before flushing its namespaces on shutdown. A real unsaved local
change wins over a value arriving from another window, and over a stored value that finishes
loading after the user already edited. A remote value that arrives while the store is dirty
is held until the save runs; if the local setter calls turned out not to change the
serialized form, the remote value is adopted instead of being lost.
## Namespaces
On desktop, `platform.storage(name)` returns a `NamespaceStorage` (`namespace.ts`): the
@@ -0,0 +1,156 @@
import { describe, expect, test } from "bun:test"
import type { AsyncStorage, PersistenceSyncAPI, PersistenceSyncCallback, SyncStorage } from "@solid-primitives/storage"
import { createRoot } from "solid-js"
import { createStore } from "solid-js/store"
import { flushPersisted, persistStore } from "./persist"
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
type State = { count: number; label: string }
function setup(input: { initial?: string | null | Promise<string | null>; delay?: number; sync?: PersistenceSyncAPI }) {
const writes: string[] = []
return createRoot((dispose) => {
const [store, setStore] = createStore<State>({ count: 0, label: "" })
const persist = persistStore({
store,
setStore,
name: "state",
// One fixture serves both the sync and the async storage shape.
storage: {
getItem: () => input.initial ?? null,
setItem: (_key: string, value: string) => {
writes.push(value)
},
removeItem: () => {},
} as SyncStorage | AsyncStorage,
serialize: JSON.stringify,
deserialize: JSON.parse,
sync: input.sync,
delay: input.delay ?? 10,
})
return { store, set: persist.setStore, persist, writes, dispose }
})
}
describe("persistStore", () => {
test("marks the store dirty on set and writes once after the delay", async () => {
const value = setup({})
value.set("count", 1)
value.set("count", 2)
value.set("label", "a")
expect(value.store.count).toBe(2)
expect(value.writes).toEqual([])
await wait(30)
expect(value.writes).toEqual([JSON.stringify({ count: 2, label: "a" })])
value.dispose()
})
test("skips the write when the serialized value did not change", () => {
const value = setup({ delay: 10_000 })
value.set("count", 1)
value.persist.flush()
value.set("count", 1)
value.persist.flush()
expect(value.writes).toHaveLength(1)
value.dispose()
})
test("hydrates synchronously from sync storage without writing back", () => {
const value = setup({ initial: JSON.stringify({ count: 5, label: "saved" }), delay: 10_000 })
expect(value.store).toEqual({ count: 5, label: "saved" })
value.persist.flush()
expect(value.writes).toEqual([])
value.dispose()
})
test("a set made while async storage loads wins over the loaded value", async () => {
const loading = Promise.withResolvers<string | null>()
const value = setup({ initial: loading.promise, delay: 10_000 })
value.set("count", 9)
loading.resolve(JSON.stringify({ count: 1, label: "old" }))
await loading.promise
expect(value.store.count).toBe(9)
value.dispose()
})
test("applies another window's value when clean and ignores it while dirty", () => {
const listeners: PersistenceSyncCallback[] = []
const sent: string[] = []
const value = setup({
delay: 10_000,
sync: [(subscriber) => listeners.push(subscriber), (_key, next) => sent.push(String(next))],
})
listeners[0]!({ key: "state", newValue: JSON.stringify({ count: 3, label: "remote" }), timeStamp: 0 })
expect(value.store).toEqual({ count: 3, label: "remote" })
value.set("label", "local")
listeners[0]!({ key: "state", newValue: JSON.stringify({ count: 4, label: "remote-2" }), timeStamp: 0 })
expect(value.store).toEqual({ count: 3, label: "local" })
value.persist.flush()
expect(sent).toEqual([JSON.stringify({ count: 3, label: "local" })])
value.dispose()
})
test("a remote value arriving during a no-op local set is adopted when the save finds no change", () => {
const listeners: PersistenceSyncCallback[] = []
const sent: string[] = []
const value = setup({
delay: 10_000,
sync: [(subscriber) => listeners.push(subscriber), (_key, next) => sent.push(String(next))],
})
value.set("count", 1)
value.persist.flush()
// Setting the same value again marks the store dirty without changing it.
value.set("count", 1)
listeners[0]!({ key: "state", newValue: JSON.stringify({ count: 1, label: "remote" }), timeStamp: 0 })
expect(value.store.label).toBe("")
value.persist.flush()
expect(value.store).toEqual({ count: 1, label: "remote" })
expect(value.writes).toHaveLength(1)
expect(sent).toHaveLength(1)
// A later save must not consider the adopted value a local change.
value.set("count", 1)
value.persist.flush()
expect(value.writes).toHaveLength(1)
value.dispose()
})
test("a remote revert to the saved value during a no-op local set clears an earlier held change", () => {
const listeners: PersistenceSyncCallback[] = []
const value = setup({ delay: 10_000, sync: [(subscriber) => listeners.push(subscriber), () => {}] })
value.set("label", "saved")
value.persist.flush()
value.set("label", "saved")
listeners[0]!({ key: "state", newValue: JSON.stringify({ count: 0, label: "changed" }), timeStamp: 0 })
listeners[0]!({ key: "state", newValue: JSON.stringify({ count: 0, label: "saved" }), timeStamp: 0 })
value.persist.flush()
expect(value.store).toEqual({ count: 0, label: "saved" })
expect(value.writes).toHaveLength(1)
value.dispose()
})
test("a remote value arriving during a real local change is dropped in favour of the local one", () => {
const listeners: PersistenceSyncCallback[] = []
const value = setup({ delay: 10_000, sync: [(subscriber) => listeners.push(subscriber), () => {}] })
value.set("count", 1)
listeners[0]!({ key: "state", newValue: JSON.stringify({ count: 9, label: "remote" }), timeStamp: 0 })
value.persist.flush()
expect(value.store).toEqual({ count: 1, label: "" })
expect(value.writes).toEqual([JSON.stringify({ count: 1, label: "" })])
value.dispose()
})
test("disposing the owner and flushPersisted both save pending changes", () => {
const first = setup({ delay: 10_000 })
first.set("count", 1)
first.dispose()
expect(first.writes).toHaveLength(1)
const second = setup({ delay: 10_000 })
second.set("count", 2)
flushPersisted()
expect(second.writes).toEqual([JSON.stringify({ count: 2, label: "" })])
second.dispose()
expect(second.writes).toHaveLength(1)
})
})
@@ -0,0 +1,100 @@
import type { AsyncStorage, PersistenceSyncAPI, SyncStorage } from "@solid-primitives/storage"
import { getOwner, onCleanup, untrack } from "solid-js"
import { reconcile, type SetStoreFunction, type Store } from "solid-js/store"
export const persistSaveDelay = 100
const pending = new Set<() => void>()
/** Serialize and write every store with unsaved changes now. */
export function flushPersisted() {
for (const save of [...pending]) save()
}
// Covers synchronous web storage. Desktop registers its own pagehide handling earlier than this
// module loads, so its shutdown path calls flushPersisted() itself before flushing namespaces.
if (typeof document !== "undefined") {
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "hidden") flushPersisted()
})
window.addEventListener("pagehide", flushPersisted)
}
// A store whose serialized form is written to storage on a schedule instead of on every setter
// call. The setter only marks the store dirty; serialization happens once per save window, once
// per owner cleanup, and when the page hides. Mirrors VS Code's Memento.
export function persistStore<T extends object>(input: {
store: Store<T>
setStore: SetStoreFunction<T>
name: string
storage: SyncStorage | AsyncStorage
serialize: (value: T) => string
deserialize: (raw: string) => T
sync?: PersistenceSyncAPI
delay?: number
}) {
const delay = input.delay ?? persistSaveDelay
let dirty = false
let touched = false
let last: string | undefined
// The newest value another window wrote while this store was dirty; applied at save time if the
// local setter calls turned out not to change anything.
let remote: string | undefined
let timer: ReturnType<typeof setTimeout> | undefined
const save = () => {
clearTimeout(timer)
timer = undefined
pending.delete(save)
if (!dirty) return
dirty = false
const held = remote
remote = undefined
const next = untrack(() => input.serialize(input.store))
if (next === last) {
if (held !== undefined && held !== last) hydrate(held)
return
}
last = next
input.sync?.[1](input.name, next)
void input.storage.setItem(input.name, next)
}
// Solid's setter overloads are too deep to spread generically; the wrapper only forwards.
const apply = input.setStore as unknown as (...values: unknown[]) => void
const setStore = ((...values: unknown[]) => {
apply(...values)
dirty = true
touched = true
pending.add(save)
timer ??= setTimeout(save, delay)
}) as unknown as SetStoreFunction<T>
const hydrate = (raw: string) => {
last = raw
input.setStore(reconcile(input.deserialize(raw)))
}
const init = input.storage.getItem(input.name)
// A value the user already changed is newer than whatever storage held.
if (init instanceof Promise) void init.then((raw) => raw && !touched && hydrate(raw))
else if (init) hydrate(init)
input.sync?.[0]((data) => {
if (data.key !== input.name || (data.url ?? location.href) !== location.href) return
if (!data.newValue) return
// A real unsaved local change wins over another window's write, as in VS Code's storage
// service; whether the change is real is only known when the store is serialized. Every
// remote value replaces the held one, including a revert to `last`, so the save sees the
// other window's final state rather than an intermediate one.
if (dirty) {
remote = data.newValue
return
}
if (data.newValue === last) return
hydrate(data.newValue)
})
if (getOwner()) onCleanup(save)
return { setStore, init, flush: save }
}
@@ -1,14 +1,15 @@
import { Platform, usePlatform } from "@/runtime/platform/platform"
import { makePersisted, messageSync, type AsyncStorage, type SyncStorage } from "@solid-primitives/storage"
import { messageSync, type AsyncStorage, type SyncStorage } from "@solid-primitives/storage"
import { checksum } from "@opencode-ai/util/encode"
import { createResource, onCleanup, type Accessor } from "solid-js"
import { createStore, type SetStoreFunction, type Store } from "solid-js/store"
import { Option, Schema } from "effect"
import { pathKey } from "@/workspaces/path-key"
import { ScopedKey, ServerScope } from "@/runtime/server/scope"
import { persistStore } from "./persist"
import { Persistence } from "./schema"
type InitType = Promise<string> | string | null
type InitType = Promise<string | null> | string | null
type PersistedWithReady<T> = [
Store<T>,
SetStoreFunction<T>,
@@ -593,13 +594,18 @@ export function persisted<S extends Schema.ConstraintCodec<object, unknown>>(
: undefined
if (channel) onCleanup(() => channel.close())
const [state, setState, init] = makePersisted<S["Type"], typeof store>(store, {
const persist = persistStore({
store: store[0],
setStore: store[1],
name: config.key,
storage,
serialize,
deserialize: Schema.decodeUnknownSync(json),
sync: channel ? messageSync(channel) : undefined,
})
const state = store[0]
const setState = persist.setStore
const init = persist.init
const isAsync = init instanceof Promise
const [ready] = createResource(
@@ -255,7 +255,7 @@ export function createChildStoreManager(input: {
disposers.set(key, dispose)
activationToggles.set(key, setInstanceQueriesEnabled)
const onPersistedInit = (init: Promise<string> | string | null, run: () => void) => {
const onPersistedInit = (init: Promise<string | null> | string | null, run: () => void) => {
if (!(init instanceof Promise)) return
void init.then(() => {
if (children[key] !== child) return
@@ -4,6 +4,7 @@ import { SessionMessage } from "@opencode-ai/schema/session-message"
import { ServerScope } from "@/runtime/server/scope"
import { createComposerState, type ComposerStore } from "@/composer/state"
import { createComposerEditorActions } from "@/composer/editor/actions"
import { flushPersisted } from "@/runtime/persistence/persist"
function setup(read: () => string | null | Promise<string | null> = () => null) {
return createRoot((dispose) => {
@@ -27,83 +28,72 @@ function setup(read: () => string | null | Promise<string | null> = () => null)
})
}
test("composer-write-batch: typing persists prompt, cursor and retry together before returning", async () => {
test("composer-write-batch: a burst of edits persists once with prompt, cursor and retry together", async () => {
const value = setup()
try {
await value.state.ready.promise
value.state.context.add({ type: "file", path: "src/queue.ts", preview: "await queue.flush()" })
value.state.retry.set({ id: SessionMessage.ID.create(), agent: "build", providerID: "test", modelID: "test" })
flushPersisted()
value.writes.length = 0
value.editor.setPrompt([{ type: "text", content: "keep ordering", start: 0, end: 13 }], 13)
value.editor.setCursor(12)
expect(value.writes).toHaveLength(0)
flushPersisted()
expect(value.writes).toHaveLength(1)
expect(value.writes[0]).toMatchObject({
prompt: [{ type: "text", content: "keep ordering", start: 0, end: 13 }],
cursor: 13,
cursor: 12,
context: { items: [{ path: "src/queue.ts", preview: "await queue.flush()" }] },
})
expect(value.writes[0].retry).toBeUndefined()
value.editor.setCursor(13)
expect(value.writes).toHaveLength(1)
value.editor.setCursor(12)
expect(value.writes.map((write) => write.cursor)).toEqual([13, 12])
value.editor.setCursor(12)
expect(value.writes).toHaveLength(2)
} finally {
value.dispose()
}
})
test("composer-write-batch: state replacement preserves an omitted cursor and resets in one write", async () => {
test("composer-write-batch: a save with no serialized change writes nothing", async () => {
const value = setup()
try {
await value.state.ready.promise
value.state.set([{ type: "text", content: "previous", start: 0, end: 8 }], 5)
value.state.mode.set("shell")
value.state.retry.set({ id: SessionMessage.ID.create(), agent: "build", providerID: "test", modelID: "test" })
flushPersisted()
value.writes.length = 0
value.editor.setCursor(5)
value.state.mode.set("shell")
flushPersisted()
expect(value.writes).toHaveLength(0)
value.state.reset()
flushPersisted()
expect(value.writes).toHaveLength(1)
expect(value.writes[0]).toMatchObject({ prompt: [{ content: "" }], cursor: 0, mode: "shell" })
} finally {
value.dispose()
}
})
test("composer-write-batch: state replacement snapshots the value at save time", async () => {
const value = setup()
try {
await value.state.ready.promise
value.state.set([{ type: "text", content: "previous", start: 0, end: 8 }], 5)
flushPersisted()
value.writes.length = 0
const prompt = [{ type: "text" as const, content: "next", start: 0, end: 4 }]
value.state.set(prompt)
prompt[0].content = "changed outside the store"
expect(value.state.current()[0]).toMatchObject({ content: "next" })
flushPersisted()
expect(value.writes).toHaveLength(1)
expect(value.writes[0].prompt).toEqual([{ type: "text", content: "next", start: 0, end: 4 }])
expect(value.writes[0].cursor).toBe(5)
expect(value.writes[0].retry).toBeUndefined()
value.state.reset()
expect(value.writes).toHaveLength(2)
expect(value.writes[1]).toMatchObject({ prompt: [{ content: "" }], cursor: 0, mode: "shell" })
value.state.mode.set("normal")
value.state.mode.set("normal")
expect(value.writes).toHaveLength(3)
expect(value.writes[2].mode).toBe("normal")
} finally {
value.dispose()
}
})
test("composer-write-batch: unchanged mode still clears a retry and context writes remain ordered", async () => {
const value = setup()
try {
await value.state.ready.promise
value.state.mode.set("normal")
value.state.retry.set({ id: SessionMessage.ID.create(), agent: "build", providerID: "test", modelID: "test" })
value.writes.length = 0
value.editor.setMode("normal")
expect(value.writes).toHaveLength(1)
expect(value.writes[0].retry).toBeUndefined()
value.state.context.add({ type: "file", path: "first.ts" })
value.state.context.add({ type: "file", path: "second.ts" })
value.state.context.remove(value.state.context.items()[0].key)
expect(value.writes.slice(1).map((write) => write.context.items.map((item) => item.path))).toEqual([
["first.ts"],
["first.ts", "second.ts"],
["second.ts"],
])
} finally {
value.dispose()
}
})
test("composer-write-batch: text replacement and insertion each persist once and retain attachments", async () => {
test("composer-write-batch: the last edit in a window wins and attachments are retained", async () => {
const value = setup()
try {
await value.state.ready.promise
@@ -120,12 +110,14 @@ test("composer-write-batch: text replacement and insertion each persist once and
],
3,
)
flushPersisted()
value.writes.length = 0
value.editor.setText("new")
value.editor.addText(" notes")
expect(value.writes).toHaveLength(2)
expect(value.writes.map((write) => write.cursor)).toEqual([3, 9])
expect(value.writes[1].prompt).toEqual([
flushPersisted()
expect(value.writes).toHaveLength(1)
expect(value.writes[0].cursor).toBe(9)
expect(value.writes[0].prompt).toEqual([
{ type: "text", content: "new notes", start: 0, end: 9 },
{
type: "image",
@@ -145,7 +137,6 @@ test("composer-write-batch: an edit still wins over a pending persisted read", a
const value = setup(() => loading.promise)
try {
value.editor.setPrompt([{ type: "text", content: "new", start: 0, end: 3 }], 3)
expect(value.writes).toHaveLength(1)
loading.resolve(
JSON.stringify({
prompt: [{ type: "text", content: "old", start: 0, end: 3 }],
@@ -156,13 +147,15 @@ test("composer-write-batch: an edit still wins over a pending persisted read", a
await value.state.ready.promise
expect(value.state.current()).toEqual([{ type: "text", content: "new", start: 0, end: 3 }])
expect(value.state.cursor()).toBe(3)
flushPersisted()
expect(value.writes).toHaveLength(1)
expect(value.writes[0].prompt).toEqual([{ type: "text", content: "new", start: 0, end: 3 }])
} finally {
value.dispose()
}
})
test("composer-write-batch: persistence still precedes reactive observers", async () => {
test("composer-write-batch: observers see every edit before anything is persisted", async () => {
const value = setup()
try {
await value.state.ready.promise
@@ -177,10 +170,22 @@ test("composer-write-batch: persistence still precedes reactive observers", asyn
value.editor.addText(" and insert")
value.state.set([{ type: "text", content: "restore", start: 0, end: 7 }], 7)
value.state.reset()
expect(observed).toEqual([0, 1, 2, 3, 4, 5])
expect(observed).toEqual([0, 0, 0, 0, 0, 0])
flushPersisted()
expect(value.writes).toHaveLength(1)
dispose()
})
} finally {
value.dispose()
}
})
test("composer-write-batch: disposing the owner saves pending edits", async () => {
const value = setup()
await value.state.ready.promise
value.editor.setPrompt([{ type: "text", content: "unsaved", start: 0, end: 7 }], 7)
expect(value.writes).toHaveLength(0)
value.dispose()
expect(value.writes).toHaveLength(1)
expect(value.writes[0].prompt).toEqual([{ type: "text", content: "unsaved", start: 0, end: 7 }])
})
@@ -1,6 +1,7 @@
import { expect, test } from "bun:test"
import { createRoot } from "solid-js"
import { ModelSelectionSchema } from "@/providers/models/selection"
import { flushPersisted } from "@/runtime/persistence/persist"
import { persisted } from "@/runtime/persistence/storage"
test("persisted model selection hydrates, updates and serializes the schema shape", () => {
@@ -25,6 +26,7 @@ test("persisted model selection hydrates, updates and serializes the schema shap
expect(state.session.session1?.agent).toBe("plan")
setState("session", "session1", { agent: "build", variant: null })
expect(state.session.session1?.agent).toBe("build")
flushPersisted()
expect(JSON.parse(localStorage.getItem(key) ?? "null")).toEqual({
session: { session1: { agent: "build", variant: null } },
})
@@ -6,6 +6,7 @@ import type { Platform } from "@/runtime/platform/platform"
import { createComposerReady, createComposerState } from "@/composer/state"
import { ServerScope } from "@/runtime/server/scope"
import { createDraftStore } from "@/runtime/persistence/drafts"
import { flushPersisted } from "@/runtime/persistence/persist"
import { Persist, persisted } from "@/runtime/persistence/storage"
let read: ((value: string | null) => void) | undefined
@@ -109,6 +110,7 @@ describe("prompt persistence", () => {
},
])
root.session.set([{ type: "text", content: "hello", start: 0, end: 5 }, ...root.session.current()])
flushPersisted()
await Bun.sleep(0)
expect(documents.get(key)).toContain("hello")
expect(documents.get(key)).toContain('"blob":{"id":"composer-image"}')
@@ -2,6 +2,7 @@ import { describe, expect, test } from "bun:test"
import { Schema, SchemaGetter } from "effect"
import { createComputed, createRoot } from "solid-js"
import type { Platform } from "@/runtime/platform/platform"
import { flushPersisted } from "@/runtime/persistence/persist"
import { Persist, persisted } from "@/runtime/persistence/storage"
import { Persistence } from "@/runtime/persistence/schema"
import { TabStorage } from "@/shell/tabs/schema"
@@ -57,6 +58,7 @@ describe("schema-backed persistence", () => {
expect(state.key).toBe("session-tab")
setState("key", undefined)
expect(state.key).toBeUndefined()
flushPersisted()
expect(localStorage.getItem(key)).toBe("{}")
} finally {
dispose()
@@ -74,6 +76,7 @@ describe("schema-backed persistence", () => {
expect(state).toEqual({ enabled: true, label: "saved" })
expect(JSON.parse(localStorage.getItem(key)!)).toEqual({ enabled: true, label: "saved" })
setState("enabled", false)
flushPersisted()
expect(JSON.parse(localStorage.getItem(key)!)).toEqual({ enabled: false, label: "saved" })
dispose()
})
@@ -125,6 +128,7 @@ describe("schema-backed persistence", () => {
label: "desktop",
})
root.state[1]("label", "changed")
flushPersisted()
expect(JSON.parse(storage.values.get("opencode.global.dat:schema-desktop")!)).toEqual({
enabled: true,
label: "changed",
@@ -1,6 +1,7 @@
import {
createDraftStore,
createNamespaceStorage,
flushPersisted,
type NamespaceStorage,
type Platform,
} from "@opencode-ai/app/desktop"
@@ -17,7 +18,12 @@ export function createDesktopStorage(api: ElectronAPI) {
namespaces.set(name, next)
return next
}
const flush = () => Promise.all([...namespaces.values()].map((namespace) => namespace.flush()))
// Dirty stores must serialize into their namespaces before the namespaces are sent; the app's
// own pagehide listener registers after the IPC client's, so it cannot be relied on here.
const flush = () => {
flushPersisted()
return Promise.all([...namespaces.values()].map((namespace) => namespace.flush()))
}
api.onStoreChanged((name, insert, remove, revision) => namespaces.get(name)?.accept(insert, remove, revision))
// Durability boundaries: the window going away, and it leaving the foreground.