Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 54 additions & 4 deletions packages/app/src/context/global-sync/bootstrap.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, test } from "bun:test"
import { createStore } from "solid-js/store"
import { QueryClient } from "@tanstack/solid-query"
import type { Config, OpencodeClient, Project } from "@opencode-ai/sdk/v2/client"
import type { Config, OpencodeClient, PermissionRequest, Project } from "@opencode-ai/sdk/v2/client"
import type { NormalizedProviderListResponse } from "@opencode-ai/ui/context"
import { bootstrapDirectory, loadPathQuery, loadProvidersQuery } from "./bootstrap"
import type { State, VcsCache } from "./types"
Expand All @@ -10,9 +10,8 @@ import { ServerScope } from "@/utils/server-scope"
const provider = { all: new Map(), connected: [], default: {} } satisfies NormalizedProviderListResponse

describe("bootstrapDirectory", () => {
test("marks a loading directory partial during bootstrap and complete after success", async () => {
const mcpReads: string[] = []
const [store, setStore] = createStore<State>({
function createState(overrides: Partial<State> = {}) {
return createStore<State>({
status: "loading",
agent: [],
command: [],
Expand Down Expand Up @@ -42,7 +41,13 @@ describe("bootstrapDirectory", () => {
message: {},
part: {},
part_text_accum_delta: {},
...overrides,
})
}

test("marks a loading directory partial during bootstrap and complete after success", async () => {
const mcpReads: string[] = []
const [store, setStore] = createState()

await bootstrapDirectory({
directory: "/project",
Expand Down Expand Up @@ -90,6 +95,51 @@ describe("bootstrapDirectory", () => {
expect(store.status).toBe("complete")
expect(mcpReads).toEqual([])
})

test("ignores stale session references while warming permission sessions", async () => {
const missing = new Error("Session not found: ses_missing", { cause: { status: 404 } })
const permission = { id: "perm_1", sessionID: "ses_missing" } as PermissionRequest
const [store, setStore] = createState()

await bootstrapDirectory({
directory: "/project",
scope: ServerScope.local,
mcp: false,
global: {
config: {} satisfies Config,
path: { state: "", config: "", worktree: "/project", directory: "/project", home: "/home" },
project: [{ id: "project", worktree: "/project" } as Project],
provider,
},
sdk: {
app: { agents: async () => ({ data: [] }) },
config: { get: async () => ({ data: {} }) },
session: {
get: async () => {
throw missing
},
status: async () => ({ data: {} }),
},
vcs: { get: async () => ({ data: undefined }) },
command: { list: async () => ({ data: [] }) },
permission: { list: async () => ({ data: [permission] }) },
question: { list: async () => ({ data: [] }) },
mcp: { status: async () => ({ data: {} }) },
provider: { list: async () => ({ data: { all: [], connected: [], default: {} } }) },
} as unknown as OpencodeClient,
store,
setStore,
vcsCache: { setStore() {} } as unknown as VcsCache,
loadSessions() {},
translate: (key) => key,
queryClient: new QueryClient(),
})

await new Promise((resolve) => setTimeout(resolve, 80))

expect(store.status).toBe("complete")
expect(store.permission.ses_missing).toEqual([permission])
})
})

describe("query keys", () => {
Expand Down
21 changes: 16 additions & 5 deletions packages/app/src/context/global-sync/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ function mergeSession(setStore: SetStoreFunction<State>, session: Session) {
})
}

const isNotFound = (error: unknown) =>
error instanceof Error &&
typeof error.cause === "object" &&
error.cause !== null &&
(error.cause as { status?: unknown }).status === 404

function warmSessions(input: {
ids: string[]
store: Store<State>
Expand All @@ -171,11 +177,16 @@ function warmSessions(input: {
if (ids.length === 0) return Promise.resolve()
return Promise.all(
ids.map((sessionID) =>
retry(() => input.sdk.session.get({ sessionID })).then((x) => {
const session = x.data
if (!session?.id) return
mergeSession(input.setStore, session)
}),
retry(() => input.sdk.session.get({ sessionID }))
.then((x) => {
const session = x.data
if (!session?.id) return
mergeSession(input.setStore, session)
})
.catch((error) => {
if (isNotFound(error)) return
throw error
}),
),
).then(() => undefined)
}
Expand Down
Loading