Skip to content

Commit 1391879

Browse files
committed
test(opencode): add regression test for lost terminal-iteration wakeup
Cover the queued-work path repaired in anomalyco#36375 (refs anomalyco#35066). When a background subagent notifies the parent by calling ensureRunning while the parent turn is still running, the runner must queue that work and cycle Running -> RunningThenRun -> Running for every arrival instead of dropping it. Exercise two back-to-back notifications so the runner re-enters RunningThenRun a second time after the first queued run is promoted. Without the fix the runner never enters RunningThenRun, so the queued wakeup is lost: the test fails (waitForState times out) on the pre-fix runner.ts and passes with the fix in place.
1 parent 3439564 commit 1391879

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

‎packages/opencode/test/effect/runner.test.ts‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,83 @@ describe("Runner", () => {
177177
}),
178178
)
179179

180+
it.live(
181+
// Regression guard for the lost-wakeup bug fixed in #36375 (refs #35066): a
182+
// completing background subagent calls ensureRunning to notify the parent while
183+
// it is still Running. Each such wakeup must survive, so the runner has to cycle
184+
// Running -> RunningThenRun -> Running for every new arrival instead of dropping
185+
// the queued work. Pre-fix the very first queued run never reached RunningThenRun.
186+
"re-queues each run that arrives after the previous queued run is promoted",
187+
Effect.gen(function* () {
188+
const s = yield* Scope.Scope
189+
const runner = Runner.make<string>(s)
190+
const ran = yield* Ref.make<string[]>([])
191+
const parentTurn = yield* Deferred.make<void>()
192+
const firstQueued = yield* Deferred.make<void>()
193+
// Set when the first notification finishes and captured when the second one
194+
// starts, so the test proves the second is promoted only after the first has
195+
// fully completed rather than merely after it began running.
196+
const firstNotificationDone = yield* Ref.make(false)
197+
const secondSawFirstDone = yield* Ref.make(false)
198+
199+
// Parent turn is in progress and stays Running until released.
200+
const parent = yield* runner
201+
.ensureRunning(
202+
Effect.gen(function* () {
203+
yield* Ref.update(ran, (a) => [...a, "parent"])
204+
yield* Deferred.await(parentTurn)
205+
return "parent"
206+
}),
207+
)
208+
.pipe(Effect.forkChild)
209+
yield* waitForState(runner, "Running")
210+
211+
// First notification arrives while the parent is still busy.
212+
const wakeA = yield* runner
213+
.ensureRunning(
214+
Effect.gen(function* () {
215+
yield* Ref.update(ran, (a) => [...a, "notify-a"])
216+
yield* Deferred.await(firstQueued)
217+
yield* Ref.set(firstNotificationDone, true)
218+
return "notify-a"
219+
}),
220+
)
221+
.pipe(Effect.forkChild)
222+
yield* waitForState(runner, "RunningThenRun")
223+
224+
// Parent finishes; the queued notification is promoted to the running turn.
225+
yield* Deferred.succeed(parentTurn, undefined)
226+
yield* waitForState(runner, "Running")
227+
228+
// Second notification arrives while the promoted one is now running, forcing
229+
// the runner back into RunningThenRun a second time.
230+
const wakeB = yield* runner
231+
.ensureRunning(
232+
Effect.gen(function* () {
233+
const firstDone = yield* Ref.get(firstNotificationDone)
234+
yield* Ref.set(secondSawFirstDone, firstDone)
235+
yield* Ref.update(ran, (a) => [...a, "notify-b"])
236+
return "notify-b"
237+
}),
238+
)
239+
.pipe(Effect.forkChild)
240+
yield* waitForState(runner, "RunningThenRun")
241+
242+
// Release the first notification; the second must run after it completes, not be lost.
243+
yield* Deferred.succeed(firstQueued, undefined)
244+
245+
const [valParent, valA, valB] = yield* Effect.all([Fiber.join(parent), Fiber.join(wakeA), Fiber.join(wakeB)])
246+
expect(valParent).toBe("parent")
247+
expect(valA).toBe("notify-a")
248+
expect(valB).toBe("notify-b")
249+
expect(yield* Ref.get(ran)).toEqual(["parent", "notify-a", "notify-b"])
250+
// The second notification observed the first as completed, proving the runner
251+
// promoted it only after the first queued run finished.
252+
expect(yield* Ref.get(secondSawFirstDone)).toBe(true)
253+
yield* waitForState(runner, "Idle")
254+
}),
255+
)
256+
180257
it.live(
181258
"cancel in RunningThenRun cancels both current and pending",
182259
Effect.gen(function* () {

0 commit comments

Comments
 (0)