Device agents on hosts¶
Adding a Mac?
The quick way is Add a Mac in two minutes: hearth hosts add and one pasted line.
This page explains what the agent does, and the older way of configuring a host by hand in
the chart values.
iOS simulators only run on macOS, and Android emulators need hardware virtualisation that many
clusters do not have. So devices can live on machines outside the cluster. Each such machine runs
hearth-agent, which reports its devices to the gateway and gets them ready for sessions. The
same binary runs inside the in-cluster emulator pods.
What the agent does¶
- Android emulators (macOS or Linux with KVM): each device is one emulator plus one Appium server (UiAutomator2). Between sessions the emulator loads a clean Quick Boot snapshot.
- iOS simulators (macOS with Xcode): each device is an APFS clone of a golden simulator plus one Appium server (XCUITest) with its own WebDriverAgent and MJPEG ports. Between sessions the used clone is deleted and a fresh one is made; simulators are never erased.
- It reports its devices to the gateway every few seconds, and at once after any change.
- For a session it downloads the app from the gateway, installs it, starts recording, and returns
the capabilities Appium needs to launch it. Afterwards it uploads
video.mp4anddevice.log, resets the device and reports it ready again. - It serves each device's screen as an MJPEG live view, and forwards the session's W3C commands to the device's Appium, which listens only on the host itself.
The agent never touches Redis or object storage. The gateway holds the only credentials for both.
Requirements¶
| Android | iOS | |
|---|---|---|
| Host | macOS or Linux with KVM | macOS with Xcode and simulator runtimes |
| Tools | Android SDK (platform-tools, emulator, build-tools for aapt2), an AVD |
xcrun simctl, plutil |
| Appium | Appium with the uiautomator2 driver |
Appium with the xcuitest driver |
| Other | ffmpeg for the live view |
A prebuilt WebDriverAgent (recommended) |
1. Declare the pools and the host¶
The gateway accepts devices only for pools that exist as a DevicePool, and only from a host it
has a token for. In the chart values:
agents:
- host: mac-1
devicePools:
- name: iphone17-ios27
platform: ios
deviceName: iPhone 17
osVersion: "27.2"
- name: pixel8-api35
platform: android
deviceName: Pixel 8
osVersion: "15"
helm upgrade hearth oci://ghcr.io/test-zeus-ai/charts/hearth -n hearth -f values.yaml
hearth agent-token mac-1 # prints the host's token
- The token lives in the Secret
<release>-agent-<host>(keytoken), labelledhearth.testzeus.com/agent: <host>. It is generated once and kept across upgrades and uninstalls.hearth agent-tokenreadshearth-agent-<host>, so it assumes the release is namedhearth. - The operator publishes the SHA-256 of every agent token within a few seconds. Only the hash is stored in Redis.
- To rotate a token, delete the Secret and run
helm upgradeagain (a new one is generated), or write a new value into the Secret yourself. Restart the agent with the new token.
Copy the token to the host through your usual secret channel; do not paste it in chat or commit it. A file readable only by the agent's user works well:
2. Write the agent's configuration¶
A TOML file. pool names must match your DevicePools, and host must match the token.
gateway_url = "https://hearth.example.com"
# The token: one of agent_token, agent_token_file, or the HEARTH_AGENT_TOKEN variable.
agent_token_file = "/etc/hearth/agent-token"
# Unique per host: letters, digits, - and _. Device ids are "<host>-<pool>-<n>".
host = "mac-1"
# How the gateway reaches this agent: an address gateway pods can route to.
agent_url = "http://10.0.0.5:4800"
listen = "0.0.0.0:4800" # default
work_dir = "/var/lib/hearth-agent" # default: $TMPDIR/hearth-agent
[[devices]]
pool = "pixel8-api35"
platform = "android"
avd = "hearth-pixel8"
count = 1
[[devices]]
pool = "iphone17-ios27"
platform = "ios"
device_type = "iPhone 17" # from `xcrun simctl list devicetypes`
runtime = "iOS 27.2" # from `xcrun simctl list runtimes`; use the identifier if names clash
count = 2
Every key:
| Key | Default | Meaning |
|---|---|---|
gateway_url |
(required) | The farm's URL, the same one tests use |
agent_token, agent_token_file |
The agent token, inline or from a file; or set HEARTH_AGENT_TOKEN |
|
host |
(required) | The host name the token was issued for |
agent_url |
(required) | Where the gateway reaches this agent; http(s), not loopback or link-local |
listen |
0.0.0.0:4800 |
Address the agent listens on |
work_dir |
$TMPDIR/hearth-agent |
Apps, recordings, logs (<work_dir>/logs/ for emulator and Appium logs) |
heartbeat_secs |
3 |
Heartbeat interval |
app_cache_entries |
8 |
Uploaded apps kept on disk (least recently used are dropped) |
[appium] bin, args |
appium, [] |
Appium command and extra appium server arguments |
[ports] appium, wda, mjpeg, android_system, emulator_console |
4723, 8100, 9100, 8200, 5554 |
Base ports. Device i (counted from 0 across the file) uses base + i; emulator console ports go up by two |
[android] sdk_root |
ANDROID_HOME, ANDROID_SDK_ROOT or the usual places |
Android SDK |
[android] snapshot |
hearth-clean |
Name of the clean Quick Boot snapshot |
[android] window |
false |
Show emulator windows |
[android] emulator_args |
[] |
Extra emulator arguments, e.g. ["-gpu", "swiftshader_indirect"] |
[android] boot_timeout_secs |
300 |
|
[ios] wda_derived_data |
unset | DerivedData of a prebuilt WebDriverAgent; adds appium:usePrebuiltWDA and appium:derivedDataPath |
[ios] golden_settle_secs |
30 |
How long a new golden simulator settles after first boot |
[ios] boot_timeout_secs, wda_launch_timeout_secs |
240, 240 |
|
[[devices]] pool, platform |
(required) | The DevicePool and android or ios |
[[devices]] avd |
Android: the AVD to run | |
[[devices]] device_type, runtime |
iOS: simulator type and runtime | |
[[devices]] count |
1 |
How many devices of this group |
[[devices]] orientation |
landscape for iPads, else portrait |
iOS start orientation |
Flags and environment variables override the main keys: --config (HEARTH_AGENT_CONFIG),
--gateway-url (HEARTH_GATEWAY_URL), --host (HEARTH_AGENT_HOST), --agent-url
(HEARTH_AGENT_URL), --listen (HEARTH_AGENT_LISTEN). Logs go to stderr; RUST_LOG sets the
level and HEARTH_LOG_FORMAT=json switches to JSON.
3. Run it¶
The release archives (hearth-<platform>.tar.gz on the
releases page) contain hearth-agent next to
hearth. Or build it: cargo build --release -p hearth-agent.
What you should see:
- iOS: the first start takes a few minutes per template: the golden simulator is created, booted, left to settle and shut down. Later starts reuse it.
- Android: on every start the emulator boots, a warm-up session installs Appium's UiAutomator2 server, and the clean snapshot is saved with it. If booting from the snapshot fails, the emulator is cold-booted once and the snapshot saved again.
- Then
kubectl -n hearth get devicepoolsshows the devices asREADY.
hearth-agent --config agent.toml cleanup deletes the simulators the agent made (every simulator
whose name starts with hearth-); add --goldens to delete the golden simulators too.
There is no installer command. Run the agent under your service manager. These are examples to adapt, not files shipped with Hearth.
macOS: a LaunchAgent (example)
Simulators need a logged-in user session, so use a LaunchAgent of the user that owns Xcode,
not a LaunchDaemon. Save as ~/Library/LaunchAgents/com.testzeus.hearth.agent.plist, then
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.testzeus.hearth.agent.plist.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key><string>com.testzeus.hearth.agent</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/hearth-agent</string>
<string>--config</string>
<string>/Users/ci/hearth/agent.toml</string>
</array>
<key>EnvironmentVariables</key>
<dict>
<!-- appium, node and the Android SDK tools must be on this PATH -->
<key>PATH</key><string>/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin</string>
</dict>
<key>RunAtLoad</key><true/>
<key>KeepAlive</key><true/>
<key>StandardErrorPath</key><string>/Users/ci/hearth/agent.log</string>
</dict>
</plist>
Linux: a systemd unit (example)
For Android emulators. The user needs access to /dev/kvm (usually the kvm group). Save as
/etc/systemd/system/hearth-agent.service, then systemctl enable --now hearth-agent.
[Unit]
Description=Hearth device agent
After=network-online.target
Wants=network-online.target
[Service]
User=hearth
SupplementaryGroups=kvm
Environment=ANDROID_HOME=/opt/android-sdk
ExecStart=/usr/local/bin/hearth-agent --config /etc/hearth/agent.toml
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
4. Networking¶
Traffic goes both ways:
| From | To | What |
|---|---|---|
| Agent | The gateway URL, path /internal/agent/... |
Heartbeats, app downloads, artifact uploads (bearer agent token) |
| Gateway pods | The agent's agent_url |
Prepare and release a device, live view, and every Appium command of a session (bearer token the agent chose) |
- The agent dials the gateway. It needs only outbound access to the farm's URL. On macOS this also avoids Local Network restrictions, which hit inbound-initiated connections from LaunchAgents, not outbound ones.
- The gateway must reach the agent.
agent_urlmust be an http(s) address that gateway pods can route to: a private network shared with the cluster, a VPN, or a tunnel. The gateway refuses heartbeats whoseagentUrlis loopback (localhost,127.0.0.1), link-local or a cloud metadata name (400 bad_agent_url). - Appium stays on the host. Each Appium listens on the host, and the gateway reaches it only
through the agent, which forwards W3C session commands for a device's running session and
nothing else. The
appiumUrlfield in heartbeats is not used for routing. - The agent checks a bearer token on every call except
GET /healthz. Serve it over a private network or put TLS in front (for example a reverse proxy, withagent_urlset tohttps://...); allow the agent's port only from the cluster's egress addresses.
Check that devices joined¶
READY counts devices the agents reported free, BUSY those in a session. A device whose last
heartbeat is more than 15 seconds old is never handed out, even if it is still counted as ready.
Heartbeats are refused when something does not match; the agent logs the gateway's answer:
| Answer | Why |
|---|---|
401 bad_token |
The token is unknown: wrong token, or the Secret lacks the hearth.testzeus.com/agent label |
403 wrong_host |
host is not the token's host |
403 wrong_device |
A device id does not start with <host>- |
403 unknown_pool |
A pool is not a DevicePool of that platform (or is a browser pool's name) |
400 bad_agent_url |
agent_url is not an http(s) URL the gateway may call |
Device states¶
booting → ready → busy → resetting → ready. A reset is tried twice; after the second failure the
device is failed. Failed devices are rebooted every two minutes and come back as ready when
that works.
Running a Mac host well¶
- One Appium per simulator. Use a prebuilt WebDriverAgent (
wda_derived_data) so sessions do not build it. - iPad templates boot in landscape by default; set
orientationto change that. - Treat a Mac host as single-tenant. Simulators share the Mac's user account, keychain and file system. Do not mix teams with different trust levels on one Mac, and do not use it for anything else.
- Simulators named
hearth-*belong to the agent. Do not use those names for your own.
Known limits¶
- Several Android devices from one AVD run with
-read-onlyand share the snapshot that exists. Run that AVD once withcount = 1first to create it. - The iOS live view works while WebDriverAgent runs, which is from the warm-up on. If a client session restarts WebDriverAgent, the stream reconnects only when the viewer reconnects.
- The app cache downloads one app at a time.
- Reserved pools that keep a team's apps between sessions are not built yet.
The protocol¶
For agent authors and for debugging.
Agent to gateway¶
With Authorization: Bearer <agent token>.
POST /internal/agent/heartbeat, every heartbeat_secs and after any state change:
{
"host": "mac-1",
"agentUrl": "http://10.0.0.5:4800",
"agentToken": "<random, made at agent start>",
"devices": [
{
"id": "mac-1-iphone17-ios27-1",
"pool": "iphone17-ios27",
"platform": "ios",
"deviceName": "iPhone 17",
"osVersion": "27.2",
"state": "ready",
"appiumUrl": "http://127.0.0.1:4724",
"caps": {"appium:udid": "6F1C..."},
"session": null
}
]
}
stateisbooting,ready,busy,resettingorfailed. While the gateway holds a device for a session, the agent'sstatedoes not take it away; if the agent reportsreadywith nosessionduring a session (it restarted), the session is marked failed.capsare device-owned Appium capabilities (udid, ports, automation name); they override what a client sends.agentTokenis 32 random bytes made at agent start; it changes when the agent restarts.
GET /internal/agent/apps/{sha256} downloads an uploaded app; the agent checks the hash before
use. PUT /internal/agent/sessions/{session}/artifacts/{name} uploads video.mp4 or
device.log for a session on one of this host's devices (201).
Gateway to agent¶
With Authorization: Bearer <agentToken from the heartbeat>:
| Call | Body | Answer |
|---|---|---|
POST /devices/{id}/prepare |
{"session": "...", "app": {"sha256", "name"} \| null} |
200 {"caps": {...}}: device caps plus appium:appPackage and appium:appActivity (APK) or appium:bundleId (iOS). 409 not ready, 404 unknown device, 500 install or recording failed |
POST /devices/{id}/release |
{"session": "..."} |
202; repeating it is also 202; 409 for another session |
GET /devices/{id}/stream |
MJPEG (multipart/x-mixed-replace) |
|
ANY /devices/{id}/appium/session[/...] |
W3C command | Forwarded to the device's Appium while it has a session; anything outside /session is refused |
GET /healthz |
200 ok, no token |