Skip to content

agent-device leases survive a worker or agent-device restart #1461

Closed
2 of 2 issues completed
Closed

Description

@V3RON

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]
Loading

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 retainOnClose loses 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, boot on a tvOS or macOS simulator lease answers target mobile. 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.allocate again 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

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 open sent 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

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]
Loading

Completion conditions:

Open work in the same area:

Written by an agent.

Sub-issues

Sub-issues

2 of 2 Issues completed
Loading 2 sub-issues

Activity

  1. added
    feature:spec
    on Nov 9, 2026
  2. added a commit that references this issue on Nov 9, 2026
  3. V3RON commented on Nov 9, 2026

    @V3RON
    ContributorAuthor

    Spec updated: business sections, Decisions (ADR 0071, PR #1462), Shared values, Behaviours and Tasks (#1464, #1465) added.

    Written by an agent.

  4. added
    feature:planned
    and removed
    feature:spec
    on Nov 9, 2026
  5. github-actions commented on Nov 9, 2026

    @github-actions
    Contributor

    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.

  6. V3RON commented on Nov 9, 2026

    @V3RON
    ContributorAuthor

    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 it
    Asleep: 65 min

    Written by an agent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature:planned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions