Normalize instance lifecycle wiring (#25501)
This commit is contained in:
parent
a6464062b7
commit
7d91d3b1ed
71 changed files with 852 additions and 936 deletions
|
|
@ -5,6 +5,7 @@ import path from "node:path"
|
|||
import { pathToFileURL } from "node:url"
|
||||
import { bootstrap as cliBootstrap } from "../../src/cli/bootstrap"
|
||||
import { Instance } from "../../src/project/instance"
|
||||
import { WithInstance } from "../../src/project/with-instance"
|
||||
import { InstanceRuntime } from "../../src/project/instance-runtime"
|
||||
import { InstanceMiddleware } from "../../src/server/routes/instance/middleware"
|
||||
import { disposeAllInstances, tmpdir } from "../fixture/fixture"
|
||||
|
|
@ -50,7 +51,7 @@ async function bootstrapFixture() {
|
|||
test("Instance.provide runs InstanceBootstrap before fn (boundary invariant)", async () => {
|
||||
await using tmp = await bootstrapFixture()
|
||||
|
||||
await Instance.provide({
|
||||
await WithInstance.provide({
|
||||
directory: tmp.path,
|
||||
fn: async () => "ok",
|
||||
})
|
||||
|
|
|
|||
|
|
@ -5,17 +5,23 @@ import { InstanceRef } from "../../src/effect/instance-ref"
|
|||
import { registerDisposer } from "../../src/effect/instance-registry"
|
||||
import { InstanceBootstrap } from "../../src/project/bootstrap-service"
|
||||
import { Instance } from "../../src/project/instance"
|
||||
import { WithInstance } from "../../src/project/with-instance"
|
||||
import { InstanceStore } from "../../src/project/instance-store"
|
||||
import { disposeAllInstances, tmpdirScoped } from "../fixture/fixture"
|
||||
import { testEffect } from "../lib/effect"
|
||||
|
||||
const noopBootstrap = Layer.succeed(InstanceBootstrap.Service, InstanceBootstrap.Service.of({ run: Effect.void }))
|
||||
let bootstrapRun: Effect.Effect<void> = Effect.void
|
||||
const noopBootstrap = Layer.succeed(
|
||||
InstanceBootstrap.Service,
|
||||
InstanceBootstrap.Service.of({ run: Effect.suspend(() => bootstrapRun) }),
|
||||
)
|
||||
|
||||
const it = testEffect(
|
||||
Layer.mergeAll(InstanceStore.defaultLayer, CrossSpawnSpawner.defaultLayer).pipe(Layer.provide(noopBootstrap)),
|
||||
)
|
||||
|
||||
afterEach(async () => {
|
||||
bootstrapRun = Effect.void
|
||||
await disposeAllInstances()
|
||||
})
|
||||
|
||||
|
|
@ -32,18 +38,16 @@ describe("InstanceStore", () => {
|
|||
}),
|
||||
)
|
||||
|
||||
it.live("runs load init with InstanceRef provided", () =>
|
||||
it.live("runs bootstrap with InstanceRef provided", () =>
|
||||
Effect.gen(function* () {
|
||||
const dir = yield* tmpdirScoped({ git: true })
|
||||
const store = yield* InstanceStore.Service
|
||||
let initializedDirectory: string | undefined
|
||||
|
||||
yield* store.load({
|
||||
directory: dir,
|
||||
init: Effect.gen(function* () {
|
||||
initializedDirectory = (yield* InstanceRef)?.directory
|
||||
}),
|
||||
bootstrapRun = Effect.gen(function* () {
|
||||
initializedDirectory = (yield* InstanceRef)?.directory
|
||||
})
|
||||
yield* store.load({ directory: dir })
|
||||
|
||||
expect(initializedDirectory).toBe(dir)
|
||||
expect(() => Instance.current).toThrow()
|
||||
|
|
@ -56,18 +60,11 @@ describe("InstanceStore", () => {
|
|||
const store = yield* InstanceStore.Service
|
||||
let initialized = 0
|
||||
|
||||
const first = yield* store.load({
|
||||
directory: dir,
|
||||
init: Effect.sync(() => {
|
||||
initialized++
|
||||
}),
|
||||
})
|
||||
const second = yield* store.load({
|
||||
directory: dir,
|
||||
init: Effect.sync(() => {
|
||||
initialized++
|
||||
}),
|
||||
bootstrapRun = Effect.sync(() => {
|
||||
initialized++
|
||||
})
|
||||
const first = yield* store.load({ directory: dir })
|
||||
const second = yield* store.load({ directory: dir })
|
||||
|
||||
expect(second).toBe(first)
|
||||
expect(initialized).toBe(1)
|
||||
|
|
@ -82,27 +79,19 @@ describe("InstanceStore", () => {
|
|||
const release = Promise.withResolvers<void>()
|
||||
let initialized = 0
|
||||
|
||||
const first = yield* store
|
||||
.load({
|
||||
directory: dir,
|
||||
init: Effect.promise(async () => {
|
||||
initialized++
|
||||
started.resolve()
|
||||
await release.promise
|
||||
}),
|
||||
})
|
||||
.pipe(Effect.forkScoped)
|
||||
bootstrapRun = Effect.promise(async () => {
|
||||
initialized++
|
||||
started.resolve()
|
||||
await release.promise
|
||||
})
|
||||
const first = yield* store.load({ directory: dir }).pipe(Effect.forkScoped)
|
||||
|
||||
yield* Effect.promise(() => started.promise)
|
||||
|
||||
const second = yield* store
|
||||
.load({
|
||||
directory: dir,
|
||||
init: Effect.sync(() => {
|
||||
initialized++
|
||||
}),
|
||||
})
|
||||
.pipe(Effect.forkScoped)
|
||||
bootstrapRun = Effect.sync(() => {
|
||||
initialized++
|
||||
})
|
||||
const second = yield* store.load({ directory: dir }).pipe(Effect.forkScoped)
|
||||
|
||||
expect(initialized).toBe(1)
|
||||
release.resolve()
|
||||
|
|
@ -119,27 +108,21 @@ describe("InstanceStore", () => {
|
|||
const store = yield* InstanceStore.Service
|
||||
let attempts = 0
|
||||
|
||||
const failed = yield* store
|
||||
.load({
|
||||
directory: dir,
|
||||
init: Effect.sync(() => {
|
||||
attempts++
|
||||
throw new Error("init failed")
|
||||
}),
|
||||
})
|
||||
.pipe(
|
||||
Effect.as(false),
|
||||
Effect.catchCause(() => Effect.succeed(true)),
|
||||
)
|
||||
bootstrapRun = Effect.sync(() => {
|
||||
attempts++
|
||||
throw new Error("init failed")
|
||||
})
|
||||
const failed = yield* store.load({ directory: dir }).pipe(
|
||||
Effect.as(false),
|
||||
Effect.catchCause(() => Effect.succeed(true)),
|
||||
)
|
||||
|
||||
expect(failed).toBe(true)
|
||||
|
||||
const ctx = yield* store.load({
|
||||
directory: dir,
|
||||
init: Effect.sync(() => {
|
||||
attempts++
|
||||
}),
|
||||
bootstrapRun = Effect.sync(() => {
|
||||
attempts++
|
||||
})
|
||||
const ctx = yield* store.load({ directory: dir })
|
||||
|
||||
expect(ctx.directory).toBe(dir)
|
||||
expect(attempts).toBe(2)
|
||||
|
|
@ -173,15 +156,11 @@ describe("InstanceStore", () => {
|
|||
yield* Effect.addFinalizer(() => Effect.sync(off))
|
||||
|
||||
const first = yield* store.load({ directory: dir })
|
||||
const reload = yield* store
|
||||
.reload({
|
||||
directory: dir,
|
||||
init: Effect.promise(async () => {
|
||||
reloading.resolve()
|
||||
await releaseReload.promise
|
||||
}),
|
||||
})
|
||||
.pipe(Effect.forkScoped)
|
||||
bootstrapRun = Effect.promise(async () => {
|
||||
reloading.resolve()
|
||||
await releaseReload.promise
|
||||
})
|
||||
const reload = yield* store.reload({ directory: dir }).pipe(Effect.forkScoped)
|
||||
|
||||
yield* Effect.promise(() => reloading.promise)
|
||||
const staleDispose = yield* store.dispose(first).pipe(Effect.forkScoped)
|
||||
|
|
@ -242,12 +221,12 @@ describe("InstanceStore", () => {
|
|||
}),
|
||||
)
|
||||
|
||||
it.live("keeps Instance.provide as the legacy ALS wrapper", () =>
|
||||
it.live("provides legacy Promise callers with instance ALS", () =>
|
||||
Effect.gen(function* () {
|
||||
const dir = yield* tmpdirScoped({ git: true })
|
||||
|
||||
const directory = yield* Effect.promise(() =>
|
||||
Instance.provide({
|
||||
WithInstance.provide({
|
||||
directory: dir,
|
||||
fn: () => Instance.directory,
|
||||
}),
|
||||
|
|
@ -258,21 +237,4 @@ describe("InstanceStore", () => {
|
|||
}),
|
||||
)
|
||||
|
||||
it.live("does not install legacy ALS around Effect init", () =>
|
||||
Effect.gen(function* () {
|
||||
const dir = yield* tmpdirScoped()
|
||||
|
||||
const directory = yield* Effect.promise(() =>
|
||||
Instance.provide({
|
||||
directory: dir,
|
||||
init: Effect.sync(() => {
|
||||
expect(() => Instance.current).toThrow()
|
||||
}),
|
||||
fn: () => Instance.directory,
|
||||
}),
|
||||
)
|
||||
|
||||
expect(directory).toBe(dir)
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import { disposeAllInstances, tmpdir } from "../fixture/fixture"
|
|||
import { AppRuntime } from "../../src/effect/app-runtime"
|
||||
import { FileWatcher } from "../../src/file/watcher"
|
||||
import { Instance } from "../../src/project/instance"
|
||||
import { WithInstance } from "../../src/project/with-instance"
|
||||
import { GlobalBus } from "../../src/bus/global"
|
||||
import { Vcs } from "@/project/vcs"
|
||||
|
||||
|
|
@ -18,7 +19,7 @@ const describeVcs = FileWatcher.hasNativeBinding() && !process.env.CI ? describe
|
|||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function withVcs(directory: string, body: () => Promise<void>) {
|
||||
return Instance.provide({
|
||||
return WithInstance.provide({
|
||||
directory,
|
||||
fn: async () => {
|
||||
await AppRuntime.runPromise(
|
||||
|
|
@ -36,7 +37,7 @@ async function withVcs(directory: string, body: () => Promise<void>) {
|
|||
}
|
||||
|
||||
function withVcsOnly(directory: string, body: () => Promise<void>) {
|
||||
return Instance.provide({
|
||||
return WithInstance.provide({
|
||||
directory,
|
||||
fn: async () => {
|
||||
await AppRuntime.runPromise(
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import path from "path"
|
|||
import { Cause, Effect, Exit, Layer } from "effect"
|
||||
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
|
||||
import { Instance } from "../../src/project/instance"
|
||||
import { WithInstance } from "../../src/project/with-instance"
|
||||
import { InstanceRuntime } from "../../src/project/instance-runtime"
|
||||
import { Worktree } from "../../src/worktree"
|
||||
import { disposeAllInstances, provideInstance, provideTmpdirInstance } from "../fixture/fixture"
|
||||
|
|
@ -138,7 +139,7 @@ describe("Worktree", () => {
|
|||
expect(props.branch).toBe(info.branch)
|
||||
|
||||
yield* Effect.promise(() =>
|
||||
Instance.provide({
|
||||
WithInstance.provide({
|
||||
directory: info.directory,
|
||||
fn: () => InstanceRuntime.disposeInstance(Instance.current),
|
||||
}),
|
||||
|
|
@ -163,7 +164,7 @@ describe("Worktree", () => {
|
|||
|
||||
yield* Effect.promise(() => ready)
|
||||
yield* Effect.promise(() =>
|
||||
Instance.provide({
|
||||
WithInstance.provide({
|
||||
directory: info.directory,
|
||||
fn: () => InstanceRuntime.disposeInstance(Instance.current),
|
||||
}),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue