GeezerVison is showing github.com — fix: improve plugin system robustness — agent/command resolution, asy… · msis/opencode@814a515
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/plugin/index.ts +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -136,7 +136,11 @@ export namespace Plugin { | 136 | 136 |
| 137 | 137 | // Notify plugins of current config | 138 | 138 | for (const hook of hooks) { | 139 | | - await (hook as any).config?.(cfg) | | 139 | + try { | | 140 | + await (hook as any).config?.(cfg) | | 141 | + } catch (err) { | | 142 | + log.error("plugin config hook failed", { error: err }) | | 143 | + } | 140 | 144 | } | 141 | 145 | }) | 142 | 146 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/server/routes/session.ts +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,6 +19,8 @@ import { PermissionID } from "@/permission/schema" | 19 | 19 | import { ModelID, ProviderID } from "@/provider/schema" | 20 | 20 | import { errors } from "../error" | 21 | 21 | import { lazy } from "../../util/lazy" | | 22 | +import { Bus } from "../../bus" | | 23 | +import { NamedError } from "@opencode-ai/util/error" | 22 | 24 |
| 23 | 25 | const log = Log.create({ service: "server" }) | 24 | 26 |
| @@ -846,7 +848,13 @@ export const SessionRoutes = lazy(() => | 846 | 848 | return stream(c, async () => { | 847 | 849 | const sessionID = c.req.valid("param").sessionID | 848 | 850 | const body = c.req.valid("json") | 849 | | - SessionPrompt.prompt({ ...body, sessionID }) | | 851 | + SessionPrompt.prompt({ ...body, sessionID }).catch((err) => { | | 852 | + log.error("prompt_async failed", { sessionID, error: err }) | | 853 | + Bus.publish(Session.Event.Error, { | | 854 | + sessionID, | | 855 | + error: new NamedError.Unknown({ message: err instanceof Error ? err.message : String(err) }).toObject(), | | 856 | + }) | | 857 | + }) | 850 | 858 | }) | 851 | 859 | }, | 852 | 860 | ) | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/session/prompt.ts +50-2Lines changed: 50 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -418,6 +418,16 @@ export namespace SessionPrompt { | 418 | 418 | ) | 419 | 419 | let executionError: Error | undefined | 420 | 420 | const taskAgent = await Agent.get(task.agent) | | 421 | + if (!taskAgent) { | | 422 | + const available = await Agent.list().then((agents) => agents.filter((a) => !a.hidden).map((a) => a.name)) | | 423 | + const hint = available.length ? ` Available agents: ${available.join(", ")}` : "" | | 424 | + const error = new NamedError.Unknown({ message: `Agent not found: "${task.agent}".${hint}` }) | | 425 | + Bus.publish(Session.Event.Error, { | | 426 | + sessionID, | | 427 | + error: error.toObject(), | | 428 | + }) | | 429 | + throw error | | 430 | + } | 421 | 431 | const taskCtx: Tool.Context = { | 422 | 432 | agent: task.agent, | 423 | 433 | messageID: assistantMessage.id, | @@ -560,6 +570,16 @@ export namespace SessionPrompt { | 560 | 570 |
| 561 | 571 | // normal processing | 562 | 572 | const agent = await Agent.get(lastUser.agent) | | 573 | + if (!agent) { | | 574 | + const available = await Agent.list().then((agents) => agents.filter((a) => !a.hidden).map((a) => a.name)) | | 575 | + const hint = available.length ? ` Available agents: ${available.join(", ")}` : "" | | 576 | + const error = new NamedError.Unknown({ message: `Agent not found: "${lastUser.agent}".${hint}` }) | | 577 | + Bus.publish(Session.Event.Error, { | | 578 | + sessionID, | | 579 | + error: error.toObject(), | | 580 | + }) | | 581 | + throw error | | 582 | + } | 563 | 583 | const maxSteps = agent.steps ?? Infinity | 564 | 584 | const isLastStep = step >= maxSteps | 565 | 585 | msgs = await insertReminders({ | @@ -964,7 +984,18 @@ export namespace SessionPrompt { | 964 | 984 | } | 965 | 985 |
| 966 | 986 | async function createUserMessage(input: PromptInput) { | 967 | | - const agent = await Agent.get(input.agent ?? (await Agent.defaultAgent())) | | 987 | + const agentName = input.agent || (await Agent.defaultAgent()) | | 988 | + const agent = await Agent.get(agentName) | | 989 | + if (!agent) { | | 990 | + const available = await Agent.list().then((agents) => agents.filter((a) => !a.hidden).map((a) => a.name)) | | 991 | + const hint = available.length ? ` Available agents: ${available.join(", ")}` : "" | | 992 | + const error = new NamedError.Unknown({ message: `Agent not found: "${agentName}".${hint}` }) | | 993 | + Bus.publish(Session.Event.Error, { | | 994 | + sessionID: input.sessionID, | | 995 | + error: error.toObject(), | | 996 | + }) | | 997 | + throw error | | 998 | + } | 968 | 999 |
| 969 | 1000 | const model = input.model ?? agent.model ?? (await lastModel(input.sessionID)) | 970 | 1001 | const full = | @@ -1531,6 +1562,16 @@ NOTE: At any point in time through this workflow you should feel free to ask the | 1531 | 1562 | await SessionRevert.cleanup(session) | 1532 | 1563 | } | 1533 | 1564 | const agent = await Agent.get(input.agent) | | 1565 | + if (!agent) { | | 1566 | + const available = await Agent.list().then((agents) => agents.filter((a) => !a.hidden).map((a) => a.name)) | | 1567 | + const hint = available.length ? ` Available agents: ${available.join(", ")}` : "" | | 1568 | + const error = new NamedError.Unknown({ message: `Agent not found: "${input.agent}".${hint}` }) | | 1569 | + Bus.publish(Session.Event.Error, { | | 1570 | + sessionID: input.sessionID, | | 1571 | + error: error.toObject(), | | 1572 | + }) | | 1573 | + throw error | | 1574 | + } | 1534 | 1575 | const model = input.model ?? agent.model ?? (await lastModel(input.sessionID)) | 1535 | 1576 | const userMsg: MessageV2.User = { | 1536 | 1577 | id: MessageID.ascending(), | @@ -1783,7 +1824,14 @@ NOTE: At any point in time through this workflow you should feel free to ask the | 1783 | 1824 | log.info("command", input) | 1784 | 1825 | const command = await Command.get(input.command) | 1785 | 1826 | if (!command) { | 1786 | | - throw new NamedError.Unknown({ message: `Command not found: "${input.command}"` }) | | 1827 | + const available = await Command.list().then((cmds) => cmds.map((c) => c.name)) | | 1828 | + const hint = available.length ? ` Available commands: ${available.join(", ")}` : "" | | 1829 | + const error = new NamedError.Unknown({ message: `Command not found: "${input.command}".${hint}` }) | | 1830 | + Bus.publish(Session.Event.Error, { | | 1831 | + sessionID: input.sessionID, | | 1832 | + error: error.toObject(), | | 1833 | + }) | | 1834 | + throw error | 1787 | 1835 | } | 1788 | 1836 | const agentName = command.agent ?? input.agent ?? (await Agent.defaultAgent()) | 1789 | 1837 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/plugin/auth-override.test.ts +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -54,3 +54,19 @@ describe("plugin.auth-override", () => { | 54 | 54 | expect(plainMethods[ProviderID.make("github-copilot")][0].label).not.toBe("Test Override Auth") | 55 | 55 | }, 30000) // Increased timeout for plugin installation | 56 | 56 | }) | | 57 | + | | 58 | +const file = path.join(import.meta.dir, "../../src/plugin/index.ts") | | 59 | + | | 60 | +describe("plugin.config-hook-error-isolation", () => { | | 61 | + test("config hooks are individually error-isolated in the layer factory", async () => { | | 62 | + const src = await Bun.file(file).text() | | 63 | + | | 64 | + // The config hook try/catch lives in the InstanceState factory (layer definition), | | 65 | + // not in init() which now just delegates to the Effect service. | | 66 | + expect(src).toContain("plugin config hook failed") | | 67 | + | | 68 | + const pattern = | | 69 | + /for\s*\(const hook of hooks\)\s*\{[\s\S]*?try\s*\{[\s\S]*?\.config\?\.\([\s\S]*?\}\s*catch\s*\(err\)\s*\{[\s\S]*?plugin config hook failed[\s\S]*?\}/ | | 70 | + expect(pattern.test(src)).toBe(true) | | 71 | + }) | | 72 | +}) |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/server/session-messages.test.ts +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -117,3 +117,16 @@ describe("session messages endpoint", () => { | 117 | 117 | }) | 118 | 118 | }) | 119 | 119 | }) | | 120 | + | | 121 | +describe("session.prompt_async error handling", () => { | | 122 | + test("prompt_async route has error handler for detached prompt call", async () => { | | 123 | + const src = await Bun.file(path.join(import.meta.dir, "../../src/server/routes/session.ts")).text() | | 124 | + const start = src.indexOf('"/:sessionID/prompt_async"') | | 125 | + const end = src.indexOf('"/:sessionID/command"', start) | | 126 | + expect(start).toBeGreaterThan(-1) | | 127 | + expect(end).toBeGreaterThan(start) | | 128 | + const route = src.slice(start, end) | | 129 | + expect(route).toContain(".catch(") | | 130 | + expect(route).toContain("Bus.publish(Session.Event.Error") | | 131 | + }) | | 132 | +}) |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/session/prompt.test.ts +76Lines changed: 76 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import path from "path" | 2 | 2 | import { describe, expect, test } from "bun:test" | | 3 | +import { NamedError } from "@opencode-ai/util/error" | 3 | 4 | import { fileURLToPath } from "url" | 4 | 5 | import { Instance } from "../../src/project/instance" | 5 | 6 | import { ModelID, ProviderID } from "../../src/provider/schema" | @@ -210,3 +211,78 @@ describe("session.prompt agent variant", () => { | 210 | 211 | } | 211 | 212 | }) | 212 | 213 | }) | | 214 | + | | 215 | +describe("session.agent-resolution", () => { | | 216 | + test("unknown agent throws typed error", async () => { | | 217 | + await using tmp = await tmpdir({ git: true }) | | 218 | + await Instance.provide({ | | 219 | + directory: tmp.path, | | 220 | + fn: async () => { | | 221 | + const session = await Session.create({}) | | 222 | + const err = await SessionPrompt.prompt({ | | 223 | + sessionID: session.id, | | 224 | + agent: "nonexistent-agent-xyz", | | 225 | + noReply: true, | | 226 | + parts: [{ type: "text", text: "hello" }], | | 227 | + }).then( | | 228 | + () => undefined, | | 229 | + (e) => e, | | 230 | + ) | | 231 | + expect(err).toBeDefined() | | 232 | + expect(err).not.toBeInstanceOf(TypeError) | | 233 | + expect(NamedError.Unknown.isInstance(err)).toBe(true) | | 234 | + if (NamedError.Unknown.isInstance(err)) { | | 235 | + expect(err.data.message).toContain('Agent not found: "nonexistent-agent-xyz"') | | 236 | + } | | 237 | + }, | | 238 | + }) | | 239 | + }, 30000) | | 240 | + | | 241 | + test("unknown agent error includes available agent names", async () => { | | 242 | + await using tmp = await tmpdir({ git: true }) | | 243 | + await Instance.provide({ | | 244 | + directory: tmp.path, | | 245 | + fn: async () => { | | 246 | + const session = await Session.create({}) | | 247 | + const err = await SessionPrompt.prompt({ | | 248 | + sessionID: session.id, | | 249 | + agent: "nonexistent-agent-xyz", | | 250 | + noReply: true, | | 251 | + parts: [{ type: "text", text: "hello" }], | | 252 | + }).then( | | 253 | + () => undefined, | | 254 | + (e) => e, | | 255 | + ) | | 256 | + expect(NamedError.Unknown.isInstance(err)).toBe(true) | | 257 | + if (NamedError.Unknown.isInstance(err)) { | | 258 | + expect(err.data.message).toContain("build") | | 259 | + } | | 260 | + }, | | 261 | + }) | | 262 | + }, 30000) | | 263 | + | | 264 | + test("unknown command throws typed error with available names", async () => { | | 265 | + await using tmp = await tmpdir({ git: true }) | | 266 | + await Instance.provide({ | | 267 | + directory: tmp.path, | | 268 | + fn: async () => { | | 269 | + const session = await Session.create({}) | | 270 | + const err = await SessionPrompt.command({ | | 271 | + sessionID: session.id, | | 272 | + command: "nonexistent-command-xyz", | | 273 | + arguments: "", | | 274 | + }).then( | | 275 | + () => undefined, | | 276 | + (e) => e, | | 277 | + ) | | 278 | + expect(err).toBeDefined() | | 279 | + expect(err).not.toBeInstanceOf(TypeError) | | 280 | + expect(NamedError.Unknown.isInstance(err)).toBe(true) | | 281 | + if (NamedError.Unknown.isInstance(err)) { | | 282 | + expect(err.data.message).toContain('Command not found: "nonexistent-command-xyz"') | | 283 | + expect(err.data.message).toContain("init") | | 284 | + } | | 285 | + }, | | 286 | + }) | | 287 | + }, 30000) | | 288 | +}) |
|