Skip to content

Automate browser based workflows with AI

Make the Task V3 auto-observe A/B flag targetable by workflow (#8337)

pedrohsdb5c813070ea2c
diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.py
index aa2ffa1af..e7c75fa04 100644
--- a/skyvern/forge/agent.py
+++ b/skyvern/forge/agent.py
@@ -1334,6 +1334,7 @@ class ForgeAgent:
close_browser_on_completion: bool,
browser_session_id: str | None,
task_block: BaseTaskBlock | None = None,
+ workflow_permanent_id: str | None = None,
) -> tuple[Step, Task]:
"""Run a whole task via the native Task V3 tool-loop (one persistent conversation).
@@ -1902,6 +1903,9 @@ class ForgeAgent:
downloads_dir=get_download_dir(download_id),
organization_id=organization.organization_id,
task_id=task.task_id,
+ # Task.workflow_permanent_id is never populated on the execution path (get_task
+ # builds Task without it); the caller sources it from the WorkflowRun row.
+ workflow_permanent_id=workflow_permanent_id,
max_action_steps=step_cap,
max_turns=max_turns,
max_tool_calls=max_tool_calls,
@@ -2375,6 +2379,7 @@ class ForgeAgent:
close_browser_on_completion=close_browser_on_completion,
browser_session_id=browser_session_id,
task_block=task_block,
+ workflow_permanent_id=workflow_run.workflow_permanent_id if workflow_run else None,
)
finally:
await app.ARTIFACT_MANAGER.flush_step_archive(step.step_id)
diff --git a/skyvern/forge/agent_functions.py b/skyvern/forge/agent_functions.py
index db3036875..ce8345149 100644
--- a/skyvern/forge/agent_functions.py
+++ b/skyvern/forge/agent_functions.py
@@ -885,7 +885,7 @@ class AgentFunction:
# OSS resolves auto-observe from the static setting; cloud overrides to bucket a run per task_id.
async def resolve_task_v3_auto_observe(
- self, *, task_id: str | None, organization_id: str | None
+ self, *, task_id: str | None, organization_id: str | None, workflow_permanent_id: str | None = None
) -> AutoObserveDecision:
return auto_observe_from_setting()
diff --git a/skyvern/forge/taskv3/engine.py b/skyvern/forge/taskv3/engine.py
index 4c05b17c2..36cd2b13a 100644
--- a/skyvern/forge/taskv3/engine.py
+++ b/skyvern/forge/taskv3/engine.py
@@ -195,6 +195,7 @@ async def run_task_v3_agent_loop(
downloads_dir: str | None = None,
organization_id: str | None = None,
task_id: str | None = None,
+ workflow_permanent_id: str | None = None,
max_turns: int = DEFAULT_MAX_TURNS,
max_tool_calls: int = DEFAULT_MAX_TOOL_CALLS,
max_action_steps: int | None = None,
@@ -310,7 +311,9 @@ async def run_task_v3_agent_loop(
auto_observe_decision = (
AutoObserveDecision(enabled=False, arm="default")
if page_free
- else await app.AGENT_FUNCTION.resolve_task_v3_auto_observe(task_id=task_id, organization_id=organization_id)
+ else await app.AGENT_FUNCTION.resolve_task_v3_auto_observe(
+ task_id=task_id, organization_id=organization_id, workflow_permanent_id=workflow_permanent_id
+ )
)
auto_observe = auto_observe_decision.enabled
if not page_free:
diff --git a/tests/unit/test_agent_task_v3.py b/tests/unit/test_agent_task_v3.py
index 54a5e8aa1..b93cab534 100644
--- a/tests/unit/test_agent_task_v3.py
+++ b/tests/unit/test_agent_task_v3.py
@@ -45,7 +45,7 @@ from skyvern.forge.sdk.workflow.models.block import (
ValidationBlock,
)
from skyvern.forge.sdk.workflow.models.parameter import CredentialParameter, OutputParameter, ParameterType
-from skyvern.forge.sdk.workflow.models.workflow import WorkflowRunStatus
+from skyvern.forge.sdk.workflow.models.workflow import WorkflowRun, WorkflowRunStatus
from skyvern.forge.taskv3.engine import MIN_ACTION_STEPS
from skyvern.forge.taskv3.loop import LoopOutcome
from skyvern.schemas.workflows import BlockStatus, BlockType
@@ -81,6 +81,7 @@ async def _run_execute_task_v3(
context_overrides: dict[str, Any] | None = None,
own_block_row: WorkflowRunBlock | None = None,
own_block_lookup_raises: BaseException | None = None,
+ workflow_permanent_id: str | None = None,
**task_overrides: Any,
) -> tuple[Step, Any, AsyncMock, AsyncMock]:
agent = ForgeAgent()
@@ -186,6 +187,7 @@ async def _run_execute_task_v3(
close_browser_on_completion=True,
browser_session_id=None,
task_block=task_block,
+ workflow_permanent_id=workflow_permanent_id,
)
finally:
skyvern_context.reset()
@@ -228,6 +230,18 @@ async def test_execute_task_v3_bills_per_browser_action(monkeypatch: pytest.Monk
assert len(billed_step.output.actions_and_results) == 3
+async def test_execute_task_v3_threads_workflow_permanent_id_to_the_loop(monkeypatch: pytest.MonkeyPatch) -> None:
+ # Task.workflow_permanent_id is never populated on the execution path (get_task builds Task
+ # without it), so the executor must receive the workflow run's wpid explicitly and hand it to
+ # the loop for per-workflow flag targeting.
+ outcome = LoopOutcome(status="completed", reason="ok", extracted_output={"k": "v"})
+ _step, _task, loop_mock, _post = await _run_execute_task_v3(
+ monkeypatch, outcome, workflow_permanent_id="wpid_from_workflow_run"
+ )
+ assert loop_mock.await_args.kwargs["workflow_permanent_id"] == "wpid_from_workflow_run"
+
+
@pytest.mark.asyncio
async def test_execute_task_v3_no_actions_bills_nothing(monkeypatch: pytest.MonkeyPatch) -> None:
# A terminate with no browser mutations: the hook still runs (it no-ops on zero actions).
@@ -982,6 +996,7 @@ async def _run_execute_step_gate(
engine: agent_module.RunEngine,
task_block: BaseTaskBlock | None,
experimentation_provider: BaseExperimentationProvider | None = None,
+ workflow_run: Any = None,
**task_overrides: Any,
) -> tuple[AsyncMock, AsyncMock]:
"""Drive ForgeAgent.execute_step through the v3 dispatch gate.
@@ -1012,7 +1027,7 @@ async def _run_execute_step_gate(
):
mock_app.DATABASE.tasks.get_task = AsyncMock(return_value=None)
mock_app.DATABASE.tasks.update_task = AsyncMock()
- mock_app.DATABASE.workflow_runs.get_workflow_run = AsyncMock(return_value=None)
+ mock_app.DATABASE.workflow_runs.get_workflow_run = AsyncMock(return_value=workflow_run)
mock_app.AGENT_FUNCTION.validate_step_execution = AsyncMock()
if experimentation_provider is not None:
mock_app.EXPERIMENTATION_PROVIDER = experimentation_provider
@@ -1037,6 +1052,31 @@ async def _run_execute_step_gate(
return v3_mock, step_engine_mock
+async def test_execute_step_sources_wpid_from_the_workflow_run_row() -> None:
+ # Task.workflow_permanent_id is never populated on the execution path, so the dispatch site
+ # must source the wpid from the WorkflowRun row it already fetched.
+ block = _make_block(TaskBlock)
+ now = datetime.now(UTC)
+ workflow_run = WorkflowRun(
+ workflow_run_id="wr_gate",
+ workflow_id="w_gate",
+ workflow_permanent_id="wpid_from_row",
+ organization_id="o_1",
+ status=WorkflowRunStatus.running,
+ created_at=now,
+ modified_at=now,
+ )
+ v3_mock, _ = await _run_execute_step_gate(
+ engine=agent_module.RunEngine.skyvern_v3,
+ task_block=block,
+ workflow_run=workflow_run,
+ workflow_run_id="wr_gate",
+ )
+ v3_mock.assert_awaited_once()
+ assert v3_mock.await_args.kwargs["workflow_permanent_id"] == "wpid_from_row"
+
+
@pytest.mark.asyncio
@pytest.mark.parametrize("block_cls,overrides", _ALLOWED_BLOCK_CASES, ids=_ALLOWED_BLOCK_IDS)
async def test_execute_step_dispatches_supported_block_types_to_v3(