Skip to content

Automate browser based workflows with AI

workday runs onto the deterministic script (#7622)

Cindy Licf0efa907649
diff --git a/skyvern/forge/agent_functions.py b/skyvern/forge/agent_functions.py
index 17fa33e35..7297082c3 100644
--- a/skyvern/forge/agent_functions.py
+++ b/skyvern/forge/agent_functions.py
@@ -802,6 +802,14 @@ class AgentFunction:
) -> bool:
return True
+ async def should_upgrade_to_code_mode(
+ self,
+ *,
+ workflow: "Workflow",
+ workflow_run: "WorkflowRun",
+ ) -> bool:
+ return False
+
async def resolve_mcp_code_only_mode(
self,
organization_id: str | None,
diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.py
index d2eb87c1e..e16745d02 100644
--- a/skyvern/forge/sdk/workflow/service.py
+++ b/skyvern/forge/sdk/workflow/service.py
@@ -9162,12 +9162,24 @@ class WorkflowService:
"""Whether this run should attempt cached-script execution.
Priority: run-level run_with > workflow-level run_with. Intended-code
- runs are then passed through the app-level code-mode gate.
+ runs are then passed through the app-level code-mode gate. Runs not
+ already intended as code may be upgraded by a flag-gated rollout;
+ an upgraded run has no matching script for a non-eligible workflow
+ and so degrades cleanly to agent per block.
"""
if workflow_run.run_with is not None:
intended_code = workflow_run.run_with == "code"
else:
intended_code = workflow.run_with == "code"
+ # A fallback retry has already made a deliberate execution-mode choice; the rollout must not
+ # re-upgrade it to code. Gate on the retry marker rather than `run_with is None`: normal runs
+ # inherit run_with="agent" from the workflow, so keying on None would disable the rollout for
+ # every ordinary run.
+ if not intended_code and workflow_run.retried_from_workflow_run_id is None:
+ intended_code = await app.AGENT_FUNCTION.should_upgrade_to_code_mode(
+ workflow=workflow,
+ workflow_run=workflow_run,
+ )
if not intended_code:
return False
return await app.AGENT_FUNCTION.should_keep_code_mode_for_workflow_run(
diff --git a/tests/unit/test_should_run_script.py b/tests/unit/test_should_run_script.py
index f42573a71..9d712547e 100644
--- a/tests/unit/test_should_run_script.py
+++ b/tests/unit/test_should_run_script.py
@@ -45,7 +45,11 @@ def _make_workflow(
)
-def _make_run(run_with: str | None = "agent", workflow_run_id: str = "wr_test") -> WorkflowRun:
+def _make_run(
+ run_with: str | None = "agent",
+ workflow_run_id: str = "wr_test",
+ retried_from_workflow_run_id: str | None = None,
+) -> WorkflowRun:
return WorkflowRun(
workflow_run_id=workflow_run_id,
workflow_id="wf_test",
@@ -53,6 +57,7 @@ def _make_run(run_with: str | None = "agent", workflow_run_id: str = "wr_test")
organization_id="org_test",
status=WorkflowRunStatus.running,
run_with=run_with,
+ retried_from_workflow_run_id=retried_from_workflow_run_id,
created_at=datetime.now(timezone.utc),
modified_at=datetime.now(timezone.utc),
)
@@ -230,6 +235,83 @@ class TestCodeModeGate:
assert len(agent_function.calls) == 1
+class _UpgradingAgentFunction(AgentFunction):
+ def __init__(self, upgrade: bool, keep_code: bool = True) -> None:
+ self.upgrade = upgrade
+ self.keep_code = keep_code
+ self.upgrade_calls: list[tuple[str, str, str]] = []
+
+ async def should_upgrade_to_code_mode(self, *, workflow: Workflow, workflow_run: WorkflowRun) -> bool:
+ self.upgrade_calls.append(
+ (workflow_run.workflow_run_id, workflow_run.organization_id, workflow.workflow_permanent_id)
+ )
+ return self.upgrade
+
+ async def should_keep_code_mode_for_workflow_run(self, *, workflow: Workflow, workflow_run: WorkflowRun) -> bool:
+ return self.keep_code
+
+
+class TestCodeModeUpgrade:
+ """A flag-gated rollout can upgrade an agent-intended run to code."""
+
+ @pytest.mark.asyncio
+ async def test_upgrade_flips_agent_to_code(self, monkeypatch):
+ agent_function = _UpgradingAgentFunction(upgrade=True, keep_code=True)
+ monkeypatch.setattr(app, "AGENT_FUNCTION", agent_function)
+ from skyvern.forge.sdk.workflow.service import WorkflowService
+
+ wf = _make_workflow(run_with="agent")
+ wr = _make_run(run_with="agent")
+ assert await WorkflowService().should_run_script(wf, wr) is True
+ assert agent_function.upgrade_calls == [("wr_test", "org_test", "wpid_test")]
+
+ @pytest.mark.asyncio
+ async def test_upgrade_disabled_stays_agent(self, monkeypatch):
+ agent_function = _UpgradingAgentFunction(upgrade=False)
+ monkeypatch.setattr(app, "AGENT_FUNCTION", agent_function)
+ from skyvern.forge.sdk.workflow.service import WorkflowService
+
+ wf = _make_workflow(run_with="agent")
+ wr = _make_run(run_with="agent")
+ assert await WorkflowService().should_run_script(wf, wr) is False
+
+ @pytest.mark.asyncio
+ async def test_upgraded_run_still_passes_keep_code_gate(self, monkeypatch):
+ """Upgrade True but keep-code gate False → still agent (gate can veto)."""
+ agent_function = _UpgradingAgentFunction(upgrade=True, keep_code=False)
+ monkeypatch.setattr(app, "AGENT_FUNCTION", agent_function)
+ from skyvern.forge.sdk.workflow.service import WorkflowService
+
+ wf = _make_workflow(run_with="agent")
+ wr = _make_run(run_with="agent")
+ assert await WorkflowService().should_run_script(wf, wr) is False
+
+ @pytest.mark.asyncio
+ async def test_upgrade_not_consulted_when_already_code(self, monkeypatch):
+ agent_function = _UpgradingAgentFunction(upgrade=False)
+ monkeypatch.setattr(app, "AGENT_FUNCTION", agent_function)
+ from skyvern.forge.sdk.workflow.service import WorkflowService
+
+ wf = _make_workflow(run_with="code")
+ wr = _make_run(run_with="code")
+ assert await WorkflowService().should_run_script(wf, wr) is True
+ assert agent_function.upgrade_calls == []
+
+ @pytest.mark.asyncio
+ async def test_fallback_retry_never_re_upgraded(self, monkeypatch):
+ # A code->agent fallback retry (run_with=agent, retried_from set) must never be re-upgraded
+ # to code, or the rollout would re-run the very script that just failed and the fallback
+ # would be a no-op for exactly the rollout population.
+ agent_function = _UpgradingAgentFunction(upgrade=True, keep_code=True)
+ monkeypatch.setattr(app, "AGENT_FUNCTION", agent_function)
+ from skyvern.forge.sdk.workflow.service import WorkflowService
+
+ wf = _make_workflow(run_with="agent")
+ wr = _make_run(run_with="agent", retried_from_workflow_run_id="wr_orig")
+ assert await WorkflowService().should_run_script(wf, wr) is False
+ assert agent_function.upgrade_calls == []
+
+
class TestScriptReviewerGate:
"""The script reviewer should only fire when the script was actually executed.