Skip to content

Automate browser based workflows with AI

feat: secure CodeBlock runner package + worker integration (3/3) (#6811)

Shuchang Zhengb97d19648226
diff --git a/skyvern/exceptions.py b/skyvern/exceptions.py
index 1d8117de7..9d7591da4 100644
--- a/skyvern/exceptions.py
+++ b/skyvern/exceptions.py
@@ -1292,3 +1292,11 @@ class ImaginarySecretValue(SkyvernException):
super().__init__(
f"The value {value} is imaginary. Try to double-check to see if this value is included in the provided information"
)
+
+
+class CodeBlockRunnerSelectionError(SkyvernException):
+ """Raised when the secure CodeBlock runner selection policy cannot be evaluated safely.
+
+ The block-execution call site catches this and fails the block closed instead of
+ silently falling back to in-process execution.
+ """
diff --git a/skyvern/forge/agent_functions.py b/skyvern/forge/agent_functions.py
index 607aaa336..e83abd141 100644
--- a/skyvern/forge/agent_functions.py
+++ b/skyvern/forge/agent_functions.py
@@ -51,6 +51,7 @@ from skyvern.webeye.utils.page import SkyvernFrame
if TYPE_CHECKING:
from skyvern.forge.sdk.db.enums import WorkflowRunTriggerType
+ from skyvern.forge.sdk.workflow.context_manager import WorkflowRunContext
from skyvern.forge.sdk.workflow.models.workflow import Workflow, WorkflowRun, WorkflowRunStatus
from skyvern.services.otp_service import OTPValue
@@ -699,6 +700,43 @@ class AgentFunction:
) -> bool:
return True
+ async def should_use_codeblock_runner(
+ self,
+ *,
+ workflow_run_id: str,
+ workflow_run_block_id: str,
+ workflow_run_context: "WorkflowRunContext",
+ organization_id: str | None,
+ block_label: str | None,
+ ) -> bool:
+ """Whether a workflow CodeBlock run should execute in the secure runner sidecar.
+
+ Gating lives here, at the block-execution call site, rather than inside
+ execute_code_block_override so the override only runs the runner. OSS has no
+ runner and returns False; cloud overrides to consult SECURE_CODEBLOCK_ENABLED.
+ """
+ return False
+
+ async def execute_code_block_override(
+ self,
+ *,
+ block: Any,
+ workflow_run_id: str,
+ workflow_run_block_id: str,
+ organization_id: str | None,
+ browser_session_id: str | None,
+ workflow_run_context: "WorkflowRunContext",
+ parameter_values: dict[str, Any],
+ credential_parameter_keys: set[str],
+ ) -> Any | None:
+ """Run a CodeBlock through the secure runner sidecar, or return None for legacy.
+
+ OSS no-op returns None so callers fall through to in-process execution. Cloud
+ overrides to dispatch to the runner. Callers must gate on
+ should_use_codeblock_runner first.
+ """
+ return None
+
async def is_workflow_tagging_enabled(self, organization_id: str) -> bool:
"""OSS always-on; cloud overrides to gate per-org for staged rollout."""
return True
diff --git a/skyvern/forge/sdk/workflow/models/block.py b/skyvern/forge/sdk/workflow/models/block.py
index f76f7dd03..ae9e81afa 100644
--- a/skyvern/forge/sdk/workflow/models/block.py
+++ b/skyvern/forge/sdk/workflow/models/block.py
@@ -49,6 +49,7 @@ from skyvern.constants import (
)
from skyvern.exceptions import (
AzureConfigurationError,
+ CodeBlockRunnerSelectionError,
ConditionalBranchEvaluationError,
ContextParameterValueNotFound,
DownloadFileMaxSizeExceeded,
@@ -4038,6 +4039,7 @@ async def wrapper({default_args}):
# get all parameters into a dictionary
parameter_values = {}
+ credential_parameter_keys: set[str] = set()
for parameter in self.parameters:
value = workflow_run_context.get_value(parameter.key)
if not parameter.parameter_type.is_secret_or_credential() and not (
@@ -4057,6 +4059,7 @@ async def wrapper({default_args}):
)
parameter_values[parameter.key] = value
continue
+ credential_parameter_keys.add(parameter.key)
if isinstance(value, dict):
real_secret_values = {}
for credential_field, credential_place_holder in value.items():
@@ -4092,6 +4095,37 @@ async def wrapper({default_args}):
secret_value = workflow_run_context.get_original_secret_value_or_none(value)
parameter_values[parameter.key] = secret_value if secret_value is not None else value
+ try:
+ use_codeblock_runner = await app.AGENT_FUNCTION.should_use_codeblock_runner(
+ workflow_run_id=workflow_run_id,
+ workflow_run_block_id=workflow_run_block_id,
+ workflow_run_context=workflow_run_context,
+ organization_id=organization_id,
+ block_label=self.label,
+ )
+ except CodeBlockRunnerSelectionError as selection_error:
+ return await self.build_block_result(
+ success=False,
+ failure_reason=str(selection_error),
+ output_parameter_value=None,
+ status=BlockStatus.failed,
+ workflow_run_block_id=workflow_run_block_id,
+ organization_id=organization_id,
+ )
+ if use_codeblock_runner:
+ secure_code_block_result = await app.AGENT_FUNCTION.execute_code_block_override(
+ block=self,
+ workflow_run_id=workflow_run_id,
+ workflow_run_block_id=workflow_run_block_id,
+ organization_id=organization_id,
+ browser_session_id=browser_session_id,
+ workflow_run_context=workflow_run_context,
+ parameter_values=parameter_values,
+ credential_parameter_keys=credential_parameter_keys,
+ )
+ if secure_code_block_result is not None:
+ return secure_code_block_result
+
workflow_run_block = None
# A prompt-bearing code block gets a task v1 + step so its recorded calls render through
diff --git a/tests/unit/force_stub_app.py b/tests/unit/force_stub_app.py
index d115e1168..d705f787b 100644
--- a/tests/unit/force_stub_app.py
+++ b/tests/unit/force_stub_app.py
@@ -21,6 +21,11 @@ def create_forge_stub_app() -> ForgeApp:
fake_app_module.AGENT_FUNCTION = _LazyNamespace()
fake_app_module.AGENT_FUNCTION.validate_block_execution = AsyncMock()
fake_app_module.AGENT_FUNCTION.validate_code_block = AsyncMock()
+ # Secure CodeBlock runner gating — _LazyNamespace would auto-mock these as
+ # truthy AsyncMocks and hijack CodeBlock.execute into the runner path. Match
+ # the real OSS base no-op so unit tests exercise the legacy in-process path.
+ fake_app_module.AGENT_FUNCTION.should_use_codeblock_runner = AsyncMock(return_value=False)
+ fake_app_module.AGENT_FUNCTION.execute_code_block_override = AsyncMock(return_value=None)
# Sync method — _LazyNamespace would auto-mock it as AsyncMock and break
# callers that unpack the return value. Match the real OSS default (None).
fake_app_module.AGENT_FUNCTION.resolve_mcp_oauth_org_lookups = MagicMock(return_value=None)
diff --git a/tests/unit/test_code_block_downloads.py b/tests/unit/test_code_block_downloads.py
index 87cc85fb9..3c66b64dd 100644
--- a/tests/unit/test_code_block_downloads.py
+++ b/tests/unit/test_code_block_downloads.py
@@ -94,7 +94,12 @@ def _wire_block_runtime(
def _fake_storage_app(monkeypatch: pytest.MonkeyPatch, *, save, get) -> None:
fake_app = SimpleNamespace(
STORAGE=SimpleNamespace(save_downloaded_files=save, get_downloaded_files=get),
- AGENT_FUNCTION=SimpleNamespace(validate_code_block=AsyncMock()),
+ AGENT_FUNCTION=SimpleNamespace(
+ validate_code_block=AsyncMock(),
+ # Secure CodeBlock runner gating — match the OSS base no-op so execute() runs legacy.
+ should_use_codeblock_runner=AsyncMock(return_value=False),
+ execute_code_block_override=AsyncMock(return_value=None),
+ ),
)
monkeypatch.setattr(block_module, "app", fake_app)