Skip to content

Commit 8a8a9d4

Browse files
authored
refactor(app): replace dead directory bootstrap with a directory cache machine (#61753)
1 parent 0cb3d68 commit 8a8a9d4

18 files changed

Lines changed: 400 additions & 1216 deletions

‎packages/app/src/runtime/server/global-sync/bootstrap.test.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test } from "bun:test"
22
import { QueryClient } from "@tanstack/solid-query"
3-
import { OpenCode } from "@opencode/client/promise"
3+
import { OpenCode, type LocationGetInput } from "@opencode/client/promise"
44
import { createStore } from "solid-js/store"
55
import { bootstrapGlobal, inferHomeDirectory, loadPathQuery, loadProjectsQuery } from "./bootstrap"
66
import { ServerScope } from "@/runtime/server/scope"
@@ -35,7 +35,6 @@ test("bootstraps projects through the native store setter and preserves subseque
3535
project: [],
3636
provider_auth: {},
3737
config: {},
38-
reload: undefined,
3938
})
4039

4140
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
@@ -114,14 +113,16 @@ describe("query keys", () => {
114113
test("partitions identical directories by server scope and loads current location metadata", async () => {
115114
const calls: unknown[] = []
116115

116+
// SAFETY: the path query reads only `directory` and `project.directory` from the location it gets.
117117
const location = {
118-
get: async (input: unknown) => {
118+
get: async (input?: LocationGetInput) => {
119119
calls.push(input)
120120

121121
return { directory: "/repo/subpath", project: { id: "project", directory: "/repo" } }
122122
},
123123
} as ServerApi["location"]
124124

125+
// SAFETY: a remote server's scope is its origin string, which is how the app scopes it.
125126
const remote = "https://debian.example" as typeof ServerScope.local
126127

127128
expect([...loadPathQuery(ServerScope.local, "/repo", location).queryKey]).toEqual(["local", "/repo", "path"])
Lines changed: 15 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,13 @@
1-
import type { Config, Path, Project, ProviderAuthResponse } from "@/runtime/server/types"
1+
import type { Config, Path } from "@/runtime/server/types"
22
import type { LocationGetInput, LocationGetOutput, ProjectListOutput } from "@opencode/client/promise"
3-
import { showToast } from "@/shell/notifications/toast"
4-
import { getFilename } from "@opencode/util/path"
53
import { retry } from "@opencode/util/retry"
6-
import { reconcile, type SetStoreFunction, type Store } from "solid-js/store"
7-
import type { State } from "./types"
4+
import type { SetStoreFunction } from "solid-js/store"
5+
import type { GlobalStore } from "./types"
86
import { normalizeProjectInfo } from "./utils"
9-
import { formatServerError } from "@/runtime/server/errors"
107
import { QueryClient, queryOptions } from "@tanstack/solid-query"
118
import type { ServerScope } from "@/runtime/server/scope"
129
import { withWorktreeInventory, worktreeInventoryKey } from "@/workspaces/inventory"
1310

14-
type GlobalStore = {
15-
path: Path
16-
project: Project[]
17-
provider_auth: ProviderAuthResponse
18-
config: Config
19-
reload: undefined | "pending" | "complete"
20-
}
21-
22-
function waitForPaint() {
23-
return new Promise<void>((resolve) => {
24-
let done = false
25-
26-
const finish = () => {
27-
if (done) return
28-
done = true
29-
resolve()
30-
}
31-
32-
const timer = setTimeout(finish, 50)
33-
34-
if (typeof requestAnimationFrame !== "function") return
35-
requestAnimationFrame(() => {
36-
setTimeout(() => {
37-
clearTimeout(timer)
38-
finish()
39-
}, 0)
40-
})
41-
})
42-
}
43-
44-
function errors(list: PromiseSettledResult<unknown>[]) {
45-
return list.filter((item): item is PromiseRejectedResult => item.status === "rejected").map((item) => item.reason)
46-
}
47-
48-
function runAll(list: Array<() => Promise<unknown>>) {
49-
return Promise.allSettled(list.map((item) => item()))
50-
}
51-
5211
export const loadGlobalConfigQuery = (scope: ServerScope) =>
5312
queryOptions({
5413
queryKey: [scope, "config"],
@@ -100,26 +59,18 @@ export async function bootstrapGlobal(input: {
10059
setGlobalStore: SetStoreFunction<GlobalStore>
10160
queryClient: QueryClient
10261
}) {
103-
const slow = [
104-
() => input.queryClient.fetchQuery(loadGlobalConfigQuery(input.scope)),
105-
() =>
106-
input.queryClient
107-
.fetchQuery(loadPathQuery(input.scope, null, input.serverAPI.location))
108-
.then((data) => input.setGlobalStore("path", data)),
109-
() =>
110-
loadProjects({
111-
scope: input.scope,
112-
project: input.serverAPI.project,
113-
setGlobalStore: input.setGlobalStore,
114-
queryClient: input.queryClient,
115-
}),
116-
]
117-
118-
await runAll(slow)
119-
}
120-
121-
function projectID(directory: string, projects: Project[]) {
122-
return projects.find((project) => project.worktree === directory || project.sandboxes?.includes(directory))?.id
62+
await Promise.allSettled([
63+
input.queryClient.fetchQuery(loadGlobalConfigQuery(input.scope)),
64+
input.queryClient
65+
.fetchQuery(loadPathQuery(input.scope, null, input.serverAPI.location))
66+
.then((data) => input.setGlobalStore("path", data)),
67+
loadProjects({
68+
scope: input.scope,
69+
project: input.serverAPI.project,
70+
setGlobalStore: input.setGlobalStore,
71+
queryClient: input.queryClient,
72+
}),
73+
])
12374
}
12475

12576
export function inferHomeDirectory(path: string) {
@@ -143,57 +94,3 @@ export const loadPathQuery = (scope: ServerScope, directory: string | null, api:
14394
home: inferHomeDirectory(location.directory) || inferHomeDirectory(location.project.directory),
14495
})),
14596
})
146-
147-
export async function bootstrapDirectory(input: {
148-
directory: string
149-
scope: ServerScope
150-
mcp: boolean
151-
api: {
152-
readonly project: ProjectApi
153-
readonly location: LocationApi
154-
}
155-
store: Store<State>
156-
setStore: SetStoreFunction<State>
157-
translate: (key: string, vars?: Record<string, string | number>) => string
158-
global: {
159-
config: Config
160-
path: Path
161-
project: Project[]
162-
}
163-
queryClient: QueryClient
164-
}) {
165-
const loading = input.store.status !== "complete"
166-
const seededProject = projectID(input.directory, input.global.project)
167-
168-
if (seededProject) input.setStore("project", seededProject)
169-
170-
if (Object.keys(input.store.config).length === 0 && Object.keys(input.global.config).length > 0) {
171-
input.setStore("config", reconcile(input.global.config, { merge: false }))
172-
}
173-
174-
if (loading) input.setStore("status", "partial")
175-
176-
const slow = [
177-
seededProject
178-
? undefined
179-
: () =>
180-
retry(() => input.api.location.get({ location: { directory: input.directory } })).then((location) =>
181-
input.setStore("project", location.project.id),
182-
),
183-
].filter((task): task is () => Promise<void> => !!task)
184-
185-
await waitForPaint()
186-
const slowErrs = errors(await runAll(slow))
187-
188-
if (slowErrs.length > 0) {
189-
console.error("Failed to finish bootstrap instance", slowErrs[0])
190-
const project = getFilename(input.directory)
191-
showToast({
192-
variant: "error",
193-
title: input.translate("toast.project.reloadFailed.title", { project }),
194-
description: formatServerError(slowErrs[0], input.translate),
195-
})
196-
}
197-
198-
if (loading && slowErrs.length === 0) input.setStore("status", "complete")
199-
}

0 commit comments

Comments
 (0)