Skip to content

feat(functions): add context bindings - #23

Merged
lutz-grex merged 14 commits into
developfrom
feat/fn-ctx-bindings
Nov 25, 2026
Merged

lutz-grex merged 14 commits into
developfrom
feat/fn-ctx-bindings

Conversation

@lutz-grex

@lutz-grex lutz-grex commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A function can now declare what it needs as a typed Context subclass: a device, credential packages, a mailbox, a proxy and tasks. The runner provides each binding before main runs. In the cloud it uses the platform's resources. Locally it uses the command-line flags, or the cloud with your own key via --cloud. The same file runs unchanged in both places.

class Ctx(Context):
    device: Device
    app_login: Credentials = binding(package="com.example.app")
    mailbox: Mailbox
    operator: Operator

@script
async def main(input: Input, ctx: Ctx) -> Output: ...

Changes

  • New interface types Device, Credentials, Mailbox, Proxy and Operator, plus binding(...). The declaration rules are checked when the function is loaded. A concrete implementation in Ctx is rejected.
  • inspect now reports a requires object (device, credentials, mailbox, proxy, tasks), so the platform knows a function's needs before it runs.
  • All bindings are resolved before main. If one is missing, the run fails early with NotAvailableError, naming the field.
  • Credentials are typed into the focused text field and are never returned to the function.
  • Mailbox and Proxy are run resources, declared as bare annotations: the Input model stays pure business input, and binding(input=...) and package options on them are refused at inspect. The input option and the mobilerun-mailbox/mobilerun-proxy formats are removed. A Proxy binding needs a Device binding; inspect refuses it otherwise.
  • The run selects the resources: RunInfo gains mailbox_id and proxy (ProxyRef(kind, id), exported with RunInfo), set by --mailbox-id and the paired --proxy-kind/--proxy-id. A half pair, an unknown kind or an invalid UUID is a usage error before main.
  • Resolution builds ctx.mailbox only from ctx.run.mailbox_id and ctx.proxy only from ctx.run.proxy. Connect attaches the selected proxy once, before main, with devices.proxy.connect(device_id, connect={"id": ...}) and needs a cloud device; byo is refused explicitly. Missing selections, byo, a local device and invalid IDs fail before any remote call, whatever the declaration order.
  • A credentials binding is bound to its declared package: type_field(device, field, *, name=None) and list() take no package. In the cloud only the declared packages are read, one per-package request each; other packages' values never enter the run. A local file's flat entry binds to the declared package.
  • wait_for_code captures one UTC baseline when the wait begins (unless after is passed) and forwards sender and after unchanged. A 204 keeps polling; 404, 409 and auth errors end the wait at once; the timeout and cancellation stop polling.
  • device.do(...) and device.expect(...) run tasks on the bound device. expect raises ExpectationFailed or ExpectationInconclusive and returns only when the check passes.
  • Credential typing refuses unless the bound package is the foreground app (Android package or iOS bundle id; an unknown foreground app is refused), and accepts only the run's own device. The value never reaches SDK debug logs, also with MOBILERUN_LOG=debug.
  • A screenshot value is read as a file only when it is the core's own temp file (mobilerun-screenshot-* in the temp directory); anything else is validated base64, so JPEGs starting with /9j/ decode.
  • Adapter I/O of a synchronous main runs on the runner's event loop, so asyncio.run(ctx.email.wait_for_code()) is safe there.
  • Run-task paths are built with the SDK's path templating; ids are never spliced into the path raw.
  • The proxy is attached only in live runs; dry_run and verification skip the connect and status() stays readable.
  • The device extra requires mobilerun-core>=1.12.0,<2.
  • The runtime reads only the declared credential packages; the function's own code can use whatever its run key allows.
  • No ctx method buys, creates or deletes resources. Tasks consume usage like any other task.
  • New run flags --cloud and --credentials-file.
  • The device extra now includes the SDK and PyYAML.
  • Functions using ctx: Context and ctx.device() keep working and log a deprecation warning.
  • The platform AI handle is AI: ai: AI, ctx.ai.openai(), base URL = API base + /ai, requires key ai, extra mobilerun-functions[ai].
  • The starter template, a new examples/bindings.py and the README now use the new form. Version 0.2.0rc1 (release candidate).

Testing

  • 478 tests pass on Python 3.11, 3.12 and 3.13. mypy and ruff are clean, and uv sync --frozen passes.
  • New tests cover the declaration rules, requires, runner target selection, resolution failures, focus-checked credential typing, task polling and timeouts, per-package credential reads, bound typing, mailbox baseline and status handling, the Proxy-needs-Device rule, the three expect outcomes, and the old ctx.device() path.

Runtime

  • inspect examples/bindings.py prints the expected requires.
  • A run without a device fails before main with the documented error.
  • A live run on a cloud device is not verified yet: no ready device was available. It follows once the platform image ships this version.

Rollout

Release 0.2.0rc1 after merge. The platform image moves to it in a separate change. The platform already accepts requires. The run-selected mailbox and proxy fields reach it in a separate change; no released consumer uses the removed input-bound form.

lutz-grex and others added 14 commits November 24, 2026 14:48
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@lutz-grex
lutz-grex merged commit 763b7d2 into develop Nov 25, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant