-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Move instance loading into Effect service #25277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f5398e7
refactor: move instance loading into service
kitlangton f1470c1
refactor: rename instance store service interface
kitlangton f0136f9
fix: keep httpapi instance reloads in layer store
kitlangton c565bd5
refactor: simplify instance store wiring
kitlangton 8a63cbe
refactor: simplify instance store concurrency
kitlangton 1b146ad
refactor: replace disposeAll dedup slot with cachedWithTTL
kitlangton 74373f8
fix: skip reload disposal for fresh instances
kitlangton d9252a0
refactor: use Effect.ignore over exit + asVoid
kitlangton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { LocalContext } from "@/util/local-context" | ||
| import type * as Project from "./project" | ||
|
|
||
| export interface InstanceContext { | ||
| directory: string | ||
| worktree: string | ||
| project: Project.Info | ||
| } | ||
|
|
||
| export const context = LocalContext.create<InstanceContext>("instance") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| import { GlobalBus } from "@/bus/global" | ||
| import { WorkspaceContext } from "@/control-plane/workspace-context" | ||
| import { disposeInstance } from "@/effect/instance-registry" | ||
| import { makeRuntime } from "@/effect/run-service" | ||
| import { AppFileSystem } from "@opencode-ai/core/filesystem" | ||
| import { Context, Deferred, Effect, Exit, Layer, Scope } from "effect" | ||
| import { context, type InstanceContext } from "./instance-context" | ||
| import * as Project from "./project" | ||
|
|
||
| export interface LoadInput { | ||
| directory: string | ||
| init?: () => Promise<unknown> | ||
| worktree?: string | ||
| project?: Project.Info | ||
| } | ||
|
|
||
| export interface Interface { | ||
| readonly load: (input: LoadInput) => Effect.Effect<InstanceContext> | ||
| readonly reload: (input: LoadInput) => Effect.Effect<InstanceContext> | ||
| readonly dispose: (ctx: InstanceContext) => Effect.Effect<void> | ||
| readonly disposeAll: () => Effect.Effect<void> | ||
| } | ||
|
|
||
| export class Service extends Context.Service<Service, Interface>()("@opencode/InstanceStore") {} | ||
|
|
||
| interface Entry { | ||
| readonly deferred: Deferred.Deferred<InstanceContext> | ||
| } | ||
|
|
||
| export const layer: Layer.Layer<Service, never, Project.Service> = Layer.effect( | ||
| Service, | ||
| Effect.gen(function* () { | ||
| const project = yield* Project.Service | ||
| const scope = yield* Scope.Scope | ||
| const cache = new Map<string, Entry>() | ||
| const disposal = { | ||
| all: undefined as Deferred.Deferred<void> | undefined, | ||
| } | ||
|
|
||
| const boot = Effect.fn("InstanceStore.boot")(function* (input: LoadInput & { directory: string }) { | ||
| const ctx = | ||
| input.project && input.worktree | ||
| ? { | ||
| directory: input.directory, | ||
| worktree: input.worktree, | ||
| project: input.project, | ||
| } | ||
| : yield* project.fromDirectory(input.directory).pipe( | ||
| Effect.map((result) => ({ | ||
| directory: input.directory, | ||
| worktree: result.sandbox, | ||
| project: result.project, | ||
| })), | ||
| ) | ||
| const init = input.init | ||
| if (init) yield* Effect.promise(() => context.provide(ctx, init)) | ||
| return ctx | ||
| }) | ||
|
|
||
| const removeEntry = (directory: string, entry: Entry) => | ||
| Effect.sync(() => { | ||
| if (cache.get(directory) !== entry) return false | ||
| cache.delete(directory) | ||
| return true | ||
| }) | ||
|
|
||
| const completeLoad = Effect.fnUntraced(function* (directory: string, input: LoadInput, entry: Entry) { | ||
| const exit = yield* Effect.exit(boot({ ...input, directory })) | ||
| if (Exit.isFailure(exit)) yield* removeEntry(directory, entry) | ||
| yield* Deferred.done(entry.deferred, exit).pipe(Effect.asVoid) | ||
| }) | ||
|
|
||
| const emitDisposed = (input: { directory: string; project?: string }) => | ||
| Effect.sync(() => | ||
| GlobalBus.emit("event", { | ||
| directory: input.directory, | ||
| project: input.project, | ||
| workspace: WorkspaceContext.workspaceID, | ||
| payload: { | ||
| type: "server.instance.disposed", | ||
| properties: { | ||
|
kitlangton marked this conversation as resolved.
|
||
| directory: input.directory, | ||
| }, | ||
| }, | ||
| }), | ||
| ) | ||
|
|
||
| const disposeContext = Effect.fn("InstanceStore.disposeContext")(function* (ctx: InstanceContext) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test |
||
| yield* Effect.logInfo("disposing instance", { directory: ctx.directory }) | ||
| yield* Effect.promise(() => disposeInstance(ctx.directory)) | ||
| yield* emitDisposed({ directory: ctx.directory, project: ctx.project.id }) | ||
| }) | ||
|
|
||
| const disposeEntry = Effect.fnUntraced(function* (directory: string, entry: Entry, ctx: InstanceContext) { | ||
| if (cache.get(directory) !== entry) return false | ||
| yield* disposeContext(ctx) | ||
| if (cache.get(directory) !== entry) return false | ||
| cache.delete(directory) | ||
| return true | ||
| }) | ||
|
|
||
| const load = Effect.fn("InstanceStore.load")(function* (input: LoadInput) { | ||
| const directory = AppFileSystem.resolve(input.directory) | ||
| return yield* Effect.uninterruptibleMask((restore) => | ||
| Effect.gen(function* () { | ||
| const existing = cache.get(directory) | ||
| if (existing) return yield* restore(Deferred.await(existing.deferred)) | ||
|
|
||
| const entry: Entry = { deferred: Deferred.makeUnsafe<InstanceContext>() } | ||
| cache.set(directory, entry) | ||
| yield* Effect.gen(function* () { | ||
| yield* Effect.logInfo("creating instance", { directory }) | ||
| yield* completeLoad(directory, input, entry) | ||
| }).pipe(Effect.forkIn(scope, { startImmediately: true })) | ||
| return yield* restore(Deferred.await(entry.deferred)) | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| const reload = Effect.fn("InstanceStore.reload")(function* (input: LoadInput) { | ||
| const directory = AppFileSystem.resolve(input.directory) | ||
| return yield* Effect.uninterruptibleMask((restore) => | ||
| Effect.gen(function* () { | ||
| const previous = cache.get(directory) | ||
| const entry: Entry = { deferred: Deferred.makeUnsafe<InstanceContext>() } | ||
| cache.set(directory, entry) | ||
| yield* Effect.gen(function* () { | ||
| yield* Effect.logInfo("reloading instance", { directory }) | ||
| if (previous) yield* Deferred.await(previous.deferred).pipe(Effect.exit, Effect.asVoid) | ||
| yield* Effect.promise(() => disposeInstance(directory)) | ||
| yield* emitDisposed({ directory, project: input.project?.id }) | ||
| yield* completeLoad(directory, input, entry) | ||
| }).pipe(Effect.forkIn(scope, { startImmediately: true })) | ||
| return yield* restore(Deferred.await(entry.deferred)) | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| const dispose = Effect.fn("InstanceStore.dispose")(function* (ctx: InstanceContext) { | ||
| const entry = cache.get(ctx.directory) | ||
| if (!entry) return yield* disposeContext(ctx) | ||
|
|
||
| const exit = yield* Deferred.await(entry.deferred).pipe(Effect.exit) | ||
| if (Exit.isFailure(exit)) return yield* removeEntry(ctx.directory, entry).pipe(Effect.asVoid) | ||
| if (exit.value !== ctx) return | ||
| yield* disposeEntry(ctx.directory, entry, ctx).pipe(Effect.asVoid) | ||
| }) | ||
|
|
||
| const disposeAll = Effect.fn("InstanceStore.disposeAll")(function* () { | ||
| return yield* Effect.uninterruptibleMask((restore) => | ||
| Effect.gen(function* () { | ||
| const existing = disposal.all | ||
| if (existing) return yield* restore(Deferred.await(existing)) | ||
|
|
||
| const done = Deferred.makeUnsafe<void>() | ||
| const entries = [...cache.entries()] | ||
| disposal.all = done | ||
| const exit = yield* Effect.gen(function* () { | ||
| yield* Effect.logInfo("disposing all instances") | ||
| yield* Effect.forEach( | ||
| entries, | ||
| (item) => | ||
| Effect.gen(function* () { | ||
| const exit = yield* Deferred.await(item[1].deferred).pipe(Effect.exit) | ||
| if (Exit.isFailure(exit)) { | ||
| yield* Effect.logWarning("instance dispose failed", { key: item[0], cause: exit.cause }) | ||
| yield* removeEntry(item[0], item[1]) | ||
| return | ||
| } | ||
| yield* disposeEntry(item[0], item[1], exit.value) | ||
| }), | ||
| { discard: true }, | ||
| ) | ||
| }).pipe(Effect.exit) | ||
| yield* Deferred.done(done, exit).pipe(Effect.asVoid) | ||
| if (disposal.all === done) { | ||
| disposal.all = undefined | ||
| } | ||
| return yield* restore(Deferred.await(done)) | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| yield* Effect.addFinalizer(() => disposeAll().pipe(Effect.ignore)) | ||
|
|
||
| return Service.of({ | ||
| load, | ||
| reload, | ||
| dispose, | ||
| disposeAll, | ||
| }) | ||
| }), | ||
| ) | ||
|
|
||
| export const defaultLayer = layer.pipe(Layer.provide(Project.defaultLayer)) | ||
|
|
||
| export const runtime = makeRuntime(Service, defaultLayer) | ||
|
|
||
| export * as InstanceStore from "./instance-store" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.