Repository navigation
Description
Request: #1103
Problem
In the reporter's words (#1103): "ADR 0019 keeps a lease across a worker restart when its device is running, and simlock status confirms that. A client that connected through the gateway's agent-device front still loses the lease a few seconds later: its commands answer lease ended by simlock; run disconnect, then open, and the device is wiped."
The same happens on a standalone worker, and when only agent-device's daemon restarts. agent-device keeps its leases in memory. After its restart it answers the next heartbeat LEASE_NOT_FOUND, and simlock's front reads that as the end of the lease. One worker restart, a crash like #1409 for example, ends every agent-device lease on the worker at once.
Who it is for
An agent or test runner driving a leased device through simlock's agent-device front, on a standalone worker or through a gateway, when the worker, the gateway or agent-device restarts under it.
Outcome
When a restart leaves the simlock lease alive, the agent keeps the same lease, the same device and the same app data. It sees retryable errors while the restart lasts, then runs open again and goes on. A lease that has really ended stays ended.
How it works for the user
flowchart TD
A[Agent runs a command] --> B[CLI heartbeats first]
B --> C{agent-device knows the lease?}
C -->|yes| OK[command runs]
C -->|no: restarted| D{simlock still holds it for this agent?}
D -->|no| E["lease ended by simlock; run disconnect, then open"]
D -->|yes| F[simlock allocates it again on the same device]
F -->|ok| G[the agent-device session is gone]
G --> H[agent runs open: same device, same app]
F -->|fails| R[retryable error; lease kept; next command tries again]
What each command answers, before:
agent-device snapshot # right after the worker restarted
→ agent-device is not available on this simlock: starting
agent-device snapshot
→ lease ended by simlock; run disconnect, then open
# the simlock lease is released, the device wiped
After:
agent-device snapshot # right after the worker restarted
→ agent-device is not available on this simlock: starting
agent-device snapshot
→ works on the same device, or asks for open when the command needs a session
agent-device open com.example.app
→ ok # same device, app still installed and signed in
Examples
| agent-device answers the heartbeat | simlock still holds the lease for this agent | Today | After |
|---|---|---|---|
LEASE_NOT_FOUND (it restarted) |
yes | lease released, device wiped | allocated again on the same device |
LEASE_NOT_FOUND |
no (released or expired) | ended by simlock | ended by simlock |
LEASE_EXPIRED or LEASE_REVOKED |
yes | lease released | lease released |
LEASE_NOT_FOUND after agent-device ended the lease itself (endedLeases) |
no | relayed | relayed |
not running yet (starting) |
yes | …: starting, retryable |
…: starting, retryable |
What could go wrong
- A re-allocate that keeps failing holds the device until the lease's TTL instead of freeing it at once. Likely only when agent-device stays broken.
- A test runner that opened with
retainOnCloseloses that flag when its entry was rebuilt (a standalone worker restart, or a gateway restart followed by a worker restart), so its next close ends the lease (agent-device: a lease re-allocated after a standalone worker restart loses retainOnClose #1463). Rare: the restart already broke its run. - After a standalone worker restart,
booton a tvOS or macOS simulator lease answers targetmobile. Rare. - A wrong ownership check could give a lease back to an agent that no longer holds it. The tests cover released, expired and other-token leases.
Words used
- re-allocate: the front sends agent-device
agent_device.lease.allocateagain for a lease it already holds, with the same lease id and device, e.g. after agent-device's daemon restarted. - front: simlock's agent-device endpoint at
/v1/agent-device/*, on a worker or a gateway. - front map: the front's in-memory record of each agent-device lease it serves (device address, platform, owner).
- adopt: rebuild a front map entry from simlock's own lease record after a restart emptied the map. Today only a gateway does it.
Non-goals
- Shortening the gap while agent-device restarts (about 7 s on Android, 28 s on iOS per the reporter). Revisit only if the reporter re-reports after the release.
- Recovering interrupted commands, recordings or agent-device session state.
- Changes to agent-device itself.
- Keeping
retainOnCloseon a rebuilt entry (agent-device: a lease re-allocated after a standalone worker restart loses retainOnClose #1463).
Completion conditions
- After agent-device restarts on a worker, an agent's next heartbeat succeeds on the same device with the simlock lease unchanged, and an
opensent through the front reaches that device. The agent-device session itself is gone; what a command needs from it is agent-device's own. - Through a gateway, the same holds after a worker restart, a gateway restart then a worker restart, and a worker restart then a gateway restart.
- After a standalone worker restart, the same holds.
- A released, expired or revoked lease, or one agent-device ended itself, still ends.
- While a re-allocate fails, the agent gets a retryable error and the simlock lease is still listed.
Open questions
none
Decisions
- ADR 0071 — A restart re-allocates a live agent-device lease on its device (PR docs(adr): propose 0071, a restart re-allocates a live agent-device lease (#1461) #1462): re-allocate on
LEASE_NOT_FOUNDfor a lease simlock still holds; a standalone worker adopts from leasing; a failed re-allocate keeps the lease.
Shared values
| Value or state | Rule | Written by | Read by | Source |
|---|---|---|---|---|
| Re-allocate request | agent_device.lease.allocate with the heartbeat's params as sent, ttlMs set to the ceiling the heartbeat is forwarded with, the address field (udid for iOS, serial for Android) set to the entry's address, and retainOnClose: true when the entry recorded it. Unlike the first allocate (forwardedBody), the heartbeat's deviceKey is kept: agent-device refuses a later heartbeat whose key the lease lacks (LEASE_SCOPE_MISMATCH) |
#1464 | #1465 (adopted entries go through it) | ADR 0071 §1; front/allocate.ts:285-312 |
| "Still held" | A worker: leasing's lease.list for the caller names the lease id. A gateway: the same list names it. When the list leaves it out but the lease index still holds it (stillHeld, a worker starting), it is neither held nor gone: the retryable reply, no re-allocate, nothing released |
#1464 | #1465 | ADR 0071 §1, §3; front/context.ts:177, front/relay-host-link.ts:225 |
| Retryable reply | COMMAND_FAILED, reason SIMLOCK_UNAVAILABLE, HTTP 503, retriable: true, message agent-device is not available on this simlock: the lease is being restored after an agent-device restart; try again |
#1464 | #1465 | shared/replies.ts:26-34 |
Front map entry's retainOnClose |
set from the first allocate's params; an adopted entry has none | #1464 | #1465 | ADR 0071 Consequences; #1463 |
| Adopted entry's target | target and bootTarget are mobile: a worker's registry holds no device class, and agent-device's allocate reply names no device, so a first allocate's target is unset (read as mobile) too |
#1465 | #1464 | front/allocate.ts:446-455; src/agent-device/testing.ts:316; src/core/domain.ts:29-32 |
| Adoptable lease on a worker | a lease in the registry snapshot whose requesterId equals its id and whose ownerId is the caller, with its device's address (driverDeviceId for a phone), platform, model and physical |
#1465 | #1464 (re-allocate reads the entry) | ADR 0071 §4; front/host-link.ts:22-31, modules/types.ts:175-180 |
Behaviours
| Behaviour (invoker, action, what it sees) | Seam | Cases | Task |
|---|---|---|---|
| After agent-device restarts on a worker, an agent's next heartbeat succeeds and the simlock lease is kept on the same device | edge.test.ts rig, rig.fake.restart() |
simulator lease; phone lease (allocate reaches the phone daemon); entry with retainOnClose; heartbeat carrying deviceKey, clientId, runId |
#1464 |
| A heartbeat answered with a lease-gone reason for a lease that really ended still ends it | edge.test.ts gone-reason table |
LEASE_NOT_FOUND with the simlock lease released; LEASE_EXPIRED; LEASE_REVOKED; LEASE_NOT_FOUND after endedLeases |
#1464 |
| While a re-allocate fails, the agent gets the retryable reply and the simlock lease stays listed; a later heartbeat recovers | edge.test.ts rig |
allocate answered with an error (LEASE_ID_IN_USE); allocate unanswered. A heartbeat while agent-device is starting gets …: starting relayed as today and sends no re-allocate |
#1464 |
| Two heartbeats at once after a restart send one re-allocate and both succeed | edge.test.ts rig |
two heartbeats for the same lease | #1464 |
| Through a gateway, a restart leaves the lease working and the device unwiped | gateway-front.test.ts rig |
worker restart; gateway restart then worker restart; worker restart then gateway restart; worker still starting (list leaves the lease out, index holds it) | #1464 |
| After a standalone worker restarts, the agent's next heartbeat succeeds on the same device | edge.test.ts rig, first.restart() |
simulator lease; phone lease; a lease of another token is not adopted; a lease whose requester id is not its id is not adopted | #1465 |
Tasks
flowchart LR
ADR[ADR 0071 PR #1462] --> T1[#1464 re-allocate on heartbeat]
T1 --> T2[#1465 standalone worker adopts after restart]
- A heartbeat re-allocates a live agent-device lease after agent-device restarts #1464 A heartbeat re-allocates a live agent-device lease after agent-device restarts — an agent keeps its device when agent-device restarts on a worker, or when a worker or gateway restarts behind a gateway. Risk: a lease that ended comes back, or a failing re-allocate holds a device until its TTL.
- A standalone worker adopts its agent-device leases after a restart #1465 A standalone worker adopts its agent-device leases after a restart — an agent keeps its device when its standalone worker restarts. Risk: a lease of another token is adopted.
Completion conditions:
- Agent-device restart on a worker: A heartbeat re-allocates a live agent-device lease after agent-device restarts #1464.
- Through a gateway, all three orders: A heartbeat re-allocates a live agent-device lease after agent-device restarts #1464.
- Standalone worker restart: A standalone worker adopts its agent-device leases after a restart #1465.
- Ended leases stay ended: A heartbeat re-allocates a live agent-device lease after agent-device restarts #1464.
- Retryable error while failing: A heartbeat re-allocates a live agent-device lease after agent-device restarts #1464.
Open work in the same area:
- agent-device: a lease re-allocated after a standalone worker restart loses retainOnClose #1463 (
retainOnCloseon a rebuilt entry): follows A standalone worker adopts its agent-device leases after a restart #1465; no Depends on, it is a later fix. - A lease renew right after a gateway restart answers UNKNOWN_LEASE and the client drops the lease #983 (renew right after a gateway restart answers
UNKNOWN_LEASE): same gateway restart window, different call (lease.renewfrom the simlock client); no shared function, no Depends on. - Worker daemon aborts on a V8 out-of-memory error after hours of agent-device e2e traffic, ending every lease #1409 (worker out-of-memory abort): a cause of restarts; this feature limits its damage; no Depends on.
Written by an agent.
Activity
docs(adr): 0071 checker notes (#1461)
V3RON commentedon Nov 9, 2026
Spec updated: business sections, Decisions (ADR 0071, PR #1462), Shared values, Behaviours and Tasks (#1464, #1465) added.
Written by an agent.
docs(adr): 0071 round-1 checker fixes (#1461)
docs(adr): accept 0071 (#1461)
docs(adr): 0071 keeps retainOnClose from a surviving entry; 0023 and …
docs(adr): propose 0071, a restart re-allocates a live agent-device l…
feat(agent-device): a standalone worker adopts its leases after a res…
github-actions commentedon Nov 9, 2026
Closing: all 2 sub-issues are closed. The PR that closed the last one walks the completion conditions (delivery rule 9); reopen this if one does not hold on
main. Flip its ADRs to Accepted if delivery has not.V3RON commentedon Nov 9, 2026
Delivery run
Merged: #1462 (ADR 0071), #1466 (#1464), #1468 (#1465)
Parked: none
Waiting: none
Decided: #1464 — four failure-table branches had no test → added as test cases; #1464 — restart tests split into new files under the 2500-line limit → Done when names them; #1465 — adopted lease's first non-heartbeat call (
snapshot,boot) untested → added as test cases; #1465 — the same test-file split → Done when names itAsleep: 65 min
Written by an agent.