Skip to content

Commit 3b4d836

Browse files
committed
fix(opencode): include instances that load during the idle check
InstanceStore.list snapshotted the cache before awaiting each entry's boot, so an instance that loaded while an earlier one was still booting was missing from the idle check but present in the disposal that followed. Re-snapshot after the await and repeat until the set is stable. Add a gated-bootstrap regression test for the case.
1 parent ddfcf14 commit 3b4d836

2 files changed

Lines changed: 63 additions & 6 deletions

File tree

‎packages/opencode/src/project/instance-store.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,17 @@ const layer: Layer.Layer<Service, never, Project.Service | InstanceBootstrap.Ser
124124
).pipe(Effect.withSpan("InstanceStore.load"))
125125
}
126126

127+
// An entry that appears while an earlier one is still booting must be
128+
// returned too, so snapshot again after the await and repeat until stable.
127129
const list = Effect.fn("InstanceStore.list")(function* () {
128-
const exits = yield* Effect.forEach([...cache.values()], (entry) =>
129-
Deferred.await(entry.deferred).pipe(Effect.exit),
130-
)
131-
return exits.filter(Exit.isSuccess).map((exit) => exit.value)
130+
while (true) {
131+
const entries = [...cache.values()]
132+
const exits = yield* Effect.forEach(entries, (entry) => Deferred.await(entry.deferred).pipe(Effect.exit))
133+
const current = [...cache.values()]
134+
if (current.length === entries.length && current.every((entry, index) => entry === entries[index])) {
135+
return exits.filter(Exit.isSuccess).map((exit) => exit.value)
136+
}
137+
}
132138
})
133139

134140
const reload = (input: LoadInput): Effect.Effect<InstanceContext> => {

‎packages/opencode/test/server/global-lifecycle.test.ts‎

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
44
import { FSUtil } from "@opencode-ai/core/fs-util"
55
import { Npm } from "@opencode-ai/core/npm"
66
import { httpClient } from "@opencode-ai/core/effect/app-node-platform"
7-
import { Effect, Fiber, Layer } from "effect"
7+
import { Deferred, Effect, Fiber, Layer } from "effect"
88
import { HttpClient } from "effect/unstable/http"
99
import { GlobalBus, type GlobalEvent } from "@/bus/global"
1010
import { Account } from "@/account/account"
@@ -23,7 +23,22 @@ import { NpmTest } from "../fake/npm"
2323
import { tmpdirScoped } from "../fixture/fixture"
2424
import { awaitWithTimeout, testEffect } from "../lib/effect"
2525

26-
const noopBootstrap = Layer.succeed(InstanceBootstrap.Service, InstanceBootstrap.Service.of({ run: Effect.void }))
26+
let bootstrapRun: Effect.Effect<void> = Effect.void
27+
const noopBootstrap = Layer.succeed(
28+
InstanceBootstrap.Service,
29+
InstanceBootstrap.Service.of({ run: Effect.suspend(() => bootstrapRun) }),
30+
)
31+
32+
const setBootstrap = (run: Effect.Effect<void>) =>
33+
Effect.acquireRelease(
34+
Effect.sync(() => {
35+
bootstrapRun = run
36+
}),
37+
() =>
38+
Effect.sync(() => {
39+
bootstrapRun = Effect.void
40+
}),
41+
)
2742
const unexpectedHttp = HttpClient.make((request) =>
2843
Effect.die(`unexpected http request: ${request.method} ${request.url}`),
2944
)
@@ -99,4 +114,40 @@ describe("reloadWhenSessionsIdle", () => {
99114
expect(yield* store.load({ directory: dir })).not.toBe(ctx)
100115
}),
101116
)
117+
118+
it.live("waits for a session on an instance that loaded while another was still booting", () =>
119+
Effect.gen(function* () {
120+
const slow = yield* tmpdirScoped({ git: true })
121+
const fast = yield* tmpdirScoped({ git: true })
122+
const store = yield* InstanceStore.Service
123+
const status = yield* SessionStatus.Service
124+
const started = yield* Deferred.make<void>()
125+
const release = yield* Deferred.make<void>()
126+
yield* setBootstrap(
127+
Effect.gen(function* () {
128+
if ((yield* InstanceRef)?.directory !== slow) return
129+
yield* Deferred.succeed(started, undefined)
130+
yield* Deferred.await(release)
131+
}),
132+
)
133+
const disposed = yield* collectGlobalDisposed()
134+
135+
yield* store.load({ directory: slow }).pipe(Effect.forkScoped({ startImmediately: true }))
136+
yield* Deferred.await(started)
137+
const reload = yield* reloadWhenSessionsIdle().pipe(Effect.forkScoped({ startImmediately: true }))
138+
const ctx = yield* store.load({ directory: fast })
139+
yield* status.set(sessionID, { type: "busy" }).pipe(Effect.provideService(InstanceRef, ctx))
140+
yield* Deferred.succeed(release, undefined)
141+
142+
yield* Effect.sleep("600 millis")
143+
expect(disposed).toHaveLength(0)
144+
expect(yield* store.load({ directory: fast })).toBe(ctx)
145+
146+
yield* status.set(sessionID, { type: "idle" }).pipe(Effect.provideService(InstanceRef, ctx))
147+
yield* awaitWithTimeout(Fiber.join(reload), "reload did not run after the session went idle")
148+
149+
expect(disposed).toHaveLength(1)
150+
expect(yield* store.load({ directory: fast })).not.toBe(ctx)
151+
}),
152+
)
102153
})

0 commit comments

Comments
 (0)