Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(SKY-11435): guard getIncrementElements against wiped global counter (#6822)
Aaron Perez4c65c2c1e84e
diff --git a/skyvern-frontend/src/routes/workflows/components/tagging/WorkflowTagEditor.clarity.test.tsx b/skyvern-frontend/src/routes/workflows/components/tagging/WorkflowTagEditor.clarity.test.tsxnew file mode 100644index 000000000..27909cb3a--- /dev/null+++ b/skyvern-frontend/src/routes/workflows/components/tagging/WorkflowTagEditor.clarity.test.tsx@@ -0,0 +1,91 @@+// @vitest-environment jsdom+import {+ afterAll,+ afterEach,+ beforeAll,+ describe,+ expect,+ it,+ vi,+} from "vitest";+import { cleanup, fireEvent, render, screen } from "@testing-library/react";++import { WorkflowTagEditor } from "./WorkflowTagEditor";++// cmdk needs ResizeObserver and scrollIntoView, which jsdom lacks. Install them+// for this suite only and restore afterward so they don't leak into other test+// files sharing the Vitest process.+class MockResizeObserver {+ observe() {}+ unobserve() {}+ disconnect() {}+}++const originalScrollIntoView = Element.prototype.scrollIntoView;++beforeAll(() => {+ vi.stubGlobal("ResizeObserver", MockResizeObserver);+ Element.prototype.scrollIntoView = () => {};+});++afterAll(() => {+ vi.unstubAllGlobals();+ if (originalScrollIntoView) {+ Element.prototype.scrollIntoView = originalScrollIntoView;+ } else {+ delete (Element.prototype as { scrollIntoView?: unknown }).scrollIntoView;+ }+});++vi.mock("../../hooks/useWorkflowTagMutations", () => ({+ useApplyWorkflowTagsMutation: () => ({ mutate: vi.fn(), isPending: false }),+}));++afterEach(cleanup);++function renderEditor() {+ return render(+ <WorkflowTagEditor workflowPermanentId="wpid_1" tags={[]} tagKeys={[]} />,+ );+}++function openAndType(query: string) {+ fireEvent.click(screen.getByRole("button", { name: /edit tags/i }));+ const input = screen.getByPlaceholderText(/add a tag/i);+ fireEvent.change(input, { target: { value: query } });+}++describe("WorkflowTagEditor group-vs-value clarity", () => {+ it("shows a bare entry as a value-only label: empty group slot, no color", () => {+ renderEditor();+ openAndType("prod");+ // The group half renders the empty "no group" slot...+ expect(screen.getByText("no group")).toBeTruthy();+ // ...and the typed value fills the label half.+ expect(screen.getAllByText("prod").length).toBeGreaterThan(0);+ // Color is grouped-only, so a value-only label exposes no swatches.+ expect(screen.queryByRole("button", { name: "blue" })).toBeNull();+ });++ it("shows a key:value entry as a grouped label: filled group + color", () => {+ renderEditor();+ openAndType("env:prod");+ // No empty group slot — the group half is filled with the key.+ expect(screen.queryByText("no group")).toBeNull();+ expect(screen.getAllByText("env").length).toBeGreaterThan(0);+ expect(screen.getAllByText("prod").length).toBeGreaterThan(0);+ // Grouped labels expose the color picker.+ expect(screen.getByRole("button", { name: "blue" })).toBeTruthy();+ });++ it("treats a group prefix as grouped before the value is typed", () => {+ renderEditor();+ openAndType("env:");+ // Group half is already filled; the label half prompts for the value.+ expect(screen.getAllByText("env").length).toBeGreaterThan(0);+ expect(screen.getByText("type a label")).toBeTruthy();+ expect(screen.queryByText("no group")).toBeNull();+ // Already in group mode, so the color picker is available.+ expect(screen.getByRole("button", { name: "blue" })).toBeTruthy();+ });+});diff --git a/skyvern-frontend/src/routes/workflows/components/tagging/WorkflowTagEditor.tsx b/skyvern-frontend/src/routes/workflows/components/tagging/WorkflowTagEditor.tsxindex dbf6216d2..06a7262b7 100644--- a/skyvern-frontend/src/routes/workflows/components/tagging/WorkflowTagEditor.tsx+++ b/skyvern-frontend/src/routes/workflows/components/tagging/WorkflowTagEditor.tsx@@ -1,5 +1,10 @@import * as React from "react";-import { PlusIcon, ReloadIcon, TokensIcon } from "@radix-ui/react-icons";+import {+ ChevronRightIcon,+ PlusIcon,+ ReloadIcon,+ TokensIcon,+} from "@radix-ui/react-icons";import { Button } from "@/components/ui/button";import {Command,@@ -36,14 +41,123 @@ import {} from "../../types/tagTypes";import { useApplyWorkflowTagsMutation } from "../../hooks/useWorkflowTagMutations";import {+ paletteDotClass,randomPaletteColor,tagColorFor,type PaletteColorName,type TagColorMap,} from "../../types/tagColors";+import { cn } from "@/util/utils";import { TagChip } from "./TagChip";import { TagColorSwatchPicker } from "./TagColorSwatchPicker";+const MICRO_LABEL = "text-[10px] font-medium uppercase tracking-wider";++// One half of the tag anatomy: a captioned cell that reads "filled" (the part+// the entry actually creates) or "empty" (the part it leaves blank), so the+// group/value split is visible structure rather than prose.+function AnatomyCell({+ part,+ value,+ placeholder,+ dotClass,+}: {+ part: string;+ value: string | null;+ placeholder: string;+ dotClass?: string;+}) {+ const filled = value !== null && value !== "";+ return (+ <div className="min-w-0 flex-1 space-y-1">+ <div className={cn(MICRO_LABEL, "text-muted-foreground")}>{part}</div>+ <div+ className={cn(+ "flex h-7 items-center gap-1.5 rounded-md border px-2 text-sm",+ filled+ ? "bg-secondary text-secondary-foreground"+ : "border-dashed text-muted-foreground",+ )}+ >+ {dotClass ? (+ <span+ aria-hidden="true"+ className={cn("h-2 w-2 shrink-0 rounded-full", dotClass)}+ />+ ) : null}+ <span className="truncate">{filled ? value : placeholder}</span>+ </div>+ </div>+ );+}++// Live anatomy of the tag the current input will create: a Label|Group mode+// strip plus the two labeled halves (group filled + colored vs. an empty "no+// group" slot), so a value-only label and a grouped one are unmistakable.+function TagCreatePreview({+ groupName,+ labelValue,+ isGroupMode,+ colorDotClass,+}: {+ groupName: string | null;+ labelValue: string | null;+ isGroupMode: boolean;+ colorDotClass: string;+}) {+ return (+ <div className="space-y-2.5">+ {/* Visual reinforcement only — the mode is conveyed in text by the+ anatomy cells below, so hide the decorative segments from SR. */}+ <div+ aria-hidden="true"+ className="inline-flex rounded-md bg-muted p-0.5 text-[11px] font-medium"+ >+ <span+ className={cn(+ "rounded px-2 py-0.5",+ isGroupMode+ ? "text-muted-foreground"+ : "bg-background text-foreground shadow-sm",+ )}+ >+ Label+ </span>+ <span+ className={cn(+ "rounded px-2 py-0.5",+ isGroupMode+ ? "bg-background text-foreground shadow-sm"+ : "text-muted-foreground",+ )}+ >+ Group+ </span>+ </div>+ <div className="flex items-end gap-1.5">+ <AnatomyCell+ part="Group"+ value={isGroupMode ? groupName : null}+ placeholder="no group"+ dotClass={isGroupMode ? colorDotClass : ""}+ />+ <ChevronRightIcon+ aria-hidden="true"+ className={cn(+ "mb-1.5 h-4 w-4 shrink-0",+ isGroupMode ? "text-muted-foreground" : "text-muted-foreground/40",+ )}+ />+ <AnatomyCell+ part="Label"+ value={labelValue}+ placeholder="type a label"+ />+ </div>+ </div>+ );+}+type Props = {workflowPermanentId: string;tags: Array<Tag>;@@ -183,6 +297,10 @@ function WorkflowTagEditor({// group; otherwise suggest groups (to start a grouped tag) and labels.const { typedKey, typedValuePartial } = parseTypedTagQuery(trimmedQuery);+ // A group key can be present before the value completes the candidate, so the+ // mode follows the typed key, not only a finished candidate.+ const isGroupMode = isGroupedCandidate || typedKey !== null;+const groupSuggestions =typedKey === null? tagKeys@@ -264,17 +382,31 @@ function WorkflowTagEditor({}}}/>- {showAdd && isGroupedCandidate ? (- <div className="border-b px-3 py-2">- <div className="mb-1.5 text-xs text-muted-foreground">Color</div>- <TagColorSwatchPicker- value={selectedColor}- onChange={setSelectedColor}+ {candidate !== null || typedKey !== null ? (+ <div className="space-y-3 border-b px-3 py-3">+ <TagCreatePreview+ groupName={candidate?.key ?? typedKey}+ labelValue={candidate?.value ?? null}+ isGroupMode={isGroupMode}+ colorDotClass={paletteDotClass(selectedColor)}/>+ {isGroupMode ? (+ <div className="space-y-1.5">+ <div className={cn(MICRO_LABEL, "text-muted-foreground")}>+ Color+ </div>+ <TagColorSwatchPicker+ value={selectedColor}+ onChange={setSelectedColor}+ />+ </div>+ ) : null}</div>) : null}<CommandList>- <CommandEmpty>Type a label or group:label.</CommandEmpty>+ <CommandEmpty>+ Type a label, or group:label to file it under a group.+ </CommandEmpty>{showAdd && candidate ? (<CommandGroup><CommandItem@@ -291,11 +423,13 @@ function WorkflowTagEditor({) : (<PlusIcon className="mr-2 h-4 w-4" />)}- Add “- {candidate.key !== null- ? `${candidate.key}: ${candidate.value}`- : candidate.value}- ”+ {isGroupedCandidate ? (+ <>+ Add “{candidate.value}” to {candidate.key}+ </>+ ) : (+ <>Add label “{candidate.value}”</>+ )}</CommandItem></CommandGroup>) : null}diff --git a/skyvern/webeye/scraper/domUtils.js b/skyvern/webeye/scraper/domUtils.jsindex 1f348bf6b..92e15650f 100644--- a/skyvern/webeye/scraper/domUtils.js+++ b/skyvern/webeye/scraper/domUtils.js@@ -2757,6 +2757,10 @@ if (window.globalDomDepthMap === undefined) {window.globalDomDepthMap = new Map();}+if (window.globalParsedElementCounter === undefined) {+ window.globalParsedElementCounter = new SafeCounter();+}+function isClassNameIncludesHidden(className) {// some hidden elements are with the classname like `class="select-items select-hide"` or `class="dropdown-container dropdown-invisible"`return (@@ -2999,8 +3003,10 @@ async function stopGlobalIncrementalObserver() {async function getIncrementElements(wait_until_finished = true) {if (wait_until_finished) {while (+ window.globalParsedElementCounter &&+ window.globalOneTimeIncrementElements &&(await window.globalParsedElementCounter.get()) <- window.globalOneTimeIncrementElements.length+ window.globalOneTimeIncrementElements.length) {await asyncSleepFor(100);}diff --git a/tests/unit/test_increment_elements_global_guard.js b/tests/unit/test_increment_elements_global_guard.jsnew file mode 100644index 000000000..a29eb44d0--- /dev/null+++ b/tests/unit/test_increment_elements_global_guard.js@@ -0,0 +1,139 @@+/**+ * Regression test for the getIncrementElements crash path in domUtils.js: a+ * mid-click context reset can wipe window.globalParsedElementCounter, and the+ * wait loop dereferenced `.get()` on undefined and threw a TypeError. The guard+ * must skip the wait when the counter is gone and return an empty result.+ * Exit 0 = pass, exit 1 = failures on stderr.+ */++const fs = require("fs");+const path = require("path");++const src = fs.readFileSync(+ path.join(__dirname, "../../skyvern/webeye/scraper/domUtils.js"),+ "utf8",+);++function extractFn(name) {+ let fnStart = src.indexOf(`async function ${name}(`);+ if (fnStart === -1) fnStart = src.indexOf(`function ${name}(`);+ if (fnStart === -1) throw new Error(`${name} not found`);+ const bodyStart = src.indexOf("{", fnStart);+ let depth = 0,+ fnEnd = -1;+ for (let i = bodyStart; i < src.length; i++) {+ if (src[i] === "{") depth++;+ else if (src[i] === "}") {+ depth--;+ if (depth === 0) {+ fnEnd = i + 1;+ break;+ }+ }+ }+ return src.substring(fnStart, fnEnd);+}++const makeGetIncrementElements = new Function(+ "window",+ "asyncSleepFor",+ "_jsConsoleError",+ "document",+ "buildElementObject",+ `${extractFn("getIncrementElements")}\nreturn getIncrementElements;`,+);++const immediateSleep = () => Promise.resolve();+const throwingDocument = {+ querySelector() {+ throw new Error(+ "document.querySelector should not run for an empty depth map",+ );+ },+};+const throwingBuild = () => {+ throw new Error("buildElementObject should not run for an empty depth map");+};++function bind(windowStub, sleep = immediateSleep) {+ return makeGetIncrementElements(+ windowStub,+ sleep,+ () => {},+ throwingDocument,+ throwingBuild,+ );+}++let passed = 0,+ failed = 0;+async function test(name, fn) {+ try {+ await fn();+ passed++;+ process.stdout.write(` PASS: ${name}\n`);+ } catch (e) {+ failed++;+ process.stderr.write(` FAIL: ${name}\n ${e.message}\n`);+ }+}+function assert(c, m) {+ if (!c) throw new Error(m);+}+function assertEmptyResult(result) {+ assert(Array.isArray(result) && result.length === 2, "result is a 2-tuple");+ assert(+ Array.isArray(result[0]) && result[0].length === 0,+ "elements list is empty",+ );+ assert(+ Array.isArray(result[1]) && result[1].length === 0,+ "tree list is empty",+ );+}++(async () => {+ // --- undefined counter after a context reset must not throw ---+ await test("getIncrementElements: undefined globalParsedElementCounter returns empty, no throw", async () => {+ const win = {+ globalParsedElementCounter: undefined,+ globalOneTimeIncrementElements: [],+ globalDomDepthMap: new Map(),+ };+ // Throws TypeError ("Cannot read properties of undefined (reading 'get')")+ // on the unguarded version; caught by test() and reported as FAIL.+ const result = await bind(win)(true);+ assertEmptyResult(result);+ });++ await test("getIncrementElements: undefined counter with wait_until_finished=false also safe", async () => {+ const win = {+ globalParsedElementCounter: undefined,+ globalOneTimeIncrementElements: undefined,+ globalDomDepthMap: new Map(),+ };+ const result = await bind(win)(false);+ assertEmptyResult(result);+ });++ // --- guard must not break the normal wait path when the counter exists ---+ await test("getIncrementElements: defined counter still waits until parsed catches up", async () => {+ let getCalls = 0;+ const win = {+ globalParsedElementCounter: {+ get: async () => (getCalls++ === 0 ? 0 : 1),+ },+ globalOneTimeIncrementElements: [{}],+ globalDomDepthMap: new Map(),+ };+ const result = await bind(win)(true);+ assert(+ getCalls >= 2,+ "wait loop ran at least one iteration before exiting",+ );+ assertEmptyResult(result);+ });++ console.log(`\n${passed + failed} tests: ${passed} passed, ${failed} failed`);+ process.exit(failed > 0 ? 1 : 0);+})();diff --git a/tests/unit/test_increment_elements_global_guard.py b/tests/unit/test_increment_elements_global_guard.pynew file mode 100644index 000000000..6d3b4d721--- /dev/null+++ b/tests/unit/test_increment_elements_global_guard.py@@ -0,0 +1,49 @@+"""+Regression tests for the getIncrementElements crash guard in domUtils.js.++A mid-click context reset (SPA re-render, iframe document swap, soft nav) can+wipe window.globalParsedElementCounter while the top-frame URL guard still+passes, so getIncrementElements used to dereference `.get()` on undefined and+throw a TypeError. The behavioral Node test proves the wait loop now skips when+the counter is gone, and the source check pins the top-level guard initializer.+"""++import re+import shutil+import subprocess+from pathlib import Path++import pytest++_REPO_ROOT = Path(__file__).parent.parent.parent+_DOMUTILS = _REPO_ROOT / "skyvern" / "webeye" / "scraper" / "domUtils.js"+_NODE = shutil.which("node")++[email protected](_NODE is None, reason="node not on PATH")+class TestIncrementElementsGlobalGuard:+ def test_js_syntax(self):+ result = subprocess.run(+ [_NODE, "--check", str(_DOMUTILS)],+ capture_output=True,+ text=True,+ )+ assert result.returncode == 0, f"JS syntax error: {result.stderr}"++ def test_behavioral(self):+ script = Path(__file__).parent / "test_increment_elements_global_guard.js"+ assert script.exists(), f"Missing {script}"+ result = subprocess.run(+ [_NODE, str(script)],+ capture_output=True,+ text=True,+ timeout=30,+ )+ assert result.returncode == 0, f"Failed:\n{result.stdout}\n{result.stderr}"++ def test_top_level_guard_initializer_present(self):+ source = _DOMUTILS.read_text()+ assert re.search(+ r"if\s*\(\s*window\.globalParsedElementCounter\s*===\s*undefined\s*\)",+ source,+ ), "globalParsedElementCounter must be guard-initialized at top level like its sibling globals"