Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(frontend): detect nested block changes in Co-Pilot diff view (SKY-9332) (#5945)
Aaron Perez097f93dd8b53
diff --git a/skyvern-frontend/src/routes/workflows/components/WorkflowVisualComparisonDrawer.tsx b/skyvern-frontend/src/routes/workflows/components/WorkflowVisualComparisonDrawer.tsxindex f7f35e9f7..652dc55f9 100644--- a/skyvern-frontend/src/routes/workflows/components/WorkflowVisualComparisonDrawer.tsx+++ b/skyvern-frontend/src/routes/workflows/components/WorkflowVisualComparisonDrawer.tsx@@ -15,6 +15,7 @@ import { FlowRenderer } from "../editor/FlowRenderer";import { getElements } from "../editor/workflowEditorUtils";import { ProxyLocation } from "@/api/types";import { AppNode } from "../editor/nodes";+import { areBlocksIdentical } from "../util/compareBlocks";type BlockComparison = {leftBlock?: WorkflowBlock;@@ -34,23 +35,6 @@ function getBlockIdentifier(block: WorkflowBlock): string {return `${block.block_type}:${block.label}`;}-function areBlocksIdentical(- block1: WorkflowBlock,- block2: WorkflowBlock,-): boolean {- // Convert blocks to string representation for comparison- // Remove dynamic fields that shouldn't affect equality- const normalize = (block: WorkflowBlock) => {- const normalized = { ...block };- // Remove output_parameter as it might have different IDs- // eslint-disable-next-line @typescript-eslint/no-unused-vars- const { output_parameter, ...rest } = normalized;- return JSON.stringify(rest, Object.keys(rest).sort());- };-- return normalize(block1) === normalize(block2);-}-function compareWorkflowBlocks(blocks1: WorkflowBlock[],blocks2: WorkflowBlock[],diff --git a/skyvern-frontend/src/routes/workflows/editor/panels/WorkflowComparisonPanel.tsx b/skyvern-frontend/src/routes/workflows/editor/panels/WorkflowComparisonPanel.tsxindex ee5d60384..b9ff6a317 100644--- a/skyvern-frontend/src/routes/workflows/editor/panels/WorkflowComparisonPanel.tsx+++ b/skyvern-frontend/src/routes/workflows/editor/panels/WorkflowComparisonPanel.tsx@@ -15,6 +15,7 @@ import { FlowRenderer } from "../FlowRenderer";import { getElements } from "../workflowEditorUtils";import { ProxyLocation } from "@/api/types";import { AppNode } from "../nodes";+import { areBlocksIdentical } from "../../util/compareBlocks";type BlockComparison = {leftBlock?: WorkflowBlock;@@ -69,23 +70,6 @@ function getBlockIdentifier(block: WorkflowBlock): string {return `${nodeType}:${block.label}`;}-function areBlocksIdentical(- block1: WorkflowBlock,- block2: WorkflowBlock,-): boolean {- // Convert blocks to string representation for comparison- // Remove dynamic fields that shouldn't affect equality- const normalize = (block: WorkflowBlock) => {- const normalized = { ...block };- // Remove output_parameter as it might have different IDs- const { output_parameter, ...rest } = normalized;- console.log(output_parameter);- return JSON.stringify(rest, Object.keys(rest).sort());- };-- return normalize(block1) === normalize(block2);-}-function compareWorkflowBlocks(blocks1: WorkflowBlock[],blocks2: WorkflowBlock[],diff --git a/skyvern-frontend/src/routes/workflows/util/compareBlocks.test.ts b/skyvern-frontend/src/routes/workflows/util/compareBlocks.test.tsnew file mode 100644index 000000000..14020fbdd--- /dev/null+++ b/skyvern-frontend/src/routes/workflows/util/compareBlocks.test.ts@@ -0,0 +1,184 @@+import { describe, test, expect } from "vitest";+import { areBlocksIdentical } from "./compareBlocks";+import { WorkflowBlock } from "../types/workflowTypes";++function makeOutputParameter(id: string) {+ return {+ parameter_type: "output" as const,+ key: "out_key",+ description: null,+ output_parameter_id: id,+ workflow_id: `wf-${id}`,+ created_at: "2026-01-01T00:00:00Z",+ modified_at: "2026-01-02T00:00:00Z",+ deleted_at: null,+ };+}++function makeWorkflowParameter(idSuffix: string, key: string) {+ return {+ parameter_type: "workflow" as const,+ key,+ description: null,+ workflow_id: `wf-${idSuffix}`,+ workflow_parameter_id: `wp-${idSuffix}`,+ workflow_parameter_type: "string" as const,+ default_value: null,+ created_at: `2026-01-01T00:00:0${idSuffix}Z`,+ modified_at: `2026-01-02T00:00:0${idSuffix}Z`,+ deleted_at: null,+ };+}++function makeTaskBlock(overrides: Record<string, unknown> = {}): WorkflowBlock {+ return {+ block_type: "task",+ label: "Block 6",+ output_parameter: makeOutputParameter("a"),+ continue_on_failure: false,+ model: null,+ url: null,+ title: "t",+ navigation_goal: "Click submit",+ data_extraction_goal: null,+ data_schema: null,+ complete_criterion: null,+ terminate_criterion: null,+ error_code_mapping: null,+ parameters: [],+ include_action_history_in_verification: false,+ engine: null,+ ...overrides,+ } as unknown as WorkflowBlock;+}++describe("areBlocksIdentical", () => {+ test("identical blocks compare equal", () => {+ expect(areBlocksIdentical(makeTaskBlock(), makeTaskBlock())).toBe(true);+ });++ test("different output_parameter IDs do not flip blocks to modified", () => {+ const a = makeTaskBlock({ output_parameter: makeOutputParameter("a") });+ const b = makeTaskBlock({ output_parameter: makeOutputParameter("b") });+ expect(areBlocksIdentical(a, b)).toBe(true);+ });++ test("different nested workflow_parameter_id values do not flip blocks to modified", () => {+ const a = makeTaskBlock({+ parameters: [makeWorkflowParameter("1", "name")],+ });+ const b = makeTaskBlock({+ parameters: [makeWorkflowParameter("2", "name")],+ });+ expect(areBlocksIdentical(a, b)).toBe(true);+ });++ test("nested ContextParameter.source IDs are stripped", () => {+ const sourceA = makeWorkflowParameter("1", "src");+ const sourceB = makeWorkflowParameter("2", "src");+ const a = makeTaskBlock({+ parameters: [+ {+ parameter_type: "context",+ key: "ctx",+ description: null,+ source: sourceA,+ value: null,+ },+ ],+ });+ const b = makeTaskBlock({+ parameters: [+ {+ parameter_type: "context",+ key: "ctx",+ description: null,+ source: sourceB,+ value: null,+ },+ ],+ });+ expect(areBlocksIdentical(a, b)).toBe(true);+ });++ test("changes to user-visible fields are detected", () => {+ const a = makeTaskBlock({ navigation_goal: "Click submit" });+ const b = makeTaskBlock({ navigation_goal: "Click cancel" });+ expect(areBlocksIdentical(a, b)).toBe(false);+ });++ test("changes to a parameter key are detected", () => {+ const a = makeTaskBlock({+ parameters: [makeWorkflowParameter("1", "old_key")],+ });+ const b = makeTaskBlock({+ parameters: [makeWorkflowParameter("1", "new_key")],+ });+ expect(areBlocksIdentical(a, b)).toBe(false);+ });++ test("changes inside error_code_mapping are detected", () => {+ const a = makeTaskBlock({ error_code_mapping: { e1: "msg-old" } });+ const b = makeTaskBlock({ error_code_mapping: { e1: "msg-new" } });+ expect(areBlocksIdentical(a, b)).toBe(false);+ });++ test("rotated branch_conditions[].id values do not flip conditional blocks to modified", () => {+ const makeConditional = (idSuffix: string): WorkflowBlock =>+ ({+ block_type: "conditional",+ label: "Conditional 1",+ output_parameter: makeOutputParameter(idSuffix),+ continue_on_failure: false,+ model: null,+ branch_conditions: [+ {+ id: `bc-${idSuffix}`,+ criteria: {+ criteria_type: "jinja2_template",+ expression: "{{ x == 1 }}",+ description: null,+ },+ next_block_label: "Block A",+ description: null,+ is_default: false,+ },+ ],+ }) as unknown as WorkflowBlock;++ expect(areBlocksIdentical(makeConditional("1"), makeConditional("2"))).toBe(+ true,+ );+ });++ test("changes to a branch criteria expression are detected", () => {+ const makeConditional = (expression: string): WorkflowBlock =>+ ({+ block_type: "conditional",+ label: "Conditional 1",+ output_parameter: makeOutputParameter("a"),+ continue_on_failure: false,+ model: null,+ branch_conditions: [+ {+ id: "bc-1",+ criteria: {+ criteria_type: "jinja2_template",+ expression,+ description: null,+ },+ next_block_label: "Block A",+ description: null,+ is_default: false,+ },+ ],+ }) as unknown as WorkflowBlock;++ expect(+ areBlocksIdentical(+ makeConditional("{{ x == 1 }}"),+ makeConditional("{{ x == 2 }}"),+ ),+ ).toBe(false);+ });+});diff --git a/skyvern-frontend/src/routes/workflows/util/compareBlocks.ts b/skyvern-frontend/src/routes/workflows/util/compareBlocks.tsnew file mode 100644index 000000000..14bfe33d5--- /dev/null+++ b/skyvern-frontend/src/routes/workflows/util/compareBlocks.ts@@ -0,0 +1,34 @@+import { stableStringify } from "@/util/stableStringify";+import { WorkflowBlock } from "../types/workflowTypes";++// Backend-generated fields that diverge across workflow versions even when the+// user-visible block content is unchanged. Copilot's rehydrated proposal+// regenerates IDs and timestamps for every parameter, and convert_workflow_definition+// rebuilds BranchCondition with a fresh UUID `id`, so leaving any of these in+// the equality input would flip every block to "modified". The bare `id` here+// targets BranchCondition.id - the only non-suffixed ID field in the block+// model today; revisit if a block type ever gains a user-meaningful `id`.+const COMPARISON_OMIT_KEYS = new Set([+ "output_parameter",+ "workflow_id",+ "created_at",+ "modified_at",+ "deleted_at",+ "id",+]);++function shouldOmitForComparison(key: string): boolean {+ return COMPARISON_OMIT_KEYS.has(key) || key.endsWith("_parameter_id");+}++function areBlocksIdentical(+ block1: WorkflowBlock,+ block2: WorkflowBlock,+): boolean {+ return (+ stableStringify(block1, { omit: shouldOmitForComparison }) ===+ stableStringify(block2, { omit: shouldOmitForComparison })+ );+}++export { areBlocksIdentical, shouldOmitForComparison };diff --git a/skyvern-frontend/src/util/stableStringify.test.ts b/skyvern-frontend/src/util/stableStringify.test.tsnew file mode 100644index 000000000..1cb5986d6--- /dev/null+++ b/skyvern-frontend/src/util/stableStringify.test.ts@@ -0,0 +1,82 @@+import { describe, test, expect } from "vitest";+import { stableStringify } from "./stableStringify";++describe("stableStringify", () => {+ test("produces identical output regardless of top-level key order", () => {+ expect(stableStringify({ a: 1, b: 2 })).toBe(+ stableStringify({ b: 2, a: 1 }),+ );+ });++ test("produces identical output regardless of nested key order", () => {+ const a = { outer: { x: 1, y: 2 } };+ const b = { outer: { y: 2, x: 1 } };+ expect(stableStringify(a)).toBe(stableStringify(b));+ });++ test("preserves nested keys (does not strip them)", () => {+ const result = stableStringify({+ block_type: "task",+ parameters: [{ key: "name", value: "Alice" }],+ });+ expect(result).toContain('"key":"name"');+ expect(result).toContain('"value":"Alice"');+ });++ test("detects differences inside nested objects", () => {+ const a = {+ block_type: "task",+ label: "Block 6",+ navigation_goal: "Click submit",+ parameters: [{ key: "name", value: "Alice" }],+ };+ const b = {+ block_type: "task",+ label: "Block 6",+ navigation_goal: "Click submit",+ parameters: [{ key: "name", value: "Bob" }],+ };+ expect(stableStringify(a)).not.toBe(stableStringify(b));+ });++ test("detects differences inside record-typed fields", () => {+ const a = { error_code_mapping: { e1: "msg-old" } };+ const b = { error_code_mapping: { e1: "msg-new" } };+ expect(stableStringify(a)).not.toBe(stableStringify(b));+ });++ test("preserves array order (does not sort arrays)", () => {+ expect(stableStringify([1, 2, 3])).toBe("[1,2,3]");+ expect(stableStringify([1, 2, 3])).not.toBe(stableStringify([3, 2, 1]));+ });++ test("handles primitives and null", () => {+ expect(stableStringify(null)).toBe("null");+ expect(stableStringify(42)).toBe("42");+ expect(stableStringify("hi")).toBe('"hi"');+ expect(stableStringify(true)).toBe("true");+ });++ test("omits undefined object values like JSON.stringify", () => {+ expect(stableStringify({ a: undefined, b: 1 })).toBe('{"b":1}');+ });++ test("omit predicate strips matching keys at every depth", () => {+ const result = stableStringify(+ { keep: 1, drop: 2, nested: { keep: 3, drop: 4 } },+ { omit: (k) => k === "drop" },+ );+ expect(result).toBe('{"keep":1,"nested":{"keep":3}}');+ });++ test("omit predicate does not omit the root", () => {+ const result = stableStringify({ keep: 1 }, { omit: () => true });+ expect(result).toBe("{}");+ });++ test("returns undefined for non-serializable top-level values", () => {+ expect(stableStringify(undefined)).toBeUndefined();+ expect(stableStringify(() => 1)).toBeUndefined();+ expect(stableStringify(Symbol("x"))).toBeUndefined();+ });+});diff --git a/skyvern-frontend/src/util/stableStringify.ts b/skyvern-frontend/src/util/stableStringify.tsnew file mode 100644index 000000000..081ec4904--- /dev/null+++ b/skyvern-frontend/src/util/stableStringify.ts@@ -0,0 +1,24 @@+type StableStringifyOptions = {+ omit?: (key: string) => boolean;+};++function stableStringify(+ value: unknown,+ options?: StableStringifyOptions,+): string | undefined {+ const omit = options?.omit;+ return JSON.stringify(value, (key, v) => {+ // Replacer is called once with key="" for the root; never omit the root.+ if (key !== "" && omit !== undefined && omit(key)) return undefined;+ if (v !== null && typeof v === "object" && !Array.isArray(v)) {+ const sorted: Record<string, unknown> = {};+ for (const k of Object.keys(v as Record<string, unknown>).sort()) {+ sorted[k] = (v as Record<string, unknown>)[k];+ }+ return sorted;+ }+ return v;+ });+}++export { stableStringify };