Skip to content

Commit b5dd83f

Browse files
committed
fix(mothership): never hold a finished workflow call on the grace extension check
The presence re-check at the end of the short grace now races the browser's result, so a run that finishes or a Stop during a slow read returns at once, and the time the check took comes out of the extension.
1 parent 2fbf76e commit b5dd83f

2 files changed

Lines changed: 52 additions & 9 deletions

File tree

‎apps/sim/lib/mothership/request/tools/workflow-client-fallback.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,40 @@ describe('raceWorkflowToolClientPickup', () => {
7878
expect(params.runOnServer).not.toHaveBeenCalled()
7979
})
8080

81+
it('returns a browser result that lands while the grace extension is still being checked', async () => {
82+
let finish!: (completion: unknown) => void
83+
waitForWorkflowToolCompletion.mockImplementation(
84+
() => new Promise((resolve) => (finish = resolve))
85+
)
86+
const params = baseParams({ graceMs: 5_000, extendGrace: () => new Promise(() => {}) })
87+
88+
const promise = raceWorkflowToolClientPickup(params as never)
89+
await vi.advanceTimersByTimeAsync(5_000)
90+
finish({ status: 'success', data: { ok: true } })
91+
const outcome = await promise
92+
93+
expect(outcome.winner).toBe('client')
94+
expect(outcome.completion).toEqual({ status: 'success', data: { ok: true } })
95+
expect(claimWorkflowToolExecution).not.toHaveBeenCalled()
96+
})
97+
98+
it('counts a slow extension check toward the extension', async () => {
99+
pendingUntilAborted()
100+
claimWorkflowToolExecution.mockResolvedValue({ toolCallId: 'tool-1' })
101+
const params = baseParams({
102+
graceMs: 5_000,
103+
extendGrace: () => new Promise((resolve) => setTimeout(() => resolve(25_000), 4_000)),
104+
})
105+
106+
const promise = raceWorkflowToolClientPickup(params as never)
107+
await vi.advanceTimersByTimeAsync(29_000)
108+
expect(claimWorkflowToolExecution).toHaveBeenCalledTimes(0)
109+
await vi.advanceTimersByTimeAsync(1_000)
110+
await promise
111+
112+
expect(claimWorkflowToolExecution).toHaveBeenCalledTimes(1)
113+
})
114+
81115
it('runs the tool server-side when the grace elapses and the claim is won', async () => {
82116
pendingUntilAborted()
83117
claimWorkflowToolExecution.mockResolvedValue({ toolCallId: 'tool-1' })

‎apps/sim/lib/mothership/request/tools/workflow-client-fallback.ts‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,28 @@ export async function raceWorkflowToolClientPickup(
196196
// would outlive the wait it is supposed to bound.
197197
const effectiveGraceMs = Math.min(graceMs, timeoutMs)
198198

199+
const clientDone = clientWait.then((completion) => ({ kind: 'client' as const, completion }))
199200
const raceGrace = (ms: number) =>
200-
Promise.race([
201-
clientWait.then((completion) => ({ kind: 'client' as const, completion })),
202-
sleep(ms).then(() => ({ kind: 'grace' as const })),
203-
])
201+
Promise.race([clientDone, sleep(ms).then(() => ({ kind: 'grace' as const }))])
204202

205203
let first = await raceGrace(effectiveGraceMs)
206204
if (first.kind === 'grace' && params.extendGrace) {
207-
const extraMs = Math.min(
208-
await params.extendGrace().catch(() => 0),
209-
timeoutMs - effectiveGraceMs
210-
)
211-
if (extraMs > 0) first = await raceGrace(extraMs)
205+
// The check counts toward the extension, and a result that lands during it wins at once.
206+
const checkStartedAt = Date.now()
207+
const extension = await Promise.race([
208+
clientDone,
209+
params
210+
.extendGrace()
211+
.catch(() => 0)
212+
.then((ms) => ({ kind: 'extend' as const, ms })),
213+
])
214+
if (extension.kind === 'client') {
215+
first = extension
216+
} else {
217+
const extraMs =
218+
Math.min(extension.ms, timeoutMs - effectiveGraceMs) - (Date.now() - checkStartedAt)
219+
if (extraMs > 0) first = await raceGrace(extraMs)
220+
}
212221
}
213222

214223
// A non-null client result inside the grace window is the normal path.

0 commit comments

Comments
 (0)