@@ -36,10 +36,12 @@ import {
3636 claimWorkflowToolExecution,
3737 completeAsyncToolCall,
3838 detachAsyncToolCall,
39+ getDetachedWorkflowExecutions,
3940 settleClientWorkflowToolExecution,
4041} from '@/lib/mothership/async-runs/repository'
4142import { getToolConfirmation } from '@/lib/mothership/persistence/tool-confirm'
4243import { waitForWorkflowToolCompletion } from '@/lib/mothership/request/tools/client'
44+ import { raceWorkflowToolClientPickup } from '@/lib/mothership/request/tools/workflow-client-fallback'
4345import {
4446 reportQueuedClientWorkflowTool,
4547 reportSettledClientWorkflowTool,
@@ -88,6 +90,7 @@ describe.runIf(Boolean(redisUrl))('settled client-claimed workflow tools', () =>
8890 const workflowId = generateId()
8991 const chatId = generateId()
9092 const runId = generateId()
93+ const streamId = generateId()
9194 const snapshotIds: string[] = []
9295
9396 beforeAll(async () => {
@@ -128,7 +131,7 @@ describe.runIf(Boolean(redisUrl))('settled client-claimed workflow tools', () =>
128131 chatId,
129132 userId,
130133 workspaceId,
131- streamId: generateId() ,
134+ streamId,
132135 toolExecutionVersion: SIM_TOOL_EXECUTION_VERSION,
133136 status: 'paused_waiting_for_tool',
134137 requestContext: { source: 'headless_lifecycle' },
@@ -490,4 +493,77 @@ describe.runIf(Boolean(redisUrl))('settled client-claimed workflow tools', () =>
490493 })
491494 expect(await publishedConfirmation(toolCallId)).toBeNull()
492495 })
496+
497+ it("settles a browser run that outlasts the worker's wait as running, bound to the call", async () => {
498+ const { toolCallId, executionId } = await claimed()
499+ const runOnServer = vi.fn()
500+
501+ const outcome = await raceWorkflowToolClientPickup({
502+ toolCallId,
503+ workflowId,
504+ timeoutMs: WAIT_MS,
505+ graceMs: WAIT_MS,
506+ waitTimeoutMs: 500,
507+ runOnServer,
508+ })
509+
510+ // The worker gets the run's ids and waits on it in slices; the browser's run goes on.
511+ expect(outcome).toMatchObject({
512+ winner: 'client',
513+ completion: {
514+ status: 'background',
515+ data: { workflowId, executionId, status: 'running' },
516+ },
517+ })
518+ expect(runOnServer).not.toHaveBeenCalled()
519+ expect(await publishedConfirmation(toolCallId)).toMatchObject({
520+ status: 'background',
521+ executionId,
522+ })
523+ // Still bound to the call and unsettled, so a Stop's open-client-run read still cancels it.
524+ expect(await toolRow(toolCallId)).toMatchObject({
525+ status: 'delivered',
526+ claimedBy: `workflow:${executionId}`,
527+ clientWorkflowExecutionId: executionId,
528+ executionSettledAt: null,
529+ })
530+ })
531+
532+ it("names the runs this run's calls started and left running, for a Stop to cancel", async () => {
533+ const insertCall = async (executableName: string, args: unknown, result: unknown) =>
534+ db.insert(copilotAsyncToolCalls).values({
535+ runId,
536+ toolCallId: generateId(),
537+ toolName: executableName,
538+ executableName,
539+ args: args as Record<string, unknown>,
540+ result: result as Record<string, unknown>,
541+ status: 'completed',
542+ })
543+ const detachedCli = generateId()
544+ const detachedTool = generateId()
545+ const record = (runIdValue: string, status: string) =>
546+ JSON.stringify({ runId: runIdValue, workflowId, status })
547+ await insertCall(
548+ 'sim_cli',
549+ { request: { invocation: { kind: 'cli', argv: ['workflows', 'run', workflowId] } } },
550+ { exitCode: 4, stdout: record(detachedCli, 'running'), stderr: '' }
551+ )
552+ // A wait on a run proves nothing about who started it.
553+ await insertCall(
554+ 'sim_cli',
555+ { request: { invocation: { kind: 'cli', argv: ['workflows', 'runs', 'wait', 'other'] } } },
556+ { exitCode: 4, stdout: record(generateId(), 'running'), stderr: '' }
557+ )
558+ await insertCall(
559+ 'run_workflow',
560+ { workflowId },
561+ { status: 'running', executionId: detachedTool, workflowId }
562+ )
563+ await insertCall('run_block', { workflowId }, { success: true, executionId: generateId() })
564+
565+ expect((await getDetachedWorkflowExecutions(streamId, userId)).sort()).toEqual(
566+ [detachedCli, detachedTool].sort()
567+ )
568+ })
493569})
0 commit comments