Skip to content

Commit 54a38c6

Browse files
committed
fix(daemon): keep the typed named-session reason on the appstate device route (#3674)
`appstate --session lost --udid <ios>` without `--platform` passed the admission guard, listed the host, resolved onto an Apple device, and answered the reasonless session-required refusal — contradicting the commands.md promise and paying for a listing the typed answer makes unnecessary. The device-runtime lane now answers a named-missing session with the same typed refusal as every other seam; a request that named no session keeps the plain session-required advice, and the non-Apple identity-selector lane is untouched. The refusal builder moved next to its sibling Apple refusals so both routes share one wire shape. Co-Authored-Worked-On-By: Claude
1 parent c13c054 commit 54a38c6

4 files changed

Lines changed: 83 additions & 29 deletions

File tree

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,39 @@ test('appstate --platform apple missing-session error never suggests omitting --
6969
}
7070
});
7171

72+
test('appstate --udid with no --platform still answers the typed reason for a named missing session', async () => {
73+
// `--udid` is the Apple identity flag, so a bare `--udid <ios>` reaches the device-runtime lane
74+
// and lands on an Apple device. The commands.md row promises the typed reason "even with an
75+
// explicit device", so this lane — the only appstate route that resolves a device — answers it
76+
// too, not the reasonless session-required refusal.
77+
const iosSim: DeviceInfo = {
78+
platform: 'apple',
79+
appleOs: 'ios',
80+
id: 'SIM-UDID-A',
81+
name: 'iPhone 17 Pro',
82+
kind: 'simulator',
83+
target: 'mobile',
84+
booted: true,
85+
};
86+
const resolved = vi.mocked(resolveTargetDevice).mockResolvedValue(iosSim);
87+
try {
88+
const response = await handleAppStateCommand({
89+
platformServices: daemonPlatformServicesFixture(),
90+
req: appStateRequest('lost', { session: 'lost', udid: 'SIM-UDID-A' }),
91+
sessionName: 'lost',
92+
sessionStore: makeSessionStore(),
93+
});
94+
95+
expect(response.ok).toBe(false);
96+
if (response.ok) return;
97+
expect(response.error.code).toBe('SESSION_NOT_FOUND');
98+
expect(response.error.details?.reason).toBe(PRE_DISPATCH_REFUSAL_REASONS.namedSessionNotFound);
99+
expect(response.error.details?.session).toBe('lost');
100+
} finally {
101+
resolved.mockReset();
102+
}
103+
});
104+
72105
test('appstate with a named session and an identity device selector keeps the stated target', async () => {
73106
// Same priority as the resolution guard (#3674): the request names a session this daemon lost
74107
// but states a concrete device, so it is not answered on the missing session. It resolves the

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import type { DaemonResponse } from '@agent-device/kernel/contracts';
22
import type { AppleApplicationState } from '@agent-device/kernel/snapshot';
33
import {
4+
isApplePlatform,
45
isMacOs,
56
isIosFamily,
67
publicPlatformString,
78
type DeviceInfo,
9+
type PlatformSelector,
810
} from '@agent-device/kernel/device';
911
import type { SessionSurface } from '@agent-device/contracts/session';
1012
import { errorResponse } from '@agent-device/kernel/contracts';
11-
import { asAppError, isRequestCanceledError } from '@agent-device/kernel/errors';
13+
import {
14+
asAppError,
15+
isRequestCanceledError,
16+
namedSessionNotFoundDetails,
17+
} from '@agent-device/kernel/errors';
1218
import { emitDiagnostic } from '@agent-device/host-kit/diagnostics';
1319
import { appStateUse } from '@agent-device/contracts/platform-runtime-operations';
1420
import { admitRuntimeUse } from '../../core/runtime-admission.ts';
@@ -30,6 +36,30 @@ const IOS_APPSTATE_SESSION_REQUIRED_MESSAGE =
3036
const MACOS_APPSTATE_SESSION_REQUIRED_MESSAGE =
3137
'macOS appstate requires an active session on the target device. Run open first (for example: open --session macos --platform macos "System Settings").';
3238

39+
/**
40+
* The refusal for an appstate whose session the daemon does not hold. Every Apple selector —
41+
* `apple`, `ios`, or `macos` — answers appstate only from an open session and its runner, so the
42+
* "omit --session" route they would otherwise be pointed at just reaches the session-required
43+
* refusal. Send them straight to opening the session.
44+
*/
45+
export function appStateSessionNotFoundResponse(
46+
sessionName: string,
47+
platform: PlatformSelector | undefined,
48+
): DaemonResponse {
49+
if (isApplePlatform(platform)) {
50+
return errorResponse(
51+
'SESSION_NOT_FOUND',
52+
`No active session "${sessionName}". Run open with --session ${sessionName} first.`,
53+
namedSessionNotFoundDetails(sessionName),
54+
);
55+
}
56+
return errorResponse(
57+
'SESSION_NOT_FOUND',
58+
`No active session "${sessionName}". Run open with --session ${sessionName} first, or omit --session to query by device selector.`,
59+
namedSessionNotFoundDetails(sessionName),
60+
);
61+
}
62+
3363
/**
3464
* Apple platforms answer appstate from a session and its runner, never from a bare device query:
3565
* this is the refusal for a request whose Apple target has no session to read.

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
import type { DaemonResponse } from '@agent-device/kernel/contracts';
21
import { appStateUse } from '@agent-device/contracts/platform-runtime-operations';
32
import {
43
isIosFamily,
54
isMacOs,
65
publicPlatformString,
76
type DeviceInfo,
87
} from '@agent-device/kernel/device';
8+
import { namesSessionExplicitly } from '@agent-device/kernel/contracts';
9+
import type { DaemonResponse } from '@agent-device/kernel/contracts';
910
import { resolveAndroidSerialAllowlist } from '@agent-device/kernel/device-isolation';
1011
import { admitRuntimeUse } from '../../core/runtime-admission.ts';
1112
import type { RuntimeCommandHandlerParams } from '../../core/session-runtime-admission.ts';
12-
import { appleSessionRequiredRefusal } from './session-appstate-apple-session.ts';
13+
import {
14+
appStateSessionNotFoundResponse,
15+
appleSessionRequiredRefusal,
16+
} from './session-appstate-apple-session.ts';
1317

1418
function sortedAndroidSerialAllowlist(value: string | undefined): string[] | undefined {
1519
const allowlist = resolveAndroidSerialAllowlist(value);
@@ -26,8 +30,16 @@ export async function queryDeviceRuntimeAppState(
2630
params: RuntimeCommandHandlerParams,
2731
device: DeviceInfo,
2832
): Promise<DaemonResponse> {
29-
if (isIosFamily(device)) return appleSessionRequiredRefusal('ios');
30-
if (isMacOs(device)) return appleSessionRequiredRefusal('macos');
33+
if (isIosFamily(device) || isMacOs(device)) {
34+
// A request that names a session this daemon does not hold and resolves onto an Apple device
35+
// is missing its session, not its device (#3674): the same typed refusal the session-required
36+
// selectors answer, whatever flag routed it here. A request that named no session keeps the
37+
// plain session-required refusal, which is the right advice for it.
38+
if (!params.sessionStore.get(params.sessionName) && namesSessionExplicitly(params.req)) {
39+
return appStateSessionNotFoundResponse(params.sessionName, device.platform);
40+
}
41+
return appleSessionRequiredRefusal(isMacOs(device) ? 'macos' : 'ios');
42+
}
3143
const flags = params.req.flags ?? {};
3244
const admitted = await admitRuntimeUse({
3345
command: 'appstate',

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

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { DaemonResponse } from '@agent-device/kernel/contracts';
2-
import { isApplePlatform, type PlatformSelector } from '@agent-device/kernel/device';
3-
import { errorResponse, namesSessionExplicitly } from '@agent-device/kernel/contracts';
4-
import { namedSessionNotFoundDetails } from '@agent-device/kernel/errors';
2+
import { namesSessionExplicitly } from '@agent-device/kernel/contracts';
3+
import { isApplePlatform } from '@agent-device/kernel/device';
54
import { hasExplicitDeviceSelector } from '../../core/device/device-selector-intent.ts';
65
import {
76
requireSessionOrExplicitSelector,
@@ -10,33 +9,13 @@ import {
109
} from '../../resources/session-device-resolution.ts';
1110
import type { RuntimeCommandHandlerParams } from '../../core/session-runtime-admission.ts';
1211
import {
12+
appStateSessionNotFoundResponse,
1313
appleSessionAppStateResponse,
1414
appleSessionRequiredRefusal,
1515
type AppStateSessionRecord,
1616
} from './session-appstate-apple-session.ts';
1717
import { queryDeviceRuntimeAppState } from './session-appstate-device-runtime.ts';
1818

19-
function appStateSessionNotFoundResponse(
20-
sessionName: string,
21-
platform: PlatformSelector | undefined,
22-
): DaemonResponse {
23-
// Every Apple selector — `apple`, `ios`, or `macos` — answers appstate only from an open
24-
// session and its runner, so the "omit --session" route they would otherwise be pointed at
25-
// just reaches the session-required refusal. Send them straight to opening the session.
26-
if (isApplePlatform(platform)) {
27-
return errorResponse(
28-
'SESSION_NOT_FOUND',
29-
`No active session "${sessionName}". Run open with --session ${sessionName} first.`,
30-
namedSessionNotFoundDetails(sessionName),
31-
);
32-
}
33-
return errorResponse(
34-
'SESSION_NOT_FOUND',
35-
`No active session "${sessionName}". Run open with --session ${sessionName} first, or omit --session to query by device selector.`,
36-
namedSessionNotFoundDetails(sessionName),
37-
);
38-
}
39-
4019
type AppStateSessionStoreEntry = ReturnType<RuntimeCommandHandlerParams['sessionStore']['get']>;
4120

4221
/** The answer of target admission: a refusal, or the authority that answers with its session. */

0 commit comments

Comments
 (0)