Skip to content

Commit 63b42f6

Browse files
authored
fix(core): report a refused Console auto-watch (#63217)
1 parent 05b32ea commit 63b42f6

2 files changed

Lines changed: 57 additions & 2 deletions

File tree

‎packages/core/src/plugin/provider/opencode-watches.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,19 @@ export const make = Effect.fnUntraced(function* (ctx: Context, http: HttpClient.
206206
// Off the tool call's path: the result never waits on Console, and a failure only skips the watch.
207207
return create(member, event.sessionID, { target }).pipe(
208208
Effect.scoped,
209+
// A refusal, such as a GitHub sign-in the member hasn't made, is told once per reason: the ID repeats with it.
210+
Effect.catchIf(
211+
(cause): cause is ToolFailure => cause instanceof ToolFailure && cause.metadata?.status === 403,
212+
(refusal) =>
213+
ctx.session.synthetic({
214+
sessionID: event.sessionID,
215+
id: SessionMessage.ID.make(
216+
`msg_watch_${Hash.sha256(`${event.sessionID}\n${refusal.message}`).slice(0, 26)}`,
217+
),
218+
text: `OpenCode Console didn't watch a pull request or push from this session, so its updates won't wake the session: ${refusal.message}`,
219+
metadata: { source: "console.watch" },
220+
}),
221+
),
209222
Effect.catch((cause) => Effect.logDebug("skipped watching a shell command's work", { cause })),
210223
Effect.forkIn(lease, { startImmediately: true }),
211224
Effect.asVoid,
@@ -245,7 +258,7 @@ export const make = Effect.fnUntraced(function* (ctx: Context, http: HttpClient.
245258
editor.add({
246259
name: "watch",
247260
description:
248-
"Wake this session when a Slack thread or a GitHub pull request, issue, commit or Actions run gets a reply or changes status, or one of the workspace's MCP servers sends an event `events` lists. Slack and GitHub writes through OpenCode Console, `gh pr create` and `git push` are already watched, a pull request's checks included, so don't watch what you just made. Use it for anything else, such as a deploy's Actions run, `gh workflow run` or a link the user shared. People often answer a Slack post in the channel rather than its thread, so after each top-level post the channel's new top-level messages are watched for 15 minutes too. The session wakes with the update, so don't poll.",
261+
"Wake this session when a Slack thread or a GitHub pull request, issue, commit or Actions run gets a reply or changes status, or one of the workspace's MCP servers sends an event `events` lists. Slack and GitHub writes through OpenCode Console, `gh pr create` and `git push` are already watched, a pull request's checks included, so don't watch what you just made (the session is told if Console refuses). Use it for anything else, such as a deploy's Actions run, `gh workflow run` or a link the user shared. People often answer a Slack post in the channel rather than its thread, so after each top-level post the channel's new top-level messages are watched for 15 minutes too. The session wakes with the update, so don't poll.",
249262
input: Schema.Struct({
250263
target: Schema.optional(
251264
Schema.String.annotate({
@@ -590,7 +603,7 @@ function expectOk(response: HttpClientResponse.HttpClientResponse) {
590603
: response.status === 404
591604
? "Event watches are off for this OpenCode Console workspace."
592605
: (body?.message ?? `OpenCode Console answered ${response.status}`)
593-
return Effect.fail(new ToolFailure({ message }))
606+
return Effect.fail(new ToolFailure({ message, metadata: { status: response.status } }))
594607
}),
595608
)
596609
}

‎packages/core/test/plugin/provider-opencode-watches.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ function fakeConsole(options: { readonly upgrade?: boolean } = {}) {
7878
requests: new Array<{ method: string; path: string; body?: unknown }>(),
7979
/** What `GET /api/event-watches/types` lists. */
8080
types: new Array<Record<string, unknown>>(),
81+
/** The status and message Console answers `POST /api/event-watches` with instead of a watch, when set. */
82+
refuse: undefined as { status: number; message: string } | undefined,
8183
}
8284
const server = Bun.serve({
8385
port: 0,
@@ -112,6 +114,8 @@ function fakeConsole(options: { readonly upgrade?: boolean } = {}) {
112114
}
113115
if (url.pathname === "/api/event-watches/types") return Response.json({ types: state.types })
114116
if (url.pathname === "/api/event-watches" && request.method === "POST") {
117+
if (state.refuse)
118+
return Response.json({ _tag: "WatchRefused", message: state.refuse.message }, { status: state.refuse.status })
115119
const created = body as { sessionID: string; server?: string; event?: string; arguments?: unknown }
116120
// Another server's event type is keyed by a hash of it, as Console keys it.
117121
return Response.json({
@@ -935,6 +939,44 @@ describe("OpencodePlugin event watches", () => {
935939
),
936940
)
937941

942+
it.effect("tells the session once why Console refused a shell command's watch, and skips other failures", () =>
943+
withConsole({}, (console) =>
944+
Effect.gen(function* () {
945+
const hooks = yield* PluginHooks.Service
946+
const { host, wakes, replays } = yield* start(console)
947+
const session = yield* host.session.create({})
948+
const created = () =>
949+
console.state.requests.filter((request) => request.method === "POST" && request.path === "/api/event-watches")
950+
const push = (refuse: { status: number; message: string }, posted: number) =>
951+
Effect.gen(function* () {
952+
console.state.refuse = refuse
953+
yield* hooks.trigger("tool", "execute.after", {
954+
tool: "shell",
955+
sessionID: session.id,
956+
agent: Agent.ID.make("build"),
957+
messageID: SessionMessage.ID.create(),
958+
id: Tool.CallID.make("call_shell"),
959+
input: { command: "gh pr create --fill" },
960+
status: "completed",
961+
result: { output: { exit: 0, output: "https://github.com/Acme/App/pull/42\n", truncated: false } },
962+
})
963+
yield* waitUntil(Effect.sync(() => created().length === posted))
964+
})
965+
const refused = { status: 403, message: "Sign in to GitHub at https://console.test/github first." }
966+
967+
yield* push({ status: 500, message: "Something went wrong" }, 1)
968+
yield* push(refused, 2)
969+
yield* waitUntil(Effect.sync(() => wakes.length === 1))
970+
yield* push(refused, 3)
971+
yield* waitUntil(Effect.sync(() => replays.length === 1))
972+
973+
expect(wakes).toHaveLength(1)
974+
expect(wakes[0]?.sessionID).toBe(session.id)
975+
expect(wakes[0]?.text).toContain(refused.message)
976+
}),
977+
),
978+
)
979+
938980
it.effect("starts and stops with the header on the Console's periodic config check", () =>
939981
withConsole({}, (console) =>
940982
Effect.gen(function* () {

0 commit comments

Comments
 (0)