Skip to content

Automate browser based workflows with AI

feat(SKY-11677): navigate dead-nav self-heal seats to the authored goto target (#7246)

pedrohsdb0ee505b24b00
diff --git a/skyvern/forge/sdk/workflow/models/block.py b/skyvern/forge/sdk/workflow/models/block.py
index 0eb8f8d57..4eff7049a 100644
--- a/skyvern/forge/sdk/workflow/models/block.py
+++ b/skyvern/forge/sdk/workflow/models/block.py
@@ -3940,6 +3940,95 @@ async def wrapper({default_args}):
best_start = step.line_start
return best
+ def _static_goto_url_from_line(self, line: str) -> str | None:
+ # AST-only on purpose: substring/regex matching would also fire on comments and string
+ # literals containing ".goto(", handing element-rot heals a URL they must never get.
+ stripped = line.strip()
+ if not stripped:
+ return None
+ try:
+ # Parses one physical line at a time, so a goto(...) call whose args wrap onto another
+ # line SyntaxErrors here and is treated as "no goto on this line" (safe, not a false match).
+ tree = ast.parse(f"async def _probe():\n {stripped}", mode="exec")
+ except SyntaxError:
+ return None
+ for node in ast.walk(tree):
+ if not isinstance(node, ast.Call):
+ continue
+ if not isinstance(node.func, ast.Attribute) or node.func.attr != "goto":
+ continue
+ # Supported authoring forms are positional (page.goto("...")) and keyword
+ # (page.goto(url="...")); check positional first, then fall back to url=.
+ if node.args:
+ url_node: ast.expr | None = node.args[0]
+ else:
+ url_node = next((keyword.value for keyword in node.keywords if keyword.arg == "url"), None)
+ if url_node is None:
+ return ""
+ if not isinstance(url_node, ast.Constant) or not isinstance(url_node.value, str):
+ return ""
+ parsed = urlparse(url_node.value)
+ return url_node.value if parsed.scheme in {"http", "https"} and parsed.netloc else ""
+ return None
+
+ def _static_url_from_goal(self) -> str:
+ # The goal is human free text (prompt + step descriptions), not code, so a URL regex is
+ # appropriate here (unlike the AST-only code scan). This is the authored destination the
+ # heal should reach when the block's own navigation rotted. Returns the first well-formed
+ # absolute http(s) URL, else "".
+ texts = [self.prompt or ""]
+ if self.steps:
+ texts.extend(step.description or "" for step in self.steps)
+ for text in texts:
+ match = re.search(r"https?://[^\s'\"<>)\]]+", text)
+ if not match:
+ continue
+ candidate = match.group(0).rstrip(".,;")
+ parsed = urlparse(candidate)
+ if parsed.scheme in {"http", "https"} and parsed.netloc:
+ return candidate
+ return ""
+
+ def _derive_escalation_navigation_url(self, failing_line: int, recording_page: RecordingPage) -> str:
+ code_lines = self.code.splitlines()
+ failing_idx = failing_line - 1
+ failing_source_line = code_lines[failing_idx] if 0 <= failing_idx < len(code_lines) else ""
+
+ # None = no goto on the failing line; "" = a goto with a non-static arg; a str = static url.
+ failing_goto_url = self._static_goto_url_from_line(failing_source_line)
+ failing_line_is_goto = failing_goto_url is not None
+
+ try:
+ page_url = recording_page.url
+ except Exception:
+ page_url = None
+ current_url = page_url if isinstance(page_url, str) else None
+ is_error_seat = bool(current_url and current_url.startswith("chrome-error://"))
+
+ # Only a navigation-failure / error-page seat gets navigation recourse — element-rot heals
+ # must never navigate away from live SPA state (H8).
+ if not (failing_line_is_goto or is_error_seat):
+ return ""
+
+ # The block goal is the source of truth for the intended destination; the code's own goto is
+ # what rotted (e.g. the host went dead), so prefer a URL the goal names over re-navigating to
+ # the failing goto's possibly-dead target. Fall back to the code goto only when the goal
+ # names no static URL (a real goto that merely failed transiently is still worth a retry).
+ goal_url = self._static_url_from_goal()
+ if goal_url:
+ return goal_url
+
+ if failing_line_is_goto:
+ return failing_goto_url or ""
+
+ # Error-page seat with a non-goto failing line: walk backward for the nearest STATIC goto,
+ # not the first in the block — an earlier goto can be stale, a later one hasn't executed.
+ for line in reversed(code_lines[:failing_idx]):
+ line_url = self._static_goto_url_from_line(line)
+ if line_url:
+ return line_url
+ return ""
+
async def _self_heal_enabled(self, workflow_run_context: WorkflowRunContext) -> bool:
# User-facing per-workflow setting, restricted to copilot-authored workflows —
# pre-copilot code blocks must never gain agentic recovery from the toggle alone.
@@ -4043,6 +4132,8 @@ async def wrapper({default_args}):
workflow_run_block_id: str,
organization_id: str | None,
browser_session_id: str | None,
+ browser_state: BrowserState,
+ page: Page,
) -> BlockResult | None:
"""Run one bounded agent mini-run on the same workflow-run browser to finish the block's goal
(narrowed to the failing step when one is confidently matched). Returns a BlockResult when a
@@ -4096,10 +4187,26 @@ async def wrapper({default_args}):
heal_max_steps = settings.MAX_STEPS_PER_RUN
if organization.max_steps_per_run is not None:
heal_max_steps = min(heal_max_steps, organization.max_steps_per_run)
- # Blank url: the heal takes over the live, half-mutated page rather than re-navigating to it
- # (a truthy task.url makes the browser manager reload the page on the browser-session path).
+ escalation_url = (
+ self._derive_escalation_navigation_url(failing_line, recording_page) if failing_line is not None else ""
+ )
+ if escalation_url:
+ # BROWSER_MANAGER can early-return a cached browser state without ever reading
+ # task.url, so escalation_task.url alone is not reliable navigation recourse.
+ # Drive the live browser_state/page the heal will run on directly instead.
+ try:
+ await browser_state.navigate_to_url(page=page, url=escalation_url)
+ except Exception:
+ LOG.warning(
+ "Self-heal dead-nav escalation navigation failed; continuing from current page",
+ workflow_run_block_id=workflow_run_block_id,
+ workflow_run_id=workflow_run_id,
+ exc_info=True,
+ )
+ # Kept in sync with the direct navigate above for task-record consistency; empty except
+ # on dead-navigation seats (element-rot heals must never navigate — H8 same-session invariant).
escalation_task = await app.DATABASE.tasks.create_task(
- url="",
+ url=escalation_url,
title=self.label,
navigation_goal=navigation_goal,
data_extraction_goal=None,
@@ -4473,6 +4580,8 @@ async def wrapper({default_args}):
workflow_run_block_id=workflow_run_block_id,
organization_id=organization_id,
browser_session_id=browser_session_id,
+ browser_state=browser_state,
+ page=page,
)
if healed is not None:
# Finalize the seat task to the heal outcome before the idempotent `finally` no-ops it,
diff --git a/tests/unit/workflow/test_code_block_escalation.py b/tests/unit/workflow/test_code_block_escalation.py
index 15f83e9c7..27e33fcdf 100644
--- a/tests/unit/workflow/test_code_block_escalation.py
+++ b/tests/unit/workflow/test_code_block_escalation.py
@@ -31,7 +31,11 @@ DEFAULT_PROMPT = "Log in and download the report"
ExtractedInformation = list[Any] | dict[str, Any] | str | None
-def _make_code_block(steps: list[CodeBlockStep] | None = None, prompt: str | None = DEFAULT_PROMPT) -> CodeBlock:
+def _make_code_block(
+ steps: list[CodeBlockStep] | None = None,
+ prompt: str | None = DEFAULT_PROMPT,
+ code: str = "await page.click('#missing')",
+) -> CodeBlock:
now = datetime.now(timezone.utc)
output_parameter = OutputParameter(
parameter_type=ParameterType.OUTPUT,
@@ -44,7 +48,7 @@ def _make_code_block(steps: list[CodeBlockStep] | None = None, prompt: str | Non
)
return CodeBlock(
label="code_1",
- code="await page.click('#missing')",
+ code=code,
prompt=prompt,
steps=steps,
output_parameter=output_parameter,
@@ -183,12 +187,19 @@ def _install_db_fakes(
return state
-def _recording_page(exception: Exception | None) -> MagicMock:
+def _recording_page(exception: Exception | None, *, url: object = "http://example.test/home") -> MagicMock:
page = MagicMock()
page.last_recorded_exception = MagicMock(return_value=exception)
+ page.url = url
return page
+def _browser_state() -> MagicMock:
+ browser_state = MagicMock()
+ browser_state.navigate_to_url = AsyncMock(return_value=None)
+ return browser_state
+
+
async def _heal(
block: CodeBlock,
context: WorkflowRunContext,
@@ -196,6 +207,8 @@ async def _heal(
recording_page: MagicMock,
*,
failing_line: int | None = 1,
+ browser_state: MagicMock | None = None,
+ page: MagicMock | None = None,
) -> BlockResult | None:
with skyvern_context.scoped(SkyvernContext(organization_id="o_test", workflow_run_id="wr_test")):
return await block._attempt_self_heal(
@@ -207,6 +220,8 @@ async def _heal(
workflow_run_block_id="wrb_test",
organization_id="o_test",
browser_session_id=None,
+ browser_state=browser_state if browser_state is not None else _browser_state(),
+ page=page if page is not None else MagicMock(),
)
@@ -367,6 +382,313 @@ async def test_matched_step_narrows_the_goal(monkeypatch: pytest.MonkeyPatch) ->
assert DEFAULT_PROMPT in goal
+async def test_failing_static_goto_sets_escalation_url(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr("skyvern.config.settings.ENABLE_CODE_BLOCK_SELF_HEALING", True, raising=False)
+ monkeypatch.setattr(CodeBlock, "record_output_parameter_value", AsyncMock(return_value=None))
+ state = _install_db_fakes(monkeypatch, final_status=TaskStatus.completed)
+ target_url = "https://dead-nav.example.com/login"
+ block = _make_code_block(code=f'await page.goto("{target_url}")')
+ exc = RuntimeError("navigation failed")
+
+ result = await _heal(block, _make_context(), exc, _recording_page(exc))
+
+ assert result is not None and result.success is True
+ assert state["create_task_kwargs"]["url"] == target_url
+
+
+async def test_failing_goto_with_variable_keeps_empty_escalation_url(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr("skyvern.config.settings.ENABLE_CODE_BLOCK_SELF_HEALING", True, raising=False)
+ monkeypatch.setattr(CodeBlock, "record_output_parameter_value", AsyncMock(return_value=None))
+ state = _install_db_fakes(monkeypatch, final_status=TaskStatus.completed)
+ block = _make_code_block(
+ code="""
+target = "https://dead-nav.example.com/login"
+await page.goto(target)
+""".strip()
+ )
+ exc = RuntimeError("navigation failed")
+
+ result = await _heal(block, _make_context(), exc, _recording_page(exc), failing_line=2)
+
+ assert result is not None and result.success is True
+ assert state["create_task_kwargs"]["url"] == ""
+
+
+async def test_failing_goto_with_keyword_url_sets_escalation_url(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr("skyvern.config.settings.ENABLE_CODE_BLOCK_SELF_HEALING", True, raising=False)
+ monkeypatch.setattr(CodeBlock, "record_output_parameter_value", AsyncMock(return_value=None))
+ state = _install_db_fakes(monkeypatch, final_status=TaskStatus.completed)
+ target_url = "https://dead-nav.example.com/login"
+ block = _make_code_block(code=f'await page.goto(url="{target_url}")')
+ exc = RuntimeError("navigation failed")
+
+ result = await _heal(block, _make_context(), exc, _recording_page(exc))
+
+ assert result is not None and result.success is True
+ assert state["create_task_kwargs"]["url"] == target_url
+
+
+async def test_failing_goto_with_dynamic_keyword_url_keeps_empty_escalation_url(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ monkeypatch.setattr("skyvern.config.settings.ENABLE_CODE_BLOCK_SELF_HEALING", True, raising=False)
+ monkeypatch.setattr(CodeBlock, "record_output_parameter_value", AsyncMock(return_value=None))
+ state = _install_db_fakes(monkeypatch, final_status=TaskStatus.completed)
+ block = _make_code_block(
+ code="""
+target = "https://dead-nav.example.com/login"
+await page.goto(url=target)
+""".strip()
+ )
+ exc = RuntimeError("navigation failed")
+
+ result = await _heal(block, _make_context(), exc, _recording_page(exc), failing_line=2)
+
+ assert result is not None and result.success is True
+ assert state["create_task_kwargs"]["url"] == ""
+
+
+async def test_element_rot_failure_never_sets_escalation_url(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr("skyvern.config.settings.ENABLE_CODE_BLOCK_SELF_HEALING", True, raising=False)
+ monkeypatch.setattr(CodeBlock, "record_output_parameter_value", AsyncMock(return_value=None))
+ state = _install_db_fakes(monkeypatch, final_status=TaskStatus.completed)
+ block = _make_code_block(code="await page.click('#download')")
+ exc = RuntimeError("click failed")
+
+ result = await _heal(
+ block,
+ _make_context(),
+ exc,
+ _recording_page(exc, url="https://app.example.com/dashboard"),
+ )
+
+ assert result is not None and result.success is True
+ assert state["create_task_kwargs"]["url"] == ""
+
+
+async def test_error_page_seat_uses_first_static_goto_url(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr("skyvern.config.settings.ENABLE_CODE_BLOCK_SELF_HEALING", True, raising=False)
+ monkeypatch.setattr(CodeBlock, "record_output_parameter_value", AsyncMock(return_value=None))
+ state = _install_db_fakes(monkeypatch, final_status=TaskStatus.completed)
+ target_url = "https://dead-nav.example.com/login"
+ block = _make_code_block(
+ code=f"""
+await page.goto("{target_url}")
+await page.click("#download")
+""".strip()
+ )
+ exc = RuntimeError("click failed after dead nav")
+
+ result = await _heal(
+ block,
+ _make_context(),
+ exc,
+ _recording_page(exc, url="chrome-error://chromewebdata/"),
+ failing_line=2,
+ )
+
+ assert result is not None and result.success is True
+ assert state["create_task_kwargs"]["url"] == target_url
+
+
+async def test_error_page_seat_skips_dynamic_goto_to_reach_later_static_url(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr("skyvern.config.settings.ENABLE_CODE_BLOCK_SELF_HEALING", True, raising=False)
+ monkeypatch.setattr(CodeBlock, "record_output_parameter_value", AsyncMock(return_value=None))
+ state = _install_db_fakes(monkeypatch, final_status=TaskStatus.completed)
+ target_url = "https://dead-nav.example.com/login"
+ block = _make_code_block(
+ code=f"""
+target = build_url()
+await page.goto(target)
+await page.click("#step1")
+await page.goto("{target_url}")
+await page.click("#download")
+""".strip()
+ )
+ exc = RuntimeError("click failed after dead nav")
+
+ result = await _heal(
+ block,
+ _make_context(),
+ exc,
+ _recording_page(exc, url="chrome-error://chromewebdata/"),
+ failing_line=5,
+ )
+
+ assert result is not None and result.success is True
+ assert state["create_task_kwargs"]["url"] == target_url
+
+
+async def test_error_page_seat_uses_nearest_preceding_goto_not_first_in_block(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """A multi-navigation block must seat recovery on the page the failure actually landed on
+ (the nearest preceding goto), not the first static goto anywhere in the block — an earlier
+ stale goto and a not-yet-executed later goto are both wrong answers here."""
+ monkeypatch.setattr("skyvern.config.settings.ENABLE_CODE_BLOCK_SELF_HEALING", True, raising=False)
+ monkeypatch.setattr(CodeBlock, "record_output_parameter_value", AsyncMock(return_value=None))
+ state = _install_db_fakes(monkeypatch, final_status=TaskStatus.completed)
+ stale_url = "https://stale-first.example.com/old"
+ correct_url = "https://correct-recovery.example.com/target"
+ never_executed_url = "https://never-executed.example.com/later"
+ block = _make_code_block(
+ code=f"""
+await page.goto("{stale_url}")
+await page.click("#step1")
+await page.goto("{correct_url}")
+await page.click("#step2")
+await page.goto("{never_executed_url}")
+""".strip()
+ )
+ exc = RuntimeError("click failed after dead nav")
+
+ result = await _heal(
+ block,
+ _make_context(),
+ exc,
+ _recording_page(exc, url="chrome-error://chromewebdata/"),
+ failing_line=4,
+ )
+
+ assert result is not None and result.success is True
+ assert state["create_task_kwargs"]["url"] == correct_url
+
+
+async def test_dead_nav_seat_navigates_live_page_before_escalation_runs(monkeypatch: pytest.MonkeyPatch) -> None:
+ """escalation_task.url alone does not reliably trigger navigation (BrowserManager can early-return
+ a cached browser state without reading it), so the heal must drive the live browser_state/page
+ directly for a dead-nav seat."""
+ monkeypatch.setattr("skyvern.config.settings.ENABLE_CODE_BLOCK_SELF_HEALING", True, raising=False)
+ monkeypatch.setattr(CodeBlock, "record_output_parameter_value", AsyncMock(return_value=None))
+ _install_db_fakes(monkeypatch, final_status=TaskStatus.completed)
+ target_url = "https://dead-nav.example.com/login"
+ block = _make_code_block(code=f'await page.goto("{target_url}")')
+ exc = RuntimeError("navigation failed")
+ browser_state = _browser_state()
+ live_page = MagicMock(name="live_page")
+
+ result = await _heal(
+ block,
+ _make_context(),
+ exc,
+ _recording_page(exc),
+ browser_state=browser_state,
+ page=live_page,
+ )
+
+ assert result is not None and result.success is True
+ browser_state.navigate_to_url.assert_awaited_once_with(page=live_page, url=target_url)
+
+
+async def test_dead_host_goto_recovers_url_from_goal_not_the_rotted_code(monkeypatch: pytest.MonkeyPatch) -> None:
+ """The canonical dead-nav rot: the block's own goto url is the dead host, and the real
+ destination lives in the goal (prompt/steps). The heal must navigate to the goal's URL, not
+ re-navigate to the code's dead goto (gauntlet H7 — otherwise it just hits the dead host again)."""
+ monkeypatch.setattr("skyvern.config.settings.ENABLE_CODE_BLOCK_SELF_HEALING", True, raising=False)
+ monkeypatch.setattr(CodeBlock, "record_output_parameter_value", AsyncMock(return_value=None))
+ _install_db_fakes(monkeypatch, final_status=TaskStatus.completed)
+ real_url = "http://localhost:8900/telco_billing/northwind/"
+ block = _make_code_block(
+ code='await page.goto("http://localhost:65531/")',
+ prompt=f"Open the billing portal at {real_url} and sign in",
+ )
+ exc = RuntimeError("net::ERR_CONNECTION_REFUSED")
+ browser_state = _browser_state()
+ live_page = MagicMock(name="live_page")
+
+ result = await _heal(
+ block,
+ _make_context(),
+ exc,
+ _recording_page(exc),
+ browser_state=browser_state,
+ page=live_page,
+ )
+
+ assert result is not None and result.success is True
+ browser_state.navigate_to_url.assert_awaited_once_with(page=live_page, url=real_url)
+
+
+async def test_element_rot_seat_never_navigates_live_page(monkeypatch: pytest.MonkeyPatch) -> None:
+ """Element-rot heals must preserve same-session SPA state (gauntlet H8 invariant); the direct
+ navigate call added for dead-nav seats must not fire when no recovery URL was derived."""
+ monkeypatch.setattr("skyvern.config.settings.ENABLE_CODE_BLOCK_SELF_HEALING", True, raising=False)
+ monkeypatch.setattr(CodeBlock, "record_output_parameter_value", AsyncMock(return_value=None))
+ _install_db_fakes(monkeypatch, final_status=TaskStatus.completed)
+ block = _make_code_block(code="await page.click('#download')")
+ exc = RuntimeError("click failed")
+ browser_state = _browser_state()
+
+ result = await _heal(
+ block,
+ _make_context(),
+ exc,
+ _recording_page(exc, url="https://app.example.com/dashboard"),
+ browser_state=browser_state,
+ )
+
+ assert result is not None and result.success is True
+ browser_state.navigate_to_url.assert_not_awaited()
+
+
+async def test_derived_escalation_url_is_not_added_to_goal_text(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr("skyvern.config.settings.ENABLE_CODE_BLOCK_SELF_HEALING", True, raising=False)
+ monkeypatch.setattr(CodeBlock, "record_output_parameter_value", AsyncMock(return_value=None))
+ target_url = "https://dead-nav.example.com/login"
+ block = _make_code_block(code=f'await page.goto("{target_url}")')
+ exc = RuntimeError("navigation failed")
+
+ state_with_url = _install_db_fakes(monkeypatch, final_status=TaskStatus.completed)
+ result_with_url = await _heal(block, _make_context(), exc, _recording_page(exc))
+ goal_with_url = state_with_url["create_task_kwargs"]["navigation_goal"]
+
+ assert result_with_url is not None and result_with_url.success is True
+ assert target_url not in goal_with_url
+
+ element_rot_block = _make_code_block(code="await page.click('#missing')")
+ state_no_url = _install_db_fakes(monkeypatch, final_status=TaskStatus.completed)
+ result_no_url = await _heal(
+ element_rot_block,
+ _make_context(),
+ exc,
+ _recording_page(exc, url="https://app.example.com/dashboard"),
+ )
+
+ assert result_no_url is not None and result_no_url.success is True
+ assert state_no_url["create_task_kwargs"]["url"] == ""
+ assert goal_with_url == state_no_url["create_task_kwargs"]["navigation_goal"]
+
+
+async def test_goto_inside_comment_or_string_never_sets_escalation_url(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr("skyvern.config.settings.ENABLE_CODE_BLOCK_SELF_HEALING", True, raising=False)
+ monkeypatch.setattr(CodeBlock, "record_output_parameter_value", AsyncMock(return_value=None))
+ for code in (
+ 'await page.click("#download") # retry via .goto("https://evil.example.com")',
+ "await page.click('.goto(\"https://evil.example.com\")')",
+ ):
+ block = _make_code_block(code=code)
+ exc = RuntimeError("click failed")
+ state = _install_db_fakes(monkeypatch, final_status=TaskStatus.completed)
+
+ result = await _heal(block, _make_context(), exc, _recording_page(exc))
+
+ assert result is not None and result.success is True
+ assert state["create_task_kwargs"]["url"] == ""
+
+
@pytest.mark.asyncio
async def test_unmapped_playwright_error_is_healed(monkeypatch: pytest.MonkeyPatch) -> None:
# An unmapped Playwright call's exception is never registered as last_recorded_exception, but a