Skip to content

A heartbeat re-allocates a live agent-device lease after agent-device restarts #1464

Closed

Description

@V3RON

Part of #1461.

Scope

When agent-device answers a heartbeat LEASE_NOT_FOUND for a lease the front's map holds and simlock still holds for the caller, the front allocates the agent-device lease again on the same device and answers with a fresh heartbeat, instead of releasing the simlock lease. This covers an agent-device restart on a worker, and through a gateway a worker restart, a gateway restart then a worker restart, and a worker restart then a gateway restart (ADR 0071 §1–§4). The case it leaves out is a standalone worker's own restart, where the map is empty. #1465 covers it.

In short

An agent whose agent-device daemon restarted keeps its simlock lease, its device and its app. Its next heartbeat succeeds. Its agent-device session is gone, so it may need open again. While the re-allocate fails, the agent gets a retryable error, agent-device is not available on this simlock: the lease is being restored after an agent-device restart; try again, and the simlock lease stays.

flowchart TD
  H[heartbeat] --> AD{agent-device answer}
  AD -->|ok| R[renew as today]
  AD -->|LEASE_EXPIRED / LEASE_REVOKED| X[release as today]
  AD -->|LEASE_NOT_FOUND| S{"map entry live and<br/>simlock still holds it? (changed)"}
  S -->|no| X
  S -->|yes| A["re-allocate same id, same device (new)"]
  A -->|ok| H2[heartbeat again, relay its reply]
  A -->|fails| Y[retryable reply, lease kept]
Loading

If this goes wrong, an agent sees lease ended by simlock after a restart, as today, or a lease that ended comes back.

Technical spec

Facts

Commit: origin/main caa5b4e40; agent-device tag v0.21.35 (e5676488).

# Fact Source
F1 A heartbeat with a lease-gone reason (LEASE_NOT_FOUND, LEASE_EXPIRED, LEASE_REVOKED) that the agent did not end releases the simlock lease and answers LEASE_ENDED_BY_HOST src/agent-device/front/heartbeat.ts:149-154; shared/rpc.ts:176-184
F2 A lease agent-device ended itself (endedBy === "agent") relays the reply and releases nothing front/heartbeat.ts:150
F3 The heartbeat is forwarded with ttlMs raised to leaseMaxTtlMs, read from the link's status front/heartbeat.ts:28-38; front/front.ts:129,151
F4 A first allocate forwards the CLI's params minus device, deviceKey, target and the spec fields, with ttlMs set to the ceiling and udid/serial set to the address front/allocate.ts:285-312; shared/selectors.ts addressField
F5 The front map entry holds address, platform, model, physical, target, identity, ttlMs, deadline, deviceKey; retainOnClose is not stored front/front-map.ts:6-25; front/allocate.ts:379-389
F6 listLeases reads leasing's lease.list for the caller (5 s timeout); renewLease treats a lease missing from that list but held by the link's index (stillHeld) as unlisted, not gone front/context.ts:177-187; front/renewals.ts:16-39
F7 The gateway's stillHeld reads the gateway's lease index front/relay-host-link.ts:225
F8 An allocate reply with no readable release scope (tenantId, runId) is read as unusable shared/rpc.ts:168; shared/release-scope.ts:21-39
F9 The worker's lease end records each allocate reply's scope per daemon and releases with it host/lease-end.ts ReleaseScopeRecord
F10 RoutedHostLink sends an allocate whose serial/udid is a physical device's driverDeviceId to the phone daemon front/host-link.ts:110-130
F11 The CLI heartbeats before each command with tenant, runId, leaseBackend, leaseProvider, clientId, deviceKey, ttlMs and leaseId agent-device v0.21.35:src/cli/commands/connection-runtime.ts:892-941,996-1018
F12 agent-device's lease registry is in memory; an unknown lease answers UNAUTHORIZED LEASE_NOT_FOUND; an expired lease is swept and also answers LEASE_NOT_FOUND agent-device v0.21.35:packages/daemon/src/resources/lease-registry.ts:56,592-608,630-636
F13 An allocate with a caller-chosen id (16–128 hex) is accepted unless that daemon already issued it (INVALID_ARGS LEASE_ID_IN_USE) agent-device v0.21.35:packages/daemon/src/resources/lease-registry-scope.ts:126-138; v0.21.35:packages/daemon/src/resources/lease-registry.ts:81-98
F14 A heartbeat checks tenantId, runId, leaseBackend, leaseProvider, deviceKey and clientId against the lease, each when sent; a mismatch is LEASE_SCOPE_MISMATCH agent-device v0.21.35:packages/daemon/src/resources/lease-registry-scope.ts:231-248
F15 The fake agent-device forgets leases on restart() and honours retainOnClose. Its heartbeat checks only the lease and its tenant; only its release checks leaseProvider, clientId, deviceKey (LEASE_SCOPE_MISMATCH) src/agent-device/testing.ts:394-400,535,575-588,606-616
F16 While a daemon starts, calls answer agent-device is not available on this simlock: starting, retryable host/upstream.ts:38-43; shared/replies.ts:26-34
F17 The gateway rebuilds its map from adoptable on every request after its own restart front/front.ts:388-414; front/relay-host-link.ts:142-167
F18 A front lease call (leaseCall) carries no abort signal; simlock sets no upstream timeout, so a hung call waits for the proxy's 5-minute default front/context.ts:87-101; agent-device v0.21.35:packages/proxy/src/daemon-proxy.ts:105,349
F19 The front map exposes no releasing flag in HeldView front/front-map.ts:29-53

Modules touched

Searches: heartbeat(, releaseOurs, FrontMapEntry, map.add(, retainOnClose, LEASE_ENDED_BY_HOST, "daemon restart" in src/agent-device, website/src/content/docs.

flowchart LR
  FR[front/front.ts] --> HB["front/heartbeat.ts (changed)"]
  HB --> CX[front/context.ts]
  HB --> MAP["front/front-map.ts (changed: retainOnClose)"]
  AL["front/allocate.ts (changed: records retainOnClose)"] --> MAP
  HB --> LINK[HostLink: RoutedHostLink / RelayHostLink]
  LINK --> HOST[host + lease-end]
Loading
sequenceDiagram
  participant CLI
  participant F as front (worker or gateway)
  participant L as leasing / gateway index
  participant AD as agent-device
  CLI->>F: heartbeat
  F->>AD: heartbeat (ttlMs = ceiling)
  AD-->>F: LEASE_NOT_FOUND
  F->>L: lease.list (or stillHeld)
  L-->>F: held
  F->>AD: allocate (same id, address, heartbeat scope)
  alt ok
    AD-->>F: lease + scope
    F->>AD: heartbeat
    AD-->>F: ok
    F-->>CLI: ok (and renew as today)
  else error or no answer
    AD-->>F: LEASE_ID_IN_USE / timeout
    F-->>CLI: retryable reply, lease kept
  end
Loading

Changed: src/agent-device/front/heartbeat.ts, front/front-map.ts (retainOnClose, releasing in the view), front/allocate.ts, front/host-link.ts:56 (comment), src/agent-device/testing.ts (the fake's heartbeat checks runId, leaseProvider, clientId, deviceKey when sent, as agent-device does, F14), src/agent-device/AGENTS.md (the lease-gone front rule, the sessions folder rule at line 48, Known gaps), website/src/content/docs/docs/using-devices/agent-device.mdx, tests in src/agent-device/edge.test.ts, gateway-front.test.ts.

Contract and event changes

none. Searched src/contract for agent-device shapes: the front's replies are not contract shapes. No event is added.

Other code on the same state

Searches: context.map. (front map writes), renewals. and releaseSimlock( in src/agent-device/front.

  • Renewals (front/renewals.ts): a renewal tick that runs during a re-allocate reads the same entry. The re-allocate does not end the entry, so the tick renews as it would. Neither wins; there is no conflict.
  • A release (front/release.ts) marks the entry releasing. A heartbeat for an entry that is releasing takes today's path (F1): no re-allocate. If a re-allocate already running lands after the release reached agent-device, the simlock lease's end releases the new agent-device lease with the scope recorded from the re-allocate's reply (F9). The release wins.
  • ended.ts (endedLeases) marks the entry endedBy: "agent". A later LEASE_NOT_FOUND is relayed (F2), so no re-allocate runs.
  • A standalone worker adopts its agent-device leases after a restart #1465 adds adopted entries to the same map. They go through this path, with no retainOnClose.

Failure modes

Call Outcome What happens Limit, counted from
lease.list (still held) names the lease re-allocate 5 s, from the call (F6)
lease.list does not name it, index does not hold it release as today (F1) —
lease.list does not name it, gateway index holds it retryable reply, nothing released —
lease.list throws or times out retryable reply, nothing released 5 s
re-allocate ok with a release scope heartbeat again, relay that reply, renew as today; the entry is not changed —
re-allocate error reply (LEASE_ID_IN_USE, DEVICE_IN_USE, scope error) retryable reply, simlock lease kept —
re-allocate unusable reply (no release scope, F8) retryable reply, simlock lease kept —
re-allocate no answer / link unreachable the call is aborted; retryable reply, simlock lease kept 10 s on the front's Clock, from sending the allocate
second heartbeat lease-gone reason retryable reply, lease kept —
second heartbeat other error relayed —

Two heartbeats at once for one lease share one re-allocate; both get its outcome, so none waits past the 10 s bound. A worker restart in the middle of a re-allocate leaves only the simlock lease, which the next heartbeat recovers.

Rules in play

  • ADR 0071 §1: "re-allocates it". The front sends the allocate only for LEASE_NOT_FOUND with a held lease.
  • ADR 0071 §2: "What stays final". LEASE_EXPIRED, LEASE_REVOKED and endedBy: "agent" keep today's paths.
  • ADR 0071 §3: "A failed re-allocate keeps the simlock lease". There is no releaseOurs on any failure row.
  • ADR 0023 (rule kept): "the simlock lease is the only clock". The re-allocate's ttlMs is the same ceiling, and renewals are unchanged.
  • src/agent-device/AGENTS.md: "Front map, renewals and release scopes live in memory only". Kept: nothing is persisted.

Retired words

  • A daemon restart loses agent-device leases: an agent-device restart re-allocates a live lease (src/agent-device/AGENTS.md:77, Known gaps).
  • Restarting a worker's daemon ends every agent-device lease: an agent-device restart, and a worker or gateway restart behind a gateway, keep a live agent-device lease, and the agent may need open again; a standalone worker's own restart still ends it until A standalone worker adopts its agent-device leases after a restart #1465 (agent-device.mdx:207).
  • a restart ends every agent-device lease: an agent-device restart, and a worker or gateway restart behind a gateway, keep a live lease; a standalone worker's own restart still ends it until A standalone worker adopts its agent-device leases after a restart #1465 (src/agent-device/AGENTS.md:48).

Tests

Seam: the edge.test.ts rig (setUp, granted, heartbeat, rig.fake.restart()) for a worker, and the gateway-front.test.ts rig (rig.restart(), addWorker) for a gateway. Model them on edge.test.ts:2309 and the reproduction test on bug/1103-repro (gateway-front.test.ts, "after the worker restarts …"); carry over that branch's setUp change (addWorker reuses existing?.driver, existing?.clock, existing?.latency).

  • After agent-device restarts on a worker, an agent's next heartbeat succeeds and the simlock lease is kept on the same device. Cases: a simulator lease; a phone lease, whose allocate reaches the phone daemon; an entry allocated with retainOnClose, whose re-allocate carries it; a heartbeat carrying deviceKey, clientId and runId, whose re-allocate carries all three, and a second heartbeat that passes the fake's new heartbeat scope check (a re-allocate that drops one of them fails it); after it, open sent through the front reaches the same fake daemon with the lease id and answers ok. Replaces edge.test.ts:2309.
  • A heartbeat answered with a lease-gone reason for a lease that really ended still ends it. Cases: LEASE_NOT_FOUND with the simlock lease already released; LEASE_EXPIRED; LEASE_REVOKED; LEASE_NOT_FOUND after endedLeases. Extends the table at edge.test.ts:1884, whose LEASE_NOT_FOUND row moves to the first line.
  • While a re-allocate fails, the agent gets the retryable reply and the simlock lease stays listed, and a later heartbeat recovers. Cases: allocate answered LEASE_ID_IN_USE; allocate unanswered for 10 s (FakeClock); agent-device still starting, whose …: starting is relayed and sends no re-allocate.
  • Two heartbeats at once after a restart send one re-allocate, and both succeed.
  • Through a gateway, a restart leaves the lease working and the device unwiped, and an open relayed through the gateway after the re-allocate reaches the worker's same fake daemon and answers ok. Cases: a worker restart; a gateway restart, then a worker restart; a worker restart, then a gateway restart; a worker still starting, whose list leaves the lease out while the index holds it, gets the retryable reply.
  • After a restart, the rows of the failure table no other line names hold at the edge.test.ts rig. Cases: a re-allocate agent-device accepts with no release scope (F8) gets the retryable reply and the simlock lease stays listed; a heartbeat on a lease whose release is running answers ended and sends no re-allocate; after a restore the simlock lease is renewed as today (its deadline moves); a second heartbeat after the re-allocate that fails with an error other than lease-gone is relayed to the agent. Extends the tables of the lines above where one fits (decided in delivery, pending the maintainer).

Done when

  • pnpm exec vitest run --project unit src/agent-device/edge.test.ts src/agent-device/edge-restart.test.ts src/agent-device/gateway-front.test.ts src/agent-device/gateway-front-restart.test.ts passes with every Tests line (restart tests split out under ADR 0063's 2500-line limit; decided in delivery, pending the maintainer).
  • src/agent-device/AGENTS.md and agent-device.mdx:198-207 say an agent-device restart, and a worker or gateway restart behind a gateway, keep a live lease, and that a standalone worker's own restart still ends it; no Retired words phrase is left (stale-refs.sh).
  • A standalone worker's own restart still ends the lease as today (edge.test.ts:2320 unchanged; A standalone worker adopts its agent-device leases after a restart #1465 changes it).

Out of scope

Depends on

Written by an agent.

Activity

  1. added
    task:draft
    on Nov 9, 2026
  2. added
    task:ready
    and removed
    task:draft
    on Nov 9, 2026
  3. self-assigned this
    on Nov 9, 2026
  4. added a commit that references this issue on Nov 9, 2026
    fadc7ae
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

task:ready

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions