Skip to content

Commit dd708f8

Browse files
committed
fix(daemon): state the session-or-selector reason on the refused route response
The daemon refuses a device-bound command before it resolves a target, which is exactly the case where dispatched:no makes a retry safe. The refusal now carries the published reason through errorResponse, and the request-router test proves the reason survives finalizeDaemonResponse to the caller on the returned-error path rather than only at its construction site. Refs #3181
1 parent aefc271 commit dd708f8

4 files changed

Lines changed: 54 additions & 2 deletions

File tree

‎src/daemon/__tests__/request-router-typed-error.test.ts‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ import {
2929
makeSession,
3030
} from '../../__tests__/test-utils/session-factories.ts';
3131
import type { DeviceInfo } from '@agent-device/kernel/device';
32-
import { AppError, retriableForErrorCode } from '@agent-device/kernel/errors';
32+
import {
33+
AppError,
34+
defaultHintForCode,
35+
PUBLISHED_ERROR_REASONS,
36+
retriableForErrorCode,
37+
} from '@agent-device/kernel/errors';
3338
import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts';
3439

3540
const mockLifecycleEffect = vi.mocked(dispatchApplicationLifecycleEffect);
@@ -239,3 +244,24 @@ test('#1391: an ordinary close-time script-save failure surfaces details.reason/
239244
fs.rmSync(targetPath, { force: true });
240245
}
241246
});
247+
248+
test('#3181: the session-or-selector refusal reaches the caller with its reason and dispatched no through the router', async () => {
249+
const { handler } = makeHandler();
250+
251+
// No session exists for `typed-error` and no device selector is given, so the
252+
// handler returns this refusal — the returned-error path finalizeDaemonResponse rebuilds.
253+
const response = await handler(request('capabilities'));
254+
255+
expect(response.ok).toBe(false);
256+
if (response.ok) return;
257+
expect(response.error.code).toBe('INVALID_ARGS');
258+
expect(response.error.message).toBe(
259+
'capabilities requires an active session or an explicit device selector (e.g. --platform ios).',
260+
);
261+
expect(response.error.details?.reason).toBe(
262+
PUBLISHED_ERROR_REASONS.sessionOrDeviceSelectorRequired,
263+
);
264+
expect(response.error.details?.dispatched).toBe('no');
265+
// The hint stays the INVALID_ARGS default: the reason, not the prose, is the new information.
266+
expect(response.error.hint).toBe(defaultHintForCode('INVALID_ARGS'));
267+
});

‎src/daemon/__tests__/session-device-resolution.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { test, expect, vi, beforeEach } from 'vitest';
22
import type { SessionState } from '../session-state.ts';
33

44
import {
5+
requireSessionOrExplicitSelector,
56
resolveCommandDevice,
67
refreshSessionDeviceIfNeeded,
78
selectorTargetsSessionDevice,
@@ -10,6 +11,7 @@ import { appleSessionObservation } from '../../platform-runtime-apple-resources.
1011
import { resolveTargetDevice } from '@agent-device/device-selection/dispatch-resolve';
1112
import { isActiveProviderDevice } from '../provider-device-admission.ts';
1213
import { ensureDeviceReady } from '../device/device-ready.ts';
14+
import { PUBLISHED_ERROR_REASONS } from '@agent-device/kernel/errors';
1315

1416
vi.mock('../../platform-runtime-apple-resources.ts', async (importOriginal) => ({
1517
...(await importOriginal<typeof import('../../platform-runtime-apple-resources.ts')>()),
@@ -155,3 +157,23 @@ test('selectorTargetsSessionDevice uses session selector conflicts for simulator
155157
false,
156158
);
157159
});
160+
161+
test('requireSessionOrExplicitSelector refuses with the published reason and dispatched no', () => {
162+
const refusal = requireSessionOrExplicitSelector('clipboard', undefined, {});
163+
164+
expect(refusal).not.toBeNull();
165+
expect(refusal?.ok).toBe(false);
166+
if (!refusal || refusal.ok) return;
167+
expect(refusal.error.code).toBe('INVALID_ARGS');
168+
expect(refusal.error.details?.reason).toBe(PUBLISHED_ERROR_REASONS.sessionOrDeviceSelectorRequired);
169+
expect(refusal.error.details?.dispatched).toBe('no');
170+
});
171+
172+
for (const [label, session, flags] of [
173+
['an active session', iosSimulatorSession, {}],
174+
['an explicit platform selector', undefined, { platform: 'ios' }],
175+
] as const) {
176+
test(`requireSessionOrExplicitSelector lets through ${label}`, () => {
177+
expect(requireSessionOrExplicitSelector('clipboard', session, flags)).toBeNull();
178+
});
179+
}

‎src/daemon/session-device-resolution.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isIosFamily, type DeviceInfo } from '@agent-device/kernel/device';
2-
import { AppError } from '@agent-device/kernel/errors';
2+
import { AppError, sessionOrDeviceSelectorRequiredDetails } from '@agent-device/kernel/errors';
33
import { isActiveProviderDevice } from './provider-device-admission.ts';
44
import { appleSessionObservation } from '../platform-runtime-apple-resources.ts';
55
import { resolveTargetDevice } from '@agent-device/device-selection/dispatch-resolve';
@@ -23,6 +23,7 @@ export function requireSessionOrExplicitSelector(
2323
return errorResponse(
2424
'INVALID_ARGS',
2525
`${command} requires an active session or an explicit device selector (e.g. --platform ios).`,
26+
sessionOrDeviceSelectorRequiredDetails(),
2627
);
2728
}
2829

‎src/daemon/session-lifecycle/__tests__/application.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { beforeEach, expect, test, vi } from 'vitest';
22
import type { DaemonRequest, DaemonResponse } from '../../daemon-request.ts';
33
import { makeSessionStore } from '../../../__tests__/test-utils/store-factory.ts';
4+
import { sessionOrDeviceSelectorRequiredDetails } from '@agent-device/kernel/errors';
45

56
vi.mock('@agent-device/device-selection/device-inventory-context', async (importOriginal) => {
67
const actual =
@@ -71,6 +72,7 @@ test('inventory commands retain their route responses and typed failures', async
7172
code: 'INVALID_ARGS',
7273
message:
7374
'capabilities requires an active session or an explicit device selector (e.g. --platform ios).',
75+
details: sessionOrDeviceSelectorRequiredDetails(),
7476
},
7577
},
7678
apps: {
@@ -79,6 +81,7 @@ test('inventory commands retain their route responses and typed failures', async
7981
code: 'INVALID_ARGS',
8082
message:
8183
'apps requires an active session or an explicit device selector (e.g. --platform ios).',
84+
details: sessionOrDeviceSelectorRequiredDetails(),
8285
},
8386
},
8487
};

0 commit comments

Comments
 (0)