Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

fix(cli): resume persisted workflows without duplicate executors

Rudy Celekli461ab0185939
diff --git a/v3/@claude-flow/cli/__tests__/workflow-resume-dispatch-r50.test.ts b/v3/@claude-flow/cli/__tests__/workflow-resume-dispatch-r50.test.ts
new file mode 100644
index 0000000000..971750485d
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/workflow-resume-dispatch-r50.test.ts
@@ -0,0 +1,59 @@
+import { beforeEach, afterEach, expect, it, vi } from 'vitest';
+import { mkdtempSync, readFileSync, writeFileSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+const state = vi.hoisted(() => ({ cwd: '', execute: vi.fn() }));
+vi.mock('../src/mcp-tools/types.js', () => ({ getProjectCwd: () => state.cwd }));
+vi.mock('../src/mcp-tools/agent-execute-core.js', () => ({ executeAgentTask: state.execute }));
+import { workflowTools } from '../src/mcp-tools/workflow-tools.js';
+const call = (name: string, input: object) => workflowTools.find(t => t.name === name)!.handler(input) as Promise<any>;
+const path = () => join(state.cwd,'.claude-flow/workflows/store.json');
+const store = () => JSON.parse(readFileSync(path(),'utf8'));
+const create = () => call('workflow_create',{name:'resume',steps:[{type:'task',config:{agentId:'a',prompt:'first'}},{type:'task',config:{agentId:'a',prompt:'Continue {{step-1.output}}'}}]});
+async function pausedSnapshot() {
+ const {workflowId}=await create();const data=store();const workflow=data.workflows[workflowId];
+ workflow.status='paused';workflow.currentStep=1;workflow.steps[0].status='completed';workflow.variables['step-1.output']='saved result';
+ writeFileSync(path(),JSON.stringify(data));return workflowId;
+}
+beforeEach(() => { state.cwd=mkdtempSync(join(tmpdir(),'ruflo-resume-'));state.execute.mockReset().mockResolvedValue({success:true,output:'finished'}); });
+afterEach(() => rmSync(state.cwd,{recursive:true,force:true}));
+it('dispatches only remaining steps from a persisted pause and preserves output bindings', async () => {
+ const workflowId=await pausedSnapshot();const result=await call('workflow_resume',{workflowId});
+ expect(result).toMatchObject({resumed:true,status:'completed'});
+ expect(state.execute).toHaveBeenCalledTimes(1);
+ expect(state.execute.mock.calls[0][0].prompt).toBe('Continue saved result');
+ expect(store().workflows[workflowId]).toMatchObject({status:'completed',currentStep:2});
+});
+it('reports a resumed step failure instead of leaving the workflow running', async () => {
+ state.execute.mockResolvedValue({success:false,error:'provider unavailable'});
+ const workflowId=await pausedSnapshot();
+ expect(await call('workflow_resume',{workflowId})).toMatchObject({status:'failed',error:'provider unavailable'});
+ expect(store().workflows[workflowId].status).toBe('failed');
+});
+it('admits one continuation when resume is requested twice', async () => {
+ const workflowId=await pausedSnapshot();
+ const results=await Promise.all([call('workflow_resume',{workflowId}),call('workflow_resume',{workflowId})]);
+ expect(results.filter(r=>r.status==='completed')).toHaveLength(1);
+ expect(state.execute).toHaveBeenCalledTimes(1);
+});
+it('joins an in-flight execution when pause is immediately followed by resume', async () => {
+ let finish!:()=>void;state.execute.mockImplementationOnce(()=>new Promise(resolve=>finish=()=>resolve({success:true,output:'first output'})));
+ const {workflowId}=await create();const executing=call('workflow_execute',{workflowId});
+ await call('workflow_pause',{workflowId});const resuming=call('workflow_resume',{workflowId});
+ expect(state.execute).toHaveBeenCalledTimes(1);finish();await executing;
+ expect(await resuming).toMatchObject({resumed:true,status:'completed'});
+ expect(state.execute.mock.calls.map(([input])=>input.prompt)).toEqual(['first','Continue first output']);
+});
+it('rejects a second executor even while the active execution is paused', async () => {
+ let finish!:()=>void;state.execute.mockImplementationOnce(()=>new Promise(resolve=>finish=()=>resolve({success:true,output:'first output'})));
+ const {workflowId}=await create();const executing=call('workflow_execute',{workflowId});await call('workflow_pause',{workflowId});
+ const duplicate=await call('workflow_execute',{workflowId});
+ expect(duplicate.error).toMatch(/already.*execut/i);
+ const resuming=call('workflow_resume',{workflowId});finish();await executing;await resuming;
+ expect(state.execute).toHaveBeenCalledTimes(2);
+});
+it('retains missing and non-paused guards without dispatching', async () => {
+ expect(await call('workflow_resume',{workflowId:'missing'})).toMatchObject({error:'Workflow not found'});
+ const {workflowId}=await create();expect(await call('workflow_resume',{workflowId})).toMatchObject({error:'Workflow not paused'});
+ expect(state.execute).not.toHaveBeenCalled();
+});
diff --git a/v3/@claude-flow/cli/src/mcp-tools/workflow-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/workflow-tools.ts
index 56232f651f..f8ebc8e82d 100644
--- a/v3/@claude-flow/cli/src/mcp-tools/workflow-tools.ts
+++ b/v3/@claude-flow/cli/src/mcp-tools/workflow-tools.ts
@@ -79,6 +79,24 @@ function saveWorkflowStore(store: WorkflowStore): void {
writeFileSync(getWorkflowPath(), JSON.stringify(store, null, 2), 'utf-8');
}
+// A paused in-flight step still owns its executor. Resume joins that execution
+// instead of dispatching the same side effect again (within this process).
+const workflowExecutions = new Map<string, Promise<unknown>>();
+const workflowExecutionKey = (workflowId: unknown): string =>
+ JSON.stringify([getWorkflowPath(), workflowId]);
+
+function withWorkflowExecution<T>(workflowId: unknown, execute: () => Promise<T>) {
+ const key = workflowExecutionKey(workflowId);
+ if (workflowExecutions.has(key)) {
+ return Promise.resolve({ workflowId, error: 'Workflow already executing' });
+ }
+ const execution = execute();
+ workflowExecutions.set(key, execution);
+ return execution.finally(() => {
+ if (workflowExecutions.get(key) === execution) workflowExecutions.delete(key);
+ });
+}
+
export const workflowTools: MCPTool[] = [
{
name: 'workflow_run',
@@ -273,7 +291,7 @@ export const workflowTools: MCPTool[] = [
},
required: ['workflowId'],
},
- handler: async (input) => {
+ handler: async (input) => withWorkflowExecution(input.workflowId, async () => {
// Validate user-provided input (#1425)
const vId = validateIdentifier(input.workflowId, 'workflowId');
if (!vId.valid) return { success: false, error: vId.error };
@@ -444,7 +462,7 @@ export const workflowTools: MCPTool[] = [
completedAt: workflow.completedAt,
durationMs: Date.now() - startedAt,
};
- },
+ }),
},
{
name: 'workflow_status',
@@ -621,27 +639,16 @@ export const workflowTools: MCPTool[] = [
return { workflowId, error: 'Workflow not paused' };
}
- workflow.status = 'running';
- saveWorkflowStore(store);
-
- // Report current step states — do not auto-complete them
- const stepStates = workflow.steps.map(step => ({
- stepId: step.stepId,
- name: step.name,
- status: step.status,
- }));
-
- const remainingSteps = workflow.steps.length - workflow.currentStep;
+ const active = workflowExecutions.get(workflowExecutionKey(workflowId));
+ if (active) {
+ workflow.status = 'running';
+ saveWorkflowStore(store);
+ return { ...((await active) as Record<string, unknown>), resumed: true };
+ }
- return {
- workflowId,
- status: workflow.status,
- resumed: true,
- currentStep: workflow.currentStep,
- remainingSteps,
- steps: stepStates,
- _note: 'Workflow resumed. Steps remain in their current state and must be executed via task tools.',
- };
+ const execute = workflowTools.find(tool => tool.name === 'workflow_execute')!;
+ const result = await execute.handler({ workflowId, startFromStep: workflow.currentStep });
+ return { ...(result as Record<string, unknown>), resumed: true };
},
},
{