Skip to content

Commit 4e78ca3

Browse files
Xin HuangPremik
authored andcommitted
fix(runner): queue work when already running instead of discarding
When a background subagent completes and calls ops.prompt() to notify the parent session, the parent's Runner is often still Running (processing the previous turn). The old ensureRunning discarded the new work item and just awaited the current run's completion, so the notification was lost and the parent session would hang until the user typed 'continue'. Add a RunningThenRun state to the Runner state machine. When ensureRunning is called while Running, the new work is stored as pending and the runner transitions to RunningThenRun. When the current run finishes, finishRun starts the pending work and transitions back to Running. Cancel in the RunningThenRun state interrupts both the current and pending work. This fixes the root cause of the parent-not-notified bug. The alternative approach in PR anomalyco#35041 moves the notification to the session completion path but still races through ensureRunning and can lose the notification when the parent is busy. Refs: anomalyco#35066
1 parent 9f6a728 commit 4e78ca3

2 files changed

Lines changed: 137 additions & 16 deletions

File tree

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

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export type State<A, E> =
3535
| { readonly _tag: "Running"; readonly run: RunHandle<A, E> }
3636
| { readonly _tag: "Shell"; readonly shell: ShellHandle<A, E> }
3737
| { readonly _tag: "ShellThenRun"; readonly shell: ShellHandle<A, E>; readonly run: PendingHandle<A, E> }
38+
| { readonly _tag: "RunningThenRun"; readonly run: RunHandle<A, E>; readonly pending: PendingHandle<A, E> }
3839

3940
export const make = <A, E = never>(
4041
scope: Scope.Scope,
@@ -67,20 +68,35 @@ export const make = <A, E = never>(
6768
const idleIfCurrent = () =>
6869
SynchronizedRef.modify(ref, (st) => [st._tag === "Idle" ? idle : Effect.void, st] as const).pipe(Effect.flatten)
6970

70-
const finishRun = (id: number, done: Deferred.Deferred<A, E | Cancelled>, exit: Exit.Exit<A, E>) =>
71-
SynchronizedRef.modify(
71+
const finishRun = (
72+
id: number,
73+
done: Deferred.Deferred<A, E | Cancelled>,
74+
exit: Exit.Exit<A, E>,
75+
): Effect.Effect<void> =>
76+
SynchronizedRef.modifyEffect(
7277
ref,
73-
(st) =>
74-
[
75-
Effect.gen(function* () {
76-
if (st._tag === "Running" && st.run.id === id) yield* idle
77-
yield* complete(done, exit)
78-
}),
79-
st._tag === "Running" && st.run.id === id ? ({ _tag: "Idle" } as const) : st,
80-
] as const,
78+
Effect.fnUntraced(function* (st) {
79+
if (st._tag === "Running" && st.run.id === id) {
80+
return [
81+
Effect.gen(function* () {
82+
yield* idle
83+
yield* complete(done, exit)
84+
}),
85+
{ _tag: "Idle" } as const,
86+
] as const
87+
}
88+
if (st._tag === "RunningThenRun" && st.run.id === id) {
89+
const run = yield* startRun(st.pending.work, st.pending.done)
90+
return [complete(done, exit), { _tag: "Running", run } as const] as const
91+
}
92+
return [complete(done, exit), st] as const
93+
}),
8194
).pipe(Effect.flatten)
8295

83-
const startRun = (work: Effect.Effect<A, E>, done: Deferred.Deferred<A, E | Cancelled>) =>
96+
const startRun = (
97+
work: Effect.Effect<A, E>,
98+
done: Deferred.Deferred<A, E | Cancelled>,
99+
): Effect.Effect<RunHandle<A, E>> =>
84100
Effect.gen(function* () {
85101
const id = next()
86102
const fiber = yield* work.pipe(
@@ -117,9 +133,18 @@ export const make = <A, E = never>(
117133
ref,
118134
Effect.fnUntraced(function* (st) {
119135
switch (st._tag) {
120-
case "Running":
121136
case "ShellThenRun":
122137
return [awaitDone(st.run.done), st] as const
138+
case "RunningThenRun":
139+
return [awaitDone(st.pending.done), st] as const
140+
case "Running": {
141+
const pending = {
142+
id: next(),
143+
done: yield* Deferred.make<A, E | Cancelled>(),
144+
work,
145+
} satisfies PendingHandle<A, E>
146+
return [awaitDone(pending.done), { _tag: "RunningThenRun", run: st.run, pending }] as const
147+
}
123148
case "Shell": {
124149
const run = {
125150
id: next(),
@@ -198,6 +223,16 @@ export const make = <A, E = never>(
198223
}),
199224
{ _tag: "Idle" } as const,
200225
] as const
226+
case "RunningThenRun":
227+
return [
228+
Effect.gen(function* () {
229+
yield* Fiber.interrupt(st.run.fiber)
230+
yield* Deferred.fail(st.run.done, new Cancelled()).pipe(Effect.asVoid)
231+
yield* Deferred.fail(st.pending.done, new Cancelled()).pipe(Effect.asVoid)
232+
yield* idleIfCurrent()
233+
}),
234+
{ _tag: "Idle" } as const,
235+
] as const
201236
}
202237
}).pipe(Effect.flatten)
203238

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

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe("Runner", () => {
5252

5353
expect(a).toBe("shared")
5454
expect(b).toBe("shared")
55-
expect(yield* Ref.get(calls)).toBe(1)
55+
expect(yield* Ref.get(calls)).toBe(2) // queued run also increments
5656
}),
5757
)
5858

@@ -87,7 +87,7 @@ describe("Runner", () => {
8787
)
8888

8989
it.live(
90-
"second ensureRunning ignores new work if already running",
90+
"second ensureRunning queues behind running and executes after",
9191
Effect.gen(function* () {
9292
const s = yield* Scope.Scope
9393
const runner = Runner.make<string>(s)
@@ -108,13 +108,99 @@ describe("Runner", () => {
108108
})
109109

110110
expect(a).toBe("first-result")
111-
expect(b).toBe("first-result")
112-
expect(yield* Ref.get(ran)).toEqual(["first"])
111+
expect(b).toBe("second-result")
112+
expect(yield* Ref.get(ran)).toEqual(["first", "second"])
113113
}),
114114
)
115115

116116
// --- cancel semantics ---
117117

118+
it.live(
119+
"ensureRunning queues behind running and transitions through RunningThenRun",
120+
Effect.gen(function* () {
121+
const s = yield* Scope.Scope
122+
const runner = Runner.make<string>(s)
123+
const gate = yield* Deferred.make<void>()
124+
125+
const first = Deferred.await(gate).pipe(Effect.as("first"))
126+
const second = Effect.succeed("second")
127+
128+
const a = yield* runner.ensureRunning(first).pipe(Effect.forkChild)
129+
yield* waitForState(runner, "Running")
130+
131+
const b = yield* runner.ensureRunning(second).pipe(Effect.forkChild)
132+
yield* waitForState(runner, "RunningThenRun")
133+
expect(runner.state._tag).toBe("RunningThenRun")
134+
135+
yield* Deferred.succeed(gate, undefined)
136+
yield* Fiber.await(a)
137+
138+
const exitB = yield* Fiber.await(b)
139+
expect(Exit.isSuccess(exitB)).toBe(true)
140+
if (Exit.isSuccess(exitB)) expect(exitB.value).toBe("second")
141+
expect(runner.state._tag).toBe("Idle")
142+
}),
143+
)
144+
145+
it.live(
146+
"third ensureRunning shares pending in RunningThenRun",
147+
Effect.gen(function* () {
148+
const s = yield* Scope.Scope
149+
const runner = Runner.make<string>(s)
150+
const gate = yield* Deferred.make<void>()
151+
const calls = yield* Ref.make(0)
152+
153+
const first = Deferred.await(gate).pipe(Effect.as("first"))
154+
const work = Effect.gen(function* () {
155+
yield* Ref.update(calls, (n) => n + 1)
156+
return "queued"
157+
})
158+
159+
const a = yield* runner.ensureRunning(first).pipe(Effect.forkChild)
160+
yield* waitForState(runner, "Running")
161+
162+
const b = yield* runner.ensureRunning(work).pipe(Effect.forkChild)
163+
yield* waitForState(runner, "RunningThenRun")
164+
165+
const c = yield* runner.ensureRunning(work).pipe(Effect.forkChild)
166+
yield* Effect.yieldNow
167+
expect(runner.state._tag).toBe("RunningThenRun")
168+
169+
yield* Deferred.succeed(gate, undefined)
170+
yield* Fiber.await(a)
171+
172+
const [exitB, exitC] = yield* Effect.all([Fiber.await(b), Fiber.await(c)])
173+
expect(Exit.isSuccess(exitB)).toBe(true)
174+
expect(Exit.isSuccess(exitC)).toBe(true)
175+
if (Exit.isSuccess(exitB)) expect(exitB.value).toBe("queued")
176+
if (Exit.isSuccess(exitC)) expect(exitC.value).toBe("queued")
177+
}),
178+
)
179+
180+
it.live(
181+
"cancel in RunningThenRun cancels both current and pending",
182+
Effect.gen(function* () {
183+
const s = yield* Scope.Scope
184+
const runner = Runner.make<string>(s, { onInterrupt: Effect.succeed("fallback") })
185+
const gate = yield* Deferred.make<void>()
186+
187+
const a = yield* runner.ensureRunning(Deferred.await(gate).pipe(Effect.as("first"))).pipe(Effect.forkChild)
188+
yield* waitForState(runner, "Running")
189+
190+
const b = yield* runner.ensureRunning(Effect.succeed("second")).pipe(Effect.forkChild)
191+
yield* waitForState(runner, "RunningThenRun")
192+
193+
yield* runner.cancel
194+
expect(runner.busy).toBe(false)
195+
196+
const [exitA, exitB] = yield* Effect.all([Fiber.await(a), Fiber.await(b)])
197+
expect(Exit.isSuccess(exitA)).toBe(true)
198+
expect(Exit.isSuccess(exitB)).toBe(true)
199+
if (Exit.isSuccess(exitA)) expect(exitA.value).toBe("fallback")
200+
if (Exit.isSuccess(exitB)) expect(exitB.value).toBe("fallback")
201+
}),
202+
)
203+
118204
it.live(
119205
"cancel interrupts running work",
120206
Effect.gen(function* () {

0 commit comments

Comments
 (0)