mirror of
https://github.com/anomalyco/opencode.git
synced 2026-05-21 03:15:11 +00:00
refactor(instance): remove ambient instance context
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
import { InstanceRuntime } from "../project/instance-runtime"
|
||||
import { context } from "../project/instance-context"
|
||||
|
||||
export async function bootstrap<T>(directory: string, cb: () => Promise<T>) {
|
||||
const ctx = await InstanceRuntime.load({ directory })
|
||||
try {
|
||||
return await context.provide(ctx, cb)
|
||||
return await cb()
|
||||
} finally {
|
||||
await InstanceRuntime.disposeInstance(ctx)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { LocalContext } from "@/util/local-context"
|
||||
import { AppFileSystem } from "@opencode-ai/core/filesystem"
|
||||
import type * as Project from "./project"
|
||||
|
||||
@@ -8,8 +7,6 @@ export interface InstanceContext {
|
||||
project: Project.Info
|
||||
}
|
||||
|
||||
export const context = LocalContext.create<InstanceContext>("instance")
|
||||
|
||||
/**
|
||||
* Check if a path is within the project boundary.
|
||||
* Returns true if path is inside ctx.directory OR ctx.worktree.
|
||||
|
||||
@@ -3,6 +3,7 @@ import { $ } from "bun"
|
||||
import fs from "node:fs/promises"
|
||||
import Http from "node:http"
|
||||
import path from "node:path"
|
||||
import { AsyncLocalStorage } from "node:async_hooks"
|
||||
import { setTimeout as delay } from "node:timers/promises"
|
||||
import { NodeHttpServer } from "@effect/platform-node"
|
||||
import { Effect, Layer, Schema } from "effect"
|
||||
@@ -14,7 +15,7 @@ import { GlobalBus, type GlobalEvent } from "@/bus/global"
|
||||
import { Database } from "@/storage/db"
|
||||
import { ProjectID } from "@/project/schema"
|
||||
import { ProjectTable } from "@/project/project.sql"
|
||||
import { context, type InstanceContext } from "@/project/instance-context"
|
||||
import type { InstanceContext } from "@/project/instance-context"
|
||||
import { InstanceRef } from "@/effect/instance-ref"
|
||||
import { Session as SessionNs } from "@/session/session"
|
||||
import { SessionID } from "@/session/schema"
|
||||
@@ -68,6 +69,7 @@ const testServerLayer = Layer.mergeAll(
|
||||
SessionNs.defaultLayer,
|
||||
)
|
||||
const it = testEffect(testServerLayer)
|
||||
const testInstanceContext = new AsyncLocalStorage<InstanceContext>()
|
||||
|
||||
type RecordedCreate = {
|
||||
info: WorkspaceInfo
|
||||
@@ -124,7 +126,7 @@ afterEach(async () => {
|
||||
async function withInstance<T>(fn: (ctx: InstanceContext) => T | Promise<T>) {
|
||||
await using tmp = await tmpdir({ git: true })
|
||||
const ctx = await AppRuntime.runPromise(InstanceStore.Service.use((store) => store.load({ directory: tmp.path })))
|
||||
return await context.provide(ctx, () => fn(ctx))
|
||||
return await testInstanceContext.run(ctx, () => fn(ctx))
|
||||
}
|
||||
|
||||
async function initGitRepo(dir: string) {
|
||||
@@ -140,11 +142,7 @@ async function initGitRepo(dir: string) {
|
||||
}
|
||||
|
||||
function currentInstance() {
|
||||
try {
|
||||
return context.use()
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
return testInstanceContext.getStore()
|
||||
}
|
||||
|
||||
const runWorkspace = <A, E>(effect: Effect.Effect<A, E, Workspace.Service>) => {
|
||||
@@ -936,7 +934,7 @@ describe("workspace CRUD", () => {
|
||||
const workspaceCtx = await AppRuntime.runPromise(
|
||||
InstanceStore.Service.use((store) => store.load({ directory: workspaceTmp.path })),
|
||||
)
|
||||
const workspaceProjectID = await context.provide(workspaceCtx, async () => {
|
||||
const workspaceProjectID = await testInstanceContext.run(workspaceCtx, async () => {
|
||||
const id = workspaceCtx.project.id
|
||||
expect(id).not.toBe(projectID)
|
||||
await warpWorkspaceSession({ workspaceID: null, sessionID: session.id })
|
||||
|
||||
Reference in New Issue
Block a user