Skip to content

Each lease is its own agent-device tenant #1424

Closed

Description

@V3RON

Part of #1419.

Scope

Each lease is its own agent-device tenant, so two test runs on one token never share a session or reach each other's device, whatever session names they pick. One slow-lane test with real agent-device proves the whole runner flow of #1419: allocate with retainOnClose, boot, closes, a new session, two same-named sessions kept apart, release.

In short

Today every call a token sends reaches agent-device under one tenant, the token's principal (F1), and agent-device keeps a session under <tenant>:<session> (F3). Two runs on one token that both use the e2e runner's default session name e2e-ios-0 (F11) share one session. After this PR every call that names a lease the token holds goes under a tenant made from the principal and the lease id. That covers RPCs that name it and non-RPC calls that carry x-agent-device-lease-id, such as uploads and artifact downloads (F7, F8). So each lease's sessions, uploads and artifacts are its own (F9). A call that names no lease keeps the principal's tenant.

tenant: agent-device's name for who owns a session, an upload or an artifact. Before, for a token whose principal is tok_one: tok_one. After, for lease 0a1b…: on a worker tok_5f_one.0a1b…, through a gateway gw.<instance>.tok_5f_one.0a1b….

Call Today's tenant After
allocate, heartbeat, release, a command of lease L, on a worker <principal> <principal part>.L
an upload or artifact download with x-agent-device-lease-id: L, on a worker <principal> <principal part>.L
the same calls through a gateway gw.<instance>.<principal part> gw.<instance>.<principal part>.L
the front's own devices call for lease L (F18) <principal> <principal part>.L
GET /health, session list, an upload with no lease header <principal> (gateway: gw.<instance>.<principal part>) the same
a tenant longer than 128 characters cut with a hash (gateway only) cut with a hash, worker and gateway

<principal part> is the principal escaped as the gateway escapes it today: a character other than A-Z a-z 0-9 - becomes _<hex>_ (F5).

sequenceDiagram
  participant A as Run A (lease L1, session e2e-ios-0)
  participant B as Run B (lease L2, session e2e-ios-0)
  participant F as front
  participant D as agent-device
  A->>F: snapshot, leaseId L1
  F->>D: tenant tok_5f_one.L1 (changed)
  B->>F: snapshot, leaseId L2
  F->>D: tenant tok_5f_one.L2 (changed)
  Note over D: two sessions, two devices
Loading

If this goes wrong, a lease's calls get two tenants and agent-device refuses its upload install or loses its session, or two runs still share a session.

Technical spec

Facts

Commit e4599cf.

# Fact Source
F1 On a worker the proxy's authenticate gives the tenant identity.principal, from the identity forward tags the request with. src/agent-device/host/host.ts:116-127, :129-150
F2 The proxy sets the tenant over whatever the client sent. agent-device v0.21.35:packages/proxy/src/daemon-proxy.ts:647
F3 agent-device keeps a session under <tenant>:<session> and shows it only to the same tenant. agent-device v0.21.35:packages/daemon/src/core/session-tenant-scope.ts:41, v0.21.35:packages/daemon/src/sessions/session-routing.ts:93-94
F4 A tenant id must match ^[a-zA-Z0-9._-]{1,128}$; simlock's release scope checks the same. agent-device v0.21.35:packages/contracts/src/daemon-http.ts:41; src/agent-device/shared/release-scope.ts:9
F5 The gateway's tenant is gw.<tenantPart(instance)>.<tenantPart(principal)>, cut to 128 characters with a 27-character hash when longer; tenantPart turns a character outside A-Za-z0-9- into _<hex>_. src/agent-device/front/relay-host-link.ts:75-97
F6 On a worker, a relayed call's identity has the gateway's tenant as its principal, and the worker uses it as the tenant. src/agent-device/worker.ts:76-86
F7 agent-device's client sends x-agent-device-lease-id on an upload and an artifact download when it has a lease. agent-device v0.21.35:packages/remote/src/upload-client.ts:218, v0.21.35:packages/remote/src/artifact-download.ts:93
F8 On a worker and a gateway the front reads the lease header of a non-RPC call and passes its lease id to the link's forward; the worker's RoutedHostLink.forward drops it, and nothing on a worker checks that the token holds that lease (its status ignores the identity). The gateway refuses another's lease with 403 refused: lease <id> is not one of yours on this gateway. src/agent-device/front/front.ts:310-340, :363-365, src/agent-device/front/host-link.ts:62, :70-86, src/agent-device/front/relay-host-link.ts:109-111, :145-147
F9 agent-device refuses an upload or artifact entry whose tenant differs from the caller's. agent-device v0.21.35:packages/daemon/src/core/tenant-owned-entry.ts:33-36
F10 The front checks lease ownership by identity.principal, not the tenant. src/agent-device/front/heartbeat.ts:101-104, src/agent-device/front/request-rules.ts:369-372, src/agent-device/front/relay-host-link.ts:145-147
F11 The e2e runner's default session name is e2e-<target>-<slot>. @e2e-dev/mobile v0.12.5:dist/pool.js:149
F12 The lease end records the tenant from each forwarded call's tenantId and removes <tenant>_<session> folders at lease end. src/agent-device/host/lease-end.ts:37-55, :144-145, :169-175
F13 A test asserts two tokens' commands reach agent-device as their principals. src/agent-device/edge.test.ts:207-218
F14 The gateway tests compute the relayed tenant with tenantOf. src/agent-device/gateway-front.test.ts:546, :987, :1110
F15 The slow lane has a two-token, two-device case to model on, and its suite runs for iOS and Android. e2e/slow-agent-device.test.ts:666-731, :819
F16 The lease-end tests assert the release simlock sends at lease end carries the principal as tenant. src/agent-device/edge-lease-end.test.ts:76-91
F17 agent-device refuses a release whose tenant differs from the lease's. agent-device v0.21.35:packages/daemon/src/resources/lease-registry-scope.ts:231-246
F18 The front asks devices once per held lease, naming that lease. src/agent-device/front/devices-filter.ts:119-131, src/agent-device/front/token-devices.ts:64-76
F19 agent-device's CLI has no flag that sends retainOnClose; its client library's leases.allocate does, and budgie allocates that way. agent-device v0.21.35:packages/daemon/src/server/http/http-rpc-methods.ts:160, v0.21.35:src/client/lease-client.ts:20-26, v0.21.35:packages/contracts/src/lease-scope.ts:119

Modules touched

Searches: relayTenant, tenantPart, authenticate, tenantId and principal: tenant in src/agent-device; readsLeaseHeader (5 hits).

flowchart LR
  FR[front/front.ts *] --> HL[front/host-link.ts *]
  FR --> RL[front/relay-host-link.ts *]
  HL --> H[host/host.ts *]
  W[worker.ts] --> HL
  T[shared/tenant.ts * new] 
  HL --> T
  RL --> T
Loading

The tenant is passed beside the identity, not as its principal, since ownership checks read the principal (F10). RoutedHostLink.forward derives it from the lease id it already receives (F8); a relayed call (via: "relay", F6) already carries the gateway's tenant as its principal and keeps it, so it is never derived twice. On a worker, a non-RPC call whose lease header names a lease the token does not hold is refused with 403 refused: lease <id> is not one of yours, as the gateway refuses it (F8). Changed: src/agent-device/front/front.ts, src/agent-device/front/host-link.ts, src/agent-device/front/relay-host-link.ts, src/agent-device/host/host.ts, a new src/agent-device/shared/tenant.ts (tenantPart and the 128-character cut move here), src/agent-device/edge.test.ts, src/agent-device/gateway-front.test.ts, src/agent-device/session-folders.test.ts, src/agent-device/edge-lease-end.test.ts (F16), e2e/slow-agent-device.test.ts, website/src/content/docs/docs/using-devices/agent-device.mdx, src/agent-device/AGENTS.md.

sequenceDiagram
  participant C as runner
  participant G as gateway front
  participant W as worker
  participant D as agent-device
  C->>G: upload, x-agent-device-lease-id L
  G->>W: relay, tenant gw.i.tok_5f_one.L
  W->>D: tenant gw.i.tok_5f_one.L
  D-->>C: upload ticket
  C->>G: install from upload, leaseId L
  G->>W: relay, tenant gw.i.tok_5f_one.L
  W->>D: same tenant: install accepted
  Note over G,W: worker gone: 503 WORKER_UNREACHABLE, as today
Loading

Contract and event changes

None in simlock's contract. The tenant sent to agent-device changes value, within F4's pattern: a lease id is 16 to 128 hex characters (agent-device v0.21.35:packages/daemon/src/resources/lease-registry-scope.ts:130), so the cut applies when the whole is over 128 characters.

Principal Lease id Where Tenant
tok_one 32 hex worker tok_5f_one.<32 hex>: 43 characters, passes
alice.ci 32 hex worker alice_2e_ci.<32 hex>: 44, passes
100 characters 32 hex worker over 128: cut to 100 plus . plus 27-character hash, passes
tok_one 32 hex gateway, instance 36 characters gw.<36>.tok_5f_one.<32 hex>: 83, passes

No event.

Other code on the same state

Searches: tenantId reads in src/agent-device (lease end, release scope, host): 3 components.

Failure modes

Call Outcome What happens Limit, counted from
a call with a lease header for a lease the token does not hold, on a worker refused 403 refused: lease <id> is not one of yours (new; reached agent-device before) none
a relayed call, worker gone unreachable 503 WORKER_UNREACHABLE, as today as today
an upload with no lease header, then an install on lease L agent-device refuses the upload is the principal's, the install is L's; the runner's client always sends the header with a lease (F7), so only a hand-made upload hits this none; no test: no client sends it

A daemon restart ends every agent-device lease today, so no stored tenant needs migrating.

Rules in play

  • ADR 0070 §3, "Every call that names a lease the token holds is sent with a tenant made from the principal and the lease id".
  • ADR 0023 §4, "Agents on different simlock tokens never see each other's agent-device sessions": still true, now also per lease.

Tests

Seam: the agent-device front on a worker and through a gateway, agent-device faked, src/agent-device/edge.test.ts and src/agent-device/gateway-front.test.ts (model: the tenant test at edge.test.ts:207-218, which this changes, and tenantOf at gateway-front.test.ts:546).

  • Calls of two leases on one token reach agent-device as two tenants, and every call of one lease as one. Cases: allocate, heartbeat, command, release of one lease; the release simlock sends at lease end (extends edge-lease-end.test.ts:76); an upload and an artifact download carrying the lease header; a lease header naming another token's lease on a worker (refused 403); two leases; a call naming no lease; gateway; a principal long enough to need the hash.
  • A lease's session folders are removed at lease end under its tenant. Cases: lease released; lease ended by simlock. Extends src/agent-device/session-folders.test.ts:14.
  • With real agent-device on iOS, a runner's lease boots, survives closes, serves a new session, keeps two same-named sessions apart and ends on release. Cases: one run of the whole flow, in the iOS suite only: one token allocates two leases with retainOnClose: true through agent-device's client library (F19), and binds the CLI to each, both with session e2e-ios-0; boot on each answers its own udid; a snapshot on each shows its own device; a bare close and a close <app> on lease 1, then session e2e-ios-1 on lease 1 opens the fixture app and its snapshot works; release of both frees both devices.

Done when

  • pnpm exec vitest run --project unit src/agent-device/edge.test.ts src/agent-device/gateway-front.test.ts src/agent-device/session-folders.test.ts passes with the first two Tests lines.
  • Slow lane (agent-device through simlock, iOS, room for two devices): scripts/slow-e2e.sh e2e/slow-agent-device.test.ts -t "keeps two same-named sessions apart" passes with the third Tests line.
  • agent-device.mdx no longer says two agents sharing a token share a session: it says each lease has its own sessions, and that a call naming no lease still sees the token's.

Out of scope

Depends on

Written by an agent.

Activity

  1. added
    task:draft
    on Nov 7, 2026
  2. added
    task:ready
    and removed
    task:draft
    on Nov 7, 2026
  3. self-assigned this
    on Nov 8, 2026
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