Skip to content

Commit a3e35d2

Browse files
committed
fix(maestro): single-source Maestro hints, unexport test-only parser, skip role-managed ids
Maestro iOS admission and hint derive from explicit MAESTRO_* lists in contracts; adapter validates values with MAESTRO_PERMISSION_VALUES. parseAndroidPermissionTarget leaves the mechanics surface so the mapping test pins backend-servable pairs against the contracts table. Android all skips role-managed ids such as WRITE_SETTINGS instead of aborting.
1 parent c3e47b3 commit a3e35d2

7 files changed

Lines changed: 235 additions & 62 deletions

File tree

‎packages/contracts/src/settings.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
IOS_PERMISSION_TARGETS,
66
isMacOsSettingSupported,
77
MACOS_PERMISSION_TARGETS,
8+
MAESTRO_ANDROID_PERMISSION_TARGETS,
9+
MAESTRO_IOS_PERMISSION_TARGETS,
810
MOBILE_PERMISSION_TARGETS,
911
parsePermissionAction,
1012
parsePermissionTarget,
@@ -108,6 +110,29 @@ describe('the declared permission vocabulary', () => {
108110
expect(MOBILE_TARGETS).toContain(target);
109111
}
110112
});
113+
114+
test('Maestro admission sets are explicit allowlists, not native denylists', () => {
115+
expect([...MAESTRO_ANDROID_PERMISSION_TARGETS]).toEqual([...ANDROID_PERMISSION_TARGETS]);
116+
expect([...MAESTRO_IOS_PERMISSION_TARGETS]).toEqual([
117+
'all',
118+
'camera',
119+
'microphone',
120+
'photos',
121+
'contacts',
122+
'notifications',
123+
'calendar',
124+
'location',
125+
'media-library',
126+
'motion',
127+
'reminders',
128+
'siri',
129+
]);
130+
for (const target of MAESTRO_IOS_PERMISSION_TARGETS) {
131+
expect(MOBILE_TARGETS).toContain(target);
132+
}
133+
expect(MAESTRO_IOS_PERMISSION_TARGETS).not.toContain('contacts-limited');
134+
expect(MAESTRO_IOS_PERMISSION_TARGETS).not.toContain('location-always');
135+
});
111136
});
112137

113138
describe('settings usage and error strings', () => {

‎packages/contracts/src/settings.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,33 @@ export const ANDROID_PERMISSION_TARGETS = [
5959
'media-library',
6060
] as const;
6161

62+
/**
63+
* The Maestro `setPermissions` names each platform accepts. An explicit
64+
* allowlist on purpose: the native `settings permission` vocabulary may grow
65+
* a target Maestro never names (today `contacts-limited` and
66+
* `location-always`, whose Maestro spellings are `photos: limited` and
67+
* `location: always`), and that target must not become a Maestro name
68+
* without a deliberate decision here. The daemon adapter derives both its
69+
* admission set and its unsupported-name hint from these lists, so the two
70+
* cannot disagree.
71+
*/
72+
export const MAESTRO_ANDROID_PERMISSION_TARGETS = ANDROID_PERMISSION_TARGETS;
73+
74+
export const MAESTRO_IOS_PERMISSION_TARGETS = [
75+
'all',
76+
'camera',
77+
'microphone',
78+
'photos',
79+
'contacts',
80+
'notifications',
81+
'calendar',
82+
'location',
83+
'media-library',
84+
'motion',
85+
'reminders',
86+
'siri',
87+
] as const;
88+
6289
export type PermissionAction = (typeof PERMISSION_ACTIONS)[number];
6390
export type PermissionMode = (typeof PERMISSION_MODES)[number];
6491
export type MobilePermissionTarget = (typeof MOBILE_PERMISSION_TARGETS)[number];

‎packages/platform-android/src/__tests__/settings-permission.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,49 @@ test('setAndroidSetting permission grant all applies the declared changeable ids
555555
);
556556
});
557557

558+
// A role-managed id (WRITE_SETTINGS on API 36: "managed by role") is
559+
// declared-but-not-changeable like INTERNET, so `all` skips it with a reason
560+
// instead of aborting — found live on com.google.android.contacts.
561+
test('setAndroidSetting permission all skips a role-managed id', async () => {
562+
const requested = [
563+
'Packages:',
564+
' Package [com.example.app] (abc):',
565+
' requested permissions:',
566+
' android.permission.WRITE_SETTINGS',
567+
' android.permission.RECORD_AUDIO',
568+
' User 0: ceDataInode=0 installed=true',
569+
' runtime permissions:',
570+
' android.permission.RECORD_AUDIO: granted=false',
571+
'Queries:',
572+
].join('\n');
573+
await withFakeAdb(
574+
fakeAdb((flat) => {
575+
if (flat === CURRENT_USER) return '0';
576+
if (flat === DUMPSYS) return requested;
577+
if (flat === 'shell pm grant --user 0 com.example.app android.permission.WRITE_SETTINGS') {
578+
return {
579+
stderr:
580+
"Exception occurred while executing 'grant':\njava.lang.SecurityException: Permission android.permission.WRITE_SETTINGS is managed by role",
581+
exitCode: 1,
582+
};
583+
}
584+
return undefined;
585+
}),
586+
async ({ device }) => {
587+
const result = await setAndroidSetting(device, 'permission', 'grant', 'com.example.app', {
588+
permissionTarget: 'all',
589+
});
590+
assert.deepEqual(result, {
591+
permission: 'all',
592+
applied: ['android.permission.RECORD_AUDIO'],
593+
warnings: [
594+
"Skipped android.permission.WRITE_SETTINGS for com.example.app: Exception occurred while executing 'grant': java.lang.SecurityException: Permission android.permission.WRITE_SETTINGS is managed by role",
595+
],
596+
});
597+
},
598+
);
599+
});
600+
558601
// Revoke under `all` warns per held permission, like the single path.
559602
test('setAndroidSetting permission revoke all warns for the held runtime id', async () => {
560603
await withFakeAdb(

‎packages/platform-android/src/mechanics.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,7 @@ export {
246246
annotateAndroidNativePerfError,
247247
buildAndroidNativeToolUnavailableHint,
248248
} from './perf-native-errors.ts';
249-
export {
250-
androidRevokedPermissionWarning,
251-
parseAndroidPermissionTarget,
252-
setAndroidPermission,
253-
} from './settings-permission.ts';
249+
export { androidRevokedPermissionWarning, setAndroidPermission } from './settings-permission.ts';
254250
export { setAndroidSetting } from './settings.ts';
255251
export {
256252
androidCaptureFailureReasonDetail,

‎packages/platform-android/src/settings-permission.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,18 +319,20 @@ async function tryPmUnit(
319319

320320
/**
321321
* Only established non-changeable signals are skipped under `all`: an install
322-
* permission `pm` cannot touch, an id the package never requested, or a name
323-
* the runtime does not know as a runtime permission. Anything else (offline
324-
* device, dropped transport, denied op) is operational and must abort the
325-
* fan-out rather than let launchApp continue with half-applied permissions.
322+
* permission `pm` cannot touch, an id the package never requested, a name
323+
* the runtime does not know as a runtime permission, or an id managed by a
324+
* role (`WRITE_SETTINGS` on API 36 reports "managed by role"). Anything else
325+
* (offline device, dropped transport, denied op) is operational and must abort
326+
* the fan-out rather than let launchApp continue with half-applied permissions.
326327
*/
327328
function isSkippablePmStderr(stderr: string): boolean {
328329
const text = stderr.toLowerCase();
329330
return (
330331
text.includes('not a changeable permission') ||
331332
text.includes('has not requested permission') ||
332333
text.includes('is not a runtime permission') ||
333-
text.includes('unknown permission')
334+
text.includes('unknown permission') ||
335+
text.includes('managed by role')
334336
);
335337
}
336338

@@ -423,7 +425,7 @@ async function revokeAndroidPermission(
423425
throw new Error(`Unhandled Android permission target: ${JSON.stringify(exhaustive)}`);
424426
}
425427

426-
export function parseAndroidPermissionTarget(
428+
function parseAndroidPermissionTarget(
427429
permissionTarget: string | undefined,
428430
permissionMode: string | undefined,
429431
):

‎src/daemon/adapters/maestro/__tests__/set-permissions-mapping.test.ts‎

Lines changed: 102 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,88 @@
11
import assert from 'node:assert/strict';
22
import { describe, test } from 'vitest';
33
import { MAESTRO_PERMISSION_VALUES } from '@agent-device/maestro';
4-
import { parseAndroidPermissionTarget } from '@agent-device/platform-android/mechanics';
5-
import { mapMaestroSetPermissions } from '../set-permissions-mapping.ts';
4+
import {
5+
MAESTRO_ANDROID_PERMISSION_TARGETS,
6+
MAESTRO_IOS_PERMISSION_TARGETS,
7+
MOBILE_PERMISSION_TARGETS,
8+
} from '@agent-device/contracts/settings';
9+
import { AppError } from '@agent-device/kernel/errors';
10+
import {
11+
MAESTRO_PERMISSION_ALIASES,
12+
mapMaestroSetPermissions,
13+
type MaestroPermissionMutation,
14+
} from '../set-permissions-mapping.ts';
15+
16+
function assertIosHintListsAdmissionSet(name: string): void {
17+
try {
18+
mapMaestroSetPermissions({ [name]: 'allow' }, 'ios');
19+
} catch (error) {
20+
assert.ok(error instanceof AppError && error.code === 'UNSUPPORTED_OPERATION');
21+
const hint = String((error.details as { hint?: unknown } | undefined)?.hint ?? '');
22+
for (const supported of MAESTRO_IOS_PERMISSION_TARGETS) {
23+
assert.ok(hint.includes(supported), `iOS hint omits ${supported} for ${name}: ${hint}`);
24+
}
25+
assert.ok(!hint.includes('contacts-limited'), `iOS hint lists native-only name: ${hint}`);
26+
assert.ok(!hint.includes('location-always'), `iOS hint lists native-only name: ${hint}`);
27+
return;
28+
}
29+
assert.fail(`expected iOS ${name} to be unsupported`);
30+
}
31+
32+
function mapAndroidOrNull(name: string, value: string): MaestroPermissionMutation[] | null {
33+
try {
34+
return mapMaestroSetPermissions({ [name]: value }, 'android');
35+
} catch (error) {
36+
assert.ok(
37+
error instanceof AppError &&
38+
(error.code === 'UNSUPPORTED_OPERATION' || error.code === 'INVALID_ARGS'),
39+
`unexpected ${String(error)} for Android ${name}:${value}`,
40+
);
41+
return null;
42+
}
43+
}
44+
45+
function assertAndroidMutationServable(
46+
mutation: MaestroPermissionMutation,
47+
servable: ReadonlySet<string>,
48+
): void {
49+
assert.ok(
50+
servable.has(mutation.permission),
51+
`Android mutation ${mutation.permission} not in ${[...servable].join(',')}`,
52+
);
53+
assert.equal(mutation.mode, undefined);
54+
}
55+
56+
function checkAndroidName(
57+
name: string,
58+
servable: ReadonlySet<string>,
59+
checkedPlain: Set<string>,
60+
): number {
61+
let checked = 0;
62+
for (const value of MAESTRO_PERMISSION_VALUES) {
63+
const mutations = mapAndroidOrNull(name, value);
64+
if (!mutations) continue;
65+
for (const mutation of mutations) {
66+
assertAndroidMutationServable(mutation, servable);
67+
checked += 1;
68+
if (value === 'allow' || value === 'deny' || value === 'unset') {
69+
checkedPlain.add(`${mutation.permission}:${value}`);
70+
}
71+
}
72+
}
73+
return checked;
74+
}
75+
76+
function assertAndroidPlainCoverage(checkedPlain: ReadonlySet<string>): void {
77+
for (const target of MAESTRO_ANDROID_PERMISSION_TARGETS) {
78+
for (const value of ['allow', 'deny', 'unset'] as const) {
79+
assert.ok(
80+
checkedPlain.has(`${target}:${value}`),
81+
`expected Android ${target}:${value} to be checked`,
82+
);
83+
}
84+
}
85+
}
686

787
describe('mapMaestroSetPermissions', () => {
888
test('maps single permissions to grant/deny/reset', () => {
@@ -122,35 +202,26 @@ describe('mapMaestroSetPermissions', () => {
122202
}
123203
});
124204

125-
test('every mutation emitted for Android passes the Android parser', () => {
126-
// Pins the adapter to mutations the backend serves: a value the adapter
127-
// accepts must never fail halfway through `settings permission`.
128-
const names = [
129-
'all',
130-
'camera',
131-
'microphone',
132-
'photos',
133-
'contacts',
134-
'notifications',
135-
'calendar',
136-
'location',
137-
'media-library',
138-
'contacts-limited',
139-
'location-always',
140-
'health',
141-
];
142-
for (const name of names) {
143-
for (const value of MAESTRO_PERMISSION_VALUES) {
144-
let mutations;
145-
try {
146-
mutations = mapMaestroSetPermissions({ [name]: value }, 'android');
147-
} catch {
148-
continue;
149-
}
150-
for (const mutation of mutations) {
151-
parseAndroidPermissionTarget(mutation.permission, mutation.mode);
152-
}
153-
}
205+
test('iOS unsupported hint lists the Maestro admission set', () => {
206+
// The admission set and the hint share one Maestro iOS list in contracts:
207+
// a native-only spelling must fail without appearing as supported.
208+
for (const name of ['contacts-limited', 'location-always', 'speech']) {
209+
assertIosHintListsAdmissionSet(name);
154210
}
155211
});
212+
213+
test('every mutation emitted for Android is a backend-servable pair', () => {
214+
// Pins the adapter to mutations the backend serves: a value the adapter
215+
// accepts must never fail halfway through `settings permission`. The
216+
// backend table is typed from the same contracts list, so membership plus
217+
// a missing mode is the whole contract on Android (granular values are
218+
// refused above, never emitted).
219+
const names = [...MOBILE_PERMISSION_TARGETS, ...Object.keys(MAESTRO_PERMISSION_ALIASES)];
220+
const servable = new Set<string>(MAESTRO_ANDROID_PERMISSION_TARGETS);
221+
const checkedPlain = new Set<string>();
222+
let checked = 0;
223+
for (const name of names) checked += checkAndroidName(name, servable, checkedPlain);
224+
assert.ok(checked > 0, 'expected at least one Android mutation to be checked');
225+
assertAndroidPlainCoverage(checkedPlain);
226+
});
156227
});

0 commit comments

Comments
 (0)