Skip to content

A standalone worker adopts its agent-device leases after a restart #1465

Closed

Description

@V3RON

Part of #1461.

Scope

After a standalone worker restarts, its front map is empty. The worker's front rebuilds an entry for each agent-device lease the caller still holds, from the registry snapshot, the same way a gateway does after its own restart. The next heartbeat then takes #1464's re-allocate path, so the agent keeps its device (ADR 0071 §4).

In short

An agent whose standalone worker restarted keeps its simlock lease and its device. Its next heartbeat succeeds and it runs open again. A lease that had retainOnClose loses that flag (#1463).

flowchart TD
  R[worker restarts: map empty] --> Q[agent's request reaches the worker front]
  Q --> A["adopt: registry snapshot leases of this caller (new on a worker)"]
  A --> H[heartbeat: #1464 re-allocate path]
Loading

If this goes wrong, an agent sees lease ended by simlock after its worker restarts, as today, or another token's lease gets an entry.

Technical spec

Facts

Commit: origin/main caa5b4e40.

# Fact Source
F1 adoptLeases runs on every front request and adds an entry, marked adopted, for each adoptable lease the map lacks src/agent-device/front/front.ts:388-414
F2 HostLink.adoptable is optional and synchronous; AdoptableLease holds leaseId, address, platform, model, physical, target, ttlMs, ttlDeadline front/host-link.ts:22-31,41
F3 RoutedHostLink (the worker's link) has no adoptable; its comment says a worker's pre-restart lease "is ended, not adopted" front/host-link.ts:40,62-131
F4 RoutedHostLink holds the module's ModuleRegistryView, whose snapshot has devices and leases front/host-link.ts:66-69; src/modules/types.ts:175-180
F5 A lease record holds id, deviceId, requesterId, ownerId, ttlMs, ttlDeadline src/core/domain.ts:403-413; src/contract/lease-schemas.ts:36-47
F6 A front lease's requester id is its lease id front/allocate.ts:194-200
F7 A first allocate's address is driverDeviceId for a physical device, address otherwise front/allocate.ts:344
F8 The gateway's adoptable keeps only leases whose requesterId equals their id, and maps the device class to a target (TARGET_OF_CLASS) front/relay-host-link.ts:142-167
F9 A worker builds RoutedHostLink and FrontMap in worker.ts src/agent-device/worker.ts:55-62
F10 Today, after a worker restart, a heartbeat for a lease the map lacks and simlock holds releases it when agent-device answers a lease-gone reason edge.test.ts:1899 ("a heartbeat naming a lease ID the front map lacks …"), edge.test.ts:2320
F11 The edge rig restarts the worker on the same data directory and FakeDriver with first.restart(between); the same fake and phone objects keep their leases unless between calls their restart() src/agent-device/test-support.ts:237-242; src/agent-device/testing.ts:394-400
F12 The gateway's link keeps its own allocates out of adoptable with asked; allocate.ts calls link.asked?.(leaseId) before it requests the lease; RoutedHostLink has no asked front/relay-host-link.ts:138-148; front/allocate.ts:248; front/host-link.ts:42
F13 AdoptableLease.target is required and becomes the entry's bootTarget and target, read by boot and the test sharding rule; a virtual device record holds no device class front/host-link.ts:28; front/front.ts:393,400; front/boot.ts:58; front/request-rules.ts:271-276; src/core/domain.ts:29-32,66-70
F14 RoutedHostLink routes a lease by #roles, empty after a restart; a lease it does not know goes to the simulator daemon front/host-link.ts:72-76,110-116
F15 A heartbeat for a lease the map lacks reaches leaseStillThere when agent-device still holds it; tests at edge.test.ts:1899 (middle row) and :1955 pin that front/heartbeat.ts:144-145; edge.test.ts:1899-1931,1955-1968

Modules touched

Searches: adoptable, AdoptableLease, RoutedHostLink, asked, ended, not adopted in src/agent-device.

RoutedHostLink gains asked (an id the front allocated in this process is never adopted, as on a gateway) and adoptable: each lease in the registry snapshot whose requesterId is its id, whose ownerId is the caller and not asked, whose device has an address (driverDeviceId for a phone), with platform, model, physical, ttlMs, ttlDeadline, and target: "mobile", so bootTarget is mobile too (feature Shared values, "Adopted entry's target"). An adopted physical lease is recorded in #roles as phones. forget(leaseId) also drops the id from asked, as the gateway does when a lease is removed (front/relay-host-link.ts:108-110).

flowchart LR
  FR[front/front.ts adoptLeases] --> RL["front/host-link.ts RoutedHostLink.adoptable (new)"]
  RL --> REG[ModuleRegistryView snapshot]
  FR --> MAP[front/front-map.ts]
Loading

Changed: src/agent-device/front/host-link.ts, src/agent-device/AGENTS.md (a new step in "Flow: agent on a worker": after a restart the map is rebuilt from the registry snapshot; the Known gap), website/src/content/docs/docs/using-devices/agent-device.mdx (a standalone worker's restart keeps a live lease too), tests in src/agent-device/edge.test.ts.

Contract and event changes

none. Searched src/contract: adoptable is in-process.

Other code on the same state

Searches: map.add(, adopted and asked( in src/agent-device/front.

  • allocate.ts asks the link (asked) before it requests a lease (F12), so a lease this process allocates is never adopted, whether its allocate keeps it, releases it, or gets no reply. The allocate decides its entry.
  • A heartbeat re-allocates a live agent-device lease after agent-device restarts #1464's re-allocate reads the adopted entry. It has no retainOnClose.
  • Renewals start for adopted entries only when a heartbeat renews, as on a gateway.

Failure modes

Call Outcome What happens Limit, counted from
registry snapshot lease's device missing or without an address not adopted; the heartbeat ends the lease as today —
registry snapshot lease owned by another principal not adopted —
registry snapshot lease whose requester id is not its id (a lease made outside the front) not adopted —

The adopt writes only the in-memory map. A restart in the middle of it leaves nothing behind. Two requests at once both skip an id the map already has.

Rules in play

  • ADR 0071 §4: "rebuilds entries the way the gateway does". It uses the same AdoptableLease shape and the same requesterId filter.
  • src/agent-device/AGENTS.md: "Front map … in memory only". Kept: the adopt reads the snapshot and stores nothing.

Retired words

  • ended, not adopted: a worker adopts its pre-restart leases (front/host-link.ts:40).

Tests

Seam: the edge.test.ts rig with first.restart(() => { first.fake.restart(); first.phone.restart(); }), so both fake daemons forget their leases as a real worker restart does (F11). Model them on edge.test.ts:2320.

  • After a standalone worker restarts, the agent's next heartbeat succeeds on the same device. Cases: a simulator lease, after which open through the front reaches the same fake daemon and answers ok; a phone lease, whose re-allocate reaches the phone daemon; a lease of another token, which is not adopted, so its heartbeat answers as today; a lease whose requester id is not its id, which is not adopted. Replaces edge.test.ts:2320 and the lease-gone rows of edge.test.ts:1899. The middle row of :1899 and the test at :1955 move onto a lease the front asked for in this process that simlock kept while agent-device still holds it, built as edge.test.ts:1357 builds it (holdAllocate, the CLI hangs up, failNextRelease, letAllocateGo); it still reaches leaseStillThere (F15).
  • After a standalone worker restarts, an adopted lease's first call that is not a heartbeat is routed as before the restart. Cases: a phone lease whose first call is snapshot, which reaches the phone daemon; standalone boot on an adopted lease, answered by the front with the lease's mobile target. At edge-restart.test.ts, extending the restart test's table 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 passes with the Tests lines (adoption tests in edge-restart.test.ts under ADR 0063's 2500-line limit; decided in delivery, pending the maintainer).
  • src/agent-device/AGENTS.md and agent-device.mdx say a standalone worker adopts its leases after a restart; ended, not adopted is gone (stale-refs.sh).

Out of scope

Depends on

Written by an agent.

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