|
| 1 | +import { GlobalBus } from "@/bus/global" |
| 2 | +import { WorkspaceContext } from "@/control-plane/workspace-context" |
| 3 | +import { disposeInstance } from "@/effect/instance-registry" |
| 4 | +import { makeRuntime } from "@/effect/run-service" |
| 5 | +import { AppFileSystem } from "@opencode-ai/core/filesystem" |
| 6 | +import { Context, Deferred, Duration, Effect, Exit, Layer, Scope } from "effect" |
| 7 | +import { context, type InstanceContext } from "./instance-context" |
| 8 | +import * as Project from "./project" |
| 9 | + |
| 10 | +export interface LoadInput { |
| 11 | + directory: string |
| 12 | + init?: () => Promise<unknown> |
| 13 | + worktree?: string |
| 14 | + project?: Project.Info |
| 15 | +} |
| 16 | + |
| 17 | +export interface Interface { |
| 18 | + readonly load: (input: LoadInput) => Effect.Effect<InstanceContext> |
| 19 | + readonly reload: (input: LoadInput) => Effect.Effect<InstanceContext> |
| 20 | + readonly dispose: (ctx: InstanceContext) => Effect.Effect<void> |
| 21 | + readonly disposeAll: () => Effect.Effect<void> |
| 22 | +} |
| 23 | + |
| 24 | +export class Service extends Context.Service<Service, Interface>()("@opencode/InstanceStore") {} |
| 25 | + |
| 26 | +interface Entry { |
| 27 | + readonly deferred: Deferred.Deferred<InstanceContext> |
| 28 | +} |
| 29 | + |
| 30 | +export const layer: Layer.Layer<Service, never, Project.Service> = Layer.effect( |
| 31 | + Service, |
| 32 | + Effect.gen(function* () { |
| 33 | + const project = yield* Project.Service |
| 34 | + const scope = yield* Scope.Scope |
| 35 | + const cache = new Map<string, Entry>() |
| 36 | + |
| 37 | + const boot = Effect.fn("InstanceStore.boot")(function* (input: LoadInput & { directory: string }) { |
| 38 | + const ctx = |
| 39 | + input.project && input.worktree |
| 40 | + ? { |
| 41 | + directory: input.directory, |
| 42 | + worktree: input.worktree, |
| 43 | + project: input.project, |
| 44 | + } |
| 45 | + : yield* project.fromDirectory(input.directory).pipe( |
| 46 | + Effect.map((result) => ({ |
| 47 | + directory: input.directory, |
| 48 | + worktree: result.sandbox, |
| 49 | + project: result.project, |
| 50 | + })), |
| 51 | + ) |
| 52 | + const init = input.init |
| 53 | + if (init) yield* Effect.promise(() => context.provide(ctx, init)) |
| 54 | + return ctx |
| 55 | + }) |
| 56 | + |
| 57 | + const removeEntry = (directory: string, entry: Entry) => |
| 58 | + Effect.sync(() => { |
| 59 | + if (cache.get(directory) !== entry) return false |
| 60 | + cache.delete(directory) |
| 61 | + return true |
| 62 | + }) |
| 63 | + |
| 64 | + const completeLoad = Effect.fnUntraced(function* (directory: string, input: LoadInput, entry: Entry) { |
| 65 | + const exit = yield* Effect.exit(boot({ ...input, directory })) |
| 66 | + if (Exit.isFailure(exit)) yield* removeEntry(directory, entry) |
| 67 | + yield* Deferred.done(entry.deferred, exit).pipe(Effect.asVoid) |
| 68 | + }) |
| 69 | + |
| 70 | + const emitDisposed = (input: { directory: string; project?: string }) => |
| 71 | + Effect.sync(() => |
| 72 | + GlobalBus.emit("event", { |
| 73 | + directory: input.directory, |
| 74 | + project: input.project, |
| 75 | + workspace: WorkspaceContext.workspaceID, |
| 76 | + payload: { |
| 77 | + type: "server.instance.disposed", |
| 78 | + properties: { |
| 79 | + directory: input.directory, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }), |
| 83 | + ) |
| 84 | + |
| 85 | + const disposeContext = Effect.fn("InstanceStore.disposeContext")(function* (ctx: InstanceContext) { |
| 86 | + yield* Effect.logInfo("disposing instance", { directory: ctx.directory }) |
| 87 | + yield* Effect.promise(() => disposeInstance(ctx.directory)) |
| 88 | + yield* emitDisposed({ directory: ctx.directory, project: ctx.project.id }) |
| 89 | + }) |
| 90 | + |
| 91 | + const disposeEntry = Effect.fnUntraced(function* (directory: string, entry: Entry, ctx: InstanceContext) { |
| 92 | + if (cache.get(directory) !== entry) return false |
| 93 | + yield* disposeContext(ctx) |
| 94 | + if (cache.get(directory) !== entry) return false |
| 95 | + cache.delete(directory) |
| 96 | + return true |
| 97 | + }) |
| 98 | + |
| 99 | + const load = Effect.fn("InstanceStore.load")(function* (input: LoadInput) { |
| 100 | + const directory = AppFileSystem.resolve(input.directory) |
| 101 | + return yield* Effect.uninterruptibleMask((restore) => |
| 102 | + Effect.gen(function* () { |
| 103 | + const existing = cache.get(directory) |
| 104 | + if (existing) return yield* restore(Deferred.await(existing.deferred)) |
| 105 | + |
| 106 | + const entry: Entry = { deferred: Deferred.makeUnsafe<InstanceContext>() } |
| 107 | + cache.set(directory, entry) |
| 108 | + yield* Effect.gen(function* () { |
| 109 | + yield* Effect.logInfo("creating instance", { directory }) |
| 110 | + yield* completeLoad(directory, input, entry) |
| 111 | + }).pipe(Effect.forkIn(scope, { startImmediately: true })) |
| 112 | + return yield* restore(Deferred.await(entry.deferred)) |
| 113 | + }), |
| 114 | + ) |
| 115 | + }) |
| 116 | + |
| 117 | + const reload = Effect.fn("InstanceStore.reload")(function* (input: LoadInput) { |
| 118 | + const directory = AppFileSystem.resolve(input.directory) |
| 119 | + return yield* Effect.uninterruptibleMask((restore) => |
| 120 | + Effect.gen(function* () { |
| 121 | + const previous = cache.get(directory) |
| 122 | + const entry: Entry = { deferred: Deferred.makeUnsafe<InstanceContext>() } |
| 123 | + cache.set(directory, entry) |
| 124 | + yield* Effect.gen(function* () { |
| 125 | + yield* Effect.logInfo("reloading instance", { directory }) |
| 126 | + if (previous) { |
| 127 | + yield* Deferred.await(previous.deferred).pipe(Effect.ignore) |
| 128 | + yield* Effect.promise(() => disposeInstance(directory)) |
| 129 | + yield* emitDisposed({ directory, project: input.project?.id }) |
| 130 | + } |
| 131 | + yield* completeLoad(directory, input, entry) |
| 132 | + }).pipe(Effect.forkIn(scope, { startImmediately: true })) |
| 133 | + return yield* restore(Deferred.await(entry.deferred)) |
| 134 | + }), |
| 135 | + ) |
| 136 | + }) |
| 137 | + |
| 138 | + const dispose = Effect.fn("InstanceStore.dispose")(function* (ctx: InstanceContext) { |
| 139 | + const entry = cache.get(ctx.directory) |
| 140 | + if (!entry) return yield* disposeContext(ctx) |
| 141 | + |
| 142 | + const exit = yield* Deferred.await(entry.deferred).pipe(Effect.exit) |
| 143 | + if (Exit.isFailure(exit)) return yield* removeEntry(ctx.directory, entry).pipe(Effect.asVoid) |
| 144 | + if (exit.value !== ctx) return |
| 145 | + yield* disposeEntry(ctx.directory, entry, ctx).pipe(Effect.asVoid) |
| 146 | + }) |
| 147 | + |
| 148 | + const disposeAllOnce = Effect.fnUntraced(function* () { |
| 149 | + yield* Effect.logInfo("disposing all instances") |
| 150 | + yield* Effect.forEach( |
| 151 | + [...cache.entries()], |
| 152 | + (item) => |
| 153 | + Effect.gen(function* () { |
| 154 | + const exit = yield* Deferred.await(item[1].deferred).pipe(Effect.exit) |
| 155 | + if (Exit.isFailure(exit)) { |
| 156 | + yield* Effect.logWarning("instance dispose failed", { key: item[0], cause: exit.cause }) |
| 157 | + yield* removeEntry(item[0], item[1]) |
| 158 | + return |
| 159 | + } |
| 160 | + yield* disposeEntry(item[0], item[1], exit.value) |
| 161 | + }), |
| 162 | + { discard: true }, |
| 163 | + ) |
| 164 | + }) |
| 165 | + |
| 166 | + const cachedDisposeAll = yield* Effect.cachedWithTTL(disposeAllOnce(), Duration.zero) |
| 167 | + const disposeAll = Effect.fn("InstanceStore.disposeAll")(function* () { |
| 168 | + return yield* cachedDisposeAll |
| 169 | + }) |
| 170 | + |
| 171 | + yield* Effect.addFinalizer(() => disposeAll().pipe(Effect.ignore)) |
| 172 | + |
| 173 | + return Service.of({ |
| 174 | + load, |
| 175 | + reload, |
| 176 | + dispose, |
| 177 | + disposeAll, |
| 178 | + }) |
| 179 | + }), |
| 180 | +) |
| 181 | + |
| 182 | +export const defaultLayer = layer.pipe(Layer.provide(Project.defaultLayer)) |
| 183 | + |
| 184 | +export const runtime = makeRuntime(Service, defaultLayer) |
| 185 | + |
| 186 | +export * as InstanceStore from "./instance-store" |
0 commit comments