You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
agent-device: a worker restart ends every lease whose device is still running #1103
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.
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.
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.
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:
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.
Activity
V3RON commentedon Nov 1, 2026
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: …" onbug/1103-repro. The worker keeps its driver, so ADR 0019 keeps the simlock lease; the test checks that first.Root cause
src/agent-device/front/heartbeat.ts:149-154: agent-device answersLEASE_NOT_FOUND, andreleaseOursreleases 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.mdlists 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.allocateagain with the same id and the entry's address, then repeat the heartbeat. Then one PR inheartbeat.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
Risk
edge.test.tsandgateway-front.test.ts, theLEASE_NOT_FOUND/LEASE_EXPIRED/LEASE_REVOKEDtable among them; the ADR decides which still hold (LEASE_REVOKEDprobably should).endedLeases) must not be re-bound.Written by an agent.
V3RON commentedon Nov 8, 2026
Decision
The maintainer approved automatic recovery of agent-device access for a surviving Simlock lease:
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.
V3RON commentedon Nov 8, 2026
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", onbug/1103-repro(8773b642, base944d2549). Cherry-picked onto14163a99, it conflicts once insetUp(keep main'slaunchand addexisting?.clock/existing?.driver), then fails the same way. It covers the gateway path only; the decision's other restart orders belong in the spec.Root cause
src/agent-device/front/heartbeat.ts:149-154: after its restart, agent-device answersLEASE_NOT_FOUND, andreleaseOurs(:154) releases the simlock lease even though the front holds a live entry andlease.liststill 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, andsrc/agent-device/AGENTS.md:77lists 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:
Both red tests pin ADR 0023 §3. I discarded the change afterwards.
Alternatives rejected
LEASE_EXPIRED/LEASE_REVOKEDas well: the decision keeps both terminal.Risk
runId,clientId,deviceKey), so the rough version skipped the allocate reply check (shared/rpc.ts:168). The worker'sReleaseScopeRecordmust learn the scope, or lease end sends no release.retainOnClose(feat(agent-device): retain leases through close #1436) from the first allocate.Fast track
Side findings
Written by an agent.
V3RON commentedon Nov 9, 2026
Being specified in #1461. This issue stays open until that feature ships.
Written by an agent.
github-actions commentedon Nov 9, 2026
Shipped as #1461.