From 8a8a9d4af3441be07ee8e8f4a7452449654f90f8 Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Wed, 4 Nov 2026 14:54:26 -0600 Subject: [PATCH] refactor(app): replace dead directory bootstrap with a directory cache machine (#61753) --- .../server/global-sync/bootstrap.test.ts | 7 +- .../runtime/server/global-sync/bootstrap.ts | 133 +----- .../server/global-sync/child-store.test.ts | 224 +-------- .../runtime/server/global-sync/child-store.ts | 435 ++++++------------ .../server/global-sync/eviction.test.ts | 29 -- .../runtime/server/global-sync/eviction.ts | 39 -- .../server/global-sync/machine.test.ts | 96 ++++ .../src/runtime/server/global-sync/machine.ts | 84 ++++ .../runtime/server/global-sync/mcp.test.ts | 55 --- .../app/src/runtime/server/global-sync/mcp.ts | 18 - .../runtime/server/global-sync/queue.test.ts | 48 -- .../src/runtime/server/global-sync/queue.ts | 98 ---- .../src/runtime/server/global-sync/types.ts | 59 +-- packages/app/src/runtime/server/runtime.tsx | 2 +- .../server/server-sync/connection.test.ts | 8 - .../runtime/server/server-sync/connection.ts | 5 - packages/app/src/runtime/server/sync.tsx | 210 +-------- .../test-browser/child-store-eviction.test.ts | 54 +-- 18 files changed, 394 insertions(+), 1210 deletions(-) delete mode 100644 packages/app/src/runtime/server/global-sync/eviction.test.ts delete mode 100644 packages/app/src/runtime/server/global-sync/eviction.ts create mode 100644 packages/app/src/runtime/server/global-sync/machine.test.ts create mode 100644 packages/app/src/runtime/server/global-sync/machine.ts delete mode 100644 packages/app/src/runtime/server/global-sync/mcp.test.ts delete mode 100644 packages/app/src/runtime/server/global-sync/mcp.ts delete mode 100644 packages/app/src/runtime/server/global-sync/queue.test.ts delete mode 100644 packages/app/src/runtime/server/global-sync/queue.ts delete mode 100644 packages/app/src/runtime/server/server-sync/connection.test.ts diff --git a/packages/app/src/runtime/server/global-sync/bootstrap.test.ts b/packages/app/src/runtime/server/global-sync/bootstrap.test.ts index c2abeb60b10..f7e96bc38bd 100644 --- a/packages/app/src/runtime/server/global-sync/bootstrap.test.ts +++ b/packages/app/src/runtime/server/global-sync/bootstrap.test.ts @@ -1,6 +1,6 @@ import { describe, expect, test } from "bun:test" import { QueryClient } from "@tanstack/solid-query" -import { OpenCode } from "@opencode/client/promise" +import { OpenCode, type LocationGetInput } from "@opencode/client/promise" import { createStore } from "solid-js/store" import { bootstrapGlobal, inferHomeDirectory, loadPathQuery, loadProjectsQuery } from "./bootstrap" import { ServerScope } from "@/runtime/server/scope" @@ -35,7 +35,6 @@ test("bootstraps projects through the native store setter and preserves subseque project: [], provider_auth: {}, config: {}, - reload: undefined, }) const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }) @@ -114,14 +113,16 @@ describe("query keys", () => { test("partitions identical directories by server scope and loads current location metadata", async () => { const calls: unknown[] = [] + // SAFETY: the path query reads only `directory` and `project.directory` from the location it gets. const location = { - get: async (input: unknown) => { + get: async (input?: LocationGetInput) => { calls.push(input) return { directory: "/repo/subpath", project: { id: "project", directory: "/repo" } } }, } as ServerApi["location"] + // SAFETY: a remote server's scope is its origin string, which is how the app scopes it. const remote = "https://debian.example" as typeof ServerScope.local expect([...loadPathQuery(ServerScope.local, "/repo", location).queryKey]).toEqual(["local", "/repo", "path"]) diff --git a/packages/app/src/runtime/server/global-sync/bootstrap.ts b/packages/app/src/runtime/server/global-sync/bootstrap.ts index 692a3f5f5d1..c63501110cf 100644 --- a/packages/app/src/runtime/server/global-sync/bootstrap.ts +++ b/packages/app/src/runtime/server/global-sync/bootstrap.ts @@ -1,54 +1,13 @@ -import type { Config, Path, Project, ProviderAuthResponse } from "@/runtime/server/types" +import type { Config, Path } from "@/runtime/server/types" import type { LocationGetInput, LocationGetOutput, ProjectListOutput } from "@opencode/client/promise" -import { showToast } from "@/shell/notifications/toast" -import { getFilename } from "@opencode/util/path" import { retry } from "@opencode/util/retry" -import { reconcile, type SetStoreFunction, type Store } from "solid-js/store" -import type { State } from "./types" +import type { SetStoreFunction } from "solid-js/store" +import type { GlobalStore } from "./types" import { normalizeProjectInfo } from "./utils" -import { formatServerError } from "@/runtime/server/errors" import { QueryClient, queryOptions } from "@tanstack/solid-query" import type { ServerScope } from "@/runtime/server/scope" import { withWorktreeInventory, worktreeInventoryKey } from "@/workspaces/inventory" -type GlobalStore = { - path: Path - project: Project[] - provider_auth: ProviderAuthResponse - config: Config - reload: undefined | "pending" | "complete" -} - -function waitForPaint() { - return new Promise((resolve) => { - let done = false - - const finish = () => { - if (done) return - done = true - resolve() - } - - const timer = setTimeout(finish, 50) - - if (typeof requestAnimationFrame !== "function") return - requestAnimationFrame(() => { - setTimeout(() => { - clearTimeout(timer) - finish() - }, 0) - }) - }) -} - -function errors(list: PromiseSettledResult[]) { - return list.filter((item): item is PromiseRejectedResult => item.status === "rejected").map((item) => item.reason) -} - -function runAll(list: Array<() => Promise>) { - return Promise.allSettled(list.map((item) => item())) -} - export const loadGlobalConfigQuery = (scope: ServerScope) => queryOptions({ queryKey: [scope, "config"], @@ -100,26 +59,18 @@ export async function bootstrapGlobal(input: { setGlobalStore: SetStoreFunction queryClient: QueryClient }) { - const slow = [ - () => input.queryClient.fetchQuery(loadGlobalConfigQuery(input.scope)), - () => - input.queryClient - .fetchQuery(loadPathQuery(input.scope, null, input.serverAPI.location)) - .then((data) => input.setGlobalStore("path", data)), - () => - loadProjects({ - scope: input.scope, - project: input.serverAPI.project, - setGlobalStore: input.setGlobalStore, - queryClient: input.queryClient, - }), - ] - - await runAll(slow) -} - -function projectID(directory: string, projects: Project[]) { - return projects.find((project) => project.worktree === directory || project.sandboxes?.includes(directory))?.id + await Promise.allSettled([ + input.queryClient.fetchQuery(loadGlobalConfigQuery(input.scope)), + input.queryClient + .fetchQuery(loadPathQuery(input.scope, null, input.serverAPI.location)) + .then((data) => input.setGlobalStore("path", data)), + loadProjects({ + scope: input.scope, + project: input.serverAPI.project, + setGlobalStore: input.setGlobalStore, + queryClient: input.queryClient, + }), + ]) } export function inferHomeDirectory(path: string) { @@ -143,57 +94,3 @@ export const loadPathQuery = (scope: ServerScope, directory: string | null, api: home: inferHomeDirectory(location.directory) || inferHomeDirectory(location.project.directory), })), }) - -export async function bootstrapDirectory(input: { - directory: string - scope: ServerScope - mcp: boolean - api: { - readonly project: ProjectApi - readonly location: LocationApi - } - store: Store - setStore: SetStoreFunction - translate: (key: string, vars?: Record) => string - global: { - config: Config - path: Path - project: Project[] - } - queryClient: QueryClient -}) { - const loading = input.store.status !== "complete" - const seededProject = projectID(input.directory, input.global.project) - - if (seededProject) input.setStore("project", seededProject) - - if (Object.keys(input.store.config).length === 0 && Object.keys(input.global.config).length > 0) { - input.setStore("config", reconcile(input.global.config, { merge: false })) - } - - if (loading) input.setStore("status", "partial") - - const slow = [ - seededProject - ? undefined - : () => - retry(() => input.api.location.get({ location: { directory: input.directory } })).then((location) => - input.setStore("project", location.project.id), - ), - ].filter((task): task is () => Promise => !!task) - - await waitForPaint() - const slowErrs = errors(await runAll(slow)) - - if (slowErrs.length > 0) { - console.error("Failed to finish bootstrap instance", slowErrs[0]) - const project = getFilename(input.directory) - showToast({ - variant: "error", - title: input.translate("toast.project.reloadFailed.title", { project }), - description: formatServerError(slowErrs[0], input.translate), - }) - } - - if (loading && slowErrs.length === 0) input.setStore("status", "complete") -} diff --git a/packages/app/src/runtime/server/global-sync/child-store.test.ts b/packages/app/src/runtime/server/global-sync/child-store.test.ts index 9940c6bff45..27fb523d57f 100644 --- a/packages/app/src/runtime/server/global-sync/child-store.test.ts +++ b/packages/app/src/runtime/server/global-sync/child-store.test.ts @@ -1,16 +1,11 @@ -import { beforeAll, describe, expect, mock, test } from "bun:test" +import { describe, expect, test } from "bun:test" import { createRoot, getOwner } from "solid-js" import { createStore } from "solid-js/store" -import type { State } from "./types" -import type { QueryOptionsApi } from "../sync" +import { createChildStoreManager } from "./child-store" import { ServerScope } from "@/runtime/server/scope" import type { Data } from "@opencode/client/solid" import type { persisted } from "@/runtime/persistence/storage" -let createChildStoreManager: typeof import("./child-store").createChildStoreManager - -const querySingles: Array<() => { queryKey?: unknown[]; enabled?: boolean }> = [] - const persist: typeof persisted = (_target, _schema, initial) => [ ...createStore(initial), null, @@ -19,203 +14,34 @@ const persist: typeof persisted = (_target, _schema, initial) => [ const path = { state: "", config: "", worktree: "", directory: "", home: "" } -const data = { - location: { - info: () => undefined, - agent: { list: () => undefined }, - command: { list: () => undefined }, - reference: { list: () => undefined }, - provider: { list: () => undefined }, - model: { list: () => undefined, default: () => undefined }, - mcp: { - server: { list: () => undefined }, - resource: { list: () => undefined }, - }, - vcs: { info: () => undefined }, - }, -} as unknown as Data - -const queryOptionsApi = { - globalConfig: () => ({ queryKey: ["globalConfig"], queryFn: async () => ({}) }), - projects: () => ({ queryKey: ["projects"], queryFn: async () => [] }), - path: (directory: string | null) => ({ - queryKey: [directory, "path"], - queryFn: async () => ({ - state: "", - config: "", - worktree: "", - directory: directory ?? "", - home: "", - }), - }), - agents: (directory: string) => ({ queryKey: [directory, "agents"], queryFn: async () => [] }), - mcp: (directory: string) => ({ queryKey: [directory, "mcp"], queryFn: async () => ({}) }), - mcpResources: (directory: string) => ({ queryKey: [directory, "mcpResources"], queryFn: async () => ({}) }), - lsp: (directory: string) => ({ queryKey: [directory, "lsp"], queryFn: async () => [] }), - references: (directory: string) => ({ queryKey: [directory, "references"], queryFn: async () => [] }), - sessions: (directory: string) => ({ queryKey: [directory, "loadSessions"] as const }), -} as unknown as QueryOptionsApi - -function setup(input: { connected?: () => boolean } = {}) { - const bootstraps: string[] = [] - const mcpLoads: string[] = [] - const offset = querySingles.length - - return createRoot((dispose) => { - const owner = getOwner() - - if (!owner) throw new Error("owner required") - - const manager = createChildStoreManager({ - owner, - connected: input.connected ?? (() => true), - scope: ServerScope.local, - persist, - isBooting: () => false, - isLoadingSessions: () => false, - onBootstrap: (directory) => bootstraps.push(directory), - onMcp: (directory) => mcpLoads.push(directory), - onDispose() {}, - translate: (key) => key, - queryOptions: queryOptionsApi, - data, - global: { path }, - }) - - return { manager, bootstraps, mcpLoads, queries: () => querySingles.slice(offset), dispose } - }) -} - -beforeAll(async () => { - mock.module("@tanstack/solid-query", () => ({ - useQuery: (options: () => { queryKey?: unknown[]; enabled?: boolean }) => { - querySingles.push(options) - - return { - get isLoading() { - return options().queryKey?.[1] === "path" - }, - get isSuccess() { - return false - }, - get isRefetchError() { - return false - }, - get data() { - if (options().queryKey?.[1] === "path") throw new Error("pending path data read") - - if (options().queryKey?.[1] === "mcp") return options().enabled ? { demo: { status: "disabled" } } : undefined - - if (options().queryKey?.[1] === "lsp") return [] - - return undefined - }, - } - }, - })) - - createChildStoreManager = (await import("./child-store")).createChildStoreManager -}) +// SAFETY: the child store reads only this location info, and only from its getters. +// oxlint-disable-next-line anti-slop/no-chained-type-assertions -- see SAFETY above +const data = { location: { info: () => undefined } } as unknown as Data describe("createChildStoreManager", () => { - test("does not evict the active directory during mark", () => { - const { manager, dispose } = setup() + test("provides the requested directory while its location is loading", () => { + createRoot((dispose) => { + const owner = getOwner() - try { - Array.from({ length: 30 }, (_, index) => `/pinned-${index}`).forEach((directory) => { - manager.children[directory] = createStore({} as State) - manager.pin(directory) + if (!owner) throw new Error("owner required") + + const manager = createChildStoreManager({ + owner, + scope: ServerScope.local, + persist, + translate: (key) => key, + data, + global: { path }, }) - manager.children["/active"] = createStore({} as State) - manager.mark("/active") + try { + const [store] = manager.child("/project") - expect(manager.children["/active"]).toBeDefined() - } finally { - dispose() - } - }) - - test("starts new child stores as loading and bootstraps them on first access", () => { - const { manager, bootstraps, dispose } = setup() - - try { - const [store] = manager.child("/project") - - expect(store.status).toBe("loading") - expect(bootstraps).toEqual(["/project"]) - } finally { - dispose() - } - }) - - test("provides the requested directory while the path query is pending", () => { - const { manager, dispose } = setup() - - try { - const [store] = manager.child("/project", { bootstrap: false }) - - expect(store.path.directory).toBe("/project") - expect(store.path.worktree).toBe("") - } finally { - dispose() - } - }) - - test("syncs MCP only when requested for the directory", () => { - const { manager, mcpLoads, queries, dispose } = setup() - - try { - const [, setStore] = manager.child("/project", { bootstrap: false }) - expect(queries()).toHaveLength(1) - - setStore("status", "complete") - manager.child("/project", { bootstrap: false, mcp: true }) - expect(mcpLoads).toEqual(["/project"]) - - manager.disableMcp("/project") - expect(manager.mcp("/project")).toBe(false) - } finally { - dispose() - } - }) - - test("keeps non-bootstrapping children passive until a real directory access", () => { - const { manager, bootstraps, queries, dispose } = setup() - - try { - const [store] = manager.child("/project", { bootstrap: false }) - - expect(queries()).toHaveLength(1) - expect(queries()[0]?.().enabled).toBe(false) - expect(store.path.directory).toBe("/project") - expect(store.provider_ready).toBe(false) - expect(store.lsp_ready).toBe(false) - expect(bootstraps).toEqual([]) - - manager.child("/project") - expect(queries()[0]?.().enabled).toBe(true) - expect(bootstraps).toEqual(["/project"]) - - manager.child("/project", { bootstrap: false }) - expect(queries()[0]?.().enabled).toBe(true) - } finally { - dispose() - } - }) - - test("does not enable location queries before the event handshake", () => { - const connection = { connected: false } - const { manager, queries, dispose } = setup({ connected: () => connection.connected }) - - try { - manager.child("/handshake") - expect(queries()[0]?.().enabled).toBe(false) - - connection.connected = true - expect(queries()[0]?.().enabled).toBe(true) - } finally { - dispose() - } + expect(store.path.directory).toBe("/project") + expect(store.path.worktree).toBe("") + } finally { + dispose() + } + }) }) }) 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 b463e342646..98e43d973fc 100644 --- a/packages/app/src/runtime/server/global-sync/child-store.ts +++ b/packages/app/src/runtime/server/global-sync/child-store.ts @@ -1,357 +1,190 @@ -import { createRoot, createSignal, getOwner, onCleanup, runWithOwner, type Accessor, type Owner } from "solid-js" +import { getOwner, onCleanup, runWithOwner, type Owner } from "solid-js" import { createStore, type SetStoreFunction, type Store } from "solid-js/store" import { Persist, persisted } from "@/runtime/persistence/storage" import type { Path } from "@/runtime/server/types" -import { - DIR_IDLE_TTL_MS, - MAX_DIR_STORES, - type ChildOptions, - type DirState, - type MetaCache, - type ProjectMeta, - type State, -} from "./types" -import { canDisposeDirectory, pickDirectoriesToEvict } from "./eviction" -import { useQuery } from "@tanstack/solid-query" -import { QueryOptionsApi } from "../sync" +import type { ProjectMeta, State } from "./types" +import { createDirectoryCacheState, transitionDirectoryCache, type DirectoryCacheEvent } from "./machine" import { directoryKey, type DirectoryKey } from "./utils" import type { ServerScope } from "@/runtime/server/scope" import type { Data } from "@opencode/client/solid" import { normalizeAgentList, normalizeProviderList } from "./utils" import { ProjectState, VcsState } from "../persistence" +type ChildHandle = { + store: [Store, SetStoreFunction] + setMeta: SetStoreFunction +} + export function createChildStoreManager(input: { owner: Owner - connected: Accessor scope: ServerScope persist: typeof persisted - isBooting: (directory: string) => boolean - isLoadingSessions: (directory: string) => boolean - onBootstrap: (directory: string) => void - onMcp: (directory: string, setStore: SetStoreFunction) => void - onDispose: (directory: string) => void translate: (key: string, vars?: Record) => string - queryOptions: QueryOptionsApi data: Data global: { path: Path } }) { - const children: Record, SetStoreFunction]> = {} - const metaCache = new Map() - const lifecycle = new Map() - const pins = new Map() - const ownerPins = new WeakMap>() - const disposers = new Map void>() - const mcpDirectories = new Set() - const activeDirectories = new Set() - const activationToggles = new Map void>() + // Read only by `dispatch`; no reader tracks the cache, so a reactive memo that accesses a directory never re-runs. + let cache = createDirectoryCacheState() + const handles = new Map() + const ownerPins = new WeakMap>() - const markKey = (key: DirectoryKey) => { - if (!key) return - lifecycle.set(key, { lastAccessAt: Date.now() }) - runEviction(key) + function dispatch(event: DirectoryCacheEvent) { + const result = transitionDirectoryCache(cache, event) + + result.effects.forEach((effect) => { + if (effect.type === "evict") return handles.delete(effect.key) + handles.set(effect.key, createHandle(effect.key, effect.directory)) + }) + // Committed after the effects, so a store that failed to build is built again on the next access. + cache = result.state } - const mark = (directory: string) => { - const key = directoryKey(directory) - markKey(key) - } - - const pin = (directory: string) => { + function access(directory: string, pin: boolean) { const key = directoryKey(directory) - if (!key) return - pins.set(key, (pins.get(key) ?? 0) + 1) - markKey(key) + if (!key) console.error("No directory provided") + dispatch({ type: "accessed", key, directory, pin, at: Date.now() }) + const handle = handles.get(key) + + if (!handle) throw new Error(input.translate("error.childStore.storeCreateFailed")) + + return handle } - const unpin = (directory: string) => { - const key = directoryKey(directory) + // Pins the directory once per reactive computation, until that computation re-runs or is disposed. + function claimForOwner(key: DirectoryKey) { + const owner = getOwner() - if (!key) return - const next = (pins.get(key) ?? 0) - 1 + if (!owner || owner === input.owner) return false + const set = ownerPins.get(owner) - if (next > 0) { - pins.set(key, next) + if (set?.has(key)) return false - return - } + if (set) set.add(key) - pins.delete(key) - runEviction() - } - - const pinned = (directory: string) => (pins.get(directoryKey(directory)) ?? 0) > 0 - - const pinForOwner = (directory: string) => { - const current = getOwner() - - if (!current) return - - if (current === input.owner) return - const key = current as object - const set = ownerPins.get(key) - - if (set?.has(directory)) return - - if (set) set.add(directory) - - if (!set) ownerPins.set(key, new Set([directory])) - pin(directory) + if (!set) ownerPins.set(owner, new Set([key])) onCleanup(() => { - const set = ownerPins.get(key) + const set = ownerPins.get(owner) if (set) { - set.delete(directory) + set.delete(key) - if (set.size === 0) ownerPins.delete(key) + if (set.size === 0) ownerPins.delete(owner) } - // 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)) + // A re-running memo cleans up before it pins its directories again. Releasing at once lets + // eviction drop them mid-run, so the memo recreates them, and their hydration re-runs it. + queueMicrotask(() => dispatch({ type: "released", key, at: Date.now() })) }) - } - - function disposeDirectory(directory: DirectoryKey) { - const key = directory - - if ( - !canDisposeDirectory({ - directory: key, - hasStore: !!children[key], - pinned: pinned(key), - booting: input.isBooting(key), - loadingSessions: input.isLoadingSessions(key), - }) - ) { - return false - } - - metaCache.delete(key) - lifecycle.delete(key) - mcpDirectories.delete(key) - activeDirectories.delete(key) - activationToggles.delete(key) - const dispose = disposers.get(key) - - if (dispose) { - dispose() - disposers.delete(key) - } - - delete children[key] - input.onDispose(key) return true } - function runEviction(skip?: string) { - const stores = Object.keys(children) + function createHandle(key: DirectoryKey, directory: string): ChildHandle { + const vcs = runWithOwner(input.owner, () => + input.persist(Persist.serverWorkspace(input.scope, directory, "vcs"), VcsState, { value: undefined }), + ) - if (stores.length === 0) return + if (!vcs) throw new Error(input.translate("error.childStore.persistedCacheCreateFailed")) + const vcsStore = vcs[0] - const list = pickDirectoriesToEvict({ - stores, - state: lifecycle, - pins: new Set(stores.filter(pinned)), - max: MAX_DIR_STORES, - ttl: DIR_IDLE_TTL_MS, - now: Date.now(), - }).filter((directory) => directory !== skip) + const meta = runWithOwner(input.owner, () => + input.persist(Persist.serverWorkspace(input.scope, directory, "project"), ProjectState, { value: undefined }), + ) - if (list.length === 0) return + if (!meta) throw new Error(input.translate("error.childStore.persistedProjectMetadataCreateFailed")) + const initialMeta = meta[0].value - for (const directory of list) { - if (!disposeDirectory(directoryKey(directory))) continue - } - } + const child = createStore({ + project: "", + projectMeta: initialMeta, + get provider_ready() { + return ( + input.data.location.provider.list({ directory }) !== undefined && + input.data.location.model.list({ directory }) !== undefined + ) + }, + get provider() { + const provider = input.data.location.provider.list({ directory }) + const model = input.data.location.model.list({ directory }) - function ensureChild(directory: string) { - const key = directoryKey(directory) + if (!provider || !model) return { all: new Map(), connected: [], default: {} } - if (!key) console.error("No directory provided") + return normalizeProviderList(provider, model) + }, + config: {}, + get path() { + const location = input.data.location.info({ directory }) - if (!children[key]) { - const vcs = runWithOwner(input.owner, () => - input.persist(Persist.serverWorkspace(input.scope, directory, "vcs"), VcsState, { value: undefined }), - ) + return { + state: "", + config: "", + worktree: location?.project.directory ?? "", + directory: location?.directory ?? directory, + home: input.global.path.home, + } + }, + get agent() { + return normalizeAgentList(input.data.location.agent.list({ directory }) ?? []) + }, + get command() { + return input.data.location.command.list({ directory }) ?? [] + }, + get reference() { + return input.data.location.reference.list({ directory }) ?? [] + }, + get mcp_ready() { + return input.data.location.mcp.server.list({ directory }) !== undefined + }, + get mcp_resource() { + return Object.fromEntries( + (input.data.location.mcp.resource.list({ directory }) ?? []).map((resource) => [ + `${resource.server}:${resource.uri}`, + resource, + ]), + ) + }, + get vcs() { + const vcs = input.data.location.vcs.info({ directory }) - if (!vcs) throw new Error(input.translate("error.childStore.persistedCacheCreateFailed")) - const vcsStore = vcs[0] + if (!vcs) return vcsStore.value - const meta = runWithOwner(input.owner, () => - input.persist(Persist.serverWorkspace(input.scope, directory, "project"), ProjectState, { value: undefined }), - ) + return { branch: vcs.branch.current, default_branch: vcs.branch.default } + }, + }) - if (!meta) throw new Error(input.translate("error.childStore.persistedProjectMetadataCreateFailed")) - metaCache.set(key, { store: meta[0], setStore: meta[1], ready: meta[3] }) - - const init = () => - createRoot((dispose) => { - const initialMeta = meta[0].value - const [instanceQueriesEnabled, setInstanceQueriesEnabled] = createSignal(false) - - const lspQuery = useQuery(() => ({ - ...input.queryOptions.lsp(key), - enabled: input.connected() && instanceQueriesEnabled(), - })) - - const child = createStore({ - project: "", - projectMeta: initialMeta, - get provider_ready() { - return ( - input.data.location.provider.list({ directory }) !== undefined && - input.data.location.model.list({ directory }) !== undefined - ) - }, - get provider() { - const provider = input.data.location.provider.list({ directory }) - const model = input.data.location.model.list({ directory }) - - if (!provider || !model) return { all: new Map(), connected: [], default: {} } - - return normalizeProviderList(provider, model) - }, - config: {}, - get path() { - const location = input.data.location.info({ directory }) - - return { - state: "", - config: "", - worktree: location?.project.directory ?? "", - directory: location?.directory ?? directory, - home: input.global.path.home, - } - }, - status: "loading" as const, - get agent() { - return normalizeAgentList(input.data.location.agent.list({ directory }) ?? []) - }, - get command() { - return input.data.location.command.list({ directory }) ?? [] - }, - get reference() { - return input.data.location.reference.list({ directory }) ?? [] - }, - get mcp_ready() { - return input.data.location.mcp.server.list({ directory }) !== undefined - }, - get mcp() { - return Object.fromEntries( - (input.data.location.mcp.server.list({ directory }) ?? []).map((server) => [ - server.name, - server.status, - ]), - ) - }, - get mcp_resource() { - return Object.fromEntries( - (input.data.location.mcp.resource.list({ directory }) ?? []).map((resource) => [ - `${resource.server}:${resource.uri}`, - resource, - ]), - ) - }, - get lsp_ready() { - return instanceQueriesEnabled() && !lspQuery.isLoading - }, - get lsp() { - return lspQuery.isLoading ? [] : (lspQuery.data ?? []) - }, - get vcs() { - const vcs = input.data.location.vcs.info({ directory }) - - if (!vcs) return vcsStore.value - - return { branch: vcs.branch.current, default_branch: vcs.branch.default } - }, - }) - - children[key] = child - disposers.set(key, dispose) - activationToggles.set(key, setInstanceQueriesEnabled) - - const onPersistedInit = (init: Promise | string | null, run: () => void) => { - if (!(init instanceof Promise)) return - void init.then(() => { - if (children[key] !== child) return - run() - }) - } - - onPersistedInit(vcs[2], () => { - const cached = vcsStore.value - - if (!cached?.branch) return - child[1]("vcs", (value) => value ?? cached) - }) - - onPersistedInit(meta[2], () => { - if (child[0].projectMeta !== initialMeta) return - child[1]("projectMeta", meta[0].value) - }) - }) - - runWithOwner(input.owner, init) + const onPersistedInit = (init: Promise | string | null, run: () => void) => { + if (!(init instanceof Promise)) return + void init.then(() => { + if (handles.get(key)?.store !== child) return + run() + }) } - markKey(key) - const childStore = children[key] + onPersistedInit(vcs[2], () => { + const cached = vcsStore.value - if (!childStore) throw new Error(input.translate("error.childStore.storeCreateFailed")) + if (!cached?.branch) return + child[1]("vcs", (value) => value ?? cached) + }) - return childStore + onPersistedInit(meta[2], () => { + if (child[0].projectMeta !== initialMeta) return + child[1]("projectMeta", meta[0].value) + }) + + return { store: child, setMeta: meta[1] } } - function child(directory: string, options: ChildOptions = {}) { - const key = directoryKey(directory) - const childStore = ensureChild(directory) - pinForOwner(key) - - if (options.mcp) enableMcp(directory, key, childStore) - const shouldBootstrap = options.bootstrap ?? true - - if (shouldBootstrap) activate(key) - - if (shouldBootstrap && childStore[0].status === "loading") { - input.onBootstrap(directory) - } - - return childStore - } - - function enableMcp(directory: string, key: DirectoryKey, childStore: [Store, SetStoreFunction]) { - if (mcpDirectories.has(key)) return - mcpDirectories.add(key) - - if (childStore[0].status !== "loading") input.onMcp(directory, childStore[1]) - } - - // Passive project metadata reads must not initialize the directory. - // A real directory access enables these queries once for the store lifetime. - function activate(key: DirectoryKey) { - if (activeDirectories.has(key)) return - activeDirectories.add(key) - activationToggles.get(key)?.(true) - } - - function disableMcp(directory: string) { - const key = directoryKey(directory) - - if (!mcpDirectories.delete(key)) return + function child(directory: string) { + return access(directory, claimForOwner(directoryKey(directory))).store } function projectMeta(directory: string, patch: ProjectMeta) { - const key = directoryKey(directory) - const [store, setStore] = ensureChild(directory) - const cached = metaCache.get(key) - - if (!cached) return - const previous = store.projectMeta ?? {} + const handle = access(directory, false) + const previous = handle.store[0].projectMeta ?? {} const icon = patch.icon ? { ...previous.icon, ...patch.icon } : previous.icon const commands = patch.commands ? { ...previous.commands, ...patch.commands } : previous.commands @@ -362,22 +195,12 @@ export function createChildStoreManager(input: { commands, } - cached.setStore("value", next) - setStore("projectMeta", next) + handle.setMeta("value", next) + handle.store[1]("projectMeta", next) } return { - children, - ensureChild, child, projectMeta, - mark, - pin, - unpin, - pinned, - mcp: (directory: string) => mcpDirectories.has(directoryKey(directory)), - active: (directory: string) => activeDirectories.has(directoryKey(directory)), - disableMcp, - disposeDirectory, } } diff --git a/packages/app/src/runtime/server/global-sync/eviction.test.ts b/packages/app/src/runtime/server/global-sync/eviction.test.ts deleted file mode 100644 index f162729a622..00000000000 --- a/packages/app/src/runtime/server/global-sync/eviction.test.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { expect, test } from "bun:test" -import { canDisposeDirectory, pickDirectoriesToEvict } from "./eviction" - -test("pickDirectoriesToEvict keeps pinned stores and evicts idle stores", () => { - const picks = pickDirectoriesToEvict({ - stores: ["a", "b", "c", "d"], - state: new Map([ - ["a", { lastAccessAt: 1_000 }], - ["b", { lastAccessAt: 4_900 }], - ["c", { lastAccessAt: 4_800 }], - ["d", { lastAccessAt: 3_000 }], - ]), - pins: new Set(["a"]), - max: 2, - ttl: 1_500, - now: 5_000, - }) - - expect(picks).toEqual(["d", "c"]) -}) - -test.each([ - [{ pinned: true, booting: false, loadingSessions: false }, false], - [{ pinned: false, booting: true, loadingSessions: false }, false], - [{ pinned: false, booting: false, loadingSessions: true }, false], - [{ pinned: false, booting: false, loadingSessions: false }, true], -])("canDisposeDirectory(%o) is %p", (state, expected) => { - expect(canDisposeDirectory({ directory: "dir", hasStore: true, ...state })).toBe(expected) -}) diff --git a/packages/app/src/runtime/server/global-sync/eviction.ts b/packages/app/src/runtime/server/global-sync/eviction.ts deleted file mode 100644 index 214a29097ef..00000000000 --- a/packages/app/src/runtime/server/global-sync/eviction.ts +++ /dev/null @@ -1,39 +0,0 @@ -import type { DisposeCheck, EvictPlan } from "./types" - -export function pickDirectoriesToEvict(input: EvictPlan) { - const overflow = Math.max(0, input.stores.length - input.max) - let pendingOverflow = overflow - - const sorted = input.stores - .filter((dir) => !input.pins.has(dir)) - .slice() - .sort((a, b) => (input.state.get(a)?.lastAccessAt ?? 0) - (input.state.get(b)?.lastAccessAt ?? 0)) - - const output: string[] = [] - - for (const dir of sorted) { - const last = input.state.get(dir)?.lastAccessAt ?? 0 - const idle = input.now - last >= input.ttl - - if (!idle && pendingOverflow <= 0) continue - output.push(dir) - - if (pendingOverflow > 0) pendingOverflow -= 1 - } - - return output -} - -export function canDisposeDirectory(input: DisposeCheck) { - if (!input.directory) return false - - if (!input.hasStore) return false - - if (input.pinned) return false - - if (input.booting) return false - - if (input.loadingSessions) return false - - return true -} diff --git a/packages/app/src/runtime/server/global-sync/machine.test.ts b/packages/app/src/runtime/server/global-sync/machine.test.ts new file mode 100644 index 00000000000..e0b137e987b --- /dev/null +++ b/packages/app/src/runtime/server/global-sync/machine.test.ts @@ -0,0 +1,96 @@ +import { describe, expect, test } from "bun:test" +import { + createDirectoryCacheState, + transitionDirectoryCache, + type DirectoryCacheEvent, + type DirectoryCacheState, +} from "./machine" +import { DIR_IDLE_TTL_MS, MAX_DIR_STORES } from "./types" +import { directoryKey } from "./utils" + +function accessed(directory: string, at: number, pin = false): DirectoryCacheEvent { + return { type: "accessed", key: directoryKey(directory), directory, pin, at } +} + +function replay(events: DirectoryCacheEvent[], state = createDirectoryCacheState()) { + return events.reduce<{ state: DirectoryCacheState; effects: unknown[] }>( + (result, event) => { + const next = transitionDirectoryCache(result.state, event) + + return { state: next.state, effects: [...result.effects, ...next.effects] } + }, + { state, effects: [] }, + ) +} + +describe("transitionDirectoryCache", () => { + test("creates a directory's store on its first access only", () => { + const result = replay([accessed("C:\\repo", 1), accessed("C:/repo", 2, true)]) + + expect(result.effects).toEqual([{ type: "create", key: directoryKey("C:\\repo"), directory: "C:\\repo" }]) + expect(result.state.get(directoryKey("C:/repo"))).toEqual({ directory: "C:\\repo", pins: 1, lastAccessAt: 2 }) + }) + + test("evicts the least recently used store over the limit, then idle stores, keeping pinned ones", () => { + const start = replay([ + accessed("/pinned", 0, true), + accessed("/older", 1_000), + accessed("/newer", 2_000), + ...Array.from({ length: MAX_DIR_STORES - 3 }, (_, index) => accessed(`/held-${index}`, 3_000, true)), + ]) + + expect(start.state.size).toBe(MAX_DIR_STORES) + + const over = transitionDirectoryCache(start.state, accessed("/next", 4_000)) + + expect(over.effects).toEqual([ + { type: "create", key: directoryKey("/next"), directory: "/next" }, + { type: "evict", key: directoryKey("/older") }, + ]) + expect(over.state.size).toBe(MAX_DIR_STORES) + + const idle = transitionDirectoryCache(over.state, accessed("/pinned", DIR_IDLE_TTL_MS + 3_000)) + + // `/newer` sat idle for the TTL; `/next` did not yet, and the pinned stores never leave. + expect(idle.effects).toEqual([{ type: "evict", key: directoryKey("/newer") }]) + expect(idle.state.has(directoryKey("/next"))).toBe(true) + expect(idle.state.has(directoryKey("/pinned"))).toBe(true) + }) + + test("keeps the directory just accessed when every other store is pinned", () => { + const pinned = replay(Array.from({ length: MAX_DIR_STORES }, (_, index) => accessed(`/pinned-${index}`, 1, true))) + const result = transitionDirectoryCache(pinned.state, accessed("/active", 2)) + + expect(result.effects).toEqual([{ type: "create", key: directoryKey("/active"), directory: "/active" }]) + expect(result.state.has(directoryKey("/active"))).toBe(true) + }) + + test("evicts a store once its last reader releases it", () => { + const pinned = replay([ + ...Array.from({ length: MAX_DIR_STORES - 1 }, (_, index) => accessed(`/pinned-${index}`, 2, true)), + accessed("/held", 1, true), + accessed("/held", 1, true), + accessed("/active", 3), + ]) + + expect(pinned.state.size).toBe(MAX_DIR_STORES + 1) + + const once = transitionDirectoryCache(pinned.state, { type: "released", key: directoryKey("/held"), at: 4 }) + expect(once.effects).toEqual([]) + expect(once.state.get(directoryKey("/held"))?.pins).toBe(1) + + const last = transitionDirectoryCache(once.state, { type: "released", key: directoryKey("/held"), at: 4 }) + expect(last.effects).toEqual([{ type: "evict", key: directoryKey("/held") }]) + expect(last.state.size).toBe(MAX_DIR_STORES) + }) + + test("ignores a release of an unknown or unpinned directory", () => { + const state = replay([accessed("/repo", 1)]).state + + for (const directory of ["/repo", "/unknown"]) { + const result = transitionDirectoryCache(state, { type: "released", key: directoryKey(directory), at: 2 }) + expect(result.state).toBe(state) + expect(result.effects).toEqual([]) + } + }) +}) diff --git a/packages/app/src/runtime/server/global-sync/machine.ts b/packages/app/src/runtime/server/global-sync/machine.ts new file mode 100644 index 00000000000..763fd23c63d --- /dev/null +++ b/packages/app/src/runtime/server/global-sync/machine.ts @@ -0,0 +1,84 @@ +import { DIR_IDLE_TTL_MS, MAX_DIR_STORES } from "./types" +import type { DirectoryKey } from "./utils" + +/** A directory's cached store. Each reactive reader pins it while it holds the store. */ +export type DirectoryEntry = { + readonly directory: string + readonly pins: number + readonly lastAccessAt: number +} + +export type DirectoryCacheState = ReadonlyMap + +export type DirectoryCacheEvent = + | { + readonly type: "accessed" + readonly key: DirectoryKey + readonly directory: string + /** The reader holds the store until it releases it. */ + readonly pin: boolean + readonly at: number + } + | { readonly type: "released"; readonly key: DirectoryKey; readonly at: number } + +export type DirectoryCacheEffect = + | { readonly type: "create"; readonly key: DirectoryKey; readonly directory: string } + | { readonly type: "evict"; readonly key: DirectoryKey } + +export type DirectoryCacheTransition = { + readonly state: DirectoryCacheState + readonly effects: readonly DirectoryCacheEffect[] +} + +export function createDirectoryCacheState(): DirectoryCacheState { + return new Map() +} + +export function transitionDirectoryCache( + state: DirectoryCacheState, + event: DirectoryCacheEvent, +): DirectoryCacheTransition { + const entry = state.get(event.key) + + if (event.type === "accessed") { + const next = new Map(state).set(event.key, { + directory: entry?.directory ?? event.directory, + pins: (entry?.pins ?? 0) + (event.pin ? 1 : 0), + lastAccessAt: event.at, + }) + + return evict( + next, + event.at, + entry ? [] : [{ type: "create", key: event.key, directory: event.directory }], + event.key, + ) + } + + if (!entry || entry.pins === 0) return { state, effects: [] } + const next = new Map(state).set(event.key, { ...entry, pins: entry.pins - 1 }) + + if (entry.pins > 1) return { state: next, effects: [] } + + return evict(next, event.at, []) +} + +// Unpinned stores leave once idle for the TTL, and the least recently used ones leave while the cache is over its +// limit. The directory just accessed always stays. +function evict( + entries: Map, + at: number, + effects: DirectoryCacheEffect[], + keep?: DirectoryKey, +): DirectoryCacheTransition { + const overflow = entries.size - MAX_DIR_STORES + + const evicted = [...entries] + .filter(([key, entry]) => key !== keep && entry.pins === 0) + .toSorted(([, a], [, b]) => a.lastAccessAt - b.lastAccessAt) + .flatMap(([key, entry], index) => (index < overflow || at - entry.lastAccessAt >= DIR_IDLE_TTL_MS ? [key] : [])) + + evicted.forEach((key) => entries.delete(key)) + + return { state: entries, effects: [...effects, ...evicted.map((key) => ({ type: "evict" as const, key }))] } +} diff --git a/packages/app/src/runtime/server/global-sync/mcp.test.ts b/packages/app/src/runtime/server/global-sync/mcp.test.ts deleted file mode 100644 index be9ebdb82b6..00000000000 --- a/packages/app/src/runtime/server/global-sync/mcp.test.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { describe, expect, test } from "bun:test" -import { toggleMcp } from "./mcp" - -describe("toggleMcp", () => { - test("runs the status action before refreshing the owning query", async () => { - const calls: string[] = [] - - const input = (status: "connected" | "needs_auth" | "disabled") => ({ - status, - connect: async () => { - calls.push("connect") - }, - disconnect: async () => { - calls.push("disconnect") - }, - authenticate: async () => { - calls.push("authenticate") - }, - refresh: async () => { - calls.push("refresh") - }, - }) - - await toggleMcp(input("connected")) - expect(calls).toEqual(["disconnect", "refresh"]) - - calls.length = 0 - await toggleMcp(input("needs_auth")) - expect(calls).toEqual(["authenticate", "refresh"]) - - calls.length = 0 - await toggleMcp(input("disabled")) - expect(calls).toEqual(["connect", "refresh"]) - }) - - test("does not toggle a server while its connection is pending", async () => { - const calls: string[] = [] - await toggleMcp({ - status: "pending", - connect: async () => { - calls.push("connect") - }, - disconnect: async () => { - calls.push("disconnect") - }, - authenticate: async () => { - calls.push("authenticate") - }, - refresh: async () => { - calls.push("refresh") - }, - }) - expect(calls).toEqual([]) - }) -}) diff --git a/packages/app/src/runtime/server/global-sync/mcp.ts b/packages/app/src/runtime/server/global-sync/mcp.ts deleted file mode 100644 index c1805ec73b5..00000000000 --- a/packages/app/src/runtime/server/global-sync/mcp.ts +++ /dev/null @@ -1,18 +0,0 @@ -import type { McpServer } from "@opencode/client/promise" - -export async function toggleMcp(input: { - status: McpServer["status"]["status"] - connect: () => Promise - disconnect: () => Promise - authenticate: () => Promise - refresh: () => Promise -}) { - if (input.status === "pending") return - await { - connected: input.disconnect, - needs_auth: input.authenticate, - disabled: input.connect, - failed: input.connect, - }[input.status]() - await input.refresh() -} diff --git a/packages/app/src/runtime/server/global-sync/queue.test.ts b/packages/app/src/runtime/server/global-sync/queue.test.ts deleted file mode 100644 index abda5127abc..00000000000 --- a/packages/app/src/runtime/server/global-sync/queue.test.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { describe, expect, test } from "bun:test" -import { createRefreshQueue } from "./queue" -import { directoryKey } from "./utils" - -const tick = () => new Promise((resolve) => setTimeout(resolve, 10)) - -describe("createRefreshQueue", () => { - test("clears queued directories by normalized key", async () => { - const calls: string[] = [] - - const queue = createRefreshQueue({ - paused: () => false, - key: directoryKey, - bootstrap: async () => {}, - bootstrapInstance: (directory) => { - calls.push(directory) - }, - }) - - queue.push("C:\\tmp\\demo") - queue.clear("C:/tmp/demo") - - await tick() - - expect(calls).toEqual([]) - queue.dispose() - }) - - test("passes the original directory to bootstrapInstance", async () => { - const calls: string[] = [] - - const queue = createRefreshQueue({ - paused: () => false, - key: directoryKey, - bootstrap: async () => {}, - bootstrapInstance: (directory) => { - calls.push(directory) - }, - }) - - queue.push("C:\\tmp\\demo") - - await tick() - - expect(calls).toEqual(["C:\\tmp\\demo"]) - queue.dispose() - }) -}) diff --git a/packages/app/src/runtime/server/global-sync/queue.ts b/packages/app/src/runtime/server/global-sync/queue.ts deleted file mode 100644 index 62cd9ccf957..00000000000 --- a/packages/app/src/runtime/server/global-sync/queue.ts +++ /dev/null @@ -1,98 +0,0 @@ -type QueueInput = { - paused: () => boolean - bootstrap: () => Promise - bootstrapInstance: (directory: string) => Promise | void - key?: (directory: string) => string -} - -export function createRefreshQueue(input: QueueInput) { - const queued = new Map() - let root = false - let running = false - let timer: ReturnType | undefined - - const key = input.key ?? ((directory: string) => directory) - - const tick = () => new Promise((resolve) => setTimeout(resolve, 0)) - - const take = (count: number) => { - if (queued.size === 0) return [] as string[] - const items: string[] = [] - - for (const [id, directory] of queued) { - queued.delete(id) - items.push(directory) - - if (items.length >= count) break - } - - return items - } - - const schedule = () => { - if (timer) return - timer = setTimeout(() => { - timer = undefined - void drain() - }, 0) - } - - const push = (directory: string) => { - if (!directory) return - queued.set(key(directory), directory) - - if (input.paused()) return - schedule() - } - - const refresh = () => { - root = true - - if (input.paused()) return - schedule() - } - - async function drain() { - if (running) return - running = true - - try { - while (true) { - if (input.paused()) return - - if (root) { - root = false - await input.bootstrap() - await tick() - continue - } - - const dirs = take(2) - - if (dirs.length === 0) return - await Promise.all(dirs.map((dir) => input.bootstrapInstance(dir))) - await tick() - } - } finally { - running = false - - // oxlint-disable-next-line no-unsafe-finally -- intentional: early return skips schedule() when paused - if (input.paused()) return - - if (root || queued.size) schedule() - } - } - - return { - push, - refresh, - clear(directory: string) { - queued.delete(key(directory)) - }, - dispose() { - if (!timer) return - clearTimeout(timer) - timer = undefined - }, - } -} diff --git a/packages/app/src/runtime/server/global-sync/types.ts b/packages/app/src/runtime/server/global-sync/types.ts index 45dfa2b6cca..ecd2cd32594 100644 --- a/packages/app/src/runtime/server/global-sync/types.ts +++ b/packages/app/src/runtime/server/global-sync/types.ts @@ -1,14 +1,26 @@ -import type { Agent, Config, LspStatus, Path, ProviderListResponse, VcsInfo } from "@/runtime/server/types" +import type { + Agent, + Config, + Path, + Project, + ProviderAuthResponse, + ProviderListResponse, + VcsInfo, +} from "@/runtime/server/types" import type { ReferenceInfo } from "@opencode/client/promise" -import type { CommandInfo, McpResource, McpServer } from "@opencode/client/promise" -import type { Accessor } from "solid-js" -import type { SetStoreFunction, Store } from "solid-js/store" +import type { CommandInfo, McpResource } from "@opencode/client/promise" import { ProjectState } from "../persistence" export type ProjectMeta = NonNullable +export type GlobalStore = { + path: Path + project: Project[] + provider_auth: ProviderAuthResponse + config: Config +} + export type State = { - status: "loading" | "partial" | "complete" agent: Agent[] command: CommandInfo[] reference: ReferenceInfo[] @@ -19,49 +31,12 @@ export type State = { config: Config path: Path mcp_ready: boolean - mcp: { - [name: string]: McpServer["status"] - } mcp_resource: { [key: string]: McpResource } - lsp_ready: boolean - lsp: LspStatus[] vcs: VcsInfo | undefined } -export type MetaCache = { - store: Store - setStore: SetStoreFunction - ready: Accessor -} - -export type ChildOptions = { - bootstrap?: boolean - mcp?: boolean -} - -export type DirState = { - lastAccessAt: number -} - -export type EvictPlan = { - stores: string[] - state: Map - pins: Set - max: number - ttl: number - now: number -} - -export type DisposeCheck = { - directory: string - hasStore: boolean - pinned: boolean - booting: boolean - loadingSessions: boolean -} - export const MAX_DIR_STORES = 30 export const DIR_IDLE_TTL_MS = 20 * 60 * 1000 diff --git a/packages/app/src/runtime/server/runtime.tsx b/packages/app/src/runtime/server/runtime.tsx index e7f8cf75f00..600abad8879 100644 --- a/packages/app/src/runtime/server/runtime.tsx +++ b/packages/app/src/runtime/server/runtime.tsx @@ -260,7 +260,7 @@ function createServerController( function withLocalMeta(project: Project): Project { if (project.id !== "global") return project - return { ...project, ...sync.child(project.worktree, { bootstrap: false })[0].projectMeta } + return { ...project, ...sync.child(project.worktree)[0].projectMeta } } const list = createMemo(() => sync.data.project.map(withLocalMeta)) diff --git a/packages/app/src/runtime/server/server-sync/connection.test.ts b/packages/app/src/runtime/server/server-sync/connection.test.ts deleted file mode 100644 index 06762bc15ba..00000000000 --- a/packages/app/src/runtime/server/server-sync/connection.test.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { expect, test } from "bun:test" -import { reconnectOrder } from "./connection" - -test("held directories refresh before the rest, otherwise keeping their order", () => { - const held = new Set(["/b", "/d"]) - expect(reconnectOrder(["/a", "/b", "/c", "/d"], (directory) => held.has(directory))).toEqual(["/b", "/d", "/a", "/c"]) - expect(reconnectOrder(["/a", "/c"], (directory) => held.has(directory))).toEqual(["/a", "/c"]) -}) diff --git a/packages/app/src/runtime/server/server-sync/connection.ts b/packages/app/src/runtime/server/server-sync/connection.ts index 6acbb6092ae..e46464e92dd 100644 --- a/packages/app/src/runtime/server/server-sync/connection.ts +++ b/packages/app/src/runtime/server/server-sync/connection.ts @@ -21,8 +21,3 @@ export function createConnectionSync(input: { return { handleEvent } } - -// Directories a mounted view holds refresh first; the rest keep their existing order behind them. -export function reconnectOrder(directories: string[], held: (directory: string) => boolean) { - return [...directories.filter(held), ...directories.filter((directory) => !held(directory))] -} diff --git a/packages/app/src/runtime/server/sync.tsx b/packages/app/src/runtime/server/sync.tsx index 0bd6b329968..4a6ac892b6d 100644 --- a/packages/app/src/runtime/server/sync.tsx +++ b/packages/app/src/runtime/server/sync.tsx @@ -1,12 +1,9 @@ -import type { Config, Path, Project, ProviderAuthResponse } from "@/runtime/server/types" -import { showToast } from "@/shell/notifications/toast" -import { getFilename } from "@opencode/util/path" -import { getOwner, onCleanup, untrack } from "solid-js" +import type { Config, Project } from "@/runtime/server/types" +import { getOwner, onCleanup } from "solid-js" import { createStore } from "solid-js/store" import { useLanguage } from "@/runtime/i18n/language" import { type ServerSDK } from "./client" import { - bootstrapDirectory, bootstrapGlobal, loadGlobalConfigQuery, loadPathQuery, @@ -14,61 +11,26 @@ import { loadProjectsQuery, } from "./global-sync/bootstrap" import { createChildStoreManager } from "./global-sync/child-store" -import type { ProjectMeta } from "./global-sync/types" -import { formatServerError } from "@/runtime/server/errors" -import { queryOptions, useMutation, useQueries, useQuery, useQueryClient } from "@tanstack/solid-query" -import { createRefreshQueue } from "./global-sync/queue" -import { directoryKey, normalizeProjectInfo, updateProjectInfo } from "./global-sync/utils" -import { PathKey } from "@/workspaces/path-key" -import type { ServerScope } from "@/runtime/server/scope" +import type { GlobalStore, ProjectMeta } from "./global-sync/types" +import { useMutation, useQueries, useQuery, useQueryClient } from "@tanstack/solid-query" +import { normalizeProjectInfo, updateProjectInfo } from "./global-sync/utils" import { persisted } from "@/runtime/persistence/storage" -import type { ServerApi } from "@/runtime/server/api" -import { toggleMcp } from "./global-sync/mcp" -import { createConnectionSync, reconnectOrder } from "./server-sync/connection" -import { usePlatform } from "@/runtime/platform/platform" +import { createConnectionSync } from "./server-sync/connection" import type { Data } from "@opencode/client/solid" import { createWorktreeInventory, withWorktreeInventory, worktreeInventoryViewKey } from "@/workspaces/inventory" -type GlobalStore = { - path: Path - project: Project[] - provider_auth: ProviderAuthResponse - config: Config - reload: undefined | "pending" | "complete" -} - -const loadLspQuery = (scope: ServerScope, directory: string) => - queryOptions({ - queryKey: [scope, directory, "lsp"] as const, - // TODO: Restore LSP status when the V2 client exposes an LSP API. - queryFn: async () => [], - }) - -function makeQueryOptionsApi(scope: ServerScope, serverAPI: () => ServerApi) { - return { - globalConfig: () => loadGlobalConfigQuery(scope), - path: () => loadPathQuery(scope, null, serverAPI().location), - lsp: (directory: PathKey) => loadLspQuery(scope, directory), - } -} - -export type QueryOptionsApi = ReturnType - function createServerSyncContextInner(serverSDK: ServerSDK, data: Data) { const language = useLanguage() - const platform = usePlatform() const owner = getOwner() if (!owner) throw new Error("ServerSync must be created within owner") - const booting = new Map>() - const queryOptionsApi = makeQueryOptionsApi(serverSDK.scope, () => serverSDK.api) const connected = () => serverSDK.connection.status() === "connected" const [configQuery, pathQuery] = useQueries(() => ({ queries: [ - { ...queryOptionsApi.globalConfig(), enabled: connected() }, - { ...queryOptionsApi.path(), enabled: connected() }, + { ...loadGlobalConfigQuery(serverSDK.scope), enabled: connected() }, + { ...loadPathQuery(serverSDK.scope, null, serverSDK.api.location), enabled: connected() }, ], })) @@ -87,9 +49,6 @@ function createServerSyncContextInner(serverSDK: ServerSDK, data: Data) { return configQuery.data ?? {} }, - get reload() { - return updateConfigMutation.isPending ? "pending" : undefined - }, }) const queryClient = useQueryClient() @@ -129,45 +88,11 @@ function createServerSyncContextInner(serverSDK: ServerSDK, data: Data) { enabled: connected(), })) - const paused = () => untrack(() => globalStore.reload) !== undefined - - const queue = createRefreshQueue({ - paused, - key: directoryKey, - bootstrap: () => queryClient.fetchQuery({ queryKey: [serverSDK.scope, "bootstrap"] }), - bootstrapInstance, - }) - const children = createChildStoreManager({ owner, - connected, scope: serverSDK.scope, persist: persisted, - isBooting: (directory) => booting.has(directory), - isLoadingSessions: () => false, - onBootstrap: (directory) => { - if (!connected()) return - void bootstrapInstance(directory) - }, - onMcp: (directory) => { - void Promise.all([ - data.location.command.sync({ directory }), - data.location.mcp.server.sync({ directory }), - data.location.mcp.resource.sync({ directory }), - ]).catch((err) => { - showToast({ - variant: "error", - title: language.t("toast.project.reloadFailed.title", { project: getFilename(directory) }), - description: formatServerError(err, language.t), - }) - }) - }, - onDispose: (directory) => { - const key = directoryKey(directory) - queue.clear(key) - }, translate: language.t, - queryOptions: queryOptionsApi, data, global: { get path() { @@ -186,55 +111,9 @@ function createServerSyncContextInner(serverSDK: ServerSDK, data: Data) { }, connected: () => { if (bootstrap.data !== undefined && !bootstrap.isFetching) void bootstrap.refetch() - // The refresh queue re-syncs two directories at a time, held ones first. Syncing every active - // directory here as well sent the whole catalog fan-out for all of them at once. - reconnectOrder(Object.keys(children.children).filter(children.active), children.pinned).forEach((directory) => - queue.push(directory), - ) }, }) - async function bootstrapInstance(directory: string) { - const key = directoryKey(directory) - - if (!key) return - const pending = booting.get(key) - - if (pending) return pending - - children.pin(key) - - const promise = Promise.resolve().then(async () => { - const child = children.ensureChild(directory) - await Promise.all([ - data.location.sync({ directory }), - bootstrapDirectory({ - directory, - scope: serverSDK.scope, - mcp: children.mcp(key), - global: { - config: globalStore.config, - path: globalStore.path, - project: globalStore.project, - }, - api: serverSDK.api, - store: child[0], - setStore: child[1], - translate: language.t, - queryClient, - }), - ]) - }) - - booting.set(key, promise) - void promise.finally(() => { - booting.delete(key) - children.unpin(key) - }) - - return promise - } - // Mirrors the server's list: unarchived projects, most recently active first. Unknown projects join the list and // archived ones leave it. function applyProjectUpdate(update: Parameters[1]) { @@ -278,30 +157,10 @@ function createServerSyncContextInner(serverSDK: ServerSDK, data: Data) { return } - if (!event.location) { - if (event.type === "config.updated" || event.type === "agent.updated") bootstrap.refetch() - - return - } - - const directory = event.location.directory - const key = directoryKey(directory) - - if (!children.active(key)) return - children.mark(key) - - if (event.type === "config.updated" || event.type === "agent.updated") queue.push(key) + if (!event.location && (event.type === "config.updated" || event.type === "agent.updated")) bootstrap.refetch() }) onCleanup(unsub) - onCleanup(() => { - queue.dispose() - }) - onCleanup(() => { - for (const directory of Object.keys(children.children)) { - children.disposeDirectory(directoryKey(directory)) - } - }) const projectApi = { update: applyProjectUpdate, @@ -330,61 +189,10 @@ function createServerSyncContextInner(serverSDK: ServerSDK, data: Data) { data: globalStore, set: setGlobalStore, child: children.child, - disableMcp: children.disableMcp, // bootstrap, updateConfig: updateConfigMutation.mutateAsync, project: projectApi, worktrees, - mcp: { - toggle: async (directory: string, name: string) => { - const key = directoryKey(directory) - const status = children.child(key, { bootstrap: false })[0].mcp[name]?.status - - if (!status) return - await toggleMcp({ - status, - connect: async () => { - await serverSDK.api.experimental.mcp.connect({ server: name, location: { directory: key } }) - }, - disconnect: async () => { - await serverSDK.api.experimental.mcp.disconnect({ server: name, location: { directory: key } }) - }, - authenticate: async () => { - const server = (await serverSDK.api.mcp.list({ location: { directory: key } })).data.find( - (item) => item.name === name, - ) - - if (!server?.integrationID) throw new Error(`MCP server ${name} has no authentication integration`) - - const integration = await serverSDK.api.integration.get({ - integrationID: server.integrationID, - location: { directory: key }, - }) - - const method = integration.data?.methods.find((item) => item.type === "oauth" && !item.form?.length) - - if (!method || method.type !== "oauth") - throw new Error(`MCP server ${name} requires an interactive authentication form`) - - const attempt = await serverSDK.api.integration.oauth.connect({ - integrationID: server.integrationID, - methodID: method.id, - location: { directory: key }, - }) - - platform.openExternal(attempt.data.url) - }, - refresh: async () => { - data.location.mcp.server.invalidate({ directory: key }) - data.location.mcp.resource.invalidate({ directory: key }) - await Promise.all([ - data.location.mcp.server.sync({ directory: key }), - data.location.mcp.resource.sync({ directory: key }), - ]) - }, - }) - }, - }, } } diff --git a/packages/app/test-browser/child-store-eviction.test.ts b/packages/app/test-browser/child-store-eviction.test.ts index 5f7f69b7ac3..71dd6f3c9b4 100644 --- a/packages/app/test-browser/child-store-eviction.test.ts +++ b/packages/app/test-browser/child-store-eviction.test.ts @@ -1,13 +1,11 @@ import { expect, test } from "bun:test" -import { createComponent, createMemo, createRoot, getOwner } from "solid-js" +import { 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. @@ -25,12 +23,6 @@ const data = { }, } 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[] = [] @@ -61,41 +53,22 @@ test("keeps project stores alive when a memo over more directories than the stor } const root = createRoot((dispose) => { - let manager: ReturnType | undefined + const owner = getOwner() - createComponent(QueryClientProvider, { - client: new QueryClient(), - get children() { - const owner = getOwner() + if (!owner) throw new Error("owner required") - 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 - }, + const manager = createChildStoreManager({ + owner, + scope: ServerScope.local, + persist, + translate: (key) => key, + data, + global: { path: { state: "", config: "", worktree: "", directory: "", home: "" } }, }) - if (!manager) throw new Error("manager required") - const children = manager + createMemo(() => directories.map((directory) => manager.child(directory)[0].projectMeta)) - createMemo(() => directories.map((directory) => children.child(directory, { bootstrap: false })[0].projectMeta)) - - return { manager: children, dispose } + return { manager, dispose } }) try { @@ -103,7 +76,8 @@ test("keeps project stores alive when a memo over more directories than the stor 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( + // Read outside the memo, so these reads pin nothing; a store evicted earlier would be created again here. + expect(directories.map((directory) => root.manager.child(directory)[0].projectMeta?.name)).toEqual( directories.map(() => "stored"), ) } finally {