Skip to content

Commit cc2a0c8

Browse files
committed
refactor(sandbox): split deployment into modules; confirm SMS charges on macro replay; shared terminal check (DRO-3975)
1 parent 7b8b06b commit cc2a0c8

41 files changed

Lines changed: 2851 additions & 2075 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/black.yml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ jobs:
1919
options: "--check --diff --verbose"
2020
src: "."
2121
version: "26.10.1"
22+
23+
- name: Type check
24+
run: |
25+
python -m pip install uv
26+
uv run --locked --extra sandbox --extra dev mypy mobilerun

‎AGENTS.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,12 @@ black --check --diff --verbose .
105105
mypy mobilerun
106106
```
107107

108-
Black 26.10.1 runs in CI. Ruff is a separate local check. Pyright is not
109-
configured or installed by the project's development dependencies; if available,
110-
`pyright mobilerun` is an additional check, not the documented CI gate.
108+
Black 26.10.1 and `mypy mobilerun` run in CI. Modules listed under
109+
`[[tool.mypy.overrides]]` in `pyproject.toml` are exempt until fixed; remove a
110+
module from that list when it is clean and never add one. Ruff is a separate
111+
local check. Pyright is not configured or installed by the project's development
112+
dependencies; if available, `pyright mobilerun` is an additional check, not the
113+
documented CI gate.
111114

112115
Smoke tests are opt-in and can use real devices, cloud resources and LLM tokens.
113116
**Ask the user before running any paid smoke test, cloud operation or LLM call.**

‎mobilerun/cli/base/menu.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
select_prompt,
1616
text_prompt,
1717
)
18+
from mobilerun.utils.terminal import can_prompt
1819

1920

2021
def is_interactive() -> bool:
21-
return sys.stdin.isatty() and sys.stdout.isatty()
22+
"""Return whether a prompt drawn on stdout can be answered."""
23+
return can_prompt(sys.stdout)
2224

2325

2426
ChoicesProvider = Callable[[click.Context], Sequence[SelectChoice]]

‎mobilerun/clients/cloud/README.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ app = await upload_app("counter.apk", billing=Billing.NO_COMMERCE)
4949

5050
Every public client function requires keyword-only `billing: Billing`, without a
5151
default. Enum values and their strings work; `None` does not. Each boundary declares
52-
its minimum with `@requires(Billing.X)`. Missing billing raises `TypeError`, an
52+
its minimum with `@requires(Billing.X)`. A call that can cost more declares it on
53+
the same line with `paid_if=`, a function of the bound arguments (a metered phone
54+
plan, a rented number or mailbox, a new Connect proxy by country); the guard never
55+
infers a rule from a module or function name. Missing billing raises `TypeError`, an
5356
invalid mode raises `BillingError`, and a lower mode raises `BillingRequired`
5457
with `needed`, `given`, and `operation`, all before transport. The order is
5558
`NO_COMMERCE < INCLUDED < PAID`. Import enums from `mobilerun.models` (clients)

‎mobilerun/clients/cloud/billing.py‎

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,19 @@
44
from collections.abc import Awaitable, Callable, Mapping
55
from functools import wraps
66
from inspect import signature
7-
from typing import ParamSpec, TypeVar
7+
from typing import Any, ParamSpec, TypeVar
88

99
from mobilerun.errors.cloud import BillingError, BillingRequired
1010
from mobilerun.models.billing import Billing, Plan, ResourcePlan
11+
from mobilerun.utils.terminal import can_prompt
1112

1213
P = ParamSpec("P")
1314
R = TypeVar("R")
1415

1516

1617
def billing_prompt_is_interactive() -> bool:
1718
"""Return whether a billing prompt can use terminal stdin and stderr."""
18-
stdin, stderr = sys.stdin, sys.stderr
19-
return (
20-
stdin is not None and stderr is not None and stdin.isatty() and stderr.isatty()
21-
)
19+
return can_prompt(sys.stderr)
2220

2321

2422
async def _confirm_billing(error: BillingRequired) -> bool:
@@ -66,10 +64,30 @@ async def with_billing_retry(
6664
return await operation(needed), needed
6765

6866

67+
def buys_connect_proxy(proxy: object) -> bool:
68+
"""Return whether a proxy spec orders a new Connect proxy by country."""
69+
connect = proxy.get("connect") if isinstance(proxy, Mapping) else None
70+
return isinstance(connect, Mapping) and bool(connect.get("country"))
71+
72+
73+
def rents_resource(arguments: Mapping[str, Any]) -> bool:
74+
"""Return whether a number or mailbox call asks for a rented resource."""
75+
plan = arguments.get("plan")
76+
return plan is not None and ResourcePlan(plan) is ResourcePlan.RENT
77+
78+
6979
def requires(
70-
minimum: Billing, *, operation: str | None = None
80+
minimum: Billing,
81+
*,
82+
operation: str | None = None,
83+
paid_if: Callable[[Mapping[str, Any]], bool] | None = None,
7184
) -> Callable[[Callable[P, R]], Callable[P, R]]:
72-
"""Declare and enforce a boundary's minimum commerce mode before any work."""
85+
"""Declare and enforce a boundary's minimum commerce mode before any work.
86+
87+
``paid_if`` receives the bound arguments and returns whether this call needs
88+
paid billing. Declare it on the function it guards: the guard never infers
89+
a rule from a module or function name.
90+
"""
7391

7492
def decorate(function: Callable[P, R]) -> Callable[P, R]:
7593
sig = signature(function)
@@ -79,30 +97,10 @@ def guarded(*args: P.args, **kwargs: P.kwargs) -> R:
7997
bound = sig.bind(*args, **kwargs)
8098
given = bound.arguments["billing"]
8199
needed = minimum
82-
params = bound.arguments.get("params", {})
83-
proxy = bound.arguments.get("proxy") or params
84-
connect = proxy.get("connect", {}) if isinstance(proxy, Mapping) else {}
85-
if function.__module__.endswith((".devices", ".proxies")):
86-
if isinstance(connect, Mapping) and connect.get("country"):
87-
needed = Billing.PAID
88100
try:
101+
if paid_if is not None and paid_if(bound.arguments):
102+
needed = Billing.PAID
89103
mode = Billing(given)
90-
if (
91-
function.__module__.endswith(".devices")
92-
and function.__name__ == "create"
93-
):
94-
plan = bound.arguments.get("plan")
95-
if plan is not None:
96-
plan = Plan(plan)
97-
if plan in (Plan.AUTO, Plan.MINUTE):
98-
needed = Billing.PAID
99-
if (
100-
function.__module__.endswith((".numbers", ".mailboxes"))
101-
and function.__name__ == "create"
102-
):
103-
plan = bound.arguments.get("plan")
104-
if plan is not None and ResourcePlan(plan) is ResourcePlan.RENT:
105-
needed = Billing.PAID
106104
except (ValueError, TypeError) as exc:
107105
error = BillingError(
108106
operation=function.__name__,

‎mobilerun/clients/cloud/devices.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""Functional cloud device SDK boundaries."""
22

3-
from collections.abc import Awaitable, Callable
3+
from collections.abc import Awaitable, Callable, Mapping
44
from contextlib import nullcontext
55
from datetime import datetime
6-
from typing import Literal, TypedDict, Unpack, cast
6+
from typing import Any, Literal, TypedDict, Unpack, cast
77

88
import httpx
99
from mobilerun_sdk import APIError, AsyncMobilerun
@@ -30,6 +30,7 @@
3030
from mobilerun.clients.cloud.billing import (
3131
_billing_preference,
3232
_validate_create_params,
33+
buys_connect_proxy,
3334
requires,
3435
)
3536
from mobilerun.clients.cloud.client import cloud_client
@@ -112,7 +113,14 @@
112113
)
113114

114115

115-
@requires(Billing.INCLUDED)
116+
def _create_needs_paid(arguments: Mapping[str, Any]) -> bool:
117+
"""A metered plan or a new Connect proxy costs money."""
118+
plan = arguments.get("plan")
119+
metered = plan is not None and Plan(plan) in (Plan.AUTO, Plan.MINUTE)
120+
return metered or buys_connect_proxy(arguments.get("proxy"))
121+
122+
123+
@requires(Billing.INCLUDED, paid_if=_create_needs_paid)
116124
async def create(
117125
*,
118126
billing: Billing,

‎mobilerun/clients/cloud/mailboxes.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from mobilerun.clients.cloud.billing import (
1818
_billing_preference,
1919
_validate_create_params,
20+
rents_resource,
2021
requires,
2122
)
2223
from mobilerun.clients.cloud.checkout import CheckoutMode, checkout_callback
@@ -33,7 +34,7 @@
3334
from mobilerun.models.checkout import PaymentSnapshot
3435

3536

36-
@requires(Billing.INCLUDED, operation="Create mailbox")
37+
@requires(Billing.INCLUDED, operation="Create mailbox", paid_if=rents_resource)
3738
async def create(
3839
*,
3940
billing: Billing,

‎mobilerun/clients/cloud/numbers.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from mobilerun.clients.cloud.billing import (
2323
_billing_preference,
2424
_validate_create_params,
25+
rents_resource,
2526
requires,
2627
)
2728
from mobilerun.clients.cloud.checkout import CheckoutMode, checkout_callback
@@ -71,7 +72,7 @@
7172
)
7273

7374

74-
@requires(Billing.INCLUDED, operation="Create number")
75+
@requires(Billing.INCLUDED, operation="Create number", paid_if=rents_resource)
7576
async def create(
7677
*,
7778
billing: Billing,

‎mobilerun/clients/cloud/proxies.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
from mobilerun_sdk.types.devices.proxy_connect_params import ProxyConnectParams
1111
from mobilerun_sdk.types.devices.proxy_status_response import ProxyStatusResponse
1212

13-
from mobilerun.clients.cloud.billing import _validate_create_params, requires
13+
from mobilerun.clients.cloud.billing import (
14+
_validate_create_params,
15+
buys_connect_proxy,
16+
requires,
17+
)
1418
from mobilerun.clients.cloud.checkout import CheckoutMode, checkout_callback
1519
from mobilerun.clients.cloud.client import cloud_client
1620
from mobilerun.clients.cloud.errors import cloud_error
@@ -29,7 +33,10 @@
2933
BuyParams = TypedDict("BuyParams", {"name": str}, total=False)
3034

3135

32-
@requires(Billing.NO_COMMERCE)
36+
@requires(
37+
Billing.NO_COMMERCE,
38+
paid_if=lambda arguments: buys_connect_proxy(arguments["params"]),
39+
)
3340
async def connect_device(
3441
device_id: str,
3542
params: ProxyConnectParams,

‎mobilerun/macro/README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ mobilerun macro replay macro.json emulator-5554
6464
See [macro_demo.py](../../tests/demos/macro_demo.py) and [CLI commands](cli/README.md).
6565
Cloud devices can incur charges.
6666

67+
`mobilerun macro replay` asks once before looking up or opening a device if
68+
the selected steps could send SMS. Recordings do not store extension roles,
69+
so replay counts every recorded extension `.send` call as possible SMS
70+
regardless of billing mode. Pass `--yes` to approve without prompting;
71+
non-interactive replay refuses without it. `--dry-run` neither asks for
72+
confirmation nor sends messages.
73+
6774
`mobilerun macro list` uses `<MOBILERUN_USER_CONFIG_DIR>/macros` by default.
6875
This replaces the previous `./trajectories` default; set
6976
`MOBILERUN_MACRO_DIRECTORY=trajectories` to keep using that directory. Relative

0 commit comments

Comments
 (0)