Skip to content

Commit fb72e25

Browse files
authored
fix(acp): wait for a cold location's plugins before the first catalog read (#53358)
1 parent 184890e commit fb72e25

3 files changed

Lines changed: 53 additions & 9 deletions

File tree

‎packages/cli/src/acp/catalog.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,17 @@ export const make = Effect.fnUntraced(function* (client: OpenCodeClient) {
124124
} satisfies Interface
125125
})
126126

127-
const load = (client: OpenCodeClient, cwd: string) =>
128-
read(client, cwd).pipe(
129-
// Providers may still be discovering models after startup.
130-
Effect.retry({
131-
while: (error) => error._tag === "ACPCatalogNotReadyError",
132-
schedule: Schedule.spaced("25 millis").pipe(Schedule.upTo({ duration: "5 seconds" })),
133-
}),
134-
Effect.withSpan("cli.acp.catalog.load"),
127+
const poll = Schedule.spaced("25 millis").pipe(Schedule.upTo({ duration: "5 seconds" }))
128+
129+
// A cold Location lists no plugins until activation finishes, and providers may still discover models after that.
130+
const load = Effect.fn("cli.acp.catalog.load")(function* (client: OpenCodeClient, cwd: string) {
131+
yield* client.plugin
132+
.list({ location: { directory: cwd } })
133+
.pipe(Effect.repeat({ until: (plugins) => plugins.data.length > 0, schedule: poll }), Effect.ignore)
134+
return yield* read(client, cwd).pipe(
135+
Effect.retry({ while: (error) => error._tag === "ACPCatalogNotReadyError", schedule: poll }),
135136
)
137+
})
136138

137139
const read = Effect.fnUntraced(function* (client: OpenCodeClient, cwd: string) {
138140
const location = { directory: cwd }

‎packages/cli/test/acp/catalog.test.ts‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test } from "bun:test"
22
import { currentValue } from "./select-options"
3-
import { rpcError, secondModel, startSession } from "./wire-fixture"
3+
import { rpcError, secondModel, startSession, startWire } from "./wire-fixture"
44

55
describe("acp catalog and config options over the wire", () => {
66
test("switches model, effort, and mode against the warm catalog", async () => {
@@ -33,4 +33,34 @@ describe("acp catalog and config options over the wire", () => {
3333
data: { modelId: "test/missing-model" },
3434
})
3535
})
36+
37+
test("answers the first session after a cold location activates its plugins (#52729, #52472)", async () => {
38+
await using acp = await startWire()
39+
const plugins = acp.server.catalog.plugins.splice(0)
40+
acp.server.catalog.models.splice(1)
41+
await acp.initialize()
42+
const activated = Bun.sleep(200).then(() => {
43+
acp.server.catalog.plugins.push(...plugins)
44+
acp.server.catalog.models.push(secondModel)
45+
})
46+
47+
const session = await acp.newSession()
48+
const selected = await acp.request("session/set_config_option", {
49+
sessionId: session.sessionId,
50+
configId: "model",
51+
value: "test/second-model",
52+
})
53+
await activated
54+
55+
expect(session.configOptions).toContainEqual(
56+
expect.objectContaining({
57+
id: "model",
58+
options: [
59+
{ value: "test/second-model", name: "test/Second Model" },
60+
{ value: "test/test-model", name: "test/Test Model" },
61+
],
62+
}),
63+
)
64+
expect(currentValue(selected, "model")).toBe("test/second-model")
65+
})
3666
})

‎packages/cli/test/acp/wire-fixture.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type { Command } from "@opencode/schema/command"
2424
import { Form } from "@opencode/schema/form"
2525
import type { Location } from "@opencode/schema/location"
2626
import type { Model } from "@opencode/schema/model"
27+
import type { Plugin } from "@opencode/schema/plugin"
2728
import type { Session } from "@opencode/schema/session"
2829
import type { SessionMessage } from "@opencode/schema/session-message"
2930
import type { TokenUsage } from "@opencode/schema/token-usage"
@@ -40,6 +41,7 @@ type CommandInfo = typeof Command.Info.Encoded
4041
type LocationRef = typeof Location.PublicRef.Encoded
4142
type ModelInfo = typeof Model.Info.Encoded
4243
type ModelRef = typeof Model.Ref.Encoded
44+
type PluginInfo = typeof Plugin.Info.Encoded
4345
type SessionInfo = typeof Session.Info.Encoded
4446
type SessionMessageInfo = typeof SessionMessage.Info.Encoded
4547
type TokenUsageInfo = typeof TokenUsage.Info.Encoded
@@ -155,6 +157,7 @@ type Catalog = {
155157
models: ModelInfo[]
156158
agents: AgentInfo[]
157159
commands: CommandInfo[]
160+
plugins: PluginInfo[]
158161
}
159162

160163
export type InitializeOptions = {
@@ -559,6 +562,14 @@ function startServer(options: WireOptions, changed: () => void) {
559562
models: [testModel, secondModel],
560563
agents: [buildAgent, planAgent],
561564
commands: [reviewCommand],
565+
plugins: [
566+
{
567+
id: "opencode.models.dev",
568+
source: { type: "builtin" },
569+
features: { server: true },
570+
state: { status: "active" },
571+
},
572+
],
562573
}
563574
const requests: ServerRequest[] = []
564575
const submissions: Submission[] = []
@@ -697,6 +708,7 @@ function startServer(options: WireOptions, changed: () => void) {
697708
"/api/model/default": { GET: catalogRoute(() => catalog.models[0] ?? null) },
698709
"/api/agent": { GET: catalogRoute(() => catalog.agents) },
699710
"/api/command": { GET: catalogRoute(() => catalog.commands) },
711+
"/api/plugin": { GET: catalogRoute(() => catalog.plugins) },
700712
"/api/session": {
701713
GET: route((_req, query) => {
702714
const sessions = [...fake.sessions.values()]

0 commit comments

Comments
 (0)