feat(remote): add a host-allocated macos-app lease backend

Merged
JA
2mo ago
+2899-110

Summary

Implements the macos-app lease backend from #3229, which confines a proxy client to one macOS app:

  • The host allocates the lease on the daemon's loopback listener with the daemon token: PUT /admin/leases/<id> with { tenantId, runId, clientId, leaseBackend: "macos-app", leaseProvider, deviceKey: "<bundleId>@<pid>", ttlMs }; renew by repeating it, revoke with DELETE. Tenant lease_allocate for macos-app is refused.
  • Requests under the lease (including batch steps) are limited to the commands whose registry descriptor declares appLease: 'allowed', lease-admission-exempt ones included (only lease_heartbeat and lease_release declare it). Every request except open, the batch envelope, heartbeat and release must run in an existing session of the leased app. open/close take only the leased bundle, only the app surface is allowed, --fullscreen, host-path or launch inputs (including a batch step's runtime, checked before any step runs) and device selectors (--udid, --serial, --device, --target) are refused, open and batch must carry --platform macos, open requires AGENT_DEVICE_MACOS_APP_BACKEND=native, and a pid-pinned lease stops at process exit. Refusals are UNAUTHORIZED with reason: MACOS_APP_LEASE_DENIED.
  • A session under the lease takes no host device claim, so one daemon serves several leased apps.
  • Daemon policy leases.require: "macos-app" (its only value) refuses requests that name no lease. /health reports leaseBackends, with macos-app only on darwin. A tenant heartbeat cannot renew a host-allocated lease past the host's ttlMs.
  • The native macOS backend's press/focus/fill act only inside the app's own on-screen windows and never on its menu bar, whose Apple menu acts for the whole Mac. This applies to the native backend generally.
  • Responses under the lease name nothing about the host: open omits the session paths and the device, failures omit logPath and diagnosticsRecord and redact host paths and the host name, a snapshot's fallback screenshot path is dropped, and the request diagnostics route is not served to a tenant that held the lease.
  • Client: a remote config naming a macos-app lease uses it as given and never allocates one.

47 files, over the 1,000-line budget (mostly tests and docs); happy to split.

Open questions: requiring the native backend, and multi-client proxies (the proxy token is shared, so the host pins tenant and lease per client).

Validation

At 7dfeb33, except the response-redaction item:

  • Checks: pnpm check:affected --run (format, lint, typecheck, layering, fallow, build, 715 vitest files, daemon wire compat) passed at 332c35f, except for timeouts in unrelated tests under heavy host load: daemon-registration-owner.test.ts and two provider-integration scenarios, which passed on rerun. 7dfeb33 only moves one helper; lint, typecheck, layering, fallow, the eager-closure budget and the touched tests passed again. The macOS helper builds with -warnings-as-errors, and pnpm test:macos-helper passes. CI is green at 7dfeb33 (12 checks pass, 3 skipped).
  • Response redaction (937471b): pnpm check:affected --run passed (716 vitest files), and CI is green at 937471b. Tests cover open and a refused request through the request handler under the lease (no state dir, host path or device name in the wire response; an unleased open is unchanged), open --foreground failures, the fallback screenshot path, the diagnostics route, and the client reading an open without device or paths. Not re-run live.
  • New tests: lease-exempt doctor and session_list refused through createRequestExecutionScope under leases.require; sessionless snapshot/screenshot/wait/find/get/is refused by assertRequestLeaseAdmission; a batch [snapshot, open] before open refused at step 1 through the request handler; the heartbeat cap; /health per host platform.
  • Live, two Macs, at edf873e: a client Mac ran the PR build against agent-device proxy on a host Mac running macOS 27, with AGENT_DEVICE_DAEMON_POLICY {"version":1,"leases":{"require":"macos-app"}}, AGENT_DEVICE_MACOS_APP_BACKEND=native, and a fixture app leased through PUT /admin/leases/<id>. The run refused sessionless snapshot/screenshot, doctor, session list, devices, another app, --fullscreen, off-window and menu-bar clicks, and every request after the admin DELETE. open, snapshot, click, fill, screenshot and close of the leased app worked. Transcript.
  • In practice: Stim (appandflow/stim) agents drive Stim Desktop hosted on another Mac through this backend (stim macos --host), and stim-server allocates each lease over the admin route.
  • Not verified: multi-client proxies (the host pins tenant and lease per client); the menu-bar ancestor check in the helper, which did not fire live because the window check refused first; native-backend actions on windows on another Space or minimized, which are now refused.