Skip to content

Automate browser based workflows with AI

Stop counting a sequential chain's few-second hand-off as a stuck run (SKY-17780) (#8830)

Aaron Perezecec47747bda
diff --git a/skyvern-frontend/src/routes/workflows/editor/nestedConditionalSave.test.ts b/skyvern-frontend/src/routes/workflows/editor/nestedConditionalSave.test.ts
index 688a563c1..60cb9f3e4 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nestedConditionalSave.test.ts
+++ b/skyvern-frontend/src/routes/workflows/editor/nestedConditionalSave.test.ts
@@ -16,10 +16,16 @@ import {
isWorkflowBlockNode,
type WorkflowBlockNode,
} from "./nodes";
+import {
+ createBranchCondition,
+ isConditionalNode,
+} from "./nodes/ConditionalNode/types";
import { rewireBlockDropInScope } from "./sortable/rewire";
+import { duplicateBlockBelow } from "./workflowDuplicate";
import { TOP_LEVEL_SCOPE } from "./sortable/scope";
import {
getElements,
+ getUpdatedNodesAfterLabelUpdateForParameterKeys,
getWorkflowBlocks,
getWorkflowSettings,
validateWorkflowBlocks,
@@ -594,3 +600,313 @@ describe("nested conditional save round-trip", () => {
expect(routingOf(secondSave)).toEqual(routingOf(firstSave));
});
});
+
+// Branches that converge on a shared block while the conditional has no merge
+// label of its own (a legal API/MCP shape).
+describe("conditional without a merge label", () => {
+ function savedRoutingSorted(blocks: Array<WorkflowBlock>) {
+ const { nodes, edges } = getElements(blocks, DEFAULT_SETTINGS, true);
+ return routingOf(getWorkflowBlocks(nodes, edges)).sort((a, b) =>
+ a.label.localeCompare(b.label),
+ );
+ }
+
+ function inputRoutingSorted(blocks: Array<WorkflowBlock>) {
+ return routingOf(blocks as never).sort((a, b) =>
+ a.label.localeCompare(b.label),
+ );
+ }
+
+ function branchOf(nodes: Array<AppNode>, label: string) {
+ const node = getNodeByLabel(nodes, label);
+ return node.parentId ? node.data.conditionalBranchId : null;
+ }
+
+ const skipFirst = (): Array<WorkflowBlock> => [
+ code("start_step", "decide"),
+ conditional("decide", null, [
+ { id: "b1", next: "join" },
+ { id: "b2", next: "branch_step", isDefault: true },
+ ]),
+ code("branch_step", "join"),
+ code("join", null),
+ ];
+
+ const skipLast = (): Array<WorkflowBlock> => [
+ code("start_step", "decide"),
+ conditional("decide", null, [
+ { id: "b1", next: "branch_step" },
+ { id: "b2", next: "join", isDefault: true },
+ ]),
+ code("branch_step", "join"),
+ code("join", null),
+ ];
+
+ test("a skip branch listed first keeps its target through load -> save", () => {
+ expect(savedRoutingSorted(skipFirst())).toEqual(
+ inputRoutingSorted(skipFirst()),
+ );
+ });
+
+ test("the shared block renders after the conditional, not inside a branch", () => {
+ for (const blocks of [skipFirst(), skipLast()]) {
+ const { nodes } = getElements(blocks, DEFAULT_SETTINGS, true);
+ expect(branchOf(nodes, "branch_step")).not.toBeNull();
+ expect(branchOf(nodes, "join")).toBeNull();
+ }
+ });
+
+ test("a skip branch listed last round-trips losslessly", () => {
+ expect(savedRoutingSorted(skipLast())).toEqual(
+ inputRoutingSorted(skipLast()),
+ );
+ });
+
+ test("converging branches inside a loop keep their targets", () => {
+ const blocks = [forLoop("loop", skipFirst())];
+ const { nodes, edges } = getElements(blocks, DEFAULT_SETTINGS, true);
+ const saved = getWorkflowBlocks(nodes, edges);
+ const loop = saved.find((block) => block.label === "loop");
+
+ expect(branchOf(nodes, "join")).toBeNull();
+ expect(
+ loop?.block_type === "for_loop"
+ ? inputRoutingSorted(loop.loop_blocks as Array<WorkflowBlock>)
+ : null,
+ ).toEqual(inputRoutingSorted(skipFirst()));
+ });
+
+ test("a single branch child with an empty default branch stays in its branch", () => {
+ const blocks: Array<WorkflowBlock> = [
+ code("start_step", "decide"),
+ conditional("decide", null, [
+ { id: "b1", next: "branch_step" },
+ { id: "b2", next: null, isDefault: true },
+ ]),
+ code("branch_step", null),
+ ];
+ const { nodes } = getElements(blocks, DEFAULT_SETTINGS, true);
+
+ expect(branchOf(nodes, "branch_step")).toBe("b1");
+ expect(savedRoutingSorted(blocks)).toEqual(inputRoutingSorted(blocks));
+ });
+
+ test("a branch targeting the conditional's own next block stays an empty branch", () => {
+ const blocks: Array<WorkflowBlock> = [
+ code("start_step", "decide"),
+ conditional("decide", "after", [
+ { id: "b1", next: "after" },
+ { id: "b2", next: null, isDefault: true },
+ ]),
+ code("after", null),
+ ];
+ const { nodes } = getElements(blocks, DEFAULT_SETTINGS, true);
+
+ expect(branchOf(nodes, "after")).toBeNull();
+ });
+
+ test("a nested conditional sharing the outer join round-trips in either order", () => {
+ const outer = conditional("outer", null, [
+ { id: "o1", next: "inner" },
+ { id: "o2", next: "alternate", isDefault: true },
+ ]);
+ const inner = conditional("inner", null, [
+ { id: "i1", next: "inner_step" },
+ { id: "i2", next: "outer_join", isDefault: true },
+ ]);
+ const tail = [
+ code("inner_step", "outer_join"),
+ code("alternate", "outer_join"),
+ code("outer_join", null),
+ ];
+ for (const blocks of [
+ [code("start_step", "outer"), outer, inner, ...tail],
+ [code("start_step", "outer"), inner, outer, ...tail],
+ ]) {
+ const { nodes } = getElements(blocks, DEFAULT_SETTINGS, true);
+
+ expect(branchOf(nodes, "outer_join")).toBeNull();
+ expect(savedRoutingSorted(blocks)).toEqual(inputRoutingSorted(blocks));
+ }
+ });
+
+ test("nested conditionals ending an outer branch keep their merges", () => {
+ const explicitInnerMerge = (): Array<WorkflowBlock> => [
+ code("start_step", "outer"),
+ conditional("outer", null, [
+ { id: "o1", next: "inner" },
+ { id: "o2", next: "p", isDefault: true },
+ ]),
+ conditional("inner", "join", [
+ { id: "n1", next: "q" },
+ { id: "n2", next: "r", isDefault: true },
+ ]),
+ code("q", "join"),
+ code("r", "join"),
+ code("p", "join"),
+ code("join", null),
+ ];
+ const outer = conditional("outer", null, [
+ { id: "o1", next: "inner" },
+ { id: "o2", next: "join", isDefault: true },
+ ]);
+ const inner = conditional("inner", null, [
+ { id: "i1", next: "a" },
+ { id: "i2", next: "b", isDefault: true },
+ ]);
+ const tail = [code("a", "join"), code("b", "join"), code("join", null)];
+
+ for (const blocks of [
+ explicitInnerMerge(),
+ [code("start_step", "outer"), outer, inner, ...tail],
+ [code("start_step", "outer"), inner, outer, ...tail],
+ ]) {
+ expect(savedRoutingSorted(blocks)).toEqual(inputRoutingSorted(blocks));
+ }
+ });
+
+ test("an empty Else the editor adds to a conditional without a default saves as null", () => {
+ const blocks: Array<WorkflowBlock> = [
+ code("start_step", "decide"),
+ conditional("decide", null, [
+ { id: "b1", next: "join" },
+ { id: "b2", next: "branch_step" },
+ ]),
+ code("branch_step", "join"),
+ code("join", null),
+ ];
+ const { nodes, edges } = getElements(blocks, DEFAULT_SETTINGS, true);
+ const decide = nodes.find(isConditionalNode)!;
+ decide.data.branches = [
+ ...decide.data.branches,
+ createBranchCondition({ id: "else", is_default: true }),
+ ];
+ const saved = getWorkflowBlocks(nodes, edges).find(
+ (block) => block.label === "decide",
+ );
+
+ expect(
+ saved?.block_type === "conditional"
+ ? saved.branch_conditions.find((branch) => branch.id === "else")
+ ?.next_block_label
+ : undefined,
+ ).toBeNull();
+ });
+
+ test("a branch that owns no blocks keeps its target when only some branches converge", () => {
+ const blocks: Array<WorkflowBlock> = [
+ code("start_step", "decide"),
+ conditional("decide", null, [
+ { id: "b1", next: "join" },
+ { id: "b2", next: "branch_step" },
+ { id: "b3", next: "other", isDefault: true },
+ ]),
+ code("branch_step", "join"),
+ code("join", null),
+ code("other", null),
+ ];
+
+ expect(savedRoutingSorted(blocks)).toEqual(inputRoutingSorted(blocks));
+ });
+
+ test("two branches sharing a first block keep it when another branch starts at the join", () => {
+ const blocks: Array<WorkflowBlock> = [
+ code("start_step", "decide"),
+ conditional("decide", null, [
+ { id: "b1", next: "shared" },
+ { id: "b2", next: "shared" },
+ { id: "b3", next: "join", isDefault: true },
+ ]),
+ code("shared", "join"),
+ code("join", null),
+ ];
+
+ expect(savedRoutingSorted(blocks)).toEqual(inputRoutingSorted(blocks));
+
+ const { nodes, edges } = getElements(blocks, DEFAULT_SETTINGS, true);
+ const renamed = getUpdatedNodesAfterLabelUpdateForParameterKeys(
+ getNodeByLabel(nodes, "shared").id,
+ "renamed",
+ nodes,
+ ) as Array<AppNode>;
+ const decide = getWorkflowBlocks(renamed, edges).find(
+ (block) => block.label === "decide",
+ );
+
+ expect(
+ decide?.block_type === "conditional"
+ ? decide.branch_conditions.map((branch) => branch.next_block_label)
+ : null,
+ ).toEqual(["renamed", "renamed", "join"]);
+ });
+
+ test("a duplicated conditional's shared branch target points at its own copy", () => {
+ const blocks: Array<WorkflowBlock> = [
+ code("start_step", "decide"),
+ conditional("decide", null, [
+ { id: "b1", next: "shared" },
+ { id: "b2", next: "shared" },
+ { id: "b3", next: "join", isDefault: true },
+ ]),
+ code("shared", "join"),
+ code("join", null),
+ ];
+ const { nodes, edges } = getElements(blocks, DEFAULT_SETTINGS, true);
+ let nextId = 0;
+ let nextLabel = 0;
+ const duplicated = duplicateBlockBelow({
+ nodes,
+ edges,
+ nodeId: getNodeByLabel(nodes, "decide").id,
+ generateId: () => `dup-${nextId++}`,
+ generateLabel: () => `copy_${nextLabel++}`,
+ })!;
+ const saved = getWorkflowBlocks(duplicated.nodes, duplicated.edges);
+ const copy = saved.find(
+ (block) => block.label === duplicated.duplicatedLabel,
+ );
+ const copyTargets =
+ copy?.block_type === "conditional"
+ ? copy.branch_conditions.map((branch) => branch.next_block_label)
+ : [];
+
+ expect(copyTargets[0]).toBe(copyTargets[1]);
+ expect(copyTargets).not.toContain("shared");
+ });
+
+ test("renaming a nested conditional's merge target keeps the merge", () => {
+ const blocks: Array<WorkflowBlock> = [
+ code("start_step", "outer"),
+ conditional("outer", null, [
+ { id: "o1", next: "inner" },
+ { id: "o2", next: "p", isDefault: true },
+ ]),
+ conditional("inner", "join", [
+ { id: "n1", next: "q" },
+ { id: "n2", next: "r", isDefault: true },
+ ]),
+ code("q", "join"),
+ code("r", "join"),
+ code("p", "join"),
+ code("join", null),
+ ];
+ const { nodes, edges } = getElements(blocks, DEFAULT_SETTINGS, true);
+ const renamed = getUpdatedNodesAfterLabelUpdateForParameterKeys(
+ getNodeByLabel(nodes, "join").id,
+ "renamed",
+ nodes,
+ ) as Array<AppNode>;
+ const routing = Object.fromEntries(
+ getWorkflowBlocks(renamed, edges).map((block) => [
+ block.label,
+ block.next_block_label,
+ ]),
+ );
+
+ expect(routing).toMatchObject({
+ inner: "renamed",
+ q: "renamed",
+ r: "renamed",
+ });
+ });
+});
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/ConditionalNode/types.ts b/skyvern-frontend/src/routes/workflows/editor/nodes/ConditionalNode/types.ts
index 2fe00ef59..2ae76ceac 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/ConditionalNode/types.ts
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/ConditionalNode/types.ts
@@ -12,6 +12,7 @@ export type ConditionalNodeData = NodeBaseData & {
branches: Array<BranchCondition>;
activeBranchId: string | null;
mergeLabel: string | null;
+ inferredMergeLabel?: string | null;
_headerHeight?: number; // internal: measured header card height for layout
};
diff --git a/skyvern-frontend/src/routes/workflows/editor/workflowDuplicate.ts b/skyvern-frontend/src/routes/workflows/editor/workflowDuplicate.ts
index a96a6a862..c86cd03d1 100644
--- a/skyvern-frontend/src/routes/workflows/editor/workflowDuplicate.ts
+++ b/skyvern-frontend/src/routes/workflows/editor/workflowDuplicate.ts
@@ -28,12 +28,14 @@ type DuplicateBlockBelowResult = {
type BranchConditionLike = {
id: string;
+ next_block_label?: string | null;
};
type ConditionalDataLike = NodeBaseData & {
activeBranchId: string | null;
branches: Array<BranchConditionLike>;
mergeLabel?: string | null;
+ inferredMergeLabel?: string | null;
};
const SKIP_KEYS_FOR_REFERENCE_REWRITE = new Set([
@@ -370,6 +372,9 @@ export function duplicateBlockBelow({
clonedData.branches = clonedData.branches.map((branch) => ({
...branch,
id: branchIdMap.get(branchKey(node.id, branch.id)) ?? branch.id,
+ next_block_label: branch.next_block_label
+ ? (labelMap.get(branch.next_block_label) ?? branch.next_block_label)
+ : branch.next_block_label,
}));
clonedData.activeBranchId = oldActiveBranchId
? (branchIdMap.get(branchKey(node.id, oldActiveBranchId)) ??
@@ -378,6 +383,14 @@ export function duplicateBlockBelow({
if (clonedData.mergeLabel && labelMap.has(clonedData.mergeLabel)) {
clonedData.mergeLabel = labelMap.get(clonedData.mergeLabel)!;
}
+ if (
+ clonedData.inferredMergeLabel &&
+ labelMap.has(clonedData.inferredMergeLabel)
+ ) {
+ clonedData.inferredMergeLabel = labelMap.get(
+ clonedData.inferredMergeLabel,
+ )!;
+ }
}
cloned.data = clonedData;
diff --git a/skyvern-frontend/src/routes/workflows/editor/workflowEditorUtils.ts b/skyvern-frontend/src/routes/workflows/editor/workflowEditorUtils.ts
index a26c67bca..3a12b01ec 100644
--- a/skyvern-frontend/src/routes/workflows/editor/workflowEditorUtils.ts
+++ b/skyvern-frontend/src/routes/workflows/editor/workflowEditorUtils.ts
@@ -36,6 +36,7 @@ import {
BranchCriteriaTypes,
debuggableWorkflowBlockTypes,
type AWSSecretParameter,
+ type BranchCondition,
type BranchCriteriaType,
type OutputParameter,
type Parameter,
@@ -1510,7 +1511,21 @@ function serializeConditionalBlock(
nodes,
edges,
);
- const nextBlockLabel = orderedNodes[0]?.data.label ?? mergeLabel ?? null;
+ // A branch that owns no blocks keeps the target it was loaded with when
+ // another branch of this same conditional owns that block (for example a
+ // shared first block), so the target can never point outside this subtree.
+ const loadedTarget =
+ branch.next_block_label &&
+ nodes.some(
+ (n) =>
+ isWorkflowBlockNode(n) &&
+ n.data.label === branch.next_block_label &&
+ n.data.conditionalNodeId === node.id,
+ )
+ ? branch.next_block_label
+ : null;
+ const nextBlockLabel =
+ orderedNodes[0]?.data.label ?? loadedTarget ?? mergeLabel ?? null;
return {
...branch,
@@ -1527,7 +1542,10 @@ function serializeConditionalBlock(
block_type: "conditional",
label: node.data.label,
continue_on_failure: node.data.continueOnFailure,
- next_block_label: mergeLabel,
+ // An inferred merge point is not written back: the backend routes to the
+ // conditional's own next_block_label when no branch target applies.
+ next_block_label:
+ mergeLabel === node.data.inferredMergeLabel ? null : mergeLabel,
branch_conditions: branchConditions,
};
}
@@ -1910,6 +1928,62 @@ function labelsAtOrBeforeConditional(
return result;
}
+// A conditional with no next_block_label whose branches all converge on one
+// block gets that block as its editor merge point, so each branch keeps only
+// its own chain instead of the last branch walk claiming the shared tail.
+// Skipped without a default branch: the editor adds an empty Else on open,
+// which would then save as the merge point instead of null.
+function inferConditionalMergeLabels(
+ blocks: Array<WorkflowBlock>,
+ blocksByLabel: Map<string, WorkflowBlock>,
+ finallyBlockLabel: string | null,
+ inferred: Map<string, string>,
+): Array<WorkflowBlock> {
+ return blocks.map((block) => {
+ if (isNestedLoopWorkflowBlock(block)) {
+ return {
+ ...block,
+ loop_blocks: inferConditionalMergeLabels(
+ block.loop_blocks,
+ blocksByLabel,
+ finallyBlockLabel,
+ inferred,
+ ),
+ } as WorkflowBlock;
+ }
+ if (
+ block.block_type !== "conditional" ||
+ block.next_block_label ||
+ !block.branch_conditions.some((branch) => branch.is_default)
+ ) {
+ return block;
+ }
+ const excludeLabels = labelsAtOrBeforeConditional(block.label, blocks);
+ const chains = block.branch_conditions.map((branch) =>
+ collectLabelsForBranch(
+ branch.next_block_label,
+ null,
+ blocksByLabel,
+ finallyBlockLabel,
+ excludeLabels,
+ ),
+ );
+ if (chains.length < 2) {
+ return block;
+ }
+ const join = chains[0]!.find((label) =>
+ chains.every((chain) => chain.includes(label)),
+ );
+ if (!join) {
+ return block;
+ }
+ inferred.set(block.label, join);
+ const withMerge = { ...block, next_block_label: join };
+ blocksByLabel.set(block.label, withMerge);
+ return withMerge;
+ });
+}
+
/**
* Reconstructs the proper hierarchical structure for conditional blocks from a flat blocks array.
* This is the deserialization counterpart to the edge-based serialization logic.
@@ -2222,7 +2296,15 @@ function findConditionalMergeLabel(
edges,
);
if (!mergeTargetId) {
- return null;
+ // A conditional ending an enclosing branch keeps its loaded merge when
+ // that is where the enclosing branch continues.
+ const parentId = conditionalNode.data.conditionalNodeId;
+ const loadedMerge = conditionalNode.data.mergeLabel;
+ return loadedMerge &&
+ parentId &&
+ findNextBlockLabel(parentId, nodes, edges) === loadedMerge
+ ? loadedMerge
+ : null;
}
const targetNode = nodes.find(
(node) => node.id === mergeTargetId && isWorkflowBlockNode(node),
@@ -2329,6 +2411,19 @@ function getElements(
}
}
+ const inferredMergeLabels = new Map<string, string>();
+ // An outer conditional's branches only reach the shared join once a nested
+ // conditional's own merge is inferred, so repeat until nothing new is found.
+ for (let found = -1; found !== inferredMergeLabels.size; ) {
+ found = inferredMergeLabels.size;
+ blocks = inferConditionalMergeLabels(
+ blocks,
+ buildLabelToBlockMap(blocks),
+ settings.finallyBlockLabel ?? null,
+ inferredMergeLabels,
+ );
+ }
+
const data = generateNodeData(blocks);
const nodes: Array<AppNode> = [];
const edges: Array<Edge> = [];
@@ -2393,6 +2488,10 @@ function getElements(
editable,
effectiveDefaultEngine,
);
+ if (isConditionalNode(node)) {
+ node.data.inferredMergeLabel =
+ inferredMergeLabels.get(node.data.label) ?? null;
+ }
nodes.push(node);
if (isWorkflowBlockNode(node)) {
labelToNode.set(node.data.label, node);
@@ -4378,6 +4477,22 @@ function getUpdatedNodesAfterLabelUpdateForParameterKeys(
...(parameterKeys !== undefined && {
parameterKeys: updatedParameterKeys,
}),
+ // Save reads a conditional's loaded branch targets and merge labels,
+ // so they must follow renames.
+ ...(node.type === "conditional" && {
+ branches: (node.data.branches as Array<BranchCondition>).map(
+ (branch) =>
+ branch.next_block_label === oldLabel
+ ? { ...branch, next_block_label: newLabel }
+ : branch,
+ ),
+ mergeLabel:
+ node.data.mergeLabel === oldLabel ? newLabel : node.data.mergeLabel,
+ inferredMergeLabel:
+ node.data.inferredMergeLabel === oldLabel
+ ? newLabel
+ : node.data.inferredMergeLabel,
+ }),
// Update the label for the node being renamed
label: node.id === id ? newLabel : node.data.label,
},