Skip to content

fix(daemon): refuse an allocate that repeats lease labels without the lease id - #4947

Merged
thymikee merged 3 commits into
0.22from
fix/0.22-run-binding-refresh-lease-id
Nov 22, 2026
Merged

thymikee merged 3 commits into
0.22from
fix/0.22-run-binding-refresh-lease-id

Conversation

@thymikee

@thymikee thymikee commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Summary

lease_allocate refreshed a live run binding from its labels alone: repeating the tenant, run, backend, provider, device key, and client id returned that lease with its lease id, though the caller held nothing. The lease id is the credential (ADR 0007); the labels are guessable (#4909). An allocate that repeats a live binding's labels without that binding's lease id is now refused as contention, DEVICE_IN_USE / DEVICE_LEASE_BUSY, with no lease id in the reply. A label that contradicts the live lease stays UNAUTHORIZED / LEASE_SCOPE_MISMATCH. A refresh that sends the lease id still gets that lease.

Before / after

allocate {tenant,run,backend,provider,deviceKey,clientId}          # no leaseId
base: { lease: { leaseId: "9f3c…" } }   head: DEVICE_IN_USE, reason DEVICE_LEASE_BUSY

allocate { …same labels, different clientId }                       # device-less lease
base: UNAUTHORIZED, reason LEASE_SCOPE_MISMATCH   head: same

allocate { …same labels, leaseId: "9f3c…" }
base: { lease: { leaseId: "9f3c…" } }   head: same

Breaking

  • Direct RPC/SDK callers that re-allocate by labels without the lease id must send the id their first reply carried.
  • CLI: re-opening a session from fresh or lost state while the old lease is live is now refused with DEVICE_IN_USE / DEVICE_LEASE_BUSY until that lease expires or the saved lease id is sent; the base handed the live lease back. connect --force after a failed release hits the same refusal.

Validation

Head 4cd3345: real-daemon RPC and CLI-replay tests assert the refusal leaks no lease id and the id-based refresh and re-open still recover; registry tests keep the scope-mismatch refusal. pnpm typecheck, lint, format, targeted vitest, and pnpm check:affected --run --base origin/0.22 pass. Risk: daemons older than caller-chosen leaseId ignore it; the docs say so.

Closes #4909

@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.08 MB 6.08 MB -1.1 kB
Package (unpacked) 6.08 MB 6.08 MB -1.1 kB
Package (download) 1.86 MB 1.86 MB -465 B

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 19.9 ms 20.3 ms +0.4 ms
CLI --help 41.9 ms 41.0 ms -0.9 ms

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

The daemon change in a395cce matches the invariant from #4909, but it changes CLI behavior and the CLI test no longer shows that.

The CLI derives a leased session's run and client labels from the session name and profile (https://github.com/callstack/agent-device/blob/a395cce/packages/remote/src/remote-connection-state.ts#L424). When it has no saved lease id, it allocates with a fresh random id (https://github.com/callstack/agent-device/blob/a395cce/src/cli/commands/connection-lease-transaction.ts#L396). Two cases reach that allocate: re-opening the same session from a fresh or lost state dir, and connect --force after the old lease could not be released (https://github.com/callstack/agent-device/blob/a395cce/src/cli/commands/connection-previous-lease.ts#L113). The base daemon handed back the live lease. At this head the allocate hits assertNoRunBindingLease (https://github.com/callstack/agent-device/blob/a395cce/packages/daemon/src/resources/lease-registry.ts#L208) and fails with DEVICE_IN_USE / DEVICE_LEASE_BUSY until the lease expires. The PR body says "The CLI is unaffected", and the Breaking section names only direct RPC/SDK callers. Keep the daemon change and own it. The rule: no test may make a CLI-captured allocation succeed by sending an id the CLI did not send. In https://github.com/callstack/agent-device/blob/a395cce/src/__tests__/cli-leased-session.test.ts#L148, leaseIdsOnRealDaemon injects the earlier lease's id (lines 153-170), so assert.equal(leaseAAgain, leaseA) at line 534 now holds by construction. Replay the captured allocations as sent, make the "one session keeps its own" case assert the fresh-state re-open is refused with DEVICE_IN_USE / DEVICE_LEASE_BUSY and gets no lease id, rename the test to match, and add the CLI case to the Breaking section.

A second independent pass found the same CLI gap from the other side. Resuming a live CLI lease should need the saved connection record, or an explicitly transferred lease id, with a matching scope (https://github.com/callstack/agent-device/blob/a395cce/src/cli/commands/connection-runtime.ts#L72). Keep a separate test that reopens with the saved credential.

A second independent pass found that the scope-mismatch refusal changed for device-less bindings (https://github.com/callstack/agent-device/blob/a395cce/packages/daemon/src/resources/lease-registry.ts#L217). With a live device-less lease that has a client id, an allocate with the same tenant, run, backend and provider but a different explicit client id was rejected on base with UNAUTHORIZED / LEASE_SCOPE_MISMATCH. At head it gets DEVICE_IN_USE / DEVICE_LEASE_BUSY, so a caller that branches on the typed scope error now sees device contention. Keep the base scope-mismatch refusal for every request the base refused, and use DEVICE_LEASE_BUSY only for requests that passed that check and would have reused the binding without the credential.

A second independent pass found that the retainOnClose documentation is now wrong (https://github.com/callstack/agent-device/blob/a395cce/packages/contracts/src/client-lease.ts#L98). Allocating with the same labels and retainOnClose: true without a per-call leaseId enabled retention on base. At head it throws DEVICE_LEASE_BUSY and retention stays off (see https://github.com/callstack/agent-device/blob/a395cce/packages/daemon/src/resources/__tests__/lease-registry.test.ts#L244). The SDK reads the id from call options (https://github.com/callstack/agent-device/blob/a395cce/packages/client/src/lease-client.ts#L20), so client-level configuration alone has the same result. State in the option's documentation that enabling it for a live lease needs the current lease id in that allocate call, with the matching scope.

Not blocking, and these can be taken or left: putHostLease still throws its own inline DEVICE_LEASE_BUSY for a live run binding (https://github.com/callstack/agent-device/blob/a395cce/packages/daemon/src/resources/lease-registry.ts#L135), so one refusal now has two spellings and a follow-up could call assertNoRunBindingLease there; the first rewritten test in lease-registry.test.ts could use isDeviceLeaseBusy plus the leaseId-absent assert, and the map key in cli-leased-session.test.ts lines 448-452 could be computed once.

The only failing check is Body rules, which looks unrelated to the code: the PR body lacks the required Summary, Before / after and Validation sections. There are no conflicts. Not verified here: the new tests and the CLI regression were judged by reading base and head code, and no tests were run. Not verified here: daemons older than the caller-chosen leaseId field against the "older daemon" doc sentences. Revyl's leases.allocate is not a daemon lease_allocate caller, but other external SDK integrators were not checked.

Before merge, replay the CLI allocations as sent, assert the fresh-state refusal, and name the CLI case in Breaking. Then add the missing PR body sections so Body rules passes.

… lease id

refreshExistingRunBinding returned a live lease, with its id, to any
lease_allocate that repeated the tenant, run, backend, provider, device
key, and client id. The lease id is the bearer credential for a leased
session (ADR 0007), and those labels are guessable, so a direct RPC or
SDK caller could receive another caller's lease id.

An allocation that repeats a live run binding's labels without that
binding's lease id is now refused as contention, with DEVICE_IN_USE and
reason DEVICE_LEASE_BUSY, and no lease id in the reply. This applies to
device-less leases too. An owner refresh that sends the lease id still
gets its lease back through refreshRequestedLease. The CLI already sends
the lease id it holds, so no caller changed.
…n contract

A device-less run binding that a request contradicts with an explicit label
stays UNAUTHORIZED / LEASE_SCOPE_MISMATCH, as the base refused it;
DEVICE_LEASE_BUSY names only the repeat that matched the binding without the
lease id.

The CLI replays prove both halves: a re-open from fresh state has lost the
lease id, so it is refused as contention and gets no lease id, and a re-open
that sends the id the allocation named gets that lease back.
@thymikee
thymikee force-pushed the fix/0.22-run-binding-refresh-lease-id branch from a395cce to 4cd3345 Compare November 22, 2026 09:22
@thymikee

Copy link
Copy Markdown
Member Author

Addressed in 4cd3345 (rebased on 0.22):

  1. leaseIdsOnRealDaemon no longer injects ids: replayLeaseAllocationsOnRealDaemon replays each captured allocation exactly as sent. The shared-profile test is renamed to "…a fresh-state re-open is refused" and asserts the re-open's refusal is DEVICE_IN_USE / DEVICE_LEASE_BUSY with no lease id in the details. A separate test keeps the resume half: replaying the CLI's allocation, which names its caller-chosen id, gets that lease back.
  2. assertNoRunBindingLease now runs assertLeaseScopeMatch for device-less bindings before the busy throw, so a contradicting label stays UNAUTHORIZED / LEASE_SCOPE_MISMATCH as on base; DEVICE_LEASE_BUSY names only a repeat whose labels all agree. Registry and contract-doc coverage added.
  3. LeaseAllocateOptions.retainOnClose now states that enabling it on a live lease needs that lease's current leaseId in the call, with the matching scope.
  4. The body has Summary, Before / after, and Validation, checked with scripts/check-pr-body.ts. The CLI fresh-state re-open refusal is named in Breaking.

Declined the optional cleanups: putHostLease is the host-admin path and its inline refusal is out of this issue's scope.

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 1 new finding on 4cd3345. The verdict and the full list are in the summary comment.

The daemon never fills a missing lease id from the session.
Tenant, run id, lease backend, client id, device key, and lease provider are labels.
The daemon refuses a label that differs from the session lease. A missing label is not a refusal and grants nothing.
An allocate that repeats the labels of a live lease without that lease's id is refused as contention, with `DEVICE_IN_USE` and reason `DEVICE_LEASE_BUSY`. It returns no lease id. A label that contradicts the live lease stays a scope mismatch, with `UNAUTHORIZED` and reason `LEASE_SCOPE_MISMATCH`. Only an allocate that sends the id of its live lease gets that lease back.

@apex-sentinel-staging apex-sentinel-staging Bot Nov 22, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer raised on 5b413c5: marked fixed in 5b413c5 by thymikee in this comment; the code it pointed at changed.

🆕 🟡 Minor · Contract doc claims any contradicting label is a scope mismatch; device-keyed leases say busy

A repeat allocate against a live device-keyed lease with a contradicting clientId (same tenant, run, backend, provider, deviceKey) returns DEVICE_IN_USE / DEVICE_LEASE_BUSY, not UNAUTHORIZED / LEASE_SCOPE_MISMATCH. Cause: assertNoRunBindingLease only calls assertLeaseScopeMatch when the live lease has no deviceKey; the same over-claim appears in the method's docstring, whose own device-keyed test asserts the busy error.

Suggested change

Scope the new sentence (and the assertNoRunBindingLease docstring) to device-less leases: a label that contradicts a device-less live lease is LEASE_SCOPE_MISMATCH, while a repeat of a live device-keyed binding is DEVICE_LEASE_BUSY whatever else its clientId says.

Details

Also raised here

Also raised at this location by persona maintainability: New doc comment claims label disagreements always stay LEASE_SCOPE_MISMATCH — An allocate that repeats a live device-KEYED lease's tenant, run, backend, provider and device but names a different clientId is refused with DEVICE_LEASE_BUSY, not the LEASE_SCOPE_MISMATCH the new comment and the new contracts-doc sentence promise: assertLeaseScopeMatch runs only under if (!existingLease.deviceKey), and deviceLeaseBusyError is thrown unconditionally after it. The same contradicting label on a device-less lease does produce LEASE_SCOPE_MISMATCH, which the new registry test pins, so the two paths answer one condition with two different reason codes. Scope the comment (and the added sentence in packages/daemon/docs/remote-device-lease-contracts.md) to device-less leases, or run the scope check for device-keyed leases too so the taxonomy matches the documentation.

persona: javascript, senior-developer, maintainability

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5b413c5: the contract doc sentence and the assertNoRunBindingLease docstring now say the scope mismatch applies to a device-less live lease, and a repeat of a device-keyed binding is DEVICE_LEASE_BUSY whatever its client id says.

@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of 5b413c5

Since the review of 4cd3345: 0 new · 0 still open · 1 no longer raised (1 marked fixed)

No open findings.

1 finding below minor hidden (user_settings.min_severity).

Reviewed with this repository's own Sentinel configuration (d1ace8139eb2).

💬 Talking to Sentinel

In a finding's thread, reply with:

Command What it does
@apex-sentinel-staging note <context> Adds context; Sentinel re-checks the cited code
@apex-sentinel-staging pivot <why> Says the finding is wrong; Sentinel re-checks the cited code
@apex-sentinel-staging like Marks the finding as useful
@apex-sentinel-staging dislike <why> Marks it as not useful, so it is not raised again

On this conversation, comment @apex-sentinel-staging review to run a full review once again.

@thymikee

Copy link
Copy Markdown
Member Author

This PR is ready at 5b413c5. The earlier findings from a395cce (#4947 (comment)) are fixed: the test helper now exercises the real refresh path, the device-less LEASE_SCOPE_MISMATCH refusal is kept, and retainOnClose is documented. All 16 checks pass at 5b413c5, and there are no conflicts. Nothing else must happen before merge.

Not blocking, and fine to take or leave: the test 'a re-open that sends the lease id it saved gets its live lease back' in src/tests/cli-leased-session.test.ts replays the first open's captured allocation twice (https://github.com/callstack/agent-device/blob/5b413c5/src/__tests__/cli-leased-session.test.ts#L556). No second CLI open runs from the saved state, so it proves the daemon's id retry, which lease-registry tests already cover, and it also passes on the base daemon. Renaming it to describe an id retry, or running a second open with the same state dir and replaying that allocation, would make the name match what it proves.

The open inline thread on the contract doc (#4947 (comment)) no longer applies. Line 80 of remote-device-lease-contracts.md now scopes the scope-mismatch wording to device-less leases and says device-keyed repeats are DEVICE_LEASE_BUSY, which matches lease-registry.ts:219-220. It can be resolved.

Not verified here: tests were not run locally, and regression validity was judged by reading the base and a395cce code against the new assertions. The older-daemon sentences in client-api.md and remote-proxy.md were checked against base code only, and no older daemon build was run.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 22, 2026
@thymikee
thymikee merged commit db59d00 into 0.22 Nov 22, 2026
16 checks passed
@thymikee
thymikee deleted the fix/0.22-run-binding-refresh-lease-id branch November 22, 2026 10:13
thymikee added a commit that referenced this pull request Nov 23, 2026
… lease id (#4947)

* fix(daemon): refuse an allocate that repeats lease labels without the lease id

refreshExistingRunBinding returned a live lease, with its id, to any
lease_allocate that repeated the tenant, run, backend, provider, device
key, and client id. The lease id is the bearer credential for a leased
session (ADR 0007), and those labels are guessable, so a direct RPC or
SDK caller could receive another caller's lease id.

An allocation that repeats a live run binding's labels without that
binding's lease id is now refused as contention, with DEVICE_IN_USE and
reason DEVICE_LEASE_BUSY, and no lease id in the reply. This applies to
device-less leases too. An owner refresh that sends the lease id still
gets its lease back through refreshRequestedLease. The CLI already sends
the lease id it holds, so no caller changed.

* fix(daemon): keep the scope-mismatch refusal and prove the CLI re-open contract

A device-less run binding that a request contradicts with an explicit label
stays UNAUTHORIZED / LEASE_SCOPE_MISMATCH, as the base refused it;
DEVICE_LEASE_BUSY names only the repeat that matched the binding without the
lease id.

The CLI replays prove both halves: a re-open from fresh state has lost the
lease id, so it is refused as contention and gets no lease id, and a re-open
that sends the id the allocation named gets that lease back.

* docs(daemon): scope the lease scope-mismatch sentence to device-less leases
hannojg pushed a commit to hannojg/agent-device that referenced this pull request Nov 23, 2026
… lease id (callstack#4947)

* fix(daemon): refuse an allocate that repeats lease labels without the lease id

refreshExistingRunBinding returned a live lease, with its id, to any
lease_allocate that repeated the tenant, run, backend, provider, device
key, and client id. The lease id is the bearer credential for a leased
session (ADR 0007), and those labels are guessable, so a direct RPC or
SDK caller could receive another caller's lease id.

An allocation that repeats a live run binding's labels without that
binding's lease id is now refused as contention, with DEVICE_IN_USE and
reason DEVICE_LEASE_BUSY, and no lease id in the reply. This applies to
device-less leases too. An owner refresh that sends the lease id still
gets its lease back through refreshRequestedLease. The CLI already sends
the lease id it holds, so no caller changed.

* fix(daemon): keep the scope-mismatch refusal and prove the CLI re-open contract

A device-less run binding that a request contradicts with an explicit label
stays UNAUTHORIZED / LEASE_SCOPE_MISMATCH, as the base refused it;
DEVICE_LEASE_BUSY names only the repeat that matched the binding without the
lease id.

The CLI replays prove both halves: a re-open from fresh state has lost the
lease id, so it is refused as contention and gets no lease id, and a re-open
that sends the id the allocation named gets that lease back.

* docs(daemon): scope the lease scope-mismatch sentence to device-less leases
thymikee added a commit that referenced this pull request Nov 24, 2026
… lease id (#4947)

* fix(daemon): refuse an allocate that repeats lease labels without the lease id

refreshExistingRunBinding returned a live lease, with its id, to any
lease_allocate that repeated the tenant, run, backend, provider, device
key, and client id. The lease id is the bearer credential for a leased
session (ADR 0007), and those labels are guessable, so a direct RPC or
SDK caller could receive another caller's lease id.

An allocation that repeats a live run binding's labels without that
binding's lease id is now refused as contention, with DEVICE_IN_USE and
reason DEVICE_LEASE_BUSY, and no lease id in the reply. This applies to
device-less leases too. An owner refresh that sends the lease id still
gets its lease back through refreshRequestedLease. The CLI already sends
the lease id it holds, so no caller changed.

* fix(daemon): keep the scope-mismatch refusal and prove the CLI re-open contract

A device-less run binding that a request contradicts with an explicit label
stays UNAUTHORIZED / LEASE_SCOPE_MISMATCH, as the base refused it;
DEVICE_LEASE_BUSY names only the repeat that matched the binding without the
lease id.

The CLI replays prove both halves: a re-open from fresh state has lost the
lease id, so it is refused as contention and gets no lease id, and a re-open
that sends the id the allocation named gets that lease back.

* docs(daemon): scope the lease scope-mismatch sentence to device-less leases
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-human Valid work that needs human implementation, judgment, or maintainer merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant