Skip to content

Commit

Permalink
Merge pull request #24 from pufflyai/23-block-node-execution-if-paren…
Browse files Browse the repository at this point in the history
…t-wasnt-previously-run

Block node execution if parent wasn't previously run
  • Loading branch information
au-re authored Aug 27, 2023
2 parents 3d350dc + c8e94d8 commit 428ae85
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ exports[`correctly map output from one node onto another 1`] = `
}
`;

exports[`do not run an executable node unless the parent was run already 1`] = `
{
"n2": {
"input": {
"data": "",
},
},
}
`;

exports[`ignore inputs from nested unreachable nodes 1`] = `
{
"n1": {
Expand Down
24 changes: 24 additions & 0 deletions packages/@pufflig/ps-chains/src/engines/dataflow/runFlow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,27 @@ test("run nodes connected through execution nodes", async () => {
expect(onNodeRunError).toHaveBeenCalledTimes(0);
expect(res).toMatchSnapshot();
});

/**
* (1) => (2) => (3)
* ^
*/
test("do not run an executable node unless the parent was run already", async () => {
const onNodeInputUpdate = jest.fn();
const onNodeRunComplete = jest.fn();
const onNodeRunError = jest.fn();
const res = await runFlow(
simpleExec,
"n2",
{},
{
onNodeRunComplete,
onNodeInputUpdate,
onNodeRunError,
}
);
expect(onNodeInputUpdate).toHaveBeenCalledTimes(1);
expect(onNodeRunComplete).toHaveBeenCalledTimes(0);
expect(onNodeRunError).toHaveBeenCalledTimes(0);
expect(res).toMatchSnapshot();
});
12 changes: 12 additions & 0 deletions packages/@pufflig/ps-chains/src/engines/dataflow/runFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ export async function runFlow(flow: Flow, nodeId: string, input: Record<string,
}
// ---

// if the node is executable, only run it if the parent is complete
const parentNodeId = Object.values(flow.definition.edges).find(
(e) => e.target === nodeId && e.targetHandle.startsWith(executionPrefix)
)?.source;
const isParentComplete = parentNodeId ? runs[parentNodeId] > 0 : true;
const isExecutableNode = !!node.execution?.inputs.length;

if (!isParentComplete && isExecutableNode) {
logger.debug("Parent node is not complete, skip execution");
return;
}

// resolve variables to use during the run
const newInput = newState[nodeId]?.input;
let resolvedInput: Record<string, ParamValue> = {};
Expand Down

0 comments on commit 428ae85

Please sign in to comment.