Skip to content

Commit 0e536c7

Browse files
committed
refactor(macos): share one permissive bundle-id check; keep the display-name rule in open
isMacOsBundleId (kernel/device) accepts [A-Za-z0-9_-]+ segments, the shape macos-app-lease.ts already used, so parseMacOsAppLeaseKey behaves as before. helper.ts keeps its own identical pattern: importing the shared one would add modules to the macos-facade eager closure (eager-closure-budgets). Only resolveMacOsApp also requires a lowercase first segment, so a dotted display name like Booking.com resolves by name instead of being passed on as a bundle id. Agent: claude
1 parent 8b2d810 commit 0e536c7

7 files changed

Lines changed: 72 additions & 11 deletions

File tree

‎packages/daemon/src/resources/__tests__/macos-app-lease.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ test('the device key names one bundle, optionally one process', () => {
5959
bundleId: 'com.example.app',
6060
pid: 4242,
6161
});
62+
assert.deepEqual(parseMacOsAppLeaseKey('Foo.Bar@4242'), { bundleId: 'Foo.Bar', pid: 4242 });
63+
assert.deepEqual(parseMacOsAppLeaseKey('com.apple.TextEdit'), { bundleId: 'com.apple.TextEdit' });
6264
for (const key of ['', 'example', 'com.example.app@', 'com.example.app@0', 'com.example app']) {
6365
assert.throws(() => parseMacOsAppLeaseKey(key), { code: 'INVALID_ARGS' });
6466
}

‎packages/daemon/src/resources/macos-app-lease.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { DeviceLease } from '@agent-device/contracts/device';
77
import { readMacOsAppBackend } from '@agent-device/contracts/session';
88
import { runCmd } from '@agent-device/host-kit/command';
99
import { isProcessAlive } from '@agent-device/host-kit/process';
10-
import { isMacOs } from '@agent-device/kernel/device';
10+
import { isMacOs, isMacOsBundleId } from '@agent-device/kernel/device';
1111
import { AppError, type DaemonError } from '@agent-device/kernel/errors';
1212
import { isAppLeaseAllowed } from '../core/daemon-command-registry.ts';
1313
import type { DaemonRequest } from '../core/daemon-request.ts';
@@ -20,15 +20,14 @@ import type { SessionState } from '../core/session-state.ts';
2020
*/
2121
export type MacOsAppLeaseKey = Readonly<{ bundleId: string; pid?: number }>;
2222

23-
const BUNDLE_ID = /^[A-Za-z0-9_-]+(?:\.[A-Za-z0-9_-]+)+$/;
2423
const PID = /^[1-9][0-9]{0,9}$/;
2524

2625
export function parseMacOsAppLeaseKey(deviceKey: string | undefined): MacOsAppLeaseKey {
2726
const value = deviceKey?.trim() ?? '';
2827
const at = value.lastIndexOf('@');
2928
const bundleId = at < 0 ? value : value.slice(0, at);
3029
const pidText = at < 0 ? undefined : value.slice(at + 1);
31-
if (!BUNDLE_ID.test(bundleId) || (pidText !== undefined && !PID.test(pidText))) {
30+
if (!isMacOsBundleId(bundleId) || (pidText !== undefined && !PID.test(pidText))) {
3231
throw new AppError(
3332
'INVALID_ARGS',
3433
'A macos-app lease device key must be <bundleId> or <bundleId>@<pid>.',

‎packages/kernel/src/device.test.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import assert from 'node:assert/strict';
22
import { test } from 'vitest';
3-
import { devicesOverlap, hostAppDevice, hostDeviceIdOf, type DeviceInfo } from './device.ts';
3+
import {
4+
devicesOverlap,
5+
hostAppDevice,
6+
hostDeviceIdOf,
7+
isMacOsBundleId,
8+
type DeviceInfo,
9+
} from './device.ts';
410

511
const mac: DeviceInfo = {
612
platform: 'apple',
@@ -27,3 +33,15 @@ test('hostDeviceIdOf names the host of an app device and any other device itself
2733
assert.equal(hostDeviceIdOf(mac.id), mac.id);
2834
assert.equal(hostDeviceIdOf('emulator-5554'), 'emulator-5554');
2935
});
36+
37+
test.each([
38+
['com.apple.TextEdit', true],
39+
['com.x.My_App', true],
40+
['Foo.Bar', true],
41+
['zoom.us', true],
42+
['TextEdit', false],
43+
['com..x', false],
44+
['com.example app', false],
45+
])('isMacOsBundleId(%j) is %s', (value, expected) => {
46+
assert.equal(isMacOsBundleId(value), expected);
47+
});

‎packages/kernel/src/device.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ export type DeviceInfo = {
7474

7575
const HOST_APP_DEVICE_ID_INFIX = ':app:';
7676

77+
const MACOS_BUNDLE_ID_PATTERN = /^[A-Za-z0-9_-]+(?:\.[A-Za-z0-9_-]+)+$/;
78+
79+
/** A macOS bundle id in reverse-DNS form such as com.apple.TextEdit; any segment may be mixed-case. */
80+
export function isMacOsBundleId(value: string): boolean {
81+
return MACOS_BUNDLE_ID_PATTERN.test(value);
82+
}
83+
7784
/** Bundle ids match without regard to case, as LaunchServices matches them. */
7885
export function hostAppDeviceId(hostDeviceId: string, bundleId: string): string {
7986
return `${hostDeviceIdPrefix(hostDeviceId)}${bundleId.toLowerCase()}`;

‎packages/platform-apple/src/os/macos/apps.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,15 @@ test('openMacOsApp keeps the typed error for an app that is not installed', asyn
216216
});
217217
expect(host.openBundle).not.toHaveBeenCalled();
218218
});
219+
220+
test.each([
221+
['com.apple.TextEdit', 'com.apple.TextEdit'],
222+
['com.x.My_App', 'com.x.My_App'],
223+
['Booking.com', 'com.booking.BookingApp'],
224+
])('resolveMacOsApp(%j) is %j', async (typed, expected) => {
225+
const host = macOsHost([{ name: 'Booking.com', bundleId: 'com.booking.BookingApp' }]);
226+
227+
expect(await host.run(() => resolveMacOsApp(typed))).toBe(expected);
228+
// Only a lowercase-first dotted id is taken as a bundle id as typed; Booking.com is a name.
229+
expect(host.listApps).toHaveBeenCalledTimes(typed === 'Booking.com' ? 1 : 0);
230+
});

‎packages/platform-apple/src/os/macos/apps.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { AppsFilter } from '@agent-device/contracts/device';
22
import { isDeepLinkTarget } from '@agent-device/contracts/command';
33
import { parseAppearanceAction } from '@agent-device/contracts/settings';
4-
import type { DeviceInfo } from '@agent-device/kernel/device';
4+
import { isMacOsBundleId, type DeviceInfo } from '@agent-device/kernel/device';
55
import type { EnvMap } from '@agent-device/kernel/source-value';
66
import { AppError } from '@agent-device/kernel/errors';
77
import { setTimeout as sleep } from 'node:timers/promises';
@@ -17,7 +17,9 @@ const MACOS_ALIASES: Record<string, string> = {
1717
settings: 'com.apple.systempreferences',
1818
};
1919

20-
const MACOS_BUNDLE_ID_PATTERN = /^[a-z0-9-]+(?:\.[a-z0-9-]+)+$/i;
20+
// `open` tells a dotted display name such as "Booking.com" from a bundle id by its lowercase
21+
// TLD-style first segment; the shared isMacOsBundleId check stays permissive for everyone else.
22+
const MACOS_BUNDLE_ID_FIRST_SEGMENT = /^[a-z0-9-]+\./;
2123
const MACOS_APP_TERMINATION_POLL_MS = 100;
2224
const MACOS_APP_TERMINATION_MAX_ATTEMPTS = 20;
2325

@@ -29,16 +31,12 @@ const MACOS_APP_RESOLUTION_CACHE_SCOPE = {
2931
} satisfies AppResolutionCacheScope;
3032
const macOsAppResolutionCache = createAppResolutionCache<string>();
3133

32-
function isMacOsBundleId(value: string): boolean {
33-
return MACOS_BUNDLE_ID_PATTERN.test(value);
34-
}
35-
3634
export async function resolveMacOsApp(app: string): Promise<string> {
3735
const trimmed = app.trim();
3836

3937
const alias = MACOS_ALIASES[trimmed.toLowerCase()];
4038
if (alias) return alias;
41-
if (isMacOsBundleId(trimmed)) {
39+
if (isMacOsBundleId(trimmed) && MACOS_BUNDLE_ID_FIRST_SEGMENT.test(trimmed)) {
4240
return trimmed;
4341
}
4442

‎packages/platform-apple/src/os/macos/helper.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
runMacOsPressAction,
1313
runMacOsReadTextAction,
1414
runMacOsScreenshotAction,
15+
quitMacOsApp,
1516
runMacOsSnapshotAction,
1617
} from './helper.ts';
1718

@@ -381,3 +382,27 @@ test('the helper vocabulary matches the table the Swift helper is held to', () =
381382
assert.deepEqual([...MACOS_HELPER_REFUSAL_REASONS], fixture.refusalReasons);
382383
assert.deepEqual([...MACOS_DELIVERY_MECHANISMS], fixture.deliveryMechanisms);
383384
});
385+
386+
test('the helper accepts a bundle id with an uppercase first segment and refuses a bare name', async () => {
387+
const receivedArgs: string[][] = [];
388+
const provider = createLocalAppleToolProvider({
389+
macosHelper: {
390+
run: async (args) => {
391+
receivedArgs.push(args);
392+
return helperReturn({
393+
bundleId: 'Foo.Bar',
394+
running: false,
395+
terminated: false,
396+
forceTerminated: false,
397+
});
398+
},
399+
},
400+
});
401+
402+
await withAppleToolProvider(provider, async () => {
403+
await quitMacOsApp('Foo.Bar', captureEnv);
404+
await assert.rejects(quitMacOsApp('TextEdit', captureEnv), { code: 'INVALID_ARGS' });
405+
});
406+
407+
assert.deepEqual(receivedArgs, [['app', 'quit', '--bundle-id', 'Foo.Bar']]);
408+
});

0 commit comments

Comments
 (0)