feat: lease local devices so several agents can share them #536
All Vouch review confidence checks passed
Residual risk 0.38 = Change Risk (0.64) × (1 − Review Confidence (0.41)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.64 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.344
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #536"
general
weight: 0.35
bhargavmehta01
Summary
Running several agents against one machine (a few Claude Code / Codex sessions, or many clients of one
--listenserver) today means they can grab the same emulator or phone and step on each other. This adds opt-in local device leasing: an agent leases a device, gets a handle, and passes that handle asdeviceto any existing tool. Others are refused politely until the lease is released or expires.Closes #534. Discussed in #534 as CONTRIBUTING asks for larger features.
What's new
| Tool | What it does |
|---|---|
|
mobile_lease_device| Leases a free local device that matchesplatform/type/device(id or exact name). Options:holderlabel,ttlSeconds(default 600, max 3600),wait+timeoutSeconds(default 300) to block until one frees. Returns{ device: "lease:<token>", lease: {...} }. ||
mobile_renew_lease| Extends a lease (optionally with a newttlSeconds) without touching the device. ||
mobile_release_lease| Releases a lease. The device state is left as-is (unlikemobile_release_remote_device). ||
mobile_list_leases| Read-only: who holds which device, until when. Never shows handles. |mobile_list_available_devicesadds aleasefield to leased devices (free devices are unchanged), plus a hint towaitwhen every local device is leased. The server instructions get two lines on when to lease.Design
tool()helper. Every tool whose schema hasdevicegets it passed throughDeviceLeaseStore.resolve()before its callback runs, so no tool schema changed and recording/crash tools (which use the id directly rather thangetRobotFromDevice) are covered too. The wrapped callback is whatmobile_batch_commandscalls, so each batch step is checked as well. Exempt:mobile_release_remote_device(cloud id),mobile_lease_device(itsdeviceis a filter) and the batch tool itself (it hands itsdeviceto the steps).readFile, with no lock and no write. If the state directory can't be read, raw ids fall through (logged) so machines that never lease are never broken.leases.jsonunder$MOBILEMCP_STATE_DIRor$XDG_STATE_HOME/mobile-mcp(~/.local/state/mobile-mcp;%LOCALAPPDATA%\mobile-mcpon Windows). Dir is0700, file is0600. Writes are temp file +rename. Changes happen under anO_EXCLlock file (leases.lock) holding{pid, hostname, nonce, createdAt}. A lock is stale if its pid is dead on this host or it's older than 10s (locks are held for milliseconds). Stale locks are moved aside with an atomic rename and restored withlink()if another process replaced them in between. A process only unlinks a lock carrying its own nonce. Because handles are tokens rather than MCP sessions, this works the same over stdio and stateless Streamable HTTP.crypto.randomBytes, base64url). Only their sha256 is stored.trace()output of args, responses and errors goes throughredactLeaseHandles()(lease:AbCd…). The publicleaseIdcan't act on a device.ttlSeconds. The file is deleted when the last lease goes.mobile_list_available_devicesmoved, unchanged, into alistLocalDevices()helper somobile_lease_devicefilters exactly the same device list (legacy robot mode included). Most of theserver.tsdiff is that move.Out of scope / open questions
forceif maintainers want it.Testing
No device needed. Everything runs under the existing
npm test(Playwright as the runner, c8 for coverage):test/device-leases.test.ts: the store on its own, with a fake clock. Covers acquire, conflict, next-free candidate, raw-id refusal naming holder/expiry, foreign/malformed handles, expiry, renew-on-use, explicit renew with new TTL, release and file cleanup, TTL clamping, redaction, a damaged file, three stale-lock cases (dead pid, old lock from another host, empty old file), a live lock timing out, 20 concurrent store instances racing for one device, and 8 separate Node processes racing for 3 devices (the store is transpiled with thetypescriptdevDependency into a temp file so no build is needed).test/server-leases.test.ts: through the MCP client. Covers raw id refused, handle allowed, batch step refused, unleased device untouched, list/renew/release tools, andformatAvailableDeviceswith leases. With a stand-inMOBILECLI_PATHshell script it also coversmobile_lease_deviceend-to-end (filters, conflict message,waitpicking the device up once it's released, no match).test/server-annotations.test.ts: annotations for the 4 new tools.