Replace Instance.disposeAll/load with fixture helper (#25418)

This commit is contained in:
Kit Langton 2026-05-02 10:56:15 -04:00 • committed by GitHub
commit 4c4860fb24
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
59 changed files with 139 additions and 130 deletions

View file

@ -1,5 +1,5 @@
import { AppRuntime } from "@/effect/app-runtime"
import { Instance } from "../project/instance"
import { InstanceStore } from "../project/instance-store"
export async function bootstrap<T>(directory: string, cb: () => Promise<T>) {
return Instance.provide({
@ -9,7 +9,7 @@ export async function bootstrap<T>(directory: string, cb: () => Promise<T>) {
const result = await cb()
return result
} finally {
await Instance.dispose()
await InstanceStore.runtime.runPromise((s) => s.dispose(Instance.current))
}
},
})

View file

@ -2,6 +2,7 @@ import { Installation } from "@/installation"
import { Server } from "@/server/server"
import * as Log from "@opencode-ai/core/util/log"
import { Instance } from "@/project/instance"
import { InstanceStore } from "@/project/instance-store"
import { Rpc } from "@/util/rpc"
import { upgrade } from "@/cli/upgrade"
import { Config } from "@/config/config"
@ -87,7 +88,7 @@ export const rpc = {
async shutdown() {
Log.Default.info("worker shutting down")
await Instance.disposeAll()
await InstanceStore.runtime.runPromise((s) => s.disposeAll())
if (server) await server.stop(true)
},
}

View file

@ -11,7 +11,9 @@ import { Flag } from "@opencode-ai/core/flag/flag"
import { Auth } from "../auth"
import { Env } from "../env"
import { applyEdits, modify } from "jsonc-parser"
import { Instance, type InstanceContext } from "../project/instance"
import { type InstanceContext } from "../project/instance"
import { InstanceStore } from "../project/instance-store"
import { InstanceRef } from "@/effect/instance-ref"
import { InstallationLocal, InstallationVersion } from "@opencode-ai/core/installation/version"
import { existsSync } from "fs"
import { GlobalBus } from "@/bus/global"
@ -736,12 +738,16 @@ export const layer = Layer.effect(
yield* fs
.writeFileString(file, JSON.stringify(mergeDeep(writable(existing), writable(config)), null, 2))
.pipe(Effect.orDie)
if (options?.dispose !== false) yield* Effect.promise(() => Instance.dispose())
if (options?.dispose !== false) {
const ctx = yield* InstanceRef
if (ctx) yield* Effect.promise(() => InstanceStore.runtime.runPromise((s) => s.dispose(ctx)))
}
})
const invalidate = Effect.fn("Config.invalidate")(function* (wait?: boolean) {
yield* invalidateGlobal
const task = Instance.disposeAll()
const task = InstanceStore.runtime
.runPromise((s) => s.disposeAll())
.catch(() => undefined)
.finally(() =>
GlobalBus.emit("event", {

View file

@ -39,6 +39,7 @@ import { Command } from "@/command"
import { Truncate } from "@/tool/truncate"
import { ToolRegistry } from "@/tool/registry"
import { Format } from "@/format"
import { InstanceStore } from "@/project/instance-store"
import { Project } from "@/project/project"
import { Vcs } from "@/project/vcs"
import { Workspace } from "@/control-plane/workspace"
@ -90,6 +91,7 @@ export const AppLayer = Layer.mergeAll(
Truncate.defaultLayer,
ToolRegistry.defaultLayer,
Format.defaultLayer,
InstanceStore.defaultLayer,
Project.defaultLayer,
Vcs.defaultLayer,
Workspace.defaultLayer,

View file

@ -6,13 +6,11 @@ export type { InstanceContext } from "./instance-context"
export type { LoadInput } from "./instance-store"
export const Instance = {
load(input: InstanceStore.LoadInput): Promise<InstanceContext> {
return InstanceStore.runtime.runPromise((store) => store.load(input))
},
async provide<R>(input: { directory: string; init?: Effect.Effect<void>; fn: () => R }): Promise<R> {
return context.provide(await Instance.load({ directory: input.directory, init: input.init }), async () =>
input.fn(),
const ctx = await InstanceStore.runtime.runPromise((store) =>
store.load({ directory: input.directory, init: input.init }),
)
return context.provide(ctx, async () => input.fn())
},
get current() {
return context.use()
@ -50,7 +48,4 @@ export const Instance = {
async dispose() {
return InstanceStore.runtime.runPromise((store) => store.dispose(Instance.current))
},
async disposeAll() {
return InstanceStore.runtime.runPromise((store) => store.disposeAll())
},
}

View file

@ -8,7 +8,7 @@ import { SyncEvent } from "@/sync"
import { GlobalBus } from "@/bus/global"
import { AppRuntime } from "@/effect/app-runtime"
import { AsyncQueue } from "@/util/queue"
import { Instance } from "../../project/instance"
import { InstanceStore } from "../../project/instance-store"
import { Installation } from "@/installation"
import { InstallationVersion } from "@opencode-ai/core/installation/version"
import * as Log from "@opencode-ai/core/util/log"
@ -200,7 +200,7 @@ export const GlobalRoutes = lazy(() =>
},
}),
async (c) => {
await Instance.disposeAll()
await InstanceStore.runtime.runPromise((s) => s.disposeAll())
GlobalBus.emit("event", {
directory: "global",
payload: {

View file

@ -6,6 +6,7 @@ import z from "zod"
import { Format } from "@/format"
import { TuiRoutes } from "./tui"
import { Instance } from "@/project/instance"
import { InstanceStore } from "@/project/instance-store"
import { Vcs } from "@/project/vcs"
import { Agent } from "@/agent/agent"
import { Skill } from "@/skill"
@ -62,7 +63,7 @@ export const InstanceRoutes = (upgrade: UpgradeWebSocket): Hono => {
},
}),
async (c) => {
await Instance.dispose()
await InstanceStore.runtime.runPromise((s) => s.dispose(Instance.current))
return c.json(true)
},
)

View file

@ -81,11 +81,7 @@ export const ProjectRoutes = lazy(() =>
Project.Service.use((svc) => svc.initGit({ directory: dir, project: prev })),
)
if (next.id === prev.id && next.vcs === prev.vcs && next.worktree === prev.worktree) return c.json(next)
await Instance.reload({
directory: dir,
worktree: dir,
project: next,
})
await Instance.reload({ directory: dir, worktree: dir, project: next })
return c.json(next)
},
)

View file

@ -1,7 +1,7 @@
import { afterEach, test, expect } from "bun:test"
import { Effect } from "effect"
import path from "path"
import { provideInstance, tmpdir } from "../fixture/fixture"
import { disposeAllInstances, provideInstance, tmpdir } from "../fixture/fixture"
import { Instance } from "../../src/project/instance"
import { Agent } from "../../src/agent/agent"
import { Permission } from "../../src/permission"
@ -18,7 +18,7 @@ function load<A>(dir: string, fn: (svc: Agent.Interface) => Effect.Effect<A>) {
}
afterEach(async () => {
await Instance.disposeAll()
await disposeAllInstances()
})
test("returns default native agents when no config", async () => {

View file

@ -4,7 +4,7 @@ import { Bus } from "../../src/bus"
import { BusEvent } from "../../src/bus/bus-event"
import { Instance } from "../../src/project/instance"
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
import { provideInstance, provideTmpdirInstance, tmpdirScoped } from "../fixture/fixture"
import { disposeAllInstances, provideInstance, provideTmpdirInstance, tmpdirScoped } from "../fixture/fixture"
import { testEffect } from "../lib/effect"
const TestEvent = {
@ -151,7 +151,7 @@ describe("Bus (Effect-native)", () => {
}).pipe(provideInstance(dir))
// Dispose from OUTSIDE the instance scope
yield* Effect.promise(() => Instance.disposeAll())
yield* Effect.promise(disposeAllInstances)
yield* Deferred.await(disposed).pipe(Effect.timeout("2 seconds"))
expect(types).toContain("test.effect.ping")

View file

@ -3,7 +3,7 @@ import { Schema } from "effect"
import { Bus } from "../../src/bus"
import { BusEvent } from "../../src/bus/bus-event"
import { Instance } from "../../src/project/instance"
import { tmpdir } from "../fixture/fixture"
import { disposeAllInstances, tmpdir } from "../fixture/fixture"
const TestEvent = BusEvent.define("test.integration", Schema.Struct({ value: Schema.Number }))
@ -12,7 +12,7 @@ function withInstance(directory: string, fn: () => Promise<void>) {
}
describe("Bus integration: acquireRelease subscriber pattern", () => {
afterEach(() => Instance.disposeAll())
afterEach(() => disposeAllInstances())
test("subscriber via callback facade receives events and cleans up on unsub", async () => {
await using tmp = await tmpdir()
@ -78,7 +78,7 @@ describe("Bus integration: acquireRelease subscriber pattern", () => {
await Bun.sleep(10)
})
await Instance.disposeAll()
await disposeAllInstances()
await Bun.sleep(50)
expect(received).toEqual([1])

View file

@ -3,7 +3,7 @@ import { Schema } from "effect"
import { Bus } from "../../src/bus"
import { BusEvent } from "../../src/bus/bus-event"
import { Instance } from "../../src/project/instance"
import { tmpdir } from "../fixture/fixture"
import { disposeAllInstances, tmpdir } from "../fixture/fixture"
const TestEvent = {
Ping: BusEvent.define("test.ping", Schema.Struct({ value: Schema.Number })),
@ -15,7 +15,7 @@ function withInstance(directory: string, fn: () => Promise<void>) {
}
describe("Bus", () => {
afterEach(() => Instance.disposeAll())
afterEach(() => disposeAllInstances())
describe("publish + subscribe", () => {
test("subscriber is live immediately after subscribe returns", async () => {
@ -208,8 +208,8 @@ describe("Bus", () => {
await Bun.sleep(10)
})
// Instance.disposeAll triggers the finalizer which publishes InstanceDisposed
await Instance.disposeAll()
// disposeAllInstances triggers the finalizer which publishes InstanceDisposed
await disposeAllInstances()
await Bun.sleep(50)
expect(received).toContain("test.ping")

View file

@ -12,7 +12,7 @@ import { Account } from "../../src/account/account"
import { AccessToken, AccountID, OrgID } from "../../src/account/schema"
import { AppFileSystem } from "@opencode-ai/core/filesystem"
import { Env } from "../../src/env"
import { provideTmpdirInstance } from "../fixture/fixture"
import { disposeAllInstances, provideTmpdirInstance } from "../fixture/fixture"
import { tmpdir } from "../fixture/fixture"
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
import { testEffect } from "../lib/effect"
@ -108,7 +108,7 @@ async function check(map: (dir: string) => string) {
},
})
} finally {
await Instance.disposeAll()
await disposeAllInstances()
;(Global.Path as { config: string }).config = prev
await clear()
}

View file

@ -21,7 +21,7 @@ import { ModelID, ProviderID } from "@/provider/schema"
import { SyncEvent } from "@/sync"
import { EventSequenceTable, EventTable } from "@/sync/event.sql"
import { resetDatabase } from "../fixture/db"
import { provideTmpdirInstance, tmpdir } from "../fixture/fixture"
import { disposeAllInstances, provideTmpdirInstance, tmpdir } from "../fixture/fixture"
import { testEffect } from "../lib/effect"
import { registerAdapter } from "../../src/control-plane/adapters"
import { WorkspaceID } from "../../src/control-plane/schema"
@ -93,7 +93,7 @@ beforeEach(() => {
afterEach(async () => {
mock.restore()
await Instance.disposeAll()
await disposeAllInstances()
Flag.OPENCODE_EXPERIMENTAL_WORKSPACES = originalWorkspacesFlag
restoreEnv()
await resetDatabase()

View file

@ -4,7 +4,7 @@ import { $ } from "bun"
import { Context, Deferred, Duration, Effect, Exit, Fiber, Layer } from "effect"
import { InstanceState } from "@/effect/instance-state"
import { Instance } from "../../src/project/instance"
import { provideInstance, tmpdirScoped } from "../fixture/fixture"
import { disposeAllInstances, provideInstance, tmpdirScoped } from "../fixture/fixture"
import { testEffect } from "../lib/effect"
const it = testEffect(CrossSpawnSpawner.defaultLayer)
@ -19,7 +19,7 @@ const tmpdirGitScoped = Effect.gen(function* () {
})
afterEach(async () => {
await Instance.disposeAll()
await disposeAllInstances()
})
it.live("InstanceState caches values per directory", () =>
@ -94,7 +94,7 @@ it.live("InstanceState invalidates on disposeAll", () =>
yield* access(state, one)
yield* access(state, two)
yield* Effect.promise(() => Instance.disposeAll())
yield* Effect.promise(disposeAllInstances)
expect(seen.sort()).toEqual([one, two].sort())
}),

View file

@ -6,10 +6,10 @@ import fs from "fs/promises"
import { File } from "../../src/file"
import { Instance } from "../../src/project/instance"
import { Filesystem } from "@/util/filesystem"
import { provideInstance, tmpdir } from "../fixture/fixture"
import { disposeAllInstances, provideInstance, tmpdir } from "../fixture/fixture"
afterEach(async () => {
await Instance.disposeAll()
await disposeAllInstances()
})
const init = () => run(File.Service.use((svc) => svc.init()))
@ -936,7 +936,7 @@ describe("file/index Filesystem patterns", () => {
},
})
await Instance.disposeAll()
await disposeAllInstances()
await fs.writeFile(path.join(tmp.path, "after.ts"), "after", "utf-8")
await fs.rm(path.join(tmp.path, "before.ts"))

View file

@ -3,7 +3,7 @@ import { afterEach, describe, expect, test } from "bun:test"
import fs from "fs/promises"
import path from "path"
import { ConfigProvider, Deferred, Effect, Layer, ManagedRuntime, Option } from "effect"
import { tmpdir } from "../fixture/fixture"
import { disposeAllInstances, tmpdir } from "../fixture/fixture"
import { Bus } from "../../src/bus"
import { Config } from "@/config/config"
import { FileWatcher } from "../../src/file/watcher"
@ -147,7 +147,7 @@ function ready(directory: string) {
describeWatcher("FileWatcher", () => {
afterEach(async () => {
await Instance.disposeAll()
await disposeAllInstances()
})
test("publishes root create, update, and delete events", async () => {

View file

@ -1,9 +1,9 @@
import { rm } from "fs/promises"
import { Instance } from "../../src/project/instance"
import { Database } from "@/storage/db"
import { disposeAllInstances } from "./fixture"
export async function resetDatabase() {
await Instance.disposeAll().catch(() => undefined)
await disposeAllInstances().catch(() => undefined)
Database.close()
await rm(Database.Path, { force: true }).catch(() => undefined)
await rm(`${Database.Path}-wal`, { force: true }).catch(() => undefined)

View file

@ -9,8 +9,16 @@ import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
import type { Config } from "@/config/config"
import { InstanceRef } from "../../src/effect/instance-ref"
import { Instance } from "../../src/project/instance"
import { InstanceStore } from "../../src/project/instance-store"
import { TestLLMServer } from "../lib/llm-server"
// Test helper for tearing down all loaded instances. Used in afterEach hooks.
// Replaces direct Instance.disposeAll() calls so the legacy promise method can be removed.
// IMPORTANT: must use InstanceStore.runtime, not AppRuntime or a test-layer Service —
// Instance.provide loads instances into InstanceStore.runtime's Service cache, and that
// Service is built per-runtime (not shared via memoMap across Effect.runPromise boundaries).
export const disposeAllInstances = () => InstanceStore.runtime.runPromise((s) => s.disposeAll())
// Strip null bytes from paths (defensive fix for CI environment issues)
function sanitizePath(p: string): string {
return p.replace(/\0/g, "")

View file

@ -2,13 +2,13 @@ import { afterEach, describe, test, expect } from "bun:test"
import { Permission } from "../src/permission"
import { Config } from "@/config/config"
import { Instance } from "../src/project/instance"
import { tmpdir } from "./fixture/fixture"
import { disposeAllInstances, tmpdir } from "./fixture/fixture"
import { AppRuntime } from "../src/effect/app-runtime"
const load = () => AppRuntime.runPromise(Config.Service.use((svc) => svc.get()))
afterEach(async () => {
await Instance.disposeAll()
await disposeAllInstances()
})
describe("Permission.evaluate for permission.task", () => {

Some files were not shown because too many files have changed in this diff Show more