Skip to content

agent-device: a worker restart ends every lease whose device is still running #1103

Closed

Description

@0xdaniell

What happens. 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.

Why. The worker's agent-device daemon restarts with it and keeps its leases in memory only. The next heartbeat answers UNAUTHORIZED / LEASE_NOT_FOUND, and heartbeat() in src/agent-device/front/heartbeat.ts reads that as the lease being over and calls releaseOurs(). A client that uses a local agent-device on the same device is not affected, so the behaviour depends on the path.

Reproduce (0.4.0, agent-device 0.21.31). Open a device with agent-device connect proxy and open, restart the worker, run snapshot a few times: agent-device is not available, then lease ended by simlock.

A fix we run and tested. When the heartbeat reports the lease gone, the entry is not marked ended and lease.list still shows the lease, send agent_device.lease.allocate again with the same lease id and the entry's address, then repeat the heartbeat. Commands fail for about 7 s (Android) to 28 s (iOS). The app process stays up, and a new open works on the same device. Tested on Android and iOS with a real app.

Open point. The front map is in memory, so a gateway restart followed by a worker restart loses the address, and that lease still ends. We keep {address, platform, principal} per lease in a file next to the state. Persisting it in the registry would be cleaner, and it is your call whether a daemon that ended a lease on its own should also be re-bound.

An issue before a PR because this changes what the tests at edge.test.ts "LEASE_NOT_FOUND" pin down.

Activity

  1. added
    bug:new
    on Nov 1, 2026
  2. self-assigned this
    on Nov 1, 2026
  3. V3RON commented on Nov 1, 2026

    @V3RON
    Contributor

    Reproduction

    Confirmed as reported. This is a decision: the code does what ADR 0023 §3 says. The test's expectation is a proposal (the issue's re-bind).

    src/agent-device/gateway-front.test.ts, "after the worker restarts with the leased device still running, … the next heartbeat through the gateway keeps it too: …" on bug/1103-repro. The worker keeps its driver, so ADR 0019 keeps the simlock lease; the test checks that first.

    pnpm exec vitest run --project unit src/agent-device/gateway-front.test.ts -t "after the worker restarts"
    
    AssertionError: expected { code: -32000, … } to be undefined
      "code": "UNAUTHORIZED", "details": { "reason": "LEASE_ENDED_BY_HOST" },
      "message": "lease ended by simlock; run disconnect, then open"
     ❯ src/agent-device/gateway-front.test.ts:1134
    

    Root cause

    src/agent-device/front/heartbeat.ts:149-154: agent-device answers LEASE_NOT_FOUND, and releaseOurs releases the simlock lease although the front holds a live entry. ADR 0023 §3 (docs/internal/adr/0023-agent-device-is-served-by-an-optional-edge-module.md:112-114) decides exactly this: "A restart loses its agent-device leases; the front answers their next heartbeat as ended by simlock and releases the simlock lease". src/agent-device/AGENTS.md lists it as a known gap. The client error and the wipe are only where it shows.

    Simplest fix

    A superseding ADR narrowing 0023 §3: for a live front entry whose simlock lease is still listed, send agent_device.lease.allocate again with the same id and the entry's address, then repeat the heartbeat. Then one PR in heartbeat.ts; a rough version passes the reproduction. The ADR also settles the issue's open point (persisting the address across a gateway restart).

    Alternatives rejected

    • Re-bind on every command too: more paths; a heartbeat arrives within seconds anyway.
    • Persist leases inside agent-device: another tool's change.
    • Re-bind without an ADR: contradicts an accepted ADR.

    Risk

    • The rough re-bind turns 9 tests red in edge.test.ts and gateway-front.test.ts, the LEASE_NOT_FOUND/LEASE_EXPIRED/LEASE_REVOKED table among them; the ADR decides which still hold (LEASE_REVOKED probably should).
    • A lease agent-device ended itself (endedLeases) must not be re-bound.
    • A direct worker's restart loses the front map, so that path still ends the lease.

    Written by an agent.

  4. removed their assignment
    on Nov 1, 2026
  5. V3RON commented on Nov 8, 2026

    @V3RON
    Contributor

    This was generated by AI during triage.

    Decision

    The maintainer approved automatic recovery of agent-device access for a surviving Simlock lease:

    • Keep the same lease, device and app data, within the existing Simlock lifetime.
    • Cover standalone workers, workers behind a gateway, agent-device daemon restarts, and gateway/worker restarts in either order.
    • Recover only LEASE_NOT_FOUND after confirming the caller still owns a live Simlock lease. Explicit revocation, expiry, release and endedLeases remain terminal; never revive an ended lease.
    • Temporary retryable errors and reopening an agent-device session are acceptable. Interrupted commands, recordings and session state need not recover.
    • Failed recovery preserves the Simlock lease until normal expiry or explicit release.
    • Investigate reusing current front-map adoption before adding persistence; no persistence design is prescribed.

    This supersedes the report's narrower scope. Its reproduction is the starting regression test; add acceptance coverage for the additional restart scenarios and terminal-error cases.

    Readiness prerequisite: ADR 0023 section 3 explicitly requires release after agent-device restart. Capture this approved change in a superseding ADR and checked feature/task spec before implementation, as the triage report recommends. No further product decision is pending from this interview. Keep bug:new until that prerequisite is resolved; this comment does not authorize violating the accepted ADR.

    Written by an agent.

  6. self-assigned this
    on Nov 8, 2026
  7. V3RON commented on Nov 8, 2026

    @V3RON
    Contributor

    Supersedes the report above.

    Reproduction

    Still reproduces on origin/main (14163a99). This is a decision: the code does what ADR 0023 §3 says. The test's expectation is the re-bind the decision above approved, so it is a proposal.

    The test is src/agent-device/gateway-front.test.ts, "after the worker restarts with the leased device still running, … and the device is not wiped", on bug/1103-repro (8773b642, base 944d2549). Cherry-picked onto 14163a99, it conflicts once in setUp (keep main's launch and add existing?.clock/existing?.driver), then fails the same way. It covers the gateway path only; the decision's other restart orders belong in the spec.

    pnpm exec vitest run --project unit src/agent-device/gateway-front.test.ts -t "after the worker restarts"
    
    AssertionError: expected { code: -32000, … } to be undefined
      "code": "UNAUTHORIZED", "details": { "reason": "LEASE_ENDED_BY_HOST" },
      "message": "lease ended by simlock; run disconnect, then open"
     ❯ src/agent-device/gateway-front.test.ts:1179:26
    

    Root cause

    src/agent-device/front/heartbeat.ts:149-154: after its restart, agent-device answers LEASE_NOT_FOUND, and releaseOurs (:154) releases the simlock lease even though the front holds a live entry and lease.list still lists the lease. ADR 0023 §3 (docs/internal/adr/0023-agent-device-is-served-by-an-optional-edge-module.md:112-114) decides exactly this, and src/agent-device/AGENTS.md:77 lists it as a known gap. The client error and the wipe are only where it shows.

    Simplest fix

    A spec session: a superseding ADR that narrows 0023 §3 to the decided scope, then a feature with tasks (standalone worker, gateway, either restart order, terminal reasons). I tried the core of the first task:

     if (entry?.endedBy === "agent") return relay(reply);
    +if (reason === "LEASE_NOT_FOUND" && entry && entry.endedAt === undefined && listed(leaseId)) {
    +  allocate again { leaseId, ttlMs: ceiling, [udid|serial]: entry.address }
    +  heartbeat again; on success → renewHeld(...)
    +}
    reproduction: 1 passed
    pnpm exec vitest run --project unit src/agent-device/   → 2 failed | 503 passed
      edge.test.ts:2171 (LEASE_NOT_FOUND row)  edge.test.ts:2595 (daemon restart)
    

    Both red tests pin ADR 0023 §3. I discarded the change afterwards.

    Alternatives rejected

    • Re-bind on LEASE_EXPIRED/LEASE_REVOKED as well: the decision keeps both terminal.
    • Persist leases inside agent-device: that changes another tool.
    • Re-bind without a superseding ADR: contradicts an accepted ADR.

    Risk

    • The re-sent allocate carries no release scope (runId, clientId, deviceKey), so the rough version skipped the allocate reply check (shared/rpc.ts:168). The worker's ReleaseScopeRecord must learn the scope, or lease end sends no release.
    • The re-sent allocate must repeat any retainOnClose (feat(agent-device): retain leases through close #1436) from the first allocate.
    • A direct worker's restart empties its front map, so that path needs the adoption or persistence the decision asks to investigate.

    Fast track

    • Defect: no — decision; ADR 0023 §3 pins it
    • Fix tried: no — two tests next to it turn red
    • Nothing public added or broken: no — documented end on restart changes
    • No ADR or rule changed: no — supersedes ADR 0023 §3
    • One fix: no — a feature of several PRs

    Side findings

    Written by an agent.

  8. removed their assignment
    on Nov 8, 2026
  9. V3RON commented on Nov 9, 2026

    @V3RON
    Contributor

    Being specified in #1461. This issue stays open until that feature ships.

    Written by an agent.

  10. github-actions commented on Nov 9, 2026

    @github-actions
    Contributor

    Shipped as #1461.

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

    bug:new

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions