Skip to content

Commit a2a83eb

Browse files
committed
perf(opencode): list sessions without bootstrapping an instance (anomalyco#171)
GET /session and /umbrella/session only need the project id and directory; resolve them through Project.fromDirectory instead of pinning a full Instance, so dashboard and sidebar listing over hundreds of historical directories no longer boots plugins, LSP and MCP per directory.
1 parent 8cd4dea commit a2a83eb

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎packages/opencode/src/project/instance-store.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface LoadInput {
2525
export interface Interface {
2626
readonly load: (input: LoadInput) => Effect.Effect<InstanceContext>
2727
readonly peek: (directory: string) => Effect.Effect<InstanceContext | undefined>
28+
readonly resolve: (directory: string) => Effect.Effect<InstanceContext>
2829
readonly size: () => number
2930
readonly reload: (input: LoadInput) => Effect.Effect<InstanceContext>
3031
readonly dispose: (ctx: InstanceContext) => Effect.Effect<void>
@@ -234,6 +235,17 @@ const layer: Layer.Layer<
234235
return Exit.isSuccess(exit) ? exit.value : undefined
235236
})
236237

238+
// Project identity without bootstrapping: DB-only routes (session listing)
239+
// need only the project id and directory, never plugins, LSP or MCP.
240+
const resolve = (input: string) =>
241+
Effect.gen(function* () {
242+
const cached = yield* peek(input)
243+
if (cached) return cached
244+
const directory = FSUtil.resolve(input)
245+
const result = yield* project.fromDirectory(directory)
246+
return { directory, worktree: result.sandbox, project: result.project } satisfies InstanceContext
247+
})
248+
237249
const sweep = Effect.fn("InstanceStore.sweep")(function* () {
238250
const now = yield* Clock.currentTimeMillis
239251
let evicted = 0
@@ -276,6 +288,7 @@ const layer: Layer.Layer<
276288
return Service.of({
277289
load,
278290
peek,
291+
resolve,
279292
size: () => cache.size,
280293
reload,
281294
dispose,

‎packages/opencode/src/server/routes/instance/httpapi/middleware/instance-context.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ function provideInstanceContext<E>(
3333
const cached = yield* store.peek(directory)
3434
if (!cached) return HttpServerResponse.jsonUnsafe(pathname === "/session/status" ? {} : [])
3535
}
36-
const ctx = yield* store.load({ directory })
36+
// Listing is DB-only; resolve project identity without pinning an Instance.
37+
const ctx =
38+
request.method === "GET" && ["/session", "/umbrella/session"].includes(pathname)
39+
? yield* store.resolve(directory)
40+
: yield* store.load({ directory })
3741
return yield* effect.pipe(
3842
Effect.provideService(InstanceRef, ctx),
3943
Effect.provideService(WorkspaceRef, route.workspaceID),

‎packages/opencode/test/server/httpapi-session.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ describe("session HttpApi", () => {
249249
expect(yield* responseJson(response)).toEqual(body)
250250
expect(store.size()).toBe(0)
251251
}
252+
// Listing resolves the project without pinning an Instance either.
253+
const listed = yield* request(`/session?directory=${encodeURIComponent(dir)}`, { headers })
254+
expect(listed.status).toBe(200)
255+
expect(yield* responseJson(listed)).toEqual([])
256+
expect(store.size()).toBe(0)
252257
const created = yield* requestJson<Session.Info>(`/session?directory=${encodeURIComponent(dir)}`, {
253258
method: "POST", headers,
254259
})

0 commit comments

Comments
 (0)