|
1 | 1 | import { describe, expect } from "bun:test" |
2 | 2 | import { InMemoryTransport, type JSONRPCMessage } from "@modelcontextprotocol/client" |
| 3 | +import { ConfigMCP } from "@opencode/schema/config/mcp" |
3 | 4 | import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" |
4 | 5 | import { Flag } from "@opencode/core/flag" |
5 | 6 | import { Mcp } from "@opencode/core/mcp/index" |
6 | 7 | import { McpClient } from "@opencode/core/mcp/client" |
7 | 8 | import { Session } from "@opencode/core/session" |
8 | 9 | import { Tool } from "@opencode/core/tool" |
9 | 10 | import { McpEventTools } from "@opencode/core/tool/plugin/mcp-events" |
10 | | -import { Effect, Layer, PubSub, Stream } from "effect" |
| 11 | +import { Effect, Fiber, Layer, Option, PubSub, Stream } from "effect" |
11 | 12 | import { hostEnvironmentLayer } from "./fixture/environment" |
12 | 13 | import { testEffect } from "./lib/effect" |
13 | 14 | import { permissionLayer } from "./lib/permission" |
@@ -58,7 +59,7 @@ const eventServer = Effect.fnUntraced(function* () { |
58 | 59 | config: { type: "attached", transport: client }, |
59 | 60 | directory: import.meta.dir, |
60 | 61 | }).pipe(Effect.provide(hostEnvironmentLayer)), |
61 | | - event: (id: string | number, eventId: string, cursor: string) => |
| 62 | + event: (id: string | number, eventId: string, cursor?: string) => |
62 | 63 | notify("notifications/events/event", id, { |
63 | 64 | eventId, |
64 | 65 | name: "job.finished", |
@@ -155,44 +156,114 @@ describe("watch_mcp_event", () => { |
155 | 156 | }), |
156 | 157 | ) |
157 | 158 |
|
158 | | - it.live("gives a replayed event the same message, follows a closed stream from its cursor once, and stops", () => |
| 159 | + it.live("gives a replayed event the same message, follows closed streams from their cursor, and stops", () => |
159 | 160 | Effect.gen(function* () { |
160 | 161 | const { state, wakes, watch, event, close, terminate, deleted } = yield* setup() |
| 162 | + const stopped = (reason: string) => ({ |
| 163 | + sessionID, |
| 164 | + description: "jobs job.finished watch stopped", |
| 165 | + text: `Watching jobs job.finished stopped: ${reason}`, |
| 166 | + metadata: { source: "mcp.event", server: "jobs", event: "job.finished" }, |
| 167 | + }) |
161 | 168 | yield* watch({ server: "jobs", event: "job.finished", once: false }) |
162 | 169 | yield* until(() => state.streams.length === 1) |
163 | 170 | const first = state.streams[0].id |
164 | 171 | yield* event(first, "e1", "c1") |
165 | 172 | yield* event(first, "e1", "c1") |
166 | 173 | yield* event(first, "e2", "c2") |
167 | | - yield* until(() => wakes.length === 3) |
| 174 | + // An event without a cursor keeps the last one. |
| 175 | + yield* event(first, "e3") |
| 176 | + yield* until(() => wakes.length === 4) |
168 | 177 | expect(wakes[1].id).toBe(wakes[0].id) |
169 | 178 | expect(wakes[2].id).not.toBe(wakes[0].id) |
170 | 179 |
|
171 | | - // The server ends the stream itself, as when it restarts in place: follow it from the last event. |
172 | | - yield* close(first) |
173 | | - yield* until(() => state.streams.length === 2) |
174 | | - expect(state.streams[1].params).toMatchObject({ name: "job.finished", cursor: "c2" }) |
| 180 | + // The server ends the stream itself, as when it restarts in place: follow it from the last cursor, until |
| 181 | + // it has closed it three times in a row without an event. |
| 182 | + for (const index of [0, 1, 2, 3, 4, 5]) { |
| 183 | + if (index === 3) yield* event(state.streams[3].id, "e4", "c4") |
| 184 | + yield* close(state.streams[index].id) |
| 185 | + yield* until(() => state.streams.length === index + 2) |
| 186 | + expect(state.streams[index + 1].params).toMatchObject({ cursor: index < 3 ? "c2" : "c4" }) |
| 187 | + } |
| 188 | + yield* close(state.streams[6].id) |
| 189 | + yield* until(() => wakes.length === 6) |
| 190 | + expect(wakes[5]).toEqual(stopped("the server kept closing the stream")) |
| 191 | + expect(state.streams).toHaveLength(7) |
| 192 | + |
175 | 193 | // Deleting the session cancels its stream. |
| 194 | + yield* watch({ server: "jobs", event: "job.finished", once: false }) |
| 195 | + yield* until(() => state.streams.length === 8) |
176 | 196 | yield* PubSub.publish(deleted, { type: "session.deleted", data: { sessionID } }) |
177 | | - yield* until(() => state.cancelled.includes(state.streams[1].id)) |
| 197 | + yield* until(() => state.cancelled.includes(state.streams[7].id)) |
178 | 198 |
|
179 | 199 | // A terminated stream ends the watch and wakes the session to say so, once. |
180 | 200 | yield* watch({ server: "jobs", event: "job.finished", once: false }) |
181 | | - yield* until(() => state.streams.length === 3) |
182 | | - const third = state.streams[2].id |
183 | | - yield* terminate(third) |
184 | | - yield* event(third, "e3", "c3") |
185 | | - yield* until(() => wakes.length === 4) |
| 201 | + yield* until(() => state.streams.length === 9) |
| 202 | + const last = state.streams[8].id |
| 203 | + yield* terminate(last) |
| 204 | + yield* event(last, "e5", "c5") |
| 205 | + yield* until(() => wakes.length === 7) |
186 | 206 | yield* Effect.sleep("20 millis") |
187 | | - expect(wakes.slice(3)).toEqual([ |
188 | | - { |
189 | | - sessionID, |
190 | | - description: "jobs job.finished watch stopped", |
191 | | - text: "Watching jobs job.finished stopped: Forbidden", |
192 | | - metadata: { source: "mcp.event", server: "jobs", event: "job.finished" }, |
| 207 | + expect(wakes.slice(6)).toEqual([stopped("Forbidden")]) |
| 208 | + expect(state.streams).toHaveLength(9) |
| 209 | + }), |
| 210 | + ) |
| 211 | + |
| 212 | + it.live("keeps a stream open past the server's execution timeout", () => |
| 213 | + Effect.gen(function* () { |
| 214 | + // A Streamable HTTP server that holds `events/stream` open as SSE and sends one event when told to. |
| 215 | + const opened: Array<(event: Record<string, unknown>) => void> = [] |
| 216 | + const http = Bun.serve({ |
| 217 | + port: 0, |
| 218 | + fetch: async (request) => { |
| 219 | + if (request.method !== "POST") return new Response(null, { status: 405 }) |
| 220 | + const message = (await request.json()) as { |
| 221 | + id?: string | number |
| 222 | + method: string |
| 223 | + params: { protocolVersion?: string } |
| 224 | + } |
| 225 | + if (message.id === undefined) return new Response(null, { status: 202 }) |
| 226 | + if (message.method === "initialize") |
| 227 | + return Response.json({ |
| 228 | + jsonrpc: "2.0", |
| 229 | + id: message.id, |
| 230 | + result: { |
| 231 | + protocolVersion: message.params.protocolVersion, |
| 232 | + capabilities: { extensions: { "io.modelcontextprotocol/events": {} } }, |
| 233 | + serverInfo: { name: "jobs", version: "1.0.0" }, |
| 234 | + }, |
| 235 | + }) |
| 236 | + const body = new ReadableStream({ |
| 237 | + start: (controller) => |
| 238 | + void opened.push((params) => |
| 239 | + controller.enqueue( |
| 240 | + new TextEncoder().encode( |
| 241 | + `data: ${JSON.stringify({ jsonrpc: "2.0", method: "notifications/events/event", params: { ...params, _meta: { "io.modelcontextprotocol/subscriptionId": message.id } } })}\n\n`, |
| 242 | + ), |
| 243 | + ), |
| 244 | + ), |
| 245 | + }) |
| 246 | + return new Response(body, { headers: { "content-type": "text/event-stream" } }) |
193 | 247 | }, |
194 | | - ]) |
195 | | - expect(state.streams).toHaveLength(3) |
| 248 | + }) |
| 249 | + yield* Effect.addFinalizer(() => Effect.promise(() => http.stop(true))) |
| 250 | + const connection = yield* McpClient.connect({ |
| 251 | + server: "jobs", |
| 252 | + config: new ConfigMCP.Remote({ |
| 253 | + type: "remote", |
| 254 | + url: http.url.href, |
| 255 | + oauth: false, |
| 256 | + timeout: new ConfigMCP.Timeout({ execution: 50 }), |
| 257 | + }), |
| 258 | + directory: import.meta.dir, |
| 259 | + }).pipe(Effect.provide(hostEnvironmentLayer)) |
| 260 | + const next = yield* Effect.forkChild(Stream.runHead(connection.events!.stream({ name: "job.finished" })), { |
| 261 | + startImmediately: true, |
| 262 | + }) |
| 263 | + yield* until(() => opened.length === 1) |
| 264 | + yield* Effect.sleep("150 millis") |
| 265 | + opened[0]({ eventId: "e1", name: "job.finished", timestamp: "2026-11-10T00:00:00Z", data: {}, cursor: "c1" }) |
| 266 | + expect(Option.getOrThrow(yield* Fiber.join(next))).toMatchObject({ eventId: "e1", cursor: "c1" }) |
196 | 267 | }), |
197 | 268 | ) |
198 | 269 |
|
|
0 commit comments