Skip to content

Commit 4c4860f

Browse files
authored
Replace Instance.disposeAll/load with fixture helper (#25418)
1 parent 5242a1c commit 4c4860f

59 files changed

Lines changed: 139 additions & 130 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/opencode/src/cli/bootstrap.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { AppRuntime } from "@/effect/app-runtime"
21
import { Instance } from "../project/instance"
2+
import { InstanceStore } from "../project/instance-store"
33

44
export async function bootstrap<T>(directory: string, cb: () => Promise<T>) {
55
return Instance.provide({
@@ -9,7 +9,7 @@ export async function bootstrap<T>(directory: string, cb: () => Promise<T>) {
99
const result = await cb()
1010
return result
1111
} finally {
12-
await Instance.dispose()
12+
await InstanceStore.runtime.runPromise((s) => s.dispose(Instance.current))
1313
}
1414
},
1515
})

packages/opencode/src/cli/cmd/tui/worker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Installation } from "@/installation"
22
import { Server } from "@/server/server"
33
import * as Log from "@opencode-ai/core/util/log"
44
import { Instance } from "@/project/instance"
5+
import { InstanceStore } from "@/project/instance-store"
56
import { Rpc } from "@/util/rpc"
67
import { upgrade } from "@/cli/upgrade"
78
import { Config } from "@/config/config"
@@ -87,7 +88,7 @@ export const rpc = {
8788
async shutdown() {
8889
Log.Default.info("worker shutting down")
8990

90-
await Instance.disposeAll()
91+
await InstanceStore.runtime.runPromise((s) => s.disposeAll())
9192
if (server) await server.stop(true)
9293
},
9394
}

packages/opencode/src/config/config.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import { Flag } from "@opencode-ai/core/flag/flag"
1111
import { Auth } from "../auth"
1212
import { Env } from "../env"
1313
import { applyEdits, modify } from "jsonc-parser"
14-
import { Instance, type InstanceContext } from "../project/instance"
14+
import { type InstanceContext } from "../project/instance"
15+
import { InstanceStore } from "../project/instance-store"
16+
import { InstanceRef } from "@/effect/instance-ref"
1517
import { InstallationLocal, InstallationVersion } from "@opencode-ai/core/installation/version"
1618
import { existsSync } from "fs"
1719
import { GlobalBus } from "@/bus/global"
@@ -736,12 +738,16 @@ export const layer = Layer.effect(
736738
yield* fs
737739
.writeFileString(file, JSON.stringify(mergeDeep(writable(existing), writable(config)), null, 2))
738740
.pipe(Effect.orDie)
739-
if (options?.dispose !== false) yield* Effect.promise(() => Instance.dispose())
741+
if (options?.dispose !== false) {
742+
const ctx = yield* InstanceRef
743+
if (ctx) yield* Effect.promise(() => InstanceStore.runtime.runPromise((s) => s.dispose(ctx)))
744+
}
740745
})
741746

742747
const invalidate = Effect.fn("Config.invalidate")(function* (wait?: boolean) {
743748
yield* invalidateGlobal
744-
const task = Instance.disposeAll()
749+
const task = InstanceStore.runtime
750+
.runPromise((s) => s.disposeAll())
745751
.catch(() => undefined)
746752
.finally(() =>
747753
GlobalBus.emit("event", {

packages/opencode/src/effect/app-runtime.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { Command } from "@/command"
3939
import { Truncate } from "@/tool/truncate"
4040
import { ToolRegistry } from "@/tool/registry"
4141
import { Format } from "@/format"
42+
import { InstanceStore } from "@/project/instance-store"
4243
import { Project } from "@/project/project"
4344
import { Vcs } from "@/project/vcs"
4445
import { Workspace } from "@/control-plane/workspace"
@@ -90,6 +91,7 @@ export const AppLayer = Layer.mergeAll(
9091
Truncate.defaultLayer,
9192
ToolRegistry.defaultLayer,
9293
Format.defaultLayer,
94+
InstanceStore.defaultLayer,
9395
Project.defaultLayer,
9496
Vcs.defaultLayer,
9597
Workspace.defaultLayer,

packages/opencode/src/project/instance.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ export type { InstanceContext } from "./instance-context"
66
export type { LoadInput } from "./instance-store"
77

88
export const Instance = {
9-
load(input: InstanceStore.LoadInput): Promise<InstanceContext> {
10-
return InstanceStore.runtime.runPromise((store) => store.load(input))
11-
},
129
async provide<R>(input: { directory: string; init?: Effect.Effect<void>; fn: () => R }): Promise<R> {
13-
return context.provide(await Instance.load({ directory: input.directory, init: input.init }), async () =>
14-
input.fn(),
10+
const ctx = await InstanceStore.runtime.runPromise((store) =>
11+
store.load({ directory: input.directory, init: input.init }),
1512
)
13+
return context.provide(ctx, async () => input.fn())
1614
},
1715
get current() {
1816
return context.use()
@@ -50,7 +48,4 @@ export const Instance = {
5048
async dispose() {
5149
return InstanceStore.runtime.runPromise((store) => store.dispose(Instance.current))
5250
},
53-
async disposeAll() {
54-
return InstanceStore.runtime.runPromise((store) => store.disposeAll())
55-
},
5651
}

packages/opencode/src/server/routes/global.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { SyncEvent } from "@/sync"
88
import { GlobalBus } from "@/bus/global"
99
import { AppRuntime } from "@/effect/app-runtime"
1010
import { AsyncQueue } from "@/util/queue"
11-
import { Instance } from "../../project/instance"
11+
import { InstanceStore } from "../../project/instance-store"
1212
import { Installation } from "@/installation"
1313
import { InstallationVersion } from "@opencode-ai/core/installation/version"
1414
import * as Log from "@opencode-ai/core/util/log"
@@ -200,7 +200,7 @@ export const GlobalRoutes = lazy(() =>
200200
},
201201
}),
202202
async (c) => {
203-
await Instance.disposeAll()
203+
await InstanceStore.runtime.runPromise((s) => s.disposeAll())
204204
GlobalBus.emit("event", {
205205
directory: "global",
206206
payload: {

packages/opencode/src/server/routes/instance/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import z from "zod"
66
import { Format } from "@/format"
77
import { TuiRoutes } from "./tui"
88
import { Instance } from "@/project/instance"
9+
import { InstanceStore } from "@/project/instance-store"
910
import { Vcs } from "@/project/vcs"
1011
import { Agent } from "@/agent/agent"
1112
import { Skill } from "@/skill"
@@ -62,7 +63,7 @@ export const InstanceRoutes = (upgrade: UpgradeWebSocket): Hono => {
6263
},
6364
}),
6465
async (c) => {
65-
await Instance.dispose()
66+
await InstanceStore.runtime.runPromise((s) => s.dispose(Instance.current))
6667
return c.json(true)
6768
},
6869
)

packages/opencode/src/server/routes/instance/project.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,7 @@ export const ProjectRoutes = lazy(() =>
8181
Project.Service.use((svc) => svc.initGit({ directory: dir, project: prev })),
8282
)
8383
if (next.id === prev.id && next.vcs === prev.vcs && next.worktree === prev.worktree) return c.json(next)
84-
await Instance.reload({
85-
directory: dir,
86-
worktree: dir,
87-
project: next,
88-
})
84+
await Instance.reload({ directory: dir, worktree: dir, project: next })
8985
return c.json(next)
9086
},
9187
)

packages/opencode/test/agent/agent.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { afterEach, test, expect } from "bun:test"
22
import { Effect } from "effect"
33
import path from "path"
4-
import { provideInstance, tmpdir } from "../fixture/fixture"
4+
import { disposeAllInstances, provideInstance, tmpdir } from "../fixture/fixture"
55
import { Instance } from "../../src/project/instance"
66
import { Agent } from "../../src/agent/agent"
77
import { Permission } from "../../src/permission"
@@ -18,7 +18,7 @@ function load<A>(dir: string, fn: (svc: Agent.Interface) => Effect.Effect<A>) {
1818
}
1919

2020
afterEach(async () => {
21-
await Instance.disposeAll()
21+
await disposeAllInstances()
2222
})
2323

2424
test("returns default native agents when no config", async () => {

packages/opencode/test/bus/bus-effect.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Bus } from "../../src/bus"
44
import { BusEvent } from "../../src/bus/bus-event"
55
import { Instance } from "../../src/project/instance"
66
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
7-
import { provideInstance, provideTmpdirInstance, tmpdirScoped } from "../fixture/fixture"
7+
import { disposeAllInstances, provideInstance, provideTmpdirInstance, tmpdirScoped } from "../fixture/fixture"
88
import { testEffect } from "../lib/effect"
99

1010
const TestEvent = {
@@ -151,7 +151,7 @@ describe("Bus (Effect-native)", () => {
151151
}).pipe(provideInstance(dir))
152152

153153
// Dispose from OUTSIDE the instance scope
154-
yield* Effect.promise(() => Instance.disposeAll())
154+
yield* Effect.promise(disposeAllInstances)
155155
yield* Deferred.await(disposed).pipe(Effect.timeout("2 seconds"))
156156

157157
expect(types).toContain("test.effect.ping")

0 commit comments

Comments
 (0)