Skip to content

agent-device: a session whose lease survived a worker restart fails every call with "Lease does not match tenant/run scope" #1472

Closed

Description

@V3RON

What happens. After a worker restart, an agent-device session whose lease survived can no longer be used. The lease and its device are kept, and heartbeats keep renewing the lease (ADR 0071, #1466, #1468). But every snapshot, screenshot, open (including a fresh open --platform ios) and close on the session then fails with:

Error (UNAUTHORIZED): Lease does not match tenant/run scope
Hint: Refresh daemon metadata and retry the command.

It does not recover: retries kept failing for 2.5 minutes. close cannot end the lease either, so it stays until its TTL or until the client runs agent-device disconnect, which does release it. ADR 0071 says that after a restart agents see retryable errors, then open again; here open fails too.

Where. The error comes from the worker's own agent-device (lease-registry-scope.js in agent-device 0.21.35). Its diagnostics show it resolving the right UDID for the lease, then rejecting the call. One guess, not checked: the entry the front rebuilds after the restart (adoptable, ADR 0071 §4) does not carry the per-lease tenant/run scope that #1453 gives each lease.

Seen. main at 30f8fad9, agent-device 0.21.35, macOS 27.0.1 arm64, iOS 26.5 simulator.

  • Standalone worker: agent-device connect --remote-config ~/.simlock/agent-device-profile.json --session s1, open --platform ios --session s1, open com.apple.Preferences --session s1, then snapshot --session s1 works. Run simlock daemon stop && simlock daemon start, wait 30 s, and snapshot, open com.apple.Preferences and open --platform ios all fail as above. simlock list --leases still shows the same lease id and device.
  • Through a gateway (worker joined, connect proxy --daemon-base-url http://127.0.0.1:<gw>/v1/agent-device): after a worker restart, the first snapshot answers the documented the worker holding this device is not connected to the gateway; try again. Every later call fails with Lease does not match tenant/run scope. A gateway restart leaves a second session in the same state.

Found during the pre-release e2e pass after v0.6.0.

Written by an agent.

Activity

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

    @V3RON
    ContributorAuthor

    Reproduction

    The re-allocate after a restart drops the lease's backend. agent-device gives the lease its default backend, ios-simulator, so every command that names the real one is refused. Heartbeats name no backend, so they keep passing. This explains each symptom in the report.

    src/agent-device/edge-restart.test.ts, "after a restart, the re-allocate names the backend the lease's first allocate named, which the heartbeat does not carry" (iOS and Android after an agent-device restart, iOS after a standalone worker restart), on bug/1472-repro:

    pnpm exec vitest run --project unit src/agent-device/edge-restart.test.ts -t "re-allocate names the backend"
    AssertionError: expected [ 'ios-instance', undefined ] to deeply equal [ 'ios-instance', 'ios-instance' ]
    AssertionError: expected [ 'android-instance', undefined ] to deeply equal [ 'android-instance', …(1) ]
    

    Root cause

    Defect. src/agent-device/front/heartbeat.ts:120-126 (allocateAgain) builds the re-allocate from the heartbeat's params. At agent-device v0.21.35 those never hold backend: leaseScopeToLeaseRpcParams (packages/contracts/src/lease-scope.ts) adds it only to lease_allocate. The allocate handler (http-rpc-methods.ts:161) then falls back to ios-simulator (normalizeLeaseBackend). Every command, open and close sends meta.leaseBackend (ios-instance or android-instance). assertLeaseScopeMatch (lease-registry-scope.ts:239) refuses that with "Lease does not match tenant/run scope". The tenant guess in the issue is wrong: the re-allocate goes through RoutedHostLink.forward with the lease id, so the tenant matches. The fake daemon (testing.ts) neither stores a backend nor checks scope on commands, which is why #1466 and #1468 passed.

    Simplest fix

    In allocateAgain, add backend: BACKEND_OF_PLATFORM[entry.platform] to the params, with BACKEND_OF_PLATFORM exported from front/allocate.ts. A first allocate can only carry that value, because foreignBackend refuses any other.

    pnpm exec vitest run --project unit src/agent-device   # 523 passed, 1 skipped
    

    Alternatives rejected

    • Store the backend on the map entry at allocate: an adopted entry has none, so it still needs the platform mapping.
    • Make agent-device send backend on heartbeat: it's another tool, and the pinned 0.21.35 stays broken.

    Risk

    • The gateway front runs the same heartbeat.ts. Only worker rigs are tested.
    • A first allocate with no backend (an older CLI) gets one on its re-allocate. The CLI at 0.21.35 always sends one.
    • Make the fake store backend (defaulting to ios-simulator) and check it on commands, so the test proves the user-visible refusal.

    Fast track

    • Defect: yes
    • Fix tried: yes
    • Nothing public added or broken: yes
    • No ADR or rule changed: yes
    • One fix: no — agent-device could send backend on heartbeat instead

    Side findings

    Written by an agent.

  4. removed their assignment
    on Nov 9, 2026
  5. added
    bug:ready
    and removed
    bug:new
    on Nov 9, 2026
  6. self-assigned this
    on Nov 9, 2026
  7. V3RON commented on Nov 9, 2026

    @V3RON
    ContributorAuthor

    Handoff

    Done: PR #1484 sends the platform's backend on the re-allocate (src/agent-device/front/heartbeat.ts) and corrects ADR 0071 (#1480). Audit clean, review round 1 passed with 0 blocking, ledger posted, PR ready.

    Not done: not merged. Close #1480 by hand after the merge: coding rule 3 lets this PR close only #1472 (see its ## Decision taken).

    Findings: none open.

    Blocked on: merge-pr.sh 1484 printed not merged: CI not green on PR #1484: fail Branch name closes its issue;. Run 42710944023 failed on the old body's Closes #1480. The current body passes (run 42713333354), but the gate reads every check run on the head commit, the superseded one included, and a re-run replays the old payload. The maintainer merges by hand, or a new head commit gives the gate fresh checks.

    Written by an agent.

  8. added
    bug:blocked
    and removed
    bug:ready
    on Nov 9, 2026
  9. removed their assignment
    on Nov 9, 2026
  10. V3RON commented on Nov 9, 2026

    @V3RON
    ContributorAuthor

    Delivery run

    Merged: none
    Parked: #1472 — PR #1484 passed review; the gate counts a superseded red Branch name closes its issue run on the head commit
    Decided: #1472 — close #1480 from this PR? → no: ADR fix kept here, #1480 closed by hand after merge (coding rule 3)
    Asleep: 15 min

    Written by an agent.

  11. added a commit that references this issue on Nov 10, 2026
    d7195cf
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:blocked

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions