From 0bac0145b358fb20fb1c72adcaf6fdf5e2d9197c Mon Sep 17 00:00:00 2001 From: parbez Date: Mon, 23 Nov 2026 12:20:18 +0530 Subject: [PATCH 1/2] fix(settings): accept a permission mode only for photos Every mobile permission target shared one form with full|limited modes, so an app id placed after `notifications` was refused as a bad mode. Only photos honours a mode (iOS photos-add); Android and the other iOS targets refuse one at runtime. Give photos its own form with the modes, so the other mobile targets refuse a fourth positional with "takes no mode", and a valid photos mode followed by another positional is refused as "takes one mode". When an app-scoped target gets a value that looks like a bundle id or package, the refusal says to pass it with --app. --- .../src/capture/settings.test.ts | 6 +-- packages/contracts/src/settings-catalog.ts | 7 +++- packages/contracts/src/settings.test.ts | 38 ++++++++++++++++- packages/contracts/src/settings.ts | 41 +++++++++++++------ .../schema/cli-help-command-usage.test.ts | 3 +- website/docs/docs/commands.md | 2 +- 6 files changed, 76 insertions(+), 21 deletions(-) diff --git a/packages/command-surface/src/capture/settings.test.ts b/packages/command-surface/src/capture/settings.test.ts index 59217f475a..7b4e5c540b 100644 --- a/packages/command-surface/src/capture/settings.test.ts +++ b/packages/command-surface/src/capture/settings.test.ts @@ -13,16 +13,16 @@ function expectInvalidArgs(run: () => unknown, message: string): void { describe('settings CLI', () => { test('reads and writes settings input', () => { - const input = settingsCliReader(['permission', 'grant', 'camera', 'limited'], flags()); + const input = settingsCliReader(['permission', 'grant', 'photos', 'limited'], flags()); expect(input).toMatchObject({ setting: 'permission', state: 'grant', - permission: 'camera', + permission: 'photos', mode: 'limited', }); expect(settingsDaemonWriter(input)).toMatchObject({ command: 'settings', - positionals: ['permission', 'grant', 'camera', 'limited'], + positionals: ['permission', 'grant', 'photos', 'limited'], }); }); diff --git a/packages/contracts/src/settings-catalog.ts b/packages/contracts/src/settings-catalog.ts index f83332a4a4..ad96f20d96 100644 --- a/packages/contracts/src/settings-catalog.ts +++ b/packages/contracts/src/settings-catalog.ts @@ -166,7 +166,12 @@ export const SETTINGS = { { kind: 'permission', actions: PERMISSION_ACTIONS, - targets: MOBILE_PERMISSION_TARGETS, + targets: MOBILE_PERMISSION_TARGETS.filter((target) => target !== 'photos'), + }, + { + kind: 'permission', + actions: PERMISSION_ACTIONS, + targets: ['photos'], modes: PERMISSION_MODES, }, { kind: 'permission', actions: ['grant', 'reset'], targets: MACOS_PERMISSION_TARGETS }, diff --git a/packages/contracts/src/settings.test.ts b/packages/contracts/src/settings.test.ts index fb805731c4..3804adaf47 100644 --- a/packages/contracts/src/settings.test.ts +++ b/packages/contracts/src/settings.test.ts @@ -48,7 +48,8 @@ describe('settings usage', () => { 'settings fingerprint ', 'settings clear-app-state [app-id]', 'settings reset-keychain clear', - `settings permission <${MOBILE_PERMISSION_TARGETS.join('|')}> [full|limited]`, + 'settings permission ', + 'settings permission photos [full|limited]', 'settings permission ', ]); }); @@ -119,7 +120,7 @@ describe('parseSettingsMutation', () => { }); expectInvalidArgs( () => parseSettingsMutation(['permission', 'grant', 'bluetooth']), - 'settings permission requires a target:', + 'settings permission requires a target: all|camera|microphone|photos|contacts|contacts-limited|notifications|calendar|location|location-always|media-library|motion|reminders|siri|accessibility|screen-recording|input-monitoring', ); expectInvalidArgs( () => parseSettingsMutation(['permission', 'grant', 'photos', 'some']), @@ -138,6 +139,39 @@ describe('parseSettingsMutation', () => { ); }); + test('only photos takes a mode among the mobile targets', () => { + expectInvalidArgs( + () => parseSettingsMutation(['permission', 'grant', 'camera', 'limited']), + 'settings permission camera takes no mode.', + ); + }); + + test('an app id in the mode slot is refused with the --app spelling', () => { + expectInvalidArgs( + () => parseSettingsMutation(['permission', 'deny', 'notifications', 'com.example.app']), + 'settings permission notifications takes no mode. Pass the app as --app com.example.app.', + ); + expectInvalidArgs( + () => parseSettingsMutation(['permission', 'grant', 'photos', 'com.example.app']), + 'settings permission mode must be full|limited. Pass the app as --app com.example.app.', + ); + expectInvalidArgs( + () => parseSettingsMutation(['permission', 'grant', 'photos', 'limited', 'com.example.app']), + 'settings permission photos takes one mode. Pass the app as --app com.example.app.', + ); + }); + + test('a macOS target takes no --app, so a dotted value gets no --app spelling', () => { + expect(() => + parseSettingsMutation(['permission', 'grant', 'accessibility', 'com.example.app']), + ).toThrow( + expect.objectContaining({ + code: 'INVALID_ARGS', + message: 'settings permission accessibility takes no mode.', + }), + ); + }); + test('clear-app-state reads its app with or without the clear word', () => { expect(parseSettingsMutation(['clear-app-state', 'com.Example.App'])).toEqual({ setting: 'clear-app-state', diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.ts index 8534baba0b..e49d670e98 100644 --- a/packages/contracts/src/settings.ts +++ b/packages/contracts/src/settings.ts @@ -6,6 +6,7 @@ import { } from '@agent-device/kernel/errors'; import { APPEARANCE_ACTIONS, + MACOS_PERMISSION_TARGETS, MOBILE_PERMISSION_TARGETS, PERMISSION_ACTIONS, SETTINGS, @@ -211,7 +212,8 @@ function formUsage(form: SettingForm, readable: boolean): string { return 'set '; case 'permission': { const modes = form.modes ? ` [${form.modes.join('|')}]` : ''; - return `<${form.actions.join('|')}> <${form.targets.join('|')}>${modes}`; + const targets = form.targets.length === 1 ? form.targets[0] : `<${form.targets.join('|')}>`; + return `<${form.actions.join('|')}> ${targets}${modes}`; } case 'app-id': return '[app-id]'; @@ -312,13 +314,15 @@ function parsePermissionPositionals( const forms = definition.forms.filter( (form): form is Extract => form.kind === 'permission', ); - const targets = forms.flatMap((form) => form.targets); - const permission = findVocabularyName(targets, positionals[2]); + const permission = findVocabularyName( + forms.flatMap((form) => form.targets), + positionals[2], + ); const form = forms.find((candidate) => candidate.targets.includes(permission ?? '')); if (permission === undefined || form === undefined) { throw new AppError( 'INVALID_ARGS', - `settings permission requires a target: ${targets.join('|')}`, + `settings permission requires a target: ${[...MOBILE_PERMISSION_TARGETS, ...MACOS_PERMISSION_TARGETS].join('|')}`, ); } if (findVocabularyName(form.actions, action) === undefined) { @@ -329,15 +333,26 @@ function parsePermissionPositionals( } if (positionals.length === 3) return { permission }; const mode = findVocabularyName(form.modes ?? [], positionals[3]); - if (mode === undefined || positionals.length > 4) { - throw new AppError( - 'INVALID_ARGS', - form.modes === undefined - ? `settings permission ${permission} takes no mode.` - : `settings permission mode must be ${form.modes.join('|')}.`, - ); - } - return { permission, mode }; + if (mode !== undefined && positionals.length === 4) return { permission, mode }; + const refusal = permissionModeRefusal(permission, form.modes, mode); + // A dotted value reads as a bundle id or package, which only an app-scoped target takes. + const app = (MOBILE_PERMISSION_TARGETS as readonly string[]).includes(permission) + ? positionals.slice(3).find((value) => value.includes('.')) + : undefined; + throw new AppError( + 'INVALID_ARGS', + app === undefined ? refusal : `${refusal} Pass the app as --app ${app}.`, + ); +} + +function permissionModeRefusal( + permission: string, + modes: readonly string[] | undefined, + mode: string | undefined, +): string { + if (modes === undefined) return `settings permission ${permission} takes no mode.`; + if (mode === undefined) return `settings permission mode must be ${modes.join('|')}.`; + return `settings permission ${permission} takes one mode.`; } function invalidSettingsArgs(): AppError { diff --git a/src/commands/schema/cli-help-command-usage.test.ts b/src/commands/schema/cli-help-command-usage.test.ts index 8f9304ccd2..570013aee9 100644 --- a/src/commands/schema/cli-help-command-usage.test.ts +++ b/src/commands/schema/cli-help-command-usage.test.ts @@ -439,8 +439,9 @@ test('settings usage documents canonical faceid states', async () => { assert.match(help, /match\|nonmatch\|enroll\|unenroll/); assert.match( help, - /camera\|microphone\|photos\|contacts\|contacts-limited\|notifications\|calendar\|location\|location-always\|media-library\|motion\|reminders\|siri/, + /camera\|microphone\|contacts\|contacts-limited\|notifications\|calendar\|location\|location-always\|media-library\|motion\|reminders\|siri>/, ); + assert.match(help, /permission photos \[full\|limited\]/); assert.doesNotMatch(help, /validate\|unvalidate/); }); diff --git a/website/docs/docs/commands.md b/website/docs/docs/commands.md index 18c5f1cc1c..58688e19ee 100644 --- a/website/docs/docs/commands.md +++ b/website/docs/docs/commands.md @@ -1692,7 +1692,7 @@ agent-device trigger-app-event screenshot_taken '{"source":"qa"}' Change OS settings and app permissions. ```bash -agent-device settings | settings location set | settings appearance | settings text-size [extra-small|small|medium|large|extra-large|extra-extra-large|extra-extra-extra-large|accessibility-medium|accessibility-large|accessibility-extra-large|accessibility-extra-extra-large|accessibility-extra-extra-extra-large] | settings | settings fingerprint | settings clear-app-state [app-id] | settings reset-keychain clear | settings permission [full|limited] | settings permission [--app ] +agent-device settings | settings location set | settings appearance | settings text-size [extra-small|small|medium|large|extra-large|extra-extra-large|extra-extra-extra-large|accessibility-medium|accessibility-large|accessibility-extra-large|accessibility-extra-extra-large|accessibility-extra-extra-extra-large] | settings | settings fingerprint | settings clear-app-state [app-id] | settings reset-keychain clear | settings permission | settings permission photos [full|limited] | settings permission [--app ] ``` | Option | What it does | From 1f9f64e1da7255143c6f9ac6ff20705a8d8870bb Mon Sep 17 00:00:00 2001 From: parbez Date: Mon, 23 Nov 2026 16:13:18 +0530 Subject: [PATCH 2/2] fix(settings): refuse a misplaced permission app with a typed reason, and list targets from the forms --- packages/contracts/src/settings.test.ts | 37 +++++++++++++++---------- packages/contracts/src/settings.ts | 17 ++++++++---- website/docs/docs/error-reasons.md | 1 + 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/packages/contracts/src/settings.test.ts b/packages/contracts/src/settings.test.ts index 3804adaf47..94c6c5a81d 100644 --- a/packages/contracts/src/settings.test.ts +++ b/packages/contracts/src/settings.test.ts @@ -120,7 +120,7 @@ describe('parseSettingsMutation', () => { }); expectInvalidArgs( () => parseSettingsMutation(['permission', 'grant', 'bluetooth']), - 'settings permission requires a target: all|camera|microphone|photos|contacts|contacts-limited|notifications|calendar|location|location-always|media-library|motion|reminders|siri|accessibility|screen-recording|input-monitoring', + 'settings permission requires a target: all|camera|microphone|contacts|contacts-limited|notifications|calendar|location|location-always|media-library|motion|reminders|siri|photos|accessibility|screen-recording|input-monitoring', ); expectInvalidArgs( () => parseSettingsMutation(['permission', 'grant', 'photos', 'some']), @@ -146,19 +146,27 @@ describe('parseSettingsMutation', () => { ); }); - test('an app id in the mode slot is refused with the --app spelling', () => { - expectInvalidArgs( - () => parseSettingsMutation(['permission', 'deny', 'notifications', 'com.example.app']), - 'settings permission notifications takes no mode. Pass the app as --app com.example.app.', - ); - expectInvalidArgs( - () => parseSettingsMutation(['permission', 'grant', 'photos', 'com.example.app']), - 'settings permission mode must be full|limited. Pass the app as --app com.example.app.', - ); - expectInvalidArgs( - () => parseSettingsMutation(['permission', 'grant', 'photos', 'limited', 'com.example.app']), - 'settings permission photos takes one mode. Pass the app as --app com.example.app.', - ); + test('an app id in the mode slot is refused with a typed reason that names the app', () => { + const appPositional = (permission: string, app: string) => + expect.objectContaining({ + code: 'INVALID_ARGS', + details: expect.objectContaining({ + reason: 'setting_permission_app_positional', + dispatched: 'no', + permission, + app, + hint: `Pass the app with --app ${app}.`, + }), + }); + for (const positionals of [ + ['permission', 'deny', 'notifications', 'com.example.app'], + ['permission', 'grant', 'photos', 'com.example.app'], + ['permission', 'grant', 'photos', 'limited', 'com.example.app'], + ]) { + expect(() => parseSettingsMutation(positionals)).toThrow( + appPositional(positionals[2]!, 'com.example.app'), + ); + } }); test('a macOS target takes no --app, so a dotted value gets no --app spelling', () => { @@ -168,6 +176,7 @@ describe('parseSettingsMutation', () => { expect.objectContaining({ code: 'INVALID_ARGS', message: 'settings permission accessibility takes no mode.', + details: undefined, }), ); }); diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.ts index e49d670e98..b9bc4b8a1d 100644 --- a/packages/contracts/src/settings.ts +++ b/packages/contracts/src/settings.ts @@ -6,7 +6,6 @@ import { } from '@agent-device/kernel/errors'; import { APPEARANCE_ACTIONS, - MACOS_PERMISSION_TARGETS, MOBILE_PERMISSION_TARGETS, PERMISSION_ACTIONS, SETTINGS, @@ -137,6 +136,8 @@ const SETTINGS_REFUSAL_REASONS = { */ // The asked-for setting applies to the target itself, so the named app consumes nothing. appNotConsumed: { reason: 'setting_app_not_consumed' }, + // A permission's trailing positional reads as a bundle id or package, which belongs in --app. + permissionAppPositional: { reason: 'setting_permission_app_positional' }, } as const satisfies ReasonTable<'settings'>; /** @@ -322,7 +323,7 @@ function parsePermissionPositionals( if (permission === undefined || form === undefined) { throw new AppError( 'INVALID_ARGS', - `settings permission requires a target: ${[...MOBILE_PERMISSION_TARGETS, ...MACOS_PERMISSION_TARGETS].join('|')}`, + `settings permission requires a target: ${forms.flatMap((candidate) => candidate.targets).join('|')}`, ); } if (findVocabularyName(form.actions, action) === undefined) { @@ -339,10 +340,14 @@ function parsePermissionPositionals( const app = (MOBILE_PERMISSION_TARGETS as readonly string[]).includes(permission) ? positionals.slice(3).find((value) => value.includes('.')) : undefined; - throw new AppError( - 'INVALID_ARGS', - app === undefined ? refusal : `${refusal} Pass the app as --app ${app}.`, - ); + if (app === undefined) throw new AppError('INVALID_ARGS', refusal); + throw new AppError('INVALID_ARGS', refusal, { + reason: SETTINGS_REFUSAL_REASONS.permissionAppPositional.reason, + dispatched: 'no', + permission, + app, + hint: `Pass the app with --app ${app}.`, + }); } function permissionModeRefusal( diff --git a/website/docs/docs/error-reasons.md b/website/docs/docs/error-reasons.md index fa20e94c1f..52d217de7e 100644 --- a/website/docs/docs/error-reasons.md +++ b/website/docs/docs/error-reasons.md @@ -344,6 +344,7 @@ Treat a reason that this page does not list as unknown, not as an error in the r | Reason | Meaning | | --- | --- | | `setting_app_not_consumed` | The asked-for setting applies to the target itself, so the named app consumes nothing. | +| `setting_permission_app_positional` | A permission's trailing positional reads as a bundle id or package, which belongs in --app. | | `android_bluetooth_adapter_missing` | The device declares no Bluetooth adapter, so there is nothing to turn on or off. | | `android_bluetooth_command_unavailable` | The build predates `svc bluetooth`, so the adapter cannot be switched from the shell. | | `bluetooth-not-applied` | The device kept Bluetooth in its prior state after `svc bluetooth` accepted the request. |