feat: add daemon policy to confine devices, commands, and device shutdown

Merged
SZ
2mo ago
+1216-43

Summary

Operators can now confine a daemon, for example a remote agent-device proxy host, to one simulator and stop agents from shutting it down. The config is one JSON file:

{
  "version": 1,
  "devices": { "allow": [{ "udid": "<simulator-udid>" }] },
  "commands": { "deny": ["boot", "shutdown"] },
  "capabilities": { "deny": ["device-shutdown"] }
}
AGENT_DEVICE_DAEMON_POLICY=./policy.json agent-device proxy

Before: the only hook was AGENT_DEVICE_HTTP_AUTH_HOOK. It sees top-level RPCs only, so batch steps (where shutdown is allowed), replay actions, device names resolved inside the daemon, and close --shutdown all got past it.
After: the daemon itself checks the policy at three points (ADR 0029):

  1. Request gate in createRequestExecutionScope. Denied commands, a batch that names a denied step, close --shutdown, and an explicit foreign --udid/--serial are refused before any lock or device resolution. batch steps and replay actions call this function again, so they are checked too.
  2. Device scope. Runtime bindings refuse a resolved device outside the scope before the gateway binds it. Device inventory, and so devices and automatic selection, lists only allowed devices.
  3. Capability gate. The device-shutdown host capability refuses every shutdown, whichever command asks.

The daemon loads the policy once and refuses to start if the file is not valid. A denial returns UNAUTHORIZED with details.reason: "DAEMON_POLICY_DENIED", the rule, and a do-not-retry hint. It never includes the policy path. The daemon writes the policy digest to daemon.json. A client that names a policy refuses to reuse a daemon that enforces a different policy or none. Without AGENT_DEVICE_DAEMON_POLICY, behavior does not change.

Docs: ADR 0029, the ADR index, remote help text, and website/docs/docs/remote-proxy.md. There is no CONTEXT.md entry: the glossary is at its 12,000-byte budget, so ADR 0029 names the term.

Validation

  • Live check on an iOS 26 simulator (iPhone 17), with an isolated state dir and the policy above:
    • devices --platform ios listed only the pinned simulator. The host has 8 available.
    • open --udid <other simulator> was denied with rule: device.
    • open com.apple.Preferences with no device selected opened on the pinned simulator.
    • snapshot worked.
    • shutdown, batch [snapshot, shutdown] (no step ran), and close --shutdown were all denied, both over the socket and over the HTTP transport in dual mode. The simulator stayed Booted throughout.
    • A caller that named a different policy file got DAEMON_POLICY_MISMATCH with a daemon stop --state-dir hint.
  • Live check on an Android 37.2 emulator (emulator-5554, policy pinning serial):
    • devices listed only the emulator; the booted iOS simulators were hidden.
    • open --serial emulator-5556 was denied with rule: device.
    • open com.android.settings with no device selected opened on the emulator, and snapshot worked.
    • shutdown, batch [snapshot, close --shutdown], and close --shutdown were all denied. The emulator stayed booted.
  • New tests cover the router, parsing, the shutdown host guard, and the reuse check. I disabled each layer in turn (request gate, bind gate, inventory filter, reuse check), and each time its test failed.
  • Checks: the layering, depgraph, agent-guidance, production-exports, fallow, and bundle-owner-files gates pass locally. pnpm check:unit passed (11666 unit tests; smoke 71/71 after pnpm build). pnpm test:integration:provider passed (217). pnpm typecheck, pnpm lint, and pnpm check:fallow --base origin/main are clean.

Tradeoffs / follow-ups

  • The policy applies to every transport, not only remote requests. Origin markers such as publicNetworkOnly must be copied to child requests by hand, so a rule based on origin could leak. A host operator can use simctl/adb directly or a separate --state-dir.
  • Command rules come from the command registry: public commands by name, internal device commands by the public command they serve (new catalog.servesPublicCommand trait) or by their own name, and protocol-only internal commands are never decided. An allow list must name runtime for react-devtools and Maestro flows. A name the registry does not know is denied under an allow list.
  • boot is allowed unless the policy denies it. There is no admin route that skips the policy.
  • The policy is not yet readable over RPC or /health. Agents learn it from denial errors, and a client connected to a remote daemon cannot verify which policy that daemon enforces.
  • lease_allocate for a device outside the scope is not refused when it is allocated. The first bind refuses it.
  • A local takeover (for example, asking for a transport that the daemon does not serve) starts a replacement daemon with the caller's environment. Remote clients cannot trigger takeover. This is documented in the ADR.
  • I did not check test (Maestro) steps directly. They dispatch through the replay port.

25 files touched (6 new). Scope: daemon request admission, runtime binding, the shutdown host capability, daemon client reuse, one command-registry catalog trait (servesPublicCommand), shared daemon-client test helpers, and docs.

Diff size: 25 files changed, 1216 insertions(+), 43 deletions(-), which is over the 1,000-line gross budget in docs/agents/pull-requests.md. Production code is 459+/15- lines (excluding tests); the rest is tests, ADR 0029, and docs.

šŸ¤– Generated with Claude Code