fix(server): avoid booting unloaded locations on pending permission reads

Listing pending permissions for a session booted its Location, and a booted
Location costs about 35 MB, so browsing the sidebar left them piling up.
Pending requests only exist in a loaded Location's memory, so an unloaded
one has nothing to report: answer from an already loaded Location, or
return an empty list without booting one.

The form list already does this through FormLocationMiddleware; this is the
same rule for the permission list.
This commit is contained in:
Kit Langton 2026-11-11 10:48:29 -08:00
parent 144438a9da
commit f62c8003bb
4 changed files with 25 additions and 10 deletions

View file

@ -88,10 +88,10 @@ test("Promise instances are lazy, share by key and Location, and stay isolated b
expect(await second.sessions.get({ sessionID })).toEqual(separateHost)
expect(configured).toEqual([])
expect(setups).toEqual([])
// Permission and form lists read instance services, so they acquire the Session's instance.
// Pending permissions and forms live only in a loaded instance, so listing them never acquires one.
expect(await first.permission.list({ sessionID })).toEqual([])
expect(await first.session.form.list({ sessionID })).toEqual([])
expect(configured).toEqual(["first:alpha"])
expect(configured).toEqual([])
await Promise.all(
[

View file

@ -60,6 +60,9 @@ export const PermissionHandler = HttpApiBuilder.group(Api, "server.permission",
"session.permission.list",
Effect.fn(function* (ctx) {
const session = yield* sessionInfo(sessions, ctx.params.sessionID)
// Pending requests live in a loaded Location's memory, so an unloaded one has none to report.
// Answering must not boot it: clients poll whole subagent trees, including deleted worktrees.
if (instances.live && !(yield* instances.live(session))) return { data: [] }
const requests = yield* Permission.Service.use((permission) =>
permission.forSession(ctx.params.sessionID),
).pipe(instances.provide(session), locationErrors)

View file

@ -38,12 +38,7 @@ it.live("returns LocationNotFoundError for a missing folder and recovers once it
const session = Schema.decodeUnknownSync(Schema.Struct({ data: Session.Info }))(
yield* Effect.promise(() => created.json()),
).data
const endpoints = [
"/api/model",
"/api/integration",
`/api/session/${session.id}/permission`,
"/api/session/global/form",
]
const endpoints = ["/api/model", "/api/integration", "/api/session/global/form"]
for (const endpoint of endpoints) {
const response = yield* Effect.promise(() =>
handler(
@ -59,6 +54,17 @@ it.live("returns LocationNotFoundError for a missing folder and recovers once it
message: `Location not found: ${directory}`,
})
}
// Pending requests live in memory, so listing them never boots the Location and cannot fail on its folder.
const pending = yield* Effect.promise(() =>
handler(
new Request(`http://opencode.local/api/session/${session.id}/permission`, {
headers: { "x-opencode-directory": encodeURIComponent(directory) },
}),
),
)
expect(pending.status).toBe(200)
expect(yield* Effect.promise(() => pending.json())).toEqual({ data: [] })
yield* Effect.promise(() => fs.mkdir(directory))
const recovered = yield* Effect.promise(() =>
handler(

View file

@ -145,6 +145,7 @@ it.live(
)
const selector = Instance.Service.of({
provide: (session) => Effect.provide(instances.get(session.id)),
live: (session) => RcMap.has(instances.rcMap, session.id),
})
const bindings: LayerNode.Replacements = [
...replacements,
@ -197,8 +198,13 @@ it.live(
expect(yield* Effect.promise<unknown>(() => response.json())).toEqual({ data: [] })
}
}
// Reading permissions or forms builds the Session's Instance and starts its plugin activation at once; the
// ordering assertion after the prompts is what proves each Session boots exactly once.
// Pending permissions and forms live only in a loaded Instance, so listing them for these cold Sessions
// must answer without acquiring one. `live` is the immediate signal; plugin activation and `boots` land later.
for (const config of configs) {
const session = yield* sessions.get(config.id)
expect(yield* instances.live!(session)).toBe(false)
}
expect(boots).toEqual([])
for (const config of configs) {
const session = yield* sessions.get(config.id)