GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — test: migrate prompt tests to HTTP mock LLM server by kitlangton · Pull Request #20304 · anomalyco/opencode

test: migrate prompt tests to HTTP mock LLM server #20304

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.

Sign up for GitHub

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
kitlangton merged 14 commits into dev from refactor/llm-server-effect
Mar 31, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

test: migrate prompt tests to HTTP mock LLM server #20304

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
459fbc9
refactor(test): migrate llm-server to Effect HTTP platform
kitlangton Mar 31, 2026
6ea467b
test: add live effect helper mode
kitlangton Mar 31, 2026
123123b
test: start moving prompt tests to mock llm server
kitlangton Mar 31, 2026
21ec320
test: extend mock llm server coverage
kitlangton Mar 31, 2026
6bd3404
test: infer mock server callback types
kitlangton Mar 31, 2026
f2fa1a6
test: move more prompt cases to mock llm server
kitlangton Mar 31, 2026
bb03949
refactor: propagate Instance context through Effect fibers via Instan…
kitlangton Mar 31, 2026
cc412f3
refactor: migrate Instance ALS reads to InstanceRef in Effect services
kitlangton Mar 31, 2026
191a747
fix: propagate InstanceRef across static function boundaries
kitlangton Mar 31, 2026
825f51c
fix: restore instance context in deferred database callbacks
kitlangton Mar 31, 2026
e24369e
fix: break installation cycle in database context binding
kitlangton Mar 31, 2026
4ff0fbc
fix: retry scoped tempdir cleanup on windows
kitlangton Mar 31, 2026
90469bb
refactor: simplify instance context helpers in prompt tests
kitlangton Mar 31, 2026
82da702
refactor: tighten instance context helper fallbacks
kitlangton Mar 31, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: propagate Instance context through Effect fibers via Instan…
…ceRef

Add a ServiceMap.Reference that carries InstanceContext through the
Effect service graph so child fibers retain instance context even when
resumed by external I/O events outside the ALS boundary.

- Add InstanceRef to instance-state.ts; InstanceState.get/has/invalidate
  try the Reference first, fall back to ALS
- makeRuntime automatically captures ALS into InstanceRef at the boundary
- provideInstance (test fixture) sets InstanceRef for Effect.runPromiseWith
- Remove all redundant provideInstance(dir) wrappers from prompt tests
- Fix test/lib/effect.ts type params (drop unnecessary S/T generics)
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@kitlangton
kitlangton committed Mar 31, 2026
commit bb039496d52acba530bf02eea347da71c4c93a80

Some comments aren't visible on the classic Files Changed page.

There are no files selected for viewing

24 changes: 19 additions & 5 deletions 24 packages/opencode/src/effect/instance-state.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import { Effect, ScopedCache, Scope } from "effect"
import { Effect, ScopedCache, Scope, ServiceMap } from "effect"
import { Instance, type InstanceContext } from "@/project/instance"
import { registerDisposer } from "./instance-registry"

const TypeId = "~opencode/InstanceState"

export const InstanceRef = ServiceMap.Reference<InstanceContext | undefined>("~opencode/InstanceRef", {
defaultValue: () => undefined,
})

const context = Effect.gen(function* () {
const ref = yield* InstanceRef
return ref ?? Instance.current
})

const directory = Effect.gen(function* () {
const ref = yield* InstanceRef
return ref ? ref.directory : Instance.directory
})

export interface InstanceState<A, E = never, R = never> {
readonly [TypeId]: typeof TypeId
readonly cache: ScopedCache.ScopedCache<string, A, E, R>
Expand All @@ -16,7 +30,7 @@ export namespace InstanceState {
Effect.gen(function* () {
const cache = yield* ScopedCache.make<string, A, E, R>({
capacity: Number.POSITIVE_INFINITY,
lookup: () => init(Instance.current),
lookup: () => Effect.gen(function* () { return yield* init(yield* context) }),
})

const off = registerDisposer((directory) => Effect.runPromise(ScopedCache.invalidate(cache, directory)))
Expand All @@ -29,7 +43,7 @@ export namespace InstanceState {
})

export const get = <A, E, R>(self: InstanceState<A, E, R>) =>
Effect.suspend(() => ScopedCache.get(self.cache, Instance.directory))
Effect.gen(function* () { return yield* ScopedCache.get(self.cache, yield* directory) })

export const use = <A, E, R, B>(self: InstanceState<A, E, R>, select: (value: A) => B) =>
Effect.map(get(self), select)
Expand All @@ -40,8 +54,8 @@ export namespace InstanceState {
) => Effect.flatMap(get(self), select)

export const has = <A, E, R>(self: InstanceState<A, E, R>) =>
Effect.suspend(() => ScopedCache.has(self.cache, Instance.directory))
Effect.gen(function* () { return yield* ScopedCache.has(self.cache, yield* directory) })

export const invalidate = <A, E, R>(self: InstanceState<A, E, R>) =>
Effect.suspend(() => ScopedCache.invalidate(self.cache, Instance.directory))
Effect.gen(function* () { return yield* ScopedCache.invalidate(self.cache, yield* directory) })
}
22 changes: 17 additions & 5 deletions 22 packages/opencode/src/effect/run-service.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
import { Effect, Layer, ManagedRuntime } from "effect"
import * as ServiceMap from "effect/ServiceMap"
import { Instance } from "@/project/instance"
import { InstanceRef } from "./instance-state"

export const memoMap = Layer.makeMemoMapUnsafe()

function provide<A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> {
try {
const ctx = Instance.current
return Effect.provideService(effect, InstanceRef, ctx)
} catch {
return effect
}
}

export function makeRuntime<I, S, E>(service: ServiceMap.Service<I, S>, layer: Layer.Layer<I, E>) {
let rt: ManagedRuntime.ManagedRuntime<I, E> | undefined
const getRuntime = () => (rt ??= ManagedRuntime.make(layer, { memoMap }))

return {
runSync: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>) => getRuntime().runSync(service.use(fn)),
runSync: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>) => getRuntime().runSync(provide(service.use(fn))),
runPromiseExit: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>, options?: Effect.RunOptions) =>
getRuntime().runPromiseExit(service.use(fn), options),
getRuntime().runPromiseExit(provide(service.use(fn)), options),
runPromise: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>, options?: Effect.RunOptions) =>
getRuntime().runPromise(service.use(fn), options),
runFork: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>) => getRuntime().runFork(service.use(fn)),
runCallback: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>) => getRuntime().runCallback(service.use(fn)),
getRuntime().runPromise(provide(service.use(fn)), options),
runFork: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>) => getRuntime().runFork(provide(service.use(fn))),
runCallback: <A, Err>(fn: (svc: S) => Effect.Effect<A, Err, I>) =>
getRuntime().runCallback(provide(service.use(fn))),
}
}
101 changes: 99 additions & 2 deletions 101 packages/opencode/test/effect/instance-state.test.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterEach, expect, test } from "bun:test"
import { Duration, Effect, Layer, ManagedRuntime, ServiceMap } from "effect"
import { InstanceState } from "../../src/effect/instance-state"
import { Cause, Deferred, Duration, Effect, Exit, Fiber, Layer, ManagedRuntime, ServiceMap } from "effect"
import { InstanceRef, InstanceState } from "../../src/effect/instance-state"
import { Instance } from "../../src/project/instance"
import { tmpdir } from "../fixture/fixture"

Expand Down Expand Up @@ -382,3 +382,100 @@ test("InstanceState dedupes concurrent lookups", async () => {
),
)
})

test("InstanceState survives deferred resume from the same instance context", async () => {
await using tmp = await tmpdir({ git: true })

interface Api {
readonly get: (gate: Deferred.Deferred<void>) => Effect.Effect<string>
}

class Test extends ServiceMap.Service<Test, Api>()("@test/DeferredResume") {
static readonly layer = Layer.effect(
Test,
Effect.gen(function* () {
const state = yield* InstanceState.make((ctx) => Effect.sync(() => ctx.directory))

return Test.of({
get: Effect.fn("Test.get")(function* (gate: Deferred.Deferred<void>) {
yield* Deferred.await(gate)
return yield* InstanceState.get(state)
}),
})
}),
)
}

const rt = ManagedRuntime.make(Test.layer)

try {
const gate = await Effect.runPromise(Deferred.make<void>())
const fiber = await Instance.provide({
directory: tmp.path,
fn: () => Promise.resolve(rt.runFork(Test.use((svc) => svc.get(gate)))),
})

await Instance.provide({
directory: tmp.path,
fn: () => Effect.runPromise(Deferred.succeed(gate, void 0)),
})
const exit = await Effect.runPromise(Fiber.await(fiber))

expect(Exit.isSuccess(exit)).toBe(true)
if (Exit.isSuccess(exit)) {
expect(exit.value).toBe(tmp.path)
}
} finally {
await rt.dispose()
}
})

test("InstanceState survives deferred resume outside ALS when InstanceRef is set", async () => {
await using tmp = await tmpdir({ git: true })

interface Api {
readonly get: (gate: Deferred.Deferred<void>) => Effect.Effect<string>
}

class Test extends ServiceMap.Service<Test, Api>()("@test/DeferredResumeOutside") {
static readonly layer = Layer.effect(
Test,
Effect.gen(function* () {
const state = yield* InstanceState.make((ctx) => Effect.sync(() => ctx.directory))

return Test.of({
get: Effect.fn("Test.get")(function* (gate: Deferred.Deferred<void>) {
yield* Deferred.await(gate)
return yield* InstanceState.get(state)
}),
})
}),
)
}

const rt = ManagedRuntime.make(Test.layer)

try {
const gate = await Effect.runPromise(Deferred.make<void>())
// Provide InstanceRef so the fiber carries the context even when
// the deferred is resolved from outside Instance.provide ALS.
const fiber = await Instance.provide({
directory: tmp.path,
fn: () =>
Promise.resolve(
rt.runFork(Test.use((svc) => svc.get(gate)).pipe(Effect.provideService(InstanceRef, Instance.current))),
),
})

// Resume from outside any Instance.provide — ALS is NOT set here
await Effect.runPromise(Deferred.succeed(gate, void 0))
const exit = await Effect.runPromise(Fiber.await(fiber))

expect(Exit.isSuccess(exit)).toBe(true)
if (Exit.isSuccess(exit)) {
expect(exit.value).toBe(tmp.path)
}
} finally {
await rt.dispose()
}
})
4 changes: 3 additions & 1 deletion 4 packages/opencode/test/fixture/fixture.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type * as PlatformError from "effect/PlatformError"
import type * as Scope from "effect/Scope"
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
import type { Config } from "../../src/config/config"
import { InstanceRef } from "../../src/effect/instance-state"
import { Instance } from "../../src/project/instance"
import { TestLLMServer } from "../lib/llm-server"

Expand Down Expand Up @@ -114,7 +115,8 @@ export const provideInstance =
Effect.promise<A>(async () =>
Instance.provide({
directory,
fn: () => Effect.runPromiseWith(services)(self),
fn: () =>
Effect.runPromiseWith(services)(self.pipe(Effect.provideService(InstanceRef, Instance.current))),
}),
),
)
Expand Down
6 changes: 3 additions & 3 deletions 6 packages/opencode/test/lib/effect.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type Body<A, E, R> = Effect.Effect<A, E, R> | (() => Effect.Effect<A, E, R>)

const body = <A, E, R>(value: Body<A, E, R>) => Effect.suspend(() => (typeof value === "function" ? value() : value))

const run = <A, E, R, E2, S>(value: Body<A, E, R | Scope.Scope>, layer: Layer.Layer<R, E2, S>) =>
const run = <A, E, R, E2>(value: Body<A, E, R | Scope.Scope>, layer: Layer.Layer<R, E2>) =>
Effect.gen(function* () {
const exit = yield* body(value).pipe(Effect.scoped, Effect.provide(layer), Effect.exit)
if (Exit.isFailure(exit)) {
Expand All @@ -19,7 +19,7 @@ const run = <A, E, R, E2, S>(value: Body<A, E, R | Scope.Scope>, layer: Layer.La
return yield* exit
}).pipe(Effect.runPromise)

const make = <R, E, S, T>(testLayer: Layer.Layer<R, E, S>, liveLayer: Layer.Layer<R, E, T>) => {
const make = <R, E>(testLayer: Layer.Layer<R, E>, liveLayer: Layer.Layer<R, E>) => {
const effect = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
test(name, () => run(value, testLayer), opts)

Expand Down Expand Up @@ -49,5 +49,5 @@ const liveEnv = TestConsole.layer

export const it = make(testEnv, liveEnv)

export const testEffect = <R, E, S>(layer: Layer.Layer<R, E, S>) =>
export const testEffect = <R, E>(layer: Layer.Layer<R, E>) =>
make(Layer.provideMerge(layer, testEnv), Layer.provideMerge(layer, liveEnv))
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.