How callstack/agent-device works
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: dispatch.ts Core dispatch · runtime-session.ts Runtime session · platform-runtime-host.ts Platform runtime host · daemon-client-rpc.ts Daemon client RPC
Components 22
Public interfaces
- CLI entry and grammar · Parses commands and dispatches them to the daemon.
src/cli.ts - Command surface
src/commands/command-surface.ts - SDK API
src/sdk/index.ts - MCP server
src/mcp/server.ts
Daemon control plane
- Daemon client RPC
src/daemon/client/daemon-client-rpc.ts - Daemon process · Starts and hosts the local device-state owner.
src/daemon/daemon-process.ts - Request router
src/daemon/request-router.ts - Execution scope · Scopes requests around claims, cleanup, and serialized execution.
src/daemon/request-execution-scope.ts - Runtime session · Owns session state and request-bound operations.
src/daemon/runtime-session.ts - Session handlers · Translate open, interaction, and snapshot commands into runtime operations.
src/daemon/handlers/interaction.ts
Core execution
- Platform contracts · Platform-neutral runtime, snapshot, and interaction boundary.
packages/contracts/src/platform-runtime.ts - Core dispatch · Selects platform plugins and normalizes backend results.
src/core/dispatch.ts - Selector pipeline · Resolves selectors against normalized session snapshot trees.
src/core/selector-pipeline.ts
Platform runtimes
- Platform runtime host · Composes local and provider runtime implementations.
src/platform-runtime-host.ts - Android runtime
packages/platform-android/src/runtime.ts - Apple runtime
packages/platform-apple/src/runtime.ts - Cloud provider runtime · Connects cloud targets including BrowserStack and AWS Device Farm.
packages/provider-webdriver/src/runtime.ts - Android snapshot helper · Captures accessibility trees and injects touch plans.
android/snapshot-helper/src/main/java/com/callstack/agentdevice/snapshothelper/SnapshotInstrumentation.java - Apple XCTest runner · Executes Apple UI automation commands.
apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift
Replay and evidence
- Replay coordinator · Coordinates script replay with verification and resume semantics.
src/daemon/handlers/session-replay-runtime.ts - AD scripts
packages/ad-script/src/index.ts - Runtime artifacts · Manages screenshots, video, logs, network data, and captures.
src/daemon/runtime-artifacts.ts
Connections 22
- 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