Skip to content

Commit 23b594d

Browse files
authored
fix: preserve bus instance context (anomalyco#28051)
1 parent 5060577 commit 23b594d

8 files changed

Lines changed: 37 additions & 49 deletions

File tree

packages/opencode/src/bus/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { GlobalBus } from "./global"
66
import { InstanceState } from "@/effect/instance-state"
77
import { makeRuntime } from "@/effect/run-service"
88
import { Identifier } from "@/id/id"
9+
import type { InstanceContext } from "@/project/instance-context"
10+
import { InstanceRef } from "@/effect/instance-ref"
911

1012
const log = Log.create({ service: "bus" })
1113

@@ -185,11 +187,12 @@ export function createID() {
185187
}
186188

187189
export async function publish<D extends BusEvent.Definition>(
190+
ctx: InstanceContext,
188191
def: D,
189192
properties: BusProperties<D>,
190193
options?: { id?: string },
191194
) {
192-
return runPromise((svc) => svc.publish(def, properties, options))
195+
return runPromise((svc) => svc.publish(def, properties, options).pipe(Effect.provideService(InstanceRef, ctx)))
193196
}
194197

195198
export function subscribe<D extends BusEvent.Definition>(def: D, callback: (event: Payload<D>) => unknown) {

packages/opencode/src/cli/cmd/tui/app.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,7 @@ function App(props: { onSnapshot?: () => Promise<string[]> }) {
859859
})
860860

861861
event.on("installation.update-available", async (evt) => {
862+
console.log("installation.update-available", evt)
862863
const version = evt.properties.version
863864

864865
const skipped = kv.get("skipped_version")
Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Bus } from "@/bus"
21
import { Config } from "@/config/config"
32
import { AppRuntime } from "@/effect/app-runtime"
43
import { Flag } from "@opencode-ai/core/flag/flag"
54
import { Installation } from "@/installation"
65
import { InstallationVersion } from "@opencode-ai/core/installation/version"
6+
import { GlobalBus } from "@/bus/global"
77

88
export async function upgrade() {
99
const config = await AppRuntime.runPromise(Config.Service.use((cfg) => cfg.getGlobal()))
@@ -13,7 +13,13 @@ export async function upgrade() {
1313
if (!latest) return
1414

1515
if (Flag.OPENCODE_ALWAYS_NOTIFY_UPDATE) {
16-
await Bus.publish(Installation.Event.UpdateAvailable, { version: latest })
16+
GlobalBus.emit("event", {
17+
directory: "global",
18+
payload: {
19+
type: Installation.Event.UpdateAvailable.type,
20+
properties: { version: latest },
21+
},
22+
})
1723
return
1824
}
1925

@@ -22,12 +28,26 @@ export async function upgrade() {
2228
const kind = Installation.getReleaseType(InstallationVersion, latest)
2329

2430
if (config.autoupdate === "notify" || kind !== "patch") {
25-
await Bus.publish(Installation.Event.UpdateAvailable, { version: latest })
31+
GlobalBus.emit("event", {
32+
directory: "global",
33+
payload: {
34+
type: Installation.Event.UpdateAvailable.type,
35+
properties: { version: latest },
36+
},
37+
})
2638
return
2739
}
2840

2941
if (method === "unknown") return
3042
await Installation.upgrade(method, latest)
31-
.then(() => Bus.publish(Installation.Event.Updated, { version: latest }))
43+
.then(() =>
44+
GlobalBus.emit("event", {
45+
directory: "global",
46+
payload: {
47+
type: Installation.Event.Updated.type,
48+
properties: { version: latest },
49+
},
50+
}),
51+
)
3252
.catch(() => {})
3353
}

packages/opencode/src/config/agent.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
export * as ConfigAgent from "./agent"
22

33
import { Exit, Schema, SchemaGetter } from "effect"
4-
import { Bus } from "@/bus"
54
import { PositiveInt } from "@opencode-ai/core/schema"
65
import * as Log from "@opencode-ai/core/util/log"
7-
import { NamedError } from "@opencode-ai/core/util/error"
86
import { Glob } from "@opencode-ai/core/util/glob"
97
import { configEntryNameFromPath } from "./entry-name"
108
import * as ConfigMarkdown from "./markdown"
@@ -112,12 +110,7 @@ export async function load(dir: string) {
112110
dot: true,
113111
symlink: true,
114112
})) {
115-
const md = await ConfigMarkdown.parse(item).catch(async (err) => {
116-
const message = ConfigMarkdown.FrontmatterError.isInstance(err)
117-
? err.data.message
118-
: `Failed to parse agent ${item}`
119-
const { Session } = await import("@/session/session")
120-
void Bus.publish(Session.Event.Error, { error: new NamedError.Unknown({ message }).toObject() })
113+
const md = await ConfigMarkdown.parse(item).catch((err) => {
121114
log.error("failed to load agent", { agent: item, err })
122115
return undefined
123116
})
@@ -144,12 +137,7 @@ export async function loadMode(dir: string) {
144137
dot: true,
145138
symlink: true,
146139
})) {
147-
const md = await ConfigMarkdown.parse(item).catch(async (err) => {
148-
const message = ConfigMarkdown.FrontmatterError.isInstance(err)
149-
? err.data.message
150-
: `Failed to parse mode ${item}`
151-
const { Session } = await import("@/session/session")
152-
void Bus.publish(Session.Event.Error, { error: new NamedError.Unknown({ message }).toObject() })
140+
const md = await ConfigMarkdown.parse(item).catch((err) => {
153141
log.error("failed to load mode", { mode: item, err })
154142
return undefined
155143
})

packages/opencode/src/config/command.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ export * as ConfigCommand from "./command"
22

33
import * as Log from "@opencode-ai/core/util/log"
44
import { Cause, Exit, Schema } from "effect"
5-
import { NamedError } from "@opencode-ai/core/util/error"
65
import { Glob } from "@opencode-ai/core/util/glob"
7-
import { Bus } from "@/bus"
86
import { configEntryNameFromPath } from "./entry-name"
97
import { InvalidError } from "./error"
108
import * as ConfigMarkdown from "./markdown"
@@ -32,12 +30,7 @@ export async function load(dir: string) {
3230
dot: true,
3331
symlink: true,
3432
})) {
35-
const md = await ConfigMarkdown.parse(item).catch(async (err) => {
36-
const message = ConfigMarkdown.FrontmatterError.isInstance(err)
37-
? err.data.message
38-
: `Failed to parse command ${item}`
39-
const { Session } = await import("@/session/session")
40-
void Bus.publish(Session.Event.Error, { error: new NamedError.Unknown({ message }).toObject() })
33+
const md = await ConfigMarkdown.parse(item).catch((err) => {
4134
log.error("failed to load command", { command: item, err })
4235
return undefined
4336
})

packages/opencode/src/file/watcher.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ export const layer = Layer.effect(
9696
)
9797

9898
const cb: ParcelWatcher.SubscribeCallback = bridge.bind((err, evts) => {
99-
if (err) return
99+
// if (err) return
100100
for (const evt of evts) {
101-
if (evt.type === "create") void Bus.publish(Event.Updated, { file: evt.path, event: "add" })
102-
if (evt.type === "update") void Bus.publish(Event.Updated, { file: evt.path, event: "change" })
103-
if (evt.type === "delete") void Bus.publish(Event.Updated, { file: evt.path, event: "unlink" })
101+
if (evt.type === "create") void Bus.publish(ctx, Event.Updated, { file: evt.path, event: "add" })
102+
if (evt.type === "update") void Bus.publish(ctx, Event.Updated, { file: evt.path, event: "change" })
103+
if (evt.type === "delete") void Bus.publish(ctx, Event.Updated, { file: evt.path, event: "unlink" })
104104
}
105105
})
106106

packages/opencode/src/lsp/lsp.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ import { InstanceState } from "@/effect/instance-state"
1313
import { containsPath } from "@/project/instance-context"
1414
import { NonNegativeInt } from "@opencode-ai/core/schema"
1515
import { RuntimeFlags } from "@/effect/runtime-flags"
16-
import { InstanceRef } from "@/effect/instance-ref"
17-
import { makeRuntime } from "@/effect/run-service"
1816

1917
const log = Log.create({ service: "lsp" })
20-
const busRuntime = makeRuntime(Bus.Service, Bus.layer)
2118

2219
export const Event = {
2320
Updated: BusEvent.define("lsp.updated", Schema.Struct({})),
@@ -294,9 +291,7 @@ export const layer = Layer.effect(
294291
if (!client) continue
295292

296293
result.push(client)
297-
void busRuntime.runPromise((bus) =>
298-
bus.publish(Event.Updated, {}).pipe(Effect.provideService(InstanceRef, ctx)),
299-
)
294+
await Bus.publish(ctx, Event.Updated, {})
300295
}
301296

302297
return result

packages/opencode/src/session/compaction.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ import { Effect, Layer, Context, Schema } from "effect"
1616
import * as DateTime from "effect/DateTime"
1717
import { InstanceState } from "@/effect/instance-state"
1818
import { isOverflow as overflow, usable } from "./overflow"
19-
import { makeRuntime } from "@/effect/run-service"
2019
import { serviceUse } from "@/effect/service-use"
2120
import { RuntimeFlags } from "@/effect/runtime-flags"
22-
import { EventV2 } from "@opencode-ai/core/event"
2321
import { EventV2Bridge } from "@/event-v2-bridge"
2422
import { SessionEvent } from "@opencode-ai/core/session-event"
2523

@@ -638,14 +636,4 @@ export const defaultLayer = Layer.suspend(() =>
638636
),
639637
)
640638

641-
const { runPromise } = makeRuntime(Service, defaultLayer)
642-
643-
export async function isOverflow(input: { tokens: MessageV2.Assistant["tokens"]; model: Provider.Model }) {
644-
return runPromise((svc) => svc.isOverflow(input))
645-
}
646-
647-
export async function prune(input: { sessionID: SessionID }) {
648-
return runPromise((svc) => svc.prune(input))
649-
}
650-
651639
export * as SessionCompaction from "./compaction"

0 commit comments

Comments
 (0)