# callstack/agent-device architecture

> Architecture diagram of the GitHub repository callstack/agent-device, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/callstack/agent-device
- Repository: https://github.com/callstack/agent-device

## Overview

#### Purpose and Entry Points
`agent-device` is a Node.js CLI, SDK, and MCP server for agent-driven inspection, interaction, verification, recording, and replay across mobile, TV, desktop, and web targets. Primary executable wiring begins at `bin/agent-device.mjs`, `src/bin.ts`, and `src/cli.ts`. Public programmatic APIs are exposed through `src/sdk/index.ts`; MCP transport/server entry is `src/mcp/server.ts`.

#### CLI-to-Daemon Control Plane
The CLI parses command grammar, resolves local/remote connection profiles, and dispatches commands to a persistent local daemon rather than directly owning device state. Command definitions/projections live in `src/commands/index.ts`, `src/commands/command-surface.ts`, and `src/cli/commands/router.ts`. The daemon client/RPC boundary is `src/daemon/client/daemon-client.ts` and `src/daemon/client/daemon-client-rpc.ts`; daemon process startup is `src/daemon/daemon-process.ts`.

#### Daemon: Session-Oriented Orchestrator
The daemon owns HTTP/RPC handling, device claims/leases, request serialization, session state, artifacts, and cleanup. Its key boundary is request routing through `src/daemon/request-router.ts`, scoped execution in `src/daemon/request-execution-scope.ts`, and runtime session lifecycle in `src/daemon/runtime-session.ts`. Session handlers translate command intents into platform/runtime operations; examples include `src/daemon/handlers/session-open.ts`, `src/daemon/handlers/interaction.ts`, and `src/daemon/handlers/snapshot.ts`. Durable recording/capture resources survive request boundaries via `src/daemon/durable-capture-resource-store.ts` and related recovery flows.

#### Contracts, Core Dispatch, and Selector/Snapshot Model
Platform-neutral types and capability contracts isolate command orchestration from implementations: `packages/contracts/src/platform-runtime.ts`, `packages/contracts/src/snapshot-types.ts`, and `packages/contracts/src/interaction.ts`. Core dispatch selects plugins/interactors and normalizes backend results in `src/core/dispatch.ts`, `src/core/platform-plugin-registry.ts`, and `src/core/snapshot-tree-ingestion.ts`. Accessibility snapshots produce session-scoped refs; selectors resolve against normalized trees through `packages/selectors/src/index.ts` and `src/core/selector-pipeline.ts`. This is the principal correctness boundary for inspect → act → settle/verify behavior.

#### Platform and Provider Runtime Layer
Local platform packages implement inventory, lifecycle, deployment, logging, networking, and recording behind contracts: `packages/platform-android/src/runtime.ts`, `packages/platform-apple/src/runtime.ts`, and `packages/platform-harmonyos/src/runtime.ts`. Host-specific execution is composed in `src/platform-runtime.ts` and `src/platform-runtime-host.ts`. Supported local backends use ADB/Android helpers (`src/platforms/android/adb.ts`, `src/platforms/android/snapshot-helper-session.ts`), Apple tooling plus XCTest runner (`src/platforms/apple/core/runner/runner-client.ts`), HDC/ArkUI, AT-SPI, Vega tooling, and agent-browser web support. Cloud/provider boundaries are implemented by `packages/provider-limrun/src/runtime.ts` and `packages/provider-webdriver/src/runtime.ts`, including BrowserStack and AWS Device Farm adapters.

#### Native Automation Sidecars
Android’s instrumented snapshot helper captures accessibility trees and injects touch plans; its protocol and instrumentation are in `android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/HelperProtocol.java` and `android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/SnapshotInstrumentation.java`. Apple automation runs through an Xcode XCTest UI-test target, centered on `apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift` and command transport in `RunnerTests+Transport.swift`. macOS has a Swift accessibility/audio helper at `apple/macos-helper/Sources/AgentDeviceMacOSHelper/main.swift`.

#### Recording, Replay, and Evidence
Session actions can be written as `.ad` scripts, replayed with target verification/resume semantics, or exported to Maestro-compatible YAML. Script format and annotations are in `packages/ad-script/src/index.ts`; replay step execution is in `packages/ad-replay/src/internal/step-loop.ts`; daemon replay coordination is `src/daemon/handlers/session-replay-runtime.ts`. The direct Maestro engine parses YAML, compiles plans, and executes through a runtime port in `packages/maestro/src/internal/engine.ts` and `packages/maestro/src/internal/replay-plan-compilation.ts`. Screenshots, video, logs, network data, and other evidence pass through capture/runtime artifacts managed by `packages/capture-kit/src/index.ts` and `src/daemon/runtime-artifacts.ts`.

Main entry points: [`src/core/dispatch.ts`](https://github.com/callstack/agent-device/blob/main/src/core/dispatch.ts) (Core dispatch), [`src/daemon/runtime-session.ts`](https://github.com/callstack/agent-device/blob/main/src/daemon/runtime-session.ts) (Runtime session), [`src/platform-runtime-host.ts`](https://github.com/callstack/agent-device/blob/main/src/platform-runtime-host.ts) (Platform runtime host), [`src/daemon/client/daemon-client-rpc.ts`](https://github.com/callstack/agent-device/blob/main/src/daemon/client/daemon-client-rpc.ts) (Daemon client RPC)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Public interfaces"]
  node_cli["CLI entry and grammar<br/>Node.js CLI<br/>[cli.ts]"]
  node_command_surface["Command surface<br/>command definitions<br/>[command-surface.ts]"]
  node_sdk["SDK API<br/>programmatic API<br/>[index.ts]"]
  node_mcp["MCP server<br/>MCP transport<br/>[server.ts]"]
end

subgraph group_daemon["Daemon control plane"]
  node_daemon_client["Daemon client RPC<br/>local RPC client"]
  node_daemon_process["Daemon process<br/>persistent process<br/>[daemon-process.ts]"]
  node_request_router["Request router<br/>daemon HTTP/RPC<br/>[request-router.ts]"]
  node_execution_scope["Execution scope<br/>lease and serialization"]
  node_runtime_session["Runtime session<br/>session lifecycle<br/>[runtime-session.ts]"]
  node_session_handlers["Session handlers<br/>intent handlers<br/>[interaction.ts]"]
end

subgraph group_core["Core execution"]
  node_contracts["Platform contracts<br/>capability contracts"]
  node_dispatch["Core dispatch<br/>plugin dispatcher<br/>[dispatch.ts]"]
  node_selector_pipeline["Selector pipeline<br/>snapshot selection"]
end

subgraph group_runtime["Platform runtimes"]
  node_platform_host["Platform runtime host<br/>runtime composition"]
  node_android_runtime["Android runtime<br/>local platform runtime<br/>[runtime.ts]"]
  node_apple_runtime["Apple runtime<br/>local platform runtime<br/>[runtime.ts]"]
  node_provider_runtime["Cloud provider runtime<br/>provider adapter<br/>[runtime.ts]"]
  node_android_sidecar{{"Android snapshot helper<br/>instrumentation sidecar"}}
  node_apple_sidecar{{"Apple XCTest runner<br/>XCTest sidecar<br/>[RunnerTests.swift]"}}
end

subgraph group_evidence["Replay and evidence"]
  node_replay["Replay coordinator<br/>daemon replay handler"]
  node_ad_script["AD scripts<br/>script format<br/>[index.ts]"]
  node_artifacts[("Runtime artifacts<br/>evidence manager")]
end

node_cli -->|"parses"| node_command_surface
node_cli -->|"dispatches"| node_daemon_client
node_sdk -->|"controls"| node_daemon_client
node_mcp -->|"controls"| node_daemon_client
node_daemon_client -->|"starts/connects"| node_daemon_process
node_daemon_process -->|"hosts"| node_request_router
node_request_router -->|"scopes request"| node_execution_scope
node_execution_scope -->|"claims and serializes"| node_runtime_session
node_runtime_session -->|"invokes"| node_session_handlers
node_session_handlers -->|"executes intents"| node_dispatch
node_dispatch -->|"uses capabilities"| node_contracts
node_dispatch -->|"normalizes snapshots"| node_selector_pipeline
node_dispatch -->|"selects runtime"| node_platform_host
node_platform_host -->|"local backend"| node_android_runtime
node_platform_host -->|"local backend"| node_apple_runtime
node_platform_host -->|"remote backend"| node_provider_runtime
node_android_runtime -->|"accessibility and touch"| node_android_sidecar
node_apple_runtime -->|"UI-test transport"| node_apple_sidecar
node_runtime_session -->|"stores evidence"| node_artifacts
node_runtime_session -->|"runs replay"| node_replay
node_replay -->|"reads steps"| node_ad_script
node_replay -->|"re-executes and verifies"| node_dispatch

click node_cli "https://github.com/callstack/agent-device/blob/main/src/cli.ts"
click node_command_surface "https://github.com/callstack/agent-device/blob/main/src/commands/command-surface.ts"
click node_sdk "https://github.com/callstack/agent-device/blob/main/src/sdk/index.ts"
click node_mcp "https://github.com/callstack/agent-device/blob/main/src/mcp/server.ts"
click node_daemon_client "https://github.com/callstack/agent-device/blob/main/src/daemon/client/daemon-client-rpc.ts"
click node_daemon_process "https://github.com/callstack/agent-device/blob/main/src/daemon/daemon-process.ts"
click node_request_router "https://github.com/callstack/agent-device/blob/main/src/daemon/request-router.ts"
click node_execution_scope "https://github.com/callstack/agent-device/blob/main/src/daemon/request-execution-scope.ts"
click node_runtime_session "https://github.com/callstack/agent-device/blob/main/src/daemon/runtime-session.ts"
click node_session_handlers "https://github.com/callstack/agent-device/blob/main/src/daemon/handlers/interaction.ts"
click node_contracts "https://github.com/callstack/agent-device/blob/main/packages/contracts/src/platform-runtime.ts"
click node_dispatch "https://github.com/callstack/agent-device/blob/main/src/core/dispatch.ts"
click node_selector_pipeline "https://github.com/callstack/agent-device/blob/main/src/core/selector-pipeline.ts"
click node_platform_host "https://github.com/callstack/agent-device/blob/main/src/platform-runtime-host.ts"
click node_android_runtime "https://github.com/callstack/agent-device/blob/main/packages/platform-android/src/runtime.ts"
click node_apple_runtime "https://github.com/callstack/agent-device/blob/main/packages/platform-apple/src/runtime.ts"
click node_provider_runtime "https://github.com/callstack/agent-device/blob/main/packages/provider-webdriver/src/runtime.ts"
click node_android_sidecar "https://github.com/callstack/agent-device/blob/main/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/SnapshotInstrumentation.java"
click node_apple_sidecar "https://github.com/callstack/agent-device/blob/main/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift"
click node_replay "https://github.com/callstack/agent-device/blob/main/src/daemon/handlers/session-replay-runtime.ts"
click node_ad_script "https://github.com/callstack/agent-device/blob/main/packages/ad-script/src/index.ts"
click node_artifacts "https://github.com/callstack/agent-device/blob/main/src/daemon/runtime-artifacts.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_command_surface,node_sdk,node_mcp toneBlue
class node_daemon_client,node_daemon_process,node_request_router,node_execution_scope,node_runtime_session,node_session_handlers toneAmber
class node_contracts,node_dispatch,node_selector_pipeline toneMint
class node_platform_host,node_android_runtime,node_apple_runtime,node_provider_runtime,node_android_sidecar,node_apple_sidecar toneRose
class node_replay,node_ad_script,node_artifacts toneIndigo
```

## Components

### Public interfaces

- [CLI entry and grammar](https://github.com/callstack/agent-device/blob/main/src/cli.ts): Node.js CLI · Parses commands and dispatches them to the daemon. · `src/cli.ts`
- [Command surface](https://github.com/callstack/agent-device/blob/main/src/commands/command-surface.ts): command definitions · `src/commands/command-surface.ts`
- [SDK API](https://github.com/callstack/agent-device/blob/main/src/sdk/index.ts): programmatic API · `src/sdk/index.ts`
- [MCP server](https://github.com/callstack/agent-device/blob/main/src/mcp/server.ts): MCP transport · `src/mcp/server.ts`

### Daemon control plane

- [Daemon client RPC](https://github.com/callstack/agent-device/blob/main/src/daemon/client/daemon-client-rpc.ts): local RPC client · `src/daemon/client/daemon-client-rpc.ts`
- [Daemon process](https://github.com/callstack/agent-device/blob/main/src/daemon/daemon-process.ts): persistent process · Starts and hosts the local device-state owner. · `src/daemon/daemon-process.ts`
- [Request router](https://github.com/callstack/agent-device/blob/main/src/daemon/request-router.ts): daemon HTTP/RPC · `src/daemon/request-router.ts`
- [Execution scope](https://github.com/callstack/agent-device/blob/main/src/daemon/request-execution-scope.ts): lease and serialization · Scopes requests around claims, cleanup, and serialized execution. · `src/daemon/request-execution-scope.ts`
- [Runtime session](https://github.com/callstack/agent-device/blob/main/src/daemon/runtime-session.ts): session lifecycle · Owns session state and request-bound operations. · `src/daemon/runtime-session.ts`
- [Session handlers](https://github.com/callstack/agent-device/blob/main/src/daemon/handlers/interaction.ts): intent handlers · Translate open, interaction, and snapshot commands into runtime operations. · `src/daemon/handlers/interaction.ts`

### Core execution

- [Platform contracts](https://github.com/callstack/agent-device/blob/main/packages/contracts/src/platform-runtime.ts): capability contracts · Platform-neutral runtime, snapshot, and interaction boundary. · `packages/contracts/src/platform-runtime.ts`
- [Core dispatch](https://github.com/callstack/agent-device/blob/main/src/core/dispatch.ts): plugin dispatcher · Selects platform plugins and normalizes backend results. · `src/core/dispatch.ts`
- [Selector pipeline](https://github.com/callstack/agent-device/blob/main/src/core/selector-pipeline.ts): snapshot selection · Resolves selectors against normalized session snapshot trees. · `src/core/selector-pipeline.ts`

### Platform runtimes

- [Platform runtime host](https://github.com/callstack/agent-device/blob/main/src/platform-runtime-host.ts): runtime composition · Composes local and provider runtime implementations. · `src/platform-runtime-host.ts`
- [Android runtime](https://github.com/callstack/agent-device/blob/main/packages/platform-android/src/runtime.ts): local platform runtime · `packages/platform-android/src/runtime.ts`
- [Apple runtime](https://github.com/callstack/agent-device/blob/main/packages/platform-apple/src/runtime.ts): local platform runtime · `packages/platform-apple/src/runtime.ts`
- [Cloud provider runtime](https://github.com/callstack/agent-device/blob/main/packages/provider-webdriver/src/runtime.ts): provider adapter · Connects cloud targets including BrowserStack and AWS Device Farm. · `packages/provider-webdriver/src/runtime.ts`
- [Android snapshot helper](https://github.com/callstack/agent-device/blob/main/android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/SnapshotInstrumentation.java): instrumentation sidecar · Captures accessibility trees and injects touch plans. · `android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/SnapshotInstrumentation.java`
- [Apple XCTest runner](https://github.com/callstack/agent-device/blob/main/apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift): XCTest sidecar · Executes Apple UI automation commands. · `apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift`

### Replay and evidence

- [Replay coordinator](https://github.com/callstack/agent-device/blob/main/src/daemon/handlers/session-replay-runtime.ts): daemon replay handler · Coordinates script replay with verification and resume semantics. · `src/daemon/handlers/session-replay-runtime.ts`
- [AD scripts](https://github.com/callstack/agent-device/blob/main/packages/ad-script/src/index.ts): script format · `packages/ad-script/src/index.ts`
- [Runtime artifacts](https://github.com/callstack/agent-device/blob/main/src/daemon/runtime-artifacts.ts): evidence manager · Manages screenshots, video, logs, network data, and captures. · `src/daemon/runtime-artifacts.ts`

## Connections

- CLI entry and grammar → Command surface: parses
- CLI entry and grammar → Daemon client RPC: dispatches
- SDK API → Daemon client RPC: controls
- MCP server → Daemon client RPC: controls
- Daemon client RPC → Daemon process: starts/connects
- Daemon process → Request router: hosts
- Request router → Execution scope: scopes request
- Execution scope → Runtime session: claims and serializes
- Runtime session → Session handlers: invokes
- Session handlers → Core dispatch: executes intents
- Core dispatch → Platform contracts: uses capabilities
- Core dispatch → Selector pipeline: normalizes snapshots
- Core dispatch → Platform runtime host: selects runtime
- Platform runtime host → Android runtime: local backend
- Platform runtime host → Apple runtime: local backend
- Platform runtime host → Cloud provider runtime: remote backend
- Android runtime → Android snapshot helper: accessibility and touch
- Apple runtime → Apple XCTest runner: UI-test transport
- Runtime session → Runtime artifacts: stores evidence
- Runtime session → Replay coordinator: runs replay
- Replay coordinator → AD scripts: reads steps
- Replay coordinator → Core dispatch: re-executes and verifies
