Skip to content

fix(daemon): show a leased session in session_list only to its lease holder - #4917

Merged
thymikee merged 5 commits into
0.22from
fix/0.22-session-list-lease-visibility
Nov 22, 2026
Merged

thymikee merged 5 commits into
0.22from
fix/0.22-session-list-lease-visibility

Conversation

@thymikee

@thymikee thymikee commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Summary

session_list filtered sessions only by the tenant label the caller sends. Every holder of a shared proxy token can send any tenant label, so one client could list another client's session names. A session that holds a lease is now visible to a request only when that request sends the same lease id. sessionVisibleToRequest in session-routing.ts owns the rule. Both callers use it: the session_list filter, and the shutdown DEVICE_IN_USE refusal, which names the owning session only for the lease holder. Sessions with no lease keep today's visibility. Labels never grant visibility. Complements #4911. Closes #4908.

Before / after

session_list (proxy token, tenant label only, no lease id)
base: { sessions: [ "default" ] }   head: { sessions: [] }
with the session's lease id: both show "default"
with another lease id: base "default", head []

Validation

Tested at ddc8c01. check:affected --base origin/0.22 --run passed with exactCommit: true (format, lint, typecheck, layering, fallow, build, vitest-related, unit, command-docs, docs-links, docs-prose). Device lanes are GitHub-authoritative.

New tests: six cases. Four fail at base for the intended reason (three tenant session_list cases, and the shutdown refusal that names the owner without the lease id). Two are guards that pass at base (the lease-id-without-tenant-isolation case, and the shutdown "sending that lease id" case). A provider-scenario probe over the real HTTP path shows base ["default"] for all three calls, head ["default"], [], []. Two integration sessions.list() callers now pass their own lease id. Unleased sessions are unchanged.

Review round two: the session list docs, action help, and both recovery hints now qualify the list by request visibility and point callers to the owning connection or an explicit lease id; client-api.md notes client.sessions.list() without a leaseId omits leased sessions. Follow-up: #4923.

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.16 MB 6.16 MB +409 B
Package (unpacked) 6.16 MB 6.16 MB +409 B
Package (download) 1.88 MB 1.88 MB +207 B

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 19.3 ms 19.5 ms +0.1 ms
CLI --help 37.4 ms 37.4 ms -0.0 ms

@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 0d9e24c. The verdict and the full list are in the summary comment.

@thymikee

Copy link
Copy Markdown
Member Author

The lease gate in session_list looks correct at 0d9e24c, but the docs and recovery hints still describe the old list, so they need one more pass.

A second independent pass found that the session list reference still says the list shows the active sessions in the request scope (commands.md:2348). A workspace can hold a leased session, and a caller can run the documented list command there without the lease id. The base returned the active session in that case. This change omits it (session-routing.ts:94-97). The named-session troubleshooting at commands.md:1392 and the action help at session.ts:15 carry the same wording. Users who inspect sessions will not learn that a leased session needs its lease id. Please state in the reference, the action help and the troubleshooting text that the list shows sessions visible in the request scope, and that a leased session requires its lease id. Point users to their saved connection or an explicitly supplied lease id.

A second independent pass found that the ambiguity recovery still sends callers to an incomplete list (session-routing.ts:307). When a workspace holds several implicit sessions, including a leased one, a platform-free session command returns AMBIGUOUS_MATCH and tells the caller to copy an address from session list. The base list gave the address of every candidate. Now the leased candidate is missing unless the follow-up carries its lease id. The named-session hint at explicit-session-resolution.ts:24 also calls the scoped list a census of all sessions the daemon holds. Please qualify both hints by request visibility and tell callers to use the connection that owns a leased session. The existing platform selector recovery for workspace ambiguity can stay.

Not blocking, and these can be taken or left: (1) an open on a device held by another client's leased session returns DEVICE_IN_USE with the session name in the message and details (open-device-contention-wait.ts:158, session-recovery-hints.ts:56-57), so a follow-up issue could require that every reply naming a session other than the request's own goes through sessionVisibleToRequest, including open contention, device-claim-conflict owner naming and the doctor same-device list; (2) the 'lease id without tenant isolation' test and the shutdown 'sending that lease id' case pass at base, so the Validation line could say four cases fail at base and two are guards; (3) client-api.md:349 could say that client.sessions.list() without a configured leaseId now omits leased sessions, since only the remote proxy page states the rule.

The code change itself looks sound. Required evidence for the PR is present. Smoke Tests (ios/macos) and one other check were still running at review time, and three cancelled runs were superseded PR-body checks with no code route. The smoke lanes drive local unleased sessions, so overlap with this diff is not expected. Before merge, the docs and hints above need the lease qualification, and the running checks need to finish green.

Not verified here: tests and the HTTP probe were not run, and regression validity was judged by reading the base predicate. It was not traced whether every MCP and CLI remote connection profile injects the saved lease id into session_list meta. That point relies on the #4911 statement that the CLI sends the saved lease id on every command, and on the client config merge in packages/client/src/index.ts.

@thymikee

Copy link
Copy Markdown
Member Author

Review round at ddc8c01 (docs and hints pass, code unchanged):

  1. Session list text. commands.md (Session inspection) now states the list shows the active sessions visible to the request scope, that a leased session appears only when the request sends its lease id, and points to the saved connection (the CLI sends the saved lease id) or --lease-id <id>. The named-session troubleshooting bullet carries the same qualification. The session action help in packages/command-surface/src/management/session.ts says list shows the sessions visible to the request scope; a leased session appears only when the request sends its lease id. pnpm generate:command-docs reports commands.md current; the edited passages sit outside generated blocks.
  2. Recovery hints. The AMBIGUOUS_MATCH hint (session-routing.ts) now adds: A leased session is listed only through the connection that owns it, or with its lease id. The platform-selector recovery sentences are unchanged. The named-session hint (explicit-session-resolution.ts) no longer calls the list a census: it now reads the sessions visible to this request, with the same lease sentence. Focused tests (session-routing.test.ts, explicit-session-resolution.test.ts) pass unchanged; no test asserted the removed prose.
  3. client-api.md. Added: client.sessions.list() without a configured leaseId omits leased sessions.
  4. PR body Validation line. Updated: four cases fail at base, two are guards (lease-id-without-tenant-isolation, shutdown "sending that lease id"). Body stays under the gate's word limit.
  5. Follow-up issue. Opened Name another session in a reply only when sessionVisibleToRequest allows it #4923: route every reply naming another session through sessionVisibleToRequest (open contention, device-claim-conflict owner naming, doctor same-device list). Links fix(daemon): show a leased session in session_list only to its lease holder #4917 and session_list exposes session names to every holder of the proxy token #4908.

Gate: pnpm check:affected --base origin/0.22 --run --head ddc8c01 passed with exactCommit: true (format, lint, typecheck, layering, fallow, build, vitest-related, unit, command-docs, docs-links, docs-prose).

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

The code at ddc8c01 fixes the problems from the earlier review (#4917 (comment)), and no new code problem was found in the delta. No conflicts are known. The Body rules check fails, and it is a PR-body lint job. The delta touches no PR body, so the failure cannot be tied to the delta. Please check the body against docs/agents/pull-requests.md before merge.

Not verified here: tests and CI were not run on the delta, which only changes strings and docs. Not verified here: how the CLI injects the saved lease id into every command's meta. Please show one live run where a session created with a lease is listed by session_list for the holder and hidden from a request with no lease, and name the output that proves both.

Not blocking, and these can be taken or left: for an implicit local request with a working directory, the filter in https://github.com/callstack/agent-device/blob/ddc8c01/packages/daemon/src/sessions/session-lifecycle/internal/inventory.ts#L126 calls resolveSessionScope for every reference. That repeats realpath, the ancestor walk for .git, and hashing for the same input, while the base inventory resolved the request scope once. Could session-routing.ts own a request-bound visibility predicate that resolves the session scope and request lease id once, then applies the policy to each session? Both call sites use the same disclosure policy, and the existing admission assertion allows missing fields and throws on conflicts, so it cannot replace this visibility check.

One open inline thread does not apply: replies that name another session are tracked by #4923 and are out of scope, and the code is unchanged (#4917 (comment)). Please resolve it.

@thymikee

Copy link
Copy Markdown
Member Author

Live check at ddc8c01: iOS 26.2 simulator (e2e-apex), an agent-device proxy built from this branch, separate client state dirs, one shared proxy token.

A: connect proxy --tenant e2b --session agentA; open com.apple.Preferences   -> Opened
A (lease holder, saved connection): session list --session agentA --json
  {"success":true,"sessions":["e2b:agentA"]}
C (same token + tenant, no lease id): session list --daemon-base-url <proxy> --tenant e2b --session-isolation tenant --session agentA --json
  {"success":true,"sessions":[]}
C with a wrong --lease-id:
  {"success":true,"sessions":[]}

Cleanup: A closed and disconnected, proxy stopped, simulator shut down. Body rules passes on ddc8c01. I did not take the optional single-predicate refactor in inventory.ts; the predicate is cheap per session, and the list is small.

@thymikee

Copy link
Copy Markdown
Member Author

The live check at ddc8c01 covers the requested evidence: the lease holder lists e2b:agentA, and a request with the same token and tenant but no lease id, or a wrong lease id, lists no sessions. Body rules also passes now. Declining the optional single-predicate refactor is fine.

This head now conflicts with main. Please rebase onto main and push; the rebased head gets a delta review.

@thymikee
thymikee force-pushed the fix/0.22-session-list-lease-visibility branch from ddc8c01 to 24f73dd Compare November 21, 2026 20:09
@thymikee

Copy link
Copy Markdown
Member Author

Rebased onto the rewritten 0.22 (80c44fe45e). Only this PR's two commits were replayed (git rebase --onto origin/0.22 b656532877), with no conflicts. New head 24f73ddc0e. check:affected --base origin/0.22 --run passed at that commit (exactCommit). The code and docs are unchanged from ddc8c01e5e; git range-diff b656532877..ddc8c01e5e origin/0.22..24f73ddc0e shows the same two patches.

@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 finding moved to a new line on 24f73dd. The verdict and the full list are in the summary comment.

@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 6f07260. The verdict and the full list are in the summary comment.

…ts lease holder

Reverts 6f07260. Naming the request's own address without the lease id
lets a guessing caller confirm that a session exists and holds the device.
@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

The conflict from the earlier review (#4917 (comment)) is fixed. Commit db3b9b6 has one problem left in the inventory filter. All 16 checks pass, and no conflicts were reported.

The base resolves the caller's workspace once per list. The head calls resolveSessionScope from inside the row filter (https://github.com/callstack/agent-device/blob/db3b9b6/packages/daemon/src/sessions/session-lifecycle/internal/inventory.ts#L126). For an implicit request with a cwd, each call runs a synchronous realpath and checks ancestor directories for .git. A read-only comparison counted one realpath and one .git probe on the base, and 32 of each on the head, for 32 stored sessions. A second comparison used a realpath stub that changed roots between calls. The base returned only workspace A. The head returned rows from both A and B. Retargeting a symlink used as the cwd while the filter runs can produce that sequence. So a listing can mix sessions from two workspaces, and ordinary listings do filesystem work for every stored session, including sessions outside the caller's scope. Please resolve the ownership scope and the request lease ID once before filtering. Inventory and shutdown should apply the same session visibility predicate.

Can session-routing.ts build that predicate once from the request's resolved scope and lease ID, and let inventory and shutdown apply it to each candidate?

The two open inline threads (#4917 (comment) and #4917 (comment)) are fixed at this head and can be resolved. The first was marked fixed in 6f07260, but db3b9b6 reverted that commit, so the code is back at the 24f73dd shape. The second is fixed because a request without a lease ID gets visible=false at session-shutdown.ts:66, and the refusal omits session and hint. The test at session-shutdown.test.ts:113-174 covers that case.

Not verified here: tests, gates and the provider-scenario HTTP probe were not run. The earlier validation was at ddc8c01, and the PR's own production diff is unchanged after the rebase, so the range-diff and the passing checks were used instead. Not verified here: #4911 (admission refusal of a lease holder that omits its ID), which is only referenced in the author's reply. Once the scope is resolved once per list, nothing else stops the merge.

@thymikee

Copy link
Copy Markdown
Member Author

Fixed at 6bea320: the session list scope is now resolved once per request.

  • session-routing.ts replaces sessionVisibleToRequest with createSessionVisibilityPredicate(req). It resolves the request's ownership scope and lease id once, then applies the same rule to each session: the session matches the inventory scope, and a leased session is visible only with its lease id.
  • inventory.ts builds the predicate once before filtering, and the shutdown refusal uses the same predicate. The disclosure rule is unchanged.
  • New test in session-inventory-scoped-paths.test.ts: realpath returns workspace A on the first call and workspace B after that. The list returns only A's sessions, and realpath runs once. The test fails at db3b9b6.

The check:affected --run stages up to and including vitest-related pass at 6bea320. The broad unit stage had one unrelated 5 s timeout (packages/client/src/__tests__/index-metro.test.ts), which passes 5/5 alone; hosted CI decides. Both inline threads were already resolved.

@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of 6bea320

Since the review of db3b9b6: 0 new · 0 still open · 0 no longer raised

No open findings.

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

💬 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 6bea320. The earlier findings from db3b9b6 (#4917 (comment)) are fixed, and no new findings came up. The delta resolves the scope and the lease id once per list, and shares one predicate between inventory and shutdown. The 16 checks are green; the author reported one unrelated 5 s timeout in packages/client index-metro.test.ts that passes alone, and the diff does not touch that route. There are no conflicts.

Not blocking: the PR body still names sessionVisibleToRequest as the rule owner and says "Tested at ddc8c01, six cases", while the head uses createSessionVisibilityPredicate and has a seventh test. Rename the symbol when the body is next edited. You can take or leave this.

Of the open inline threads, the one on the shutdown refusal losing the name for the lease holder (#4917 (comment)) no longer applies, because db3b9b6 reverted that behavior and the head keeps the visible-only naming at session-shutdown.ts:66-72 with a covering test. The one on a request without a lease id (#4917 (comment)) is fixed at the head: that request gets visible=false, the refusal omits session and hint, and session-shutdown.test.ts covers both lease-id cases. Please resolve both threads.

Not verified here: the tests and gates, including the new realpath test, were not run; the test was traced from the code. The provider-scenario HTTP probe and the validation run in the PR body are at ddc8c01, and the delta is a behavior-preserving refactor of the predicate that was not re-probed at the head. #4923 (replies naming another session) was not reviewed.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 21, 2026
@thymikee
thymikee merged commit f409db1 into 0.22 Nov 22, 2026
16 checks passed
@thymikee
thymikee deleted the fix/0.22-session-list-lease-visibility branch November 22, 2026 07:02
thymikee added a commit that referenced this pull request Nov 23, 2026
…holder (#4917)

* fix(daemon): show a leased session in session_list only to its lease holder

* docs(sessions): say that session list shows only sessions visible to the request

* fix(daemon): name the request's own session in a shutdown refusal

* revert(daemon): name a leased session in a shutdown refusal only to its lease holder

Reverts 6f07260. Naming the request's own address without the lease id
lets a guessing caller confirm that a session exists and holds the device.

* fix(daemon): resolve the session list scope once per request
hannojg pushed a commit to hannojg/agent-device that referenced this pull request Nov 23, 2026
…holder (callstack#4917)

* fix(daemon): show a leased session in session_list only to its lease holder

* docs(sessions): say that session list shows only sessions visible to the request

* fix(daemon): name the request's own session in a shutdown refusal

* revert(daemon): name a leased session in a shutdown refusal only to its lease holder

Reverts 6f07260. Naming the request's own address without the lease id
lets a guessing caller confirm that a session exists and holds the device.

* fix(daemon): resolve the session list scope once per request
thymikee added a commit that referenced this pull request Nov 24, 2026
…holder (#4917)

* fix(daemon): show a leased session in session_list only to its lease holder

* docs(sessions): say that session list shows only sessions visible to the request

* fix(daemon): name the request's own session in a shutdown refusal

* revert(daemon): name a leased session in a shutdown refusal only to its lease holder

Reverts 6f07260. Naming the request's own address without the lease id
lets a guessing caller confirm that a session exists and holds the device.

* fix(daemon): resolve the session list scope once per request
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