Skip to content

Commit 81b22ab

Browse files
committed
fix(daemon): answer Apple appstate on the named session whatever the selector states
appstate's identity-selector exception over-preserved: a named missing session with an explicit Apple device fell through to the session-required refusal, a route with no sessionless answer. Keep the typed named-session refusal wherever the command cannot serve the stated target without a session, and let only non-Apple identity selectors keep their target. Also pins the appstate refusal's typed reason/session/dispatched details, adds the record seam's no-inventory assertion, and trims seam comments to their invariants.
1 parent 7c3657d commit 81b22ab

9 files changed

Lines changed: 21 additions & 31 deletions

File tree

‎packages/daemon/src/core/explicit-session-resolution.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ export function namedSessionNotFoundError(sessionAddress: string): AppError {
1818
return new AppError(
1919
'SESSION_NOT_FOUND',
2020
`No active session "${sessionAddress}". Run agent-device open <app> --session ${sessionArg} first, or omit --session to run against a device selector.`,
21-
// One producer for the refusal shape: the reason, dispatched marker, and session address come
22-
// from the kernel builder every named-session seam shares (#3674); only this site's message
23-
// adds the open/omit guidance and the session-list hint.
2421
{
2522
...namedSessionNotFoundDetails(sessionAddress),
2623
hint:

‎packages/daemon/src/core/snapshot-session.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,8 @@ export async function resolveSessionDevice(
1414
) {
1515
const ref = boundRef ?? sessionStore.lookup(sessionName);
1616
const session = ref ? sessionStore.requireCurrent(ref) : undefined;
17-
// The guard asks whether a *missing* session was named, so a held session answers it for free
18-
// and the refused path is the only one that loads the module — keeping the guard off the eager
19-
// closure of every request that resolves through here (ADR-0019 loading shape, #3674). It also
20-
// keeps a first-time module load out of the held-session hot path, whose transform cost under
21-
// coverage instrumentation alone overran `wait-runtime.test.ts`'s fake-timer budget when the
22-
// import ran unconditionally.
17+
// Only a missing session can be refused for being named, so only that branch pays the
18+
// function-scoped import (ADR-0019 loading shape).
2319
if (!session) {
2420
const { requireNamedSessionHoldsDevice } = await import('./explicit-session-resolution.ts');
2521
requireNamedSessionHoldsDevice({ req, sessionAddress: sessionName, session });

‎packages/daemon/src/execution/selector-runtime-backend.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,8 @@ async function resolveSelectorRuntimeDevice(
115115
params.captureProof ??= {};
116116
const ref = params.sessionStore.lookup(params.sessionName);
117117
const session = ref?.session;
118-
// Ordered before the required-session return on purpose: a request that *named* this address
119-
// deserves the typed named-session refusal, not the generic one; one that named nothing still
120-
// lands on noActiveSessionError below, which is its own pre-existing refusal. The guard can only
121-
// fire for a missing session, so only that path pays the function-scoped import (ADR-0019
122-
// loading shape).
118+
// Ordered before the required-session return: a named missing session answers with its own
119+
// typed reason. Only that branch pays the function-scoped import (ADR-0019 loading shape).
123120
if (!session) {
124121
const { requireNamedSessionHoldsDevice } =
125122
await import('../core/explicit-session-resolution.ts');

‎packages/daemon/src/server/__tests__/request-router-named-session.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ test('a record start naming a missing session answers on the session, not on app
127127
expect(response.error.code).toBe('SESSION_NOT_FOUND');
128128
expect(response.error.details?.reason).toBe(PRE_DISPATCH_REFUSAL_REASONS.namedSessionNotFound);
129129
expect(response.error.details?.session).toBe(LOST_SESSION);
130+
expect(listings.count).toBe(0);
130131
});
131132

132133
test('the same command with no session named still resolves a device sessionlessly', async () => {

‎packages/daemon/src/sessions/handlers/__tests__/session-appstate-input.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
mockInspectDeviceRuntimeFacts,
1717
} from '../../../__tests__/support/handlers/session-command-harness.ts';
1818
import { mkdtempForTestSync } from '@agent-device/test-kit';
19-
import { AppError } from '@agent-device/kernel/errors';
19+
import { AppError, PRE_DISPATCH_REFUSAL_REASONS } from '@agent-device/kernel/errors';
2020
import type {
2121
AppStateRuntimeInput,
2222
AppStateRuntimeResult,
@@ -239,6 +239,9 @@ test('appstate with explicit missing session returns SESSION_NOT_FOUND', async (
239239
expect(response.error.code).toBe('SESSION_NOT_FOUND');
240240
expect(response.error.message).toMatch(/no active session "sim"/i);
241241
expect(response.error.message).not.toMatch(/omit --session/i);
242+
expect(response.error.details?.reason).toBe(PRE_DISPATCH_REFUSAL_REASONS.namedSessionNotFound);
243+
expect(response.error.details?.session).toBe('sim');
244+
expect(response.error.details?.dispatched).toBe('no');
242245
}
243246
});
244247

‎packages/daemon/src/sessions/handlers/record-runtime.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,8 @@ async function handleRecordCommandUnsafe(
9292
const { req, sessionName, sessionStore } = params;
9393
const existingRef = sessionStore.lookup(sessionName);
9494
const existingSession = existingRef?.session;
95-
// The named-session refusal runs before any record-specific refusal: a request whose session
96-
// this daemon lost is answered on the session first, whatever it wanted to record (#3674). The
97-
// guard can only fire for a missing session, so only that path pays the function-scoped import
98-
// (ADR-0019 loading shape).
95+
// Ordered before every record-specific refusal: a named missing session answers on the
96+
// session. Only that branch pays the function-scoped import (ADR-0019 loading shape).
9997
if (!existingSession) {
10098
const { requireNamedSessionHoldsDevice } =
10199
await import('../../core/explicit-session-resolution.ts');

‎packages/daemon/src/sessions/handlers/session-appstate.ts‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ function appStateSessionNotFoundResponse(
2323
// Every Apple selector — `apple`, `ios`, or `macos` — answers appstate only from an open
2424
// session and its runner, so the "omit --session" route they would otherwise be pointed at
2525
// just reaches the session-required refusal. Send them straight to opening the session.
26-
// The details are the shared named-session refusal's, so one branch on
27-
// `named_session_not_found` sees the same shape wherever a lost session answers (#3674).
2826
if (isApplePlatform(platform)) {
2927
return errorResponse(
3028
'SESSION_NOT_FOUND',
@@ -60,13 +58,15 @@ function admitAppStateTarget(params: RuntimeCommandHandlerParams): AppStateTarge
6058
const flags = req.flags ?? {};
6159
const normalizedPlatform = flags.platform;
6260

63-
// Same priority as the resolution guard (#3674), read for what this command can actually serve:
64-
// an identity selector (`--device`/`--udid`/`--serial`) states a concrete target the daemon can
65-
// answer sessionlessly — an Android device, or an iOS one the caller will run `open` against —
66-
// so the refusal stays out of the way there. A bare `--platform` filter states no device this
67-
// command can answer without a session (Apple appstate is session-required one step later), so
68-
// for those the typed named-session refusal remains the truest answer.
69-
if (!session && namesSessionExplicitly(req) && !hasExplicitDeviceSelector(flags)) {
61+
// Same priority as the resolution guard: a non-Apple identity selector states a target this
62+
// command answers sessionlessly, so the refusal stays out of the way there. Apple-family
63+
// appstate requires an open session whatever the selector states, so the typed refusal remains
64+
// the only actionable answer for it.
65+
if (
66+
!session &&
67+
namesSessionExplicitly(req) &&
68+
(!hasExplicitDeviceSelector(flags) || isApplePlatform(normalizedPlatform))
69+
) {
7070
return { refusal: appStateSessionNotFoundResponse(sessionName, normalizedPlatform) };
7171
}
7272

‎packages/daemon/src/sessions/session-lifecycle/internal/session-close.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,6 @@ async function closeWithoutSession(params: {
433433
bindDevice?: BindDeviceRuntime;
434434
}): Promise<DaemonResponse> {
435435
const { req, sessionName, logPath, inspectFacts, bindDevice, platformServices } = params;
436-
// There is no session here by definition, so this is exactly the refused shape; the guard's own
437-
// provenance check still decides between the typed refusal and sessionless close.
438436
const { requireNamedSessionHoldsDevice } =
439437
await import('../../../core/explicit-session-resolution.ts');
440438
requireNamedSessionHoldsDevice({ req, sessionAddress: sessionName, session: undefined });

‎website/docs/docs/commands.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ Read-only commands such as `get`, `snapshot`, and `wait` report `no`, and so do
485485
These refusals carry `dispatched: no` and a typed `error.details.reason`, so branch on the reason, not the message:
486486
487487
- `session_or_device_selector_required`: the request had neither an active session nor a device selector. Open a session or pass `--platform`. Commands that require a session or a device selector before they read the request's target (`boot`, `clipboard`, `push`, `apps`, and siblings) answer this even when the request named a session the daemon does not hold.
488-
- `named_session_not_found`: the request named a session this daemon does not hold, with no device selector to fall back on. The daemon lost its sessions (replaced or restarted), or the name is wrong; `agent-device session list` shows what it holds.
488+
- `named_session_not_found`: the request named a session this daemon does not hold, with no device selector to fall back on (or no selector it can answer without the session — `appstate` on an Apple target refuses this even with an explicit device). The daemon lost its sessions (replaced or restarted), or the name is wrong; `agent-device session list` shows what it holds.
489489
- `session_app_required`: a per-app setting was requested but the session binds no app and none was named. Run `open <app>`, or name the app on the request for a setting that takes one (`settings clear-app-state com.example.app`).
490490
- `app_not_installed`: the named app resolves to no installed app on the target. Run `apps --all` for the exact bundle id or package, or install the app, then retry.
491491
- `webdriver_route_unsupported`: on a WebDriver-backed device cloud, the driver does not implement the route; the limit is the cloud's driver, not the device. A command with a second route (`orientation` has two) tries it before failing. After an ambiguous failure on that transport, only read-only requests are resent.

0 commit comments

Comments
 (0)