From be1eb706c5eb70fc4abb75e61daf1aff22722b5c Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Tue, 14 Apr 2026 19:46:13 -0400 Subject: [PATCH] refactor(effect): scope bridges to callback setup --- packages/opencode/src/command/index.ts | 2 +- packages/opencode/src/mcp/index.ts | 9 +++++---- packages/opencode/src/plugin/index.ts | 10 +++++----- packages/opencode/src/provider/provider.ts | 2 +- packages/opencode/src/pty/index.ts | 2 +- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/packages/opencode/src/command/index.ts b/packages/opencode/src/command/index.ts index 2e6dbac528..91a9e1b405 100644 --- a/packages/opencode/src/command/index.ts +++ b/packages/opencode/src/command/index.ts @@ -79,10 +79,10 @@ export namespace Command { const config = yield* Config.Service const mcp = yield* MCP.Service const skill = yield* Skill.Service - const bridge = yield* EffectBridge.make() const init = Effect.fn("Command.state")(function* (ctx: InstanceContext) { const cfg = yield* config.get() + const bridge = yield* EffectBridge.make() const commands: Record = {} commands[Default.INIT] = { diff --git a/packages/opencode/src/mcp/index.ts b/packages/opencode/src/mcp/index.ts index be8e8ddb70..b2b3708728 100644 --- a/packages/opencode/src/mcp/index.ts +++ b/packages/opencode/src/mcp/index.ts @@ -444,7 +444,6 @@ export namespace MCP { return { mcpClient, status, defs: listed } satisfies CreateResult }) const cfgSvc = yield* Config.Service - const bridge = yield* EffectBridge.make() const descendants = Effect.fnUntraced( function* (pid: number) { @@ -472,7 +471,7 @@ export namespace MCP { Effect.catch(() => Effect.succeed([] as number[])), ) - function watch(s: State, name: string, client: MCPClient, timeout?: number) { + function watch(s: State, name: string, client: MCPClient, bridge: EffectBridge.Shape, timeout?: number) { client.setNotificationHandler(ToolListChangedNotificationSchema, async () => { log.info("tools list changed notification received", { server: name }) if (s.clients[name] !== client || s.status[name]?.status !== "connected") return @@ -489,6 +488,7 @@ export namespace MCP { const state = yield* InstanceState.make( Effect.fn("MCP.state")(function* () { const cfg = yield* cfgSvc.get() + const bridge = yield* EffectBridge.make() const config = cfg.mcp ?? {} const s: State = { status: {}, @@ -517,7 +517,7 @@ export namespace MCP { if (result.mcpClient) { s.clients[key] = result.mcpClient s.defs[key] = result.defs! - watch(s, key, result.mcpClient, mcp.timeout) + watch(s, key, result.mcpClient, bridge, mcp.timeout) } }), { concurrency: "unbounded" }, @@ -564,11 +564,12 @@ export namespace MCP { listed: MCPToolDef[], timeout?: number, ) { + const bridge = yield* EffectBridge.make() yield* closeClient(s, name) s.status[name] = { status: "connected" } s.clients[name] = client s.defs[name] = listed - watch(s, name, client, timeout) + watch(s, name, client, bridge, timeout) return s.status[name] }) diff --git a/packages/opencode/src/plugin/index.ts b/packages/opencode/src/plugin/index.ts index dfd9c55600..8fae8487cb 100644 --- a/packages/opencode/src/plugin/index.ts +++ b/packages/opencode/src/plugin/index.ts @@ -108,15 +108,15 @@ export namespace Plugin { Effect.gen(function* () { const bus = yield* Bus.Service const config = yield* Config.Service - const bridge = yield* EffectBridge.make() - - function publishPluginError(message: string) { - bridge.fork(bus.publish(Session.Event.Error, { error: new NamedError.Unknown({ message }).toObject() })) - } const state = yield* InstanceState.make( Effect.fn("Plugin.state")(function* (ctx) { const hooks: Hooks[] = [] + const bridge = yield* EffectBridge.make() + + function publishPluginError(message: string) { + bridge.fork(bus.publish(Session.Event.Error, { error: new NamedError.Unknown({ message }).toObject() })) + } const { Server } = yield* Effect.promise(() => import("../server/server")) diff --git a/packages/opencode/src/provider/provider.ts b/packages/opencode/src/provider/provider.ts index 91bcb9074e..53f044163f 100644 --- a/packages/opencode/src/provider/provider.ts +++ b/packages/opencode/src/provider/provider.ts @@ -1039,11 +1039,11 @@ export namespace Provider { const auth = yield* Auth.Service const env = yield* Env.Service const plugin = yield* Plugin.Service - const bridge = yield* EffectBridge.make() const state = yield* InstanceState.make(() => Effect.gen(function* () { using _ = log.time("state") + const bridge = yield* EffectBridge.make() const cfg = yield* config.get() const modelsDev = yield* Effect.promise(() => ModelsDev.get()) const database = mapValues(modelsDev, fromModelsDevProvider) diff --git a/packages/opencode/src/pty/index.ts b/packages/opencode/src/pty/index.ts index 4660ffa4e9..716700e10b 100644 --- a/packages/opencode/src/pty/index.ts +++ b/packages/opencode/src/pty/index.ts @@ -119,7 +119,6 @@ export namespace Pty { Effect.gen(function* () { const bus = yield* Bus.Service const plugin = yield* Plugin.Service - const bridge = yield* EffectBridge.make() function teardown(session: Active) { try { session.process.kill() @@ -174,6 +173,7 @@ export namespace Pty { const create = Effect.fn("Pty.create")(function* (input: CreateInput) { const s = yield* InstanceState.get(state) + const bridge = yield* EffectBridge.make() const id = PtyID.ascending() const command = input.command || Shell.preferred() const args = input.args || []