🌊 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
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
fix(workflows): preserve control signals and sibling state after awaits
Rudy Celekli09be6122b7c9
diff --git a/v3/@claude-flow/cli/__tests__/workflow-progress-ownership-r50.test.ts b/v3/@claude-flow/cli/__tests__/workflow-progress-ownership-r50.test.tsnew file mode 100644index 0000000000..b95a853383--- /dev/null+++ b/v3/@claude-flow/cli/__tests__/workflow-progress-ownership-r50.test.ts@@ -0,0 +1,65 @@+import { beforeEach, afterEach, expect, it, vi } from 'vitest';+import { mkdtempSync, readFileSync, 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 store = () => JSON.parse(readFileSync(join(state.cwd,'.claude-flow/workflows/store.json'),'utf8'));+const create = () => call('workflow_create',{name:'running',steps:[{type:'task',config:{agentId:'a',prompt:'one'}},{type:'task',config:{agentId:'a',prompt:'two'}}]});+let finish: () => void;+beforeEach(() => { state.cwd=mkdtempSync(join(tmpdir(),'ruflo-progress-'));state.execute.mockReset();state.execute.mockImplementationOnce(()=>new Promise(resolve=>finish=()=>resolve({success:true,output:'one'}))).mockResolvedValue({success:true,output:'two'}); });+afterEach(() => rmSync(state.cwd,{recursive:true,force:true}));+it('honors a pause received during an awaited step and saves its continuation index', async () => {+ const {workflowId}=await create();const execution=call('workflow_execute',{workflowId});+ await call('workflow_pause',{workflowId});finish();+ expect(await execution).toMatchObject({status:'paused'});+ expect(state.execute).toHaveBeenCalledTimes(1);+ expect(store().workflows[workflowId]).toMatchObject({status:'paused',currentStep:1});+});+it.each(['workflow_cancel','workflow_stop'])('preserves %s during an awaited step and never dispatches its successor', async action => {+ const {workflowId}=await create();const execution=call('workflow_execute',{workflowId});+ await call(action,{workflowId,reason:'operator cancelled'});finish();+ expect(await execution).toMatchObject({status:'failed'});+ expect(state.execute).toHaveBeenCalledTimes(1);+ expect(store().workflows[workflowId]).toMatchObject({status:'failed',error:action==='workflow_cancel'?'operator cancelled':'Stopped by user'});+ expect(store().workflows[workflowId].steps[1].status).toBe('skipped');+});+it('retains sibling workflows and templates created while a step is pending', async () => {+ const {workflowId}=await create();const execution=call('workflow_execute',{workflowId});+ const sibling=await call('workflow_create',{name:'sibling'});+ const template=await call('workflow_template',{action:'save',workflowId:sibling.workflowId});+ finish();await execution;+ expect(store().workflows[sibling.workflowId].name).toBe('sibling');+ expect(store().templates[template.templateId]).toBeDefined();+});+it('does not recreate a paused workflow deleted while its current step finishes', async () => {+ const {workflowId}=await create();const execution=call('workflow_execute',{workflowId});+ await call('workflow_pause',{workflowId});await call('workflow_delete',{workflowId});finish();await execution;+ expect(store().workflows[workflowId]).toBeUndefined();+ expect(state.execute).toHaveBeenCalledTimes(1);+});+it('pause then resume before completion does not replay or overlap the in-flight step', async () => {+ const {workflowId}=await create();const execution=call('workflow_execute',{workflowId});+ await call('workflow_pause',{workflowId});const resumed=call('workflow_resume',{workflowId});+ expect(state.execute).toHaveBeenCalledTimes(1);finish();await execution;await resumed;+ expect(state.execute.mock.calls.map(([input])=>input.prompt)).toEqual(['one','two']);+ expect(store().workflows[workflowId].status).toBe('completed');+});++it('still permits an explicit retry of a previously failed workflow', async () => {+ state.execute.mockReset().mockResolvedValueOnce({success:false,error:'provider failure'}).mockResolvedValue({success:true,output:'retried'});+ const {workflowId}=await create();expect(await call('workflow_execute',{workflowId})).toMatchObject({status:'failed'});+ let finishRetry!: () => void;+ state.execute.mockImplementationOnce(() => new Promise(resolve => finishRetry = () => resolve({success:true,output:'retried'})));+ const retry = call('workflow_execute',{workflowId});+ expect(store().workflows[workflowId].error).toBeUndefined();+ expect(store().workflows[workflowId].completedAt).toBeUndefined();+ finishRetry();+ const result = await retry;+ expect(result).toMatchObject({status:'completed'});+ expect(result.error).toBeUndefined();+ expect(store().workflows[workflowId].error).toBeUndefined();+});diff --git a/v3/@claude-flow/cli/src/mcp-tools/workflow-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/workflow-tools.tsindex 56232f651f..39e757a531 100644--- a/v3/@claude-flow/cli/src/mcp-tools/workflow-tools.ts+++ b/v3/@claude-flow/cli/src/mcp-tools/workflow-tools.ts@@ -289,6 +289,25 @@ export const workflowTools: MCPTool[] = [return { workflowId, error: 'Workflow already running' };}+ const persistProgress = (): void => {+ // An awaited step may overlap pause/stop/delete or a sibling workflow+ // mutation. Merge only this workflow into the latest store snapshot.+ const latest = loadWorkflowStore();+ const current = latest.workflows[workflowId];+ if (!current) {+ workflow.status = 'failed';+ workflow.error = 'Workflow was deleted during execution';+ return;+ }+ if (current.status === 'failed') {+ Object.assign(workflow, current);+ return;+ }+ if (current.status === 'paused' && workflow.status !== 'failed') workflow.status = 'paused';+ latest.workflows[workflowId] = workflow;+ saveWorkflowStore(latest);+ };+// Inject runtime variablesif (input.variables) {workflow.variables = { ...workflow.variables, ...(input.variables as Record<string, unknown>) };@@ -296,6 +315,8 @@ export const workflowTools: MCPTool[] = [workflow.status = 'running';workflow.startedAt = new Date().toISOString();+ delete workflow.error;+ delete workflow.completedAt;workflow.currentStep = (input.startFromStep as number) || 0;saveWorkflowStore(store);@@ -327,15 +348,19 @@ export const workflowTools: MCPTool[] = [while (i < workflow.steps.length) {// Honor pause/cancel signals between steps.const live = loadWorkflowStore().workflows[workflowId];- if (!live || live.status === 'paused') {+ if (!live) {+ workflow.status = 'failed';+ workflow.error = 'Workflow was deleted during execution';+ break;+ }+ if (live.status === 'paused') {workflow.status = 'paused';workflow.currentStep = i;- saveWorkflowStore(store);+ persistProgress();break;}if (live.status === 'failed') {- workflow.status = 'failed';- saveWorkflowStore(store);+ Object.assign(workflow, live);break;}@@ -343,7 +368,7 @@ export const workflowTools: MCPTool[] = [step.status = 'running';step.startedAt = new Date().toISOString();const stepStart = Date.now();- saveWorkflowStore(store);+ persistProgress();let stepEntry: typeof stepResults[number] = { stepId: step.stepId, type: step.type, status: 'running' };@@ -407,7 +432,7 @@ export const workflowTools: MCPTool[] = [workflow.status = 'failed';workflow.error = msg;workflow.completedAt = new Date().toISOString();- saveWorkflowStore(store);+ persistProgress();return {workflowId,status: 'failed',@@ -422,19 +447,20 @@ export const workflowTools: MCPTool[] = [if (typeof stepEntry.durationMs !== 'number') stepEntry.durationMs = Date.now() - stepStart;stepResults.push(stepEntry);workflow.currentStep = i + 1;- saveWorkflowStore(store);+ persistProgress();i++;}if (workflow.status === 'running') {workflow.status = 'completed';workflow.completedAt = new Date().toISOString();- saveWorkflowStore(store);+ persistProgress();}return {workflowId,status: workflow.status,+ ...(workflow.error ? { error: workflow.error } : {}),totalSteps: workflow.steps.length,stepsCompleted: stepResults.filter(s => s.status === 'completed').length,stepsSkipped: stepResults.filter(s => s.status === 'skipped').length,