Skip to content

Automate browser based workflows with AI

SKY-10323: send replMode on main-world Runtime.evaluate to avoid let/const redeclare (#6690)

LawyZheng3799f019fdb7
diff --git a/skyvern/webeye/main_world_eval.py b/skyvern/webeye/main_world_eval.py
index 6a2878301..0d0fa72c3 100644
--- a/skyvern/webeye/main_world_eval.py
+++ b/skyvern/webeye/main_world_eval.py
@@ -43,12 +43,46 @@ def _resolve_prefix(page: Page) -> str | None:
# ``(a = (1+2)) => a``) don't match — current callers don't use that shape.
_ARROW_FN_RE = re.compile(r"^\s*(async\s+)?(\([^()]*\)|\w+)\s*=>")
_FUNCTION_DECL_RE = re.compile(r"^\s*(async\s+)?function\b")
+# After leading whitespace + line/block comments are stripped, does the body
+# begin with a token that re-declaring on a repeated injection would actually
+# fail? Only the lexical bindings (``let``/``const``/``class``) need
+# ``replMode`` — re-declaring a top-level ``var`` or ``function`` is legal
+# in JS, so forcing replMode on them just risks the Chromium
+# ``returnByValue`` -> ``{}`` envelope rewrite for value-returning bodies
+# like ``function rows() {...}; rows()``.
+_TOP_LEVEL_DECL_TOKEN_RE = re.compile(r"(?:let|const|class)\b")
def _is_function_form(expression: str) -> bool:
return bool(_ARROW_FN_RE.match(expression) or _FUNCTION_DECL_RE.match(expression))
+def _starts_with_top_level_declaration(body: str) -> bool:
+ """True if ``body`` begins with a top-level declaration token after any
+ leading whitespace, line (``//``) and block (``/* */``) comments."""
+ i = 0
+ n = len(body)
+ while i < n:
+ ch = body[i]
+ if ch in " \t\r\n":
+ i += 1
+ continue
+ if body.startswith("//", i):
+ nl = body.find("\n", i + 2)
+ if nl == -1:
+ return False
+ i = nl + 1
+ continue
+ if body.startswith("/*", i):
+ end = body.find("*/", i + 2)
+ if end == -1:
+ return False
+ i = end + 2
+ continue
+ break
+ return bool(_TOP_LEVEL_DECL_TOKEN_RE.match(body, i))
+
+
def _extract_runtime_result(result: dict[str, Any]) -> Any:
# CDP returns ``value`` for JSON-serialisable types; unserialisable primitives
# (NaN/Infinity/-0/BigInt) fall through to None, matching what page.evaluate
@@ -75,7 +109,8 @@ async def evaluate_in_main_world(page: Page, expression: str, arg: Any = None) -
return await page.evaluate(expression)
return await page.evaluate(expression, arg)
- if _is_function_form(expression):
+ is_function_form = _is_function_form(expression)
+ if is_function_form:
if arg is None:
wrapped = f"({expression})()"
else:
@@ -83,16 +118,23 @@ async def evaluate_in_main_world(page: Page, expression: str, arg: Any = None) -
else:
wrapped = expression
body = f"{prefix}\n{wrapped}"
+ # ``replMode`` only for statement-form bodies that begin with a lexical
+ # top-level declaration (``let``/``const``/``class``). Bare expressions
+ # and value-returning forms must skip it — Chromium rewrites their
+ # ``returnByValue`` envelope to ``{}`` under replMode. The check runs
+ # against ``body`` so a prefix that itself carries declarations is
+ # covered too.
+ needs_repl_mode = not is_function_form and _starts_with_top_level_declaration(body)
cdp_session = await page.context.new_cdp_session(page)
try:
- result = await cdp_session.send(
- "Runtime.evaluate",
- {
- "expression": body,
- "returnByValue": True,
- "awaitPromise": True,
- },
- )
+ params: dict[str, Any] = {
+ "expression": body,
+ "returnByValue": True,
+ "awaitPromise": True,
+ }
+ if needs_repl_mode:
+ params["replMode"] = True
+ result = await cdp_session.send("Runtime.evaluate", params)
finally:
with contextlib.suppress(Exception):
await cdp_session.detach()
diff --git a/tests/unit/webeye/test_main_world_eval.py b/tests/unit/webeye/test_main_world_eval.py
index 38d49c06d..2fe47230e 100644
--- a/tests/unit/webeye/test_main_world_eval.py
+++ b/tests/unit/webeye/test_main_world_eval.py
@@ -273,6 +273,219 @@ class TestRuntimeResultDecoding:
await evaluate_in_main_world(page, "() => 1")
+class TestRedeclarationSafety:
+ @pytest.mark.asyncio
+ async def test_runtime_evaluate_request_carries_repl_mode(self) -> None:
+ page, _ = _mock_page(prefix="// P")
+ page.context.new_cdp_session.return_value.send = AsyncMock(return_value={"result": {"value": None}})
+
+ await evaluate_in_main_world(page, "let x = 1;")
+
+ params = page.context.new_cdp_session.return_value.send.await_args.args[1]
+ assert params.get("replMode") is True
+
+ @pytest.mark.asyncio
+ async def test_function_form_does_not_carry_repl_mode(self) -> None:
+ # Protocol invariant: function-form IIFEs must not carry replMode.
+ # Chromium rewrites returnByValue async-IIFE array results to {} under
+ # replMode, which surfaces in Python as "not enough values to unpack".
+ page, _ = _mock_page(prefix="// P")
+ page.context.new_cdp_session.return_value.send = AsyncMock(return_value={"result": {"value": 1}})
+
+ await evaluate_in_main_world(page, "() => 1")
+
+ params = page.context.new_cdp_session.return_value.send.await_args.args[1]
+ assert "replMode" not in params
+
+ @pytest.mark.asyncio
+ async def test_repl_mode_sent_on_every_call_not_just_first(self) -> None:
+ page, _ = _mock_page(prefix="// P")
+ page.context.new_cdp_session.return_value.send = AsyncMock(return_value={"result": {"value": None}})
+
+ await evaluate_in_main_world(page, "let foo = 1;")
+ await evaluate_in_main_world(page, "let foo = 1;")
+
+ send_mock = page.context.new_cdp_session.return_value.send
+ assert send_mock.await_count == 2
+ for call in send_mock.await_args_list:
+ params = call.args[1]
+ assert params.get("replMode") is True
+
+ @pytest.mark.asyncio
+ async def test_repl_mode_set_for_top_level_class_declaration_after_comment(self) -> None:
+ # Lexical declarations (``let``/``const``/``class``) still trigger
+ # replMode after the comment-strip; the helper-script injection
+ # relies on this to safely redeclare on a repeated call.
+ page, _ = _mock_page(prefix="// P")
+ page.context.new_cdp_session.return_value.send = AsyncMock(return_value={"result": {"value": None}})
+
+ await evaluate_in_main_world(page, "// header comment\nclass Foo { constructor() { this.x = 1; } }")
+
+ params = page.context.new_cdp_session.return_value.send.await_args.args[1]
+ assert params.get("replMode") is True
+
+ @pytest.mark.asyncio
+ async def test_repl_mode_set_for_top_level_const_declaration(self) -> None:
+ page, _ = _mock_page(prefix="// P")
+ page.context.new_cdp_session.return_value.send = AsyncMock(return_value={"result": {"value": None}})
+
+ await evaluate_in_main_world(page, "const x = 1;")
+
+ params = page.context.new_cdp_session.return_value.send.await_args.args[1]
+ assert params.get("replMode") is True
+
+ @pytest.mark.asyncio
+ async def test_repl_mode_not_set_for_top_level_function_declaration(self) -> None:
+ # Top-level ``function`` re-declaration is legal in JS, so it does NOT
+ # need replMode. A value-returning script like
+ # ``function rows(){ return [1,2]; }; rows()`` must round-trip cleanly
+ # without the Chromium ``returnByValue`` -> ``{}`` envelope rewrite.
+ page, _ = _mock_page(prefix="// P")
+ page.context.new_cdp_session.return_value.send = AsyncMock(return_value={"result": {"value": [1, 2]}})
+
+ await evaluate_in_main_world(page, "function rows(){ return [1, 2]; }; rows()")
+
+ params = page.context.new_cdp_session.return_value.send.await_args.args[1]
+ assert "replMode" not in params
+
+ @pytest.mark.asyncio
+ async def test_repl_mode_not_set_for_bare_object_literal_expression(self) -> None:
+ # Bare expressions (object/array literals) must not take replMode —
+ # Chromium rewrites their returnByValue envelopes under replMode.
+ page, _ = _mock_page(prefix="// P")
+ page.context.new_cdp_session.return_value.send = AsyncMock(return_value={"result": {"value": {"foo": 1}}})
+
+ await evaluate_in_main_world(page, "({foo: 1})")
+
+ params = page.context.new_cdp_session.return_value.send.await_args.args[1]
+ assert "replMode" not in params
+
+ @pytest.mark.asyncio
+ async def test_repl_mode_not_set_for_bare_array_literal_expression(self) -> None:
+ page, _ = _mock_page(prefix="// P")
+ page.context.new_cdp_session.return_value.send = AsyncMock(return_value={"result": {"value": [1, 2, 3]}})
+
+ await evaluate_in_main_world(page, "[1, 2, 3]")
+
+ params = page.context.new_cdp_session.return_value.send.await_args.args[1]
+ assert "replMode" not in params
+
+ @pytest.mark.asyncio
+ async def test_repl_mode_not_set_for_top_level_await_resolving_to_array(self) -> None:
+ # Reachable via ExecuteJsAction / streaming evaluate_js; top-level
+ # ``await`` that resolves to an array must NOT carry replMode, or
+ # Chromium will mangle the returnByValue envelope to ``{}``.
+ page, _ = _mock_page(prefix="// P")
+ page.context.new_cdp_session.return_value.send = AsyncMock(return_value={"result": {"value": [1, 2]}})
+
+ await evaluate_in_main_world(page, "await Promise.resolve([1, 2])")
+
+ params = page.context.new_cdp_session.return_value.send.await_args.args[1]
+ assert "replMode" not in params
+
+ @pytest.mark.asyncio
+ async def test_repl_mode_not_set_for_top_level_var_declaration(self) -> None:
+ # ``var`` redeclaration is legal in JS, so it doesn't need replMode.
+ # A value-returning script like ``var rows = [1, 2]; rows`` must
+ # round-trip cleanly without the returnByValue->{} rewrite.
+ page, _ = _mock_page(prefix="// P")
+ page.context.new_cdp_session.return_value.send = AsyncMock(return_value={"result": {"value": [1, 2]}})
+
+ await evaluate_in_main_world(page, "var rows = [1, 2]; rows")
+
+ params = page.context.new_cdp_session.return_value.send.await_args.args[1]
+ assert "replMode" not in params
+ assert params["expression"] == "// P\nvar rows = [1, 2]; rows"
+
+
+class TestReplModeAgainstMarkerPrefix:
+ """A configured marker prefix is just an opaque comment; pin that the
+ declaration check correctly sees through it so:
+ * helper-script injection (leads with a lexical declaration) keeps
+ carrying replMode;
+ * bare value-returning expressions under the same prefix do NOT.
+ Also covers the forward-defensive case where a prefix itself carries
+ top-level declarations (single-call ``Runtime.evaluate`` can't avoid
+ declaration collisions on repeated injection without replMode there,
+ so we accept the replMode envelope risk for value-returning expressions
+ in that hypothetical config — see the comment in main_world_eval.py)."""
+
+ MARKER = "// @marker"
+ HELPER_SCRIPT_SHAPE = (
+ "// leading comment\n"
+ "let workaroundName = 'chromium';\n"
+ "class Counter { constructor() { this.value = 0; } }\n"
+ "function buildTree() { return []; }\n"
+ )
+
+ @pytest.mark.asyncio
+ async def test_helper_script_injection_under_marker_prefix_sets_repl_mode(self) -> None:
+ page, _ = _mock_page(prefix=self.MARKER)
+ page.context.new_cdp_session.return_value.send = AsyncMock(return_value={"result": {"value": None}})
+
+ await evaluate_in_main_world(page, self.HELPER_SCRIPT_SHAPE)
+
+ params = page.context.new_cdp_session.return_value.send.await_args.args[1]
+ assert params.get("replMode") is True
+ assert params["expression"].startswith(self.MARKER + "\n")
+
+ @pytest.mark.asyncio
+ async def test_helper_script_injection_can_be_repeated_under_marker_prefix(self) -> None:
+ # Two consecutive injections of the helper-script shape must both
+ # carry replMode so Chromium accepts the redeclared bindings.
+ page, _ = _mock_page(prefix=self.MARKER)
+ page.context.new_cdp_session.return_value.send = AsyncMock(return_value={"result": {"value": None}})
+
+ await evaluate_in_main_world(page, self.HELPER_SCRIPT_SHAPE)
+ await evaluate_in_main_world(page, self.HELPER_SCRIPT_SHAPE)
+
+ send_mock = page.context.new_cdp_session.return_value.send
+ assert send_mock.await_count == 2
+ for call in send_mock.await_args_list:
+ assert call.args[1].get("replMode") is True
+
+ @pytest.mark.asyncio
+ async def test_bare_object_literal_under_marker_prefix_does_not_carry_repl_mode(self) -> None:
+ page, _ = _mock_page(prefix=self.MARKER)
+ page.context.new_cdp_session.return_value.send = AsyncMock(return_value={"result": {"value": {"foo": 1}}})
+
+ await evaluate_in_main_world(page, "({foo: 1})")
+
+ params = page.context.new_cdp_session.return_value.send.await_args.args[1]
+ assert "replMode" not in params
+ assert params["expression"] == f"{self.MARKER}\n({{foo: 1}})"
+
+ @pytest.mark.asyncio
+ async def test_top_level_await_array_under_marker_prefix_does_not_carry_repl_mode(self) -> None:
+ page, _ = _mock_page(prefix=self.MARKER)
+ page.context.new_cdp_session.return_value.send = AsyncMock(return_value={"result": {"value": [1, 2]}})
+
+ await evaluate_in_main_world(page, "await Promise.resolve([1, 2])")
+
+ params = page.context.new_cdp_session.return_value.send.await_args.args[1]
+ assert "replMode" not in params
+
+ @pytest.mark.asyncio
+ async def test_declaration_in_prefix_forces_repl_mode_even_when_expression_is_bare_expression(
+ self,
+ ) -> None:
+ # Forward-defensive: if a future caller registers a prefix that itself
+ # carries top-level declarations, repeated invocation would otherwise
+ # surface ``Identifier ... has already been declared``. We choose
+ # replMode here so the prefix can re-inject safely; the cost is that
+ # value-returning expressions on this hypothetical config may see the
+ # Chromium ``{}`` envelope rewrite, which the caller accepts when they
+ # opt into a declaration-bearing prefix.
+ declaration_prefix = "let __probe__ = 1;"
+ page, _ = _mock_page(prefix=declaration_prefix)
+ page.context.new_cdp_session.return_value.send = AsyncMock(return_value={"result": {"value": {"foo": 1}}})
+
+ await evaluate_in_main_world(page, "({foo: 1})")
+
+ params = page.context.new_cdp_session.return_value.send.await_args.args[1]
+ assert params.get("replMode") is True
+
+
class TestPrefixRegistry:
def test_configure_and_get_round_trip(self) -> None:
context = _MockBrowserContext()
diff --git a/tests/unit/webeye/test_skyvern_frame_main_world_routing.py b/tests/unit/webeye/test_skyvern_frame_main_world_routing.py
index 303c0c999..4dee9c234 100644
--- a/tests/unit/webeye/test_skyvern_frame_main_world_routing.py
+++ b/tests/unit/webeye/test_skyvern_frame_main_world_routing.py
@@ -149,6 +149,62 @@ class TestSkyvernFrameEvaluateRouting:
clear_main_world_prefix(ctx) # type: ignore[arg-type]
+class TestRepeatedHelperInjectionRegression:
+ @pytest.mark.asyncio
+ async def test_repeated_helper_injection_via_runtime_evaluate_sets_repl_mode(self) -> None:
+ page, ctx = _make_page_mock(prefix="// MARK")
+ page.context.new_cdp_session.return_value.send = AsyncMock( # type: ignore[attr-defined]
+ return_value={"result": {"value": None}}
+ )
+
+ script_with_top_level_let = "let browserNameForWorkarounds = 'chromium';\nfunction helper() { return 1; }"
+ try:
+ await SkyvernFrame.evaluate(page, script_with_top_level_let)
+ await SkyvernFrame.evaluate(page, script_with_top_level_let)
+ finally:
+ clear_main_world_prefix(ctx) # type: ignore[arg-type]
+
+ send_mock = page.context.new_cdp_session.return_value.send # type: ignore[attr-defined]
+ assert send_mock.await_count == 2
+ for call in send_mock.await_args_list:
+ method, params = call.args
+ assert method == "Runtime.evaluate"
+ assert params.get("replMode") is True
+
+ @pytest.mark.asyncio
+ async def test_repeated_injection_succeeds_against_redeclare_simulating_cdp(self) -> None:
+ page, ctx = _make_page_mock(prefix="// MARK")
+
+ success_sentinel = "second-call-also-returned"
+
+ async def fake_send(method: str, params: dict[str, object]) -> dict[str, object]:
+ if not params.get("replMode"):
+ return {
+ "exceptionDetails": {
+ "text": "Uncaught",
+ "exception": {
+ "description": "SyntaxError: Identifier 'foo' has already been declared",
+ },
+ }
+ }
+ return {"result": {"value": success_sentinel}}
+
+ page.context.new_cdp_session.return_value.send = AsyncMock(side_effect=fake_send) # type: ignore[attr-defined]
+
+ try:
+ first = await SkyvernFrame.evaluate(page, "let foo = 1;")
+ second = await SkyvernFrame.evaluate(page, "let foo = 1;")
+ finally:
+ clear_main_world_prefix(ctx) # type: ignore[arg-type]
+
+ # Both calls returning the sentinel proves no SyntaxError was raised on
+ # the second injection — the regression decoder would have surfaced the
+ # redeclare as RuntimeError before we got a value back.
+ assert first == success_sentinel
+ assert second == success_sentinel
+ assert page.context.new_cdp_session.return_value.send.await_count == 2 # type: ignore[attr-defined]
+
+
class TestNavigationRecoveryRouting:
"""Recovery loop must keep using the main-world hook for prefixed Pages,
otherwise the marker is dropped on the post-navigation re-injection + retry."""