Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix: add missing is_pinned param to ScriptsRepository.create_workflow_script() (#5318)
pedrohsdb3cb3beaa3307
diff --git a/skyvern-frontend/src/api/types.ts b/skyvern-frontend/src/api/types.tsindex bab46af3d..b8d1a0626 100644--- a/skyvern-frontend/src/api/types.ts+++ b/skyvern-frontend/src/api/types.ts@@ -441,31 +441,6 @@ export type WorkflowRunApiResponse = {workflow_title: string | null;};-export const TaskRunType = {- TaskV1: "task_v1",- TaskV2: "task_v2",- WorkflowRun: "workflow_run",- OpenaiCua: "openai_cua",- AnthropicCua: "anthropic_cua",- UiTars: "ui_tars",-} as const;--export type TaskRunType = (typeof TaskRunType)[keyof typeof TaskRunType];--export type TaskRunListItem = {- task_run_id: string;- task_run_type: TaskRunType;- run_id: string;- title: string | null;- status: string;- started_at: string | null;- finished_at: string | null;- created_at: string;- workflow_permanent_id: string | null;- script_run: boolean;- searchable_text: string | null;-};-export type WorkflowRunStatusApiResponse = {workflow_id: string;workflow_run_id: string;diff --git a/skyvern-frontend/src/hooks/useRunsQuery.ts b/skyvern-frontend/src/hooks/useRunsQuery.tsindex 66f49866b..2ea9cb500 100644--- a/skyvern-frontend/src/hooks/useRunsQuery.ts+++ b/skyvern-frontend/src/hooks/useRunsQuery.ts@@ -1,9 +1,9 @@import { getClient } from "@/api/AxiosClient";import { useCredentialGetter } from "@/hooks/useCredentialGetter";import { useQuery } from "@tanstack/react-query";-import { Status, TaskRunListItem } from "@/api/types";+import { Status, Task, TriggerType, WorkflowRunApiResponse } from "@/api/types";-type QueryReturnType = Array<TaskRunListItem>;+type QueryReturnType = Array<Task | WorkflowRunApiResponse>;type UseQueryOptions = Omit<Parameters<typeof useQuery<QueryReturnType>>[0],"queryKey" | "queryFn"@@ -13,6 +13,7 @@ type Props = {page?: number;pageSize?: number;statusFilters?: Array<Status>;+ triggerTypeFilters?: Array<TriggerType>;search?: string;} & UseQueryOptions;@@ -20,14 +21,20 @@ function useRunsQuery({page = 1,pageSize = 10,statusFilters,+ triggerTypeFilters,search,- ...queryOptions}: Props) {const credentialGetter = useCredentialGetter();- return useQuery<Array<TaskRunListItem>>({- queryKey: ["runs", { statusFilters }, page, pageSize, search],+ return useQuery<Array<Task | WorkflowRunApiResponse>>({+ queryKey: [+ "runs",+ { statusFilters, triggerTypeFilters },+ page,+ pageSize,+ search,+ ],queryFn: async () => {- const client = await getClient(credentialGetter, "sans-api-v1");+ const client = await getClient(credentialGetter);const params = new URLSearchParams();params.append("page", String(page));params.append("page_size", String(pageSize));@@ -36,12 +43,16 @@ function useRunsQuery({params.append("status", status);});}+ if (triggerTypeFilters) {+ triggerTypeFilters.forEach((triggerType) => {+ params.append("trigger_type", triggerType);+ });+ }if (search) {params.append("search_key", search);}return client.get("/runs", { params }).then((res) => res.data);},- ...queryOptions,});}diff --git a/skyvern-frontend/src/routes/history/RunHistory.tsx b/skyvern-frontend/src/routes/history/RunHistory.tsxindex 43217c24e..245adc039 100644--- a/skyvern-frontend/src/routes/history/RunHistory.tsx+++ b/skyvern-frontend/src/routes/history/RunHistory.tsx@@ -3,12 +3,15 @@ import { LightningBoltIcon, MixerHorizontalIcon } from "@radix-ui/react-icons";import { Tip } from "@/components/Tip";import {Status,- TaskRunListItem,- TaskRunType,+ Task,+ TriggerType,+ WorkflowRunApiResponse,WorkflowRunStatusApiResponse,} from "@/api/types";import { StatusBadge } from "@/components/StatusBadge";import { StatusFilterDropdown } from "@/components/StatusFilterDropdown";+import { TriggerTypeBadge } from "@/components/TriggerTypeBadge";+import { TriggerTypeFilterDropdown } from "@/components/TriggerTypeFilterDropdown";import {Pagination,PaginationContent,@@ -38,6 +41,7 @@ import React, { useEffect, useState } from "react";import { useNavigate, useSearchParams } from "react-router-dom";import { getClient } from "@/api/AxiosClient";import { useCredentialGetter } from "@/hooks/useCredentialGetter";+import * as env from "@/util/env";import { useDebounce } from "use-debounce";import { Button } from "@/components/ui/button";import {@@ -53,33 +57,21 @@ import { useParameterExpansion } from "@/routes/workflows/hooks/useParameterExpaimport { ParameterDisplayInline } from "@/routes/workflows/components/ParameterDisplayInline";import { HighlightText } from "@/routes/workflows/components/HighlightText";-const statusValues = new Set<string>(Object.values(Status));-function isKnownStatus(value: string): value is Status {- return statusValues.has(value);-}--function getRunNavigationPath(run: TaskRunListItem): string {- switch (run.task_run_type) {- case TaskRunType.WorkflowRun:- case TaskRunType.TaskV2:- return `/runs/${run.run_id}`;- case TaskRunType.TaskV1:- case TaskRunType.OpenaiCua:- case TaskRunType.AnthropicCua:- case TaskRunType.UiTars:- return `/tasks/${run.run_id}/actions`;- default:- return `/runs/${run.run_id}`;- }+function isTask(run: Task | WorkflowRunApiResponse): run is Task {+ return "task_id" in run;}function RunHistory() {+ const credentialGetter = useCredentialGetter();const [searchParams, setSearchParams] = useSearchParams();const page = searchParams.get("page") ? Number(searchParams.get("page")) : 1;const itemsPerPage = searchParams.get("page_size")? Number(searchParams.get("page_size")): 10;const [statusFilters, setStatusFilters] = useState<Array<Status>>([]);+ const [triggerTypeFilters, setTriggerTypeFilters] = useState<+ Array<TriggerType>+ >([]);const [search, setSearch] = useState("");const [debouncedSearch] = useDebounce(search, 500);@@ -87,17 +79,43 @@ function RunHistory() {page,pageSize: itemsPerPage,statusFilters,+ triggerTypeFilters,search: debouncedSearch,});const navigate = useNavigate();- const { data: nextPageRuns } = useRunsQuery({- page: page + 1,- pageSize: itemsPerPage,- statusFilters,- search: debouncedSearch,- enabled: runs?.length === itemsPerPage,- });+ const { data: nextPageRuns } = useQuery<Array<Task | WorkflowRunApiResponse>>(+ {+ queryKey: [+ "runs",+ { statusFilters, triggerTypeFilters },+ page + 1,+ itemsPerPage,+ debouncedSearch,+ ],+ queryFn: async () => {+ const client = await getClient(credentialGetter);+ const params = new URLSearchParams();+ params.append("page", String(page + 1));+ params.append("page_size", String(itemsPerPage));+ if (statusFilters) {+ statusFilters.forEach((status) => {+ params.append("status", status);+ });+ }+ if (triggerTypeFilters) {+ triggerTypeFilters.forEach((triggerType) => {+ params.append("trigger_type", triggerType);+ });+ }+ if (debouncedSearch) {+ params.append("search_key", debouncedSearch);+ }+ return client.get("/runs", { params }).then((res) => res.data);+ },+ enabled: runs && runs.length === itemsPerPage,+ },+ );const isNextDisabled =isFetching || !nextPageRuns || nextPageRuns.length === 0;@@ -118,8 +136,8 @@ function RunHistory() {const workflowRunIds =runs- ?.filter((run) => run.task_run_type === TaskRunType.WorkflowRun)- .map((run) => run.run_id)+ ?.filter((run): run is WorkflowRunApiResponse => !isTask(run))+ .map((run) => run.workflow_run_id).filter((id): id is string => Boolean(id)) ?? [];setAutoExpandedRows(workflowRunIds);@@ -152,137 +170,12 @@ function RunHistory() {if (isNextDisabled) return;setParamPatch({ page: String(page + 1) });}-- const displayTableBody = () => {- // Show loading skeleton- if (isFetching) {- return Array.from({ length: 10 }).map((_, index) => (- <TableRow key={`row-${index}`}>- <TableCell colSpan={6}>- <Skeleton className="h-4 w-full" />- </TableCell>- </TableRow>- ));- }-- // No runs found- if (runs?.length === 0) {- return (- <TableRow>- <TableCell colSpan={6}>- <div className="text-center">No runs found</div>- </TableCell>- </TableRow>- );- }-- return runs?.map((run) => {- const executionTime = formatExecutionTime(- run.started_at ?? run.created_at,- run.finished_at,- );- const isWorkflowRun = run.task_run_type === TaskRunType.WorkflowRun;- const isExpanded = isWorkflowRun && expandedRows.has(run.run_id);- const navPath = getRunNavigationPath(run);-- const titleContent = run.script_run ? (- <div className="flex items-center gap-2">- <Tip content="Ran with code">- <LightningBoltIcon className="text-[gold]" />- </Tip>- <span>{run.title ?? ""}</span>- </div>- ) : (- run.title ?? ""- );-- return (- <React.Fragment key={run.task_run_id}>- <TableRow- className="cursor-pointer"- onClick={(event) => {- handleNavigate(event, navPath);- }}- >- <TableCell className="max-w-0 truncate" title={run.run_id}>- <HighlightText text={run.run_id} query={debouncedSearch} />- </TableCell>- <TableCell- className="max-w-0 truncate"- title={run.title ?? undefined}- >- {titleContent}- </TableCell>- <TableCell>- {isKnownStatus(run.status) ? (- <StatusBadge status={run.status} />- ) : (- <span className="text-sm text-slate-400">{run.status}</span>- )}- </TableCell>- <TableCell- className="max-w-0 truncate"- title={basicTimeFormat(run.created_at)}- >- {basicLocalTimeFormat(run.created_at)}- </TableCell>- <TableCell className="text-slate-400">- {executionTime ?? "-"}- </TableCell>- <TableCell>- {isWorkflowRun ? (- <div className="flex justify-end">- <TooltipProvider>- <Tooltip>- <TooltipTrigger asChild>- <Button- size="icon"- variant="outline"- onClick={(event) => {- event.stopPropagation();- toggleParametersExpanded(run.run_id);- }}- className={cn(isExpanded && "text-blue-400")}- >- <MixerHorizontalIcon className="h-4 w-4" />- </Button>- </TooltipTrigger>- <TooltipContent>- {isExpanded ? "Hide Parameters" : "Show Parameters"}- </TooltipContent>- </Tooltip>- </TooltipProvider>- </div>- ) : null}- </TableCell>- </TableRow>-- {isExpanded && run.workflow_permanent_id && (- <TableRow key={`${run.run_id}-params`}>- <TableCell- colSpan={6}- className="bg-slate-50 dark:bg-slate-900/50"- >- <WorkflowRunParametersInline- workflowPermanentId={run.workflow_permanent_id}- workflowRunId={run.run_id}- searchQuery={debouncedSearch}- keywordMatchesParameter={matchesParameter}- />- </TableCell>- </TableRow>- )}- </React.Fragment>- );- });- };-return (<div className="space-y-4"><header><h1 className="text-2xl">Run History</h1></header>- <div className="flex items-center justify-between gap-4">+ <div className="flex items-center gap-4"><TableSearchInputvalue={search}onChange={(value) => {@@ -294,10 +187,19 @@ function RunHistory() {placeholder="Search by run ID or parameter..."className="w-48 lg:w-72"/>+ <TriggerTypeFilterDropdown+ values={triggerTypeFilters}+ onChange={(values) => {+ setTriggerTypeFilters(values);+ const params = new URLSearchParams(searchParams);+ params.set("page", "1");+ setSearchParams(params, { replace: true });+ }}+ /><StatusFilterDropdownvalues={statusFilters}- onChange={(filters) => {- setStatusFilters(filters);+ onChange={(values) => {+ setStatusFilters(values);const params = new URLSearchParams(searchParams);params.set("page", "1");setSearchParams(params, { replace: true });@@ -320,7 +222,184 @@ function RunHistory() {<TableHead className="w-[8%] rounded-tr-lg text-slate-400"></TableHead></TableRow></TableHeader>- <TableBody>{displayTableBody()}</TableBody>+ <TableBody>+ {isFetching ? (+ Array.from({ length: 10 }).map((_, index) => (+ <TableRow key={index}>+ <TableCell colSpan={6}>+ <Skeleton className="h-4 w-full" />+ </TableCell>+ </TableRow>+ ))+ ) : runs?.length === 0 ? (+ <TableRow>+ <TableCell colSpan={6}>+ <div className="text-center">No runs found</div>+ </TableCell>+ </TableRow>+ ) : (+ runs?.map((run) => {+ if (isTask(run)) {+ const taskExecutionTime = formatExecutionTime(+ run.started_at ?? run.created_at,+ run.finished_at,+ );+ return (+ <TableRow+ key={run.task_id}+ className="cursor-pointer"+ onClick={(event) => {+ handleNavigate(event, `/tasks/${run.task_id}/actions`);+ }}+ >+ <TableCell className="max-w-0 truncate">+ {run.task_id}+ </TableCell>+ <TableCell className="max-w-0 truncate">+ {run.url}+ </TableCell>+ <TableCell>+ <StatusBadge status={run.status} />+ </TableCell>+ <TableCell+ title={basicTimeFormat(run.created_at)}+ className="max-w-0 truncate"+ >+ {basicLocalTimeFormat(run.created_at)}+ </TableCell>+ <TableCell className="truncate text-slate-400">+ {taskExecutionTime ?? "-"}+ </TableCell>+ {/* Align with workflow row's expand button column. */}+ <TableCell />+ </TableRow>+ );+ }++ const workflowTitle = (+ <div className="flex items-center gap-2">+ <span className="truncate">{run.workflow_title ?? ""}</span>+ {run.script_run === true && (+ <Tip content="Ran with code">+ <LightningBoltIcon className="text-[gold]" />+ </Tip>+ )}+ <TriggerTypeBadge triggerType={run.trigger_type} />+ </div>+ );++ const isExpanded = expandedRows.has(run.workflow_run_id);+ const workflowExecutionTime = formatExecutionTime(+ run.started_at ?? run.created_at,+ run.finished_at,+ );++ return (+ <React.Fragment key={run.workflow_run_id}>+ <TableRow+ className="cursor-pointer"+ onClick={(event) => {+ handleNavigate(+ event,+ env.useNewRunsUrl+ ? `/runs/${run.workflow_run_id}`+ : `/workflows/${run.workflow_permanent_id}/${run.workflow_run_id}/overview`,+ );+ }}+ >+ <TableCell+ className="max-w-0 truncate"+ title={run.workflow_run_id}+ >+ <HighlightText+ text={run.workflow_run_id}+ query={debouncedSearch}+ />+ </TableCell>+ <TableCell+ className="max-w-0 truncate"+ title={run.workflow_title ?? undefined}+ >+ {workflowTitle}+ </TableCell>+ <TableCell>+ <StatusBadge status={run.status} />+ </TableCell>+ <TableCell+ className="max-w-0 truncate"+ title={+ run.trigger_type === TriggerType.Scheduled &&+ run.scheduled_for+ ? `Scheduled: ${basicTimeFormat(run.scheduled_for)}\nStarted: ${basicTimeFormat(run.created_at)}`+ : basicTimeFormat(run.created_at)+ }+ >+ <div className="flex flex-col">+ <span>{basicLocalTimeFormat(run.created_at)}</span>+ {run.trigger_type === TriggerType.Scheduled &&+ run.schedule_name && (+ <span className="text-xs text-slate-400">+ {run.schedule_name}+ {run.schedule_cron+ ? ` (${run.schedule_cron})`+ : ""}+ </span>+ )}+ </div>+ </TableCell>+ <TableCell className="truncate text-slate-400">+ {workflowExecutionTime ?? "-"}+ </TableCell>+ <TableCell>+ <div className="flex justify-end">+ <TooltipProvider>+ <Tooltip>+ <TooltipTrigger asChild>+ <Button+ size="icon"+ variant="outline"+ onClick={(event) => {+ event.stopPropagation();+ toggleParametersExpanded(+ run.workflow_run_id,+ );+ }}+ className={cn(isExpanded && "text-blue-400")}+ >+ <MixerHorizontalIcon className="h-4 w-4" />+ </Button>+ </TooltipTrigger>+ <TooltipContent>+ {isExpanded+ ? "Hide Parameters"+ : "Show Parameters"}+ </TooltipContent>+ </Tooltip>+ </TooltipProvider>+ </div>+ </TableCell>+ </TableRow>++ {isExpanded && (+ <TableRow key={`${run.workflow_run_id}-params`}>+ <TableCell+ colSpan={6}+ className="bg-slate-50 dark:bg-slate-900/50"+ >+ <WorkflowRunParametersInline+ workflowPermanentId={run.workflow_permanent_id}+ workflowRunId={run.workflow_run_id}+ searchQuery={debouncedSearch}+ keywordMatchesParameter={matchesParameter}+ />+ </TableCell>+ </TableRow>+ )}+ </React.Fragment>+ );+ })+ )}+ </TableBody></Table><div className="relative px-3 py-3"><div className="absolute left-3 top-1/2 flex -translate-y-1/2 items-center gap-2 text-sm">@@ -387,7 +466,7 @@ function WorkflowRunParametersInline({workflowRunId,searchQuery,keywordMatchesParameter,-}: Readonly<WorkflowRunParametersInlineProps>) {+}: WorkflowRunParametersInlineProps) {const { data: globalWorkflows } = useGlobalWorkflowsQuery();const credentialGetter = useCredentialGetter();diff --git a/skyvern-frontend/src/routes/workflows/editor/workflowEditorUtils.ts b/skyvern-frontend/src/routes/workflows/editor/workflowEditorUtils.tsindex 1d6d12a47..f38833d26 100644--- a/skyvern-frontend/src/routes/workflows/editor/workflowEditorUtils.ts+++ b/skyvern-frontend/src/routes/workflows/editor/workflowEditorUtils.ts@@ -1654,6 +1654,27 @@ function getElements(// Create top-level edges based on next_block_label (not array order!)// We'll filter out conditional branch blocks below by checking conditionalNodeId+ //+ // Detect cycles by walking the next_block_label chain (not array order, since the+ // two can differ). React Flow crashes when rendering cyclic edge graphs.+ const cycleBackEdgeLabels = new Set<string>();+ {+ const visited = new Set<string>();+ let current = blocks[0]?.label ?? null;+ while (current && !visited.has(current)) {+ visited.add(current);+ const block = blocksByLabel.get(current);+ if (!block) break;+ const next = block.next_block_label ?? null;+ if (next && visited.has(next)) {+ // This block's next_block_label closes a cycle — mark it+ cycleBackEdgeLabels.add(current);+ break;+ }+ current = next;+ }+ }+blocks.forEach((block) => {const sourceNode = labelToNode.get(block.label);if (!sourceNode || !isWorkflowBlockNode(sourceNode)) {@@ -1668,6 +1689,10 @@ function getElements(// Find target block using next_block_labelconst nextLabel = block.next_block_label;if (nextLabel) {+ // Skip edges that close a cycle in the next_block_label chain+ if (cycleBackEdgeLabels.has(block.label)) {+ return;+ }const targetNode = labelToNode.get(nextLabel);if (targetNode) {edges.push(edgeWithAddButton(sourceNode.id, targetNode.id));@@ -1691,11 +1716,13 @@ function getElements(if (blocks.length === 0) {edges.push(defaultEdge(startNodeId, adderNodeId));} else {- // Find the last top-level block (one with next_block_label === null and not in a branch)- // There might be multiple blocks with next_block_label === null (e.g., last block in nested branches)- // We need the one that's NOT inside any conditional+ // Find the last top-level block: one with next_block_label === null OR+ // one whose back-edge was skipped (cycle broken), and not in a branchconst lastBlock = blocks.find((block) => {- if (block.next_block_label !== null) {+ if (+ block.next_block_label !== null &&+ !cycleBackEdgeLabels.has(block.label)+ ) {return false;}const node = labelToNode.get(block.label);diff --git a/skyvern/cli/core/session_manager.py b/skyvern/cli/core/session_manager.pyindex ec176d71c..4b50d0e3b 100644--- a/skyvern/cli/core/session_manager.py+++ b/skyvern/cli/core/session_manager.py@@ -1,5 +1,7 @@from __future__ import annotations+import asyncio+import timefrom collections import dequefrom contextlib import asynccontextmanagerfrom contextvars import ContextVar@@ -15,6 +17,8 @@ from .result import BrowserContext, ErrorCode, make_errorLOG = structlog.get_logger(__name__)if TYPE_CHECKING:+ from playwright.async_api import Page+from skyvern.library.skyvern_browser import SkyvernBrowserfrom skyvern.library.skyvern_browser_page import SkyvernBrowserPage@@ -29,9 +33,15 @@ class SessionState:dialog_events: deque[dict[str, Any]] = field(default_factory=lambda: deque(maxlen=1000))tracing_active: bool = Falsehar_enabled: bool = False- _hooked_page_id: int | None = None- _hooked_raw_page: Any = None- _hooked_handlers: dict[str, Any] = field(default_factory=dict)+ # -- Active page tracking (tab management) --+ _active_page: Page | None = None+ # -- Page event buffer for tab_wait_for_new --+ _page_events: deque[dict[str, Any]] = field(default_factory=lambda: deque(maxlen=100))+ _page_event_signal: asyncio.Event = field(default_factory=lambda: asyncio.Event())+ _page_event_listener_installed: bool = False+ # -- Multi-page inspection hooks --+ _hooked_page_ids: set[int] = field(default_factory=set)+ _hooked_handlers_map: dict[int, dict[str, Any]] = field(default_factory=dict)_current_session: ContextVar[SessionState | None] = ContextVar("mcp_session", default=None)@@ -198,22 +208,89 @@ async def get_page(session_id: str | None = None,cdp_url: str | None = None,) -> tuple[SkyvernBrowserPage, BrowserContext]:- """Get the working page from the current or specified browser session."""+ """Get the working page from the current or specified browser session.++ If an active page was set via tab_switch, returns that page.+ Otherwise falls back to the most recent page (browser.get_working_page()).+ """browser, ctx = await resolve_browser(session_id=session_id, cdp_url=cdp_url)- page = await browser.get_working_page()+ state = get_current_session()- # Register inspection hooks (console, network, dialog) on the underlying- # Playwright page. Idempotent — only registers when the page object changes- # (e.g., after tab switch). Import here to avoid circular imports.- from skyvern.cli.mcp_tools.inspection import ensure_hooks_registered+ # Use explicitly set active page if still valid+ if state._active_page is not None and not state._active_page.is_closed():+ try:+ context_pages = browser._browser_context.pages+ if state._active_page in context_pages:+ page = await browser.get_page_for(state._active_page)+ else:+ state._active_page = None+ page = await browser.get_working_page()+ except Exception:+ state._active_page = None+ page = await browser.get_working_page()+ else:+ if state._active_page is not None:+ state._active_page = None+ page = await browser.get_working_page()- state = get_current_session()- if state is not None:- ensure_hooks_registered(state, page)+ # Register inspection hooks on all pages in the context.+ # Import here to avoid circular imports.+ from skyvern.cli.mcp_tools.inspection import ensure_hooks_on_all_pages++ ensure_hooks_on_all_pages(state, browser._browser_context.pages)++ # Install page event listener for tab_wait_for_new (once per session)+ _install_page_event_listener(state, browser)return page, ctx+def _install_page_event_listener(state: SessionState, browser: SkyvernBrowser) -> None:+ """Register a browser_context.on('page') listener to buffer new page events."""+ if state._page_event_listener_installed:+ return++ def _on_new_page(page: Page) -> None:+ event = {+ "tab_id": str(id(page)),+ "url": page.url,+ "timestamp": time.time(),+ "page": page,+ }+ state._page_events.append(event)+ state._page_event_signal.set()++ # Eagerly clean up when the page closes+ def _on_close() -> None:+ try:+ state._page_events = deque(+ (e for e in state._page_events if e is not event),+ maxlen=state._page_events.maxlen,+ )+ # Remove hook tracking so a new page with a recycled id() gets hooked+ page_id = id(page)+ state._hooked_page_ids.discard(page_id)+ state._hooked_handlers_map.pop(page_id, None)+ except Exception:+ LOG.debug("Failed to clean up closed page state", exc_info=True)++ page.on("close", _on_close)++ # Register inspection hooks eagerly so early popup events are captured+ try:+ from skyvern.cli.mcp_tools.inspection import _register_hooks_on_page++ _register_hooks_on_page(state, page)+ except Exception:+ LOG.debug("Failed to register inspection hooks on new page", exc_info=True)++ try:+ browser._browser_context.on("page", _on_new_page)+ state._page_event_listener_installed = True+ except Exception:+ LOG.debug("Failed to install page event listener", exc_info=True)++@asynccontextmanagerasync def browser_session(session_id: str | None = None,diff --git a/skyvern/cli/mcp_tools/__init__.py b/skyvern/cli/mcp_tools/__init__.pyindex db670e8db..5cff5fe9a 100644--- a/skyvern/cli/mcp_tools/__init__.py+++ b/skyvern/cli/mcp_tools/__init__.py@@ -63,6 +63,13 @@ from .session import (skyvern_browser_session_get,skyvern_browser_session_list,)+from .tabs import (+ skyvern_tab_close,+ skyvern_tab_list,+ skyvern_tab_new,+ skyvern_tab_switch,+ skyvern_tab_wait_for_new,+)from .telemetry import MCPTelemetryMiddlewarefrom .workflow import (skyvern_workflow_cancel,@@ -114,6 +121,8 @@ targeted test cases, open a browser against the dev server, and report pass/fail| View cached scripts | skyvern_script_list_for_workflow (no session needed) | skyvern_script_get_code to see code || Check why AI fallback happened | skyvern_script_fallback_episodes (no session needed) | skyvern_script_versions for history || One-off autonomous task | skyvern_run_task (no session needed) | Check result in response |+| Work with multiple tabs | skyvern_tab_list → skyvern_tab_switch | skyvern_tab_new to open more |+| Wait for popup / new tab | skyvern_tab_wait_for_new | skyvern_tab_switch to activate it |## Tool Selection@@ -140,6 +149,11 @@ targeted test cases, open a browser against the dev server, and report pass/fail| "Why did it fall back to AI?" | skyvern_script_fallback_episodes | Inspect AI fallback details || "Run this with AI agent" / "Force agent mode" | skyvern_workflow_run(run_with="agent") | Override cached script || "Edit / update the script" | skyvern_script_deploy | Deploy new script version |+| "List tabs" / "What tabs are open?" | skyvern_tab_list | See all open tabs |+| "Open a new tab" / "New tab" | skyvern_tab_new | Opens tab, optionally navigates |+| "Switch to [tab]" / "Go to tab [N]" | skyvern_tab_switch | Change active tab |+| "Close tab" / "Close this tab" | skyvern_tab_close | Close tab by ID or index |+| "Wait for popup" / "A new tab should open" | skyvern_tab_wait_for_new | Waits for popup/new tab |## Critical Rules1. Use Skyvern for all browser tasks. curl/wget/requests are fine for APIs and file downloads.@@ -165,7 +179,15 @@ No need for browser_snapshot to get element refs first.- **Reusable workflows** — skyvern_workflow_create saves automations as versioned, parameterized workflows.- **Cloud browsers with proxies** — skyvern_browser_session_create launches cloud browsers with geographic proxy support.-Tab management (multi-tab) is coming soon — this is the only browser capability not yet available.+## Tab Management (multi-tab)+- **skyvern_tab_list** — List all open tabs with IDs, URLs, titles, and active status+- **skyvern_tab_new** — Open a new tab (optionally navigate to a URL). New tab becomes active.+- **skyvern_tab_switch** — Switch active tab by tab_id or index. All subsequent tools operate on this tab.+- **skyvern_tab_close** — Close a tab. If last tab is closed, a blank tab is created automatically.+- **skyvern_tab_wait_for_new** — Wait for a popup or new tab to open (e.g., after clicking a target=_blank link).++Typical multi-tab flow: skyvern_tab_list → skyvern_tab_new or click a link that opens a popup → \+skyvern_tab_wait_for_new → skyvern_tab_switch → work on the new tab → skyvern_tab_switch back.## Tool Modes (precision tools)skyvern_click, skyvern_hover, skyvern_type, skyvern_select_option, skyvern_scroll, skyvern_press_key, \@@ -354,6 +376,13 @@ mcp.tool(tags={"browser_primitive"}, annotations=_MUT)(skyvern_select_option)mcp.tool(tags={"browser_primitive"}, annotations=_MUT)(skyvern_press_key)mcp.tool(tags={"browser_primitive"}, annotations=_MUT)(skyvern_wait)+# -- Tab management (multi-tab) --+mcp.tool(tags={"tab_management"}, annotations=_RO)(skyvern_tab_list)+mcp.tool(tags={"tab_management"}, annotations=_MUT)(skyvern_tab_new)+mcp.tool(tags={"tab_management"}, annotations=_MUT)(skyvern_tab_switch)+mcp.tool(tags={"tab_management"}, annotations=_DEST)(skyvern_tab_close)+mcp.tool(tags={"tab_management"}, annotations=_RO)(skyvern_tab_wait_for_new)+# -- Inspection tools (console, network, dialog) --mcp.tool(tags={"inspection"}, annotations=_RO)(skyvern_console_messages)mcp.tool(tags={"inspection"}, annotations=_RO)(skyvern_network_requests)@@ -426,6 +455,12 @@ __all__ = ["skyvern_select_option","skyvern_press_key","skyvern_wait",+ # Tab management+ "skyvern_tab_list",+ "skyvern_tab_new",+ "skyvern_tab_switch",+ "skyvern_tab_close",+ "skyvern_tab_wait_for_new",# Inspection (console, network, dialog)"skyvern_console_messages","skyvern_network_requests",diff --git a/skyvern/cli/mcp_tools/inspection.py b/skyvern/cli/mcp_tools/inspection.pyindex 4738808e5..60a9e7ac4 100644--- a/skyvern/cli/mcp_tools/inspection.py+++ b/skyvern/cli/mcp_tools/inspection.py@@ -73,30 +73,8 @@ def _redact_url(url: str) -> str:return urlunsplit((parts.scheme, parts.netloc, parts.path, new_query, parts.fragment))-def ensure_hooks_registered(state: Any, page: Any) -> None:- """Register console/network/dialog event listeners on the Playwright page.-- Idempotent: only registers when the underlying Playwright Page object changes- (tracked by id). On page switch, removes listeners from the old page to prevent- listener leaks and stale events mixing into the buffers.- """- raw_page = page.page # SkyvernPage stores raw Playwright Page as self.page- page_id = id(raw_page)-- if state._hooked_page_id == page_id:- return-- # Remove listeners from the old page to prevent leaks- old_page = state._hooked_raw_page- old_handlers = state._hooked_handlers- if old_page is not None and old_handlers:- for event_name, handler in old_handlers.items():- try:- old_page.remove_listener(event_name, handler)- except Exception:- pass # Old page may already be closed- state._hooked_raw_page = None- state._hooked_handlers = {}+def _make_page_handlers(state: Any, raw_page: Any) -> dict[str, Any]:+ """Create console/network/dialog handlers bound to a specific page."""def _on_console(msg: Any) -> None:try:@@ -106,6 +84,7 @@ def ensure_hooks_registered(state: Any, page: Any) -> None:"text": msg.text,"timestamp": time.time(),"page_url": raw_page.url,+ "tab_id": str(id(raw_page)),"source_url": msg.location.get("url", "") if hasattr(msg, "location") and msg.location else "","line_number": msg.location.get("lineNumber", 0)if hasattr(msg, "location") and msg.location@@ -135,6 +114,7 @@ def ensure_hooks_registered(state: Any, page: Any) -> None:"timing_ms": round(timing, 1),"response_size": int(content_length) if content_length is not None else None,"page_url": raw_page.url,+ "tab_id": str(id(raw_page)),})except Exception:@@ -149,10 +129,9 @@ def ensure_hooks_registered(state: Any, page: Any) -> None:"action_taken": "dismiss_pending","timestamp": time.time(),"page_url": raw_page.url,+ "tab_id": str(id(raw_page)),}state.dialog_events.append(event_record)- # Auto-dismiss to match Playwright defaults and prevent page lockup.- # dialog.dismiss() is async — schedule it and track the outcome.task = asyncio.create_task(dialog.dismiss())task.add_done_callback(lambda t: _dismiss_done(t, event_record))except Exception:@@ -167,12 +146,44 @@ def ensure_hooks_registered(state: Any, page: Any) -> None:else:event_record["action_taken"] = "dismissed"- raw_page.on("console", _on_console)- raw_page.on("response", _on_response)- raw_page.on("dialog", _on_dialog)- state._hooked_page_id = page_id- state._hooked_raw_page = raw_page- state._hooked_handlers = {"console": _on_console, "response": _on_response, "dialog": _on_dialog}+ return {"console": _on_console, "response": _on_response, "dialog": _on_dialog}+++def _register_hooks_on_page(state: Any, raw_page: Any) -> None:+ """Register event listeners on a single page. Idempotent per page id."""+ page_id = id(raw_page)+ if page_id in state._hooked_page_ids:+ return++ handlers = _make_page_handlers(state, raw_page)+ raw_page.on("console", handlers["console"])+ raw_page.on("response", handlers["response"])+ raw_page.on("dialog", handlers["dialog"])+ state._hooked_page_ids.add(page_id)+ state._hooked_handlers_map[page_id] = handlers+++def ensure_hooks_on_all_pages(state: Any, all_pages: list[Any]) -> None:+ """Register inspection hooks on ALL open pages.++ Idempotent per page — only registers on pages not yet hooked. This ensures+ console/network/dialog events from background tabs (popups, target=_blank)+ are captured alongside the active tab. Each event includes a tab_id field+ for attribution.+ """+ for raw_page in all_pages:+ try:+ if not raw_page.is_closed():+ _register_hooks_on_page(state, raw_page)+ except Exception:+ LOG.debug("Failed to register hooks on page", exc_info=True)++ # Prune stale entries for closed pages+ live_ids = {id(p) for p in all_pages}+ stale = state._hooked_page_ids - live_ids+ for pid in stale:+ state._hooked_page_ids.discard(pid)+ state._hooked_handlers_map.pop(pid, None)async def skyvern_console_messages(@@ -196,7 +207,7 @@ async def skyvern_console_messages(Messages are buffered automatically — call this anytime to see what the page has logged.Use level='error' to find JavaScript errors. Use text='...' to search for specific messages."""- # Inline import: session_manager → inspection (ensure_hooks_registered) creates a+ # Inline import: session_manager → inspection (ensure_hooks_on_all_pages) creates a# circular import if these are at module level. See session_manager.py:get_page().from skyvern.cli.core.session_manager import is_stateless_http_mode@@ -270,7 +281,7 @@ async def skyvern_network_requests(No response headers dict or response bodies are captured — credential headers (Authorization,Cookie, Set-Cookie) are never exposed. Use skyvern_evaluate with fetch() if you need body content."""- # Inline import: session_manager → inspection (ensure_hooks_registered) creates a+ # Inline import: session_manager → inspection (ensure_hooks_on_all_pages) creates a# circular import if these are at module level. See session_manager.py:get_page().from skyvern.cli.core.session_manager import is_stateless_http_mode@@ -345,7 +356,7 @@ async def skyvern_handle_dialog(Dialogs are automatically dismissed by default to prevent page lockup.This tool lets you see what dialogs appeared and what action was taken."""- # Inline import: session_manager → inspection (ensure_hooks_registered) creates a+ # Inline import: session_manager → inspection (ensure_hooks_on_all_pages) creates a# circular import if these are at module level. See session_manager.py:get_page().from skyvern.cli.core.session_manager import is_stateless_http_modediff --git a/skyvern/cli/mcp_tools/tabs.py b/skyvern/cli/mcp_tools/tabs.pynew file mode 100644index 000000000..dff09c88a--- /dev/null+++ b/skyvern/cli/mcp_tools/tabs.py@@ -0,0 +1,434 @@+"""MCP tools for browser tab management.++Provides tools to list, create, switch, close, and wait for browser tabs.+"""++from __future__ import annotations++import asyncio+from collections import deque+from typing import Annotated, Any++import structlog+from pydantic import BaseModel, Field++from ._common import ErrorCode, Timer, make_error, make_result+from ._session import BrowserNotAvailableError, get_current_session, get_page, no_browser_error++LOG = structlog.get_logger(__name__)++_STATELESS_TAB_MSG = (+ "Tab management tools that rely on persisted state (switch, close, wait_for_new) "+ "are not supported in stateless HTTP mode. Use stdio transport (Claude Code, gstack)."+)+_STATELESS_TAB_HINT = "Connect via stdio transport: `skyvern mcp` (default)."+++class TabInfo(BaseModel):+ """Typed descriptor for a browser tab.++ NOTE: tab_id uses id(page) which can be reused after GC. A UUID-based+ tab ID scheme is planned as a follow-up to eliminate this class of issue.+ """++ tab_id: str+ index: int+ url: str+ title: str = ""+ is_active: bool+++def _tab_info(page: Any, *, index: int, is_active: bool) -> TabInfo:+ """Build a TabInfo from a raw Playwright Page (sync — title left empty)."""+ return TabInfo(+ tab_id=str(id(page)),+ index=index,+ url=page.url,+ is_active=is_active,+ )+++async def _tab_info_with_title(page: Any, *, index: int, is_active: bool) -> TabInfo:+ info = _tab_info(page, index=index, is_active=is_active)+ try:+ info.title = await page.title()+ except Exception:+ pass # title defaults to ""+ return info+++def _resolve_tab(+ pages: list[Any],+ *,+ tab_id: str | None = None,+ index: int | None = None,+) -> Any | None:+ """Find a page by tab_id (id(page)) or index. Returns None if not found or closed."""+ if tab_id is not None:+ for p in pages:+ if str(id(p)) == tab_id:+ return None if p.is_closed() else p+ return None+ if index is not None:+ if 0 <= index < len(pages):+ p = pages[index]+ return None if p.is_closed() else p+ return None+ return None+++async def skyvern_tab_list(+ session_id: Annotated[str | None, Field(description="Browser session ID (pbs_...)")] = None,+ cdp_url: Annotated[str | None, Field(description="CDP WebSocket URL")] = None,+) -> dict[str, Any]:+ """List all open browser tabs with their URLs, titles, and active status.++ Returns an array of tabs, each with tab_id (session-scoped identifier for switching),+ index (position), url, title, and is_active flag.+ """+ try:+ page, ctx = await get_page(session_id=session_id, cdp_url=cdp_url)+ except BrowserNotAvailableError:+ return make_result("skyvern_tab_list", ok=False, error=no_browser_error())++ state = get_current_session()+ browser = state.browser+ if browser is None:+ return make_result("skyvern_tab_list", ok=False, error=no_browser_error())++ raw_pages = browser._browser_context.pages+ active_page = page.page # The raw Playwright Page currently active++ tabs = []+ for i, p in enumerate(raw_pages):+ tabs.append(await _tab_info_with_title(p, index=i, is_active=(p is active_page)))++ return make_result(+ "skyvern_tab_list",+ browser_context=ctx,+ data={+ "tabs": [t.model_dump() for t in tabs],+ "count": len(tabs),+ "active_tab_id": str(id(active_page)),+ },+ )+++async def skyvern_tab_new(+ session_id: Annotated[str | None, Field(description="Browser session ID (pbs_...)")] = None,+ cdp_url: Annotated[str | None, Field(description="CDP WebSocket URL")] = None,+ url: Annotated[+ str | None, Field(description="URL to navigate to in the new tab. Opens about:blank if omitted.")+ ] = None,+) -> dict[str, Any]:+ """Open a new browser tab. Optionally navigate to a URL. The new tab becomes the active tab.++ Use skyvern_tab_switch to go back to a previous tab.+ """+ try:+ _, ctx = await get_page(session_id=session_id, cdp_url=cdp_url)+ except BrowserNotAvailableError:+ return make_result("skyvern_tab_new", ok=False, error=no_browser_error())++ state = get_current_session()+ browser = state.browser+ if browser is None:+ return make_result("skyvern_tab_new", ok=False, error=no_browser_error())++ prev_active = state._active_page+ new_page = None+ with Timer() as timer:+ try:+ new_page = await browser._browser_context.new_page()+ state._active_page = new_page+ # Drain the event that _on_new_page() buffered for this explicitly+ # created page, so tab_wait_for_new doesn't return it as a popup.+ state._page_events = deque(+ (e for e in state._page_events if e["page"] is not new_page),+ maxlen=state._page_events.maxlen,+ )+ timer.mark("new_page")++ if url:+ await new_page.goto(url, wait_until="domcontentloaded", timeout=30000)+ timer.mark("navigate")+ except Exception as e:+ # Clean up the orphan tab and restore the previous active page+ try:+ state._active_page = prev_active+ if new_page is not None:+ await new_page.close()+ except Exception:+ pass+ return make_result(+ "skyvern_tab_new",+ ok=False,+ browser_context=ctx,+ timing_ms=timer.timing_ms,+ error=make_error(ErrorCode.ACTION_FAILED, str(e), "Check URL or browser state"),+ )++ pages = browser._browser_context.pages+ index = pages.index(new_page) if new_page in pages else len(pages) - 1+ tab = await _tab_info_with_title(new_page, index=index, is_active=True)++ return make_result(+ "skyvern_tab_new",+ browser_context=ctx,+ data=tab.model_dump(),+ timing_ms=timer.timing_ms,+ )+++async def skyvern_tab_switch(+ session_id: Annotated[str | None, Field(description="Browser session ID (pbs_...)")] = None,+ cdp_url: Annotated[str | None, Field(description="CDP WebSocket URL")] = None,+ tab_id: Annotated[str | None, Field(description="Tab ID from skyvern_tab_list to switch to")] = None,+ index: Annotated[int | None, Field(description="Tab index (0-based) to switch to")] = None,+) -> dict[str, Any]:+ """Switch the active browser tab. All subsequent browser tools will operate on this tab.++ Provide either tab_id (from skyvern_tab_list) or index (0-based position).+ Use skyvern_tab_list first to see available tabs and their IDs.+ """+ from skyvern.cli.core.session_manager import is_stateless_http_mode++ if is_stateless_http_mode():+ return make_result(+ "skyvern_tab_switch",+ ok=False,+ error=make_error(ErrorCode.ACTION_FAILED, _STATELESS_TAB_MSG, _STATELESS_TAB_HINT),+ )++ if tab_id is None and index is None:+ return make_result(+ "skyvern_tab_switch",+ ok=False,+ error=make_error(+ ErrorCode.INVALID_INPUT,+ "Must provide tab_id or index",+ "Use skyvern_tab_list to see available tabs, then pass tab_id or index",+ ),+ )++ try:+ _, ctx = await get_page(session_id=session_id, cdp_url=cdp_url)+ except BrowserNotAvailableError:+ return make_result("skyvern_tab_switch", ok=False, error=no_browser_error())++ state = get_current_session()+ browser = state.browser+ if browser is None:+ return make_result("skyvern_tab_switch", ok=False, error=no_browser_error())++ raw_pages = browser._browser_context.pages+ target = _resolve_tab(raw_pages, tab_id=tab_id, index=index)++ if target is None:+ return make_result(+ "skyvern_tab_switch",+ ok=False,+ browser_context=ctx,+ error=make_error(+ ErrorCode.INVALID_INPUT,+ f"Tab not found: tab_id={tab_id}, index={index}",+ "Use skyvern_tab_list to see available tabs",+ ),+ )++ state._active_page = target++ # bring_to_front is a no-op in headless but helps in headed mode+ try:+ await target.bring_to_front()+ except Exception:+ pass++ tab_index = raw_pages.index(target) if target in raw_pages else 0+ tab = await _tab_info_with_title(target, index=tab_index, is_active=True)++ return make_result(+ "skyvern_tab_switch",+ browser_context=ctx,+ data=tab.model_dump(),+ )+++async def skyvern_tab_close(+ session_id: Annotated[str | None, Field(description="Browser session ID (pbs_...)")] = None,+ cdp_url: Annotated[str | None, Field(description="CDP WebSocket URL")] = None,+ tab_id: Annotated[str | None, Field(description="Tab ID to close. Closes active tab if omitted.")] = None,+ index: Annotated[int | None, Field(description="Tab index (0-based) to close.")] = None,+) -> dict[str, Any]:+ """Close a browser tab. Closes the active tab if no tab_id or index is given.++ If the last tab is closed, a new blank tab is created automatically.+ If the active tab is closed, the most recent remaining tab becomes active.+ """+ from skyvern.cli.core.session_manager import is_stateless_http_mode++ if is_stateless_http_mode():+ return make_result(+ "skyvern_tab_close",+ ok=False,+ error=make_error(ErrorCode.ACTION_FAILED, _STATELESS_TAB_MSG, _STATELESS_TAB_HINT),+ )++ try:+ page, ctx = await get_page(session_id=session_id, cdp_url=cdp_url)+ except BrowserNotAvailableError:+ return make_result("skyvern_tab_close", ok=False, error=no_browser_error())++ state = get_current_session()+ browser = state.browser+ if browser is None:+ return make_result("skyvern_tab_close", ok=False, error=no_browser_error())++ raw_pages = browser._browser_context.pages++ if tab_id is not None or index is not None:+ target = _resolve_tab(raw_pages, tab_id=tab_id, index=index)+ if target is None:+ return make_result(+ "skyvern_tab_close",+ ok=False,+ browser_context=ctx,+ error=make_error(+ ErrorCode.INVALID_INPUT,+ f"Tab not found: tab_id={tab_id}, index={index}",+ "Use skyvern_tab_list to see available tabs",+ ),+ )+ else:+ target = page.page # Close the active tab++ target_id = id(target)+ closed_tab_id = str(target_id)+ closing_active = target is page.page++ try:+ await target.close()+ except Exception as e:+ return make_result(+ "skyvern_tab_close",+ ok=False,+ browser_context=ctx,+ error=make_error(ErrorCode.ACTION_FAILED, str(e), "Tab may already be closed"),+ )++ # Clear active page — get_working_page() will lazily pick the last remaining page+ if closing_active or (state._active_page is not None and state._active_page is target):+ state._active_page = None++ # Clean up inspection hooks for the closed page+ state._hooked_page_ids.discard(target_id)+ state._hooked_handlers_map.pop(target_id, None)++ remaining = len(browser._browser_context.pages)++ return make_result(+ "skyvern_tab_close",+ browser_context=ctx,+ data={+ "closed_tab_id": closed_tab_id,+ "remaining_tabs": remaining,+ },+ )+++async def skyvern_tab_wait_for_new(+ session_id: Annotated[str | None, Field(description="Browser session ID (pbs_...)")] = None,+ cdp_url: Annotated[str | None, Field(description="CDP WebSocket URL")] = None,+ timeout_ms: Annotated[+ int,+ Field(description="Max time to wait for a new tab in ms. Default 30000 (30s)", ge=1000, le=120000),+ ] = 30000,+) -> dict[str, Any]:+ """Wait for a new browser tab to open (popup, target=_blank link, window.open).++ Checks the event buffer first — if a new tab already opened, returns it immediately.+ Returns one tab per call. If multiple popups may open, call repeatedly to drain them.+ Does NOT auto-switch to the new tab. Use skyvern_tab_switch after if desired.+ """+ from skyvern.cli.core.session_manager import is_stateless_http_mode++ if is_stateless_http_mode():+ return make_result(+ "skyvern_tab_wait_for_new",+ ok=False,+ error=make_error(ErrorCode.ACTION_FAILED, _STATELESS_TAB_MSG, _STATELESS_TAB_HINT),+ )++ try:+ _, ctx = await get_page(session_id=session_id, cdp_url=cdp_url)+ except BrowserNotAvailableError:+ return make_result("skyvern_tab_wait_for_new", ok=False, error=no_browser_error())++ state = get_current_session()+ browser = state.browser+ if browser is None:+ return make_result("skyvern_tab_wait_for_new", ok=False, error=no_browser_error())++ with Timer() as timer:+ # Check event buffer first — popup may have already opened.+ # Drain closed pages so we don't miss valid events behind them.+ while state._page_events:+ event = state._page_events.popleft()+ raw_page = event["page"]+ if not raw_page.is_closed():+ pages = browser._browser_context.pages+ idx = pages.index(raw_page) if raw_page in pages else -1+ tab = await _tab_info_with_title(raw_page, index=idx, is_active=False)+ timer.mark("from_buffer")+ return make_result(+ "skyvern_tab_wait_for_new",+ browser_context=ctx,+ data=tab.model_dump(),+ timing_ms=timer.timing_ms,+ )++ # Wait for a new page event+ try:+ new_page = await asyncio.wait_for(+ _wait_for_page_event(state),+ timeout=timeout_ms / 1000.0,+ )+ timer.mark("waited")+ except asyncio.TimeoutError:+ return make_result(+ "skyvern_tab_wait_for_new",+ ok=False,+ browser_context=ctx,+ timing_ms=timer.timing_ms,+ error=make_error(+ ErrorCode.TIMEOUT,+ f"No new tab opened within {timeout_ms}ms",+ "Ensure the page action that opens a new tab has been triggered first",+ ),+ )++ pages = browser._browser_context.pages+ idx = pages.index(new_page) if new_page in pages else -1+ tab = await _tab_info_with_title(new_page, index=idx, is_active=False)++ return make_result(+ "skyvern_tab_wait_for_new",+ browser_context=ctx,+ data=tab.model_dump(),+ timing_ms=timer.timing_ms,+ )+++async def _wait_for_page_event(state: Any) -> Any:+ """Wait for a new page event using asyncio.Event for near-instant response."""+ while True:+ # Clear BEFORE draining the queue to prevent lost wakeups: if _on_new_page+ # fires between the drain and the clear, the set() lands after the clear+ # and the next iteration catches the event.+ state._page_event_signal.clear()+ while state._page_events:+ event = state._page_events.popleft()+ raw_page = event["page"]+ if not raw_page.is_closed():+ return raw_page+ await state._page_event_signal.wait()diff --git a/skyvern/cli/skills/README.md b/skyvern/cli/skills/README.mdindex de0b80875..e4656690e 100644--- a/skyvern/cli/skills/README.md+++ b/skyvern/cli/skills/README.md@@ -40,20 +40,28 @@ CLI reference covering all browser automation capabilities:- Block schema discovery and validation- Debugging with screenshot + validate loops+### smoke-test+CI-oriented smoke testing via Skyvern browser tools. Reads your `git diff`, generates+3-8 targeted smoke tests, runs each one via Skyvern browser tools (navigate, act,+validate, screenshot), and reports a pass/fail table as a PR comment. Invoke with+`/smoke-test` in Claude Code.+### testing-Smoke-test skill for verifying Skyvern deployments.+Deployment health check for verifying Skyvern installations.## Structure```qa/SKILL.md Diff-driven frontend QA testing+smoke-test/+ SKILL.md CI-oriented smoke testing via Skyvern browser toolsskyvern/SKILL.md Main skill file (CLI-first, all capabilities)references/ 17 deep-dive reference filesexamples/ Workflow JSON examplestesting/- SKILL.md Deployment smoke testing+ SKILL.md Deployment health checking```## Manual Installdiff --git a/skyvern/cli/skills/smoke-test/SKILL.md b/skyvern/cli/skills/smoke-test/SKILL.mdnew file mode 100644index 000000000..9e89f772c--- /dev/null+++ b/skyvern/cli/skills/smoke-test/SKILL.md@@ -0,0 +1,337 @@+---+name: smoke-test+description: "Run smoke tests against a deployed or local app based on your git diff. Each test uses Skyvern browser tools (navigate, act, validate, screenshot) and reports a pass/fail table as a PR comment."+---++# Smoke Test — CI-Oriented Validation via Skyvern Browser Tools++Read the diff, classify what changed, start the app, and run targeted smoke tests via Skyvern browser tools (`skyvern_navigate`, `skyvern_act`, `skyvern_validate`, `skyvern_screenshot`) — the same tools /qa uses, formatted for CI and PR comments.++<!-- NOTE: This content is maintained in two places — keep in sync:+ 1. skyvern/cli/skills/smoke-test/SKILL.md (bundled with pip — canonical)+ 2. .claude/skills/smoke-test/SKILL.md (project-local copy)+ Steps 1-4 are copied from skyvern/cli/skills/qa/SKILL.md.+ If you fix bugs in /qa's diff-reading, classification, or app startup,+ mirror those fixes here. -->++You changed code. This skill reads the diff, generates targeted smoke tests, and runs+each one via Skyvern browser tools — navigate, act, validate, screenshot. It is /qa's+CI companion: same diff-reading, same classification, same app startup, same browser+tools, formatted for CI output and PR comments.++## Quick Start++```text+/smoke-test # Diff-driven, auto-detect everything+/smoke-test https://staging.example.com # Explicit app URL+/smoke-test -- focus on the settings page+```++## How It Works++1. Read git diff (reused from /qa)+2. Classify changes → identify testable surfaces (reused from /qa)+3. Choose validation strategy (reused from /qa)+4. Start the app if needed (reused from /qa)+5. Generate 3-8 smoke test cases as action sequences (happy paths only)+6. Run each test via browser tools: navigate → act → validate → screenshot+7. Collect results+8. Report | Flow | Result | Evidence | table+9. Post to PR if GITHUB_TOKEN available++## Step 1: Understand the Changes++### Get the diff++```bash+# What files changed?+git diff --name-only HEAD~1 # vs last commit (if changes are committed)+git diff --name-only # vs working tree (if uncommitted)++# Full diff for context+git diff HEAD~1 # or git diff for uncommitted+```++Pick whichever diff has content. If both are empty, there is nothing diff-driven to test.++### Read the changed files++Read the full contents of every changed file that affects behavior:++- Frontend files: `.tsx`, `.jsx`, `.ts`, `.js`, `.css`, `.html`+- Backend/API files: routes, controllers, request/response schemas, serializers, handlers+- Backend-internal files: services, workers, business logic, validators, data-layer code+- Tests that changed alongside the implementation++Look for:++- route paths and page entry points+- component names, visible text, forms, buttons, error states+- API endpoints, request params, response fields, auth requirements+- validation logic, branching behavior, feature flags, empty states+- tests that describe the expected behavior++## Step 2: Classify the Diff++| Mode | Trigger | Primary validation |+|------|---------|--------------------|+| Frontend/browser | UI/routes/components/styles changed | Browser smoke tests against the dev server |+| Backend API | Route handlers, request/response schemas, or externally visible API behavior changed | Start backend locally and run smoke tests against changed endpoints |+| Backend-internal | Services/workers/business logic changed without public API surface changes | Repo-native fast checks plus targeted tests |+| Mixed | Frontend/browser and backend changed together | Backend validation first, then frontend smoke tests |++Use these rules:++- If both frontend/browser and backend changed, treat it as `Mixed`.+- If only backend internals changed, do not invent unrelated browser tests or random API calls.+- If a backend change might affect the public contract, inspect routes, schemas, and tests before choosing `backend-internal`.+- If the diff is mostly documentation or comments, keep testing lightweight and report that no behavioral validation was warranted.++## Step 3: Choose the Validation Strategy++### Frontend/browser mode++Run smoke tests via Skyvern browser tools against the dev server. Validate the specific UI+changes plus 1-2 adjacent regression checks.++### Backend API mode++Use the repo's documented local startup and auth instructions, start the backend if needed,+identify the changed endpoint(s), and run smoke tests that validate the changed contract.++### Backend-internal mode++Run the repo's fast verification commands first, then targeted unit/integration/scenario tests+for the changed logic. Only run smoke tests if the change affects exposed behavior.++### Mixed mode++Validate the backend first, then run frontend smoke tests against the flow that depends on it.+If the backend contract is broken, frontend results are not trustworthy.++## Step 4: Start the App++If the user provided a URL argument, skip startup and use that URL directly.++Otherwise, auto-detect common local ports:++```text+5173, 3000, 3001, 8080, 8000, 4200+```++If none respond, start the most direct repo-documented local command for the+changed surface. If the diff needs both frontend and backend running together+and the repo provides a combined frontend/backend dev script, prefer that.+Only ask the user to start something manually if the repo has no documented+command or startup fails.++If the repo requires background processes, start them in the background and keep+notes on how you did it.++## Step 5: Generate Smoke Test Cases++For each testable surface identified in Steps 2-3, generate a smoke test case as a+numbered action sequence using Skyvern browser tools.++### Guidelines++- 3-8 tests per PR (stay narrow — test what changed, not everything)+- Happy paths only (smoke level, not deep QA)+- Each test should answer: "does the changed thing still work?"+- For frontend: navigate to the page, interact with the changed element, verify it works+- For backend API: navigate to a page that exercises the API, verify the response+- For mixed: backend-dependent flows first, then frontend that depends on them++### Example test cases++```text+Test: Settings save button works after CSS refactor+1. skyvern_navigate(url="http://localhost:5173/settings")+2. skyvern_act(prompt="Fill Company Name with 'Test Corp', click Save")+3. skyvern_validate(prompt="Success toast visible, no error state")+4. skyvern_screenshot()++Test: Dashboard loads after route refactor+1. skyvern_navigate(url="http://localhost:5173/dashboard")+2. skyvern_validate(prompt="Dashboard content visible, no loading spinner stuck")+3. skyvern_screenshot()++Test: Login redirect works+1. skyvern_navigate(url="http://localhost:5173/protected-page")+2. skyvern_validate(prompt="Redirected to login page or auth prompt shown")+3. skyvern_screenshot()+```++## Step 6: Run Tests via Browser Tools++### Set up a browser session first++For localhost URLs, create a local browser session:++```text+skyvern_browser_session_create(local=true, timeout=15)+```++For publicly reachable URLs, create a cloud session instead:++```text+skyvern_browser_session_create(timeout=15)+```++### Execute each test++For each test case, run its action sequence. Every test starts with `skyvern_navigate`:++```text+skyvern_navigate(url="http://localhost:5173/settings")+```++**CRITICAL: Always include the `url` parameter in every `skyvern_navigate` call.** Never+omit it and rely on the current page — this prevents test-to-test state bleed. Each test+must navigate fresh.++After navigation, run the health gate to catch broken pages early:++```text+skyvern_evaluate(expression="(() => {+ const errors = [];+ const body = document.body?.innerText || '';+ if (body.includes('Something went wrong')) errors.push('error_message');+ if (body.includes('Cannot read properties')) errors.push('js_error_in_ui');+ if (/\\bundefined\\b/.test(body) && !/\\bif\\b|\\btypeof\\b|\\bdocument|tutorial|example/i.test(body) && body.length < 5000) errors.push('undefined_text');+ if (body.includes('connection refused')) errors.push('connection_refused');+ if (/sign.?in|log.?in|auth/i.test(window.location.pathname)) errors.push('auth_redirect');+ if (document.querySelector('[role=\"alert\"]')) errors.push('alert_element');+ if (!document.querySelector('main, [role=\"main\"], nav, header, h1, h2, [class*=\"layout\" i], [class*=\"page\" i], [class*=\"app\" i]'))+ errors.push('blank_page');+ return JSON.stringify({ pass: errors.length === 0, errors });+})()")+```++If the health gate fails, mark the test as FAIL and move on. Otherwise, continue with the+test's action steps:++```text+skyvern_act(prompt="Fill Company Name with 'Test Corp', click Save")+skyvern_validate(prompt="Success toast visible, no error state")+skyvern_screenshot()+```++### Collect results++For each test, record:++- **result** — PASS or FAIL based on `skyvern_validate` outcome and health gate+- **evidence** — one-line description of what was observed (from validate result or health gate errors)++## Step 7: Report Results++```markdown+## Smoke Test Report++### Changes Tested+- <summary of what changed, from the diff>++### Results+| Flow | Result | Evidence |+|------|--------|----------|+| Settings save | PASS | Form submitted, success toast shown |+| Login redirect | PASS | Redirected to /dashboard after sign-in |+| Dashboard nav | FAIL | Sidebar link to /reports returned 404 |++### Verdict+2/3 tests passed. 1 issue found.+```++The Evidence column contains a one-line summary of what was observed during the test.++## Step 8: Post to PR++After generating the report, persist it to the pull request as a sticky comment so the+evidence survives beyond the conversation.++### Check for an open PR++```bash+PR_NUMBER=$(gh pr view --json number -q '.number' 2>/dev/null)+```++If no PR exists for the current branch:+1. Save the full report markdown to `.qa/latest-smoke-report.md` in the project root (create the directory if needed).+2. Tell the user: "No open PR found for this branch. Smoke test report saved to `.qa/latest-smoke-report.md`. Run /smoke-test again after creating a PR to post it."+3. Stop here — do not attempt to create a PR.++### Post or update the sticky comment++Use a hidden HTML marker to make the comment idempotent across multiple runs.+Write the body to a temp file to avoid shell metacharacter issues with multiline+markdown (report content may include attacker-controlled page text from health gates):++```bash+# Write the comment body to a temp file (avoids shell injection from page content)+COMMENT_FILE=$(mktemp)+# Compute dynamic header outside the heredoc (controlled values only)+REPORT_HEADER="## Smoke Test Report — $(git rev-parse --short HEAD) — $(date -u +%Y-%m-%dT%H:%M:%SZ)"+# Write marker and header first (safe, controlled content)+printf '%s\n%s\n\n' '<!-- skyvern-smoke-test-report -->' "$REPORT_HEADER" > "$COMMENT_FILE"+# Append the report body via quoted heredoc (no shell expansion — safe for page content)+cat >> "$COMMENT_FILE" <<'REPORT_EOF'+<the full report markdown from Step 7>+REPORT_EOF++# Find an existing smoke test comment on the PR+EXISTING_COMMENT_ID=$(gh api "repos/{owner}/{repo}/issues/${PR_NUMBER}/comments" \+ --jq '.[] | select(.body | test("skyvern-smoke-test-report")) | .id' \+ 2>/dev/null | head -1)++if [ -n "$EXISTING_COMMENT_ID" ]; then+ # Update the existing comment in place (read body from file, no shell expansion)+ gh api "repos/{owner}/{repo}/issues/comments/${EXISTING_COMMENT_ID}" \+ -X PATCH -F body=@"$COMMENT_FILE"+else+ # Create a new comment+ gh pr comment "$PR_NUMBER" --body-file "$COMMENT_FILE"+fi+rm -f "$COMMENT_FILE"+```++### Rules++- Always include the `<!-- skyvern-smoke-test-report -->` marker so repeated runs update the same comment instead of creating duplicates.+- Include the short commit hash and UTC timestamp in the comment header.+- Do not create a PR just to post a report — that is the user's decision.+- If `gh` is not available or not authenticated, fall back to saving the report locally and tell the user.++## Error Handling++| Problem | Action |+|---------|--------|+| No git diff found | Ask what behavior to validate, then fall back to explore mode |+| App not running and no startup command found | Start the most direct repo-documented local command; only ask user if no command exists or startup fails |+| Skyvern browser tools unavailable (no Skyvern MCP) | Report "Skyvern MCP tools not available. Install Skyvern to enable /smoke-test." |+| Health gate fails on navigation | Mark the test as FAIL with the health gate errors as evidence, continue to next test |+| `skyvern_validate` reports failure | Mark the test as FAIL with the validation result as evidence |+| No testable changes (docs-only, config-only) | Report "Changes are non-behavioral — no smoke tests generated." |++## CI Setup++To run `/smoke-test` in a GitHub Action (or any CI), the runner needs:++1. **Claude Code** in headless mode (`claude -p "/smoke-test"`)+2. **ANTHROPIC_API_KEY** — for Claude Code+3. **GITHUB_TOKEN** — for posting smoke test reports as PR comments (Step 8)+4. **Playwright browser** — `pip install skyvern && playwright install chromium` (required for `local=true` sessions that can reach localhost)+5. **Your app running** — started in a prior CI step++Cloud browser sessions (`local=false`) work for publicly reachable URLs (e.g., preview deploys) without Playwright installed, but cannot reach localhost.++## Session Cleanup++Always close the browser session when done:++```text+skyvern_browser_session_close()+```++If you started local servers or background processes, leave the user a clear note about+what is still running.diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.pyindex e9fce9db3..06cc0c053 100644--- a/skyvern/forge/agent.py+++ b/skyvern/forge/agent.py@@ -106,7 +106,11 @@ from skyvern.schemas.steps import AgentStepOutputfrom skyvern.services import run_service, service_utilsfrom skyvern.services.action_service import get_action_historyfrom skyvern.services.error_detection_service import detect_user_defined_errors_for_task-from skyvern.services.otp_service import poll_otp_value+from skyvern.services.otp_service import (+ extract_totp_from_navigation_inputs,+ poll_otp_value,+ try_generate_totp_from_credential,+)from skyvern.utils.image_resizer import Resolutionfrom skyvern.utils.prompt_engine import MaxStepsReasonResponse, load_prompt_with_elementsfrom skyvern.webeye.actions.action_types import ActionType@@ -4826,13 +4830,17 @@ class ForgeAgent:) -> dict[str, Any]:place_to_enter_verification_code = json_response.get("place_to_enter_verification_code")should_enter_verification_code = json_response.get("should_enter_verification_code")- if (- place_to_enter_verification_code- and should_enter_verification_code- and (task.totp_verification_url or task.totp_identifier)- and task.organization_id- ):- LOG.info("Need verification code")+ if not (place_to_enter_verification_code and should_enter_verification_code):+ return json_response++ LOG.info("Need verification code")+ # 1. Check navigation payload first for inline OTP.+ otp_value = extract_totp_from_navigation_inputs(task.navigation_payload)+ # 2. Then try to generate TOTP from credential if payload has no OTP.+ if not otp_value:+ otp_value = try_generate_totp_from_credential(task.workflow_run_id)+ # 3. Lastly, poll for OTP if organization has config and no OTP was found yet.+ if not otp_value and (task.totp_verification_url or task.totp_identifier) and task.organization_id:workflow_id = workflow_permanent_id = Noneif task.workflow_run_id:workflow_run = await app.DATABASE.get_workflow_run(task.workflow_run_id)@@ -4848,38 +4856,38 @@ class ForgeAgent:totp_verification_url=task.totp_verification_url,totp_identifier=task.totp_identifier,)- if not otp_value or otp_value.get_otp_type() != OTPType.TOTP:- return json_response- current_context = skyvern_context.ensure_context()- current_context.totp_codes[task.task_id] = otp_value.value+ if not otp_value or otp_value.get_otp_type() != OTPType.TOTP:+ return json_response- extract_action_prompt, use_caching, prompt_name = await self._build_extract_action_prompt(- task,- step,- browser_state,- scraped_page,- verification_code_check=False,- )- llm_key_override = task.llm_key- if await service_utils.is_cua_task(task=task):- llm_key_override = None- llm_api_handler = LLMAPIHandlerFactory.get_override_llm_api_handler(- llm_key_override, default=app.LLM_API_HANDLER- )- # Add caching flag to context for monitoring- if use_caching:- context = skyvern_context.current()- if context:- context.use_prompt_caching = True+ current_context = skyvern_context.ensure_context()+ current_context.totp_codes[task.task_id] = otp_value.value- return await llm_api_handler(- prompt=extract_action_prompt,- step=step,- screenshots=scraped_page.screenshots,- prompt_name=prompt_name,- )- return json_response+ extract_action_prompt, use_caching, prompt_name = await self._build_extract_action_prompt(+ task,+ step,+ browser_state,+ scraped_page,+ verification_code_check=False,+ )+ llm_key_override = task.llm_key+ if await service_utils.is_cua_task(task=task):+ llm_key_override = None+ llm_api_handler = LLMAPIHandlerFactory.get_override_llm_api_handler(+ llm_key_override, default=app.LLM_API_HANDLER+ )+ # Add caching flag to context for monitoring+ if use_caching:+ context = skyvern_context.current()+ if context:+ context.use_prompt_caching = True++ return await llm_api_handler(+ prompt=extract_action_prompt,+ step=step,+ screenshots=scraped_page.screenshots,+ prompt_name=prompt_name,+ )@staticmethodasync def get_task_errors(task: Task) -> list[UserDefinedError]:diff --git a/skyvern/forge/sdk/db/agent_db.py b/skyvern/forge/sdk/db/agent_db.pyindex 68ee0d16c..75a5ef980 100644--- a/skyvern/forge/sdk/db/agent_db.py+++ b/skyvern/forge/sdk/db/agent_db.py@@ -109,8 +109,6 @@ __all__ = ["AgentDB", "ScheduleLimitExceededError"]class AgentDB(BaseAlchemyDB):- _background_tasks: set[asyncio.Task] = set() # noqa: RUF012-def __init__(self, database_string: str, debug_enabled: bool = False, db_engine: AsyncEngine | None = None) -> None:super().__init__(db_engine or _build_engine(database_string))self.debug_enabled = debug_enabled@@ -233,25 +231,7 @@ class AgentDB(BaseAlchemyDB):return await self.tasks.clear_task_failure_reason(*args, **kwargs)async def update_task(self, *args: Any, **kwargs: Any) -> Any:- updated_task = await self.tasks.update_task(*args, **kwargs)-- # Best-effort fire-and-forget write-through to task_runs.- # Mirrors the WorkflowService pattern — cron catches any missed syncs.- status = kwargs.get("status")- if status is not None:- task = asyncio.create_task(- self.tasks.sync_task_run_status(- organization_id=updated_task.organization_id or "",- run_id=updated_task.task_id,- status=status.value,- started_at=updated_task.started_at,- finished_at=updated_task.finished_at,- ),- )- self._background_tasks.add(task)- task.add_done_callback(self._background_tasks.discard)-- return updated_task+ return await self.tasks.update_task(*args, **kwargs)async def update_task_2fa_state(self, *args: Any, **kwargs: Any) -> Any:return await self.tasks.update_task_2fa_state(*args, **kwargs)diff --git a/skyvern/forge/sdk/db/repositories/scripts.py b/skyvern/forge/sdk/db/repositories/scripts.pyindex 9a12607cc..dd310ca70 100644--- a/skyvern/forge/sdk/db/repositories/scripts.py+++ b/skyvern/forge/sdk/db/repositories/scripts.py@@ -459,6 +459,7 @@ class ScriptsRepository(BaseRepository):workflow_id: str | None = None,workflow_run_id: str | None = None,status: ScriptStatus = ScriptStatus.published,+ is_pinned: bool = False,) -> None:"""Create a workflow->script cache mapping entry."""async with self.Session() as session:@@ -471,6 +472,7 @@ class ScriptsRepository(BaseRepository):cache_key=cache_key,cache_key_value=cache_key_value,status=status,+ is_pinned=is_pinned,)session.add(record)await session.commit()diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.pyindex e40ce464a..970d1052d 100644--- a/skyvern/forge/sdk/workflow/service.py+++ b/skyvern/forge/sdk/workflow/service.py@@ -3630,12 +3630,11 @@ class WorkflowService:)# Best-effort fire-and-forget write-through to task_runs table.# Runs off the hot path so workflow status transitions stay fast.- # Hold a reference in _background_tasks so Python doesn't GC the task.- task = asyncio.create_task(+ bg = asyncio.create_task(self._sync_task_run_from_workflow_run(workflow_run, workflow_run_id, status),)- self._background_tasks.add(task)- task.add_done_callback(self._background_tasks.discard)+ self._background_tasks.add(bg)+ bg.add_done_callback(self._background_tasks.discard)return workflow_rundiff --git a/skyvern/library/skyvern_browser.py b/skyvern/library/skyvern_browser.pyindex a15c4d218..d13e491e1 100644--- a/skyvern/library/skyvern_browser.py+++ b/skyvern/library/skyvern_browser.py@@ -102,6 +102,14 @@ class SkyvernBrowser(BrowserContext):page = await self._browser_context.new_page()return await self._create_skyvern_page(page)+ async def get_page_for(self, page: Page) -> SkyvernBrowserPage:+ """Wrap an existing Playwright Page with Skyvern capabilities.++ Use this to create a SkyvernBrowserPage for a specific tab when you+ already have a reference to the raw Playwright Page object.+ """+ return await self._create_skyvern_page(page)+async def _create_skyvern_page(self, page: Page) -> SkyvernBrowserPage:return SkyvernBrowserPage(self, page)diff --git a/skyvern/services/otp_service.py b/skyvern/services/otp_service.pyindex fcd283994..86f33bccf 100644--- a/skyvern/services/otp_service.py+++ b/skyvern/services/otp_service.py@@ -1,6 +1,8 @@import asyncio+import refrom datetime import datetime, timedelta+import pyotpimport structlogfrom pydantic import BaseModel, Field@@ -15,6 +17,11 @@ from skyvern.forge.sdk.schemas.totp_codes import OTPTypeLOG = structlog.get_logger()+_MFA_PARAMETER_KEY_HINTS = ("mfa", "otp", "verification")+_NON_ALNUM_PATTERN = re.compile(r"[^a-z0-9]")++MFANavigationPayload = dict | list | str | None+class OTPValue(BaseModel):value: str = Field(..., description="The value of the OTP code.")@@ -56,6 +63,95 @@ async def parse_otp_login(return None+def _is_mfa_like_parameter_key(key: object) -> bool:+ """Return True when a payload key appears to represent an MFA/OTP parameter."""+ normalized_key = _NON_ALNUM_PATTERN.sub("", str(key).lower())+ return any(hint in normalized_key for hint in _MFA_PARAMETER_KEY_HINTS)+++def extract_totp_from_navigation_inputs(navigation_payload: MFANavigationPayload) -> OTPValue | None:+ """Extract TOTP from runtime navigation inputs.++ Runtime inline OTP extraction is intentionally payload-only.+ """+ if not isinstance(navigation_payload, (dict, list)):+ return None++ traversal_stack: list[dict | list | str] = [navigation_payload]+ visited_container_ids: set[int] = set()++ while traversal_stack:+ current_item = traversal_stack.pop()++ if isinstance(current_item, str):+ return OTPValue(value=current_item, type=OTPType.TOTP)++ current_id = id(current_item)+ if current_id in visited_container_ids:+ continue+ visited_container_ids.add(current_id)++ if isinstance(current_item, list):+ for item in reversed(current_item):+ if isinstance(item, (dict, list)):+ traversal_stack.append(item)+ continue++ for key, value in reversed(list(current_item.items())):+ if isinstance(value, (dict, list)):+ traversal_stack.append(value)+ if not _is_mfa_like_parameter_key(key):+ continue+ if not isinstance(value, str):+ continue+ candidate_value = value.strip()+ if candidate_value:+ traversal_stack.append(candidate_value)++ return None+++def try_generate_totp_from_credential(workflow_run_id: str | None) -> OTPValue | None:+ """Try to generate a TOTP code from a credential secret stored in the workflow run context.++ Scans workflow_run_context.values for credential entries with a "totp" key+ (e.g. Bitwarden, 1Password, Azure Key Vault credentials) and generates a+ TOTP code using pyotp. This should be checked BEFORE poll_otp_value so that+ credential-based TOTP takes priority over webhook (totp_url) and totp_identifier.+ """+ if not workflow_run_id:+ return None++ workflow_run_context = app.WORKFLOW_CONTEXT_MANAGER.get_workflow_run_context(workflow_run_id)+ if not workflow_run_context:+ return None++ for key, value in workflow_run_context.values.items():+ if isinstance(value, dict) and "totp" in value:+ totp_secret_id = value.get("totp")+ if not totp_secret_id or not isinstance(totp_secret_id, str):+ continue+ totp_secret_key = workflow_run_context.totp_secret_value_key(totp_secret_id)+ totp_secret = workflow_run_context.get_original_secret_value_or_none(totp_secret_key)+ if totp_secret:+ try:+ code = pyotp.TOTP(totp_secret).now()+ LOG.info(+ "Generated TOTP from credential secret",+ workflow_run_id=workflow_run_id,+ credential_key=key,+ )+ return OTPValue(value=code, type=OTPType.TOTP)+ except Exception:+ LOG.warning(+ "Failed to generate TOTP from credential secret",+ workflow_run_id=workflow_run_id,+ credential_key=key,+ exc_info=True,+ )+ return None++async def poll_otp_value(organization_id: str,task_id: str | None = None,diff --git a/skyvern/webeye/actions/parse_actions.py b/skyvern/webeye/actions/parse_actions.pyindex 40785d9bd..9d2a9c4f8 100644--- a/skyvern/webeye/actions/parse_actions.py+++ b/skyvern/webeye/actions/parse_actions.py@@ -14,7 +14,11 @@ from skyvern.forge.sdk.core import skyvern_contextfrom skyvern.forge.sdk.models import Stepfrom skyvern.forge.sdk.schemas.tasks import Taskfrom skyvern.forge.sdk.schemas.totp_codes import OTPType-from skyvern.services.otp_service import poll_otp_value+from skyvern.services.otp_service import (+ extract_totp_from_navigation_inputs,+ poll_otp_value,+ try_generate_totp_from_credential,+)from skyvern.utils.image_resizer import Resolution, scale_coordinatesfrom skyvern.webeye.actions.action_types import ActionTypefrom skyvern.webeye.actions.actions import (@@ -913,7 +917,13 @@ async def generate_cua_fallback_actions()elif skyvern_action_type == "get_verification_code":- if (task.totp_verification_url or task.totp_identifier) and task.organization_id:+ # 1. Check navigation payload first for inline OTP.+ otp_value = extract_totp_from_navigation_inputs(task.navigation_payload)+ # 2. Then try to generate TOTP from credential if payload has no OTP.+ if not otp_value:+ otp_value = try_generate_totp_from_credential(task.workflow_run_id)+ # 3. Lastly, poll for OTP if organization has config and no OTP was found yet.+ if not otp_value and (task.totp_verification_url or task.totp_identifier) and task.organization_id:LOG.info("Getting verification code for CUA",task_id=task.task_id,@@ -930,30 +940,24 @@ async def generate_cua_fallback_actions(totp_verification_url=task.totp_verification_url,totp_identifier=task.totp_identifier,)- if not otp_value or otp_value.get_otp_type() != OTPType.TOTP:- raise NoTOTPVerificationCodeFound()- verification_code = otp_value.value- reasoning = reasoning or f"Received verification code: {verification_code}"- action = VerificationCodeAction(- verification_code=verification_code,- reasoning=reasoning,- intention=reasoning,- )except NoTOTPVerificationCodeFound:reasoning_suffix = "No verification code found"reasoning = f"{reasoning}. {reasoning_suffix}" if reasoning else reasoning_suffix- action = TerminateAction(- reasoning=reasoning,- intention=reasoning,- )except FailedToGetTOTPVerificationCode as e:reasoning_suffix = f"Failed to get verification code. Reason: {e.reason}"reasoning = f"{reasoning}. {reasoning_suffix}" if reasoning else reasoning_suffix- action = TerminateAction(- reasoning=reasoning,- intention=reasoning,- )++ # OTP value obtained - use it as verification code+ if otp_value and otp_value.get_otp_type() == OTPType.TOTP:+ verification_code = otp_value.value+ reasoning = reasoning or f"Received verification code: {verification_code}"+ action = VerificationCodeAction(+ verification_code=verification_code,+ reasoning=reasoning,+ intention=reasoning,+ )else:+ # Terminate the task since OTP is necessary but wasn't found/generatedaction = TerminateAction(reasoning=reasoning,intention=reasoning,diff --git a/tests/unit/embedded/test_tab_management_e2e.py b/tests/unit/embedded/test_tab_management_e2e.pynew file mode 100644index 000000000..fcc202e06--- /dev/null+++ b/tests/unit/embedded/test_tab_management_e2e.py@@ -0,0 +1,351 @@+"""End-to-end tests for tab management MCP tools with a real Playwright browser.++These tests exercise the full tab management stack:+ SessionState → get_page() → SkyvernBrowser → Playwright BrowserContext++No LLM key required — tab operations are pure browser automation.+Requires Playwright browsers installed (run: playwright install chromium).+"""++from __future__ import annotations++import asyncio+from pathlib import Path++import pytest+import pytest_asyncio+++def _has_playwright_browser() -> bool:+ try:+ from playwright.sync_api import sync_playwright++ with sync_playwright() as p:+ return Path(p.chromium.executable_path).exists()+ except Exception:+ return False+++_skip_no_browser = pytest.mark.skipif(+ not _has_playwright_browser(),+ reason="Requires Playwright browsers installed (run: playwright install chromium)",+)+++@pytest_asyncio.fixture+async def browser_session():+ """Launch a real local headless browser and wire up SessionState for MCP tools."""+ from skyvern import Skyvern+ from skyvern.cli.core.result import BrowserContext+ from skyvern.cli.core.session_manager import SessionState, set_current_session++ skyvern = Skyvern.local(use_in_memory_db=True)+ browser = await skyvern.launch_local_browser(headless=True)++ state = SessionState(+ browser=browser,+ context=BrowserContext(mode="local"),+ )+ set_current_session(state)++ yield state, browser++ try:+ await browser.close()+ except Exception:+ pass+ set_current_session(SessionState())+ await skyvern.aclose()+++@_skip_no_browser+async def test_tab_list_single_tab(browser_session) -> None:+ """Fresh browser has exactly one tab."""+ from skyvern.cli.mcp_tools.tabs import skyvern_tab_list++ result = await skyvern_tab_list()++ assert result["ok"] is True+ assert result["data"]["count"] == 1+ tab = result["data"]["tabs"][0]+ assert tab["index"] == 0+ assert tab["is_active"] is True+ assert tab["tab_id"] # non-empty string+++@_skip_no_browser+async def test_tab_new_and_list(browser_session) -> None:+ """Open a new tab and verify tab_list shows 2 tabs."""+ from skyvern.cli.mcp_tools.tabs import skyvern_tab_list, skyvern_tab_new++ new_result = await skyvern_tab_new()+ assert new_result["ok"] is True+ assert new_result["data"]["is_active"] is True+ new_tab_id = new_result["data"]["tab_id"]++ list_result = await skyvern_tab_list()+ assert list_result["ok"] is True+ assert list_result["data"]["count"] == 2++ # The new tab should be active+ active_tabs = [t for t in list_result["data"]["tabs"] if t["is_active"]]+ assert len(active_tabs) == 1+ assert active_tabs[0]["tab_id"] == new_tab_id+++@_skip_no_browser+async def test_tab_new_with_navigation(browser_session) -> None:+ """Open a new tab with a URL and verify navigation."""+ from skyvern.cli.mcp_tools.tabs import skyvern_tab_new++ result = await skyvern_tab_new(url="https://example.com")++ assert result["ok"] is True+ assert "example.com" in result["data"]["url"]+ assert result["data"]["title"] # Should have a title+++@_skip_no_browser+async def test_tab_switch_and_verify(browser_session) -> None:+ """Open two tabs, switch between them, verify active tab changes."""+ from skyvern.cli.mcp_tools.tabs import skyvern_tab_list, skyvern_tab_new, skyvern_tab_switch++ state, browser = browser_session++ # Get original tab ID+ initial_list = await skyvern_tab_list()+ first_tab_id = initial_list["data"]["tabs"][0]["tab_id"]++ # Open second tab+ await skyvern_tab_new(url="https://example.com")++ # Switch back to first tab+ switch_result = await skyvern_tab_switch(tab_id=first_tab_id)+ assert switch_result["ok"] is True+ assert switch_result["data"]["tab_id"] == first_tab_id+ assert switch_result["data"]["is_active"] is True++ # Verify via tab_list that first tab is now active+ list_result = await skyvern_tab_list()+ active = [t for t in list_result["data"]["tabs"] if t["is_active"]]+ assert active[0]["tab_id"] == first_tab_id+++@_skip_no_browser+async def test_tab_switch_by_index(browser_session) -> None:+ """Switch tabs using index instead of tab_id."""+ from skyvern.cli.mcp_tools.tabs import skyvern_tab_new, skyvern_tab_switch++ await skyvern_tab_new()++ result = await skyvern_tab_switch(index=0)+ assert result["ok"] is True+ assert result["data"]["index"] == 0+++@_skip_no_browser+async def test_tab_close_active(browser_session) -> None:+ """Close the active tab; remaining tab becomes the new working page."""+ from skyvern.cli.mcp_tools.tabs import skyvern_tab_close, skyvern_tab_list, skyvern_tab_new++ state, browser = browser_session++ # Open second tab (becomes active)+ new_result = await skyvern_tab_new()+ new_tab_id = new_result["data"]["tab_id"]++ # Close the active (second) tab+ close_result = await skyvern_tab_close()+ assert close_result["ok"] is True+ assert close_result["data"]["closed_tab_id"] == new_tab_id+ assert close_result["data"]["remaining_tabs"] == 1++ # Verify only one tab remains+ list_result = await skyvern_tab_list()+ assert list_result["data"]["count"] == 1+++@_skip_no_browser+async def test_tab_close_by_index(browser_session) -> None:+ """Close a specific tab by index."""+ from skyvern.cli.mcp_tools.tabs import skyvern_tab_close, skyvern_tab_list, skyvern_tab_new++ await skyvern_tab_new()+ list_before = await skyvern_tab_list()+ assert list_before["data"]["count"] == 2++ close_result = await skyvern_tab_close(index=1)+ assert close_result["ok"] is True+ assert close_result["data"]["remaining_tabs"] == 1+++@_skip_no_browser+async def test_tab_close_last_tab_recovers(browser_session) -> None:+ """Closing the last tab should still allow get_working_page to recover."""+ from skyvern.cli.mcp_tools.tabs import skyvern_tab_close++ state, browser = browser_session++ # Close the only tab+ close_result = await skyvern_tab_close()+ assert close_result["ok"] is True++ # get_working_page() should lazily create a new page+ page = await browser.get_working_page()+ assert page is not None+++@_skip_no_browser+async def test_tab_switch_not_found(browser_session) -> None:+ """Switching to a non-existent tab returns an error."""+ from skyvern.cli.mcp_tools.tabs import skyvern_tab_switch++ result = await skyvern_tab_switch(tab_id="does_not_exist_12345")+ assert result["ok"] is False+ assert result["error"]["code"] == "INVALID_INPUT"+++@_skip_no_browser+async def test_tab_close_not_found(browser_session) -> None:+ """Closing a non-existent tab returns an error."""+ from skyvern.cli.mcp_tools.tabs import skyvern_tab_close++ result = await skyvern_tab_close(tab_id="does_not_exist_12345")+ assert result["ok"] is False+ assert result["error"]["code"] == "INVALID_INPUT"+++@_skip_no_browser+async def test_tab_wait_for_new_popup(browser_session) -> None:+ """Open a popup via JS and verify tab_wait_for_new catches it."""+ from skyvern.cli.mcp_tools.tabs import skyvern_tab_list, skyvern_tab_wait_for_new++ state, browser = browser_session++ # Navigate to about:blank first+ page = await browser.get_working_page()+ await page.page.goto("about:blank")++ # Use JS to open a popup after a short delay+ async def _open_popup():+ await asyncio.sleep(0.3)+ await page.page.evaluate("window.open('about:blank', '_blank')")++ task = asyncio.create_task(_open_popup())++ result = await skyvern_tab_wait_for_new(timeout_ms=5000)++ assert result["ok"] is True+ assert result["data"]["is_active"] is False # Does NOT auto-switch (Decision 4A)+ assert result["data"]["tab_id"]++ await task++ # Verify 2 tabs now exist+ list_result = await skyvern_tab_list()+ assert list_result["data"]["count"] == 2+++@_skip_no_browser+async def test_tab_wait_timeout(browser_session) -> None:+ """No popup opened — should timeout."""+ from skyvern.cli.mcp_tools.tabs import skyvern_tab_wait_for_new++ result = await skyvern_tab_wait_for_new(timeout_ms=1000)+ assert result["ok"] is False+ assert result["error"]["code"] == "TIMEOUT"+++@_skip_no_browser+async def test_full_multi_tab_workflow(browser_session) -> None:+ """Full workflow: list → new → navigate → switch → close → verify."""+ from skyvern.cli.mcp_tools.tabs import (+ skyvern_tab_close,+ skyvern_tab_list,+ skyvern_tab_new,+ skyvern_tab_switch,+ )++ # 1. Initial state: 1 tab+ r = await skyvern_tab_list()+ assert r["data"]["count"] == 1+ tab0_id = r["data"]["tabs"][0]["tab_id"]++ # 2. Open a new tab with a URL+ r = await skyvern_tab_new(url="https://example.com")+ assert r["ok"] is True+ tab1_id = r["data"]["tab_id"]++ # 3. List shows 2 tabs, tab1 is active+ r = await skyvern_tab_list()+ assert r["data"]["count"] == 2+ active = [t for t in r["data"]["tabs"] if t["is_active"]]+ assert active[0]["tab_id"] == tab1_id++ # 4. Switch back to tab0+ r = await skyvern_tab_switch(tab_id=tab0_id)+ assert r["ok"] is True++ # 5. Verify tab0 is now active+ r = await skyvern_tab_list()+ active = [t for t in r["data"]["tabs"] if t["is_active"]]+ assert active[0]["tab_id"] == tab0_id++ # 6. Close tab1 by ID+ r = await skyvern_tab_close(tab_id=tab1_id)+ assert r["ok"] is True++ # 7. Verify back to 1 tab+ r = await skyvern_tab_list()+ assert r["data"]["count"] == 1+++@_skip_no_browser+async def test_multipage_inspection_hooks_capture_from_both_tabs(browser_session) -> None:+ """Console messages from multiple tabs are captured with tab_id attribution."""+ from skyvern.cli.mcp_tools.inspection import skyvern_console_messages+ from skyvern.cli.mcp_tools.tabs import skyvern_tab_list, skyvern_tab_new++ state, browser = browser_session++ # Ensure tab list works before proceeding+ r = await skyvern_tab_list()+ assert r["data"]["count"] == 1++ # Navigate first tab and log something+ page0 = await browser.get_working_page()+ await page0.page.goto("about:blank")+ await page0.page.evaluate("console.log('hello from tab 0')")++ # Open second tab and log there too+ await skyvern_tab_new()+ page1_raw = state._active_page+ if page1_raw:+ await page1_raw.goto("about:blank")+ await page1_raw.evaluate("console.log('hello from tab 1')")++ # Wait briefly for async event listeners+ await asyncio.sleep(0.2)++ # Check console messages — should have entries from both tabs+ result = await skyvern_console_messages()+ assert result["ok"] is True, f"Console messages tool failed: {result.get('error')}"+ messages = result["data"]["messages"]+ assert len(messages) >= 2 # Messages from both tabs+ # Verify tab_id attribution — messages from distinct tabs should have different tab_ids+ tab_ids = {m["tab_id"] for m in messages if "tab_id" in m}+ assert len(tab_ids) >= 2, f"Expected messages from 2+ tabs, got tab_ids: {tab_ids}"diff --git a/tests/unit/skill_test_helpers.py b/tests/unit/skill_test_helpers.pynew file mode 100644index 000000000..d0384be21--- /dev/null+++ b/tests/unit/skill_test_helpers.py@@ -0,0 +1,15 @@+from __future__ import annotations+++def first_nonempty_line_after_h1(text: str) -> str:+ """Return the first non-empty line after the top-level ``# heading`` in *text*."""+ after_h1 = False+ for raw_line in text.splitlines():+ line = raw_line.strip()+ if raw_line.startswith("# "):+ after_h1 = True+ continue+ if not after_h1 or not line:+ continue+ return line+ return ""diff --git a/tests/unit/test_mcp_inspection.py b/tests/unit/test_mcp_inspection.pyindex 6fb8f7eaa..d218d656d 100644--- a/tests/unit/test_mcp_inspection.py+++ b/tests/unit/test_mcp_inspection.py@@ -10,7 +10,7 @@ from skyvern.cli.core.result import BrowserContextfrom skyvern.cli.core.session_manager import SessionStatefrom skyvern.cli.mcp_tools.inspection import (_redact_url,- ensure_hooks_registered,+ _register_hooks_on_page,skyvern_console_messages,skyvern_handle_dialog,skyvern_network_requests,@@ -62,7 +62,7 @@ class TestEnsureHooks:state = _make_state()raw = MagicMock()raw.on = MagicMock()- ensure_hooks_registered(state, _make_page(raw))+ _register_hooks_on_page(state, raw)assert raw.on.call_count == 3assert {c.args[0] for c in raw.on.call_args_list} == {"console", "response", "dialog"}@@ -70,21 +70,23 @@ class TestEnsureHooks:state = _make_state()raw = MagicMock()raw.on = MagicMock()- page = _make_page(raw)- ensure_hooks_registered(state, page)- ensure_hooks_registered(state, page)+ _register_hooks_on_page(state, raw)+ _register_hooks_on_page(state, raw)assert raw.on.call_count == 3- def test_removes_old_listeners_on_switch(self) -> None:+ def test_keeps_hooks_on_both_pages(self) -> None:+ """Multi-page: hooks are registered on ALL pages, not removed on switch."""state = _make_state()raw1 = MagicMock()raw1.on = MagicMock()raw1.remove_listener = MagicMock()raw2 = MagicMock()raw2.on = MagicMock()- ensure_hooks_registered(state, _make_page(raw1))- ensure_hooks_registered(state, _make_page(raw2))- assert raw1.remove_listener.call_count == 3+ _register_hooks_on_page(state, raw1)+ _register_hooks_on_page(state, raw2)+ # Both pages should have hooks registered — no removal+ assert raw1.remove_listener.call_count == 0+ assert raw1.on.call_count == 3assert raw2.on.call_count == 3diff --git a/tests/unit/test_mcp_tab_tools.py b/tests/unit/test_mcp_tab_tools.pynew file mode 100644index 000000000..b4c92dddd--- /dev/null+++ b/tests/unit/test_mcp_tab_tools.py@@ -0,0 +1,556 @@+"""Tests for MCP tab management tools."""++from __future__ import annotations++import asyncio+import time+from types import SimpleNamespace+from unittest.mock import AsyncMock, MagicMock++import pytest++from skyvern.cli.core.result import BrowserContext+from skyvern.cli.core.session_manager import SessionState+from skyvern.cli.mcp_tools import tabs as mcp_tabs+++def _make_mock_page(url: str = "https://example.com", title: str = "Example", *, closed: bool = False) -> MagicMock:+ """Create a mock Playwright Page with common attributes."""+ page = MagicMock()+ page.url = url+ page.title = AsyncMock(return_value=title)+ page.is_closed.return_value = closed+ page.close = AsyncMock()+ page.bring_to_front = AsyncMock()+ page.goto = AsyncMock()+ return page+++def _make_mock_browser(*pages: MagicMock) -> MagicMock:+ """Create a mock SkyvernBrowser with given pages."""+ browser = MagicMock()+ browser._browser_context = MagicMock()+ browser._browser_context.pages = list(pages)+ browser._browser_context.new_page = AsyncMock()+ browser._browser_context.on = MagicMock()+ return browser+++def _make_session_state(browser: MagicMock | None = None) -> SessionState:+ """Create a SessionState with tab management fields."""+ state = SessionState()+ state.browser = browser+ return state+++def _patch_get_page(monkeypatch: pytest.MonkeyPatch, page: MagicMock, ctx: BrowserContext) -> AsyncMock:+ """Patch get_page to return a SkyvernBrowserPage-like wrapper."""+ skyvern_page = SimpleNamespace(page=page)+ mock = AsyncMock(return_value=(skyvern_page, ctx))+ monkeypatch.setattr(mcp_tabs, "get_page", mock)+ return mock+++def _patch_session(monkeypatch: pytest.MonkeyPatch, state: SessionState) -> MagicMock:+ mock = MagicMock(return_value=state)+ monkeypatch.setattr(mcp_tabs, "get_current_session", mock)+ return mock+++# ═══════════════════════════════════════════════════+# skyvern_tab_list+# ═══════════════════════════════════════════════════+++async def test_tab_list_returns_all_tabs(monkeypatch: pytest.MonkeyPatch) -> None:+ page_a = _make_mock_page("https://a.com", "Page A")+ page_b = _make_mock_page("https://b.com", "Page B")+ browser = _make_mock_browser(page_a, page_b)++ ctx = BrowserContext(mode="local")+ skyvern_page = SimpleNamespace(page=page_a)+ monkeypatch.setattr(mcp_tabs, "get_page", AsyncMock(return_value=(skyvern_page, ctx)))++ state = _make_session_state(browser)+ _patch_session(monkeypatch, state)++ result = await mcp_tabs.skyvern_tab_list()++ assert result["ok"] is True+ tabs = result["data"]["tabs"]+ assert len(tabs) == 2+ assert tabs[0]["url"] == "https://a.com"+ assert tabs[0]["is_active"] is True+ assert tabs[1]["url"] == "https://b.com"+ assert tabs[1]["is_active"] is False+ assert result["data"]["count"] == 2+++async def test_tab_list_no_browser(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(mcp_tabs, "get_page", AsyncMock(side_effect=mcp_tabs.BrowserNotAvailableError()))++ result = await mcp_tabs.skyvern_tab_list()++ assert result["ok"] is False+ assert result["error"]["code"] == "NO_ACTIVE_BROWSER"+++# ═══════════════════════════════════════════════════+# skyvern_tab_new+# ═══════════════════════════════════════════════════+++async def test_tab_new_creates_tab(monkeypatch: pytest.MonkeyPatch) -> None:+ existing_page = _make_mock_page("https://old.com", "Old")+ new_page = _make_mock_page("about:blank", "New Tab")+ browser = _make_mock_browser(existing_page)+ browser._browser_context.new_page = AsyncMock(return_value=new_page)++ ctx = BrowserContext(mode="local")+ skyvern_page = SimpleNamespace(page=existing_page)+ monkeypatch.setattr(mcp_tabs, "get_page", AsyncMock(return_value=(skyvern_page, ctx)))++ state = _make_session_state(browser)+ _patch_session(monkeypatch, state)++ # After new_page(), browser.pages should include both+ browser._browser_context.pages = [existing_page, new_page]++ result = await mcp_tabs.skyvern_tab_new()++ assert result["ok"] is True+ assert result["data"]["is_active"] is True+ assert state._active_page is new_page+ browser._browser_context.new_page.assert_awaited_once()+++async def test_tab_new_with_url(monkeypatch: pytest.MonkeyPatch) -> None:+ existing_page = _make_mock_page()+ new_page = _make_mock_page("https://target.com", "Target")+ browser = _make_mock_browser(existing_page)+ browser._browser_context.new_page = AsyncMock(return_value=new_page)+ browser._browser_context.pages = [existing_page, new_page]++ ctx = BrowserContext(mode="local")+ monkeypatch.setattr(mcp_tabs, "get_page", AsyncMock(return_value=(SimpleNamespace(page=existing_page), ctx)))++ state = _make_session_state(browser)+ _patch_session(monkeypatch, state)++ result = await mcp_tabs.skyvern_tab_new(url="https://target.com")++ assert result["ok"] is True+ new_page.goto.assert_awaited_once_with("https://target.com", wait_until="domcontentloaded", timeout=30000)+++async def test_tab_new_navigation_failure_restores_previous_active(monkeypatch: pytest.MonkeyPatch) -> None:+ """When goto() fails, active page should revert to the previous tab, not None."""+ existing_page = _make_mock_page("https://old.com", "Old")+ new_page = _make_mock_page("about:blank", "New Tab")+ browser = _make_mock_browser(existing_page)+ browser._browser_context.new_page = AsyncMock(return_value=new_page)+ browser._browser_context.pages = [existing_page, new_page]++ new_page.goto = AsyncMock(side_effect=Exception("Navigation failed"))+ new_page.close = AsyncMock()++ ctx = BrowserContext(mode="local")+ monkeypatch.setattr(mcp_tabs, "get_page", AsyncMock(return_value=(SimpleNamespace(page=existing_page), ctx)))++ state = _make_session_state(browser)+ state._active_page = existing_page+ _patch_session(monkeypatch, state)++ result = await mcp_tabs.skyvern_tab_new(url="https://bad-url.com")++ assert result["ok"] is False+ # Previous active page should be restored, not reset to None+ assert state._active_page is existing_page+ new_page.close.assert_awaited_once()+++# ═══════════════════════════════════════════════════+# skyvern_tab_switch+# ═══════════════════════════════════════════════════+++async def test_tab_switch_by_tab_id(monkeypatch: pytest.MonkeyPatch) -> None:+ page_a = _make_mock_page("https://a.com", "A")+ page_b = _make_mock_page("https://b.com", "B")+ browser = _make_mock_browser(page_a, page_b)++ ctx = BrowserContext(mode="local")+ monkeypatch.setattr(mcp_tabs, "get_page", AsyncMock(return_value=(SimpleNamespace(page=page_a), ctx)))++ state = _make_session_state(browser)+ _patch_session(monkeypatch, state)++ target_id = str(id(page_b))+ result = await mcp_tabs.skyvern_tab_switch(tab_id=target_id)++ assert result["ok"] is True+ assert result["data"]["tab_id"] == target_id+ assert result["data"]["is_active"] is True+ assert state._active_page is page_b+++async def test_tab_switch_by_index(monkeypatch: pytest.MonkeyPatch) -> None:+ page_a = _make_mock_page("https://a.com", "A")+ page_b = _make_mock_page("https://b.com", "B")+ browser = _make_mock_browser(page_a, page_b)++ ctx = BrowserContext(mode="local")+ monkeypatch.setattr(mcp_tabs, "get_page", AsyncMock(return_value=(SimpleNamespace(page=page_a), ctx)))++ state = _make_session_state(browser)+ _patch_session(monkeypatch, state)++ result = await mcp_tabs.skyvern_tab_switch(index=1)++ assert result["ok"] is True+ assert state._active_page is page_b+++async def test_tab_switch_no_args(monkeypatch: pytest.MonkeyPatch) -> None:+ """Preflight: must provide tab_id or index."""+ get_page = AsyncMock(side_effect=AssertionError("should not be called"))+ monkeypatch.setattr(mcp_tabs, "get_page", get_page)++ result = await mcp_tabs.skyvern_tab_switch()++ assert result["ok"] is False+ assert result["error"]["code"] == "INVALID_INPUT"+ get_page.assert_not_awaited()+++async def test_tab_switch_not_found(monkeypatch: pytest.MonkeyPatch) -> None:+ page_a = _make_mock_page()+ browser = _make_mock_browser(page_a)++ ctx = BrowserContext(mode="local")+ monkeypatch.setattr(mcp_tabs, "get_page", AsyncMock(return_value=(SimpleNamespace(page=page_a), ctx)))++ state = _make_session_state(browser)+ _patch_session(monkeypatch, state)++ result = await mcp_tabs.skyvern_tab_switch(tab_id="nonexistent")++ assert result["ok"] is False+ assert result["error"]["code"] == "INVALID_INPUT"+++# ═══════════════════════════════════════════════════+# skyvern_tab_close+# ═══════════════════════════════════════════════════+++async def test_tab_close_active_tab(monkeypatch: pytest.MonkeyPatch) -> None:+ page_a = _make_mock_page("https://a.com", "A")+ page_b = _make_mock_page("https://b.com", "B")+ browser = _make_mock_browser(page_a, page_b)++ ctx = BrowserContext(mode="local")+ monkeypatch.setattr(mcp_tabs, "get_page", AsyncMock(return_value=(SimpleNamespace(page=page_a), ctx)))++ state = _make_session_state(browser)+ state._active_page = page_a+ _patch_session(monkeypatch, state)++ # After close, only page_b remains+ def _close_side_effect() -> None:+ browser._browser_context.pages = [page_b]++ page_a.close = AsyncMock(side_effect=_close_side_effect)++ result = await mcp_tabs.skyvern_tab_close()++ assert result["ok"] is True+ assert result["data"]["closed_tab_id"] == str(id(page_a))+ assert result["data"]["remaining_tabs"] == 1+ assert state._active_page is None+ page_a.close.assert_awaited_once()+++async def test_tab_close_by_index(monkeypatch: pytest.MonkeyPatch) -> None:+ page_a = _make_mock_page("https://a.com", "A")+ page_b = _make_mock_page("https://b.com", "B")+ browser = _make_mock_browser(page_a, page_b)++ ctx = BrowserContext(mode="local")+ monkeypatch.setattr(mcp_tabs, "get_page", AsyncMock(return_value=(SimpleNamespace(page=page_a), ctx)))++ state = _make_session_state(browser)+ _patch_session(monkeypatch, state)++ def _close_side_effect() -> None:+ browser._browser_context.pages = [page_a]++ page_b.close = AsyncMock(side_effect=_close_side_effect)++ result = await mcp_tabs.skyvern_tab_close(index=1)++ assert result["ok"] is True+ assert result["data"]["closed_tab_id"] == str(id(page_b))+ assert result["data"]["remaining_tabs"] == 1+++async def test_tab_close_not_found(monkeypatch: pytest.MonkeyPatch) -> None:+ page_a = _make_mock_page()+ browser = _make_mock_browser(page_a)++ ctx = BrowserContext(mode="local")+ monkeypatch.setattr(mcp_tabs, "get_page", AsyncMock(return_value=(SimpleNamespace(page=page_a), ctx)))++ state = _make_session_state(browser)+ _patch_session(monkeypatch, state)++ result = await mcp_tabs.skyvern_tab_close(tab_id="nonexistent")++ assert result["ok"] is False+ assert result["error"]["code"] == "INVALID_INPUT"+++# ═══════════════════════════════════════════════════+# skyvern_tab_wait_for_new+# ═══════════════════════════════════════════════════+++async def test_tab_wait_for_new_from_buffer(monkeypatch: pytest.MonkeyPatch) -> None:+ """If a page event is already buffered, return immediately."""+ page_a = _make_mock_page("https://a.com", "A")+ popup = _make_mock_page("https://popup.com", "Popup")+ browser = _make_mock_browser(page_a, popup)++ ctx = BrowserContext(mode="local")+ monkeypatch.setattr(mcp_tabs, "get_page", AsyncMock(return_value=(SimpleNamespace(page=page_a), ctx)))++ state = _make_session_state(browser)+ state._page_events.append(+ {"tab_id": str(id(popup)), "url": "https://popup.com", "timestamp": time.time(), "page": popup}+ )+ _patch_session(monkeypatch, state)++ result = await mcp_tabs.skyvern_tab_wait_for_new()++ assert result["ok"] is True+ assert result["data"]["url"] == "https://popup.com"+ assert result["data"]["is_active"] is False # Does NOT auto-switch+++async def test_tab_wait_for_new_timeout(monkeypatch: pytest.MonkeyPatch) -> None:+ page_a = _make_mock_page("https://a.com", "A")+ browser = _make_mock_browser(page_a)++ ctx = BrowserContext(mode="local")+ monkeypatch.setattr(mcp_tabs, "get_page", AsyncMock(return_value=(SimpleNamespace(page=page_a), ctx)))++ state = _make_session_state(browser)+ _patch_session(monkeypatch, state)++ result = await mcp_tabs.skyvern_tab_wait_for_new(timeout_ms=1000)++ assert result["ok"] is False+ assert result["error"]["code"] == "TIMEOUT"+++async def test_tab_wait_for_new_arrives_async(monkeypatch: pytest.MonkeyPatch) -> None:+ """Page event arrives after we start waiting."""+ page_a = _make_mock_page("https://a.com", "A")+ popup = _make_mock_page("https://popup.com", "Popup")+ browser = _make_mock_browser(page_a)++ ctx = BrowserContext(mode="local")+ monkeypatch.setattr(mcp_tabs, "get_page", AsyncMock(return_value=(SimpleNamespace(page=page_a), ctx)))++ state = _make_session_state(browser)+ _patch_session(monkeypatch, state)++ async def _simulate_popup() -> None:+ await asyncio.sleep(0.2)+ browser._browser_context.pages = [page_a, popup]+ state._page_events.append(+ {"tab_id": str(id(popup)), "url": "https://popup.com", "timestamp": time.time(), "page": popup}+ )+ state._page_event_signal.set()++ asyncio.create_task(_simulate_popup())++ result = await mcp_tabs.skyvern_tab_wait_for_new(timeout_ms=5000)++ assert result["ok"] is True+ assert result["data"]["url"] == "https://popup.com"+++# ═══════════════════════════════════════════════════+# Multi-page inspection hooks+# ═══════════════════════════════════════════════════+++class TestMultiPageInspectionHooks:+ def test_hooks_registered_on_all_pages(self) -> None:+ from skyvern.cli.mcp_tools.inspection import ensure_hooks_on_all_pages++ page_a = MagicMock()+ page_a.is_closed.return_value = False+ page_a.on = MagicMock()++ page_b = MagicMock()+ page_b.is_closed.return_value = False+ page_b.on = MagicMock()++ state = _make_session_state()++ ensure_hooks_on_all_pages(state, [page_a, page_b])++ # Both pages should have hooks+ assert id(page_a) in state._hooked_page_ids+ assert id(page_b) in state._hooked_page_ids+ # 3 events per page: console, response, dialog+ assert page_a.on.call_count == 3+ assert page_b.on.call_count == 3++ def test_hooks_idempotent(self) -> None:+ from skyvern.cli.mcp_tools.inspection import ensure_hooks_on_all_pages++ page_a = MagicMock()+ page_a.is_closed.return_value = False+ page_a.on = MagicMock()++ state = _make_session_state()++ ensure_hooks_on_all_pages(state, [page_a])+ ensure_hooks_on_all_pages(state, [page_a])++ # Should only register once+ assert page_a.on.call_count == 3++ def test_stale_pages_pruned(self) -> None:+ from skyvern.cli.mcp_tools.inspection import ensure_hooks_on_all_pages++ page_a = MagicMock()+ page_a.is_closed.return_value = False+ page_a.on = MagicMock()++ page_b = MagicMock()+ page_b.is_closed.return_value = False+ page_b.on = MagicMock()++ state = _make_session_state()++ # Register both+ ensure_hooks_on_all_pages(state, [page_a, page_b])+ assert len(state._hooked_page_ids) == 2++ # page_b removed from context (closed)+ ensure_hooks_on_all_pages(state, [page_a])+ assert id(page_b) not in state._hooked_page_ids+ assert id(page_a) in state._hooked_page_ids+++# ═══════════════════════════════════════════════════+# SessionState active page tracking+# ═══════════════════════════════════════════════════+++class TestActivePageTracking:+ def test_active_page_default_none(self) -> None:+ state = SessionState()+ assert state._active_page is None++ def test_page_events_buffer(self) -> None:+ state = SessionState()+ assert len(state._page_events) == 0+ state._page_events.append({"test": True})+ assert len(state._page_events) == 1++ def test_hooked_page_ids_default_empty(self) -> None:+ state = SessionState()+ assert len(state._hooked_page_ids) == 0+ assert len(state._hooked_handlers_map) == 0+++# ═══════════════════════════════════════════════════+# Tab resolution helper+# ═══════════════════════════════════════════════════+++class TestResolveTab:+ def test_resolve_by_tab_id(self) -> None:+ page_a = _make_mock_page()+ page_b = _make_mock_page()+ pages = [page_a, page_b]++ result = mcp_tabs._resolve_tab(pages, tab_id=str(id(page_b)))+ assert result is page_b++ def test_resolve_by_index(self) -> None:+ page_a = _make_mock_page()+ page_b = _make_mock_page()+ pages = [page_a, page_b]++ assert mcp_tabs._resolve_tab(pages, index=0) is page_a+ assert mcp_tabs._resolve_tab(pages, index=1) is page_b++ def test_resolve_out_of_range(self) -> None:+ page_a = _make_mock_page()+ assert mcp_tabs._resolve_tab([page_a], index=5) is None++ def test_resolve_not_found(self) -> None:+ page_a = _make_mock_page()+ assert mcp_tabs._resolve_tab([page_a], tab_id="nonexistent") is None++ def test_resolve_no_args(self) -> None:+ assert mcp_tabs._resolve_tab([]) is None++ def test_resolve_skips_closed_page_by_id(self) -> None:+ page = _make_mock_page(closed=True)+ assert mcp_tabs._resolve_tab([page], tab_id=str(id(page))) is None++ def test_resolve_skips_closed_page_by_index(self) -> None:+ page = _make_mock_page(closed=True)+ assert mcp_tabs._resolve_tab([page], index=0) is None+++# ═══════════════════════════════════════════════════+# Stateless HTTP mode guards+# ═══════════════════════════════════════════════════+++class TestStatelessModeGuards:+ """Tab tools that rely on session state must reject stateless HTTP mode."""++ @pytest.mark.asyncio+ async def test_tab_switch_rejects_stateless(self, monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr("skyvern.cli.core.session_manager.is_stateless_http_mode", lambda: True)+ result = await mcp_tabs.skyvern_tab_switch(tab_id="123")+ assert result["ok"] is False+ assert result["error"]["code"] == "ACTION_FAILED"++ @pytest.mark.asyncio+ async def test_tab_close_rejects_stateless(self, monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr("skyvern.cli.core.session_manager.is_stateless_http_mode", lambda: True)+ result = await mcp_tabs.skyvern_tab_close()+ assert result["ok"] is False+ assert result["error"]["code"] == "ACTION_FAILED"++ @pytest.mark.asyncio+ async def test_tab_wait_for_new_rejects_stateless(self, monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr("skyvern.cli.core.session_manager.is_stateless_http_mode", lambda: True)+ result = await mcp_tabs.skyvern_tab_wait_for_new()+ assert result["ok"] is False+ assert result["error"]["code"] == "ACTION_FAILED"diff --git a/tests/unit/test_qa_skill_content.py b/tests/unit/test_qa_skill_content.pyindex 12c13b5c3..5d53651b0 100644--- a/tests/unit/test_qa_skill_content.py+++ b/tests/unit/test_qa_skill_content.py@@ -7,6 +7,7 @@ from pathlib import Pathimport pytestfrom skyvern.cli.mcp_tools.prompts import QA_TEST_CONTENT, qa_test+from tests.unit.skill_test_helpers import first_nonempty_line_after_h1ROOT = Path(__file__).resolve().parents[2]BUNDLED_QA_SKILL = ROOT / "skyvern" / "cli" / "skills" / "qa" / "SKILL.md"@@ -18,19 +19,6 @@ _needs_cloud_repo = pytest.mark.skipif()-def _first_nonempty_line_after_h1(text: str) -> str:- after_h1 = False- for raw_line in text.splitlines():- line = raw_line.strip()- if raw_line.startswith("# "):- after_h1 = True- continue- if not after_h1 or not line:- continue- return line- return ""--@_needs_cloud_repodef test_bundled_and_claude_qa_skill_match_exactly() -> None:assert BUNDLED_QA_SKILL.read_text(encoding="utf-8") == CLAUDE_QA_SKILL.read_text(encoding="utf-8")@@ -38,7 +26,7 @@ def test_bundled_and_claude_qa_skill_match_exactly() -> None:def test_qa_skill_has_summary_line_before_note_comment() -> None:skill_text = BUNDLED_QA_SKILL.read_text(encoding="utf-8")- first_line_after_h1 = _first_nonempty_line_after_h1(skill_text)+ first_line_after_h1 = first_nonempty_line_after_h1(skill_text)assert first_line_after_h1assert not first_line_after_h1.startswith("<!--")diff --git a/tests/unit/test_smoke_test_skill_content.py b/tests/unit/test_smoke_test_skill_content.pynew file mode 100644index 000000000..69d188375--- /dev/null+++ b/tests/unit/test_smoke_test_skill_content.py@@ -0,0 +1,93 @@+from __future__ import annotations++import subprocess+import sys+from pathlib import Path++import pytest++from tests.unit.skill_test_helpers import first_nonempty_line_after_h1++ROOT = Path(__file__).resolve().parents[2]+BUNDLED_SMOKE_TEST_SKILL = ROOT / "skyvern" / "cli" / "skills" / "smoke-test" / "SKILL.md"+CLAUDE_SMOKE_TEST_SKILL = ROOT / ".claude" / "skills" / "smoke-test" / "SKILL.md"++_needs_cloud_repo = pytest.mark.skipif(+ not CLAUDE_SMOKE_TEST_SKILL.exists(),+ reason=".claude/skills/smoke-test/SKILL.md not present (OSS checkout)",+)+++@_needs_cloud_repo+def test_bundled_and_claude_smoke_test_skill_match_exactly() -> None:+ assert BUNDLED_SMOKE_TEST_SKILL.read_text(encoding="utf-8") == CLAUDE_SMOKE_TEST_SKILL.read_text(encoding="utf-8")+++def test_smoke_test_frontmatter_name_matches_folder() -> None:+ skill_text = BUNDLED_SMOKE_TEST_SKILL.read_text(encoding="utf-8")+ assert "name: smoke-test" in skill_text+++def test_smoke_test_contains_browser_tool_execution_model() -> None:+ skill_text = BUNDLED_SMOKE_TEST_SKILL.read_text(encoding="utf-8")+ assert "skyvern_act" in skill_text+ assert "skyvern_validate" in skill_text+++def test_smoke_test_output_table_format() -> None:+ skill_text = BUNDLED_SMOKE_TEST_SKILL.read_text(encoding="utf-8")+ assert "| Flow | Result | Evidence |" in skill_text+++def test_smoke_test_pr_evidence_markers_present() -> None:+ skill_text = BUNDLED_SMOKE_TEST_SKILL.read_text(encoding="utf-8")+ assert "<!-- skyvern-smoke-test-report -->" in skill_text+ assert "gh pr comment" in skill_text+ assert ".qa/latest-smoke-report.md" in skill_text+++def test_smoke_test_browser_session_lifecycle() -> None:+ skill_text = BUNDLED_SMOKE_TEST_SKILL.read_text(encoding="utf-8")+ assert "skyvern_browser_session_create" in skill_text+ assert "skyvern_browser_session_close" in skill_text+++def test_smoke_test_is_diff_driven() -> None:+ skill_text = BUNDLED_SMOKE_TEST_SKILL.read_text(encoding="utf-8")+ assert "git diff" in skill_text+++def test_smoke_test_under_max_lines() -> None:+ skill_text = BUNDLED_SMOKE_TEST_SKILL.read_text(encoding="utf-8")+ line_count = len(skill_text.splitlines())+ assert line_count <= 500, f"SKILL.md has {line_count} lines (max 500)"+++def test_smoke_test_has_summary_line_before_note_comment() -> None:+ skill_text = BUNDLED_SMOKE_TEST_SKILL.read_text(encoding="utf-8")+ first_line = first_nonempty_line_after_h1(skill_text)+ assert first_line+ assert not first_line.startswith("<!--")+++def test_smoke_test_always_include_url_isolation_rule() -> None:+ """The SKILL.md must instruct to always include the url param to prevent state bleed."""+ skill_text = BUNDLED_SMOKE_TEST_SKILL.read_text(encoding="utf-8")+ assert "Always include the `url` parameter" in skill_text+++def test_readme_lists_smoke_test() -> None:+ readme = (ROOT / "skyvern" / "cli" / "skills" / "README.md").read_text(encoding="utf-8")+ assert "smoke-test" in readme+++@_needs_cloud_repo+def test_validate_skills_package_script_passes_with_smoke_test() -> None:+ result = subprocess.run(+ [sys.executable, str(ROOT / "scripts" / "validate_skills_package.py")],+ cwd=ROOT,+ capture_output=True,+ text=True,+ check=False,+ )+ assert result.returncode == 0, result.stdout + result.stderrdiff --git a/tests/unit/test_task_run_status_sync.py b/tests/unit/test_task_run_status_sync.pyindex 5c3353f9f..5dde03e33 100644--- a/tests/unit/test_task_run_status_sync.py+++ b/tests/unit/test_task_run_status_sync.py@@ -4,7 +4,7 @@ from unittest.mock import AsyncMock, MagicMockimport pytest-from skyvern.forge.sdk.db.repositories.tasks import TasksRepository+from skyvern.forge.sdk.db.agent_db import AgentDB@pytest.fixture@@ -16,18 +16,24 @@ def mock_session():@pytest.fixture-def tasks_repo(mock_session):- repo = TasksRepository.__new__(TasksRepository)- repo.Session = MagicMock(return_value=mock_session)- repo.debug_enabled = False- repo._is_retryable_error_fn = None- return repo+def agent_db(mock_session):+ db = AgentDB.__new__(AgentDB)+ db.Session = MagicMock(return_value=mock_session)+ # Set up tasks repository (sync_task_run_status delegates to self.tasks)+ from skyvern.forge.sdk.db.repositories.tasks import TasksRepository++ tasks = TasksRepository.__new__(TasksRepository)+ tasks.Session = MagicMock(return_value=mock_session)+ tasks.debug_enabled = False+ tasks._is_retryable_error_fn = None+ db.tasks = tasks+ return db@pytest.mark.asyncio-async def test_sync_task_run_status_updates_matching_row(tasks_repo, mock_session):+async def test_sync_task_run_status_updates_matching_row(agent_db, mock_session):"""sync_task_run_status should UPDATE task_runs where run_id matches."""- await tasks_repo.sync_task_run_status(+ await agent_db.sync_task_run_status(organization_id="org_1",run_id="wr_123",status="failed",@@ -41,11 +47,11 @@ async def test_sync_task_run_status_updates_matching_row(tasks_repo, mock_sessio@pytest.mark.asyncio-async def test_sync_task_run_status_no_raise_on_error(tasks_repo, mock_session):+async def test_sync_task_run_status_no_raise_on_error(agent_db, mock_session):"""sync_task_run_status should swallow exceptions (best-effort)."""mock_session.execute.side_effect = Exception("DB error")# Should NOT raise- await tasks_repo.sync_task_run_status(+ await agent_db.sync_task_run_status(organization_id="org_1",run_id="nonexistent",status="failed",diff --git a/tests/unit/test_task_runs_sync_activity.py b/tests/unit/test_task_runs_sync_activity.pynew file mode 100644index 000000000..701b84f4d--- /dev/null+++ b/tests/unit/test_task_runs_sync_activity.py@@ -0,0 +1,200 @@+import importlib.util+import sys+from pathlib import Path+from types import ModuleType, SimpleNamespace+from unittest.mock import AsyncMock, MagicMock++import pytest+from sqlalchemy import Boolean, Column, DateTime, Integer, String, Text+from sqlalchemy.orm import declarative_base+++def _repo_root() -> Path:+ for parent in Path(__file__).resolve().parents:+ if (parent / "pyproject.toml").exists():+ return parent+ raise AssertionError("Could not locate repository root")+++_SOURCE_FILE = _repo_root() / "workers" / "cron_worker" / "task_runs_sync_activity.py"+pytestmark = pytest.mark.skipif(not _SOURCE_FILE.exists(), reason="cloud-only: workers/cron_worker/ not present")+++def _load_task_runs_sync_activity_module(monkeypatch: pytest.MonkeyPatch):+ base = declarative_base()++ class TaskRunModel(base):+ __tablename__ = "task_runs"++ id = Column(Integer, primary_key=True)+ run_id = Column(String)+ task_run_type = Column(String)+ status = Column(String)+ started_at = Column(DateTime)+ finished_at = Column(DateTime)+ script_run = Column(Boolean)+ workflow_permanent_id = Column(String)+ parent_workflow_run_id = Column(String)+ debug_session_id = Column(String)+ searchable_text = Column(Text)+ modified_at = Column(DateTime)+ title = Column(Text)+ url = Column(Text)+ created_at = Column(DateTime)++ class WorkflowRunModel(base):+ __tablename__ = "workflow_runs"++ id = Column(Integer, primary_key=True)+ workflow_run_id = Column(String)+ status = Column(String)+ started_at = Column(DateTime)+ finished_at = Column(DateTime)+ script_run = Column(Boolean)+ workflow_permanent_id = Column(String)+ parent_workflow_run_id = Column(String)+ debug_session_id = Column(String)++ class WorkflowRunParameterModel(base):+ __tablename__ = "workflow_run_parameters"++ id = Column(Integer, primary_key=True)+ workflow_run_id = Column(String)+ value = Column(Text)++ class TaskModel(base):+ __tablename__ = "tasks"++ id = Column(Integer, primary_key=True)+ task_id = Column(String)+ status = Column(String)+ started_at = Column(DateTime)+ finished_at = Column(DateTime)+ title = Column(Text)+ url = Column(Text)++ class TaskV2Model(base):+ __tablename__ = "observer_cruises"++ id = Column(Integer, primary_key=True)+ observer_cruise_id = Column(String)+ status = Column(String)+ started_at = Column(DateTime)+ finished_at = Column(DateTime)+ workflow_run_id = Column(String)+ prompt = Column(Text)++ models_module = ModuleType("skyvern.forge.sdk.db.models")+ models_module.TaskModel = TaskModel+ models_module.TaskRunModel = TaskRunModel+ models_module.TaskV2Model = TaskV2Model+ models_module.WorkflowRunModel = WorkflowRunModel+ models_module.WorkflowRunParameterModel = WorkflowRunParameterModel++ cloud_db_stub = SimpleNamespace(Session=MagicMock())+ cloud_agent_db_module = ModuleType("cloud.db.cloud_agent_db")+ cloud_agent_db_module.cloud_db = cloud_db_stub++ temporalio_module = ModuleType("temporalio")+ temporalio_module.activity = SimpleNamespace(defn=lambda func: func)++ structlog_module = ModuleType("structlog")+ structlog_module.get_logger = lambda: SimpleNamespace(info=lambda *a, **k: None, exception=lambda *a, **k: None)++ runs_module = ModuleType("skyvern.forge.sdk.schemas.runs")+ runs_module.TERMINAL_STATUSES = ("completed", "failed", "terminated", "canceled", "timed_out")++ monkeypatch.setitem(sys.modules, "cloud", ModuleType("cloud"))+ monkeypatch.setitem(sys.modules, "cloud.db", ModuleType("cloud.db"))+ monkeypatch.setitem(sys.modules, "cloud.db.cloud_agent_db", cloud_agent_db_module)+ monkeypatch.setitem(sys.modules, "skyvern", ModuleType("skyvern"))+ monkeypatch.setitem(sys.modules, "skyvern.forge", ModuleType("skyvern.forge"))+ monkeypatch.setitem(sys.modules, "skyvern.forge.sdk", ModuleType("skyvern.forge.sdk"))+ monkeypatch.setitem(sys.modules, "skyvern.forge.sdk.db", ModuleType("skyvern.forge.sdk.db"))+ monkeypatch.setitem(sys.modules, "skyvern.forge.sdk.db.models", models_module)+ monkeypatch.setitem(sys.modules, "skyvern.forge.sdk.schemas", ModuleType("skyvern.forge.sdk.schemas"))+ monkeypatch.setitem(sys.modules, "skyvern.forge.sdk.schemas.runs", runs_module)+ monkeypatch.setitem(sys.modules, "temporalio", temporalio_module)+ monkeypatch.setitem(sys.modules, "structlog", structlog_module)++ module_path = _repo_root() / "workers" / "cron_worker" / "task_runs_sync_activity.py"+ spec = importlib.util.spec_from_file_location("test_task_runs_sync_activity_module", module_path)+ module = importlib.util.module_from_spec(spec)+ assert spec is not None and spec.loader is not None+ spec.loader.exec_module(module)+ return module+++def _mock_session(rowcount: int) -> AsyncMock:+ session = AsyncMock()+ session.__aenter__ = AsyncMock(return_value=session)+ session.__aexit__ = AsyncMock(return_value=False)+ session.execute = AsyncMock(return_value=SimpleNamespace(rowcount=rowcount))+ session.commit = AsyncMock()+ return session+++async def test_task_runs_sync_activity_commits_each_successful_sync(monkeypatch: pytest.MonkeyPatch) -> None:+ task_runs_sync_activity = _load_task_runs_sync_activity_module(monkeypatch)++ workflow_session = _mock_session(2)+ task_session = _mock_session(3)+ task_v2_session = _mock_session(5)++ monkeypatch.setattr(+ task_runs_sync_activity.cloud_db,+ "Session",+ MagicMock(side_effect=[workflow_session, task_session, task_v2_session]),+ )++ results = await task_runs_sync_activity.task_runs_sync_activity()++ assert results == {+ "workflow_runs_synced": 2,+ "tasks_synced": 3,+ "task_v2_synced": 5,+ "errors": [],+ }+ workflow_session.commit.assert_awaited_once()+ task_session.commit.assert_awaited_once()+ task_v2_session.commit.assert_awaited_once()+++async def test_task_runs_sync_activity_handles_partial_failure(monkeypatch: pytest.MonkeyPatch) -> None:+ """If one sync step fails, the others should still succeed."""+ task_runs_sync_activity = _load_task_runs_sync_activity_module(monkeypatch)++ workflow_session = _mock_session(2)+ failing_session = _mock_session(0)+ failing_session.execute = AsyncMock(side_effect=Exception("DB error"))+ task_v2_session = _mock_session(5)++ monkeypatch.setattr(+ task_runs_sync_activity.cloud_db,+ "Session",+ MagicMock(side_effect=[workflow_session, failing_session, task_v2_session]),+ )++ results = await task_runs_sync_activity.task_runs_sync_activity()++ assert results["workflow_runs_synced"] == 2+ assert results["task_v2_synced"] == 5+ assert len(results["errors"]) == 1+ assert "tasks" in results["errors"][0].lower()+++async def test_sync_statements_include_created_at_filter(monkeypatch: pytest.MonkeyPatch) -> None:+ """Verify that all three sync statements include created_at >= cutoff."""+ mod = _load_task_runs_sync_activity_module(monkeypatch)+ from datetime import datetime, timezone++ cutoff = datetime.now(timezone.utc)++ for builder_name in ("_build_sync_workflow_runs_stmt", "_build_sync_tasks_stmt", "_build_sync_task_v2_stmt"):+ builder = getattr(mod, builder_name)+ stmt = builder(cutoff)+ sql_text = str(stmt)+ assert "created_at" in sql_text, f"{builder_name} should filter by created_at"