feat: add integrated device leasing

Merged
TH
5mo ago
+4576-761

Summary

Add integrated device leasing for shared remote/proxy access, so multiple agents can use one long-lived agent-device proxy without fighting over the same simulator/device or surfacing low-level runner ownership errors.

Before this PR, remote/proxy clients could connect to the same host daemon but ownership was effectively enforced late by platform runner/helper locks. In practice, two sandboxed agents targeting the same iOS device could trip over errors like “iOS runner is already owned by another agent-device daemon”, and recovery often required manual cleanup or restarting the proxy.

After this PR, remote access has an explicit lease lifecycle:

  • connect stores the remote/proxy profile and stable client identity.
  • open lazily resolves the target device and acquires a provider-aware device lease.
  • Commands after open heartbeat the lease under the daemon request lock.
  • close releases the active session/device lease; inactive proxy leases expire after five minutes.
  • Contention fails early with DEVICE_LEASE_BUSY, including backend/provider/device metadata, instead of leaking into runner ownership failures.

Example direct-proxy usage:

# Device host Mac: start one long-lived proxy
agent-device proxy --port 4310
# Expose it through a tunnel
cloudflared tunnel --url http://127.0.0.1:4310
# or: ngrok http 4310
# Agent/client A
agent-device connect proxy \
  --daemon-base-url https://example.trycloudflare.com \
  --daemon-auth-token <token>
agent-device open Settings --platform ios --device "iPhone 17 Pro"
agent-device snapshot -i
agent-device click @e2
agent-device close
agent-device disconnect
# Agent/client B can share the same proxy
agent-device connect proxy \
  --daemon-base-url https://example.trycloudflare.com/agent-device \
  --daemon-auth-token <token>
agent-device open Maps --platform ios --device "iPhone 17 Pro Max"
agent-device snapshot -i
agent-device close
agent-device disconnect

If client B targets the exact device currently leased by client A, it gets a device-lease busy error and can retry after A closes or after the inactivity TTL expires. The proxy process itself is intended to stay up.

Implementation scope spans remote connection CLI/profile handling, daemon lease admission/session lifecycle, provider-aware device contention, generated non-secret proxy profiles, iOS runner lease diagnostics, docs, and focused regression coverage.

Touched-file count: 61.

Validation

Verified with focused lease/proxy coverage, static gates, and build:

  • pnpm exec vitest run src/daemon/__tests__/lease-context.test.ts src/daemon/__tests__/lease-registry.test.ts src/core/__tests__/lease-scope.test.ts src/__tests__/remote-connection.test.ts src/daemon/__tests__/lease-lifecycle.test.ts src/daemon/__tests__/request-execution-scope.test.ts src/platforms/ios/__tests__/runner-session.test.ts passed: 7 files / 142 tests.
  • pnpm exec vitest run src/__tests__/remote-connection.test.ts src/daemon/__tests__/lease-registry.test.ts passed after the final cleanup: 2 files / 49 tests.
  • pnpm format passed.
  • pnpm check:quick passed.
  • pnpm check:fallow --base origin/main passed: no issues in 61 changed files.
  • pnpm build passed.
  • pnpm check:unit unit phase passed: 277 files / 2719 tests. The aggregate still exits in test/integration/smoke-daemon-clean.test.ts:40 on the known daemon PID liveness assertion, outside the changed lease/proxy paths.

Manual proxy verification exercised one host proxy with multiple sandboxed clients opening stock apps on separate iOS simulators, confirmed same-device contention returns DEVICE_LEASE_BUSY, and confirmed close/reacquire works without restarting the proxy.