From be674e3158abb5f1b0c16b2bdeb3a9ba2b072774 Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Tue, 13 Oct 2026 19:22:40 +1000 Subject: [PATCH] fix(app): stop project store eviction loop on launch (#55078) --- .../runtime/server/global-sync/child-store.ts | 4 +- .../test-browser/child-store-eviction.test.ts | 112 ++++++++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 packages/app/test-browser/child-store-eviction.test.ts diff --git a/packages/app/src/runtime/server/global-sync/child-store.ts b/packages/app/src/runtime/server/global-sync/child-store.ts index 00485a0ad40..011b14068c4 100644 --- a/packages/app/src/runtime/server/global-sync/child-store.ts +++ b/packages/app/src/runtime/server/global-sync/child-store.ts @@ -110,7 +110,9 @@ export function createChildStoreManager(input: { if (set.size === 0) ownerPins.delete(key) } - unpin(directory) + // A re-running memo cleans up before it pins its directories again. Unpinning at once lets + // eviction dispose them mid-run, so the memo recreates them, and their hydration re-runs it. + queueMicrotask(() => unpin(directory)) }) } diff --git a/packages/app/test-browser/child-store-eviction.test.ts b/packages/app/test-browser/child-store-eviction.test.ts new file mode 100644 index 00000000000..5f7f69b7ac3 --- /dev/null +++ b/packages/app/test-browser/child-store-eviction.test.ts @@ -0,0 +1,112 @@ +import { expect, test } from "bun:test" +import { createComponent, createMemo, createRoot, getOwner } from "solid-js" +import { createStore, reconcile } from "solid-js/store" +import { QueryClient, QueryClientProvider } from "@tanstack/solid-query" +import { Predicate } from "effect" +import type { Data } from "@opencode/client/solid" +import type { persisted } from "@/runtime/persistence/storage" +import { createChildStoreManager } from "@/runtime/server/global-sync/child-store" +import { MAX_DIR_STORES } from "@/runtime/server/global-sync/types" +import type { QueryOptionsApi } from "@/runtime/server/sync" +import { ServerScope } from "@/runtime/server/scope" + +// SAFETY: the child store reads only these location lists, and only from its getters. +// oxlint-disable-next-line anti-slop/no-chained-type-assertions -- see SAFETY above +const data = { + location: { + info: () => undefined, + agent: { list: () => undefined }, + command: { list: () => undefined }, + reference: { list: () => undefined }, + provider: { list: () => undefined }, + model: { list: () => undefined }, + mcp: { server: { list: () => undefined }, resource: { list: () => undefined } }, + vcs: { info: () => undefined }, + }, +} as unknown as Data + +// SAFETY: the child store builds only the LSP query, which this value provides. +// oxlint-disable-next-line anti-slop/no-chained-type-assertions -- see SAFETY above +const queryOptions = { + lsp: (directory: string) => ({ queryKey: [directory, "lsp"], queryFn: async () => [] }), +} as unknown as QueryOptionsApi + +// Runs under the browser build: the server build of Solid never re-runs the memo. +test("keeps project stores alive when a memo over more directories than the store limit re-runs", async () => { + const created: string[] = [] + const hydrations: Promise[] = [] + const directories = Array.from({ length: MAX_DIR_STORES + 10 }, (_, index) => `/project-${index}`) + // Stops hydrating after a bound so a regression fails instead of hanging the runner. + const limit = directories.length * 10 + + // Hydrates a stored project row asynchronously, as persistStore does for a row in drafts.sqlite. + const persist: typeof persisted = (target, _schema, initial) => { + const store = createStore(initial) + const key = Predicate.isString(target) ? target : target.key + created.push(key) + + if (key !== "workspace:project" || created.length > limit) { + return [...store, null, Object.assign(() => true, { promise: undefined })] + } + + const init = Promise.resolve().then(() => { + store[1](reconcile({ value: { name: "stored" } })) + + return "stored" + }) + + hydrations.push(init) + + return [...store, init, Object.assign(() => true, { promise: init })] + } + + const root = createRoot((dispose) => { + let manager: ReturnType | undefined + + createComponent(QueryClientProvider, { + client: new QueryClient(), + get children() { + const owner = getOwner() + + if (!owner) throw new Error("owner required") + + manager = createChildStoreManager({ + owner, + connected: () => false, + scope: ServerScope.local, + persist, + isBooting: () => false, + isLoadingSessions: () => false, + onBootstrap() {}, + onMcp() {}, + onDispose() {}, + translate: (key) => key, + queryOptions, + data, + global: { path: { state: "", config: "", worktree: "", directory: "", home: "" } }, + }) + + return undefined + }, + }) + + if (!manager) throw new Error("manager required") + const children = manager + + createMemo(() => directories.map((directory) => children.child(directory, { bootstrap: false })[0].projectMeta)) + + return { manager: children, dispose } + }) + + try { + // A recreated store starts another hydration, so wait until none is left. + while (hydrations.length > 0) await Promise.all(hydrations.splice(0)) + + expect(created.filter((key) => key === "workspace:project")).toHaveLength(directories.length) + expect(directories.map((directory) => root.manager.children[directory]?.[0].projectMeta?.name)).toEqual( + directories.map(() => "stored"), + ) + } finally { + root.dispose() + } +})