Skip to content

Automate browser based workflows with AI

Carry the billing tier into Task V3's engine-routing experiments (SKY-16082) (#8508)

Shuchang Zheng1c3af4c9690a
diff --git a/skyvern/forge/agent_functions.py b/skyvern/forge/agent_functions.py
index 1b276e63b..88ed288b0 100644
--- a/skyvern/forge/agent_functions.py
+++ b/skyvern/forge/agent_functions.py
@@ -46,6 +46,7 @@ from skyvern.forge.sdk.copilot.config import (
)
from skyvern.forge.sdk.core import skyvern_context
from skyvern.forge.sdk.db.agent_db import AgentDB
+from skyvern.forge.sdk.experimentation.billing_tier import BillingTier
from skyvern.forge.sdk.models import Step, StepStatus
from skyvern.forge.sdk.schemas.credentials import (
CreateCredentialRequest,
@@ -917,6 +918,11 @@ class AgentFunction:
) -> RunEngine:
return requested_engine
+ # OSS has no billing tiers; cloud overrides with the organization_pricing lookup. UNKNOWN keeps
+ # a tier-targeted experiment condition from matching rather than guessing a tier for everyone.
+ async def resolve_billing_tier(self, organization_id: str | None) -> BillingTier:
+ return BillingTier.UNKNOWN
+
# OSS has no ATS-scoped guidance; cloud overrides to supply pre-authorized eligibility defaults
# behind a flag when the task targets a gated application-tracking-system host.
async def resolve_task_v3_extra_guidance(self, *, task: Task, organization: Organization) -> str | None:
diff --git a/skyvern/forge/sdk/experimentation/billing_tier.py b/skyvern/forge/sdk/experimentation/billing_tier.py
new file mode 100644
index 000000000..869346a49
--- /dev/null
+++ b/skyvern/forge/sdk/experimentation/billing_tier.py
@@ -0,0 +1,15 @@
+from __future__ import annotations
+
+from enum import StrEnum
+
+# Matched verbatim by experiment release conditions, so every routing surface has to spell the
+# key and the values identically or a tier-targeted rollout silently misses one of them.
+BILLING_TIER_PROPERTY = "billing_tier"
+
+
+class BillingTier(StrEnum):
+ ENTERPRISE = "enterprise"
+ SELF_SERVE = "self_serve"
+ # The tier could not be read. Distinct from ENTERPRISE so an outage is visible in telemetry
+ # rather than reported as a real tier, and so a condition targeting self-serve cannot match it.
+ UNKNOWN = "unknown"
diff --git a/skyvern/forge/sdk/experimentation/workflow_block_engine.py b/skyvern/forge/sdk/experimentation/workflow_block_engine.py
index 7cf276d04..ed8d202b9 100644
--- a/skyvern/forge/sdk/experimentation/workflow_block_engine.py
+++ b/skyvern/forge/sdk/experimentation/workflow_block_engine.py
@@ -6,6 +6,7 @@ import structlog
from skyvern.forge import app
from skyvern.forge.sdk.core import skyvern_context
+from skyvern.forge.sdk.experimentation.billing_tier import BILLING_TIER_PROPERTY, BillingTier
from skyvern.forge.sdk.experimentation.providers import NoOpExperimentationProvider
from skyvern.schemas.run_enums import RunEngine
@@ -30,6 +31,20 @@ async def task_v3_disabled(distinct_id: str, organization_id: str | None) -> boo
)
+async def _billing_tier_for_arm(organization_id: str | None) -> BillingTier:
+ """Never raises: a targeting property must not be able to decide the arm.
+
+ The resolver's own catch-all turns any exception into control, so letting a tier read escape
+ would let a billing-lookup failure silently move every run of the experiment onto v1 -- an
+ outage in a system the arm never depended on before.
+ """
+ try:
+ return await app.AGENT_FUNCTION.resolve_billing_tier(organization_id)
+ except Exception:
+ LOG.warning("Failed to resolve the billing tier for the engine arm", exc_info=True)
+ return BillingTier.UNKNOWN
+
+
async def resolve_workflow_block_engine_arm(
context: skyvern_context.SkyvernContext,
*,
@@ -60,22 +75,31 @@ async def resolve_workflow_block_engine_arm(
return
override: RunEngine | None = None
run_is_eligible = ineligibility_reason is None
+ billing_tier: BillingTier | None = None
try:
if run_is_eligible:
# The kill switch, shared with the dispatch gate via task_v3_disabled so both
# evaluations use the same cache key, wins over the experiment. A kill flipped
# mid-run still takes effect at dispatch while these rows already read v3:
- # stopping the run wins over its attribution.
+ # stopping the run wins over its attribution. Read FIRST, and before the billing
+ # lookup below: a Redis or pooler incident is exactly when someone flips this, and
+ # this lock is held throughout, so the kill switch must not queue behind that call.
disabled = await task_v3_disabled(workflow_run_id, organization_id)
- if not disabled and await provider.is_feature_enabled_cached(
- WORKFLOW_TASK_V3_AB_FLAG,
- workflow_run_id,
- properties={
- "organization_id": organization_id,
- "workflow_permanent_id": workflow_permanent_id or "not_workflow",
- },
- ):
- override = RunEngine.skyvern_v3
+ # billing_tier rides along so a release condition can hold enterprise and self-serve
+ # at different percentages. Deliberately not passed to task_v3_disabled: that flag's
+ # two callers must build identical provider cache keys, see its docstring.
+ if not disabled:
+ billing_tier = await _billing_tier_for_arm(organization_id)
+ if await provider.is_feature_enabled_cached(
+ WORKFLOW_TASK_V3_AB_FLAG,
+ workflow_run_id,
+ properties={
+ "organization_id": organization_id,
+ "workflow_permanent_id": workflow_permanent_id or "not_workflow",
+ BILLING_TIER_PROPERTY: billing_tier.value,
+ },
+ ):
+ override = RunEngine.skyvern_v3
except Exception:
LOG.warning(
"Failed to resolve the workflow-block engine arm; using control",
@@ -92,6 +116,10 @@ async def resolve_workflow_block_engine_arm(
arm="treatment" if override else "control",
run_is_eligible=run_is_eligible,
ineligibility_reason=ineligibility_reason,
+ # None whenever the A/B was never consulted -- an ineligible run, or one the kill switch
+ # already stopped -- so the field means "the tier this run was bucketed on" and never
+ # doubles as "nobody looked".
+ billing_tier=billing_tier.value if billing_tier else None,
)
diff --git a/tests/unit/test_agent_task_v3.py b/tests/unit/test_agent_task_v3.py
index 8be966f6b..bf81e74d0 100644
--- a/tests/unit/test_agent_task_v3.py
+++ b/tests/unit/test_agent_task_v3.py
@@ -31,6 +31,7 @@ from skyvern.forge.sdk.core.skyvern_context import SkyvernContext
from skyvern.forge.sdk.db.enums import TaskType
from skyvern.forge.sdk.db.exceptions import NotFoundError
from skyvern.forge.sdk.db.utils import hydrate_action
+from skyvern.forge.sdk.experimentation.billing_tier import BillingTier
from skyvern.forge.sdk.experimentation.providers import BaseExperimentationProvider
from skyvern.forge.sdk.experimentation.workflow_block_engine import DISABLE_TASK_V3_FLAG
from skyvern.forge.sdk.models import Step, StepStatus
@@ -726,6 +727,17 @@ async def test_execute_task_v3_extraction_redaction_respects_word_boundaries(
# ---------------------------------------------------------------------------
+def stub_workflow_block_engine_app(mock_wbe_app: MagicMock, *, billing_tier: BillingTier = BillingTier.UNKNOWN) -> None:
+ """Give a patched workflow_block_engine ``app`` an awaitable billing-tier seam.
+
+ Without it, awaiting the bare MagicMock's ``resolve_billing_tier`` raises TypeError, which
+ ``_billing_tier_for_arm`` swallows into UNKNOWN (with a logged traceback); the arm is still
+ decided by the flag, but with ``billing_tier="unknown"`` in its properties. Only tests that
+ assert on that property need this; ``ForgeAgent.execute_step`` never reaches the arm resolver.
+ """
+ mock_wbe_app.AGENT_FUNCTION.resolve_billing_tier = AsyncMock(return_value=billing_tier)
+
+
def _v3_task(llm_key: str | None = None, workflow_permanent_id: str | None = None) -> SimpleNamespace:
return SimpleNamespace(
llm_key=llm_key, task_id="tsk_x", organization_id="o_test", workflow_permanent_id=workflow_permanent_id
diff --git a/tests/unit/test_workflow_block_engine.py b/tests/unit/test_workflow_block_engine.py
index eea743ec9..fcc4e5275 100644
--- a/tests/unit/test_workflow_block_engine.py
+++ b/tests/unit/test_workflow_block_engine.py
@@ -16,6 +16,7 @@ import pytest
from skyvern.forge import app
from skyvern.forge.sdk.core import skyvern_context
from skyvern.forge.sdk.core.skyvern_context import SkyvernContext
+from skyvern.forge.sdk.experimentation.billing_tier import BILLING_TIER_PROPERTY, BillingTier
from skyvern.forge.sdk.experimentation.providers import BaseExperimentationProvider, NoOpExperimentationProvider
from skyvern.forge.sdk.experimentation.workflow_block_engine import (
DISABLE_TASK_V3_FLAG,
@@ -53,7 +54,12 @@ from skyvern.schemas.run_enums import RunEngine
from skyvern.schemas.workflows import BlockResult, BlockType
from skyvern.services import script_service
from tests.unit.helpers import make_organization
-from tests.unit.test_agent_task_v3 import _make_block, _make_output_parameter, _run_execute_step_gate
+from tests.unit.test_agent_task_v3 import (
+ _make_block,
+ _make_output_parameter,
+ _run_execute_step_gate,
+ stub_workflow_block_engine_app,
+)
from tests.unit.test_block_description_caching import _block_result, _setup_mocks
from tests.unit.test_missing_starter_url import _mock_block_execute_deps
@@ -101,6 +107,7 @@ async def _resolve(
) -> None:
with patch(WORKFLOW_BLOCK_ENGINE_APP_TARGET) as mock_app:
mock_app.EXPERIMENTATION_PROVIDER = provider
+ stub_workflow_block_engine_app(mock_app)
await resolve_workflow_block_engine_arm(
context,
workflow_run_id=workflow_run_id,
@@ -322,6 +329,7 @@ async def test_noop_provider_never_queried_and_leaves_engine_unchanged(scoped_co
spy = AsyncMock(wraps=provider._is_feature_enabled)
with patch(WORKFLOW_BLOCK_ENGINE_APP_TARGET) as mock_app, patch.object(provider, "_is_feature_enabled", spy):
mock_app.EXPERIMENTATION_PROVIDER = provider
+ stub_workflow_block_engine_app(mock_app)
await resolve_workflow_block_engine_arm(
scoped_context,
workflow_run_id="wr_noop",
@@ -648,6 +656,7 @@ async def test_resolver_logs_the_ineligibility_reason(scoped_context: SkyvernCon
patch("skyvern.forge.sdk.experimentation.workflow_block_engine.LOG") as mock_log,
):
mock_app.EXPERIMENTATION_PROVIDER = provider
+ stub_workflow_block_engine_app(mock_app)
await resolve_workflow_block_engine_arm(
scoped_context,
workflow_run_id="wr_logged",
@@ -799,3 +808,80 @@ async def test_agent_create_site_leaves_the_script_run_marker_unset(scoped_conte
create_kwargs = mock_app.DATABASE.observer.create_workflow_run_block.await_args.kwargs
assert create_kwargs["engine"] == RunEngine.skyvern_v3
assert create_kwargs.get("ai_fallback_triggered") is None
+
+
+async def test_arm_offers_the_billing_tier_to_the_ab_flag_but_not_the_kill_switch(
+ scoped_context: SkyvernContext,
+) -> None:
+ # The tier is what lets one release condition hold enterprise and self-serve at different
+ # percentages. It must stay off DISABLE_TASK_V3: this resolver and the dispatch gate both
+ # evaluate that flag through task_v3_disabled, and the provider caches on
+ # (flag, distinct_id, properties), so adding a property on one side only would let the kill
+ # switch answer differently for the same run.
+ provider = _FakeExperimentationProvider({WORKFLOW_TASK_V3_AB_FLAG: True})
+ with patch(WORKFLOW_BLOCK_ENGINE_APP_TARGET) as mock_app:
+ mock_app.EXPERIMENTATION_PROVIDER = provider
+ stub_workflow_block_engine_app(mock_app, billing_tier=BillingTier.SELF_SERVE)
+ await resolve_workflow_block_engine_arm(
+ scoped_context,
+ workflow_run_id="wr_tier",
+ organization_id="org_1",
+ workflow_permanent_id="wpid_1",
+ ineligibility_reason=None,
+ )
+
+ by_flag = {flag: properties or {} for flag, _distinct_id, properties in provider.calls}
+ assert by_flag[WORKFLOW_TASK_V3_AB_FLAG][BILLING_TIER_PROPERTY] == "self_serve"
+ assert BILLING_TIER_PROPERTY not in by_flag[DISABLE_TASK_V3_FLAG]
+ assert scoped_context.workflow_block_engine_override == RunEngine.skyvern_v3
+
+
+async def test_a_run_that_never_reaches_the_ab_pays_for_no_tier_lookup(scoped_context: SkyvernContext) -> None:
+ # An ineligible run reads no flag, so a tier read would be pure cost on a path that cannot use
+ # it, and a logged tier would claim the run was bucketed on one. The same holds for a run the
+ # kill switch stops: a Redis or pooler incident is exactly when that switch gets flipped, and
+ # this resolver holds a lock while it runs.
+ provider = _FakeExperimentationProvider({WORKFLOW_TASK_V3_AB_FLAG: True})
+ for reason, run_id in ((V3AbIneligibleReason.script_run, "wr_inelig"), (None, "wr_killed")):
+ provider.flags[DISABLE_TASK_V3_FLAG] = reason is None
+ tier = AsyncMock(return_value=BillingTier.ENTERPRISE)
+ with (
+ patch(WORKFLOW_BLOCK_ENGINE_APP_TARGET) as mock_app,
+ patch("skyvern.forge.sdk.experimentation.workflow_block_engine.LOG") as mock_log,
+ ):
+ mock_app.EXPERIMENTATION_PROVIDER = provider
+ mock_app.AGENT_FUNCTION.resolve_billing_tier = tier
+ await resolve_workflow_block_engine_arm(
+ scoped_context,
+ workflow_run_id=run_id,
+ organization_id="org_1",
+ workflow_permanent_id="wpid_1",
+ ineligibility_reason=reason,
+ )
+ tier.assert_not_awaited()
+ assert mock_log.info.call_args.kwargs["billing_tier"] is None
+ assert scoped_context.workflow_block_engine_override is None
+
+
+async def test_a_failing_billing_tier_lookup_cannot_decide_the_arm(scoped_context: SkyvernContext) -> None:
+ # The resolver's catch-all turns any exception inside it into control, so a tier read that
+ # escaped would let a billing-lookup outage move every run of the experiment onto v1 -- a
+ # dependency the arm never had. The treated run must still be treated.
+ provider = _FakeExperimentationProvider({WORKFLOW_TASK_V3_AB_FLAG: True})
+ with patch(WORKFLOW_BLOCK_ENGINE_APP_TARGET) as mock_app:
+ mock_app.EXPERIMENTATION_PROVIDER = provider
+ mock_app.AGENT_FUNCTION.resolve_billing_tier = AsyncMock(side_effect=RuntimeError("billing down"))
+ await resolve_workflow_block_engine_arm(
+ scoped_context,
+ workflow_run_id="wr_tier_down",
+ organization_id="org_1",
+ workflow_permanent_id="wpid_1",
+ ineligibility_reason=None,
+ )
+
+ assert scoped_context.workflow_block_engine_override == RunEngine.skyvern_v3
+ by_flag = {flag: properties or {} for flag, _distinct_id, properties in provider.calls}
+ assert by_flag[WORKFLOW_TASK_V3_AB_FLAG][BILLING_TIER_PROPERTY] == "unknown"